From 1ced3302371bf31d3db0ffa072241c6d7af94864 Mon Sep 17 00:00:00 2001 From: "Kevin M. Godby" Date: Mon, 4 Jul 2016 23:52:22 -0500 Subject: [PATCH 01/24] Refactored to pull common code into OSVRTrackedDevice base class. --- src/CMakeLists.txt | 5 +- src/OSVRTrackedDevice.cpp | 915 +----------------- src/OSVRTrackedDevice.h | 144 ++- src/OSVRTrackedHMD.cpp | 508 ++++++++++ src/OSVRTrackedHMD.h | 144 +++ src/OSVRTrackingReference.cpp | 526 +--------- src/OSVRTrackingReference.h | 85 +- src/PropertyMap.h | 47 + ...vice_properties.h => PropertyProperties.h} | 11 +- src/ServerDriver_OSVR.cpp | 4 +- 10 files changed, 858 insertions(+), 1531 deletions(-) create mode 100644 src/OSVRTrackedHMD.cpp create mode 100644 src/OSVRTrackedHMD.h create mode 100644 src/PropertyMap.h rename src/{osvr_device_properties.h => PropertyProperties.h} (98%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 10d7f2c..202174a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,6 +40,8 @@ add_library(driver_osvr Logging.h OSVRTrackedDevice.cpp OSVRTrackedDevice.h + OSVRTrackedHMD.cpp + OSVRTrackedHMD.h OSVRTrackingReference.cpp OSVRTrackingReference.h ServerDriver_OSVR.cpp @@ -51,7 +53,8 @@ add_library(driver_osvr identity.h make_unique.h matrix_cast.h - osvr_device_properties.h + PropertyProperties.h + PropertyMap.h osvr_dll_export.h platform_fixes.h pretty_print.h diff --git a/src/OSVRTrackedDevice.cpp b/src/OSVRTrackedDevice.cpp index a2c4b82..d0e4190 100644 --- a/src/OSVRTrackedDevice.cpp +++ b/src/OSVRTrackedDevice.cpp @@ -29,7 +29,6 @@ #include "osvr_compiler_detection.h" #include "make_unique.h" #include "matrix_cast.h" -#include "osvr_device_properties.h" #include "ValveStrCpy.h" #include "platform_fixes.h" // strcasecmp #include "make_unique.h" @@ -55,17 +54,9 @@ #include #include // for std::find -OSVRTrackedDevice::OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::IDriverLog* driver_log) : context_(context), driverHost_(driver_host), pose_(), deviceClass_(vr::TrackedDeviceClass_HMD) +OSVRTrackedDevice::OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::ETrackedDeviceClass device_class) : context_(context), driverHost_(driver_host), pose_(), deviceClass_(device_class) { - OSVR_LOG(trace) << "OSVRTrackedDevice::OSVRTrackedDevice() called."; - - OSVR_LOG(debug) << "Client context: " << (&context_); - settings_ = std::make_unique(driver_host->GetSettings(vr::IVRSettings_Version)); - if (driver_log) { - Logging::instance().setDriverLog(driver_log); - } - - configure(); + settings_ = std::make_unique(driverHost_->GetSettings(vr::IVRSettings_Version)); } OSVRTrackedDevice::~OSVRTrackedDevice() @@ -75,114 +66,38 @@ OSVRTrackedDevice::~OSVRTrackedDevice() vr::EVRInitError OSVRTrackedDevice::Activate(uint32_t object_id) { - OSVR_LOG(trace) << "OSVRTrackedDevice::Activate() called."; - objectId_ = object_id; - - const std::time_t waitTime = 5; // wait up to 5 seconds for init - - // Register tracker callback - if (trackerInterface_.notEmpty()) { - trackerInterface_.free(); - } - - // Ensure context is fully started up - OSVR_LOG(trace) << "OSVRTrackedDevice::Activate(): Waiting for the context to fully start up...\n"; - std::time_t startTime = std::time(nullptr); - while (!context_.checkStatus()) { - context_.update(); - if (std::time(nullptr) > startTime + waitTime) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Context startup timed out!\n"; - return vr::VRInitError_Driver_Failed; - } - } - - configureDistortionParameters(); - - displayConfig_ = osvr::clientkit::DisplayConfig(context_); - - // Ensure display is fully started up - OSVR_LOG(trace) << "OSVRTrackedDevice::Activate(): Waiting for the display to fully start up, including receiving initial pose update...\n"; - startTime = std::time(nullptr); - while (!displayConfig_.checkStartup()) { - context_.update(); - if (std::time(nullptr) > startTime + waitTime) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Display startup timed out!\n"; - return vr::VRInitError_Driver_Failed; - } - } - - // Verify valid display config - if ((displayConfig_.getNumViewers() != 1) && (displayConfig_.getViewer(0).getNumEyes() != 2) && (displayConfig_.getViewer(0).getEye(0).getNumSurfaces() == 1) && (displayConfig_.getViewer(0).getEye(1).getNumSurfaces() != 1)) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Unexpected display parameters!\n"; - - if (displayConfig_.getNumViewers() < 1) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): At least one viewer must exist.\n"; - return vr::VRInitError_Driver_HmdDisplayNotFound; - } else if (displayConfig_.getViewer(0).getNumEyes() < 2) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): At least two eyes must exist.\n"; - return vr::VRInitError_Driver_HmdDisplayNotFound; - } else if ((displayConfig_.getViewer(0).getEye(0).getNumSurfaces() < 1) || (displayConfig_.getViewer(0).getEye(1).getNumSurfaces() < 1)) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): At least one surface must exist for each eye.\n"; - return vr::VRInitError_Driver_HmdDisplayNotFound; - } - } - - // Register tracker callback - trackerInterface_ = context_.getInterface("/me/head"); - trackerInterface_.registerCallback(&OSVRTrackedDevice::HmdTrackerCallback, this); - - auto configString = context_.getStringParameter("/renderManagerConfig"); - - // If the /renderManagerConfig parameter is missing from the configuration - // file, use an empty dictionary instead. This allows the render manager - // config to zero out its values. - if (configString.empty()) { - OSVR_LOG(info) << "OSVRTrackedDevice::Activate(): Render Manager config is empty, using default values.\n"; - configString = "{}"; - } - - try { - renderManagerConfig_.parse(configString); - } catch(const std::exception& e) { - OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Exception parsing Render Manager config: " << e.what() << "\n"; - } - - driverHost_->ProximitySensorState(objectId_, true); - - OSVR_LOG(trace) << "OSVRTrackedDevice::Activate(): Activation complete.\n"; return vr::VRInitError_None; } void OSVRTrackedDevice::Deactivate() { - OSVR_LOG(trace) << "OSVRTrackedDevice::Deactivate() called."; - - /// Have to force freeing here - if (trackerInterface_.notEmpty()) { - trackerInterface_.free(); - } + // do nothing } void OSVRTrackedDevice::PowerOff() { - // FIXME Implement + // do nothing } void* OSVRTrackedDevice::GetComponent(const char* component_name_and_version) { if (!strcasecmp(component_name_and_version, vr::IVRDisplayComponent_Version)) { - return static_cast(this); + return dynamic_cast(this); + } else if (!strcasecmp(component_name_and_version, vr::IVRDriverDirectModeComponent_Version)) { + return dynamic_cast(this); + } else if (!strcasecmp(component_name_and_version, vr::IVRControllerComponent_Version)) { + return dynamic_cast(this); + } else if (!strcasecmp(component_name_and_version, vr::IVRCameraComponent_Version)) { + return dynamic_cast(this); + } else { + OSVR_LOG(warn) << "Unknown component [" << component_name_and_version << "] requested."; + return nullptr; } - - // Override this to add a component to a driver - return nullptr; } void OSVRTrackedDevice::DebugRequest(const char* request, char* response_buffer, uint32_t response_buffer_size) { - // TODO - // Log the requests just to see what info clients are looking for OSVR_LOG(debug) << "Received debug request [" << request << "] with response buffer size of " << response_buffer_size << "]."; @@ -193,119 +108,6 @@ void OSVRTrackedDevice::DebugRequest(const char* request, char* response_buffer, } } -void OSVRTrackedDevice::GetWindowBounds(int32_t* x, int32_t* y, uint32_t* width, uint32_t* height) -{ - int nDisplays = displayConfig_.getNumDisplayInputs(); - if (nDisplays != 1) { - OSVR_LOG(err) << "OSVRTrackedDevice::OSVRTrackedDevice(): Unexpected display number of displays!\n"; - } - osvr::clientkit::DisplayDimensions displayDims = displayConfig_.getDisplayDimensions(0); - *x = renderManagerConfig_.getWindowXPosition(); // todo: assumes desktop display of 1920. get this from display config when it's exposed. - *y = renderManagerConfig_.getWindowYPosition(); - *width = static_cast(displayDims.width); - *height = static_cast(displayDims.height); - -#if defined(OSVR_WINDOWS) || defined(OSVR_MACOSX) - // ... until we've added code for other platforms - *x = display_.position.x; - *y = display_.position.y; - *height = display_.size.height; - *width = display_.size.width; -#endif -} - -bool OSVRTrackedDevice::IsDisplayOnDesktop() -{ - // If the current display still appeara in the active displays list, - // then it's attached to the desktop. - const auto displays = osvr::display::getDisplays(); - const auto display_on_desktop = (end(displays) != std::find(begin(displays), end(displays), display_)); - OSVR_LOG(trace) << "OSVRTrackedDevice::IsDisplayOnDesktop(): " << (display_on_desktop ? "yes" : "no"); - return display_on_desktop; -} - -bool OSVRTrackedDevice::IsDisplayRealDisplay() -{ - // TODO get this info from display description? - return true; -} - -void OSVRTrackedDevice::GetRecommendedRenderTargetSize(uint32_t* width, uint32_t* height) -{ - /// @todo calculate overfill factor properly - double overfill_factor = 1.0; - int32_t x, y; - uint32_t w, h; - GetWindowBounds(&x, &y, &w, &h); - - *width = static_cast(w * overfill_factor); - *height = static_cast(h * overfill_factor); -} - -void OSVRTrackedDevice::GetEyeOutputViewport(vr::EVREye eye, uint32_t* x, uint32_t* y, uint32_t* width, uint32_t* height) -{ - osvr::clientkit::RelativeViewport viewPort = displayConfig_.getViewer(0).getEye(eye).getSurface(0).getRelativeViewport(); - *x = static_cast(viewPort.left); - *y = static_cast(viewPort.bottom); - *width = static_cast(viewPort.width); - *height = static_cast(viewPort.height); -} - -void OSVRTrackedDevice::GetProjectionRaw(vr::EVREye eye, float* left, float* right, float* top, float* bottom) -{ - // Reference: https://github.com/ValveSoftware/openvr/wiki/IVRSystem::GetProjectionRaw - // SteamVR expects top and bottom to be swapped! - osvr::clientkit::ProjectionClippingPlanes pl = displayConfig_.getViewer(0).getEye(eye).getSurface(0).getProjectionClippingPlanes(); - *left = static_cast(pl.left); - *right = static_cast(pl.right); - *bottom = static_cast(pl.top); // SWAPPED - *top = static_cast(pl.bottom); // SWAPPED -} - -vr::DistortionCoordinates_t OSVRTrackedDevice::ComputeDistortion(vr::EVREye eye, float u, float v) -{ - // Note that RenderManager expects the (0, 0) to be the lower-left corner and (1, 1) to be the upper-right corner while SteamVR assumes (0, 0) is upper-left and (1, 1) is lower-right. - // To accommodate this, we need to flip the y-coordinate before passing it to RenderManager and flip it again before returning the value to SteamVR. - OSVR_LOG(trace) << "OSVRTrackedDevice::ComputeDistortion(" << eye << ", " << u << ", " << v << ") called."; - - using osvr::renderkit::DistortionCorrectTextureCoordinate; - static const size_t COLOR_RED = 0; - static const size_t COLOR_GREEN = 1; - static const size_t COLOR_BLUE = 2; - - const auto osvr_eye = static_cast(eye); - const auto distortion_parameters = distortionParameters_[osvr_eye]; - const auto in_coords = osvr::renderkit::Float2 {{u, 1.0 - v}}; // flip v-coordinate - - auto interpolators = &leftEyeInterpolators_; - if (vr::Eye_Right == eye) { - interpolators = &rightEyeInterpolators_; - } - - auto coords_red = DistortionCorrectTextureCoordinate( - osvr_eye, in_coords, distortion_parameters, - COLOR_RED, overfillFactor_, *interpolators); - - auto coords_green = DistortionCorrectTextureCoordinate( - osvr_eye, in_coords, distortion_parameters, - COLOR_GREEN, overfillFactor_, *interpolators); - - auto coords_blue = DistortionCorrectTextureCoordinate( - osvr_eye, in_coords, distortion_parameters, - COLOR_BLUE, overfillFactor_, *interpolators); - - vr::DistortionCoordinates_t coords; - // flip v-coordinates again - coords.rfRed[0] = coords_red[0]; - coords.rfRed[1] = 1.0 - coords_red[1]; - coords.rfGreen[0] = coords_green[0]; - coords.rfGreen[1] = 1.0 - coords_green[1]; - coords.rfBlue[0] = coords_blue[0]; - coords.rfBlue[1] = 1.0 - coords_blue[1]; - - return coords; -} - vr::DriverPose_t OSVRTrackedDevice::GetPose() { return pose_; @@ -313,376 +115,22 @@ vr::DriverPose_t OSVRTrackedDevice::GetPose() bool OSVRTrackedDevice::GetBoolTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) { - const bool default_value = false; - - const auto result = checkProperty(prop, bool()); - if (vr::TrackedProp_Success != result) { - if (error) - *error = result; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - // Prop_ContainsProximitySensor_Bool spams our log files. Ignoring it here. - //OSVR_LOG(trace) << "OSVRTrackedDevice::GetBoolTrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // Properties that apply to all device classes - case vr::Prop_WillDriftInYaw_Bool: - if (error) - *error = vr::TrackedProp_Success; - return true; - break; - case vr::Prop_DeviceIsWireless_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_DeviceIsCharging_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_Firmware_UpdateAvailable_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_Firmware_ManualUpdate_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_BlockServerShutdown_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_CanUnifyCoordinateSystemWithHmd_Bool: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - break; - case vr::Prop_ContainsProximitySensor_Bool: - if (error) - *error = vr::TrackedProp_Success; - return true; - break; - case vr::Prop_DeviceProvidesBatteryStatus_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_DeviceCanPowerOff_Bool: - if (error) - *error = vr::TrackedProp_Success; - return true; - break; - case vr::Prop_HasCamera_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - // Properties that apply to HMDs - case vr::Prop_ReportsTimeSinceVSync_Bool: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - break; - case vr::Prop_IsOnDesktop_Bool: - if (error) - *error = vr::TrackedProp_Success; - return this->IsDisplayOnDesktop(); - break; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackedDevice::GetBoolTrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; + return GetTrackedDeviceProperty(prop, error, false); } float OSVRTrackedDevice::GetFloatTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) { - const float default_value = 0.0f; - - const auto result = checkProperty(prop, float()); - if (vr::TrackedProp_Success != result) { - if (error) - *error = result; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackedDevice::GetFloatTrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_DeviceBatteryPercentage_Float: - if (error) - *error = vr::TrackedProp_Success; - return 1.0f; // full battery - // Properties that are unique to TrackedDeviceClass_HMD - case vr::Prop_SecondsFromVsyncToPhotons_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayFrequency_Float: - if (error) - *error = vr::TrackedProp_Success; - return static_cast(display_.verticalRefreshRate); - case vr::Prop_UserIpdMeters_Float: - if (error) - *error = vr::TrackedProp_Success; - return GetIPD(); - case vr::Prop_DisplayMCOffset_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayMCScale_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayGCBlackClamp_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayGCOffset_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayGCScale_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayGCPrescale_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_LensCenterLeftU_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_LensCenterLeftV_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_LensCenterRightU_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_LensCenterRightV_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_UserHeadToEyeDepthMeters_Float: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - // Properties that are unique to TrackedDeviceClass_TrackingReference - case vr::Prop_FieldOfViewLeftDegrees_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FieldOfViewRightDegrees_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FieldOfViewTopDegrees_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FieldOfViewBottomDegrees_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_TrackingRangeMinimumMeters_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_TrackingRangeMaximumMeters_Float: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackedDevice::GetFloatTrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; + return GetTrackedDeviceProperty(prop, error, 0.0f); } int32_t OSVRTrackedDevice::GetInt32TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) { - const int32_t default_value = 0; - - const auto result = checkProperty(prop, int32_t()); - if (vr::TrackedProp_Success != result) { - if (error) - *error = result; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackedDevice::GetInt32TrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_DeviceClass_Int32: - if (error) - *error = vr::TrackedProp_Success; - return deviceClass_; - // Properties that are unique to TrackedDeviceClass_HMD - case vr::Prop_DisplayMCType_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_EdidVendorID_Int32: - if (error) - *error = vr::TrackedProp_Success; - return static_cast(display_.edidVendorId); - case vr::Prop_EdidProductID_Int32: - if (error) - *error = vr::TrackedProp_Success; - return static_cast(display_.edidProductId); - case vr::Prop_DisplayGCType_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_CameraCompatibilityMode_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - // Properties that are unique to TrackedDeviceClass_Controller - case vr::Prop_Axis0Type_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Axis1Type_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Axis2Type_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Axis3Type_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Axis4Type_Int32: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackedDevice::GetInt32TrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; + return GetTrackedDeviceProperty(prop, error, static_cast(0)); } uint64_t OSVRTrackedDevice::GetUint64TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) { - const uint64_t default_value = 0; - - const auto result = checkProperty(prop, uint64_t()); - if (vr::TrackedProp_Success != result) { - if (error) - *error = result; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackedDevice::GetUint64TrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_HardwareRevision_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FirmwareVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FPGAVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_VRCVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_RadioVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DongleVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - // Properties that are unique to TrackedDeviceClass_HMD - case vr::Prop_CurrentUniverseId_Uint64: - if (error) - *error = vr::TrackedProp_Success; - return 1; - case vr::Prop_PreviousUniverseId_Uint64: - if (error) - *error = vr::TrackedProp_Success; - return 1; - case vr::Prop_DisplayFirmwareVersion_Uint64: - /// @todo This really should be read from the server - if (error) - *error = vr::TrackedProp_Success; - return 192; - case vr::Prop_CameraFirmwareVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayFPGAVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayBootloaderVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayHardwareVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_AudioFirmwareVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - // Properties that are unique to TrackedDeviceClass_Controller - case vr::Prop_SupportedButtons_Uint64: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackedDevice::GetUint64TrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; + return GetTrackedDeviceProperty(prop, error, static_cast(0)); } vr::HmdMatrix34_t OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) @@ -690,38 +138,7 @@ vr::HmdMatrix34_t OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(vr::ETrack // Default value is identity matrix vr::HmdMatrix34_t default_value; map(default_value) = Matrix34f::Identity(); - - const auto result = checkProperty(prop, vr::HmdMatrix34_t()); - if (vr::TrackedProp_Success != result) { - if (error) - *error = result; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_StatusDisplayTransform_Matrix34: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - // Properties that are unique to TrackedDeviceClass_HMD - case vr::Prop_CameraToHeadTransform_Matrix34: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; + return GetTrackedDeviceProperty(prop, error, default_value); } uint32_t OSVRTrackedDevice::GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, char* value, uint32_t buffer_size, vr::ETrackedPropertyError *error) @@ -735,303 +152,25 @@ uint32_t OSVRTrackedDevice::GetStringTrackedDeviceProperty(vr::ETrackedDevicePro return default_value; } - OSVR_LOG(trace) << "OSVRTrackedDevice::GetStringTrackedDeviceProperty(): Requested property: " << prop << "\n"; - - std::string sValue = GetStringTrackedDeviceProperty(prop, error); + const auto str = GetStringTrackedDeviceProperty(prop, error); if (*error == vr::TrackedProp_Success) { - if (sValue.size() + 1 > buffer_size) { + if (str.size() + 1 > buffer_size) { *error = vr::TrackedProp_BufferTooSmall; } else { - valveStrCpy(sValue, value, buffer_size); + valveStrCpy(str, value, buffer_size); } - return static_cast(sValue.size()) + 1; + return static_cast(str.size()) + 1; } return 0; } - // ------------------------------------ - // Private Methods - // ------------------------------------ +// ------------------------------------ +// Protected Methods +// ------------------------------------ std::string OSVRTrackedDevice::GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError *error) { - std::string default_value = ""; - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_TrackingSystemName_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_ModelNumber_String: - if (error) - *error = vr::TrackedProp_Success; - return "OSVR HMD"; - case vr::Prop_SerialNumber_String: - if (error) - *error = vr::TrackedProp_Success; - return this->GetId(); - case vr::Prop_RenderModelName_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_ManufacturerName_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_TrackingFirmwareVersion_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_HardwareRevision_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_AllWirelessDongleDescriptions_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_ConnectedWirelessDongle_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Firmware_ManualUpdateURL_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Firmware_ProgrammingTarget_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DriverVersion_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - - // Properties that are unique to TrackedDeviceClass_HMD - case vr::Prop_DisplayMCImageLeft_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayMCImageRight_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DisplayGCImage_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_CameraFirmwareDescription_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - - // Properties that are unique to TrackedDeviceClass_Controller - case vr::Prop_AttachedDeviceId_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - - // Properties that are unique to TrackedDeviceClass_TrackingReference - case vr::Prop_ModeLabel_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackedDevice::GetStringTrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - -void OSVRTrackedDevice::HmdTrackerCallback(void* userdata, const OSVR_TimeValue*, const OSVR_PoseReport* report) -{ - if (!userdata) - return; - - auto* self = static_cast(userdata); - - vr::DriverPose_t pose; - pose.poseTimeOffset = 0; // close enough - - Eigen::Vector3d::Map(pose.vecWorldFromDriverTranslation) = Eigen::Vector3d::Zero(); - Eigen::Vector3d::Map(pose.vecDriverFromHeadTranslation) = Eigen::Vector3d::Zero(); - - map(pose.qWorldFromDriverRotation) = Eigen::Quaterniond::Identity(); - - map(pose.qDriverFromHeadRotation) = Eigen::Quaterniond::Identity(); - - // Position - Eigen::Vector3d::Map(pose.vecPosition) = osvr::util::vecMap(report->pose.translation); - - // Position velocity and acceleration are not currently consistently provided - Eigen::Vector3d::Map(pose.vecVelocity) = Eigen::Vector3d::Zero(); - Eigen::Vector3d::Map(pose.vecAcceleration) = Eigen::Vector3d::Zero(); - - // Orientation - map(pose.qRotation) = osvr::util::fromQuat(report->pose.rotation); - - // Angular velocity and acceleration are not currently consistently provided - Eigen::Vector3d::Map(pose.vecAngularVelocity) = Eigen::Vector3d::Zero(); - Eigen::Vector3d::Map(pose.vecAngularAcceleration) = Eigen::Vector3d::Zero(); - - pose.result = vr::TrackingResult_Running_OK; - pose.poseIsValid = true; - pose.willDriftInYaw = true; - pose.shouldApplyHeadModel = true; - - self->pose_ = pose; - self->driverHost_->TrackedDevicePoseUpdated(self->objectId_, self->pose_); -} - -float OSVRTrackedDevice::GetIPD() -{ - OSVR_Pose3 leftEye, rightEye; - - if (displayConfig_.getViewer(0).getEye(0).getPose(leftEye) != true) { - OSVR_LOG(err) << "OSVRTrackedDevice::GetHeadFromEyePose(): Unable to get left eye pose!\n"; - } - - if (displayConfig_.getViewer(0).getEye(1).getPose(rightEye) != true) { - OSVR_LOG(err) << "OSVRTrackedDevice::GetHeadFromEyePose(): Unable to get right eye pose!\n"; - } - - float ipd = static_cast((osvr::util::vecMap(leftEye.translation) - osvr::util::vecMap(rightEye.translation)).norm()); - return ipd; -} - -const char* OSVRTrackedDevice::GetId() -{ - return display_.name.c_str(); -} - -void OSVRTrackedDevice::configure() -{ - // Get settings from config file - const bool verbose_logging = settings_->getSetting("verbose", false); - if (verbose_logging) { - OSVR_LOG(info) << "Verbose logging enabled."; - Logging::instance().setLogLevel(trace); - } else { - OSVR_LOG(info) << "Verbose logging disabled."; - Logging::instance().setLogLevel(info); - } - - // The name of the display we want to use - const std::string display_name = settings_->getSetting("displayName", "OSVR"); - - // Detect displays and find the one we're using as an HMD - bool display_found = false; - auto displays = osvr::display::getDisplays(); - for (const auto& display : displays) { - if (std::string::npos == display.name.find(display_name)) - continue; - - display_ = display; - display_found = true; - break; - } - - if (!display_found) { - // Default to OSVR HDK display settings - display_.adapter.description = "Unknown"; - display_.name = "OSVR HDK"; - display_.size.width = 1920; - display_.size.height = 1080; - display_.position.x = 1920; - display_.position.y = 0; - display_.rotation = osvr::display::Rotation::Zero; - display_.verticalRefreshRate = 60.0; - display_.attachedToDesktop = true; - display_.edidVendorId = 0xd24e;// 53838 - display_.edidProductId = 0x1019; // 4121 - } - - if (display_found) { - OSVR_LOG(info) << "Detected display named [" << display_.name << "]:"; - } else { - OSVR_LOG(info) << "Default display:"; - } - OSVR_LOG(info) << " Adapter: " << display_.adapter.description; - OSVR_LOG(info) << " Monitor name: " << display_.name; - OSVR_LOG(info) << " Resolution: " << display_.size.width << "x" << display_.size.height; - OSVR_LOG(info) << " Position: (" << display_.position.x << ", " << display_.position.y << ")"; - switch (display_.rotation) { - case osvr::display::Rotation::Zero: - OSVR_LOG(info) << " Rotation: Landscape"; - break; - case osvr::display::Rotation::Ninety: - OSVR_LOG(info) << " Rotation: Portrait"; - break; - case osvr::display::Rotation::OneEighty: - OSVR_LOG(info) << " Rotation: Landscape (flipped)"; - break; - case osvr::display::Rotation::TwoSeventy: - OSVR_LOG(info) << " Rotation: Portrait (flipped)"; - break; - default: - OSVR_LOG(info) << " Rotation: Landscape"; - break; - } - OSVR_LOG(info) << " Refresh rate: " << display_.verticalRefreshRate; - OSVR_LOG(info) << " " << (display_.attachedToDesktop ? "Extended mode" : "Direct mode"); - OSVR_LOG(info) << " EDID vendor ID: " << display_.edidVendorId; - OSVR_LOG(info) << " EDID product ID: " << display_.edidProductId; -} - -void OSVRTrackedDevice::configureDistortionParameters() -{ - // Parse the display descriptor - displayDescription_ = context_.getStringParameter("/display"); - displayConfiguration_ = OSVRDisplayConfiguration(displayDescription_); - - // Initialize the distortion parameters - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Number of eyes: " << displayConfiguration_.getEyes().size() << "."; - for (size_t i = 0; i < displayConfiguration_.getEyes().size(); ++i) { - auto distortion = osvr::renderkit::DistortionParameters { displayConfiguration_, i }; - distortion.m_desiredTriangles = 200 * 64; - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Adding distortion for eye " << i << "."; - distortionParameters_.push_back(distortion); - } - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Number of distortion parameters: " << distortionParameters_.size() << "."; - - // Make the interpolators to be used by each eye. - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Creating mesh interpolators for the left eye."; - if (!makeUnstructuredMeshInterpolators(distortionParameters_[0], 0, leftEyeInterpolators_)) { - OSVR_LOG(err) << "OSVRTrackedDevice::configureDistortionParameters(): Could not create mesh interpolators for left eye."; - } - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Number of left eye interpolators: " << leftEyeInterpolators_.size() << "."; - - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Creating mesh interpolators for the right eye."; - if (!makeUnstructuredMeshInterpolators(distortionParameters_[1], 1, rightEyeInterpolators_)) { - OSVR_LOG(err) << "OSVRTrackedDevice::configureDistortionParameters(): Could not create mesh interpolators for right eye."; - } - OSVR_LOG(debug) << "OSVRTrackedDevice::configureDistortionParameters(): Number of right eye interpolators: " << leftEyeInterpolators_.size() << "."; -} - -template -vr::ETrackedPropertyError OSVRTrackedDevice::checkProperty(vr::ETrackedDeviceProperty prop, const T&) -{ - if (isWrongDataType(prop, T())) { - return vr::TrackedProp_WrongDataType; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - return vr::TrackedProp_WrongDeviceClass; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - return vr::TrackedProp_InvalidDevice; - } - - return vr::TrackedProp_Success; + return GetTrackedDeviceProperty(prop, error, std::string{""}); } diff --git a/src/OSVRTrackedDevice.h b/src/OSVRTrackedDevice.h index 4764f00..599c1e4 100644 --- a/src/OSVRTrackedDevice.h +++ b/src/OSVRTrackedDevice.h @@ -29,6 +29,8 @@ #include "osvr_compiler_detection.h" // for OSVR_OVERRIDE #include "Settings.h" #include "display/Display.h" +#include "PropertyMap.h" +#include "PropertyProperties.h" // OpenVR includes #include @@ -46,12 +48,13 @@ #include #include -class OSVRTrackedDevice : public vr::ITrackedDeviceServerDriver, public vr::IVRDisplayComponent { +class OSVRTrackedDevice : public vr::ITrackedDeviceServerDriver { friend class ServerDriver_OSVR; public: - OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::IDriverLog* driver_log = nullptr); + OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::ETrackedDeviceClass device_class); virtual ~OSVRTrackedDevice(); + // ------------------------------------ // Management Methods // ------------------------------------ @@ -92,53 +95,10 @@ friend class ServerDriver_OSVR; */ virtual void DebugRequest(const char* request, char* response_buffer, uint32_t response_buffer_size) OSVR_OVERRIDE; - // ------------------------------------ - // Display Methods - // ------------------------------------ - - /** - * Size and position that the window needs to be on the VR display. - */ - virtual void GetWindowBounds(int32_t* x, int32_t* y, uint32_t* width, uint32_t* height) OSVR_OVERRIDE; - - /** - * Returns true if the display is extending the desktop. - */ - virtual bool IsDisplayOnDesktop() OSVR_OVERRIDE; - - /** - * Returns true if the display is real and not a fictional display. - */ - virtual bool IsDisplayRealDisplay() OSVR_OVERRIDE; - - /** - * Suggested size for the intermediate render target that the distortion - * pulls from. - */ - virtual void GetRecommendedRenderTargetSize(uint32_t* width, uint32_t* height) OSVR_OVERRIDE; - - /** - * Gets the viewport in the frame buffer to draw the output of the distortion - * into - */ - virtual void GetEyeOutputViewport(vr::EVREye eye, uint32_t* x, uint32_t* y, uint32_t* width, uint32_t* height) OSVR_OVERRIDE; - - /** - * The components necessary to build your own projection matrix in case your - * application is doing something fancy like infinite Z - */ - virtual void GetProjectionRaw(vr::EVREye eye, float* left, float* right, float* top, float* bottom) OSVR_OVERRIDE; - - /** - * Returns the result of the distortion function for the specified eye and - * input UVs. UVs go from 0,0 in the upper left of that eye's viewport and - * 1,1 in the lower right of that eye's viewport. - */ - virtual vr::DistortionCoordinates_t ComputeDistortion(vr::EVREye eye, float u, float v) OSVR_OVERRIDE; - // ------------------------------------ // Tracking Methods // ------------------------------------ + virtual vr::DriverPose_t GetPose() OSVR_OVERRIDE; // ------------------------------------ @@ -187,29 +147,8 @@ friend class ServerDriver_OSVR; virtual uint32_t GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, char* value, uint32_t buffer_size, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; protected: - const char* GetId(); - -private: std::string GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError *error); - /** - * Callback function which is called whenever new data has been received - * from the tracker. - */ - static void HmdTrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report); - - float GetIPD(); - - /** - * Read configuration settings from configuration file. - */ - void configure(); - - /** - * Configure RenderManager and distortion parameters. - */ - void configureDistortionParameters(); - /** * Cecks to see if the requested property is valid for the device class and * type requested. @@ -223,30 +162,69 @@ friend class ServerDriver_OSVR; template vr::ETrackedPropertyError checkProperty(vr::ETrackedDeviceProperty prop, const T&); + template + T GetTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error, const T& default_value); + osvr::clientkit::ClientContext& context_; - std::string displayDescription_; - osvr::clientkit::DisplayConfig displayConfig_; - osvr::client::RenderManagerConfig renderManagerConfig_; vr::IServerDriverHost* driverHost_ = nullptr; - osvr::clientkit::Interface trackerInterface_; vr::DriverPose_t pose_; vr::ETrackedDeviceClass deviceClass_; std::unique_ptr settings_; uint32_t objectId_ = 0; - std::vector distortionParameters_; - OSVRDisplayConfiguration displayConfiguration_; - - // per-eye mesh interpolators - using MeshInterpolators = std::vector>; - MeshInterpolators leftEyeInterpolators_; - MeshInterpolators rightEyeInterpolators_; - float overfillFactor_ = 1.0; // TODO get from RenderManager - - // Settings - bool verboseLogging_ = false; - osvr::display::Display display_ = {}; + /** \name Collections of properties and their values. */ + //@{ + //std::map boolProperties_; + //std::map floatProperties_; + //std::map int32Properties_; + //std::map uint64Properties_; + //std::map matrix34Properties; + //std::map uint32Properties_; + //std::map stringProperties_; + PropertyMap properties_; + //@} }; +//template +//inline T OSVRTrackedDevice::GetTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error, const std::map& map, const T& default_value) +template +inline T OSVRTrackedDevice::GetTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error, const T& default_value) +{ + const auto result = checkProperty(prop, T()); + if (vr::TrackedProp_Success != result) { + if (error) + *error = result; + return default_value; + } + + if (properties_.find(prop) != end(properties_)) { + if (error) + *error = vr::TrackedProp_Success; + return boost::get(properties_[prop]); + } else { + if (error) + *error = vr::TrackedProp_ValueNotProvidedByDevice; + return default_value; + } +} + +template +inline vr::ETrackedPropertyError OSVRTrackedDevice::checkProperty(vr::ETrackedDeviceProperty prop, const T&) +{ + if (isWrongDataType(prop, T())) { + return vr::TrackedProp_WrongDataType; + } + + if (isWrongDeviceClass(prop, deviceClass_)) { + return vr::TrackedProp_WrongDeviceClass; + } + + if (vr::TrackedDeviceClass_Invalid == deviceClass_) { + return vr::TrackedProp_InvalidDevice; + } + + return vr::TrackedProp_Success; +} + #endif // INCLUDED_OSVRTrackedDevice_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 diff --git a/src/OSVRTrackedHMD.cpp b/src/OSVRTrackedHMD.cpp new file mode 100644 index 0000000..0394f4d --- /dev/null +++ b/src/OSVRTrackedHMD.cpp @@ -0,0 +1,508 @@ +/** @file + @brief OSVR tracked device + + @date 2015 + + @author + Sensics, Inc. + +*/ + +// Copyright 2015 Sensics, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Internal Includes +#include "OSVRTrackedHMD.h" +#include "Logging.h" + +#include "osvr_compiler_detection.h" +#include "make_unique.h" +#include "matrix_cast.h" +#include "ValveStrCpy.h" +#include "platform_fixes.h" // strcasecmp +#include "make_unique.h" +#include "osvr_platform.h" +#include "display/DisplayEnumerator.h" + +// OpenVR includes +#include + +// Library/third-party includes +#include +#include +#include +#include +#include + +// Standard includes +#include +#include +#include +#include +#include +#include // for std::find + +OSVRTrackedHMD::OSVRTrackedHMD(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host) : OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_HMD) +{ + OSVR_LOG(trace) << "OSVRTrackedHMD::OSVRTrackedHMD() called."; + + settings_ = std::make_unique(driver_host->GetSettings(vr::IVRSettings_Version)); + configure(); +} + +OSVRTrackedHMD::~OSVRTrackedHMD() +{ + // do nothing +} + +vr::EVRInitError OSVRTrackedHMD::Activate(uint32_t object_id) +{ + OSVR_LOG(trace) << "OSVRTrackedHMD::Activate() called."; + + objectId_ = object_id; + + const std::time_t waitTime = 5; // wait up to 5 seconds for init + + // Register tracker callback + if (trackerInterface_.notEmpty()) { + trackerInterface_.free(); + } + + // Ensure context is fully started up + OSVR_LOG(trace) << "OSVRTrackedHMD::Activate(): Waiting for the context to fully start up...\n"; + std::time_t startTime = std::time(nullptr); + while (!context_.checkStatus()) { + context_.update(); + if (std::time(nullptr) > startTime + waitTime) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): Context startup timed out!\n"; + return vr::VRInitError_Driver_Failed; + } + } + + configureDistortionParameters(); + + displayConfig_ = osvr::clientkit::DisplayConfig(context_); + + // Ensure display is fully started up + OSVR_LOG(trace) << "OSVRTrackedHMD::Activate(): Waiting for the display to fully start up, including receiving initial pose update...\n"; + startTime = std::time(nullptr); + while (!displayConfig_.checkStartup()) { + context_.update(); + if (std::time(nullptr) > startTime + waitTime) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): Display startup timed out!\n"; + return vr::VRInitError_Driver_Failed; + } + } + + // Verify valid display config + if ((displayConfig_.getNumViewers() != 1) && (displayConfig_.getViewer(0).getNumEyes() != 2) && (displayConfig_.getViewer(0).getEye(0).getNumSurfaces() == 1) && (displayConfig_.getViewer(0).getEye(1).getNumSurfaces() != 1)) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): Unexpected display parameters!\n"; + + if (displayConfig_.getNumViewers() < 1) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): At least one viewer must exist.\n"; + return vr::VRInitError_Driver_HmdDisplayNotFound; + } else if (displayConfig_.getViewer(0).getNumEyes() < 2) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): At least two eyes must exist.\n"; + return vr::VRInitError_Driver_HmdDisplayNotFound; + } else if ((displayConfig_.getViewer(0).getEye(0).getNumSurfaces() < 1) || (displayConfig_.getViewer(0).getEye(1).getNumSurfaces() < 1)) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): At least one surface must exist for each eye.\n"; + return vr::VRInitError_Driver_HmdDisplayNotFound; + } + } + + // Register tracker callback + trackerInterface_ = context_.getInterface("/me/head"); + trackerInterface_.registerCallback(&OSVRTrackedHMD::HmdTrackerCallback, this); + + auto configString = context_.getStringParameter("/renderManagerConfig"); + + // If the /renderManagerConfig parameter is missing from the configuration + // file, use an empty dictionary instead. This allows the render manager + // config to zero out its values. + if (configString.empty()) { + OSVR_LOG(info) << "OSVRTrackedHMD::Activate(): Render Manager config is empty, using default values.\n"; + configString = "{}"; + } + + try { + renderManagerConfig_.parse(configString); + } catch(const std::exception& e) { + OSVR_LOG(err) << "OSVRTrackedHMD::Activate(): Exception parsing Render Manager config: " << e.what() << "\n"; + } + + driverHost_->ProximitySensorState(objectId_, true); + + OSVR_LOG(trace) << "OSVRTrackedHMD::Activate(): Activation complete.\n"; + return vr::VRInitError_None; +} + +void OSVRTrackedHMD::Deactivate() +{ + OSVR_LOG(trace) << "OSVRTrackedHMD::Deactivate() called."; + + /// Have to force freeing here + if (trackerInterface_.notEmpty()) { + trackerInterface_.free(); + } +} + +void OSVRTrackedHMD::GetWindowBounds(int32_t* x, int32_t* y, uint32_t* width, uint32_t* height) +{ + int nDisplays = displayConfig_.getNumDisplayInputs(); + if (nDisplays != 1) { + OSVR_LOG(err) << "OSVRTrackedHMD::OSVRTrackedHMD(): Unexpected display number of displays!\n"; + } + osvr::clientkit::DisplayDimensions displayDims = displayConfig_.getDisplayDimensions(0); + *x = renderManagerConfig_.getWindowXPosition(); // todo: assumes desktop display of 1920. get this from display config when it's exposed. + *y = renderManagerConfig_.getWindowYPosition(); + *width = static_cast(displayDims.width); + *height = static_cast(displayDims.height); + +#if defined(OSVR_WINDOWS) || defined(OSVR_MACOSX) + // ... until we've added code for other platforms + *x = display_.position.x; + *y = display_.position.y; + *height = display_.size.height; + *width = display_.size.width; +#endif +} + +bool OSVRTrackedHMD::IsDisplayOnDesktop() +{ + // If the current display still appeara in the active displays list, + // then it's attached to the desktop. + const auto displays = osvr::display::getDisplays(); + const auto display_on_desktop = (end(displays) != std::find(begin(displays), end(displays), display_)); + OSVR_LOG(trace) << "OSVRTrackedHMD::IsDisplayOnDesktop(): " << (display_on_desktop ? "yes" : "no"); + return display_on_desktop; +} + +bool OSVRTrackedHMD::IsDisplayRealDisplay() +{ + // TODO get this info from display description? + return true; +} + +void OSVRTrackedHMD::GetRecommendedRenderTargetSize(uint32_t* width, uint32_t* height) +{ + /// @todo calculate overfill factor properly + double overfill_factor = 1.0; + int32_t x, y; + uint32_t w, h; + GetWindowBounds(&x, &y, &w, &h); + + *width = static_cast(w * overfill_factor); + *height = static_cast(h * overfill_factor); +} + +void OSVRTrackedHMD::GetEyeOutputViewport(vr::EVREye eye, uint32_t* x, uint32_t* y, uint32_t* width, uint32_t* height) +{ + osvr::clientkit::RelativeViewport viewPort = displayConfig_.getViewer(0).getEye(eye).getSurface(0).getRelativeViewport(); + *x = static_cast(viewPort.left); + *y = static_cast(viewPort.bottom); + *width = static_cast(viewPort.width); + *height = static_cast(viewPort.height); +} + +void OSVRTrackedHMD::GetProjectionRaw(vr::EVREye eye, float* left, float* right, float* top, float* bottom) +{ + // Reference: https://github.com/ValveSoftware/openvr/wiki/IVRSystem::GetProjectionRaw + // SteamVR expects top and bottom to be swapped! + osvr::clientkit::ProjectionClippingPlanes pl = displayConfig_.getViewer(0).getEye(eye).getSurface(0).getProjectionClippingPlanes(); + *left = static_cast(pl.left); + *right = static_cast(pl.right); + *bottom = static_cast(pl.top); // SWAPPED + *top = static_cast(pl.bottom); // SWAPPED +} + +vr::DistortionCoordinates_t OSVRTrackedHMD::ComputeDistortion(vr::EVREye eye, float u, float v) +{ + // Note that RenderManager expects the (0, 0) to be the lower-left corner and (1, 1) to be the upper-right corner while SteamVR assumes (0, 0) is upper-left and (1, 1) is lower-right. + // To accommodate this, we need to flip the y-coordinate before passing it to RenderManager and flip it again before returning the value to SteamVR. + OSVR_LOG(trace) << "OSVRTrackedHMD::ComputeDistortion(" << eye << ", " << u << ", " << v << ") called."; + + using osvr::renderkit::DistortionCorrectTextureCoordinate; + static const size_t COLOR_RED = 0; + static const size_t COLOR_GREEN = 1; + static const size_t COLOR_BLUE = 2; + + const auto osvr_eye = static_cast(eye); + const auto distortion_parameters = distortionParameters_[osvr_eye]; + const auto in_coords = osvr::renderkit::Float2 {{u, 1.0f - v}}; // flip v-coordinate + + auto interpolators = &leftEyeInterpolators_; + if (vr::Eye_Right == eye) { + interpolators = &rightEyeInterpolators_; + } + + auto coords_red = DistortionCorrectTextureCoordinate( + osvr_eye, in_coords, distortion_parameters, + COLOR_RED, overfillFactor_, *interpolators); + + auto coords_green = DistortionCorrectTextureCoordinate( + osvr_eye, in_coords, distortion_parameters, + COLOR_GREEN, overfillFactor_, *interpolators); + + auto coords_blue = DistortionCorrectTextureCoordinate( + osvr_eye, in_coords, distortion_parameters, + COLOR_BLUE, overfillFactor_, *interpolators); + + vr::DistortionCoordinates_t coords; + // flip v-coordinates again + coords.rfRed[0] = coords_red[0]; + coords.rfRed[1] = 1.0f - coords_red[1]; + coords.rfGreen[0] = coords_green[0]; + coords.rfGreen[1] = 1.0f - coords_green[1]; + coords.rfBlue[0] = coords_blue[0]; + coords.rfBlue[1] = 1.0f - coords_blue[1]; + + return coords; +} + +void OSVRTrackedHMD::HmdTrackerCallback(void* userdata, const OSVR_TimeValue*, const OSVR_PoseReport* report) +{ + if (!userdata) + return; + + auto* self = static_cast(userdata); + + vr::DriverPose_t pose; + pose.poseTimeOffset = 0; // close enough + + Eigen::Vector3d::Map(pose.vecWorldFromDriverTranslation) = Eigen::Vector3d::Zero(); + Eigen::Vector3d::Map(pose.vecDriverFromHeadTranslation) = Eigen::Vector3d::Zero(); + + map(pose.qWorldFromDriverRotation) = Eigen::Quaterniond::Identity(); + + map(pose.qDriverFromHeadRotation) = Eigen::Quaterniond::Identity(); + + // Position + Eigen::Vector3d::Map(pose.vecPosition) = osvr::util::vecMap(report->pose.translation); + + // Position velocity and acceleration are not currently consistently provided + Eigen::Vector3d::Map(pose.vecVelocity) = Eigen::Vector3d::Zero(); + Eigen::Vector3d::Map(pose.vecAcceleration) = Eigen::Vector3d::Zero(); + + // Orientation + map(pose.qRotation) = osvr::util::fromQuat(report->pose.rotation); + + // Angular velocity and acceleration are not currently consistently provided + Eigen::Vector3d::Map(pose.vecAngularVelocity) = Eigen::Vector3d::Zero(); + Eigen::Vector3d::Map(pose.vecAngularAcceleration) = Eigen::Vector3d::Zero(); + + pose.result = vr::TrackingResult_Running_OK; + pose.poseIsValid = true; + pose.willDriftInYaw = true; + pose.shouldApplyHeadModel = true; + + self->pose_ = pose; + self->driverHost_->TrackedDevicePoseUpdated(self->objectId_, self->pose_); +} + +float OSVRTrackedHMD::GetIPD() +{ + OSVR_Pose3 leftEye, rightEye; + + if (displayConfig_.getViewer(0).getEye(0).getPose(leftEye) != true) { + OSVR_LOG(err) << "OSVRTrackedHMD::GetHeadFromEyePose(): Unable to get left eye pose!\n"; + } + + if (displayConfig_.getViewer(0).getEye(1).getPose(rightEye) != true) { + OSVR_LOG(err) << "OSVRTrackedHMD::GetHeadFromEyePose(): Unable to get right eye pose!\n"; + } + + float ipd = static_cast((osvr::util::vecMap(leftEye.translation) - osvr::util::vecMap(rightEye.translation)).norm()); + return ipd; +} + +void OSVRTrackedHMD::configure() +{ + // The name of the display we want to use + const std::string display_name = settings_->getSetting("displayName", "OSVR"); + + // Detect displays and find the one we're using as an HMD + bool display_found = false; + auto displays = osvr::display::getDisplays(); + for (const auto& display : displays) { + if (std::string::npos == display.name.find(display_name)) + continue; + + display_ = display; + display_found = true; + break; + } + + if (!display_found) { + // Default to OSVR HDK display settings + display_.adapter.description = "Unknown"; + display_.name = "OSVR HDK"; + display_.size.width = 1920; + display_.size.height = 1080; + display_.position.x = 1920; + display_.position.y = 0; + display_.rotation = osvr::display::Rotation::Zero; + display_.verticalRefreshRate = 60.0; + display_.attachedToDesktop = true; + display_.edidVendorId = 0xd24e;// 53838 + display_.edidProductId = 0x1019; // 4121 + } + + if (display_found) { + OSVR_LOG(info) << "Detected display named [" << display_.name << "]:"; + } else { + OSVR_LOG(info) << "Default display:"; + } + OSVR_LOG(info) << " Adapter: " << display_.adapter.description; + OSVR_LOG(info) << " Monitor name: " << display_.name; + OSVR_LOG(info) << " Resolution: " << display_.size.width << "x" << display_.size.height; + OSVR_LOG(info) << " Position: (" << display_.position.x << ", " << display_.position.y << ")"; + switch (display_.rotation) { + case osvr::display::Rotation::Zero: + OSVR_LOG(info) << " Rotation: Landscape"; + break; + case osvr::display::Rotation::Ninety: + OSVR_LOG(info) << " Rotation: Portrait"; + break; + case osvr::display::Rotation::OneEighty: + OSVR_LOG(info) << " Rotation: Landscape (flipped)"; + break; + case osvr::display::Rotation::TwoSeventy: + OSVR_LOG(info) << " Rotation: Portrait (flipped)"; + break; + default: + OSVR_LOG(info) << " Rotation: Landscape"; + break; + } + OSVR_LOG(info) << " Refresh rate: " << display_.verticalRefreshRate; + OSVR_LOG(info) << " " << (display_.attachedToDesktop ? "Extended mode" : "Direct mode"); + OSVR_LOG(info) << " EDID vendor ID: " << display_.edidVendorId; + OSVR_LOG(info) << " EDID product ID: " << display_.edidProductId; +} + +void OSVRTrackedHMD::configureDistortionParameters() +{ + // Parse the display descriptor + displayDescription_ = context_.getStringParameter("/display"); + displayConfiguration_ = OSVRDisplayConfiguration(displayDescription_); + + // Initialize the distortion parameters + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Number of eyes: " << displayConfiguration_.getEyes().size() << "."; + for (size_t i = 0; i < displayConfiguration_.getEyes().size(); ++i) { + auto distortion = osvr::renderkit::DistortionParameters { displayConfiguration_, i }; + distortion.m_desiredTriangles = 200 * 64; + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Adding distortion for eye " << i << "."; + distortionParameters_.push_back(distortion); + } + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Number of distortion parameters: " << distortionParameters_.size() << "."; + + // Make the interpolators to be used by each eye. + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Creating mesh interpolators for the left eye."; + if (!makeUnstructuredMeshInterpolators(distortionParameters_[0], 0, leftEyeInterpolators_)) { + OSVR_LOG(err) << "OSVRTrackedHMD::configureDistortionParameters(): Could not create mesh interpolators for left eye."; + } + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Number of left eye interpolators: " << leftEyeInterpolators_.size() << "."; + + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Creating mesh interpolators for the right eye."; + if (!makeUnstructuredMeshInterpolators(distortionParameters_[1], 1, rightEyeInterpolators_)) { + OSVR_LOG(err) << "OSVRTrackedHMD::configureDistortionParameters(): Could not create mesh interpolators for right eye."; + } + OSVR_LOG(debug) << "OSVRTrackedHMD::configureDistortionParameters(): Number of right eye interpolators: " << leftEyeInterpolators_.size() << "."; +} + +void OSVRTrackedHMD::configureProperties() +{ + // General properties that apply to all device classes + + properties_[vr::Prop_WillDriftInYaw_Bool] = true; + properties_[vr::Prop_DeviceIsWireless_Bool] = false; + properties_[vr::Prop_DeviceIsCharging_Bool] = false; + properties_[vr::Prop_Firmware_UpdateAvailable_Bool] = false; + properties_[vr::Prop_Firmware_ManualUpdate_Bool] = false; + properties_[vr::Prop_BlockServerShutdown_Bool] = false; + //properties_[vr::Prop_CanUnifyCoordinateSystemWithHmd_Bool] = true; + properties_[vr::Prop_ContainsProximitySensor_Bool] = false; + properties_[vr::Prop_DeviceProvidesBatteryStatus_Bool] = false; + properties_[vr::Prop_DeviceCanPowerOff_Bool] = true; + properties_[vr::Prop_HasCamera_Bool] = false; + + properties_[vr::Prop_DeviceBatteryPercentage_Float] = 1.0f; // full battery + + properties_[vr::Prop_DeviceClass_Int32] = deviceClass_; + + //properties_[vr::Prop_HardwareRevision_Uint64] = 0ul; + //properties_[vr::Prop_FirmwareVersion_Uint64] = 0ul; + //properties_[vr::Prop_FPGAVersion_Uint64] = 0ul; + //properties_[vr::Prop_VRCVersion_Uint64] = 0ul; + //properties_[vr::Prop_RadioVersion_Uint64] = 0ul; + //properties_[vr::Prop_DongleVersion_Uint64] = 0ul; + + //properties_[vr::Prop_StatusDisplayTransform_Matrix34] = /* TODO */ + + //properties_[vr::Prop_TrackingSystemName_String] = ""; + properties_[vr::Prop_ModelNumber_String] = "OSVR HMD"; + properties_[vr::Prop_SerialNumber_String] = display_.name; + //properties_[vr::Prop_RenderModelName_String] = ""; + //properties_[vr::Prop_ManufacturerName_String] = ""; + //properties_[vr::Prop_TrackingFirmwareVersion_String] = ""; + //properties_[vr::Prop_HardwareRevision_String] = ""; + //properties_[vr::Prop_AllWirelessDongleDescriptions_String] = ""; + //properties_[vr::Prop_ConnectedWirelessDongle_String] = ""; + //properties_[vr::Prop_Firmware_ManualUpdateURL_String] = ""; + //properties_[vr::Prop_Firmware_ProgrammingTarget_String] = ""; + //properties_[vr::Prop_DriverVersion_String] = ""; + + + // Properties that apply to HMDs + + //properties_[vr::Prop_ReportsTimeSinceVSync_Bool] = false; + properties_[vr::Prop_IsOnDesktop_Bool] = IsDisplayOnDesktop(); + + //properties_[vr::Prop_SecondsFromVsyncToPhotons_Float] = 0.0; + properties_[vr::Prop_DisplayFrequency_Float] = static_cast(display_.verticalRefreshRate); + properties_[vr::Prop_UserIpdMeters_Float] = GetIPD(); + //properties_[vr::Prop_DisplayMCOffset_Float] = 0.0; + //properties_[vr::Prop_DisplayMCScale_Float] = 0.0; + //properties_[vr::Prop_DisplayGCBlackClamp_Float] = 0.0; + //properties_[vr::Prop_DisplayGCOffset_Float] = 0.0; + //properties_[vr::Prop_DisplayGCScale_Float] = 0.0; + //properties_[vr::Prop_DisplayGCPrescale_Float] = 0.0; + //properties_[vr::Prop_LensCenterLeftU_Float] = 0.0; + //properties_[vr::Prop_LensCenterLeftV_Float] = 0.0; + //properties_[vr::Prop_LensCenterRightU_Float] = 0.0; + //properties_[vr::Prop_LensCenterRightV_Float] = 0.0; + //properties_[vr::Prop_UserHeadToEyeDepthMeters_Float] = 0.0; + + //properties_[vr::Prop_DisplayMCType_Int32] = 0; + properties_[vr::Prop_EdidVendorID_Int32] = static_cast(display_.edidVendorId); + properties_[vr::Prop_EdidProductID_Int32] = static_cast(display_.edidProductId); + //properties_[vr::Prop_DisplayGCType_Int32] = 0; + //properties_[vr::Prop_CameraCompatibilityMode_Int32] = 0; + + properties_[vr::Prop_CurrentUniverseId_Uint64] = 1; + properties_[vr::Prop_PreviousUniverseId_Uint64] = 1; + properties_[vr::Prop_DisplayFirmwareVersion_Uint64] = 192; // FIXME read from OSVR server + //properties_[vr::Prop_CameraFirmwareVersion_Uint64] = 0ul; + //properties_[vr::Prop_DisplayFPGAVersion_Uint64] = 0ul; + //properties_[vr::Prop_DisplayBootloaderVersion_Uint64] = 0ul; + //properties_[vr::Prop_DisplayHardwareVersion_Uint64] = 0ul; + //properties_[vr::Prop_AudioFirmwareVersion_Uint64] = 0ul; + + //properties_[vr::Prop_CameraToHeadTransform_Matrix34] = /* TODO */ + + //properties_[vr::Prop_DisplayMCImageLeft_String] = ""; + //properties_[vr::Prop_DisplayMCImageRight_String] = ""; + //properties_[vr::Prop_DisplayGCImage_String] = ""; + //properties_[vr::Prop_CameraFirmwareDescription_String] = ""; +} + diff --git a/src/OSVRTrackedHMD.h b/src/OSVRTrackedHMD.h new file mode 100644 index 0000000..96125bb --- /dev/null +++ b/src/OSVRTrackedHMD.h @@ -0,0 +1,144 @@ +/** @file + @brief OSVR tracked device + + @date 2015 + + @author + Sensics, Inc. + +*/ + +// Copyright 2015 Sensics, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef INCLUDED_OSVRTrackedHMD_h_GUID_233AC6EA_4833_4EE2_B4ED_1F60A2208C9D +#define INCLUDED_OSVRTrackedHMD_h_GUID_233AC6EA_4833_4EE2_B4ED_1F60A2208C9D + +// Internal Includes +#include "OSVRTrackedDevice.h" +#include "display/Display.h" + +// Library/third-party includes +#include + +#include +#include +#include +#include +#include + +// Standard includes +#include +#include +#include + +class OSVRTrackedHMD : public OSVRTrackedDevice, public vr::IVRDisplayComponent { +friend class ServerDriver_OSVR; +public: + OSVRTrackedHMD(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host); + + virtual ~OSVRTrackedHMD(); + + // ------------------------------------ + // Management Methods + // ------------------------------------ + + virtual vr::EVRInitError Activate(uint32_t object_id) OSVR_OVERRIDE; + virtual void Deactivate() OSVR_OVERRIDE; + + // ------------------------------------ + // Display Methods + // ------------------------------------ + + /** + * Size and position that the window needs to be on the VR display. + */ + virtual void GetWindowBounds(int32_t* x, int32_t* y, uint32_t* width, uint32_t* height) OSVR_OVERRIDE; + + /** + * Returns true if the display is extending the desktop. + */ + virtual bool IsDisplayOnDesktop() OSVR_OVERRIDE; + + /** + * Returns true if the display is real and not a fictional display. + */ + virtual bool IsDisplayRealDisplay() OSVR_OVERRIDE; + + /** + * Suggested size for the intermediate render target that the distortion + * pulls from. + */ + virtual void GetRecommendedRenderTargetSize(uint32_t* width, uint32_t* height) OSVR_OVERRIDE; + + /** + * Gets the viewport in the frame buffer to draw the output of the distortion + * into + */ + virtual void GetEyeOutputViewport(vr::EVREye eye, uint32_t* x, uint32_t* y, uint32_t* width, uint32_t* height) OSVR_OVERRIDE; + + /** + * The components necessary to build your own projection matrix in case your + * application is doing something fancy like infinite Z + */ + virtual void GetProjectionRaw(vr::EVREye eye, float* left, float* right, float* top, float* bottom) OSVR_OVERRIDE; + + /** + * Returns the result of the distortion function for the specified eye and + * input UVs. UVs go from 0,0 in the upper left of that eye's viewport and + * 1,1 in the lower right of that eye's viewport. + */ + virtual vr::DistortionCoordinates_t ComputeDistortion(vr::EVREye eye, float u, float v) OSVR_OVERRIDE; + +private: + /** + * Callback function which is called whenever new data has been received + * from the tracker. + */ + static void HmdTrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report); + + float GetIPD(); + + /** + * Read configuration settings from configuration file. + */ + void configure(); + + /** + * Configure RenderManager and distortion parameters. + */ + void configureDistortionParameters(); + + void configureProperties(); + + std::string displayDescription_; + osvr::clientkit::DisplayConfig displayConfig_; + osvr::client::RenderManagerConfig renderManagerConfig_; + osvr::clientkit::Interface trackerInterface_; + std::vector distortionParameters_; + OSVRDisplayConfiguration displayConfiguration_; + + // per-eye mesh interpolators + using MeshInterpolators = std::vector>; + MeshInterpolators leftEyeInterpolators_; + MeshInterpolators rightEyeInterpolators_; + + float overfillFactor_ = 1.0; // TODO get from RenderManager + + // Settings + osvr::display::Display display_ = {}; +}; + +#endif // INCLUDED_OSVRTrackedHMD_h_GUID_233AC6EA_4833_4EE2_B4ED_1F60A2208C9D + diff --git a/src/OSVRTrackingReference.cpp b/src/OSVRTrackingReference.cpp index 4d43e11..51995f8 100644 --- a/src/OSVRTrackingReference.cpp +++ b/src/OSVRTrackingReference.cpp @@ -29,7 +29,6 @@ #include "osvr_compiler_detection.h" #include "make_unique.h" #include "matrix_cast.h" -#include "osvr_device_properties.h" #include "ValveStrCpy.h" #include "platform_fixes.h" // strcasecmp #include "make_unique.h" @@ -49,19 +48,15 @@ #include #include -OSVRTrackingReference::OSVRTrackingReference(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::IDriverLog* driver_log) : m_Context(context), driver_host_(driver_host), pose_(), deviceClass_(vr::TrackedDeviceClass_TrackingReference) +OSVRTrackingReference::OSVRTrackingReference(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host) : OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_TrackingReference) { OSVR_LOG(trace) << "OSVRTrackingReference::OSVRTrackingReference() called."; - settings_ = std::make_unique(driver_host->GetSettings(vr::IVRSettings_Version)); - if (driver_log) { - Logging::instance().setDriverLog(driver_log); - } configure(); } OSVRTrackingReference::~OSVRTrackingReference() { - driver_host_ = nullptr; + // do nothing } vr::EVRInitError OSVRTrackingReference::Activate(uint32_t object_id) @@ -75,7 +70,7 @@ vr::EVRInitError OSVRTrackingReference::Activate(uint32_t object_id) } // Register tracker callback - m_TrackerInterface = m_Context.getInterface(trackerPath_); + m_TrackerInterface = context_.getInterface(trackerPath_); m_TrackerInterface.registerCallback(&OSVRTrackingReference::TrackerCallback, this); return vr::VRInitError_None; @@ -91,460 +86,6 @@ void OSVRTrackingReference::Deactivate() } } -void OSVRTrackingReference::PowerOff() -{ - // do nothing -} - -void* OSVRTrackingReference::GetComponent(const char* component_name_and_version) -{ - if (!strcasecmp(component_name_and_version, vr::ITrackedDeviceServerDriver_Version)) { - return static_cast(this); - } - - return nullptr; -} - -void OSVRTrackingReference::DebugRequest(const char* request, char* response_buffer, uint32_t response_buffer_size) -{ - // TODO - // make use of (from vrtypes.h) static const uint32_t k_unMaxDriverDebugResponseSize = 32768; - // return empty string for now - if (response_buffer_size > 0) { - response_buffer[0] = '\0'; - } -} - -vr::DriverPose_t OSVRTrackingReference::GetPose() -{ - return pose_; -} - -bool OSVRTrackingReference::GetBoolTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) -{ - const bool default_value = false; - - if (isWrongDataType(prop, bool())) { - if (error) - *error = vr::TrackedProp_WrongDataType; - return default_value; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - if (error) - *error = vr::TrackedProp_WrongDeviceClass; - return default_value; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - if (error) - *error = vr::TrackedProp_InvalidDevice; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - //OSVR_LOG(trace) << "OSVRTrackingReference::GetBoolTrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // Properties that apply to all device classes - case vr::Prop_WillDriftInYaw_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_DeviceIsWireless_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_DeviceIsCharging_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_Firmware_UpdateAvailable_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_Firmware_ManualUpdate_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_BlockServerShutdown_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_CanUnifyCoordinateSystemWithHmd_Bool: // TODO - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - break; - case vr::Prop_ContainsProximitySensor_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_DeviceProvidesBatteryStatus_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_DeviceCanPowerOff_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - case vr::Prop_HasCamera_Bool: - if (error) - *error = vr::TrackedProp_Success; - return false; - break; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackingReference::GetBoolTrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - -float OSVRTrackingReference::GetFloatTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) -{ - const float default_value = 0.0f; - - if (isWrongDataType(prop, float())) { - if (error) - *error = vr::TrackedProp_WrongDataType; - return default_value; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - if (error) - *error = vr::TrackedProp_WrongDeviceClass; - return default_value; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - if (error) - *error = vr::TrackedProp_InvalidDevice; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackingReference::GetFloatTrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_DeviceBatteryPercentage_Float: - if (error) - *error = vr::TrackedProp_Success; - return 1.0f; // full battery - // Properties that are unique to TrackedDeviceClass_TrackingReference - case vr::Prop_FieldOfViewLeftDegrees_Float: - if (error) - *error = vr::TrackedProp_Success; - return fovLeft_; - case vr::Prop_FieldOfViewRightDegrees_Float: - if (error) - *error = vr::TrackedProp_Success; - return fovRight_; - case vr::Prop_FieldOfViewTopDegrees_Float: - if (error) - *error = vr::TrackedProp_Success; - return fovTop_; - case vr::Prop_FieldOfViewBottomDegrees_Float: - if (error) - *error = vr::TrackedProp_Success; - return fovBottom_; - case vr::Prop_TrackingRangeMinimumMeters_Float: - if (error) - *error = vr::TrackedProp_Success; - return minTrackingRange_; - case vr::Prop_TrackingRangeMaximumMeters_Float: - if (error) - *error = vr::TrackedProp_Success; - return maxTrackingRange_; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackingReference::GetFloatTrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - -int32_t OSVRTrackingReference::GetInt32TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) -{ - const int32_t default_value = 0; - - if (isWrongDataType(prop, int32_t())) { - if (error) - *error = vr::TrackedProp_WrongDataType; - return default_value; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - if (error) - *error = vr::TrackedProp_WrongDeviceClass; - return default_value; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - if (error) - *error = vr::TrackedProp_InvalidDevice; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackingReference::GetInt32TrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_DeviceClass_Int32: - if (error) - *error = vr::TrackedProp_Success; - return deviceClass_; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackingReference::GetInt32TrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - -uint64_t OSVRTrackingReference::GetUint64TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) -{ - const uint64_t default_value = 0; - - if (isWrongDataType(prop, uint64_t())) { - if (error) - *error = vr::TrackedProp_WrongDataType; - return default_value; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - if (error) - *error = vr::TrackedProp_WrongDeviceClass; - return default_value; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - if (error) - *error = vr::TrackedProp_InvalidDevice; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackingReference::GetUint64TrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_HardwareRevision_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FirmwareVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_FPGAVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_VRCVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_RadioVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DongleVersion_Uint64: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackingReference::GetUint64TrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - -vr::HmdMatrix34_t OSVRTrackingReference::GetMatrix34TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) -{ - // Default value is identity matrix - vr::HmdMatrix34_t default_value; - map(default_value) = Matrix34f::Identity(); - - if (isWrongDataType(prop, vr::HmdMatrix34_t())) { - if (error) - *error = vr::TrackedProp_WrongDataType; - return default_value; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - if (error) - *error = vr::TrackedProp_WrongDeviceClass; - return default_value; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - if (error) - *error = vr::TrackedProp_InvalidDevice; - return default_value; - } - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - OSVR_LOG(trace) << "OSVRTrackingReference::GetMatrix34TrackedDeviceProperty(): Requested property: " << prop << "\n"; - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_StatusDisplayTransform_Matrix34: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackingReference::GetMatrix34TrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - -uint32_t OSVRTrackingReference::GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, char *pchValue, uint32_t unBufferSize, vr::ETrackedPropertyError *pError) -{ - uint32_t default_value = 0; - if (isWrongDataType(prop, pchValue)) { - if (pError) - *pError = vr::TrackedProp_WrongDataType; - return default_value; - } - - if (isWrongDeviceClass(prop, deviceClass_)) { - if (pError) - *pError = vr::TrackedProp_WrongDeviceClass; - return default_value; - } - - if (vr::TrackedDeviceClass_Invalid == deviceClass_) { - if (pError) - *pError = vr::TrackedProp_InvalidDevice; - return default_value; - } - - OSVR_LOG(trace) << "OSVRTrackingReference::GetStringTrackedDeviceProperty(): Requested property: " << prop << "\n"; - - std::string sValue = GetStringTrackedDeviceProperty(prop, pError); - if (*pError == vr::TrackedProp_Success) { - if (sValue.size() + 1 > unBufferSize) { - *pError = vr::TrackedProp_BufferTooSmall; - } else { - valveStrCpy(sValue, pchValue, unBufferSize); - } - return static_cast(sValue.size()) + 1; - } - - return 0; -} - - // ------------------------------------ - // Private Methods - // ------------------------------------ - -std::string OSVRTrackingReference::GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError *error) -{ - std::string default_value = ""; - -#include "ignore-warning/push" -#include "ignore-warning/switch-enum" - - switch (prop) { - // General properties that apply to all device classes - case vr::Prop_TrackingSystemName_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_ModelNumber_String: - if (error) - *error = vr::TrackedProp_Success; - return "OSVR HMD"; - case vr::Prop_SerialNumber_String: - if (error) - *error = vr::TrackedProp_Success; - return this->GetId(); - case vr::Prop_RenderModelName_String: - if (error) - *error = vr::TrackedProp_Success; - return "dk2_camera"; // FIXME replace with HDK IR camera model - case vr::Prop_ManufacturerName_String: - if (error) - *error = vr::TrackedProp_Success; - return "OSVR"; // FIXME read value from server - case vr::Prop_TrackingFirmwareVersion_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_HardwareRevision_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_AllWirelessDongleDescriptions_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_ConnectedWirelessDongle_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Firmware_ManualUpdateURL_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_Firmware_ProgrammingTarget_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - case vr::Prop_DriverVersion_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - // Properties that are unique to TrackedDeviceClass_TrackingReference - case vr::Prop_ModeLabel_String: - if (error) - *error = vr::TrackedProp_ValueNotProvidedByDevice; - return default_value; - } - -#include "ignore-warning/pop" - - OSVR_LOG(warn) << "OSVRTrackingReference::GetStringTrackedDeviceProperty(): Unknown property " << prop << " requested.\n"; - if (error) - *error = vr::TrackedProp_UnknownProperty; - return default_value; -} - const char* OSVRTrackingReference::GetId() { return "OSVR IR camera"; @@ -583,21 +124,11 @@ void OSVRTrackingReference::TrackerCallback(void* userdata, const OSVR_TimeValue pose.willDriftInYaw = false; pose.shouldApplyHeadModel = false; self->pose_ = pose; - self->driver_host_->TrackedDevicePoseUpdated(self->objectId_, self->pose_); + self->driverHost_->TrackedDevicePoseUpdated(self->objectId_, self->pose_); } void OSVRTrackingReference::configure() { - // Get settings from config file - const bool verbose_logging = settings_->getSetting("verbose", verboseLogging_); - if (verbose_logging) { - OSVR_LOG(info) << "Verbose logging enabled."; - Logging::instance().setLogLevel(trace); - } else { - OSVR_LOG(info) << "Verbose logging disabled."; - Logging::instance().setLogLevel(info); - } - // Read tracking reference values from config file trackerPath_ = settings_->getSetting("cameraPath", trackerPath_); fovLeft_ = settings_->getSetting("cameraFOVLeftDegrees", fovLeft_); @@ -606,5 +137,54 @@ void OSVRTrackingReference::configure() fovBottom_ = settings_->getSetting("cameraFOVBottomDegrees", fovBottom_); minTrackingRange_ = settings_->getSetting("minTrackingRangeMeters", minTrackingRange_); maxTrackingRange_ = settings_->getSetting("maxTrackingRangeMeters", maxTrackingRange_); + + configureProperties(); } +void OSVRTrackingReference::configureProperties() +{ + // Properties that apply to all device classes + properties_[vr::Prop_WillDriftInYaw_Bool] = false; + properties_[vr::Prop_DeviceIsWireless_Bool] = false; + properties_[vr::Prop_DeviceIsCharging_Bool] = false; + properties_[vr::Prop_Firmware_UpdateAvailable_Bool] = false; + properties_[vr::Prop_Firmware_ManualUpdate_Bool] = false; + properties_[vr::Prop_BlockServerShutdown_Bool] = false; + //properties_[vr::Prop_CanUnifyCoordinateSystemWithHmd_Bool] = true; + properties_[vr::Prop_ContainsProximitySensor_Bool] = false; + properties_[vr::Prop_DeviceProvidesBatteryStatus_Bool] = false; + properties_[vr::Prop_DeviceCanPowerOff_Bool] = false; + properties_[vr::Prop_HasCamera_Bool] = false; + properties_[vr::Prop_DeviceBatteryPercentage_Float] = 1.0f; + properties_[vr::Prop_DeviceClass_Int32] = deviceClass_; + //properties_[vr::Prop_HardwareRevision_Uint64] = 0ul; + //properties_[vr::Prop_FirmwareVersion_Uint64] = 0ul; + //properties_[vr::Prop_FPGAVersion_Uint64] = 0ul; + //properties_[vr::Prop_VRCVersion_Uint64] = 0ul; + //properties_[vr::Prop_RadioVersion_Uint64] = 0ul; + //properties_[vr::Prop_DongleVersion_Uint64] = 0ul; + //properties_[vr::Prop_StatusDisplayTransform_Matrix34] = /* ... */; + //properties_[vr::Prop_TrackingSystemName_String] = ""; + properties_[vr::Prop_ModelNumber_String] = "OSVR Tracking Reference"; + properties_[vr::Prop_SerialNumber_String] = GetId(); + properties_[vr::Prop_RenderModelName_String] = "dk2_camera"; // FIXME replace with HDK IR camera model + properties_[vr::Prop_ManufacturerName_String] = "OSVR"; // FIXME read value from server + //properties_[vr::Prop_TrackingFirmwareVersion_String] = ""; + //properties_[vr::Prop_HardwareRevision_String] = ""; + //properties_[vr::Prop_AllWirelessDongleDescriptions_String] = ""; + //properties_[vr::Prop_ConnectedWirelessDongle_String] = ""; + //properties_[vr::Prop_Firmware_ManualUpdateURL_String] = ""; + //properties_[vr::Prop_Firmware_ProgrammingTarget_String] = ""; + //properties_[vr::Prop_DriverVersion_String] = ""; + + // Properties that are unique to TrackedDeviceClass_TrackingReference + properties_[vr::Prop_FieldOfViewLeftDegrees_Float] = fovLeft_; + properties_[vr::Prop_FieldOfViewRightDegrees_Float] = fovRight_; + properties_[vr::Prop_FieldOfViewTopDegrees_Float] = fovTop_; + properties_[vr::Prop_FieldOfViewBottomDegrees_Float] = fovBottom_; + properties_[vr::Prop_TrackingRangeMinimumMeters_Float] = minTrackingRange_; + properties_[vr::Prop_TrackingRangeMaximumMeters_Float] = maxTrackingRange_; + //properties_[vr::Prop_ModeLabel_String] = ""; +} + + diff --git a/src/OSVRTrackingReference.h b/src/OSVRTrackingReference.h index 308e3c3..a1ed1d8 100644 --- a/src/OSVRTrackingReference.h +++ b/src/OSVRTrackingReference.h @@ -26,7 +26,7 @@ #define INCLUDED_OSVRTrackingReference_h_GUID_250D4551_4054_9D37_A8F6_F2FCFDB1C188 // Internal Includes -#include "osvr_compiler_detection.h" // for OSVR_OVERRIDE +#include "OSVRTrackedDevice.h" #include "Settings.h" // OpenVR includes @@ -39,10 +39,10 @@ #include #include -class OSVRTrackingReference : public vr::ITrackedDeviceServerDriver { +class OSVRTrackingReference : public OSVRTrackedDevice { friend class ServerDriver_OSVR; public: - OSVRTrackingReference(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::IDriverLog* driver_log = nullptr); + OSVRTrackingReference(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host); virtual ~OSVRTrackingReference(); @@ -66,98 +66,21 @@ friend class ServerDriver_OSVR; */ virtual void Deactivate() OSVR_OVERRIDE; - /** - * Handles a request from the system to power off this device. - */ - virtual void PowerOff() OSVR_OVERRIDE; - - /** - * Requests a component interface of the driver for device-specific - * functionality. The driver should return NULL if the requested interface - * or version is not supported. - */ - virtual void* GetComponent(const char* component_name_and_version) OSVR_OVERRIDE; - - /** - * A VR Client has made this debug request of the driver. The set of valid - * requests is entirely up to the driver and the client to figure out, as is - * the format of the response. Responses that exceed the length of the - * supplied buffer should be truncated and null terminated. - */ - virtual void DebugRequest(const char* request, char* response_buffer, uint32_t response_buffer_size) OSVR_OVERRIDE; - - // ------------------------------------ - // Tracking Methods - // ------------------------------------ - virtual vr::DriverPose_t GetPose() OSVR_OVERRIDE; - - // ------------------------------------ - // Property Methods - // ------------------------------------ - - /** - * Returns a bool property. If the property is not available this function - * will return false. - */ - virtual bool GetBoolTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; - - /** - * Returns a float property. If the property is not available this function - * will return 0. - */ - virtual float GetFloatTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; - - /** - * Returns an int property. If the property is not available this function - * will return 0. - */ - virtual int32_t GetInt32TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; - - /** - * Returns a uint64 property. If the property is not available this function - * will return 0. - */ - virtual uint64_t GetUint64TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; - - /** - * Returns a matrix property. If the device index is not valid or the - * property is not a matrix type, this function will return identity. - */ - virtual vr::HmdMatrix34_t GetMatrix34TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; - - /** - * Returns a string property. If the property is not available this function - * will return 0 and @p error will be set to an error. Otherwise it returns - * the length of the number of bytes necessary to hold this string including - * the trailing null. If the buffer is too small the error will be - * @c TrackedProp_BufferTooSmall. Strings will generally fit in buffers of - * @c k_unTrackingStringSize characters. Drivers may not return strings longer - * than @c k_unMaxPropertyStringSize. - */ - virtual uint32_t GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, char* value, uint32_t buffer_size, vr::ETrackedPropertyError* error) OSVR_OVERRIDE; - protected: const char* GetId(); private: - std::string GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError *error); static void TrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report); /** * Read configuration settings from configuration file. */ void configure(); + void configureProperties(); - osvr::clientkit::ClientContext& m_Context; - vr::IServerDriverHost* driver_host_ = nullptr; osvr::clientkit::Interface m_TrackerInterface; - vr::DriverPose_t pose_; - vr::ETrackedDeviceClass deviceClass_; - std::unique_ptr settings_; - uint32_t objectId_ = 0; // Settings - bool verboseLogging_ = false; std::string trackerPath_ = "/org_osvr_filter_videoimufusion/HeadFusion/semantic/camera"; // Default values are those for the OSVR HDK IR camera diff --git a/src/PropertyMap.h b/src/PropertyMap.h new file mode 100644 index 0000000..4e642cd --- /dev/null +++ b/src/PropertyMap.h @@ -0,0 +1,47 @@ +/** @file + @brief Header + + @date 2016 + + @author + Sensics, Inc. + + +*/ + +// Copyright 2016 Sensics, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef INCLUDED_PropertyMap_h_GUID_D9C1D812_9B04_485C_BD5D_4316974D0043 +#define INCLUDED_PropertyMap_h_GUID_D9C1D812_9B04_485C_BD5D_4316974D0043 + +// Internal Includes +// - none + +// Library/third-party includes +#include + +#include + +// Standard includes +#include +#include +#include + +using Property = boost::variant; + +using PropertyMap = std::map; + +#endif // INCLUDED_PropertyMap_h_GUID_D9C1D812_9B04_485C_BD5D_4316974D0043 + diff --git a/src/osvr_device_properties.h b/src/PropertyProperties.h similarity index 98% rename from src/osvr_device_properties.h rename to src/PropertyProperties.h index 9865ec6..4fcad36 100644 --- a/src/osvr_device_properties.h +++ b/src/PropertyProperties.h @@ -22,8 +22,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef INCLUDED_osvr_device_properties_h_GUID_5212DE9D_B211_4139_A140_45A578EFA47E -#define INCLUDED_osvr_device_properties_h_GUID_5212DE9D_B211_4139_A140_45A578EFA47E +#ifndef INCLUDED_PropertyProperties_h_GUID_5212DE9D_B211_4139_A140_45A578EFA47E +#define INCLUDED_PropertyProperties_h_GUID_5212DE9D_B211_4139_A140_45A578EFA47E // Internal Includes // - none @@ -546,6 +546,11 @@ inline bool isWrongDataType(vr::ETrackedDeviceProperty prop, const char*) return true; } +inline bool isWrongDataType(vr::ETrackedDeviceProperty prop, std::string) +{ + return isWrongDataType(prop, ""); +} + inline bool isWrongDataType(vr::ETrackedDeviceProperty prop, vr::HmdMatrix34_t) { switch (prop) { @@ -756,5 +761,5 @@ inline bool isWrongDeviceClass(vr::ETrackedDeviceProperty prop, vr::ETrackedDevi return true; } -#endif // INCLUDED_osvr_device_properties_h_GUID_5212DE9D_B211_4139_A140_45A578EFA47E +#endif // INCLUDED_PropertyProperties_h_GUID_5212DE9D_B211_4139_A140_45A578EFA47E diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index d46ec24..05c3e42 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -25,7 +25,7 @@ // Internal Includes #include "ServerDriver_OSVR.h" -#include "OSVRTrackedDevice.h" // for OSVRTrackedDevice +#include "OSVRTrackedHMD.h" // for OSVRTrackedHMD #include "OSVRTrackingReference.h" // for OSVRTrackingReference #include "platform_fixes.h" // strcasecmp #include "make_unique.h" // for std::make_unique @@ -49,7 +49,7 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IDriverLog* driver_log, vr::IServer context_ = std::make_unique("org.osvr.SteamVR"); - trackedDevices_.emplace_back(std::make_unique(*(context_.get()), driver_host)); + trackedDevices_.emplace_back(std::make_unique(*(context_.get()), driver_host)); trackedDevices_.emplace_back(std::make_unique(*(context_.get()), driver_host)); return vr::VRInitError_None; From a4fc526f17a4aeb71df5f6cd24bad3ff305f89a6 Mon Sep 17 00:00:00 2001 From: "Kevin M. Godby" Date: Tue, 5 Jul 2016 18:00:15 -0500 Subject: [PATCH 02/24] Removed obsolete code. --- src/OSVRTrackedDevice.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/OSVRTrackedDevice.h b/src/OSVRTrackedDevice.h index 599c1e4..19f50c2 100644 --- a/src/OSVRTrackedDevice.h +++ b/src/OSVRTrackedDevice.h @@ -174,19 +174,10 @@ friend class ServerDriver_OSVR; /** \name Collections of properties and their values. */ //@{ - //std::map boolProperties_; - //std::map floatProperties_; - //std::map int32Properties_; - //std::map uint64Properties_; - //std::map matrix34Properties; - //std::map uint32Properties_; - //std::map stringProperties_; PropertyMap properties_; //@} }; -//template -//inline T OSVRTrackedDevice::GetTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error, const std::map& map, const T& default_value) template inline T OSVRTrackedDevice::GetTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error, const T& default_value) { From 5a4a62f8eb945e048da7d9dc134976f3df00c399 Mon Sep 17 00:00:00 2001 From: "Kevin M. Godby" Date: Tue, 5 Jul 2016 20:59:45 -0500 Subject: [PATCH 03/24] Initial import of controller support. --- src/CMakeLists.txt | 2 + src/OSVRTrackedController.cpp | 392 ++++++++++++++++++++++++++++++++++ src/OSVRTrackedController.h | 135 ++++++++++++ src/OSVRTrackedHMD.cpp | 2 +- src/OSVRTrackingReference.cpp | 2 +- 5 files changed, 531 insertions(+), 2 deletions(-) create mode 100644 src/OSVRTrackedController.cpp create mode 100644 src/OSVRTrackedController.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 202174a..8feb5f1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,6 +40,8 @@ add_library(driver_osvr Logging.h OSVRTrackedDevice.cpp OSVRTrackedDevice.h + OSVRTrackedController.cpp + OSVRTrackedController.h OSVRTrackedHMD.cpp OSVRTrackedHMD.h OSVRTrackingReference.cpp diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp new file mode 100644 index 0000000..288b237 --- /dev/null +++ b/src/OSVRTrackedController.cpp @@ -0,0 +1,392 @@ +/** @file + @brief OSVR tracked controller + + @date 2015 + + @author + Sensics, Inc. + +*/ + +// Copyright 2015 Sensics, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Internal Includes +#include "OSVRTrackedController.h" + +#include "osvr_compiler_detection.h" +#include "make_unique.h" +#include "matrix_cast.h" +#include "ValveStrCpy.h" +#include "platform_fixes.h" // strcasecmp +#include "Logging.h" + +// OpenVR includes +#include + +// Library/third-party includes +#include +#include +#include +#include + +// Standard includes +#include +#include +#include +#include +#include + +// TODO: +// Trackpad +// OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::MotionController_Left_Thumbstick, "/controller/left/joystick/button"), +// OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::MotionController_Left_Shoulder, "/controller/left/bumper"), +// OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::SpecialLeft, "/controller/left/middle"), + +OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, int controller_index) : OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_Controller), controllerIndex_(controller_index) +{ + controllerName_ = "OSVRController" + std::to_string(controller_index); + + numAxis_ = 0; + for (int iter_axis = 0; iter_axis < NUM_AXIS; iter_axis++) { + analogInterface_[iter_axis].parentController = this; + analogInterface_[iter_axis].axisType = vr::EVRControllerAxisType::k_eControllerAxis_None; + } +} + +OSVRTrackedController::~OSVRTrackedController() +{ + // do nothing +} + +vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) +{ + OSVRTrackedDevice::Activate(object_id); + + const std::time_t wait_time = 5; // wait up to 5 seconds for init + + freeInterfaces(); + numAxis_ = 0; + + // Ensure context is fully started up + OSVR_LOG(info) << "Waiting for the context to fully start up..."; + const auto start_time = std::time(nullptr); + while (!context_.checkStatus()) { + context_.update(); + if (std::time(nullptr) > start_time + wait_time) { + OSVR_LOG(warn) << "Context startup timed out!"; + return vr::VRInitError_Driver_Failed; + } + } + + // Register callbacks + std::string trackerPath; + std::string buttonPath; + std::string triggerPath; + std::string joystickPath; + if (controllerIndex_ == 0) { + trackerPath = "/me/hands/left"; + buttonPath = "/controller/left/"; + triggerPath = "/controller/left/trigger"; + joystickPath = "/controller/left/joystick"; + } else if (controllerIndex_ == 1) { + trackerPath = "/me/hands/right"; + buttonPath = "/controller/right/"; + triggerPath = "/controller/right/trigger"; + joystickPath = "/controller/right/joystick"; + } else { + buttonPath = "/controller" + std::to_string(controllerIndex_) + "/"; + triggerPath = "/controller" + std::to_string(controllerIndex_) + "/trigger"; + joystickPath = "/controller" + std::to_string(controllerIndex_) + "/joystick"; + } + + if (!trackerPath.empty()) { + trackerInterface_ = context_.getInterface(trackerPath); + trackerInterface_.registerCallback(&OSVRTrackedController::controllerTrackerCallback, this); + } + + for (int iter_button = 0; iter_button < NUM_BUTTONS; iter_button++) { + buttonInterface_[iter_button] = context_.getInterface(buttonPath + std::to_string(iter_button)); + if (buttonInterface_[iter_button].notEmpty()) { + buttonInterface_[iter_button].registerCallback(&OSVRTrackedController::controllerButtonCallback, this); + } else { + buttonInterface_[iter_button].free(); + } + } + + // TODO: ADD TOUCHPAD PART HERE + numAxis_ = 0; + + numAxis_ = 1; + for (int iter_trigger = 0; iter_trigger < NUM_TRIGGER; iter_trigger++) { + if (numAxis_ >= NUM_AXIS) + break; + + if (iter_trigger == 0) { + analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(triggerPath); + } else { + analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(triggerPath + std::to_string(iter_trigger)); + } + + if (analogInterface_[numAxis_].analogInterfaceX.notEmpty()) { + analogInterface_[numAxis_].axisIndex = numAxis_; + analogInterface_[numAxis_].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Trigger; + analogInterface_[numAxis_].analogInterfaceX.registerCallback(&OSVRTrackedController::controllerTriggerCallback, &analogInterface_[numAxis_]); + numAxis_++; + } else { + analogInterface_[numAxis_].analogInterfaceX.free(); + } + } + + numAxis_ = 2; + for (int iter_joystick = 0; iter_joystick < NUM_JOYSTICKS; iter_joystick++) { + if (numAxis_ >= NUM_AXIS) + break; + + if (iter_joystick == 0) { + analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(joystickPath + "/x"); + analogInterface_[numAxis_].analogInterfaceY = context_.getInterface(joystickPath + "/y"); + } else { + analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(joystickPath + std::to_string(iter_joystick) + "/x"); + analogInterface_[numAxis_].analogInterfaceY = context_.getInterface(joystickPath + std::to_string(iter_joystick) + "/y"); + } + + bool somethingFound = false; + + if (analogInterface_[numAxis_].analogInterfaceX.notEmpty()) { + analogInterface_[numAxis_].axisIndex = numAxis_; + analogInterface_[numAxis_].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Joystick; + analogInterface_[numAxis_].analogInterfaceX.registerCallback(&OSVRTrackedController::controllerJoystickXCallback, &analogInterface_[numAxis_]); + somethingFound = true; + } else { + analogInterface_[numAxis_].analogInterfaceX.free(); + } + + if (analogInterface_[numAxis_].analogInterfaceY.notEmpty()) { + analogInterface_[numAxis_].axisIndex = numAxis_; + analogInterface_[numAxis_].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Joystick; + analogInterface_[numAxis_].analogInterfaceY.registerCallback(&OSVRTrackedController::controllerJoystickYCallback, &analogInterface_[numAxis_]); + somethingFound = true; + } else { + analogInterface_[numAxis_].analogInterfaceY.free(); + } + + if (somethingFound) + numAxis_++; + } + + return vr::VRInitError_None; +} + +void OSVRTrackedController::Deactivate() +{ + /// Have to force freeing here + freeInterfaces(); +} + +vr::VRControllerState_t OSVRTrackedController::GetControllerState() +{ + // TODO + vr::VRControllerState_t state; + state.unPacketNum = 0; + return state; +} + +bool OSVRTrackedController::TriggerHapticPulse(uint32_t axis_id, uint16_t pulse_duration_microseconds) +{ + return false; +} + +void OSVRTrackedController::freeInterfaces() +{ + if (trackerInterface_.notEmpty()) { + trackerInterface_.free(); + } + + for (int iter_axis = 0; iter_axis < NUM_AXIS; iter_axis++) { + if (analogInterface_[iter_axis].analogInterfaceX.notEmpty()) + analogInterface_[iter_axis].analogInterfaceX.free(); + if (analogInterface_[iter_axis].analogInterfaceY.notEmpty()) + analogInterface_[iter_axis].analogInterfaceY.free(); + } + + for (int iter_button = 0; iter_button < NUM_BUTTONS; iter_button++) { + if (buttonInterface_[iter_button].notEmpty()) + buttonInterface_[iter_button].free(); + } +} + +inline vr::HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, double z) +{ + vr::HmdQuaternion_t quat; + quat.w = w; + quat.x = x; + quat.y = y; + quat.z = z; + return quat; +} + +void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report) +{ + if (!userdata) + return; + + auto* self = static_cast(userdata); + + vr::DriverPose_t pose = { 0 }; + pose.poseTimeOffset = 0; // close enough + + Eigen::Vector3d::Map(pose.vecWorldFromDriverTranslation) = Eigen::Vector3d::Zero(); + Eigen::Vector3d::Map(pose.vecDriverFromHeadTranslation) = Eigen::Vector3d::Zero(); + + map(pose.qWorldFromDriverRotation) = Eigen::Quaterniond::Identity(); + map(pose.qDriverFromHeadRotation) = Eigen::Quaterniond::Identity(); + //pose.qWorldFromDriverRotation = HmdQuaternion_Init(1, 0, 0, 0); + //pose.qDriverFromHeadRotation = HmdQuaternion_Init(1, 0, 0, 0); + + // Position + Eigen::Vector3d::Map(pose.vecPosition) = osvr::util::vecMap(report->pose.translation); + + // Position velocity and acceleration are not currently consistently provided + Eigen::Vector3d::Map(pose.vecVelocity) = Eigen::Vector3d::Zero(); + Eigen::Vector3d::Map(pose.vecAcceleration) = Eigen::Vector3d::Zero(); + + // Orientation + map(pose.qRotation) = osvr::util::fromQuat(report->pose.rotation); + + // Angular velocity and acceleration are not currently consistently provided + Eigen::Vector3d::Map(pose.vecAngularVelocity) = Eigen::Vector3d::Zero(); + Eigen::Vector3d::Map(pose.vecAngularAcceleration) = Eigen::Vector3d::Zero(); + + pose.result = vr::TrackingResult_Running_OK; + pose.poseIsValid = true; + //pose.willDriftInYaw = true; + //pose.shouldApplyHeadModel = true; + pose.deviceIsConnected = true; + + self->pose_ = pose; + + self->driverHost_->TrackedDevicePoseUpdated(self->objectId_, self->pose_); +} + +void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report) +{ + if (!userdata) + return; + + auto* self = static_cast(userdata); + + vr::EVRButtonId button_id; + if ((report->sensor >= 0 && report->sensor <= 7) || (report->sensor >= 32 && report->sensor <= 36)) { + button_id = static_cast(report->sensor); + } else if (report->sensor >= 8 && report->sensor <= 12) { + button_id = static_cast(report->sensor + 24); + } else { + return; + } + + if (OSVR_BUTTON_PRESSED == report->state) { + self->driverHost_->TrackedDeviceButtonPressed(self->objectId_, button_id, 0); + } else { + self->driverHost_->TrackedDeviceButtonUnpressed(self->objectId_, button_id, 0); + } +} + +void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) +{ + if (!userdata) + return; + + auto* analog_interface = static_cast(userdata); + OSVRTrackedController* self = analog_interface->parentController; + + analog_interface->x = report->state; + + vr::VRControllerAxis_t axis_state; + axis_state.x = static_cast(analog_interface->x); + + self->driverHost_->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); +} + +void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) +{ + if (!userdata) + return; + + auto* analog_interface = static_cast(userdata); + OSVRTrackedController* self = analog_interface->parentController; + + analog_interface->x = report->state; + + vr::VRControllerAxis_t axis_state; + axis_state.x = static_cast(analog_interface->x); + axis_state.y = static_cast(analog_interface->y); + + self->driverHost_->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); +} + +void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) +{ + if (!userdata) + return; + + auto* analog_interface = static_cast(userdata); + OSVRTrackedController* self = analog_interface->parentController; + + analog_interface->y = report->state; + + vr::VRControllerAxis_t axis_state; + axis_state.x = static_cast(analog_interface->x); + axis_state.y = static_cast(analog_interface->y); + + self->driverHost_->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); +} + +const char* OSVRTrackedController::GetId() +{ + /// @todo When available, return the actual unique ID of the HMD + return controllerName_.c_str(); +} + +void OSVRTrackedController::configure() +{ + configureProperties(); +} + +void OSVRTrackedController::configureProperties() +{ + properties_[vr::Prop_DeviceClass_Int32] = static_cast(deviceClass_); + properties_[vr::Prop_Axis0Type_Int32] = static_cast(analogInterface_[0].axisType); + properties_[vr::Prop_Axis1Type_Int32] = static_cast(analogInterface_[1].axisType); + properties_[vr::Prop_Axis2Type_Int32] = static_cast(analogInterface_[2].axisType); + properties_[vr::Prop_Axis3Type_Int32] = static_cast(analogInterface_[3].axisType); + properties_[vr::Prop_Axis4Type_Int32] = static_cast(analogInterface_[4].axisType); + + properties_[vr::Prop_SupportedButtons_Uint64] = static_cast(NUM_BUTTONS); + + properties_[vr::Prop_ModelNumber_String] = "OSVR Controller"; + properties_[vr::Prop_SerialNumber_String] = controllerName_.c_str(); + properties_[vr::Prop_RenderModelName_String] = ""; + + // Properties that are unique to TrackedDeviceClass_Controller + //Prop_AttachedDeviceId_String = 3000, + //Prop_SupportedButtons_Uint64 = 3001, + //Prop_Axis0Type_Int32 = 3002, // Return value is of type EVRControllerAxisType + //Prop_Axis1Type_Int32 = 3003, // Return value is of type EVRControllerAxisType + //Prop_Axis2Type_Int32 = 3004, // Return value is of type EVRControllerAxisType + //Prop_Axis3Type_Int32 = 3005, // Return value is of type EVRControllerAxisType + //Prop_Axis4Type_Int32 = 3006, // Return value is of type EVRControllerAxisType + +} + diff --git a/src/OSVRTrackedController.h b/src/OSVRTrackedController.h new file mode 100644 index 0000000..c20b54d --- /dev/null +++ b/src/OSVRTrackedController.h @@ -0,0 +1,135 @@ +/** @file + @brief OSVR tracked controller + + @date 2015 + + @author + Sensics, Inc. + +*/ + +// Copyright 2015 Sensics, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef INCLUDED_OSVRTrackedController_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 +#define INCLUDED_OSVRTrackedController_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 + + +#define NUM_BUTTONS 64 +#define NUM_TOUCHPAD 1 // only SteamVR Axis 0 for the moment (not implemented yet) +#define NUM_TRIGGER 1 // only SteamVR Axis 1 for the moment +#define NUM_JOYSTICKS 3 // only SteamVR Axis 2,3,4 for the moment (there is always x and y in one joystick) +#define NUM_AXIS 5 + +// Internal Includes +#include "OSVRTrackedDevice.h" +#include "osvr_compiler_detection.h" // for OSVR_OVERRIDE + +// OpenVR includes +#include + +// Library/third-party includes +#include +#include + +// Standard includes +#include + +class OSVRTrackedController; + +struct AnalogInterface { + osvr::clientkit::Interface analogInterfaceX; + osvr::clientkit::Interface analogInterfaceY; + + OSVRTrackedController* parentController; + + vr::EVRControllerAxisType axisType; + double x; + double y; + uint32_t axisIndex; +}; + + +class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRControllerComponent { + friend class ServerDriver_OSVR; + +public: + OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, int controller_index); + + virtual ~OSVRTrackedController(); + + // ------------------------------------ + // Management Methods + // ------------------------------------ + + /** + * This is called before an HMD is returned to the application. It will + * always be called before any display or tracking methods. Memory and + * processor use by the ITrackedDeviceServerDriver object should be kept to + * a minimum until it is activated. The pose listener is guaranteed to be + * valid until Deactivate is called, but should not be used after that + * point. + */ + virtual vr::EVRInitError Activate(uint32_t object_id) OSVR_OVERRIDE; + + /** + * This is called when The VR system is switching from this Hmd being the + * active display to another Hmd being the active display. The driver should + * clean whatever memory and thread use it can when it is deactivated. + */ + virtual void Deactivate() OSVR_OVERRIDE; + + // ------------------------------------ + // Controller Methods + // ------------------------------------ + + /** + * Gets the current state of a controller. + */ + virtual vr::VRControllerState_t GetControllerState() OSVR_OVERRIDE; + + /** + * Returns a uint64 property. If the property is not available this function will return 0. + */ + virtual bool TriggerHapticPulse(uint32_t axis_id, uint16_t pulse_duration_microseconds) OSVR_OVERRIDE; + +protected: + const char* GetId(); + +private: + void configure(); + void configureProperties(); + + void freeInterfaces(); + + /** + * Callback function which is called whenever new data has been received + * from the tracker. + */ + static void controllerTrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report); + static void controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report); + static void controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); + static void controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); + static void controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); + + std::string controllerName_; + int controllerIndex_; + osvr::clientkit::Interface trackerInterface_; + osvr::clientkit::Interface buttonInterface_[NUM_BUTTONS]; + uint32_t numAxis_; + AnalogInterface analogInterface_[NUM_AXIS]; +}; + +#endif // INCLUDED_OSVRTrackedDevice_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 + diff --git a/src/OSVRTrackedHMD.cpp b/src/OSVRTrackedHMD.cpp index 0394f4d..4889026 100644 --- a/src/OSVRTrackedHMD.cpp +++ b/src/OSVRTrackedHMD.cpp @@ -70,7 +70,7 @@ vr::EVRInitError OSVRTrackedHMD::Activate(uint32_t object_id) { OSVR_LOG(trace) << "OSVRTrackedHMD::Activate() called."; - objectId_ = object_id; + OSVRTrackedDevice::Activate(object_id); const std::time_t waitTime = 5; // wait up to 5 seconds for init diff --git a/src/OSVRTrackingReference.cpp b/src/OSVRTrackingReference.cpp index 51995f8..24d2006 100644 --- a/src/OSVRTrackingReference.cpp +++ b/src/OSVRTrackingReference.cpp @@ -62,7 +62,7 @@ OSVRTrackingReference::~OSVRTrackingReference() vr::EVRInitError OSVRTrackingReference::Activate(uint32_t object_id) { OSVR_LOG(trace) << "OSVRTrackingReference::Activate() called."; - objectId_ = object_id; + OSVRTrackedDevice::Activate(object_id); // Clean up tracker callback if exists if (m_TrackerInterface.notEmpty()) { From 61332c88706e09f5818d2fec76f37ecbb2e9ca2e Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 7 Jul 2017 20:04:53 +1200 Subject: [PATCH 04/24] Initial Commit - merged and updated so that it compiles. --- .gitignore | 1 + src/OSVRTrackedController.cpp | 73 ++++++++++++++++++++++++++--------- src/OSVRTrackedController.h | 4 +- src/OSVRTrackingReference.cpp | 3 +- src/PrettyPrint.cpp | 30 +++++++------- 5 files changed, 75 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 232aa8c..6c586a3 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ # Build directory build +nbproject diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 288b237..d9cefb4 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -48,14 +48,24 @@ #include #include + + // TODO: // Trackpad // OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::MotionController_Left_Thumbstick, "/controller/left/joystick/button"), // OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::MotionController_Left_Shoulder, "/controller/left/bumper"), // OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::SpecialLeft, "/controller/left/middle"), -OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, int controller_index) : OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_Controller), controllerIndex_(controller_index) +// TODO: +// use vr::VRServerDriverHost() insteead of vr::IServerDriverHost +// Interface is IVRServerDriverHost +OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IVRServerDriverHost* driver_host, int controller_index) : + //OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_Controller), controllerIndex_(controller_index) + //OSVRTrackedDevice(context, driver_host, getDeviceClass()), controllerIndex_(controller_index) + OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) + //OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::ETrackedDeviceClass device_class, const std::string& name = "OSVR device"); { + this->driver_host = driver_host; controllerName_ = "OSVRController" + std::to_string(controller_index); numAxis_ = 0; @@ -277,7 +287,10 @@ void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR self->pose_ = pose; - self->driverHost_->TrackedDevicePoseUpdated(self->objectId_, self->pose_); + //self->driver_host->TrackedDevicePoseUpdated(self->objectId_, self->pose_); + self->driver_host->TrackedDevicePoseUpdated(self->objectId_, self->pose_,sizeof(self->pose_)); + //TrackedDevicePoseUpdated( uint32_t unWhichDevice, const DriverPose_t & newPose, uint32_t unPoseStructSize ) + //vr::IVRServerDriverHost::TrackedDevicePoseUpdated(uint32_t&, vr::DriverPose_t&)' } void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report) @@ -297,9 +310,9 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ } if (OSVR_BUTTON_PRESSED == report->state) { - self->driverHost_->TrackedDeviceButtonPressed(self->objectId_, button_id, 0); + self->driver_host->TrackedDeviceButtonPressed(self->objectId_, button_id, 0); } else { - self->driverHost_->TrackedDeviceButtonUnpressed(self->objectId_, button_id, 0); + self->driver_host->TrackedDeviceButtonUnpressed(self->objectId_, button_id, 0); } } @@ -316,7 +329,7 @@ void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR vr::VRControllerAxis_t axis_state; axis_state.x = static_cast(analog_interface->x); - self->driverHost_->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + self->driver_host->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) @@ -333,7 +346,7 @@ void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OS axis_state.x = static_cast(analog_interface->x); axis_state.y = static_cast(analog_interface->y); - self->driverHost_->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + self->driver_host->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) @@ -350,7 +363,7 @@ void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OS axis_state.x = static_cast(analog_interface->x); axis_state.y = static_cast(analog_interface->y); - self->driverHost_->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + self->driver_host->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } const char* OSVRTrackedController::GetId() @@ -366,18 +379,40 @@ void OSVRTrackedController::configure() void OSVRTrackedController::configureProperties() { - properties_[vr::Prop_DeviceClass_Int32] = static_cast(deviceClass_); - properties_[vr::Prop_Axis0Type_Int32] = static_cast(analogInterface_[0].axisType); - properties_[vr::Prop_Axis1Type_Int32] = static_cast(analogInterface_[1].axisType); - properties_[vr::Prop_Axis2Type_Int32] = static_cast(analogInterface_[2].axisType); - properties_[vr::Prop_Axis3Type_Int32] = static_cast(analogInterface_[3].axisType); - properties_[vr::Prop_Axis4Type_Int32] = static_cast(analogInterface_[4].axisType); - - properties_[vr::Prop_SupportedButtons_Uint64] = static_cast(NUM_BUTTONS); - - properties_[vr::Prop_ModelNumber_String] = "OSVR Controller"; - properties_[vr::Prop_SerialNumber_String] = controllerName_.c_str(); - properties_[vr::Prop_RenderModelName_String] = ""; + + propertyContainer_ = vr::VRProperties()->TrackedDeviceToPropertyContainer(this->objectId_); + + //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_WillDriftInYaw_Bool, true); + //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_DeviceIsWireless_Bool, false); + //ETrackedPropertyError SetBoolProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, bool bNewValue ); + //ETrackedPropertyError SetFloatProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, float fNewValue ); + //ETrackedPropertyError SetInt32Property( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, int32_t nNewValue ); + //ETrackedPropertyError SetUint64Property( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, uint64_t ulNewValue ); + //ETrackedPropertyError SetStringProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, const char *pchNewValue ); + + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_DeviceClass_Int32, static_cast(deviceClass_)); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis0Type_Int32, static_cast(analogInterface_[0].axisType)); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis1Type_Int32, static_cast(analogInterface_[1].axisType)); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis2Type_Int32, static_cast(analogInterface_[2].axisType)); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis3Type_Int32, static_cast(analogInterface_[3].axisType)); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis4Type_Int32, static_cast(analogInterface_[4].axisType)); + vr::VRProperties()->SetUint64Property(propertyContainer_, vr::Prop_SupportedButtons_Uint64, static_cast(NUM_BUTTONS)); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_ModelNumber_String, "OSVR Controller"); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_SerialNumber_String, controllerName_.c_str()); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, ""); + + //properties_[vr::Prop_DeviceClass_Int32] = static_cast(deviceClass_); + //properties_[vr::Prop_Axis0Type_Int32] = static_cast(analogInterface_[0].axisType); + //properties_[vr::Prop_Axis1Type_Int32] = static_cast(analogInterface_[1].axisType); + //properties_[vr::Prop_Axis2Type_Int32] = static_cast(analogInterface_[2].axisType); + //properties_[vr::Prop_Axis3Type_Int32] = static_cast(analogInterface_[3].axisType); + //properties_[vr::Prop_Axis4Type_Int32] = static_cast(analogInterface_[4].axisType); + + //properties_[vr::Prop_SupportedButtons_Uint64] = static_cast(NUM_BUTTONS); + + //properties_[vr::Prop_ModelNumber_String] = "OSVR Controller"; + //properties_[vr::Prop_SerialNumber_String] = controllerName_.c_str(); + //properties_[vr::Prop_RenderModelName_String] = ""; // Properties that are unique to TrackedDeviceClass_Controller //Prop_AttachedDeviceId_String = 3000, diff --git a/src/OSVRTrackedController.h b/src/OSVRTrackedController.h index c20b54d..cc7e9f8 100644 --- a/src/OSVRTrackedController.h +++ b/src/OSVRTrackedController.h @@ -60,12 +60,11 @@ struct AnalogInterface { uint32_t axisIndex; }; - class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRControllerComponent { friend class ServerDriver_OSVR; public: - OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, int controller_index); + OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IVRServerDriverHost* driver_host, int controller_index); virtual ~OSVRTrackedController(); @@ -129,6 +128,7 @@ class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRController osvr::clientkit::Interface buttonInterface_[NUM_BUTTONS]; uint32_t numAxis_; AnalogInterface analogInterface_[NUM_AXIS]; + vr::IVRServerDriverHost* driver_host; }; #endif // INCLUDED_OSVRTrackedDevice_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 diff --git a/src/OSVRTrackingReference.cpp b/src/OSVRTrackingReference.cpp index 068c564..57da5f5 100644 --- a/src/OSVRTrackingReference.cpp +++ b/src/OSVRTrackingReference.cpp @@ -212,7 +212,8 @@ void OSVRTrackingReference::configure() minTrackingRange_ = settings_->getSetting("minTrackingRangeMeters", minTrackingRange_); maxTrackingRange_ = settings_->getSetting("maxTrackingRangeMeters", maxTrackingRange_); - configureProperties(); + //configureProperties(); + setProperties(); } std::string OSVRTrackingReference::getTrackerPath() const diff --git a/src/PrettyPrint.cpp b/src/PrettyPrint.cpp index b8c28ec..c2b622f 100644 --- a/src/PrettyPrint.cpp +++ b/src/PrettyPrint.cpp @@ -127,8 +127,8 @@ std::string to_string(const vr::ETrackedDeviceProperty& value) return "Prop_ViveSystemButtonFixRequired_Bool"; case vr::Prop_ParentDriver_Uint64: return "Prop_ParentDriver_Uint64"; - case vr::Prop_ResourceRoot_String: - return "Prop_ResourceRoot_String"; + //case vr::Prop_ResourceRoot_String: + // return "Prop_ResourceRoot_String"; case vr::Prop_ReportsTimeSinceVSync_Bool: return "Prop_ReportsTimeSinceVSync_Bool"; case vr::Prop_SecondsFromVsyncToPhotons_Float: @@ -213,8 +213,8 @@ std::string to_string(const vr::ETrackedDeviceProperty& value) return "Prop_DisplayMCImageNumChannels_Int32"; case vr::Prop_DisplayMCImageData_Binary: return "Prop_DisplayMCImageData_Binary"; - case vr::Prop_SecondsFromPhotonsToVblank_Float: - return "Prop_SecondsFromPhotonsToVblank_Float"; + //case vr::Prop_SecondsFromPhotonsToVblank_Float: + // return "Prop_SecondsFromPhotonsToVblank_Float"; case vr::Prop_AttachedDeviceId_String: return "Prop_AttachedDeviceId_String"; case vr::Prop_SupportedButtons_Uint64: @@ -271,20 +271,22 @@ std::string to_string(const vr::ETrackedDeviceProperty& value) return "Prop_UserConfigPath_String"; case vr::Prop_InstallPath_String: return "Prop_InstallPath_String"; - case vr::Prop_HasDisplayComponent_Bool: - return "Prop_HasDisplayComponent_Bool"; - case vr::Prop_HasControllerComponent_Bool: - return "Prop_HasControllerComponent_Bool"; - case vr::Prop_HasCameraComponent_Bool: - return "Prop_HasCameraComponent_Bool"; - case vr::Prop_HasDriverDirectModeComponent_Bool: - return "Prop_HasDriverDirectModeComponent_Bool"; - case vr::Prop_HasVirtualDisplayComponent_Bool: - return "Prop_HasVirtualDisplayComponent_Bool"; + //case vr::Prop_HasDisplayComponent_Bool: + // return "Prop_HasDisplayComponent_Bool"; + //case vr::Prop_HasControllerComponent_Bool: + // return "Prop_HasControllerComponent_Bool"; + //case vr::Prop_HasCameraComponent_Bool: + // return "Prop_HasCameraComponent_Bool"; + //case vr::Prop_HasDriverDirectModeComponent_Bool: + // return "Prop_HasDriverDirectModeComponent_Bool"; + //case vr::Prop_HasVirtualDisplayComponent_Bool: + // return "Prop_HasVirtualDisplayComponent_Bool"; case vr::Prop_VendorSpecific_Reserved_Start: return "Prop_VendorSpecific_Reserved_Start"; case vr::Prop_VendorSpecific_Reserved_End: return "Prop_VendorSpecific_Reserved_End"; + case vr::Prop_UsesDriverDirectMode_Bool: + return "Prop_UsesDriverDirectMode_Bool"; default: { std::ostringstream oss; From 1e110c1445073b1e02dc72f546ca5ed1afac4c3f Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 7 Jul 2017 20:18:32 +1200 Subject: [PATCH 05/24] Ignoring cmake --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 6c586a3..270e39c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ build nbproject +CMakeFiles +openvr From 5dfd2177ebce206910ae71067a88d7b3174251df Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 7 Jul 2017 21:56:24 +1200 Subject: [PATCH 06/24] Working on integration. --- src/OSVRTrackedController.cpp | 21 ++++++++++++--------- src/OSVRTrackedController.h | 4 ++-- src/ServerDriver_OSVR.cpp | 3 +++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index d9cefb4..3a45d2b 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -59,13 +59,16 @@ // TODO: // use vr::VRServerDriverHost() insteead of vr::IServerDriverHost // Interface is IVRServerDriverHost -OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IVRServerDriverHost* driver_host, int controller_index) : +//OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context) : +// OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) +OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, int controller_index) : + OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) + //OSVRTrackedDevice(context, vr::TrackedDeviceClass_Controller, "OSVRTrackedController") //OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_Controller), controllerIndex_(controller_index) //OSVRTrackedDevice(context, driver_host, getDeviceClass()), controllerIndex_(controller_index) - OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) + //OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) //OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::ETrackedDeviceClass device_class, const std::string& name = "OSVR device"); { - this->driver_host = driver_host; controllerName_ = "OSVRController" + std::to_string(controller_index); numAxis_ = 0; @@ -288,7 +291,7 @@ void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR self->pose_ = pose; //self->driver_host->TrackedDevicePoseUpdated(self->objectId_, self->pose_); - self->driver_host->TrackedDevicePoseUpdated(self->objectId_, self->pose_,sizeof(self->pose_)); + vr::VRServerDriverHost()->TrackedDevicePoseUpdated(self->objectId_, self->pose_,sizeof(self->pose_)); //TrackedDevicePoseUpdated( uint32_t unWhichDevice, const DriverPose_t & newPose, uint32_t unPoseStructSize ) //vr::IVRServerDriverHost::TrackedDevicePoseUpdated(uint32_t&, vr::DriverPose_t&)' } @@ -310,9 +313,9 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ } if (OSVR_BUTTON_PRESSED == report->state) { - self->driver_host->TrackedDeviceButtonPressed(self->objectId_, button_id, 0); + vr::VRServerDriverHost()->TrackedDeviceButtonPressed(self->objectId_, button_id, 0); } else { - self->driver_host->TrackedDeviceButtonUnpressed(self->objectId_, button_id, 0); + vr::VRServerDriverHost()->TrackedDeviceButtonUnpressed(self->objectId_, button_id, 0); } } @@ -329,7 +332,7 @@ void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR vr::VRControllerAxis_t axis_state; axis_state.x = static_cast(analog_interface->x); - self->driver_host->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) @@ -346,7 +349,7 @@ void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OS axis_state.x = static_cast(analog_interface->x); axis_state.y = static_cast(analog_interface->y); - self->driver_host->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) @@ -363,7 +366,7 @@ void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OS axis_state.x = static_cast(analog_interface->x); axis_state.y = static_cast(analog_interface->y); - self->driver_host->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } const char* OSVRTrackedController::GetId() diff --git a/src/OSVRTrackedController.h b/src/OSVRTrackedController.h index cc7e9f8..a51f908 100644 --- a/src/OSVRTrackedController.h +++ b/src/OSVRTrackedController.h @@ -64,7 +64,8 @@ class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRController friend class ServerDriver_OSVR; public: - OSVRTrackedController(osvr::clientkit::ClientContext& context, vr::IVRServerDriverHost* driver_host, int controller_index); + OSVRTrackedController(osvr::clientkit::ClientContext& context, int controller_index); + //OSVRTrackedController(osvr::clientkit::ClientContext& context); virtual ~OSVRTrackedController(); @@ -128,7 +129,6 @@ class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRController osvr::clientkit::Interface buttonInterface_[NUM_BUTTONS]; uint32_t numAxis_; AnalogInterface analogInterface_[NUM_AXIS]; - vr::IVRServerDriverHost* driver_host; }; #endif // INCLUDED_OSVRTrackedDevice_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index 59c79d9..db0fea7 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -27,6 +27,7 @@ #include "OSVRTrackedHMD.h" // for OSVRTrackedHMD #include "OSVRTrackingReference.h" // for OSVRTrackingReference +#include "OSVRTrackedController.h" // for OSVRTrackingReference #include "platform_fixes.h" // strcasecmp #include "make_unique.h" // for std::make_unique #include "Logging.h" // for OSVR_LOG, Logging @@ -86,6 +87,8 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IVRDriverContext* driver_context) trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); + trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 0)); // left hand + trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 1)); // right hand for (auto& tracked_device : trackedDevices_) { vr::VRServerDriverHost()->TrackedDeviceAdded(tracked_device->getId(), tracked_device->getDeviceClass(), tracked_device.get()); From 814097f3bff7e1c11e17f42e1ba4bfe72d0ec731 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Mon, 10 Jul 2017 21:37:05 +1200 Subject: [PATCH 07/24] Working on developing. --- src/OSVRTrackedController.cpp | 33 ++++++++++++++++++++++----------- src/OSVRTrackedController.h | 1 - src/ServerDriver_OSVR.cpp | 2 ++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 3a45d2b..569c360 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -59,17 +59,10 @@ // TODO: // use vr::VRServerDriverHost() insteead of vr::IServerDriverHost // Interface is IVRServerDriverHost -//OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context) : -// OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, int controller_index) : - OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) - //OSVRTrackedDevice(context, vr::TrackedDeviceClass_Controller, "OSVRTrackedController") - //OSVRTrackedDevice(context, driver_host, vr::TrackedDeviceClass_Controller), controllerIndex_(controller_index) - //OSVRTrackedDevice(context, driver_host, getDeviceClass()), controllerIndex_(controller_index) - //OSVRTrackedDevice(context, getDeviceClass()), controllerIndex_(controller_index) - //OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::ETrackedDeviceClass device_class, const std::string& name = "OSVR device"); + OSVRTrackedDevice(context, getDeviceClass(), "OSVR controller"+std::to_string(controller_index)), controllerIndex_(controller_index) { - controllerName_ = "OSVRController" + std::to_string(controller_index); + OSVR_LOG(trace) << "OSVRTrackedController::constructor() called. name = " << name_ <<"\n"; numAxis_ = 0; for (int iter_axis = 0; iter_axis < NUM_AXIS; iter_axis++) { @@ -85,7 +78,9 @@ OSVRTrackedController::~OSVRTrackedController() vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) { + OSVR_LOG(trace) << "OSVRTrackedController::Activate() called."; OSVRTrackedDevice::Activate(object_id); + OSVR_LOG(trace) << "OSVRTrackedController::Activate() after Activate called."; const std::time_t wait_time = 5; // wait up to 5 seconds for init @@ -102,6 +97,7 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) return vr::VRInitError_Driver_Failed; } } + OSVR_LOG(trace) << "OSVRTrackedController::Activate() after while loop."; // Register callbacks std::string trackerPath; @@ -138,6 +134,7 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) } } + OSVR_LOG(trace) << "OSVRTrackedController::Activate() before TODOtouchpad."; // TODO: ADD TOUCHPAD PART HERE numAxis_ = 0; @@ -199,6 +196,7 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) numAxis_++; } + OSVR_LOG(trace) << "OSVRTrackedController::Activate() end of function."; return vr::VRInitError_None; } @@ -210,6 +208,7 @@ void OSVRTrackedController::Deactivate() vr::VRControllerState_t OSVRTrackedController::GetControllerState() { + OSVR_LOG(trace) << "OSVRTrackedController::GetControllerState()."; // TODO vr::VRControllerState_t state; state.unPacketNum = 0; @@ -218,11 +217,13 @@ vr::VRControllerState_t OSVRTrackedController::GetControllerState() bool OSVRTrackedController::TriggerHapticPulse(uint32_t axis_id, uint16_t pulse_duration_microseconds) { + OSVR_LOG(trace) << "OSVRTrackedController::TriggerHapticPulse()."; return false; } void OSVRTrackedController::freeInterfaces() { + OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces()."; if (trackerInterface_.notEmpty()) { trackerInterface_.free(); } @@ -247,11 +248,13 @@ inline vr::HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, doub quat.x = x; quat.y = y; quat.z = z; + OSVR_LOG(trace) << "OSVRTrackedController::HmdQuat()."; return quat; } void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report) { + OSVR_LOG(trace) << "OSVRTrackedController::controllerTrackerCallback()."; if (!userdata) return; @@ -298,6 +301,7 @@ void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report) { + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback()."; if (!userdata) return; @@ -321,6 +325,7 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { + OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback()."; if (!userdata) return; @@ -337,6 +342,7 @@ void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { + OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickXCallback()."; if (!userdata) return; @@ -354,6 +360,7 @@ void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OS void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { + OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickYCallback()."; if (!userdata) return; @@ -371,18 +378,21 @@ void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OS const char* OSVRTrackedController::GetId() { + OSVR_LOG(trace) << "OSVRTrackedController::GetId()."; /// @todo When available, return the actual unique ID of the HMD - return controllerName_.c_str(); + return name_.c_str(); } void OSVRTrackedController::configure() { + OSVR_LOG(trace) << "OSVRTrackedController::configure()."; configureProperties(); } void OSVRTrackedController::configureProperties() { + OSVR_LOG(trace) << "OSVRTrackedController::configureProperties()."; propertyContainer_ = vr::VRProperties()->TrackedDeviceToPropertyContainer(this->objectId_); //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_WillDriftInYaw_Bool, true); @@ -401,7 +411,7 @@ void OSVRTrackedController::configureProperties() vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis4Type_Int32, static_cast(analogInterface_[4].axisType)); vr::VRProperties()->SetUint64Property(propertyContainer_, vr::Prop_SupportedButtons_Uint64, static_cast(NUM_BUTTONS)); vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_ModelNumber_String, "OSVR Controller"); - vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_SerialNumber_String, controllerName_.c_str()); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_SerialNumber_String, name_.c_str()); vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, ""); //properties_[vr::Prop_DeviceClass_Int32] = static_cast(deviceClass_); @@ -426,5 +436,6 @@ void OSVRTrackedController::configureProperties() //Prop_Axis3Type_Int32 = 3005, // Return value is of type EVRControllerAxisType //Prop_Axis4Type_Int32 = 3006, // Return value is of type EVRControllerAxisType + OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() end of function."; } diff --git a/src/OSVRTrackedController.h b/src/OSVRTrackedController.h index a51f908..cafa347 100644 --- a/src/OSVRTrackedController.h +++ b/src/OSVRTrackedController.h @@ -123,7 +123,6 @@ class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRController static void controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); static void controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); - std::string controllerName_; int controllerIndex_; osvr::clientkit::Interface trackerInterface_; osvr::clientkit::Interface buttonInterface_[NUM_BUTTONS]; diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index db0fea7..eec6e95 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -87,7 +87,9 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IVRDriverContext* driver_context) trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); + OSVR_LOG(debug) << "ServerDriver_OSVR - before left hand\n"; trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 0)); // left hand + OSVR_LOG(debug) << "ServerDriver_OSVR - before right hand\n"; trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 1)); // right hand for (auto& tracked_device : trackedDevices_) { From 0565ef58a5ac62417745f0b15c64cf359784c83b Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Tue, 18 Jul 2017 09:48:32 +1200 Subject: [PATCH 08/24] Added print statement on deactivate. --- src/OSVRTrackedController.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 569c360..15a2bd2 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -202,6 +202,7 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) void OSVRTrackedController::Deactivate() { + OSVR_LOG(trace) << "OSVRTrackedController::Deactivate"; /// Have to force freeing here freeInterfaces(); } From f72ba137afc036b9df2924e19ad50b61f89cd070 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Thu, 3 Aug 2017 15:55:48 +1200 Subject: [PATCH 09/24] Working with Kevin on fixing problems with constructor to get steamvr to recognize the controllers. --- src/OSVRTrackedController.cpp | 31 ++++++++++++++++++++++++++++--- src/ServerDriver_OSVR.cpp | 2 ++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 15a2bd2..85b6a48 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -60,7 +60,7 @@ // use vr::VRServerDriverHost() insteead of vr::IServerDriverHost // Interface is IVRServerDriverHost OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, int controller_index) : - OSVRTrackedDevice(context, getDeviceClass(), "OSVR controller"+std::to_string(controller_index)), controllerIndex_(controller_index) + OSVRTrackedDevice(context, vr::TrackedDeviceClass_Controller, "OSVR controller"+std::to_string(controller_index)), controllerIndex_(controller_index) { OSVR_LOG(trace) << "OSVRTrackedController::constructor() called. name = " << name_ <<"\n"; @@ -73,7 +73,9 @@ OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& con OSVRTrackedController::~OSVRTrackedController() { - // do nothing + // DO NOTHING + //vr::IDriverLog* logger_ = nullptr; + //vr::IServerDriverHost* driver_host = nullptr; } vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) @@ -196,6 +198,9 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) numAxis_++; } + // added in for testing not sure if its correct or not. + configure(); + OSVR_LOG(trace) << "OSVRTrackedController::Activate() end of function."; return vr::VRInitError_None; } @@ -394,8 +399,29 @@ void OSVRTrackedController::configureProperties() { OSVR_LOG(trace) << "OSVRTrackedController::configureProperties()."; + OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() NAME = "<SetStringProperty(propertyContainer_, vr::Prop_ManufacturerName_String, "LYRobotix"); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_TrackingFirmwareVersion_String, "0.1.0"); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_HardwareRevision_String, "0.1.0"); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_AllWirelessDongleDescriptions_String, ""); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_ConnectedWirelessDongle_String, ""); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_Firmware_ManualUpdateURL_String, ""); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_Firmware_ProgrammingTarget_String, ""); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_DriverVersion_String, "0.1.0"); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_AttachedDeviceId_String, "3000"); + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_ModeLabel_String, ""); + + //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_WillDriftInYaw_Bool, true); //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_DeviceIsWireless_Bool, false); //ETrackedPropertyError SetBoolProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, bool bNewValue ); @@ -439,4 +465,3 @@ void OSVRTrackedController::configureProperties() OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() end of function."; } - diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index eec6e95..e4300d2 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -93,7 +93,9 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IVRDriverContext* driver_context) trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 1)); // right hand for (auto& tracked_device : trackedDevices_) { + OSVR_LOG(debug) << "ServerDriver_OSVR - for loop - before TrackedDeviceAdded\n"; vr::VRServerDriverHost()->TrackedDeviceAdded(tracked_device->getId(), tracked_device->getDeviceClass(), tracked_device.get()); + OSVR_LOG(debug) << "ServerDriver_OSVR - for loop - after TrackedDeviceAdded\n"; } client_update_thread_quit.store(false); From 9b1e0b66a6ffe43171c9476b66b155f9d412a071 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Thu, 3 Aug 2017 17:20:22 +1200 Subject: [PATCH 10/24] Added back openvr submodule and cleaned up for latest version. --- .gitignore | 1 - .gitmodules | 2 +- openvr | 1 + src/PrettyPrint.cpp | 4 ++-- 4 files changed, 4 insertions(+), 4 deletions(-) create mode 160000 openvr diff --git a/.gitignore b/.gitignore index 270e39c..b59be1e 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,3 @@ build nbproject CMakeFiles -openvr diff --git a/.gitmodules b/.gitmodules index aea5c43..795e89e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "openvr"] path = openvr - url = https://github.com/ValveSoftware/openvr.git + url = https://github.com/ValveSoftware/openvr [submodule "vendor/OSVR-Display"] path = vendor/OSVR-Display url = https://github.com/OSVR/OSVR-Display.git diff --git a/openvr b/openvr new file mode 160000 index 0000000..5d0574b --- /dev/null +++ b/openvr @@ -0,0 +1 @@ +Subproject commit 5d0574bf6473130d25dd296ad30206ccd148590b diff --git a/src/PrettyPrint.cpp b/src/PrettyPrint.cpp index c2b622f..d16d722 100644 --- a/src/PrettyPrint.cpp +++ b/src/PrettyPrint.cpp @@ -285,8 +285,8 @@ std::string to_string(const vr::ETrackedDeviceProperty& value) return "Prop_VendorSpecific_Reserved_Start"; case vr::Prop_VendorSpecific_Reserved_End: return "Prop_VendorSpecific_Reserved_End"; - case vr::Prop_UsesDriverDirectMode_Bool: - return "Prop_UsesDriverDirectMode_Bool"; + //case vr::Prop_UsesDriverDirectMode_Bool: + // return "Prop_UsesDriverDirectMode_Bool"; default: { std::ostringstream oss; From 124fd8ea442f2896c930828227f0e82f33121356 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Tue, 8 Aug 2017 01:10:22 +1200 Subject: [PATCH 11/24] Still lots of comments and needs to be cleaned up. But, controller buttons are sorted. I don't think the axis is correct on the trackpad though. There is also a Button_A that I wasn't sure what to map to. --- src/OSVRTrackedController.cpp | 275 ++++++++++++++++++++++++++++++---- src/OSVRTrackedController.h | 14 +- 2 files changed, 260 insertions(+), 29 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 85b6a48..81ef2c6 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -63,12 +63,6 @@ OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& con OSVRTrackedDevice(context, vr::TrackedDeviceClass_Controller, "OSVR controller"+std::to_string(controller_index)), controllerIndex_(controller_index) { OSVR_LOG(trace) << "OSVRTrackedController::constructor() called. name = " << name_ <<"\n"; - - numAxis_ = 0; - for (int iter_axis = 0; iter_axis < NUM_AXIS; iter_axis++) { - analogInterface_[iter_axis].parentController = this; - analogInterface_[iter_axis].axisType = vr::EVRControllerAxisType::k_eControllerAxis_None; - } } OSVRTrackedController::~OSVRTrackedController() @@ -106,27 +100,124 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) std::string buttonPath; std::string triggerPath; std::string joystickPath; + std::string trackpadPath; if (controllerIndex_ == 0) { - trackerPath = "/me/hands/left"; - buttonPath = "/controller/left/"; - triggerPath = "/controller/left/trigger"; + trackerPath = "/me/hands/left"; + buttonPath = "/controller/left/"; + triggerPath = "/controller/left/trigger"; joystickPath = "/controller/left/joystick"; + trackpadPath = "/controller/left/trackpad"; } else if (controllerIndex_ == 1) { - trackerPath = "/me/hands/right"; - buttonPath = "/controller/right/"; - triggerPath = "/controller/right/trigger"; + trackerPath = "/me/hands/right"; + buttonPath = "/controller/right/"; + triggerPath = "/controller/right/trigger"; joystickPath = "/controller/right/joystick"; + trackpadPath = "/controller/right/trackpad"; } else { buttonPath = "/controller" + std::to_string(controllerIndex_) + "/"; triggerPath = "/controller" + std::to_string(controllerIndex_) + "/trigger"; joystickPath = "/controller" + std::to_string(controllerIndex_) + "/joystick"; } + +/* +"controller": { + "left": { + "$target": "tracker/2", + "system": "button/3", + "menu": "button/2", + "grip": "button/4", + "trackpad": { + "x": "analog/0", + "y": "analog/1", + "touch": "button/5", + "button": "button/0" + }, + "trigger": { + "button": "button/1" + }, + "battery": "analog/2" + }, +*/ + +// pathing + +// TRACKER +// /controller/left/target + +// BUTTONS +// /controller/left/system +// /controller/left/menu +// /controller/left/grip + + +// TRIGGER +// /controller/left/trigger +// /controller/left/trigger/button + +// TRACKPAD +// /controller/left/trackpad +// --- TRACKPAD BUTTONS +// /controller/left/trackpad/touch +// /controller/left/trackpad/button + + +// battery???? +// /controller/left/battery + if (!trackerPath.empty()) { trackerInterface_ = context_.getInterface(trackerPath); - trackerInterface_.registerCallback(&OSVRTrackedController::controllerTrackerCallback, this); + if(trackerInterface_.notEmpty()){ + OSVR_LOG(trace) << "OSVRTrackedController::Activate() tracker Interface is empty "<= NUM_AXIS) @@ -160,7 +253,9 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) analogInterface_[numAxis_].analogInterfaceX.free(); } } +*/ +/* numAxis_ = 2; for (int iter_joystick = 0; iter_joystick < NUM_JOYSTICKS; iter_joystick++) { if (numAxis_ >= NUM_AXIS) @@ -197,6 +292,7 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) if (somethingFound) numAxis_++; } +*/ // added in for testing not sure if its correct or not. configure(); @@ -234,6 +330,7 @@ void OSVRTrackedController::freeInterfaces() trackerInterface_.free(); } + OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces() before freeing axis."; for (int iter_axis = 0; iter_axis < NUM_AXIS; iter_axis++) { if (analogInterface_[iter_axis].analogInterfaceX.notEmpty()) analogInterface_[iter_axis].analogInterfaceX.free(); @@ -241,10 +338,12 @@ void OSVRTrackedController::freeInterfaces() analogInterface_[iter_axis].analogInterfaceY.free(); } + OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces() before freeing buttons."; for (int iter_button = 0; iter_button < NUM_BUTTONS; iter_button++) { - if (buttonInterface_[iter_button].notEmpty()) - buttonInterface_[iter_button].free(); + if (buttonInterface_[iter_button].buttonInterface.notEmpty()) + buttonInterface_[iter_button].buttonInterface.free(); } + OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces() end."; } inline vr::HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, double z) @@ -260,7 +359,7 @@ inline vr::HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, doub void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_PoseReport* report) { - OSVR_LOG(trace) << "OSVRTrackedController::controllerTrackerCallback()."; + //OSVR_LOG(trace) << "OSVRTrackedController::controllerTrackerCallback()."; if (!userdata) return; @@ -305,15 +404,24 @@ void OSVRTrackedController::controllerTrackerCallback(void* userdata, const OSVR //vr::IVRServerDriverHost::TrackedDevicePoseUpdated(uint32_t&, vr::DriverPose_t&)' } -void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report) -{ +void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report){ OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback()."; if (!userdata) return; - auto* self = static_cast(userdata); + auto* button_interface = static_cast(userdata); + OSVRTrackedController* self = button_interface->parentController; + if(!self){ + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). self isn't here :("; + return; + } + + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). report->sensor "<sensor; + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). button_id "<button_id; + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). objectId "<objectId_; + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). report state "<state; - vr::EVRButtonId button_id; + /* if ((report->sensor >= 0 && report->sensor <= 7) || (report->sensor >= 32 && report->sensor <= 36)) { button_id = static_cast(report->sensor); } else if (report->sensor >= 8 && report->sensor <= 12) { @@ -321,11 +429,14 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ } else { return; } - - if (OSVR_BUTTON_PRESSED == report->state) { - vr::VRServerDriverHost()->TrackedDeviceButtonPressed(self->objectId_, button_id, 0); - } else { - vr::VRServerDriverHost()->TrackedDeviceButtonUnpressed(self->objectId_, button_id, 0); + */ + // error checking + if((button_interface->button_id >=0 && button_interface->button_id <= 7) || (button_interface->button_id >= 31 && button_interface->button_id <= 36)){ + if (OSVR_BUTTON_PRESSED == report->state) { + vr::VRServerDriverHost()->TrackedDeviceButtonPressed(self->objectId_, button_interface->button_id, 0); + } else { + vr::VRServerDriverHost()->TrackedDeviceButtonUnpressed(self->objectId_, button_interface->button_id, 0); + } } } @@ -348,7 +459,7 @@ void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickXCallback()."; + //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickXCallback()."; if (!userdata) return; @@ -366,7 +477,44 @@ void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OS void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickYCallback()."; + //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickYCallback()."; + if (!userdata) + return; + + auto* analog_interface = static_cast(userdata); + OSVRTrackedController* self = analog_interface->parentController; + + analog_interface->y = report->state; + + vr::VRControllerAxis_t axis_state; + axis_state.x = static_cast(analog_interface->x); + axis_state.y = static_cast(analog_interface->y); + + vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); +} + +void OSVRTrackedController::controllerXAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) +{ + //OSVR_LOG(trace) << "OSVRTrackedController::controllerXAxisCallback()."; + if (!userdata) + return; + + auto* analog_interface = static_cast(userdata); + OSVRTrackedController* self = analog_interface->parentController; + + analog_interface->x = report->state; + + vr::VRControllerAxis_t axis_state; + axis_state.x = static_cast(analog_interface->x); + axis_state.y = static_cast(analog_interface->y); + + vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); + //virtual void TrackedDeviceAxisUpdated( uint32_t unWhichDevice, uint32_t unWhichAxis, const VRControllerAxis_t & axisState ) = 0; +} + +void OSVRTrackedController::controllerYAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) +{ + //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickYCallback()."; if (!userdata) return; @@ -401,11 +549,13 @@ void OSVRTrackedController::configureProperties() OSVR_LOG(trace) << "OSVRTrackedController::configureProperties()."; OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() NAME = "<SetBoolProperty(propertyContainer_, vr::Prop_WillDriftInYaw_Bool, true); //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_DeviceIsWireless_Bool, false); //ETrackedPropertyError SetBoolProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, bool bNewValue ); @@ -431,15 +582,19 @@ void OSVRTrackedController::configureProperties() //ETrackedPropertyError SetStringProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, const char *pchNewValue ); vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_DeviceClass_Int32, static_cast(deviceClass_)); + /* vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis0Type_Int32, static_cast(analogInterface_[0].axisType)); vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis1Type_Int32, static_cast(analogInterface_[1].axisType)); vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis2Type_Int32, static_cast(analogInterface_[2].axisType)); vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis3Type_Int32, static_cast(analogInterface_[3].axisType)); vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis4Type_Int32, static_cast(analogInterface_[4].axisType)); vr::VRProperties()->SetUint64Property(propertyContainer_, vr::Prop_SupportedButtons_Uint64, static_cast(NUM_BUTTONS)); + */ vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_ModelNumber_String, "OSVR Controller"); vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_SerialNumber_String, name_.c_str()); - vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, ""); + // set the vive controller at the default. + vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, "vr_controller_vive_1_5"); + //vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, settings_->getSetting("cameraRenderModel", "").c_str()); //properties_[vr::Prop_DeviceClass_Int32] = static_cast(deviceClass_); //properties_[vr::Prop_Axis0Type_Int32] = static_cast(analogInterface_[0].axisType); @@ -465,3 +620,67 @@ void OSVRTrackedController::configureProperties() OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() end of function."; } + +/** + * Registers a button based on the path and id. + * @param id the id of the button which is used for indexing an array. + * @param path the complete path to the button. + */ +void OSVRTrackedController::registerButton(int id, std::string path, vr::EVRButtonId button_id){ + OSVR_LOG(trace) << "OSVRTrackedController::registerButton start of function."; + OSVR_LOG(trace) << "OSVRTrackedController::registerButton id = "<TrackedDeviceToPropertyContainer(this->objectId_); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis0Type_Int32, static_cast(analogInterface_[id].axisType)); + } else { + analogInterface_[id].analogInterfaceX.free(); + } + + if (analogInterface_[id].analogInterfaceY.notEmpty()) { + analogInterface_[id].axisIndex = id; + analogInterface_[id].axisType = vr::EVRControllerAxisType::k_eControllerAxis_TrackPad; + analogInterface_[id].parentController = this; + analogInterface_[id].analogInterfaceY.registerCallback(&OSVRTrackedController::controllerYAxisCallback, &analogInterface_[id]); + propertyContainer_ = vr::VRProperties()->TrackedDeviceToPropertyContainer(this->objectId_); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis0Type_Int32, static_cast(analogInterface_[id].axisType)); + } else { + analogInterface_[id].analogInterfaceY.free(); + } +} diff --git a/src/OSVRTrackedController.h b/src/OSVRTrackedController.h index cafa347..cf534ed 100644 --- a/src/OSVRTrackedController.h +++ b/src/OSVRTrackedController.h @@ -48,6 +48,12 @@ class OSVRTrackedController; +struct ButtonInterface { + osvr::clientkit::Interface buttonInterface; + OSVRTrackedController* parentController; + vr::EVRButtonId button_id; +}; + struct AnalogInterface { osvr::clientkit::Interface analogInterfaceX; osvr::clientkit::Interface analogInterfaceY; @@ -122,12 +128,18 @@ class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRController static void controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); static void controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); static void controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); + static void controllerXAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); + static void controllerYAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report); int controllerIndex_; osvr::clientkit::Interface trackerInterface_; - osvr::clientkit::Interface buttonInterface_[NUM_BUTTONS]; + //osvr::clientkit::Interface buttonInterface_[NUM_BUTTONS]; uint32_t numAxis_; AnalogInterface analogInterface_[NUM_AXIS]; + ButtonInterface buttonInterface_[NUM_BUTTONS]; + void registerButton(int id, std::string path, vr::EVRButtonId button_id); + void registerTrigger(int id, std::string path); + void registerTrackpad(int id, std::string path); }; #endif // INCLUDED_OSVRTrackedDevice_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 From 16564da5d2f028ca18a8b6d16fd70bf5f7e68962 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Wed, 9 Aug 2017 18:15:24 +1200 Subject: [PATCH 12/24] Added touch press and unpress for trackpad! --- src/OSVRTrackedController.cpp | 48 ++++++++++++++++++++++++++++++++++- src/OSVRTrackedController.h | 2 ++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 81ef2c6..21003fc 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -190,7 +190,7 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) registerButton(button_id++, buttonPath+"menu", vr::k_EButton_ApplicationMenu); registerButton(button_id++, buttonPath+"grip",vr::k_EButton_Grip); registerButton(button_id++, buttonPath+"trackpad/button",vr::k_EButton_SteamVR_Touchpad); - //registerButton(button_id++, buttonPath+"trackpad/touch",vr::k_EButton_SteamVR_Touchpad); + registerButtonTouch(button_id++, buttonPath+"trackpad/touch",vr::k_EButton_SteamVR_Touchpad); //registerButton(button_id++, buttonPath+"trackpad/button", vr::k_EButton_A); registerButton(button_id++, buttonPath+"trigger/button", vr::k_EButton_SteamVR_Trigger); // defined @@ -440,6 +440,32 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ } } +void OSVRTrackedController::controllerButtonTouchCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report){ + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback()."; + if (!userdata) + return; + + auto* button_interface = static_cast(userdata); + OSVRTrackedController* self = button_interface->parentController; + if(!self){ + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). self isn't here :("; + return; + } + + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). report->sensor "<sensor; + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). button_id "<button_id; + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). objectId "<objectId_; + OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). report state "<state; + + // error checking + if((button_interface->button_id >=0 && button_interface->button_id <= 7) || (button_interface->button_id >= 31 && button_interface->button_id <= 36)){ + if (OSVR_BUTTON_PRESSED == report->state) { + vr::VRServerDriverHost()->TrackedDeviceButtonTouched(self->objectId_, button_interface->button_id, 0); + } else { + vr::VRServerDriverHost()->TrackedDeviceButtonUntouched(self->objectId_, button_interface->button_id, 0); + } + } +} void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback()."; @@ -641,6 +667,26 @@ void OSVRTrackedController::registerButton(int id, std::string path, vr::EVRButt OSVR_LOG(trace) << "OSVRTrackedController::registerButton end of function."; } +/** + * Registers a touch button based on the path and id. + * @param id the id of the button which is used for indexing an array. + * @param path the complete path to the button. + */ +void OSVRTrackedController::registerButtonTouch(int id, std::string path, vr::EVRButtonId button_id){ + OSVR_LOG(trace) << "OSVRTrackedController::registerButtonTouch start of function."; + OSVR_LOG(trace) << "OSVRTrackedController::registerButtonTouch id = "<(userdata); OSVRTrackedController* self = analog_interface->parentController; + OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before report"; analog_interface->x = report->state; + OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before set state"; vr::VRControllerAxis_t axis_state; axis_state.x = static_cast(analog_interface->x); + axis_state.y = 0; + OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before send to openvr"; vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } @@ -697,9 +702,12 @@ void OSVRTrackedController::registerTrigger(int id, std::string path){ analogInterface_[id].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Trigger; analogInterface_[id].parentController = this; analogInterface_[id].analogInterfaceX.registerCallback(&OSVRTrackedController::controllerTriggerCallback, &analogInterface_[id]); + propertyContainer_ = vr::VRProperties()->TrackedDeviceToPropertyContainer(this->objectId_); + vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis1Type_Int32, static_cast(analogInterface_[id].axisType)); } else { analogInterface_[id].analogInterfaceX.free(); } + OSVR_LOG(trace) << "OSVRTrackedController::registerTrigger end of function."; } void OSVRTrackedController::registerTrackpad(int id, std::string path){ From ad059b47cec03ccb25cecc5dc18b94267343fb8b Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Wed, 9 Aug 2017 22:18:08 +1200 Subject: [PATCH 14/24] Added battery interaction. --- src/OSVRTrackedController.cpp | 153 +++++++--------------------------- src/OSVRTrackedController.h | 9 +- 2 files changed, 38 insertions(+), 124 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 0c0e93a..acb18eb 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -51,14 +51,11 @@ // TODO: -// Trackpad +// Joystick support // OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::MotionController_Left_Thumbstick, "/controller/left/joystick/button"), // OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::MotionController_Left_Shoulder, "/controller/left/bumper"), // OSVRButton(OSVR_BUTTON_TYPE_DIGITAL, FGamepadKeyNames::SpecialLeft, "/controller/left/middle"), -// TODO: -// use vr::VRServerDriverHost() insteead of vr::IServerDriverHost -// Interface is IVRServerDriverHost OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& context, int controller_index) : OSVRTrackedDevice(context, vr::TrackedDeviceClass_Controller, "OSVR controller"+std::to_string(controller_index)), controllerIndex_(controller_index) { @@ -68,8 +65,6 @@ OSVRTrackedController::OSVRTrackedController(osvr::clientkit::ClientContext& con OSVRTrackedController::~OSVRTrackedController() { // DO NOTHING - //vr::IDriverLog* logger_ = nullptr; - //vr::IServerDriverHost* driver_host = nullptr; } vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) @@ -81,7 +76,6 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) const std::time_t wait_time = 5; // wait up to 5 seconds for init freeInterfaces(); - numAxis_ = 0; // Ensure context is fully started up OSVR_LOG(info) << "Waiting for the context to fully start up..."; @@ -101,45 +95,27 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) std::string triggerPath; std::string joystickPath; std::string trackpadPath; + std::string batteryPath; if (controllerIndex_ == 0) { trackerPath = "/me/hands/left"; buttonPath = "/controller/left/"; triggerPath = "/controller/left/trigger"; joystickPath = "/controller/left/joystick"; trackpadPath = "/controller/left/trackpad"; + batteryPath = "/controller/left/battery"; } else if (controllerIndex_ == 1) { trackerPath = "/me/hands/right"; buttonPath = "/controller/right/"; triggerPath = "/controller/right/trigger"; joystickPath = "/controller/right/joystick"; trackpadPath = "/controller/right/trackpad"; + batteryPath = "/controller/right/battery"; } else { buttonPath = "/controller" + std::to_string(controllerIndex_) + "/"; triggerPath = "/controller" + std::to_string(controllerIndex_) + "/trigger"; joystickPath = "/controller" + std::to_string(controllerIndex_) + "/joystick"; } - -/* -"controller": { - "left": { - "$target": "tracker/2", - "system": "button/3", - "menu": "button/2", - "grip": "button/4", - "trackpad": { - "x": "analog/0", - "y": "analog/1", - "touch": "button/5", - "button": "button/0" - }, - "trigger": { - "button": "button/1" - }, - "battery": "analog/2" - }, -*/ - // pathing // TRACKER @@ -217,87 +193,12 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) // register triggers registerTrigger(axis_id++, triggerPath); -/* - for (int iter_button = 0; iter_button < NUM_BUTTONS; iter_button++) { - buttonInterface_[iter_button] = context_.getInterface(buttonPath + std::to_string(iter_button)); - if (buttonInterface_[iter_button].notEmpty()) { - buttonInterface_[iter_button].registerCallback(&OSVRTrackedController::controllerButtonCallback, this); - } else { - buttonInterface_[iter_button].free(); - } - } -*/ - - OSVR_LOG(trace) << "OSVRTrackedController::Activate() before TODOtouchpad."; - // TODO: ADD TOUCHPAD PART HERE - numAxis_ = 0; - -/* - numAxis_ = 1; - for (int iter_trigger = 0; iter_trigger < NUM_TRIGGER; iter_trigger++) { - if (numAxis_ >= NUM_AXIS) - break; - - if (iter_trigger == 0) { - analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(triggerPath); - } else { - analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(triggerPath + std::to_string(iter_trigger)); - } - - if (analogInterface_[numAxis_].analogInterfaceX.notEmpty()) { - analogInterface_[numAxis_].axisIndex = numAxis_; - analogInterface_[numAxis_].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Trigger; - analogInterface_[numAxis_].analogInterfaceX.registerCallback(&OSVRTrackedController::controllerTriggerCallback, &analogInterface_[numAxis_]); - numAxis_++; - } else { - analogInterface_[numAxis_].analogInterfaceX.free(); - } - } -*/ - -/* - numAxis_ = 2; - for (int iter_joystick = 0; iter_joystick < NUM_JOYSTICKS; iter_joystick++) { - if (numAxis_ >= NUM_AXIS) - break; - - if (iter_joystick == 0) { - analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(joystickPath + "/x"); - analogInterface_[numAxis_].analogInterfaceY = context_.getInterface(joystickPath + "/y"); - } else { - analogInterface_[numAxis_].analogInterfaceX = context_.getInterface(joystickPath + std::to_string(iter_joystick) + "/x"); - analogInterface_[numAxis_].analogInterfaceY = context_.getInterface(joystickPath + std::to_string(iter_joystick) + "/y"); - } - - bool somethingFound = false; - - if (analogInterface_[numAxis_].analogInterfaceX.notEmpty()) { - analogInterface_[numAxis_].axisIndex = numAxis_; - analogInterface_[numAxis_].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Joystick; - analogInterface_[numAxis_].analogInterfaceX.registerCallback(&OSVRTrackedController::controllerJoystickXCallback, &analogInterface_[numAxis_]); - somethingFound = true; - } else { - analogInterface_[numAxis_].analogInterfaceX.free(); - } - - if (analogInterface_[numAxis_].analogInterfaceY.notEmpty()) { - analogInterface_[numAxis_].axisIndex = numAxis_; - analogInterface_[numAxis_].axisType = vr::EVRControllerAxisType::k_eControllerAxis_Joystick; - analogInterface_[numAxis_].analogInterfaceY.registerCallback(&OSVRTrackedController::controllerJoystickYCallback, &analogInterface_[numAxis_]); - somethingFound = true; - } else { - analogInterface_[numAxis_].analogInterfaceY.free(); - } - - if (somethingFound) - numAxis_++; - } -*/ + // register the battery + registerBattery(batteryPath); // added in for testing not sure if its correct or not. configure(); - OSVR_LOG(trace) << "OSVRTrackedController::Activate() end of function."; return vr::VRInitError_None; } @@ -325,12 +226,10 @@ bool OSVRTrackedController::TriggerHapticPulse(uint32_t axis_id, uint16_t pulse_ void OSVRTrackedController::freeInterfaces() { - OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces()."; if (trackerInterface_.notEmpty()) { trackerInterface_.free(); } - OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces() before freeing axis."; for (int iter_axis = 0; iter_axis < NUM_AXIS; iter_axis++) { if (analogInterface_[iter_axis].analogInterfaceX.notEmpty()) analogInterface_[iter_axis].analogInterfaceX.free(); @@ -338,12 +237,13 @@ void OSVRTrackedController::freeInterfaces() analogInterface_[iter_axis].analogInterfaceY.free(); } - OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces() before freeing buttons."; for (int iter_button = 0; iter_button < NUM_BUTTONS; iter_button++) { if (buttonInterface_[iter_button].buttonInterface.notEmpty()) buttonInterface_[iter_button].buttonInterface.free(); } - OSVR_LOG(trace) << "OSVRTrackedController::freeInterfaces() end."; + + if (batteryInterface.interface.notEmpty()) + batteryInterface.interface.free(); } inline vr::HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, double z) @@ -421,15 +321,6 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). objectId "<objectId_; OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). report state "<state; - /* - if ((report->sensor >= 0 && report->sensor <= 7) || (report->sensor >= 32 && report->sensor <= 36)) { - button_id = static_cast(report->sensor); - } else if (report->sensor >= 8 && report->sensor <= 12) { - button_id = static_cast(report->sensor + 24); - } else { - return; - } - */ // error checking if((button_interface->button_id >=0 && button_interface->button_id <= 7) || (button_interface->button_id >= 31 && button_interface->button_id <= 36)){ if (OSVR_BUTTON_PRESSED == report->state) { @@ -468,26 +359,32 @@ void OSVRTrackedController::controllerButtonTouchCallback(void* userdata, const } void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback()."; if (!userdata) return; - OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before cast"; auto* analog_interface = static_cast(userdata); OSVRTrackedController* self = analog_interface->parentController; - OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before report"; analog_interface->x = report->state; - OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before set state"; vr::VRControllerAxis_t axis_state; axis_state.x = static_cast(analog_interface->x); axis_state.y = 0; - OSVR_LOG(trace) << "OSVRTrackedController::controllerTriggerCallback(). before send to openvr"; vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); } +void OSVRTrackedController::controllerBatteryCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) +{ + if (!userdata) + return; + + auto* batteryInterface = static_cast(userdata); + OSVRTrackedController* self = batteryInterface->parentController; + vr::PropertyContainerHandle_t container = vr::VRProperties()->TrackedDeviceToPropertyContainer(self->objectId_); + vr::VRProperties()->SetFloatProperty(container, vr::Prop_DeviceBatteryPercentage_Float, report->state); +} + void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickXCallback()."; @@ -692,6 +589,16 @@ void OSVRTrackedController::registerButtonTouch(int id, std::string path, vr::EV OSVR_LOG(trace) << "OSVRTrackedController::registerButtonTouch end of function."; } +void OSVRTrackedController::registerBattery(std::string path){ + batteryInterface.interface = context_.getInterface(path); + if (batteryInterface.interface.notEmpty()) { + batteryInterface.parentController = this; + batteryInterface.interface.registerCallback(&OSVRTrackedController::controllerBatteryCallback, &batteryInterface); + } else { + batteryInterface.interface.free(); + } +} + void OSVRTrackedController::registerTrigger(int id, std::string path){ OSVR_LOG(trace) << "OSVRTrackedController::registerTrigger start of function."; OSVR_LOG(trace) << "OSVRTrackedController::registerTrigger id = "<pose_ = pose; - //self->driver_host->TrackedDevicePoseUpdated(self->objectId_, self->pose_); vr::VRServerDriverHost()->TrackedDevicePoseUpdated(self->objectId_, self->pose_,sizeof(self->pose_)); - //TrackedDevicePoseUpdated( uint32_t unWhichDevice, const DriverPose_t & newPose, uint32_t unPoseStructSize ) - //vr::IVRServerDriverHost::TrackedDevicePoseUpdated(uint32_t&, vr::DriverPose_t&)' } -void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report){ - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback()."; +void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report) +{ if (!userdata) return; auto* button_interface = static_cast(userdata); OSVRTrackedController* self = button_interface->parentController; if(!self){ - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). self isn't here :("; return; } - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). report->sensor "<sensor; - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). button_id "<button_id; - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). objectId "<objectId_; - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonCallback(). report state "<state; - // error checking if((button_interface->button_id >=0 && button_interface->button_id <= 7) || (button_interface->button_id >= 31 && button_interface->button_id <= 36)){ if (OSVR_BUTTON_PRESSED == report->state) { @@ -331,23 +267,17 @@ void OSVRTrackedController::controllerButtonCallback(void* userdata, const OSVR_ } } -void OSVRTrackedController::controllerButtonTouchCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report){ - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback()."; +void OSVRTrackedController::controllerButtonTouchCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_ButtonReport* report) +{ if (!userdata) return; auto* button_interface = static_cast(userdata); OSVRTrackedController* self = button_interface->parentController; if(!self){ - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). self isn't here :("; return; } - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). report->sensor "<sensor; - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). button_id "<button_id; - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). objectId "<objectId_; - OSVR_LOG(trace) << "OSVRTrackedController::controllerButtonTouchCallback(). report state "<state; - // error checking if((button_interface->button_id >=0 && button_interface->button_id <= 7) || (button_interface->button_id >= 31 && button_interface->button_id <= 36)){ if (OSVR_BUTTON_PRESSED == report->state) { @@ -357,6 +287,7 @@ void OSVRTrackedController::controllerButtonTouchCallback(void* userdata, const } } } + void OSVRTrackedController::controllerTriggerCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { if (!userdata) @@ -379,16 +310,14 @@ void OSVRTrackedController::controllerBatteryCallback(void* userdata, const OSVR if (!userdata) return; - OSVR_LOG(trace) << "OSVRTrackedController::controllerBatteryCallback() - data = "<state; - auto* batteryInterface = static_cast(userdata); - OSVRTrackedController* self = batteryInterface->parentController; + auto* batteryInterface = static_cast(userdata); + OSVRTrackedController* self = batteryInterface->parentController; vr::PropertyContainerHandle_t container = vr::VRProperties()->TrackedDeviceToPropertyContainer(self->objectId_); vr::VRProperties()->SetFloatProperty(container, vr::Prop_DeviceBatteryPercentage_Float, report->state); } void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickXCallback()."; if (!userdata) return; @@ -406,7 +335,6 @@ void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OS void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickYCallback()."; if (!userdata) return; @@ -424,7 +352,6 @@ void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OS void OSVRTrackedController::controllerXAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - //OSVR_LOG(trace) << "OSVRTrackedController::controllerXAxisCallback()."; if (!userdata) return; @@ -438,12 +365,10 @@ void OSVRTrackedController::controllerXAxisCallback(void* userdata, const OSVR_T axis_state.y = static_cast(analog_interface->y); vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); - //virtual void TrackedDeviceAxisUpdated( uint32_t unWhichDevice, uint32_t unWhichAxis, const VRControllerAxis_t & axisState ) = 0; } void OSVRTrackedController::controllerYAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { - //OSVR_LOG(trace) << "OSVRTrackedController::controllerJoystickYCallback()."; if (!userdata) return; @@ -461,32 +386,19 @@ void OSVRTrackedController::controllerYAxisCallback(void* userdata, const OSVR_T const char* OSVRTrackedController::GetId() { - OSVR_LOG(trace) << "OSVRTrackedController::GetId()."; /// @todo When available, return the actual unique ID of the HMD return name_.c_str(); } void OSVRTrackedController::configure() { - OSVR_LOG(trace) << "OSVRTrackedController::configure()."; configureProperties(); } void OSVRTrackedController::configureProperties() { - OSVR_LOG(trace) << "OSVRTrackedController::configureProperties()."; - OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() NAME = "<SetStringProperty(propertyContainer_, vr::Prop_ManufacturerName_String, "LYRobotix"); @@ -499,17 +411,8 @@ void OSVRTrackedController::configureProperties() vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_DriverVersion_String, "0.1.0"); vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_AttachedDeviceId_String, "3000"); vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_ModeLabel_String, ""); - - - //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_WillDriftInYaw_Bool, true); //vr::VRProperties()->SetBoolProperty(propertyContainer_, vr::Prop_DeviceIsWireless_Bool, false); - //ETrackedPropertyError SetBoolProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, bool bNewValue ); - //ETrackedPropertyError SetFloatProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, float fNewValue ); - //ETrackedPropertyError SetInt32Property( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, int32_t nNewValue ); - //ETrackedPropertyError SetUint64Property( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, uint64_t ulNewValue ); - //ETrackedPropertyError SetStringProperty( PropertyContainerHandle_t ulContainerHandle, ETrackedDeviceProperty prop, const char *pchNewValue ); - vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_DeviceClass_Int32, static_cast(deviceClass_)); /* vr::VRProperties()->SetInt32Property(propertyContainer_, vr::Prop_Axis0Type_Int32, static_cast(analogInterface_[0].axisType)); @@ -524,30 +427,6 @@ void OSVRTrackedController::configureProperties() // set the vive controller at the default. vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, "vr_controller_vive_1_5"); //vr::VRProperties()->SetStringProperty(propertyContainer_, vr::Prop_RenderModelName_String, settings_->getSetting("cameraRenderModel", "").c_str()); - - //properties_[vr::Prop_DeviceClass_Int32] = static_cast(deviceClass_); - //properties_[vr::Prop_Axis0Type_Int32] = static_cast(analogInterface_[0].axisType); - //properties_[vr::Prop_Axis1Type_Int32] = static_cast(analogInterface_[1].axisType); - //properties_[vr::Prop_Axis2Type_Int32] = static_cast(analogInterface_[2].axisType); - //properties_[vr::Prop_Axis3Type_Int32] = static_cast(analogInterface_[3].axisType); - //properties_[vr::Prop_Axis4Type_Int32] = static_cast(analogInterface_[4].axisType); - - //properties_[vr::Prop_SupportedButtons_Uint64] = static_cast(NUM_BUTTONS); - - //properties_[vr::Prop_ModelNumber_String] = "OSVR Controller"; - //properties_[vr::Prop_SerialNumber_String] = controllerName_.c_str(); - //properties_[vr::Prop_RenderModelName_String] = ""; - - // Properties that are unique to TrackedDeviceClass_Controller - //Prop_AttachedDeviceId_String = 3000, - //Prop_SupportedButtons_Uint64 = 3001, - //Prop_Axis0Type_Int32 = 3002, // Return value is of type EVRControllerAxisType - //Prop_Axis1Type_Int32 = 3003, // Return value is of type EVRControllerAxisType - //Prop_Axis2Type_Int32 = 3004, // Return value is of type EVRControllerAxisType - //Prop_Axis3Type_Int32 = 3005, // Return value is of type EVRControllerAxisType - //Prop_Axis4Type_Int32 = 3006, // Return value is of type EVRControllerAxisType - - OSVR_LOG(trace) << "OSVRTrackedController::configureProperties() end of function."; } /** @@ -556,18 +435,14 @@ void OSVRTrackedController::configureProperties() * @param path the complete path to the button. */ void OSVRTrackedController::registerButton(int id, std::string path, vr::EVRButtonId button_id){ - OSVR_LOG(trace) << "OSVRTrackedController::registerButton start of function."; - OSVR_LOG(trace) << "OSVRTrackedController::registerButton id = "< Date: Wed, 9 Aug 2017 23:02:21 +1200 Subject: [PATCH 17/24] Removed comments. --- src/ServerDriver_OSVR.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index e4300d2..094a2b0 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -87,9 +87,7 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IVRDriverContext* driver_context) trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); - OSVR_LOG(debug) << "ServerDriver_OSVR - before left hand\n"; trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 0)); // left hand - OSVR_LOG(debug) << "ServerDriver_OSVR - before right hand\n"; trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 1)); // right hand for (auto& tracked_device : trackedDevices_) { From bd45e00f20d9dce0326ef3d341c32f91d5f39cdc Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 16:29:42 +1200 Subject: [PATCH 18/24] changed openvr path back. --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 795e89e..aea5c43 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "openvr"] path = openvr - url = https://github.com/ValveSoftware/openvr + url = https://github.com/ValveSoftware/openvr.git [submodule "vendor/OSVR-Display"] path = vendor/OSVR-Display url = https://github.com/OSVR/OSVR-Display.git From 0e1eb86bb6571c6070df4d794055f3db080335c7 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 18:21:20 +1200 Subject: [PATCH 19/24] Using enum to differentiate between left and right controller. --- src/OSVRTrackedController.cpp | 4 ++-- src/ServerDriver_OSVR.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index e3792f2..b3b2aee 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -90,14 +90,14 @@ vr::EVRInitError OSVRTrackedController::Activate(uint32_t object_id) std::string joystickPath; std::string trackpadPath; std::string batteryPath; - if (controllerIndex_ == 0) { + if (controllerIndex_ == vr::TrackedControllerRole_LeftHand) { trackerPath = "/me/hands/left"; buttonPath = "/controller/left/"; triggerPath = "/controller/left/trigger"; joystickPath = "/controller/left/joystick"; trackpadPath = "/controller/left/trackpad"; batteryPath = "/controller/left/battery"; - } else if (controllerIndex_ == 1) { + } else if (controllerIndex_ == vr::TrackedControllerRole_RightHand) { trackerPath = "/me/hands/right"; buttonPath = "/controller/right/"; triggerPath = "/controller/right/trigger"; diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index 094a2b0..ab65fb6 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -87,8 +87,8 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IVRDriverContext* driver_context) trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); trackedDevices_.emplace_back(std::make_unique(*(context_.get()))); - trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 0)); // left hand - trackedDevices_.emplace_back(std::make_unique(*(context_.get()), 1)); // right hand + trackedDevices_.emplace_back(std::make_unique(*(context_.get()), vr::TrackedControllerRole_LeftHand)); // left hand + trackedDevices_.emplace_back(std::make_unique(*(context_.get()), vr::TrackedControllerRole_RightHand)); // right hand for (auto& tracked_device : trackedDevices_) { OSVR_LOG(debug) << "ServerDriver_OSVR - for loop - before TrackedDeviceAdded\n"; From 4f46a403cbf00bd925197c88b323e7a3b17fd47e Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 18:22:36 +1200 Subject: [PATCH 20/24] Removed unnecessary log entry. --- src/ServerDriver_OSVR.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ServerDriver_OSVR.cpp b/src/ServerDriver_OSVR.cpp index ab65fb6..f6ac2c8 100644 --- a/src/ServerDriver_OSVR.cpp +++ b/src/ServerDriver_OSVR.cpp @@ -91,9 +91,7 @@ vr::EVRInitError ServerDriver_OSVR::Init(vr::IVRDriverContext* driver_context) trackedDevices_.emplace_back(std::make_unique(*(context_.get()), vr::TrackedControllerRole_RightHand)); // right hand for (auto& tracked_device : trackedDevices_) { - OSVR_LOG(debug) << "ServerDriver_OSVR - for loop - before TrackedDeviceAdded\n"; vr::VRServerDriverHost()->TrackedDeviceAdded(tracked_device->getId(), tracked_device->getDeviceClass(), tracked_device.get()); - OSVR_LOG(debug) << "ServerDriver_OSVR - for loop - after TrackedDeviceAdded\n"; } client_update_thread_quit.store(false); From 61c5bbabc86e778eca521b3d7669c159439c1d5c Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 18:25:37 +1200 Subject: [PATCH 21/24] Added back in commented out (due to version mismatch). --- src/PrettyPrint.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PrettyPrint.cpp b/src/PrettyPrint.cpp index d16d722..08d3f09 100644 --- a/src/PrettyPrint.cpp +++ b/src/PrettyPrint.cpp @@ -127,8 +127,8 @@ std::string to_string(const vr::ETrackedDeviceProperty& value) return "Prop_ViveSystemButtonFixRequired_Bool"; case vr::Prop_ParentDriver_Uint64: return "Prop_ParentDriver_Uint64"; - //case vr::Prop_ResourceRoot_String: - // return "Prop_ResourceRoot_String"; + case vr::Prop_ResourceRoot_String: + return "Prop_ResourceRoot_String"; case vr::Prop_ReportsTimeSinceVSync_Bool: return "Prop_ReportsTimeSinceVSync_Bool"; case vr::Prop_SecondsFromVsyncToPhotons_Float: From 62fd7b725a9440fca61c99aa9a390b9fde0c4e4b Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 18:59:33 +1200 Subject: [PATCH 22/24] Removed unused function. --- src/OSVRTrackedController.cpp | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index b3b2aee..c4af5e9 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -316,40 +316,6 @@ void OSVRTrackedController::controllerBatteryCallback(void* userdata, const OSVR vr::VRProperties()->SetFloatProperty(container, vr::Prop_DeviceBatteryPercentage_Float, report->state); } -void OSVRTrackedController::controllerJoystickXCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) -{ - if (!userdata) - return; - - auto* analog_interface = static_cast(userdata); - OSVRTrackedController* self = analog_interface->parentController; - - analog_interface->x = report->state; - - vr::VRControllerAxis_t axis_state; - axis_state.x = static_cast(analog_interface->x); - axis_state.y = static_cast(analog_interface->y); - - vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); -} - -void OSVRTrackedController::controllerJoystickYCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) -{ - if (!userdata) - return; - - auto* analog_interface = static_cast(userdata); - OSVRTrackedController* self = analog_interface->parentController; - - analog_interface->y = report->state; - - vr::VRControllerAxis_t axis_state; - axis_state.x = static_cast(analog_interface->x); - axis_state.y = static_cast(analog_interface->y); - - vr::VRServerDriverHost()->TrackedDeviceAxisUpdated(self->objectId_, analog_interface->axisIndex, axis_state); -} - void OSVRTrackedController::controllerXAxisCallback(void* userdata, const OSVR_TimeValue* timestamp, const OSVR_AnalogReport* report) { if (!userdata) From 2cd70f4a45dfcf8cdd3aa5875bf4ccd5abb51d2b Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 19:03:14 +1200 Subject: [PATCH 23/24] String optimization --- src/OSVRTrackedController.cpp | 10 +++++----- src/OSVRTrackedController.h | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index c4af5e9..834b888 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -400,7 +400,7 @@ void OSVRTrackedController::configureProperties() * @param id the id of the button which is used for indexing an array. * @param path the complete path to the button. */ -void OSVRTrackedController::registerButton(int id, std::string path, vr::EVRButtonId button_id){ +void OSVRTrackedController::registerButton(int id, const std::string& path, vr::EVRButtonId button_id){ buttonInterface_[id].buttonInterface = context_.getInterface(path); if (buttonInterface_[id].buttonInterface.notEmpty()) { buttonInterface_[id].button_id = button_id; @@ -416,7 +416,7 @@ void OSVRTrackedController::registerButton(int id, std::string path, vr::EVRButt * @param id the id of the button which is used for indexing an array. * @param path the complete path to the button. */ -void OSVRTrackedController::registerButtonTouch(int id, std::string path, vr::EVRButtonId button_id){ +void OSVRTrackedController::registerButtonTouch(int id, const std::string& path, vr::EVRButtonId button_id){ buttonInterface_[id].buttonInterface = context_.getInterface(path); if (buttonInterface_[id].buttonInterface.notEmpty()) { buttonInterface_[id].button_id = button_id; @@ -427,7 +427,7 @@ void OSVRTrackedController::registerButtonTouch(int id, std::string path, vr::EV } } -void OSVRTrackedController::registerBattery(std::string path){ +void OSVRTrackedController::registerBattery(const std::string& path){ batteryInterface.interface = context_.getInterface(path); if (batteryInterface.interface.notEmpty()) { batteryInterface.parentController = this; @@ -439,7 +439,7 @@ void OSVRTrackedController::registerBattery(std::string path){ } } -void OSVRTrackedController::registerTrigger(int id, std::string path){ +void OSVRTrackedController::registerTrigger(int id, const std::string& path){ analogInterface_[id].analogInterfaceX = context_.getInterface(path); if (analogInterface_[id].analogInterfaceX.notEmpty()) { analogInterface_[id].axisIndex = id; @@ -453,7 +453,7 @@ void OSVRTrackedController::registerTrigger(int id, std::string path){ } } -void OSVRTrackedController::registerTrackpad(int id, std::string path){ +void OSVRTrackedController::registerTrackpad(int id, const std::string& path){ analogInterface_[id].analogInterfaceX = context_.getInterface(path + "/x"); analogInterface_[id].analogInterfaceY = context_.getInterface(path + "/y"); diff --git a/src/OSVRTrackedController.h b/src/OSVRTrackedController.h index 667b21d..70013ee 100644 --- a/src/OSVRTrackedController.h +++ b/src/OSVRTrackedController.h @@ -144,11 +144,11 @@ class OSVRTrackedController : public OSVRTrackedDevice, public vr::IVRController AnalogInterface analogInterface_[NUM_AXIS]; ButtonInterface buttonInterface_[NUM_BUTTONS]; BatteryInterface batteryInterface; - void registerButton(int id, std::string path, vr::EVRButtonId button_id); - void registerButtonTouch(int id, std::string path, vr::EVRButtonId button_id); - void registerTrigger(int id, std::string path); - void registerTrackpad(int id, std::string path); - void registerBattery(std::string path); + void registerButton(int id, const std::string& path, vr::EVRButtonId button_id); + void registerButtonTouch(int id, const std::string& path, vr::EVRButtonId button_id); + void registerTrigger(int id, const std::string& path); + void registerTrackpad(int id, const std::string& path); + void registerBattery(const std::string& path ); }; #endif // INCLUDED_OSVRTrackedDevice_h_GUID_128E3B29_F5FC_4221_9B38_14E3F402E645 From 3e3b4b4b24e2a46e825ad0271f7fbd7926862538 Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 11 Aug 2017 19:12:35 +1200 Subject: [PATCH 24/24] Added error checking for array bounds. --- src/OSVRTrackedController.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/OSVRTrackedController.cpp b/src/OSVRTrackedController.cpp index 834b888..ff0807f 100644 --- a/src/OSVRTrackedController.cpp +++ b/src/OSVRTrackedController.cpp @@ -401,6 +401,11 @@ void OSVRTrackedController::configureProperties() * @param path the complete path to the button. */ void OSVRTrackedController::registerButton(int id, const std::string& path, vr::EVRButtonId button_id){ + // check bounds + if(id < 0 || id >= NUM_BUTTONS){ + return; + } + buttonInterface_[id].buttonInterface = context_.getInterface(path); if (buttonInterface_[id].buttonInterface.notEmpty()) { buttonInterface_[id].button_id = button_id; @@ -417,6 +422,11 @@ void OSVRTrackedController::registerButton(int id, const std::string& path, vr:: * @param path the complete path to the button. */ void OSVRTrackedController::registerButtonTouch(int id, const std::string& path, vr::EVRButtonId button_id){ + // check bounds + if(id < 0 || id >= NUM_BUTTONS){ + return; + } + buttonInterface_[id].buttonInterface = context_.getInterface(path); if (buttonInterface_[id].buttonInterface.notEmpty()) { buttonInterface_[id].button_id = button_id; @@ -440,6 +450,10 @@ void OSVRTrackedController::registerBattery(const std::string& path){ } void OSVRTrackedController::registerTrigger(int id, const std::string& path){ + // check bounds + if(id < 0 || id >= NUM_AXIS){ + return; + } analogInterface_[id].analogInterfaceX = context_.getInterface(path); if (analogInterface_[id].analogInterfaceX.notEmpty()) { analogInterface_[id].axisIndex = id; @@ -454,6 +468,10 @@ void OSVRTrackedController::registerTrigger(int id, const std::string& path){ } void OSVRTrackedController::registerTrackpad(int id, const std::string& path){ + // check bounds + if(id < 0 || id >= NUM_AXIS){ + return; + } analogInterface_[id].analogInterfaceX = context_.getInterface(path + "/x"); analogInterface_[id].analogInterfaceY = context_.getInterface(path + "/y");