RetroArch/input/drivers_joypad/linuxraw_joypad.c

364 lines
9.2 KiB
C
Raw Normal View History

2012-09-29 00:26:21 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 04:33:01 +01:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2012-09-29 00:26:21 +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>
2016-09-01 05:51:08 +02:00
#include <stdlib.h>
2015-09-05 20:49:57 +02:00
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
2015-09-05 20:49:57 +02:00
2012-09-29 00:26:21 +02:00
#include <sys/types.h>
#include <sys/inotify.h>
2012-09-29 00:26:21 +02:00
#include <linux/joystick.h>
2016-09-06 00:56:00 +02:00
#include <compat/strl.h>
2015-11-30 18:05:10 +01:00
#include "../common/epoll_common.h"
2015-09-05 20:49:57 +02:00
#include "../input_autodetect.h"
2016-09-01 05:51:08 +02:00
#include "../input_driver.h"
#include "../../configuration.h"
#include "../../runloop.h"
2015-11-23 12:03:38 +01:00
#include "../../verbosity.h"
2015-09-05 20:49:57 +02:00
2012-09-29 00:26:21 +02:00
#define NUM_BUTTONS 32
#define NUM_AXES 32
struct linuxraw_joypad
{
int fd;
2015-02-15 01:57:29 +01:00
uint64_t buttons;
2012-09-29 00:26:21 +02:00
int16_t axes[NUM_AXES];
2013-04-26 00:22:12 +02:00
char *ident;
2012-09-29 00:26:21 +02:00
};
2015-01-05 01:58:00 +01:00
static struct linuxraw_joypad linuxraw_pads[MAX_USERS];
2015-11-30 18:20:50 +01:00
static int g_inotify;
static bool g_hotplug;
2015-04-03 01:19:51 +02:00
static void linuxraw_poll_pad(struct linuxraw_joypad *pad)
{
struct js_event event;
while (read(pad->fd, &event, sizeof(event)) == (ssize_t)sizeof(event))
{
unsigned type = event.type & ~JS_EVENT_INIT;
switch (type)
{
case JS_EVENT_BUTTON:
if (event.number < NUM_BUTTONS)
{
if (event.value)
2015-02-15 01:57:29 +01:00
BIT64_SET(pad->buttons, event.number);
else
2015-02-15 01:57:29 +01:00
BIT64_CLEAR(pad->buttons, event.number);
}
break;
case JS_EVENT_AXIS:
if (event.number < NUM_AXES)
pad->axes[event.number] = event.value;
break;
}
}
}
static bool linuxraw_joypad_init_pad(const char *path, struct linuxraw_joypad *pad)
{
if (pad->fd >= 0)
return false;
2015-01-20 20:32:47 -03:00
/* Device can have just been created, but not made accessible (yet).
IN_ATTRIB will signal when permissions change. */
if (access(path, R_OK) < 0)
return false;
pad->fd = open(path, O_RDONLY | O_NONBLOCK);
*pad->ident = '\0';
if (pad->fd >= 0)
{
settings_t *settings = config_get_ptr();
2016-09-25 15:11:37 +02:00
if (ioctl(pad->fd,
JSIOCGNAME(sizeof(settings->input.device_names[0])), pad->ident) >= 0)
{
RARCH_LOG("[Device]: Found pad: %s on %s.\n", pad->ident, path);
if (g_hotplug)
{
2016-10-22 04:52:46 +02:00
char msg[512];
2015-06-13 00:02:28 +02:00
2016-10-22 04:52:46 +02:00
msg[0] = '\0';
snprintf(msg, sizeof(msg), "Device connected. #%u (%s).",
2016-09-25 15:11:37 +02:00
(unsigned)(pad - linuxraw_pads), pad->ident);
2015-12-07 15:32:14 +01:00
runloop_msg_queue_push(msg, 0, 60, false);
2013-04-27 00:14:01 +02:00
}
}
else
RARCH_ERR("[Device]: Didn't find ident of %s.\n", path);
2015-11-30 18:14:07 +01:00
if (!epoll_add(pad->fd, pad))
goto error;
return true;
}
2014-08-27 03:28:22 +02:00
2015-11-30 18:14:07 +01:00
error:
RARCH_ERR("[Device]: Failed to open pad %s (error: %s).\n", path, strerror(errno));
2014-08-27 03:28:22 +02:00
return false;
}
static void handle_plugged_pad(void)
{
2013-10-22 21:26:33 +02:00
int i, rc;
2015-04-03 01:19:51 +02:00
size_t event_size = sizeof(struct inotify_event) + NAME_MAX + 1;
uint8_t *event_buf = (uint8_t*)calloc(1, event_size);
if (!event_buf)
return;
2015-11-30 18:20:50 +01:00
while ((rc = read(g_inotify, event_buf, event_size)) >= 0)
{
2015-09-28 16:20:26 +02:00
struct inotify_event *event = (struct inotify_event*)&event_buf[0];
/* Can read multiple events in one read() call. */
2013-10-22 21:26:33 +02:00
for (i = 0; i < rc; i += event->len + sizeof(struct inotify_event))
{
unsigned idx;
2015-03-27 16:57:58 +01:00
autoconfig_params_t params = {{0}};
event = (struct inotify_event*)&event_buf[i];
if (strstr(event->name, "js") != event->name)
continue;
idx = strtoul(event->name + 2, NULL, 0);
2015-01-05 01:58:00 +01:00
if (idx >= MAX_USERS)
continue;
if (event->mask & IN_DELETE)
{
if (linuxraw_pads[idx].fd >= 0)
{
if (g_hotplug)
input_config_autoconfigure_disconnect(idx, linuxraw_pads[idx].ident);
close(linuxraw_pads[idx].fd);
linuxraw_pads[idx].buttons = 0;
memset(linuxraw_pads[idx].axes, 0, sizeof(linuxraw_pads[idx].axes));
linuxraw_pads[idx].fd = -1;
*linuxraw_pads[idx].ident = '\0';
2013-04-26 14:36:36 +02:00
/* TODO - implement VID/PID? */
2015-03-27 16:57:58 +01:00
params.idx = idx;
input_config_autoconfigure_joypad(&params);
}
}
2015-11-30 18:24:35 +01:00
/* Sometimes, device will be created before access to it is established. */
else if (event->mask & (IN_CREATE | IN_ATTRIB))
{
bool ret;
char path[PATH_MAX_LENGTH];
path[0] = '\0';
snprintf(path, sizeof(path), "/dev/input/%s", event->name);
ret = linuxraw_joypad_init_pad(path, &linuxraw_pads[idx]);
2013-04-26 14:36:36 +02:00
if (*linuxraw_pads[idx].ident && ret)
2015-03-27 16:57:58 +01:00
{
params.idx = idx;
strlcpy(params.name, linuxraw_pads[idx].ident, sizeof(params.name));
strlcpy(params.driver, linuxraw_joypad.ident, sizeof(params.driver));
/* TODO - implement VID/PID? */
2015-03-27 16:57:58 +01:00
input_config_autoconfigure_joypad(&params);
}
}
}
}
free(event_buf);
}
static void linuxraw_joypad_poll(void)
{
2013-10-22 21:26:33 +02:00
int i, ret;
2015-01-05 01:58:00 +01:00
struct epoll_event events[MAX_USERS + 1];
retry:
2015-11-30 18:08:00 +01:00
ret = epoll_waiting(events, MAX_USERS + 1, 0);
if (ret < 0 && errno == EINTR)
goto retry;
2013-10-22 21:26:33 +02:00
for (i = 0; i < ret; i++)
{
2016-09-25 15:11:37 +02:00
struct linuxraw_joypad *ptr = (struct linuxraw_joypad*)
events[i].data.ptr;
if (ptr)
linuxraw_poll_pad(ptr);
else
handle_plugged_pad();
}
}
static bool linuxraw_joypad_init(void *data)
2012-09-29 00:26:21 +02:00
{
2013-10-22 21:26:33 +02:00
unsigned i;
2015-11-30 18:05:10 +01:00
if (!epoll_new(true))
return false;
2012-09-29 00:26:21 +02:00
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
2012-09-29 00:26:21 +02:00
{
char path[PATH_MAX_LENGTH];
2015-03-27 16:57:58 +01:00
autoconfig_params_t params = {{0}};
struct linuxraw_joypad *pad = (struct linuxraw_joypad*)&linuxraw_pads[i];
2016-09-25 15:11:37 +02:00
settings_t *settings = config_get_ptr();
path[0] = '\0';
2016-09-25 15:11:37 +02:00
params.idx = i;
pad->fd = -1;
pad->ident = settings->input.device_names[i];
2012-09-29 00:26:21 +02:00
snprintf(path, sizeof(path), "/dev/input/js%u", i);
2013-04-26 15:08:18 +02:00
if (linuxraw_joypad_init_pad(path, pad))
{
2015-03-27 16:57:58 +01:00
strlcpy(params.name, pad->ident, sizeof(params.name));
strlcpy(params.driver, "linuxraw", sizeof(params.driver));
/* TODO - implement VID/PID? */
input_config_autoconfigure_joypad(&params);
2015-04-03 01:19:51 +02:00
linuxraw_poll_pad(pad);
2013-04-26 15:08:18 +02:00
}
else
2015-03-27 16:57:58 +01:00
input_config_autoconfigure_joypad(&params);
2012-09-29 00:26:21 +02:00
}
2015-11-30 18:20:50 +01:00
g_inotify = inotify_init();
2015-11-30 18:14:07 +01:00
2015-11-30 18:20:50 +01:00
if (g_inotify >= 0)
{
2015-11-30 18:20:50 +01:00
fcntl(g_inotify, F_SETFL, fcntl(g_inotify, F_GETFL) | O_NONBLOCK);
inotify_add_watch(g_inotify, "/dev/input", IN_DELETE | IN_CREATE | IN_ATTRIB);
epoll_add(g_inotify, NULL);
}
g_hotplug = true;
return true;
2012-09-29 00:26:21 +02:00
}
static void linuxraw_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++)
2012-09-29 00:26:21 +02:00
{
if (linuxraw_pads[i].fd >= 0)
close(linuxraw_pads[i].fd);
2012-09-29 00:26:21 +02:00
}
memset(linuxraw_pads, 0, sizeof(linuxraw_pads));
2015-01-05 01:58:00 +01:00
for (i = 0; i < MAX_USERS; i++)
linuxraw_pads[i].fd = -1;
2015-11-30 18:20:50 +01:00
if (g_inotify >= 0)
close(g_inotify);
g_inotify = -1;
2015-11-30 18:05:10 +01:00
epoll_free(true);
g_hotplug = false;
2012-09-29 00:26:21 +02:00
}
static bool linuxraw_joypad_button(unsigned port, uint16_t joykey)
{
2016-09-25 15:11:37 +02:00
const struct linuxraw_joypad *pad = (const struct linuxraw_joypad*)
&linuxraw_pads[port];
2015-02-15 01:57:29 +01:00
return joykey < NUM_BUTTONS && BIT64_GET(pad->buttons, joykey);
}
static uint64_t linuxraw_joypad_get_buttons(unsigned port)
{
2016-09-25 15:11:37 +02:00
const struct linuxraw_joypad *pad = (const struct linuxraw_joypad*)
&linuxraw_pads[port];
2015-02-15 01:57:29 +01:00
return pad->buttons;
2012-09-29 00:26:21 +02:00
}
static int16_t linuxraw_joypad_axis(unsigned port, uint32_t joyaxis)
{
int16_t val = 0;
const struct linuxraw_joypad *pad = NULL;
2012-09-29 00:26:21 +02:00
if (joyaxis == AXIS_NONE)
return 0;
pad = (const struct linuxraw_joypad*)&linuxraw_pads[port];
2012-09-29 00:26:21 +02:00
if (AXIS_NEG_GET(joyaxis) < NUM_AXES)
{
val = pad->axes[AXIS_NEG_GET(joyaxis)];
if (val > 0)
val = 0;
/* Kernel returns values in range [-0x7fff, 0x7fff]. */
2012-09-29 00:26:21 +02:00
}
else if (AXIS_POS_GET(joyaxis) < NUM_AXES)
{
val = pad->axes[AXIS_POS_GET(joyaxis)];
if (val < 0)
val = 0;
}
return val;
}
static bool linuxraw_joypad_query_pad(unsigned pad)
{
2015-01-05 01:58:00 +01:00
return pad < MAX_USERS && linuxraw_pads[pad].fd >= 0;
}
static const char *linuxraw_joypad_name(unsigned pad)
{
2015-01-05 01:58:00 +01:00
if (pad >= MAX_USERS)
return NULL;
return *linuxraw_pads[pad].ident ? linuxraw_pads[pad].ident : NULL;
}
input_device_driver_t linuxraw_joypad = {
2012-09-29 00:26:21 +02:00
linuxraw_joypad_init,
linuxraw_joypad_query_pad,
2012-09-29 00:26:21 +02:00
linuxraw_joypad_destroy,
linuxraw_joypad_button,
2015-02-15 01:57:29 +01:00
linuxraw_joypad_get_buttons,
2012-09-29 00:26:21 +02:00
linuxraw_joypad_axis,
linuxraw_joypad_poll,
NULL,
linuxraw_joypad_name,
2012-09-29 00:26:21 +02:00
"linuxraw",
};