revision 615:38b69ec2f4c8
summary |
tree |
shortlog |
changelog |
graph |
changeset |
raw | bz2 | zip | gz changeset | 615:38b69ec2f4c8 |
parent | 614:a2d239d4438a |
child | 616:527ab167712b |
author | nkeynes |
date | Mon Jan 28 11:17:15 2008 +0000 (16 years ago) |
Split axes into positive and negative sides. Works better now
src/display.c | view | annotate | diff | log | ||
src/display.h | view | annotate | diff | log | ||
src/drivers/joy_linux.c | view | annotate | diff | log | ||
src/drivers/joy_linux.h | view | annotate | diff | log |
1.1 --- a/src/display.c Mon Jan 28 02:38:09 2008 +00001.2 +++ b/src/display.c Mon Jan 28 11:17:15 2008 +00001.3 @@ -113,6 +113,19 @@1.4 return TRUE;1.5 }1.7 +gboolean input_has_device( const gchar *id )1.8 +{1.9 + GList *ptr;1.10 + for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {1.11 + input_driver_entry_t entry = (input_driver_entry_t)ptr->data;1.12 + if( strcasecmp(entry->driver->id, id) == 0 ) {1.13 + return TRUE;1.14 + }1.15 + }1.16 + return FALSE;1.17 +}1.18 +1.19 +1.20 void input_unregister_device( input_driver_t driver )1.21 {1.22 GList *ptr;
2.1 --- a/src/display.h Mon Jan 28 02:38:09 2008 +00002.2 +++ b/src/display.h Mon Jan 28 11:17:15 2008 +00002.3 @@ -275,6 +275,13 @@2.4 gboolean input_register_device( input_driver_t driver, uint16_t max_keycode );2.6 /**2.7 + * Determine if the system has an input driver with the given unique ID.2.8 + * @param id driver id to check2.9 + * @return TRUE if the device exists, otherwise FALSE2.10 + */2.11 +gboolean input_has_device( const gchar *id );2.12 +2.13 +/**2.14 * Unregister an input driver.2.15 * @param driver the driver to unregister2.16 * If the driver is not in fact registered, this function has no effect.
3.1 --- a/src/drivers/joy_linux.c Mon Jan 28 02:38:09 2008 +00003.2 +++ b/src/drivers/joy_linux.c Mon Jan 28 11:17:15 2008 +00003.3 @@ -44,9 +44,6 @@3.5 } *linux_joystick_t;3.7 -static GList *linux_joysticks = NULL;3.8 -3.9 -3.10 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,3.11 gpointer data );3.12 static linux_joystick_t linux_joystick_add( const gchar *filename, int fd );3.13 @@ -55,9 +52,10 @@3.14 static void linux_joystick_destroy( input_driver_t joy );3.16 /**3.17 - * Convert keysym to keycode. Keysyms are either Button%d or Axis%d, with buttons3.18 - * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count.3.19 + * Convert keysym to keycode. Keysyms are either Button%d or Axis%d[+-], with buttons3.20 + * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count*2.3.21 * The first button is Button1. (no Button0)3.22 + * The first axis is Axis1+, then Axis1-, Axis2+ and so forth.3.23 */3.24 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str )3.25 {3.26 @@ -69,11 +67,17 @@3.27 }3.28 return (uint16_t)button;3.29 } else if( strncasecmp( str, "Axis", 4 ) == 0 ) {3.30 - unsigned long axis = strtoul( str+4, NULL, 10 );3.31 + char *endptr;3.32 + unsigned long axis = strtoul( str+4, &endptr, 10 );3.33 if( axis > joy->axis_count || axis == 0 ) {3.34 return 0;3.35 }3.36 - return (uint16_t)(axis+joy->button_count);3.37 + int keycode = ((axis - 1) << 1) + joy->button_count + 1;3.38 + if( *endptr == '-' ) {3.39 + return keycode + 1;3.40 + } else {3.41 + return keycode;3.42 + }3.43 } else {3.44 return 0;3.45 }3.46 @@ -88,8 +92,13 @@3.47 if( keycode <= joy->button_count ) {3.48 return g_strdup_printf( "Button%d", keycode );3.49 }3.50 - if( keycode <= joy->button_count + joy->axis_count ) {3.51 - return g_strdup_printf( "Axis%d", keycode - joy->button_count );3.52 + if( keycode <= joy->button_count + joy->axis_count*2 ) {3.53 + int axis = keycode - joy->button_count - 1;3.54 + if( (axis & 1) == 0 ) {3.55 + return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );3.56 + } else {3.57 + return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );3.58 + }3.59 }3.60 return NULL;3.61 }3.62 @@ -124,18 +133,25 @@3.63 if( condition & G_IO_IN ) {3.64 struct js_event event;3.65 while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {3.66 - int keycode = 0;3.67 if( event.type == JS_EVENT_BUTTON ) {3.68 - keycode = event.number+1;3.69 - } else if( event.type == JS_EVENT_AXIS ) {3.70 - keycode = event.number+1 + joy->button_count;3.71 - }3.72 - if( keycode != 0 ) {3.73 + int keycode = event.number+1;3.74 if( event.value == 0 ) {3.75 - input_event_keyup( (input_driver_t)joy, keycode, event.value );3.76 + input_event_keyup( (input_driver_t)joy, keycode, 0 );3.77 } else {3.78 input_event_keydown( (input_driver_t)joy, keycode, event.value );3.79 }3.80 + } else if( event.type == JS_EVENT_AXIS ) {3.81 + int keycode = (event.number*2) + joy->button_count + 1;3.82 + if( event.value == 0 ) {3.83 + input_event_keyup( (input_driver_t)joy, keycode, 0 );3.84 + input_event_keyup( (input_driver_t)joy, keycode+1, 0 );3.85 + } else if( event.value < 0 ) {3.86 + input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );3.87 + input_event_keyup( (input_driver_t)joy, keycode, 0 );3.88 + } else {3.89 + input_event_keydown( (input_driver_t)joy, keycode, event.value );3.90 + input_event_keyup( (input_driver_t)joy, keycode+1, 0 );3.91 + }3.92 }3.93 }3.94 return TRUE;3.95 @@ -147,7 +163,7 @@3.96 * descriptor. The joystick is automatically added to the watch list.3.97 * @return The new joystick, or NULL if an error occurred.3.98 */3.99 -linux_joystick_t linux_joystick_add( const gchar *filename, int fd )3.100 +linux_joystick_t linux_joystick_new( const gchar *filename, int fd )3.101 {3.102 linux_joystick_t joy = g_malloc0(sizeof(struct linux_joystick));3.103 joy->filename = filename;3.104 @@ -174,14 +190,12 @@3.106 joy->channel = g_io_channel_unix_new(fd);3.107 g_io_add_watch( joy->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, linux_joystick_callback, joy );3.108 - input_register_device( (input_driver_t)joy, joy->axis_count + joy->button_count );3.109 - INFO( "Attached joystick '%s' at %s (%d buttons, %d axes)", joy->name, joy->filename,3.110 - joy->button_count, joy->axis_count );3.111 return joy;3.112 }3.114 int linux_joystick_init()3.115 {3.116 + int joysticks = 0;3.117 struct dirent *ent;3.118 DIR *dir = opendir(INPUT_PATH);3.120 @@ -191,16 +205,22 @@3.122 while( (ent = readdir(dir)) != NULL ) {3.123 if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&3.124 - isdigit(ent->d_name[2]) ) {3.125 + isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {3.126 gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );3.127 int fd = open(name, O_RDONLY|O_NONBLOCK);3.128 if( fd == -1 ) {3.129 g_free( name );3.130 } else {3.131 - linux_joystick_t js = linux_joystick_add( name, fd );3.132 + linux_joystick_t joy = linux_joystick_new( name, fd );3.133 + input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );3.134 + INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)",3.135 + joy->driver.id, joy->name, joy->button_count, joy->axis_count );3.136 + joysticks++;3.137 }3.138 }3.139 }3.141 closedir(dir);3.142 + return joysticks;3.143 }3.144 +
4.1 --- a/src/drivers/joy_linux.h Mon Jan 28 02:38:09 2008 +00004.2 +++ b/src/drivers/joy_linux.h Mon Jan 28 11:17:15 2008 +00004.3 @@ -25,11 +25,6 @@4.4 gboolean linux_joystick_init();4.6 /**4.7 - * Re-scan the available joystick devices, adding any new ones.4.8 - */4.9 -gboolean linux_joystick_rescan();4.10 -4.11 -/**4.12 * Shutdown the linux joystick system4.13 */4.14 void linux_joystick_shutdown();
.