SDL  2.0
SDL_androidvideo.c
Go to the documentation of this file.
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 #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 
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_ScreenWidth = 0;
63 int Android_ScreenHeight = 0;
65 static int Android_ScreenRate = 0;
66 
68 
69 /* Currently only one window */
71 
72 static int
73 Android_Available(void)
74 {
75  return 1;
76 }
77 
78 static void
79 Android_SuspendScreenSaver(_THIS)
80 {
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 {
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 
122 
123  device->free = Android_DeleteDevice;
124 
125  /* GL pointers */
127  device->GL_GetProcAddress = Android_GLES_GetProcAddress;
128  device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
131  device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
132  device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
134  device->GL_DeleteContext = Android_GLES_DeleteContext;
135 
136 #if SDL_VIDEO_VULKAN
137  device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
138  device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
139  device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
140  device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
141 #endif
142 
143  /* Screensaver */
144  device->SuspendScreenSaver = Android_SuspendScreenSaver;
145 
146  /* Text input */
150 
151  /* Screen keyboard */
154 
155  /* Clipboard */
159 
160  return device;
161 }
162 
164  ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
165  Android_Available, Android_CreateDevice
166 };
167 
168 
169 int
170 Android_VideoInit(_THIS)
171 {
173 
175  mode.w = Android_ScreenWidth;
176  mode.h = Android_ScreenHeight;
177  mode.refresh_rate = Android_ScreenRate;
178  mode.driverdata = NULL;
179  if (SDL_AddBasicVideoDisplay(&mode) < 0) {
180  return -1;
181  }
182 
183  SDL_AddDisplayMode(&_this->displays[0], &mode);
184 
186 
188 
190 
191  /* We're done! */
192  return 0;
193 }
194 
195 void
196 Android_VideoQuit(_THIS)
197 {
199 }
200 
201 void
203 {
205  SDL_VideoDisplay *display;
209  Android_ScreenRate = rate;
210 
211  /*
212  Update the resolution of the desktop mode, so that the window
213  can be properly resized. The screen resolution change can for
214  example happen when the Activity enters or exists immersive mode,
215  which can happen after VideoInit().
216  */
217  device = SDL_GetVideoDevice();
218  if (device && device->num_displays > 0)
219  {
220  display = &device->displays[0];
224  display->desktop_mode.refresh_rate = Android_ScreenRate;
225  }
226 
227  if (Android_Window) {
228  SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
229 
230  /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
231  * will fall back to the old mode */
232  display = SDL_GetDisplayForWindow(Android_Window);
233 
234  display->current_mode.format = format;
235  display->current_mode.w = width;
236  display->current_mode.h = height;
237  display->current_mode.refresh_rate = rate;
238  }
239 }
240 
241 #endif /* SDL_VIDEO_DRIVER_ANDROID */
242 
243 /* vi: set ts=4 sw=4 expandtab: */
int(* Vulkan_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:270
void Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
int Android_GLES_SwapWindow(_THIS, SDL_Window *window)
int Android_GLES_LoadLibrary(_THIS, const char *path)
void Android_DestroyWindow(_THIS, SDL_Window *window)
SDL_bool Android_HasScreenKeyboardSupport(_THIS)
SDL_bool(* IsScreenKeyboardShown)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:294
char * Android_GetClipboardText(_THIS)
void(* free)(_THIS)
Definition: SDL_sysvideo.h:390
int Android_ScreenHeight
int SDL_AddBasicVideoDisplay(const SDL_DisplayMode *desktop_mode)
Definition: SDL_video.c:592
void Android_InitMouse(void)
int(* GL_SetSwapInterval)(_THIS, int interval)
Definition: SDL_sysvideo.h:260
The structure that defines a display mode.
Definition: SDL_video.h:53
void(* StartTextInput)(_THIS)
Definition: SDL_sysvideo.h:286
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
void Android_InitKeyboard(void)
void Android_InitTouch(void)
int(* GL_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:254
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_sem * Android_PauseSem
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
static SDL_AudioDeviceID device
Definition: loopwave.c:37
SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window *window)
void(* SetTextInputRect)(_THIS, SDL_Rect *rect)
Definition: SDL_sysvideo.h:288
SDL_bool(* Vulkan_GetInstanceExtensions)(_THIS, SDL_Window *window, unsigned *count, const char **names)
Definition: SDL_sysvideo.h:272
VideoBootStrap Android_bootstrap
void * SDL_calloc(size_t nmemb, size_t size)
SDL_bool(* GetWindowWMInfo)(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
Definition: SDL_sysvideo.h:247
int Android_SetClipboardText(_THIS, const char *text)
SDL_GLContext(* GL_CreateContext)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:257
SDL_bool(* Vulkan_CreateSurface)(_THIS, SDL_Window *window, VkInstance instance, VkSurfaceKHR *surface)
Definition: SDL_sysvideo.h:273
#define _THIS
int(* GL_MakeCurrent)(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: SDL_sysvideo.h:258
void SDL_free(void *mem)
void * driverdata
Definition: SDL_video.h:59
void(* Vulkan_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:271
Uint32 Android_ScreenFormat
SDL_DisplayMode current_mode
Definition: SDL_sysvideo.h:132
SDL_Window * Android_Window
GLenum mode
SDL_VideoDisplay * displays
Definition: SDL_sysvideo.h:312
void(* DestroyWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:234
void Android_PumpEvents(_THIS)
void(* StopTextInput)(_THIS)
Definition: SDL_sysvideo.h:287
void Android_StartTextInput(_THIS)
void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
int Android_ScreenWidth
SDL_bool(* HasScreenKeyboardSupport)(_THIS)
Definition: SDL_sysvideo.h:291
void Android_SetTextInputRect(_THIS, SDL_Rect *rect)
void(* GL_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:256
#define NULL
Definition: begin_code.h:164
int(* CreateSDLWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:210
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_DisplayMode desktop_mode
Definition: SDL_sysvideo.h:131
void Android_SetWindowTitle(_THIS, SDL_Window *window)
void(* VideoQuit)(_THIS)
Definition: SDL_sysvideo.h:166
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_sem * Android_ResumeSem
SDL_VideoDisplay * SDL_GetDisplayForWindow(SDL_Window *window)
Definition: SDL_video.c:1073
int(* GL_SwapWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:262
The type used to identify a window.
Definition: SDL_sysvideo.h:73
int Android_CreateWindow(_THIS, SDL_Window *window)
void Android_StopTextInput(_THIS)
SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
SDL_bool(* HasClipboardText)(_THIS)
Definition: SDL_sysvideo.h:299
SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
Definition: SDL_video.c:743
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:586
int(* VideoInit)(_THIS)
Definition: SDL_sysvideo.h:160
SDL_bool suspend_screensaver
Definition: SDL_sysvideo.h:310
void(* GL_DeleteContext)(_THIS, SDL_GLContext context)
Definition: SDL_sysvideo.h:263
char *(* GetClipboardText)(_THIS)
Definition: SDL_sysvideo.h:298
Uint32 format
Definition: SDL_video.h:55
void(* SetWindowTitle)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:212
int(* GL_GetSwapInterval)(_THIS)
Definition: SDL_sysvideo.h:261
int(* SetClipboardText)(_THIS, const char *text)
Definition: SDL_sysvideo.h:297
SDL_bool Android_HasClipboardText(_THIS)
void(* SuspendScreenSaver)(_THIS)
Definition: SDL_sysvideo.h:283
SDL_bool Android_IsScreenKeyboardShown(_THIS, SDL_Window *window)
void *(* GL_GetProcAddress)(_THIS, const char *proc)
Definition: SDL_sysvideo.h:255
void Android_QuitTouch(void)
void(* PumpEvents)(_THIS)
Definition: SDL_sysvideo.h:280
int Android_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)