The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

aux/android: Add lifecycle callback container.

Co-Authored-By: Jarvis Huang <quic_jarvhuan@quicinc.com>
Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/1655>

+197 -1
+3 -1
src/xrt/auxiliary/android/CMakeLists.txt
··· 17 17 android_custom_surface.h 18 18 android_globals.cpp 19 19 android_globals.h 20 + android_lifecycle_callbacks.cpp 21 + android_lifecycle_callbacks.h 20 22 android_load_class.cpp 21 23 android_load_class.hpp 22 24 android_looper.cpp ··· 27 29 ) 28 30 target_link_libraries( 29 31 aux_android 30 - PUBLIC aux_util 32 + PUBLIC aux_util xrt-interfaces 31 33 PRIVATE 32 34 ${ANDROID_LIBRARY} 33 35 ${ANDROID_LOG_LIBRARY}
+99
src/xrt/auxiliary/android/android_lifecycle_callbacks.cpp
··· 1 + // Copyright 2021-2024, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Implementation of a callback collection for Android lifecycle events. 6 + * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 + * @ingroup aux_android 8 + */ 9 + 10 + #include "android_lifecycle_callbacks.h" 11 + 12 + #include "xrt/xrt_config_android.h" 13 + #include "xrt/xrt_android.h" 14 + #include "util/u_logging.h" 15 + #include "util/u_generic_callbacks.hpp" 16 + 17 + #include <memory> 18 + 19 + using xrt::auxiliary::util::GenericCallbacks; 20 + 21 + struct android_lifecycle_callbacks 22 + { 23 + explicit android_lifecycle_callbacks(xrt_instance_android *xinst_android) : instance_android(xinst_android) {} 24 + xrt_instance_android *instance_android; 25 + GenericCallbacks<xrt_android_lifecycle_event_handler_t, enum xrt_android_lifecycle_event> callback_collection; 26 + }; 27 + 28 + #define CATCH_CLAUSES(ACTION, RET) \ 29 + catch (std::exception const &e) \ 30 + { \ 31 + U_LOG_E("Exception while " ACTION "! %s", e.what()); \ 32 + return RET; \ 33 + } \ 34 + catch (...) \ 35 + { \ 36 + U_LOG_E("Unknown exception while " ACTION "!"); \ 37 + return RET; \ 38 + } 39 + 40 + int 41 + android_lifecycle_callbacks_register_callback(struct android_lifecycle_callbacks *alc, 42 + xrt_android_lifecycle_event_handler_t callback, 43 + enum xrt_android_lifecycle_event event_mask, 44 + void *userdata) 45 + { 46 + try { 47 + alc->callback_collection.addCallback(callback, event_mask, userdata); 48 + return 0; 49 + } 50 + CATCH_CLAUSES("adding callback to collection", -1) 51 + } 52 + 53 + int 54 + android_lifecycle_callbacks_remove_callback(struct android_lifecycle_callbacks *alc, 55 + xrt_android_lifecycle_event_handler_t callback, 56 + enum xrt_android_lifecycle_event event_mask, 57 + void *userdata) 58 + { 59 + try { 60 + return alc->callback_collection.removeCallback(callback, event_mask, userdata); 61 + } 62 + CATCH_CLAUSES("removing callback", -1) 63 + } 64 + 65 + int 66 + android_lifecycle_callbacks_invoke(struct android_lifecycle_callbacks *alc, enum xrt_android_lifecycle_event event) 67 + { 68 + try { 69 + return alc->callback_collection.invokeCallbacks( 70 + event, [=](enum xrt_android_lifecycle_event event, xrt_android_lifecycle_event_handler_t callback, 71 + void *userdata) { return callback(alc->instance_android, event, userdata); }); 72 + } 73 + CATCH_CLAUSES("invoking callbacks", -1) 74 + } 75 + 76 + struct android_lifecycle_callbacks * 77 + android_lifecycle_callbacks_create(struct xrt_instance_android *xinst_android) 78 + { 79 + try { 80 + auto ret = std::make_unique<android_lifecycle_callbacks>(xinst_android); 81 + 82 + return ret.release(); 83 + } 84 + CATCH_CLAUSES("creating callbacks structure", nullptr) 85 + } 86 + 87 + void 88 + android_lifecycle_callbacks_destroy(struct android_lifecycle_callbacks **ptr_callbacks) 89 + { 90 + if (ptr_callbacks == nullptr) { 91 + return; 92 + } 93 + struct android_lifecycle_callbacks *alc = *ptr_callbacks; 94 + if (alc == nullptr) { 95 + return; 96 + } 97 + delete alc; 98 + *ptr_callbacks = nullptr; 99 + }
+95
src/xrt/auxiliary/android/android_lifecycle_callbacks.h
··· 1 + // Copyright 2021-2024, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief An implementation of a callback collection for the Android lifecycle. 6 + * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 + * @ingroup aux_android 8 + */ 9 + 10 + #pragma once 11 + 12 + #include <xrt/xrt_config_os.h> 13 + #include <xrt/xrt_android.h> 14 + 15 + #ifdef __cplusplus 16 + extern "C" { 17 + #endif 18 + 19 + struct xrt_instance_android; 20 + 21 + /*! 22 + * @class android_lifecycle_callbacks 23 + * @brief An object handling a collection of callbacks for the Android lifecycle. 24 + */ 25 + struct android_lifecycle_callbacks; 26 + 27 + /*! 28 + * Create an @ref android_lifecycle_callbacks object. 29 + * 30 + * @param xinst_android The instance that will be passed to all callbacks. 31 + * 32 + * @public @memberof android_lifecycle_callbacks 33 + */ 34 + struct android_lifecycle_callbacks * 35 + android_lifecycle_callbacks_create(struct xrt_instance_android *xinst_android); 36 + 37 + /*! 38 + * Destroy an @ref android_lifecycle_callbacks object. 39 + * @public @memberof android_lifecycle_callbacks 40 + */ 41 + void 42 + android_lifecycle_callbacks_destroy(struct android_lifecycle_callbacks **ptr_callbacks); 43 + 44 + /*! 45 + * Register a lifecycle event callback. 46 + * 47 + * @param alc Pointer to self 48 + * @param callback Function pointer for callback 49 + * @param event_mask bitwise-OR of one or more values from @ref xrt_android_lifecycle_event 50 + * @param userdata An opaque pointer for use by the callback. Whatever you pass here will be passed to the 51 + * callback when invoked. 52 + * 53 + * @return 0 on success, <0 on error. 54 + * @public @memberof android_lifecycle_callbacks 55 + */ 56 + int 57 + android_lifecycle_callbacks_register_callback(struct android_lifecycle_callbacks *alc, 58 + xrt_android_lifecycle_event_handler_t callback, 59 + enum xrt_android_lifecycle_event event_mask, 60 + void *userdata); 61 + 62 + /*! 63 + * Remove a lifecycle event callback that matches the supplied parameters. 64 + * 65 + * @param alc Pointer to self 66 + * @param callback Function pointer for callback 67 + * @param event_mask bitwise-OR of one or more values from @ref xrt_android_lifecycle_event 68 + * @param userdata An opaque pointer for use by the callback, must match the one originally supplied 69 + * 70 + * @return number of callbacks removed (typically 1) on success, <0 on error. 71 + * @public @memberof android_lifecycle_callbacks 72 + */ 73 + int 74 + android_lifecycle_callbacks_remove_callback(struct android_lifecycle_callbacks *alc, 75 + xrt_android_lifecycle_event_handler_t callback, 76 + enum xrt_android_lifecycle_event event_mask, 77 + void *userdata); 78 + 79 + 80 + /*! 81 + * Invoke all lifecycle event callbacks that match a given event. 82 + * 83 + * @param alc Pointer to self 84 + * @param event The event from @ref xrt_android_lifecycle_event 85 + * 86 + * @return the number of invoked callbacks on success, <0 on error. 87 + * @public @memberof android_lifecycle_callbacks 88 + */ 89 + int 90 + android_lifecycle_callbacks_invoke(struct android_lifecycle_callbacks *alc, enum xrt_android_lifecycle_event event); 91 + 92 + 93 + #ifdef __cplusplus 94 + } // extern "C" 95 + #endif