Monado OpenXR Runtime
device.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 device header - inherits xrt_device.
6 * @author Shawn Wallace <yungwallace@live.com>
7 * @ingroup drv_steamvr_lh
8 */
9
11
13
14#include "xrt/xrt_device.h"
15
16#include "vive/vive_common.h"
17
18#include "vp2/vp2_hid.h"
19
20#include "openvr_driver.h"
21
22#include <string>
23#include <vector>
24#include <memory>
25#include <unordered_map>
26
27#include <condition_variable>
28#include <mutex>
29
30class Context;
31struct InputClass;
32
34{
35 using ContextPtr = std::shared_ptr<Context>;
36 ContextPtr ctx;
37 vr::ITrackedDeviceServerDriver *driver;
38 const char *serial;
39 const std::string &steam_install;
40
41 DeviceBuilder(const ContextPtr &p_ctx,
42 vr::ITrackedDeviceServerDriver *p_driver,
43 const char *p_serial,
44 const std::string &p_stream_install)
45 : ctx{p_ctx}, driver{p_driver}, serial{p_serial}, steam_install{p_stream_install}
46 {}
47
48 // no copies!
49 DeviceBuilder(const DeviceBuilder &) = delete;
51 operator=(const DeviceBuilder &) = delete;
52};
53
55{
56public:
57 Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize);
58
59 vr::PropertyTypeTag_t tag;
60 std::vector<uint8_t> buffer;
61};
62
63class Device : public xrt_device
64{
65
66public:
67 m_relation_history *relation_hist;
68
69 virtual ~Device();
70
71 xrt_input *
72 get_input_from_name(std::string_view name);
73
76
77 void
78 update_pose(const vr::DriverPose_t &newPose) const;
79
80 //! Helper to use the @ref m_relation_history member.
81 void
82 get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation);
83
84 vr::ETrackedPropertyError
85 handle_properties(const vr::PropertyWrite_t *batch, uint32_t count);
86
87 vr::ETrackedPropertyError
88 handle_read_properties(vr::PropertyRead_t *batch, uint32_t count);
89
90 //! Maps to @ref xrt_device::get_tracked_pose.
91 virtual xrt_result_t
92 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
93
95 get_battery_status(bool *out_present, bool *out_charging, float *out_charge);
96
97protected:
98 Device(const DeviceBuilder &builder);
99 std::shared_ptr<Context> ctx;
100 vr::PropertyContainerHandle_t container_handle{0};
101 std::unordered_map<vr::ETrackedDeviceProperty, Property> properties;
102 std::unordered_map<std::string_view, xrt_input *> inputs_map;
103 std::vector<xrt_input> inputs_vec;
104 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
105 const InputClass *input_class;
106 std::string manufacturer;
107 std::string model;
108 float vsync_to_photon_ns{0.f};
109 bool provides_battery_status{false};
110 bool charging{false};
111 float charge{1.0F};
112
113 vr::ETrackedPropertyError
114 handle_generic_property_write(const vr::PropertyWrite_t &prop);
115 vr::ETrackedPropertyError
116 handle_generic_property_read(vr::PropertyRead_t &prop);
117
118 virtual vr::ETrackedPropertyError
119 handle_property_write(const vr::PropertyWrite_t &prop);
120
121private:
122 vr::ITrackedDeviceServerDriver *driver;
123 uint64_t current_frame{0};
124
125 std::mutex frame_mutex;
126
127 void
128 init_chaperone(const std::string &steam_install);
129};
130
132{
133 vp2_hid *hid{nullptr};
134
136 {
137 if (hid != nullptr) {
138 vp2_hid_destroy(hid);
139 hid = nullptr;
140 }
141 }
142};
143
144class HmdDevice : public Device
145{
146public:
147 VIVE_VARIANT variant{VIVE_UNKNOWN};
148 xrt_pose eye[2] = {XRT_POSE_IDENTITY, XRT_POSE_IDENTITY};
149 float ipd{0.063}; // meters
150 struct Parts
151 {
152 xrt_hmd_parts base;
153 vr::IVRDisplayComponent *display;
154 };
155
157 {
158 float min{0.1f};
159 float max{1.0f};
160 };
161
163 {
164 };
165
166 HmdDevice(const DeviceBuilder &builder);
167
169 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
170
171 void
172 SetDisplayEyeToHead(uint32_t unWhichDevice,
173 const vr::HmdMatrix34_t &eyeToHeadLeft,
174 const vr::HmdMatrix34_t &eyeToHeadRight);
175
177 get_view_poses(const xrt_vec3 *default_eye_relation,
178 uint64_t at_timestamp_ns,
179 uint32_t view_count,
180 xrt_space_relation *out_head_relation,
181 xrt_fov *out_fovs,
182 xrt_pose *out_poses);
183
185 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
186
187 void
188 set_hmd_parts(std::unique_ptr<Parts> parts);
189
190 inline float
191 get_ipd() const
192 {
193 return ipd;
194 }
195
197 get_brightness(float *out_brightness);
199 set_brightness(float brightness, bool relative);
200
201 bool
202 init_vive_pro_2(struct xrt_prober *xp);
203
204private:
205 std::unique_ptr<Parts> hmd_parts{nullptr};
206
207 vr::ETrackedPropertyError
208 handle_property_write(const vr::PropertyWrite_t &prop) override;
209
210 void
211 set_nominal_frame_interval(uint64_t interval_ns);
212
213 std::condition_variable hmd_parts_cv;
214 std::mutex hmd_parts_mut;
215 float brightness{1.0f};
216 AnalogGainRange analog_gain_range{};
217};
218
220{
221public:
222 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
223
226
227 void
228 set_haptic_handle(vr::VRInputComponentHandle_t handle);
229
231 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
232
234 get_finger_from_name(std::string_view name);
235
238 int64_t desired_timestamp_ns,
239 struct xrt_hand_joint_set *out_value,
240 int64_t *out_timestamp_ns);
241
243 get_xrt_hand();
244
245 void
246 update_hand_tracking(int64_t desired_timestamp_ns, struct xrt_hand_joint_set *out);
247
248protected:
249 void
250 set_input_class(const InputClass *input_class);
251
252private:
253 vr::VRInputComponentHandle_t haptic_handle{0};
254 std::unique_ptr<xrt_output> output{nullptr};
255 bool has_index_hand_tracking{false};
256 std::vector<IndexFingerInput> finger_inputs_vec;
257 std::unordered_map<std::string_view, IndexFingerInput *> finger_inputs_map;
258 uint64_t hand_tracking_timestamp;
259
260 void
261 set_hand_tracking_hand(xrt_input_name name);
262
263 vr::ETrackedPropertyError
264 handle_property_write(const vr::PropertyWrite_t &prop) override;
265};
Definition: context.hpp:57
Definition: device.hpp:220
Definition: device.hpp:64
void get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation)
Helper to use the m_relation_history member.
Definition: device.cpp:477
xrt_result_t update_inputs()
Update any attached inputs.
Definition: device.cpp:443
virtual xrt_result_t get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation)=0
Maps to xrt_device::get_tracked_pose.
Definition: device.hpp:145
Definition: device.hpp:55
SteamVR driver context - implements xrt_tracking_origin and IVRDriverContext.
VIVE_VARIANT
Headset variant.
Definition: vive_common.h:42
xrt_hand
Enumeration for left and right hand.
Definition: xrt_defines.h:1373
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1317
enum xrt_result xrt_result_t
Result type used across Monado.
xrt_output_name
Name of a output with a baked in type.
Definition: xrt_defines.h:1947
Small utility for keeping track of the history of an xrt_space_relation, ie.
Definition: device.hpp:34
Definition: device.hpp:157
Definition: device.hpp:151
Definition: device.hpp:163
Definition: context.hpp:41
Definition: device.cpp:51
Definition: device.hpp:132
Definition: m_relation_history.cpp:49
Definition: vp2_hid.c:34
A single HMD or input device.
Definition: xrt_device.h:282
xrt_result_t(* set_output)(struct xrt_device *xdev, enum xrt_output_name name, const struct xrt_output_value *value)
Set a output value.
Definition: xrt_device.h:461
xrt_result_t(* compute_distortion)(struct xrt_device *xdev, uint32_t view, float u, float v, struct xrt_uv_triplet *out_result)
Compute the distortion at a single point.
Definition: xrt_device.h:587
xrt_result_t(* get_hand_tracking)(struct xrt_device *xdev, enum xrt_input_name name, int64_t desired_timestamp_ns, struct xrt_hand_joint_set *out_value, int64_t *out_timestamp_ns)
Get relationship of hand joints to the tracking origin space as the base space.
Definition: xrt_device.h:383
xrt_result_t(* set_brightness)(struct xrt_device *xdev, float brightness, bool relative)
Set the display brightness.
Definition: xrt_device.h:666
xrt_result_t(* get_battery_status)(struct xrt_device *xdev, bool *out_present, bool *out_charging, float *out_charge)
Get battery status information.
Definition: xrt_device.h:642
xrt_result_t(* get_view_poses)(struct xrt_device *xdev, const struct xrt_vec3 *default_eye_relation, int64_t at_timestamp_ns, uint32_t view_count, struct xrt_space_relation *out_head_relation, struct xrt_fov *out_fovs, struct xrt_pose *out_poses)
Get the per-view pose in relation to the view space.
Definition: xrt_device.h:565
xrt_result_t(* get_brightness)(struct xrt_device *xdev, float *out_brightness)
Get the current display brightness.
Definition: xrt_device.h:654
enum xrt_device_name name
Enum identifier of the device.
Definition: xrt_device.h:284
xrt_result_t(* get_tracked_pose)(struct xrt_device *xdev, enum xrt_input_name name, int64_t at_timestamp_ns, struct xrt_space_relation *out_relation)
Get relationship of a tracked device to the tracking origin space as the base space.
Definition: xrt_device.h:355
Describes a projection matrix fov.
Definition: xrt_defines.h:484
Joint set type used for hand tracking.
Definition: xrt_defines.h:1416
All of the device components that deals with interfacing to a users head.
Definition: xrt_device.h:92
A single named input, that sits on a xrt_device.
Definition: xrt_device.h:163
A union of all output types.
Definition: xrt_defines.h:2063
A pose composed of a position and orientation.
Definition: xrt_defines.h:464
The main prober that probes and manages found but not opened HMD devices that are connected to the sy...
Definition: xrt_prober.h:132
A relation with two spaces, includes velocity and acceleration.
Definition: xrt_defines.h:655
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition: xrt_defines.h:264
A 3 element vector with single floats.
Definition: xrt_defines.h:274
Common things like defines for Vive and Index.
Implementation of the Vive Pro 2 HID interface.
Header defining an xrt display or controller device.