Monado OpenXR Runtime
u_system.h
Go to the documentation of this file.
1// Copyright 2023, Collabora, Ltd.
2// Copyright 2024-2025, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Helper to implement @ref xrt_system.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup aux_util
9 */
10
11#pragma once
12
13#include "xrt/xrt_system.h"
14#include "xrt/xrt_session.h"
15#include "os/os_threading.h"
16
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22struct xrt_session;
25
26/*!
27 * A pair of @ref xrt_session and @ref xrt_session_event_sink that has been
28 * registered to this system, used to multiplex events to all sessions.
29 *
30 * @ingroup aux_util
31 */
33{
34 struct xrt_session *xs;
35 struct xrt_session_event_sink *xses;
36};
37
38/*!
39 * A helper to implement a @ref xrt_system, takes care of multiplexing events
40 * to sessions.
41 *
42 * @ingroup aux_util
43 * @implements xrt_system
44 */
46{
47 struct xrt_system base;
48
49 //! Pushes events to all sessions created from this system.
51
52 struct
53 {
54 struct os_mutex mutex;
55
56 //! Number of session and event sink pairs.
57 uint32_t count;
58 //! Capacity of the session array.
59 uint32_t capacity;
60 //! Array of session and event sink pairs.
62 } sessions;
63
64 /*!
65 * Used to implement @ref xrt_system::create_session, can be NULL. This
66 * field should be set with @ref u_system_set_system_compositor.
67 */
69};
70
71/*!
72 * Create a @ref u_system, creates a fully working system. Objects wishing to
73 * use @ref u_system as a parent class should use @ref u_system_init.
74 *
75 * @public @memberof u_system
76 * @ingroup aux_util
77 * @see u_system_init
78 */
79struct u_system *
80u_system_create(void);
81
82/*!
83 * Inits a @ref u_system struct when used as a parent class, only to be used
84 * by base class. Not needed to be called if created by @ref u_system_create.
85 *
86 * @protected @memberof u_system
87 * @ingroup aux_util
88 */
89bool
90u_system_init(struct u_system *usys, void (*destroy_fn)(struct xrt_system *));
91
92/*!
93 * Finalizes a @ref u_system struct when used as a parent class, only to be used
94 * by base class. This will not free the @ref u_system pointer itself but will
95 * free any resources created by the default implementation functions. Not
96 * needed to be called if created by @ref u_system_create, instead use
97 * xrt_system::destroy.
98 *
99 * @protected @memberof u_system
100 * @ingroup aux_util
101 */
102void
103u_system_fini(struct u_system *usys);
104
105/*!
106 * Add a @ref xrt_session to be tracked and to receive multiplexed events.
107 *
108 * @public @memberof u_system
109 * @ingroup aux_util
110 */
111void
112u_system_add_session(struct u_system *usys, struct xrt_session *xs, struct xrt_session_event_sink *xses);
113
114/*!
115 * Remove a @ref xrt_session from tracking, it will no longer receive events,
116 * the given @p xses needs to match when it was added.
117 *
118 * @public @memberof u_system
119 * @ingroup aux_util
120 */
121void
122u_system_remove_session(struct u_system *usys, struct xrt_session *xs, struct xrt_session_event_sink *xses);
123
124/*!
125 * Broadcast event to all sessions under this system.
126 *
127 * @public @memberof u_system
128 * @ingroup aux_util
129 */
130void
131u_system_broadcast_event(struct u_system *usys, const union xrt_session_event *xse);
132
133/*!
134 * Set the system compositor, used in the @ref xrt_system_create_session call.
135 *
136 * @public @memberof u_system
137 * @ingroup aux_util
138 */
139void
141
142/*!
143 * Fill system properties.
144 *
145 * @public @memberof u_system
146 * @ingroup aux_util
147 */
148void
149u_system_fill_properties(struct u_system *usys, const char *name);
150
151/*!
152 * Destroy an @ref u_system_create allocated @ref u_system - helper function.
153 *
154 * @param[in,out] usys_ptr A pointer to the @ref u_system_create allocated
155 * struct pointer.
156 *
157 * Will destroy the system devices if @p *usys_ptr is not NULL. Will then set
158 * @p *usys_ptr to NULL.
159 *
160 * @public @memberof u_system
161 */
162static inline void
163u_system_destroy(struct u_system **usys_ptr)
164{
165 struct u_system *usys = *usys_ptr;
166 if (usys == NULL) {
167 return;
168 }
169
170 *usys_ptr = NULL;
171 usys->base.destroy(&usys->base);
172}
173
174
175#ifdef __cplusplus
176}
177#endif
bool u_system_init(struct u_system *usys, void(*destroy_fn)(struct xrt_system *))
Inits a u_system struct when used as a parent class, only to be used by base class.
Definition: u_system.c:123
void u_system_broadcast_event(struct u_system *usys, const union xrt_session_event *xse)
Broadcast event to all sessions under this system.
Definition: u_system.c:236
void u_system_add_session(struct u_system *usys, struct xrt_session *xs, struct xrt_session_event_sink *xses)
Add a xrt_session to be tracked and to receive multiplexed events.
Definition: u_system.c:162
void u_system_remove_session(struct u_system *usys, struct xrt_session *xs, struct xrt_session_event_sink *xses)
Remove a xrt_session from tracking, it will no longer receive events, the given xses needs to match w...
Definition: u_system.c:189
void u_system_set_system_compositor(struct u_system *usys, struct xrt_system_compositor *xsysc)
Set the system compositor, used in the xrt_system_create_session call.
Definition: u_system.c:253
struct u_system * u_system_create(void)
Create a u_system, creates a fully working system.
Definition: u_system.c:109
void u_system_fill_properties(struct u_system *usys, const char *name)
Fill system properties.
Definition: u_system.c:261
void u_system_fini(struct u_system *usys)
Finalizes a u_system struct when used as a parent class, only to be used by base class.
Definition: u_system.c:147
Wrapper around OS threading native functions.
A wrapper around a native mutex.
Definition: os_threading.h:55
A pair of xrt_session and xrt_session_event_sink that has been registered to this system,...
Definition: u_system.h:33
A helper to implement a xrt_system, takes care of multiplexing events to sessions.
Definition: u_system.h:46
uint32_t count
Number of session and event sink pairs.
Definition: u_system.h:57
static void u_system_destroy(struct u_system **usys_ptr)
Destroy an u_system_create allocated u_system - helper function.
Definition: u_system.h:163
uint32_t capacity
Capacity of the session array.
Definition: u_system.h:59
struct xrt_session_event_sink broadcast
Pushes events to all sessions created from this system.
Definition: u_system.h:50
struct xrt_system_compositor * xsysc
Used to implement xrt_system::create_session, can be NULL.
Definition: u_system.h:68
struct u_system_session_pair * pairs
Array of session and event sink pairs.
Definition: u_system.h:61
Used internally from producers of events to push events into session, some sinks might multiplex even...
Definition: xrt_session.h:206
The XRT representation of XrSession, this object does not have all of the functionality of a session,...
Definition: xrt_session.h:246
The system compositor handles composition for a system.
Definition: xrt_compositor.h:2423
A system is a collection of devices, policies and optionally a compositor that is organised into a ch...
Definition: xrt_system.h:63
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:86
Union of all session events, used to return multiple events through one call.
Definition: xrt_session.h:186
Header for session object.
Header for system objects.