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
117 HmdDevice(const DeviceBuilder &builder);
118
120 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
121
122 void
123 SetDisplayEyeToHead(uint32_t unWhichDevice,
124 const vr::HmdMatrix34_t &eyeToHeadLeft,
125 const vr::HmdMatrix34_t &eyeToHeadRight);
126
127 void
128 get_view_poses(const xrt_vec3 *default_eye_relation,
129 uint64_t at_timestamp_ns,
130 uint32_t view_count,
131 xrt_space_relation *out_head_relation,
132 xrt_fov *out_fovs,
133 xrt_pose *out_poses);
134
135 bool
136 compute_distortion(uint32_t view, float u, float v, xrt_uv_triplet *out_result);
137
138 void
139 set_hmd_parts(std::unique_ptr<Parts> parts);
140
141 inline float
142 get_ipd() const
143 {
144 return ipd;
145 }
146
147private:
148 std::unique_ptr<Parts> hmd_parts{nullptr};
149
150 void
151 handle_property_write(const vr::PropertyWrite_t &prop) override;
152
153 void
154 set_nominal_frame_interval(uint64_t interval_ns);
155
156 std::condition_variable hmd_parts_cv;
157 std::mutex hmd_parts_mut;
158};
159
161{
162public:
163 ControllerDevice(vr::PropertyContainerHandle_t container_handle, const DeviceBuilder &builder);
164
165 void
167
168 void
169 set_haptic_handle(vr::VRInputComponentHandle_t handle);
170
172 get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns, xrt_space_relation *out_relation) override;
173
175 get_finger_from_name(std::string_view name);
176
177 void
179 int64_t desired_timestamp_ns,
180 struct xrt_hand_joint_set *out_value,
181 int64_t *out_timestamp_ns);
182
184 get_xrt_hand();
185
186 void
187 update_hand_tracking(int64_t desired_timestamp_ns, struct xrt_hand_joint_set *out);
188
189protected:
190 void
191 set_input_class(const InputClass *input_class);
192
193private:
194 vr::VRInputComponentHandle_t haptic_handle{0};
195 std::unique_ptr<xrt_output> output{nullptr};
196 bool has_index_hand_tracking{false};
197 std::vector<IndexFingerInput> finger_inputs_vec;
198 std::unordered_map<std::string_view, IndexFingerInput *> finger_inputs_map;
199 uint64_t hand_tracking_timestamp;
200
201 void
202 set_hand_tracking_hand(xrt_input_name name);
203
204 void
205 handle_property_write(const vr::PropertyWrite_t &prop) override;
206};
Definition: context.hpp:57
Definition: device.hpp:161
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:415
xrt_result_t update_inputs()
Update any attached inputs.
Definition: device.cpp:382
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:1354
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1298
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:1801
Small utility for keeping track of the history of an xrt_space_relation, ie.
Definition: device.hpp:27
Definition: device.hpp:112
Definition: context.hpp:41
Definition: device.cpp:40
Definition: m_relation_history.cpp:46
A single HMD or input device.
Definition: xrt_device.h:241
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:412
void(* 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:350
void(* 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:451
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:528
enum xrt_device_name name
Enum identifier of the device.
Definition: xrt_device.h:243
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:322
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:473
Describes a projection matrix fov.
Definition: xrt_defines.h:486
Joint set type used for hand tracking.
Definition: xrt_defines.h:1397
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:1887
Header defining an xrt display or controller device.