Search
lxdream.org :: lxdream/src/drivers/joy_linux.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/joy_linux.c
changeset 615:38b69ec2f4c8
prev614:a2d239d4438a
next620:d9b28f78b952
author nkeynes
date Mon Jan 28 11:17:15 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Split axes into positive and negative sides. Works better now
file annotate diff log raw
1.1 --- a/src/drivers/joy_linux.c Mon Jan 28 02:38:09 2008 +0000
1.2 +++ b/src/drivers/joy_linux.c Mon Jan 28 11:17:15 2008 +0000
1.3 @@ -44,9 +44,6 @@
1.4
1.5 } *linux_joystick_t;
1.6
1.7 -static GList *linux_joysticks = NULL;
1.8 -
1.9 -
1.10 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,
1.11 gpointer data );
1.12 static linux_joystick_t linux_joystick_add( const gchar *filename, int fd );
1.13 @@ -55,9 +52,10 @@
1.14 static void linux_joystick_destroy( input_driver_t joy );
1.15
1.16 /**
1.17 - * Convert keysym to keycode. Keysyms are either Button%d or Axis%d, with buttons
1.18 - * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count.
1.19 + * Convert keysym to keycode. Keysyms are either Button%d or Axis%d[+-], with buttons
1.20 + * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count*2.
1.21 * The first button is Button1. (no Button0)
1.22 + * The first axis is Axis1+, then Axis1-, Axis2+ and so forth.
1.23 */
1.24 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str )
1.25 {
1.26 @@ -69,11 +67,17 @@
1.27 }
1.28 return (uint16_t)button;
1.29 } else if( strncasecmp( str, "Axis", 4 ) == 0 ) {
1.30 - unsigned long axis = strtoul( str+4, NULL, 10 );
1.31 + char *endptr;
1.32 + unsigned long axis = strtoul( str+4, &endptr, 10 );
1.33 if( axis > joy->axis_count || axis == 0 ) {
1.34 return 0;
1.35 }
1.36 - return (uint16_t)(axis+joy->button_count);
1.37 + int keycode = ((axis - 1) << 1) + joy->button_count + 1;
1.38 + if( *endptr == '-' ) {
1.39 + return keycode + 1;
1.40 + } else {
1.41 + return keycode;
1.42 + }
1.43 } else {
1.44 return 0;
1.45 }
1.46 @@ -88,8 +92,13 @@
1.47 if( keycode <= joy->button_count ) {
1.48 return g_strdup_printf( "Button%d", keycode );
1.49 }
1.50 - if( keycode <= joy->button_count + joy->axis_count ) {
1.51 - return g_strdup_printf( "Axis%d", keycode - joy->button_count );
1.52 + if( keycode <= joy->button_count + joy->axis_count*2 ) {
1.53 + int axis = keycode - joy->button_count - 1;
1.54 + if( (axis & 1) == 0 ) {
1.55 + return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );
1.56 + } else {
1.57 + return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );
1.58 + }
1.59 }
1.60 return NULL;
1.61 }
1.62 @@ -124,18 +133,25 @@
1.63 if( condition & G_IO_IN ) {
1.64 struct js_event event;
1.65 while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {
1.66 - int keycode = 0;
1.67 if( event.type == JS_EVENT_BUTTON ) {
1.68 - keycode = event.number+1;
1.69 - } else if( event.type == JS_EVENT_AXIS ) {
1.70 - keycode = event.number+1 + joy->button_count;
1.71 - }
1.72 - if( keycode != 0 ) {
1.73 + int keycode = event.number+1;
1.74 if( event.value == 0 ) {
1.75 - input_event_keyup( (input_driver_t)joy, keycode, event.value );
1.76 + input_event_keyup( (input_driver_t)joy, keycode, 0 );
1.77 } else {
1.78 input_event_keydown( (input_driver_t)joy, keycode, event.value );
1.79 }
1.80 + } else if( event.type == JS_EVENT_AXIS ) {
1.81 + int keycode = (event.number*2) + joy->button_count + 1;
1.82 + if( event.value == 0 ) {
1.83 + input_event_keyup( (input_driver_t)joy, keycode, 0 );
1.84 + input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
1.85 + } else if( event.value < 0 ) {
1.86 + input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );
1.87 + input_event_keyup( (input_driver_t)joy, keycode, 0 );
1.88 + } else {
1.89 + input_event_keydown( (input_driver_t)joy, keycode, event.value );
1.90 + input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
1.91 + }
1.92 }
1.93 }
1.94 return TRUE;
1.95 @@ -147,7 +163,7 @@
1.96 * descriptor. The joystick is automatically added to the watch list.
1.97 * @return The new joystick, or NULL if an error occurred.
1.98 */
1.99 -linux_joystick_t linux_joystick_add( const gchar *filename, int fd )
1.100 +linux_joystick_t linux_joystick_new( const gchar *filename, int fd )
1.101 {
1.102 linux_joystick_t joy = g_malloc0(sizeof(struct linux_joystick));
1.103 joy->filename = filename;
1.104 @@ -174,14 +190,12 @@
1.105
1.106 joy->channel = g_io_channel_unix_new(fd);
1.107 g_io_add_watch( joy->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, linux_joystick_callback, joy );
1.108 - input_register_device( (input_driver_t)joy, joy->axis_count + joy->button_count );
1.109 - INFO( "Attached joystick '%s' at %s (%d buttons, %d axes)", joy->name, joy->filename,
1.110 - joy->button_count, joy->axis_count );
1.111 return joy;
1.112 }
1.113
1.114 int linux_joystick_init()
1.115 {
1.116 + int joysticks = 0;
1.117 struct dirent *ent;
1.118 DIR *dir = opendir(INPUT_PATH);
1.119
1.120 @@ -191,16 +205,22 @@
1.121
1.122 while( (ent = readdir(dir)) != NULL ) {
1.123 if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&
1.124 - isdigit(ent->d_name[2]) ) {
1.125 + isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {
1.126 gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );
1.127 int fd = open(name, O_RDONLY|O_NONBLOCK);
1.128 if( fd == -1 ) {
1.129 g_free( name );
1.130 } else {
1.131 - linux_joystick_t js = linux_joystick_add( name, fd );
1.132 + linux_joystick_t joy = linux_joystick_new( name, fd );
1.133 + input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );
1.134 + INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)",
1.135 + joy->driver.id, joy->name, joy->button_count, joy->axis_count );
1.136 + joysticks++;
1.137 }
1.138 }
1.139 }
1.140
1.141 closedir(dir);
1.142 + return joysticks;
1.143 }
1.144 +
.