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
117 void
118 wait_for_discover();
119
120 void
121 extend_discover();
122
123private:
124 std::condition_variable discover_cv;
125 std::chrono::steady_clock::time_point discover_end_time;
126 std::atomic<bool> frame_thread_run;
127 std::binary_semaphore frame_thread_event{0};
128 std::thread frame_thread;
129
130 void
131 add_event_locked(vr::VREvent_t event);
132
133public:
134 ~Context();
135
136 [[nodiscard]] static std::shared_ptr<Context>
137 create(const std::string &steam_install,
138 const std::string &steamvr_install,
139 std::vector<vr::IServerTrackedDeviceProvider *> providers);
140
141 size_t
142 add_haptic_event(vr::VREvent_HapticVibration_t event, size_t old_event_index);
143
144 void
145 add_vendor_event(vr::EVREventType type, const vr::VREvent_Data_t &data = {})
146 {
147 VendorSpecificEvent(0, type, data, 0);
148 }
149
150 void
151 Log(const char *pchLogMessage) override;
152
153 /***** IVRDriverContext methods *****/
154
155 void *
156 GetGenericInterface(const char *pchInterfaceVersion, vr::EVRInitError *peError) override;
157
158 vr::DriverHandle_t
159 GetDriverHandle() override;
160
161 /***** IVRServerDriverHost methods *****/
162 bool
163 TrackedDeviceAdded(const char *pchDeviceSerialNumber,
164 vr::ETrackedDeviceClass eDeviceClass,
165 vr::ITrackedDeviceServerDriver *pDriver) override;
166
167 void
168 TrackedDevicePoseUpdated(uint32_t unWhichDevice,
169 const vr::DriverPose_t &newPose,
170 uint32_t unPoseStructSize) override;
171
172 void
173 VsyncEvent(double vsyncTimeOffsetSeconds) override;
174
175 void
176 VendorSpecificEvent(uint32_t unWhichDevice,
177 vr::EVREventType eventType,
178 const vr::VREvent_Data_t &eventData,
179 double eventTimeOffset) override;
180
181 bool
182 IsExiting() override;
183
184 bool
185 PollNextEvent(vr::VREvent_t *pEvent, uint32_t uncbVREvent) override;
186
187 void
188 GetRawTrackedDevicePoses(float fPredictedSecondsFromNow,
189 vr::TrackedDevicePose_t *pTrackedDevicePoseArray,
190 uint32_t unTrackedDevicePoseArrayCount) override;
191
192 void
193 RequestRestart(const char *pchLocalizedReason,
194 const char *pchExecutableToStart,
195 const char *pchArguments,
196 const char *pchWorkingDirectory) override;
197
198 uint32_t
199 GetFrameTimings(vr::Compositor_FrameTiming *pTiming, uint32_t nFrames) override;
200
201 void
202 SetDisplayEyeToHead(uint32_t unWhichDevice,
203 const vr::HmdMatrix34_t &eyeToHeadLeft,
204 const vr::HmdMatrix34_t &eyeToHeadRight) override;
205
206 void
207 SetDisplayProjectionRaw(uint32_t unWhichDevice,
208 const vr::HmdRect2_t &eyeLeft,
209 const vr::HmdRect2_t &eyeRight) override;
210
211 void
212 SetRecommendedRenderTargetSize(uint32_t unWhichDevice, uint32_t nWidth, uint32_t nHeight) override;
213
214 /***** IVRDriverInput methods *****/
215
216 vr::EVRInputError
217 CreateBooleanComponent(vr::PropertyContainerHandle_t ulContainer,
218 const char *pchName,
219 vr::VRInputComponentHandle_t *pHandle) override;
220
221 vr::EVRInputError
222 UpdateBooleanComponent(vr::VRInputComponentHandle_t ulComponent, bool bNewValue, double fTimeOffset) override;
223
224 vr::EVRInputError
225 CreateScalarComponent(vr::PropertyContainerHandle_t ulContainer,
226 const char *pchName,
227 vr::VRInputComponentHandle_t *pHandle,
228 vr::EVRScalarType eType,
229 vr::EVRScalarUnits eUnits) override;
230
231 vr::EVRInputError
232 UpdateScalarComponent(vr::VRInputComponentHandle_t ulComponent, float fNewValue, double fTimeOffset) override;
233
234 vr::EVRInputError
235 CreateHapticComponent(vr::PropertyContainerHandle_t ulContainer,
236 const char *pchName,
237 vr::VRInputComponentHandle_t *pHandle) override;
238
239 vr::EVRInputError
240 CreateSkeletonComponent(vr::PropertyContainerHandle_t ulContainer,
241 const char *pchName,
242 const char *pchSkeletonPath,
243 const char *pchBasePosePath,
244 vr::EVRSkeletalTrackingLevel eSkeletalTrackingLevel,
245 const vr::VRBoneTransform_t *pGripLimitTransforms,
246 uint32_t unGripLimitTransformCount,
247 vr::VRInputComponentHandle_t *pHandle) override;
248
249 vr::EVRInputError
250 UpdateSkeletonComponent(vr::VRInputComponentHandle_t ulComponent,
251 vr::EVRSkeletalMotionRange eMotionRange,
252 const vr::VRBoneTransform_t *pTransforms,
253 uint32_t unTransformCount) override;
254
255 vr::EVRInputError
256 CreatePoseComponent(vr::PropertyContainerHandle_t ulContainer,
257 const char *pchName,
258 vr::VRInputComponentHandle_t *pHandle) override;
259
260 vr::EVRInputError
261 UpdatePoseComponent(vr::VRInputComponentHandle_t ulComponent,
262 const vr::HmdMatrix34_t *pMatPoseOffset,
263 double fTimeOffset) override;
264
265 vr::EVRInputError
266 CreateEyeTrackingComponent(vr::PropertyContainerHandle_t ulContainer,
267 const char *pchName,
268 vr::VRInputComponentHandle_t *pHandle) override;
269
270 vr::EVRInputError
271 UpdateEyeTrackingComponent(vr::VRInputComponentHandle_t ulComponent,
272 const vr::VREyeTrackingData_t *pEyeTrackingData,
273 double fTimeOffset) override;
274
275 /***** IVRProperties methods *****/
276
277 vr::ETrackedPropertyError
278 ReadPropertyBatch(vr::PropertyContainerHandle_t ulContainerHandle,
279 vr::PropertyRead_t *pBatch,
280 uint32_t unBatchEntryCount) override;
281
282 vr::ETrackedPropertyError
283 WritePropertyBatch(vr::PropertyContainerHandle_t ulContainerHandle,
284 vr::PropertyWrite_t *pBatch,
285 uint32_t unBatchEntryCount) override;
286
287 const char *
288 GetPropErrorNameFromEnum(vr::ETrackedPropertyError error) override;
289
290 vr::PropertyContainerHandle_t
291 TrackedDeviceToPropertyContainer(vr::TrackedDeviceIndex_t nDevice) override;
292};
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:162
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.