Skip to content

Commit

Permalink
Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE
Browse files Browse the repository at this point in the history
to set whether the event loop will block itself when the app is paused.
  • Loading branch information
1bsyl committed Apr 5, 2019
1 parent b470cd9 commit 05333a6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion WhatsNew.txt
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/README-android.md
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -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.
Expand Down
17 changes: 3 additions & 14 deletions src/video/android/SDL_androidevents.c
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -146,10 +139,8 @@ Android_PumpEvents(_THIS)
}
}

#else

void
Android_PumpEvents(_THIS)
Android_PumpEvents_NonBlocking(_THIS)
{
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;

Expand Down Expand Up @@ -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: */
3 changes: 2 additions & 1 deletion src/video/android/SDL_androidevents.h
Expand Up @@ -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: */
9 changes: 8 additions & 1 deletion src/video/android/SDL_androidvideo.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
Expand All @@ -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;

Expand Down

0 comments on commit 05333a6

Please sign in to comment.