From c5b342840808654953f669928f79c2a8b1fc2734 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 22 Sep 2019 12:40:40 +0200 Subject: [PATCH] Add sdl2_common.c/.h --- Makefile.common | 3 +- gfx/common/sdl2_common.c | 80 ++++++++++++++++++++++++++++++++ gfx/common/sdl2_common.h | 27 +++++++++++ gfx/drivers/sdl2_gfx.c | 35 +++++--------- gfx/drivers_context/sdl_gl_ctx.c | 24 +++++++--- gfx/video_defines.h | 1 + griffin/griffin.c | 1 + 7 files changed, 139 insertions(+), 32 deletions(-) create mode 100644 gfx/common/sdl2_common.c create mode 100644 gfx/common/sdl2_common.h diff --git a/Makefile.common b/Makefile.common index a4a967f2ec..7c96f65352 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1273,7 +1273,8 @@ endif ifeq ($(HAVE_SDL2), 1) HAVE_SDL_COMMON = 1 - OBJ += gfx/drivers/sdl2_gfx.o + OBJ += gfx/drivers/sdl2_gfx.o \ + gfx/common/sdl2_common.o DEF_FLAGS += $(SDL2_CFLAGS) LIBS += $(SDL2_LIBS) else ifeq ($(HAVE_SDL), 1) diff --git a/gfx/common/sdl2_common.c b/gfx/common/sdl2_common.c new file mode 100644 index 0000000000..fd6853f979 --- /dev/null +++ b/gfx/common/sdl2_common.c @@ -0,0 +1,80 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2011-2019 - Daniel De Matteis + * + * 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 +#include + +#ifdef HAVE_CONFIG_H +#include "../../config.h" +#endif + +#include + +#include "sdl2_common.h" +#include "../../retroarch.h" + +#ifdef HAVE_SDL2 +#include "SDL.h" +#include "SDL_syswm.h" + +void sdl2_set_handles(void *data, enum rarch_display_type display_type) +{ + /* SysWMinfo headers are broken on OSX. */ + SDL_SysWMinfo info; + SDL_Window *window = (SDL_Window*)data; + SDL_VERSION(&info.version); + + if (SDL_GetWindowWMInfo(window, &info) != 1) + return; + + video_driver_display_userdata_set((uintptr_t)window); + + switch (display_type) + { + case RARCH_DISPLAY_WIN32: +#if defined(_WIN32) + video_driver_display_type_set(RARCH_DISPLAY_WIN32); + video_driver_display_set(0); + video_driver_window_set((uintptr_t)info.info.win.window); +#endif + break; + case RARCH_DISPLAY_X11: +#if defined(HAVE_X11) + video_driver_display_type_set(RARCH_DISPLAY_X11); + video_driver_display_set((uintptr_t)info.info.x11.display); + video_driver_window_set((uintptr_t)info.info.x11.window); +#endif + break; + case RARCH_DISPLAY_OSX: +#ifdef HAVE_COCOA + video_driver_display_type_set(RARCH_DISPLAY_OSX); + video_driver_display_set(0); + video_driver_window_set((uintptr_t)info.info.cocoa.window); +#endif + break; + case RARCH_DISPLAY_WAYLAND: +#ifdef HAVE_WAYLAND + video_driver_display_type_set(RARCH_DISPLAY_WAYLAND); + video_driver_display_set((uintptr_t)info.info.wl.display); + video_driver_window_set((uintptr_t)info.info.wl.surface); +#endif + break; + default: + case RARCH_DISPLAY_NONE: + break; + } +} + +#endif diff --git a/gfx/common/sdl2_common.h b/gfx/common/sdl2_common.h new file mode 100644 index 0000000000..fe61ad4d60 --- /dev/null +++ b/gfx/common/sdl2_common.h @@ -0,0 +1,27 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2011-2019 - Daniel De Matteis + * + * 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 . + */ + +#ifndef SDL2_COMMON_H__ +#define SDL2_COMMON_H__ + +#include +#include + +#include "../video_defines.h" + +void sdl2_set_handles(void *data, enum rarch_display_type + display_type); + +#endif diff --git a/gfx/drivers/sdl2_gfx.c b/gfx/drivers/sdl2_gfx.c index e4a2b12c46..e7ac5ed5c6 100644 --- a/gfx/drivers/sdl2_gfx.c +++ b/gfx/drivers/sdl2_gfx.c @@ -36,6 +36,10 @@ #include "SDL.h" #include "SDL_syswm.h" +#ifdef HAVE_SDL2 +#include "../common/sdl2_common.h" +#endif + #include "../font_driver.h" #include "../../configuration.h" @@ -211,29 +215,6 @@ static void sdl2_render_msg(sdl2_video_t *vid, const char *msg) } } -static void sdl2_gfx_set_handles(sdl2_video_t *vid) -{ - /* SysWMinfo headers are broken on OSX. */ -#if defined(_WIN32) || defined(HAVE_X11) - SDL_SysWMinfo info; - SDL_VERSION(&info.version); - - if (SDL_GetWindowWMInfo(vid->window, &info) != 1) - return; - - video_driver_display_userdata_set((uintptr_t)vid->window); -#if defined(_WIN32) - video_driver_display_type_set(RARCH_DISPLAY_WIN32); - video_driver_display_set(0); - video_driver_window_set((uintptr_t)info.info.win.window); -#elif defined(HAVE_X11) - video_driver_display_type_set(RARCH_DISPLAY_X11); - video_driver_display_set((uintptr_t)info.info.x11.display); - video_driver_window_set((uintptr_t)info.info.x11.window); -#endif -#endif -} - static void sdl2_init_renderer(sdl2_video_t *vid) { unsigned flags = SDL_RENDERER_ACCELERATED; @@ -449,7 +430,13 @@ static void *sdl2_gfx_init(const video_info_t *video, sdl2_init_renderer(vid); sdl2_init_font(vid, settings->paths.path_font, settings->floats.video_font_size); - sdl2_gfx_set_handles(vid); +#if defined(_WIN32) + sdl2_set_handles(vid->window, RARCH_DISPLAY_WIN32); +#elif defined(HAVE_X11) + sdl2_set_handles(vid->window, RARCH_DISPLAY_X11); +#elif defined(HAVE_COCOA) + sdl2_set_handles(vid->window, RARCH_DISPLAY_OSX); +#endif sdl_refresh_viewport(vid); diff --git a/gfx/drivers_context/sdl_gl_ctx.c b/gfx/drivers_context/sdl_gl_ctx.c index 65a4eac25e..bc35ca33b6 100644 --- a/gfx/drivers_context/sdl_gl_ctx.c +++ b/gfx/drivers_context/sdl_gl_ctx.c @@ -28,6 +28,10 @@ #include "SDL.h" +#ifdef HAVE_SDL2 +#include "../common/sdl2_common.h" +#endif + static enum gfx_ctx_api sdl_api = GFX_CTX_OPENGL_API; static unsigned g_major = 2; static unsigned g_minor = 1; @@ -214,6 +218,14 @@ static bool sdl_ctx_set_video_mode(void *data, goto error; #ifdef HAVE_SDL2 +#if defined(_WIN32) + sdl2_set_handles(sdl->g_win, RARCH_DISPLAY_WIN32); +#elif defined(HAVE_X11) + sdl2_set_handles(sdl->g_win, RARCH_DISPLAY_X11); +#elif defined(HAVE_COCOA) + sdl2_set_handles(sdl->g_win, RARCH_DISPLAY_OSX); +#endif + if (sdl->g_ctx) video_driver_set_video_cache_context_ack(); else @@ -276,20 +288,18 @@ static void sdl_ctx_get_video_size(void *data, static void sdl_ctx_update_title(void *data, void *data2) { char title[128]; - title[0] = '\0'; video_driver_get_window_title(title, sizeof(title)); -#ifdef HAVE_SDL2 - gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data; - - if (sdl && title[0]) - SDL_SetWindowTitle(sdl->g_win, title); -#else if (title[0]) + { +#ifdef HAVE_SDL2 + SDL_SetWindowTitle((SDL_Window*)video_driver_display_userdata_get(), title); +#else SDL_WM_SetCaption(title, NULL); #endif + } } static void sdl_ctx_check_window(void *data, bool *quit, diff --git a/gfx/video_defines.h b/gfx/video_defines.h index a460c17556..900b9c786b 100644 --- a/gfx/video_defines.h +++ b/gfx/video_defines.h @@ -76,6 +76,7 @@ enum rarch_display_type RARCH_DISPLAY_X11, /* video_display => N/A, video_window => HWND */ RARCH_DISPLAY_WIN32, + RARCH_DISPLAY_WAYLAND, RARCH_DISPLAY_OSX }; diff --git a/griffin/griffin.c b/griffin/griffin.c index 9e40b2d095..314e6b72d8 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -406,6 +406,7 @@ VIDEO DRIVER #ifdef HAVE_SDL2 #include "../gfx/drivers/sdl2_gfx.c" +#include "../gfx/common/sdl2_common.c" #endif #ifdef HAVE_VG