RetroArch/input/drivers/sdl_input.c

439 lines
12 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2015-01-07 17:46:50 +01:00
* Copyright (C) 2014-2015 - Higor Euripedes
*
2012-04-21 23:13:50 +02: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.
*
2012-04-21 23:13:50 +02:00
* 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.
*
2012-04-21 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <boolean.h>
2016-01-21 02:48:00 +01:00
#include <string/stdstring.h>
#include <libretro.h>
2012-09-28 22:38:42 +02:00
#include "SDL.h"
2015-01-12 06:16:52 +01:00
#include "../input_keymaps.h"
#include "../../configuration.h"
#include "../../retroarch.h"
#include "../../verbosity.h"
#include "../../tasks/tasks_internal.h"
2019-02-03 15:49:35 -08:00
/* TODO/FIXME -
* fix game focus toggle */
2012-09-28 22:38:42 +02:00
typedef struct sdl_input
{
2020-08-30 14:41:45 +02:00
int mouse_x;
int mouse_y;
int mouse_abs_x;
int mouse_abs_y;
int mouse_l;
int mouse_r;
int mouse_m;
int mouse_b4;
int mouse_b5;
int mouse_wu;
int mouse_wd;
int mouse_wl;
int mouse_wr;
2012-09-28 22:38:42 +02:00
} sdl_input_t;
static void *sdl_input_init(const char *joypad_driver)
{
sdl_input_t *sdl = (sdl_input_t*)calloc(1, sizeof(*sdl));
if (!sdl)
return NULL;
input_keymaps_init_keyboard_lut(rarch_key_map_sdl);
return sdl;
}
2011-01-08 22:15:02 +01:00
static bool sdl_key_pressed(int key)
{
int num_keys;
2015-03-16 13:59:55 -03:00
#ifdef HAVE_SDL2
2017-06-06 05:17:25 +02:00
const uint8_t *keymap = SDL_GetKeyboardState(&num_keys);
unsigned sym = SDL_GetScancodeFromKey(rarch_keysym_lut[(enum retro_key)key]);
2014-08-11 14:00:30 -03:00
#else
2017-06-06 05:17:25 +02:00
const uint8_t *keymap = SDL_GetKeyState(&num_keys);
unsigned sym = rarch_keysym_lut[(enum retro_key)key];
2014-08-11 14:00:30 -03:00
#endif
2017-06-06 05:17:25 +02:00
2015-03-16 14:26:29 -03:00
if (sym >= (unsigned)num_keys)
2012-09-15 15:17:34 +02:00
return false;
return keymap[sym];
}
static int16_t sdl_pressed_analog(
2020-08-30 16:19:11 +02:00
sdl_input_t *sdl,
2020-08-30 14:41:45 +02:00
const struct retro_keybind *binds,
2014-10-20 20:31:00 +02:00
unsigned idx, unsigned id)
{
2020-08-30 16:19:11 +02:00
int id_minus_key = 0;
int id_plus_key = 0;
unsigned id_minus = 0;
unsigned id_plus = 0;
int16_t ret = 0;
bool id_plus_valid = false;
bool id_minus_valid = false;
input_conv_analog_id_to_bind_id(idx, id, id_minus, id_plus);
2020-08-30 16:19:11 +02:00
id_minus_valid = binds[id_minus].valid;
id_plus_valid = binds[id_plus].valid;
id_minus_key = binds[id_minus].key;
id_plus_key = binds[id_plus].key;
2020-08-30 16:19:11 +02:00
if (id_plus_valid && id_plus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_plus_key];
if (sdl_key_pressed(sym))
ret = 0x7fff;
}
if (id_minus_valid && id_minus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_minus_key];
if (sdl_key_pressed(sym))
ret += -0x7fff;
}
return ret;
}
static int16_t sdl_mouse_device_state(sdl_input_t *sdl, unsigned id)
{
switch (id)
{
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_MOUSE_LEFT:
2011-01-10 14:29:00 +01:00
return sdl->mouse_l;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_MOUSE_RIGHT:
2011-01-10 14:29:00 +01:00
return sdl->mouse_r;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
return sdl->mouse_wu;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
return sdl->mouse_wd;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_MOUSE_X:
2011-01-10 14:29:00 +01:00
return sdl->mouse_x;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_MOUSE_Y:
2011-01-10 14:29:00 +01:00
return sdl->mouse_y;
2014-04-25 23:44:53 +02:00
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return sdl->mouse_m;
2017-11-17 12:00:24 +00:00
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
return sdl->mouse_b4;
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
return sdl->mouse_b5;
}
return 0;
}
2014-10-20 20:31:00 +02:00
static int16_t sdl_pointer_device_state(sdl_input_t *sdl,
unsigned idx, unsigned id, bool screen)
{
struct video_viewport vp;
const int edge_detect = 32700;
bool inside = false;
int16_t res_x = 0;
int16_t res_y = 0;
int16_t res_screen_x = 0;
int16_t res_screen_y = 0;
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
2020-06-10 04:00:40 +02:00
if (!(video_driver_translate_coord_viewport_wrap(
&vp, sdl->mouse_abs_x, sdl->mouse_abs_y,
&res_x, &res_y, &res_screen_x, &res_screen_y)))
return 0;
2013-01-11 16:23:04 +01:00
if (screen)
{
res_x = res_screen_x;
res_y = res_screen_y;
}
2020-06-11 16:58:49 +02:00
inside = (res_x >= -edge_detect)
&& (res_y >= -edge_detect)
&& (res_x <= edge_detect)
&& (res_y <= edge_detect);
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;
2020-06-11 16:58:49 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
return !inside;
}
return 0;
}
2012-04-07 11:55:37 +02:00
static int16_t sdl_lightgun_device_state(sdl_input_t *sdl, unsigned id)
{
switch (id)
{
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_X:
return sdl->mouse_x;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_Y:
return sdl->mouse_y;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
return sdl->mouse_l;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
return sdl->mouse_m;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
return sdl->mouse_r;
2012-04-15 17:08:43 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_START:
return sdl->mouse_m && sdl->mouse_r;
2012-04-15 17:08:43 +02:00
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
return sdl->mouse_m && sdl->mouse_l;
}
return 0;
}
2020-08-30 05:29:32 +02:00
static int16_t sdl_input_state(
void *data,
const input_device_driver_t *joypad,
const input_device_driver_t *sec_joypad,
2020-02-27 07:33:14 +01:00
rarch_joypad_info_t *joypad_info,
const struct retro_keybind **binds,
2014-10-20 20:31:00 +02:00
unsigned port, unsigned device, unsigned idx, unsigned id)
{
sdl_input_t *sdl = (sdl_input_t*)data;
switch (device)
{
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_JOYPAD:
if (id == RETRO_DEVICE_ID_JOYPAD_MASK)
{
unsigned i;
2020-08-30 05:29:32 +02:00
int16_t ret = joypad->state(
joypad_info, binds[port], port);
2019-07-22 01:20:00 +02:00
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
2020-06-12 18:28:07 +02:00
if (binds[port][i].valid)
if (sdl_key_pressed(binds[port][i].key))
2020-06-12 18:28:07 +02:00
ret |= (1 << i);
}
2019-07-22 01:20:00 +02:00
return ret;
}
else
{
if (id < RARCH_BIND_LIST_END)
2020-06-12 18:28:07 +02:00
{
if (binds[port][id].valid)
{
2020-08-30 05:29:32 +02:00
if (button_is_pressed(joypad,
2020-06-12 18:28:07 +02:00
joypad_info, binds[port], port, id))
return 1;
else if (sdl_key_pressed(binds[port][id].key))
return 1;
}
}
}
2019-07-22 01:20:00 +02:00
break;
2012-06-28 17:57:50 +02:00
case RETRO_DEVICE_ANALOG:
2017-06-07 01:45:09 +02:00
if (binds[port])
return sdl_pressed_analog(sdl, binds[port], idx, id);
2017-06-07 01:45:09 +02:00
break;
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_MOUSE:
2020-06-13 17:46:39 +02:00
if (config_get_ptr()->uints.input_mouse_index[ port ] == 0)
return sdl_mouse_device_state(sdl, id);
break;
case RARCH_DEVICE_MOUSE_SCREEN:
2020-06-13 17:46:39 +02:00
if (config_get_ptr()->uints.input_mouse_index[ port ] == 0)
return sdl_mouse_device_state(sdl, id);
break;
case RETRO_DEVICE_POINTER:
2013-01-11 16:23:04 +01:00
case RARCH_DEVICE_POINTER_SCREEN:
if (idx == 0)
2019-07-22 01:20:00 +02:00
return sdl_pointer_device_state(sdl, idx, id,
device == RARCH_DEVICE_POINTER_SCREEN);
break;
case RETRO_DEVICE_KEYBOARD:
2017-06-07 01:45:09 +02:00
return (id < RETROK_LAST) && sdl_key_pressed(id);
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_LIGHTGUN:
2017-06-07 01:45:09 +02:00
return sdl_lightgun_device_state(sdl, id);
}
2014-10-20 20:31:00 +02:00
return 0;
}
static void sdl_input_free(void *data)
{
2016-03-20 05:24:05 +01:00
#ifndef HAVE_SDL2
SDL_Event event;
#endif
sdl_input_t *sdl = (sdl_input_t*)data;
2012-09-28 22:38:42 +02:00
if (!data)
return;
/* Flush out all pending events. */
2014-08-11 14:00:30 -03:00
#ifdef HAVE_SDL2
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
#else
2012-09-28 22:38:42 +02:00
while (SDL_PollEvent(&event));
2014-08-11 14:00:30 -03:00
#endif
2012-09-28 22:38:42 +02:00
free(data);
}
#ifdef HAVE_SDL2
2020-08-30 05:29:32 +02:00
static void sdl2_grab_mouse(void *data, bool state)
{
struct temp
{
SDL_Window *w;
};
2014-08-11 14:00:30 -03:00
if (string_is_not_equal(video_driver_get_ident(), "sdl2"))
2016-01-21 02:48:00 +01:00
return;
/* First member of sdl2_video_t is the window */
SDL_SetWindowGrab(((struct temp*)video_driver_get_ptr(false))->w,
state ? SDL_TRUE : SDL_FALSE);
}
2020-08-30 05:29:32 +02:00
#endif
2014-08-11 14:00:30 -03:00
2020-08-30 05:29:32 +02:00
static bool sdl_set_rumble(
const input_device_driver_t *joypad,
const input_device_driver_t *sec_joypad,
unsigned port,
2014-10-20 20:31:00 +02:00
enum retro_rumble_effect effect, uint16_t strength)
{
2020-08-30 05:29:32 +02:00
if (joypad)
return input_joypad_set_rumble(joypad, port, effect, strength);
2020-08-29 22:22:26 +02:00
return false;
}
2011-01-10 14:29:00 +01:00
static void sdl_poll_mouse(sdl_input_t *sdl)
{
Uint8 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;
2017-11-17 12:00:24 +00:00
sdl->mouse_b4 = (SDL_BUTTON(SDL_BUTTON_X1) & btn) ? 1 : 0;
sdl->mouse_b5 = (SDL_BUTTON(SDL_BUTTON_X2) & btn) ? 1 : 0;
2014-08-11 14:00:30 -03:00
#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;
2014-08-11 14:00:30 -03:00
#endif
2011-01-10 14:29:00 +01:00
}
static void sdl_input_poll(void *data)
{
2015-06-26 18:35:35 +02:00
SDL_Event event;
2020-08-30 05:29:32 +02:00
sdl_input_t *sdl = (sdl_input_t*)data;
2011-06-10 18:01:44 +02:00
SDL_PumpEvents();
sdl_poll_mouse(sdl);
2014-08-11 14:00:30 -03:00
2014-09-02 21:35:28 -03:00
#ifdef HAVE_SDL2
2020-06-10 04:00:40 +02:00
while (SDL_PeepEvents(&event, 1,
SDL_GETEVENT, SDL_KEYDOWN, SDL_MOUSEWHEEL) > 0)
2014-09-02 21:35:28 -03:00
#else
2020-06-10 04:00:40 +02:00
while (SDL_PeepEvents(&event, 1,
SDL_GETEVENT, SDL_KEYEVENTMASK) > 0)
2014-09-02 21:35:28 -03:00
#endif
2014-08-11 14:00:30 -03:00
{
2014-09-02 21:35:28 -03:00
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
{
uint16_t mod = 0;
2020-06-10 04:00:40 +02:00
unsigned code = input_keymaps_translate_keysym_to_rk(
event.key.keysym.sym);
2014-09-02 21:35:28 -03:00
if (event.key.keysym.mod & KMOD_SHIFT)
mod |= RETROKMOD_SHIFT;
if (event.key.keysym.mod & KMOD_CTRL)
mod |= RETROKMOD_CTRL;
if (event.key.keysym.mod & KMOD_ALT)
mod |= RETROKMOD_ALT;
if (event.key.keysym.mod & KMOD_NUM)
mod |= RETROKMOD_NUMLOCK;
if (event.key.keysym.mod & KMOD_CAPS)
mod |= RETROKMOD_CAPSLOCK;
2015-01-29 23:59:46 +01:00
input_keyboard_event(event.type == SDL_KEYDOWN, code, code, mod,
RETRO_DEVICE_KEYBOARD);
2014-09-02 21:35:28 -03:00
}
#ifdef HAVE_SDL2
else if (event.type == SDL_MOUSEWHEEL)
2014-08-11 14:00:30 -03:00
{
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
2014-09-02 21:35:28 -03:00
}
}
static uint64_t sdl_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;
}
2014-09-11 07:06:20 +02:00
input_driver_t input_sdl = {
2011-12-24 13:46:12 +01:00
sdl_input_init,
sdl_input_poll,
sdl_input_state,
sdl_input_free,
NULL,
NULL,
sdl_get_capabilities,
2014-08-11 14:00:30 -03:00
#ifdef HAVE_SDL2
"sdl2",
2020-08-30 05:29:32 +02:00
sdl2_grab_mouse,
2014-08-11 14:00:30 -03:00
#else
2012-09-28 22:38:42 +02:00
"sdl",
2020-08-30 05:29:32 +02:00
NULL, /* grab_mouse */
2014-08-11 14:00:30 -03:00
#endif
NULL,
sdl_set_rumble,
false
};