src/video/android/SDL_androidevents.c
author Gabriel Jacobo <gabomdq@gmail.com>
Thu, 27 Sep 2012 15:00:34 -0300
changeset 6469 ce84310d85ef
parent 6330 0fa55ca2efdd
child 6664 2deb17aefbaf
permissions -rwxr-xr-x
Don't block Android event loop until all relevant events reach the app.
paul@4701
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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
*/
paul@4701
    21
#include "SDL_config.h"
paul@4701
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_ANDROID
slouken@6044
    24
paul@4713
    25
#include "SDL_androidevents.h"
gabomdq@6469
    26
#include "SDL_events.h"
paul@4713
    27
paul@4701
    28
void
paul@4701
    29
Android_PumpEvents(_THIS)
paul@4701
    30
{
gabomdq@6330
    31
    static int isPaused = 0;
gabomdq@6469
    32
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6469
    33
    static int isPausing = 0;
gabomdq@6469
    34
#endif
slouken@4980
    35
    /* No polling necessary */
gabomdq@6330
    36
gabomdq@6330
    37
    /*
gabomdq@6330
    38
     * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
gabomdq@6330
    39
     * When the pause semaphoe is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
gabomdq@6330
    40
     * When the resume semaphore is signaled, SDL_GL_CreateContext is called which in turn calls Java code
gabomdq@6330
    41
     * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
gabomdq@6330
    42
     */
gabomdq@6469
    43
gabomdq@6330
    44
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6469
    45
    if (isPaused && !isPausing) {
gabomdq@6330
    46
        if(SDL_SemWait(Android_ResumeSem) == 0) {
gabomdq@6330
    47
#else
gabomdq@6469
    48
    if (isPaused) {
gabomdq@6330
    49
        if(SDL_SemTryWait(Android_ResumeSem) == 0) {
gabomdq@6330
    50
#endif
gabomdq@6330
    51
            isPaused = 0;
gabomdq@6330
    52
            /* TODO: Should we double check if we are on the same thread as the one that made the original GL context?
gabomdq@6330
    53
             * This call will go through the following chain of calls in Java:
gabomdq@6330
    54
             * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
gabomdq@6330
    55
             * SDLActivity::createEGLContext will attempt to restore the GL context first, and if that fails it will create a new one
gabomdq@6330
    56
             * If a new GL context is created, the user needs to restore the textures manually (TODO: notify the user that this happened with a message)
gabomdq@6330
    57
             */
gabomdq@6330
    58
            SDL_GL_CreateContext(Android_Window);
gabomdq@6330
    59
        }
gabomdq@6330
    60
    }
gabomdq@6330
    61
    else {
gabomdq@6469
    62
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6469
    63
        if( isPausing || SDL_SemTryWait(Android_PauseSem) == 0 ) {
gabomdq@6469
    64
            /* We've been signaled to pause, but before we block ourselves, we need to make sure that
gabomdq@6469
    65
            SDL_WINDOWEVENT_FOCUS_LOST and SDL_WINDOWEVENT_MINIMIZED have reached the app */
gabomdq@6469
    66
            if (SDL_HasEvent(SDL_WINDOWEVENT)) {
gabomdq@6469
    67
                isPausing = 1;
gabomdq@6469
    68
            }
gabomdq@6469
    69
            else {
gabomdq@6469
    70
                isPausing = 0;
gabomdq@6469
    71
                isPaused = 1;
gabomdq@6469
    72
            }
gabomdq@6469
    73
        }
gabomdq@6469
    74
#else
gabomdq@6330
    75
        if(SDL_SemTryWait(Android_PauseSem) == 0) {
gabomdq@6330
    76
            /* If we fall in here, the system is/was paused */
gabomdq@6330
    77
            isPaused = 1;
gabomdq@6330
    78
        }
gabomdq@6469
    79
#endif
gabomdq@6469
    80
gabomdq@6330
    81
    }
paul@4713
    82
}
paul@4713
    83
slouken@6044
    84
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
    85
paul@4701
    86
/* vi: set ts=4 sw=4 expandtab: */