src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 11662 a996f135cc81
child 11865 af91d564aa40
permissions -rw-r--r--
Updated copyright for 2018
     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 /* Android SDL video driver implementation
    26 */
    27 
    28 #include "SDL_video.h"
    29 #include "SDL_mouse.h"
    30 #include "../SDL_sysvideo.h"
    31 #include "../SDL_pixels_c.h"
    32 #include "../../events/SDL_events_c.h"
    33 #include "../../events/SDL_windowevents_c.h"
    34 
    35 #include "SDL_androidvideo.h"
    36 #include "SDL_androidgl.h"
    37 #include "SDL_androidclipboard.h"
    38 #include "SDL_androidevents.h"
    39 #include "SDL_androidkeyboard.h"
    40 #include "SDL_androidmouse.h"
    41 #include "SDL_androidtouch.h"
    42 #include "SDL_androidwindow.h"
    43 #include "SDL_androidvulkan.h"
    44 
    45 #define ANDROID_VID_DRIVER_NAME "Android"
    46 
    47 /* Initialization/Query functions */
    48 static int Android_VideoInit(_THIS);
    49 static void Android_VideoQuit(_THIS);
    50 int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
    51 
    52 #include "../SDL_egl_c.h"
    53 #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
    54 #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
    55 #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
    56 #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
    57 #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
    58 
    59 /* Android driver bootstrap functions */
    60 
    61 
    62 /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
    63 int Android_ScreenWidth = 0;
    64 int Android_ScreenHeight = 0;
    65 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    66 static int Android_ScreenRate = 0;
    67 
    68 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    69 
    70 /* Currently only one window */
    71 SDL_Window *Android_Window = NULL;
    72 
    73 static int
    74 Android_Available(void)
    75 {
    76     return 1;
    77 }
    78 
    79 static void
    80 Android_SuspendScreenSaver(_THIS)
    81 {
    82     Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
    83 }
    84 
    85 static void
    86 Android_DeleteDevice(SDL_VideoDevice * device)
    87 {
    88     SDL_free(device->driverdata);
    89     SDL_free(device);
    90 }
    91 
    92 static SDL_VideoDevice *
    93 Android_CreateDevice(int devindex)
    94 {
    95     SDL_VideoDevice *device;
    96     SDL_VideoData *data;
    97 
    98     /* Initialize all variables that we clean on shutdown */
    99     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   100     if (!device) {
   101         SDL_OutOfMemory();
   102         return NULL;
   103     }
   104 
   105     data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
   106     if (!data) {
   107         SDL_OutOfMemory();
   108         SDL_free(device);
   109         return NULL;
   110     }
   111 
   112     device->driverdata = data;
   113 
   114     /* Set the function pointers */
   115     device->VideoInit = Android_VideoInit;
   116     device->VideoQuit = Android_VideoQuit;
   117     device->PumpEvents = Android_PumpEvents;
   118 
   119     device->GetDisplayDPI = Android_GetDisplayDPI;
   120 
   121     device->CreateSDLWindow = Android_CreateWindow;
   122     device->SetWindowTitle = Android_SetWindowTitle;
   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_DisplayMode mode;
   176 
   177     mode.format = Android_ScreenFormat;
   178     mode.w = Android_ScreenWidth;
   179     mode.h = Android_ScreenHeight;
   180     mode.refresh_rate = Android_ScreenRate;
   181     mode.driverdata = NULL;
   182     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   183         return -1;
   184     }
   185 
   186     SDL_AddDisplayMode(&_this->displays[0], &mode);
   187 
   188     Android_InitKeyboard();
   189 
   190     Android_InitTouch();
   191 
   192     Android_InitMouse();
   193 
   194     /* We're done! */
   195     return 0;
   196 }
   197 
   198 void
   199 Android_VideoQuit(_THIS)
   200 {
   201     Android_QuitTouch();
   202 }
   203 
   204 int
   205 Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
   206 {
   207     return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
   208 }
   209 
   210 void
   211 Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
   212 {
   213 	SDL_VideoDevice* device;
   214 	SDL_VideoDisplay *display;
   215     Android_ScreenWidth = width;
   216     Android_ScreenHeight = height;
   217     Android_ScreenFormat = format;
   218     Android_ScreenRate = rate;
   219 
   220     /*
   221       Update the resolution of the desktop mode, so that the window
   222       can be properly resized. The screen resolution change can for
   223       example happen when the Activity enters or exists immersive mode,
   224       which can happen after VideoInit().
   225     */
   226     device = SDL_GetVideoDevice();
   227     if (device && device->num_displays > 0)
   228     {
   229         display = &device->displays[0];
   230         display->desktop_mode.format = Android_ScreenFormat;
   231         display->desktop_mode.w = Android_ScreenWidth;
   232         display->desktop_mode.h = Android_ScreenHeight;
   233         display->desktop_mode.refresh_rate  = Android_ScreenRate;
   234     }
   235 
   236     if (Android_Window) {
   237         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   238 
   239         /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
   240          * will fall back to the old mode */
   241         display = SDL_GetDisplayForWindow(Android_Window);
   242 
   243         display->current_mode.format = format;
   244         display->current_mode.w = width;
   245         display->current_mode.h = height;
   246         display->current_mode.refresh_rate = rate;
   247     }
   248 }
   249 
   250 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   251 
   252 /* vi: set ts=4 sw=4 expandtab: */