670 lines
17 KiB
C
Raw Normal View History

2013-09-25 19:32:18 +02:00
/* RetroArch - A frontend for libretro.
2015-01-07 17:46:50 +01:00
* Copyright (C) 2010-2015 - Hans-Kristian Arntzen
2016-01-10 04:33:01 +01:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2013-09-25 19:32:18 +02:00
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
2015-09-05 20:49:57 +02:00
#include <unistd.h>
2013-09-25 19:32:18 +02:00
#include <string.h>
#include <limits.h>
2015-09-05 20:49:57 +02:00
#include <fcntl.h>
2013-09-25 19:32:18 +02:00
#include <errno.h>
2015-09-05 20:49:57 +02:00
2013-09-25 19:32:18 +02:00
#include <sys/types.h>
#include <sys/stat.h>
2015-12-17 03:02:48 +01:00
#include <sys/poll.h>
#include <libudev.h>
2013-09-25 19:32:18 +02:00
#include <linux/types.h>
2013-09-25 22:40:34 +02:00
#include <linux/input.h>
2013-09-25 19:32:18 +02:00
2015-03-15 04:52:46 +01:00
#include <retro_inline.h>
2015-12-26 07:54:17 +01:00
#include <string/stdstring.h>
2015-03-15 04:52:46 +01:00
2015-09-05 20:49:57 +02:00
#include "../input_autodetect.h"
#include "../../general.h"
2015-11-23 12:03:38 +01:00
#include "../../verbosity.h"
2015-09-05 20:49:57 +02:00
/* Udev/evdev Linux joypad driver.
* More complex and extremely low level,
* but only Linux driver which can support joypad rumble.
*
* Uses udev for device detection + hotplug.
*
* Code adapted from SDL 2.0's implementation.
*/
2013-09-25 19:32:18 +02:00
#define UDEV_NUM_BUTTONS 32
2013-09-25 19:32:18 +02:00
#define NUM_AXES 32
#define NUM_HATS 4
2015-06-03 20:20:25 +02:00
#define test_bit(nr, addr) \
(((1UL << ((nr) % (sizeof(long) * CHAR_BIT))) & ((addr)[(nr) / (sizeof(long) * CHAR_BIT)])) != 0)
#define NBITS(x) ((((x) - 1) / (sizeof(long) * CHAR_BIT)) + 1)
2013-09-25 19:32:18 +02:00
struct udev_joypad
{
int fd;
dev_t device;
/* Input state polled. */
2015-02-15 01:57:29 +01:00
uint64_t buttons;
2013-09-25 19:32:18 +02:00
int16_t axes[NUM_AXES];
2013-09-25 21:41:26 +02:00
int8_t hats[NUM_HATS][2];
2013-09-25 19:32:18 +02:00
/* Maps keycodes -> button/axes */
2013-09-25 19:32:18 +02:00
uint8_t button_bind[KEY_MAX];
uint8_t axes_bind[ABS_MAX];
struct input_absinfo absinfo[NUM_AXES];
2013-09-25 22:40:34 +02:00
int num_effects;
int effects[2]; /* [0] - strong, [1] - weak */
2013-09-26 11:20:13 +02:00
bool has_set_ff[2];
uint16_t strength[2];
uint16_t configured_strength[2];
2013-09-25 22:40:34 +02:00
2015-12-17 03:02:48 +01:00
char ident[PATH_MAX_LENGTH];
2013-09-25 21:41:26 +02:00
char *path;
int32_t vid;
int32_t pid;
2013-09-25 19:32:18 +02:00
};
2015-12-17 03:02:48 +01:00
static struct udev *g_udev;
static struct udev_monitor *g_udev_mon;
2015-01-05 01:58:00 +01:00
static struct udev_joypad udev_pads[MAX_USERS];
2013-09-25 19:32:18 +02:00
2015-04-03 01:37:20 +02:00
static INLINE int16_t udev_compute_axis(const struct input_absinfo *info, int value)
2013-09-25 19:32:18 +02:00
{
2013-09-25 21:41:26 +02:00
int range = info->maximum - info->minimum;
int axis = (value - info->minimum) * 0xffffll / range - 0x7fffll;
2013-09-25 21:41:26 +02:00
if (axis > 0x7fff)
return 0x7fff;
else if (axis < -0x7fff)
return -0x7fff;
2014-08-27 03:28:22 +02:00
return axis;
2013-09-25 19:32:18 +02:00
}
2015-02-11 06:44:16 +01:00
static void udev_poll_pad(struct udev_joypad *pad, unsigned p)
2013-09-25 19:32:18 +02:00
{
int i, len;
struct input_event events[32];
2013-09-25 19:32:18 +02:00
if (pad->fd < 0)
return;
while ((len = read(pad->fd, events, sizeof(events))) > 0)
{
len /= sizeof(*events);
2013-10-22 21:26:33 +02:00
for (i = 0; i < len; i++)
2013-09-25 19:32:18 +02:00
{
int code = events[i].code;
switch (events[i].type)
{
case EV_KEY:
if (code >= BTN_MISC || (code >= KEY_UP && code <= KEY_DOWN))
{
if (events[i].value)
2015-02-15 01:57:29 +01:00
BIT64_SET(pad->buttons, pad->button_bind[code]);
else
2015-02-15 01:57:29 +01:00
BIT64_CLEAR(pad->buttons, pad->button_bind[code]);
}
2013-09-25 19:32:18 +02:00
break;
case EV_ABS:
if (code >= ABS_MISC)
break;
switch (code)
{
case ABS_HAT0X:
case ABS_HAT0Y:
case ABS_HAT1X:
case ABS_HAT1Y:
case ABS_HAT2X:
case ABS_HAT2Y:
case ABS_HAT3X:
case ABS_HAT3Y:
{
2015-04-03 01:37:20 +02:00
code -= ABS_HAT0X;
pad->hats[code >> 1][code & 1] = events[i].value;
2013-09-25 19:32:18 +02:00
break;
}
default:
{
2015-04-03 01:37:20 +02:00
unsigned axis = pad->axes_bind[code];
pad->axes[axis] = udev_compute_axis(&pad->absinfo[axis], events[i].value);
2013-09-25 19:32:18 +02:00
break;
}
}
break;
default:
break;
}
}
}
}
2015-12-17 03:02:48 +01:00
static bool udev_hotplug_available(void)
{
struct pollfd fds = {0};
if (!g_udev_mon)
return false;
fds.fd = udev_monitor_get_fd(g_udev_mon);
fds.events = POLLIN;
return (poll(&fds, 1, 0) == 1) && (fds.revents & POLLIN);
}
static int udev_find_vacant_pad(void)
2013-09-25 19:32:18 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
2013-09-25 21:41:26 +02:00
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
if (udev_pads[i].fd < 0)
return i;
return -1;
2013-09-25 19:32:18 +02:00
}
2015-04-03 01:37:20 +02:00
static int udev_open_joystick(const char *path)
2013-09-25 19:32:18 +02:00
{
2015-06-03 18:39:52 +02:00
unsigned long evbit[NBITS(EV_MAX)] = {0};
2013-09-25 19:32:18 +02:00
unsigned long keybit[NBITS(KEY_MAX)] = {0};
unsigned long absbit[NBITS(ABS_MAX)] = {0};
int fd = open(path, O_RDWR | O_NONBLOCK);
if (fd < 0)
return fd;
2013-09-25 19:32:18 +02:00
if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
(ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
(ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0))
2013-09-25 21:41:26 +02:00
goto error;
2013-09-25 19:32:18 +02:00
/* Has to at least support EV_KEY interface. */
2013-09-25 19:32:18 +02:00
if (!test_bit(EV_KEY, evbit))
2013-09-25 21:41:26 +02:00
goto error;
2013-09-25 19:32:18 +02:00
return fd;
2013-09-25 21:41:26 +02:00
error:
close(fd);
return -1;
2013-09-25 19:32:18 +02:00
}
2015-06-03 19:02:48 +02:00
static int udev_add_pad(struct udev_device *dev, unsigned p, int fd, const char *path)
2013-09-25 19:32:18 +02:00
{
2013-10-22 21:26:33 +02:00
int i;
struct stat st;
2015-06-13 00:02:28 +02:00
int ret = 0;
const char *buf = NULL;
2015-06-03 18:39:52 +02:00
unsigned buttons = 0;
unsigned axes = 0;
2015-06-03 18:36:38 +02:00
struct udev_device *parent = NULL;
struct udev_joypad *pad = (struct udev_joypad*)&udev_pads[p];
unsigned long keybit[NBITS(KEY_MAX)] = {0};
unsigned long absbit[NBITS(ABS_MAX)] = {0};
unsigned long ffbit[NBITS(FF_MAX)] = {0};
2015-06-03 18:36:38 +02:00
autoconfig_params_t params = {{0}};
settings_t *settings = config_get_ptr();
2015-06-03 19:50:48 +02:00
strlcpy(pad->ident, settings->input.device_names[p], sizeof(pad->ident));
if (ioctl(fd, EVIOCGNAME(sizeof(pad->ident)), pad->ident) < 0)
2013-09-25 23:58:02 +02:00
{
2015-06-03 19:50:48 +02:00
RARCH_LOG("[udev]: Failed to get pad name: %s.\n", pad->ident);
2015-06-03 19:02:48 +02:00
return -1;
2013-09-25 23:58:02 +02:00
}
2013-09-25 19:32:18 +02:00
/* Don't worry about unref'ing the parent. */
parent = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
pad->vid = pad->pid = 0;
if ((buf = udev_device_get_sysattr_value(parent, "idVendor")) != NULL)
pad->vid = strtol(buf, NULL, 16);
if ((buf = udev_device_get_sysattr_value(parent, "idProduct")) != NULL)
pad->pid = strtol(buf, NULL, 16);
RARCH_LOG("[udev]: Plugged pad: %s (%04x:%04x) on port #%u.\n",
pad->ident, pad->vid, pad->pid, p);
2013-09-25 19:32:18 +02:00
if (fstat(fd, &st) < 0)
2015-06-03 19:02:48 +02:00
return -1;
2013-09-25 19:32:18 +02:00
if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
(ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0))
2015-06-03 19:02:48 +02:00
return -1;
2013-09-25 19:32:18 +02:00
/* Go through all possible keycodes, check if they are used,
* and map them to button/axes/hat indices.
*/
for (i = KEY_UP; i <= KEY_DOWN && buttons < UDEV_NUM_BUTTONS; i++)
2013-09-25 19:32:18 +02:00
if (test_bit(i, keybit))
pad->button_bind[i] = buttons++;
for (i = BTN_MISC; i < KEY_MAX && buttons < UDEV_NUM_BUTTONS; i++)
2013-09-25 19:32:18 +02:00
if (test_bit(i, keybit))
pad->button_bind[i] = buttons++;
2013-10-22 21:26:33 +02:00
for (i = 0; i < ABS_MISC && axes < NUM_AXES; i++)
2013-09-25 19:32:18 +02:00
{
/* Skip hats for now. */
2013-09-25 19:32:18 +02:00
if (i == ABS_HAT0X)
{
i = ABS_HAT3Y;
continue;
}
if (test_bit(i, absbit))
{
struct input_absinfo *abs = &pad->absinfo[axes];
2013-09-25 19:32:18 +02:00
if (ioctl(fd, EVIOCGABS(i), abs) < 0)
continue;
2013-09-25 21:41:26 +02:00
if (abs->maximum > abs->minimum)
{
2015-04-03 01:37:20 +02:00
pad->axes[axes] = udev_compute_axis(abs, abs->value);
2013-09-25 21:41:26 +02:00
pad->axes_bind[i] = axes++;
}
2013-09-25 19:32:18 +02:00
}
}
pad->device = st.st_rdev;
2015-06-03 18:36:38 +02:00
pad->fd = fd;
pad->path = strdup(path);
2013-09-25 21:41:26 +02:00
if (*pad->ident)
2015-03-27 16:57:58 +01:00
{
params.idx = p;
strlcpy(params.name, pad->ident, sizeof(params.name));
params.vid = pad->vid;
params.pid = pad->pid;
2015-07-30 00:25:09 +02:00
settings->input.pid[p] = params.pid;
settings->input.vid[p] = params.vid;
strlcpy(settings->input.device_names[p], params.name, sizeof(settings->input.device_names[p]));
2015-03-27 16:57:58 +01:00
strlcpy(params.driver, udev_joypad.ident, sizeof(params.driver));
input_config_autoconfigure_joypad(&params);
2015-06-03 19:02:48 +02:00
ret = 1;
2015-03-27 16:57:58 +01:00
}
2013-09-25 19:32:18 +02:00
/* Check for rumble features. */
2013-09-25 22:40:34 +02:00
if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) >= 0)
{
if (test_bit(FF_RUMBLE, ffbit))
RARCH_LOG("[udev]: Pad #%u (%s) supports force feedback.\n",
2013-10-26 00:26:31 +02:00
p, path);
2013-09-25 22:40:34 +02:00
if (ioctl(fd, EVIOCGEFFECTS, &pad->num_effects) >= 0)
2013-10-26 00:26:31 +02:00
RARCH_LOG("[udev]: Pad #%u (%s) supports %d force feedback effects.\n", p, path, pad->num_effects);
2013-09-25 22:40:34 +02:00
}
2015-06-03 19:02:48 +02:00
return ret;
2013-09-25 19:32:18 +02:00
}
2015-04-03 01:37:20 +02:00
static void udev_check_device(struct udev_device *dev, const char *path, bool hotplugged)
2013-09-25 19:32:18 +02:00
{
2015-06-03 19:02:48 +02:00
int ret;
int pad, fd;
2013-10-22 21:26:33 +02:00
unsigned i;
2013-09-25 19:32:18 +02:00
struct stat st;
2013-09-25 19:32:18 +02:00
if (stat(path, &st) < 0)
return;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
2013-09-25 21:41:26 +02:00
{
if (st.st_rdev == udev_pads[i].device)
2013-09-25 21:41:26 +02:00
{
RARCH_LOG("[udev]: Device ID %u is already plugged.\n", (unsigned)st.st_rdev);
2013-09-25 19:32:18 +02:00
return;
2013-09-25 21:41:26 +02:00
}
}
2013-09-25 19:32:18 +02:00
2015-04-03 01:37:20 +02:00
pad = udev_find_vacant_pad();
2013-09-25 19:32:18 +02:00
if (pad < 0)
return;
2013-09-26 00:49:13 +02:00
2015-04-03 01:37:20 +02:00
fd = udev_open_joystick(path);
2013-09-26 00:49:13 +02:00
if (fd < 0)
return;
2013-09-25 19:32:18 +02:00
2015-06-03 19:02:48 +02:00
ret = udev_add_pad(dev, pad, fd, path);
2015-06-03 19:02:48 +02:00
switch (ret)
{
case -1:
RARCH_ERR("[udev]: Failed to add pad: %s.\n", path);
close(fd);
break;
case 1:
/* Pad was autoconfigured. */
break;
case 0:
default:
if (hotplugged)
{
2015-12-17 03:02:48 +01:00
char msg[PATH_MAX_LENGTH] = {0};
2015-06-13 00:02:28 +02:00
2015-06-03 19:02:48 +02:00
snprintf(msg, sizeof(msg), "Device #%u (%s) connected.", pad, path);
2015-12-07 15:32:14 +01:00
runloop_msg_queue_push(msg, 0, 60, false);
2015-06-03 19:02:48 +02:00
RARCH_LOG("[udev]: %s\n", msg);
}
break;
}
2013-09-25 21:41:26 +02:00
}
2015-06-03 19:55:50 +02:00
static void udev_free_pad(unsigned pad)
{
if (udev_pads[pad].fd >= 0)
close(udev_pads[pad].fd);
2015-06-03 19:55:50 +02:00
if (udev_pads[pad].path)
free(udev_pads[pad].path);
udev_pads[pad].path = NULL;
2015-12-26 07:54:17 +01:00
if (!string_is_empty(udev_pads[pad].ident))
2015-06-03 19:50:48 +02:00
udev_pads[pad].ident[0] = '\0';
memset(&udev_pads[pad], 0, sizeof(udev_pads[pad]));
udev_pads[pad].fd = -1;
}
2015-02-20 20:56:22 -03:00
static void udev_joypad_remove_device(const char *path)
2013-09-25 21:41:26 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
2013-09-25 21:41:26 +02:00
{
2016-01-21 02:48:00 +01:00
if (udev_pads[i].path && string_is_equal(udev_pads[i].path, path))
2013-09-25 21:41:26 +02:00
{
input_config_autoconfigure_disconnect(i, udev_pads[i].ident);
2015-06-03 19:55:50 +02:00
udev_free_pad(i);
2013-09-25 21:41:26 +02:00
break;
}
}
2013-09-25 19:32:18 +02:00
}
static void udev_joypad_destroy(void)
{
2013-10-22 21:26:33 +02:00
unsigned i;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
2015-06-03 19:55:50 +02:00
udev_free_pad(i);
2013-09-25 19:32:18 +02:00
2015-12-17 03:02:48 +01:00
if (g_udev_mon)
udev_monitor_unref(g_udev_mon);
g_udev_mon = NULL;
if (g_udev)
udev_unref(g_udev);
g_udev = NULL;
2013-09-25 19:32:18 +02:00
}
static void udev_joypad_handle_hotplug(void)
{
2015-12-17 03:02:48 +01:00
struct udev_device *dev = udev_monitor_receive_device(g_udev_mon);
2015-06-26 18:35:35 +02:00
const char *val;
const char *action;
const char *devnode;
if (!dev)
return;
2015-06-26 18:35:35 +02:00
val = udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
action = udev_device_get_action(dev);
devnode = udev_device_get_devnode(dev);
2016-01-21 02:48:00 +01:00
if (!val || !string_is_equal(val, "1") || !devnode)
goto end;
2016-01-21 02:48:00 +01:00
if (string_is_equal(action, "add"))
{
RARCH_LOG("[udev]: Hotplug add: %s.\n", devnode);
udev_check_device(dev, devnode, true);
}
2016-01-21 02:48:00 +01:00
else if (string_is_equal(action, "remove"))
{
RARCH_LOG("[udev]: Hotplug remove: %s.\n", devnode);
udev_joypad_remove_device(devnode);
}
end:
udev_device_unref(dev);
}
static bool udev_set_rumble(unsigned i, enum retro_rumble_effect effect, uint16_t strength)
{
int old_effect;
uint16_t old_strength;
struct udev_joypad *pad = (struct udev_joypad*)&udev_pads[i];
if (pad->fd < 0)
return false;
if (pad->num_effects < 2)
return false;
old_strength = pad->strength[effect];
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. */
2015-06-03 18:31:06 +02:00
struct ff_effect e = {0};
e.type = FF_RUMBLE;
2015-06-03 18:31:06 +02:00
e.id = old_effect;
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;
}
2015-06-03 18:31:06 +02:00
pad->effects[effect] = e.id;
pad->has_set_ff[effect] = true;
pad->configured_strength[effect] = strength;
}
pad->strength[effect] = strength;
/* It seems that we can update strength with EVIOCSFF atomically. */
if ((!!strength) != (!!old_strength))
{
struct input_event play = {{0}};
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;
}
}
return true;
}
static void udev_joypad_poll(void)
{
unsigned i;
2015-12-17 03:02:48 +01:00
while (udev_hotplug_available())
udev_joypad_handle_hotplug();
for (i = 0; i < MAX_USERS; i++)
udev_poll_pad(&udev_pads[i], i);
}
static bool udev_joypad_init(void *data)
2013-09-25 19:32:18 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
struct udev_list_entry *devs = NULL;
struct udev_list_entry *item = NULL;
struct udev_enumerate *enumerate = NULL;
2015-03-20 22:41:15 +01:00
settings_t *settings = config_get_ptr();
(void)data;
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
udev_pads[i].fd = -1;
2013-09-25 21:41:26 +02:00
2015-12-17 03:02:48 +01:00
g_udev = udev_new();
if (!g_udev)
return false;
2013-09-25 19:32:18 +02:00
2015-12-17 03:02:48 +01:00
g_udev_mon = udev_monitor_new_from_netlink(g_udev, "udev");
if (g_udev_mon)
{
udev_monitor_filter_add_match_subsystem_devtype(g_udev_mon, "input", NULL);
udev_monitor_enable_receiving(g_udev_mon);
}
enumerate = udev_enumerate_new(g_udev);
2013-09-25 19:32:18 +02:00
if (!enumerate)
goto error;
udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
udev_enumerate_scan_devices(enumerate);
devs = udev_enumerate_get_list_entry(enumerate);
2013-10-22 21:26:33 +02:00
for (item = devs; item; item = udev_list_entry_get_next(item))
2013-09-25 19:32:18 +02:00
{
2015-06-03 18:31:06 +02:00
const char *name = udev_list_entry_get_name(item);
2015-12-17 03:02:48 +01:00
struct udev_device *dev = udev_device_new_from_syspath(g_udev, name);
2015-06-03 18:31:06 +02:00
const char *devnode = udev_device_get_devnode(dev);
2013-09-26 00:49:13 +02:00
if (devnode)
2015-04-03 01:37:20 +02:00
udev_check_device(dev, devnode, false);
2013-09-25 19:32:18 +02:00
udev_device_unref(dev);
}
udev_enumerate_unref(enumerate);
return true;
error:
if (enumerate)
udev_enumerate_unref(enumerate);
udev_joypad_destroy();
return false;
}
2013-09-25 21:41:26 +02:00
static bool udev_joypad_hat(const struct udev_joypad *pad, uint16_t hat)
{
unsigned h = GET_HAT(hat);
2013-09-25 21:41:26 +02:00
if (h >= NUM_HATS)
return false;
switch (GET_HAT_DIR(hat))
{
case HAT_LEFT_MASK:
return pad->hats[h][0] < 0;
case HAT_RIGHT_MASK:
return pad->hats[h][0] > 0;
case HAT_UP_MASK:
return pad->hats[h][1] < 0;
case HAT_DOWN_MASK:
return pad->hats[h][1] > 0;
2013-09-25 21:41:26 +02:00
}
2015-03-09 19:01:12 +01:00
return 0;
2013-09-25 21:41:26 +02:00
}
2013-09-25 19:32:18 +02:00
static bool udev_joypad_button(unsigned port, uint16_t joykey)
{
const struct udev_joypad *pad = (const struct udev_joypad*)&udev_pads[port];
2013-09-25 19:32:18 +02:00
2013-09-25 21:41:26 +02:00
if (GET_HAT_DIR(joykey))
return udev_joypad_hat(pad, joykey);
2015-02-15 01:57:29 +01:00
return joykey < UDEV_NUM_BUTTONS && BIT64_GET(pad->buttons, joykey);
}
static uint64_t udev_joypad_get_buttons(unsigned port)
{
const struct udev_joypad *pad = (const struct udev_joypad*)&udev_pads[port];
if (!pad)
return 0;
return pad->buttons;
2013-09-25 19:32:18 +02:00
}
static int16_t udev_joypad_axis(unsigned port, uint32_t joyaxis)
{
int16_t val = 0;
2015-12-17 03:02:48 +01:00
const struct udev_joypad *pad;
2013-09-25 19:32:18 +02:00
if (joyaxis == AXIS_NONE)
return 0;
2015-12-17 03:02:48 +01:00
pad = (const struct udev_joypad*)&udev_pads[port];
2013-09-25 19:32:18 +02:00
if (AXIS_NEG_GET(joyaxis) < NUM_AXES)
{
val = pad->axes[AXIS_NEG_GET(joyaxis)];
if (val > 0)
val = 0;
}
else if (AXIS_POS_GET(joyaxis) < NUM_AXES)
{
val = pad->axes[AXIS_POS_GET(joyaxis)];
if (val < 0)
val = 0;
}
return val;
}
static bool udev_joypad_query_pad(unsigned pad)
{
2015-01-05 01:58:00 +01:00
return pad < MAX_USERS && udev_pads[pad].fd >= 0;
2013-09-25 19:32:18 +02:00
}
static const char *udev_joypad_name(unsigned pad)
{
2015-01-05 01:58:00 +01:00
if (pad >= MAX_USERS)
2013-09-25 19:32:18 +02:00
return NULL;
return *udev_pads[pad].ident ? udev_pads[pad].ident : NULL;
2013-09-25 19:32:18 +02:00
}
input_device_driver_t udev_joypad = {
2013-09-25 19:32:18 +02:00
udev_joypad_init,
udev_joypad_query_pad,
udev_joypad_destroy,
udev_joypad_button,
2015-02-15 01:57:29 +01:00
udev_joypad_get_buttons,
2013-09-25 19:32:18 +02:00
udev_joypad_axis,
udev_joypad_poll,
udev_set_rumble,
2013-09-25 19:32:18 +02:00
udev_joypad_name,
"udev",
};