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