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>
This commit is contained in:
Rylie Pavlik 2021-03-26 18:48:01 -05:00
parent 31ee231f20
commit 8cf94c20a9
3 changed files with 197 additions and 1 deletions

View file

@ -17,6 +17,8 @@ add_library(
android_custom_surface.h
android_globals.cpp
android_globals.h
android_lifecycle_callbacks.cpp
android_lifecycle_callbacks.h
android_load_class.cpp
android_load_class.hpp
android_looper.cpp
@ -27,7 +29,7 @@ add_library(
)
target_link_libraries(
aux_android
PUBLIC aux_util
PUBLIC aux_util xrt-interfaces
PRIVATE
${ANDROID_LIBRARY}
${ANDROID_LOG_LIBRARY}

View file

@ -0,0 +1,99 @@
// Copyright 2021-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Implementation of a callback collection for Android lifecycle events.
* @author Rylie Pavlik <rylie.pavlik@collabora.com>
* @ingroup aux_android
*/
#include "android_lifecycle_callbacks.h"
#include "xrt/xrt_config_android.h"
#include "xrt/xrt_android.h"
#include "util/u_logging.h"
#include "util/u_generic_callbacks.hpp"
#include <memory>
using xrt::auxiliary::util::GenericCallbacks;
struct android_lifecycle_callbacks
{
explicit android_lifecycle_callbacks(xrt_instance_android *xinst_android) : instance_android(xinst_android) {}
xrt_instance_android *instance_android;
GenericCallbacks<xrt_android_lifecycle_event_handler_t, enum xrt_android_lifecycle_event> callback_collection;
};
#define CATCH_CLAUSES(ACTION, RET) \
catch (std::exception const &e) \
{ \
U_LOG_E("Exception while " ACTION "! %s", e.what()); \
return RET; \
} \
catch (...) \
{ \
U_LOG_E("Unknown exception while " ACTION "!"); \
return RET; \
}
int
android_lifecycle_callbacks_register_callback(struct android_lifecycle_callbacks *alc,
xrt_android_lifecycle_event_handler_t callback,
enum xrt_android_lifecycle_event event_mask,
void *userdata)
{
try {
alc->callback_collection.addCallback(callback, event_mask, userdata);
return 0;
}
CATCH_CLAUSES("adding callback to collection", -1)
}
int
android_lifecycle_callbacks_remove_callback(struct android_lifecycle_callbacks *alc,
xrt_android_lifecycle_event_handler_t callback,
enum xrt_android_lifecycle_event event_mask,
void *userdata)
{
try {
return alc->callback_collection.removeCallback(callback, event_mask, userdata);
}
CATCH_CLAUSES("removing callback", -1)
}
int
android_lifecycle_callbacks_invoke(struct android_lifecycle_callbacks *alc, enum xrt_android_lifecycle_event event)
{
try {
return alc->callback_collection.invokeCallbacks(
event, [=](enum xrt_android_lifecycle_event event, xrt_android_lifecycle_event_handler_t callback,
void *userdata) { return callback(alc->instance_android, event, userdata); });
}
CATCH_CLAUSES("invoking callbacks", -1)
}
struct android_lifecycle_callbacks *
android_lifecycle_callbacks_create(struct xrt_instance_android *xinst_android)
{
try {
auto ret = std::make_unique<android_lifecycle_callbacks>(xinst_android);
return ret.release();
}
CATCH_CLAUSES("creating callbacks structure", nullptr)
}
void
android_lifecycle_callbacks_destroy(struct android_lifecycle_callbacks **ptr_callbacks)
{
if (ptr_callbacks == nullptr) {
return;
}
struct android_lifecycle_callbacks *alc = *ptr_callbacks;
if (alc == nullptr) {
return;
}
delete alc;
*ptr_callbacks = nullptr;
}

View file

@ -0,0 +1,95 @@
// Copyright 2021-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief An implementation of a callback collection for the Android lifecycle.
* @author Rylie Pavlik <rylie.pavlik@collabora.com>
* @ingroup aux_android
*/
#pragma once
#include <xrt/xrt_config_os.h>
#include <xrt/xrt_android.h>
#ifdef __cplusplus
extern "C" {
#endif
struct xrt_instance_android;
/*!
* @class android_lifecycle_callbacks
* @brief An object handling a collection of callbacks for the Android lifecycle.
*/
struct android_lifecycle_callbacks;
/*!
* Create an @ref android_lifecycle_callbacks object.
*
* @param xinst_android The instance that will be passed to all callbacks.
*
* @public @memberof android_lifecycle_callbacks
*/
struct android_lifecycle_callbacks *
android_lifecycle_callbacks_create(struct xrt_instance_android *xinst_android);
/*!
* Destroy an @ref android_lifecycle_callbacks object.
* @public @memberof android_lifecycle_callbacks
*/
void
android_lifecycle_callbacks_destroy(struct android_lifecycle_callbacks **ptr_callbacks);
/*!
* Register a lifecycle event callback.
*
* @param alc Pointer to self
* @param callback Function pointer for callback
* @param event_mask bitwise-OR of one or more values from @ref xrt_android_lifecycle_event
* @param userdata An opaque pointer for use by the callback. Whatever you pass here will be passed to the
* callback when invoked.
*
* @return 0 on success, <0 on error.
* @public @memberof android_lifecycle_callbacks
*/
int
android_lifecycle_callbacks_register_callback(struct android_lifecycle_callbacks *alc,
xrt_android_lifecycle_event_handler_t callback,
enum xrt_android_lifecycle_event event_mask,
void *userdata);
/*!
* Remove a lifecycle event callback that matches the supplied parameters.
*
* @param alc Pointer to self
* @param callback Function pointer for callback
* @param event_mask bitwise-OR of one or more values from @ref xrt_android_lifecycle_event
* @param userdata An opaque pointer for use by the callback, must match the one originally supplied
*
* @return number of callbacks removed (typically 1) on success, <0 on error.
* @public @memberof android_lifecycle_callbacks
*/
int
android_lifecycle_callbacks_remove_callback(struct android_lifecycle_callbacks *alc,
xrt_android_lifecycle_event_handler_t callback,
enum xrt_android_lifecycle_event event_mask,
void *userdata);
/*!
* Invoke all lifecycle event callbacks that match a given event.
*
* @param alc Pointer to self
* @param event The event from @ref xrt_android_lifecycle_event
*
* @return the number of invoked callbacks on success, <0 on error.
* @public @memberof android_lifecycle_callbacks
*/
int
android_lifecycle_callbacks_invoke(struct android_lifecycle_callbacks *alc, enum xrt_android_lifecycle_event event);
#ifdef __cplusplus
} // extern "C"
#endif