454 lines
10 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
2016-01-10 04:41:52 +01:00
* Copyright (C) 2011-2016 - 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/>.
*/
2015-09-16 05:53:34 +02:00
#include "SDL.h"
#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"
#include "../../runloop.h"
#include "../common/gl_common.h"
2016-02-22 13:26:26 +01:00
static enum gfx_ctx_api sdl_api = GFX_CTX_OPENGL_API;
2014-08-20 22:09:30 -03:00
static unsigned g_major = 2;
static unsigned g_minor = 1;
2014-10-24 05:43:47 +02:00
typedef struct gfx_ctx_sdl_data
{
int g_width;
int g_height;
int g_new_width;
int g_new_height;
bool g_full;
bool g_resized;
2014-08-20 22:09:30 -03:00
2014-10-24 05:43:47 +02:00
int g_frame_count;
#ifdef HAVE_SDL2
SDL_Window *g_win;
SDL_GLContext g_ctx;
#else
SDL_Surface *g_win;
#endif
} gfx_ctx_sdl_data_t;
2014-08-20 22:09:30 -03:00
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
2014-10-24 05:43:47 +02:00
if (sdl->g_ctx)
SDL_GL_DeleteContext(sdl->g_ctx);
if (sdl->g_win)
SDL_DestroyWindow(sdl->g_win);
sdl->g_ctx = NULL;
2014-08-20 22:09:30 -03:00
#else
2014-10-24 05:43:47 +02:00
if (sdl->g_win)
SDL_FreeSurface(sdl->g_win);
2014-08-20 22:09:30 -03:00
#endif
2014-10-24 05:43:47 +02:00
sdl->g_win = NULL;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
2014-08-20 22:09:30 -03:00
static void *sdl_ctx_init(void *video_driver)
2014-08-20 22:09:30 -03:00
{
2015-11-23 21:28:54 +01: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));
if (!sdl)
return NULL;
2014-08-20 22:09:30 -03:00
#ifdef HAVE_X11
XInitThreads();
#endif
if (SDL_WasInit(0) == 0)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
goto error;
}
else if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
goto error;
RARCH_LOG("[SDL_GL] SDL %i.%i.%i gfx context driver initialized.\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
return sdl;
2014-08-20 22:09:30 -03:00
error:
RARCH_WARN("[SDL_GL]: Failed to initialize SDL gfx context driver: %s\n",
SDL_GetError());
2014-10-24 05:43:47 +02:00
sdl_ctx_destroy_resources(sdl);
if (sdl)
free(sdl);
return NULL;
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;
2014-08-20 22:09:30 -03:00
2014-10-24 05:43:47 +02:00
if (!sdl)
return;
sdl_ctx_destroy_resources(sdl);
free(sdl);
2014-08-20 22:09:30 -03:00
}
static bool sdl_ctx_bind_api(void *data, enum gfx_ctx_api api, unsigned major,
unsigned minor)
{
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
g_major = major;
g_minor = minor;
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
}
static void sdl_ctx_swap_interval(void *data, unsigned interval)
{
(void)data;
#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
{
2014-10-24 05:43:47 +02:00
unsigned fsflag = 0;
2015-03-20 22:08:36 +01:00
settings_t *settings = config_get_ptr();
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
2014-08-20 22:09:30 -03:00
2014-10-24 05:43:47 +02:00
sdl->g_new_width = width;
sdl->g_new_height = height;
2014-08-20 22:09:30 -03:00
#ifdef HAVE_SDL2
if (fullscreen)
{
2015-03-20 22:08:36 +01:00
if (settings->video.windowed_fullscreen)
2014-08-20 22:09:30 -03:00
fsflag = SDL_WINDOW_FULLSCREEN_DESKTOP;
else
fsflag = SDL_WINDOW_FULLSCREEN;
}
2014-10-24 05:43:47 +02:00
if (sdl->g_win)
2014-08-20 22:09:30 -03:00
{
2014-10-24 05:43:47 +02:00
SDL_SetWindowSize(sdl->g_win, width, height);
2014-08-20 22:09:30 -03:00
if (fullscreen)
2014-10-24 05:43:47 +02:00
SDL_SetWindowFullscreen(sdl->g_win, fsflag);
2014-08-20 22:09:30 -03:00
}
else
{
2015-03-20 22:08:36 +01:00
unsigned display = settings->video.monitor_index;
2014-10-24 05:43:47 +02:00
sdl->g_win = SDL_CreateWindow("", 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;
2014-10-24 05:43:47 +02:00
sdl->g_win = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | fsflag);
2014-08-20 22:09:30 -03:00
#endif
2014-10-24 05:43:47 +02:00
if (!sdl->g_win)
2014-08-20 22:09:30 -03:00
goto error;
#ifdef HAVE_SDL2
2014-10-24 05:43:47 +02:00
if (sdl->g_ctx)
video_driver_set_video_cache_context_ack();
2014-08-20 22:09:30 -03:00
else
{
2014-10-24 05:43:47 +02:00
sdl->g_ctx = SDL_GL_CreateContext(sdl->g_win);
2014-08-20 22:09:30 -03:00
2014-10-24 05:43:47 +02:00
if (!sdl->g_ctx)
2014-08-20 22:09:30 -03:00
goto error;
}
#endif
2014-10-24 05:43:47 +02:00
sdl->g_full = fullscreen;
sdl->g_width = width;
sdl->g_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
{
2015-03-20 22:08:36 +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;
*width = sdl->g_width;
*height = sdl->g_height;
if (!sdl->g_win)
2014-08-20 22:09:30 -03:00
{
#ifdef HAVE_SDL2
SDL_DisplayMode mode = {0};
2015-09-29 17:35:28 +02:00
int i = settings->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_window_title(void *data)
{
2015-06-13 02:10:06 +02:00
char buf[128] = {0};
char buf_fps[128] = {0};
2015-03-20 22:08:36 +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;
if (video_monitor_get_fps(buf, sizeof(buf),
2015-03-07 23:03:56 +01:00
buf_fps, sizeof(buf_fps)))
2014-08-20 22:09:30 -03:00
{
#ifdef HAVE_SDL2
2014-10-24 05:43:47 +02:00
SDL_SetWindowTitle(sdl->g_win, buf);
2014-08-20 22:09:30 -03:00
#else
SDL_WM_SetCaption(buf, NULL);
#endif
}
2015-03-20 22:08:36 +01:00
if (settings->fps_show)
2015-12-07 15:32:14 +01:00
runloop_msg_queue_push(buf_fps, 1, 1, false);
2014-08-20 22:09:30 -03:00
}
static void sdl_ctx_check_window(void *data, bool *quit, bool *resize,unsigned *width,
unsigned *height, unsigned frame_count)
{
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
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_QUIT, SDL_WINDOWEVENT) > 0)
#else
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)
{
2014-10-24 05:43:47 +02:00
sdl->g_resized = true;
sdl->g_new_width = event.window.data1;
sdl->g_new_height = event.window.data2;
2014-08-20 22:09:30 -03:00
}
#else
case SDL_VIDEORESIZE:
sdl->g_resized = true;
sdl->g_new_width = event.resize.w;
sdl->g_new_height = event.resize.h;
#endif
2014-08-20 22:09:30 -03:00
break;
default:
break;
}
}
2014-10-24 05:43:47 +02:00
if (sdl->g_resized)
2014-08-20 22:09:30 -03:00
{
2014-10-24 05:43:47 +02:00
*width = sdl->g_new_width;
*height = sdl->g_new_height;
2014-08-20 22:09:30 -03:00
*resize = true;
2014-10-24 05:43:47 +02:00
sdl->g_resized = false;
2014-08-20 22:09:30 -03:00
}
2014-10-24 05:43:47 +02:00
sdl->g_frame_count = frame_count;
2014-08-20 22:09:30 -03:00
}
static bool sdl_ctx_set_resize(void *data, unsigned width, unsigned height)
2014-08-20 22:09:30 -03:00
{
(void)data;
(void)width;
(void)height;
return 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;
flags = (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS);
2014-10-24 05:43:47 +02:00
return (SDL_GetWindowFlags(sdl->g_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
}
2015-01-18 22:32:14 +01:00
static bool sdl_ctx_suppress_screensaver(void *data, bool enable)
{
(void)data;
(void)enable;
return false;
}
static bool sdl_ctx_has_windowed(void *data)
{
(void)data;
return true;
}
2014-08-20 22:09:30 -03:00
static void sdl_ctx_swap_buffers(void *data)
{
#ifdef HAVE_SDL2
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
2014-10-24 05:43:47 +02:00
SDL_GL_SwapWindow(sdl->g_win);
2014-08-20 22:09:30 -03:00
#else
SDL_GL_SwapBuffers();
#endif
2014-10-24 05:43:47 +02:00
(void)data;
2014-08-20 22:09:30 -03:00
}
static void sdl_ctx_input_driver(void *data, const input_driver_t **input, void **input_data)
{
(void)data;
*input = NULL;
*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
}
static void sdl_ctx_show_mouse(void *data, bool state)
{
(void)data;
SDL_ShowCursor(state);
}
static uint32_t sdl_ctx_get_flags(void *data)
2016-05-05 17:35:28 +02:00
{
uint32_t flags = 0;
BIT32_SET(flags, GFX_CTX_FLAGS_NONE);
return flags;
}
static void sdl_ctx_set_flags(void *data, uint32_t flags)
{
(void)data;
}
2014-08-20 22:09:30 -03:00
const gfx_ctx_driver_t gfx_ctx_sdl_gl =
{
sdl_ctx_init,
sdl_ctx_destroy,
sdl_ctx_bind_api,
sdl_ctx_swap_interval,
sdl_ctx_set_video_mode,
sdl_ctx_get_video_size,
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 */
sdl_ctx_update_window_title,
sdl_ctx_check_window,
sdl_ctx_set_resize,
sdl_ctx_has_focus,
2015-01-18 22:32:14 +01:00
sdl_ctx_suppress_screensaver,
sdl_ctx_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,
"sdl_gl",
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
};