Monado OpenXR Runtime
xrt_android.h
Go to the documentation of this file.
1// Copyright 2021-2024, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Header holding Android-specific details.
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 * @ingroup xrt_iface
8 */
9
10#pragma once
11
12#include "xrt/xrt_config_os.h"
13#include "xrt/xrt_compiler.h"
14#include "xrt/xrt_results.h"
15
16#include <stdbool.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22struct _JavaVM;
24
25
26/*!
27 * @interface xrt_instance_android
28 *
29 * This is the interface to the Android-specific "aspect" of @ref xrt_instance.
30 *
31 * It is expected that your implementation of this interface will be nested in your
32 * implementation of @ref xrt_instance. It does not have a separate create or
33 * destroy function as it is an (optional) aspect of the instance.
34 */
36
37/*!
38 * Distinguishes the possible Android lifecycle events from each other.
39 *
40 * Used as a bitmask when registering for callbacks.
41 */
43{
44 XRT_ANDROID_LIVECYCLE_EVENT_ON_CREATE = 1 << 0,
45 XRT_ANDROID_LIVECYCLE_EVENT_ON_DESTROY = 1 << 1,
46 XRT_ANDROID_LIVECYCLE_EVENT_ON_PAUSE = 1 << 2,
47 XRT_ANDROID_LIVECYCLE_EVENT_ON_RESUME = 1 << 3,
48 XRT_ANDROID_LIVECYCLE_EVENT_ON_START = 1 << 4,
49 XRT_ANDROID_LIVECYCLE_EVENT_ON_STOP = 1 << 5,
50};
51
52/*!
53 * A callback type for a handler of Android lifecycle events.
54 *
55 * Return true to be removed from the callback list.
56 */
59 void *userdata);
60
61#ifdef XRT_OS_ANDROID
62
64{
65
66 /*!
67 * @name Interface Methods
68 *
69 * All Android-based implementations of the xrt_instance interface must additionally populate all these function
70 * pointers with their implementation methods. To use this interface, see the helper functions.
71 * @{
72 */
73 /*!
74 * Retrieve the stored Java VM instance pointer.
75 *
76 * @note Code consuming this interface should use xrt_instance_android_get_vm()
77 *
78 * @param xinst_android Pointer to self
79 *
80 * @return The VM pointer.
81 */
82 struct _JavaVM *(*get_vm)(const struct xrt_instance_android *xinst_android);
83
84 /*!
85 * Retrieve the stored activity android.content.Context jobject.
86 *
87 * For usage, cast the return value to jobject - a typedef whose definition
88 * differs between C (a void *) and C++ (a pointer to an empty class)
89 *
90 * @note Code consuming this interface should use xrt_instance_android_get_context()
91 *
92 * @param xinst_android Pointer to self
93 *
94 * @return The activity context.
95 */
96 void *(*get_context)(const struct xrt_instance_android *xinst_android);
97
98 /*!
99 * Register a activity lifecycle event callback.
100 *
101 * @note Code consuming this interface should use xrt_instance_android_register_activity_lifecycle_callback()
102 *
103 * @param xinst_android Pointer to self
104 * @param callback Function pointer for callback
105 * @param event_mask bitwise-OR of one or more values from @ref xrt_android_lifecycle_event
106 * @param userdata An opaque pointer for use by the callback. Whatever you pass here will be passed to the
107 * callback when invoked.
108 *
109 * @return XRT_SUCCESS on success, other error code on error.
110 */
111 xrt_result_t (*register_activity_lifecycle_callback)(struct xrt_instance_android *xinst_android,
113 enum xrt_android_lifecycle_event event_mask,
114 void *userdata);
115
116 /*!
117 * Remove a activity lifecycle event callback that matches the supplied parameters.
118 *
119 * @note Code consuming this interface should use xrt_instance_android_remove_activity_lifecycle_callback()
120 *
121 * @param xinst_android Pointer to self
122 * @param callback Function pointer for callback
123 * @param event_mask bitwise-OR of one or more values from @ref xrt_android_lifecycle_event
124 * @param userdata An opaque pointer for use by the callback. Whatever you pass here will be passed to the
125 * callback when invoked.
126 *
127 * @return XRT_SUCCESS on success (at least one callback was removed), @ref XRT_ERROR_ANDROID on error.
128 */
129 xrt_result_t (*remove_activity_lifecycle_callback)(struct xrt_instance_android *xinst_android,
131 enum xrt_android_lifecycle_event event_mask,
132 void *userdata);
133
134 /*!
135 * @}
136 */
137};
138
139/*!
140 * @copydoc xrt_instance_android::get_vm
141 *
142 * Helper for calling through the function pointer.
143 *
144 * @public @memberof xrt_instance_android
145 */
146static inline struct _JavaVM *
147xrt_instance_android_get_vm(struct xrt_instance_android *xinst_android)
148{
149 return xinst_android->get_vm(xinst_android);
150}
151
152/*!
153 * @copydoc xrt_instance_android::get_context
154 *
155 * Helper for calling through the function pointer.
156 *
157 * @public @memberof xrt_instance_android
158 */
159static inline void *
160xrt_instance_android_get_context(struct xrt_instance_android *xinst_android)
161{
162 return xinst_android->get_context(xinst_android);
163}
164
165/*!
166 * @copydoc xrt_instance_android::register_activity_lifecycle_callback
167 *
168 * Helper for calling through the function pointer.
169 *
170 * @public @memberof xrt_instance_android
171 */
172static inline xrt_result_t
173xrt_instance_android_register_activity_lifecycle_callback(struct xrt_instance_android *xinst_android,
175 enum xrt_android_lifecycle_event event_mask,
176 void *userdata)
177{
178 return xinst_android->register_activity_lifecycle_callback(xinst_android, callback, event_mask, userdata);
179}
180
181/*!
182 * @copydoc xrt_instance_android::remove_activity_lifecycle_callback
183 *
184 * Helper for calling through the function pointer.
185 *
186 * @public @memberof xrt_instance_android
187 */
188static inline xrt_result_t
189xrt_instance_android_remove_activity_lifecycle_callback(struct xrt_instance_android *xinst_android,
191 enum xrt_android_lifecycle_event event_mask,
192 void *userdata)
193{
194 return xinst_android->remove_activity_lifecycle_callback(xinst_android, callback, event_mask, userdata);
195}
196
197#endif // XRT_OS_ANDROID
198
199#ifdef __cplusplus
200}
201#endif
enum xrt_result xrt_result_t
Result type used across Monado.
This is the interface to the Android-specific "aspect" of xrt_instance.
Information provided by the application at instance create time.
Definition: xrt_instance.h:94
bool(* xrt_android_lifecycle_event_handler_t)(struct xrt_instance_android *xinst_android, enum xrt_android_lifecycle_event event, void *userdata)
A callback type for a handler of Android lifecycle events.
Definition: xrt_android.h:57
xrt_android_lifecycle_event
Distinguishes the possible Android lifecycle events from each other.
Definition: xrt_android.h:43
Header holding common defines.
Auto detect OS and certain features.
Internal result type for XRT.