src/video/android/SDL_androidwindow.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 02 Jun 2014 09:01:26 -0700
changeset 8799 e2fd743d84ea
parent 8149 681eb46b8ac4
child 8803 ace0e63268f3
permissions -rw-r--r--
Added a way to get the native Android window and EGL context
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_internal.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 SDL_bool
   119 Android_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   120 {
   121     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   122 
   123     if (info->version.major == SDL_MAJOR_VERSION &&
   124         info->version.minor == SDL_MINOR_VERSION) {
   125         info->subsystem = SDL_SYSWM_ANDROID;
   126         info->info.android.window = data->native_window;
   127         info->info.android.surface = data->egl_surface;
   128         return SDL_TRUE;
   129     } else {
   130         SDL_SetError("Application not compiled with SDL %d.%d\n",
   131                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   132         return SDL_FALSE;
   133     }
   134 }
   135 
   136 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   137 
   138 /* vi: set ts=4 sw=4 expandtab: */