The open source OpenXR runtime

d/opengloves: Add force feedback extension support

authored by

Daniel Willmott and committed by
Moses Turner
ddda9ec7 74f14f24

+88 -2
+25 -1
src/xrt/drivers/opengloves/encoding/alpha_encoding.cpp
··· 140 140 {"", OPENGLOVES_ALPHA_ENCODING_MAX} // Junk key 141 141 }; 142 142 143 + static const std::map<int, std::string> opengloves_alpha_encoding_output_key_string{ 144 + {OPENGLOVES_ALPHA_ENCODING_FinThumb, "A"}, // thumb force feedback 145 + {OPENGLOVES_ALPHA_ENCODING_FinIndex, "B"}, // index force feedback 146 + {OPENGLOVES_ALPHA_ENCODING_FinMiddle, "C"}, // middle force feedback 147 + {OPENGLOVES_ALPHA_ENCODING_FinRing, "D"}, // ring force feedback 148 + {OPENGLOVES_ALPHA_ENCODING_FinPinky, "E"}, // pinky force feedback 149 + }; 150 + 143 151 static std::map<int, std::string> 144 152 opengloves_alpha_encoding_parse_to_map(const std::string &str) 145 153 { ··· 247 255 out->gestures.grab.activated = input_map.find(OPENGLOVES_ALPHA_ENCODING_GesGrab) != input_map.end(); 248 256 out->gestures.pinch.activated = input_map.find(OPENGLOVES_ALPHA_ENCODING_GesPinch) != input_map.end(); 249 257 out->buttons.menu.pressed = input_map.find(OPENGLOVES_ALPHA_ENCODING_BtnMenu) != input_map.end(); 250 - } 258 + } 259 + 260 + void 261 + opengloves_alpha_encoding_encode(const struct opengloves_output *output, char *out_buff) 262 + { 263 + sprintf(out_buff, "%s%d%s%d%s%d%s%d%s%d\n", 264 + opengloves_alpha_encoding_output_key_string.at(OPENGLOVES_ALPHA_ENCODING_FinThumb).c_str(), 265 + (int)(output->force_feedback.thumb * 1000), 266 + opengloves_alpha_encoding_output_key_string.at(OPENGLOVES_ALPHA_ENCODING_FinIndex).c_str(), 267 + (int)(output->force_feedback.index * 1000), 268 + opengloves_alpha_encoding_output_key_string.at(OPENGLOVES_ALPHA_ENCODING_FinMiddle).c_str(), 269 + (int)(output->force_feedback.middle * 1000), 270 + opengloves_alpha_encoding_output_key_string.at(OPENGLOVES_ALPHA_ENCODING_FinRing).c_str(), 271 + (int)(output->force_feedback.ring * 1000), 272 + opengloves_alpha_encoding_output_key_string.at(OPENGLOVES_ALPHA_ENCODING_FinPinky).c_str(), 273 + (int)(output->force_feedback.little * 1000)); 274 + }
+2
src/xrt/drivers/opengloves/encoding/alpha_encoding.h
··· 17 17 void 18 18 opengloves_alpha_encoding_decode(const char *data, struct opengloves_input *out_kv); 19 19 20 + void 21 + opengloves_alpha_encoding_encode(const struct opengloves_output *output, char *out_buff); 20 22 #ifdef __cplusplus 21 23 } 22 24 #endif
+14
src/xrt/drivers/opengloves/encoding/encoding.h
··· 66 66 struct opengloves_input_gestures gestures; 67 67 }; 68 68 69 + struct opengloves_output_force_feedback 70 + { 71 + float thumb; 72 + float index; 73 + float middle; 74 + float ring; 75 + float little; 76 + }; 77 + 78 + struct opengloves_output 79 + { 80 + struct opengloves_output_force_feedback force_feedback; 81 + }; 82 + 69 83 #ifdef __cplusplus 70 84 } 71 85 #endif
+47 -1
src/xrt/drivers/opengloves/opengloves_device.c
··· 160 160 } 161 161 162 162 static void 163 + opengloves_ffb_location_convert(const struct xrt_output_force_feedback *xrt_ffb, 164 + struct opengloves_output_force_feedback *out_ffb) 165 + { 166 + switch (xrt_ffb->location) { 167 + case XRT_FORCE_FEEDBACK_LOCATION_LEFT_THUMB: out_ffb->thumb = xrt_ffb->value; break; 168 + case XRT_FORCE_FEEDBACK_LOCATION_LEFT_INDEX: out_ffb->index = xrt_ffb->value; break; 169 + case XRT_FORCE_FEEDBACK_LOCATION_LEFT_MIDDLE: out_ffb->middle = xrt_ffb->value; break; 170 + case XRT_FORCE_FEEDBACK_LOCATION_LEFT_RING: out_ffb->ring = xrt_ffb->value; break; 171 + case XRT_FORCE_FEEDBACK_LOCATION_LEFT_PINKY: out_ffb->little = xrt_ffb->value; break; 172 + } 173 + } 174 + 175 + static void 176 + opengloves_device_set_output(struct xrt_device *xdev, enum xrt_output_name name, const union xrt_output_value *value) 177 + { 178 + struct opengloves_device *od = opengloves_device(xdev); 179 + 180 + switch (name) { 181 + case XRT_OUTPUT_NAME_FORCE_FEEDBACK_LEFT: 182 + case XRT_OUTPUT_NAME_FORCE_FEEDBACK_RIGHT: { 183 + struct opengloves_output out; 184 + 185 + int location_count = value->force_feedback.force_feedback_location_count; 186 + const struct xrt_output_force_feedback *ffb = value->force_feedback.force_feedback; 187 + 188 + for (int i = 0; i < location_count; i++) { 189 + opengloves_ffb_location_convert(ffb + i, &out.force_feedback); 190 + } 191 + 192 + char buff[64]; 193 + opengloves_alpha_encoding_encode(&out, buff); 194 + 195 + opengloves_communication_device_write(od->ocd, buff, strlen(buff)); 196 + } 197 + default: break; 198 + } 199 + } 200 + 201 + static void 163 202 opengloves_device_destroy(struct xrt_device *xdev) 164 203 { 165 204 struct opengloves_device *od = opengloves_device(xdev); ··· 235 274 opengloves_device_create(struct opengloves_communication_device *ocd, enum xrt_hand hand) 236 275 { 237 276 enum u_device_alloc_flags flags = (enum u_device_alloc_flags)(U_DEVICE_ALLOC_TRACKING_NONE); 238 - struct opengloves_device *od = U_DEVICE_ALLOCATE(struct opengloves_device, flags, 8, 0); 277 + 278 + struct opengloves_device *od = U_DEVICE_ALLOCATE(struct opengloves_device, flags, 8, 1); 239 279 240 280 od->base.name = XRT_DEVICE_HAND_TRACKER; 241 281 od->base.device_type = XRT_DEVICE_TYPE_HAND_TRACKER; ··· 251 291 od->hand == XRT_HAND_LEFT ? XRT_INPUT_GENERIC_HAND_TRACKING_LEFT : XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT; 252 292 253 293 od->base.hand_tracking_supported = true; 294 + od->base.force_feedback_supported = true; 254 295 255 296 // inputs 256 297 od->base.update_inputs = opengloves_device_update_inputs; ··· 265 306 266 307 od->base.inputs[OPENGLOVES_INDEX_JOYSTICK_MAIN].name = XRT_INPUT_INDEX_THUMBSTICK; 267 308 od->base.inputs[OPENGLOVES_INDEX_JOYSTICK_MAIN_CLICK].name = XRT_INPUT_INDEX_THUMBSTICK_CLICK; 309 + 310 + // outputs 311 + od->base.outputs[0].name = 312 + od->hand == XRT_HAND_LEFT ? XRT_OUTPUT_NAME_FORCE_FEEDBACK_LEFT : XRT_OUTPUT_NAME_FORCE_FEEDBACK_RIGHT; 313 + od->base.set_output = opengloves_device_set_output; 268 314 269 315 // startup thread 270 316 int ret = os_thread_helper_init(&od->oth);