(SDL2) Add input driver

This commit is contained in:
Higor Eurípedes 2014-08-10 15:00:21 -03:00
parent 1ea504fcfe
commit 46beb117ef
7 changed files with 439 additions and 13 deletions

View File

@ -214,7 +214,7 @@ ifeq ($(HAVE_SDL), 1)
endif
ifeq ($(HAVE_SDL2), 1)
OBJ += gfx/sdl2_gfx.o
OBJ += gfx/sdl2_gfx.o input/sdl2_input.o
DEFINES += $(SDL2_CFLAGS) $(BSD_LOCAL_INC)
LIBS += $(SDL2_LIBS)
endif

View File

@ -155,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

View File

@ -627,6 +627,7 @@ 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;

View File

@ -47,12 +47,12 @@ typedef struct sdl2_tex
typedef struct sdl_video
{
SDL_Window *window;
SDL_Renderer *renderer;
sdl2_tex_t frame;
sdl2_tex_t menu;
sdl2_tex_t font;
SDL_Window *window;
bool gl;
bool quitting;
@ -434,17 +434,17 @@ static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **inp
if (input && input_data)
{
// sdl_input = input_sdl.init();
// if (sdl_input)
// {
// *input = &input_sdl;
// *input_data = sdl_input;
// }
// else
// {
void *sdl2_input = input_sdl2.init();
if (sdl2_input)
{
*input = &input_sdl2;
*input_data = sdl2_input;
}
else
{
*input = NULL;
*input_data = NULL;
// }
}
}
return vid;
@ -681,6 +681,12 @@ void sdl2_show_mouse(void *data, bool state)
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
@ -695,7 +701,7 @@ static video_poke_interface_t sdl2_video_poke_interface = {
#endif
sdl2_poke_set_osd_msg,
sdl2_show_mouse,
NULL,
sdl2_grab_mouse_toggle,
NULL,
};

View File

@ -30,7 +30,7 @@
#include <dinput.h>
#endif
#ifdef HAVE_SDL
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
#include "SDL.h"
#endif
@ -464,6 +464,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 },

View File

@ -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[];

324
input/sdl2_input.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "../driver.h"
#include "SDL.h"
#include "../gfx/gfx_context.h"
#include "../boolean.h"
#include "../general.h"
#include <stdint.h>
#include <stdlib.h>
#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 = NULL;//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_Event event;
while (SDL_PollEvent(&event));
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;
while (SDL_PollEvent(&event))
{
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,
};