src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 12 Mar 2019 14:44:25 -0700
changeset 12625 e58468e48179
parent 12556 9b2e48b381db
child 12693 d311fcb8066a
permissions -rw-r--r--
Fixed initial display orientation at Android app start
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2019 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 /* Android SDL video driver implementation */
    26 
    27 #include "SDL_video.h"
    28 #include "SDL_mouse.h"
    29 #include "../SDL_sysvideo.h"
    30 #include "../SDL_pixels_c.h"
    31 #include "../../events/SDL_events_c.h"
    32 #include "../../events/SDL_windowevents_c.h"
    33 
    34 #include "SDL_androidvideo.h"
    35 #include "SDL_androidgl.h"
    36 #include "SDL_androidclipboard.h"
    37 #include "SDL_androidevents.h"
    38 #include "SDL_androidkeyboard.h"
    39 #include "SDL_androidmouse.h"
    40 #include "SDL_androidtouch.h"
    41 #include "SDL_androidwindow.h"
    42 #include "SDL_androidvulkan.h"
    43 
    44 #define ANDROID_VID_DRIVER_NAME "Android"
    45 
    46 /* Initialization/Query functions */
    47 static int Android_VideoInit(_THIS);
    48 static void Android_VideoQuit(_THIS);
    49 int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi);
    50 
    51 #include "../SDL_egl_c.h"
    52 #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
    53 #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
    54 #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
    55 #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
    56 #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
    57 
    58 /* Android driver bootstrap functions */
    59 
    60 
    61 /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
    62 int Android_SurfaceWidth           = 0;
    63 int Android_SurfaceHeight          = 0;
    64 static int Android_DeviceWidth     = 0;
    65 static int Android_DeviceHeight    = 0;
    66 static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    67 static int Android_ScreenRate      = 0;
    68 SDL_sem *Android_PauseSem          = NULL;
    69 SDL_sem *Android_ResumeSem         = NULL;
    70 SDL_mutex *Android_ActivityMutex   = NULL;
    71 
    72 static int
    73 Android_Available(void)
    74 {
    75     return 1;
    76 }
    77 
    78 static void
    79 Android_SuspendScreenSaver(_THIS)
    80 {
    81     Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
    82 }
    83 
    84 static void
    85 Android_DeleteDevice(SDL_VideoDevice *device)
    86 {
    87     SDL_free(device->driverdata);
    88     SDL_free(device);
    89 }
    90 
    91 static SDL_VideoDevice *
    92 Android_CreateDevice(int devindex)
    93 {
    94     SDL_VideoDevice *device;
    95     SDL_VideoData *data;
    96 
    97     /* Initialize all variables that we clean on shutdown */
    98     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    99     if (!device) {
   100         SDL_OutOfMemory();
   101         return NULL;
   102     }
   103 
   104     data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   105     if (!data) {
   106         SDL_OutOfMemory();
   107         SDL_free(device);
   108         return NULL;
   109     }
   110 
   111     device->driverdata = data;
   112 
   113     /* Set the function pointers */
   114     device->VideoInit = Android_VideoInit;
   115     device->VideoQuit = Android_VideoQuit;
   116     device->PumpEvents = Android_PumpEvents;
   117 
   118     device->GetDisplayDPI = Android_GetDisplayDPI;
   119 
   120     device->CreateSDLWindow = Android_CreateWindow;
   121     device->SetWindowTitle = Android_SetWindowTitle;
   122     device->SetWindowFullscreen = Android_SetWindowFullscreen;
   123     device->DestroyWindow = Android_DestroyWindow;
   124     device->GetWindowWMInfo = Android_GetWindowWMInfo;
   125 
   126     device->free = Android_DeleteDevice;
   127 
   128     /* GL pointers */
   129     device->GL_LoadLibrary = Android_GLES_LoadLibrary;
   130     device->GL_GetProcAddress = Android_GLES_GetProcAddress;
   131     device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
   132     device->GL_CreateContext = Android_GLES_CreateContext;
   133     device->GL_MakeCurrent = Android_GLES_MakeCurrent;
   134     device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
   135     device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
   136     device->GL_SwapWindow = Android_GLES_SwapWindow;
   137     device->GL_DeleteContext = Android_GLES_DeleteContext;
   138 
   139 #if SDL_VIDEO_VULKAN
   140     device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
   141     device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
   142     device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
   143     device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
   144 #endif
   145 
   146     /* Screensaver */
   147     device->SuspendScreenSaver = Android_SuspendScreenSaver;
   148 
   149     /* Text input */
   150     device->StartTextInput = Android_StartTextInput;
   151     device->StopTextInput = Android_StopTextInput;
   152     device->SetTextInputRect = Android_SetTextInputRect;
   153 
   154     /* Screen keyboard */
   155     device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
   156     device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
   157 
   158     /* Clipboard */
   159     device->SetClipboardText = Android_SetClipboardText;
   160     device->GetClipboardText = Android_GetClipboardText;
   161     device->HasClipboardText = Android_HasClipboardText;
   162 
   163     return device;
   164 }
   165 
   166 VideoBootStrap Android_bootstrap = {
   167     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   168     Android_Available, Android_CreateDevice
   169 };
   170 
   171 
   172 int
   173 Android_VideoInit(_THIS)
   174 {
   175     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   176     int display_index;
   177     SDL_VideoDisplay *display;
   178     SDL_DisplayMode mode;
   179 
   180     videodata->isPaused  = SDL_FALSE;
   181     videodata->isPausing = SDL_FALSE;
   182 
   183     mode.format          = Android_ScreenFormat;
   184     mode.w               = Android_DeviceWidth;
   185     mode.h               = Android_DeviceHeight;
   186     mode.refresh_rate    = Android_ScreenRate;
   187     mode.driverdata      = NULL;
   188 
   189     display_index = SDL_AddBasicVideoDisplay(&mode);
   190     if (display_index < 0) {
   191         return -1;
   192     }
   193     display = SDL_GetDisplay(display_index);
   194     display->orientation = Android_JNI_GetDisplayOrientation();    
   195 
   196     SDL_AddDisplayMode(&_this->displays[0], &mode);
   197 
   198     Android_InitKeyboard();
   199 
   200     Android_InitTouch();
   201 
   202     Android_InitMouse();
   203 
   204     /* We're done! */
   205     return 0;
   206 }
   207 
   208 void
   209 Android_VideoQuit(_THIS)
   210 {
   211     Android_QuitMouse();
   212     Android_QuitTouch();
   213 }
   214 
   215 int
   216 Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
   217 {
   218     return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
   219 }
   220 
   221 void
   222 Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
   223 {
   224     Android_SurfaceWidth  = surfaceWidth;
   225     Android_SurfaceHeight = surfaceHeight;
   226     Android_DeviceWidth   = deviceWidth;
   227     Android_DeviceHeight  = deviceHeight;
   228     Android_ScreenFormat  = format;
   229     Android_ScreenRate    = (int)rate;
   230 }
   231 
   232 void Android_SendResize(SDL_Window *window)
   233 {
   234     /*
   235       Update the resolution of the desktop mode, so that the window
   236       can be properly resized. The screen resolution change can for
   237       example happen when the Activity enters or exits immersive mode,
   238       which can happen after VideoInit().
   239     */
   240     SDL_VideoDevice *device = SDL_GetVideoDevice();
   241     if (device && device->num_displays > 0)
   242     {
   243         SDL_VideoDisplay *display          = &device->displays[0];
   244         display->desktop_mode.format       = Android_ScreenFormat;
   245         display->desktop_mode.w            = Android_DeviceWidth;
   246         display->desktop_mode.h            = Android_DeviceHeight;
   247         display->desktop_mode.refresh_rate = Android_ScreenRate;
   248     }
   249 
   250     if (window) {
   251         /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
   252          * will fall back to the old mode */
   253         SDL_VideoDisplay *display              = SDL_GetDisplayForWindow(window);
   254         display->display_modes[0].format       = Android_ScreenFormat;
   255         display->display_modes[0].w            = Android_DeviceWidth;
   256         display->display_modes[0].h            = Android_DeviceHeight;
   257         display->display_modes[0].refresh_rate = Android_ScreenRate;
   258         display->current_mode                  = display->display_modes[0];
   259 
   260         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, Android_SurfaceWidth, Android_SurfaceHeight);
   261     }
   262 }
   263 
   264 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   265 
   266 /* vi: set ts=4 sw=4 expandtab: */