diff --git a/Makefile.common b/Makefile.common index 66ef85993d..349aaf8ec6 100644 --- a/Makefile.common +++ b/Makefile.common @@ -110,6 +110,7 @@ OBJ += frontend/frontend.o \ gfx/fonts/bitmapfont.o \ input/input_autodetect.o \ input/input_context.o \ + input/input_joypad.o \ input/input_common.o \ input/input_keymaps.o \ input/keyboard_line.o \ diff --git a/griffin/griffin.c b/griffin/griffin.c index d0bc9a99f8..1fdab314d9 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -282,6 +282,7 @@ INPUT ============================================================ */ #include "../input/input_autodetect.c" #include "../input/input_context.c" +#include "../input/input_joypad.c" #include "../input/input_common.c" #include "../input/input_keymaps.c" #include "../input/keyboard_line.c" diff --git a/input/android_input.c b/input/android_input.c index e6ac6835aa..b4afd0671e 100644 --- a/input/android_input.c +++ b/input/android_input.c @@ -22,6 +22,7 @@ #include "../frontend/platform/platform_android.h" #include "input_autodetect.h" #include "input_common.h" +#include "input_joypad.h" #include "../performance.h" #include "../general.h" #include "../driver.h" diff --git a/input/apple_input.c b/input/apple_input.c index e693e9ab5f..e5bb98edb1 100644 --- a/input/apple_input.c +++ b/input/apple_input.c @@ -18,6 +18,7 @@ #include #include "input_common.h" +#include "input_joypad.h" #include "input_keymaps.h" #include "apple_input.h" #include "../general.h" diff --git a/input/dinput.c b/input/dinput.c index 1f1feed923..ca1657da12 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -26,6 +26,7 @@ #include #include "input_autodetect.h" #include "input_common.h" +#include "input_joypad.h" #include "input_keymaps.h" #include "retroarch_logger.h" #include diff --git a/input/input_common.c b/input/input_common.c index d085652ac2..0c05cd8573 100644 --- a/input/input_common.c +++ b/input/input_common.c @@ -24,161 +24,6 @@ #include "../config.h" #endif -const char *input_joypad_name(const rarch_joypad_driver_t *drv, - unsigned joypad) -{ - if (!drv) - return NULL; - return drv->name(joypad); -} - -bool input_joypad_set_rumble(const rarch_joypad_driver_t *drv, - unsigned port, enum retro_rumble_effect effect, uint16_t strength) -{ - unsigned joy_idx = g_settings.input.joypad_map[port]; - - if (!drv || !drv->set_rumble) - return false; - - if (joy_idx >= MAX_USERS) - 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) -{ - const struct retro_keybind *auto_binds; - float scaled_axis; - int16_t axis; - uint32_t joyaxis; - uint64_t joykey; - unsigned joy_idx = g_settings.input.joypad_map[port]; - - if (joy_idx >= MAX_USERS) - return false; - - /* Auto-binds are per joypad, not per user. */ - auto_binds = g_settings.input.autoconf_binds[joy_idx]; - - joykey = binds[key].joykey; - if (joykey == NO_BTN) - joykey = auto_binds[key].joykey; - - if (drv->button(joy_idx, (uint16_t)joykey)) - return true; - - joyaxis = binds[key].joyaxis; - if (joyaxis == AXIS_NONE) - joyaxis = auto_binds[key].joyaxis; - - axis = drv->axis(joy_idx, joyaxis); - 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) -{ - uint32_t axis_minus, axis_plus; - uint64_t key_minus, key_plus; - int16_t pressed_minus, pressed_plus, res; - unsigned ident_minus = 0, ident_plus = 0; - int16_t digital_left = 0, digital_right = 0; - const struct retro_keybind *auto_binds = NULL; - const struct retro_keybind *bind_minus = NULL; - const struct retro_keybind *bind_plus = NULL; - unsigned joy_idx = g_settings.input.joypad_map[port]; - - if (!drv) - return 0; - - if (joy_idx >= MAX_USERS) - return 0; - - /* Auto-binds are per joypad, not per user. */ - auto_binds = g_settings.input.autoconf_binds[joy_idx]; - - input_conv_analog_id_to_bind_id(idx, ident, &ident_minus, &ident_plus); - - bind_minus = &binds[ident_minus]; - bind_plus = &binds[ident_plus]; - if (!bind_minus->valid || !bind_plus->valid) - return 0; - - axis_minus = bind_minus->joyaxis; - 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; - - pressed_minus = abs(drv->axis(joy_idx, axis_minus)); - pressed_plus = abs(drv->axis(joy_idx, axis_plus)); - res = pressed_plus - pressed_minus; - - if (res != 0) - return res; - - key_minus = bind_minus->joykey; - 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; - - if (drv->button(joy_idx, (uint16_t)key_minus)) - digital_left = -0x7fff; - if (drv->button(joy_idx, (uint16_t)key_plus)) - digital_right = 0x7fff; - return digital_right + digital_left; -} - -int16_t input_joypad_axis_raw(const rarch_joypad_driver_t *drv, - unsigned joypad, unsigned axis) -{ - if (!drv) - return 0; - return drv->axis(joypad, AXIS_POS(axis)) + - drv->axis(joypad, AXIS_NEG(axis)); -} - -bool input_joypad_button_raw(const rarch_joypad_driver_t *drv, - unsigned joypad, unsigned button) -{ - if (!drv) - return false; - return drv->button(joypad, button); -} - -bool input_joypad_hat_raw(const rarch_joypad_driver_t *drv, - unsigned joypad, unsigned hat_dir, unsigned hat) -{ - if (!drv) - return false; - return drv->button(joypad, HAT_MAP(hat, hat_dir)); -} - 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) diff --git a/input/input_common.h b/input/input_common.h index 641763c43f..3a2d6b84f5 100644 --- a/input/input_common.h +++ b/input/input_common.h @@ -26,62 +26,8 @@ extern "C" { #include #include "../driver.h" - typedef uint64_t retro_input_t ; -static inline void input_conv_analog_id_to_bind_id(unsigned idx, unsigned ident, - unsigned *ident_minus, unsigned *ident_plus) -{ - switch ((idx << 1) | ident) - { - case (RETRO_DEVICE_INDEX_ANALOG_LEFT << 1) | RETRO_DEVICE_ID_ANALOG_X: - *ident_minus = RARCH_ANALOG_LEFT_X_MINUS; - *ident_plus = RARCH_ANALOG_LEFT_X_PLUS; - break; - - case (RETRO_DEVICE_INDEX_ANALOG_LEFT << 1) | RETRO_DEVICE_ID_ANALOG_Y: - *ident_minus = RARCH_ANALOG_LEFT_Y_MINUS; - *ident_plus = RARCH_ANALOG_LEFT_Y_PLUS; - break; - - case (RETRO_DEVICE_INDEX_ANALOG_RIGHT << 1) | RETRO_DEVICE_ID_ANALOG_X: - *ident_minus = RARCH_ANALOG_RIGHT_X_MINUS; - *ident_plus = RARCH_ANALOG_RIGHT_X_PLUS; - break; - - case (RETRO_DEVICE_INDEX_ANALOG_RIGHT << 1) | RETRO_DEVICE_ID_ANALOG_Y: - *ident_minus = RARCH_ANALOG_RIGHT_Y_MINUS; - *ident_plus = RARCH_ANALOG_RIGHT_Y_PLUS; - break; - } -} - -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); - -bool input_joypad_pressed(const rarch_joypad_driver_t *driver, - unsigned port, const struct retro_keybind *binds, unsigned key); - -int16_t input_joypad_analog(const rarch_joypad_driver_t *driver, - unsigned port, unsigned idx, unsigned ident, - const struct retro_keybind *binds); - -bool input_joypad_set_rumble(const rarch_joypad_driver_t *driver, - unsigned port, enum retro_rumble_effect effect, uint16_t strength); - -int16_t input_joypad_axis_raw(const rarch_joypad_driver_t *driver, - unsigned joypad, unsigned axis); - -bool input_joypad_button_raw(const rarch_joypad_driver_t *driver, - unsigned joypad, unsigned button); - -bool input_joypad_hat_raw(const rarch_joypad_driver_t *driver, - unsigned joypad, unsigned hat_dir, unsigned hat); - -const char *input_joypad_name(const rarch_joypad_driver_t *driver, - unsigned joypad); - /* Input config. */ struct input_bind_map { @@ -101,6 +47,10 @@ struct input_bind_map extern const struct input_bind_map input_config_bind_map[]; +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); + /* auto_bind can be NULL. */ void input_get_bind_string(char *buf, const struct retro_keybind *bind, const struct retro_keybind *auto_bind, size_t size); diff --git a/input/input_joypad.c b/input/input_joypad.c new file mode 100644 index 0000000000..81d24d4ade --- /dev/null +++ b/input/input_joypad.c @@ -0,0 +1,180 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2014 - Hans-Kristian Arntzen + * Copyright (C) 2011-2015 - 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 . + */ + +#include "input_joypad.h" +#include +#include +#include + +#include "../general.h" +#ifdef HAVE_CONFIG_H +#include "../config.h" +#endif + +const char *input_joypad_name(const rarch_joypad_driver_t *drv, + unsigned joypad) +{ + if (!drv) + return NULL; + return drv->name(joypad); +} + +bool input_joypad_set_rumble(const rarch_joypad_driver_t *drv, + unsigned port, enum retro_rumble_effect effect, uint16_t strength) +{ + unsigned joy_idx = g_settings.input.joypad_map[port]; + + if (!drv || !drv->set_rumble) + return false; + + if (joy_idx >= MAX_USERS) + 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) +{ + const struct retro_keybind *auto_binds; + float scaled_axis; + int16_t axis; + uint32_t joyaxis; + uint64_t joykey; + unsigned joy_idx = g_settings.input.joypad_map[port]; + + if (joy_idx >= MAX_USERS) + return false; + + /* Auto-binds are per joypad, not per user. */ + auto_binds = g_settings.input.autoconf_binds[joy_idx]; + + joykey = binds[key].joykey; + if (joykey == NO_BTN) + joykey = auto_binds[key].joykey; + + if (drv->button(joy_idx, (uint16_t)joykey)) + return true; + + joyaxis = binds[key].joyaxis; + if (joyaxis == AXIS_NONE) + joyaxis = auto_binds[key].joyaxis; + + axis = drv->axis(joy_idx, joyaxis); + 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) +{ + uint32_t axis_minus, axis_plus; + uint64_t key_minus, key_plus; + int16_t pressed_minus, pressed_plus, res; + unsigned ident_minus = 0, ident_plus = 0; + int16_t digital_left = 0, digital_right = 0; + const struct retro_keybind *auto_binds = NULL; + const struct retro_keybind *bind_minus = NULL; + const struct retro_keybind *bind_plus = NULL; + unsigned joy_idx = g_settings.input.joypad_map[port]; + + if (!drv) + return 0; + + if (joy_idx >= MAX_USERS) + return 0; + + /* Auto-binds are per joypad, not per user. */ + auto_binds = g_settings.input.autoconf_binds[joy_idx]; + + input_conv_analog_id_to_bind_id(idx, ident, &ident_minus, &ident_plus); + + bind_minus = &binds[ident_minus]; + bind_plus = &binds[ident_plus]; + if (!bind_minus->valid || !bind_plus->valid) + return 0; + + axis_minus = bind_minus->joyaxis; + 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; + + pressed_minus = abs(drv->axis(joy_idx, axis_minus)); + pressed_plus = abs(drv->axis(joy_idx, axis_plus)); + res = pressed_plus - pressed_minus; + + if (res != 0) + return res; + + key_minus = bind_minus->joykey; + 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; + + if (drv->button(joy_idx, (uint16_t)key_minus)) + digital_left = -0x7fff; + if (drv->button(joy_idx, (uint16_t)key_plus)) + digital_right = 0x7fff; + return digital_right + digital_left; +} + +int16_t input_joypad_axis_raw(const rarch_joypad_driver_t *drv, + unsigned joypad, unsigned axis) +{ + if (!drv) + return 0; + return drv->axis(joypad, AXIS_POS(axis)) + + drv->axis(joypad, AXIS_NEG(axis)); +} + +bool input_joypad_button_raw(const rarch_joypad_driver_t *drv, + unsigned joypad, unsigned button) +{ + if (!drv) + return false; + return drv->button(joypad, button); +} + +bool input_joypad_hat_raw(const rarch_joypad_driver_t *drv, + unsigned joypad, unsigned hat_dir, unsigned hat) +{ + if (!drv) + return false; + return drv->button(joypad, HAT_MAP(hat, hat_dir)); +} diff --git a/input/input_joypad.h b/input/input_joypad.h new file mode 100644 index 0000000000..d9212531d0 --- /dev/null +++ b/input/input_joypad.h @@ -0,0 +1,83 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2014 - Hans-Kristian Arntzen + * Copyright (C) 2011-2015 - 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 . + */ + +#ifndef INPUT_JOYPAD_H__ +#define INPUT_JOYPAD_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "input_context.h" +#include +#include +#include "../driver.h" + +static inline void input_conv_analog_id_to_bind_id(unsigned idx, unsigned ident, + unsigned *ident_minus, unsigned *ident_plus) +{ + switch ((idx << 1) | ident) + { + case (RETRO_DEVICE_INDEX_ANALOG_LEFT << 1) | RETRO_DEVICE_ID_ANALOG_X: + *ident_minus = RARCH_ANALOG_LEFT_X_MINUS; + *ident_plus = RARCH_ANALOG_LEFT_X_PLUS; + break; + + case (RETRO_DEVICE_INDEX_ANALOG_LEFT << 1) | RETRO_DEVICE_ID_ANALOG_Y: + *ident_minus = RARCH_ANALOG_LEFT_Y_MINUS; + *ident_plus = RARCH_ANALOG_LEFT_Y_PLUS; + break; + + case (RETRO_DEVICE_INDEX_ANALOG_RIGHT << 1) | RETRO_DEVICE_ID_ANALOG_X: + *ident_minus = RARCH_ANALOG_RIGHT_X_MINUS; + *ident_plus = RARCH_ANALOG_RIGHT_X_PLUS; + break; + + case (RETRO_DEVICE_INDEX_ANALOG_RIGHT << 1) | RETRO_DEVICE_ID_ANALOG_Y: + *ident_minus = RARCH_ANALOG_RIGHT_Y_MINUS; + *ident_plus = RARCH_ANALOG_RIGHT_Y_PLUS; + break; + } +} + +bool input_joypad_pressed(const rarch_joypad_driver_t *driver, + unsigned port, const struct retro_keybind *binds, unsigned key); + +int16_t input_joypad_analog(const rarch_joypad_driver_t *driver, + unsigned port, unsigned idx, unsigned ident, + const struct retro_keybind *binds); + +bool input_joypad_set_rumble(const rarch_joypad_driver_t *driver, + unsigned port, enum retro_rumble_effect effect, uint16_t strength); + +int16_t input_joypad_axis_raw(const rarch_joypad_driver_t *driver, + unsigned joypad, unsigned axis); + +bool input_joypad_button_raw(const rarch_joypad_driver_t *driver, + unsigned joypad, unsigned button); + +bool input_joypad_hat_raw(const rarch_joypad_driver_t *driver, + unsigned joypad, unsigned hat_dir, unsigned hat); + +const char *input_joypad_name(const rarch_joypad_driver_t *driver, + unsigned joypad); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/input/linuxraw_input.c b/input/linuxraw_input.c index a08f8f5e71..83f7a4c8e6 100644 --- a/input/linuxraw_input.c +++ b/input/linuxraw_input.c @@ -25,6 +25,7 @@ #include "../general.h" #include "input_keymaps.h" #include "input_common.h" +#include "input_joypad.h" static long oldKbmd = 0xffff; static struct termios oldTerm, newTerm; diff --git a/input/sdl_input.c b/input/sdl_input.c index 1e251e4af7..8a6db243e0 100644 --- a/input/sdl_input.c +++ b/input/sdl_input.c @@ -25,6 +25,7 @@ #include "../libretro.h" #include "input_autodetect.h" #include "input_common.h" +#include "input_joypad.h" #include "input_keymaps.h" #include "keyboard_line.h" diff --git a/input/udev_input.c b/input/udev_input.c index 4fb1b5285d..5322203cf8 100644 --- a/input/udev_input.c +++ b/input/udev_input.c @@ -14,6 +14,7 @@ */ #include "input_common.h" +#include "input_joypad.h" #include "input_keymaps.h" #include "../general.h" #include diff --git a/input/x11_input.c b/input/x11_input.c index 4935253b9c..d0deeb0486 100644 --- a/input/x11_input.c +++ b/input/x11_input.c @@ -14,6 +14,7 @@ */ #include "input_common.h" +#include "input_joypad.h" #include "input_keymaps.h" #include "../driver.h" diff --git a/menu/menu_input_line_cb.c b/menu/menu_input_line_cb.c index f48a5079c5..cbd7ed9007 100644 --- a/menu/menu_input_line_cb.c +++ b/menu/menu_input_line_cb.c @@ -30,6 +30,7 @@ #include "menu_shader.h" #include "../performance.h" #include "../settings_data.h" +#include "../input/input_joypad.h" void menu_key_start_line(void *data, const char *label, const char *label_setting, unsigned type, unsigned idx, diff --git a/tools/retroarch-joyconfig-griffin.c b/tools/retroarch-joyconfig-griffin.c index 6dd31d33e8..9f3e0f0e5f 100644 --- a/tools/retroarch-joyconfig-griffin.c +++ b/tools/retroarch-joyconfig-griffin.c @@ -50,6 +50,7 @@ #include "../input/nullinput_joypad.c" #include "../input/input_context.c" +#include "../input/input_joypad.c" #include "../input/input_common.c" #include "../input/input_keymaps.c" diff --git a/tools/retroarch-joyconfig.c b/tools/retroarch-joyconfig.c index faa4a86ca2..7b32c7eee1 100644 --- a/tools/retroarch-joyconfig.c +++ b/tools/retroarch-joyconfig.c @@ -24,6 +24,7 @@ #include #include #include "../input/input_common.h" +#include "../input/input_joypad.h" #include "../general.h" #include