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