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 "xrt/xrt_config_drivers.h"
20
21#include "vp2/vp2_hid.h"
22#ifdef XRT_BUILD_DRIVER_PIMAX_P2
23#include "pimax_p2/pimax_p2.h"
24#endif
25
26#include "openvr_driver.h"
27
28#include <atomic>
29#include <string>
30#include <vector>
31#include <memory>
32#include <unordered_map>
33#include <span>
34#include <array>
35#include <optional>
36
37#include <condition_variable>
38#include <mutex>
39
40class Context;
41struct InputClass;
42
44{
45 using ContextPtr = std::shared_ptr<Context>;
46 ContextPtr ctx;
47 vr::ITrackedDeviceServerDriver *driver;
48 const char *serial;
49 const std::string &steam_install;
50
51 DeviceBuilder(const ContextPtr &p_ctx,
52 vr::ITrackedDeviceServerDriver *p_driver,
53 const char *p_serial,
54 const std::string &p_stream_install)
55 : ctx{p_ctx}, driver{p_driver}, serial{p_serial}, steam_install{p_stream_install}
56 {}
57
58 // no copies!
59 DeviceBuilder(const DeviceBuilder &) = delete;
61 operator=(const DeviceBuilder &) = delete;
62};
63
65{
66public:
67 Property(vr::PropertyTypeTag_t tag, void *buffer, uint32_t bufferSize);
68
69 vr::PropertyTypeTag_t tag;
70 std::vector<uint8_t> buffer;
71};
72
73class Device : public xrt_device
74{
75public:
76 m_relation_history *relation_hist;
77
78 //! The component handles that are attached to this device.
79 std::vector<vr::VRInputComponentHandle_t> handles;
80
81 virtual ~Device();
82
83 xrt_input *
84 get_input_from_name(std::string_view name);
85
86 void
87 update_pose(const vr::DriverPose_t &newPose) const;
88
89 //! Helper to use the @ref m_relation_history member.
90 void
91 get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation);
92
93 vr::ETrackedPropertyError
94 handle_properties(const vr::PropertyWrite_t *batch, uint32_t count);
95
96 vr::ETrackedPropertyError
97 handle_read_properties(vr::PropertyRead_t *batch, uint32_t count);
98
99 //! Maps to @ref xrt_device::get_tracked_pose.
100 virtual xrt_result_t
101 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
102
104 get_battery_status(bool *out_present, bool *out_charging, float *out_charge);
105
106 //! Call with `devices_mut` held.
107 inline vr::VRInputComponentHandle_t
109 {
110 vr::VRInputComponentHandle_t h = ++this->ctx->input.next_handle;
111 this->handles.push_back(h);
112 return h;
113 }
114
115protected:
116 Device(const DeviceBuilder &builder);
117 std::shared_ptr<Context> ctx;
118 vr::PropertyContainerHandle_t container_handle{0};
119 std::unordered_map<vr::ETrackedDeviceProperty, Property> properties;
120 std::unordered_map<std::string_view, xrt_input *> inputs_map;
121 std::vector<xrt_input> inputs_vec;
122 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
123 const InputClass *input_class{nullptr};
124 std::string manufacturer;
125 std::string model;
126 std::atomic<time_duration_ns> vsync_to_photon_ns{0};
127 bool provides_battery_status{false};
128 bool charging{false};
129 float charge{1.0F};
130
131 vr::ETrackedPropertyError
132 handle_generic_property_write(const vr::PropertyWrite_t &prop);
133 vr::ETrackedPropertyError
134 handle_generic_property_read(vr::PropertyRead_t &prop);
135
136 virtual vr::ETrackedPropertyError
137 handle_property_write(const vr::PropertyWrite_t &prop);
138
139 void
140 set_input_class(const InputClass *input_class);
141
142private:
143 vr::ITrackedDeviceServerDriver *driver;
144
145 void
146 init_chaperone(const std::string &steam_install);
147};
148
150{
151 vp2_hid *hid{nullptr};
152
154 {
155 if (hid != nullptr) {
156 vp2_hid_destroy(hid);
157 hid = nullptr;
158 }
159 }
160};
161
162#ifdef XRT_BUILD_DRIVER_PIMAX_P2
163struct PimaxData
164{
165 pimax_p2_device *dev{nullptr};
166
167 ~PimaxData()
168 {
169 if (dev != nullptr) {
170 pimax_p2_destroy(dev);
171 }
172 }
173};
174#endif
175
176class HmdDevice : public Device
177{
178public:
179 VIVE_VARIANT variant{VIVE_UNKNOWN};
180 xrt_pose eye[2] = {XRT_POSE_IDENTITY, XRT_POSE_IDENTITY};
181 float lh_ipd{0.063}; // meters
182 struct Parts
183 {
184 xrt_hmd_parts base;
185 vr::IVRDisplayComponent *display;
186 };
187
189 {
190 float min{0.1f};
191 float max{1.0f};
192 };
193
194 // @todo Remove when clang-format is updated in CI
195 // clang-format off
197#ifdef XRT_BUILD_DRIVER_PIMAX_P2
198 struct PimaxData pimax{};
199#endif
200 // clang-format on
201
202 HmdDevice(const DeviceBuilder &builder);
203
204 float
205 getIpdM();
206
209
211 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
212
213 void
214 SetDisplayEyeToHead(uint32_t unWhichDevice,
215 const vr::HmdMatrix34_t &eyeToHeadLeft,
216 const vr::HmdMatrix34_t &eyeToHeadRight);
217
219 get_view_poses(const xrt_vec3 *default_eye_relation,
220 uint64_t at_timestamp_ns,
221 xrt_view_type view_type,
222 uint32_t view_count,
223 xrt_space_relation *out_head_relation,
224 xrt_fov *out_fovs,
225 xrt_pose *out_poses);
226
228 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
229
230 void
231 set_hmd_parts(std::unique_ptr<Parts> parts);
232
234 get_brightness(float *out_brightness);
236 set_brightness(float brightness, bool relative);
237
240
241 bool
242 init_vive_pro_2(struct xrt_prober *xp);
243
244#ifdef XRT_BUILD_DRIVER_PIMAX_P2
245 bool
246 init_pimax(pimax_p2_device *pdev);
247#endif
248
249private:
250 std::unique_ptr<Parts> hmd_parts{nullptr};
251
252 void
253 set_hmd_class(const InputClass *hmd_class);
254
255 vr::ETrackedPropertyError
256 handle_property_write(const vr::PropertyWrite_t &prop) override;
257
258 void
259 set_nominal_frame_interval(uint64_t interval_ns);
260
261 std::condition_variable hmd_parts_cv;
262 std::mutex hmd_parts_mut;
263 float brightness{1.0f};
264 AnalogGainRange analog_gain_range{};
265};
266
268{
269public:
270 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
271
273 set_output(xrt_output_name name, const xrt_output_value *value);
274
275 void
276 set_haptic_handle(vr::VRInputComponentHandle_t handle);
277
279 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
280
283 int64_t desired_timestamp_ns,
284 struct xrt_hand_joint_set *out_value,
285 int64_t *out_timestamp_ns);
286
288 get_xrt_hand();
289
290 void
291 update_skeleton_transforms(std::span<const vr::VRBoneTransform_t> bones);
292
293 void
294 set_skeleton(std::span<const vr::VRBoneTransform_t> bones, xrt_hand hand, bool is_simulated, const char *path);
295
297 notify_chirality(bool has_chirality, xrt_hand chirality);
298
299protected:
300 void
301 generate_palm_pose_offset(std::span<const vr::VRBoneTransform_t> bones, xrt_hand hand);
302
303private:
304 vr::VRInputComponentHandle_t haptic_handle{0};
305 size_t latest_haptic_event{0};
306 std::unique_ptr<xrt_output> output{nullptr};
307 bool has_hand_tracking{false};
308 xrt_hand skeleton_hand = XRT_HAND_LEFT;
309 std::array<std::optional<xrt_pose>, 2> palm_offsets;
310 std::array<xrt_input, 2> hand_tracking_inputs{};
311
312 struct JointsWithTimestamp
313 {
314 xrt_hand_joint_set joint_set;
315 int64_t timestamp{0};
316 };
318
319 vr::ETrackedPropertyError
320 handle_property_write(const vr::PropertyWrite_t &prop) override;
321};
Definition context.hpp:44
Definition device.hpp:268
Definition device.hpp:74
vr::VRInputComponentHandle_t new_input_handle_locked()
Call with devices_mut held.
Definition device.hpp:108
std::vector< vr::VRInputComponentHandle_t > handles
The component handles that are attached to this device.
Definition device.hpp:79
void get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation)
Helper to use the m_relation_history member.
Definition device.cpp:803
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:177
xrt_result_t update_inputs()
Update any attached inputs.
Definition device.cpp:924
Definition device.hpp:65
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
void pimax_p2_destroy(struct pimax_p2_device *dev)
Clean up a pimax_p2_device.
Definition pimax_p2.c:1111
xrt_hand
Enumeration for left and right hand.
Definition xrt_defines.h:1581
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition xrt_defines.h:1031
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:1715
Small utility for keeping track of the history of an xrt_space_relation, ie.
Pimax P2 driver header - inherits xrt_device.
Definition device.hpp:44
Definition device.hpp:189
Definition device.hpp:183
Definition device.hpp:196
Definition device.cpp:61
Definition device.hpp:150
Definition pimax_p2.h:152
Definition vp2_hid.c:45
Compositor information for a device.
Definition xrt_device.h:120
Compositor mode information for a device.
Definition xrt_device.h:169
A single HMD or input device.
Definition xrt_device.h:345
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:543
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:663
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:444
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:783
xrt_result_t(* set_brightness)(struct xrt_device *xdev, float brightness, bool relative)
Set the display brightness.
Definition xrt_device.h:743
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:719
xrt_result_t(* get_brightness)(struct xrt_device *xdev, float *out_brightness)
Get the current display brightness.
Definition xrt_device.h:731
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:754
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:640
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:416
Describes a projection matrix fov.
Definition xrt_defines.h:555
Joint set type used for hand tracking.
Definition xrt_defines.h:1624
All of the device components that deals with interfacing to a users head.
Definition xrt_device.h:182
A single named input, that sits on a xrt_device.
Definition xrt_device.h:253
A union of all output types.
Definition xrt_defines.h:2492
A pose composed of a position and orientation.
Definition xrt_defines.h:535
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:726
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition xrt_defines.h:313
A 3 element vector with single floats.
Definition xrt_defines.h:323
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:2523
Header defining an xrt display or controller device.