Monado OpenXR Runtime
Loading...
Searching...
No Matches
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
15#include "xrt/xrt_device.h"
16
17#include "vive/vive_common.h"
18
19#include "vp2/vp2_hid.h"
20
21#include "openvr_driver.h"
22
23#include <string>
24#include <vector>
25#include <memory>
26#include <unordered_map>
27#include <span>
28#include <array>
29#include <optional>
30
31#include <condition_variable>
32#include <mutex>
33
34class Context;
35struct InputClass;
36
38{
39 using ContextPtr = std::shared_ptr<Context>;
40 ContextPtr ctx;
41 vr::ITrackedDeviceServerDriver *driver;
42 const char *serial;
43 const std::string &steam_install;
44
45 DeviceBuilder(const ContextPtr &p_ctx,
46 vr::ITrackedDeviceServerDriver *p_driver,
47 const char *p_serial,
48 const std::string &p_stream_install)
49 : ctx{p_ctx}, driver{p_driver}, serial{p_serial}, steam_install{p_stream_install}
50 {}
51
52 // no copies!
53 DeviceBuilder(const DeviceBuilder &) = delete;
55 operator=(const DeviceBuilder &) = delete;
56};
57
59{
60public:
61 Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize);
62
63 vr::PropertyTypeTag_t tag;
64 std::vector<uint8_t> buffer;
65};
66
67class Device : public xrt_device
68{
69public:
70 m_relation_history *relation_hist;
71
72 //! The component handles that are attached to this device.
73 std::vector<vr::VRInputComponentHandle_t> handles;
74
75 virtual ~Device();
76
77 xrt_input *
78 get_input_from_name(std::string_view name);
79
80 void
81 update_pose(const vr::DriverPose_t &newPose) const;
82
83 //! Helper to use the @ref m_relation_history member.
84 void
85 get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation);
86
87 vr::ETrackedPropertyError
88 handle_properties(const vr::PropertyWrite_t *batch, uint32_t count);
89
90 vr::ETrackedPropertyError
91 handle_read_properties(vr::PropertyRead_t *batch, uint32_t count);
92
93 //! Maps to @ref xrt_device::get_tracked_pose.
94 virtual xrt_result_t
95 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
96
98 get_battery_status(bool *out_present, bool *out_charging, float *out_charge);
99
100 //! Call with `devices_mut` held.
101 inline vr::VRInputComponentHandle_t
103 {
104 vr::VRInputComponentHandle_t h = ++this->ctx->input.next_handle;
105 this->handles.push_back(h);
106 return h;
107 }
108
109protected:
110 Device(const DeviceBuilder &builder);
111 std::shared_ptr<Context> ctx;
112 vr::PropertyContainerHandle_t container_handle{0};
113 std::unordered_map<vr::ETrackedDeviceProperty, Property> properties;
114 std::unordered_map<std::string_view, xrt_input *> inputs_map;
115 std::vector<xrt_input> inputs_vec;
116 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
117 const InputClass *input_class;
118 std::string manufacturer;
119 std::string model;
120 float vsync_to_photon_ns{0.f};
121 bool provides_battery_status{false};
122 bool charging{false};
123 float charge{1.0F};
124
125 vr::ETrackedPropertyError
126 handle_generic_property_write(const vr::PropertyWrite_t &prop);
127 vr::ETrackedPropertyError
128 handle_generic_property_read(vr::PropertyRead_t &prop);
129
130 virtual vr::ETrackedPropertyError
131 handle_property_write(const vr::PropertyWrite_t &prop);
132
133private:
134 vr::ITrackedDeviceServerDriver *driver;
135
136 void
137 init_chaperone(const std::string &steam_install);
138};
139
141{
142 vp2_hid *hid{nullptr};
143
145 {
146 if (hid != nullptr) {
147 vp2_hid_destroy(hid);
148 hid = nullptr;
149 }
150 }
151};
152
153class HmdDevice : public Device
154{
155public:
156 VIVE_VARIANT variant{VIVE_UNKNOWN};
157 xrt_pose eye[2] = {XRT_POSE_IDENTITY, XRT_POSE_IDENTITY};
158 float ipd{0.063}; // meters
159 struct Parts
160 {
161 xrt_hmd_parts base;
162 vr::IVRDisplayComponent *display;
163 };
164
166 {
167 float min{0.1f};
168 float max{1.0f};
169 };
170
171 // @todo Remove when clang-format is updated in CI
172 // clang-format off
174 // clang-format on
175
176 HmdDevice(const DeviceBuilder &builder);
177
179 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
180
181 void
182 SetDisplayEyeToHead(uint32_t unWhichDevice,
183 const vr::HmdMatrix34_t &eyeToHeadLeft,
184 const vr::HmdMatrix34_t &eyeToHeadRight);
185
187 get_view_poses(const xrt_vec3 *default_eye_relation,
188 uint64_t at_timestamp_ns,
189 xrt_view_type view_type,
190 uint32_t view_count,
191 xrt_space_relation *out_head_relation,
192 xrt_fov *out_fovs,
193 xrt_pose *out_poses);
194
196 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
197
198 void
199 set_hmd_parts(std::unique_ptr<Parts> parts);
200
201 inline float
202 get_ipd() const
203 {
204 return ipd;
205 }
206
208 get_brightness(float *out_brightness);
210 set_brightness(float brightness, bool relative);
211
214
215 bool
216 init_vive_pro_2(struct xrt_prober *xp);
217
218private:
219 std::unique_ptr<Parts> hmd_parts{nullptr};
220
221 vr::ETrackedPropertyError
222 handle_property_write(const vr::PropertyWrite_t &prop) override;
223
224 void
225 set_nominal_frame_interval(uint64_t interval_ns);
226
227 std::condition_variable hmd_parts_cv;
228 std::mutex hmd_parts_mut;
229 float brightness{1.0f};
230 AnalogGainRange analog_gain_range{};
231};
232
234{
235public:
236 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
237
239 set_output(xrt_output_name name, const xrt_output_value *value);
240
241 void
242 set_haptic_handle(vr::VRInputComponentHandle_t handle);
243
245 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
246
249 int64_t desired_timestamp_ns,
250 struct xrt_hand_joint_set *out_value,
251 int64_t *out_timestamp_ns);
252
254 get_xrt_hand();
255
256 void
257 update_skeleton_transforms(std::span<const vr::VRBoneTransform_t> bones);
258
259 void
260 set_skeleton(std::span<const vr::VRBoneTransform_t> bones, xrt_hand hand, bool is_simulated, const char *path);
261
263 notify_chirality(bool has_chirality, xrt_hand chirality);
264
265protected:
266 void
267 set_input_class(const InputClass *input_class);
268
269 void
270 generate_palm_pose_offset(std::span<const vr::VRBoneTransform_t> bones, xrt_hand hand);
271
272private:
273 vr::VRInputComponentHandle_t haptic_handle{0};
274 size_t latest_haptic_event{0};
275 std::unique_ptr<xrt_output> output{nullptr};
276 bool has_hand_tracking{false};
277 xrt_hand skeleton_hand = XRT_HAND_LEFT;
278 std::array<std::optional<xrt_pose>, 2> palm_offsets;
279 std::array<xrt_input, 2> hand_tracking_inputs{};
280
281 struct JointsWithTimestamp
282 {
283 xrt_hand_joint_set joint_set;
284 int64_t timestamp{0};
285 };
287
288 vr::ETrackedPropertyError
289 handle_property_write(const vr::PropertyWrite_t &prop) override;
290};
Definition context.hpp:44
Definition device.hpp:234
Definition device.hpp:68
vr::VRInputComponentHandle_t new_input_handle_locked()
Call with devices_mut held.
Definition device.hpp:102
std::vector< vr::VRInputComponentHandle_t > handles
The component handles that are attached to this device.
Definition device.hpp:73
void get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation)
Helper to use the m_relation_history member.
Definition device.cpp:778
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:154
Definition device.hpp:59
Stores some number of values in a ring buffer, overwriting the earliest-pushed-remaining element if o...
Definition u_template_historybuf.hpp:38
SteamVR driver context - implements xrt_tracking_origin and IVRDriverContext.
VIVE_VARIANT
Headset variant.
Definition vive_common.h:44
xrt_hand
Enumeration for left and right hand.
Definition xrt_defines.h:1499
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition xrt_defines.h:951
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:1633
Small utility for keeping track of the history of an xrt_space_relation, ie.
Definition device.hpp:38
Definition device.hpp:166
Definition device.hpp:160
Definition device.hpp:173
Definition device.cpp:59
Definition device.hpp:141
Definition m_relation_history.cpp:49
Definition vp2_hid.c:40
Compositor information for a device.
Definition xrt_device.h:110
Compositor mode information for a device.
Definition xrt_device.h:142
A single HMD or input device.
Definition xrt_device.h:357
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:560
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:680
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:461
xrt_result_t(* notify_chirality)(struct xrt_device *xdev, bool has_chirality, enum xrt_hand chirality)
Notify the device of it's new chirality, or that it no longer has a set chirality.
Definition xrt_device.h:800
xrt_result_t(* set_brightness)(struct xrt_device *xdev, float brightness, bool relative)
Set the display brightness.
Definition xrt_device.h:760
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:736
xrt_result_t(* get_brightness)(struct xrt_device *xdev, float *out_brightness)
Get the current display brightness.
Definition xrt_device.h:748
xrt_result_t(* get_compositor_info)(struct xrt_device *xdev, const struct xrt_device_compositor_mode *mode, struct xrt_device_compositor_info *out_info)
Gets the compositor info for a device for the given mode.
Definition xrt_device.h:771
xrt_result_t(* get_view_poses)(struct xrt_device *xdev, const struct xrt_vec3 *default_eye_relation, int64_t at_timestamp_ns, enum xrt_view_type view_type, 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:657
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:433
Describes a projection matrix fov.
Definition xrt_defines.h:533
Joint set type used for hand tracking.
Definition xrt_defines.h:1542
All of the device components that deals with interfacing to a users head.
Definition xrt_device.h:155
A single named input, that sits on a xrt_device.
Definition xrt_device.h:226
A union of all output types.
Definition xrt_defines.h:2407
A pose composed of a position and orientation.
Definition xrt_defines.h:513
The main prober that probes and manages found but not opened HMD devices that are connected to the sy...
Definition xrt_prober.h:136
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:704
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition xrt_defines.h:300
A 3 element vector with single floats.
Definition xrt_defines.h:310
Ringbuffer implementation for keeping track of the past state of things.
Common things like defines for Vive and Index.
Implementation of the Vive Pro 2 HID interface.
xrt_view_type
View type to be rendered to by the compositor.
Definition xrt_defines.h:2438
Header defining an xrt display or controller device.