Simplify axis state code for joypad drivers

This commit is contained in:
libretroadmin 2023-02-20 10:10:07 +01:00
parent 1e9501c672
commit 6f26f37cec
12 changed files with 149 additions and 143 deletions

View File

@ -87,7 +87,6 @@ static int16_t android_joypad_axis_state(
if (val > 0)
return val;
}
return 0;
}

View File

@ -211,9 +211,8 @@ 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;
if (port_idx < DEFAULT_MAX_PADS)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
/* Auto-binds are per joypad, not per user. */
@ -223,6 +222,7 @@ static int16_t dos_joypad_state(
buf, (uint16_t)joykey))
ret |= ( 1 << i);
}
}
return ret;
}

View File

@ -538,18 +538,16 @@ 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)
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)
int16_t val = mfi_axes[port][axis];
if (val > 0)
return val;
}
return 0;
@ -564,9 +562,8 @@ 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;
if (port_idx < DEFAULT_MAX_PADS)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
/* Auto-binds are per joypad, not per user. */
@ -585,6 +582,7 @@ static int16_t apple_gamecontroller_joypad_state(
/ 0x8000) > joypad_info->axis_threshold)
ret |= (1 << i);
}
}
return ret;
}

View File

@ -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,

View File

@ -97,24 +97,10 @@ 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;
}
else if (AXIS_POS_GET(joyaxis) < 4)
{
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
}
else
return 0;
int16_t val = 0;
int16_t axis = AXIS_NEG_GET(joyaxis);
switch (axis)
{
case 0:
@ -126,12 +112,28 @@ static int16_t ps2_joypad_axis_state(unsigned port_num, uint32_t joyaxis)
val = analog_state[port_num][1][axis - 2];
break;
}
if (is_neg && val > 0)
return 0;
else if (is_pos && val < 0)
return 0;
if (val < 0)
return val;
}
else if (AXIS_POS_GET(joyaxis) < 4)
{
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;
}
return 0;
}
static int16_t ps2_joypad_state(
@ -143,9 +145,8 @@ 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;
if (port_idx < DEFAULT_MAX_PADS)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
/* Auto-binds are per joypad, not per user. */
@ -163,6 +164,7 @@ static int16_t ps2_joypad_state(
/ 0x8000) > joypad_info->axis_threshold)
ret |= (1 << i);
}
}
return ret;
}

View File

@ -374,12 +374,14 @@ 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 < 0)
{
/* Clamp - -0x8000 can cause trouble if we later abs() it. */
if (val < -0x7fff)
return -0x7fff;
else if (val < 0)
return val;
}
}
else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
{
int16_t val = sdl_pad_get_axis(pad, AXIS_POS_GET(joyaxis));

View File

@ -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:

View File

@ -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. */

View File

@ -133,24 +133,10 @@ 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;
}
else if (AXIS_POS_GET(joyaxis) <= 5)
{
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
}
else
return 0;
int16_t val = 0;
int16_t axis = AXIS_NEG_GET(joyaxis);
switch (axis)
{
case 0:
@ -166,15 +152,37 @@ static int16_t xdk_joypad_axis_state(XINPUT_GAMEPAD *pad,
val = pad->sThumbRY;
break;
}
if (is_neg && val > 0)
return 0;
else if (is_pos && val < 0)
return 0;
if (val < 0)
{
/* Clamp to avoid warnings */
else if (val == -32768)
if (val == -32768)
return -32767;
return val;
}
}
else if (AXIS_POS_GET(joyaxis) <= 5)
{
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;
}
return 0;
}
static int16_t xdk_joypad_axis(unsigned port, uint32_t joyaxis)