Skip to content

Commit 651a76a

Browse files
committed
Add version and input support via protobuf
1 parent 54baeb1 commit 651a76a

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ To update vcpkg packages set the vcpkg registry submodule to a newer commit and
5858

5959
### Updating protobuf messages
6060

61-
You can modify the protobuf messages for communication between the server and this driver at [src\bridge\ProtobufMessages.proto](src\bridge\ProtobufMessages.proto)
61+
You can modify the protobuf messages for communication between the server and this driver in [src\bridge\ProtobufMessages.proto](src\bridge\ProtobufMessages.proto). Please update PROTOCOL_VERSION in [src\bridge\BridgeClient.hpp](src\bridge\BridgeClient.hpp)
6262

6363
To update the protobuf messages on the server, you will need to install [protoc](https://protobuf.dev/installation/) (we use version 4.31.1), then make sure that your SlimeVR-Server and SlimeVR-OpenVR-Driver repositories are in the same parent directory and run `protobuf_update.bat` located at `SlimeVR-Server\server\desktop`

src/TrackerDevice.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "TrackerDevice.hpp"
22

3-
SlimeVRDriver::TrackerDevice::TrackerDevice(std::string serial, int device_id, TrackerRole tracker_role, bool fingertracking_enabled):
3+
SlimeVRDriver::TrackerDevice::TrackerDevice(std::string serial, int device_id, TrackerRole tracker_role):
44
serial_(serial),
55
tracker_role_(tracker_role),
66
device_id_(device_id),
77
is_left_hand_(tracker_role_ == TrackerRole::LEFT_CONTROLLER || tracker_role_ == TrackerRole::LEFT_HAND),
88
is_right_hand_(tracker_role_ == TrackerRole::RIGHT_CONTROLLER || tracker_role_ == TrackerRole::RIGHT_HAND),
9-
fingertracking_enabled_real_(fingertracking_enabled && (is_left_hand_ || is_right_hand_)),
9+
fingertracking_enabled_(is_left_hand_ || is_right_hand_),
1010
is_controller_(tracker_role_ == TrackerRole::LEFT_CONTROLLER || tracker_role_ == TrackerRole::RIGHT_CONTROLLER),
1111
last_pose_(MakeDefaultPose()),
1212
last_pose_atomic_(MakeDefaultPose())
@@ -75,12 +75,28 @@ void SlimeVRDriver::TrackerDevice::PositionMessage(messages::Position &position)
7575
}
7676

7777
if (is_controller_) {
78+
bool tap = false;
79+
bool double_tap = false;
80+
bool triple_tap = false;
81+
82+
// Get inputs from protobuf
83+
for (int i = 0; i < position.input_size(); ++i) {
84+
if (position.input(i).type() == messages::Input_InputType_TAP) {
85+
tap = true;
86+
} else if (position.input(i).type() == messages::Input_InputType_DOUBLE_TAP) {
87+
double_tap = true;
88+
} else if (position.input(i).type() == messages::Input_InputType_TRIPLE_TAP) {
89+
triple_tap = true;
90+
}
91+
}
92+
7893
// Set inputs
79-
// TODO
80-
//GetDriver()->GetInput()->UpdateBooleanComponent(this->tap_component_, false, 0);
94+
GetDriver()->GetInput()->UpdateBooleanComponent(this->tap_component_, tap, 0);
95+
GetDriver()->GetInput()->UpdateBooleanComponent(this->double_tap_component_, double_tap, 0);
96+
GetDriver()->GetInput()->UpdateBooleanComponent(this->triple_tap_component_, triple_tap, 0);
8197
}
8298

83-
if (fingertracking_enabled_real_) {
99+
if (fingertracking_enabled_) {
84100
// Set finger rotations
85101
vr::VRBoneTransform_t finger_skeleton_[31]{};
86102
for (int i = 0; i < position.finger_bone_rotations_size(); i++)
@@ -99,7 +115,7 @@ void SlimeVRDriver::TrackerDevice::PositionMessage(messages::Position &position)
99115
};
100116
}
101117

