2012-09-28 22:26:21 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2013-01-01 00:37:37 +00:00
|
|
|
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
|
2012-09-28 22:26:21 +00: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 "input_common.h"
|
|
|
|
#include "../general.h"
|
|
|
|
#include <unistd.h>
|
2013-04-25 21:28:26 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2012-09-28 22:26:21 +00:00
|
|
|
#include <sys/types.h>
|
2013-04-25 21:28:26 +00:00
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/epoll.h>
|
2012-09-28 22:26:21 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/joystick.h>
|
|
|
|
|
|
|
|
#define NUM_BUTTONS 32
|
|
|
|
#define NUM_AXES 32
|
|
|
|
|
|
|
|
struct linuxraw_joypad
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
bool buttons[NUM_BUTTONS];
|
|
|
|
int16_t axes[NUM_AXES];
|
2013-04-25 21:28:26 +00:00
|
|
|
|
2013-04-25 22:22:12 +00:00
|
|
|
char *ident;
|
2012-09-28 22:26:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct linuxraw_joypad g_pads[MAX_PLAYERS];
|
2013-04-25 21:28:26 +00:00
|
|
|
static int g_notify;
|
|
|
|
static int g_epoll;
|
2013-04-25 22:43:14 +00:00
|
|
|
static bool g_hotplug;
|
|
|
|
|
2012-09-29 07:57:28 +00:00
|
|
|
static void 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)
|
|
|
|
pad->buttons[event.number] = event.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JS_EVENT_AXIS:
|
|
|
|
if (event.number < NUM_AXES)
|
|
|
|
pad->axes[event.number] = event.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 13:02:18 +00:00
|
|
|
static bool linuxraw_joypad_init_pad(const char *path, struct linuxraw_joypad *pad)
|
2012-09-29 07:57:28 +00:00
|
|
|
{
|
2013-04-25 21:28:26 +00:00
|
|
|
if (pad->fd >= 0)
|
2013-04-26 13:02:18 +00:00
|
|
|
return false;
|
2013-04-25 21:28:26 +00: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)
|
2013-04-26 13:02:18 +00:00
|
|
|
return false;
|
2013-04-25 21:28:26 +00:00
|
|
|
|
|
|
|
pad->fd = open(path, O_RDONLY | O_NONBLOCK);
|
|
|
|
|
|
|
|
*pad->ident = '\0';
|
|
|
|
if (pad->fd >= 0)
|
2012-09-29 07:57:28 +00:00
|
|
|
{
|
2013-04-25 22:22:12 +00:00
|
|
|
if (ioctl(pad->fd, JSIOCGNAME(sizeof(g_settings.input.device_names[0])), pad->ident) >= 0)
|
2013-04-25 22:43:14 +00:00
|
|
|
{
|
2013-04-25 21:28:26 +00:00
|
|
|
RARCH_LOG("[Joypad]: Found pad: %s on %s.\n", pad->ident, path);
|
2013-04-25 22:43:14 +00:00
|
|
|
|
|
|
|
if (g_hotplug)
|
|
|
|
{
|
|
|
|
char msg[512];
|
|
|
|
snprintf(msg, sizeof(msg), "Joypad #%u (%s) connected.", (unsigned)(pad - g_pads), pad->ident);
|
2013-04-26 22:14:01 +00:00
|
|
|
#ifndef IS_JOYCONFIG
|
2013-04-25 22:43:14 +00:00
|
|
|
msg_queue_push(g_extern.msg_queue, msg, 0, 60);
|
|
|
|
#endif
|
2013-04-26 22:14:01 +00:00
|
|
|
}
|
2013-04-25 22:43:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 21:28:26 +00:00
|
|
|
else
|
|
|
|
RARCH_ERR("[Joypad]: Didn't find ident of %s.\n", path);
|
|
|
|
|
|
|
|
struct epoll_event event;
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
event.data.ptr = pad;
|
|
|
|
epoll_ctl(g_epoll, EPOLL_CTL_ADD, pad->fd, &event);
|
2013-04-26 13:02:18 +00:00
|
|
|
return true;
|
2013-04-25 21:28:26 +00:00
|
|
|
}
|
|
|
|
else
|
2013-04-26 13:02:18 +00:00
|
|
|
{
|
2013-04-25 21:28:26 +00:00
|
|
|
RARCH_ERR("[Joypad]: Failed to open pad %s (error: %s).\n", path, strerror(errno));
|
2013-04-26 13:02:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-25 21:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_plugged_pad(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
while ((rc = read(g_notify, event_buf, event_size)) >= 0)
|
|
|
|
{
|
|
|
|
struct inotify_event *event = NULL;
|
|
|
|
// Can read multiple events in one read() call.
|
|
|
|
for (int i = 0; i < rc; i += event->len + sizeof(struct inotify_event))
|
|
|
|
{
|
|
|
|
event = (struct inotify_event*)&event_buf[i];
|
|
|
|
|
|
|
|
if (strstr(event->name, "js") != event->name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned index = strtoul(event->name + 2, NULL, 0);
|
|
|
|
if (index >= MAX_PLAYERS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (event->mask & IN_DELETE)
|
|
|
|
{
|
|
|
|
if (g_pads[index].fd >= 0)
|
|
|
|
{
|
2013-04-25 22:43:14 +00:00
|
|
|
if (g_hotplug)
|
|
|
|
{
|
|
|
|
char msg[512];
|
|
|
|
snprintf(msg, sizeof(msg), "Joypad #%u (%s) disconnected.", index, g_pads[index].ident);
|
2013-04-26 22:14:01 +00:00
|
|
|
#ifndef IS_JOYCONFIG
|
2013-04-25 22:43:14 +00:00
|
|
|
msg_queue_push(g_extern.msg_queue, msg, 0, 60);
|
|
|
|
#endif
|
2013-04-26 22:14:01 +00:00
|
|
|
}
|
2013-04-25 22:43:14 +00:00
|
|
|
|
2013-04-25 21:28:26 +00:00
|
|
|
RARCH_LOG("[Joypad]: Joypad %s disconnected.\n", g_pads[index].ident);
|
|
|
|
close(g_pads[index].fd);
|
2013-04-25 22:22:12 +00:00
|
|
|
memset(g_pads[index].buttons, 0, sizeof(g_pads[index].buttons));
|
|
|
|
memset(g_pads[index].axes, 0, sizeof(g_pads[index].axes));
|
2013-04-25 21:28:26 +00:00
|
|
|
g_pads[index].fd = -1;
|
2013-04-25 22:22:12 +00:00
|
|
|
*g_pads[index].ident = '\0';
|
2013-04-26 12:36:36 +00:00
|
|
|
|
|
|
|
input_config_autoconfigure_joypad(index, NULL, NULL);
|
2013-04-25 21:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sometimes, device will be created before acess to it is established.
|
|
|
|
else if (event->mask & (IN_CREATE | IN_ATTRIB))
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
snprintf(path, sizeof(path), "/dev/input/%s", event->name);
|
2013-04-26 13:02:18 +00:00
|
|
|
bool ret = linuxraw_joypad_init_pad(path, &g_pads[index]);
|
2013-04-26 12:36:36 +00:00
|
|
|
|
2013-04-26 13:02:18 +00:00
|
|
|
if (*g_pads[index].ident && ret)
|
2013-04-26 12:36:36 +00:00
|
|
|
input_config_autoconfigure_joypad(index, g_pads[index].ident, "linuxraw");
|
2013-04-25 21:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-29 07:57:28 +00:00
|
|
|
|
2013-04-25 21:28:26 +00:00
|
|
|
free(event_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linuxraw_joypad_poll(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct epoll_event events[MAX_PLAYERS + 1];
|
|
|
|
|
|
|
|
retry:
|
|
|
|
ret = epoll_wait(g_epoll, events, MAX_PLAYERS + 1, 0);
|
|
|
|
if (ret < 0 && errno == EINTR)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
for (int i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
if (events[i].data.ptr)
|
|
|
|
poll_pad((struct linuxraw_joypad*)events[i].data.ptr);
|
|
|
|
else
|
|
|
|
handle_plugged_pad();
|
2012-09-29 07:57:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 21:28:26 +00:00
|
|
|
static void linuxraw_joypad_setup_notify(void)
|
|
|
|
{
|
|
|
|
fcntl(g_notify, F_SETFL, fcntl(g_notify, F_GETFL) | O_NONBLOCK);
|
|
|
|
inotify_add_watch(g_notify, "/dev/input", IN_DELETE | IN_CREATE | IN_ATTRIB);
|
|
|
|
}
|
|
|
|
|
2012-09-28 22:26:21 +00:00
|
|
|
static bool linuxraw_joypad_init(void)
|
|
|
|
{
|
2013-04-25 21:28:26 +00:00
|
|
|
g_epoll = epoll_create(MAX_PLAYERS + 1);
|
|
|
|
if (g_epoll < 0)
|
|
|
|
return false;
|
2012-09-28 22:26:21 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < MAX_PLAYERS; i++)
|
|
|
|
{
|
|
|
|
struct linuxraw_joypad *pad = &g_pads[i];
|
2013-04-25 21:28:26 +00:00
|
|
|
pad->fd = -1;
|
2013-04-25 22:22:12 +00:00
|
|
|
pad->ident = g_settings.input.device_names[i];
|
2013-04-25 21:28:26 +00:00
|
|
|
|
2012-09-28 22:26:21 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
snprintf(path, sizeof(path), "/dev/input/js%u", i);
|
|
|
|
|
2013-04-26 13:08:18 +00:00
|
|
|
if (linuxraw_joypad_init_pad(path, pad))
|
|
|
|
{
|
|
|
|
input_config_autoconfigure_joypad(i, pad->ident, "linuxraw");
|
2013-04-25 21:28:26 +00:00
|
|
|
poll_pad(pad);
|
2013-04-26 13:08:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
input_config_autoconfigure_joypad(i, NULL, NULL);
|
2012-09-28 22:26:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 21:28:26 +00:00
|
|
|
g_notify = inotify_init();
|
|
|
|
if (g_notify >= 0)
|
|
|
|
{
|
|
|
|
linuxraw_joypad_setup_notify();
|
|
|
|
|
|
|
|
struct epoll_event event;
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
event.data.ptr = NULL;
|
|
|
|
epoll_ctl(g_epoll, EPOLL_CTL_ADD, g_notify, &event);
|
|
|
|
}
|
2012-09-29 07:57:28 +00:00
|
|
|
|
2013-04-25 22:43:14 +00:00
|
|
|
g_hotplug = true;
|
|
|
|
|
2013-04-25 21:28:26 +00:00
|
|
|
return true;
|
2012-09-28 22:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void linuxraw_joypad_destroy(void)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < MAX_PLAYERS; i++)
|
|
|
|
{
|
|
|
|
if (g_pads[i].fd >= 0)
|
|
|
|
close(g_pads[i].fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(g_pads, 0, sizeof(g_pads));
|
|
|
|
for (unsigned i = 0; i < MAX_PLAYERS; i++)
|
|
|
|
g_pads[i].fd = -1;
|
2013-04-25 21:28:26 +00:00
|
|
|
|
|
|
|
if (g_notify >= 0)
|
|
|
|
close(g_notify);
|
|
|
|
g_notify = -1;
|
|
|
|
|
|
|
|
if (g_epoll >= 0)
|
|
|
|
close(g_epoll);
|
|
|
|
g_epoll = -1;
|
2013-04-25 22:43:14 +00:00
|
|
|
|
|
|
|
g_hotplug = false;
|
2012-09-28 22:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool linuxraw_joypad_button(unsigned port, uint16_t joykey)
|
|
|
|
{
|
|
|
|
const struct linuxraw_joypad *pad = &g_pads[port];
|
|
|
|
|
|
|
|
return joykey < NUM_BUTTONS && pad->buttons[joykey];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int16_t linuxraw_joypad_axis(unsigned port, uint32_t joyaxis)
|
|
|
|
{
|
|
|
|
if (joyaxis == AXIS_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const struct linuxraw_joypad *pad = &g_pads[port];
|
|
|
|
|
|
|
|
int16_t val = 0;
|
|
|
|
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].
|
|
|
|
}
|
|
|
|
else if (AXIS_POS_GET(joyaxis) < NUM_AXES)
|
|
|
|
{
|
|
|
|
val = pad->axes[AXIS_POS_GET(joyaxis)];
|
|
|
|
if (val < 0)
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-09-29 19:57:03 +00:00
|
|
|
static bool linuxraw_joypad_query_pad(unsigned pad)
|
|
|
|
{
|
|
|
|
return pad < MAX_PLAYERS && g_pads[pad].fd >= 0;
|
|
|
|
}
|
|
|
|
|
2013-04-26 11:25:40 +00:00
|
|
|
static const char *linuxraw_joypad_name(unsigned pad)
|
|
|
|
{
|
|
|
|
if (pad >= MAX_PLAYERS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return *g_pads[pad].ident ? g_pads[pad].ident : NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-28 22:26:21 +00:00
|
|
|
const rarch_joypad_driver_t linuxraw_joypad = {
|
|
|
|
linuxraw_joypad_init,
|
2012-09-29 19:57:03 +00:00
|
|
|
linuxraw_joypad_query_pad,
|
2012-09-28 22:26:21 +00:00
|
|
|
linuxraw_joypad_destroy,
|
|
|
|
linuxraw_joypad_button,
|
|
|
|
linuxraw_joypad_axis,
|
|
|
|
linuxraw_joypad_poll,
|
2013-04-26 11:25:40 +00:00
|
|
|
linuxraw_joypad_name,
|
2012-09-28 22:26:21 +00:00
|
|
|
"linuxraw",
|
|
|
|
};
|
|
|
|
|