Monado OpenXR Runtime
xrt_system.h
Go to the documentation of this file.
1 // Copyright 2020-2024, Collabora, Ltd.
2 // Copyright 2023, NVIDIA CORPORATION.
3 // SPDX-License-Identifier: BSL-1.0
4 /*!
5  * @file
6  * @brief Header for system objects.
7  * @author Jakob Bornecrantz <jakob@collabora.com>
8  * @author Korcan Hussein <korcan.hussein@collabora.com>
9  * @ingroup xrt_iface
10  */
11 
12 #pragma once
13 
14 #include "xrt/xrt_compiler.h"
15 #include "xrt/xrt_defines.h"
16 
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 struct xrt_instance;
23 struct xrt_system_devices;
24 struct xrt_session;
26 struct xrt_session_info;
27 
28 
29 /*
30  *
31  * System.
32  *
33  */
34 
35 #define XRT_SYSTEM_ID 1
36 
37 #define XRT_MAX_SYSTEM_NAME_SIZE 256
38 
39 /*!
40  * Properties provided by the system.
41  */
43 {
44  uint32_t vendor_id;
45  char name[XRT_MAX_SYSTEM_NAME_SIZE];
46 };
47 
48 /*!
49  * A system is a collection of devices, policies and optionally a compositor
50  * that is organised into a chosive group that is usable by one user, most of
51  * the functionality of a system is exposed through other objects, this is the
52  * main object. It is from this you create sessions that is used to by apps to
53  * interact with the "system".
54  *
55  * Sibling objects: @ref xrt_system_devices, @ref xrt_system_compositor and
56  * @ref xrt_space_overseer.
57  *
58  * @ingroup xrt_iface
59  */
60 struct xrt_system
61 {
62  /*!
63  * Create a @ref xrt_session and optionally a @ref xrt_compositor_native
64  * for this system.
65  *
66  * param[in] xsys Pointer to self.
67  * param[in] xsi Session info.
68  * param[out] out_xs Created session.
69  * param[out] out_xcn Native compositor for this session, optional.
70  */
72  const struct xrt_session_info *xsi,
73  struct xrt_session **out_xs,
74  struct xrt_compositor_native **out_xcn);
75 
76  /*!
77  * Destroy the system, must be destroyed after system devices and system
78  * compositor has been destroyed.
79  *
80  * Code consuming this interface should use @ref xrt_system_destroy.
81  *
82  * @param xsys Pointer to self
83  */
84  void (*destroy)(struct xrt_system *xsys);
85 
86  struct xrt_system_properties properties;
87 };
88 
89 /*!
90  * @copydoc xrt_system::create_session
91  *
92  * Helper for calling through the function pointer.
93  *
94  * @public @memberof xrt_system
95  */
96 static inline xrt_result_t
98  const struct xrt_session_info *xsi,
99  struct xrt_session **out_xs,
100  struct xrt_compositor_native **out_xcn)
101 {
102  return xsys->create_session(xsys, xsi, out_xs, out_xcn);
103 }
104 
105 /*!
106  * Destroy an xrt_system - helper function.
107  *
108  * @param[in,out] xsysd_ptr A pointer to the xrt_system struct pointer.
109  *
110  * Will destroy the system if `*xsys_ptr` is not NULL. Will then set
111  * `*xsys_ptr` to NULL.
112  *
113  * @public @memberof xrt_system
114  */
115 static inline void
116 xrt_system_destroy(struct xrt_system **xsys_ptr)
117 {
118  struct xrt_system *xsys = *xsys_ptr;
119  if (xsys == NULL) {
120  return;
121  }
122 
123  *xsys_ptr = NULL;
124  xsys->destroy(xsys);
125 }
126 
127 
128 /*
129  *
130  * System devices.
131  *
132  */
133 
134 /*!
135  * Maximum number of devices simultaneously usable by an implementation of
136  * @ref xrt_system_devices
137  *
138  * @ingroup xrt_iface
139  */
140 #define XRT_SYSTEM_MAX_DEVICES (32)
141 
142 /*!
143  * Data associating a device index (in @ref xrt_system_devices::xdevs) with a
144  * given "role" for dynamic role switching.
145  *
146  * For each of the named roles, a negative value means unpopulated/not available.
147  *
148  * Populated by a call from the @ref xrt_system_devices interface.
149  *
150  * When the caller of @ref xrt_system_devices_get_roles sees a change (based on
151  * comparing @ref generation_id) the caller must do the needed actions to handle
152  * device changes. For example, for the OpenXR state tracker this may include
153  * rebinding, queuing a change to the current interaction profile, and queuing
154  * the events associated with such a change.
155  *
156  * @see xrt_system_devices
157  * @ingroup xrt_iface
158  */
160 {
161  /*!
162  * Monotonically increasing generation counter for the association
163  * between role and index.
164  *
165  * Increment whenever the roles are changed.
166  *
167  * All valid values are greater then zero; this is to
168  * make init easier where any cache can start at zero and be guaranteed
169  * to be replaced with a new @ref xrt_system_roles.
170  */
171  uint64_t generation_id;
172 
173  /*!
174  * Index in @ref xrt_system_devices::xdevs for the user's left
175  * controller/hand, or negative if none available.
176  */
177  int32_t left;
178 
179  /*!
180  * Index in @ref xrt_system_devices::xdevs for the user's right
181  * controller/hand, or negative if none available.
182  */
183  int32_t right;
184 
185  /*!
186  * Index in @ref xrt_system_devices::xdevs for the user's gamepad
187  * device, or negative if none available.
188  */
189  int32_t gamepad;
190 
191  enum xrt_device_name left_profile;
192 
193  enum xrt_device_name right_profile;
194 
195  enum xrt_device_name gamepad_profile;
196 };
197 
198 /*!
199  * Guaranteed invalid constant for @ref xrt_system_roles, not using designated
200  * initializers due to C++.
201  *
202  * @ingroup xrt_iface
203  * @relates xrt_system_roles
204  */
205 #define XRT_SYSTEM_ROLES_INIT \
206  { \
207  0, -1, -1, -1, XRT_DEVICE_INVALID, XRT_DEVICE_INVALID, XRT_DEVICE_INVALID, \
208  }
209 
210 
211 /*!
212  * A collection of @ref xrt_device, and an interface for identifying the roles
213  * they have been assigned.
214  *
215  * @see xrt_device, xrt_instance.
216  */
218 {
219  /*!
220  * All devices known in the system.
221  *
222  * This is conventionally considered the "owning" reference to the devices.
223  * Valid entries are contiguous.
224  */
226 
227  /*!
228  * The number of elements in @ref xdevs that are valid.
229  */
230  size_t xdev_count;
231 
232  /*!
233  * Observing pointers for devices in some static (unchangeable) roles.
234  *
235  * All pointers in this struct must also exist in @ref xdevs. The
236  * association between a member of this struct and a given device
237  * cannot change during runtime.
238  */
239  struct
240  {
241  /*!
242  * An observing pointer to the device serving as the "head"
243  * (and HMD).
244  *
245  * Required.
246  */
247  struct xrt_device *head;
248 
249  /*!
250  * An observing pointer to the device providing eye tracking
251  * (optional).
252  */
253  struct xrt_device *eyes;
254 
255  /*!
256  * An observing pointer to the device providing face tracking
257  * (optional).
258  */
259  struct xrt_device *face;
260 
261  /*!
262  * Devices providing optical (or otherwise more directly
263  * measured than from controller estimation) hand tracking.
264  */
265  struct
266  {
267 
268  /*!
269  * An observing pointer to the device providing hand
270  * tracking for the left hand (optional).
271  *
272  * Currently this is used for both optical and
273  * controller driven hand-tracking.
274  */
275  struct xrt_device *left;
276 
277  /*!
278  * An observing pointer to the device providing hand
279  * tracking for the right hand (optional).
280  *
281  * Currently this is used for both optical and
282  * controller driven hand-tracking.
283  */
284  struct xrt_device *right;
287 
288 
289  /*!
290  * Function to get the dynamic input device roles from this system
291  * devices, see @ref xrt_system_roles for more information.
292  *
293  * @param xsysd Pointer to self
294  * @param[out] out_roles Pointer to xrt_system_roles
295  */
296  xrt_result_t (*get_roles)(struct xrt_system_devices *xsysd, struct xrt_system_roles *out_roles);
297 
298  /*!
299  * Destroy all the devices that are owned by this system devices.
300  *
301  * Code consuming this interface should use @ref xrt_system_devices_destroy.
302  *
303  * @param xsysd Pointer to self
304  */
305  void (*destroy)(struct xrt_system_devices *xsysd);
306 };
307 
308 /*!
309  * @copydoc xrt_system_devices::get_roles
310  *
311  * Helper for calling through the function pointer.
312  *
313  * @public @memberof xrt_system_devices
314  */
315 static inline xrt_result_t
317 {
318  return xsysd->get_roles(xsysd, out_roles);
319 }
320 
321 /*!
322  * Destroy an xrt_system_devices and owned devices - helper function.
323  *
324  * @param[in,out] xsysd_ptr A pointer to the xrt_system_devices struct pointer.
325  *
326  * Will destroy the system devices if `*xsysd_ptr` is not NULL. Will then set
327  * `*xsysd_ptr` to NULL.
328  *
329  * @public @memberof xrt_system_devices
330  */
331 static inline void
333 {
334  struct xrt_system_devices *xsysd = *xsysd_ptr;
335  if (xsysd == NULL) {
336  return;
337  }
338 
339  *xsysd_ptr = NULL;
340  xsysd->destroy(xsysd);
341 }
342 
343 
344 #ifdef __cplusplus
345 }
346 #endif
#define XRT_SYSTEM_MAX_DEVICES
Maximum number of devices simultaneously usable by an implementation of xrt_system_devices.
Definition: xrt_system.h:140
enum xrt_result xrt_result_t
Result type used across Monado.
Main compositor server interface.
Definition: xrt_compositor.h:2196
A single HMD or input device.
Definition: xrt_device.h:230
This interface acts as a root object for Monado.
Definition: xrt_instance.h:67
Session information, mostly overlay extension data.
Definition: xrt_compositor.h:930
The XRT representation of XrSession, this object does not have all of the functionality of a session,...
Definition: xrt_session.h:233
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition: xrt_system.h:218
struct xrt_system_devices::@235::@236 hand_tracking
Devices providing optical (or otherwise more directly measured than from controller estimation) hand ...
size_t xdev_count
The number of elements in xdevs that are valid.
Definition: xrt_system.h:230
static xrt_result_t xrt_system_devices_get_roles(struct xrt_system_devices *xsysd, struct xrt_system_roles *out_roles)
Function to get the dynamic input device roles from this system devices, see xrt_system_roles for mor...
Definition: xrt_system.h:316
xrt_result_t(* get_roles)(struct xrt_system_devices *xsysd, struct xrt_system_roles *out_roles)
Function to get the dynamic input device roles from this system devices, see xrt_system_roles for mor...
Definition: xrt_system.h:296
void(* destroy)(struct xrt_system_devices *xsysd)
Destroy all the devices that are owned by this system devices.
Definition: xrt_system.h:305
struct xrt_device * head
An observing pointer to the device serving as the "head" (and HMD).
Definition: xrt_system.h:247
struct xrt_device * face
An observing pointer to the device providing face tracking (optional).
Definition: xrt_system.h:259
struct xrt_device * eyes
An observing pointer to the device providing eye tracking (optional).
Definition: xrt_system.h:253
struct xrt_device * xdevs[(32)]
All devices known in the system.
Definition: xrt_system.h:225
struct xrt_system_devices::@235 static_roles
Observing pointers for devices in some static (unchangeable) roles.
struct xrt_device * left
An observing pointer to the device providing hand tracking for the left hand (optional).
Definition: xrt_system.h:275
struct xrt_device * right
An observing pointer to the device providing hand tracking for the right hand (optional).
Definition: xrt_system.h:284
static void xrt_system_devices_destroy(struct xrt_system_devices **xsysd_ptr)
Destroy an xrt_system_devices and owned devices - helper function.
Definition: xrt_system.h:332
Properties provided by the system.
Definition: xrt_system.h:43
Data associating a device index (in xrt_system_devices::xdevs) with a given "role" for dynamic role s...
Definition: xrt_system.h:160
int32_t right
Index in xrt_system_devices::xdevs for the user's right controller/hand, or negative if none availabl...
Definition: xrt_system.h:183
uint64_t generation_id
Monotonically increasing generation counter for the association between role and index.
Definition: xrt_system.h:171
int32_t gamepad
Index in xrt_system_devices::xdevs for the user's gamepad device, or negative if none available.
Definition: xrt_system.h:189
int32_t left
Index in xrt_system_devices::xdevs for the user's left controller/hand, or negative if none available...
Definition: xrt_system.h:177
A system is a collection of devices, policies and optionally a compositor that is organised into a ch...
Definition: xrt_system.h:61
xrt_result_t(* create_session)(struct xrt_system *xsys, const struct xrt_session_info *xsi, struct xrt_session **out_xs, struct xrt_compositor_native **out_xcn)
Create a xrt_session and optionally a xrt_compositor_native for this system.
Definition: xrt_system.h:71
static xrt_result_t xrt_system_create_session(struct xrt_system *xsys, const struct xrt_session_info *xsi, struct xrt_session **out_xs, struct xrt_compositor_native **out_xcn)
Create a xrt_session and optionally a xrt_compositor_native for this system.
Definition: xrt_system.h:97
static void xrt_system_destroy(struct xrt_system **xsys_ptr)
Destroy an xrt_system - helper function.
Definition: xrt_system.h:116
void(* destroy)(struct xrt_system *xsys)
Destroy the system, must be destroyed after system devices and system compositor has been destroyed.
Definition: xrt_system.h:84
Header holding common defines.
Common defines and enums for XRT.
xrt_device_name
A enum that is used to name devices so that the state trackers can reason about the devices easier.
Definition: xrt_defines.h:712