revision 620:d9b28f78b952
summary |
tree |
shortlog |
changelog |
graph |
changeset |
raw | bz2 | zip | gz changeset | 620:d9b28f78b952 |
parent | 619:0800a0137472 |
child | 621:225d147d2b43 |
author | nkeynes |
date | Wed Jan 30 11:31:21 2008 +0000 (16 years ago) |
Watch for new joystick devices and add them
src/drivers/joy_linux.c | view | annotate | diff | log | ||
src/drivers/video_gtk.c | view | annotate | diff | log |
1.1 --- a/src/drivers/joy_linux.c Wed Jan 30 09:38:24 2008 +00001.2 +++ b/src/drivers/joy_linux.c Wed Jan 30 11:31:21 2008 +00001.3 @@ -16,10 +16,12 @@1.4 * GNU General Public License for more details.1.5 */1.7 +#define _GNU_SOURCE1.8 #include <sys/types.h>1.9 #include <sys/ioctl.h>1.10 #include <errno.h>1.11 #include <stdio.h>1.12 +#include <signal.h>1.13 #include <string.h>1.14 #include <fcntl.h>1.15 #include <dirent.h>1.16 @@ -50,6 +52,8 @@1.17 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str );1.18 static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode );1.19 static void linux_joystick_destroy( input_driver_t joy );1.20 +static gboolean linux_joystick_install_watch( const gchar *dir );1.21 +static void linux_joystick_uninstall_watch( void );1.23 /**1.24 * Convert keysym to keycode. Keysyms are either Button%d or Axis%d[+-], with buttons1.25 @@ -125,7 +129,6 @@1.26 linux_joystick_t joy = (linux_joystick_t)data;1.28 if( condition & G_IO_HUP ) {1.29 - close(joy->fd);1.30 INFO( "Joystick '%s' disconnected\n", joy->name );1.31 input_unregister_device((input_driver_t)joy);1.32 return FALSE;1.33 @@ -195,6 +198,12 @@1.35 int linux_joystick_init()1.36 {1.37 + linux_joystick_install_watch(INPUT_PATH);1.38 + linux_joystick_scan();1.39 +}1.40 +1.41 +int linux_joystick_scan()1.42 +{1.43 int joysticks = 0;1.44 struct dirent *ent;1.45 DIR *dir = opendir(INPUT_PATH);1.46 @@ -224,3 +233,51 @@1.47 return joysticks;1.48 }1.50 +void linux_joystick_shutdown(void)1.51 +{1.52 + linux_joystick_uninstall_watch();1.53 +}1.54 +1.55 +/*************************** dnotify support **********************/1.56 +1.57 +static volatile gboolean need_input_rescan = FALSE;1.58 +static int watch_dir_fd;1.59 +1.60 +static gboolean gtk_loop_check_input(gpointer data)1.61 +{1.62 + if( need_input_rescan ) {1.63 + int js = linux_joystick_scan();1.64 + if( js > 0 ) {1.65 + maple_reattach_all();1.66 + }1.67 + }1.68 + return TRUE;1.69 +}1.70 +1.71 +static void dnotify_handler(int sig )1.72 +{1.73 + need_input_rescan = TRUE;1.74 +}1.75 +1.76 +static gboolean linux_joystick_install_watch( const gchar *dir )1.77 +{1.78 + int fd = open( dir, O_RDONLY|O_NONBLOCK );1.79 + if( fd == -1 ) {1.80 + return FALSE;1.81 + }1.82 +1.83 + signal( SIGRTMIN+1, dnotify_handler );1.84 + fcntl(fd, F_SETSIG, SIGRTMIN + 1);1.85 + if( fcntl(fd, F_NOTIFY, DN_CREATE|DN_MULTISHOT) == -1 ) {1.86 + close(fd);1.87 + return FALSE;1.88 + }1.89 + watch_dir_fd = fd;1.90 + g_timeout_add( 500, gtk_loop_check_input, NULL );1.91 +}1.92 +1.93 +static void linux_joystick_uninstall_watch(void)1.94 +{1.95 + signal( SIGRTMIN+1, SIG_IGN );1.96 + close( watch_dir_fd );1.97 +}
2.1 --- a/src/drivers/video_gtk.c Wed Jan 30 09:38:24 2008 +00002.2 +++ b/src/drivers/video_gtk.c Wed Jan 30 11:31:21 2008 +00002.3 @@ -195,6 +195,8 @@2.4 if( video_win != NULL ) {2.5 video_glx_shutdown();2.6 }2.7 -2.8 +#ifdef HAVE_LINUX_JOYSTICK2.9 + linux_joystick_shutdown();2.10 +#endif2.11 }
.