diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml new file mode 100644 index 0000000..0b1a9e3 --- /dev/null +++ b/.github/workflows/c-cpp.yml @@ -0,0 +1,80 @@ +name: SlimeVR OpenVR Driver + +on: [ push, pull_request ] + +jobs: + build: + + name: Build for ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-latest] # Others are disabled, this only targets Windows + include: + - os: windows-latest + triplet: x64-windows + env: + # Indicates the CMake build directory where project files and binaries are being produced. + CMAKE_BUILD_DIR: ${{ github.workspace }}/build + # Indicates the location of the vcpkg as a Git submodule of the project repository. + VCPKG_ROOT: ${{ github.workspace }}/vcpkg + + steps: + - uses: actions/checkout@v2 + with: + submodules: true + + - uses: lukka/get-cmake@latest + + - name: Clone vcpkg + uses: actions/checkout@v2 + with: + repository: microsoft/vcpkg + path: ${{ env.VCPKG_ROOT }} + submodules: true + + - name: Restore vcpkg and its artifacts + uses: actions/cache@v2 + with: + # The first path is where vcpkg generates artifacts while consuming the vcpkg.json manifest file. + # The second path is the location of vcpkg (it contains the vcpkg executable and data files). + # The other paths starting with "!" are exclusions: they contain termporary files generated during the build of the installed packages. + path: | + ${{ env.CMAKE_BUILD_DIR }}/vcpkg_installed/ + ${{ env.VCPKG_ROOT }} + !${{ env.VCPKG_ROOT }}/buildtrees + !${{ env.VCPKG_ROOT }}/packages + !${{ env.VCPKG_ROOT }}/downloads + # The key is composed in a way that it gets properly invalidated: this must happen whenever vcpkg's Git commit id changes, or the list of packages changes. In this case a cache miss must happen and a new entry with a new key with be pushed to GitHub the cache service. + # The key includes: hash of the vcpkg.json file, the hash of the vcpkg Git commit id, and the used vcpkg's triplet. The vcpkg's commit id would suffice, but computing an hash out it does not harm. + # Note: given a key, the cache content is immutable. If a cache entry has been created improperly, in order the recreate the right content the key must be changed as well, and it must be brand new (i.e. not existing already). + key: | + ${{ hashFiles( 'vcpkg_manifest/vcpkg.json' ) }}-${{ hashFiles( '.git/modules/vcpkg/HEAD' )}}-${{ hashFiles( '${{ env.VCPKG_ROOT }}/.git/HEAD' )}}-${{ matrix.triplet }}-invalidate + + - if: matrix.os == 'windows-latest' + name: Set up vcpkg for Windows + run: ${{ env.VCPKG_ROOT }}/bootstrap-vcpkg.bat + + - if: matrix.os != 'windows-latest' + name: Set up vcpkg for Unix + run: ${{ env.VCPKG_ROOT }}/bootstrap-vcpkg.sh + + - name: Install dependencies + run: ${{ env.VCPKG_ROOT }}/vcpkg install protobuf protobuf:${{ matrix.triplet }} + + - name: Configure CMake + run: cmake -S "${{github.workspace}}" -B "${{env.CMAKE_BUILD_DIR}}" + + - name: Build + run: cmake --build "${{env.CMAKE_BUILD_DIR}}" --config Release --target ALL_BUILD -j 6 -- + + - name: Upload a build artifact + uses: actions/upload-artifact@v2 + with: + # Artifact name + name: slimevr-openvr-driver-${{ matrix.triplet }} # optional, default is artifact + # A file, directory or wildcard pattern that describes what to upload + # Using wildcards so that only the driver directory gets included (if you specify it, then it won't be included) + path: | + ${{env.CMAKE_BUILD_DIR}}/Release/driver/* diff --git a/CMakeLists.txt b/CMakeLists.txt index 011f536..7716374 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,29 @@ cmake_minimum_required(VERSION 3.0.0) -project(SlimeVR-OpenVR-Driver VERSION 0.1.0) + +# If the toolchain is already defined, do not attempt to find it +if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) + # If the VCPKG_ROOT environment variable is not defined, try to automatically define it from AppData/home + if(NOT DEFINED ENV{VCPKG_ROOT}) + if(WIN32) + file(READ "$ENV{LOCALAPPDATA}/vcpkg/vcpkg.path.txt" VCPKG_ROOT) + elseif(UNIX) + file(READ "$ENV{HOME}/.vcpkg/vcpkg.path.txt" VCPKG_ROOT) + endif() + + set(VCPKG_PATH "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") + else() + set(VCPKG_PATH "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") + endif() + + if(EXISTS "${VCPKG_PATH}") + message("vcpkg CMake toolchain was found at \"${VCPKG_PATH}\"") + set(CMAKE_TOOLCHAIN_FILE "${VCPKG_PATH}") + else() + message(FATAL_ERROR "vcpkg could not be found") + endif() +endif() + +project(SlimeVR-OpenVR-Driver VERSION 0.2.0) set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(DRIVER_NAME "SlimeVR") @@ -26,15 +50,26 @@ endif() find_library(OPENVR_LIB openvr_api HINTS "${CMAKE_CURRENT_SOURCE_DIR}/libraries/openvr/lib/${PLATFORM_NAME}${PROCESSOR_ARCH}/" NO_DEFAULT_PATH ) +# Protobuf +# Installation: +# Please refer to this readme to install protobuf in your system: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md +# WARNING: CLang has an arror building protobuf messages, use MSVC 2019 +INCLUDE(FindProtobuf) +find_package(protobuf CONFIG REQUIRED) +protobuf_generate_cpp(PROTO_SRC PROTO_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/src/bridge/ProtobufMessages.proto") +SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE) + # Project file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp") file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") -add_library("${PROJECT_NAME}" SHARED "${HEADERS}" "${SOURCES}") +add_library("${PROJECT_NAME}" SHARED "${HEADERS}" "${SOURCES}" ${PROTO_HEADER} ${PROTO_SRC}) target_include_directories("${PROJECT_NAME}" PUBLIC "${OPENVR_INCLUDE_DIR}") target_include_directories("${PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/libraries/linalg") target_include_directories("${PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src/") -target_link_libraries("${PROJECT_NAME}" PUBLIC "${OPENVR_LIB}") +target_link_libraries("${PROJECT_NAME}" PUBLIC "${OPENVR_LIB}" protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite) set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 17) +include_directories(${Protobuf_INCLUDE_DIRS}) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) # IDE Config source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Header Files" FILES ${HEADERS}) @@ -62,3 +97,12 @@ add_custom_command( $ $/driver/${DRIVER_NAME}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/driver_${DRIVER_NAME}$ ) + +# Copy libprotobuf dll to output folder +add_custom_command( + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + $/libprotobuf.dll + $/driver/${DRIVER_NAME}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/libprotobuf.dll +) diff --git a/src/ControllerDevice.cpp b/src/ControllerDevice.cpp index 474bbb8..39331b8 100644 --- a/src/ControllerDevice.cpp +++ b/src/ControllerDevice.cpp @@ -1,9 +1,8 @@ #include "ControllerDevice.hpp" #include -SlimeVRDriver::ControllerDevice::ControllerDevice(std::string serial, ControllerDevice::Handedness handedness): - serial_(serial), - handedness_(handedness) +SlimeVRDriver::ControllerDevice::ControllerDevice(std::string serial, int deviceId, ControllerDevice::Handedness handedness): + serial_(serial), handedness_(handedness), deviceId_(deviceId) { } @@ -186,6 +185,27 @@ vr::EVRInitError SlimeVRDriver::ControllerDevice::Activate(uint32_t unObjectId) return vr::EVRInitError::VRInitError_None; } +void SlimeVRDriver::ControllerDevice::PositionMessage(messages::Position &position) +{ + // Setup pose for this frame + auto pose = this->last_pose_; + //send the new position and rotation from the pipe to the tracker object + if(position.has_x()) { + pose.vecPosition[0] = position.x(); + pose.vecPosition[1] = position.y(); + pose.vecPosition[2] = position.z(); + } + + pose.qRotation.w = position.qw(); + pose.qRotation.x = position.qx(); + pose.qRotation.y = position.qy(); + pose.qRotation.z = position.qz(); + + // Post pose + GetDriver()->GetDriverHost()->TrackedDevicePoseUpdated(this->device_index_, pose, sizeof(vr::DriverPose_t)); + this->last_pose_ = pose; +} + void SlimeVRDriver::ControllerDevice::Deactivate() { this->device_index_ = vr::k_unTrackedDeviceIndexInvalid; @@ -210,3 +230,8 @@ vr::DriverPose_t SlimeVRDriver::ControllerDevice::GetPose() { return last_pose_; } + +int SlimeVRDriver::ControllerDevice::getDeviceId() +{ + return deviceId_; +} diff --git a/src/ControllerDevice.hpp b/src/ControllerDevice.hpp index eb558ce..850cd0a 100644 --- a/src/ControllerDevice.hpp +++ b/src/ControllerDevice.hpp @@ -18,7 +18,7 @@ namespace SlimeVRDriver { ANY }; - ControllerDevice(std::string serial, Handedness handedness = Handedness::ANY); + ControllerDevice(std::string serial, int deviceId, Handedness handedness = Handedness::ANY); ~ControllerDevice() = default; // Inherited via IVRDevice @@ -34,6 +34,8 @@ namespace SlimeVRDriver { virtual void* GetComponent(const char* pchComponentNameAndVersion) override; virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) override; virtual vr::DriverPose_t GetPose() override; + virtual int getDeviceId() override; + virtual void PositionMessage(messages::Position &position) override; private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; @@ -43,6 +45,7 @@ namespace SlimeVRDriver { vr::DriverPose_t last_pose_; bool did_vibrate_ = false; + int deviceId_; float vibrate_anim_state_ = 0.f; vr::VRInputComponentHandle_t haptic_component_ = 0; diff --git a/src/DeviceType.hpp b/src/DeviceType.hpp index 6ed83ac..c2d362b 100644 --- a/src/DeviceType.hpp +++ b/src/DeviceType.hpp @@ -1,4 +1,5 @@ #pragma once + enum class DeviceType { HMD, CONTROLLER, diff --git a/src/HMDDevice.cpp b/src/HMDDevice.cpp index 7da4d6e..c75d278 100644 --- a/src/HMDDevice.cpp +++ b/src/HMDDevice.cpp @@ -1,7 +1,8 @@ #include "HMDDevice.hpp" #include -SlimeVRDriver::HMDDevice::HMDDevice(std::string serial):serial_(serial) +SlimeVRDriver::HMDDevice::HMDDevice(std::string serial, int deviceId): + serial_(serial), deviceId_(deviceId) { } @@ -58,6 +59,27 @@ void SlimeVRDriver::HMDDevice::Update() this->last_pose_ = pose; } +void SlimeVRDriver::HMDDevice::PositionMessage(messages::Position &position) +{ + // Setup pose for this frame + auto pose = this->last_pose_; + //send the new position and rotation from the pipe to the tracker object + if(position.has_x()) { + pose.vecPosition[0] = position.x(); + pose.vecPosition[1] = position.y(); + pose.vecPosition[2] = position.z(); + } + + pose.qRotation.w = position.qw(); + pose.qRotation.x = position.qx(); + pose.qRotation.y = position.qy(); + pose.qRotation.z = position.qz(); + + // Post pose + GetDriver()->GetDriverHost()->TrackedDevicePoseUpdated(this->device_index_, pose, sizeof(vr::DriverPose_t)); + this->last_pose_ = pose; +} + DeviceType SlimeVRDriver::HMDDevice::GetDeviceType() { return DeviceType::HMD; @@ -220,4 +242,9 @@ vr::DistortionCoordinates_t SlimeVRDriver::HMDDevice::ComputeDistortion(vr::EVRE coordinates.rfRed[0] = fU; coordinates.rfRed[1] = fV; return coordinates; +} + +int SlimeVRDriver::HMDDevice::getDeviceId() +{ + return deviceId_; } \ No newline at end of file diff --git a/src/HMDDevice.hpp b/src/HMDDevice.hpp index f58c95d..a38e59d 100644 --- a/src/HMDDevice.hpp +++ b/src/HMDDevice.hpp @@ -11,7 +11,7 @@ namespace SlimeVRDriver { class HMDDevice : public IVRDevice, public vr::IVRDisplayComponent { public: - HMDDevice(std::string serial); + HMDDevice(std::string serial, int deviceId); ~HMDDevice() = default; // Inherited via IVRDevice @@ -35,9 +35,12 @@ namespace SlimeVRDriver { virtual void GetEyeOutputViewport(vr::EVREye eEye, uint32_t* pnX, uint32_t* pnY, uint32_t* pnWidth, uint32_t* pnHeight) override; virtual void GetProjectionRaw(vr::EVREye eEye, float* pfLeft, float* pfRight, float* pfTop, float* pfBottom) override; virtual vr::DistortionCoordinates_t ComputeDistortion(vr::EVREye eEye, float fU, float fV) override; + virtual int getDeviceId() override; + virtual void PositionMessage(messages::Position &position) override; private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; std::string serial_; + int deviceId_; vr::DriverPose_t last_pose_ = IVRDevice::MakeDefaultPose(); diff --git a/src/IVRDevice.hpp b/src/IVRDevice.hpp index 18d8bf5..6e58de8 100644 --- a/src/IVRDevice.hpp +++ b/src/IVRDevice.hpp @@ -3,6 +3,7 @@ #include #include #include +#include "ProtobufMessages.pb.h" namespace SlimeVRDriver { @@ -58,6 +59,9 @@ namespace SlimeVRDriver { virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) = 0; virtual vr::DriverPose_t GetPose() = 0; + virtual int getDeviceId() = 0; + virtual void PositionMessage(messages::Position& position) = 0; + ~IVRDevice() = default; }; }; \ No newline at end of file diff --git a/src/TrackerDevice.cpp b/src/TrackerDevice.cpp index 6cb8e30..9ad99da 100644 --- a/src/TrackerDevice.cpp +++ b/src/TrackerDevice.cpp @@ -1,10 +1,10 @@ #include "TrackerDevice.hpp" #include -SlimeVRDriver::TrackerDevice::TrackerDevice(std::string serial, HANDLE pipe, int deviceId): - serial_(serial), hpipe(pipe) + +SlimeVRDriver::TrackerDevice::TrackerDevice(std::string serial, int deviceId, TrackerRole trackerRole_): + serial_(serial), trackerRole(trackerRole_), deviceId_(deviceId) { - this->deviceId = deviceId; this->last_pose_ = MakeDefaultPose(); this->isSetup = false; } @@ -40,64 +40,27 @@ void SlimeVRDriver::TrackerDevice::Update() this->vibrate_anim_state_ = 0.0f; } } +} +void SlimeVRDriver::TrackerDevice::PositionMessage(messages::Position &position) +{ // Setup pose for this frame auto pose = this->last_pose_; - - if (PeekNamedPipe(hpipe, NULL, 0, NULL, &dwRead, NULL) != FALSE) - { - //if data is ready, - if (dwRead > 0) - { - //we go and read it into our buffer - if (ReadFile(hpipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE) - { - - buffer[dwRead] = '\0'; //add terminating zero - //convert our buffer to string - std::string s = buffer; - - //first three variables are a position vector - double a; - double b; - double c; - - //second four are rotation quaternion - double qw; - double qx; - double qy; - double qz; - - //convert to string stream - std::istringstream iss(s); - - //read to our variables - iss >> a; - iss >> b; - iss >> c; - iss >> qw; - iss >> qx; - iss >> qy; - iss >> qz; - - //send the new position and rotation from the pipe to the tracker object - pose.vecPosition[0] = a; - pose.vecPosition[1] = b; - pose.vecPosition[2] = c; - - pose.qRotation.w = qw; - pose.qRotation.x = qx; - pose.qRotation.y = qy; - pose.qRotation.z = qz; - - // Post pose - GetDriver()->GetDriverHost()->TrackedDevicePoseUpdated(this->device_index_, pose, sizeof(vr::DriverPose_t)); - this->last_pose_ = pose; - } - } - } - + //send the new position and rotation from the pipe to the tracker object + if(position.has_x()) { + pose.vecPosition[0] = position.x(); + pose.vecPosition[1] = position.y(); + pose.vecPosition[2] = position.z(); + } + pose.qRotation.w = position.qw(); + pose.qRotation.x = position.qx(); + pose.qRotation.y = position.qy(); + pose.qRotation.z = position.qz(); + + // Post pose + GetDriver()->GetDriverHost()->TrackedDevicePoseUpdated(this->device_index_, pose, sizeof(vr::DriverPose_t)); + this->last_pose_ = pose; } DeviceType SlimeVRDriver::TrackerDevice::GetDeviceType() @@ -114,17 +77,11 @@ vr::EVRInitError SlimeVRDriver::TrackerDevice::Activate(uint32_t unObjectId) { this->device_index_ = unObjectId; - GetDriver()->Log("[SlimeVR] Activating tracker " + this->serial_); + GetDriver()->Log("Activating tracker " + this->serial_); // Get the properties handle auto props = GetDriver()->GetProperties()->TrackedDeviceToPropertyContainer(this->device_index_); - // Setup inputs and outputs - //GetDriver()->GetInput()->CreateHapticComponent(props, "/output/haptic", &this->haptic_component_); - - //GetDriver()->GetInput()->CreateBooleanComponent(props, "/input/system/click", &this->system_click_component_); - //GetDriver()->GetInput()->CreateBooleanComponent(props, "/input/system/touch", &this->system_touch_component_); - // Set some universe ID (Must be 2 or higher) GetDriver()->GetProperties()->SetUint64Property(props, vr::Prop_CurrentUniverseId_Uint64, 4); @@ -133,13 +90,12 @@ vr::EVRInitError SlimeVRDriver::TrackerDevice::Activate(uint32_t unObjectId) // Opt out of hand selection GetDriver()->GetProperties()->SetInt32Property(props, vr::Prop_ControllerRoleHint_Int32, vr::ETrackedControllerRole::TrackedControllerRole_OptOut); + vr::VRProperties()->SetInt32Property(props, vr::Prop_DeviceClass_Int32, vr::TrackedDeviceClass_GenericTracker); + vr::VRProperties()->SetInt32Property(props, vr::Prop_ControllerHandSelectionPriority_Int32, -1); // Set up a render model path GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_RenderModelName_String, "{htc}/rendermodels/vr_tracker_vive_1_0"); - // Set controller profile - //GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_InputProfilePath_String, "{slimevr}/input/example_tracker_bindings.json"); - // Set the icon GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceReady_String, "{slimevr}/icons/tracker_ready.png"); @@ -151,18 +107,14 @@ vr::EVRInitError SlimeVRDriver::TrackerDevice::Activate(uint32_t unObjectId) GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceStandby_String, "{slimevr}/icons/tracker_not_ready.png"); GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceAlertLow_String, "{slimevr}/icons/tracker_not_ready.png"); - switch (deviceId) - { - case 0: - GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_ControllerType_String, "vive_tracker_waist"); - break; - case 1: - GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_ControllerType_String, "vive_tracker_left_foot"); - break; - case 2: - GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_ControllerType_String, "vive_tracker_right_foot"); - break; - } + // Automatically select vive tracker roles and set hints for games that need it (Beat Saber avatar mod, for example) + auto roleHint = getViveRoleHint(trackerRole); + if(roleHint != "") + GetDriver()->GetProperties()->SetStringProperty(props, vr::Prop_ControllerType_String, roleHint.c_str()); + + auto role = getViveRole(trackerRole); + if(role != "") + vr::VRSettings()->SetString(vr::k_pch_Trackers_Section, ("/devices/slimevr/" + this->serial_).c_str(), role.c_str()); return vr::EVRInitError::VRInitError_None; } @@ -192,3 +144,7 @@ vr::DriverPose_t SlimeVRDriver::TrackerDevice::GetPose() return last_pose_; } +int SlimeVRDriver::TrackerDevice::getDeviceId() +{ + return deviceId_; +} diff --git a/src/TrackerDevice.hpp b/src/TrackerDevice.hpp index b3f744d..7839f01 100644 --- a/src/TrackerDevice.hpp +++ b/src/TrackerDevice.hpp @@ -13,12 +13,14 @@ #include #include #include +#include "bridge/bridge.hpp" +#include "TrackerRole.hpp" namespace SlimeVRDriver { class TrackerDevice : public IVRDevice { public: - TrackerDevice(std::string serial, HANDLE pipe, int deviceId); + TrackerDevice(std::string serial, int deviceId, TrackerRole trackerRole); ~TrackerDevice() = default; // Inherited via IVRDevice @@ -33,17 +35,18 @@ namespace SlimeVRDriver { virtual void* GetComponent(const char* pchComponentNameAndVersion) override; virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) override; virtual vr::DriverPose_t GetPose() override; - + virtual int getDeviceId() override; + virtual void PositionMessage(messages::Position &position) override; private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; std::string serial_; - HANDLE hpipe; bool isSetup; char buffer[1024]; DWORD dwWritten; DWORD dwRead; - int deviceId; + int deviceId_; + TrackerRole trackerRole; vr::DriverPose_t last_pose_ = IVRDevice::MakeDefaultPose(); diff --git a/src/TrackerRole.cpp b/src/TrackerRole.cpp new file mode 100644 index 0000000..2cb4b4a --- /dev/null +++ b/src/TrackerRole.cpp @@ -0,0 +1,112 @@ +/* + SlimeVR Code is placed under the MIT license + Copyright (c) 2021 Eiren Rain + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +#include "TrackerRole.hpp" + + +std::string getViveRoleHint(TrackerRole role) { + switch(role) { + case LEFT_CONTROLLER: + case RIGHT_CONTROLLER: + case GENERIC_CONTROLLER: + case LEFT_HAND: + case RIGHT_HAND: + return "vive_tracker_handed"; + case LEFT_FOOT: + return "vive_tracker_left_foot"; + case RIGHT_FOOT: + return "vive_tracker_right_foot"; + case LEFT_SHOULDER: + return "vive_tracker_left_shoulder"; + case RIGHT_SHOULDER: + return "vive_tracker_right_shoulder"; + case LEFT_ELBOW: + return "vive_tracker_left_elbow"; + case RIGHT_ELBOW: + return "vive_tracker_right_elbow"; + case LEFT_KNEE: + return "vive_tracker_left_knee"; + case RIGHT_KNEE: + return "vive_tracker_right_knee"; + case WAIST: + return "vive_tracker_waist"; + case CHEST: + return "vive_tracker_chest"; + case CAMERA: + return "vive_tracker_camera"; + case KEYBOARD: + return "vive_tracker_keyboard"; + + } + return ""; +} + +std::string getViveRole(TrackerRole role) { + switch(role) { + case LEFT_CONTROLLER: + case RIGHT_CONTROLLER: + case GENERIC_CONTROLLER: + case LEFT_HAND: + case RIGHT_HAND: + return "TrackerRole_Handed"; + case LEFT_FOOT: + return "TrackerRole_LeftFoot"; + case RIGHT_FOOT: + return "TrackerRole_RightFoot"; + case LEFT_SHOULDER: + return "TrackerRole_LeftShoulder"; + case RIGHT_SHOULDER: + return "TrackerRole_RightShoulder"; + case LEFT_ELBOW: + return "TrackerRole_LeftElbow"; + case RIGHT_ELBOW: + return "TrackerRole_RightElbow"; + case LEFT_KNEE: + return "TrackerRole_LeftKnee"; + case RIGHT_KNEE: + return "TrackerRole_RightKnee"; + case WAIST: + return "TrackerRole_Waist"; + case CHEST: + return "TrackerRole_Chest"; + case CAMERA: + return "TrackerRole_Camera"; + case KEYBOARD: + return "TrackerRole_Keyboard"; + + } + return ""; +} + +DeviceType getDeviceType(TrackerRole role) { + switch(role) { + case LEFT_CONTROLLER: + case RIGHT_CONTROLLER: + case GENERIC_CONTROLLER: + return DeviceType::CONTROLLER; + case HMD: + return DeviceType::HMD; + case BEACON: + return DeviceType::TRACKING_REFERENCE; + } + return DeviceType::TRACKER; +} diff --git a/src/TrackerRole.hpp b/src/TrackerRole.hpp new file mode 100644 index 0000000..c0854cf --- /dev/null +++ b/src/TrackerRole.hpp @@ -0,0 +1,57 @@ +/* + SlimeVR Code is placed under the MIT license + Copyright (c) 2021 Eiren Rain + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +#pragma once + +#include +#include "DeviceType.hpp" + +enum TrackerRole { + NONE = 0, + WAIST = 1, + LEFT_FOOT = 2, + RIGHT_FOOT = 3, + CHEST = 4, + LEFT_KNEE = 5, + RIGHT_KNEE = 6, + LEFT_ELBOW = 7, + RIGHT_ELBOW = 8, + LEFT_SHOULDER = 9, + RIGHT_SHOULDER = 10, + LEFT_HAND = 11, + RIGHT_HAND = 12, + LEFT_CONTROLLER = 13, + RIGHT_CONTROLLER = 14, + HEAD = 15, + NECK = 16, + CAMERA = 17, + KEYBOARD = 18, + HMD = 19, + BEACON = 20, + GENERIC_CONTROLLER = 21, +}; + +std::string getViveRoleHint(TrackerRole role); + +std::string getViveRole(TrackerRole role); + +DeviceType getDeviceType(TrackerRole role); diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index d2a0b8a..42ec322 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -3,6 +3,10 @@ #include #include #include +#include "bridge/bridge.hpp" +#include "TrackerRole.hpp" +#include + vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverContext) { @@ -11,88 +15,8 @@ vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverCont return init_error; } - Log("[SlimeVR] Activating SlimeVR Driver..."); - - // Add a HMD - //this->AddDevice(std::make_shared("Example_HMDDevice")); - - // Add a couple controllers - //this->AddDevice(std::make_shared("Example_ControllerDevice_Left", ControllerDevice::Handedness::LEFT)); - //this->AddDevice(std::make_shared("Example_ControllerDevice_Right", ControllerDevice::Handedness::RIGHT)); - - std::string hmdPipeName = "\\\\.\\pipe\\HMDPipe"; - - //open the pipe - hmdPipe = CreateFileA(hmdPipeName.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - 0, - NULL); - - if (hmdPipe == INVALID_HANDLE_VALUE) - { - //if connection was unsuccessful, return an error. This means SteamVR will start without this driver running - return vr::EVRInitError::VRInitError_Driver_Failed; - } - //wait for a second to ensure data was sent and next pipe is set up if there is more than one tracker - - Sleep(1000); - - // Add a tracker - char buffer[1024]; - DWORD dwWritten; - DWORD dwRead; - - //on init, we try to connect to our pipes - for (int i = 0; i < pipeNum; i++) - { - //MessageBoxA(NULL, "It works! " + pipeNum, "Example Driver", MB_OK); - HANDLE pipe; - //pipe name, same as in our server program - std::string pipeName = "\\\\.\\pipe\\TrackPipe" + std::to_string(i); - - //open the pipe - pipe = CreateFileA(pipeName.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - 0, - NULL); - - if (pipe == INVALID_HANDLE_VALUE) - { - //if connection was unsuccessful, return an error. This means SteamVR will start without this driver running - return vr::EVRInitError::VRInitError_Driver_Failed; - } - - //wait for a second to ensure data was sent and next pipe is set up if there is more than one tracker - Sleep(1000); - - //read the number of pipes and smoothing factor from the pipe - if (ReadFile(pipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE) - { - //we receive raw data, so we first add terminating zero and save to a string. - buffer[dwRead] = '\0'; //add terminating zero - std::string s = buffer; - //from a string, we convert to a string stream for easier reading of each sent value - std::istringstream iss(s); - //read each value into our variables - - iss >> pipeNum; - iss >> smoothFactor; - } - //save our pipe to global - this->AddDevice(std::make_shared("SlimeVRTracker"+std::to_string(i),pipe, i)); - } - - // Add a couple tracking references - //this->AddDevice(std::make_shared("Example_TrackingReference_A")); - //this->AddDevice(std::make_shared("Example_TrackingReference_B")); - - Log("[SlimeVR] SlimeVR Driver Loaded Successfully"); + Log("Activating SlimeVR Driver..."); + Log("SlimeVR Driver Loaded Successfully"); return vr::VRInitError_None; } @@ -103,6 +27,8 @@ void SlimeVRDriver::VRDriver::Cleanup() void SlimeVRDriver::VRDriver::RunFrame() { + google::protobuf::Arena arena; + // Collect events vr::VREvent_t event; std::vector events; @@ -110,7 +36,7 @@ void SlimeVRDriver::VRDriver::RunFrame() { events.push_back(event); } - this->openvr_events_ = events; + this->openvr_events_ = std::move(events); // Update frame timing std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); @@ -118,31 +44,76 @@ void SlimeVRDriver::VRDriver::RunFrame() this->last_frame_time_ = now; // Update devices - for (auto& device : this->devices_) + for(auto& device : this->devices_) device->Update(); - - vr::TrackedDevicePose_t hmd_pose[10]; - vr::VRServerDriverHost()->GetRawTrackedDevicePoses(0, hmd_pose, 10); - - vr::HmdQuaternion_t q = GetRotation(hmd_pose[0].mDeviceToAbsoluteTracking); - vr::HmdVector3_t pos = GetPosition(hmd_pose[0].mDeviceToAbsoluteTracking); - - std::string s; - s = std::to_string(pos.v[0]) + - " " + std::to_string(pos.v[1]) + - " " + std::to_string(pos.v[2]) + - " " + std::to_string(q.w) + - " " + std::to_string(q.x) + - " " + std::to_string(q.y) + - " " + std::to_string(q.z) + "\n"; - - DWORD dwWritten; - WriteFile(hmdPipe, - s.c_str(), - (s.length() + 1), // = length of string + terminating '\0' !!! - &dwWritten, - NULL); + BridgeStatus status = runBridgeFrame(*this); + if(status == BRIDGE_CONNECTED) { + messages::ProtobufMessage* message = google::protobuf::Arena::CreateMessage(&arena); + // Read all messages from the bridge + while(getNextBridgeMessage(*message, *this)) { + if(message->has_tracker_added()) { + messages::TrackerAdded ta = message->tracker_added(); + switch(getDeviceType(static_cast(ta.tracker_role()))) { + case DeviceType::TRACKER: + this->AddDevice(std::make_shared(ta.tracker_serial(), ta.tracker_id(), static_cast(ta.tracker_role()))); + Log("New tracker device added " + ta.tracker_serial() + " (id " + std::to_string(ta.tracker_id()) + ")"); + break; + } + } else if(message->has_position()) { + messages::Position pos = message->position(); + auto device = this->devices_by_id.find(pos.tracker_id()); + if(device != this->devices_by_id.end()) { + device->second->PositionMessage(pos); + } + } + } + + if(!sentHmdAddMessage) { + // Send add message for HMD + messages::TrackerAdded* trackerAdded = google::protobuf::Arena::CreateMessage(&arena); + message->set_allocated_tracker_added(trackerAdded); + trackerAdded->set_tracker_id(0); + trackerAdded->set_tracker_role(TrackerRole::HMD); + trackerAdded->set_tracker_serial("HMD"); + trackerAdded->set_tracker_name("HMD"); + sendBridgeMessage(*message, *this); + + messages::TrackerStatus* trackerStatus = google::protobuf::Arena::CreateMessage(&arena); + message->set_allocated_tracker_status(trackerStatus); + trackerStatus->set_tracker_id(0); + trackerStatus->set_status(messages::TrackerStatus_Status::TrackerStatus_Status_OK); + sendBridgeMessage(*message, *this); + + sentHmdAddMessage = true; + Log("Sent HMD hello message"); + } + + vr::TrackedDevicePose_t hmd_pose[10]; + vr::VRServerDriverHost()->GetRawTrackedDevicePoses(0, hmd_pose, 10); + + vr::HmdQuaternion_t q = GetRotation(hmd_pose[0].mDeviceToAbsoluteTracking); + vr::HmdVector3_t pos = GetPosition(hmd_pose[0].mDeviceToAbsoluteTracking); + + messages::Position* hmdPosition = google::protobuf::Arena::CreateMessage(&arena); + message->set_allocated_position(hmdPosition); + + hmdPosition->set_tracker_id(0); + hmdPosition->set_data_source(messages::Position_DataSource_FULL); + hmdPosition->set_x(pos.v[0]); + hmdPosition->set_y(pos.v[1]); + hmdPosition->set_z(pos.v[2]); + hmdPosition->set_qx((float) q.x); + hmdPosition->set_qy((float) q.y); + hmdPosition->set_qz((float) q.z); + hmdPosition->set_qw((float) q.w); + + sendBridgeMessage(*message, *this); + } else { + // If bridge not connected, assume we need to resend hmd tracker add message + sentHmdAddMessage = false; + + } } bool SlimeVRDriver::VRDriver::ShouldBlockStandbyMode() @@ -194,8 +165,19 @@ bool SlimeVRDriver::VRDriver::AddDevice(std::shared_ptr device) return false; } bool result = vr::VRServerDriverHost()->TrackedDeviceAdded(device->GetSerial().c_str(), openvr_device_class, device.get()); - if(result) + if(result) { this->devices_.push_back(device); + this->devices_by_id[device->getDeviceId()] = device; + this->devices_by_serial[device->GetSerial()] = device; + } else { + std::shared_ptr oldDevice = this->devices_by_serial[device->GetSerial()]; + if(oldDevice->getDeviceId() != device->getDeviceId()) { + this->devices_by_id[device->getDeviceId()] = oldDevice; + Log("Device overriden from id " + std::to_string(oldDevice->getDeviceId()) + " to " + std::to_string(device->getDeviceId()) + " for serial " + device->GetSerial()); + } else { + Log("Device readded id " + std::to_string(device->getDeviceId()) + ", serial " + device->GetSerial()); + } + } return result; } @@ -253,7 +235,7 @@ vr::IVRServerDriverHost* SlimeVRDriver::VRDriver::GetDriverHost() // from: https://github.com/Omnifinity/OpenVR-Tracking-Example/blob/master/HTC%20Lighthouse%20Tracking%20Example/LighthouseTracking.cpp //----------------------------------------------------------------------------- -vr::HmdQuaternion_t SlimeVRDriver::VRDriver::GetRotation(vr::HmdMatrix34_t matrix) { +vr::HmdQuaternion_t SlimeVRDriver::VRDriver::GetRotation(vr::HmdMatrix34_t &matrix) { vr::HmdQuaternion_t q; q.w = sqrt(fmax(0, 1 + matrix.m[0][0] + matrix.m[1][1] + matrix.m[2][2])) / 2; @@ -270,7 +252,7 @@ vr::HmdQuaternion_t SlimeVRDriver::VRDriver::GetRotation(vr::HmdMatrix34_t matri // from: https://github.com/Omnifinity/OpenVR-Tracking-Example/blob/master/HTC%20Lighthouse%20Tracking%20Example/LighthouseTracking.cpp //----------------------------------------------------------------------------- -vr::HmdVector3_t SlimeVRDriver::VRDriver::GetPosition(vr::HmdMatrix34_t matrix) { +vr::HmdVector3_t SlimeVRDriver::VRDriver::GetPosition(vr::HmdMatrix34_t &matrix) { vr::HmdVector3_t vector; vector.v[0] = matrix.m[0][3]; diff --git a/src/VRDriver.hpp b/src/VRDriver.hpp index 0e1c730..b54c379 100644 --- a/src/VRDriver.hpp +++ b/src/VRDriver.hpp @@ -3,14 +3,12 @@ #include #include -#include #include #include #include - namespace SlimeVRDriver { class VRDriver : public IVRDriver { public: @@ -37,17 +35,17 @@ namespace SlimeVRDriver { virtual ~VRDriver() = default; private: - HANDLE hmdPipe; std::vector> devices_; std::vector openvr_events_; + std::map> devices_by_id; + std::map> devices_by_serial; std::chrono::milliseconds frame_timing_ = std::chrono::milliseconds(16); std::chrono::system_clock::time_point last_frame_time_ = std::chrono::system_clock::now(); - std::string settings_key_ = "driver_apriltag"; + std::string settings_key_ = "driver_slimevr"; - vr::HmdQuaternion_t GetRotation(vr::HmdMatrix34_t matrix); - vr::HmdVector3_t GetPosition(vr::HmdMatrix34_t matrix); + vr::HmdQuaternion_t GetRotation(vr::HmdMatrix34_t &matrix); + vr::HmdVector3_t GetPosition(vr::HmdMatrix34_t &matrix); - int pipeNum = 1; - double smoothFactor = 0.2; + bool sentHmdAddMessage = false; }; }; \ No newline at end of file diff --git a/src/bridge/ProtobufMessages.proto b/src/bridge/ProtobufMessages.proto new file mode 100644 index 0000000..de5d271 --- /dev/null +++ b/src/bridge/ProtobufMessages.proto @@ -0,0 +1,113 @@ +/* + SlimeVR Code is placed under the MIT license + Copyright (c) 2021 Eiren Rain + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +/* + Define all Proto Buffer messages that server and driver/app can exchange + */ +syntax = "proto3"; + +package messages; + +/** + Tracker designations clarifications + + tracker_id field contains internal tracker id of a tracker on the SENDING side. Recieving + side can have their own ids, names, serials, etc. If two sides exchange tracker information + in both ways, they're allowed to reuse tracker ids for the trackers they create themselves. + + tracker_name is user-readable descriptive name of a tracker, it is allowed to be non-unique. + + tracker_serial is unique identificator of a tracker on the sender side, can be used to remap + trackers from one id to another during reconnect or session restart, or to save persistent + data or configuration between sessions + */ + +/** + * Can be send each frame if there is nothing else to send, + * signaling to the other side that we're alive and ready to + * recieve messages this frame. + */ + +option java_package = "dev.slimevr.bridge"; +option java_outer_classname = "ProtobufMessages"; + +message PingPong { +} + +message Position { + int32 tracker_id = 1; + optional float x = 2; + optional float y = 3; + optional float z = 4; + float qx = 5; + float qy = 6; + float qz = 7; + float qw = 8; + enum DataSource { + NONE = 0; + IMU = 1; + PRECISION = 2; + FULL = 3; + } + optional DataSource data_source = 9; +} + +message UserAction { + string name = 1; + map action_arguments = 2; +} + +message TrackerAdded { + int32 tracker_id = 1; + string tracker_serial = 2; + string tracker_name = 3; + int32 tracker_role = 4; +} + +message TrackerStatus { + int32 tracker_id = 1; + enum Status { + DISCONNECTED = 0; + OK = 1; + BUSY = 2; + ERROR = 3; + OCCLUDED = 4; + } + Status status = 2; + map extra = 3; + enum Confidence { + NO = 0; + LOW = 1; + MEDIUM = 5; + HIGH = 10; + } + optional Confidence confidence = 4; +} + +message ProtobufMessage { + oneof message { + Position position = 1; + UserAction user_action = 2; + TrackerAdded tracker_added = 3; + TrackerStatus tracker_status = 4; + } +} \ No newline at end of file diff --git a/src/bridge/bridge-windows-pipes.cpp b/src/bridge/bridge-windows-pipes.cpp new file mode 100644 index 0000000..ef73846 --- /dev/null +++ b/src/bridge/bridge-windows-pipes.cpp @@ -0,0 +1,137 @@ +/* + SlimeVR Code is placed under the MIT license + Copyright (c) 2021 Eiren Rain + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +/** + * Windows specific IPC between SteamVR driver/app and SlimeVR server based + * on named pipes + */ +#include "bridge.hpp" +#if defined(WIN32) && defined(BRIDGE_USE_PIPES) +#include + +#define PIPE_NAME "\\\\.\\pipe\\SlimeVRDriver" + +unsigned long lastReconnectFrame = 0; + +HANDLE pipe = INVALID_HANDLE_VALUE; +BridgeStatus currentBridgeStatus = BRIDGE_DISCONNECTED; +char buffer[1024]; + +void updatePipe(SlimeVRDriver::VRDriver &driver); +void resetPipe(SlimeVRDriver::VRDriver &driver); +void attemptPipeConnect(SlimeVRDriver::VRDriver &driver); + +BridgeStatus runBridgeFrame(SlimeVRDriver::VRDriver &driver) { + switch(currentBridgeStatus) { + case BRIDGE_DISCONNECTED: + attemptPipeConnect(driver); + break; + case BRIDGE_ERROR: + resetPipe(driver); + break; + case BRIDGE_CONNECTED: + updatePipe(driver); + break; + } + + return currentBridgeStatus; +} + +bool getNextBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver) { + DWORD dwRead; + DWORD dwAvailable; + if(currentBridgeStatus == BRIDGE_CONNECTED) { + if(PeekNamedPipe(pipe, buffer, 4, &dwRead, &dwAvailable, NULL)) { + if(dwRead == 4) { + uint32_t messageLength = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0]; + if(messageLength > 1024) { + // TODO Buffer overflow + } + if(dwAvailable >= messageLength) { + if(ReadFile(pipe, buffer, messageLength, &dwRead, NULL)) { + if(message.ParseFromArray(buffer + 4, messageLength - 4)) + return true; + } else { + currentBridgeStatus = BRIDGE_ERROR; + driver.Log("Bridge error: " + std::to_string(GetLastError())); + } + } + } + } else { + currentBridgeStatus = BRIDGE_ERROR; + driver.Log("Bridge error: " + std::to_string(GetLastError())); + } + } + return false; +} + +bool sendBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver) { + if(currentBridgeStatus == BRIDGE_CONNECTED) { + uint32_t size = (uint32_t) message.ByteSizeLong(); + if(size > 1020) { + driver.Log("Message too big"); + return false; + } + message.SerializeToArray(buffer + 4, size); + size += 4; + buffer[0] = size & 0xFF; + buffer[1] = (size >> 8) & 0xFF; + buffer[2] = (size >> 16) & 0xFF; + buffer[3] = (size >> 24) & 0xFF; + if(WriteFile(pipe, buffer, size, NULL, NULL)) { + return true; + } + currentBridgeStatus = BRIDGE_ERROR; + driver.Log("Bridge error: " + std::to_string(GetLastError())); + } + return false; +} + +void updatePipe(SlimeVRDriver::VRDriver &driver) { +} + +void resetPipe(SlimeVRDriver::VRDriver &driver) { + if(pipe != INVALID_HANDLE_VALUE) { + CloseHandle(pipe); + pipe = INVALID_HANDLE_VALUE; + currentBridgeStatus = BRIDGE_DISCONNECTED; + driver.Log("Pipe was reset"); + } +} + +void attemptPipeConnect(SlimeVRDriver::VRDriver &driver) { + pipe = CreateFileA(PIPE_NAME, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + 0, // TODO : Overlapped + NULL); + if(pipe != INVALID_HANDLE_VALUE) { + currentBridgeStatus = BRIDGE_CONNECTED; + driver.Log("Pipe was connected"); + return; + } +} + + +#endif // PLATFORM_WINDOWS && BRIDGE_USE_PIPES \ No newline at end of file diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp new file mode 100644 index 0000000..fb1e1bf --- /dev/null +++ b/src/bridge/bridge.hpp @@ -0,0 +1,45 @@ +/* + SlimeVR Code is placed under the MIT license + Copyright (c) 2021 Eiren Rain + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +/** + * Header file for cross-platform handling of IPC between SteamVR driver/app + * and SlimeVR server + */ +#pragma once + +#define BRIDGE_USE_PIPES 1 +#include "ProtobufMessages.pb.h" +#include +#include +#include "../VRDriver.hpp" + +enum BridgeStatus { + BRIDGE_DISCONNECTED = 0, + BRIDGE_CONNECTED = 1, + BRIDGE_ERROR = 2 +}; + +BridgeStatus runBridgeFrame(SlimeVRDriver::VRDriver &driver); + +bool getNextBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver); + +bool sendBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver); \ No newline at end of file