src/video/android/SDL_androidevents.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 /* We're going to do this by default */
    26 #define SDL_ANDROID_BLOCK_ON_PAUSE  1
    27 
    28 #include "SDL_androidevents.h"
    29 #include "SDL_events.h"
    30 #include "SDL_androidwindow.h"
    31 
    32 /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
    33  * because of THIS redefinition */
    34 extern void ANDROIDAUDIO_ResumeDevices(void);
    35 extern void ANDROIDAUDIO_PauseDevices(void);
    36 
    37 static void 
    38 android_egl_context_restore() 
    39 {
    40     SDL_Event event;
    41     SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
    42     if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) {
    43         /* The context is no longer valid, create a new one */
    44         data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window);
    45         SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context);
    46         event.type = SDL_RENDER_DEVICE_RESET;
    47         SDL_PushEvent(&event);
    48     }
    49 }
    50 
    51 static void 
    52 android_egl_context_backup() 
    53 {
    54     /* Keep a copy of the EGL Context so we can try to restore it when we resume */
    55     SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
    56     data->egl_context = SDL_GL_GetCurrentContext();
    57     /* We need to do this so the EGLSurface can be freed */
    58     SDL_GL_MakeCurrent(Android_Window, NULL);
    59 }
    60 
    61 void
    62 Android_PumpEvents(_THIS)
    63 {
    64     static int isPaused = 0;
    65 #if SDL_ANDROID_BLOCK_ON_PAUSE
    66     static int isPausing = 0;
    67 #endif
    68     /* No polling necessary */
    69 
    70     /*
    71      * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
    72      * 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.
    73      */
    74 
    75 #if SDL_ANDROID_BLOCK_ON_PAUSE
    76     if (isPaused && !isPausing) {
    77         /* Make sure this is the last thing we do before pausing */
    78         android_egl_context_backup();
    79         ANDROIDAUDIO_PauseDevices();
    80         if(SDL_SemWait(Android_ResumeSem) == 0) {
    81 #else
    82     if (isPaused) {
    83         if(SDL_SemTryWait(Android_ResumeSem) == 0) {
    84 #endif
    85             isPaused = 0;
    86             ANDROIDAUDIO_ResumeDevices();
    87             /* Restore the GL Context from here, as this operation is thread dependent */
    88             if (!SDL_HasEvent(SDL_QUIT)) {
    89                 android_egl_context_restore();
    90             }
    91         }
    92     }
    93     else {
    94 #if SDL_ANDROID_BLOCK_ON_PAUSE
    95         if( isPausing || SDL_SemTryWait(Android_PauseSem) == 0 ) {
    96             /* We've been signaled to pause, but before we block ourselves, 
    97             we need to make sure that certain key events have reached the app */
    98             if (SDL_HasEvent(SDL_WINDOWEVENT) || SDL_HasEvent(SDL_APP_WILLENTERBACKGROUND) || SDL_HasEvent(SDL_APP_DIDENTERBACKGROUND) ) {
    99                 isPausing = 1;
   100             }
   101             else {
   102                 isPausing = 0;
   103                 isPaused = 1;
   104             }
   105         }
   106 #else
   107         if(SDL_SemTryWait(Android_PauseSem) == 0) {
   108             android_egl_context_backup();
   109             ANDROIDAUDIO_PauseDevices();
   110             isPaused = 1;
   111         }
   112 #endif
   113     }
   114 }
   115 
   116 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   117 
   118 /* vi: set ts=4 sw=4 expandtab: */