The open source OpenXR runtime

a/util: Add logic to allow filling gamepad roles

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2486>

authored by

Beyley Cardellio and committed by
Marge Bot
40fcca9b fdfcc2e4

+61 -22
+17 -1
src/xrt/auxiliary/util/u_builders.c
··· 106 106 u_builder_setup_tracking_origins(struct xrt_device *head, 107 107 struct xrt_device *left, 108 108 struct xrt_device *right, 109 + struct xrt_device *gamepad, 109 110 struct xrt_vec3 *global_tracking_origin_offset) 110 111 { 111 112 struct xrt_tracking_origin *head_origin = head ? head->tracking_origin : NULL; 112 113 struct xrt_tracking_origin *left_origin = left ? left->tracking_origin : NULL; 113 114 struct xrt_tracking_origin *right_origin = right ? right->tracking_origin : NULL; 115 + struct xrt_tracking_origin *gamepad_origin = gamepad ? gamepad->tracking_origin : NULL; 114 116 115 117 if (left_origin != NULL && left_origin->type == XRT_TRACKING_TYPE_NONE) { 116 118 left_origin->initial_offset.position.x = -0.2f; ··· 124 126 right_origin->initial_offset.position.z = -0.5f; 125 127 } 126 128 129 + if (gamepad_origin != NULL && gamepad_origin->type == XRT_TRACKING_TYPE_NONE) { 130 + gamepad_origin->initial_offset.position.x = 0.0f; 131 + gamepad_origin->initial_offset.position.y = 1.3f; 132 + gamepad_origin->initial_offset.position.z = -0.5f; 133 + } 134 + 127 135 // Head comes last, because left and right may share tracking origin. 128 136 if (head_origin != NULL && head_origin->type == XRT_TRACKING_TYPE_NONE) { 129 137 // "nominal height" 1.6m ··· 141 149 if (right_origin && right_origin != head_origin && right_origin != left_origin) { 142 150 apply_offset(&right->tracking_origin->initial_offset.position, global_tracking_origin_offset); 143 151 } 152 + if (gamepad_origin && gamepad_origin != head_origin && gamepad_origin != right_origin && 153 + gamepad_origin != left_origin) { 154 + apply_offset(&right->tracking_origin->initial_offset.position, global_tracking_origin_offset); 155 + } 144 156 } 145 157 146 158 void ··· 148 160 struct xrt_device *head, 149 161 struct xrt_device *left, 150 162 struct xrt_device *right, 163 + struct xrt_device *gamepad, 151 164 struct xrt_device **xdevs, 152 165 uint32_t xdev_count, 153 166 bool root_is_unbounded, ··· 168 181 head, // 169 182 left, // 170 183 right, // 184 + gamepad, // 171 185 &global_tracking_origin_offset); // 172 186 173 187 ··· 235 249 u_system_devices_static_finalize( // 236 250 usysds, // usysds 237 251 ubrh.left, // left 238 - ubrh.right); // right 252 + ubrh.right, // right 253 + ubrh.gamepad); // gamepad 239 254 240 255 241 256 /* ··· 248 263 ubrh.head, // head 249 264 ubrh.left, // left 250 265 ubrh.right, // right 266 + ubrh.gamepad, // gamepad 251 267 xsysd->xdevs, // xdevs 252 268 xsysd->xdev_count, // xdev_count 253 269 false, // root_is_unbounded
+3
src/xrt/auxiliary/util/u_builders.h
··· 85 85 struct xrt_device *head; 86 86 struct xrt_device *left; 87 87 struct xrt_device *right; 88 + struct xrt_device *gamepad; 88 89 89 90 struct 90 91 { ··· 156 157 u_builder_setup_tracking_origins(struct xrt_device *head, 157 158 struct xrt_device *left, 158 159 struct xrt_device *right, 160 + struct xrt_device *gamepad, 159 161 struct xrt_vec3 *global_tracking_origin_offset); 160 162 161 163 /*! ··· 171 173 struct xrt_device *head, 172 174 struct xrt_device *left, 173 175 struct xrt_device *right, 176 + struct xrt_device *gamepad, 174 177 struct xrt_device **xdevs, 175 178 uint32_t xdev_count, 176 179 bool root_is_unbounded,
+7 -1
src/xrt/auxiliary/util/u_device.c
··· 360 360 } 361 361 362 362 void 363 - u_device_assign_xdev_roles(struct xrt_device **xdevs, size_t xdev_count, int *head, int *left, int *right) 363 + u_device_assign_xdev_roles(struct xrt_device **xdevs, size_t xdev_count, int *head, int *left, int *right, int *gamepad) 364 364 { 365 365 *head = XRT_DEVICE_ROLE_UNASSIGNED; 366 366 *left = XRT_DEVICE_ROLE_UNASSIGNED; 367 367 *right = XRT_DEVICE_ROLE_UNASSIGNED; 368 + *gamepad = XRT_DEVICE_ROLE_UNASSIGNED; 368 369 assert(xdev_count < INT_MAX); 369 370 370 371 for (size_t i = 0; i < xdev_count; i++) { ··· 388 389 try_move_assignment(xdevs, right, left); 389 390 if (*right == XRT_DEVICE_ROLE_UNASSIGNED) { 390 391 *right = (int)i; 392 + } 393 + break; 394 + case XRT_DEVICE_TYPE_GAMEPAD: 395 + if (*gamepad == XRT_DEVICE_ROLE_UNASSIGNED) { 396 + *gamepad = (int)i; 391 397 } 392 398 break; 393 399 case XRT_DEVICE_TYPE_ANY_HAND_CONTROLLER:
+3 -2
src/xrt/auxiliary/util/u_device.h
··· 131 131 #define XRT_DEVICE_ROLE_UNASSIGNED (-1) 132 132 133 133 /*! 134 - * Helper function to assign head, left hand and right hand roles. 134 + * Helper function to assign head, left hand, right hand, and gamepad roles. 135 135 * 136 136 * @ingroup aux_util 137 137 */ 138 138 void 139 - u_device_assign_xdev_roles(struct xrt_device **xdevs, size_t xdev_count, int *head, int *left, int *right); 139 + u_device_assign_xdev_roles( 140 + struct xrt_device **xdevs, size_t xdev_count, int *head, int *left, int *right, int *gamepad); 140 141 141 142 /*! 142 143 * Helper function for `get_view_pose` in an HMD driver.
+10 -3
src/xrt/auxiliary/util/u_system_helpers.c
··· 192 192 void 193 193 u_system_devices_static_finalize(struct u_system_devices_static *usysds, 194 194 struct xrt_device *left, 195 - struct xrt_device *right) 195 + struct xrt_device *right, 196 + struct xrt_device *gamepad) 196 197 { 197 198 struct xrt_system_devices *xsysd = &usysds->base.base; 198 199 int32_t left_index = get_index_for_device(xsysd, left); 199 200 int32_t right_index = get_index_for_device(xsysd, right); 201 + int32_t gamepad_index = get_index_for_device(xsysd, gamepad); 200 202 201 203 U_LOG_D( 202 204 "Devices:" 203 205 "\n\t%i: %p" 206 + "\n\t%i: %p" 204 207 "\n\t%i: %p", 205 - left_index, (void *)left, // 206 - right_index, (void *)right); // 208 + left_index, (void *)left, // 209 + right_index, (void *)right, // 210 + gamepad_index, (void *)gamepad); // 207 211 208 212 // Consistency checking. 209 213 assert(usysds->cached.generation_id == 0); ··· 211 215 assert(left_index >= 0 || left == NULL); 212 216 assert(right_index < 0 || right != NULL); 213 217 assert(right_index >= 0 || right == NULL); 218 + assert(gamepad_index < 0 || gamepad != NULL); 219 + assert(gamepad_index >= 0 || gamepad == NULL); 214 220 215 221 // Completely clear the struct. 216 222 usysds->cached = (struct xrt_system_roles)XRT_SYSTEM_ROLES_INIT; 217 223 usysds->cached.generation_id = 1; 218 224 usysds->cached.left = left_index; 219 225 usysds->cached.right = right_index; 226 + usysds->cached.gamepad = gamepad_index; 220 227 } 221 228 222 229
+2 -1
src/xrt/auxiliary/util/u_system_helpers.h
··· 148 148 void 149 149 u_system_devices_static_finalize(struct u_system_devices_static *usysds, 150 150 struct xrt_device *left, 151 - struct xrt_device *right); 151 + struct xrt_device *right, 152 + struct xrt_device *gamepad); 152 153 153 154 154 155 /*
+6 -4
src/xrt/drivers/qwerty/qwerty_sdl.c
··· 50 50 int head; 51 51 int left; 52 52 int right; 53 - head = left = right = XRT_DEVICE_ROLE_UNASSIGNED; 54 - u_device_assign_xdev_roles(xdevs, xdev_count, &head, &left, &right); 53 + int gamepad; 54 + head = left = right = gamepad = XRT_DEVICE_ROLE_UNASSIGNED; 55 + u_device_assign_xdev_roles(xdevs, xdev_count, &head, &left, &right, &gamepad); 55 56 56 57 struct xrt_device *xd_hmd = qsys->hmd ? &qsys->hmd->base.base : NULL; 57 58 struct xrt_device *xd_left = &qsys->lctrl->base.base; ··· 78 79 int head; 79 80 int left; 80 81 int right; 81 - head = left = right = XRT_DEVICE_ROLE_UNASSIGNED; 82 - u_device_assign_xdev_roles(xdevs, xdev_count, &head, &left, &right); 82 + int gamepad; 83 + head = left = right = gamepad = XRT_DEVICE_ROLE_UNASSIGNED; 84 + u_device_assign_xdev_roles(xdevs, xdev_count, &head, &left, &right, &gamepad); 83 85 84 86 struct xrt_device *xd_left = &qsys->lctrl->base.base; 85 87 struct xrt_device *xd_right = &qsys->rctrl->base.base;
+1 -5
src/xrt/drivers/steamvr_lh/steamvr_lh.cpp
··· 709 709 bool update_gen = false; 710 710 int head, left, right, gamepad; 711 711 712 - if (out_roles->generation_id == 0) { 713 - gamepad = XRT_DEVICE_ROLE_UNASSIGNED; // No gamepads in steamvr_lh set this unassigned first run 714 - } 715 - 716 - u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head, &left, &right); 712 + u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head, &left, &right, &gamepad); 717 713 718 714 if (left != out_roles->left || right != out_roles->right || gamepad != out_roles->gamepad) { 719 715 update_gen = true;
+1
src/xrt/include/xrt/xrt_defines.h
··· 791 791 XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER, 792 792 XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER, 793 793 XRT_DEVICE_TYPE_ANY_HAND_CONTROLLER, 794 + XRT_DEVICE_TYPE_GAMEPAD, 794 795 XRT_DEVICE_TYPE_GENERIC_TRACKER, 795 796 XRT_DEVICE_TYPE_HAND_TRACKER, 796 797 XRT_DEVICE_TYPE_EYE_TRACKER,
+1 -1
src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp
··· 1522 1522 1523 1523 // use steamvr room setup instead 1524 1524 struct xrt_vec3 offset = {0, 0, 0}; 1525 - u_builder_setup_tracking_origins(m_xhmd, left_xdev, right_xdev, &offset); 1525 + u_builder_setup_tracking_origins(m_xhmd, left_xdev, right_xdev, NULL, &offset); 1526 1526 1527 1527 if (left_xdev) { 1528 1528 m_left = new CDeviceDriver_Monado_Controller(m_xinst, left_xdev, XRT_HAND_LEFT);
+7 -3
src/xrt/targets/common/target_builder_legacy.c
··· 151 151 * Setup the roles. 152 152 */ 153 153 154 - int head_idx, left_idx, right_idx; 155 - u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head_idx, &left_idx, &right_idx); 154 + int head_idx, left_idx, right_idx, gamepad_idx; 155 + u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head_idx, &left_idx, &right_idx, &gamepad_idx); 156 156 157 157 struct xrt_device *head = NULL; 158 - struct xrt_device *left = NULL, *right = NULL; 158 + struct xrt_device *left = NULL, *right = NULL, *gamepad = NULL; 159 159 struct xrt_device *left_ht = NULL, *right_ht = NULL; 160 160 161 161 if (head_idx >= 0) { ··· 167 167 if (right_idx >= 0) { 168 168 right = xsysd->xdevs[right_idx]; 169 169 } 170 + if (gamepad_idx >= 0) { 171 + gamepad = xsysd->xdevs[gamepad_idx]; 172 + } 170 173 171 174 // Find hand tracking devices. 172 175 left_ht = u_system_devices_get_ht_device_left(xsysd); ··· 176 179 ubrh->head = head; 177 180 ubrh->left = left; 178 181 ubrh->right = right; 182 + ubrh->gamepad = gamepad; 179 183 ubrh->hand_tracking.left = left_ht; 180 184 ubrh->hand_tracking.right = right_ht; 181 185
+2 -1
src/xrt/targets/common/target_builder_lighthouse.c
··· 622 622 int head_idx = -1; 623 623 int left_idx = -1; 624 624 int right_idx = -1; 625 + int gamepad_idx = -1; 625 626 626 - u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head_idx, &left_idx, &right_idx); 627 + u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head_idx, &left_idx, &right_idx, &gamepad_idx); 627 628 628 629 if (head_idx < 0) { 629 630 LH_ERROR("Unable to find HMD");
+1
src/xrt/targets/sdl_test/sdl_instance.c
··· 151 151 head, // head 152 152 NULL, // left 153 153 NULL, // right 154 + NULL, // gamepad 154 155 sp->xsysd_base.xdevs, // xdevs 155 156 sp->xsysd_base.xdev_count, // xdev_count 156 157 false, // root_is_unbounded