From 2ed83e87dbd2c114305f5f105f175cf3e0c31a4c Mon Sep 17 00:00:00 2001 From: Coreforge Date: Fri, 25 Apr 2025 23:08:18 +0200 Subject: [PATCH] steamvr_lh: properly handle other properties Part-of: --- src/xrt/drivers/steamvr_lh/device.cpp | 90 ++++++++++++++++++++--- src/xrt/drivers/steamvr_lh/device.hpp | 26 ++++++- src/xrt/drivers/steamvr_lh/steamvr_lh.cpp | 10 ++- 3 files changed, 108 insertions(+), 18 deletions(-) diff --git a/src/xrt/drivers/steamvr_lh/device.cpp b/src/xrt/drivers/steamvr_lh/device.cpp index 857148eb9..04ad60cb0 100644 --- a/src/xrt/drivers/steamvr_lh/device.cpp +++ b/src/xrt/drivers/steamvr_lh/device.cpp @@ -208,6 +208,13 @@ brightness_to_analog_gain(float brightness) } } // namespace +Property::Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize) +{ + this->tag = tag; + this->buffer.resize(bufferSize); + std::memcpy(this->buffer.data(), buffer, bufferSize); +} + HmdDevice::HmdDevice(const DeviceBuilder &builder) : Device(builder) { this->name = XRT_DEVICE_GENERIC_HMD; @@ -789,12 +796,28 @@ Device::update_pose(const vr::DriverPose_t &newPose) const m_relation_history_push(relation_hist, &relation, ts); } -void +vr::ETrackedPropertyError Device::handle_properties(const vr::PropertyWrite_t *batch, uint32_t count) { for (uint32_t i = 0; i < count; ++i) { - handle_property_write(batch[i]); + vr::ETrackedPropertyError err = handle_property_write(batch[i]); + if (err != vr::ETrackedPropertyError::TrackedProp_Success) { + return err; + } + } + return vr::ETrackedPropertyError::TrackedProp_Success; +} + +vr::ETrackedPropertyError +Device::handle_read_properties(vr::PropertyRead_t *batch, uint32_t count) +{ + for (uint32_t i = 0; i < count; ++i) { + vr::ETrackedPropertyError err = handle_generic_property_read(batch[i]); + if (err != vr::ETrackedPropertyError::TrackedProp_Success) { + return err; + } } + return vr::ETrackedPropertyError::TrackedProp_Success; } void @@ -829,7 +852,52 @@ parse_profile(std::string_view path) } } // namespace -void +vr::ETrackedPropertyError +Device::handle_generic_property_write(const vr::PropertyWrite_t &prop) +{ + switch (prop.writeType) { + case vr::EPropertyWriteType::PropertyWrite_Set: + if (properties.count(prop.prop) > 0) { + Property &p = properties.at(prop.prop); + if (p.tag != prop.unTag) { + return vr::ETrackedPropertyError::TrackedProp_WrongDataType; + } + p.buffer.resize(prop.unBufferSize); + std::memcpy(p.buffer.data(), prop.pvBuffer, prop.unBufferSize); + return vr::ETrackedPropertyError::TrackedProp_Success; + } else { + properties.emplace(std::piecewise_construct, std::forward_as_tuple(prop.prop), + std::forward_as_tuple(prop.unTag, prop.pvBuffer, prop.unBufferSize)); + } + break; + case vr::EPropertyWriteType::PropertyWrite_Erase: properties.erase(prop.prop); break; + case vr::EPropertyWriteType::PropertyWrite_SetError: + DEV_DEBUG("Property write type SetError not supported! (property %d)", prop.prop); + break; + } + return vr::ETrackedPropertyError::TrackedProp_Success; +} + +vr::ETrackedPropertyError +Device::handle_generic_property_read(vr::PropertyRead_t &prop) +{ + if (properties.count(prop.prop) == 0) { + // not verified if this is the correct error + return vr::ETrackedPropertyError::TrackedProp_UnknownProperty; + } + Property &p = properties.at(prop.prop); + prop.unTag = p.tag; + prop.unRequiredBufferSize = p.buffer.size(); + if (prop.pvBuffer == nullptr || prop.unBufferSize < p.buffer.size()) { + prop.eError = vr::ETrackedPropertyError::TrackedProp_BufferTooSmall; + return prop.eError; + } + std::memcpy(prop.pvBuffer, p.buffer.data(), p.buffer.size()); + prop.eError = vr::ETrackedPropertyError::TrackedProp_Success; + return prop.eError; +} + +vr::ETrackedPropertyError Device::handle_property_write(const vr::PropertyWrite_t &prop) { switch (prop.prop) { @@ -854,9 +922,10 @@ Device::handle_property_write(const vr::PropertyWrite_t &prop) break; } } + return handle_generic_property_write(prop); } -void +vr::ETrackedPropertyError HmdDevice::handle_property_write(const vr::PropertyWrite_t &prop) { switch (prop.prop) { @@ -907,13 +976,13 @@ HmdDevice::handle_property_write(const vr::PropertyWrite_t &prop) break; } default: { - Device::handle_property_write(prop); - break; + return Device::handle_property_write(prop); } } + return handle_generic_property_write(prop); } -void +vr::ETrackedPropertyError ControllerDevice::handle_property_write(const vr::PropertyWrite_t &prop) { switch (prop.prop) { @@ -943,8 +1012,7 @@ ControllerDevice::handle_property_write(const vr::PropertyWrite_t &prop) (this->manufacturer.size() != name.size()); fixedProp.unBufferSize = name.end() - (char *)fixedProp.pvBuffer; } - Device::handle_property_write(fixedProp); - break; + return Device::handle_property_write(fixedProp); } case vr::Prop_ControllerRoleHint_Int32: { vr::ETrackedControllerRole role = *static_cast(prop.pvBuffer); @@ -1037,8 +1105,8 @@ ControllerDevice::handle_property_write(const vr::PropertyWrite_t &prop) break; } default: { - Device::handle_property_write(prop); - break; + return Device::handle_property_write(prop); } } + return handle_generic_property_write(prop); } diff --git a/src/xrt/drivers/steamvr_lh/device.hpp b/src/xrt/drivers/steamvr_lh/device.hpp index 0d160c87d..6aafb0467 100644 --- a/src/xrt/drivers/steamvr_lh/device.hpp +++ b/src/xrt/drivers/steamvr_lh/device.hpp @@ -44,6 +44,15 @@ struct DeviceBuilder operator=(const DeviceBuilder &) = delete; }; +class Property +{ +public: + Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize); + + vr::PropertyTypeTag_t tag; + std::vector buffer; +}; + class Device : public xrt_device { @@ -65,9 +74,12 @@ public: void get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation); - void + vr::ETrackedPropertyError handle_properties(const vr::PropertyWrite_t *batch, uint32_t count); + vr::ETrackedPropertyError + handle_read_properties(vr::PropertyRead_t *batch, uint32_t count); + //! Maps to @ref xrt_device::get_tracked_pose. virtual xrt_result_t get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0; @@ -79,6 +91,7 @@ protected: Device(const DeviceBuilder &builder); std::shared_ptr ctx; vr::PropertyContainerHandle_t container_handle{0}; + std::unordered_map properties; std::unordered_map inputs_map; std::vector inputs_vec; inline static xrt_pose chaperone = XRT_POSE_IDENTITY; @@ -90,7 +103,12 @@ protected: bool charging{false}; float charge{1.0F}; - virtual void + vr::ETrackedPropertyError + handle_generic_property_write(const vr::PropertyWrite_t &prop); + vr::ETrackedPropertyError + handle_generic_property_read(vr::PropertyRead_t &prop); + + virtual vr::ETrackedPropertyError handle_property_write(const vr::PropertyWrite_t &prop); private: @@ -158,7 +176,7 @@ public: private: std::unique_ptr hmd_parts{nullptr}; - void + vr::ETrackedPropertyError handle_property_write(const vr::PropertyWrite_t &prop) override; void @@ -214,6 +232,6 @@ private: void set_hand_tracking_hand(xrt_input_name name); - void + vr::ETrackedPropertyError handle_property_write(const vr::PropertyWrite_t &prop) override; }; diff --git a/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp b/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp index 2a7da6c2d..fd5ef2d77 100644 --- a/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp +++ b/src/xrt/drivers/steamvr_lh/steamvr_lh.cpp @@ -667,7 +667,12 @@ Context::ReadPropertyBatch(vr::PropertyContainerHandle_t ulContainerHandle, vr::PropertyRead_t *pBatch, uint32_t unBatchEntryCount) { - return vr::TrackedProp_Success; + Device *device = prop_container_to_device(ulContainerHandle); + if (!device) + return vr::TrackedProp_InvalidContainer; + if (!pBatch) + return vr::TrackedProp_InvalidOperation; // not verified vs steamvr + return device->handle_read_properties(pBatch, unBatchEntryCount); } vr::ETrackedPropertyError @@ -680,8 +685,7 @@ Context::WritePropertyBatch(vr::PropertyContainerHandle_t ulContainerHandle, return vr::TrackedProp_InvalidContainer; if (!pBatch) return vr::TrackedProp_InvalidOperation; // not verified vs steamvr - device->handle_properties(pBatch, unBatchEntryCount); - return vr::TrackedProp_Success; + return device->handle_properties(pBatch, unBatchEntryCount); } const char * -- 2.51.2