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
58 void
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 void
73 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) = 0;
74
75protected:
76 Device(const DeviceBuilder &builder);
77 std::shared_ptr<Context> ctx;
78 vr::PropertyContainerHandle_t container_handle{0};
79 std::unordered_map<std::string_view, xrt_input *> inputs_map;
80 std::vector<xrt_input> inputs_vec;
81 inline static xrt_pose chaperone = XRT_POSE_IDENTITY;
82 const InputClass *input_class;
83 std::string manufacturer;
84 std::string model;
85 float vsync_to_photon_ns{0.f};
86
87 virtual void
88 handle_property_write(const vr::PropertyWrite_t &prop);
89
90private:
91 vr::ITrackedDeviceServerDriver *driver;
92 uint64_t current_frame{0};
93
94 std::mutex frame_mutex;
95
96 void
97 init_chaperone(const std::string &steam_install);
98};
99
100class HmdDevice : public Device
101{
102public:
103 xrt_pose eye[2];
104 float ipd{0.063}; // meters
105 struct Parts
106 {
107 xrt_hmd_parts base;
108 vr::IVRDisplayComponent *display;
109 };
110
111 HmdDevice(const DeviceBuilder &builder);
112
113 void
114 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
115
116 void
117 SetDisplayEyeToHead(uint32_t unWhichDevice,
118 const vr::HmdMatrix34_t &eyeToHeadLeft,
119 const vr::HmdMatrix34_t &eyeToHeadRight);
120
121 void
122 get_view_poses(const xrt_vec3 *default_eye_relation,
123 uint64_t at_timestamp_ns,
124 uint32_t view_count,
125 xrt_space_relation *out_head_relation,
126 xrt_fov *out_fovs,
127 xrt_pose *out_poses);
128
129 bool
130 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
131
132 void
133 set_hmd_parts(std::unique_ptr<Parts> parts);
134
135 inline float
136 get_ipd() const
137 {
138 return ipd;
139 }
140
141private:
142 std::unique_ptr<Parts> hmd_parts{nullptr};
143
144 void
145 handle_property_write(const vr::PropertyWrite_t &prop) override;
146
147 void
148 set_nominal_frame_interval(uint64_t interval_ns);
149
150 std::condition_variable hmd_parts_cv;
151 std::mutex hmd_parts_mut;
152};
153
155{
156public:
157 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
158
159 void
161
162 void
163 set_haptic_handle(vr::VRInputComponentHandle_t handle);
164
165 void
166 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
167
169 get_finger_from_name(std::string_view name);
170
171 void
173 uint64_t desired_timestamp_ns,
174 struct xrt_hand_joint_set *out_value,
175 uint64_t *out_timestamp_ns);
176
178 get_xrt_hand();
179
180 void
181 update_hand_tracking(struct xrt_hand_joint_set *out);
182
183protected:
184 void
185 set_input_class(const InputClass *input_class);
186
187private:
188 vr::VRInputComponentHandle_t haptic_handle{0};
189 std::unique_ptr<xrt_output> output{nullptr};
190 bool has_index_hand_tracking{false};
191 std::vector<IndexFingerInput> finger_inputs_vec;
192 std::unordered_map<std::string_view, IndexFingerInput *> finger_inputs_map;
193 uint64_t hand_tracking_timestamp;
194
195 void
196 set_hand_tracking_hand(xrt_input_name name);
197
198 void
199 handle_property_write(const vr::PropertyWrite_t &prop) override;
200};
Definition: context.hpp:57
Definition: device.hpp:155
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:412
void update_inputs()
Update any attached inputs.
Definition: device.cpp:380
virtual void 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:101
SteamVR driver context - implements xrt_tracking_origin and IVRDriverContext.
xrt_hand
Enumeration for left and right hand.
Definition: xrt_defines.h:1349
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1293
xrt_output_name
Name of a output with a baked in type.
Definition: xrt_defines.h:1686
Small utility for keeping track of the history of an xrt_space_relation, ie.
Definition: device.hpp:27
Definition: device.hpp:106
Definition: context.hpp:41
Definition: device.cpp:40
Definition: m_relation_history.cpp:46
A single HMD or input device.
Definition: xrt_device.h:230
void(* set_output)(struct xrt_device *xdev, enum xrt_output_name name, const union xrt_output_value *value)
Set a output value.
Definition: xrt_device.h:397
void(* get_view_poses)(struct xrt_device *xdev, const struct xrt_vec3 *default_eye_relation, uint64_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:436
void(* get_tracked_pose)(struct xrt_device *xdev, enum xrt_input_name name, uint64_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:310
enum xrt_device_name name
Enum identifier of the device.
Definition: xrt_device.h:232
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:458
void(* get_hand_tracking)(struct xrt_device *xdev, enum xrt_input_name name, uint64_t desired_timestamp_ns, struct xrt_hand_joint_set *out_value, uint64_t *out_timestamp_ns)
Get relationship of hand joints to the tracking origin space as the base space.
Definition: xrt_device.h:338
Describes a projection matrix fov.
Definition: xrt_defines.h:486
Joint set type used for hand tracking.
Definition: xrt_defines.h:1392
All of the device components that deals with interfacing to a users head.
Definition: xrt_device.h:90
A single named input, that sits on a xrt_device.
Definition: xrt_device.h:161
A pose composed of a position and orientation.
Definition: xrt_defines.h:465
A relation with two spaces, includes velocity and acceleration.
Definition: xrt_defines.h:657
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition: xrt_defines.h:261
A 3 element vector with single floats.
Definition: xrt_defines.h:271
A union of all output types.
Definition: xrt_defines.h:1772
Header defining an xrt display or controller device.