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
10#include <string>
11#include <vector>
12#include <memory>
13#include <unordered_map>
14
15#include <condition_variable>
16#include <mutex>
17
20#include "xrt/xrt_device.h"
21#include "openvr_driver.h"
22#include "vive/vive_common.h"
23
24class Context;
25struct InputClass;
26
28{
29 using ContextPtr = std::shared_ptr<Context>;
30 ContextPtr ctx;
31 vr::ITrackedDeviceServerDriver *driver;
32 const char *serial;
33 const std::string &steam_install;
34
35 DeviceBuilder(const ContextPtr &p_ctx,
36 vr::ITrackedDeviceServerDriver *p_driver,
37 const char *p_serial,
38 const std::string &p_stream_install)
39 : ctx{p_ctx}, driver{p_driver}, serial{p_serial}, steam_install{p_stream_install}
40 {}
41
42 // no copies!
43 DeviceBuilder(const DeviceBuilder &) = delete;
45 operator=(const DeviceBuilder &) = delete;
46};
47
49{
50public:
51 Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize);
52
53 vr::PropertyTypeTag_t tag;
54 std::vector<uint8_t> buffer;
55};
56
57class Device : public xrt_device
58{
59
60public:
61 m_relation_history *relation_hist;
62
63 virtual ~Device();
64
65 xrt_input *
66 get_input_from_name(std::string_view name);
67
70
71 void
72 update_pose(const vr::DriverPose_t &newPose) const;
73
74 //! Helper to use the @ref m_relation_history member.
75 void
76 get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation);
77
78 vr::ETrackedPropertyError
79 handle_properties(const vr::PropertyWrite_t *batch, uint32_t count);
80
81 vr::ETrackedPropertyError
82 handle_read_properties(vr::PropertyRead_t *batch, uint32_t count);
83
84 //! Maps to @ref xrt_device::get_tracked_pose.
85 virtual xrt_result_t
86 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
87
89 get_battery_status(bool *out_present, bool *out_charging, float *out_charge);
90
91protected:
92 Device(const DeviceBuilder &builder);
93 std::shared_ptr<Context> ctx;
94 vr::PropertyContainerHandle_t container_handle{0};
95 std::unordered_map<vr::ETrackedDeviceProperty, Property> properties;
96 std::unordered_map<std::string_view, xrt_input *> inputs_map;
97 std::vector<xrt_input> inputs_vec;
98 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
99 const InputClass *input_class;
100 std::string manufacturer;
101 std::string model;
102 float vsync_to_photon_ns{0.f};
103 bool provides_battery_status{false};
104 bool charging{false};
105 float charge{1.0F};
106
107 vr::ETrackedPropertyError
108 handle_generic_property_write(const vr::PropertyWrite_t &prop);
109 vr::ETrackedPropertyError
110 handle_generic_property_read(vr::PropertyRead_t &prop);
111
112 virtual vr::ETrackedPropertyError
113 handle_property_write(const vr::PropertyWrite_t &prop);
114
115private:
116 vr::ITrackedDeviceServerDriver *driver;
117 uint64_t current_frame{0};
118
119 std::mutex frame_mutex;
120
121 void
122 init_chaperone(const std::string &steam_install);
123};
124
125class HmdDevice : public Device
126{
127public:
128 xrt_pose eye[2];
129 float ipd{0.063}; // meters
130 struct Parts
131 {
132 xrt_hmd_parts base;
133 vr::IVRDisplayComponent *display;
134 };
135
137 {
138 float min{0.1f};
139 float max{1.0f};
140 };
141
142 HmdDevice(const DeviceBuilder &builder);
143
145 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
146
147 void
148 SetDisplayEyeToHead(uint32_t unWhichDevice,
149 const vr::HmdMatrix34_t &eyeToHeadLeft,
150 const vr::HmdMatrix34_t &eyeToHeadRight);
151
153 get_view_poses(const xrt_vec3 *default_eye_relation,
154 uint64_t at_timestamp_ns,
155 uint32_t view_count,
156 xrt_space_relation *out_head_relation,
157 xrt_fov *out_fovs,
158 xrt_pose *out_poses);
159
161 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
162
163 void
164 set_hmd_parts(std::unique_ptr<Parts> parts);
165
166 inline float
167 get_ipd() const
168 {
169 return ipd;
170 }
171
173 get_brightness(float *out_brightness);
175 set_brightness(float brightness, bool relative);
176
177private:
178 std::unique_ptr<Parts> hmd_parts{nullptr};
179
180 vr::ETrackedPropertyError
181 handle_property_write(const vr::PropertyWrite_t &prop) override;
182
183 void
184 set_nominal_frame_interval(uint64_t interval_ns);
185
186 std::condition_variable hmd_parts_cv;
187 std::mutex hmd_parts_mut;
188 float brightness{1.0f};
189 AnalogGainRange analog_gain_range{};
190 VIVE_VARIANT variant{VIVE_UNKNOWN};
191};
192
194{
195public:
196 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
197
200
201 void
202 set_haptic_handle(vr::VRInputComponentHandle_t handle);
203
205 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
206
208 get_finger_from_name(std::string_view name);
209
212 int64_t desired_timestamp_ns,
213 struct xrt_hand_joint_set *out_value,
214 int64_t *out_timestamp_ns);
215
217 get_xrt_hand();
218
219 void
220 update_hand_tracking(int64_t desired_timestamp_ns, struct xrt_hand_joint_set *out);
221
222protected:
223 void
224 set_input_class(const InputClass *input_class);
225
226private:
227 vr::VRInputComponentHandle_t haptic_handle{0};
228 std::unique_ptr<xrt_output> output{nullptr};
229 bool has_index_hand_tracking{false};
230 std::vector<IndexFingerInput> finger_inputs_vec;
231 std::unordered_map<std::string_view, IndexFingerInput *> finger_inputs_map;
232 uint64_t hand_tracking_timestamp;
233
234 void
235 set_hand_tracking_hand(xrt_input_name name);
236
237 vr::ETrackedPropertyError
238 handle_property_write(const vr::PropertyWrite_t &prop) override;
239};
Definition: context.hpp:57
Definition: device.hpp:194
Definition: device.hpp:58
void get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation)
Helper to use the m_relation_history member.
Definition: device.cpp:469
xrt_result_t update_inputs()
Update any attached inputs.
Definition: device.cpp:435
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:126
Definition: device.hpp:49
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:1370
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1314
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:1944
Small utility for keeping track of the history of an xrt_space_relation, ie.
Definition: device.hpp:28
Definition: device.hpp:137
Definition: device.hpp:131
Definition: context.hpp:41
Definition: device.cpp:43
Definition: m_relation_history.cpp:49
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:1413
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:2069
A pose composed of a position and orientation.
Definition: xrt_defines.h:464
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.
Header defining an xrt display or controller device.