SDL  2.0
SDL_androidvideo.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 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 #include "SDL_video.h"
28 #include "SDL_mouse.h"
29 #include "SDL_hints.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 static int Android_DeviceWidth = 0;
66 static int Android_DeviceHeight = 0;
67 static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
68 static int Android_ScreenRate = 0;
69 SDL_sem *Android_PauseSem = NULL;
70 SDL_sem *Android_ResumeSem = NULL;
72 
73 static int
74 Android_Available(void)
75 {
76  return 1;
77 }
78 
79 static void
80 Android_SuspendScreenSaver(_THIS)
81 {
83 }
84 
85 static void
86 Android_DeleteDevice(SDL_VideoDevice *device)
87 {
88  SDL_free(device->driverdata);
90 }
91 
92 static SDL_VideoDevice *
93 Android_CreateDevice(int devindex)
94 {
97  SDL_bool block_on_pause;
98 
99  /* Initialize all variables that we clean on shutdown */
101  if (!device) {
102  SDL_OutOfMemory();
103  return NULL;
104  }
105 
106  data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
107  if (!data) {
108  SDL_OutOfMemory();
109  SDL_free(device);
110  return NULL;
111  }
112 
113  device->driverdata = data;
114 
115  /* Set the function pointers */
116  device->VideoInit = Android_VideoInit;
117  device->VideoQuit = Android_VideoQuit;
119  if (block_on_pause) {
120  device->PumpEvents = Android_PumpEvents_Blocking;
121  } else {
123  }
124 
125  device->GetDisplayDPI = Android_GetDisplayDPI;
126 
127  device->CreateSDLWindow = Android_CreateWindow;
128  device->SetWindowTitle = Android_SetWindowTitle;
129  device->SetWindowFullscreen = Android_SetWindowFullscreen;
130  device->MinimizeWindow = Android_MinimizeWindow;
131  device->DestroyWindow = Android_DestroyWindow;
132  device->GetWindowWMInfo = Android_GetWindowWMInfo;
133 
134  device->free = Android_DeleteDevice;
135 
136  /* GL pointers */
137  device->GL_LoadLibrary = Android_GLES_LoadLibrary;
138  device->GL_GetProcAddress = Android_GLES_GetProcAddress;
139  device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
140  device->GL_CreateContext = Android_GLES_CreateContext;
141  device->GL_MakeCurrent = Android_GLES_MakeCurrent;
142  device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
143  device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
144  device->GL_SwapWindow = Android_GLES_SwapWindow;
145  device->GL_DeleteContext = Android_GLES_DeleteContext;
146 
147 #if SDL_VIDEO_VULKAN
148  device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
149  device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
150  device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
151  device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
152 #endif
153 
154  /* Screensaver */
155  device->SuspendScreenSaver = Android_SuspendScreenSaver;
156 
157  /* Text input */
158  device->StartTextInput = Android_StartTextInput;
159  device->StopTextInput = Android_StopTextInput;
160  device->SetTextInputRect = Android_SetTextInputRect;
161 
162  /* Screen keyboard */
163  device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
164  device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
165 
166  /* Clipboard */
167  device->SetClipboardText = Android_SetClipboardText;
168  device->GetClipboardText = Android_GetClipboardText;
169  device->HasClipboardText = Android_HasClipboardText;
170 
171  return device;
172 }
173 
175  ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
176  Android_Available, Android_CreateDevice
177 };
178 
179 
180 int
181 Android_VideoInit(_THIS)
182 {
183  SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
184  int display_index;
185  SDL_VideoDisplay *display;
187 
188  videodata->isPaused = SDL_FALSE;
189  videodata->isPausing = SDL_FALSE;
190 
191  mode.format = Android_ScreenFormat;
192  mode.w = Android_DeviceWidth;
193  mode.h = Android_DeviceHeight;
194  mode.refresh_rate = Android_ScreenRate;
195  mode.driverdata = NULL;
196 
197  display_index = SDL_AddBasicVideoDisplay(&mode);
198  if (display_index < 0) {
199  return -1;
200  }
201  display = SDL_GetDisplay(display_index);
202  display->orientation = Android_JNI_GetDisplayOrientation();
203 
205 
207 
209 
211 
212  /* We're done! */
213  return 0;
214 }
215 
216 void
217 Android_VideoQuit(_THIS)
218 {
221 }
222 
223 int
224 Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
225 {
226  return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
227 }
228 
229 void
230 Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
231 {
232  Android_SurfaceWidth = surfaceWidth;
233  Android_SurfaceHeight = surfaceHeight;
234  Android_DeviceWidth = deviceWidth;
235  Android_DeviceHeight = deviceHeight;
236  Android_ScreenFormat = format;
237  Android_ScreenRate = (int)rate;
238 }
239 
241 {
242  /*
243  Update the resolution of the desktop mode, so that the window
244  can be properly resized. The screen resolution change can for
245  example happen when the Activity enters or exits immersive mode,
246  which can happen after VideoInit().
247  */
249  if (device && device->num_displays > 0)
250  {
251  SDL_VideoDisplay *display = &device->displays[0];
252  display->desktop_mode.format = Android_ScreenFormat;
253  display->desktop_mode.w = Android_DeviceWidth;
254  display->desktop_mode.h = Android_DeviceHeight;
255  display->desktop_mode.refresh_rate = Android_ScreenRate;
256  }
257 
258  if (window) {
259  /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
260  * will fall back to the old mode */
262  display->display_modes[0].format = Android_ScreenFormat;
263  display->display_modes[0].w = Android_DeviceWidth;
264  display->display_modes[0].h = Android_DeviceHeight;
265  display->display_modes[0].refresh_rate = Android_ScreenRate;
266  display->current_mode = display->display_modes[0];
267 
269  }
270 }
271 
272 #endif /* SDL_VIDEO_DRIVER_ANDROID */
273 
274 /* vi: set ts=4 sw=4 expandtab: */
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1571
SDL_VideoDisplay::display_modes
SDL_DisplayMode * display_modes
Definition: SDL_sysvideo.h:130
Android_InitKeyboard
void Android_InitKeyboard(void)
SDL_DisplayMode::format
Uint32 format
Definition: SDL_video.h:55
SDL_androidmouse.h
SDL_VideoDevice::driverdata
void * driverdata
Definition: SDL_sysvideo.h:381
SDL_androidevents.h
Android_ActivityMutex
SDL_mutex * Android_ActivityMutex
Android_JNI_GetDisplayOrientation
SDL_DisplayOrientation Android_JNI_GetDisplayOrientation(void)
SDL_mouse.h
NULL
#define NULL
Definition: begin_code.h:167
mode
GLenum mode
Definition: SDL_opengl_glext.h:1122
Android_GetClipboardText
char * Android_GetClipboardText(_THIS)
SDL_mutex
Definition: SDL_sysmutex.c:29
Android_GLES_MakeCurrent
int Android_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
SDL_GetDisplayForWindow
SDL_VideoDisplay * SDL_GetDisplayForWindow(SDL_Window *window)
Definition: SDL_video.c:1089
Android_GLES_SwapWindow
int Android_GLES_SwapWindow(_THIS, SDL_Window *window)
SDL_androidvideo.h
SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:155
SDL_androidvulkan.h
SDL_VideoDisplay::desktop_mode
SDL_DisplayMode desktop_mode
Definition: SDL_sysvideo.h:131
Android_bootstrap
VideoBootStrap Android_bootstrap
Android_InitMouse
void Android_InitMouse(void)
Android_SetWindowTitle
void Android_SetWindowTitle(_THIS, SDL_Window *window)
SDL_DisplayMode::h
int h
Definition: SDL_video.h:57
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1973
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_DisplayMode
The structure that defines a display mode.
Definition: SDL_video.h:53
SDL_AddDisplayMode
SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
Definition: SDL_video.c:751
Android_DestroyWindow
void Android_DestroyWindow(_THIS, SDL_Window *window)
Android_SurfaceHeight
int Android_SurfaceHeight
SDL_GetHintBoolean
#define SDL_GetHintBoolean
Definition: SDL_dynapi_overrides.h:608
SDL_androidwindow.h
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
Android_PumpEvents_NonBlocking
void Android_PumpEvents_NonBlocking(_THIS)
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
Android_SendResize
void Android_SendResize(SDL_Window *window)
Android_SetTextInputRect
void Android_SetTextInputRect(_THIS, SDL_Rect *rect)
SDL_DisplayMode::refresh_rate
int refresh_rate
Definition: SDL_video.h:58
SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_VideoDevice::displays
SDL_VideoDisplay * displays
Definition: SDL_sysvideo.h:316
Android_SetWindowFullscreen
void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
Android_HasScreenKeyboardSupport
SDL_bool Android_HasScreenKeyboardSupport(_THIS)
SDL_GetDisplay
SDL_VideoDisplay * SDL_GetDisplay(int displayIndex)
Definition: SDL_video.c:1021
Android_StopTextInput
void Android_StopTextInput(_THIS)
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_VideoData::isPausing
int isPausing
Definition: SDL_androidvideo.h:40
Android_SurfaceWidth
int Android_SurfaceWidth
SDL_DisplayMode::w
int w
Definition: SDL_video.h:56
Android_QuitTouch
void Android_QuitTouch(void)
SDL_HINT_ANDROID_BLOCK_ON_PAUSE
#define SDL_HINT_ANDROID_BLOCK_ON_PAUSE
A variable to control whether the event loop will block itself when the app is paused.
Definition: SDL_hints.h:885
SDL_VideoData::isPaused
int isPaused
Definition: SDL_androidvideo.h:39
Android_PauseSem
SDL_sem * Android_PauseSem
SDL_VideoDevice
Definition: SDL_sysvideo.h:148
Android_GLES_CreateContext
SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window *window)
Android_CreateWindow
int Android_CreateWindow(_THIS, SDL_Window *window)
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_androidtouch.h
Android_SetScreenResolution
void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
Android_PumpEvents_Blocking
void Android_PumpEvents_Blocking(_THIS)
SDL_AddBasicVideoDisplay
int SDL_AddBasicVideoDisplay(const SDL_DisplayMode *desktop_mode)
Definition: SDL_video.c:589
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
Android_JNI_SuspendScreenSaver
void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
SDL_VideoDisplay
Definition: SDL_sysvideo.h:125
SDL_TRUE
Definition: SDL_stdinc.h:164
Android_GetWindowWMInfo
SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
SDL_hints.h
SDL_PIXELFORMAT_UNKNOWN
Definition: SDL_pixels.h:173
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
SDL_androidclipboard.h
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
Android_InitTouch
void Android_InitTouch(void)
Android_SetClipboardText
int Android_SetClipboardText(_THIS, const char *text)
SDL_androidkeyboard.h
SDL_video.h
SDL_GetVideoDevice
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:583
Android_GLES_LoadLibrary
int Android_GLES_LoadLibrary(_THIS, const char *path)
SDL_VideoDisplay::current_mode
SDL_DisplayMode current_mode
Definition: SDL_sysvideo.h:132
SDL_androidgl.h
Android_MinimizeWindow
void Android_MinimizeWindow(_THIS, SDL_Window *window)
Android_StartTextInput
void Android_StartTextInput(_THIS)
VideoBootStrap
Definition: SDL_sysvideo.h:397
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
Android_QuitMouse
void Android_QuitMouse(void)
SDL_VideoDevice::suspend_screensaver
SDL_bool suspend_screensaver
Definition: SDL_sysvideo.h:314
Android_IsScreenKeyboardShown
SDL_bool Android_IsScreenKeyboardShown(_THIS, SDL_Window *window)
Android_JNI_GetDisplayDPI
int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
SDL_VideoData
Definition: SDL_androidvideo.h:36
Android_HasClipboardText
SDL_bool Android_HasClipboardText(_THIS)
Android_ResumeSem
SDL_sem * Android_ResumeSem