forked from terminal29/Simple-OpenVR-Driver-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VRDriver.cpp
161 lines (136 loc) · 5.08 KB
/
VRDriver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "VRDriver.hpp"
#include <Driver/HMDDevice.hpp>
#include <Driver/TrackerDevice.hpp>
#include <Driver/ControllerDevice.hpp>
#include <Driver/TrackingReferenceDevice.hpp>
vr::EVRInitError ExampleDriver::VRDriver::Init(vr::IVRDriverContext* pDriverContext)
{
// Perform driver context initialisation
if (vr::EVRInitError init_error = vr::InitServerDriverContext(pDriverContext); init_error != vr::EVRInitError::VRInitError_None) {
return init_error;
}
Log("Activating ExampleDriver...");
// Add a HMD
this->AddDevice(std::make_shared<HMDDevice>("Example_HMDDevice"));
// Add a couple controllers
this->AddDevice(std::make_shared<ControllerDevice>("Example_ControllerDevice_Left", ControllerDevice::Handedness::LEFT));
this->AddDevice(std::make_shared<ControllerDevice>("Example_ControllerDevice_Right", ControllerDevice::Handedness::RIGHT));
// Add a tracker
this->AddDevice(std::make_shared<TrackerDevice>("Example_TrackerDevice"));
// Add a couple tracking references
this->AddDevice(std::make_shared<TrackingReferenceDevice>("Example_TrackingReference_A"));
this->AddDevice(std::make_shared<TrackingReferenceDevice>("Example_TrackingReference_B"));
Log("ExampleDriver Loaded Successfully");
return vr::VRInitError_None;
}
void ExampleDriver::VRDriver::Cleanup()
{
}
void ExampleDriver::VRDriver::RunFrame()
{
// Collect events
vr::VREvent_t event;
std::vector<vr::VREvent_t> events;
while (vr::VRServerDriverHost()->PollNextEvent(&event, sizeof(event)))
{
events.push_back(event);
}
this->openvr_events_ = events;
// Update frame timing
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
this->frame_timing_ = std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_frame_time_);
this->last_frame_time_ = now;
// Update devices
for (auto& device : this->devices_)
device->Update();
}
bool ExampleDriver::VRDriver::ShouldBlockStandbyMode()
{
return false;
}
void ExampleDriver::VRDriver::EnterStandby()
{
}
void ExampleDriver::VRDriver::LeaveStandby()
{
}
std::vector<std::shared_ptr<ExampleDriver::IVRDevice>> ExampleDriver::VRDriver::GetDevices()
{
return this->devices_;
}
std::vector<vr::VREvent_t> ExampleDriver::VRDriver::GetOpenVREvents()
{
return this->openvr_events_;
}
std::chrono::milliseconds ExampleDriver::VRDriver::GetLastFrameTime()
{
return this->frame_timing_;
}
bool ExampleDriver::VRDriver::AddDevice(std::shared_ptr<IVRDevice> device)
{
vr::ETrackedDeviceClass openvr_device_class;
// Remember to update this switch when new device types are added
switch (device->GetDeviceType()) {
case DeviceType::CONTROLLER:
openvr_device_class = vr::ETrackedDeviceClass::TrackedDeviceClass_Controller;
break;
case DeviceType::HMD:
openvr_device_class = vr::ETrackedDeviceClass::TrackedDeviceClass_HMD;
break;
case DeviceType::TRACKER:
openvr_device_class = vr::ETrackedDeviceClass::TrackedDeviceClass_GenericTracker;
break;
case DeviceType::TRACKING_REFERENCE:
openvr_device_class = vr::ETrackedDeviceClass::TrackedDeviceClass_TrackingReference;
break;
default:
return false;
}
bool result = vr::VRServerDriverHost()->TrackedDeviceAdded(device->GetSerial().c_str(), openvr_device_class, device.get());
if(result)
this->devices_.push_back(device);
return result;
}
ExampleDriver::SettingsValue ExampleDriver::VRDriver::GetSettingsValue(std::string key)
{
vr::EVRSettingsError err = vr::EVRSettingsError::VRSettingsError_None;
int int_value = vr::VRSettings()->GetInt32(settings_key_.c_str(), key.c_str(), &err);
if (err == vr::EVRSettingsError::VRSettingsError_None) {
return int_value;
}
err = vr::EVRSettingsError::VRSettingsError_None;
float float_value = vr::VRSettings()->GetFloat(settings_key_.c_str(), key.c_str(), &err);
if (err == vr::EVRSettingsError::VRSettingsError_None) {
return float_value;
}
err = vr::EVRSettingsError::VRSettingsError_None;
bool bool_value = vr::VRSettings()->GetBool(settings_key_.c_str(), key.c_str(), &err);
if (err == vr::EVRSettingsError::VRSettingsError_None) {
return bool_value;
}
std::string str_value;
str_value.reserve(1024);
vr::VRSettings()->GetString(settings_key_.c_str(), key.c_str(), str_value.data(), 1024, &err);
if (err == vr::EVRSettingsError::VRSettingsError_None) {
return str_value;
}
err = vr::EVRSettingsError::VRSettingsError_None;
return SettingsValue();
}
void ExampleDriver::VRDriver::Log(std::string message)
{
std::string message_endl = message + "\n";
vr::VRDriverLog()->Log(message_endl.c_str());
}
vr::IVRDriverInput* ExampleDriver::VRDriver::GetInput()
{
return vr::VRDriverInput();
}
vr::CVRPropertyHelpers* ExampleDriver::VRDriver::GetProperties()
{
return vr::VRProperties();
}
vr::IVRServerDriverHost* ExampleDriver::VRDriver::GetDriverHost()
{
return vr::VRServerDriverHost();
}