Refactor buttons variable in udev joypad driver

This commit is contained in:
twinaphex 2014-10-16 22:25:09 +02:00
parent a84d11abda
commit 3fff1abb83
2 changed files with 29 additions and 18 deletions

View File

@ -45,7 +45,7 @@ struct udev_joypad
dev_t device;
// Input state polled
bool buttons[NUM_BUTTONS];
int16_t buttons;
int16_t axes[NUM_AXES];
int8_t hats[NUM_HATS][2];
@ -83,12 +83,12 @@ static inline int16_t compute_axis(const struct input_absinfo *info, int value)
static void poll_pad(unsigned p)
{
struct udev_joypad *pad = &g_pads[p];
if (pad->fd < 0)
return;
int i, len;
struct input_event events[32];
struct udev_joypad *pad = &g_pads[p];
if (pad->fd < 0)
return;
while ((len = read(pad->fd, events, sizeof(events))) > 0)
{
@ -100,7 +100,12 @@ static void poll_pad(unsigned p)
{
case EV_KEY:
if (code >= BTN_MISC || (code >= KEY_UP && code <= KEY_DOWN))
pad->buttons[pad->button_bind[code]] = events[i].value;
{
if (events[i].value)
BIT16_SET(pad->buttons, pad->button_bind[code]);
else
BIT16_CLEAR(pad->buttons, pad->button_bind[code]);
}
break;
case EV_ABS:
@ -552,7 +557,7 @@ static bool udev_joypad_button(unsigned port, uint16_t joykey)
if (GET_HAT_DIR(joykey))
return udev_joypad_hat(pad, joykey);
return joykey < NUM_BUTTONS && pad->buttons[joykey];
return joykey < NUM_BUTTONS && BIT16_GET(pad->buttons, joykey);
}
static int16_t udev_joypad_axis(unsigned port, uint32_t joyaxis)

View File

@ -116,23 +116,29 @@ typedef struct
uint32_t data[8];
} rarch_bits_t;
#define BIT128_SET(a, bit) ((a).data[(bit) >> 5] |= (1 << ((bit) & 31))
#define BIT128_CLEAR(a, bit) ((a).data[(bit) >> 5] &= ~(1 << ((bit) & 31)))
#define BIT128_GET(a, bit) ((a).data[(bit) >> 5] & (1 << ((bit) & 31)))
#define BIT128_CLEAR_ALL(a) memset(&(a), 0, sizeof(a));
#define BIT_SET(a, bit) ((a)[(bit) >> 3] |= (1 << ((bit) & 7)))
#define BIT_CLEAR(a, bit) ((a)[(bit) >> 3] &= ~(1 << ((bit) & 7)))
#define BIT_GET(a, bit) ((a)[(bit) >> 3] & (1 << ((bit) & 7)))
#define BIT64_SET(a, bit) ((a) |= (1ULL << ((bit) & 63)))
#define BIT64_CLEAR(a, bit) ((a) &= ~(1ULL << ((bit) & 63)))
#define BIT64_GET(a, bit) (!!((a) & (1ULL << ((bit) & 63))))
#define BIT64_CLEAR_ALL(a) ((a) = 0)
#define BIT16_SET(a, bit) ((a) |= (1 << ((bit) & 15)))
#define BIT16_CLEAR(a, bit) ((a) &= ~(1 << ((bit) & 15)))
#define BIT16_GET(a, bit) (!!((a) & (1 << ((bit) & 15))))
#define BIT16_CLEAR_ALL(a) ((a) = 0)
#define BIT32_SET(a, bit) ((a) |= (1 << ((bit) & 31)))
#define BIT32_CLEAR(a, bit) ((a) &= ~(1 << ((bit) & 31)))
#define BIT32_GET(a, bit) (!!((a) & (1 << ((bit) & 31))))
#define BIT32_CLEAR_ALL(a) ((a) = 0)
#define BIT_SET(a, bit) ((a)[(bit) >> 3] |= (1 << ((bit) & 7)))
#define BIT_CLEAR(a, bit) ((a)[(bit) >> 3] &= ~(1 << ((bit) & 7)))
#define BIT_GET(a, bit) ((a)[(bit) >> 3] & (1 << ((bit) & 7)))
#define BIT64_SET(a, bit) ((a) |= (1ULL << ((bit) & 63)))
#define BIT64_CLEAR(a, bit) ((a) &= ~(1ULL << ((bit) & 63)))
#define BIT64_GET(a, bit) (!!((a) & (1ULL << ((bit) & 63))))
#define BIT64_CLEAR_ALL(a) ((a) = 0)
#define BIT128_SET(a, bit) ((a).data[(bit) >> 5] |= (1 << ((bit) & 31))
#define BIT128_CLEAR(a, bit) ((a).data[(bit) >> 5] &= ~(1 << ((bit) & 31)))
#define BIT128_GET(a, bit) ((a).data[(bit) >> 5] & (1 << ((bit) & 31)))
#define BIT128_CLEAR_ALL(a) memset(&(a), 0, sizeof(a));
#endif