Monado OpenXR Runtime
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1// Copyright 2023, Shawn Wallace
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief SteamVR driver context - implements xrt_tracking_origin and IVRDriverContext.
6 * @author Shawn Wallace <yungwallace@live.com>
7 * @ingroup drv_steamvr_lh
8 */
9
10#pragma once
11
12#include <condition_variable>
13#include <unordered_map>
14#include <memory>
15#include <optional>
16#include <chrono>
17#include <deque>
18#include <mutex>
19#include <thread>
20
21#include "openvr_driver.h"
22
23#include "settings.hpp"
24#include "resources.hpp"
25#include "iobuffer.hpp"
26#include "driver_manager.hpp"
27#include "server.hpp"
28#include "blockqueue.hpp"
29#include "paths.hpp"
30
31#include "xrt/xrt_tracking.h"
32
33struct xrt_input;
34class Device;
36class Context final : public xrt_tracking_origin,
37 public vr::IVRDriverContext,
38 public vr::IVRServerDriverHost,
39 public vr::IVRDriverInput,
40 public vr::IVRProperties,
41 public vr::IVRDriverLog,
42 public std::enable_shared_from_this<Context>
43
44{
45public:
46 Settings settings;
47
48private:
49 Resources resources;
50 IOBuffer iobuf;
51 DriverManager man;
52 Server server;
53 BlockQueue blockqueue;
54 Paths paths;
55
56 uint64_t current_frame{0};
57
58 std::vector<vr::VRInputComponentHandle_t> handles;
59 std::unordered_map<vr::VRInputComponentHandle_t, xrt_input *> handle_to_input;
60 struct Vec2Components
61 {
62 vr::VRInputComponentHandle_t x;
63 vr::VRInputComponentHandle_t y;
64 };
65 std::unordered_map<vr::VRInputComponentHandle_t, Vec2Components *> vec2_inputs;
66 std::unordered_map<xrt_input *, std::unique_ptr<Vec2Components>> vec2_input_to_components;
67 std::unordered_map<vr::VRInputComponentHandle_t, ControllerDevice *> skeleton_to_controller;
68
69 struct Event
70 {
71 std::chrono::steady_clock::time_point insert_time;
72 vr::VREvent_t inner;
73 };
74 std::deque<Event> events;
75 size_t events_tail{0};
76 std::mutex event_queue_mut;
77 std::condition_variable event_popped;
78
79 Device *
80 prop_container_to_device(vr::PropertyContainerHandle_t handle);
81
82 vr::EVRInputError
83 create_component_common(vr::PropertyContainerHandle_t container,
84 const char *name,
85 vr::VRInputComponentHandle_t *handle);
86
87 xrt_input *
88 update_component_common(vr::VRInputComponentHandle_t handle,
89 double offset,
90 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now());
91
92 bool
93 setup_hmd(const char *serial, vr::ITrackedDeviceServerDriver *driver);
94
95 bool
96 setup_controller(const char *serial, vr::ITrackedDeviceServerDriver *driver);
97 std::vector<vr::IServerTrackedDeviceProvider *> providers;
98
99 inline vr::VRInputComponentHandle_t
100 new_handle()
101 {
102 vr::VRInputComponentHandle_t h = handles.size() + 1;
103 handles.push_back(h);
104 return h;
105 }
106
107public:
108 Context(const std::string &steam_install, const std::string &steamvr_install, u_logging_level level);
109
110 // These are owned by monado, context is destroyed when these are destroyed
111 std::mutex devices_mut;
112 class HmdDevice *hmd{nullptr};
113 class ControllerDevice *controller[16]{nullptr};
114 bool in_setup{true};
115 const u_logging_level log_level;
116
117private:
118 std::atomic<bool> frame_thread_run;
119 std::binary_semaphore frame_thread_event{0};
120 std::thread frame_thread;
121
122 void
123 add_event_locked(vr::VREvent_t event);
124
125public:
126 ~Context();
127
128 [[nodiscard]] static std::shared_ptr<Context>
129 create(const std::string &steam_install,
130 const std::string &steamvr_install,
131 std::vector<vr::IServerTrackedDeviceProvider *> providers);
132
133 size_t
134 add_haptic_event(vr::VREvent_HapticVibration_t event, size_t old_event_index);
135
136 void
137 add_vendor_event(vr::EVREventType type, const vr::VREvent_Data_t &data = {})
138 {
139 VendorSpecificEvent(0, type, data, 0);
140 }
141
142 void
143 Log(const char *pchLogMessage) override;
144
145 /***** IVRDriverContext methods *****/
146
147 void *
148 GetGenericInterface(const char *pchInterfaceVersion, vr::EVRInitError *peError) override;
149
150 vr::DriverHandle_t
151 GetDriverHandle() override;
152
153 /***** IVRServerDriverHost methods *****/
154 bool
155 TrackedDeviceAdded(const char *pchDeviceSerialNumber,
156 vr::ETrackedDeviceClass eDeviceClass,
157 vr::ITrackedDeviceServerDriver *pDriver) override;
158
159 void
160 TrackedDevicePoseUpdated(uint32_t unWhichDevice,
161 const vr::DriverPose_t &newPose,
162 uint32_t unPoseStructSize) override;
163
164 void
165 VsyncEvent(double vsyncTimeOffsetSeconds) override;
166
167 void
168 VendorSpecificEvent(uint32_t unWhichDevice,
169 vr::EVREventType eventType,
170 const vr::VREvent_Data_t &eventData,
171 double eventTimeOffset) override;
172
173 bool
174 IsExiting() override;
175
176 bool
177 PollNextEvent(vr::VREvent_t *pEvent, uint32_t uncbVREvent) override;
178
179 void
180 GetRawTrackedDevicePoses(float fPredictedSecondsFromNow,
181 vr::TrackedDevicePose_t *pTrackedDevicePoseArray,
182 uint32_t unTrackedDevicePoseArrayCount) override;
183
184 void
185 RequestRestart(const char *pchLocalizedReason,
186 const char *pchExecutableToStart,
187 const char *pchArguments,
188 const char *pchWorkingDirectory) override;
189
190 uint32_t
191 GetFrameTimings(vr::Compositor_FrameTiming *pTiming, uint32_t nFrames) override;
192
193 void
194 SetDisplayEyeToHead(uint32_t unWhichDevice,
195 const vr::HmdMatrix34_t &eyeToHeadLeft,
196 const vr::HmdMatrix34_t &eyeToHeadRight) override;
197
198 void
199 SetDisplayProjectionRaw(uint32_t unWhichDevice,
200 const vr::HmdRect2_t &eyeLeft,
201 const vr::HmdRect2_t &eyeRight) override;
202
203 void
204 SetRecommendedRenderTargetSize(uint32_t unWhichDevice, uint32_t nWidth, uint32_t nHeight) override;
205
206 /***** IVRDriverInput methods *****/
207
208 vr::EVRInputError
209 CreateBooleanComponent(vr::PropertyContainerHandle_t ulContainer,
210 const char *pchName,
211 vr::VRInputComponentHandle_t *pHandle) override;
212
213 vr::EVRInputError
214 UpdateBooleanComponent(vr::VRInputComponentHandle_t ulComponent, bool bNewValue, double fTimeOffset) override;
215
216 vr::EVRInputError
217 CreateScalarComponent(vr::PropertyContainerHandle_t ulContainer,
218 const char *pchName,
219 vr::VRInputComponentHandle_t *pHandle,
220 vr::EVRScalarType eType,
221 vr::EVRScalarUnits eUnits) override;
222
223 vr::EVRInputError
224 UpdateScalarComponent(vr::VRInputComponentHandle_t ulComponent, float fNewValue, double fTimeOffset) override;
225
226 vr::EVRInputError
227 CreateHapticComponent(vr::PropertyContainerHandle_t ulContainer,
228 const char *pchName,
229 vr::VRInputComponentHandle_t *pHandle) override;
230
231 vr::EVRInputError
232 CreateSkeletonComponent(vr::PropertyContainerHandle_t ulContainer,
233 const char *pchName,
234 const char *pchSkeletonPath,
235 const char *pchBasePosePath,
236 vr::EVRSkeletalTrackingLevel eSkeletalTrackingLevel,
237 const vr::VRBoneTransform_t *pGripLimitTransforms,
238 uint32_t unGripLimitTransformCount,
239 vr::VRInputComponentHandle_t *pHandle) override;
240
241 vr::EVRInputError
242 UpdateSkeletonComponent(vr::VRInputComponentHandle_t ulComponent,
243 vr::EVRSkeletalMotionRange eMotionRange,
244 const vr::VRBoneTransform_t *pTransforms,
245 uint32_t unTransformCount) override;
246
247 vr::EVRInputError
248 CreatePoseComponent(vr::PropertyContainerHandle_t ulContainer,
249 const char *pchName,
250 vr::VRInputComponentHandle_t *pHandle) override;
251
252 vr::EVRInputError
253 UpdatePoseComponent(vr::VRInputComponentHandle_t ulComponent,
254 const vr::HmdMatrix34_t *pMatPoseOffset,
255 double fTimeOffset) override;
256
257 vr::EVRInputError
258 CreateEyeTrackingComponent(vr::PropertyContainerHandle_t ulContainer,
259 const char *pchName,
260 vr::VRInputComponentHandle_t *pHandle) override;
261
262 vr::EVRInputError
263 UpdateEyeTrackingComponent(vr::VRInputComponentHandle_t ulComponent,
264 const vr::VREyeTrackingData_t *pEyeTrackingData,
265 double fTimeOffset) override;
266
267 /***** IVRProperties methods *****/
268
269 vr::ETrackedPropertyError
270 ReadPropertyBatch(vr::PropertyContainerHandle_t ulContainerHandle,
271 vr::PropertyRead_t *pBatch,
272 uint32_t unBatchEntryCount) override;
273
274 vr::ETrackedPropertyError
275 WritePropertyBatch(vr::PropertyContainerHandle_t ulContainerHandle,
276 vr::PropertyWrite_t *pBatch,
277 uint32_t unBatchEntryCount) override;
278
279 const char *
280 GetPropErrorNameFromEnum(vr::ETrackedPropertyError error) override;
281
282 vr::PropertyContainerHandle_t
283 TrackedDeviceToPropertyContainer(vr::TrackedDeviceIndex_t nDevice) override;
284};
OpenVR IVRBlockQueue interface header.
This interface is missing in the C++ header but present in the C one, and the lighthouse driver requi...
Definition blockqueue.hpp:43
Definition context.hpp:44
static std::shared_ptr< Context > create(const std::string &steam_install, const std::string &steamvr_install, std::vector< vr::IServerTrackedDeviceProvider * > providers)
Since only the devices will live after our get_devices function is called, we make our Context a shar...
Definition steamvr_lh.cpp:160
Definition device.hpp:222
Definition device.hpp:68
Definition driver_manager.hpp:15
Definition device.hpp:143
Definition iobuffer.hpp:15
This interface is missing in the C++ header but present in the C one, and the lighthouse driver requi...
Definition paths.hpp:21
Definition resources.hpp:16
An internal interface utilized by the lighthouse driver.
Definition server.hpp:15
Definition settings.hpp:18
OpenVR IVRDriverManager interface header.
u_logging_level
Logging level enum.
Definition u_logging.h:45
OpenVR IVRIOBuffer interface header.
OpenVR IVRPaths interface header.
OpenVR IVRResources interface header.
OpenVR IVRServer internal interface header.
OpenVR IVRSettings interface header.
A single named input, that sits on a xrt_device.
Definition xrt_device.h:210
A tracking system or device origin.
Definition xrt_tracking.h:78
enum xrt_tracking_type type
What can the state tracker expect from this tracking system.
Definition xrt_tracking.h:83
Header defining the tracking system integration in Monado.