The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

d/steamvr_lh: refactor driver

Co-authored-by: galister <galister@librevr.org>
Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2297>

authored by

Bones
galister
and committed by
Marge Bot
235cc78e 30d8e29e

+117 -97
+33 -78
src/xrt/drivers/steamvr_lh/steamvr_lh.cpp
··· 24 24 #include "device.hpp" 25 25 #include "util/u_device.h" 26 26 #include "util/u_misc.h" 27 - #include "util/u_space_overseer.h" 28 - #include "util/u_builders.h" 29 27 #include "util/u_device.h" 30 28 #include "util/u_system_helpers.h" 31 29 #include "vive/vive_bindings.h" 30 + #include "util/u_device.h" 32 31 33 32 #include "math/m_api.h" 34 33 ··· 36 35 37 36 DEBUG_GET_ONCE_LOG_OPTION(lh_log, "LIGHTHOUSE_LOG", U_LOGGING_INFO) 38 37 DEBUG_GET_ONCE_BOOL_OPTION(lh_load_slimevr, "LH_LOAD_SLIMEVR", false) 38 + DEBUG_GET_ONCE_NUM_OPTION(lh_discover_wait_ms, "LH_DISCOVER_WAIT_MS", 3000) 39 39 40 - static const size_t MAX_CONTROLLERS = 16; 40 + static constexpr size_t MAX_CONTROLLERS = 16; 41 41 42 42 43 43 struct steamvr_lh_system ··· 45 45 // System devices wrapper. 46 46 struct xrt_system_devices base; 47 47 48 - //! Origin for all devices. 49 - struct xrt_tracking_origin origin; 50 - 51 48 //! Pointer to driver context 52 49 std::shared_ptr<Context> ctx; 53 - 54 - //! Index to the left controller. 55 - int32_t left_index; 56 - 57 - //! Index to the right controller. 58 - int32_t right_index; 59 - 60 - //! Index to the gamepad controller. 61 - int32_t gamepad_index; 62 - 63 - //! Index to the hmd. 64 - int32_t head_index; 65 50 }; 66 51 67 52 struct steamvr_lh_system *svrs = U_TYPED_CALLOC(struct steamvr_lh_system); ··· 303 288 CTX_INFO("New device added: %s", pchDeviceSerialNumber); 304 289 switch (eDeviceClass) { 305 290 case vr::TrackedDeviceClass_HMD: { 291 + CTX_INFO("Found lighthouse HMD: %s", pchDeviceSerialNumber); 306 292 return setup_hmd(pchDeviceSerialNumber, pDriver); 307 - break; 308 293 } 309 294 case vr::TrackedDeviceClass_Controller: { 295 + CTX_INFO("Found lighthouse controller: %s", pchDeviceSerialNumber); 310 296 return setup_controller(pchDeviceSerialNumber, pDriver); 311 - break; 312 297 } 313 298 case vr::TrackedDeviceClass_TrackingReference: { 314 - CTX_INFO("Found lighthouse device: %s", pchDeviceSerialNumber); 299 + CTX_INFO("Found lighthouse base station: %s", pchDeviceSerialNumber); 315 300 return false; 316 301 } 317 302 case vr::TrackedDeviceClass_GenericTracker: { 318 - CTX_INFO("Found generic tracker device: %s", pchDeviceSerialNumber); 303 + CTX_INFO("Found lighthouse tracker: %s", pchDeviceSerialNumber); 319 304 return setup_controller(pchDeviceSerialNumber, pDriver); 320 - break; 321 305 } 322 306 default: { 323 307 CTX_WARN("Attempted to add unsupported device class: %u", eDeviceClass); ··· 687 671 } 688 672 default: { 689 673 // If the handle corresponds to a controller 690 - if (handle >= 2 && handle <= 17) { 674 + if (handle >= 2 && handle <= MAX_CONTROLLERS + 1) { 691 675 return controller[handle - 2]; 692 676 } else { 693 677 return nullptr; ··· 721 705 xrt_result_t 722 706 get_roles(struct xrt_system_devices *xsysd, struct xrt_system_roles *out_roles) 723 707 { 724 - out_roles->left = svrs->left_index; 725 - out_roles->right = svrs->right_index; 726 - out_roles->gamepad = svrs->gamepad_index; 727 - out_roles->generation_id = 1; 708 + bool update_gen = false; 709 + int head, left, right, gamepad; 710 + 711 + if (out_roles->generation_id == 0) { 712 + gamepad = XRT_DEVICE_ROLE_UNASSIGNED; // No gamepads in steamvr_lh set this unassigned first run 713 + } 714 + 715 + u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &head, &left, &right); 716 + 717 + if (left != out_roles->left || right != out_roles->right || gamepad != out_roles->gamepad) { 718 + update_gen = true; 719 + } 720 + 721 + if (update_gen) { 722 + out_roles->generation_id++; 723 + 724 + out_roles->left = left; 725 + out_roles->right = right; 726 + out_roles->gamepad = gamepad; 727 + } 728 728 729 729 return XRT_SUCCESS; 730 730 } ··· 738 738 } 739 739 740 740 extern "C" enum xrt_result 741 - steamvr_lh_create_devices(struct xrt_session_event_sink *broadcast, 742 - struct xrt_system_devices **out_xsysd, 743 - struct xrt_space_overseer **out_xso) 741 + steamvr_lh_create_devices(struct xrt_system_devices **out_xsysd) 744 742 { 745 743 u_logging_level level = debug_get_log_option_lh_log(); 746 744 // The driver likes to create a bunch of transient folders - ··· 807 805 U_LOG_IFL_I(level, "Lighthouse initialization complete, giving time to setup connected devices..."); 808 806 // RunFrame needs to be called to detect controllers 809 807 using namespace std::chrono_literals; 810 - auto start_time = std::chrono::steady_clock::now(); 808 + auto end_time = std::chrono::steady_clock::now() + 1ms * debug_get_num_option_lh_discover_wait_ms(); 811 809 while (true) { 812 810 svrs->ctx->run_frame(); 813 811 auto cur_time = std::chrono::steady_clock::now(); 814 - if (cur_time - start_time >= 3s) { 812 + if (cur_time > end_time) { 815 813 break; 816 814 } 817 815 std::this_thread::sleep_for(20ms); ··· 829 827 xsysd->destroy = destroy; 830 828 xsysd->get_roles = get_roles; 831 829 832 - // Do creation. 833 - // Devices to populate. 834 - struct xrt_device *head = NULL; 835 - struct xrt_device *left = NULL, *right = NULL; 836 - struct xrt_device *left_ht = NULL, *right_ht = NULL; 837 - 838 - svrs->head_index = -1; 839 - svrs->left_index = -1; 840 - svrs->right_index = -1; 841 - svrs->gamepad_index = -1; 842 - 843 - 844 830 // Include the HMD 845 831 if (svrs->ctx->hmd) { 832 + // Always have a head at index 0 and iterate dev count. 846 833 xsysd->xdevs[xsysd->xdev_count] = svrs->ctx->hmd; 847 - head = xsysd->xdevs[xsysd->xdev_count++]; // Always have a head at index 0 and iterate dev count. 848 - xsysd->static_roles.head = head; 834 + xsysd->static_roles.head = xsysd->xdevs[xsysd->xdev_count++]; 849 835 } 850 836 851 837 // Include the controllers ··· 855 841 } 856 842 } 857 843 858 - u_device_assign_xdev_roles(xsysd->xdevs, xsysd->xdev_count, &svrs->head_index, &svrs->left_index, 859 - &svrs->right_index); 860 - 861 - if (svrs->left_index >= 0) { 862 - left = xsysd->xdevs[svrs->left_index]; 863 - left_ht = u_system_devices_get_ht_device_left(xsysd); 864 - xsysd->static_roles.hand_tracking.left = left_ht; 865 - } 866 - 867 - if (svrs->right_index >= 0) { 868 - right = xsysd->xdevs[svrs->right_index]; 869 - right_ht = u_system_devices_get_ht_device_right(xsysd); 870 - xsysd->static_roles.hand_tracking.right = right_ht; 871 - } 872 - 873 - if (!head) { 874 - U_LOG_IFL_E(level, "Unable to find HMD"); 875 - destroy(xsysd); 876 - return xrt_result::XRT_ERROR_DEVICE_CREATION_FAILED; 877 - } 878 - 879 844 *out_xsysd = xsysd; 880 - 881 - u_builder_create_space_overseer_legacy( // 882 - broadcast, // broadcast 883 - head, // head 884 - left, // left 885 - right, // right 886 - xsysd->xdevs, // xdevs 887 - xsysd->xdev_count, // xdev_count 888 - false, // root_is_unbounded 889 - out_xso); // out_xso 890 845 891 846 return xrt_result::XRT_SUCCESS; 892 847 }
+1 -3
src/xrt/drivers/steamvr_lh/steamvr_lh_interface.h
··· 34 34 * @ingroup drv_steamvr_lh 35 35 */ 36 36 enum xrt_result 37 - steamvr_lh_create_devices(struct xrt_session_event_sink *broadcast, 38 - struct xrt_system_devices **out_xsysd, 39 - struct xrt_space_overseer **out_xso); 37 + steamvr_lh_create_devices(struct xrt_system_devices **out_xsysd); 40 38 41 39 42 40 #ifdef __cplusplus
+83 -16
src/xrt/targets/common/target_builder_steamvr.c
··· 8 8 * @author Jakob Bornecrantz <jakob@collabora.com> 9 9 * @ingroup xrt_iface 10 10 */ 11 + #include "util/u_builders.h" 12 + #include "xrt/xrt_config_drivers.h" 11 13 12 - #include "tracking/t_hand_tracking.h" 14 + 15 + #include <assert.h> 16 + #include <stdbool.h> 17 + 13 18 #include "tracking/t_tracking.h" 14 19 15 20 #include "xrt/xrt_config_drivers.h" 16 21 #include "xrt/xrt_device.h" 17 22 #include "xrt/xrt_prober.h" 18 23 19 - #include "util/u_builders.h" 20 - #include "util/u_config_json.h" 21 24 #include "util/u_debug.h" 22 - #include "util/u_device.h" 23 - #include "util/u_sink.h" 24 25 #include "util/u_system_helpers.h" 25 26 26 27 #include "vive/vive_builder.h" ··· 28 29 #include "target_builder_interface.h" 29 30 30 31 #include "steamvr_lh/steamvr_lh_interface.h" 32 + #include "xrt/xrt_results.h" 33 + 34 + #include "xrt/xrt_space.h" 35 + #include "util/u_space_overseer.h" 31 36 32 37 #ifndef XRT_BUILD_DRIVER_STEAMVR_LIGHTHOUSE 33 38 #error "This builder requires the SteamVR Lighthouse driver" 34 39 #endif 35 40 41 + DEBUG_GET_ONCE_LOG_OPTION(svr_log, "STEAMVR_LH_LOG", U_LOGGING_INFO) 42 + 43 + #define SVR_TRACE(...) U_LOG_IFL_T(debug_get_log_option_svr_log(), __VA_ARGS__) 44 + #define SVR_DEBUG(...) U_LOG_IFL_D(debug_get_log_option_svr_log(), __VA_ARGS__) 45 + #define SVR_INFO(...) U_LOG_IFL_I(debug_get_log_option_svr_log(), __VA_ARGS__) 46 + #define SVR_WARN(...) U_LOG_IFL_W(debug_get_log_option_svr_log(), __VA_ARGS__) 47 + #define SVR_ERROR(...) U_LOG_IFL_E(debug_get_log_option_svr_log(), __VA_ARGS__) 48 + #define SVR_ASSERT(predicate, ...) \ 49 + do { \ 50 + bool p = predicate; \ 51 + if (!p) { \ 52 + U_LOG(U_LOGGING_ERROR, __VA_ARGS__); \ 53 + assert(false && "SVR_ASSERT failed: " #predicate); \ 54 + exit(EXIT_FAILURE); \ 55 + } \ 56 + } while (false); 57 + #define SVR_ASSERT_(predicate) SVR_ASSERT(predicate, "Assertion failed " #predicate) 58 + 36 59 37 60 /* 38 61 * ··· 50 73 { 51 74 struct xrt_builder base; 52 75 53 - /*! 54 - * Is our HMD a Valve Index? 55 - */ 76 + struct xrt_device *head; 77 + struct xrt_device *left_ht, *right_ht; 78 + 56 79 bool is_valve_index; 57 80 }; 58 - 59 81 60 82 /* 61 83 * ··· 86 108 } 87 109 } 88 110 111 + static void 112 + steamvr_destroy(struct xrt_builder *xb) 113 + { 114 + struct steamvr_builder *svrb = (struct steamvr_builder *)xb; 115 + free(svrb); 116 + } 117 + 89 118 static xrt_result_t 90 119 steamvr_open_system(struct xrt_builder *xb, 91 120 cJSON *config, ··· 94 123 struct xrt_system_devices **out_xsysd, 95 124 struct xrt_space_overseer **out_xso) 96 125 { 126 + struct steamvr_builder *svrb = (struct steamvr_builder *)xb; 127 + 97 128 assert(out_xsysd != NULL); 98 129 assert(*out_xsysd == NULL); 99 130 100 - return steamvr_lh_create_devices(broadcast, out_xsysd, out_xso); 101 - } 131 + enum xrt_result result = steamvr_lh_create_devices(out_xsysd); 132 + 133 + if (result != XRT_SUCCESS) { 134 + SVR_ERROR("Unable to create devices"); 135 + steamvr_destroy(xb); 136 + return result; 137 + } 138 + 139 + struct xrt_system_devices *xsysd = NULL; 140 + xsysd = *out_xsysd; 141 + 142 + if (xsysd->static_roles.head == NULL) { 143 + SVR_ERROR("Unable to find HMD"); 144 + steamvr_destroy(xb); 145 + return XRT_ERROR_DEVICE_CREATION_FAILED; 146 + } 147 + 148 + svrb->head = xsysd->static_roles.head; 149 + 150 + svrb->left_ht = u_system_devices_get_ht_device_left(xsysd); 151 + xsysd->static_roles.hand_tracking.left = svrb->left_ht; 152 + 153 + svrb->right_ht = u_system_devices_get_ht_device_right(xsysd); 154 + xsysd->static_roles.hand_tracking.right = svrb->right_ht; 155 + 156 + /* 157 + * Space overseer. 158 + */ 102 159 103 - static void 104 - steamvr_destroy(struct xrt_builder *xb) 105 - { 106 - struct steamvr_builder *svrb = (struct steamvr_builder *)xb; 107 - free(svrb); 160 + struct u_space_overseer *uso = u_space_overseer_create(broadcast); 161 + 162 + struct xrt_pose T_stage_local = XRT_POSE_IDENTITY; 163 + 164 + u_space_overseer_legacy_setup( // 165 + uso, // uso 166 + xsysd->xdevs, // xdevs 167 + xsysd->xdev_count, // xdev_count 168 + svrb->head, // head 169 + &T_stage_local, // local_offset 170 + false); // root_is_unbounded 171 + 172 + *out_xso = (struct xrt_space_overseer *)uso; 173 + 174 + return result; 108 175 } 109 176 110 177