src/video/android/SDL_androidwindow.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 11 Feb 2018 18:23:37 -0800
changeset 11865 af91d564aa40
parent 11811 5d94cb6b24d3
child 12012 6de756c9975a
permissions -rw-r--r--
On Android show the system UI when an SDL window is windowed, hide the system UI when it's fullscreen, like we do on iOS.
We're increasing the Android SDK minimum version to API 19, this doesn't increase the minimum target API, which is API 14.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 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_syswm.h"
    26 #include "../SDL_sysvideo.h"
    27 #include "../../events/SDL_keyboard_c.h"
    28 #include "../../events/SDL_mouse_c.h"
    29 
    30 #include "SDL_androidvideo.h"
    31 #include "SDL_androidwindow.h"
    32 #include "SDL_hints.h"
    33 
    34 int
    35 Android_CreateWindow(_THIS, SDL_Window * window)
    36 {
    37     SDL_WindowData *data;
    38     
    39     if (Android_Window) {
    40         return SDL_SetError("Android only supports one window");
    41     }
    42     
    43     Android_PauseSem = SDL_CreateSemaphore(0);
    44     Android_ResumeSem = SDL_CreateSemaphore(0);
    45 
    46     /* Set orientation */
    47     Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
    48 
    49     /* Adjust the window data to match the screen */
    50     window->x = 0;
    51     window->y = 0;
    52     window->w = Android_ScreenWidth;
    53     window->h = Android_ScreenHeight;
    54 
    55     window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
    56     window->flags &= ~SDL_WINDOW_HIDDEN;
    57     window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    58     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
    59 
    60     /* One window, it always has focus */
    61     SDL_SetMouseFocus(window);
    62     SDL_SetKeyboardFocus(window);
    63     
    64     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
    65     if (!data) {
    66         return SDL_OutOfMemory();
    67     }
    68     
    69     data->native_window = Android_JNI_GetNativeWindow();
    70     
    71     if (!data->native_window) {
    72         SDL_free(data);
    73         return SDL_SetError("Could not fetch native window");
    74     }
    75 
    76     /* Do not create EGLSurface for Vulkan window since it will then make the window
    77        incompatible with vkCreateAndroidSurfaceKHR */
    78     if ((window->flags & SDL_WINDOW_VULKAN) == 0) {
    79         data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
    80 
    81         if (data->egl_surface == EGL_NO_SURFACE) {
    82             ANativeWindow_release(data->native_window);
    83             SDL_free(data);
    84             return SDL_SetError("Could not create GLES window surface");
    85         }
    86     }
    87 
    88     window->driverdata = data;
    89     Android_Window = window;
    90     
    91     return 0;
    92 }
    93 
    94 void
    95 Android_SetWindowTitle(_THIS, SDL_Window * window)
    96 {
    97     Android_JNI_SetActivityTitle(window->title);
    98 }
    99 
   100 void
   101 Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   102 {
   103     Android_JNI_SetWindowStyle(fullscreen);
   104 }
   105 
   106 void
   107 Android_DestroyWindow(_THIS, SDL_Window * window)
   108 {
   109     SDL_WindowData *data;
   110     
   111     if (window == Android_Window) {
   112         Android_Window = NULL;
   113         if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
   114         if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
   115         Android_PauseSem = NULL;
   116         Android_ResumeSem = NULL;
   117         
   118         if(window->driverdata) {
   119             data = (SDL_WindowData *) window->driverdata;
   120             if (data->egl_surface != EGL_NO_SURFACE) {
   121                 SDL_EGL_DestroySurface(_this, data->egl_surface);
   122             }
   123             if (data->native_window) {
   124                 ANativeWindow_release(data->native_window);
   125             }
   126             SDL_free(window->driverdata);
   127             window->driverdata = NULL;
   128         }
   129     }
   130 }
   131 
   132 SDL_bool
   133 Android_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   134 {
   135     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   136 
   137     if (info->version.major == SDL_MAJOR_VERSION &&
   138         info->version.minor == SDL_MINOR_VERSION) {
   139         info->subsystem = SDL_SYSWM_ANDROID;
   140         info->info.android.window = data->native_window;
   141         info->info.android.surface = data->egl_surface;
   142         return SDL_TRUE;
   143     } else {
   144         SDL_SetError("Application not compiled with SDL %d.%d",
   145                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   146         return SDL_FALSE;
   147     }
   148 }
   149 
   150 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   151 
   152 /* vi: set ts=4 sw=4 expandtab: */