src/video/android/SDL_androidwindow.c
author Sylvain Becker <sylvain.becker@gmail.com>
Thu, 03 Jan 2019 16:22:33 +0100
changeset 12497 9e13f3286831
parent 12496 93771c30420b
child 12498 c2e223dc5da9
permissions -rw-r--r--
Android: prevent the error message from SDL_EGL_CreateSurface() to be masked.
     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 #include "../../core/android/SDL_android.h"
    31 
    32 #include "SDL_androidvideo.h"
    33 #include "SDL_androidwindow.h"
    34 #include "SDL_hints.h"
    35 
    36 /* Currently only one window */
    37 SDL_Window *Android_Window = NULL;
    38 
    39 int
    40 Android_CreateWindow(_THIS, SDL_Window * window)
    41 {
    42     SDL_WindowData *data;
    43     int retval = 0;
    44 
    45     SDL_SemWait(Android_ActivitySem);
    46 
    47     if (Android_Window) {
    48         retval = SDL_SetError("Android only supports one window");
    49         goto endfunction;
    50     }
    51 
    52     Android_PauseSem = SDL_CreateSemaphore(0);
    53     Android_ResumeSem = SDL_CreateSemaphore(0);
    54 
    55     /* Set orientation */
    56     Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
    57 
    58     /* Adjust the window data to match the screen */
    59     window->x = 0;
    60     window->y = 0;
    61     window->w = Android_SurfaceWidth;
    62     window->h = Android_SurfaceHeight;
    63 
    64     window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
    65     window->flags &= ~SDL_WINDOW_HIDDEN;
    66     window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    67     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
    68 
    69     /* One window, it always has focus */
    70     SDL_SetMouseFocus(window);
    71     SDL_SetKeyboardFocus(window);
    72 
    73     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
    74     if (!data) {
    75         retval = SDL_OutOfMemory();
    76         goto endfunction;
    77     }
    78 
    79     data->native_window = Android_JNI_GetNativeWindow();
    80 
    81     if (!data->native_window) {
    82         SDL_free(data);
    83         retval = SDL_SetError("Could not fetch native window");
    84         goto endfunction;
    85     }
    86 
    87     /* Do not create EGLSurface for Vulkan window since it will then make the window
    88        incompatible with vkCreateAndroidSurfaceKHR */
    89     if ((window->flags & SDL_WINDOW_VULKAN) == 0) {
    90         data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
    91 
    92         if (data->egl_surface == EGL_NO_SURFACE) {
    93             ANativeWindow_release(data->native_window);
    94             SDL_free(data);
    95             retval = -1;
    96             goto endfunction;
    97         }
    98     }
    99 
   100     window->driverdata = data;
   101     Android_Window = window;
   102 
   103 endfunction:
   104 
   105     SDL_SemPost(Android_ActivitySem);
   106 
   107     return retval;
   108 }
   109 
   110 void
   111 Android_SetWindowTitle(_THIS, SDL_Window *window)
   112 {
   113     Android_JNI_SetActivityTitle(window->title);
   114 }
   115 
   116 void
   117 Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
   118 {
   119     /* If the window is being destroyed don't change visible state */
   120     if (!window->is_destroying) {
   121         Android_JNI_SetWindowStyle(fullscreen);
   122     }
   123 
   124     /* Ensure our size matches reality after we've executed the window style change.
   125      *
   126      * It is possible that we've set width and height to the full-size display, but on
   127      * Samsung DeX or Chromebooks or other windowed Android environemtns, our window may 
   128      * still not be the full display size.
   129      */
   130     if (!SDL_IsDeXMode() && !SDL_IsChromebook()) {
   131         return;
   132     }
   133 
   134     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
   135 
   136     if (!data || !data->native_window) {
   137         return;
   138     }
   139 
   140     int old_w = window->w;
   141     int old_h = window->h;
   142 
   143     int new_w = ANativeWindow_getWidth(data->native_window);
   144     int new_h = ANativeWindow_getHeight(data->native_window);
   145 
   146     if (old_w != new_w || old_h != new_h) {
   147         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
   148     }
   149 }
   150 
   151 void
   152 Android_DestroyWindow(_THIS, SDL_Window *window)
   153 {
   154     SDL_SemWait(Android_ActivitySem);
   155 
   156     if (window == Android_Window) {
   157         Android_Window = NULL;
   158         if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
   159         if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
   160         Android_PauseSem = NULL;
   161         Android_ResumeSem = NULL;
   162 
   163         if (window->driverdata) {
   164             SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   165             if (data->egl_surface != EGL_NO_SURFACE) {
   166                 SDL_EGL_DestroySurface(_this, data->egl_surface);
   167             }
   168             if (data->native_window) {
   169                 ANativeWindow_release(data->native_window);
   170             }
   171             SDL_free(window->driverdata);
   172             window->driverdata = NULL;
   173         }
   174     }
   175 
   176     SDL_SemPost(Android_ActivitySem);
   177 }
   178 
   179 SDL_bool
   180 Android_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
   181 {
   182     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   183 
   184     if (info->version.major == SDL_MAJOR_VERSION &&
   185         info->version.minor == SDL_MINOR_VERSION) {
   186         info->subsystem = SDL_SYSWM_ANDROID;
   187         info->info.android.window = data->native_window;
   188         info->info.android.surface = data->egl_surface;
   189         return SDL_TRUE;
   190     } else {
   191         SDL_SetError("Application not compiled with SDL %d.%d",
   192                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   193         return SDL_FALSE;
   194     }
   195 }
   196 
   197 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   198 
   199 /* vi: set ts=4 sw=4 expandtab: */