src/video/android/SDL_androidvideo.c
author Sylvain Becker <sylvain.becker@gmail.com>
Thu, 06 Dec 2018 09:22:00 +0100
changeset 12462 b57435750c5b
parent 12201 8bdc4d340419
child 12494 6053ab61996a
permissions -rw-r--r--
Fix warnings detected on Android build
     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_SurfaceWidth = 0;
    64 int Android_SurfaceHeight = 0;
    65 int Android_DeviceWidth = 0;
    66 int Android_DeviceHeight = 0;
    67 static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    68 static int Android_ScreenRate = 0;
    69 
    70 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    71 
    72 /* Currently only one window */
    73 SDL_Window *Android_Window = NULL;
    74 
    75 static int
    76 Android_Available(void)
    77 {
    78     return 1;
    79 }
    80 
    81 static void
    82 Android_SuspendScreenSaver(_THIS)
    83 {
    84     Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
    85 }
    86 
    87 static void
    88 Android_DeleteDevice(SDL_VideoDevice * device)
    89 {
    90     SDL_free(device->driverdata);
    91     SDL_free(device);
    92 }
    93 
    94 static SDL_VideoDevice *
    95 Android_CreateDevice(int devindex)
    96 {
    97     SDL_VideoDevice *device;
    98     SDL_VideoData *data;
    99 
   100     /* Initialize all variables that we clean on shutdown */
   101     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   102     if (!device) {
   103         SDL_OutOfMemory();
   104         return NULL;
   105     }
   106 
   107     data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
   108     if (!data) {
   109         SDL_OutOfMemory();
   110         SDL_free(device);
   111         return NULL;
   112     }
   113 
   114     device->driverdata = data;
   115 
   116     /* Set the function pointers */
   117     device->VideoInit = Android_VideoInit;
   118     device->VideoQuit = Android_VideoQuit;
   119     device->PumpEvents = Android_PumpEvents;
   120 
   121     device->GetDisplayDPI = Android_GetDisplayDPI;
   122 
   123     device->CreateSDLWindow = Android_CreateWindow;
   124     device->SetWindowTitle = Android_SetWindowTitle;
   125     device->SetWindowFullscreen = Android_SetWindowFullscreen;
   126     device->DestroyWindow = Android_DestroyWindow;
   127     device->GetWindowWMInfo = Android_GetWindowWMInfo;
   128 
   129     device->free = Android_DeleteDevice;
   130 
   131     /* GL pointers */
   132     device->GL_LoadLibrary = Android_GLES_LoadLibrary;
   133     device->GL_GetProcAddress = Android_GLES_GetProcAddress;
   134     device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
   135     device->GL_CreateContext = Android_GLES_CreateContext;
   136     device->GL_MakeCurrent = Android_GLES_MakeCurrent;
   137     device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
   138     device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
   139     device->GL_SwapWindow = Android_GLES_SwapWindow;
   140     device->GL_DeleteContext = Android_GLES_DeleteContext;
   141 
   142 #if SDL_VIDEO_VULKAN
   143     device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
   144     device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
   145     device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
   146     device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
   147 #endif
   148 
   149     /* Screensaver */
   150     device->SuspendScreenSaver = Android_SuspendScreenSaver;
   151 
   152     /* Text input */
   153     device->StartTextInput = Android_StartTextInput;
   154     device->StopTextInput = Android_StopTextInput;
   155     device->SetTextInputRect = Android_SetTextInputRect;
   156 
   157     /* Screen keyboard */
   158     device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
   159     device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
   160 
   161     /* Clipboard */
   162     device->SetClipboardText = Android_SetClipboardText;
   163     device->GetClipboardText = Android_GetClipboardText;
   164     device->HasClipboardText = Android_HasClipboardText;
   165 
   166     return device;
   167 }
   168 
   169 VideoBootStrap Android_bootstrap = {
   170     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   171     Android_Available, Android_CreateDevice
   172 };
   173 
   174 
   175 int
   176 Android_VideoInit(_THIS)
   177 {
   178     SDL_DisplayMode mode;
   179 
   180     mode.format = Android_ScreenFormat;
   181     mode.w = Android_DeviceWidth;
   182     mode.h = Android_DeviceHeight;
   183     mode.refresh_rate = Android_ScreenRate;
   184     mode.driverdata = NULL;
   185     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   186         return -1;
   187     }
   188 
   189     SDL_AddDisplayMode(&_this->displays[0], &mode);
   190 
   191     Android_InitKeyboard();
   192 
   193     Android_InitTouch();
   194 
   195     Android_InitMouse();
   196 
   197     /* We're done! */
   198     return 0;
   199 }
   200 
   201 void
   202 Android_VideoQuit(_THIS)
   203 {
   204     Android_QuitMouse();
   205     Android_QuitTouch();
   206 }
   207 
   208 int
   209 Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
   210 {
   211     return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
   212 }
   213 
   214 void
   215 Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
   216 {
   217     SDL_VideoDevice* device;
   218     SDL_VideoDisplay *display;
   219     Android_SurfaceWidth = surfaceWidth;
   220     Android_SurfaceHeight = surfaceHeight;
   221     Android_DeviceWidth = deviceWidth;
   222     Android_DeviceHeight = deviceHeight;
   223     Android_ScreenFormat = format;
   224     Android_ScreenRate = (int)rate;
   225 
   226     /*
   227       Update the resolution of the desktop mode, so that the window
   228       can be properly resized. The screen resolution change can for
   229       example happen when the Activity enters or exits immersive mode,
   230       which can happen after VideoInit().
   231     */
   232     device = SDL_GetVideoDevice();
   233     if (device && device->num_displays > 0)
   234     {
   235         display = &device->displays[0];
   236         display->desktop_mode.format = Android_ScreenFormat;
   237         display->desktop_mode.w = Android_DeviceWidth;
   238         display->desktop_mode.h = Android_DeviceHeight;
   239         display->desktop_mode.refresh_rate  = Android_ScreenRate;
   240     }
   241 
   242     if (Android_Window) {
   243         /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
   244          * will fall back to the old mode */
   245         display = SDL_GetDisplayForWindow(Android_Window);
   246 
   247         display->display_modes[0].format = format;
   248         display->display_modes[0].w = Android_DeviceWidth;
   249         display->display_modes[0].h = Android_DeviceHeight;
   250         display->display_modes[0].refresh_rate = (int)rate;
   251         display->current_mode = display->display_modes[0];
   252 
   253         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
   254     }
   255 }
   256 
   257 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   258 
   259 /* vi: set ts=4 sw=4 expandtab: */