diff --git a/Makefile.common b/Makefile.common index 8990771f76..dd45a2a332 100644 --- a/Makefile.common +++ b/Makefile.common @@ -525,6 +525,7 @@ ifeq ($(HAVE_UDEV), 1) DEFINES += $(UDEV_CFLAGS) LIBS += $(UDEV_LIBS) OBJ += input/drivers/udev_input.o \ + input/common/udev_common.o \ input/drivers_keyboard/keyboard_event_udev.o \ input/drivers_joypad/udev_joypad.o endif diff --git a/griffin/griffin.c b/griffin/griffin.c index d1b7c8d33e..baa902c4f4 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -385,6 +385,7 @@ INPUT #ifdef HAVE_UDEV #include "../input/drivers/udev_input.c" +#include "../input/common/udev_common.c" #include "../input/drivers_keyboard/keyboard_event_udev.c" #include "../input/drivers_joypad/udev_joypad.c" #endif diff --git a/input/common/udev_common.c b/input/common/udev_common.c new file mode 100644 index 0000000000..de8bfc3882 --- /dev/null +++ b/input/common/udev_common.c @@ -0,0 +1,31 @@ +#include +#include "udev_common.h" + +struct udev_monitor *g_udev_mon; +struct udev *g_udev; + +bool udev_mon_new(void) +{ + g_udev = udev_new(); + if (!g_udev) + return false; + + 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); + } + + return true; +} + +void udev_mon_free(void) +{ + if (g_udev_mon) + udev_monitor_unref(g_udev_mon); + g_udev_mon = NULL; + if (g_udev) + udev_unref(g_udev); + g_udev = NULL; +} diff --git a/input/common/udev_common.h b/input/common/udev_common.h new file mode 100644 index 0000000000..ed641ff1b1 --- /dev/null +++ b/input/common/udev_common.h @@ -0,0 +1,31 @@ +/* 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 _UDEV_COMMON_H +#define _UDEV_COMMON_H + +#include +#include + +#include + +extern struct udev_monitor *g_udev_mon; +extern struct udev *g_udev; + +bool udev_mon_new(void); + +void udev_mon_free(void); + +#endif diff --git a/input/drivers_joypad/udev_joypad.c b/input/drivers_joypad/udev_joypad.c index 5d6b50ca39..c11a10aed7 100644 --- a/input/drivers_joypad/udev_joypad.c +++ b/input/drivers_joypad/udev_joypad.c @@ -23,13 +23,13 @@ #include #include #include -#include #include #include #include #include "../input_autodetect.h" +#include "../common/udev_common.h" #include "../../general.h" #include "../../verbosity.h" @@ -77,8 +77,6 @@ struct udev_joypad int32_t pid; }; -static struct udev *g_udev; -static struct udev_monitor *g_udev_mon; static struct udev_joypad udev_pads[MAX_USERS]; static INLINE int16_t udev_compute_axis(const struct input_absinfo *info, int value) @@ -405,12 +403,7 @@ static void udev_joypad_destroy(void) for (i = 0; i < MAX_USERS; i++) udev_free_pad(i); - if (g_udev_mon) - udev_monitor_unref(g_udev_mon); - g_udev_mon = NULL; - if (g_udev) - udev_unref(g_udev); - g_udev = NULL; + udev_mon_free(); } static void udev_joypad_handle_hotplug(void) @@ -536,16 +529,8 @@ static bool udev_joypad_init(void *data) for (i = 0; i < MAX_USERS; i++) udev_pads[i].fd = -1; - g_udev = udev_new(); - if (!g_udev) - return false; - - 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); - } + if (!udev_mon_new()) + goto error; enumerate = udev_enumerate_new(g_udev); if (!enumerate)