src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 02 Jun 2014 09:01:26 -0700
changeset 8799 e2fd743d84ea
parent 8149 681eb46b8ac4
child 9173 c677ab1148c5
permissions -rw-r--r--
Added a way to get the native Android window and EGL context
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_androidclipboard.h"
    37 #include "SDL_androidevents.h"
    38 #include "SDL_androidkeyboard.h"
    39 #include "SDL_androidtouch.h"
    40 #include "SDL_androidwindow.h"
    41 
    42 #define ANDROID_VID_DRIVER_NAME "Android"
    43 
    44 /* Initialization/Query functions */
    45 static int Android_VideoInit(_THIS);
    46 static void Android_VideoQuit(_THIS);
    47 
    48 #include "../SDL_egl_c.h"
    49 /* GL functions (SDL_androidgl.c) */
    50 extern SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window * window);
    51 extern int Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
    52 extern void Android_GLES_SwapWindow(_THIS, SDL_Window * window);
    53 extern int Android_GLES_LoadLibrary(_THIS, const char *path);
    54 #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
    55 #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
    56 #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
    57 #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
    58 #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
    59 
    60 /* Android driver bootstrap functions */
    61 
    62 
    63 /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
    64 int Android_ScreenWidth = 0;
    65 int Android_ScreenHeight = 0;
    66 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    67 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    68 
    69 /* Currently only one window */
    70 SDL_Window *Android_Window = NULL;
    71 
    72 static int
    73 Android_Available(void)
    74 {
    75     return 1;
    76 }
    77 
    78 static void
    79 Android_DeleteDevice(SDL_VideoDevice * device)
    80 {
    81     SDL_free(device);
    82 }
    83 
    84 static SDL_VideoDevice *
    85 Android_CreateDevice(int devindex)
    86 {
    87     SDL_VideoDevice *device;
    88     SDL_VideoData *data;
    89 
    90     /* Initialize all variables that we clean on shutdown */
    91     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    92     if (!device) {
    93         SDL_OutOfMemory();
    94         return NULL;
    95     }
    96 
    97     data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
    98     if (!data) {
    99         SDL_OutOfMemory();
   100         SDL_free(device);
   101         return NULL;
   102     }
   103 
   104     device->driverdata = data;
   105 
   106     /* Set the function pointers */
   107     device->VideoInit = Android_VideoInit;
   108     device->VideoQuit = Android_VideoQuit;
   109     device->PumpEvents = Android_PumpEvents;
   110 
   111     device->CreateWindow = Android_CreateWindow;
   112     device->SetWindowTitle = Android_SetWindowTitle;
   113     device->DestroyWindow = Android_DestroyWindow;
   114     device->GetWindowWMInfo = Android_GetWindowWMInfo;
   115 
   116     device->free = Android_DeleteDevice;
   117 
   118     /* GL pointers */
   119     device->GL_LoadLibrary = Android_GLES_LoadLibrary;
   120     device->GL_GetProcAddress = Android_GLES_GetProcAddress;
   121     device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
   122     device->GL_CreateContext = Android_GLES_CreateContext;
   123     device->GL_MakeCurrent = Android_GLES_MakeCurrent;
   124     device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
   125     device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
   126     device->GL_SwapWindow = Android_GLES_SwapWindow;
   127     device->GL_DeleteContext = Android_GLES_DeleteContext;
   128 
   129     /* Text input */
   130     device->StartTextInput = Android_StartTextInput;
   131     device->StopTextInput = Android_StopTextInput;
   132     device->SetTextInputRect = Android_SetTextInputRect;
   133 
   134     /* Screen keyboard */
   135     device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
   136     device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
   137 
   138     /* Clipboard */
   139     device->SetClipboardText = Android_SetClipboardText;
   140     device->GetClipboardText = Android_GetClipboardText;
   141     device->HasClipboardText = Android_HasClipboardText;
   142 
   143     return device;
   144 }
   145 
   146 VideoBootStrap Android_bootstrap = {
   147     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   148     Android_Available, Android_CreateDevice
   149 };
   150 
   151 
   152 int
   153 Android_VideoInit(_THIS)
   154 {
   155     SDL_DisplayMode mode;
   156 
   157     mode.format = Android_ScreenFormat;
   158     mode.w = Android_ScreenWidth;
   159     mode.h = Android_ScreenHeight;
   160     mode.refresh_rate = 0;
   161     mode.driverdata = NULL;
   162     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   163         return -1;
   164     }
   165 
   166     SDL_AddDisplayMode(&_this->displays[0], &mode);
   167 
   168     Android_InitKeyboard();
   169 
   170     Android_InitTouch();
   171 
   172     /* We're done! */
   173     return 0;
   174 }
   175 
   176 void
   177 Android_VideoQuit(_THIS)
   178 {
   179 }
   180 
   181 /* This function gets called before VideoInit() */
   182 void
   183 Android_SetScreenResolution(int width, int height, Uint32 format)
   184 {
   185     Android_ScreenWidth = width;
   186     Android_ScreenHeight = height;
   187     Android_ScreenFormat = format;
   188 
   189     if (Android_Window) {
   190         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   191     }
   192 }
   193 
   194 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   195 
   196 /* vi: set ts=4 sw=4 expandtab: */