diff --git a/Makefile b/Makefile
index 101ac4ff14..c81682a0da 100644
--- a/Makefile
+++ b/Makefile
@@ -213,6 +213,14 @@ ifeq ($(HAVE_SDL), 1)
LIBS += $(SDL_LIBS)
endif
+ifeq ($(HAVE_SDL2), 1)
+ OBJ += gfx/sdl2_gfx.o input/sdl2_input.o input/sdl2_joypad.o audio/sdl_audio.o
+ JOYCONFIG_OBJ += input/sdl2_joypad.o
+ JOYCONFIG_LIBS += $(SDL2_LIBS)
+ DEFINES += $(SDL2_CFLAGS) $(BSD_LOCAL_INC)
+ LIBS += $(SDL2_LIBS)
+endif
+
ifeq ($(HAVE_OMAP), 1)
OBJ += gfx/omap_gfx.o
endif
diff --git a/Makefile.win b/Makefile.win
index 50f3b89ac7..17c871e04f 100644
--- a/Makefile.win
+++ b/Makefile.win
@@ -131,6 +131,15 @@ ifeq ($(HAVE_MENU_COMMON), 1)
OBJ += frontend/menu/backend/menu_common_backend.o frontend/menu/menu_input_line_cb.o frontend/menu/menu_common.o frontend/menu/menu_navigation.o
endif
+ifeq ($(HAVE_SDL2), 1)
+ OBJ += gfx/sdl2_gfx.o input/sdl2_input.o input/sdl2_joypad.o audio/sdl_audio.o
+ JOBJ += input/sdl2_joypad.o
+ LIBS += -lSDL2
+ JLIBS += -lSDL2
+ DEFINES += -ISDL2 -DHAVE_SDL2
+ HAVE_SDL = 0
+endif
+
ifeq ($(HAVE_SDL), 1)
OBJ += gfx/sdl_gfx.o input/sdl_input.o input/sdl_joypad.o audio/sdl_audio.o
JOBJ += input/sdl_joypad.o
diff --git a/audio/sdl_audio.c b/audio/sdl_audio.c
index 832670f67a..e302e41f41 100644
--- a/audio/sdl_audio.c
+++ b/audio/sdl_audio.c
@@ -193,6 +193,10 @@ const audio_driver_t audio_sdl = {
sdl_audio_set_nonblock_state,
sdl_audio_free,
NULL,
+#ifdef HAVE_SDL2
+ "sdl2"
+#else
"sdl"
+#endif
};
diff --git a/config.features.h b/config.features.h
index 107bdac1d4..530b6c1430 100644
--- a/config.features.h
+++ b/config.features.h
@@ -14,6 +14,12 @@ static const bool _sdl_supp = true;
static const bool _sdl_supp = false;
#endif
+#ifdef HAVE_SDL2
+static const bool _sdl2_supp = true;
+#else
+static const bool _sdl2_supp = false;
+#endif
+
#ifdef HAVE_THREADS
static const bool _thread_supp = true;
#else
diff --git a/driver.c b/driver.c
index db92897428..8a0afe0a0b 100644
--- a/driver.c
+++ b/driver.c
@@ -69,7 +69,7 @@ static const audio_driver_t *audio_drivers[] = {
#ifdef HAVE_JACK
&audio_jack,
#endif
-#ifdef HAVE_SDL
+#if defined(HAVE_SDL) || defined(HAVE_SDL2)
&audio_sdl,
#endif
#ifdef HAVE_XAUDIO
@@ -121,6 +121,9 @@ static const video_driver_t *video_drivers[] = {
#ifdef HAVE_SDL
&video_sdl,
#endif
+#ifdef HAVE_SDL2
+ &video_sdl2,
+#endif
#ifdef HAVE_XVIDEO
&video_xvideo,
#endif
@@ -152,6 +155,9 @@ static const input_driver_t *input_drivers[] = {
#ifdef HAVE_SDL
&input_sdl,
#endif
+#ifdef HAVE_SDL2
+ &input_sdl2,
+#endif
#ifdef HAVE_DINPUT
&input_dinput,
#endif
diff --git a/driver.h b/driver.h
index fab6852719..843505988c 100644
--- a/driver.h
+++ b/driver.h
@@ -620,12 +620,14 @@ extern const video_driver_t video_xenon360;
extern const video_driver_t video_xvideo;
extern const video_driver_t video_xdk_d3d;
extern const video_driver_t video_sdl;
+extern const video_driver_t video_sdl2;
extern const video_driver_t video_vg;
extern const video_driver_t video_null;
extern const video_driver_t video_omap;
extern const video_driver_t video_exynos;
extern const input_driver_t input_android;
extern const input_driver_t input_sdl;
+extern const input_driver_t input_sdl2;
extern const input_driver_t input_dinput;
extern const input_driver_t input_x;
extern const input_driver_t input_wayland;
diff --git a/gfx/sdl2_gfx.c b/gfx/sdl2_gfx.c
new file mode 100644
index 0000000000..163e8c9a06
--- /dev/null
+++ b/gfx/sdl2_gfx.c
@@ -0,0 +1,746 @@
+/* RetroArch - A frontend for libretro.
+ * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
+ *
+ * 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 .
+ */
+
+#include "SDL.h"
+#include "SDL_syswm.h"
+#include "../driver.h"
+#include
+#include
+#include "../general.h"
+#include "scaler/scaler.h"
+#include "gfx_common.h"
+#include "gfx_context.h"
+#include "fonts/fonts.h"
+
+#ifdef HAVE_X11
+#include "context/x11_common.h"
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "SDL2/SDL_syswm.h"
+
+typedef struct sdl2_tex
+{
+ SDL_Texture *tex;
+
+ int w;
+ int h;
+ int pitch;
+ bool active;
+ bool rgb32;
+} sdl2_tex_t;
+
+typedef struct _sdl2_video
+{
+ SDL_Window *window;
+ SDL_Renderer *renderer;
+
+ sdl2_tex_t frame;
+ sdl2_tex_t menu;
+ sdl2_tex_t font;
+
+ bool gl;
+ bool quitting;
+
+ void *font_data;
+ const font_renderer_driver_t *font_driver;
+ uint8_t font_r;
+ uint8_t font_g;
+ uint8_t font_b;
+
+ struct rarch_viewport vp;
+
+ video_info_t video;
+
+ bool should_resize;
+ double rotation;
+
+} sdl2_video_t;
+
+static void sdl2_gfx_free(void *data);
+
+static inline void sdl_tex_zero(sdl2_tex_t *t)
+{
+ if (t->tex)
+ {
+ SDL_DestroyTexture(t->tex);
+ }
+
+ t->tex = NULL;
+ t->w = t->h = t->pitch = 0;
+}
+
+static void sdl2_init_font(sdl2_video_t *vid, const char *font_path,
+ unsigned font_size)
+{
+ if (!g_settings.video.font_enable)
+ return;
+
+ if (font_renderer_create_default(&vid->font_driver, &vid->font_data,
+ *font_path ? font_path : NULL, font_size))
+ {
+ int r = g_settings.video.msg_color_r * 255;
+ int g = g_settings.video.msg_color_g * 255;
+ int b = g_settings.video.msg_color_b * 255;
+
+ r = r < 0 ? 0 : (r > 255 ? 255 : r);
+ g = g < 0 ? 0 : (g > 255 ? 255 : g);
+ b = b < 0 ? 0 : (b > 255 ? 255 : b);
+
+ vid->font_r = r;
+ vid->font_g = g;
+ vid->font_b = b;
+ }
+ else
+ {
+ RARCH_WARN("[SDL]: Could not initialize fonts.\n");
+ return;
+ }
+
+ const struct font_atlas *atlas = vid->font_driver->get_atlas(vid->font_data);
+
+ SDL_Surface *tmp = SDL_CreateRGBSurfaceFrom(atlas->buffer, atlas->width,
+ atlas->height, 8, atlas->width,
+ 0, 0, 0, 0);
+ SDL_Color colors[256];
+ int i;
+
+ for (i = 0; i < 256; ++i)
+ {
+ colors[i].r = colors[i].g = colors[i].b = i;
+ colors[i].a = 255;
+ }
+
+ SDL_Palette *pal = SDL_AllocPalette(256);
+ SDL_SetPaletteColors(pal, colors, 0, 256);
+ SDL_SetSurfacePalette(tmp, pal);
+ SDL_SetColorKey(tmp, SDL_TRUE, 0);
+
+ vid->font.tex = SDL_CreateTextureFromSurface(vid->renderer, tmp);
+
+ if (vid->font.tex)
+ {
+ vid->font.w = atlas->width;
+ vid->font.h = atlas->height;
+ vid->font.active = true;
+
+ SDL_SetTextureBlendMode(vid->font.tex, SDL_BLENDMODE_ADD);
+ }
+ else
+ RARCH_WARN("[SDL]: Failed to initialize font texture: %s\n", SDL_GetError());
+
+ SDL_FreePalette(pal);
+ SDL_FreeSurface(tmp);
+}
+
+static void sdl2_render_msg(sdl2_video_t *vid, const char *msg)
+{
+ unsigned width = vid->vp.width;
+ unsigned height = vid->vp.height;
+
+ int x, y, delta_x, delta_y;
+
+ if (!vid->font_data)
+ return;
+
+ x = g_settings.video.msg_pos_x * width;
+ y = (1.0f - g_settings.video.msg_pos_y) * height;
+ delta_x = 0;
+ delta_y = 0;
+
+ SDL_SetTextureColorMod(vid->font.tex, vid->font_r, vid->font_g, vid->font_b);
+
+ for (; *msg; msg++)
+ {
+ const struct font_glyph *gly = vid->font_driver->get_glyph(vid->font_data, (uint8_t)*msg);
+
+ if (!gly)
+ gly = vid->font_driver->get_glyph(vid->font_data, '?');
+
+ if (!gly)
+ continue;
+
+ int off_x = gly->draw_offset_x;
+ int off_y = gly->draw_offset_y;
+ int tex_x = gly->atlas_offset_x;
+ int tex_y = gly->atlas_offset_y;
+
+ SDL_Rect srcrect = {
+ tex_x, tex_y,
+ gly->width, gly->height
+ };
+
+ SDL_Rect dstrect = {
+ x + delta_x + off_x, y + delta_y + off_y,
+ gly->width, gly->height
+ };
+
+ SDL_RenderCopyEx(vid->renderer, vid->font.tex, &srcrect, &dstrect, 0, NULL, SDL_FLIP_NONE);
+
+ delta_x += gly->advance_x;
+ delta_y -= gly->advance_y;
+ }
+}
+
+static void sdl2_gfx_set_handles(sdl2_video_t *vid)
+{
+ // SysWMinfo headers are broken on OSX. :(
+#if defined(_WIN32)
+ SDL_SysWMinfo info;
+ SDL_VERSION(&info.version);
+
+ if (SDL_GetWindowWMInfo(vid->window, &info) == 1)
+ {
+ driver.display_type = RARCH_DISPLAY_WIN32;
+ driver.video_display = 0;
+ driver.video_window = (uintptr_t)info.window;
+ }
+#elif defined(HAVE_X11)
+ SDL_SysWMinfo info;
+ SDL_VERSION(&info.version);
+
+ if (SDL_GetWindowWMInfo(vid->window, &info) == 1)
+ {
+ driver.display_type = RARCH_DISPLAY_X11;
+ driver.video_display = (uintptr_t)info.info.x11.display;
+ driver.video_window = (uintptr_t)info.info.x11.window;
+ }
+#endif
+}
+
+static void sdl2_init_renderer(sdl2_video_t *vid)
+{
+ unsigned flags = SDL_RENDERER_ACCELERATED;
+
+ if (vid->video.vsync)
+ flags |= SDL_RENDERER_PRESENTVSYNC;
+
+ SDL_ClearHints();
+ SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC,
+ vid->video.vsync ? "1" : "0", SDL_HINT_OVERRIDE);
+ vid->renderer = SDL_CreateRenderer(vid->window, -1, flags);
+
+ if (!vid->renderer)
+ {
+ RARCH_ERR("[SDL]: Failed to initialize renderer: %s", SDL_GetError());
+ return;
+ }
+
+ SDL_SetRenderDrawColor(vid->renderer, 0, 0, 0, 255);
+}
+
+static void sdl_refresh_renderer(sdl2_video_t *vid)
+{
+ SDL_RenderClear(vid->renderer);
+ SDL_Rect r = { vid->vp.x, vid->vp.y, vid->vp.width, vid->vp.height };
+ SDL_RenderSetViewport(vid->renderer, &r);
+
+ // breaks int scaling
+ // SDL_RenderSetLogicalSize(vid->renderer, vid->vp.width, vid->vp.height);
+}
+
+static void sdl_refresh_viewport(sdl2_video_t *vid)
+{
+ int win_w, win_h;
+
+ SDL_GetWindowSize(vid->window, &win_w, &win_h);
+
+ vid->vp.x = 0;
+ vid->vp.y = 0;
+ vid->vp.width = win_w;
+ vid->vp.height = win_h;
+ vid->vp.full_width = win_w;
+ vid->vp.full_height = win_h;
+
+ if (g_settings.video.scale_integer)
+ gfx_scale_integer(&vid->vp, win_w, win_h, g_extern.system.aspect_ratio,
+ vid->video.force_aspect);
+ else if (g_settings.video.aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
+ {
+ const struct rarch_viewport *custom = &g_extern.console.screen.viewports.custom_vp;
+
+ vid->vp.x = custom->x;
+ vid->vp.y = custom->y;
+ vid->vp.width = custom->width;
+ vid->vp.height = custom->height;
+ }
+ else if (vid->video.force_aspect)
+ {
+ float delta;
+ float device_aspect = (float)win_w / win_h;
+ float desired_aspect = g_extern.system.aspect_ratio;
+
+ if (fabsf(device_aspect - desired_aspect) < 0.0001f)
+ {
+ /* If the aspect ratios of screen and desired aspect ratio are
+ * sufficiently equal (floating point stuff), assume they are
+ * actually equal. */
+ }
+ else if (device_aspect > desired_aspect)
+ {
+ delta = (desired_aspect / device_aspect - 1.0f) / 2.0f + 0.5f;
+ vid->vp.x = (int)roundf(win_w * (0.5f - delta));
+ vid->vp.width = (unsigned)roundf(2.0f * win_w * delta);
+ }
+ else
+ {
+ delta = (device_aspect / desired_aspect - 1.0f) / 2.0f + 0.5f;
+ vid->vp.y = (int)roundf(win_h * (0.5f - delta));
+ vid->vp.height = (unsigned)roundf(2.0f * win_h * delta);
+ }
+ }
+
+ vid->should_resize = false;
+
+ sdl_refresh_renderer(vid);
+}
+
+static void sdl_refresh_input_size(sdl2_video_t *vid, bool menu, bool rgb32,
+ unsigned width, unsigned height, unsigned pitch)
+{
+ sdl2_tex_t *target = menu ? &vid->menu : &vid->frame;
+
+ if (!target->tex || target->w != width || target->h != height
+ || target->rgb32 != rgb32 || target->pitch != pitch)
+ {
+ unsigned format;
+
+ sdl_tex_zero(target);
+
+ RARCH_PERFORMANCE_INIT(sdl_create_texture);
+ RARCH_PERFORMANCE_START(sdl_create_texture);
+
+ if (menu)
+ format = rgb32 ? SDL_PIXELFORMAT_ARGB8888 : SDL_PIXELFORMAT_RGBA4444;
+ else
+ {
+ switch (g_extern.system.pix_fmt)
+ {
+ case RETRO_PIXEL_FORMAT_0RGB1555:
+ format = SDL_PIXELFORMAT_ARGB1555;
+ break;
+ case RETRO_PIXEL_FORMAT_XRGB8888:
+ format = SDL_PIXELFORMAT_ARGB8888;
+ break;
+ case RETRO_PIXEL_FORMAT_RGB565:
+ format = SDL_PIXELFORMAT_RGB565;
+ break;
+ default:
+ RARCH_ERR("Unknown/unsupported video format.\n");
+ return;
+ break;
+ }
+ }
+
+ SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY,
+ (vid->video.smooth || menu) ? "linear" : "nearest",
+ SDL_HINT_OVERRIDE);
+
+ target->tex = SDL_CreateTexture(vid->renderer, format,
+ SDL_TEXTUREACCESS_STREAMING, width, height);
+
+ RARCH_PERFORMANCE_STOP(sdl_create_texture);
+
+ if (!target->tex)
+ {
+ RARCH_ERR("Failed to create %s texture: %s\n", menu ? "menu" : "main",
+ SDL_GetError());
+ return;
+ }
+
+ if (menu)
+ SDL_SetTextureBlendMode(target->tex, SDL_BLENDMODE_BLEND);
+
+ target->w = width;
+ target->h = height;
+ target->pitch = pitch;
+ target->rgb32 = rgb32;
+ target->active = true;
+ }
+}
+
+static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **input, void **input_data)
+{
+#ifdef _WIN32
+ gfx_set_dwm();
+#endif
+
+#ifdef HAVE_X11
+ XInitThreads();
+#endif
+
+ int i;
+
+ SDL_InitSubSystem(SDL_INIT_VIDEO);
+
+ sdl2_video_t *vid = (sdl2_video_t*)calloc(1, sizeof(*vid));
+ if (!vid)
+ return NULL;
+
+// RARCH_LOG("[SDL]: supported video drivers (change with $SDL_VIDEODRIVER):\n");
+
+// for (i = 0; i < SDL_GetNumVideoDrivers(); ++i)
+// {
+// RARCH_LOG("\t%s\n", SDL_GetVideoDriver(i));
+// }
+
+ RARCH_LOG("[SDL]: Available displays:\n");
+ for(i = 0; i < SDL_GetNumVideoDisplays(); ++i)
+ {
+ SDL_DisplayMode mode;
+
+ if (SDL_GetCurrentDisplayMode(i, &mode) == 0)
+ RARCH_LOG("\tDisplay #%i mode: unknown.\n", i);
+ else
+ RARCH_LOG("\tDisplay #%i mode: %ix%i@%ihz.\n", i, mode.w, mode.h,
+ mode.refresh_rate);
+ }
+
+// void *sdl_input = NULL;
+
+ if (!video->fullscreen)
+ RARCH_LOG("[SDL]: Creating window @ %ux%u\n", video->width, video->height);
+
+ unsigned flags;
+
+ if (video->fullscreen)
+ {
+ flags = g_settings.video.windowed_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN;
+ }
+ else
+ flags = SDL_WINDOW_RESIZABLE;
+
+ vid->window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+ video->width, video->height, flags);
+
+ if (!vid->window)
+ {
+ RARCH_ERR("[SDL]: Failed to init SDL window: %s\n", SDL_GetError());
+ goto error;
+ }
+
+ vid->video.smooth = g_settings.video.smooth;
+ vid->should_resize = true;
+
+ sdl_tex_zero(&vid->frame);
+ sdl_tex_zero(&vid->menu);
+
+ if (video->fullscreen)
+ SDL_ShowCursor(SDL_DISABLE);
+
+ sdl2_init_renderer(vid);
+ sdl2_init_font(vid, g_settings.video.font_path, g_settings.video.font_size);
+
+ sdl2_gfx_set_handles(vid);
+
+ if (input && input_data)
+ {
+ void *sdl2_input = input_sdl2.init();
+ if (sdl2_input)
+ {
+ *input = &input_sdl2;
+ *input_data = sdl2_input;
+ }
+ else
+ {
+ *input = NULL;
+ *input_data = NULL;
+ }
+ }
+
+ return vid;
+
+error:
+ sdl2_gfx_free(vid);
+ return NULL;
+}
+
+static void check_window(sdl2_video_t *vid)
+{
+ SDL_Event event;
+
+ SDL_PumpEvents();
+ while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_QUIT, SDL_WINDOWEVENT) > 0)
+ {
+ switch (event.type)
+ {
+ case SDL_QUIT:
+ vid->quitting = true;
+ break;
+
+ case SDL_WINDOWEVENT:
+ if (event.window.event == SDL_WINDOWEVENT_RESIZED)
+ vid->should_resize = true;
+ break;
+
+ default:
+ break;
+ }
+ }
+}
+
+static bool sdl2_gfx_frame(void *data, const void *frame, unsigned width,
+ unsigned height, unsigned pitch, const char *msg)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+
+ if (vid->should_resize)
+ sdl_refresh_viewport(vid);
+
+ if (frame)
+ {
+ sdl_refresh_input_size(vid, false, vid->frame.rgb32/*vid->video.rgb32*/, width, height, pitch);
+
+ RARCH_PERFORMANCE_INIT(sdl_copy_frame);
+ RARCH_PERFORMANCE_START(sdl_copy_frame);
+
+ SDL_UpdateTexture(vid->frame.tex, NULL, frame, pitch);
+
+ RARCH_PERFORMANCE_STOP(sdl_copy_frame);
+ }
+
+ SDL_RenderCopyEx(vid->renderer, vid->frame.tex, NULL, NULL, vid->rotation, NULL, SDL_FLIP_NONE);
+
+#ifdef HAVE_MENU
+ if (g_extern.lifecycle_state & (1ULL << MODE_MENU) && driver.menu_ctx && driver.menu_ctx->frame)
+ driver.menu_ctx->frame();
+#endif
+
+ if (vid->menu.active)
+ SDL_RenderCopy(vid->renderer, vid->menu.tex, NULL, NULL);
+
+ if (msg)
+ sdl2_render_msg(vid, msg);
+
+ SDL_RenderPresent(vid->renderer);
+
+ char buf[128];
+ if (gfx_get_fps(buf, sizeof(buf), NULL, 0))
+ SDL_SetWindowTitle(vid->window, buf);
+
+ g_extern.frame_count++;
+
+ return true;
+}
+
+static void sdl2_gfx_set_nonblock_state(void *data, bool toggle)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+
+ vid->video.vsync = !toggle;
+ sdl_refresh_renderer(vid);
+}
+
+static bool sdl2_gfx_alive(void *data)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ check_window(vid);
+ return !vid->quitting;
+}
+
+static bool sdl2_gfx_focus(void *data)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ unsigned flags = (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS);
+ return (SDL_GetWindowFlags(vid->window) & flags) == flags;
+}
+
+static void sdl2_gfx_free(void *data)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ if (!vid)
+ return;
+
+ SDL_QuitSubSystem(SDL_INIT_VIDEO);
+
+ if (vid->font_data)
+ vid->font_driver->free(vid->font_data);
+
+ free(vid);
+}
+
+static void sdl2_gfx_set_rotation(void *data, unsigned rotation)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ vid->rotation = 270 * rotation;
+}
+
+static void sdl2_gfx_viewport_info(void *data, struct rarch_viewport *vp)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ *vp = vid->vp;
+}
+
+static bool sdl2_gfx_read_viewport(void *data, uint8_t *buffer)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+
+ RARCH_PERFORMANCE_INIT(sdl2_gfx_read_viewport);
+ RARCH_PERFORMANCE_START(sdl2_gfx_read_viewport);
+
+ rarch_render_cached_frame();
+
+ SDL_Surface *surf = SDL_GetWindowSurface(vid->window);
+ SDL_Surface *bgr24 = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_BGR24, 0);
+
+ if (!bgr24)
+ {
+ RARCH_WARN("Failed to convert viewport data to BGR24: %s", SDL_GetError());
+ return false;
+ }
+
+ memcpy(buffer, bgr24->pixels, bgr24->h * bgr24->pitch);
+
+ RARCH_PERFORMANCE_STOP(sdl2_gfx_read_viewport);
+
+ return true;
+}
+
+void sdl2_poke_set_filtering(void *data, unsigned index, bool smooth)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ vid->video.smooth = smooth;
+
+// sdl_refresh_renderer(vid);
+
+ sdl_tex_zero(&vid->frame);
+}
+
+static void sdl2_poke_set_aspect_ratio(void *data, unsigned aspectratio_index)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+
+ switch (aspectratio_index)
+ {
+ case ASPECT_RATIO_SQUARE:
+ gfx_set_square_pixel_viewport(g_extern.system.av_info.geometry.base_width,
+ g_extern.system.av_info.geometry.base_height);
+ break;
+
+ case ASPECT_RATIO_CORE:
+ gfx_set_core_viewport();
+ break;
+
+ case ASPECT_RATIO_CONFIG:
+ gfx_set_config_viewport();
+ break;
+
+ default:
+ break;
+ }
+
+ g_extern.system.aspect_ratio = aspectratio_lut[aspectratio_index].value;
+
+ vid->video.force_aspect = true;
+ vid->should_resize = true;
+}
+
+void sdl2_poke_apply_state_changes(void *data)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ vid->should_resize = true;
+}
+
+void sdl2_poke_set_texture_frame(void *data, const void *frame, bool rgb32,
+ unsigned width, unsigned height, float alpha)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+
+ if (frame)
+ {
+ sdl_refresh_input_size(vid, true, rgb32, width, height,
+ width * (rgb32 ? 4 : 2));
+
+ RARCH_PERFORMANCE_INIT(copy_texture_frame);
+ RARCH_PERFORMANCE_START(copy_texture_frame);
+
+ SDL_UpdateTexture(vid->menu.tex, NULL, frame, vid->menu.pitch);
+
+ RARCH_PERFORMANCE_STOP(copy_texture_frame);
+ }
+}
+
+void sdl2_poke_texture_enable(void *data, bool enable, bool full_screen)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+
+ vid->menu.active = enable;
+}
+
+void sdl2_poke_set_osd_msg(void *data, const char *msg, const struct font_params *params)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ sdl2_render_msg(vid, msg);
+ fprintf(stderr, "[SDL]: OSD MSG: %s\n", msg);
+}
+
+void sdl2_show_mouse(void *data, bool state)
+{
+ (void)data;
+ SDL_ShowCursor(state);
+}
+
+void sdl2_grab_mouse_toggle(void *data)
+{
+ sdl2_video_t *vid = (sdl2_video_t*)data;
+ SDL_SetWindowGrab(vid->window, SDL_GetWindowGrab(vid->window));
+}
+
+static video_poke_interface_t sdl2_video_poke_interface = {
+ sdl2_poke_set_filtering,
+#ifdef HAVE_FBO
+ NULL,
+ NULL,
+#endif
+ sdl2_poke_set_aspect_ratio,
+ sdl2_poke_apply_state_changes,
+#ifdef HAVE_MENU
+ sdl2_poke_set_texture_frame,
+ sdl2_poke_texture_enable,
+#endif
+ sdl2_poke_set_osd_msg,
+ sdl2_show_mouse,
+ sdl2_grab_mouse_toggle,
+ NULL,
+};
+
+void sdl2_gfx_poke_interface(void *data, const video_poke_interface_t **iface)
+{
+ (void)data;
+ *iface = &sdl2_video_poke_interface;
+}
+
+const video_driver_t video_sdl2 = {
+ sdl2_gfx_init,
+ sdl2_gfx_frame,
+ sdl2_gfx_set_nonblock_state,
+ sdl2_gfx_alive,
+ sdl2_gfx_focus,
+ NULL,
+ sdl2_gfx_free,
+ "sdl2",
+
+ sdl2_gfx_set_rotation,
+ sdl2_gfx_viewport_info,
+ sdl2_gfx_read_viewport,
+#ifdef HAVE_OVERLAY
+ NULL, //void (*overlay_interface)(void *data, const video_overlay_interface_t **iface);
+#endif
+ sdl2_gfx_poke_interface
+};
+
diff --git a/input/input_common.c b/input/input_common.c
index ce0f0a7aea..bd5122185b 100644
--- a/input/input_common.c
+++ b/input/input_common.c
@@ -30,7 +30,7 @@
#include
#endif
-#ifdef HAVE_SDL
+#if defined(HAVE_SDL) || defined(HAVE_SDL2)
#include "SDL.h"
#endif
@@ -77,6 +77,9 @@ static const rarch_joypad_driver_t *joypad_drivers[] = {
#ifdef HAVE_SDL
&sdl_joypad,
#endif
+#ifdef HAVE_SDL2
+ &sdl2_joypad,
+#endif
#ifdef __MACH__
&apple_joypad,
#endif
@@ -464,6 +467,96 @@ const struct rarch_key_map rarch_key_map_sdl[] = {
};
#endif
+#ifdef HAVE_SDL2
+const struct rarch_key_map rarch_key_map_sdl2[] = {
+ { SDL_SCANCODE_LEFT, RETROK_LEFT },
+ { SDL_SCANCODE_RIGHT, RETROK_RIGHT },
+ { SDL_SCANCODE_UP, RETROK_UP },
+ { SDL_SCANCODE_DOWN, RETROK_DOWN },
+ { SDL_SCANCODE_RETURN, RETROK_RETURN },
+ { SDL_SCANCODE_TAB, RETROK_TAB },
+ { SDL_SCANCODE_INSERT, RETROK_INSERT },
+ { SDL_SCANCODE_DELETE, RETROK_DELETE },
+ { SDL_SCANCODE_RSHIFT, RETROK_RSHIFT },
+ { SDL_SCANCODE_LSHIFT, RETROK_LSHIFT },
+ { SDL_SCANCODE_LCTRL, RETROK_LCTRL },
+ { SDL_SCANCODE_END, RETROK_END },
+ { SDL_SCANCODE_HOME, RETROK_HOME },
+ { SDL_SCANCODE_PAGEDOWN, RETROK_PAGEDOWN },
+ { SDL_SCANCODE_PAGEUP, RETROK_PAGEUP },
+ { SDL_SCANCODE_LALT, RETROK_LALT },
+ { SDL_SCANCODE_SPACE, RETROK_SPACE },
+ { SDL_SCANCODE_ESCAPE, RETROK_ESCAPE },
+ { SDL_SCANCODE_BACKSPACE, RETROK_BACKSPACE },
+ { SDL_SCANCODE_KP_ENTER, RETROK_KP_ENTER },
+ { SDL_SCANCODE_KP_PLUS, RETROK_KP_PLUS },
+ { SDL_SCANCODE_KP_MINUS, RETROK_KP_MINUS },
+ { SDL_SCANCODE_KP_MULTIPLY, RETROK_KP_MULTIPLY },
+ { SDL_SCANCODE_KP_DIVIDE, RETROK_KP_DIVIDE },
+ { SDL_SCANCODE_GRAVE, RETROK_BACKQUOTE },
+ { SDL_SCANCODE_PAUSE, RETROK_PAUSE },
+ { SDL_SCANCODE_KP_0, RETROK_KP0 },
+ { SDL_SCANCODE_KP_1, RETROK_KP1 },
+ { SDL_SCANCODE_KP_2, RETROK_KP2 },
+ { SDL_SCANCODE_KP_3, RETROK_KP3 },
+ { SDL_SCANCODE_KP_4, RETROK_KP4 },
+ { SDL_SCANCODE_KP_5, RETROK_KP5 },
+ { SDL_SCANCODE_KP_6, RETROK_KP6 },
+ { SDL_SCANCODE_KP_7, RETROK_KP7 },
+ { SDL_SCANCODE_KP_8, RETROK_KP8 },
+ { SDL_SCANCODE_KP_9, RETROK_KP9 },
+ { SDL_SCANCODE_0, RETROK_0 },
+ { SDL_SCANCODE_1, RETROK_1 },
+ { SDL_SCANCODE_2, RETROK_2 },
+ { SDL_SCANCODE_3, RETROK_3 },
+ { SDL_SCANCODE_4, RETROK_4 },
+ { SDL_SCANCODE_5, RETROK_5 },
+ { SDL_SCANCODE_6, RETROK_6 },
+ { SDL_SCANCODE_7, RETROK_7 },
+ { SDL_SCANCODE_8, RETROK_8 },
+ { SDL_SCANCODE_9, RETROK_9 },
+ { SDL_SCANCODE_F1, RETROK_F1 },
+ { SDL_SCANCODE_F2, RETROK_F2 },
+ { SDL_SCANCODE_F3, RETROK_F3 },
+ { SDL_SCANCODE_F4, RETROK_F4 },
+ { SDL_SCANCODE_F5, RETROK_F5 },
+ { SDL_SCANCODE_F6, RETROK_F6 },
+ { SDL_SCANCODE_F7, RETROK_F7 },
+ { SDL_SCANCODE_F8, RETROK_F8 },
+ { SDL_SCANCODE_F9, RETROK_F9 },
+ { SDL_SCANCODE_F10, RETROK_F10 },
+ { SDL_SCANCODE_F11, RETROK_F11 },
+ { SDL_SCANCODE_F12, RETROK_F12 },
+ { SDL_SCANCODE_A, RETROK_a },
+ { SDL_SCANCODE_B, RETROK_b },
+ { SDL_SCANCODE_C, RETROK_c },
+ { SDL_SCANCODE_D, RETROK_d },
+ { SDL_SCANCODE_E, RETROK_e },
+ { SDL_SCANCODE_F, RETROK_f },
+ { SDL_SCANCODE_G, RETROK_g },
+ { SDL_SCANCODE_H, RETROK_h },
+ { SDL_SCANCODE_I, RETROK_i },
+ { SDL_SCANCODE_J, RETROK_j },
+ { SDL_SCANCODE_K, RETROK_k },
+ { SDL_SCANCODE_L, RETROK_l },
+ { SDL_SCANCODE_M, RETROK_m },
+ { SDL_SCANCODE_N, RETROK_n },
+ { SDL_SCANCODE_O, RETROK_o },
+ { SDL_SCANCODE_P, RETROK_p },
+ { SDL_SCANCODE_Q, RETROK_q },
+ { SDL_SCANCODE_R, RETROK_r },
+ { SDL_SCANCODE_S, RETROK_s },
+ { SDL_SCANCODE_T, RETROK_t },
+ { SDL_SCANCODE_U, RETROK_u },
+ { SDL_SCANCODE_V, RETROK_v },
+ { SDL_SCANCODE_W, RETROK_w },
+ { SDL_SCANCODE_X, RETROK_x },
+ { SDL_SCANCODE_Y, RETROK_y },
+ { SDL_SCANCODE_Z, RETROK_z },
+ { 0, RETROK_UNKNOWN },
+};
+#endif
+
#ifdef HAVE_DINPUT
const struct rarch_key_map rarch_key_map_dinput[] = {
{ DIK_LEFT, RETROK_LEFT },
diff --git a/input/input_common.h b/input/input_common.h
index d72346df2e..407aa8918b 100644
--- a/input/input_common.h
+++ b/input/input_common.h
@@ -104,6 +104,7 @@ extern const rarch_joypad_driver_t linuxraw_joypad;
extern const rarch_joypad_driver_t udev_joypad;
extern const rarch_joypad_driver_t winxinput_joypad; // Named as such to avoid confusion with xb1/360 port code
extern const rarch_joypad_driver_t sdl_joypad;
+extern const rarch_joypad_driver_t sdl2_joypad;
extern const rarch_joypad_driver_t ps3_joypad;
extern const rarch_joypad_driver_t psp_joypad;
extern const rarch_joypad_driver_t xdk_joypad;
@@ -120,6 +121,7 @@ struct rarch_key_map
extern const struct rarch_key_map rarch_key_map_x11[];
extern const struct rarch_key_map rarch_key_map_sdl[];
+extern const struct rarch_key_map rarch_key_map_sdl2[];
extern const struct rarch_key_map rarch_key_map_dinput[];
extern const struct rarch_key_map rarch_key_map_rwebinput[];
extern const struct rarch_key_map rarch_key_map_linux[];
diff --git a/input/sdl2_input.c b/input/sdl2_input.c
new file mode 100644
index 0000000000..f67440ba37
--- /dev/null
+++ b/input/sdl2_input.c
@@ -0,0 +1,324 @@
+/* RetroArch - A frontend for libretro.
+ * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
+ *
+ * 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 .
+ */
+
+#include "../driver.h"
+
+#include "SDL.h"
+#include "../gfx/gfx_context.h"
+#include "../boolean.h"
+#include "../general.h"
+#include
+#include
+#include "../libretro.h"
+#include "input_common.h"
+
+typedef struct sdl2_input
+{
+ const rarch_joypad_driver_t *joypad;
+
+ int mouse_x, mouse_y;
+ int mouse_abs_x, mouse_abs_y;
+ int mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd, mouse_wl, mouse_wr;
+} sdl2_input_t;
+
+static void *sdl2_input_init(void)
+{
+ input_init_keyboard_lut(rarch_key_map_sdl2);
+ sdl2_input_t *sdl = (sdl2_input_t*)calloc(1, sizeof(*sdl));
+ if (!sdl)
+ return NULL;
+
+ sdl->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
+ return sdl;
+}
+
+static bool sdl2_key_pressed(int key)
+{
+ if (key >= RETROK_LAST)
+ return false;
+
+ int sym = input_translate_rk_to_keysym((enum retro_key)key);
+
+ int num_keys;
+ const uint8_t *keymap = SDL_GetKeyboardState(&num_keys);
+ if (sym < 0 || sym >= num_keys)
+ return false;
+
+ return keymap[sym];
+}
+
+static bool sdl2_is_pressed(sdl2_input_t *sdl, unsigned port_num, const struct retro_keybind *binds, unsigned key)
+{
+ if (sdl2_key_pressed(binds[key].key))
+ return true;
+
+ return input_joypad_pressed(sdl->joypad, port_num, binds, key);
+}
+
+static int16_t sdl2_analog_pressed(sdl2_input_t *sdl, const struct retro_keybind *binds,
+ unsigned index, unsigned id)
+{
+ unsigned id_minus = 0;
+ unsigned id_plus = 0;
+ input_conv_analog_id_to_bind_id(index, id, &id_minus, &id_plus);
+
+ int16_t pressed_minus = sdl2_key_pressed(binds[id_minus].key) ? -0x7fff : 0;
+ int16_t pressed_plus = sdl2_key_pressed(binds[id_plus].key) ? 0x7fff : 0;
+ return pressed_plus + pressed_minus;
+}
+
+static bool sdl2_bind_button_pressed(void *data, int key)
+{
+ const struct retro_keybind *binds = g_settings.input.binds[0];
+ if (key >= 0 && key < RARCH_BIND_LIST_END)
+ return sdl2_is_pressed((sdl2_input_t*)data, 0, binds, key);
+ else
+ return false;
+}
+
+static int16_t sdl2_joypad_device_state(sdl2_input_t *sdl, const struct retro_keybind **binds_,
+ unsigned port_num, unsigned id)
+{
+ const struct retro_keybind *binds = binds_[port_num];
+ if (id < RARCH_BIND_LIST_END)
+ return binds[id].valid && sdl2_is_pressed(sdl, port_num, binds, id);
+ else
+ return 0;
+}
+
+static int16_t sdl2_analog_device_state(sdl2_input_t *sdl, const struct retro_keybind **binds,
+ unsigned port_num, unsigned index, unsigned id)
+{
+ int16_t ret = sdl2_analog_pressed(sdl, binds[port_num], index, id);
+ if (!ret)
+ ret = input_joypad_analog(sdl->joypad, port_num, index, id, binds[port_num]);
+ return ret;
+}
+
+static int16_t sdl2_keyboard_device_state(sdl2_input_t *sdl, unsigned id)
+{
+ return sdl2_key_pressed(id);
+}
+
+static int16_t sdl2_mouse_device_state(sdl2_input_t *sdl, unsigned id)
+{
+ switch (id)
+ {
+ case RETRO_DEVICE_ID_MOUSE_LEFT:
+ return sdl->mouse_l;
+ case RETRO_DEVICE_ID_MOUSE_RIGHT:
+ return sdl->mouse_r;
+ case RETRO_DEVICE_ID_MOUSE_WHEELUP:
+ return sdl->mouse_wu;
+ case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
+ return sdl->mouse_wd;
+ case RETRO_DEVICE_ID_MOUSE_X:
+ return sdl->mouse_x;
+ case RETRO_DEVICE_ID_MOUSE_Y:
+ return sdl->mouse_y;
+ case RETRO_DEVICE_ID_MOUSE_MIDDLE:
+ return sdl->mouse_m;
+ default:
+ return 0;
+ }
+}
+
+static int16_t sdl2_pointer_device_state(sdl2_input_t *sdl, unsigned index, unsigned id, bool screen)
+{
+ if (index != 0)
+ return 0;
+
+ int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0;
+ bool valid = input_translate_coord_viewport(sdl->mouse_abs_x, sdl->mouse_abs_y,
+ &res_x, &res_y, &res_screen_x, &res_screen_y);
+
+ if (!valid)
+ return 0;
+
+ if (screen)
+ {
+ res_x = res_screen_x;
+ res_y = res_screen_y;
+ }
+
+ bool inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
+
+ if (!inside)
+ return 0;
+
+ switch (id)
+ {
+ case RETRO_DEVICE_ID_POINTER_X:
+ return res_x;
+ case RETRO_DEVICE_ID_POINTER_Y:
+ return res_y;
+ case RETRO_DEVICE_ID_POINTER_PRESSED:
+ return sdl->mouse_l;
+ default:
+ return 0;
+ }
+}
+
+static int16_t sdl2_lightgun_device_state(sdl2_input_t *sdl, unsigned id)
+{
+ switch (id)
+ {
+ case RETRO_DEVICE_ID_LIGHTGUN_X:
+ return sdl->mouse_x;
+ case RETRO_DEVICE_ID_LIGHTGUN_Y:
+ return sdl->mouse_y;
+ case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
+ return sdl->mouse_l;
+ case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
+ return sdl->mouse_m;
+ case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
+ return sdl->mouse_r;
+ case RETRO_DEVICE_ID_LIGHTGUN_START:
+ return sdl->mouse_m && sdl->mouse_r;
+ case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
+ return sdl->mouse_m && sdl->mouse_l;
+ default:
+ return 0;
+ }
+}
+
+static int16_t sdl2_input_state(void *data_, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned index, unsigned id)
+{
+ sdl2_input_t *data = (sdl2_input_t*)data_;
+ switch (device)
+ {
+ case RETRO_DEVICE_JOYPAD:
+ return sdl2_joypad_device_state(data, binds, port, id);
+ case RETRO_DEVICE_ANALOG:
+ return sdl2_analog_device_state(data, binds, port, index, id);
+ case RETRO_DEVICE_MOUSE:
+ return sdl2_mouse_device_state(data, id);
+ case RETRO_DEVICE_POINTER:
+ case RARCH_DEVICE_POINTER_SCREEN:
+ return sdl2_pointer_device_state(data, index, id, device == RARCH_DEVICE_POINTER_SCREEN);
+ case RETRO_DEVICE_KEYBOARD:
+ return sdl2_keyboard_device_state(data, id);
+ case RETRO_DEVICE_LIGHTGUN:
+ return sdl2_lightgun_device_state(data, id);
+
+ default:
+ return 0;
+ }
+}
+
+static void sdl2_input_free(void *data)
+{
+ if (!data)
+ return;
+
+ // Flush out all pending events.
+ SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
+
+ sdl2_input_t *sdl = (sdl2_input_t*)data;
+
+ if (sdl->joypad)
+ sdl->joypad->destroy();
+
+ free(data);
+}
+
+static void sdl2_grab_mouse(void *data, bool state)
+{
+ sdl2_input_t *sdl = (sdl2_input_t*)data;
+
+ if (driver.video == &video_sdl2)
+ {
+ /* first member of sdl2_video_t is the window */
+ struct temp{
+ SDL_Window *w;
+ };
+ SDL_SetWindowGrab(((struct temp*)driver.video_data)->w,
+ state ? SDL_TRUE : SDL_FALSE);
+ }
+}
+
+static bool sdl2_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength)
+{
+ sdl2_input_t *sdl = (sdl2_input_t*)data;
+ return input_joypad_set_rumble(sdl->joypad, port, effect, strength);
+}
+
+static const rarch_joypad_driver_t *sdl2_get_joypad_driver(void *data)
+{
+ sdl2_input_t *sdl = (sdl2_input_t*)data;
+ return sdl->joypad;
+}
+
+static void sdl2_input_poll(void *data)
+{
+ sdl2_input_t *sdl = (sdl2_input_t*)data;
+
+ SDL_Event event;
+
+ SDL_PumpEvents();
+ while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEWHEEL, SDL_MOUSEWHEEL) > 0)
+ {
+ if (event.type == SDL_MOUSEWHEEL)
+ {
+ sdl->mouse_wu = event.wheel.y < 0;
+ sdl->mouse_wd = event.wheel.y > 0;
+ sdl->mouse_wl = event.wheel.x < 0;
+ sdl->mouse_wr = event.wheel.x > 0;
+ break;
+ }
+ }
+
+ if (sdl->joypad)
+ sdl->joypad->poll();
+
+ uint8_t btn = SDL_GetRelativeMouseState(&sdl->mouse_x, &sdl->mouse_y);
+ SDL_GetMouseState(&sdl->mouse_abs_x, &sdl->mouse_abs_y);
+
+ sdl->mouse_l = SDL_BUTTON(SDL_BUTTON_LEFT) & btn ? 1 : 0;
+ sdl->mouse_r = SDL_BUTTON(SDL_BUTTON_RIGHT) & btn ? 1 : 0;
+ sdl->mouse_m = SDL_BUTTON(SDL_BUTTON_MIDDLE) & btn ? 1 : 0;
+}
+
+static uint64_t sdl2_get_capabilities(void *data)
+{
+ uint64_t caps = 0;
+
+ caps |= (1 << RETRO_DEVICE_JOYPAD);
+ caps |= (1 << RETRO_DEVICE_MOUSE);
+ caps |= (1 << RETRO_DEVICE_KEYBOARD);
+ caps |= (1 << RETRO_DEVICE_LIGHTGUN);
+ caps |= (1 << RETRO_DEVICE_POINTER);
+ caps |= (1 << RETRO_DEVICE_ANALOG);
+
+ return caps;
+}
+
+const input_driver_t input_sdl2 = {
+ sdl2_input_init,
+ sdl2_input_poll,
+ sdl2_input_state,
+ sdl2_bind_button_pressed,
+ sdl2_input_free,
+ NULL,
+ NULL,
+ NULL,
+ sdl2_get_capabilities,
+ NULL,
+ "sdl2",
+ sdl2_grab_mouse,
+ sdl2_set_rumble,
+ sdl2_get_joypad_driver,
+};
+
diff --git a/input/sdl2_joypad.c b/input/sdl2_joypad.c
new file mode 100644
index 0000000000..f934abccdc
--- /dev/null
+++ b/input/sdl2_joypad.c
@@ -0,0 +1,315 @@
+/* RetroArch - A frontend for libretro.
+ * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
+ *
+ * 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 .
+ */
+
+#include "input_common.h"
+#include "SDL.h"
+#include "../general.h"
+
+typedef struct _sdl2_joypad
+{
+ SDL_Joystick *joypad;
+ SDL_Haptic *haptic;
+ int rumble_effect; // -1 = not initialized, -2 = error/unsupported
+ unsigned num_axes;
+ unsigned num_buttons;
+ unsigned num_hats;
+ unsigned num_balls;
+} sdl2_joypad_t;
+
+static sdl2_joypad_t g_pads[MAX_PLAYERS];
+static bool has_haptic;
+
+static void sdl2_joypad_destroy(void)
+{
+ unsigned i;
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+
+ }
+
+ SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
+ memset(g_pads, 0, sizeof(g_pads));
+}
+
+static void sdl2_joypad_connect(int id)
+{
+ sdl2_joypad_t *pad = &g_pads[id];
+ pad->joypad = SDL_JoystickOpen(id);
+ if (!pad->joypad)
+ {
+ RARCH_ERR("[SDL]: Couldn't open SDL joystick #%u.\n", id);
+ return;
+ }
+
+ RARCH_LOG("[SDL]: Joypad #%u connected: %s.\n",
+ id, SDL_JoystickName(pad->joypad));
+
+ pad->haptic = has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL;
+
+ if (has_haptic && !pad->haptic)
+ RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n",
+ id, SDL_GetError());
+
+ pad->rumble_effect = -1;
+
+ if (pad->haptic)
+ {
+ SDL_HapticEffect efx;
+ efx.type = SDL_HAPTIC_LEFTRIGHT;
+ efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
+ efx.leftright.large_magnitude = efx.leftright.small_magnitude = 0x4000;
+ efx.leftright.length = 5000;
+
+ if (SDL_HapticEffectSupported(pad->haptic, &efx) == SDL_FALSE)
+ {
+ pad->rumble_effect = -2;
+ RARCH_WARN("[SDL]: Joypad #%u does not support rumble.\n", id);
+ }
+ }
+
+ pad->num_axes = SDL_JoystickNumAxes(pad->joypad);
+ pad->num_buttons = SDL_JoystickNumButtons(pad->joypad);
+ pad->num_hats = SDL_JoystickNumHats(pad->joypad);
+ pad->num_balls = SDL_JoystickNumBalls(pad->joypad);
+
+ RARCH_LOG("[SDL]: Joypad #%u has: %u axes, %u buttons, %u hats and %u trackballs.\n",
+ id, pad->num_axes, pad->num_buttons, pad->num_hats, pad->num_balls);
+}
+
+static void sdl2_joypad_disconnect(int id)
+{
+ RARCH_LOG("[SDL]: Joypad #%u disconnected.\n", id);
+
+ if (g_pads[id].haptic)
+ SDL_HapticClose(g_pads[id].haptic);
+
+ if (g_pads[id].joypad)
+ SDL_JoystickClose(g_pads[id].joypad);
+
+ memset(&g_pads[id], 0, sizeof(g_pads[id]));
+}
+
+static bool sdl2_joypad_init(void)
+{
+ unsigned i;
+
+ has_haptic = false;
+ if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
+ {
+ RARCH_WARN("[SDL]: Failed to initialize joystick interface: %s\n",
+ SDL_GetError());
+ return false;
+ }
+
+ SDL_JoystickEventState(SDL_ENABLE);
+
+ // TODO: Add SDL_GameController support.
+// if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0)
+// RARCH_LOG("[SDL]: Failed to initialize game controller interface: %s\n",
+// SDL_GetError());
+
+ if (SDL_Init(SDL_INIT_HAPTIC) < 0)
+ RARCH_WARN("[SDL]: Failed to initialize haptic device support: %s\n",
+ SDL_GetError());
+ else
+ has_haptic = true;
+
+ unsigned num_sticks = SDL_NumJoysticks();
+ if (num_sticks > MAX_PLAYERS)
+ num_sticks = MAX_PLAYERS;
+
+ for (i = 0; i < num_sticks; i++)
+ {
+ sdl2_joypad_connect(i);
+ }
+
+ num_sticks = 0;
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+ if (g_pads[i].joypad)
+ num_sticks++;
+ }
+
+ if (num_sticks == 0)
+ goto error;
+
+ return true;
+
+error:
+ sdl2_joypad_destroy();
+ return false;
+}
+
+static bool sdl2_joypad_button(unsigned port, uint16_t joykey)
+{
+ if (joykey == NO_BTN)
+ return false;
+
+ const sdl2_joypad_t *pad = &g_pads[port];
+ if (!pad->joypad)
+ return false;
+
+ // Check hat.
+ if (GET_HAT_DIR(joykey))
+ {
+ uint16_t hat = GET_HAT(joykey);
+ if (hat >= pad->num_hats)
+ return false;
+
+ Uint8 dir = SDL_JoystickGetHat(pad->joypad, hat);
+ switch (GET_HAT_DIR(joykey))
+ {
+ case HAT_UP_MASK:
+ return dir & SDL_HAT_UP;
+ case HAT_DOWN_MASK:
+ return dir & SDL_HAT_DOWN;
+ case HAT_LEFT_MASK:
+ return dir & SDL_HAT_LEFT;
+ case HAT_RIGHT_MASK:
+ return dir & SDL_HAT_RIGHT;
+ default:
+ return false;
+ }
+ }
+ else // Check the button
+ {
+ if (joykey < pad->num_buttons && SDL_JoystickGetButton(pad->joypad, joykey))
+ return true;
+
+ return false;
+ }
+}
+
+static int16_t sdl2_joypad_axis(unsigned port, uint32_t joyaxis)
+{
+ if (joyaxis == AXIS_NONE)
+ return 0;
+
+ const sdl2_joypad_t *pad = &g_pads[port];
+ if (!pad->joypad)
+ return false;
+
+ Sint16 val = 0;
+ if (AXIS_NEG_GET(joyaxis) < pad->num_axes)
+ {
+ val = SDL_JoystickGetAxis(pad->joypad, AXIS_NEG_GET(joyaxis));
+
+ if (val > 0)
+ val = 0;
+ else if (val < -0x7fff) // -0x8000 can cause trouble if we later abs() it.
+ val = -0x7fff;
+ }
+ else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
+ {
+ val = SDL_JoystickGetAxis(pad->joypad, AXIS_POS_GET(joyaxis));
+
+ if (val < 0)
+ val = 0;
+ }
+
+ return val;
+}
+
+static void sdl2_joypad_poll(void)
+{
+ SDL_Event event;
+
+ SDL_PumpEvents();
+ while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0)
+ {
+ if (event.type == SDL_JOYDEVICEADDED)
+ {
+ sdl2_joypad_connect(event.jdevice.which);
+ }
+ else if (event.type == SDL_JOYDEVICEREMOVED)
+ {
+ sdl2_joypad_disconnect(event.jdevice.which);
+ }
+ }
+}
+
+static bool sdl2_joypad_set_rumble(unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
+{
+ SDL_HapticEffect efx;
+ memset(&efx, 0, sizeof(efx));
+
+ sdl2_joypad_t *joypad = &g_pads[pad];
+
+ if (!joypad->joypad || !joypad->haptic)
+ return false;
+
+ efx.type = SDL_HAPTIC_LEFTRIGHT;
+ efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
+ efx.leftright.length = 5000;
+
+ switch (effect)
+ {
+ case RETRO_RUMBLE_STRONG: efx.leftright.large_magnitude = strength; break;
+ case RETRO_RUMBLE_WEAK: efx.leftright.small_magnitude = strength; break;
+ default: return false;
+ }
+
+ if (joypad->rumble_effect == -1)
+ {
+ joypad->rumble_effect = SDL_HapticNewEffect(g_pads[pad].haptic, &efx);
+ if (joypad->rumble_effect < 0)
+ {
+ RARCH_WARN("[SDL]: Failed to create rumble effect for joypad %u: %s\n",
+ pad, SDL_GetError());
+ joypad->rumble_effect = -2;
+ return false;
+ }
+ }
+ else if (joypad->rumble_effect >= 0)
+ SDL_HapticUpdateEffect(joypad->haptic, joypad->rumble_effect, &efx);
+
+ if (joypad->rumble_effect < 0)
+ return false;
+
+ if (SDL_HapticRunEffect(joypad->haptic, joypad->rumble_effect, 1) < 0)
+ {
+ RARCH_WARN("[SDL]: Failed to set rumble effect on joypad %u: %s\n",
+ pad, SDL_GetError());
+ return false;
+ }
+
+ return true;
+}
+
+static bool sdl2_joypad_query_pad(unsigned pad)
+{
+ return pad < MAX_PLAYERS && g_pads[pad].joypad;
+}
+
+static const char *sdl2_joypad_name(unsigned pad)
+{
+ if (pad >= MAX_PLAYERS)
+ return NULL;
+
+ return SDL_JoystickName(g_pads[pad].joypad);
+}
+
+const rarch_joypad_driver_t sdl2_joypad = {
+ sdl2_joypad_init,
+ sdl2_joypad_query_pad,
+ sdl2_joypad_destroy,
+ sdl2_joypad_button,
+ sdl2_joypad_axis,
+ sdl2_joypad_poll,
+ sdl2_joypad_set_rumble,
+ sdl2_joypad_name,
+ "sdl2",
+};
+
diff --git a/qb/config.libs.sh b/qb/config.libs.sh
index 4de145f5d3..1680d94d90 100644
--- a/qb/config.libs.sh
+++ b/qb/config.libs.sh
@@ -160,6 +160,18 @@ check_lib COREAUDIO "-framework AudioUnit" AudioUnitInitialize
check_pkgconf SDL sdl 1.2.10
+if [ "$HAVE_SDL2" == 'yes' ]; then
+ check_pkgconf SDL2 sdl2 2.0.0
+
+ if [ "$HAVE_SDL2" == 'yes' ] && [ "$HAVE_SDL" == 'yes' ]; then
+ echo "SDL drivers will be replaced by SDL2 ones."
+ HAVE_SDL=no
+ elif [ "$HAVE_SDL2" == 'no' ]; then
+ echo "SDL2 not found, skipping."
+ HAVE_SDL2=no
+ fi
+fi
+
if [ "$HAVE_OPENGL" != 'no' ] && [ "$HAVE_GLES" != 'yes' ]; then
if [ "$OS" = 'Darwin' ]; then
check_lib CG "-framework Cg" cgCreateContext
@@ -278,6 +290,6 @@ add_define_make OS "$OS"
# Creates config.mk and config.h.
add_define_make GLOBAL_CONFIG_DIR "$GLOBAL_CONFIG_DIR"
-VARS="RGUI LAKKA ALSA OSS OSS_BSD OSS_LIB AL RSOUND ROAR JACK COREAUDIO PULSE SDL OPENGL LIMA OMAP GLES GLES3 VG EGL KMS GBM DRM DYLIB GETOPT_LONG THREADS CG LIBXML2 ZLIB DYNAMIC FFMPEG AVCODEC AVFORMAT AVUTIL SWSCALE FREETYPE XKBCOMMON XVIDEO X11 XEXT XF86VM XINERAMA WAYLAND MALI_FBDEV NETPLAY NETWORK_CMD STDIN_CMD COMMAND SOCKET_LEGACY FBO STRL STRCASESTR MMAP PYTHON FFMPEG_ALLOC_CONTEXT3 FFMPEG_AVCODEC_OPEN2 FFMPEG_AVIO_OPEN FFMPEG_AVFORMAT_WRITE_HEADER FFMPEG_AVFORMAT_NEW_STREAM FFMPEG_AVCODEC_ENCODE_AUDIO2 FFMPEG_AVCODEC_ENCODE_VIDEO2 BSV_MOVIE VIDEOCORE NEON FLOATHARD FLOATSOFTFP UDEV V4L2 AV_CHANNEL_LAYOUT"
+VARS="RGUI LAKKA ALSA OSS OSS_BSD OSS_LIB AL RSOUND ROAR JACK COREAUDIO PULSE SDL SDL2 OPENGL LIMA OMAP GLES GLES3 VG EGL KMS GBM DRM DYLIB GETOPT_LONG THREADS CG LIBXML2 ZLIB DYNAMIC FFMPEG AVCODEC AVFORMAT AVUTIL SWSCALE FREETYPE XKBCOMMON XVIDEO X11 XEXT XF86VM XINERAMA WAYLAND MALI_FBDEV NETPLAY NETWORK_CMD STDIN_CMD COMMAND SOCKET_LEGACY FBO STRL STRCASESTR MMAP PYTHON FFMPEG_ALLOC_CONTEXT3 FFMPEG_AVCODEC_OPEN2 FFMPEG_AVIO_OPEN FFMPEG_AVFORMAT_WRITE_HEADER FFMPEG_AVFORMAT_NEW_STREAM FFMPEG_AVCODEC_ENCODE_AUDIO2 FFMPEG_AVCODEC_ENCODE_VIDEO2 BSV_MOVIE VIDEOCORE NEON FLOATHARD FLOATSOFTFP UDEV V4L2 AV_CHANNEL_LAYOUT"
create_config_make config.mk $VARS
create_config_header config.h $VARS
diff --git a/qb/config.params.sh b/qb/config.params.sh
index 06000ee69b..57915b8cd8 100644
--- a/qb/config.params.sh
+++ b/qb/config.params.sh
@@ -2,6 +2,7 @@ HAVE_RGUI=yes # Disable RGUI
HAVE_LAKKA=no # Enable Lakka menu
HAVE_DYNAMIC=yes # Disable dynamic loading of libretro library
HAVE_SDL=auto # SDL support
+HAVE_SDL2=no # SDL2 support (disables SDL 1.x)
HAVE_UDEV=auto # Udev/Evdev gamepad support
HAVE_LIBRETRO= # libretro library used
HAVE_MAN_DIR= # Manpage install directory
diff --git a/retroarch.c b/retroarch.c
index 9613113038..9d4e00b8d3 100644
--- a/retroarch.c
+++ b/retroarch.c
@@ -823,6 +823,7 @@ static void print_features(void)
puts("");
puts("Features:");
_PSUPP(sdl, "SDL", "SDL drivers");
+ _PSUPP(sdl2, "SDL2", "SDL2 drivers");
_PSUPP(x11, "X11", "X11 drivers");
_PSUPP(wayland, "wayland", "Wayland drivers");
_PSUPP(thread, "Threads", "Threading support");