From 4d8935625b7252153e620656a233cb2bc7d7c52b Mon Sep 17 00:00:00 2001 From: Andreas Streichardt Date: Sun, 3 Dec 2017 11:33:30 +0100 Subject: [PATCH] Remove separate thread for android --- src/android_init.c | 18 ++++++------------ src/android_platform.h | 9 +++++++-- src/android_window.c | 28 +++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/android_init.c b/src/android_init.c index ba572a4d6..b0cc1b84b 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -28,11 +28,12 @@ #include #include "internal.h" +struct android_app* _globalApp; + extern int main(); void handle_cmd(struct android_app* _app, int32_t cmd) { switch (cmd) { case APP_CMD_INIT_WINDOW: { - _glfw.app = _app; // The window is being shown so the initialization is finished. break; } case APP_CMD_LOST_FOCUS: { @@ -50,16 +51,9 @@ void handle_cmd(struct android_app* _app, int32_t cmd) { // Android Entry Point void android_main(struct android_app *app) { app->onAppCmd = handle_cmd; - pthread_create(&(pthread_t){0}, NULL, (void*)&main, NULL); // Call the main entry point - - while (1) { - struct android_poll_source* source; - // Process events - while ((ALooper_pollAll(0, NULL, NULL,(void**)&source)) >= 0) - if (source != NULL) - source->process(app, source); - } - + // hmmm...global....eek + _globalApp = app; + main(); } ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// @@ -67,8 +61,8 @@ void android_main(struct android_app *app) { int _glfwPlatformInit(void) { + _glfw.gstate.app = _globalApp; _glfwInitTimerPOSIX(); - while (_glfw.app == NULL); // Wait for the app to be initialized or the app will crash occasionally return GLFW_TRUE; } diff --git a/src/android_platform.h b/src/android_platform.h index 1e9da6210..6dcf631f5 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -40,8 +40,8 @@ #define _glfw_dlsym(handle, name) dlsym(handle, name) #define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android->window) -#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android -#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE struct android_app* app +#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android; +#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE android_gstate gstate; #define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE @@ -51,6 +51,11 @@ float x,y; +typedef struct android_gstate { + struct android_app* app; + struct android_poll_source* source; +} android_gstate; + typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; typedef struct VkAndroidSurfaceCreateInfoKHR { diff --git a/src/android_window.c b/src/android_window.c index 708aeda01..0ebcdac76 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -41,6 +41,15 @@ static int32_t handle_input(struct android_app* app, AInputEvent* event) return 0; } +static void handleEvents(int timeout) { + ALooper_pollOnce(0, NULL, NULL,(void**)&_glfw.gstate.source); + + if (_glfw.gstate.source != NULL) { + _glfw.gstate.source->process(_glfw.gstate.app, _glfw.gstate.source); + } + _glfwInputCursorPos(_glfw.windowListHead, x, y); +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -50,10 +59,15 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - window->android = _glfw.app; + // wait for window to become ready + while (_glfw.gstate.app->window == NULL) { + handleEvents(-1); + } + // hmmm maybe should be ANative_Window only? + window->android = _glfw.gstate.app; window->android->onInputEvent = handle_input; - ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); + //ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); if (ctxconfig->client != GLFW_NO_API) { @@ -136,7 +150,10 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) { - + // the underlying buffergeometry is currently being initialized from the + // window width and height...so high resolution displays are currently + // not supported...so it is safe to just call GetWindowSize() for now + _glfwPlatformGetWindowSize(window, width, height); } void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, @@ -212,16 +229,17 @@ int _glfwPlatformWindowVisible(_GLFWwindow* window) void _glfwPlatformPollEvents(void) { - _glfwInputCursorPos(_glfw.windowListHead, x, y); + handleEvents(0); } void _glfwPlatformWaitEvents(void) { - _glfwPlatformPollEvents(); + handleEvents(-1); } void _glfwPlatformWaitEventsTimeout(double timeout) { + handleEvents(timeout * 1e3); } void _glfwPlatformPostEmptyEvent(void)