RetroArch/input/input_common.c

523 lines
17 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - 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 <http://www.gnu.org/licenses/>.
*/
#include "input_common.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "../general.h"
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#ifdef HAVE_DINPUT
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#endif
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
#include "SDL.h"
#endif
const char *input_joypad_name(const rarch_joypad_driver_t *drv,
unsigned joypad)
{
if (drv)
return drv->name(joypad);
return NULL;
}
bool input_joypad_set_rumble(const rarch_joypad_driver_t *drv,
unsigned port, enum retro_rumble_effect effect, uint16_t strength)
{
if (!drv || !drv->set_rumble)
return false;
unsigned int joy_idx = g_settings.input.joypad_map[port];
if (joy_idx >= MAX_PLAYERS)
return false;
return drv->set_rumble(joy_idx, effect, strength);
}
static bool input_joypad_is_pressed(
const rarch_joypad_driver_t *drv,
unsigned port,
const struct retro_keybind *binds,
unsigned key)
{
unsigned int joy_idx = g_settings.input.joypad_map[port];
if (joy_idx >= MAX_PLAYERS)
return false;
/* Auto-binds are per joypad, not per player. */
const struct retro_keybind *auto_binds =
g_settings.input.autoconf_binds[joy_idx];
uint64_t joykey = binds[key].joykey;
if (joykey == NO_BTN)
joykey = auto_binds[key].joykey;
if (drv->button(joy_idx, (uint16_t)joykey))
return true;
uint32_t joyaxis = binds[key].joyaxis;
if (joyaxis == AXIS_NONE)
joyaxis = auto_binds[key].joyaxis;
int16_t axis = drv->axis(joy_idx, joyaxis);
float scaled_axis = (float)abs(axis) / 0x8000;
return scaled_axis > g_settings.input.axis_threshold;
}
bool input_joypad_pressed(const rarch_joypad_driver_t *drv,
unsigned port, const struct retro_keybind *binds, unsigned key)
{
if (!drv)
return false;
if (!binds[key].valid)
return false;
if (input_joypad_is_pressed(drv, port, binds, key))
return true;
return false;
}
int16_t input_joypad_analog(const rarch_joypad_driver_t *drv,
unsigned port, unsigned idx, unsigned ident,
const struct retro_keybind *binds)
{
if (!drv)
return 0;
unsigned int joy_idx = g_settings.input.joypad_map[port];
if (joy_idx >= MAX_PLAYERS)
return 0;
/* Auto-binds are per joypad, not per player. */
const struct retro_keybind *auto_binds =
g_settings.input.autoconf_binds[joy_idx];
unsigned ident_minus = 0;
unsigned ident_plus = 0;
input_conv_analog_id_to_bind_id(idx, ident, &ident_minus, &ident_plus);
const struct retro_keybind *bind_minus = &binds[ident_minus];
const struct retro_keybind *bind_plus = &binds[ident_plus];
if (!bind_minus->valid || !bind_plus->valid)
return 0;
uint32_t axis_minus = bind_minus->joyaxis;
uint32_t axis_plus = bind_plus->joyaxis;
if (axis_minus == AXIS_NONE)
axis_minus = auto_binds[ident_minus].joyaxis;
if (axis_plus == AXIS_NONE)
axis_plus = auto_binds[ident_plus].joyaxis;
int16_t pressed_minus = abs(drv->axis(joy_idx, axis_minus));
int16_t pressed_plus = abs(drv->axis(joy_idx, axis_plus));
int16_t res = pressed_plus - pressed_minus;
if (res != 0)
return res;
uint64_t key_minus = bind_minus->joykey;
uint64_t key_plus = bind_plus->joykey;
if (key_minus == NO_BTN)
key_minus = auto_binds[ident_minus].joykey;
if (key_plus == NO_BTN)
key_plus = auto_binds[ident_plus].joykey;
int16_t digital_left = drv->button(joy_idx,
(uint16_t)key_minus) ? -0x7fff : 0;
int16_t digital_right = drv->button(joy_idx,
(uint16_t)key_plus) ? 0x7fff : 0;
return digital_right + digital_left;
}
int16_t input_joypad_axis_raw(const rarch_joypad_driver_t *drv,
unsigned joypad, unsigned axis)
{
if (drv)
return drv->axis(joypad, AXIS_POS(axis)) +
drv->axis(joypad, AXIS_NEG(axis));
return 0;
}
bool input_joypad_button_raw(const rarch_joypad_driver_t *drv,
unsigned joypad, unsigned button)
{
if (drv)
return drv->button(joypad, button);
return false;
}
bool input_joypad_hat_raw(const rarch_joypad_driver_t *drv,
unsigned joypad, unsigned hat_dir, unsigned hat)
{
if (drv)
return drv->button(joypad, HAT_MAP(hat, hat_dir));
return false;
}
bool input_translate_coord_viewport(int mouse_x, int mouse_y,
int16_t *res_x, int16_t *res_y, int16_t *res_screen_x,
int16_t *res_screen_y)
{
struct rarch_viewport vp = {0};
bool have_viewport_info = driver.video && driver.video->viewport_info;
if (!have_viewport_info)
return false;
driver.video->viewport_info(driver.video_data, &vp);
int scaled_screen_x = (2 * mouse_x * 0x7fff) / (int)vp.full_width - 0x7fff;
int scaled_screen_y = (2 * mouse_y * 0x7fff) / (int)vp.full_height - 0x7fff;
if (scaled_screen_x < -0x7fff || scaled_screen_x > 0x7fff)
scaled_screen_x = -0x8000; /* OOB */
if (scaled_screen_y < -0x7fff || scaled_screen_y > 0x7fff)
scaled_screen_y = -0x8000; /* OOB */
mouse_x -= vp.x;
mouse_y -= vp.y;
int scaled_x = (2 * mouse_x * 0x7fff) / (int)vp.width - 0x7fff;
int scaled_y = (2 * mouse_y * 0x7fff) / (int)vp.height - 0x7fff;
if (scaled_x < -0x7fff || scaled_x > 0x7fff)
scaled_x = -0x8000; /* OOB */
if (scaled_y < -0x7fff || scaled_y > 0x7fff)
scaled_y = -0x8000; /* OOB */
*res_x = scaled_x;
*res_y = scaled_y;
*res_screen_x = scaled_screen_x;
*res_screen_y = scaled_screen_y;
return true;
}
static const char *bind_player_prefix[MAX_PLAYERS] = {
"input_player1",
"input_player2",
"input_player3",
"input_player4",
"input_player5",
"input_player6",
"input_player7",
"input_player8",
"input_player9",
"input_player10",
"input_player11",
"input_player12",
"input_player13",
"input_player14",
"input_player15",
"input_player16",
};
#define DECLARE_BIND(x, bind, desc) { true, 0, #x, desc, bind }
#define DECLARE_META_BIND(level, x, bind, desc) { true, level, #x, desc, bind }
const struct input_bind_map input_config_bind_map[RARCH_BIND_LIST_END_NULL] = {
DECLARE_BIND(b, RETRO_DEVICE_ID_JOYPAD_B, "B button (down)"),
DECLARE_BIND(y, RETRO_DEVICE_ID_JOYPAD_Y, "Y button (left)"),
DECLARE_BIND(select, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select button"),
DECLARE_BIND(start, RETRO_DEVICE_ID_JOYPAD_START, "Start button"),
DECLARE_BIND(up, RETRO_DEVICE_ID_JOYPAD_UP, "Up D-pad"),
DECLARE_BIND(down, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down D-pad"),
DECLARE_BIND(left, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left D-pad"),
DECLARE_BIND(right, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right D-pad"),
DECLARE_BIND(a, RETRO_DEVICE_ID_JOYPAD_A, "A button (right)"),
DECLARE_BIND(x, RETRO_DEVICE_ID_JOYPAD_X, "X button (top)"),
DECLARE_BIND(l, RETRO_DEVICE_ID_JOYPAD_L, "L button (shoulder)"),
DECLARE_BIND(r, RETRO_DEVICE_ID_JOYPAD_R, "R button (shoulder)"),
DECLARE_BIND(l2, RETRO_DEVICE_ID_JOYPAD_L2, "L2 button (trigger)"),
DECLARE_BIND(r2, RETRO_DEVICE_ID_JOYPAD_R2, "R2 button (trigger)"),
DECLARE_BIND(l3, RETRO_DEVICE_ID_JOYPAD_L3, "L3 button (thumb)"),
DECLARE_BIND(r3, RETRO_DEVICE_ID_JOYPAD_R3, "R3 button (thumb)"),
DECLARE_BIND(l_x_plus, RARCH_ANALOG_LEFT_X_PLUS, "Left analog X+ (right)"),
DECLARE_BIND(l_x_minus, RARCH_ANALOG_LEFT_X_MINUS, "Left analog X- (left)"),
DECLARE_BIND(l_y_plus, RARCH_ANALOG_LEFT_Y_PLUS, "Left analog Y+ (down)"),
DECLARE_BIND(l_y_minus, RARCH_ANALOG_LEFT_Y_MINUS, "Left analog Y- (up)"),
DECLARE_BIND(r_x_plus, RARCH_ANALOG_RIGHT_X_PLUS, "Right analog X+ (right)"),
DECLARE_BIND(r_x_minus, RARCH_ANALOG_RIGHT_X_MINUS, "Right analog X- (left)"),
DECLARE_BIND(r_y_plus, RARCH_ANALOG_RIGHT_Y_PLUS, "Right analog Y+ (down)"),
DECLARE_BIND(r_y_minus, RARCH_ANALOG_RIGHT_Y_MINUS, "Right analog Y- (up)"),
DECLARE_BIND(turbo, RARCH_TURBO_ENABLE, "Turbo enable"),
DECLARE_META_BIND(1, toggle_fast_forward, RARCH_FAST_FORWARD_KEY, "Fast forward toggle"),
DECLARE_META_BIND(2, hold_fast_forward, RARCH_FAST_FORWARD_HOLD_KEY, "Fast forward hold"),
DECLARE_META_BIND(1, load_state, RARCH_LOAD_STATE_KEY, "Load state"),
DECLARE_META_BIND(1, save_state, RARCH_SAVE_STATE_KEY, "Save state"),
DECLARE_META_BIND(2, toggle_fullscreen, RARCH_FULLSCREEN_TOGGLE_KEY, "Fullscreen toggle"),
DECLARE_META_BIND(2, exit_emulator, RARCH_QUIT_KEY, "Quit RetroArch"),
DECLARE_META_BIND(2, state_slot_increase, RARCH_STATE_SLOT_PLUS, "Savestate slot +"),
DECLARE_META_BIND(2, state_slot_decrease, RARCH_STATE_SLOT_MINUS, "Savestate slot -"),
DECLARE_META_BIND(1, rewind, RARCH_REWIND, "Rewind"),
DECLARE_META_BIND(2, movie_record_toggle, RARCH_MOVIE_RECORD_TOGGLE, "Movie record toggle"),
DECLARE_META_BIND(2, pause_toggle, RARCH_PAUSE_TOGGLE, "Pause toggle"),
DECLARE_META_BIND(2, frame_advance, RARCH_FRAMEADVANCE, "Frameadvance"),
DECLARE_META_BIND(2, reset, RARCH_RESET, "Reset game"),
DECLARE_META_BIND(2, shader_next, RARCH_SHADER_NEXT, "Next shader"),
DECLARE_META_BIND(2, shader_prev, RARCH_SHADER_PREV, "Previous shader"),
DECLARE_META_BIND(2, cheat_index_plus, RARCH_CHEAT_INDEX_PLUS, "Cheat index +"),
DECLARE_META_BIND(2, cheat_index_minus, RARCH_CHEAT_INDEX_MINUS, "Cheat index -"),
DECLARE_META_BIND(2, cheat_toggle, RARCH_CHEAT_TOGGLE, "Cheat toggle"),
DECLARE_META_BIND(2, screenshot, RARCH_SCREENSHOT, "Take screenshot"),
DECLARE_META_BIND(2, audio_mute, RARCH_MUTE, "Audio mute toggle"),
DECLARE_META_BIND(2, netplay_flip_players, RARCH_NETPLAY_FLIP, "Netplay flip players"),
DECLARE_META_BIND(2, slowmotion, RARCH_SLOWMOTION, "Slow motion"),
DECLARE_META_BIND(2, enable_hotkey, RARCH_ENABLE_HOTKEY, "Enable hotkeys"),
DECLARE_META_BIND(2, volume_up, RARCH_VOLUME_UP, "Volume +"),
DECLARE_META_BIND(2, volume_down, RARCH_VOLUME_DOWN, "Volume -"),
DECLARE_META_BIND(2, overlay_next, RARCH_OVERLAY_NEXT, "Overlay next"),
DECLARE_META_BIND(2, disk_eject_toggle, RARCH_DISK_EJECT_TOGGLE, "Disk eject toggle"),
DECLARE_META_BIND(2, disk_next, RARCH_DISK_NEXT, "Disk next"),
DECLARE_META_BIND(2, disk_prev, RARCH_DISK_NEXT, "Disk prev"),
DECLARE_META_BIND(2, grab_mouse_toggle, RARCH_GRAB_MOUSE_TOGGLE, "Grab mouse toggle"),
#ifdef HAVE_MENU
DECLARE_META_BIND(1, menu_toggle, RARCH_MENU_TOGGLE, "Menu toggle"),
#endif
};
void input_config_parse_key(config_file_t *conf,
const char *prefix, const char *btn,
struct retro_keybind *bind)
{
char tmp[64];
char key[64];
snprintf(key, sizeof(key), "%s_%s", prefix, btn);
if (config_get_array(conf, key, tmp, sizeof(tmp)))
bind->key = input_translate_str_to_rk(tmp);
}
const char *input_config_get_prefix(unsigned player, bool meta)
{
if (player == 0)
return meta ? "input" : bind_player_prefix[player];
else if (player != 0 && !meta)
return bind_player_prefix[player];
/* Don't bother with meta bind for anyone else than first player. */
return NULL;
}
unsigned input_translate_str_to_bind_id(const char *str)
{
unsigned i;
for (i = 0; input_config_bind_map[i].valid; i++)
if (!strcmp(str, input_config_bind_map[i].base))
return i;
return RARCH_BIND_LIST_END;
}
static void parse_hat(struct retro_keybind *bind, const char *str)
{
if (!bind || !str)
return;
if (!isdigit(*str))
return;
char *dir = NULL;
uint16_t hat = strtoul(str, &dir, 0);
uint16_t hat_dir = 0;
if (!dir)
{
RARCH_WARN("Found invalid hat in config!\n");
return;
}
if (strcasecmp(dir, "up") == 0)
hat_dir = HAT_UP_MASK;
else if (strcasecmp(dir, "down") == 0)
hat_dir = HAT_DOWN_MASK;
else if (strcasecmp(dir, "left") == 0)
hat_dir = HAT_LEFT_MASK;
else if (strcasecmp(dir, "right") == 0)
hat_dir = HAT_RIGHT_MASK;
if (hat_dir)
bind->joykey = HAT_MAP(hat, hat_dir);
}
void input_config_parse_joy_button(config_file_t *conf, const char *prefix,
const char *btn, struct retro_keybind *bind)
{
char tmp[64];
char key[64];
snprintf(key, sizeof(key), "%s_%s_btn", prefix, btn);
if (config_get_array(conf, key, tmp, sizeof(tmp)))
{
btn = tmp;
if (strcmp(btn, "nul") == 0)
bind->joykey = NO_BTN;
else
{
if (*btn == 'h')
parse_hat(bind, btn + 1);
else
bind->joykey = strtoull(tmp, NULL, 0);
}
}
}
void input_config_parse_joy_axis(config_file_t *conf, const char *prefix,
const char *axis, struct retro_keybind *bind)
{
char tmp[64];
char key[64];
snprintf(key, sizeof(key), "%s_%s_axis", prefix, axis);
if (config_get_array(conf, key, tmp, sizeof(tmp)))
{
if (strcmp(tmp, "nul") == 0)
bind->joyaxis = AXIS_NONE;
else if (strlen(tmp) >= 2 && (*tmp == '+' || *tmp == '-'))
{
int i_axis = strtol(tmp + 1, NULL, 0);
if (*tmp == '+')
bind->joyaxis = AXIS_POS(i_axis);
else
bind->joyaxis = AXIS_NEG(i_axis);
}
/* Ensure that d-pad emulation doesn't screw this over. */
bind->orig_joyaxis = bind->joyaxis;
}
}
#if !defined(IS_JOYCONFIG)
static void input_get_bind_string_joykey(char *buf, const char *prefix,
const struct retro_keybind *bind, size_t size)
{
if (GET_HAT_DIR(bind->joykey))
{
const char *dir;
switch (GET_HAT_DIR(bind->joykey))
{
case HAT_UP_MASK:
dir = "up";
break;
case HAT_DOWN_MASK:
dir = "down";
break;
case HAT_LEFT_MASK:
dir = "left";
break;
case HAT_RIGHT_MASK:
dir = "right";
break;
default:
dir = "?";
break;
}
snprintf(buf, size, "%sHat #%u %s ", prefix,
(unsigned)GET_HAT(bind->joykey), dir);
}
else
snprintf(buf, size, "%s%u (btn) ", prefix, (unsigned)bind->joykey);
}
static void input_get_bind_string_joyaxis(char *buf, const char *prefix,
const struct retro_keybind *bind, size_t size)
{
unsigned axis = 0;
char dir = '\0';
if (AXIS_NEG_GET(bind->joyaxis) != AXIS_DIR_NONE)
{
dir = '-';
axis = AXIS_NEG_GET(bind->joyaxis);
}
else if (AXIS_POS_GET(bind->joyaxis) != AXIS_DIR_NONE)
{
dir = '+';
axis = AXIS_POS_GET(bind->joyaxis);
}
snprintf(buf, size, "%s%c%u (axis) ", prefix, dir, axis);
}
void input_get_bind_string(char *buf, const struct retro_keybind *bind,
const struct retro_keybind *auto_bind, size_t size)
{
*buf = '\0';
if (bind->joykey != NO_BTN)
input_get_bind_string_joykey(buf, "", bind, size);
else if (bind->joyaxis != AXIS_NONE)
input_get_bind_string_joyaxis(buf, "", bind, size);
else if (auto_bind && auto_bind->joykey != NO_BTN)
input_get_bind_string_joykey(buf, "Auto: ", auto_bind, size);
else if (auto_bind && auto_bind->joyaxis != AXIS_NONE)
input_get_bind_string_joyaxis(buf, "Auto: ", auto_bind, size);
#ifndef RARCH_CONSOLE
char key[64];
input_translate_rk_to_str(bind->key, key, sizeof(key));
if (!strcmp(key, "nul"))
*key = '\0';
char keybuf[64];
snprintf(keybuf, sizeof(keybuf), "(Key: %s)", key);
strlcat(buf, keybuf, size);
#endif
}
#endif
void input_push_analog_dpad(struct retro_keybind *binds, unsigned mode)
{
unsigned i, j;
for (i = RETRO_DEVICE_ID_JOYPAD_UP; i <= RETRO_DEVICE_ID_JOYPAD_RIGHT; i++)
binds[i].orig_joyaxis = binds[i].joyaxis;
switch (mode)
{
case ANALOG_DPAD_LSTICK:
j = RARCH_ANALOG_LEFT_X_PLUS + 3;
/* Inherit joyaxis from analogs. */
for (i = RETRO_DEVICE_ID_JOYPAD_UP; i <= RETRO_DEVICE_ID_JOYPAD_RIGHT; i++)
binds[i].joyaxis = binds[j--].joyaxis;
break;
case ANALOG_DPAD_RSTICK:
j = RARCH_ANALOG_RIGHT_X_PLUS + 3;
/* Inherit joyaxis from analogs. */
for (i = RETRO_DEVICE_ID_JOYPAD_UP; i <= RETRO_DEVICE_ID_JOYPAD_RIGHT; i++)
binds[i].joyaxis = binds[j--].joyaxis;
break;
}
}
/* Restore binds temporarily overridden by input_push_analog_dpad. */
void input_pop_analog_dpad(struct retro_keybind *binds)
{
unsigned i;
for (i = RETRO_DEVICE_ID_JOYPAD_UP; i <= RETRO_DEVICE_ID_JOYPAD_RIGHT; i++)
binds[i].joyaxis = binds[i].orig_joyaxis;
}