From 806eb393b9f47826f204e82e7c01110d2477606b Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Thu, 21 Apr 2016 10:46:07 +0300 Subject: [PATCH] Android support fixes: #119 --- android/dlangui_source_files.mk | 1 + src/dlangui/graphics/glsupport.d | 6 +- src/dlangui/platforms/android/androidapp.d | 66 ++++++++-------------- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/android/dlangui_source_files.mk b/android/dlangui_source_files.mk index c9c07536..c3010ad0 100644 --- a/android/dlangui_source_files.mk +++ b/android/dlangui_source_files.mk @@ -3,6 +3,7 @@ #echo "DLANGUI DIR: $DLANGUI_DIR" DLANGUI_SOURCES="\ +$DLANGUI_DIR/src/dlangui/platforms/android/androidapp.d \ $DLANGUI_DIR/src/dlangui/platforms/common/startup.d \ $DLANGUI_DIR/src/dlangui/platforms/common/platform.d \ $DLANGUI_DIR/src/dlangui/dialogs/filedlg.d \ diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 7d8cf45e..1b25f872 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -86,11 +86,7 @@ template checkgl(alias func) { debug auto checkgl(string functionName=__FUNCTION__, int line=__LINE__, Args...)(Args args) { - version (Android) { - scope(success) checkError(__traits(identifier, func), functionName, line); - } else { - scope(success) checkError(func.stringof, functionName, line); - } + scope(success) checkError(__traits(identifier, func), functionName, line); return func(args); } else alias checkgl = func; diff --git a/src/dlangui/platforms/android/androidapp.d b/src/dlangui/platforms/android/androidapp.d index 0a04d69f..a06e1463 100644 --- a/src/dlangui/platforms/android/androidapp.d +++ b/src/dlangui/platforms/android/androidapp.d @@ -14,8 +14,8 @@ import dlangui.platforms.common.platform; import android.input, android.looper : ALooper_pollAll; import android.native_window : ANativeWindow_setBuffersGeometry; -import android.sensor, android.log, android.android_native_app_glue; - +import android.configuration; +import android.log, android.android_native_app_glue; /** * Window abstraction layer. Widgets can be shown only inside window. @@ -87,10 +87,6 @@ class AndroidPlatform : Platform { protected EGLContext _context; protected int _width; protected int _height; - protected ASensorManager* _sensorManager; - protected const(ASensor)* _accelerometerSensor; - protected ASensorEventQueue* _sensorEventQueue; - this(android_app* state) { _appstate = state; @@ -99,13 +95,6 @@ class AndroidPlatform : Platform { state.onAppCmd = &engine_handle_cmd; state.onInputEvent = &engine_handle_input; - // Prepare to monitor accelerometer - _sensorManager = ASensorManager_getInstance(); - _accelerometerSensor = ASensorManager_getDefaultSensor(_sensorManager, - ASENSOR_TYPE_ACCELEROMETER); - _sensorEventQueue = ASensorManager_createEventQueue(_sensorManager, - state.looper, LOOPER_ID_USER, null, null); - if (state.savedState != null) { // We are starting with a previous saved state; restore from it. _engine.state = *cast(saved_state*)state.savedState; @@ -114,16 +103,16 @@ class AndroidPlatform : Platform { } ~this() { - engine_term_display(); + termDisplay(); } /** * Initialize an EGL context for the current display. */ - int engine_init_display() { + int initDisplay() { // initialize OpenGL ES and EGL - Log.i("engine_init_display"); + Log.i("initDisplay"); /* * Here specify the attributes of the desired configuration. @@ -196,8 +185,8 @@ class AndroidPlatform : Platform { /** * Tear down the EGL context currently associated with the display. */ - void engine_term_display() { - Log.i("engine_term_display"); + void termDisplay() { + Log.i("termDisplay"); if (_display != EGL_NO_DISPLAY) { eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); if (_context != EGL_NO_CONTEXT) { @@ -243,31 +232,20 @@ class AndroidPlatform : Platform { case APP_CMD_INIT_WINDOW: // The window is being shown, get it ready. if (_appstate.window != null) { - engine_init_display(); + initDisplay(); drawWindow(); } break; case APP_CMD_TERM_WINDOW: // The window is being hidden or closed, clean it up. - engine_term_display(); + termDisplay(); break; case APP_CMD_GAINED_FOCUS: - // When our app gains focus, we start monitoring the accelerometer. - if (_accelerometerSensor != null) { - ASensorEventQueue_enableSensor(_sensorEventQueue, - _accelerometerSensor); - // We'd like to get 60 events per second (in us). - ASensorEventQueue_setEventRate(_sensorEventQueue, - _accelerometerSensor, (1000L/60)*1000); - } + // When our app gains focus break; case APP_CMD_LOST_FOCUS: - // When our app loses focus, we stop monitoring the accelerometer. + // When our app loses focus // This is to avoid consuming battery while not being used. - if (_accelerometerSensor != null) { - ASensorEventQueue_disableSensor(_sensorEventQueue, - _accelerometerSensor); - } // Also stop animating. _engine.animating = 0; drawWindow(); @@ -378,6 +356,7 @@ class AndroidPlatform : Platform { // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { + /* if (_accelerometerSensor != null) { ASensorEvent event; while (ASensorEventQueue_getEvents(_sensorEventQueue, @@ -387,6 +366,7 @@ class AndroidPlatform : Platform { event.acceleration.z); } } + */ } // Check if we are exiting. @@ -446,18 +426,7 @@ struct saved_state { * Shared state for our app. */ struct engine { - //android_app* app; - - //ASensorManager* sensorManager; - //const(ASensor)* accelerometerSensor; - //ASensorEventQueue* sensorEventQueue; - int animating; - //EGLDisplay display; - //EGLSurface surface; - //EGLContext context; - //int width; - //int height; saved_state state; } @@ -479,6 +448,14 @@ extern(C) void engine_handle_cmd(android_app* app, int cmd) { void main(){} +int getDensityDpi(android_app * app) { + AConfiguration * config = AConfiguration_new(); + AConfiguration_fromAssetManager(config, app.activity.assetManager); + int res = AConfiguration_getDensity(config); + AConfiguration_delete(config); + return res; +} + __gshared AndroidPlatform _platform; /** @@ -501,6 +478,7 @@ extern (C) void android_main(android_app* state) { assert(false); } initResourceManagers(); + SCREEN_DPI = getDensityDpi(state); currentTheme = createDefaultTheme();