SDL_androidvideo.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_ANDROID
/* Android SDL video driver implementation
*/
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_windowevents_c.h"
#include "SDL_androidvideo.h"
#include "SDL_androidgl.h"
#include "SDL_androidclipboard.h"
#include "SDL_androidevents.h"
#include "SDL_androidkeyboard.h"
#include "SDL_androidmouse.h"
#include "SDL_androidtouch.h"
#include "SDL_androidwindow.h"
#include "SDL_androidvulkan.h"
#define ANDROID_VID_DRIVER_NAME "Android"
/* Initialization/Query functions */
static int Android_VideoInit(_THIS);
static void Android_VideoQuit(_THIS);
int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
#include "../SDL_egl_c.h"
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
/* Android driver bootstrap functions */
/* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
int Android_ScreenWidth = 0;
int Android_ScreenHeight = 0;
Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
static int Android_ScreenRate = 0;
SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
/* Currently only one window */
SDL_Window *Android_Window = NULL;
static int
Android_Available(void)
{
return 1;
}
static void
Android_SuspendScreenSaver(_THIS)
{
Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
}
static void
Android_DeleteDevice(SDL_VideoDevice * device)
{
SDL_free(device->driverdata);
SDL_free(device);
}
static SDL_VideoDevice *
Android_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
SDL_VideoData *data;
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_OutOfMemory();
return NULL;
}
data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
if (!data) {
SDL_OutOfMemory();
SDL_free(device);
return NULL;
}
device->driverdata = data;
/* Set the function pointers */
device->VideoInit = Android_VideoInit;
device->VideoQuit = Android_VideoQuit;
device->PumpEvents = Android_PumpEvents;
device->GetDisplayDPI = Android_GetDisplayDPI;
device->CreateSDLWindow = Android_CreateWindow;
device->SetWindowTitle = Android_SetWindowTitle;
device->SetWindowFullscreen = Android_SetWindowFullscreen;
device->DestroyWindow = Android_DestroyWindow;
device->GetWindowWMInfo = Android_GetWindowWMInfo;
device->free = Android_DeleteDevice;
/* GL pointers */
device->GL_LoadLibrary = Android_GLES_LoadLibrary;
device->GL_GetProcAddress = Android_GLES_GetProcAddress;
device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
device->GL_CreateContext = Android_GLES_CreateContext;
device->GL_MakeCurrent = Android_GLES_MakeCurrent;
device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
device->GL_SwapWindow = Android_GLES_SwapWindow;
device->GL_DeleteContext = Android_GLES_DeleteContext;
#if SDL_VIDEO_VULKAN
device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
#endif
/* Screensaver */
device->SuspendScreenSaver = Android_SuspendScreenSaver;
/* Text input */
device->StartTextInput = Android_StartTextInput;
device->StopTextInput = Android_StopTextInput;
device->SetTextInputRect = Android_SetTextInputRect;
/* Screen keyboard */
device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
/* Clipboard */
device->SetClipboardText = Android_SetClipboardText;
device->GetClipboardText = Android_GetClipboardText;
device->HasClipboardText = Android_HasClipboardText;
return device;
}
VideoBootStrap Android_bootstrap = {
ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
Android_Available, Android_CreateDevice
};
int
Android_VideoInit(_THIS)
{
SDL_DisplayMode mode;
mode.format = Android_ScreenFormat;
mode.w = Android_ScreenWidth;
mode.h = Android_ScreenHeight;
mode.refresh_rate = Android_ScreenRate;
mode.driverdata = NULL;
if (SDL_AddBasicVideoDisplay(&mode) < 0) {
return -1;
}
SDL_AddDisplayMode(&_this->displays[0], &mode);
Android_InitKeyboard();
Android_InitTouch();
Android_InitMouse();
/* We're done! */
return 0;
}
void
Android_VideoQuit(_THIS)
{
Android_QuitTouch();
}
int
Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
{
return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
}
void
Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
{
SDL_VideoDevice* device;
SDL_VideoDisplay *display;
Android_ScreenWidth = width;
Android_ScreenHeight = height;
Android_ScreenFormat = format;
Android_ScreenRate = rate;
/*
Update the resolution of the desktop mode, so that the window
can be properly resized. The screen resolution change can for
example happen when the Activity enters or exits immersive mode,
which can happen after VideoInit().
*/
device = SDL_GetVideoDevice();
if (device && device->num_displays > 0)
{
display = &device->displays[0];
display->desktop_mode.format = Android_ScreenFormat;
display->desktop_mode.w = Android_ScreenWidth;
display->desktop_mode.h = Android_ScreenHeight;
display->desktop_mode.refresh_rate = Android_ScreenRate;
}
if (Android_Window) {
/* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
* will fall back to the old mode */
display = SDL_GetDisplayForWindow(Android_Window);
display->display_modes[0].format = format;
display->display_modes[0].w = width;
display->display_modes[0].h = height;
display->display_modes[0].refresh_rate = rate;
display->current_mode = display->display_modes[0];
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
}
}
#endif /* SDL_VIDEO_DRIVER_ANDROID */
/* vi: set ts=4 sw=4 expandtab: */
SDL_VideoDevice::Vulkan_LoadLibrary
int(* Vulkan_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:270
Android_SetScreenResolution
void Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
Android_GLES_SwapWindow
int Android_GLES_SwapWindow(_THIS, SDL_Window *window)
Android_JNI_GetDisplayDPI
int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
SDL_VideoDevice::GetDisplayDPI
int(* GetDisplayDPI)(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
Definition: SDL_sysvideo.h:186
Android_GLES_LoadLibrary
int Android_GLES_LoadLibrary(_THIS, const char *path)
SDL_androidwindow.h
SDL_androidevents.h
Android_DestroyWindow
void Android_DestroyWindow(_THIS, SDL_Window *window)
Android_HasScreenKeyboardSupport
SDL_bool Android_HasScreenKeyboardSupport(_THIS)
SDL_DisplayMode::w
int w
Definition: SDL_video.h:56
SDL_VideoDevice::IsScreenKeyboardShown
SDL_bool(* IsScreenKeyboardShown)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:294
SDL_androidvulkan.h
Android_GetClipboardText
char * Android_GetClipboardText(_THIS)
SDL_VideoDevice::free
void(* free)(_THIS)
Definition: SDL_sysvideo.h:390
Android_ScreenHeight
int Android_ScreenHeight
SDL_AddBasicVideoDisplay
int SDL_AddBasicVideoDisplay(const SDL_DisplayMode *desktop_mode)
Definition: SDL_video.c:592
Android_InitMouse
void Android_InitMouse(void)
SDL_VideoDevice::driverdata
void * driverdata
Definition: SDL_sysvideo.h:377
SDL_VideoDevice::GL_SetSwapInterval
int(* GL_SetSwapInterval)(_THIS, int interval)
Definition: SDL_sysvideo.h:260
SDL_androidkeyboard.h
SDL_DisplayMode
The structure that defines a display mode.
Definition: SDL_video.h:53
SDL_VideoDevice::StartTextInput
void(* StartTextInput)(_THIS)
Definition: SDL_sysvideo.h:286
SDL_DisplayMode::refresh_rate
int refresh_rate
Definition: SDL_video.h:58
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:88
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
Android_InitKeyboard
void Android_InitKeyboard(void)
Android_InitTouch
void Android_InitTouch(void)
SDL_androidvideo.h
SDL_VideoDevice::GL_LoadLibrary
int(* GL_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:254
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Android_SetWindowFullscreen
void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
Android_PauseSem
SDL_sem * Android_PauseSem
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
Android_GLES_CreateContext
SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window *window)
SDL_VideoDevice::SetTextInputRect
void(* SetTextInputRect)(_THIS, SDL_Rect *rect)
Definition: SDL_sysvideo.h:288
SDL_video.h
SDL_VideoDevice::Vulkan_GetInstanceExtensions
SDL_bool(* Vulkan_GetInstanceExtensions)(_THIS, SDL_Window *window, unsigned *count, const char **names)
Definition: SDL_sysvideo.h:272
Android_bootstrap
VideoBootStrap Android_bootstrap
SDL_androidgl.h
SDL_VideoDevice::GetWindowWMInfo
SDL_bool(* GetWindowWMInfo)(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
Definition: SDL_sysvideo.h:247
Android_SetClipboardText
int Android_SetClipboardText(_THIS, const char *text)
SDL_VideoDevice::GL_CreateContext
SDL_GLContext(* GL_CreateContext)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:257
SDL_VideoDevice::Vulkan_CreateSurface
SDL_bool(* Vulkan_CreateSurface)(_THIS, SDL_Window *window, VkInstance instance, VkSurfaceKHR *surface)
Definition: SDL_sysvideo.h:273
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_VideoDevice::GL_MakeCurrent
int(* GL_MakeCurrent)(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: SDL_sysvideo.h:258
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_DisplayMode::driverdata
void * driverdata
Definition: SDL_video.h:59
SDL_mouse.h
VideoBootStrap
Definition: SDL_sysvideo.h:393
SDL_VideoDevice::num_displays
int num_displays
Definition: SDL_sysvideo.h:311
SDL_VideoDevice::Vulkan_UnloadLibrary
void(* Vulkan_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:271
SDL_VideoDisplay::display_modes
SDL_DisplayMode * display_modes
Definition: SDL_sysvideo.h:130
Android_ScreenFormat
Uint32 Android_ScreenFormat
SDL_PIXELFORMAT_UNKNOWN
Definition: SDL_pixels.h:173
SDL_VideoDisplay::current_mode
SDL_DisplayMode current_mode
Definition: SDL_sysvideo.h:132
Android_Window
SDL_Window * Android_Window
mode
GLenum mode
Definition: SDL_opengl_glext.h:1122
SDL_VideoDevice::displays
SDL_VideoDisplay * displays
Definition: SDL_sysvideo.h:312
SDL_VideoDevice::DestroyWindow
void(* DestroyWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:234
Android_PumpEvents
void Android_PumpEvents(_THIS)
SDL_VideoDevice::StopTextInput
void(* StopTextInput)(_THIS)
Definition: SDL_sysvideo.h:287
SDL_DisplayMode::h
int h
Definition: SDL_video.h:57
Android_StartTextInput
void Android_StartTextInput(_THIS)
Android_JNI_SuspendScreenSaver
void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
Android_ScreenWidth
int Android_ScreenWidth
SDL_VideoDevice::HasScreenKeyboardSupport
SDL_bool(* HasScreenKeyboardSupport)(_THIS)
Definition: SDL_sysvideo.h:291
SDL_VideoDisplay
Definition: SDL_sysvideo.h:125
Android_SetTextInputRect
void Android_SetTextInputRect(_THIS, SDL_Rect *rect)
SDL_VideoDevice::GL_UnloadLibrary
void(* GL_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:256
NULL
#define NULL
Definition: begin_code.h:164
SDL_VideoDevice::CreateSDLWindow
int(* CreateSDLWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:210
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_VideoDisplay::desktop_mode
SDL_DisplayMode desktop_mode
Definition: SDL_sysvideo.h:131
Android_SetWindowTitle
void Android_SetWindowTitle(_THIS, SDL_Window *window)
SDL_VideoDevice::VideoQuit
void(* VideoQuit)(_THIS)
Definition: SDL_sysvideo.h:166
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
Android_ResumeSem
SDL_sem * Android_ResumeSem
SDL_GetDisplayForWindow
SDL_VideoDisplay * SDL_GetDisplayForWindow(SDL_Window *window)
Definition: SDL_video.c:1073
SDL_VideoDevice::GL_SwapWindow
int(* GL_SwapWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:262
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
Android_CreateWindow
int Android_CreateWindow(_THIS, SDL_Window *window)
Android_StopTextInput
void Android_StopTextInput(_THIS)
Android_GetWindowWMInfo
SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
SDL_VideoDevice
Definition: SDL_sysvideo.h:147
SDL_VideoData
Definition: SDL_androidvideo.h:35
SDL_VideoDevice::HasClipboardText
SDL_bool(* HasClipboardText)(_THIS)
Definition: SDL_sysvideo.h:299
SDL_AddDisplayMode
SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
Definition: SDL_video.c:743
SDL_GetVideoDevice
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:586
SDL_VideoDevice::VideoInit
int(* VideoInit)(_THIS)
Definition: SDL_sysvideo.h:160
SDL_VideoDevice::suspend_screensaver
SDL_bool suspend_screensaver
Definition: SDL_sysvideo.h:310
SDL_androidclipboard.h
SDL_VideoDevice::SetWindowFullscreen
void(* SetWindowFullscreen)(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
Definition: SDL_sysvideo.h:230
SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:155
SDL_VideoDevice::GL_DeleteContext
void(* GL_DeleteContext)(_THIS, SDL_GLContext context)
Definition: SDL_sysvideo.h:263
SDL_VideoDevice::GetClipboardText
char *(* GetClipboardText)(_THIS)
Definition: SDL_sysvideo.h:298
SDL_DisplayMode::format
Uint32 format
Definition: SDL_video.h:55
SDL_VideoDevice::SetWindowTitle
void(* SetWindowTitle)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:212
SDL_VideoDevice::GL_GetSwapInterval
int(* GL_GetSwapInterval)(_THIS)
Definition: SDL_sysvideo.h:261
SDL_VideoDevice::SetClipboardText
int(* SetClipboardText)(_THIS, const char *text)
Definition: SDL_sysvideo.h:297
Android_HasClipboardText
SDL_bool Android_HasClipboardText(_THIS)
SDL_androidtouch.h
SDL_androidmouse.h
SDL_VideoDevice::SuspendScreenSaver
void(* SuspendScreenSaver)(_THIS)
Definition: SDL_sysvideo.h:283
Android_IsScreenKeyboardShown
SDL_bool Android_IsScreenKeyboardShown(_THIS, SDL_Window *window)
SDL_VideoDevice::GL_GetProcAddress
void *(* GL_GetProcAddress)(_THIS, const char *proc)
Definition: SDL_sysvideo.h:255
Android_QuitTouch
void Android_QuitTouch(void)
SDL_VideoDevice::PumpEvents
void(* PumpEvents)(_THIS)
Definition: SDL_sysvideo.h:280
Android_GLES_MakeCurrent
int Android_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)