src/video/android/SDL_androidevents.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6664 2deb17aefbaf
child 7633 171a7ab71f28
permissions -rw-r--r--
Happy New Year!
paul@4701
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
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"
paul@4713
    30
paul@4701
    31
void
paul@4701
    32
Android_PumpEvents(_THIS)
paul@4701
    33
{
gabomdq@6330
    34
    static int isPaused = 0;
gabomdq@6469
    35
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6469
    36
    static int isPausing = 0;
gabomdq@6469
    37
#endif
slouken@4980
    38
    /* No polling necessary */
gabomdq@6330
    39
gabomdq@6330
    40
    /*
gabomdq@6330
    41
     * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
slouken@6664
    42
     * 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
    43
     * When the resume semaphore is signaled, SDL_GL_CreateContext is called which in turn calls Java code
gabomdq@6330
    44
     * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
gabomdq@6330
    45
     */
gabomdq@6469
    46
gabomdq@6330
    47
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6469
    48
    if (isPaused && !isPausing) {
gabomdq@6330
    49
        if(SDL_SemWait(Android_ResumeSem) == 0) {
gabomdq@6330
    50
#else
gabomdq@6469
    51
    if (isPaused) {
gabomdq@6330
    52
        if(SDL_SemTryWait(Android_ResumeSem) == 0) {
gabomdq@6330
    53
#endif
gabomdq@6330
    54
            isPaused = 0;
gabomdq@6330
    55
            /* TODO: Should we double check if we are on the same thread as the one that made the original GL context?
gabomdq@6330
    56
             * This call will go through the following chain of calls in Java:
gabomdq@6330
    57
             * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
gabomdq@6330
    58
             * SDLActivity::createEGLContext will attempt to restore the GL context first, and if that fails it will create a new one
gabomdq@6330
    59
             * 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
    60
             */
gabomdq@6330
    61
            SDL_GL_CreateContext(Android_Window);
gabomdq@6330
    62
        }
gabomdq@6330
    63
    }
gabomdq@6330
    64
    else {
gabomdq@6469
    65
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6469
    66
        if( isPausing || SDL_SemTryWait(Android_PauseSem) == 0 ) {
gabomdq@6469
    67
            /* We've been signaled to pause, but before we block ourselves, we need to make sure that
gabomdq@6469
    68
            SDL_WINDOWEVENT_FOCUS_LOST and SDL_WINDOWEVENT_MINIMIZED have reached the app */
gabomdq@6469
    69
            if (SDL_HasEvent(SDL_WINDOWEVENT)) {
gabomdq@6469
    70
                isPausing = 1;
gabomdq@6469
    71
            }
gabomdq@6469
    72
            else {
gabomdq@6469
    73
                isPausing = 0;
gabomdq@6469
    74
                isPaused = 1;
gabomdq@6469
    75
            }
gabomdq@6469
    76
        }
gabomdq@6469
    77
#else
gabomdq@6330
    78
        if(SDL_SemTryWait(Android_PauseSem) == 0) {
gabomdq@6330
    79
            /* If we fall in here, the system is/was paused */
gabomdq@6330
    80
            isPaused = 1;
gabomdq@6330
    81
        }
gabomdq@6469
    82
#endif
gabomdq@6330
    83
    }
paul@4713
    84
}
paul@4713
    85
slouken@6044
    86
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
    87
paul@4701
    88
/* vi: set ts=4 sw=4 expandtab: */