diff --git a/input/common/epoll_common.c b/input/common/epoll_common.c index 3aa4792383..34b00d101d 100644 --- a/input/common/epoll_common.c +++ b/input/common/epoll_common.c @@ -1,9 +1,13 @@ #include +#include +#include #include #include "epoll_common.h" -int g_epoll; +#include "../../verbosity.h" + +static int g_epoll; static bool epoll_inited; static bool epoll_first_inited_is_joypad; @@ -39,3 +43,21 @@ int epoll_waiting(struct epoll_event *events, int maxevents, int timeout) { return epoll_wait(g_epoll, events, maxevents, timeout); } + +bool epoll_add(int fd, void *device) +{ + struct epoll_event event = {0}; + + event.events = EPOLLIN; + event.data.ptr = device; + + /* Shouldn't happen, but just check it. */ + if (epoll_ctl(g_epoll, EPOLL_CTL_ADD, fd, &event) < 0) + { + RARCH_ERR("Failed to add FD (%d) to epoll list (%s).\n", + fd, strerror(errno)); + return false; + } + + return true; +} diff --git a/input/common/epoll_common.h b/input/common/epoll_common.h index 00a6376e84..a26296f4bc 100644 --- a/input/common/epoll_common.h +++ b/input/common/epoll_common.h @@ -23,12 +23,12 @@ #include -extern int g_epoll; - bool epoll_new(bool is_joypad); void epoll_free(bool is_joypad); int epoll_waiting(struct epoll_event *events, int maxevents, int timeout); +bool epoll_add(int fd, void *device); + #endif diff --git a/input/drivers/udev_input.c b/input/drivers/udev_input.c index 52ceebd53d..9ad9f2a28a 100644 --- a/input/drivers/udev_input.c +++ b/input/drivers/udev_input.c @@ -224,7 +224,6 @@ static bool add_device(udev_input_t *udev, udev_input_device_t **tmp; udev_input_device_t *device = NULL; struct stat st = {0}; - struct epoll_event event = {0}; if (stat(devnode, &st) < 0) return false; @@ -258,13 +257,7 @@ static bool add_device(udev_input_t *udev, tmp[udev->num_devices++] = device; udev->devices = tmp; - event.events = EPOLLIN; - event.data.ptr = device; - - /* Shouldn't happen, but just check it. */ - if (epoll_ctl(g_epoll, EPOLL_CTL_ADD, fd, &event) < 0) - RARCH_ERR("Failed to add FD (%d) to epoll list (%s).\n", - fd, strerror(errno)); + epoll_add(fd, device); return true; diff --git a/input/drivers_joypad/linuxraw_joypad.c b/input/drivers_joypad/linuxraw_joypad.c index ba334d8ff9..51e456a590 100644 --- a/input/drivers_joypad/linuxraw_joypad.c +++ b/input/drivers_joypad/linuxraw_joypad.c @@ -89,8 +89,6 @@ static bool linuxraw_joypad_init_pad(const char *path, struct linuxraw_joypad *p *pad->ident = '\0'; if (pad->fd >= 0) { - struct epoll_event event; - 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); @@ -103,16 +101,16 @@ static bool linuxraw_joypad_init_pad(const char *path, struct linuxraw_joypad *p rarch_main_msg_queue_push(msg, 0, 60, false); } } - else RARCH_ERR("[Device]: Didn't find ident of %s.\n", path); - event.events = EPOLLIN; - event.data.ptr = pad; - epoll_ctl(g_epoll, EPOLL_CTL_ADD, pad->fd, &event); + if (!epoll_add(pad->fd, pad)) + goto error; + return true; } +error: RARCH_ERR("[Device]: Failed to open pad %s (error: %s).\n", path, strerror(errno)); return false; } @@ -253,15 +251,11 @@ static bool linuxraw_joypad_init(void *data) } g_notify = inotify_init(); + if (g_notify >= 0) { - struct epoll_event event; - linuxraw_joypad_setup_notify(); - - event.events = EPOLLIN; - event.data.ptr = NULL; - epoll_ctl(g_epoll, EPOLL_CTL_ADD, g_notify, &event); + epoll_add(g_notify, NULL); } g_hotplug = true;