src/video/android/SDL_androidevents.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Jun 2012 13:57:42 -0300
changeset 6330 0fa55ca2efdd
parent 6138 4c64952a58fb
child 6469 ce84310d85ef
permissions -rwxr-xr-x
Fixes #1422, restores GL context automatically under Android
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"
paul@4713
    26
paul@4701
    27
void
paul@4701
    28
Android_PumpEvents(_THIS)
paul@4701
    29
{
gabomdq@6330
    30
    static int isPaused = 0;
slouken@4980
    31
    /* No polling necessary */
gabomdq@6330
    32
gabomdq@6330
    33
    /*
gabomdq@6330
    34
     * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
gabomdq@6330
    35
     * 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
    36
     * When the resume semaphore is signaled, SDL_GL_CreateContext is called which in turn calls Java code
gabomdq@6330
    37
     * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
gabomdq@6330
    38
     */
gabomdq@6330
    39
    if (isPaused) {
gabomdq@6330
    40
#if SDL_ANDROID_BLOCK_ON_PAUSE
gabomdq@6330
    41
        if(SDL_SemWait(Android_ResumeSem) == 0) {
gabomdq@6330
    42
#else
gabomdq@6330
    43
        if(SDL_SemTryWait(Android_ResumeSem) == 0) {
gabomdq@6330
    44
#endif
gabomdq@6330
    45
            isPaused = 0;
gabomdq@6330
    46
            /* TODO: Should we double check if we are on the same thread as the one that made the original GL context?
gabomdq@6330
    47
             * This call will go through the following chain of calls in Java:
gabomdq@6330
    48
             * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
gabomdq@6330
    49
             * SDLActivity::createEGLContext will attempt to restore the GL context first, and if that fails it will create a new one
gabomdq@6330
    50
             * 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
    51
             */
gabomdq@6330
    52
            SDL_GL_CreateContext(Android_Window);
gabomdq@6330
    53
        }
gabomdq@6330
    54
    }
gabomdq@6330
    55
    else {
gabomdq@6330
    56
        if(SDL_SemTryWait(Android_PauseSem) == 0) {
gabomdq@6330
    57
            /* If we fall in here, the system is/was paused */
gabomdq@6330
    58
            isPaused = 1;
gabomdq@6330
    59
        }
gabomdq@6330
    60
    }
paul@4713
    61
}
paul@4713
    62
slouken@6044
    63
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
    64
paul@4701
    65
/* vi: set ts=4 sw=4 expandtab: */