From 11709126ec7d8c75e0ef3a7924bda7d0e0f8c8a8 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Sat, 18 Sep 2021 19:10:54 +0300 Subject: [PATCH 01/13] Initial setup for new IPC, added Protobuf --- CMakeLists.txt | 14 ++++++++++++- src/bridge/bridge-windows-pipes.cpp | 32 +++++++++++++++++++++++++++++ src/bridge/bridge.hpp | 28 +++++++++++++++++++++++++ src/bridge/protobuf.proto | 26 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/bridge/bridge-windows-pipes.cpp create mode 100644 src/bridge/bridge.hpp create mode 100644 src/bridge/protobuf.proto diff --git a/CMakeLists.txt b/CMakeLists.txt index 011f536..a8552ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.0.0) -project(SlimeVR-OpenVR-Driver VERSION 0.1.0) +project(SlimeVR-OpenVR-Driver VERSION 0.2.0) set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(DRIVER_NAME "SlimeVR") @@ -36,6 +36,18 @@ target_include_directories("${PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR} target_link_libraries("${PROJECT_NAME}" PUBLIC "${OPENVR_LIB}") set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 17) +# Protobuf +# Installation: +# Please refer to this readme to install protobuf in your system: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md +# If you're unsing vcpkg, don't forget to add its directory to PATH for FindProtobuf to find the protobuf +INCLUDE(FindProtobuf) +find_package(Protobuf REQUIRED) +include_directories(${Protobuf_INCLUDE_DIRS}) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) +protobuf_generate_cpp(PROTO_SRC PROTO_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/src/bridge/protobuf.proto") +SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE) +#add_library(proto ${PROTO_HEADER} ${PROTO_SRC}) + # IDE Config source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Header Files" FILES ${HEADERS}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Source Files" FILES ${SOURCES}) diff --git a/src/bridge/bridge-windows-pipes.cpp b/src/bridge/bridge-windows-pipes.cpp new file mode 100644 index 0000000..967d143 --- /dev/null +++ b/src/bridge/bridge-windows-pipes.cpp @@ -0,0 +1,32 @@ +/* + 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(PLATFORM_WINDOWS) && defined(BRIDGE_USE_PIPES) + + + +#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..3703338 --- /dev/null +++ b/src/bridge/bridge.hpp @@ -0,0 +1,28 @@ +/* + 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 + */ +#define BRIDGE_USE_PIPES 1 +#include "protobuf.pb.h" diff --git a/src/bridge/protobuf.proto b/src/bridge/protobuf.proto new file mode 100644 index 0000000..ed16f1b --- /dev/null +++ b/src/bridge/protobuf.proto @@ -0,0 +1,26 @@ +/* + 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"; From f3b61d19850cd8e72b7b26df50324de14183ae57 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Sat, 18 Sep 2021 23:28:25 +0300 Subject: [PATCH 02/13] Fix protobuf cmake --- CMakeLists.txt | 10 ++++---- src/bridge/bridge.hpp | 2 +- src/bridge/{protobuf.proto => messages.proto} | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) rename src/bridge/{protobuf.proto => messages.proto} (75%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8552ac..75a3e86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,14 +39,16 @@ set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 17) # Protobuf # Installation: # Please refer to this readme to install protobuf in your system: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md -# If you're unsing vcpkg, don't forget to add its directory to PATH for FindProtobuf to find the protobuf +# Set env var CMAKE_INCLUDE_PATH to point to vcpkg\installed\x64-windows\include +# Set env var CMAKE_LIBRARY_PATH to point to vcpkg\installed\x64-windows\lib +# WARNING: CLang has an arror building protobuf messages, use MSVC 2019 INCLUDE(FindProtobuf) find_package(Protobuf REQUIRED) +protobuf_generate_cpp(PROTO_SRC PROTO_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/src/bridge/messages.proto") +SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE) +add_library(proto ${PROTO_HEADER} ${PROTO_SRC}) include_directories(${Protobuf_INCLUDE_DIRS}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) -protobuf_generate_cpp(PROTO_SRC PROTO_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/src/bridge/protobuf.proto") -SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE) -#add_library(proto ${PROTO_HEADER} ${PROTO_SRC}) # IDE Config source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Header Files" FILES ${HEADERS}) diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp index 3703338..4d5353d 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -25,4 +25,4 @@ * and SlimeVR server */ #define BRIDGE_USE_PIPES 1 -#include "protobuf.pb.h" +#include "messages.pb.h" diff --git a/src/bridge/protobuf.proto b/src/bridge/messages.proto similarity index 75% rename from src/bridge/protobuf.proto rename to src/bridge/messages.proto index ed16f1b..fc5dde9 100644 --- a/src/bridge/protobuf.proto +++ b/src/bridge/messages.proto @@ -24,3 +24,27 @@ * Define all Proto Buffer messages that server and driver/app can exchange */ syntax = "proto3"; + +package dev.slimevr.bridge; + +message Position { + int32 tracker_id = 1; + float x = 2; + float y = 3; + float z = 4; + float qx = 5; + float qy = 6; + float qz = 7; + float qw = 8; + enum DataSource { + IMU = 0; + CAM = 1; + FULL = 2; + } + optional DataSource data_source = 9; +} + +message Action { + string name = 1; + map action_arguments = 2; +} \ No newline at end of file From 080426739cfdeba2f53bc2630c1bf7b2c97b6ddc Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Sat, 18 Sep 2021 23:44:22 +0300 Subject: [PATCH 03/13] Add more protobuf messages --- CMakeLists.txt | 2 +- ...{messages.proto => ProtobufMessages.proto} | 34 ++++++++++++++++--- src/bridge/bridge.hpp | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) rename src/bridge/{messages.proto => ProtobufMessages.proto} (74%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75a3e86..e9eccf6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 17) # WARNING: CLang has an arror building protobuf messages, use MSVC 2019 INCLUDE(FindProtobuf) find_package(Protobuf REQUIRED) -protobuf_generate_cpp(PROTO_SRC PROTO_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/src/bridge/messages.proto") +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) add_library(proto ${PROTO_HEADER} ${PROTO_SRC}) include_directories(${Protobuf_INCLUDE_DIRS}) diff --git a/src/bridge/messages.proto b/src/bridge/ProtobufMessages.proto similarity index 74% rename from src/bridge/messages.proto rename to src/bridge/ProtobufMessages.proto index fc5dde9..2b91ca6 100644 --- a/src/bridge/messages.proto +++ b/src/bridge/ProtobufMessages.proto @@ -37,14 +37,40 @@ message Position { float qz = 7; float qw = 8; enum DataSource { - IMU = 0; - CAM = 1; - FULL = 2; + NONE = 0; + IMU = 1; + PRECISION = 2; + FULL = 3; } optional DataSource data_source = 9; } -message Action { +message UserAction { string name = 1; map action_arguments = 2; +} + +message TrackerAdded { + int32 tracker_id = 1; + string tracker_name = 2; + string location = 3; +} + +message TrackerStatus { + int32 tracker_id = 1; + enum Status { + DISCONNECTED = 0; + OK = 1; + BUSY = 2; + ERROR = 3; + OCCLUDED = 4; + } + Status status = 2; + enum Confidence { + NO = 0; + LOW = 1; + MEDIUM = 5; + HIGH = 10; + } + optional Confidence confidence = 3; } \ No newline at end of file diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp index 4d5353d..c4f4cdd 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -25,4 +25,4 @@ * and SlimeVR server */ #define BRIDGE_USE_PIPES 1 -#include "messages.pb.h" +#include "ProtobufMessages.pb.h" From 4aa44dc5b93d53f8a9658e6ed3bb2665ac803f76 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Sun, 19 Sep 2021 03:52:54 +0300 Subject: [PATCH 04/13] I hate protobuf --- CMakeLists.txt | 31 +++++----- src/TrackerDevice.cpp | 36 ++++++----- src/TrackerDevice.hpp | 3 +- src/VRDriver.cpp | 92 ++++------------------------- src/bridge/ProtobufMessages.proto | 9 +++ src/bridge/bridge-windows-pipes.cpp | 89 +++++++++++++++++++++++++++- src/bridge/bridge.hpp | 18 ++++++ 7 files changed, 166 insertions(+), 112 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e9eccf6..10b0434 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,8 @@ cmake_minimum_required(VERSION 3.0.0) + +# Edit this to target your vcpkg toolchain +set(CMAKE_TOOLCHAIN_FILE "C:/_work/vcpkg/scripts/buildsystems/vcpkg.cmake") + project(SlimeVR-OpenVR-Driver VERSION 0.2.0) set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(DRIVER_NAME "SlimeVR") @@ -26,29 +30,26 @@ endif() find_library(OPENVR_LIB openvr_api HINTS "${CMAKE_CURRENT_SOURCE_DIR}/libraries/openvr/lib/${PLATFORM_NAME}${PROCESSOR_ARCH}/" NO_DEFAULT_PATH ) -# 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}") -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}") -set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 17) - # Protobuf # Installation: # Please refer to this readme to install protobuf in your system: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md -# Set env var CMAKE_INCLUDE_PATH to point to vcpkg\installed\x64-windows\include -# Set env var CMAKE_LIBRARY_PATH to point to vcpkg\installed\x64-windows\lib # WARNING: CLang has an arror building protobuf messages, use MSVC 2019 INCLUDE(FindProtobuf) -find_package(Protobuf REQUIRED) +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) -add_library(proto ${PROTO_HEADER} ${PROTO_SRC}) + +# 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}" ${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}" 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}) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) # IDE Config source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Header Files" FILES ${HEADERS}) diff --git a/src/TrackerDevice.cpp b/src/TrackerDevice.cpp index 6cb8e30..298533a 100644 --- a/src/TrackerDevice.cpp +++ b/src/TrackerDevice.cpp @@ -41,8 +41,6 @@ void SlimeVRDriver::TrackerDevice::Update() } } - // Setup pose for this frame - auto pose = this->last_pose_; if (PeekNamedPipe(hpipe, NULL, 0, NULL, &dwRead, NULL) != FALSE) { @@ -80,19 +78,7 @@ void SlimeVRDriver::TrackerDevice::Update() 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; + } } } @@ -100,6 +86,26 @@ void SlimeVRDriver::TrackerDevice::Update() } +void SlimeVRDriver::TrackerDevice::positionMessage(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 + 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() { return DeviceType::TRACKER; diff --git a/src/TrackerDevice.hpp b/src/TrackerDevice.hpp index b3f744d..4ac719f 100644 --- a/src/TrackerDevice.hpp +++ b/src/TrackerDevice.hpp @@ -13,6 +13,7 @@ #include #include #include +#include "bridge/bridge.hpp" namespace SlimeVRDriver { class TrackerDevice : public IVRDevice { @@ -33,7 +34,7 @@ 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 void SlimeVRDriver::TrackerDevice::positionMessage(Position &position); private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; std::string serial_; diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index d2a0b8a..14168bf 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "bridge/bridge.hpp" vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverContext) { @@ -12,86 +13,6 @@ vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverCont } 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"); return vr::VRInitError_None; @@ -120,6 +41,17 @@ void SlimeVRDriver::VRDriver::RunFrame() // Update devices for (auto& device : this->devices_) device->Update(); + + runBridgeFrame(); + ProtobufMessage message = {}; + while(getNextBridgeMessage(message)) { + if(message.has_tracker_added()) { + TrackerAdded ta = message.tracker_added(); + this->AddDevice(std::make_shared("SlimeVRTracker"+ ta.tracker_id(), ta.tracker_id())); + } else if(message.has_position()) { + + } + } vr::TrackedDevicePose_t hmd_pose[10]; vr::VRServerDriverHost()->GetRawTrackedDevicePoses(0, hmd_pose, 10); diff --git a/src/bridge/ProtobufMessages.proto b/src/bridge/ProtobufMessages.proto index 2b91ca6..bb59d2e 100644 --- a/src/bridge/ProtobufMessages.proto +++ b/src/bridge/ProtobufMessages.proto @@ -73,4 +73,13 @@ message TrackerStatus { HIGH = 10; } optional Confidence confidence = 3; +} + +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 index 967d143..da45c03 100644 --- a/src/bridge/bridge-windows-pipes.cpp +++ b/src/bridge/bridge-windows-pipes.cpp @@ -25,8 +25,95 @@ * on named pipes */ #include "bridge.hpp" -#if defined(PLATFORM_WINDOWS) && defined(BRIDGE_USE_PIPES) +#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(); +void resetPipe(); +void attemptPipeConnect(); + +BridgeStatus runBridgeFrame() { + switch(currentBridgeStatus) { + case BRIDGE_DISCONNECTED: + attemptPipeConnect(); + break; + case BRIDGE_ERROR: + resetPipe(); + break; + case BRIDGE_CONNECTED: + updatePipe(); + break; + } + + return currentBridgeStatus; +} + +bool getNextBridgeMessage(ProtobufMessage &message) { + DWORD dwRead; + DWORD dwAvailable; + if(currentBridgeStatus == BRIDGE_CONNECTED) { + if(PeekNamedPipe(pipe, buffer, 4, &dwRead, &dwAvailable, NULL)) { + if(dwRead == 4) { + uint32_t messageLength = *reinterpret_cast(buffer); + 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; + } + } + } + } else { + currentBridgeStatus = BRIDGE_ERROR; + } + } + return false; +} + +bool sendBridgeMEssage(ProtobufMessage &message) { + if(currentBridgeStatus == BRIDGE_CONNECTED) { + + } + return false; +} + +void updatePipe() { +} + +void resetPipe() { + if(pipe != INVALID_HANDLE_VALUE) { + CloseHandle(pipe); + pipe = INVALID_HANDLE_VALUE; + currentBridgeStatus = BRIDGE_DISCONNECTED; + } +} + +void attemptPipeConnect() { + pipe = CreateFileA(PIPE_NAME, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + 0, // TODO : Overlapped + NULL); + if(pipe != INVALID_HANDLE_VALUE) { + currentBridgeStatus = BRIDGE_CONNECTED; + // Log 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 index c4f4cdd..5a208c0 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -24,5 +24,23 @@ * 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 + +using namespace dev::slimevr::bridge; + +enum BridgeStatus { + BRIDGE_DISCONNECTED = 0, + BRIDGE_CONNECTED = 1, + BRIDGE_ERROR = 2 +}; + +BridgeStatus runBridgeFrame(); + +bool getNextBridgeMessage(ProtobufMessage &message); + +bool sendBridgeMEssage(ProtobufMessage &message); \ No newline at end of file From 7e8392b687bb6b4512988bc6545754e47b7a6692 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Sun, 19 Sep 2021 21:57:32 +0300 Subject: [PATCH 05/13] New bridge WIP, implemented HMD position sending --- src/TrackerDevice.cpp | 48 +------------------ src/TrackerDevice.hpp | 5 +- src/VRDriver.cpp | 73 +++++++++++++++++------------ src/VRDriver.hpp | 4 +- src/bridge/bridge-windows-pipes.cpp | 16 +++++-- src/bridge/bridge.hpp | 2 +- 6 files changed, 62 insertions(+), 86 deletions(-) diff --git a/src/TrackerDevice.cpp b/src/TrackerDevice.cpp index 298533a..81263cf 100644 --- a/src/TrackerDevice.cpp +++ b/src/TrackerDevice.cpp @@ -1,8 +1,8 @@ #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): + serial_(serial) { this->deviceId = deviceId; this->last_pose_ = MakeDefaultPose(); @@ -40,50 +40,6 @@ void SlimeVRDriver::TrackerDevice::Update() this->vibrate_anim_state_ = 0.0f; } } - - - 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; - - - } - } - } - - } void SlimeVRDriver::TrackerDevice::positionMessage(Position &position) diff --git a/src/TrackerDevice.hpp b/src/TrackerDevice.hpp index 4ac719f..475f35c 100644 --- a/src/TrackerDevice.hpp +++ b/src/TrackerDevice.hpp @@ -19,7 +19,7 @@ namespace SlimeVRDriver { class TrackerDevice : public IVRDevice { public: - TrackerDevice(std::string serial, HANDLE pipe, int deviceId); + TrackerDevice(std::string serial, int deviceId); ~TrackerDevice() = default; // Inherited via IVRDevice @@ -34,11 +34,10 @@ 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 void SlimeVRDriver::TrackerDevice::positionMessage(Position &position); + virtual void positionMessage(Position &position); private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; std::string serial_; - HANDLE hpipe; bool isSetup; char buffer[1024]; diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index 14168bf..abe44b0 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -42,39 +42,52 @@ void SlimeVRDriver::VRDriver::RunFrame() for (auto& device : this->devices_) device->Update(); - runBridgeFrame(); - ProtobufMessage message = {}; - while(getNextBridgeMessage(message)) { - if(message.has_tracker_added()) { - TrackerAdded ta = message.tracker_added(); - this->AddDevice(std::make_shared("SlimeVRTracker"+ ta.tracker_id(), ta.tracker_id())); - } else if(message.has_position()) { + BridgeStatus status = runBridgeFrame(); + if(status == BRIDGE_CONNECTED) { + ProtobufMessage message = {}; + // Read all messages from the bridge + while(getNextBridgeMessage(message)) { + if(message.has_tracker_added()) { + TrackerAdded ta = message.tracker_added(); + this->AddDevice(std::make_shared("SlimeVRTracker"+ ta.tracker_id(), ta.tracker_id())); + } else if(message.has_position()) { + + } + } + if(!sentHmdAddMessage) { + // Send add message for HMD + message.Clear(); + TrackerAdded trackerAdded = {}; + message.set_allocated_tracker_added(&trackerAdded); + trackerAdded.set_location("HMD"); + trackerAdded.set_tracker_id(0); + trackerAdded.set_tracker_name("HMD"); + sendBridgeMessage(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); - - 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); - + 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); + + message.Clear(); + Position hmdPosition = {}; + message.set_allocated_position(&hmdPosition); + + hmdPosition.set_tracker_id(0); + hmdPosition.set_data_source(Position_DataSource_FULL); + hmdPosition.set_x(pos.v[0]); + hmdPosition.set_y(pos.v[1]); + hmdPosition.set_z(pos.v[2]); + hmdPosition.set_qx(q.x); + hmdPosition.set_qy(q.y); + hmdPosition.set_qz(q.z); + hmdPosition.set_qw(q.w); + + sendBridgeMessage(message); + } } bool SlimeVRDriver::VRDriver::ShouldBlockStandbyMode() diff --git a/src/VRDriver.hpp b/src/VRDriver.hpp index 0e1c730..ac4979e 100644 --- a/src/VRDriver.hpp +++ b/src/VRDriver.hpp @@ -37,7 +37,6 @@ namespace SlimeVRDriver { virtual ~VRDriver() = default; private: - HANDLE hmdPipe; std::vector> devices_; std::vector openvr_events_; std::chrono::milliseconds frame_timing_ = std::chrono::milliseconds(16); @@ -47,7 +46,6 @@ namespace SlimeVRDriver { 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/bridge-windows-pipes.cpp b/src/bridge/bridge-windows-pipes.cpp index da45c03..4082c2f 100644 --- a/src/bridge/bridge-windows-pipes.cpp +++ b/src/bridge/bridge-windows-pipes.cpp @@ -62,7 +62,7 @@ bool getNextBridgeMessage(ProtobufMessage &message) { if(currentBridgeStatus == BRIDGE_CONNECTED) { if(PeekNamedPipe(pipe, buffer, 4, &dwRead, &dwAvailable, NULL)) { if(dwRead == 4) { - uint32_t messageLength = *reinterpret_cast(buffer); + uint32_t messageLength = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); if(messageLength > 1024) { // TODO Buffer overflow } @@ -82,9 +82,19 @@ bool getNextBridgeMessage(ProtobufMessage &message) { return false; } -bool sendBridgeMEssage(ProtobufMessage &message) { +bool sendBridgeMessage(ProtobufMessage &message) { if(currentBridgeStatus == BRIDGE_CONNECTED) { - + int size = message.ByteSize(); + 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; } return false; } diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp index 5a208c0..d769e72 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -43,4 +43,4 @@ BridgeStatus runBridgeFrame(); bool getNextBridgeMessage(ProtobufMessage &message); -bool sendBridgeMEssage(ProtobufMessage &message); \ No newline at end of file +bool sendBridgeMessage(ProtobufMessage &message); \ No newline at end of file From 73d06d76ec7516e9b8127f65859545401b72fa3c Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Thu, 23 Sep 2021 01:13:37 +0300 Subject: [PATCH 06/13] New bridge implemented and properly compiles (untested) --- src/ControllerDevice.cpp | 29 ++++++- src/ControllerDevice.hpp | 5 +- src/DeviceType.hpp | 1 + src/HMDDevice.cpp | 27 ++++++- src/HMDDevice.hpp | 5 +- src/IVRDevice.hpp | 5 ++ src/TrackerDevice.cpp | 44 +++++------ src/TrackerDevice.hpp | 9 ++- src/TrackerRole.cpp | 112 ++++++++++++++++++++++++++++ src/TrackerRole.hpp | 57 ++++++++++++++ src/VRDriver.cpp | 38 +++++++--- src/VRDriver.hpp | 7 +- src/bridge/ProtobufMessages.proto | 5 +- src/bridge/bridge-windows-pipes.cpp | 8 +- src/bridge/bridge.hpp | 6 +- 15 files changed, 297 insertions(+), 61 deletions(-) create mode 100644 src/TrackerRole.cpp create mode 100644 src/TrackerRole.hpp diff --git a/src/ControllerDevice.cpp b/src/ControllerDevice.cpp index 474bbb8..e822df7 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,25 @@ 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 + 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 +228,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..ccecaf6 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,25 @@ 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 + 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 +240,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..c949441 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,10 @@ 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) { + } + ~IVRDevice() = default; }; }; \ No newline at end of file diff --git a/src/TrackerDevice.cpp b/src/TrackerDevice.cpp index 81263cf..8e897d5 100644 --- a/src/TrackerDevice.cpp +++ b/src/TrackerDevice.cpp @@ -1,10 +1,10 @@ #include "TrackerDevice.hpp" #include -SlimeVRDriver::TrackerDevice::TrackerDevice(std::string serial, int deviceId): - serial_(serial) + +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; } @@ -42,9 +42,8 @@ void SlimeVRDriver::TrackerDevice::Update() } } -void SlimeVRDriver::TrackerDevice::positionMessage(Position &position) +void SlimeVRDriver::TrackerDevice::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 @@ -81,12 +80,6 @@ vr::EVRInitError SlimeVRDriver::TrackerDevice::Activate(uint32_t unObjectId) // 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); @@ -95,13 +88,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"); @@ -113,18 +105,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; } @@ -154,3 +142,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 475f35c..7839f01 100644 --- a/src/TrackerDevice.hpp +++ b/src/TrackerDevice.hpp @@ -14,12 +14,13 @@ #include #include #include "bridge/bridge.hpp" +#include "TrackerRole.hpp" namespace SlimeVRDriver { class TrackerDevice : public IVRDevice { public: - TrackerDevice(std::string serial, int deviceId); + TrackerDevice(std::string serial, int deviceId, TrackerRole trackerRole); ~TrackerDevice() = default; // Inherited via IVRDevice @@ -34,7 +35,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 void positionMessage(Position &position); + virtual int getDeviceId() override; + virtual void PositionMessage(messages::Position &position) override; private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; std::string serial_; @@ -43,7 +45,8 @@ namespace SlimeVRDriver { 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 abe44b0..98cf170 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -4,6 +4,7 @@ #include #include #include "bridge/bridge.hpp" +#include "TrackerRole.hpp" vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverContext) { @@ -31,7 +32,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(); @@ -39,26 +40,34 @@ void SlimeVRDriver::VRDriver::RunFrame() this->last_frame_time_ = now; // Update devices - for (auto& device : this->devices_) + for(auto& device : this->devices_) device->Update(); BridgeStatus status = runBridgeFrame(); if(status == BRIDGE_CONNECTED) { - ProtobufMessage message = {}; + messages::ProtobufMessage message = {}; // Read all messages from the bridge while(getNextBridgeMessage(message)) { if(message.has_tracker_added()) { - TrackerAdded ta = message.tracker_added(); - this->AddDevice(std::make_shared("SlimeVRTracker"+ ta.tracker_id(), ta.tracker_id())); + messages::TrackerAdded ta = message.tracker_added(); + switch(getDeviceType(static_cast(ta.tracker_role()))) { + case DeviceType::TRACKER: + this->AddDevice(std::make_shared("SlimeVRTracker"+ ta.tracker_id(), ta.tracker_id(), static_cast(ta.tracker_role()))); + 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 message.Clear(); - TrackerAdded trackerAdded = {}; + messages::TrackerAdded trackerAdded = {}; message.set_allocated_tracker_added(&trackerAdded); trackerAdded.set_location("HMD"); trackerAdded.set_tracker_id(0); @@ -73,11 +82,11 @@ void SlimeVRDriver::VRDriver::RunFrame() vr::HmdVector3_t pos = GetPosition(hmd_pose[0].mDeviceToAbsoluteTracking); message.Clear(); - Position hmdPosition = {}; + messages::Position hmdPosition = {}; message.set_allocated_position(&hmdPosition); hmdPosition.set_tracker_id(0); - hmdPosition.set_data_source(Position_DataSource_FULL); + 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]); @@ -87,6 +96,9 @@ void SlimeVRDriver::VRDriver::RunFrame() hmdPosition.set_qw(q.w); sendBridgeMessage(message); + } else { + // If bridge not connected, assume we need to resend hmd tracker add message + sentHmdAddMessage = false; } } @@ -139,8 +151,10 @@ 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; + } return result; } @@ -198,7 +212,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; @@ -215,7 +229,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 ac4979e..a47325a 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: @@ -39,12 +37,13 @@ namespace SlimeVRDriver { private: std::vector> devices_; std::vector openvr_events_; + std::map> devices_by_id; 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"; - 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); bool sentHmdAddMessage = false; }; diff --git a/src/bridge/ProtobufMessages.proto b/src/bridge/ProtobufMessages.proto index bb59d2e..24f9d31 100644 --- a/src/bridge/ProtobufMessages.proto +++ b/src/bridge/ProtobufMessages.proto @@ -25,7 +25,7 @@ */ syntax = "proto3"; -package dev.slimevr.bridge; +package messages; message Position { int32 tracker_id = 1; @@ -53,7 +53,8 @@ message UserAction { message TrackerAdded { int32 tracker_id = 1; string tracker_name = 2; - string location = 3; + int32 tracker_role = 3; + string location = 4; } message TrackerStatus { diff --git a/src/bridge/bridge-windows-pipes.cpp b/src/bridge/bridge-windows-pipes.cpp index 4082c2f..959e09d 100644 --- a/src/bridge/bridge-windows-pipes.cpp +++ b/src/bridge/bridge-windows-pipes.cpp @@ -56,13 +56,13 @@ BridgeStatus runBridgeFrame() { return currentBridgeStatus; } -bool getNextBridgeMessage(ProtobufMessage &message) { +bool getNextBridgeMessage(messages::ProtobufMessage &message) { DWORD dwRead; DWORD dwAvailable; if(currentBridgeStatus == BRIDGE_CONNECTED) { if(PeekNamedPipe(pipe, buffer, 4, &dwRead, &dwAvailable, NULL)) { if(dwRead == 4) { - uint32_t messageLength = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); + uint32_t messageLength = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0]; if(messageLength > 1024) { // TODO Buffer overflow } @@ -82,9 +82,9 @@ bool getNextBridgeMessage(ProtobufMessage &message) { return false; } -bool sendBridgeMessage(ProtobufMessage &message) { +bool sendBridgeMessage(messages::ProtobufMessage &message) { if(currentBridgeStatus == BRIDGE_CONNECTED) { - int size = message.ByteSize(); + uint32_t size = message.ByteSize(); message.SerializeToArray(buffer + 4, size); size += 4; buffer[0] = size & 0xFF; diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp index d769e72..13926cb 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -31,8 +31,6 @@ #include #include -using namespace dev::slimevr::bridge; - enum BridgeStatus { BRIDGE_DISCONNECTED = 0, BRIDGE_CONNECTED = 1, @@ -41,6 +39,6 @@ enum BridgeStatus { BridgeStatus runBridgeFrame(); -bool getNextBridgeMessage(ProtobufMessage &message); +bool getNextBridgeMessage(messages::ProtobufMessage &message); -bool sendBridgeMessage(ProtobufMessage &message); \ No newline at end of file +bool sendBridgeMessage(messages::ProtobufMessage &message); \ No newline at end of file From 51719c49c521783a1b7c8a0cf59a57daf1331231 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Thu, 23 Sep 2021 03:16:16 +0300 Subject: [PATCH 07/13] Update protocol after discussions --- src/VRDriver.cpp | 2 +- src/bridge/ProtobufMessages.proto | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index 98cf170..4951f4c 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -69,8 +69,8 @@ void SlimeVRDriver::VRDriver::RunFrame() message.Clear(); messages::TrackerAdded trackerAdded = {}; message.set_allocated_tracker_added(&trackerAdded); - trackerAdded.set_location("HMD"); trackerAdded.set_tracker_id(0); + trackerAdded.set_tracker_serial("HMD"); trackerAdded.set_tracker_name("HMD"); sendBridgeMessage(message); } diff --git a/src/bridge/ProtobufMessages.proto b/src/bridge/ProtobufMessages.proto index 24f9d31..4458be1 100644 --- a/src/bridge/ProtobufMessages.proto +++ b/src/bridge/ProtobufMessages.proto @@ -20,13 +20,27 @@ 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 +/* + 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 + */ + message Position { int32 tracker_id = 1; float x = 2; @@ -52,9 +66,9 @@ message UserAction { message TrackerAdded { int32 tracker_id = 1; - string tracker_name = 2; - int32 tracker_role = 3; - string location = 4; + string tracker_serial = 2; + string tracker_name = 3; + int32 tracker_role = 4; } message TrackerStatus { @@ -67,13 +81,14 @@ message TrackerStatus { OCCLUDED = 4; } Status status = 2; + map extra = 3; enum Confidence { NO = 0; LOW = 1; MEDIUM = 5; HIGH = 10; } - optional Confidence confidence = 3; + optional Confidence confidence = 4; } message ProtobufMessage { From 479d2b3847dab499f10b32bc040decb55f5c3170 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Thu, 30 Sep 2021 14:24:37 +0300 Subject: [PATCH 08/13] Make position data optional in message, added ping-pong empty message --- src/ControllerDevice.cpp | 8 +++++--- src/HMDDevice.cpp | 8 +++++--- src/TrackerDevice.cpp | 8 +++++--- src/bridge/ProtobufMessages.proto | 14 +++++++++++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/ControllerDevice.cpp b/src/ControllerDevice.cpp index e822df7..39331b8 100644 --- a/src/ControllerDevice.cpp +++ b/src/ControllerDevice.cpp @@ -190,9 +190,11 @@ void SlimeVRDriver::ControllerDevice::PositionMessage(messages::Position &positi // Setup pose for this frame auto pose = this->last_pose_; //send the new position and rotation from the pipe to the tracker object - pose.vecPosition[0] = position.x(); - pose.vecPosition[1] = position.y(); - pose.vecPosition[2] = position.z(); + 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(); diff --git a/src/HMDDevice.cpp b/src/HMDDevice.cpp index ccecaf6..c75d278 100644 --- a/src/HMDDevice.cpp +++ b/src/HMDDevice.cpp @@ -64,9 +64,11 @@ 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 - pose.vecPosition[0] = position.x(); - pose.vecPosition[1] = position.y(); - pose.vecPosition[2] = position.z(); + 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(); diff --git a/src/TrackerDevice.cpp b/src/TrackerDevice.cpp index 8e897d5..f3e895c 100644 --- a/src/TrackerDevice.cpp +++ b/src/TrackerDevice.cpp @@ -47,9 +47,11 @@ void SlimeVRDriver::TrackerDevice::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 - pose.vecPosition[0] = position.x(); - pose.vecPosition[1] = position.y(); - pose.vecPosition[2] = position.z(); + 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(); diff --git a/src/bridge/ProtobufMessages.proto b/src/bridge/ProtobufMessages.proto index 4458be1..6daac98 100644 --- a/src/bridge/ProtobufMessages.proto +++ b/src/bridge/ProtobufMessages.proto @@ -41,11 +41,19 @@ package messages; 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. + */ +message PingPong { +} + message Position { int32 tracker_id = 1; - float x = 2; - float y = 3; - float z = 4; + optional float x = 2; + optional float y = 3; + optional float z = 4; float qx = 5; float qy = 6; float qz = 7; From e3d8ef2006d0cca369e6ee35a5c50f8da995140d Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Fri, 1 Oct 2021 12:26:46 +0300 Subject: [PATCH 09/13] Fix all IPC problems and test it lightly --- src/TrackerDevice.cpp | 2 +- src/VRDriver.cpp | 87 ++++++++++++++++++----------- src/VRDriver.hpp | 3 +- src/bridge/ProtobufMessages.proto | 4 ++ src/bridge/bridge-windows-pipes.cpp | 36 +++++++----- src/bridge/bridge.hpp | 7 ++- 6 files changed, 88 insertions(+), 51 deletions(-) diff --git a/src/TrackerDevice.cpp b/src/TrackerDevice.cpp index f3e895c..9ad99da 100644 --- a/src/TrackerDevice.cpp +++ b/src/TrackerDevice.cpp @@ -77,7 +77,7 @@ 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_); diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index 4951f4c..7f0edcf 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -5,6 +5,8 @@ #include #include "bridge/bridge.hpp" #include "TrackerRole.hpp" +#include + vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverContext) { @@ -13,8 +15,8 @@ vr::EVRInitError SlimeVRDriver::VRDriver::Init(vr::IVRDriverContext* pDriverCont return init_error; } - Log("[SlimeVR] Activating SlimeVR Driver..."); - Log("[SlimeVR] SlimeVR Driver Loaded Successfully"); + Log("Activating SlimeVR Driver..."); + Log("SlimeVR Driver Loaded Successfully"); return vr::VRInitError_None; } @@ -25,6 +27,8 @@ void SlimeVRDriver::VRDriver::Cleanup() void SlimeVRDriver::VRDriver::RunFrame() { + google::protobuf::Arena arena; + // Collect events vr::VREvent_t event; std::vector events; @@ -43,20 +47,21 @@ void SlimeVRDriver::VRDriver::RunFrame() for(auto& device : this->devices_) device->Update(); - BridgeStatus status = runBridgeFrame(); + BridgeStatus status = runBridgeFrame(*this); if(status == BRIDGE_CONNECTED) { - messages::ProtobufMessage message = {}; + messages::ProtobufMessage* message = google::protobuf::Arena::CreateMessage(&arena); // Read all messages from the bridge - while(getNextBridgeMessage(message)) { - if(message.has_tracker_added()) { - messages::TrackerAdded ta = message.tracker_added(); + 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("SlimeVRTracker"+ ta.tracker_id(), ta.tracker_id(), static_cast(ta.tracker_role()))); + 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(); + } 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); @@ -66,13 +71,22 @@ void SlimeVRDriver::VRDriver::RunFrame() if(!sentHmdAddMessage) { // Send add message for HMD - message.Clear(); - messages::TrackerAdded trackerAdded = {}; - message.set_allocated_tracker_added(&trackerAdded); - trackerAdded.set_tracker_id(0); - trackerAdded.set_tracker_serial("HMD"); - trackerAdded.set_tracker_name("HMD"); - sendBridgeMessage(message); + 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]; @@ -81,24 +95,24 @@ void SlimeVRDriver::VRDriver::RunFrame() vr::HmdQuaternion_t q = GetRotation(hmd_pose[0].mDeviceToAbsoluteTracking); vr::HmdVector3_t pos = GetPosition(hmd_pose[0].mDeviceToAbsoluteTracking); - message.Clear(); - messages::Position hmdPosition = {}; - 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(q.x); - hmdPosition.set_qy(q.y); - hmdPosition.set_qz(q.z); - hmdPosition.set_qw(q.w); - - sendBridgeMessage(message); + 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; + } } @@ -154,6 +168,15 @@ bool SlimeVRDriver::VRDriver::AddDevice(std::shared_ptr device) 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; } diff --git a/src/VRDriver.hpp b/src/VRDriver.hpp index a47325a..b54c379 100644 --- a/src/VRDriver.hpp +++ b/src/VRDriver.hpp @@ -38,9 +38,10 @@ namespace SlimeVRDriver { 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); diff --git a/src/bridge/ProtobufMessages.proto b/src/bridge/ProtobufMessages.proto index 6daac98..de5d271 100644 --- a/src/bridge/ProtobufMessages.proto +++ b/src/bridge/ProtobufMessages.proto @@ -46,6 +46,10 @@ package messages; * 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 { } diff --git a/src/bridge/bridge-windows-pipes.cpp b/src/bridge/bridge-windows-pipes.cpp index 959e09d..ef73846 100644 --- a/src/bridge/bridge-windows-pipes.cpp +++ b/src/bridge/bridge-windows-pipes.cpp @@ -36,27 +36,27 @@ HANDLE pipe = INVALID_HANDLE_VALUE; BridgeStatus currentBridgeStatus = BRIDGE_DISCONNECTED; char buffer[1024]; -void updatePipe(); -void resetPipe(); -void attemptPipeConnect(); +void updatePipe(SlimeVRDriver::VRDriver &driver); +void resetPipe(SlimeVRDriver::VRDriver &driver); +void attemptPipeConnect(SlimeVRDriver::VRDriver &driver); -BridgeStatus runBridgeFrame() { +BridgeStatus runBridgeFrame(SlimeVRDriver::VRDriver &driver) { switch(currentBridgeStatus) { case BRIDGE_DISCONNECTED: - attemptPipeConnect(); + attemptPipeConnect(driver); break; case BRIDGE_ERROR: - resetPipe(); + resetPipe(driver); break; case BRIDGE_CONNECTED: - updatePipe(); + updatePipe(driver); break; } return currentBridgeStatus; } -bool getNextBridgeMessage(messages::ProtobufMessage &message) { +bool getNextBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver) { DWORD dwRead; DWORD dwAvailable; if(currentBridgeStatus == BRIDGE_CONNECTED) { @@ -72,19 +72,25 @@ bool getNextBridgeMessage(messages::ProtobufMessage &message) { 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) { +bool sendBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver) { if(currentBridgeStatus == BRIDGE_CONNECTED) { - uint32_t size = message.ByteSize(); + 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; @@ -95,22 +101,24 @@ bool sendBridgeMessage(messages::ProtobufMessage &message) { return true; } currentBridgeStatus = BRIDGE_ERROR; + driver.Log("Bridge error: " + std::to_string(GetLastError())); } return false; } -void updatePipe() { +void updatePipe(SlimeVRDriver::VRDriver &driver) { } -void resetPipe() { +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() { +void attemptPipeConnect(SlimeVRDriver::VRDriver &driver) { pipe = CreateFileA(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, @@ -120,7 +128,7 @@ void attemptPipeConnect() { NULL); if(pipe != INVALID_HANDLE_VALUE) { currentBridgeStatus = BRIDGE_CONNECTED; - // Log connected + driver.Log("Pipe was connected"); return; } } diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp index 13926cb..fb1e1bf 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -30,6 +30,7 @@ #include "ProtobufMessages.pb.h" #include #include +#include "../VRDriver.hpp" enum BridgeStatus { BRIDGE_DISCONNECTED = 0, @@ -37,8 +38,8 @@ enum BridgeStatus { BRIDGE_ERROR = 2 }; -BridgeStatus runBridgeFrame(); +BridgeStatus runBridgeFrame(SlimeVRDriver::VRDriver &driver); -bool getNextBridgeMessage(messages::ProtobufMessage &message); +bool getNextBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver); -bool sendBridgeMessage(messages::ProtobufMessage &message); \ No newline at end of file +bool sendBridgeMessage(messages::ProtobufMessage &message, SlimeVRDriver::VRDriver &driver); \ No newline at end of file From e426c825c3496dbc18feedcc207cecf658a1caa1 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Fri, 1 Oct 2021 17:58:06 +0300 Subject: [PATCH 10/13] Minor cleanup --- src/IVRDevice.hpp | 3 +-- src/VRDriver.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/IVRDevice.hpp b/src/IVRDevice.hpp index c949441..6e58de8 100644 --- a/src/IVRDevice.hpp +++ b/src/IVRDevice.hpp @@ -60,8 +60,7 @@ namespace SlimeVRDriver { virtual vr::DriverPose_t GetPose() = 0; virtual int getDeviceId() = 0; - virtual void PositionMessage(messages::Position& position) { - } + virtual void PositionMessage(messages::Position& position) = 0; ~IVRDevice() = default; }; diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index 7f0edcf..42ec322 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -84,7 +84,7 @@ void SlimeVRDriver::VRDriver::RunFrame() trackerStatus->set_tracker_id(0); trackerStatus->set_status(messages::TrackerStatus_Status::TrackerStatus_Status_OK); sendBridgeMessage(*message, *this); - + sentHmdAddMessage = true; Log("Sent HMD hello message"); } From 7b5c040c3b8e54822855ddf920712b2aa716c918 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Fri, 1 Oct 2021 18:18:19 +0300 Subject: [PATCH 11/13] Read vcpkg path from vcpkg.path.txt --- CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 10b0434..6214935 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,12 @@ cmake_minimum_required(VERSION 3.0.0) # Edit this to target your vcpkg toolchain -set(CMAKE_TOOLCHAIN_FILE "C:/_work/vcpkg/scripts/buildsystems/vcpkg.cmake") +if(WIN32) + file(READ "$ENV{LOCALAPPDATA}\\vcpkg\\vcpkg.path.txt" VCPKG_PATH) +elseif(UNIX) + file(READ "$ENV{HOME}\\.vcpkg\\vcpkg.path.txt" VCPKG_PATH) +endif() +set(CMAKE_TOOLCHAIN_FILE "${VCPKG_PATH}/scripts/buildsystems/vcpkg.cmake") project(SlimeVR-OpenVR-Driver VERSION 0.2.0) set_property(GLOBAL PROPERTY USE_FOLDERS ON) From f8d4b9d43feb7ef20f575b4dd9c3e194b8f67e39 Mon Sep 17 00:00:00 2001 From: ButterscotchVanilla Date: Fri, 1 Oct 2021 13:54:06 -0400 Subject: [PATCH 12/13] Add GitHub Actions workflow and check VCPKG_ROOT environment variable --- .github/workflows/c-cpp.yml | 81 +++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 36 ++++++++++++++--- 2 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/c-cpp.yml diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml new file mode 100644 index 0000000..2077cba --- /dev/null +++ b/.github/workflows/c-cpp.yml @@ -0,0 +1,81 @@ +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/* + !${{env.CMAKE_BUILD_DIR}}/Release/*.* diff --git a/CMakeLists.txt b/CMakeLists.txt index 6214935..7716374 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,27 @@ cmake_minimum_required(VERSION 3.0.0) -# Edit this to target your vcpkg toolchain -if(WIN32) - file(READ "$ENV{LOCALAPPDATA}\\vcpkg\\vcpkg.path.txt" VCPKG_PATH) -elseif(UNIX) - file(READ "$ENV{HOME}\\.vcpkg\\vcpkg.path.txt" VCPKG_PATH) +# 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() -set(CMAKE_TOOLCHAIN_FILE "${VCPKG_PATH}/scripts/buildsystems/vcpkg.cmake") project(SlimeVR-OpenVR-Driver VERSION 0.2.0) set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -82,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 +) From b76185b955540ec5b52f6c519183ca58e045c649 Mon Sep 17 00:00:00 2001 From: Eiren Rain Date: Sat, 2 Oct 2021 09:05:33 +0300 Subject: [PATCH 13/13] Update c-cpp.yml Fix artifact folder structure in github workflow --- .github/workflows/c-cpp.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 2077cba..0b1a9e3 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -77,5 +77,4 @@ jobs: # 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/* - !${{env.CMAKE_BUILD_DIR}}/Release/*.* + ${{env.CMAKE_BUILD_DIR}}/Release/driver/*