2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2023-01-09 09:41:41 -08:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2022-11-29 18:34:15 -08:00
|
|
|
#include "SDL_internal.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-03-29 21:49:01 +00:00
|
|
|
#ifdef SDL_VIDEO_DRIVER_ANDROID
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2019-01-03 13:14:16 +01:00
|
|
|
/* Android SDL video driver implementation */
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
#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"
|
2016-12-02 02:25:12 -08:00
|
|
|
#include "SDL_androidgl.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
#include "SDL_androidclipboard.h"
|
|
|
|
|
#include "SDL_androidevents.h"
|
|
|
|
|
#include "SDL_androidkeyboard.h"
|
2016-01-12 22:23:00 +01:00
|
|
|
#include "SDL_androidmouse.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
#include "SDL_androidtouch.h"
|
|
|
|
|
#include "SDL_androidwindow.h"
|
2017-08-27 22:15:57 -04:00
|
|
|
#include "SDL_androidvulkan.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
#define ANDROID_VID_DRIVER_NAME "Android"
|
|
|
|
|
|
|
|
|
|
/* Initialization/Query functions */
|
|
|
|
|
static int Android_VideoInit(_THIS);
|
|
|
|
|
static void Android_VideoQuit(_THIS);
|
|
|
|
|
|
|
|
|
|
#include "../SDL_egl_c.h"
|
2022-11-30 12:51:59 -08:00
|
|
|
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddressInternal
|
|
|
|
|
#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
|
2015-06-21 17:33:46 +02:00
|
|
|
#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
|
|
|
|
|
#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
|
2022-11-30 12:51:59 -08:00
|
|
|
#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
/* Android driver bootstrap functions */
|
|
|
|
|
|
|
|
|
|
/* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
|
2022-11-30 12:51:59 -08:00
|
|
|
int Android_SurfaceWidth = 0;
|
|
|
|
|
int Android_SurfaceHeight = 0;
|
|
|
|
|
static int Android_DeviceWidth = 0;
|
|
|
|
|
static int Android_DeviceHeight = 0;
|
2021-04-22 18:06:17 +02:00
|
|
|
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; /* Default SurfaceView format, in case this is queried before being filled */
|
2023-01-25 10:22:43 -08:00
|
|
|
float Android_ScreenDensity = 1.0f;
|
2023-01-02 15:47:19 -08:00
|
|
|
static float Android_ScreenRate = 0.0f;
|
2023-04-28 07:31:12 -07:00
|
|
|
SDL_Semaphore *Android_PauseSem = NULL;
|
|
|
|
|
SDL_Semaphore *Android_ResumeSem = NULL;
|
|
|
|
|
SDL_Mutex *Android_ActivityMutex = NULL;
|
2023-03-07 00:01:34 -08:00
|
|
|
static SDL_SystemTheme Android_SystemTheme;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-02-09 15:34:43 +01:00
|
|
|
static int Android_SuspendScreenSaver(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-02-09 15:34:43 +01:00
|
|
|
return Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void Android_DeleteDevice(SDL_VideoDevice *device)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
SDL_free(device->driverdata);
|
|
|
|
|
SDL_free(device);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static SDL_VideoDevice *Android_CreateDevice(void)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
SDL_VideoDevice *device;
|
|
|
|
|
SDL_VideoData *data;
|
2019-04-05 09:16:30 +02:00
|
|
|
SDL_bool block_on_pause;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
/* Initialize all variables that we clean on shutdown */
|
2022-11-30 12:51:59 -08:00
|
|
|
device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
|
2022-11-27 17:38:43 +01:00
|
|
|
if (device == NULL) {
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_OutOfMemory();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
data = (SDL_VideoData *)SDL_calloc(1, sizeof(SDL_VideoData));
|
2022-11-27 17:38:43 +01:00
|
|
|
if (data == NULL) {
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_OutOfMemory();
|
|
|
|
|
SDL_free(device);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device->driverdata = data;
|
2023-03-07 00:01:34 -08:00
|
|
|
device->system_theme = Android_SystemTheme;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
/* Set the function pointers */
|
|
|
|
|
device->VideoInit = Android_VideoInit;
|
|
|
|
|
device->VideoQuit = Android_VideoQuit;
|
2019-04-05 09:16:30 +02:00
|
|
|
block_on_pause = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, SDL_TRUE);
|
|
|
|
|
if (block_on_pause) {
|
|
|
|
|
device->PumpEvents = Android_PumpEvents_Blocking;
|
|
|
|
|
} else {
|
|
|
|
|
device->PumpEvents = Android_PumpEvents_NonBlocking;
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2017-08-28 00:43:14 -07:00
|
|
|
device->CreateSDLWindow = Android_CreateWindow;
|
2015-06-21 17:33:46 +02:00
|
|
|
device->SetWindowTitle = Android_SetWindowTitle;
|
2018-02-11 18:23:37 -08:00
|
|
|
device->SetWindowFullscreen = Android_SetWindowFullscreen;
|
2019-06-10 21:58:03 +02:00
|
|
|
device->MinimizeWindow = Android_MinimizeWindow;
|
2022-03-23 10:13:56 +01:00
|
|
|
device->SetWindowResizable = Android_SetWindowResizable;
|
2015-06-21 17:33:46 +02:00
|
|
|
device->DestroyWindow = Android_DestroyWindow;
|
|
|
|
|
device->GetWindowWMInfo = Android_GetWindowWMInfo;
|
|
|
|
|
|
|
|
|
|
device->free = Android_DeleteDevice;
|
|
|
|
|
|
|
|
|
|
/* GL pointers */
|
2023-03-29 21:49:01 +00:00
|
|
|
#ifdef SDL_VIDEO_OPENGL_EGL
|
2015-06-21 17:33:46 +02:00
|
|
|
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;
|
2022-03-11 15:14:51 +01:00
|
|
|
#endif
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-03-29 21:49:01 +00:00
|
|
|
#ifdef SDL_VIDEO_VULKAN
|
2017-08-27 22:15:57 -04:00
|
|
|
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
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
/* 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",
|
2020-07-12 19:11:15 -04:00
|
|
|
Android_CreateDevice
|
2015-06-21 17:33:46 +02:00
|
|
|
};
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int Android_VideoInit(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-01-29 13:30:55 -08:00
|
|
|
SDL_VideoData *videodata = _this->driverdata;
|
|
|
|
|
SDL_DisplayID displayID;
|
2019-03-12 14:44:25 -07:00
|
|
|
SDL_VideoDisplay *display;
|
|
|
|
|
SDL_DisplayMode mode;
|
2019-01-16 10:31:51 +01:00
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
videodata->isPaused = SDL_FALSE;
|
2019-01-16 10:31:51 +01:00
|
|
|
videodata->isPausing = SDL_FALSE;
|
2020-09-25 10:14:42 +02:00
|
|
|
videodata->pauseAudio = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO, SDL_TRUE);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-01-25 03:36:35 -08:00
|
|
|
SDL_zero(mode);
|
2022-11-30 12:51:59 -08:00
|
|
|
mode.format = Android_ScreenFormat;
|
2023-01-27 10:46:51 -08:00
|
|
|
mode.pixel_w = Android_DeviceWidth;
|
|
|
|
|
mode.pixel_h = Android_DeviceHeight;
|
2023-01-25 10:22:43 -08:00
|
|
|
mode.display_scale = Android_ScreenDensity;
|
2022-11-30 12:51:59 -08:00
|
|
|
mode.refresh_rate = Android_ScreenRate;
|
|
|
|
|
mode.driverdata = NULL;
|
2019-01-17 11:05:05 +01:00
|
|
|
|
2023-01-29 13:30:55 -08:00
|
|
|
displayID = SDL_AddBasicVideoDisplay(&mode);
|
|
|
|
|
if (displayID == 0) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-29 13:30:55 -08:00
|
|
|
display = SDL_GetVideoDisplay(displayID);
|
2021-04-22 18:06:17 +02:00
|
|
|
display->orientation = Android_JNI_GetDisplayOrientation();
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
Android_InitTouch();
|
|
|
|
|
|
2016-01-12 22:23:00 +01:00
|
|
|
Android_InitMouse();
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
/* We're done! */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
void Android_VideoQuit(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2018-06-18 13:14:04 -07:00
|
|
|
Android_QuitMouse();
|
2015-06-21 17:33:46 +02:00
|
|
|
Android_QuitTouch();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 14:55:50 +01:00
|
|
|
void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-11-30 12:51:59 -08:00
|
|
|
Android_SurfaceWidth = surfaceWidth;
|
2018-06-07 17:07:03 -07:00
|
|
|
Android_SurfaceHeight = surfaceHeight;
|
2022-11-30 12:51:59 -08:00
|
|
|
Android_DeviceWidth = deviceWidth;
|
|
|
|
|
Android_DeviceHeight = deviceHeight;
|
2023-01-25 10:22:43 -08:00
|
|
|
Android_ScreenDensity = (density > 0.0f) ? density : 1.0f;
|
2023-01-02 15:47:19 -08:00
|
|
|
Android_ScreenRate = rate;
|
2019-01-17 11:05:05 +01:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static Uint32 format_to_pixelFormat(int format)
|
|
|
|
|
{
|
2021-04-22 18:06:17 +02:00
|
|
|
Uint32 pf;
|
2022-11-30 12:51:59 -08:00
|
|
|
if (format == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM) { /* 1 */
|
2021-04-22 18:06:17 +02:00
|
|
|
pf = SDL_PIXELFORMAT_RGBA8888;
|
|
|
|
|
} else if (format == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM) { /* 2 */
|
|
|
|
|
pf = SDL_PIXELFORMAT_RGBX8888;
|
|
|
|
|
} else if (format == AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM) { /* 3 */
|
|
|
|
|
pf = SDL_PIXELFORMAT_RGB24;
|
|
|
|
|
} else if (format == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM) { /* 4*/
|
|
|
|
|
pf = SDL_PIXELFORMAT_RGB565;
|
|
|
|
|
} else if (format == 5) {
|
|
|
|
|
pf = SDL_PIXELFORMAT_BGRA8888;
|
|
|
|
|
} else if (format == 6) {
|
|
|
|
|
pf = SDL_PIXELFORMAT_RGBA5551;
|
|
|
|
|
} else if (format == 7) {
|
|
|
|
|
pf = SDL_PIXELFORMAT_RGBA4444;
|
2022-09-03 23:40:14 +02:00
|
|
|
} else if (format == 0x115) {
|
|
|
|
|
/* HAL_PIXEL_FORMAT_BGR_565 */
|
|
|
|
|
pf = SDL_PIXELFORMAT_RGB565;
|
2021-04-22 18:06:17 +02:00
|
|
|
} else {
|
|
|
|
|
pf = SDL_PIXELFORMAT_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
return pf;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
void Android_SetFormat(int format_wanted, int format_got)
|
2021-04-22 18:06:17 +02:00
|
|
|
{
|
|
|
|
|
Uint32 pf_wanted;
|
|
|
|
|
Uint32 pf_got;
|
|
|
|
|
|
|
|
|
|
pf_wanted = format_to_pixelFormat(format_wanted);
|
|
|
|
|
pf_got = format_to_pixelFormat(format_got);
|
|
|
|
|
|
|
|
|
|
Android_ScreenFormat = pf_got;
|
|
|
|
|
|
|
|
|
|
SDL_Log("pixel format wanted %s (%d), got %s (%d)",
|
|
|
|
|
SDL_GetPixelFormatName(pf_wanted), format_wanted,
|
|
|
|
|
SDL_GetPixelFormatName(pf_got), format_got);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 11:05:05 +01:00
|
|
|
void Android_SendResize(SDL_Window *window)
|
|
|
|
|
{
|
2016-10-01 14:18:29 -07:00
|
|
|
/*
|
|
|
|
|
Update the resolution of the desktop mode, so that the window
|
|
|
|
|
can be properly resized. The screen resolution change can for
|
2018-02-11 18:23:37 -08:00
|
|
|
example happen when the Activity enters or exits immersive mode,
|
2016-10-01 14:18:29 -07:00
|
|
|
which can happen after VideoInit().
|
|
|
|
|
*/
|
2019-01-17 11:05:05 +01:00
|
|
|
SDL_VideoDevice *device = SDL_GetVideoDevice();
|
2022-11-27 17:38:43 +01:00
|
|
|
if (device && device->num_displays > 0) {
|
2022-11-30 12:51:59 -08:00
|
|
|
SDL_VideoDisplay *display = &device->displays[0];
|
2023-01-27 10:46:51 -08:00
|
|
|
SDL_DisplayMode desktop_mode;
|
|
|
|
|
|
|
|
|
|
SDL_zero(desktop_mode);
|
|
|
|
|
desktop_mode.format = Android_ScreenFormat;
|
|
|
|
|
desktop_mode.pixel_w = Android_DeviceWidth;
|
|
|
|
|
desktop_mode.pixel_h = Android_DeviceHeight;
|
|
|
|
|
desktop_mode.display_scale = Android_ScreenDensity;
|
|
|
|
|
desktop_mode.refresh_rate = Android_ScreenRate;
|
|
|
|
|
SDL_SetDesktopDisplayMode(display, &desktop_mode);
|
2016-10-01 14:18:29 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-03 13:14:16 +01:00
|
|
|
if (window) {
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-01-31 21:23:14 -08:00
|
|
|
int w = (int)SDL_floorf(Android_SurfaceWidth / Android_ScreenDensity);
|
|
|
|
|
int h = (int)SDL_floorf(Android_SurfaceHeight / Android_ScreenDensity);
|
2023-01-25 10:22:43 -08:00
|
|
|
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, w, h);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 00:01:34 -08:00
|
|
|
void Android_SetDarkMode(SDL_bool enabled)
|
|
|
|
|
{
|
|
|
|
|
SDL_VideoDevice *device = SDL_GetVideoDevice();
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
Android_SystemTheme = SDL_SYSTEM_THEME_DARK;
|
|
|
|
|
} else {
|
|
|
|
|
Android_SystemTheme = SDL_SYSTEM_THEME_LIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (device) {
|
|
|
|
|
SDL_SetSystemTheme(Android_SystemTheme);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
#endif /* SDL_VIDEO_DRIVER_ANDROID */
|