diff --git a/Makefile b/Makefile index c81682a0da..9d56346158 100644 --- a/Makefile +++ b/Makefile @@ -214,7 +214,7 @@ ifeq ($(HAVE_SDL), 1) endif ifeq ($(HAVE_SDL2), 1) - OBJ += gfx/sdl2_gfx.o input/sdl2_input.o input/sdl2_joypad.o audio/sdl_audio.o + OBJ += gfx/sdl2_gfx.o input/sdl_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) diff --git a/Makefile.win b/Makefile.win index 17c871e04f..73e84bb8b9 100644 --- a/Makefile.win +++ b/Makefile.win @@ -132,7 +132,7 @@ ifeq ($(HAVE_MENU_COMMON), 1) endif ifeq ($(HAVE_SDL2), 1) - OBJ += gfx/sdl2_gfx.o input/sdl2_input.o input/sdl2_joypad.o audio/sdl_audio.o + OBJ += gfx/sdl2_gfx.o input/sdl_input.o input/sdl2_joypad.o audio/sdl_audio.o JOBJ += input/sdl2_joypad.o LIBS += -lSDL2 JLIBS += -lSDL2 diff --git a/driver.c b/driver.c index 95af6b3149..89588b58ec 100644 --- a/driver.c +++ b/driver.c @@ -152,12 +152,9 @@ static const input_driver_t *input_drivers[] = { #if defined(SN_TARGET_PSP2) || defined(PSP) &input_psp, #endif -#ifdef HAVE_SDL +#if defined(HAVE_SDL) || defined(HAVE_SDL2) &input_sdl, #endif -#ifdef HAVE_SDL2 - &input_sdl2, -#endif #ifdef HAVE_DINPUT &input_dinput, #endif diff --git a/driver.h b/driver.h index 97ff3b072e..92bc5e15aa 100644 --- a/driver.h +++ b/driver.h @@ -628,7 +628,6 @@ 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 index a377360d04..1b14cf7335 100644 --- a/gfx/sdl2_gfx.c +++ b/gfx/sdl2_gfx.c @@ -449,11 +449,11 @@ static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **inp if (input && input_data) { - void *sdl2_input = input_sdl2.init(); - if (sdl2_input) + void *sdl_input = input_sdl.init(); + if (sdl_input) { - *input = &input_sdl2; - *input_data = sdl2_input; + *input = &input_sdl; + *input_data = sdl_input; } else { diff --git a/input/input_common.c b/input/input_common.c index bd5122185b..117e5b6e5d 100644 --- a/input/input_common.c +++ b/input/input_common.c @@ -468,7 +468,7 @@ const struct rarch_key_map rarch_key_map_sdl[] = { #endif #ifdef HAVE_SDL2 -const struct rarch_key_map rarch_key_map_sdl2[] = { +const struct rarch_key_map rarch_key_map_sdl[] = { { SDL_SCANCODE_LEFT, RETROK_LEFT }, { SDL_SCANCODE_RIGHT, RETROK_RIGHT }, { SDL_SCANCODE_UP, RETROK_UP }, diff --git a/input/sdl2_input.c b/input/sdl2_input.c deleted file mode 100644 index f67440ba37..0000000000 --- a/input/sdl2_input.c +++ /dev/null @@ -1,324 +0,0 @@ -/* 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/sdl_input.c b/input/sdl_input.c index 085ebbaeec..04c2791078 100644 --- a/input/sdl_input.c +++ b/input/sdl_input.c @@ -30,7 +30,7 @@ typedef struct sdl_input int mouse_x, mouse_y; int mouse_abs_x, mouse_abs_y; - int mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd; + int mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd, mouse_wl, mouse_wr; } sdl_input_t; static void *sdl_input_init(void) @@ -41,6 +41,8 @@ static void *sdl_input_init(void) return NULL; sdl->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); + + RARCH_LOG("[SDL]: Input driver initialized.\n"); return sdl; } @@ -52,7 +54,12 @@ static bool sdl_key_pressed(int key) int sym = input_translate_rk_to_keysym((enum retro_key)key); int num_keys; - Uint8 *keymap = SDL_GetKeyState(&num_keys); + const uint8_t *keymap; +#if HAVE_SDL2 + keymap = SDL_GetKeyboardState(&num_keys); +#else + keymap = SDL_GetKeyState(&num_keys); +#endif if (sym < 0 || sym >= num_keys) return false; @@ -224,8 +231,12 @@ static void sdl_input_free(void *data) return; // Flush out all pending events. +#ifdef HAVE_SDL2 + SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); +#else SDL_Event event; while (SDL_PollEvent(&event)); +#endif sdl_input_t *sdl = (sdl_input_t*)data; @@ -235,6 +246,23 @@ static void sdl_input_free(void *data) free(data); } +#ifdef HAVE_SDL2 +static void sdl_grab_mouse(void *data, bool state) +{ + sdl_input_t *sdl = (sdl_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); + } +} +#endif + static bool sdl_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength) { sdl_input_t *sdl = (sdl_input_t*)data; @@ -254,8 +282,10 @@ static void sdl_poll_mouse(sdl_input_t *sdl) 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; +#ifndef HAVE_SDL2 sdl->mouse_wu = SDL_BUTTON(SDL_BUTTON_WHEELUP) & btn ? 1 : 0; sdl->mouse_wd = SDL_BUTTON(SDL_BUTTON_WHEELDOWN) & btn ? 1 : 0; +#endif } static void sdl_input_poll(void *data) @@ -266,6 +296,21 @@ static void sdl_input_poll(void *data) if (sdl->joypad) sdl->joypad->poll(); sdl_poll_mouse(sdl); + +#ifdef HAVE_SDL2 + SDL_Event event; + 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; + } + } +#endif } static uint64_t sdl_get_capabilities(void *data) @@ -293,8 +338,13 @@ const input_driver_t input_sdl = { NULL, sdl_get_capabilities, NULL, +#ifdef HAVE_SDL2 + "sdl2", + sdl_grab_mouse, +#else "sdl", NULL, +#endif sdl_set_rumble, sdl_get_joypad_driver, };