2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2012-01-08 00:08:18 +00:00
|
|
|
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
2011-01-06 17:34:11 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-01-06 17:34:11 +00:00
|
|
|
* 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 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-01-06 17:34:11 +00:00
|
|
|
* 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 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-01-06 17:34:11 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-24 23:59:46 +00:00
|
|
|
#include "../driver.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2012-05-26 02:47:24 +00:00
|
|
|
#include "../gfx/gfx_context.h"
|
2011-12-24 12:46:12 +00:00
|
|
|
#include "../boolean.h"
|
2011-12-24 23:59:46 +00:00
|
|
|
#include "../general.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2012-04-07 09:55:37 +00:00
|
|
|
#include "../libretro.h"
|
2012-04-21 21:25:32 +00:00
|
|
|
#include "rarch_sdl_input.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-09-13 12:40:33 +00:00
|
|
|
struct key_bind
|
|
|
|
{
|
|
|
|
unsigned sdl;
|
2012-07-28 01:21:37 +00:00
|
|
|
enum retro_key sk;
|
2011-09-13 12:40:33 +00:00
|
|
|
};
|
|
|
|
|
2012-07-28 01:21:37 +00:00
|
|
|
static unsigned keysym_lut[RETROK_LAST];
|
2011-09-13 12:40:33 +00:00
|
|
|
static const struct key_bind lut_binds[] = {
|
2012-07-28 01:21:37 +00:00
|
|
|
{ SDLK_LEFT, RETROK_LEFT },
|
|
|
|
{ SDLK_RIGHT, RETROK_RIGHT },
|
|
|
|
{ SDLK_UP, RETROK_UP },
|
|
|
|
{ SDLK_DOWN, RETROK_DOWN },
|
|
|
|
{ SDLK_RETURN, RETROK_RETURN },
|
|
|
|
{ SDLK_TAB, RETROK_TAB },
|
|
|
|
{ SDLK_INSERT, RETROK_INSERT },
|
|
|
|
{ SDLK_DELETE, RETROK_DELETE },
|
|
|
|
{ SDLK_RSHIFT, RETROK_RSHIFT },
|
|
|
|
{ SDLK_LSHIFT, RETROK_LSHIFT },
|
|
|
|
{ SDLK_LCTRL, RETROK_LCTRL },
|
|
|
|
{ SDLK_END, RETROK_END },
|
|
|
|
{ SDLK_HOME, RETROK_HOME },
|
|
|
|
{ SDLK_PAGEDOWN, RETROK_PAGEDOWN },
|
|
|
|
{ SDLK_PAGEUP, RETROK_PAGEUP },
|
|
|
|
{ SDLK_LALT, RETROK_LALT },
|
|
|
|
{ SDLK_SPACE, RETROK_SPACE },
|
|
|
|
{ SDLK_ESCAPE, RETROK_ESCAPE },
|
|
|
|
{ SDLK_BACKSPACE, RETROK_BACKSPACE },
|
|
|
|
{ SDLK_KP_ENTER, RETROK_KP_ENTER },
|
|
|
|
{ SDLK_KP_PLUS, RETROK_KP_PLUS },
|
|
|
|
{ SDLK_KP_MINUS, RETROK_KP_MINUS },
|
|
|
|
{ SDLK_KP_MULTIPLY, RETROK_KP_MULTIPLY },
|
|
|
|
{ SDLK_KP_DIVIDE, RETROK_KP_DIVIDE },
|
|
|
|
{ SDLK_BACKQUOTE, RETROK_BACKQUOTE },
|
|
|
|
{ SDLK_PAUSE, RETROK_PAUSE },
|
|
|
|
{ SDLK_KP0, RETROK_KP0 },
|
|
|
|
{ SDLK_KP1, RETROK_KP1 },
|
|
|
|
{ SDLK_KP2, RETROK_KP2 },
|
|
|
|
{ SDLK_KP3, RETROK_KP3 },
|
|
|
|
{ SDLK_KP4, RETROK_KP4 },
|
|
|
|
{ SDLK_KP5, RETROK_KP5 },
|
|
|
|
{ SDLK_KP6, RETROK_KP6 },
|
|
|
|
{ SDLK_KP7, RETROK_KP7 },
|
|
|
|
{ SDLK_KP8, RETROK_KP8 },
|
|
|
|
{ SDLK_KP9, RETROK_KP9 },
|
|
|
|
{ SDLK_0, RETROK_0 },
|
|
|
|
{ SDLK_1, RETROK_1 },
|
|
|
|
{ SDLK_2, RETROK_2 },
|
|
|
|
{ SDLK_3, RETROK_3 },
|
|
|
|
{ SDLK_4, RETROK_4 },
|
|
|
|
{ SDLK_5, RETROK_5 },
|
|
|
|
{ SDLK_6, RETROK_6 },
|
|
|
|
{ SDLK_7, RETROK_7 },
|
|
|
|
{ SDLK_8, RETROK_8 },
|
|
|
|
{ SDLK_9, RETROK_9 },
|
|
|
|
{ SDLK_F1, RETROK_F1 },
|
|
|
|
{ SDLK_F2, RETROK_F2 },
|
|
|
|
{ SDLK_F3, RETROK_F3 },
|
|
|
|
{ SDLK_F4, RETROK_F4 },
|
|
|
|
{ SDLK_F5, RETROK_F5 },
|
|
|
|
{ SDLK_F6, RETROK_F6 },
|
|
|
|
{ SDLK_F7, RETROK_F7 },
|
|
|
|
{ SDLK_F8, RETROK_F8 },
|
|
|
|
{ SDLK_F9, RETROK_F9 },
|
|
|
|
{ SDLK_F10, RETROK_F10 },
|
|
|
|
{ SDLK_F11, RETROK_F11 },
|
|
|
|
{ SDLK_F12, RETROK_F12 },
|
|
|
|
{ SDLK_a, RETROK_a },
|
|
|
|
{ SDLK_b, RETROK_b },
|
|
|
|
{ SDLK_c, RETROK_c },
|
|
|
|
{ SDLK_d, RETROK_d },
|
|
|
|
{ SDLK_e, RETROK_e },
|
|
|
|
{ SDLK_f, RETROK_f },
|
|
|
|
{ SDLK_g, RETROK_g },
|
|
|
|
{ SDLK_h, RETROK_h },
|
|
|
|
{ SDLK_i, RETROK_i },
|
|
|
|
{ SDLK_j, RETROK_j },
|
|
|
|
{ SDLK_k, RETROK_k },
|
|
|
|
{ SDLK_l, RETROK_l },
|
|
|
|
{ SDLK_m, RETROK_m },
|
|
|
|
{ SDLK_n, RETROK_n },
|
|
|
|
{ SDLK_o, RETROK_o },
|
|
|
|
{ SDLK_p, RETROK_p },
|
|
|
|
{ SDLK_q, RETROK_q },
|
|
|
|
{ SDLK_r, RETROK_r },
|
|
|
|
{ SDLK_s, RETROK_s },
|
|
|
|
{ SDLK_t, RETROK_t },
|
|
|
|
{ SDLK_u, RETROK_u },
|
|
|
|
{ SDLK_v, RETROK_v },
|
|
|
|
{ SDLK_w, RETROK_w },
|
|
|
|
{ SDLK_x, RETROK_x },
|
|
|
|
{ SDLK_y, RETROK_y },
|
|
|
|
{ SDLK_z, RETROK_z },
|
2011-09-13 12:40:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void init_lut(void)
|
|
|
|
{
|
|
|
|
memset(keysym_lut, 0, sizeof(keysym_lut));
|
|
|
|
for (unsigned i = 0; i < sizeof(lut_binds) / sizeof(lut_binds[0]); i++)
|
|
|
|
keysym_lut[lut_binds[i].sk] = lut_binds[i].sdl;
|
|
|
|
}
|
2011-06-10 15:34:04 +00:00
|
|
|
|
2011-11-02 18:31:36 +00:00
|
|
|
static void *sdl_input_init(void)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-09-13 12:40:33 +00:00
|
|
|
init_lut();
|
2011-12-24 12:46:12 +00:00
|
|
|
sdl_input_t *sdl = (sdl_input_t*)calloc(1, sizeof(*sdl));
|
2011-01-06 17:34:11 +00:00
|
|
|
if (!sdl)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-10 15:34:04 +00:00
|
|
|
#ifdef HAVE_DINPUT
|
|
|
|
sdl->di = sdl_dinput_init();
|
|
|
|
if (!sdl->di)
|
|
|
|
{
|
|
|
|
free(sdl);
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("Failed to init SDL/DInput.\n");
|
2011-06-10 15:34:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
2011-01-06 17:34:11 +00:00
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-08 18:28:49 +00:00
|
|
|
SDL_JoystickEventState(SDL_IGNORE);
|
2011-01-06 17:34:11 +00:00
|
|
|
sdl->num_joysticks = SDL_NumJoysticks();
|
2011-01-10 07:18:03 +00:00
|
|
|
|
2011-01-10 15:53:37 +00:00
|
|
|
for (unsigned i = 0; i < MAX_PLAYERS; i++)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2012-01-30 00:20:35 +00:00
|
|
|
if (g_settings.input.joypad_map[i] < 0)
|
2011-01-10 15:53:37 +00:00
|
|
|
continue;
|
|
|
|
|
2012-02-18 11:09:20 +00:00
|
|
|
unsigned port = g_settings.input.joypad_map[i];
|
|
|
|
|
|
|
|
if (sdl->num_joysticks <= port)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sdl->joysticks[i] = SDL_JoystickOpen(port);
|
|
|
|
if (!sdl->joysticks[i])
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("Couldn't open SDL joystick #%u on SNES port %u\n", port, i + 1);
|
2012-02-18 11:09:20 +00:00
|
|
|
free(sdl);
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
return NULL;
|
2011-01-10 07:18:03 +00:00
|
|
|
}
|
2012-02-18 11:09:20 +00:00
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("Opened Joystick: %s (#%u) on port %u\n",
|
2012-02-18 11:09:20 +00:00
|
|
|
SDL_JoystickName(port), port, i + 1);
|
|
|
|
|
|
|
|
sdl->num_axes[i] = SDL_JoystickNumAxes(sdl->joysticks[i]);
|
|
|
|
sdl->num_buttons[i] = SDL_JoystickNumButtons(sdl->joysticks[i]);
|
|
|
|
sdl->num_hats[i] = SDL_JoystickNumHats(sdl->joysticks[i]);
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("Joypad has: %u axes, %u buttons, %u hats.\n",
|
2012-02-18 11:09:20 +00:00
|
|
|
sdl->num_axes[i], sdl->num_buttons[i], sdl->num_hats[i]);
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
2011-06-10 15:34:04 +00:00
|
|
|
#endif
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-03-12 23:44:49 +00:00
|
|
|
sdl->use_keyboard = true;
|
2011-01-06 17:34:11 +00:00
|
|
|
return sdl;
|
|
|
|
}
|
|
|
|
|
2011-01-08 21:15:02 +00:00
|
|
|
static bool sdl_key_pressed(int key)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2012-07-28 01:21:37 +00:00
|
|
|
return key < RETROK_LAST && gfx_ctx_key_pressed(keysym_lut[key]);
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 15:34:04 +00:00
|
|
|
#ifndef HAVE_DINPUT
|
2012-03-09 17:20:03 +00:00
|
|
|
static bool sdl_joykey_pressed(sdl_input_t *sdl, int port_num, uint16_t joykey)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-06-11 20:46:51 +00:00
|
|
|
if (joykey == NO_BTN)
|
|
|
|
return false;
|
|
|
|
|
2011-01-08 21:15:02 +00:00
|
|
|
// Check hat.
|
|
|
|
if (GET_HAT_DIR(joykey))
|
|
|
|
{
|
2012-02-18 11:32:49 +00:00
|
|
|
uint16_t hat = GET_HAT(joykey);
|
|
|
|
if (hat >= sdl->num_hats[port_num])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Uint8 dir = SDL_JoystickGetHat(sdl->joysticks[port_num], hat);
|
|
|
|
switch (GET_HAT_DIR(joykey))
|
2011-01-08 21:15:02 +00:00
|
|
|
{
|
2012-02-18 11:32:49 +00:00
|
|
|
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;
|
2011-01-08 21:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Check the button
|
|
|
|
{
|
|
|
|
if (joykey < sdl->num_buttons[port_num] && SDL_JoystickGetButton(sdl->joysticks[port_num], joykey))
|
|
|
|
return true;
|
2012-02-18 11:32:49 +00:00
|
|
|
|
|
|
|
return false;
|
2011-01-08 21:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2012-06-28 15:57:50 +00:00
|
|
|
static int16_t sdl_axis_analog(sdl_input_t *sdl, unsigned port_num, uint32_t joyaxis)
|
2011-01-08 21:15:02 +00:00
|
|
|
{
|
2011-06-11 20:46:51 +00:00
|
|
|
if (joyaxis == AXIS_NONE)
|
2012-06-28 15:57:50 +00:00
|
|
|
return 0;
|
2011-06-11 20:46:51 +00:00
|
|
|
|
2012-06-28 15:57:50 +00:00
|
|
|
Sint16 val = 0;
|
2011-06-11 20:46:51 +00:00
|
|
|
if (AXIS_NEG_GET(joyaxis) < sdl->num_axes[port_num])
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2012-06-28 15:57:50 +00:00
|
|
|
val = SDL_JoystickGetAxis(sdl->joysticks[port_num], AXIS_NEG_GET(joyaxis));
|
|
|
|
|
|
|
|
if (val > 0)
|
|
|
|
val = 0;
|
|
|
|
else if (val < -0x8000) // -0x8000 can cause trouble if we later abs() it.
|
|
|
|
val = -0x7fff;
|
2011-06-11 20:46:51 +00:00
|
|
|
}
|
2012-06-28 15:57:50 +00:00
|
|
|
else if (AXIS_POS_GET(joyaxis) < sdl->num_axes[port_num])
|
2011-06-11 20:46:51 +00:00
|
|
|
{
|
2012-06-28 15:57:50 +00:00
|
|
|
val = SDL_JoystickGetAxis(sdl->joysticks[port_num], AXIS_POS_GET(joyaxis));
|
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
val = 0;
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:57:50 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sdl_axis_pressed(sdl_input_t *sdl, unsigned port_num, uint32_t joyaxis)
|
|
|
|
{
|
|
|
|
int16_t val = sdl_axis_analog(sdl, port_num, joyaxis);
|
|
|
|
|
|
|
|
float scaled = (float)abs(val) / 0x8000;
|
|
|
|
return scaled > g_settings.input.axis_threshold;
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
2011-06-10 15:34:04 +00:00
|
|
|
#endif
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2012-07-07 15:19:32 +00:00
|
|
|
static bool sdl_is_pressed(sdl_input_t *sdl, unsigned port_num, const struct retro_keybind *key)
|
2011-01-08 21:15:02 +00:00
|
|
|
{
|
2011-03-12 23:44:49 +00:00
|
|
|
if (sdl->use_keyboard && sdl_key_pressed(key->key))
|
2011-01-08 21:15:02 +00:00
|
|
|
return true;
|
2011-06-10 15:34:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DINPUT
|
|
|
|
return sdl_dinput_pressed(sdl->di, port_num, key);
|
|
|
|
#else
|
2011-01-10 07:18:03 +00:00
|
|
|
if (sdl->joysticks[port_num] == NULL)
|
2011-01-08 21:15:02 +00:00
|
|
|
return false;
|
|
|
|
if (sdl_joykey_pressed(sdl, port_num, key->joykey))
|
|
|
|
return true;
|
|
|
|
if (sdl_axis_pressed(sdl, port_num, key->joyaxis))
|
|
|
|
return true;
|
2011-06-10 15:34:04 +00:00
|
|
|
#endif
|
2011-01-08 21:15:02 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:37:45 +00:00
|
|
|
static bool sdl_bind_button_pressed(void *data, int key)
|
|
|
|
{
|
2012-07-07 15:19:32 +00:00
|
|
|
const struct retro_keybind *binds = g_settings.input.binds[0];
|
2012-04-21 21:25:32 +00:00
|
|
|
if (key >= 0 && key < RARCH_BIND_LIST_END)
|
2011-01-08 17:37:45 +00:00
|
|
|
{
|
2012-07-07 15:19:32 +00:00
|
|
|
const struct retro_keybind *bind = &binds[key];
|
2012-01-30 00:45:18 +00:00
|
|
|
return sdl_is_pressed((sdl_input_t*)data, 0, bind);
|
2011-01-08 17:37:45 +00:00
|
|
|
}
|
2012-01-30 00:45:18 +00:00
|
|
|
else
|
|
|
|
return false;
|
2011-01-08 17:37:45 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 15:19:32 +00:00
|
|
|
static int16_t sdl_joypad_device_state(sdl_input_t *sdl, const struct retro_keybind **binds_,
|
2012-01-30 00:45:18 +00:00
|
|
|
unsigned port_num, unsigned id)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2012-07-07 15:19:32 +00:00
|
|
|
const struct retro_keybind *binds = binds_[port_num];
|
2012-04-21 21:25:32 +00:00
|
|
|
if (id < RARCH_BIND_LIST_END)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2012-07-07 15:19:32 +00:00
|
|
|
const struct retro_keybind *bind = &binds[id];
|
2012-01-30 00:45:18 +00:00
|
|
|
return bind->valid ? (sdl_is_pressed(sdl, port_num, bind) ? 1 : 0) : 0;
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
2012-01-30 00:45:18 +00:00
|
|
|
else
|
|
|
|
return 0;
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:57:50 +00:00
|
|
|
static void conv_analog_id_to_bind_id(unsigned index, unsigned id,
|
|
|
|
unsigned *id_minus, unsigned *id_plus)
|
|
|
|
{
|
|
|
|
switch ((index << 1) | id)
|
|
|
|
{
|
|
|
|
case (RETRO_DEVICE_INDEX_ANALOG_LEFT << 1) | RETRO_DEVICE_ID_ANALOG_X:
|
|
|
|
*id_minus = RARCH_ANALOG_LEFT_X_MINUS;
|
|
|
|
*id_plus = RARCH_ANALOG_LEFT_X_PLUS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (RETRO_DEVICE_INDEX_ANALOG_LEFT << 1) | RETRO_DEVICE_ID_ANALOG_Y:
|
|
|
|
*id_minus = RARCH_ANALOG_LEFT_Y_MINUS;
|
|
|
|
*id_plus = RARCH_ANALOG_LEFT_Y_PLUS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (RETRO_DEVICE_INDEX_ANALOG_RIGHT << 1) | RETRO_DEVICE_ID_ANALOG_X:
|
|
|
|
*id_minus = RARCH_ANALOG_RIGHT_X_MINUS;
|
|
|
|
*id_plus = RARCH_ANALOG_RIGHT_X_PLUS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (RETRO_DEVICE_INDEX_ANALOG_RIGHT << 1) | RETRO_DEVICE_ID_ANALOG_Y:
|
|
|
|
*id_minus = RARCH_ANALOG_RIGHT_Y_MINUS;
|
|
|
|
*id_plus = RARCH_ANALOG_RIGHT_Y_PLUS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-07 15:19:32 +00:00
|
|
|
static int16_t sdl_analog_device_state(sdl_input_t *sdl, const struct retro_keybind **binds_,
|
2012-06-28 15:57:50 +00:00
|
|
|
unsigned port_num, unsigned index, unsigned id)
|
|
|
|
{
|
2012-07-07 15:19:32 +00:00
|
|
|
const struct retro_keybind *binds = binds_[port_num];
|
2012-06-28 15:57:50 +00:00
|
|
|
if (id >= RARCH_BIND_LIST_END)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned id_minus = 0;
|
|
|
|
unsigned id_plus = 0;
|
|
|
|
conv_analog_id_to_bind_id(index, id, &id_minus, &id_plus);
|
|
|
|
|
2012-07-07 15:19:32 +00:00
|
|
|
const struct retro_keybind *bind_minus = &binds[id_minus];
|
|
|
|
const struct retro_keybind *bind_plus = &binds[id_plus];
|
2012-06-28 15:57:50 +00:00
|
|
|
if (!bind_minus->valid || !bind_plus->valid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// A user might have bound minus axis to positive axis in SDL.
|
|
|
|
#ifdef HAVE_DINPUT
|
|
|
|
int16_t pressed_minus = abs(sdl_dinput_axis(sdl->di, port_num, bind_minus));
|
|
|
|
int16_t pressed_plus = abs(sdl_dinput_axis(sdl->di, port_num, bind_plus));
|
|
|
|
#else
|
|
|
|
int16_t pressed_minus = abs(sdl_axis_analog(sdl, port_num, bind_minus->joyaxis));
|
|
|
|
int16_t pressed_plus = abs(sdl_axis_analog(sdl, port_num, bind_plus->joyaxis));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int16_t res = pressed_plus - pressed_minus;
|
|
|
|
|
|
|
|
// TODO: Does it make sense to use axis thresholding here?
|
|
|
|
if (res != 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
int16_t digital_left = sdl_is_pressed(sdl, port_num, bind_minus) ? -0x7fff : 0;
|
|
|
|
int16_t digital_right = sdl_is_pressed(sdl, port_num, bind_plus) ? 0x7fff : 0;
|
|
|
|
return digital_right + digital_left;
|
|
|
|
}
|
|
|
|
|
2012-07-28 01:21:37 +00:00
|
|
|
static int16_t sdl_keyboard_device_state(sdl_input_t *sdl, unsigned id)
|
|
|
|
{
|
|
|
|
return sdl->use_keyboard && sdl_key_pressed(id);
|
|
|
|
}
|
|
|
|
|
2011-06-10 15:34:04 +00:00
|
|
|
static int16_t sdl_mouse_device_state(sdl_input_t *sdl, unsigned id)
|
2011-01-10 07:40:44 +00:00
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_MOUSE_LEFT:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_l;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_MOUSE_RIGHT:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_r;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_MOUSE_X:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_x;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_MOUSE_Y:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_y;
|
2011-01-10 07:40:44 +00:00
|
|
|
default:
|
2011-01-10 13:29:00 +00:00
|
|
|
return 0;
|
2011-01-10 07:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-07 09:55:37 +00:00
|
|
|
static int16_t sdl_lightgun_device_state(sdl_input_t *sdl, unsigned id)
|
2011-01-10 07:40:44 +00:00
|
|
|
{
|
2011-01-10 14:01:34 +00:00
|
|
|
switch (id)
|
|
|
|
{
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_LIGHTGUN_X:
|
2011-01-10 14:01:34 +00:00
|
|
|
return sdl->mouse_x;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_LIGHTGUN_Y:
|
2011-01-10 14:01:34 +00:00
|
|
|
return sdl->mouse_y;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
|
2011-01-10 14:01:34 +00:00
|
|
|
return sdl->mouse_l;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
|
2011-01-10 14:01:34 +00:00
|
|
|
return sdl->mouse_m;
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
|
2011-01-10 14:01:34 +00:00
|
|
|
return sdl->mouse_r;
|
2012-04-15 15:08:43 +00:00
|
|
|
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;
|
2011-01-10 14:01:34 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-10 07:40:44 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 15:19:32 +00:00
|
|
|
static int16_t sdl_input_state(void *data_, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned index, unsigned id)
|
2011-01-10 07:40:44 +00:00
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
sdl_input_t *data = (sdl_input_t*)data_;
|
2011-01-10 07:40:44 +00:00
|
|
|
switch (device)
|
|
|
|
{
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_JOYPAD:
|
|
|
|
return sdl_joypad_device_state(data, binds, port, id);
|
2012-06-28 15:57:50 +00:00
|
|
|
case RETRO_DEVICE_ANALOG:
|
|
|
|
return sdl_analog_device_state(data, binds, port, index, id);
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_MOUSE:
|
2011-06-10 15:34:04 +00:00
|
|
|
return sdl_mouse_device_state(data, id);
|
2012-07-28 01:21:37 +00:00
|
|
|
case RETRO_DEVICE_KEYBOARD:
|
|
|
|
return sdl_keyboard_device_state(data, id);
|
2012-04-07 09:55:37 +00:00
|
|
|
case RETRO_DEVICE_LIGHTGUN:
|
|
|
|
return sdl_lightgun_device_state(data, id);
|
2011-01-10 07:40:44 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static void sdl_input_free(void *data)
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
{
|
2011-01-08 18:22:58 +00:00
|
|
|
// Flush out all pending events.
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event));
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
sdl_input_t *sdl = (sdl_input_t*)data;
|
2011-06-10 15:34:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DINPUT
|
|
|
|
sdl_dinput_free(sdl->di);
|
|
|
|
#else
|
2011-01-10 15:53:37 +00:00
|
|
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
2011-01-10 07:18:03 +00:00
|
|
|
{
|
|
|
|
if (sdl->joysticks[i])
|
|
|
|
SDL_JoystickClose(sdl->joysticks[i]);
|
|
|
|
}
|
2011-06-10 15:34:04 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
#endif
|
2011-01-06 17:34:11 +00:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-10 13:29:00 +00:00
|
|
|
static void sdl_poll_mouse(sdl_input_t *sdl)
|
|
|
|
{
|
|
|
|
int _x, _y;
|
|
|
|
Uint8 btn = SDL_GetRelativeMouseState(&_x, &_y);
|
|
|
|
sdl->mouse_x = _x;
|
|
|
|
sdl->mouse_y = _y;
|
2011-01-10 14:01:34 +00:00
|
|
|
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;
|
2011-01-10 13:29:00 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static void sdl_input_poll(void *data)
|
|
|
|
{
|
|
|
|
SDL_PumpEvents();
|
2011-12-24 12:46:12 +00:00
|
|
|
sdl_input_t *sdl = (sdl_input_t*)data;
|
2011-06-10 16:01:44 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DINPUT
|
|
|
|
sdl_dinput_poll(sdl->di);
|
|
|
|
#else
|
2011-01-08 18:28:49 +00:00
|
|
|
SDL_JoystickUpdate();
|
2011-06-10 16:01:44 +00:00
|
|
|
#endif
|
|
|
|
|
2011-06-11 10:54:14 +00:00
|
|
|
sdl_poll_mouse(sdl);
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const input_driver_t input_sdl = {
|
2011-12-24 12:46:12 +00:00
|
|
|
sdl_input_init,
|
|
|
|
sdl_input_poll,
|
|
|
|
sdl_input_state,
|
|
|
|
sdl_bind_button_pressed,
|
|
|
|
sdl_input_free,
|
|
|
|
"sdl"
|
2011-01-06 17:34:11 +00:00
|
|
|
};
|
|
|
|
|