From 430f79a7cafb94048a736924701e4cdbb62ca550 Mon Sep 17 00:00:00 2001
From: Ryan Pavlik <ryan.pavlik@collabora.com>
Date: Tue, 18 Aug 2020 15:46:26 -0500
Subject: [PATCH] aux/util: Add u_android for global state.

---
 src/xrt/auxiliary/CMakeLists.txt   |  6 +++
 src/xrt/auxiliary/util/u_android.c | 60 ++++++++++++++++++++++++++
 src/xrt/auxiliary/util/u_android.h | 68 ++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 src/xrt/auxiliary/util/u_android.c
 create mode 100644 src/xrt/auxiliary/util/u_android.h

diff --git a/src/xrt/auxiliary/CMakeLists.txt b/src/xrt/auxiliary/CMakeLists.txt
index 7c4f7d108..4f559c898 100644
--- a/src/xrt/auxiliary/CMakeLists.txt
+++ b/src/xrt/auxiliary/CMakeLists.txt
@@ -116,6 +116,12 @@ set(UTIL_SOURCE_FILES
 	util/u_var.cpp
 	util/u_var.h
 	)
+if(ANDROID)
+	list(APPEND UTIL_SOURCE_FILES
+		util/u_android.c
+		util/u_android.h
+		)
+endif()
 
 set(VK_SOURCE_FILES
 	vk/vk_documentation.h
diff --git a/src/xrt/auxiliary/util/u_android.c b/src/xrt/auxiliary/util/u_android.c
new file mode 100644
index 000000000..756c373df
--- /dev/null
+++ b/src/xrt/auxiliary/util/u_android.c
@@ -0,0 +1,60 @@
+// Copyright 2020, Collabora, Ltd.
+// SPDX-License-Identifier: BSL-1.0
+/*!
+ * @file
+ * @brief  Functions for Android-specific global state.
+ * @author Ryan Pavlik <ryan.pavlik@collabora.com>
+ * @ingroup aux_util
+ */
+
+#include "u_android.h"
+
+#include <stddef.h>
+
+/*!
+ * @todo Do we need locking here? Do we need to create global refs for the
+ * supplied jobjects?
+ */
+static struct
+{
+	struct _JavaVM *vm;
+	void *activity;
+	void *context;
+} android_globals = {NULL, NULL, NULL};
+
+void
+u_android_store_vm_and_activity(struct _JavaVM *vm, void *activity)
+{
+	android_globals.vm = vm;
+	android_globals.activity = activity;
+}
+
+void
+u_android_store_vm_and_context(struct _JavaVM *vm, void *context)
+{
+
+	android_globals.vm = vm;
+	android_globals.context = context;
+}
+
+struct _JavaVM *
+u_android_get_vm()
+{
+	return android_globals.vm;
+}
+
+void *
+u_android_get_activity()
+{
+	return android_globals.activity;
+}
+
+void *
+u_android_get_context()
+{
+	void *ret = android_globals.context;
+	if (ret == NULL) {
+		ret = android_globals.activity;
+	}
+	return ret;
+}
diff --git a/src/xrt/auxiliary/util/u_android.h b/src/xrt/auxiliary/util/u_android.h
new file mode 100644
index 000000000..f57cac8c0
--- /dev/null
+++ b/src/xrt/auxiliary/util/u_android.h
@@ -0,0 +1,68 @@
+// Copyright 2020, Collabora, Ltd.
+// SPDX-License-Identifier: BSL-1.0
+/*!
+ * @file
+ * @brief  Functions for Android-specific global state.
+ * @author Ryan Pavlik <ryan.pavlik@collabora.com>
+ * @ingroup aux_util
+ */
+
+#pragma once
+
+#include <xrt/xrt_config_os.h>
+
+#ifdef XRT_OS_ANDROID
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct _JNIEnv;
+struct _JavaVM;
+
+
+/*!
+ * Store the Java VM pointer and the android.app.Activity jobject.
+ */
+void
+u_android_store_vm_and_activity(struct _JavaVM *vm, void *activity);
+
+
+/*!
+ * Store the Java VM pointer and the android.content.Context jobject.
+ */
+void
+u_android_store_vm_and_context(struct _JavaVM *vm, void *context);
+
+/*!
+ * Retrieve the Java VM pointer previously stored, if any.
+ */
+struct _JavaVM *
+u_android_get_vm();
+
+/*!
+ * Retrieve the android.app.Activity jobject previously stored, if any.
+ *
+ * For usage, cast the return value to jobject - a typedef whose definition
+ * differs between C (a void *) and C++ (a pointer to an empty class)
+ */
+void *
+u_android_get_activity();
+
+/*!
+ * Retrieve the android.content.Context jobject previously stored, if any.
+ *
+ * Since android.app.Activity is a sub-class of android.content.Context, the
+ * activity jobject will be returned if it has been set but the context has not.
+ *
+ * For usage, cast the return value to jobject - a typedef whose definition
+ * differs between C (a void *) and C++ (a pointer to an empty class)
+ */
+void *
+u_android_get_context();
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // XRT_OS_ANDROID