src/video/android/SDL_androidwindow.c
author Gabriel Jacobo <gabomdq@gmail.com>
Thu, 14 Nov 2013 20:14:02 -0300
changeset 7986 ffb7bf531644
parent 7659 ac4ce59c40e7
child 8093 b43765095a6f
permissions -rw-r--r--
Clean up the EGL related video backends (X11, Android, RPi)
     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         ANativeWindow_release(data->native_window);
    76         SDL_free(data);
    77         return SDL_SetError("Could not create GLES window surface");
    78     }
    79 
    80     window->driverdata = data;
    81     Android_Window = window;
    82     
    83     return 0;
    84 }
    85 
    86 void
    87 Android_SetWindowTitle(_THIS, SDL_Window * window)
    88 {
    89     Android_JNI_SetActivityTitle(window->title);
    90 }
    91 
    92 void
    93 Android_DestroyWindow(_THIS, SDL_Window * window)
    94 {
    95     SDL_WindowData *data;
    96     
    97     if (window == Android_Window) {
    98         Android_Window = NULL;
    99         if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
   100         if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
   101         Android_PauseSem = NULL;
   102         Android_ResumeSem = NULL;
   103         
   104         if(window->driverdata) {
   105             data = (SDL_WindowData *) window->driverdata;
   106             if (data->egl_surface != EGL_NO_SURFACE) {
   107                 SDL_EGL_DestroySurface(_this, data->egl_surface);
   108             }
   109             if(data->native_window) {
   110                 ANativeWindow_release(data->native_window);
   111             }
   112             SDL_free(window->driverdata);
   113             window->driverdata = NULL;
   114         }
   115     }
   116 }
   117 
   118 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   119 
   120 /* vi: set ts=4 sw=4 expandtab: */