src/video/android/SDL_androidvideo.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 10937 9ff840e45cfb
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 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 
    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 
    50 #include "../SDL_egl_c.h"
    51 #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
    52 #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
    53 #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
    54 #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
    55 #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
    56 
    57 /* Android driver bootstrap functions */
    58 
    59 
    60 /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
    61 int Android_ScreenWidth = 0;
    62 int Android_ScreenHeight = 0;
    63 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    64 static int Android_ScreenRate = 0;
    65 
    66 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    67 
    68 /* Currently only one window */
    69 SDL_Window *Android_Window = NULL;
    70 
    71 static int
    72 Android_Available(void)
    73 {
    74     return 1;
    75 }
    76 
    77 static void
    78 Android_SuspendScreenSaver(_THIS)
    79 {
    80     Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
    81 }
    82 
    83 static void
    84 Android_DeleteDevice(SDL_VideoDevice * device)
    85 {
    86     SDL_free(device->driverdata);
    87     SDL_free(device);
    88 }
    89 
    90 static SDL_VideoDevice *
    91 Android_CreateDevice(int devindex)
    92 {
    93     SDL_VideoDevice *device;
    94     SDL_VideoData *data;
    95 
    96     /* Initialize all variables that we clean on shutdown */
    97     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    98     if (!device) {
    99         SDL_OutOfMemory();
   100         return NULL;
   101     }
   102 
   103     data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
   104     if (!data) {
   105         SDL_OutOfMemory();
   106         SDL_free(device);
   107         return NULL;
   108     }
   109 
   110     device->driverdata = data;
   111 
   112     /* Set the function pointers */
   113     device->VideoInit = Android_VideoInit;
   114     device->VideoQuit = Android_VideoQuit;
   115     device->PumpEvents = Android_PumpEvents;
   116 
   117     device->CreateWindow = Android_CreateWindow;
   118     device->SetWindowTitle = Android_SetWindowTitle;
   119     device->DestroyWindow = Android_DestroyWindow;
   120     device->GetWindowWMInfo = Android_GetWindowWMInfo;
   121 
   122     device->free = Android_DeleteDevice;
   123 
   124     /* GL pointers */
   125     device->GL_LoadLibrary = Android_GLES_LoadLibrary;
   126     device->GL_GetProcAddress = Android_GLES_GetProcAddress;
   127     device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
   128     device->GL_CreateContext = Android_GLES_CreateContext;
   129     device->GL_MakeCurrent = Android_GLES_MakeCurrent;
   130     device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
   131     device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
   132     device->GL_SwapWindow = Android_GLES_SwapWindow;
   133     device->GL_DeleteContext = Android_GLES_DeleteContext;
   134 
   135     /* Screensaver */
   136     device->SuspendScreenSaver = Android_SuspendScreenSaver;
   137 
   138     /* Text input */
   139     device->StartTextInput = Android_StartTextInput;
   140     device->StopTextInput = Android_StopTextInput;
   141     device->SetTextInputRect = Android_SetTextInputRect;
   142 
   143     /* Screen keyboard */
   144     device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
   145     device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
   146 
   147     /* Clipboard */
   148     device->SetClipboardText = Android_SetClipboardText;
   149     device->GetClipboardText = Android_GetClipboardText;
   150     device->HasClipboardText = Android_HasClipboardText;
   151 
   152     return device;
   153 }
   154 
   155 VideoBootStrap Android_bootstrap = {
   156     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   157     Android_Available, Android_CreateDevice
   158 };
   159 
   160 
   161 int
   162 Android_VideoInit(_THIS)
   163 {
   164     SDL_DisplayMode mode;
   165 
   166     mode.format = Android_ScreenFormat;
   167     mode.w = Android_ScreenWidth;
   168     mode.h = Android_ScreenHeight;
   169     mode.refresh_rate = Android_ScreenRate;
   170     mode.driverdata = NULL;
   171     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   172         return -1;
   173     }
   174 
   175     SDL_AddDisplayMode(&_this->displays[0], &mode);
   176 
   177     Android_InitKeyboard();
   178 
   179     Android_InitTouch();
   180 
   181     Android_InitMouse();
   182 
   183     /* We're done! */
   184     return 0;
   185 }
   186 
   187 void
   188 Android_VideoQuit(_THIS)
   189 {
   190     Android_QuitTouch();
   191 }
   192 
   193 void
   194 Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
   195 {
   196     Android_ScreenWidth = width;
   197     Android_ScreenHeight = height;
   198     Android_ScreenFormat = format;
   199     Android_ScreenRate = rate;
   200 
   201     /*
   202       Update the resolution of the desktop mode, so that the window
   203       can be properly resized. The screen resolution change can for
   204       example happen when the Activity enters or exists immersive mode,
   205       which can happen after VideoInit().
   206     */
   207     SDL_VideoDevice* device = SDL_GetVideoDevice();
   208     if (device && device->num_displays > 0)
   209     {
   210         SDL_VideoDisplay* display = &device->displays[0];
   211         display->desktop_mode.format = Android_ScreenFormat;
   212         display->desktop_mode.w = Android_ScreenWidth;
   213         display->desktop_mode.h = Android_ScreenHeight;
   214         display->desktop_mode.refresh_rate  = Android_ScreenRate;
   215     }
   216 
   217     if (Android_Window) {
   218         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   219 
   220         /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
   221          * will fall back to the old mode */
   222         SDL_VideoDisplay *display = SDL_GetDisplayForWindow(Android_Window);
   223 
   224         display->current_mode.format = format;
   225         display->current_mode.w = width;
   226         display->current_mode.h = height;
   227         display->current_mode.refresh_rate = rate;
   228     }
   229 }
   230 
   231 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   232 
   233 /* vi: set ts=4 sw=4 expandtab: */