dinput_joypad_button - further cleanups

This commit is contained in:
twinaphex 2019-07-23 18:37:28 +02:00
parent 051fb92e83
commit f06607141d
2 changed files with 51 additions and 58 deletions

View File

@ -49,8 +49,10 @@ struct dinput_joypad_data
/* For DIJOYSTATE2 struct, rgbButtons will always have 128 elements */ /* For DIJOYSTATE2 struct, rgbButtons will always have 128 elements */
#define ARRAY_SIZE_RGB_BUTTONS 128 #define ARRAY_SIZE_RGB_BUTTONS 128
/* For DIJOYSTATE2 struct, rgdwPOV will always have 4 elements */
#define ARRAY_SIZE_RGDWPOV 4 #ifndef NUM_HATS
#define NUM_HATS 4
#endif
static struct dinput_joypad_data g_pads[MAX_USERS]; static struct dinput_joypad_data g_pads[MAX_USERS];
static unsigned g_joypad_cnt; static unsigned g_joypad_cnt;
@ -423,71 +425,59 @@ static bool dinput_joypad_init(void *data)
static bool dinput_joypad_button(unsigned port_num, uint16_t joykey) static bool dinput_joypad_button(unsigned port_num, uint16_t joykey)
{ {
unsigned hat_dir = 0;
const struct dinput_joypad_data *pad = &g_pads[port_num]; const struct dinput_joypad_data *pad = &g_pads[port_num];
unsigned hat_dir = GET_HAT_DIR(joykey);
if (!pad || !pad->joypad)
return false;
hat_dir = GET_HAT_DIR(joykey);
/* Check hat. */
if (hat_dir) if (hat_dir)
{ {
unsigned pov; unsigned h = GET_HAT(joykey);
unsigned hat = GET_HAT(joykey); if (h < NUM_HATS)
if (hat >= ARRAY_SIZE_RGDWPOV)
return false;
pov = pad->joy_state.rgdwPOV[hat];
switch (hat_dir)
{ {
case HAT_UP_MASK: unsigned pov = pad->joy_state.rgdwPOV[h];
{ switch (hat_dir)
static const unsigned check1 = (JOY_POVRIGHT/2); {
static const unsigned check2 = (JOY_POVLEFT+JOY_POVRIGHT/2); case HAT_UP_MASK:
return {
(pov == JOY_POVFORWARD) || static const unsigned check1 = (JOY_POVRIGHT/2);
(pov == check1) || static const unsigned check2 = (JOY_POVLEFT+JOY_POVRIGHT/2);
(pov == check2); return
} (pov == JOY_POVFORWARD) ||
case HAT_RIGHT_MASK: (pov == check1) ||
{ (pov == check2);
static const unsigned check1 = (JOY_POVRIGHT/2); }
static const unsigned check2 = (JOY_POVRIGHT+JOY_POVRIGHT/2); case HAT_RIGHT_MASK:
return {
(pov == JOY_POVRIGHT) || static const unsigned check1 = (JOY_POVRIGHT/2);
(pov == check1) || static const unsigned check2 = (JOY_POVRIGHT+JOY_POVRIGHT/2);
(pov == check2); return
} (pov == JOY_POVRIGHT) ||
case HAT_DOWN_MASK: (pov == check1) ||
{ (pov == check2);
static const unsigned check1 = (JOY_POVRIGHT+JOY_POVRIGHT/2); }
static const unsigned check2 = (JOY_POVBACKWARD+JOY_POVRIGHT/2); case HAT_DOWN_MASK:
return {
(pov == JOY_POVBACKWARD) || static const unsigned check1 = (JOY_POVRIGHT+JOY_POVRIGHT/2);
(pov == check1) || static const unsigned check2 = (JOY_POVBACKWARD+JOY_POVRIGHT/2);
(pov == check2); return
} (pov == JOY_POVBACKWARD) ||
case HAT_LEFT_MASK: (pov == check1) ||
{ (pov == check2);
static const unsigned check1 = (JOY_POVBACKWARD+JOY_POVRIGHT/2); }
static const unsigned check2 = (JOY_POVLEFT+JOY_POVRIGHT/2); case HAT_LEFT_MASK:
return {
(pov == JOY_POVLEFT) || static const unsigned check1 = (JOY_POVBACKWARD+JOY_POVRIGHT/2);
(pov == check1) || static const unsigned check2 = (JOY_POVLEFT+JOY_POVRIGHT/2);
(pov == check2); return
} (pov == JOY_POVLEFT) ||
(pov == check1) ||
(pov == check2);
}
}
} }
return false; return false;
} }
return (joykey < ARRAY_SIZE_RGB_BUTTONS) && pad->joy_state.rgbButtons[joykey];
return (joykey < ARRAY_SIZE_RGB_BUTTONS)
&& pad->joy_state.rgbButtons[joykey];
} }
static int16_t dinput_joypad_axis(unsigned port_num, uint32_t joyaxis) static int16_t dinput_joypad_axis(unsigned port_num, uint32_t joyaxis)

View File

@ -51,7 +51,10 @@
#define UDEV_NUM_BUTTONS 32 #define UDEV_NUM_BUTTONS 32
#define NUM_AXES 32 #define NUM_AXES 32
#ifndef NUM_HATS
#define NUM_HATS 4 #define NUM_HATS 4
#endif
#define test_bit(nr, addr) \ #define test_bit(nr, addr) \
(((1UL << ((nr) % (sizeof(long) * CHAR_BIT))) & ((addr)[(nr) / (sizeof(long) * CHAR_BIT)])) != 0) (((1UL << ((nr) % (sizeof(long) * CHAR_BIT))) & ((addr)[(nr) / (sizeof(long) * CHAR_BIT)])) != 0)