diff --git a/Makefile.common b/Makefile.common index dd45a2a332..85174c4f9e 100644 --- a/Makefile.common +++ b/Makefile.common @@ -78,6 +78,7 @@ ifneq ($(findstring Linux,$(OS)),) LIBS += -lrt OBJ += input/drivers/linuxraw_input.o \ input/common/linux_common.o \ + input/common/epoll_common.o \ input/drivers_joypad/linuxraw_joypad.o \ frontend/drivers/platform_linux.o endif diff --git a/griffin/griffin.c b/griffin/griffin.c index baa902c4f4..b230d4128f 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -375,6 +375,7 @@ INPUT #if defined(__linux__) && !defined(ANDROID) #include "../input/common/linux_common.c" +#include "../input/common/epoll_common.c" #include "../input/drivers/linuxraw_input.c" #include "../input/drivers_joypad/linuxraw_joypad.c" #endif diff --git a/input/common/epoll_common.c b/input/common/epoll_common.c new file mode 100644 index 0000000000..3362a5085e --- /dev/null +++ b/input/common/epoll_common.c @@ -0,0 +1,35 @@ +#include + +#include "epoll_common.h" + +int g_epoll; +static bool epoll_inited; +static bool epoll_first_inited_is_joypad; + +bool epoll_new(bool is_joypad) +{ + if (epoll_inited) + return true; + + g_epoll = epoll_create(32); + if (g_epoll < 0) + return false; + + epoll_first_inited_is_joypad = is_joypad; + epoll_inited = true; + + return true; +} + +void epoll_free(bool is_joypad) +{ + if (!epoll_inited || (is_joypad && !epoll_first_inited_is_joypad)) + return; + + if (g_epoll >= 0) + close(g_epoll); + g_epoll = -1; + + epoll_inited = false; + epoll_first_inited_is_joypad = false; +} diff --git a/input/common/epoll_common.h b/input/common/epoll_common.h new file mode 100644 index 0000000000..22a6db6a86 --- /dev/null +++ b/input/common/epoll_common.h @@ -0,0 +1,32 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2011-2015 - Daniel De Matteis + * + * 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 . + */ + +#ifndef _EPOLL_COMMON_H +#define _EPOLL_COMMON_H + +#include + +#include +#include + +#include + +extern int g_epoll; + +bool epoll_new(bool is_joypad); + +void epoll_free(bool is_joypad); + +#endif diff --git a/input/drivers/udev_input.c b/input/drivers/udev_input.c index 16ab9f6dc3..4ecb077a64 100644 --- a/input/drivers/udev_input.c +++ b/input/drivers/udev_input.c @@ -24,8 +24,6 @@ #include #include -#include - #include #include #include @@ -35,6 +33,7 @@ #include "../drivers_keyboard/keyboard_event_udev.h" #include "../common/linux_common.h" +#include "../common/epoll_common.h" #include "../common/udev_common.h" #include "../input_config.h" @@ -81,7 +80,6 @@ struct udev_input bool blocked; const input_device_driver_t *joypad; - int epfd; udev_input_device_t **devices; unsigned num_devices; @@ -264,7 +262,7 @@ static bool add_device(udev_input_t *udev, event.data.ptr = device; /* Shouldn't happen, but just check it. */ - if (epoll_ctl(udev->epfd, EPOLL_CTL_ADD, fd, &event) < 0) + 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)); @@ -367,7 +365,7 @@ static void udev_input_poll(void *data) while (udev_mon_hotplug_available()) udev_input_handle_hotplug(udev); - ret = epoll_wait(udev->epfd, events, ARRAY_SIZE(events), 0); + ret = epoll_wait(g_epoll, events, ARRAY_SIZE(events), 0); for (i = 0; i < ret; i++) { @@ -559,8 +557,7 @@ static void udev_input_free(void *data) if (udev->joypad) udev->joypad->destroy(); - if (udev->epfd >= 0) - close(udev->epfd); + epoll_free(false); for (i = 0; i < udev->num_devices; i++) { @@ -629,8 +626,7 @@ static void *udev_input_init(void) goto error; #endif - udev->epfd = epoll_create(32); - if (udev->epfd < 0) + if (!epoll_new(false)) { RARCH_ERR("Failed to create epoll FD.\n"); goto error; diff --git a/input/drivers_joypad/linuxraw_joypad.c b/input/drivers_joypad/linuxraw_joypad.c index 20ecd4d802..d21447e12c 100644 --- a/input/drivers_joypad/linuxraw_joypad.c +++ b/input/drivers_joypad/linuxraw_joypad.c @@ -21,10 +21,9 @@ #include #include -#include -#include #include +#include "../common/epoll_common.h" #include "../input_autodetect.h" #include "../../general.h" #include "../../verbosity.h" @@ -43,7 +42,6 @@ struct linuxraw_joypad static struct linuxraw_joypad linuxraw_pads[MAX_USERS]; static int g_notify; -static int g_epoll; static bool g_hotplug; static void linuxraw_poll_pad(struct linuxraw_joypad *pad) @@ -220,8 +218,7 @@ static bool linuxraw_joypad_init(void *data) unsigned i; settings_t *settings = config_get_ptr(); - g_epoll = epoll_create(MAX_USERS + 1); - if (g_epoll < 0) + if (!epoll_new(true)) return false; (void)data; @@ -291,9 +288,7 @@ static void linuxraw_joypad_destroy(void) close(g_notify); g_notify = -1; - if (g_epoll >= 0) - close(g_epoll); - g_epoll = -1; + epoll_free(true); g_hotplug = false; }