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