mirror of
https://github.com/libretro/RetroArch
synced 2025-02-07 03:40:24 +00:00
Simplify axis state code for joypad drivers
This commit is contained in:
parent
1e9501c672
commit
6f26f37cec
@ -87,7 +87,6 @@ static int16_t android_joypad_axis_state(
|
||||
if (val > 0)
|
||||
return val;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -64,12 +64,12 @@ static int32_t ctr_joypad_button(unsigned port_num, uint16_t joykey)
|
||||
|
||||
static void ctr_joypad_get_buttons(unsigned port_num, input_bits_t *state)
|
||||
{
|
||||
if (port_num < DEFAULT_MAX_PADS)
|
||||
if (port_num < DEFAULT_MAX_PADS)
|
||||
{
|
||||
BITS_COPY16_PTR( state, pad_state );
|
||||
}
|
||||
BITS_COPY16_PTR( state, pad_state );
|
||||
}
|
||||
else
|
||||
BIT256_CLEAR_ALL_PTR(state);
|
||||
BIT256_CLEAR_ALL_PTR(state);
|
||||
}
|
||||
|
||||
static int16_t ctr_joypad_axis_state(unsigned port_num, uint32_t joyaxis)
|
||||
|
@ -211,17 +211,17 @@ static int16_t dos_joypad_state(
|
||||
uint16_t port_idx = joypad_info->joy_idx;
|
||||
uint16_t *buf = dos_keyboard_state_get(port_idx);
|
||||
|
||||
if (port_idx >= DEFAULT_MAX_PADS)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||
if (port_idx < DEFAULT_MAX_PADS)
|
||||
{
|
||||
/* Auto-binds are per joypad, not per user. */
|
||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
||||
if ((uint16_t)joykey != NO_BTN && dos_joypad_button_state(
|
||||
buf, (uint16_t)joykey))
|
||||
ret |= ( 1 << i);
|
||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||
{
|
||||
/* Auto-binds are per joypad, not per user. */
|
||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
||||
if ((uint16_t)joykey != NO_BTN && dos_joypad_button_state(
|
||||
buf, (uint16_t)joykey))
|
||||
ret |= ( 1 << i);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -258,12 +258,12 @@ static int32_t gx_joypad_button(unsigned port, uint16_t joykey)
|
||||
|
||||
static void gx_joypad_get_buttons(unsigned port, input_bits_t *state)
|
||||
{
|
||||
if (port < DEFAULT_MAX_PADS)
|
||||
if (port < DEFAULT_MAX_PADS)
|
||||
{
|
||||
BITS_COPY16_PTR( state, pad_state[port] );
|
||||
}
|
||||
BITS_COPY16_PTR( state, pad_state[port] );
|
||||
}
|
||||
else
|
||||
BIT256_CLEAR_ALL_PTR(state);
|
||||
BIT256_CLEAR_ALL_PTR(state);
|
||||
}
|
||||
|
||||
static int16_t gx_joypad_axis_state(unsigned port, uint32_t joyaxis)
|
||||
|
@ -538,19 +538,17 @@ static int16_t apple_gamecontroller_joypad_axis(
|
||||
{
|
||||
if (AXIS_NEG_GET(joyaxis) < 4)
|
||||
{
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_NEG_GET(joyaxis);
|
||||
if (axis >= 0 && axis < 4)
|
||||
if ((val = mfi_axes[port][axis]) < 0)
|
||||
return val;
|
||||
int16_t val = mfi_axes[port][axis];
|
||||
if (val < 0)
|
||||
return val;
|
||||
}
|
||||
else if(AXIS_POS_GET(joyaxis) < 4)
|
||||
{
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_POS_GET(joyaxis);
|
||||
if (axis >= 0 && axis < 4)
|
||||
if ((val = mfi_axes[port][axis]) > 0)
|
||||
return val;
|
||||
int16_t val = mfi_axes[port][axis];
|
||||
if (val > 0)
|
||||
return val;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -564,26 +562,26 @@ static int16_t apple_gamecontroller_joypad_state(
|
||||
int16_t ret = 0;
|
||||
uint16_t port_idx = joypad_info->joy_idx;
|
||||
|
||||
if (port_idx >= DEFAULT_MAX_PADS)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||
if (port_idx < DEFAULT_MAX_PADS)
|
||||
{
|
||||
/* Auto-binds are per joypad, not per user. */
|
||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
||||
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
||||
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
||||
if ( (uint16_t)joykey != NO_BTN
|
||||
&& !GET_HAT_DIR(i)
|
||||
&& (i < 32)
|
||||
&& ((mfi_buttons[port_idx] & (1 << i)) != 0)
|
||||
)
|
||||
ret |= ( 1 << i);
|
||||
else if (joyaxis != AXIS_NONE &&
|
||||
((float)abs(apple_gamecontroller_joypad_axis(port_idx, joyaxis))
|
||||
/ 0x8000) > joypad_info->axis_threshold)
|
||||
ret |= (1 << i);
|
||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||
{
|
||||
/* Auto-binds are per joypad, not per user. */
|
||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
||||
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
||||
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
||||
if ( (uint16_t)joykey != NO_BTN
|
||||
&& !GET_HAT_DIR(i)
|
||||
&& (i < 32)
|
||||
&& ((mfi_buttons[port_idx] & (1 << i)) != 0)
|
||||
)
|
||||
ret |= ( 1 << i);
|
||||
else if (joyaxis != AXIS_NONE &&
|
||||
((float)abs(apple_gamecontroller_joypad_axis(port_idx, joyaxis))
|
||||
/ 0x8000) > joypad_info->axis_threshold)
|
||||
ret |= (1 << i);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -340,11 +340,8 @@ static int32_t parport_joypad_button(unsigned port, uint16_t joykey)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int16_t parport_joypad_axis(unsigned port, uint32_t joyaxis)
|
||||
{
|
||||
/* Parport does not support analog sticks */
|
||||
return 0;
|
||||
}
|
||||
/* TODO/FIXME - Parport does not support analog sticks */
|
||||
static int16_t parport_joypad_axis(unsigned port, uint32_t joyaxis) { return 0; }
|
||||
|
||||
static int16_t parport_joypad_state(
|
||||
rarch_joypad_info_t *joypad_info,
|
||||
|
@ -97,41 +97,43 @@ static int32_t ps2_joypad_button(unsigned port, uint16_t joykey)
|
||||
|
||||
static int16_t ps2_joypad_axis_state(unsigned port_num, uint32_t joyaxis)
|
||||
{
|
||||
int val = 0;
|
||||
int axis = -1;
|
||||
bool is_neg = false;
|
||||
bool is_pos = false;
|
||||
|
||||
if (AXIS_NEG_GET(joyaxis) < 4)
|
||||
{
|
||||
axis = AXIS_NEG_GET(joyaxis);
|
||||
is_neg = true;
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_NEG_GET(joyaxis);
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
val = analog_state[port_num][0][axis];
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
val = analog_state[port_num][1][axis - 2];
|
||||
break;
|
||||
}
|
||||
if (val < 0)
|
||||
return val;
|
||||
}
|
||||
else if (AXIS_POS_GET(joyaxis) < 4)
|
||||
{
|
||||
axis = AXIS_POS_GET(joyaxis);
|
||||
is_pos = true;
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_POS_GET(joyaxis);
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
val = analog_state[port_num][0][axis];
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
val = analog_state[port_num][1][axis - 2];
|
||||
break;
|
||||
}
|
||||
if (val > 0)
|
||||
return val;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
val = analog_state[port_num][0][axis];
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
val = analog_state[port_num][1][axis - 2];
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_neg && val > 0)
|
||||
return 0;
|
||||
else if (is_pos && val < 0)
|
||||
return 0;
|
||||
return val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int16_t ps2_joypad_state(
|
||||
@ -143,25 +145,25 @@ static int16_t ps2_joypad_state(
|
||||
int16_t ret = 0;
|
||||
uint16_t port_idx = joypad_info->joy_idx;
|
||||
|
||||
if (port_idx >= DEFAULT_MAX_PADS)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||
if (port_idx < DEFAULT_MAX_PADS)
|
||||
{
|
||||
/* Auto-binds are per joypad, not per user. */
|
||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
||||
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
||||
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
||||
if (
|
||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||
{
|
||||
/* Auto-binds are per joypad, not per user. */
|
||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
||||
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
||||
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
||||
if (
|
||||
(uint16_t)joykey != NO_BTN
|
||||
&& pad_state[port_idx] & (UINT64_C(1) << joykey)
|
||||
)
|
||||
ret |= ( 1 << i);
|
||||
else if (joyaxis != AXIS_NONE &&
|
||||
((float)abs(ps2_joypad_axis_state(port_idx, joyaxis))
|
||||
/ 0x8000) > joypad_info->axis_threshold)
|
||||
ret |= (1 << i);
|
||||
&& pad_state[port_idx] & (UINT64_C(1) << joykey)
|
||||
)
|
||||
ret |= ( 1 << i);
|
||||
else if (joyaxis != AXIS_NONE &&
|
||||
((float)abs(ps2_joypad_axis_state(port_idx, joyaxis))
|
||||
/ 0x8000) > joypad_info->axis_threshold)
|
||||
ret |= (1 << i);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -31,7 +31,7 @@
|
||||
|
||||
/* TODO/FIXME - static globals */
|
||||
static uint64_t pad_state[DEFAULT_MAX_PADS];
|
||||
static int16_t analog_state[DEFAULT_MAX_PADS][2][2];
|
||||
static int16_t analog_state[DEFAULT_MAX_PADS][2][2];
|
||||
static uint64_t pads_connected[DEFAULT_MAX_PADS];
|
||||
#if 0
|
||||
sensor_t accelerometer_state[DEFAULT_MAX_PADS];
|
||||
|
@ -373,16 +373,18 @@ static int16_t sdl_joypad_axis_state(
|
||||
{
|
||||
if (AXIS_NEG_GET(joyaxis) < pad->num_axes)
|
||||
{
|
||||
int16_t val = sdl_pad_get_axis(pad, AXIS_NEG_GET(joyaxis));
|
||||
/* -0x8000 can cause trouble if we later abs() it. */
|
||||
if (val < -0x7fff)
|
||||
return -0x7fff;
|
||||
else if (val < 0)
|
||||
int16_t val = sdl_pad_get_axis(pad, AXIS_NEG_GET(joyaxis));
|
||||
if (val < 0)
|
||||
{
|
||||
/* Clamp - -0x8000 can cause trouble if we later abs() it. */
|
||||
if (val < -0x7fff)
|
||||
return -0x7fff;
|
||||
return val;
|
||||
}
|
||||
}
|
||||
else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
|
||||
{
|
||||
int16_t val = sdl_pad_get_axis(pad, AXIS_POS_GET(joyaxis));
|
||||
int16_t val = sdl_pad_get_axis(pad, AXIS_POS_GET(joyaxis));
|
||||
if (val > 0)
|
||||
return val;
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ static int16_t switch_joypad_axis_state(unsigned port, uint32_t joyaxis)
|
||||
{
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_NEG_GET(joyaxis);
|
||||
switch(axis)
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
@ -137,7 +137,7 @@ static int16_t switch_joypad_axis_state(unsigned port, uint32_t joyaxis)
|
||||
{
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_POS_GET(joyaxis);
|
||||
switch(axis)
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
|
@ -259,7 +259,7 @@ static int udev_add_pad(struct udev_device *dev, unsigned p, int fd, const char
|
||||
pad->axes[axes] = udev_compute_axis(abs, abs->value);
|
||||
/* Deal with analog triggers that report -32767 to 32767
|
||||
by testing if the axis initial value is negative, allowing for
|
||||
for some slop (1300 =~ 4%)in an axis centred around 0.
|
||||
for some slop (1300 =~ 4%) in an axis centred around 0.
|
||||
The actual work is done in udev_joypad_axis.
|
||||
All bets are off if you're sitting on it. Reinitailise it by unpluging
|
||||
and plugging back in. */
|
||||
|
@ -133,48 +133,56 @@ static int32_t xdk_joypad_button(unsigned port, uint16_t joykey)
|
||||
static int16_t xdk_joypad_axis_state(XINPUT_GAMEPAD *pad,
|
||||
unsigned port, uint32_t joyaxis)
|
||||
{
|
||||
int val = 0;
|
||||
int axis = -1;
|
||||
bool is_neg = false;
|
||||
bool is_pos = false;
|
||||
|
||||
if (AXIS_NEG_GET(joyaxis) <= 3)
|
||||
{
|
||||
axis = AXIS_NEG_GET(joyaxis);
|
||||
is_neg = true;
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_NEG_GET(joyaxis);
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
val = pad->sThumbLX;
|
||||
break;
|
||||
case 1:
|
||||
val = pad->sThumbLY;
|
||||
break;
|
||||
case 2:
|
||||
val = pad->sThumbRX;
|
||||
break;
|
||||
case 3:
|
||||
val = pad->sThumbRY;
|
||||
break;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
/* Clamp to avoid warnings */
|
||||
if (val == -32768)
|
||||
return -32767;
|
||||
return val;
|
||||
}
|
||||
}
|
||||
else if (AXIS_POS_GET(joyaxis) <= 5)
|
||||
{
|
||||
axis = AXIS_POS_GET(joyaxis);
|
||||
is_pos = true;
|
||||
int16_t val = 0;
|
||||
int16_t axis = AXIS_POS_GET(joyaxis);
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
val = pad->sThumbLX;
|
||||
break;
|
||||
case 1:
|
||||
val = pad->sThumbLY;
|
||||
break;
|
||||
case 2:
|
||||
val = pad->sThumbRX;
|
||||
break;
|
||||
case 3:
|
||||
val = pad->sThumbRY;
|
||||
break;
|
||||
}
|
||||
if (val > 0)
|
||||
return val;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
|
||||
switch (axis)
|
||||
{
|
||||
case 0:
|
||||
val = pad->sThumbLX;
|
||||
break;
|
||||
case 1:
|
||||
val = pad->sThumbLY;
|
||||
break;
|
||||
case 2:
|
||||
val = pad->sThumbRX;
|
||||
break;
|
||||
case 3:
|
||||
val = pad->sThumbRY;
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_neg && val > 0)
|
||||
return 0;
|
||||
else if (is_pos && val < 0)
|
||||
return 0;
|
||||
/* Clamp to avoid warnings */
|
||||
else if (val == -32768)
|
||||
return -32767;
|
||||
return val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int16_t xdk_joypad_axis(unsigned port, uint32_t joyaxis)
|
||||
|
Loading…
x
Reference in New Issue
Block a user