From f0254aa40c7a37de4bb10f70f005a563bbf40b36 Mon Sep 17 00:00:00 2001 From: Beyley Cardellio Date: Sat, 30 Aug 2025 00:36:20 -0700 Subject: [PATCH] d/steamvr_lh + t/common: Initialize VP2 HID driver when applicable Part-of: --- src/xrt/drivers/steamvr_lh/device.cpp | 100 +++++++++++++++--- src/xrt/drivers/steamvr_lh/device.hpp | 40 +++++-- src/xrt/drivers/steamvr_lh/steamvr_lh.cpp | 7 +- .../drivers/steamvr_lh/steamvr_lh_interface.h | 2 +- .../targets/common/target_builder_steamvr.c | 2 +- 5 files changed, 124 insertions(+), 27 deletions(-) diff --git a/src/xrt/drivers/steamvr_lh/device.cpp b/src/xrt/drivers/steamvr_lh/device.cpp index bcdc12d8b..a232381b9 100644 --- a/src/xrt/drivers/steamvr_lh/device.cpp +++ b/src/xrt/drivers/steamvr_lh/device.cpp @@ -4,9 +4,33 @@ * @file * @brief SteamVR driver device implementation. * @author Shawn Wallace + * @author Beyley Cardellio * @ingroup drv_steamvr_lh */ +#include "math/m_api.h" +#include "math/m_relation_history.h" +#include "math/m_space.h" + +#include "interfaces/context.hpp" + +#include "util/u_debug.h" +#include "util/u_device.h" +#include "util/u_hand_simulation.h" +#include "util/u_hand_tracking.h" +#include "util/u_logging.h" +#include "util/u_json.hpp" + +#include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" +#include "xrt/xrt_prober.h" + +#include "vive/vive_poses.h" + +#include "openvr_driver.h" + +#include "device.hpp" + #include #include #include @@ -14,22 +38,6 @@ #include #include -#include "math/m_api.h" -#include "math/m_relation_history.h" -#include "math/m_space.h" -#include "device.hpp" -#include "interfaces/context.hpp" -#include "util/u_debug.h" -#include "util/u_device.h" -#include "util/u_hand_simulation.h" -#include "util/u_hand_tracking.h" -#include "util/u_logging.h" -#include "util/u_json.hpp" -#include "xrt/xrt_defines.h" -#include "xrt/xrt_device.h" - -#include "vive/vive_poses.h" -#include "openvr_driver.h" #define DEV_ERR(...) U_LOG_IFL_E(ctx->log_level, __VA_ARGS__) #define DEV_WARN(...) U_LOG_IFL_W(ctx->log_level, __VA_ARGS__) @@ -947,12 +955,70 @@ Device::handle_property_write(const vr::PropertyWrite_t &prop) return handle_generic_property_write(prop); } +bool +HmdDevice::init_vive_pro_2(struct xrt_prober *xp) +{ + xrt_result_t xret; + int ret = 0; + + struct xrt_prober_device **devices = nullptr; + size_t device_count; + + xret = xrt_prober_lock_list(xp, &devices, &device_count); + if (xret != XRT_SUCCESS) { + DEV_ERR("Failed to lock prober device list"); + return false; + } + + for (size_t i = 0; i < device_count; i++) { + struct xrt_prober_device *dev = devices[i]; + + if (dev->vendor_id == VP2_VID && dev->product_id == VP2_PID) { + DEV_INFO("Found Vive Pro 2 HID device"); + struct os_hid_device *hid_dev = nullptr; + ret = xrt_prober_open_hid_interface(xp, dev, 0, &hid_dev); + if (ret != 0) { + DEV_ERR("Failed to open Vive Pro 2 HID interface"); + break; + } + + ret = vp2_hid_open(hid_dev, &this->vp2.hid); + if (ret != 0) { + DEV_ERR("Failed to open Vive Pro 2 HID device"); + break; + } + + break; + } + } + + xrt_prober_unlock_list(xp, &devices); + + int width, height; + vp2_resolution_get_extents(vp2_get_resolution(this->vp2.hid), &width, &height); + + for (int i = 0; i < 2; i++) { + this->hmd_parts->base.views[i].display.w_pixels = width / 2; + this->hmd_parts->base.views[i].display.h_pixels = height; + + this->hmd_parts->base.views[i].viewport.w_pixels = width / 2; + this->hmd_parts->base.views[i].viewport.h_pixels = height; + } + + this->hmd_parts->base.views[1].viewport.x_pixels = width / 2; + + this->hmd_parts->base.screens[0].w_pixels = width; + this->hmd_parts->base.screens[0].h_pixels = height; + + return ret == 0; +} + vr::ETrackedPropertyError HmdDevice::handle_property_write(const vr::PropertyWrite_t &prop) { switch (prop.prop) { case vr::Prop_ModelNumber_String: { - auto model_number = std::string(static_cast(prop.pvBuffer), prop.unBufferSize); + std::string model_number(static_cast(prop.pvBuffer), prop.unBufferSize); this->variant = vive_determine_variant(model_number.c_str()); diff --git a/src/xrt/drivers/steamvr_lh/device.hpp b/src/xrt/drivers/steamvr_lh/device.hpp index 812f7daca..626eed7cd 100644 --- a/src/xrt/drivers/steamvr_lh/device.hpp +++ b/src/xrt/drivers/steamvr_lh/device.hpp @@ -7,6 +7,18 @@ * @ingroup drv_steamvr_lh */ +#include "interfaces/context.hpp" + +#include "math/m_relation_history.h" + +#include "xrt/xrt_device.h" + +#include "vive/vive_common.h" + +#include "vp2/vp2_hid.h" + +#include "openvr_driver.h" + #include #include #include @@ -15,12 +27,6 @@ #include #include -#include "interfaces/context.hpp" -#include "math/m_relation_history.h" -#include "xrt/xrt_device.h" -#include "openvr_driver.h" -#include "vive/vive_common.h" - class Context; struct InputClass; @@ -122,9 +128,23 @@ private: init_chaperone(const std::string &steam_install); }; +struct VivePro2Data +{ + vp2_hid *hid{nullptr}; + + ~VivePro2Data() + { + if (hid != nullptr) { + vp2_hid_destroy(hid); + hid = nullptr; + } + } +}; + class HmdDevice : public Device { public: + VIVE_VARIANT variant{VIVE_UNKNOWN}; xrt_pose eye[2]; float ipd{0.063}; // meters struct Parts @@ -139,6 +159,10 @@ public: float max{1.0f}; }; + struct VivePro2Data vp2 + { + }; + HmdDevice(const DeviceBuilder &builder); xrt_result_t @@ -174,6 +198,9 @@ public: xrt_result_t set_brightness(float brightness, bool relative); + bool + init_vive_pro_2(struct xrt_prober *xp); + private: std::unique_ptr hmd_parts{nullptr}; @@ -187,7 +214,6 @@ private: std::mutex hmd_parts_mut; float brightness{1.0f}; AnalogGainRange analog_gain_range{}; - VIVE_VARIANT variant{VIVE_UNKNOWN}; }; class ControllerDevice : public Device diff --git a/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp b/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp index fd5ef2d77..8a4c55ed4 100644 --- a/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp +++ b/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp @@ -237,6 +237,7 @@ Context::setup_hmd(const char *serial, vr::ITrackedDeviceServerDriver *driver) hmd_parts->display = display; hmd->set_hmd_parts(std::move(hmd_parts)); + return true; } @@ -770,7 +771,7 @@ destroy(struct xrt_system_devices *xsysd) } extern "C" enum xrt_result -steamvr_lh_create_devices(struct xrt_system_devices **out_xsysd) +steamvr_lh_create_devices(struct xrt_prober *xp, struct xrt_system_devices **out_xsysd) { u_logging_level level = debug_get_log_option_lh_log(); // The driver likes to create a bunch of transient folders - @@ -861,6 +862,10 @@ steamvr_lh_create_devices(struct xrt_system_devices **out_xsysd) // Include the HMD if (svrs->ctx->hmd) { + if (svrs->ctx->hmd->variant == VIVE_VARIANT_PRO2 && !svrs->ctx->hmd->init_vive_pro_2(xp)) { + U_LOG_IFL_W(level, "Found Vive Pro 2, but failed to initialize."); + } + // Always have a head at index 0 and iterate dev count. xsysd->xdevs[xsysd->xdev_count] = svrs->ctx->hmd; xsysd->static_roles.head = xsysd->xdevs[xsysd->xdev_count++]; diff --git a/src/xrt/drivers/steamvr_lh/steamvr_lh_interface.h b/src/xrt/drivers/steamvr_lh/steamvr_lh_interface.h index 23621fe68..15ed38d82 100644 --- a/src/xrt/drivers/steamvr_lh/steamvr_lh_interface.h +++ b/src/xrt/drivers/steamvr_lh/steamvr_lh_interface.h @@ -34,7 +34,7 @@ extern "C" { * @ingroup drv_steamvr_lh */ enum xrt_result -steamvr_lh_create_devices(struct xrt_system_devices **out_xsysd); +steamvr_lh_create_devices(struct xrt_prober *xp, struct xrt_system_devices **out_xsysd); #ifdef __cplusplus diff --git a/src/xrt/targets/common/target_builder_steamvr.c b/src/xrt/targets/common/target_builder_steamvr.c index a603eeaf5..76acaf16a 100644 --- a/src/xrt/targets/common/target_builder_steamvr.c +++ b/src/xrt/targets/common/target_builder_steamvr.c @@ -140,7 +140,7 @@ steamvr_open_system(struct xrt_builder *xb, assert(out_xsysd != NULL); assert(*out_xsysd == NULL); - enum xrt_result result = steamvr_lh_create_devices(out_xsysd); + enum xrt_result result = steamvr_lh_create_devices(xp, out_xsysd); if (result != XRT_SUCCESS) { SVR_ERROR("Unable to create devices");