--- a/src/drivers/joy_linux.c Mon May 12 10:00:13 2008 +0000 +++ b/src/drivers/joy_linux.c Thu Jan 15 04:15:11 2009 +0000 @@ -51,7 +51,7 @@ } *linux_joystick_t; static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition, - gpointer data ); + gpointer data ); static int linux_joystick_scan(); static linux_joystick_t linux_joystick_new( const gchar *filename, int fd ); static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str ); @@ -70,25 +70,25 @@ { linux_joystick_t joy = (linux_joystick_t)dev; if( strncasecmp( str, "Button", 6 ) == 0 ){ - unsigned long button = strtoul( str+6, NULL, 10 ); - if( button > joy->button_count ) { - return 0; - } - return (uint16_t)button; + unsigned long button = strtoul( str+6, NULL, 10 ); + if( button > joy->button_count ) { + return 0; + } + return (uint16_t)button; } else if( strncasecmp( str, "Axis", 4 ) == 0 ) { - char *endptr; - unsigned long axis = strtoul( str+4, &endptr, 10 ); - if( axis > joy->axis_count || axis == 0 ) { - return 0; - } - int keycode = ((axis - 1) << 1) + joy->button_count + 1; - if( *endptr == '-' ) { - return keycode + 1; - } else { - return keycode; - } + char *endptr; + unsigned long axis = strtoul( str+4, &endptr, 10 ); + if( axis > joy->axis_count || axis == 0 ) { + return 0; + } + int keycode = ((axis - 1) << 1) + joy->button_count + 1; + if( *endptr == '-' ) { + return keycode + 1; + } else { + return keycode; + } } else { - return 0; + return 0; } } @@ -96,18 +96,18 @@ { linux_joystick_t joy = (linux_joystick_t)dev; if( keycode == 0 ) { - return NULL; + return NULL; } if( keycode <= joy->button_count ) { - return g_strdup_printf( "Button%d", keycode ); + return g_strdup_printf( "Button%d", keycode ); } if( keycode <= joy->button_count + joy->axis_count*2 ) { - int axis = keycode - joy->button_count - 1; - if( (axis & 1) == 0 ) { - return g_strdup_printf( "Axis%d+", (axis >> 1)+1 ); - } else { - return g_strdup_printf( "Axis%d-", (axis >> 1)+1 ); - } + int axis = keycode - joy->button_count - 1; + if( (axis & 1) == 0 ) { + return g_strdup_printf( "Axis%d+", (axis >> 1)+1 ); + } else { + return g_strdup_printf( "Axis%d-", (axis >> 1)+1 ); + } } return NULL; } @@ -129,39 +129,39 @@ * On error, close the channel and delete the device. */ static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition, - gpointer data ) + gpointer data ) { linux_joystick_t joy = (linux_joystick_t)data; if( condition & G_IO_HUP ) { - INFO( "Joystick '%s' disconnected\n", joy->name ); - input_unregister_device((input_driver_t)joy); - return FALSE; + INFO( "Joystick '%s' disconnected\n", joy->name ); + input_unregister_device((input_driver_t)joy); + return FALSE; } if( condition & G_IO_IN ) { - struct js_event event; - while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) { - if( event.type == JS_EVENT_BUTTON ) { - int keycode = event.number+1; - if( event.value == 0 ) { - input_event_keyup( (input_driver_t)joy, keycode, 0 ); - } else { - input_event_keydown( (input_driver_t)joy, keycode, event.value ); - } - } else if( event.type == JS_EVENT_AXIS ) { - int keycode = (event.number*2) + joy->button_count + 1; - if( event.value == 0 ) { - input_event_keyup( (input_driver_t)joy, keycode, 0 ); - input_event_keyup( (input_driver_t)joy, keycode+1, 0 ); - } else if( event.value < 0 ) { - input_event_keydown( (input_driver_t)joy, keycode+1, -event.value ); - input_event_keyup( (input_driver_t)joy, keycode, 0 ); - } else { - input_event_keydown( (input_driver_t)joy, keycode, event.value ); - input_event_keyup( (input_driver_t)joy, keycode+1, 0 ); - } - } - } + struct js_event event; + while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) { + if( event.type == JS_EVENT_BUTTON ) { + int keycode = event.number+1; + if( event.value == 0 ) { + input_event_keyup( (input_driver_t)joy, keycode, 0 ); + } else { + input_event_keydown( (input_driver_t)joy, keycode, event.value ); + } + } else if( event.type == JS_EVENT_AXIS ) { + int keycode = (event.number*2) + joy->button_count + 1; + if( event.value == 0 ) { + input_event_keyup( (input_driver_t)joy, keycode, 0 ); + input_event_keyup( (input_driver_t)joy, keycode+1, 0 ); + } else if( event.value < 0 ) { + input_event_keydown( (input_driver_t)joy, keycode+1, -event.value ); + input_event_keyup( (input_driver_t)joy, keycode, 0 ); + } else { + input_event_keydown( (input_driver_t)joy, keycode, event.value ); + input_event_keyup( (input_driver_t)joy, keycode+1, 0 ); + } + } + } } return TRUE; } @@ -183,17 +183,17 @@ char *p = strrchr(filename, '/'); if( p == NULL ) { - joy->driver.id = filename; + joy->driver.id = filename; } else { - joy->driver.id = p+1; + joy->driver.id = p+1; } if( ioctl( fd, JSIOCGNAME(128), joy->name ) == -1 || - ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 || - ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) { - ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) ); - g_free(joy); - return NULL; + ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 || + ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) { + ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) ); + g_free(joy); + return NULL; } joy->channel = g_io_channel_unix_new(fd); @@ -208,24 +208,24 @@ DIR *dir = opendir(INPUT_PATH); if( dir == NULL ) { - return 0; + return 0; } - + while( (ent = readdir(dir)) != NULL ) { - if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' && - isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) { - gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name ); - int fd = open(name, O_RDONLY|O_NONBLOCK); - if( fd == -1 ) { - g_free( name ); - } else { - linux_joystick_t joy = linux_joystick_new( name, fd ); - input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count ); - INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)", - joy->driver.id, joy->name, joy->button_count, joy->axis_count ); - joysticks++; - } - } + if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' && + isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) { + gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name ); + int fd = open(name, O_RDONLY|O_NONBLOCK); + if( fd == -1 ) { + g_free( name ); + } else { + linux_joystick_t joy = linux_joystick_new( name, fd ); + input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count ); + INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)", + joy->driver.id, joy->name, joy->button_count, joy->axis_count ); + joysticks++; + } + } } closedir(dir); @@ -235,7 +235,7 @@ gboolean linux_joystick_init() { if( !linux_joystick_install_watch(INPUT_PATH) ) { - return FALSE; + return FALSE; } linux_joystick_scan(); return TRUE; @@ -254,11 +254,11 @@ static gboolean gtk_loop_check_input(gpointer data) { if( need_input_rescan == 1 ) { - need_input_rescan = 0; - int js = linux_joystick_scan(); - if( js > 0 ) { - maple_reattach_all(); - } + need_input_rescan = 0; + int js = linux_joystick_scan(); + if( js > 0 ) { + maple_reattach_all(); + } } return need_input_rescan != 2; } @@ -272,14 +272,14 @@ { int fd = open( dir, O_RDONLY|O_NONBLOCK ); if( fd == -1 ) { - return FALSE; + return FALSE; } - + signal( SIGRTMIN+1, dnotify_handler ); fcntl(fd, F_SETSIG, SIGRTMIN + 1); if( fcntl(fd, F_NOTIFY, DN_CREATE|DN_MULTISHOT) == -1 ) { - close(fd); - return FALSE; + close(fd); + return FALSE; } watch_dir_fd = fd; g_timeout_add( 500, gtk_loop_check_input, NULL );