465 lines
11 KiB
C
Raw Normal View History

2014-08-20 22:09:30 -03:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2014-08-20 22:09:30 -03:00
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2014-08-20 22:09:30 -03:00
#ifdef HAVE_X11
#include <X11/Xlib.h>
#endif
#include "../../configuration.h"
2021-10-20 15:57:34 +02:00
#include "../../gfx/video_defines.h"
2021-11-12 11:20:52 +00:00
#include "../../gfx/video_driver.h"
2019-02-02 22:02:24 +01:00
#include "../../verbosity.h"
2017-12-31 14:31:51 +01:00
#include "SDL.h"
2019-09-22 12:40:40 +02:00
#ifdef HAVE_SDL2
#include "../common/sdl2_common.h"
#endif
#if defined(WEBOS) && defined(HAVE_SDL2)
#include <SDL_webOS.h>
#endif
2014-10-24 05:43:47 +02:00
typedef struct gfx_ctx_sdl_data
{
2020-07-17 20:35:11 +02:00
int width;
int height;
int new_width;
int new_height;
2014-10-24 05:43:47 +02:00
2020-07-17 20:35:11 +02:00
bool full;
bool resized;
bool subsystem_inited;
2014-08-20 22:09:30 -03:00
2014-10-24 05:43:47 +02:00
#ifdef HAVE_SDL2
2020-07-17 20:35:11 +02:00
SDL_Window *win;
SDL_GLContext ctx;
2014-10-24 05:43:47 +02:00
#else
2020-07-17 20:35:11 +02:00
SDL_Surface *win;
2014-10-24 05:43:47 +02:00
#endif
} gfx_ctx_sdl_data_t;
2014-08-20 22:09:30 -03:00
2020-07-17 16:46:00 +02:00
/* TODO/FIXME - static global */
static enum gfx_ctx_api sdl_api = GFX_CTX_OPENGL_API;
2014-10-24 05:43:47 +02:00
static void sdl_ctx_destroy_resources(gfx_ctx_sdl_data_t *sdl)
{
if (!sdl)
return;
2014-08-20 22:09:30 -03:00
#ifdef HAVE_SDL2
2020-07-17 20:35:11 +02:00
if (sdl->ctx)
SDL_GL_DeleteContext(sdl->ctx);
2014-10-24 05:43:47 +02:00
2020-07-17 20:35:11 +02:00
if (sdl->win)
SDL_DestroyWindow(sdl->win);
2014-10-24 05:43:47 +02:00
2020-07-17 20:35:11 +02:00
sdl->ctx = NULL;
2014-08-20 22:09:30 -03:00
#else
2020-07-17 20:35:11 +02:00
if (sdl->win)
SDL_FreeSurface(sdl->win);
2014-08-20 22:09:30 -03:00
#endif
2020-07-17 20:35:11 +02:00
sdl->win = NULL;
2014-10-24 05:43:47 +02:00
}
2014-08-20 22:09:30 -03:00
static void sdl_ctx_destroy(void *data)
{
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
if (!sdl)
return;
sdl_ctx_destroy_resources(sdl);
2022-01-12 10:12:34 +04:00
#ifndef WEBOS
if (sdl->subsystem_inited)
2022-01-12 10:12:34 +04:00
#endif
SDL_QuitSubSystem(SDL_INIT_VIDEO);
free(sdl);
}
2020-03-07 00:39:06 +01:00
static void *sdl_ctx_init(void *video_driver)
2014-08-20 22:09:30 -03:00
{
2021-03-23 12:46:25 +00:00
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)
2014-10-24 05:43:47 +02:00
calloc(1, sizeof(gfx_ctx_sdl_data_t));
2021-03-23 12:46:25 +00:00
uint32_t sdl_subsystem_flags = SDL_WasInit(0);
2014-10-24 05:43:47 +02:00
if (!sdl)
return NULL;
2014-08-20 22:09:30 -03:00
#ifdef HAVE_X11
XInitThreads();
#endif
#ifdef WEBOS
SDL_SetHint(SDL_HINT_WEBOS_ACCESS_POLICY_KEYS_BACK, "true");
SDL_SetHint(SDL_HINT_WEBOS_ACCESS_POLICY_KEYS_EXIT, "true");
SDL_SetHint(SDL_HINT_WEBOS_CURSOR_SLEEP_TIME, "1000");
#endif
2021-03-23 12:46:25 +00:00
/* Initialise graphics subsystem, if required */
if (sdl_subsystem_flags == 0)
2014-08-20 22:09:30 -03:00
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
goto error;
}
2021-03-23 12:46:25 +00:00
else if ((sdl_subsystem_flags & SDL_INIT_VIDEO) == 0)
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
goto error;
sdl->subsystem_inited = true;
2021-03-23 12:46:25 +00:00
}
2014-08-20 22:09:30 -03:00
RARCH_LOG("[SDL_GL] SDL %i.%i.%i gfx context driver initialized.\n",
2020-07-15 03:22:01 +02:00
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
2014-08-20 22:09:30 -03:00
return sdl;
2014-08-20 22:09:30 -03:00
error:
RARCH_WARN("[SDL_GL]: Failed to initialize SDL gfx context driver: %s\n",
2020-07-15 03:22:01 +02:00
SDL_GetError());
2014-08-20 22:09:30 -03:00
sdl_ctx_destroy(sdl);
2014-10-24 05:43:47 +02:00
return NULL;
2014-08-20 22:09:30 -03:00
}
2020-07-15 03:22:01 +02:00
static enum gfx_ctx_api sdl_ctx_get_api(void *data) { return sdl_api; }
2018-03-01 21:16:54 +01:00
static bool sdl_ctx_bind_api(void *data,
enum gfx_ctx_api api, unsigned major,
unsigned minor)
2014-08-20 22:09:30 -03:00
{
2015-09-29 17:35:28 +02:00
#ifdef HAVE_SDL2
unsigned profile;
2014-08-20 22:09:30 -03:00
if (api != GFX_CTX_OPENGL_API && api != GFX_CTX_OPENGL_ES_API)
return false;
profile = SDL_GL_CONTEXT_PROFILE_COMPATIBILITY;
2014-08-20 22:09:30 -03:00
if (api == GFX_CTX_OPENGL_ES_API)
profile = SDL_GL_CONTEXT_PROFILE_ES;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
#endif
2016-08-28 18:20:31 +02:00
2016-02-22 13:26:26 +01:00
sdl_api = api;
2014-08-20 22:09:30 -03:00
2016-08-28 18:20:31 +02:00
#ifndef HAVE_SDL2
if (api != GFX_CTX_OPENGL_API)
return false;
2014-08-20 22:09:30 -03:00
#endif
2016-08-28 18:20:31 +02:00
return true;
2014-08-20 22:09:30 -03:00
}
2018-09-12 00:07:43 +02:00
static void sdl_ctx_swap_interval(void *data, int interval)
2014-08-20 22:09:30 -03:00
{
#ifdef HAVE_SDL2
SDL_GL_SetSwapInterval(interval);
#else
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, interval);
#endif
}
static bool sdl_ctx_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
2014-08-20 22:09:30 -03:00
{
2020-07-17 20:35:11 +02:00
unsigned fsflag = 0;
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
settings_t *settings = config_get_ptr();
bool windowed_fullscreen = settings->bools.video_windowed_fullscreen;
unsigned video_monitor_index = settings->uints.video_monitor_index;
2014-08-20 22:09:30 -03:00
2020-07-17 20:35:11 +02:00
sdl->new_width = width;
sdl->new_height = height;
2014-08-20 22:09:30 -03:00
#ifdef HAVE_SDL2
if (fullscreen)
{
if (windowed_fullscreen)
2014-08-20 22:09:30 -03:00
fsflag = SDL_WINDOW_FULLSCREEN_DESKTOP;
else
fsflag = SDL_WINDOW_FULLSCREEN;
}
2020-07-17 20:35:11 +02:00
if (sdl->win)
2014-08-20 22:09:30 -03:00
{
2020-07-17 20:35:11 +02:00
SDL_SetWindowSize(sdl->win, width, height);
2014-08-20 22:09:30 -03:00
if (fullscreen)
2020-07-17 20:35:11 +02:00
SDL_SetWindowFullscreen(sdl->win, fsflag);
2014-08-20 22:09:30 -03:00
}
else
{
unsigned display = video_monitor_index;
sdl->win = SDL_CreateWindow("RetroArch",
SDL_WINDOWPOS_UNDEFINED_DISPLAY(display),
2014-08-20 22:09:30 -03:00
SDL_WINDOWPOS_UNDEFINED_DISPLAY(display),
width, height, SDL_WINDOW_OPENGL | fsflag);
}
#else
if (fullscreen)
fsflag = SDL_FULLSCREEN;
2020-07-17 20:35:11 +02:00
sdl->win = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | fsflag);
2014-08-20 22:09:30 -03:00
#endif
2020-07-17 20:35:11 +02:00
if (!sdl->win)
2014-08-20 22:09:30 -03:00
goto error;
#ifdef HAVE_SDL2
2019-09-22 12:40:40 +02:00
#if defined(_WIN32)
2020-07-17 20:35:11 +02:00
sdl2_set_handles(sdl->win, RARCH_DISPLAY_WIN32);
2019-09-22 12:40:40 +02:00
#elif defined(HAVE_X11)
2020-07-17 20:35:11 +02:00
sdl2_set_handles(sdl->win, RARCH_DISPLAY_X11);
2019-09-22 12:40:40 +02:00
#elif defined(HAVE_COCOA)
2020-07-17 20:35:11 +02:00
sdl2_set_handles(sdl->win, RARCH_DISPLAY_OSX);
2019-09-22 12:40:40 +02:00
#endif
2020-07-17 20:35:11 +02:00
if (sdl->ctx)
video_driver_set_video_cache_context_ack();
2014-08-20 22:09:30 -03:00
else
{
2020-07-17 20:35:11 +02:00
sdl->ctx = SDL_GL_CreateContext(sdl->win);
2014-08-20 22:09:30 -03:00
2020-07-17 20:35:11 +02:00
if (!sdl->ctx)
2014-08-20 22:09:30 -03:00
goto error;
}
#endif
2020-07-17 20:35:11 +02:00
sdl->full = fullscreen;
sdl->width = width;
sdl->height = height;
2014-08-20 22:09:30 -03:00
return true;
error:
RARCH_WARN("[SDL_GL]: Failed to set video mode: %s\n", SDL_GetError());
return false;
}
2014-10-24 05:43:47 +02:00
static void sdl_ctx_get_video_size(void *data,
unsigned *width, unsigned *height)
2014-08-20 22:09:30 -03:00
{
2017-01-13 16:09:32 +01:00
settings_t *settings = config_get_ptr();
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
2014-10-24 05:43:47 +02:00
if (!sdl)
return;
2020-07-17 20:35:11 +02:00
*width = sdl->width;
*height = sdl->height;
2014-10-24 05:43:47 +02:00
2020-07-17 20:35:11 +02:00
if (!sdl->win)
2014-08-20 22:09:30 -03:00
{
#ifdef HAVE_SDL2
SDL_DisplayMode mode = {0};
2017-04-28 21:03:04 +02:00
int i = settings->uints.video_monitor_index;
2014-08-20 22:09:30 -03:00
if (SDL_GetCurrentDisplayMode(i, &mode) < 0)
RARCH_WARN("[SDL_GL]: Failed to get display #%i mode: %s\n", i,
SDL_GetError());
#else
SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
SDL_Rect mode = {0};
if (!modes)
RARCH_WARN("[SDL_GL]: Failed to detect available video modes: %s\n",
SDL_GetError());
else if (*modes)
mode = **modes;
#endif
*width = mode.w;
*height = mode.h;
}
}
static void sdl_ctx_update_title(void *data)
2014-08-20 22:09:30 -03:00
{
char title[128];
title[0] = '\0';
video_driver_get_window_title(title, sizeof(title));
2019-09-22 12:40:40 +02:00
if (title[0])
{
2014-08-20 22:09:30 -03:00
#ifdef HAVE_SDL2
2020-07-15 03:22:01 +02:00
SDL_SetWindowTitle((SDL_Window*)
video_driver_display_userdata_get(), title);
2014-08-20 22:09:30 -03:00
#else
SDL_WM_SetCaption(title, NULL);
2014-08-20 22:09:30 -03:00
#endif
2019-09-22 12:40:40 +02:00
}
2014-08-20 22:09:30 -03:00
}
static void sdl_ctx_check_window(void *data, bool *quit,
bool *resize,unsigned *width,
2020-03-06 20:29:15 +01:00
unsigned *height)
2014-08-20 22:09:30 -03:00
{
SDL_Event event;
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
2014-08-20 22:09:30 -03:00
SDL_PumpEvents();
2014-08-20 22:09:30 -03:00
#ifdef HAVE_SDL2
2020-07-15 03:22:01 +02:00
while (SDL_PeepEvents(&event, 1,
SDL_GETEVENT, SDL_QUIT, SDL_WINDOWEVENT) > 0)
#else
2020-07-15 03:22:01 +02:00
while (SDL_PeepEvents(&event, 1,
SDL_GETEVENT, SDL_QUITMASK|SDL_VIDEORESIZEMASK) > 0)
#endif
2014-08-20 22:09:30 -03:00
{
switch (event.type)
{
case SDL_QUIT:
#ifdef HAVE_SDL2
2014-08-20 22:09:30 -03:00
case SDL_APP_TERMINATING:
#endif
2014-08-20 22:09:30 -03:00
*quit = true;
break;
#ifdef HAVE_SDL2
2014-08-20 22:09:30 -03:00
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED)
{
2020-07-17 20:35:11 +02:00
sdl->resized = true;
sdl->new_width = event.window.data1;
sdl->new_height = event.window.data2;
2014-08-20 22:09:30 -03:00
}
#else
case SDL_VIDEORESIZE:
2020-07-17 20:35:11 +02:00
sdl->resized = true;
sdl->new_width = event.resize.w;
sdl->new_height = event.resize.h;
#endif
2014-08-20 22:09:30 -03:00
break;
default:
break;
}
}
2020-07-17 20:35:11 +02:00
if (sdl->resized)
2014-08-20 22:09:30 -03:00
{
2020-07-17 20:35:11 +02:00
*width = sdl->new_width;
*height = sdl->new_height;
2020-07-17 01:17:37 +02:00
*resize = true;
2020-07-17 20:35:11 +02:00
sdl->resized = false;
2014-08-20 22:09:30 -03:00
}
}
static bool sdl_ctx_has_focus(void *data)
{
unsigned flags;
2014-08-20 22:09:30 -03:00
#ifdef HAVE_SDL2
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
#ifdef WEBOS
// We do not receive mouse focus when non-magic remote is used.
flags = (SDL_WINDOW_INPUT_FOCUS);
#else
flags = (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS);
#endif
2020-07-17 20:35:11 +02:00
return (SDL_GetWindowFlags(sdl->win) & flags) == flags;
2014-08-20 22:09:30 -03:00
#else
flags = (SDL_APPINPUTFOCUS | SDL_APPACTIVE);
2014-08-20 22:09:30 -03:00
return (SDL_GetAppState() & flags) == flags;
#endif
}
static void sdl_ctx_swap_buffers(void *data)
2014-08-20 22:09:30 -03:00
{
#ifdef HAVE_SDL2
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
if (sdl)
2020-07-17 20:35:11 +02:00
SDL_GL_SwapWindow(sdl->win);
2014-08-20 22:09:30 -03:00
#else
SDL_GL_SwapBuffers();
#endif
}
static void sdl_ctx_input_driver(void *data,
const char *name,
input_driver_t **input, void **input_data)
2014-08-20 22:09:30 -03:00
{
*input = NULL;
2014-08-20 22:09:30 -03:00
*input_data = NULL;
}
static gfx_ctx_proc_t sdl_ctx_get_proc_address(const char *name)
{
2014-09-06 22:14:09 -03:00
return (gfx_ctx_proc_t)SDL_GL_GetProcAddress(name);
2014-08-20 22:09:30 -03:00
}
2020-07-15 03:22:01 +02:00
static void sdl_ctx_show_mouse(void *data, bool state) { SDL_ShowCursor(state); }
2014-08-20 22:09:30 -03:00
static uint32_t sdl_ctx_get_flags(void *data)
2016-05-05 17:35:28 +02:00
{
uint32_t flags = 0;
2020-05-29 18:03:25 +01:00
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_GLSL);
2016-05-05 17:35:28 +02:00
return flags;
}
2020-07-17 01:09:00 +02:00
static bool sdl_ctx_suppress_screensaver(void *data, bool enable) { return false; }
2020-07-15 03:22:01 +02:00
static void sdl_ctx_set_flags(void *data, uint32_t flags) { }
2014-08-20 22:09:30 -03:00
const gfx_ctx_driver_t gfx_ctx_sdl_gl =
{
sdl_ctx_init,
sdl_ctx_destroy,
2018-03-01 21:16:54 +01:00
sdl_ctx_get_api,
2014-08-20 22:09:30 -03:00
sdl_ctx_bind_api,
sdl_ctx_swap_interval,
sdl_ctx_set_video_mode,
sdl_ctx_get_video_size,
NULL, /* get_refresh_rate */
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_metrics */
2014-08-20 22:09:30 -03:00
NULL, /* translate_aspect */
2017-01-18 17:41:27 +01:00
sdl_ctx_update_title,
2014-08-20 22:09:30 -03:00
sdl_ctx_check_window,
NULL, /* set_resize */
2014-08-20 22:09:30 -03:00
sdl_ctx_has_focus,
2015-01-18 22:32:14 +01:00
sdl_ctx_suppress_screensaver,
true, /* has_windowed */
2014-08-20 22:09:30 -03:00
sdl_ctx_swap_buffers,
sdl_ctx_input_driver,
sdl_ctx_get_proc_address,
NULL,
NULL,
sdl_ctx_show_mouse,
2020-07-20 18:22:33 +02:00
"gl_sdl",
sdl_ctx_get_flags,
2016-05-05 17:35:28 +02:00
sdl_ctx_set_flags,
2016-08-31 15:02:07 +02:00
NULL, /* bind_hw_render */
NULL,
NULL
};