src/video/android/SDL_androidwindow.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 12 Jun 2018 13:22:58 -0700
changeset 12017 d8a165136edf
parent 12012 6de756c9975a
child 12255 d2e5d4520677
permissions -rw-r--r--
Deal with fullscreen limitations under windowed Android environments (Chromebook, DeX, etc.) (Thanks Rachel!)
     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 #include "../../events/SDL_windowevents_c.h"
    30 
    31 #include "SDL_androidvideo.h"
    32 #include "SDL_androidwindow.h"
    33 #include "SDL_hints.h"
    34 
    35 int
    36 Android_CreateWindow(_THIS, SDL_Window * window)
    37 {
    38     SDL_WindowData *data;
    39     
    40     if (Android_Window) {
    41         return SDL_SetError("Android only supports one window");
    42     }
    43     
    44     Android_PauseSem = SDL_CreateSemaphore(0);
    45     Android_ResumeSem = SDL_CreateSemaphore(0);
    46 
    47     /* Set orientation */
    48     Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
    49 
    50     /* Adjust the window data to match the screen */
    51     window->x = 0;
    52     window->y = 0;
    53     window->w = Android_SurfaceWidth;
    54     window->h = Android_SurfaceHeight;
    55 
    56     window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
    57     window->flags &= ~SDL_WINDOW_HIDDEN;
    58     window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    59     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
    60 
    61     /* One window, it always has focus */
    62     SDL_SetMouseFocus(window);
    63     SDL_SetKeyboardFocus(window);
    64     
    65     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
    66     if (!data) {
    67         return SDL_OutOfMemory();
    68     }
    69     
    70     data->native_window = Android_JNI_GetNativeWindow();
    71     
    72     if (!data->native_window) {
    73         SDL_free(data);
    74         return SDL_SetError("Could not fetch native window");
    75     }
    76 
    77     /* Do not create EGLSurface for Vulkan window since it will then make the window
    78        incompatible with vkCreateAndroidSurfaceKHR */
    79     if ((window->flags & SDL_WINDOW_VULKAN) == 0) {
    80         data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
    81 
    82         if (data->egl_surface == EGL_NO_SURFACE) {
    83             ANativeWindow_release(data->native_window);
    84             SDL_free(data);
    85             return SDL_SetError("Could not create GLES window surface");
    86         }
    87     }
    88 
    89     window->driverdata = data;
    90     Android_Window = window;
    91     
    92     return 0;
    93 }
    94 
    95 void
    96 Android_SetWindowTitle(_THIS, SDL_Window * window)
    97 {
    98     Android_JNI_SetActivityTitle(window->title);
    99 }
   100 
   101 void
   102 Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   103 {
   104     Android_JNI_SetWindowStyle(fullscreen);
   105 
   106     // Ensure our size matches reality after we've executed the window style change.
   107     //
   108     // It is possible that we've set width and height to the full-size display, but on
   109     // Samsung DeX or Chromebooks or other windowed Android environemtns, our window may 
   110     // still not be the full display size.
   111     //
   112     SDL_WindowData * data = (SDL_WindowData *)window->driverdata;
   113 
   114     if (!data || !data->native_window) {
   115         return;
   116     }
   117 
   118     int old_w = window->w;
   119     int old_h = window->h;
   120 
   121     int new_w = ANativeWindow_getWidth(data->native_window);
   122     int new_h = ANativeWindow_getHeight(data->native_window);
   123 
   124     if (old_w != new_w || old_h != new_h) {
   125         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
   126     }
   127 }
   128 
   129 void
   130 Android_DestroyWindow(_THIS, SDL_Window * window)
   131 {
   132     SDL_WindowData *data;
   133     
   134     if (window == Android_Window) {
   135         Android_Window = NULL;
   136         if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
   137         if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
   138         Android_PauseSem = NULL;
   139         Android_ResumeSem = NULL;
   140         
   141         if(window->driverdata) {
   142             data = (SDL_WindowData *) window->driverdata;
   143             if (data->egl_surface != EGL_NO_SURFACE) {
   144                 SDL_EGL_DestroySurface(_this, data->egl_surface);
   145             }
   146             if (data->native_window) {
   147                 ANativeWindow_release(data->native_window);
   148             }
   149             SDL_free(window->driverdata);
   150             window->driverdata = NULL;
   151         }
   152     }
   153 }
   154 
   155 SDL_bool
   156 Android_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   157 {
   158     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   159 
   160     if (info->version.major == SDL_MAJOR_VERSION &&
   161         info->version.minor == SDL_MINOR_VERSION) {
   162         info->subsystem = SDL_SYSWM_ANDROID;
   163         info->info.android.window = data->native_window;
   164         info->info.android.surface = data->egl_surface;
   165         return SDL_TRUE;
   166     } else {
   167         SDL_SetError("Application not compiled with SDL %d.%d",
   168                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   169         return SDL_FALSE;
   170     }
   171 }
   172 
   173 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   174 
   175 /* vi: set ts=4 sw=4 expandtab: */