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
47class Device : public xrt_device
48{
49
50public:
51 m_relation_history *relation_hist;
52
53 virtual ~Device();
54
55 xrt_input *
56 get_input_from_name(std::string_view name);
57
60
61 void
62 update_pose(const vr::DriverPose_t &newPose) const;
63
64 //! Helper to use the @ref m_relation_history member.
65 void
66 get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation);
67
68 void
69 handle_properties(const vr::PropertyWrite_t *batch, uint32_t count);
70
71 //! Maps to @ref xrt_device::get_tracked_pose.
72 virtual xrt_result_t
73 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
74
76 get_battery_status(bool *out_present, bool *out_charging, float *out_charge);
77
78protected:
79 Device(const DeviceBuilder &builder);
80 std::shared_ptr<Context> ctx;
81 vr::PropertyContainerHandle_t container_handle{0};
82 std::unordered_map<std::string_view, xrt_input *> inputs_map;
83 std::vector<xrt_input> inputs_vec;
84 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
85 const InputClass *input_class;
86 std::string manufacturer;
87 std::string model;
88 float vsync_to_photon_ns{0.f};
89 bool provides_battery_status{false};
90 bool charging{false};
91 float charge{1.0F};
92
93 virtual void
94 handle_property_write(const vr::PropertyWrite_t &prop);
95
96private:
97 vr::ITrackedDeviceServerDriver *driver;
98 uint64_t current_frame{0};
99
100 std::mutex frame_mutex;
101
102 void
103 init_chaperone(const std::string &steam_install);
104};
105
106class HmdDevice : public Device
107{
108public:
109 xrt_pose eye[2];
110 float ipd{0.063}; // meters
111 struct Parts
112 {
113 xrt_hmd_parts base;
114 vr::IVRDisplayComponent *display;
115 };
116
118 {
119 float min{0.1f};
120 float max{1.0f};
121 };
122
123 HmdDevice(const DeviceBuilder &builder);
124
126 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
127
128 void
129 SetDisplayEyeToHead(uint32_t unWhichDevice,
130 const vr::HmdMatrix34_t &eyeToHeadLeft,
131 const vr::HmdMatrix34_t &eyeToHeadRight);
132
134 get_view_poses(const xrt_vec3 *default_eye_relation,
135 uint64_t at_timestamp_ns,
136 uint32_t view_count,
137 xrt_space_relation *out_head_relation,
138 xrt_fov *out_fovs,
139 xrt_pose *out_poses);
140
141 bool
142 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
143
144 void
145 set_hmd_parts(std::unique_ptr<Parts> parts);
146
147 inline float
148 get_ipd() const
149 {
150 return ipd;
151 }
152
154 get_brightness(float *out_brightness);
156 set_brightness(float brightness, bool relative);
157
158private:
159 std::unique_ptr<Parts> hmd_parts{nullptr};
160
161 void
162 handle_property_write(const vr::PropertyWrite_t &prop) override;
163
164 void
165 set_nominal_frame_interval(uint64_t interval_ns);
166
167 std::condition_variable hmd_parts_cv;
168 std::mutex hmd_parts_mut;
169 float brightness{1.0f};
170 AnalogGainRange analog_gain_range{};
171};
172
174{
175public:
176 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
177
180
181 void
182 set_haptic_handle(vr::VRInputComponentHandle_t handle);
183
185 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
186
188 get_finger_from_name(std::string_view name);
189
192 int64_t desired_timestamp_ns,
193 struct xrt_hand_joint_set *out_value,
194 int64_t *out_timestamp_ns);
195
197 get_xrt_hand();
198
199 void
200 update_hand_tracking(int64_t desired_timestamp_ns, struct xrt_hand_joint_set *out);
201
202protected:
203 void
204 set_input_class(const InputClass *input_class);
205
206private:
207 vr::VRInputComponentHandle_t haptic_handle{0};
208 std::unique_ptr<xrt_output> output{nullptr};
209 bool has_index_hand_tracking{false};
210 std::vector<IndexFingerInput> finger_inputs_vec;
211 std::unordered_map<std::string_view, IndexFingerInput *> finger_inputs_map;
212 uint64_t hand_tracking_timestamp;
213
214 void
215 set_hand_tracking_hand(xrt_input_name name);
216
217 void
218 handle_property_write(const vr::PropertyWrite_t &prop) override;
219};
Definition: context.hpp:57
Definition: device.hpp:174
Definition: device.hpp:48
void get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation)
Helper to use the m_relation_history member.
Definition: device.cpp:462
xrt_result_t update_inputs()
Update any attached inputs.
Definition: device.cpp:428
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:107
SteamVR driver context - implements xrt_tracking_origin and IVRDriverContext.
xrt_hand
Enumeration for left and right hand.
Definition: xrt_defines.h:1369
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1313
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:1926
Small utility for keeping track of the history of an xrt_space_relation, ie.
Definition: device.hpp:27
Definition: device.hpp:118
Definition: device.hpp:112
Definition: context.hpp:41
Definition: device.cpp:43
Definition: m_relation_history.cpp:49
A single HMD or input device.
Definition: xrt_device.h:281
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:444
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:382
xrt_result_t(* set_brightness)(struct xrt_device *xdev, float brightness, bool relative)
Set the display brightness.
Definition: xrt_device.h:649
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:625
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:548
xrt_result_t(* get_brightness)(struct xrt_device *xdev, float *out_brightness)
Get the current display brightness.
Definition: xrt_device.h:637
enum xrt_device_name name
Enum identifier of the device.
Definition: xrt_device.h:283
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:354
bool(* 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:570
Describes a projection matrix fov.
Definition: xrt_defines.h:483
Joint set type used for hand tracking.
Definition: xrt_defines.h:1412
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:2042
A pose composed of a position and orientation.
Definition: xrt_defines.h:463
A relation with two spaces, includes velocity and acceleration.
Definition: xrt_defines.h:654
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition: xrt_defines.h:263
A 3 element vector with single floats.
Definition: xrt_defines.h:273
Header defining an xrt display or controller device.