(udev_joypad.c) Cleanups
This commit is contained in:
parent
96d39a78cc
commit
42a0618812
|
@ -335,28 +335,16 @@ static void udev_check_device(struct udev_device *dev, const char *path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pad = udev_find_vacant_pad();
|
if ((pad = udev_find_vacant_pad()) < 0)
|
||||||
if (pad < 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fd = udev_open_joystick(path);
|
if ((fd = udev_open_joystick(path)) < 0)
|
||||||
if (fd < 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ret = udev_add_pad(dev, pad, fd, path);
|
if (udev_add_pad(dev, pad, fd, path) == -1)
|
||||||
|
|
||||||
switch (ret)
|
|
||||||
{
|
{
|
||||||
case -1:
|
RARCH_ERR("[udev]: Failed to add pad: %s.\n", path);
|
||||||
RARCH_ERR("[udev]: Failed to add pad: %s.\n", path);
|
close(fd);
|
||||||
close(fd);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
/* Pad was autoconfigured. */
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -412,7 +400,6 @@ static void udev_joypad_destroy(void)
|
||||||
static bool udev_set_rumble(unsigned i,
|
static bool udev_set_rumble(unsigned i,
|
||||||
enum retro_rumble_effect effect, uint16_t strength)
|
enum retro_rumble_effect effect, uint16_t strength)
|
||||||
{
|
{
|
||||||
int old_effect;
|
|
||||||
uint16_t old_strength;
|
uint16_t old_strength;
|
||||||
struct udev_joypad *pad = (struct udev_joypad*)&udev_pads[i];
|
struct udev_joypad *pad = (struct udev_joypad*)&udev_pads[i];
|
||||||
|
|
||||||
|
@ -422,64 +409,64 @@ static bool udev_set_rumble(unsigned i,
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
old_strength = pad->strength[effect];
|
old_strength = pad->strength[effect];
|
||||||
if (old_strength == strength)
|
if (old_strength != strength)
|
||||||
return true;
|
|
||||||
|
|
||||||
old_effect = pad->has_set_ff[effect] ? pad->effects[effect] : -1;
|
|
||||||
|
|
||||||
if (strength && strength != pad->configured_strength[effect])
|
|
||||||
{
|
{
|
||||||
/* Create new or update old playing state. */
|
int old_effect = pad->has_set_ff[effect] ? pad->effects[effect] : -1;
|
||||||
struct ff_effect e = {0};
|
|
||||||
/* This defines the length of the effect and
|
|
||||||
the delay before playing it. This means there
|
|
||||||
is a limit on the maximum vibration time, but
|
|
||||||
it's hopefully sufficient for most cases. Maybe
|
|
||||||
there's a better way? */
|
|
||||||
struct ff_replay replay = {0xffff, 0};
|
|
||||||
|
|
||||||
e.type = FF_RUMBLE;
|
if (strength && strength != pad->configured_strength[effect])
|
||||||
e.id = old_effect;
|
|
||||||
e.replay = replay;
|
|
||||||
|
|
||||||
switch (effect)
|
|
||||||
{
|
{
|
||||||
case RETRO_RUMBLE_STRONG:
|
/* Create new or update old playing state. */
|
||||||
e.u.rumble.strong_magnitude = strength;
|
struct ff_effect e = {0};
|
||||||
break;
|
/* This defines the length of the effect and
|
||||||
case RETRO_RUMBLE_WEAK:
|
the delay before playing it. This means there
|
||||||
e.u.rumble.weak_magnitude = strength;
|
is a limit on the maximum vibration time, but
|
||||||
break;
|
it's hopefully sufficient for most cases. Maybe
|
||||||
default:
|
there's a better way? */
|
||||||
|
struct ff_replay replay = {0xffff, 0};
|
||||||
|
|
||||||
|
e.type = FF_RUMBLE;
|
||||||
|
e.id = old_effect;
|
||||||
|
e.replay = replay;
|
||||||
|
|
||||||
|
switch (effect)
|
||||||
|
{
|
||||||
|
case RETRO_RUMBLE_STRONG:
|
||||||
|
e.u.rumble.strong_magnitude = strength;
|
||||||
|
break;
|
||||||
|
case RETRO_RUMBLE_WEAK:
|
||||||
|
e.u.rumble.weak_magnitude = strength;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ioctl(pad->fd, EVIOCSFF, &e) < 0)
|
||||||
|
{
|
||||||
|
RARCH_ERR("Failed to set rumble effect on pad #%u.\n", i);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pad->effects[effect] = e.id;
|
||||||
|
pad->has_set_ff[effect] = true;
|
||||||
|
pad->configured_strength[effect] = strength;
|
||||||
}
|
}
|
||||||
|
pad->strength[effect] = strength;
|
||||||
|
|
||||||
if (ioctl(pad->fd, EVIOCSFF, &e) < 0)
|
/* It seems that we can update strength with EVIOCSFF atomically. */
|
||||||
|
if ((!!strength) != (!!old_strength))
|
||||||
{
|
{
|
||||||
RARCH_ERR("Failed to set rumble effect on pad #%u.\n", i);
|
struct input_event play;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pad->effects[effect] = e.id;
|
play.type = EV_FF;
|
||||||
pad->has_set_ff[effect] = true;
|
play.code = pad->effects[effect];
|
||||||
pad->configured_strength[effect] = strength;
|
play.value = !!strength;
|
||||||
}
|
|
||||||
pad->strength[effect] = strength;
|
|
||||||
|
|
||||||
/* It seems that we can update strength with EVIOCSFF atomically. */
|
if (write(pad->fd, &play, sizeof(play)) < (ssize_t)sizeof(play))
|
||||||
if ((!!strength) != (!!old_strength))
|
{
|
||||||
{
|
RARCH_ERR("[udev]: Failed to play rumble effect #%u on pad #%u.\n",
|
||||||
struct input_event play = {{0}};
|
effect, i);
|
||||||
|
return false;
|
||||||
play.type = EV_FF;
|
}
|
||||||
play.code = pad->effects[effect];
|
|
||||||
play.value = !!strength;
|
|
||||||
|
|
||||||
if (write(pad->fd, &play, sizeof(play)) < (ssize_t)sizeof(play))
|
|
||||||
{
|
|
||||||
RARCH_ERR("[udev]: Failed to play rumble effect #%u on pad #%u.\n",
|
|
||||||
effect, i);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -602,27 +589,23 @@ static void *udev_joypad_init(void *data)
|
||||||
for (i = 0; i < MAX_USERS; i++)
|
for (i = 0; i < MAX_USERS; i++)
|
||||||
udev_pads[i].fd = -1;
|
udev_pads[i].fd = -1;
|
||||||
|
|
||||||
udev_joypad_fd = udev_new();
|
if (!(udev_joypad_fd = udev_new()))
|
||||||
if (!udev_joypad_fd)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
udev_joypad_mon = udev_monitor_new_from_netlink(udev_joypad_fd, "udev");
|
if ((udev_joypad_mon = udev_monitor_new_from_netlink(udev_joypad_fd, "udev")))
|
||||||
if (udev_joypad_mon)
|
|
||||||
{
|
{
|
||||||
udev_monitor_filter_add_match_subsystem_devtype(
|
udev_monitor_filter_add_match_subsystem_devtype(
|
||||||
udev_joypad_mon, "input", NULL);
|
udev_joypad_mon, "input", NULL);
|
||||||
udev_monitor_enable_receiving(udev_joypad_mon);
|
udev_monitor_enable_receiving(udev_joypad_mon);
|
||||||
}
|
}
|
||||||
|
|
||||||
enumerate = udev_enumerate_new(udev_joypad_fd);
|
if (!(enumerate = udev_enumerate_new(udev_joypad_fd)))
|
||||||
if (!enumerate)
|
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
|
udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
|
||||||
udev_enumerate_add_match_subsystem(enumerate, "input");
|
udev_enumerate_add_match_subsystem(enumerate, "input");
|
||||||
udev_enumerate_scan_devices(enumerate);
|
udev_enumerate_scan_devices(enumerate);
|
||||||
devs = udev_enumerate_get_list_entry(enumerate);
|
if (!(devs = udev_enumerate_get_list_entry(enumerate)))
|
||||||
if (!devs)
|
|
||||||
RARCH_DBG("[udev]: Couldn't open any joypads. Are permissions set correctly for /dev/input/event* and /run/udev/?\n");
|
RARCH_DBG("[udev]: Couldn't open any joypads. Are permissions set correctly for /dev/input/event* and /run/udev/?\n");
|
||||||
|
|
||||||
udev_list_entry_foreach(item, devs)
|
udev_list_entry_foreach(item, devs)
|
||||||
|
@ -750,31 +733,31 @@ static int16_t udev_joypad_state(
|
||||||
const struct retro_keybind *binds,
|
const struct retro_keybind *binds,
|
||||||
unsigned port)
|
unsigned port)
|
||||||
{
|
{
|
||||||
unsigned i;
|
|
||||||
int16_t ret = 0;
|
int16_t ret = 0;
|
||||||
uint16_t port_idx = joypad_info->joy_idx;
|
uint16_t port_idx = joypad_info->joy_idx;
|
||||||
const struct udev_joypad *pad = (const struct udev_joypad*)
|
|
||||||
&udev_pads[port_idx];
|
|
||||||
|
|
||||||
if (port_idx >= DEFAULT_MAX_PADS)
|
if (port_idx < DEFAULT_MAX_PADS)
|
||||||
return 0;
|
|
||||||
|
|
||||||
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
|
||||||
{
|
{
|
||||||
/* Auto-binds are per joypad, not per user. */
|
unsigned i;
|
||||||
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
const struct udev_joypad *pad = (const struct udev_joypad*)
|
||||||
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
&udev_pads[port_idx];
|
||||||
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
||||||
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
{
|
||||||
if (
|
/* 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
|
(uint16_t)joykey != NO_BTN
|
||||||
&& udev_joypad_button_state(pad, port_idx, (uint16_t)joykey)
|
&& udev_joypad_button_state(pad, port_idx, (uint16_t)joykey)
|
||||||
)
|
)
|
||||||
ret |= ( 1 << i);
|
ret |= ( 1 << i);
|
||||||
else if (joyaxis != AXIS_NONE &&
|
else if (joyaxis != AXIS_NONE &&
|
||||||
((float)abs(udev_joypad_axis_state(pad, port_idx, joyaxis))
|
((float)abs(udev_joypad_axis_state(pad, port_idx, joyaxis))
|
||||||
/ 0x8000) > joypad_info->axis_threshold)
|
/ 0x8000) > joypad_info->axis_threshold)
|
||||||
ret |= (1 << i);
|
ret |= (1 << i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
Loading…
Reference in New Issue