src/video/android/SDL_androidwindow.c
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 19 Aug 2013 16:29:46 -0300
changeset 7659 ac4ce59c40e7
parent 7254 497b83000293
child 7986 ffb7bf531644
permissions -rw-r--r--
Fixes bug #2037, common EGL code for Android and X11
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 #include "../SDL_sysvideo.h"
    26 #include "../../events/SDL_keyboard_c.h"
    27 #include "../../events/SDL_mouse_c.h"
    28 
    29 #include "SDL_androidvideo.h"
    30 #include "SDL_androidwindow.h"
    31 
    32 int
    33 Android_CreateWindow(_THIS, SDL_Window * window)
    34 {
    35     SDL_WindowData *data;
    36     
    37     if (Android_Window) {
    38         return SDL_SetError("Android only supports one window");
    39     }
    40     
    41     Android_PauseSem = SDL_CreateSemaphore(0);
    42     Android_ResumeSem = SDL_CreateSemaphore(0);
    43 
    44     /* Adjust the window data to match the screen */
    45     window->x = 0;
    46     window->y = 0;
    47     window->w = Android_ScreenWidth;
    48     window->h = Android_ScreenHeight;
    49 
    50     window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
    51     window->flags |= SDL_WINDOW_FULLSCREEN;     /* window is always fullscreen */
    52     window->flags &= ~SDL_WINDOW_HIDDEN;
    53     window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    54     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
    55 
    56     /* One window, it always has focus */
    57     SDL_SetMouseFocus(window);
    58     SDL_SetKeyboardFocus(window);
    59     
    60     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
    61     if (!data) {
    62         return SDL_OutOfMemory();
    63     }
    64     
    65     data->native_window = Android_JNI_GetNativeWindow();
    66     
    67     if (!data->native_window) {
    68         SDL_free(data);
    69         return SDL_SetError("Could not fetch native window");
    70     }
    71     
    72     data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
    73 
    74     if (data->egl_surface == EGL_NO_SURFACE) {
    75         SDL_free(data);
    76         return SDL_SetError("Could not create GLES window surface");
    77     }
    78 
    79     window->driverdata = data;
    80     Android_Window = window;
    81     
    82     return 0;
    83 }
    84 
    85 void
    86 Android_SetWindowTitle(_THIS, SDL_Window * window)
    87 {
    88     Android_JNI_SetActivityTitle(window->title);
    89 }
    90 
    91 void
    92 Android_DestroyWindow(_THIS, SDL_Window * window)
    93 {
    94     SDL_WindowData *data;
    95     
    96     if (window == Android_Window) {
    97         Android_Window = NULL;
    98         if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
    99         if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
   100         Android_PauseSem = NULL;
   101         Android_ResumeSem = NULL;
   102         
   103         if(window->driverdata) {
   104             data = (SDL_WindowData *) window->driverdata;
   105             if(data->native_window) {
   106                 ANativeWindow_release(data->native_window);
   107             }
   108             SDL_free(window->driverdata);
   109             window->driverdata = NULL;
   110         }
   111     }
   112 }
   113 
   114 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   115 
   116 /* vi: set ts=4 sw=4 expandtab: */