Monado OpenXR Runtime
u_system_helpers.h
Go to the documentation of this file.
1 // Copyright 2022-2023, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Helpers for system objects like @ref xrt_system_devices.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup aux_util
8  */
9 
10 #pragma once
11 
12 #include "xrt/xrt_results.h"
13 #include "xrt/xrt_frame.h"
14 #include "xrt/xrt_system.h"
15 #include "xrt/xrt_instance.h"
16 #include "xrt/xrt_tracking.h"
17 
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
24 
25 
26 /*!
27  * Helper struct to manage devices by implementing the @ref xrt_system_devices.
28  *
29  * The default destroy function that is set by @ref u_system_devices_allocate
30  * will first destroy all of the @ref xrt_device and then destroy all nodes
31  * in the @ref xrt_frame_context.
32  *
33  * @ingroup aux_util
34  */
36 {
37  struct xrt_system_devices base;
38 
39  //! Optional frame context for visual tracking.
40  struct xrt_frame_context xfctx;
41 
42  //! Optional shared tracking origin.
44 };
45 
46 /*!
47  * Small inline helper to cast from @ref xrt_system_devices.
48  *
49  * @ingroup aux_util
50  */
51 static inline struct u_system_devices *
53 {
54  return (struct u_system_devices *)xsysd;
55 }
56 
57 /*!
58  * Allocates a empty @ref u_system_devices to be filled in by the caller, only
59  * the destroy function is filled in.
60  *
61  * @ingroup aux_util
62  */
63 struct u_system_devices *
65 
66 /*!
67  * Destroys all devices and clears out the frame context, doesn't free the
68  * struct itself, useful for code embedding the system devices struct into
69  * other objects where it's not the first member or C++ classes.
70  *
71  * @ingroup aux_util
72  */
73 void
75 
76 /*!
77  * Destroy an u_system_devices_allocate and owned devices - helper function.
78  *
79  * @param[in,out] usysd_ptr A pointer to the u_system_devices_allocate struct pointer.
80  *
81  * Will destroy the system devices if *usysd_ptr is not NULL. Will then set *usysd_ptr to NULL.
82  *
83  * @public @memberof u_system_devices_allocate
84  */
85 static inline void
86 u_system_devices_destroy(struct u_system_devices **usysd_ptr)
87 {
88  struct u_system_devices *usysd = *usysd_ptr;
89  if (usysd == NULL) {
90  return;
91  }
92 
93  *usysd_ptr = NULL;
94  usysd->base.destroy(&usysd->base);
95 }
96 
97 
98 /*
99  *
100  * Static helper.
101  *
102  */
103 
104 /*!
105  * Helper struct to manage devices by implementing the @ref xrt_system_devices,
106  * this has only static device roles.
107  *
108  * @ingroup aux_util
109  */
111 {
112  struct u_system_devices base;
113 
114  //! Is automatically returned.
115  struct xrt_system_roles cached;
116 };
117 
118 /*!
119  * Small inline helper to cast from @ref xrt_system_devices.
120  *
121  * @ingroup aux_util
122  */
123 static inline struct u_system_devices_static *
125 {
126  return (struct u_system_devices_static *)xsysd;
127 }
128 
129 /*!
130  * Allocates a empty @ref u_system_devices to be filled in by the caller, only
131  * the destroy function is filled in.
132  *
133  * @ingroup aux_util
134  */
137 
138 /*!
139  * Finalizes the static struct with the given input devices, the system devices
140  * will always return these devices for the left and right role. This function
141  * must be called before @ref xrt_system_devices_get_roles is called.
142  *
143  * @ingroup aux_util
144  */
145 void
147  struct xrt_device *left,
148  struct xrt_device *right);
149 
150 
151 /*
152  *
153  * Generic system devices helper.
154  *
155  */
156 
157 /*!
158  * Takes a @ref xrt_instance, gets the prober from it and then uses the prober
159  * to allocate a filled in @ref u_system_devices.
160  *
161  * @ingroup aux_util
162  */
165  struct xrt_session_event_sink *broadcast,
166  struct xrt_system_devices **out_xsysd,
167  struct xrt_space_overseer **out_xso);
168 
169 /*!
170  * Helper function.
171  *
172  * Looks through @ref xrt_system_devices's devices and returns the first device
173  * that supports hand tracking and the supplied input name.
174  *
175  * Used by target_builder_lighthouse to find Knuckles controllers in the list of
176  * devices returned, the legacy builder to find hand tracking devices, etc.
177  *
178  * @ingroup aux_util
179  */
180 struct xrt_device *
182 
183 /*!
184  * Helper to get the first left hand-tracking device,
185  * uses @ref u_system_devices_get_ht_device.
186  *
187  * @ingroup aux_util
188  */
189 static inline struct xrt_device *
191 {
192  return u_system_devices_get_ht_device(xsysd, XRT_INPUT_GENERIC_HAND_TRACKING_LEFT);
193 }
194 
195 /*!
196  * Helper to get the first right hand-tracking device,
197  * uses @ref u_system_devices_get_ht_device.
198  *
199  * @ingroup aux_util
200  */
201 static inline struct xrt_device *
203 {
204  return u_system_devices_get_ht_device(xsysd, XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT);
205 }
206 
207 
208 #ifdef __cplusplus
209 }
210 #endif
void u_system_devices_static_finalize(struct u_system_devices_static *usysds, struct xrt_device *left, struct xrt_device *right)
Finalizes the static struct with the given input devices, the system devices will always return these...
Definition: u_system_helpers.c:116
struct u_system_devices_static * u_system_devices_static_allocate(void)
Allocates a empty u_system_devices to be filled in by the caller, only the destroy function is filled...
Definition: u_system_helpers.c:106
struct xrt_device * u_system_devices_get_ht_device(struct xrt_system_devices *xsysd, enum xrt_input_name name)
Helper function.
Definition: u_system_helpers.c:183
void u_system_devices_close(struct xrt_system_devices *xsysd)
Destroys all devices and clears out the frame context, doesn't free the struct itself,...
Definition: u_system_helpers.c:92
static struct xrt_device * u_system_devices_get_ht_device_left(struct xrt_system_devices *xsysd)
Helper to get the first left hand-tracking device, uses u_system_devices_get_ht_device.
Definition: u_system_helpers.h:190
xrt_result_t u_system_devices_create_from_prober(struct xrt_instance *xinst, struct xrt_session_event_sink *broadcast, struct xrt_system_devices **out_xsysd, struct xrt_space_overseer **out_xso)
Takes a xrt_instance, gets the prober from it and then uses the prober to allocate a filled in u_syst...
Definition: u_system_helpers.c:153
static struct xrt_device * u_system_devices_get_ht_device_right(struct xrt_system_devices *xsysd)
Helper to get the first right hand-tracking device, uses u_system_devices_get_ht_device.
Definition: u_system_helpers.h:202
static struct u_system_devices_static * u_system_devices_static(struct xrt_system_devices *xsysd)
Small inline helper to cast from xrt_system_devices.
Definition: u_system_helpers.h:124
struct u_system_devices * u_system_devices_allocate(void)
Allocates a empty u_system_devices to be filled in by the caller, only the destroy function is filled...
Definition: u_system_helpers.c:83
static struct u_system_devices * u_system_devices(struct xrt_system_devices *xsysd)
Small inline helper to cast from xrt_system_devices.
Definition: u_system_helpers.h:52
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1134
enum xrt_result xrt_result_t
Result type used across Monado.
Helper struct to manage devices by implementing the xrt_system_devices, this has only static device r...
Definition: u_system_helpers.h:111
struct xrt_system_roles cached
Is automatically returned.
Definition: u_system_helpers.h:115
Helper struct to manage devices by implementing the xrt_system_devices.
Definition: u_system_helpers.h:36
struct xrt_tracking_origin origin
Optional shared tracking origin.
Definition: u_system_helpers.h:43
struct xrt_frame_context xfctx
Optional frame context for visual tracking.
Definition: u_system_helpers.h:40
A single HMD or input device.
Definition: xrt_device.h:230
enum xrt_device_name name
Enum identifier of the device.
Definition: xrt_device.h:232
Object used to track all sinks and frame producers in a graph.
Definition: xrt_frame.h:108
This interface acts as a root object for Monado.
Definition: xrt_instance.h:67
Used internally from producers of events to push events into session, some sinks might mutliplex even...
Definition: xrt_session.h:193
Object that oversees and manages spaces, one created for each XR system.
Definition: xrt_space.h:95
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition: xrt_system.h:218
void(* destroy)(struct xrt_system_devices *xsysd)
Destroy all the devices that are owned by this system devices.
Definition: xrt_system.h:305
Data associating a device index (in xrt_system_devices::xdevs) with a given "role" for dynamic role s...
Definition: xrt_system.h:160
A tracking system or device origin.
Definition: xrt_tracking.h:71
Data frame header.
Header for xrt_instance object.
Internal result type for XRT.
Header for system objects.
Header defining the tracking system integration in Monado.