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
23class Context;
24struct InputClass;
25
27{
28 using ContextPtr = std::shared_ptr<Context>;
29 ContextPtr ctx;
30 vr::ITrackedDeviceServerDriver *driver;
31 const char *serial;
32 const std::string &steam_install;
33
34 DeviceBuilder(const ContextPtr &p_ctx,
35 vr::ITrackedDeviceServerDriver *p_driver,
36 const char *p_serial,
37 const std::string &p_stream_install)
38 : ctx{p_ctx}, driver{p_driver}, serial{p_serial}, steam_install{p_stream_install}
39 {}
40
41 // no copies!
42 DeviceBuilder(const DeviceBuilder &) = delete;
44 operator=(const DeviceBuilder &) = delete;
45};
46
48{
49public:
50 Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize);
51
52 vr::PropertyTypeTag_t tag;
53 std::vector<uint8_t> buffer;
54};
55
56class Device : public xrt_device
57{
58
59public:
60 m_relation_history *relation_hist;
61
62 virtual ~Device();
63
64 xrt_input *
65 get_input_from_name(std::string_view name);
66
69
70 void
71 update_pose(const vr::DriverPose_t &newPose) const;
72
73 //! Helper to use the @ref m_relation_history member.
74 void
75 get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation);
76
77 vr::ETrackedPropertyError
78 handle_properties(const vr::PropertyWrite_t *batch, uint32_t count);
79
80 vr::ETrackedPropertyError
81 handle_read_properties(vr::PropertyRead_t *batch, uint32_t count);
82
83 //! Maps to @ref xrt_device::get_tracked_pose.
84 virtual xrt_result_t
85 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
86
88 get_battery_status(bool *out_present, bool *out_charging, float *out_charge);
89
90protected:
91 Device(const DeviceBuilder &builder);
92 std::shared_ptr<Context> ctx;
93 vr::PropertyContainerHandle_t container_handle{0};
94 std::unordered_map<vr::ETrackedDeviceProperty, Property> properties;
95 std::unordered_map<std::string_view, xrt_input *> inputs_map;
96 std::vector<xrt_input> inputs_vec;
97 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
98 const InputClass *input_class;
99 std::string manufacturer;
100 std::string model;
101 float vsync_to_photon_ns{0.f};
102 bool provides_battery_status{false};
103 bool charging{false};
104 float charge{1.0F};
105
106 vr::ETrackedPropertyError
107 handle_generic_property_write(const vr::PropertyWrite_t &prop);
108 vr::ETrackedPropertyError
109 handle_generic_property_read(vr::PropertyRead_t &prop);
110
111 virtual vr::ETrackedPropertyError
112 handle_property_write(const vr::PropertyWrite_t &prop);
113
114private:
115 vr::ITrackedDeviceServerDriver *driver;
116 uint64_t current_frame{0};
117
118 std::mutex frame_mutex;
119
120 void
121 init_chaperone(const std::string &steam_install);
122};
123
124class HmdDevice : public Device
125{
126public:
127 xrt_pose eye[2];
128 float ipd{0.063}; // meters
129 struct Parts
130 {
131 xrt_hmd_parts base;
132 vr::IVRDisplayComponent *display;
133 };
134
136 {
137 float min{0.1f};
138 float max{1.0f};
139 };
140
141 HmdDevice(const DeviceBuilder &builder);
142
144 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
145
146 void
147 SetDisplayEyeToHead(uint32_t unWhichDevice,
148 const vr::HmdMatrix34_t &eyeToHeadLeft,
149 const vr::HmdMatrix34_t &eyeToHeadRight);
150
152 get_view_poses(const xrt_vec3 *default_eye_relation,
153 uint64_t at_timestamp_ns,
154 uint32_t view_count,
155 xrt_space_relation *out_head_relation,
156 xrt_fov *out_fovs,
157 xrt_pose *out_poses);
158
160 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
161
162 void
163 set_hmd_parts(std::unique_ptr<Parts> parts);
164
165 inline float
166 get_ipd() const
167 {
168 return ipd;
169 }
170
172 get_brightness(float *out_brightness);
174 set_brightness(float brightness, bool relative);
175
176private:
177 std::unique_ptr<Parts> hmd_parts{nullptr};
178
179 vr::ETrackedPropertyError
180 handle_property_write(const vr::PropertyWrite_t &prop) override;
181
182 void
183 set_nominal_frame_interval(uint64_t interval_ns);
184
185 std::condition_variable hmd_parts_cv;
186 std::mutex hmd_parts_mut;
187 float brightness{1.0f};
188 AnalogGainRange analog_gain_range{};
189};
190
192{
193public:
194 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
195
198
199 void
200 set_haptic_handle(vr::VRInputComponentHandle_t handle);
201
203 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
204
206 get_finger_from_name(std::string_view name);
207
210 int64_t desired_timestamp_ns,
211 struct xrt_hand_joint_set *out_value,
212 int64_t *out_timestamp_ns);
213
215 get_xrt_hand();
216
217 void
218 update_hand_tracking(int64_t desired_timestamp_ns, struct xrt_hand_joint_set *out);
219
220protected:
221 void
222 set_input_class(const InputClass *input_class);
223
224private:
225 vr::VRInputComponentHandle_t haptic_handle{0};
226 std::unique_ptr<xrt_output> output{nullptr};
227 bool has_index_hand_tracking{false};
228 std::vector<IndexFingerInput> finger_inputs_vec;
229 std::unordered_map<std::string_view, IndexFingerInput *> finger_inputs_map;
230 uint64_t hand_tracking_timestamp;
231
232 void
233 set_hand_tracking_hand(xrt_input_name name);
234
235 vr::ETrackedPropertyError
236 handle_property_write(const vr::PropertyWrite_t &prop) override;
237};
Definition: context.hpp:57
Definition: device.hpp:192
Definition: device.hpp:57
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:125
Definition: device.hpp:48
SteamVR driver context - implements xrt_tracking_origin and IVRDriverContext.
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:27
Definition: device.hpp:136
Definition: device.hpp:130
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
Header defining an xrt display or controller device.