Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE
authorSylvain Becker <sylvain.becker@gmail.com>
Fri, 05 Apr 2019 09:16:30 +0200
changeset 12693d311fcb8066a
parent 12692 69f24f4ef99e
child 12694 c9410703cbdd
Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE
to set whether the event loop will block itself when the app is paused.
WhatsNew.txt
docs/README-android.md
include/SDL_hints.h
src/video/android/SDL_androidevents.c
src/video/android/SDL_androidevents.h
src/video/android/SDL_androidvideo.c
     1.1 --- a/WhatsNew.txt	Fri Apr 05 08:36:31 2019 +0200
     1.2 +++ b/WhatsNew.txt	Fri Apr 05 09:16:30 2019 +0200
     1.3 @@ -10,7 +10,7 @@
     1.4  * Removed SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH (replaced by SDL_HINT_MOUSE_TOUCH_EVENTS and SDL_HINT_TOUCH_MOUSE_EVENTS)
     1.5    SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH=1, should be replaced by setting both previous hints to 0.
     1.6    SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH=0, should be replaced by setting both previous hints to 1.
     1.7 -
     1.8 +* Added the hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE to set whether the event loop will block itself when the app is paused.
     1.9  
    1.10  ---------------------------------------------------------------------------
    1.11  2.0.9:
     2.1 --- a/docs/README-android.md	Fri Apr 05 08:36:31 2019 +0200
     2.2 +++ b/docs/README-android.md	Fri Apr 05 09:16:30 2019 +0200
     2.3 @@ -174,7 +174,7 @@
     2.4   Pause / Resume behaviour
     2.5  ================================================================================
     2.6  
     2.7 -If SDL is compiled with SDL_ANDROID_BLOCK_ON_PAUSE defined (the default),
     2.8 +If SDL_HINT_ANDROID_BLOCK_ON_PAUSE hint is set (the default),
     2.9  the event loop will block itself when the app is paused (ie, when the user
    2.10  returns to the main Android dashboard). Blocking is better in terms of battery
    2.11  use, and it allows your app to spring back to life instantaneously after resume
     3.1 --- a/include/SDL_hints.h	Fri Apr 05 08:36:31 2019 +0200
     3.2 +++ b/include/SDL_hints.h	Fri Apr 05 09:16:30 2019 +0200
     3.3 @@ -887,6 +887,17 @@
     3.4   */
     3.5  #define SDL_HINT_ANDROID_TRAP_BACK_BUTTON "SDL_ANDROID_TRAP_BACK_BUTTON"
     3.6  
     3.7 +/**
     3.8 + * \brief A variable to control whether the event loop will block itself when the app is paused.
     3.9 + *
    3.10 + * The variable can be set to the following values:
    3.11 + *   "0"       - Non blocking.
    3.12 + *   "1"       - Blocking. (default)
    3.13 + *
    3.14 + * The value should be set before SDL is initialized.
    3.15 + */
    3.16 +#define SDL_HINT_ANDROID_BLOCK_ON_PAUSE "SDL_ANDROID_BLOCK_ON_PAUSE"
    3.17 +
    3.18   /**
    3.19   * \brief A variable to control whether the return key on the soft keyboard
    3.20   *        should hide the soft keyboard on Android and iOS.
     4.1 --- a/src/video/android/SDL_androidevents.c	Fri Apr 05 08:36:31 2019 +0200
     4.2 +++ b/src/video/android/SDL_androidevents.c	Fri Apr 05 09:16:30 2019 +0200
     4.3 @@ -22,9 +22,6 @@
     4.4  
     4.5  #if SDL_VIDEO_DRIVER_ANDROID
     4.6  
     4.7 -/* We're going to do this by default */
     4.8 -#define SDL_ANDROID_BLOCK_ON_PAUSE  1
     4.9 -
    4.10  #include "SDL_androidevents.h"
    4.11  #include "SDL_events.h"
    4.12  #include "SDL_androidkeyboard.h"
    4.13 @@ -49,14 +46,12 @@
    4.14  static void openslES_PauseDevices(void) {}
    4.15  #endif
    4.16  
    4.17 -#if SDL_ANDROID_BLOCK_ON_PAUSE
    4.18  /* Number of 'type' events in the event queue */
    4.19  static int
    4.20  SDL_NumberOfEvents(Uint32 type)
    4.21  {
    4.22      return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
    4.23  }
    4.24 -#endif /* SDL_ANDROID_BLOCK_ON_PAUSE */
    4.25  
    4.26  static void
    4.27  android_egl_context_restore(SDL_Window *window)
    4.28 @@ -89,15 +84,13 @@
    4.29  
    4.30  /*
    4.31   * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
    4.32 - * 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.
    4.33 + * When the pause semaphore is signaled, if Android_PumpEvents_Blocking is used, the event loop will block until the resume signal is emitted.
    4.34   *
    4.35   * No polling necessary
    4.36   */
    4.37  
    4.38 -#if SDL_ANDROID_BLOCK_ON_PAUSE
    4.39 -
    4.40  void
    4.41 -Android_PumpEvents(_THIS)
    4.42 +Android_PumpEvents_Blocking(_THIS)
    4.43  {
    4.44      SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
    4.45  
    4.46 @@ -146,10 +139,8 @@
    4.47      }
    4.48  }
    4.49  
    4.50 -#else
    4.51 -
    4.52  void
    4.53 -Android_PumpEvents(_THIS)
    4.54 +Android_PumpEvents_NonBlocking(_THIS)
    4.55  {
    4.56      SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
    4.57  
    4.58 @@ -188,8 +179,6 @@
    4.59      }
    4.60  }
    4.61  
    4.62 -#endif /* SDL_ANDROID_BLOCK_ON_PAUSE */
    4.63 -
    4.64  #endif /* SDL_VIDEO_DRIVER_ANDROID */
    4.65  
    4.66  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/android/SDL_androidevents.h	Fri Apr 05 08:36:31 2019 +0200
     5.2 +++ b/src/video/android/SDL_androidevents.h	Fri Apr 05 09:16:30 2019 +0200
     5.3 @@ -22,6 +22,7 @@
     5.4  
     5.5  #include "SDL_androidvideo.h"
     5.6  
     5.7 -extern void Android_PumpEvents(_THIS);
     5.8 +extern void Android_PumpEvents_Blocking(_THIS);
     5.9 +extern void Android_PumpEvents_NonBlocking(_THIS);
    5.10  
    5.11  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/android/SDL_androidvideo.c	Fri Apr 05 08:36:31 2019 +0200
     6.2 +++ b/src/video/android/SDL_androidvideo.c	Fri Apr 05 09:16:30 2019 +0200
     6.3 @@ -26,6 +26,7 @@
     6.4  
     6.5  #include "SDL_video.h"
     6.6  #include "SDL_mouse.h"
     6.7 +#include "SDL_hints.h"
     6.8  #include "../SDL_sysvideo.h"
     6.9  #include "../SDL_pixels_c.h"
    6.10  #include "../../events/SDL_events_c.h"
    6.11 @@ -93,6 +94,7 @@
    6.12  {
    6.13      SDL_VideoDevice *device;
    6.14      SDL_VideoData *data;
    6.15 +    SDL_bool block_on_pause;
    6.16  
    6.17      /* Initialize all variables that we clean on shutdown */
    6.18      device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    6.19 @@ -113,7 +115,12 @@
    6.20      /* Set the function pointers */
    6.21      device->VideoInit = Android_VideoInit;
    6.22      device->VideoQuit = Android_VideoQuit;
    6.23 -    device->PumpEvents = Android_PumpEvents;
    6.24 +    block_on_pause = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, SDL_TRUE);
    6.25 +    if (block_on_pause) {
    6.26 +        device->PumpEvents = Android_PumpEvents_Blocking;
    6.27 +    } else {
    6.28 +        device->PumpEvents = Android_PumpEvents_NonBlocking;
    6.29 +    }
    6.30  
    6.31      device->GetDisplayDPI = Android_GetDisplayDPI;
    6.32