From c1478665623a8de22e4b4c3d09a0dbf9b23e1a12 Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Tue, 19 Feb 2019 00:10:32 +0100 Subject: [PATCH] Implemented analog support for ps2 controllers --- input/drivers_joypad/ps2_joypad.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/input/drivers_joypad/ps2_joypad.c b/input/drivers_joypad/ps2_joypad.c index b1acfbde9b..e9a539d608 100644 --- a/input/drivers_joypad/ps2_joypad.c +++ b/input/drivers_joypad/ps2_joypad.c @@ -23,13 +23,23 @@ #define PS2_MAX_PADS 2 #define PS2_PAD_SLOT 0 /* Always zero if not using multitap */ +#define PS2_ANALOG_STICKS 2 +#define PS2_ANALOG_AXIS 2 static unsigned char padBuf[2][256] ALIGNED(64); static uint64_t pad_state[PS2_MAX_PADS]; +static int16_t analog_state[PS2_MAX_PADS][PS2_ANALOG_STICKS][PS2_ANALOG_AXIS]; extern uint64_t lifecycle_state; +static INLINE int16_t convert_u8_to_s16(uint8_t val) +{ + if (val == 0) + return -0x7fff; + return val * 0x0101 - 0x8000; +} + static const char *ps2_joypad_name(unsigned pad) { return "PS2 Controller"; @@ -115,6 +125,12 @@ static void ps2_joypad_poll(void) pad_state[player] |= (state_tmp & PAD_R3) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_R3) : 0; pad_state[player] |= (state_tmp & PAD_L3) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_L3) : 0; + /* Analog */ + analog_state[player][RETRO_DEVICE_INDEX_ANALOG_LEFT] [RETRO_DEVICE_ID_ANALOG_X] = convert_u8_to_s16(buttons.ljoy_h); + analog_state[player][RETRO_DEVICE_INDEX_ANALOG_LEFT] [RETRO_DEVICE_ID_ANALOG_Y] = convert_u8_to_s16(buttons.ljoy_v);; + analog_state[player][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_X] = convert_u8_to_s16(buttons.rjoy_h);; + analog_state[player][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_Y] = convert_u8_to_s16(buttons.rjoy_v);; + } } }