102-
// Update the finger skeleton for this hand. With and without controller are the same.
118+
// Update the finger skeleton for this hand. With and without controller have the same pose.
103119
vr::VRDriverInput()->UpdateSkeletonComponent(skeletal_component_handle_, vr::VRSkeletalMotionRange_WithController, finger_skeleton_, 31);
104120
vr::VRDriverInput()->UpdateSkeletonComponent(skeletal_component_handle_, vr::VRSkeletalMotionRange_WithoutController, finger_skeleton_, 31);
105121
}
@@ -203,6 +219,7 @@ vr::EVRInitError SlimeVRDriver::TrackerDevice::Activate(uint32_t unObjectId) {
203219
// Should be treated as controller or as tracker? (Hand = Tracker and Controller = Controller)
204220
if (is_controller_) {
205221
vr::VRProperties()->SetInt32Property(props, vr::Prop_DeviceClass_Int32, vr::TrackedDeviceClass_Controller);
222+
vr::VRProperties()->SetInt32Property(props, vr::Prop_ControllerHandSelectionPriority_Int32, 2147483647); // Prioritizes our controller over whatever else.
206223
} else {
207224
vr::VRProperties()->SetInt32Property(props, vr::Prop_DeviceClass_Int32, vr::TrackedDeviceClass_GenericTracker);
208225
}
@@ -247,7 +264,7 @@ vr::EVRInitError SlimeVRDriver::TrackerDevice::Activate(uint32_t unObjectId) {
247264
}
248265

249266
// Setup skeletal input for fingertracking
250-
if (fingertracking_enabled_real_) {
267+
if (fingertracking_enabled_) {
251268
vr::VRDriverInput()->CreateSkeletonComponent(
252269
props,
253270
is_right_hand_ ? "/input/skeleton/right" : "/input/skeleton/left",

src/TrackerDevice.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace SlimeVRDriver {
2020
class TrackerDevice : public IVRDevice {
2121
public:
22-
TrackerDevice(std::string serial, int device_id, TrackerRole tracker_role, bool fingertracking_enabled);
22+
TrackerDevice(std::string serial, int device_id, TrackerRole tracker_role);
2323
~TrackerDevice() = default;
2424

2525
// Inherited via IVRDevice
@@ -49,7 +49,7 @@ namespace SlimeVRDriver {
4949

5050
int device_id_;
5151
TrackerRole tracker_role_;
52-
bool fingertracking_enabled_real_;
52+
bool fingertracking_enabled_;
5353

5454
vr::DriverPose_t last_pose_ = IVRDevice::MakeDefaultPose();
5555
std::atomic<vr::DriverPose_t> last_pose_atomic_ = IVRDevice::MakeDefaultPose();

src/VRDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void SlimeVRDriver::VRDriver::OnBridgeMessage(const messages::ProtobufMessage& m
195195
std::lock_guard<std::mutex> lock(devices_mutex_);
196196
if (message.has_tracker_added()) {
197197
messages::TrackerAdded ta = message.tracker_added();
198-
AddDevice(std::make_shared<TrackerDevice>(ta.tracker_serial(), ta.tracker_id(), static_cast<TrackerRole>(ta.tracker_role()), ta.fingertracking_enabled()));
198+
AddDevice(std::make_shared<TrackerDevice>(ta.tracker_serial(), ta.tracker_id(), static_cast<TrackerRole>(ta.tracker_role())));
199199
} else if (message.has_position()) {
200200
messages::Position pos = message.position();
201201
auto device = devices_by_id_.find(pos.tracker_id());

src/bridge/BridgeClient.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void BridgeClient::CreateConnection() {
4040
logger_->Log("[%s] connected", path.c_str());
4141
connected_ = true;
4242
last_error_ = std::nullopt;
43+
SendVersion();
4344
});
4445
connection_handle_->on<uvw::end_event>([this, path](const uvw::end_event&, uvw::pipe_handle&) {
4546
logger_->Log("[%s] disconnected", path.c_str());
@@ -79,3 +80,12 @@ void BridgeClient::CloseConnectionHandles() {
7980
if (reconnect_timeout_) reconnect_timeout_->close();
8081
connected_ = false;
8182
}
83+
84+
void BridgeClient::SendVersion() {
85+
messages::ProtobufMessage* message = google::protobuf::Arena::CreateMessage<messages::ProtobufMessage>(&arena_);
86+
messages::Version* version = google::protobuf::Arena::CreateMessage<messages::Version>(&arena_);
87+
message->set_allocated_version(version);
88+
version->set_protocol_version(PROTOCOL_VERSION);
89+
SendBridgeMessage(*message);
90+
arena_.Reset();
91+
}

src/bridge/BridgeClient.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "BridgeTransport.hpp"
3030

31+
#define PROTOCOL_VERSION 1
32+
3133
/**
3234
* @brief Client implementation for communication with SlimeVR Server using pipes or unix sockets.
3335
*
@@ -49,8 +51,11 @@ class BridgeClient: public BridgeTransport {
4951
void ResetConnection() override;
5052
void CloseConnectionHandles() override;
5153
void Reconnect();
54+
void SendVersion();
5255

5356
std::optional<std::string> last_error_;
5457
std::optional<std::string> last_path_;
5558
std::shared_ptr<uvw::timer_handle> reconnect_timeout_;
59+
60+
google::protobuf::Arena arena_;
5661
};

src/bridge/ProtobufMessages.proto

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ option optimize_for = LITE_RUNTIME;
5454
message PingPong {
5555
}
5656

57+
message Version {
58+
int32 protocol_version = 1;
59+
}
60+
5761
message FingerBoneRotation {
5862
enum FingerBoneName {
5963
THUMB_METACARPAL = 0;
@@ -79,6 +83,15 @@ message FingerBoneRotation {
7983
float w = 5;
8084
}
8185

86+
message Input {
87+
enum InputType {
88+
TAP = 0;
89+
DOUBLE_TAP = 1;
90+
TRIPLE_TAP = 2;
91+
}
92+
InputType type = 1;
93+
}
94+
8295
message Position {
8396
int32 tracker_id = 1;
8497
optional float x = 2;
@@ -96,6 +109,8 @@ message Position {
96109
}
97110
optional DataSource data_source = 9;
98111
repeated FingerBoneRotation finger_bone_rotations = 10;
112+
repeated Input input = 11;
113+
99114
}
100115

101116
message UserAction {
@@ -108,7 +123,6 @@ message TrackerAdded {
108123
string tracker_serial = 2;
109124
string tracker_name = 3;
110125
int32 tracker_role = 4;
111-
optional bool fingertracking_enabled = 10;
112126
}
113127

114128
message TrackerStatus {
@@ -144,5 +158,6 @@ message ProtobufMessage {
144158
TrackerAdded tracker_added = 3;
145159
TrackerStatus tracker_status = 4;
146160
Battery battery = 5;
161+
Version version = 6;
147162
}
148163
}

0 commit comments

Comments
 (0)