Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE

to set whether the event loop will block itself when the app is paused.
This commit is contained in:
Sylvain Becker 2019-04-05 09:16:30 +02:00
parent b470cd9b09
commit 05333a6e9f
6 changed files with 26 additions and 18 deletions

View File

@ -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) * 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=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. 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: 2.0.9:

View File

@ -174,7 +174,7 @@ http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-a
Pause / Resume behaviour 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 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 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 use, and it allows your app to spring back to life instantaneously after resume

View File

@ -887,6 +887,17 @@ extern "C" {
*/ */
#define SDL_HINT_ANDROID_TRAP_BACK_BUTTON "SDL_ANDROID_TRAP_BACK_BUTTON" #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 * \brief A variable to control whether the return key on the soft keyboard
* should hide the soft keyboard on Android and iOS. * should hide the soft keyboard on Android and iOS.

View File

@ -22,9 +22,6 @@
#if SDL_VIDEO_DRIVER_ANDROID #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_androidevents.h"
#include "SDL_events.h" #include "SDL_events.h"
#include "SDL_androidkeyboard.h" #include "SDL_androidkeyboard.h"
@ -49,14 +46,12 @@ static void openslES_ResumeDevices(void) {}
static void openslES_PauseDevices(void) {} static void openslES_PauseDevices(void) {}
#endif #endif
#if SDL_ANDROID_BLOCK_ON_PAUSE
/* Number of 'type' events in the event queue */ /* Number of 'type' events in the event queue */
static int static int
SDL_NumberOfEvents(Uint32 type) SDL_NumberOfEvents(Uint32 type)
{ {
return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type); return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
} }
#endif /* SDL_ANDROID_BLOCK_ON_PAUSE */
static void static void
android_egl_context_restore(SDL_Window *window) 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 * 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 * No polling necessary
*/ */
#if SDL_ANDROID_BLOCK_ON_PAUSE
void void
Android_PumpEvents(_THIS) Android_PumpEvents_Blocking(_THIS)
{ {
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
@ -146,10 +139,8 @@ Android_PumpEvents(_THIS)
} }
} }
#else
void void
Android_PumpEvents(_THIS) Android_PumpEvents_NonBlocking(_THIS)
{ {
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; 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 */ #endif /* SDL_VIDEO_DRIVER_ANDROID */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -22,6 +22,7 @@
#include "SDL_androidvideo.h" #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: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -26,6 +26,7 @@
#include "SDL_video.h" #include "SDL_video.h"
#include "SDL_mouse.h" #include "SDL_mouse.h"
#include "SDL_hints.h"
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h" #include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h" #include "../../events/SDL_events_c.h"
@ -93,6 +94,7 @@ Android_CreateDevice(int devindex)
{ {
SDL_VideoDevice *device; SDL_VideoDevice *device;
SDL_VideoData *data; SDL_VideoData *data;
SDL_bool block_on_pause;
/* Initialize all variables that we clean on shutdown */ /* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice)); device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
@ -113,7 +115,12 @@ Android_CreateDevice(int devindex)
/* Set the function pointers */ /* Set the function pointers */
device->VideoInit = Android_VideoInit; device->VideoInit = Android_VideoInit;
device->VideoQuit = Android_VideoQuit; 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; device->GetDisplayDPI = Android_GetDisplayDPI;