Commit 05333a6e9f9990407f6798b4d20067f567bcf4a2

Sylvain Becker 2019-04-05T09:16:30

Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE to set whether the event loop will block itself when the app is paused.

diff --git a/WhatsNew.txt b/WhatsNew.txt
index 34860cb..f9fe558 100644
--- a/WhatsNew.txt
+++ b/WhatsNew.txt
@@ -10,7 +10,7 @@ Android:
 * Removed SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH (replaced by SDL_HINT_MOUSE_TOUCH_EVENTS and SDL_HINT_TOUCH_MOUSE_EVENTS)
   SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH=1, should be replaced by setting both previous hints to 0.
   SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH=0, should be replaced by setting both previous hints to 1.
-
+* Added the hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE to set whether the event loop will block itself when the app is paused.
 
 ---------------------------------------------------------------------------
 2.0.9:
diff --git a/docs/README-android.md b/docs/README-android.md
index 4feb42d..78dcdd4 100644
--- a/docs/README-android.md
+++ b/docs/README-android.md
@@ -174,7 +174,7 @@ http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-a
  Pause / Resume behaviour
 ================================================================================
 
-If SDL is compiled with SDL_ANDROID_BLOCK_ON_PAUSE defined (the default),
+If SDL_HINT_ANDROID_BLOCK_ON_PAUSE hint is set (the default),
 the event loop will block itself when the app is paused (ie, when the user
 returns to the main Android dashboard). Blocking is better in terms of battery
 use, and it allows your app to spring back to life instantaneously after resume
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 20d510b..666c6ea 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -887,6 +887,17 @@ extern "C" {
  */
 #define SDL_HINT_ANDROID_TRAP_BACK_BUTTON "SDL_ANDROID_TRAP_BACK_BUTTON"
 
+/**
+ * \brief A variable to control whether the event loop will block itself when the app is paused.
+ *
+ * The variable can be set to the following values:
+ *   "0"       - Non blocking.
+ *   "1"       - Blocking. (default)
+ *
+ * The value should be set before SDL is initialized.
+ */
+#define SDL_HINT_ANDROID_BLOCK_ON_PAUSE "SDL_ANDROID_BLOCK_ON_PAUSE"
+
  /**
  * \brief A variable to control whether the return key on the soft keyboard
  *        should hide the soft keyboard on Android and iOS.
diff --git a/src/video/android/SDL_androidevents.c b/src/video/android/SDL_androidevents.c
index a021e2b..e13916f 100644
--- a/src/video/android/SDL_androidevents.c
+++ b/src/video/android/SDL_androidevents.c
@@ -22,9 +22,6 @@
 
 #if SDL_VIDEO_DRIVER_ANDROID
 
-/* We're going to do this by default */
-#define SDL_ANDROID_BLOCK_ON_PAUSE  1
-
 #include "SDL_androidevents.h"
 #include "SDL_events.h"
 #include "SDL_androidkeyboard.h"
@@ -49,14 +46,12 @@ static void openslES_ResumeDevices(void) {}
 static void openslES_PauseDevices(void) {}
 #endif
 
-#if SDL_ANDROID_BLOCK_ON_PAUSE
 /* Number of 'type' events in the event queue */
 static int
 SDL_NumberOfEvents(Uint32 type)
 {
     return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
 }
-#endif /* SDL_ANDROID_BLOCK_ON_PAUSE */
 
 static void
 android_egl_context_restore(SDL_Window *window)
@@ -89,15 +84,13 @@ android_egl_context_backup(SDL_Window *window)
 
 /*
  * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
- * When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
+ * When the pause semaphore is signaled, if Android_PumpEvents_Blocking is used, the event loop will block until the resume signal is emitted.
  *
  * No polling necessary
  */
 
-#if SDL_ANDROID_BLOCK_ON_PAUSE
-
 void
-Android_PumpEvents(_THIS)
+Android_PumpEvents_Blocking(_THIS)
 {
     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
 
@@ -146,10 +139,8 @@ Android_PumpEvents(_THIS)
     }
 }
 
-#else
-
 void
-Android_PumpEvents(_THIS)
+Android_PumpEvents_NonBlocking(_THIS)
 {
     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
 
@@ -188,8 +179,6 @@ Android_PumpEvents(_THIS)
     }
 }
 
-#endif /* SDL_ANDROID_BLOCK_ON_PAUSE */
-
 #endif /* SDL_VIDEO_DRIVER_ANDROID */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/android/SDL_androidevents.h b/src/video/android/SDL_androidevents.h
index 2ccb746..07d1570 100644
--- a/src/video/android/SDL_androidevents.h
+++ b/src/video/android/SDL_androidevents.h
@@ -22,6 +22,7 @@
 
 #include "SDL_androidvideo.h"
 
-extern void Android_PumpEvents(_THIS);
+extern void Android_PumpEvents_Blocking(_THIS);
+extern void Android_PumpEvents_NonBlocking(_THIS);
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index 9ee60d7..3057ee9 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -26,6 +26,7 @@
 
 #include "SDL_video.h"
 #include "SDL_mouse.h"
+#include "SDL_hints.h"
 #include "../SDL_sysvideo.h"
 #include "../SDL_pixels_c.h"
 #include "../../events/SDL_events_c.h"
@@ -93,6 +94,7 @@ Android_CreateDevice(int devindex)
 {
     SDL_VideoDevice *device;
     SDL_VideoData *data;
+    SDL_bool block_on_pause;
 
     /* Initialize all variables that we clean on shutdown */
     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
@@ -113,7 +115,12 @@ Android_CreateDevice(int devindex)
     /* Set the function pointers */
     device->VideoInit = Android_VideoInit;
     device->VideoQuit = Android_VideoQuit;
-    device->PumpEvents = Android_PumpEvents;
+    block_on_pause = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, SDL_TRUE);
+    if (block_on_pause) {
+        device->PumpEvents = Android_PumpEvents_Blocking;
+    } else {
+        device->PumpEvents = Android_PumpEvents_NonBlocking;
+    }
 
     device->GetDisplayDPI = Android_GetDisplayDPI;