filename | src/drivers/joy_linux.c |
changeset | 736:a02d1475ccfd |
prev | 669:ab344e42bca9 |
next | 984:a01567058a47 |
author | nkeynes |
date | Thu Jan 15 04:15:11 2009 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Add support for the Intel ICC compiler (C only, icc doesn't support Obj-C) - Rename Obj-C source to .m - Separate paths.c into paths_unix.c and paths_osx.m - Add configuration detection of ICC, along with specific opt flags |
file | annotate | diff | log | raw |
1.1 --- a/src/drivers/joy_linux.c Mon May 12 10:00:13 2008 +00001.2 +++ b/src/drivers/joy_linux.c Thu Jan 15 04:15:11 2009 +00001.3 @@ -51,7 +51,7 @@1.4 } *linux_joystick_t;1.6 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,1.7 - gpointer data );1.8 + gpointer data );1.9 static int linux_joystick_scan();1.10 static linux_joystick_t linux_joystick_new( const gchar *filename, int fd );1.11 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str );1.12 @@ -70,25 +70,25 @@1.13 {1.14 linux_joystick_t joy = (linux_joystick_t)dev;1.15 if( strncasecmp( str, "Button", 6 ) == 0 ){1.16 - unsigned long button = strtoul( str+6, NULL, 10 );1.17 - if( button > joy->button_count ) {1.18 - return 0;1.19 - }1.20 - return (uint16_t)button;1.21 + unsigned long button = strtoul( str+6, NULL, 10 );1.22 + if( button > joy->button_count ) {1.23 + return 0;1.24 + }1.25 + return (uint16_t)button;1.26 } else if( strncasecmp( str, "Axis", 4 ) == 0 ) {1.27 - char *endptr;1.28 - unsigned long axis = strtoul( str+4, &endptr, 10 );1.29 - if( axis > joy->axis_count || axis == 0 ) {1.30 - return 0;1.31 - }1.32 - int keycode = ((axis - 1) << 1) + joy->button_count + 1;1.33 - if( *endptr == '-' ) {1.34 - return keycode + 1;1.35 - } else {1.36 - return keycode;1.37 - }1.38 + char *endptr;1.39 + unsigned long axis = strtoul( str+4, &endptr, 10 );1.40 + if( axis > joy->axis_count || axis == 0 ) {1.41 + return 0;1.42 + }1.43 + int keycode = ((axis - 1) << 1) + joy->button_count + 1;1.44 + if( *endptr == '-' ) {1.45 + return keycode + 1;1.46 + } else {1.47 + return keycode;1.48 + }1.49 } else {1.50 - return 0;1.51 + return 0;1.52 }1.53 }1.55 @@ -96,18 +96,18 @@1.56 {1.57 linux_joystick_t joy = (linux_joystick_t)dev;1.58 if( keycode == 0 ) {1.59 - return NULL;1.60 + return NULL;1.61 }1.62 if( keycode <= joy->button_count ) {1.63 - return g_strdup_printf( "Button%d", keycode );1.64 + return g_strdup_printf( "Button%d", keycode );1.65 }1.66 if( keycode <= joy->button_count + joy->axis_count*2 ) {1.67 - int axis = keycode - joy->button_count - 1;1.68 - if( (axis & 1) == 0 ) {1.69 - return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );1.70 - } else {1.71 - return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );1.72 - }1.73 + int axis = keycode - joy->button_count - 1;1.74 + if( (axis & 1) == 0 ) {1.75 + return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );1.76 + } else {1.77 + return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );1.78 + }1.79 }1.80 return NULL;1.81 }1.82 @@ -129,39 +129,39 @@1.83 * On error, close the channel and delete the device.1.84 */1.85 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,1.86 - gpointer data )1.87 + gpointer data )1.88 {1.89 linux_joystick_t joy = (linux_joystick_t)data;1.91 if( condition & G_IO_HUP ) {1.92 - INFO( "Joystick '%s' disconnected\n", joy->name );1.93 - input_unregister_device((input_driver_t)joy);1.94 - return FALSE;1.95 + INFO( "Joystick '%s' disconnected\n", joy->name );1.96 + input_unregister_device((input_driver_t)joy);1.97 + return FALSE;1.98 }1.99 if( condition & G_IO_IN ) {1.100 - struct js_event event;1.101 - while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {1.102 - if( event.type == JS_EVENT_BUTTON ) {1.103 - int keycode = event.number+1;1.104 - if( event.value == 0 ) {1.105 - input_event_keyup( (input_driver_t)joy, keycode, 0 );1.106 - } else {1.107 - input_event_keydown( (input_driver_t)joy, keycode, event.value );1.108 - }1.109 - } else if( event.type == JS_EVENT_AXIS ) {1.110 - int keycode = (event.number*2) + joy->button_count + 1;1.111 - if( event.value == 0 ) {1.112 - input_event_keyup( (input_driver_t)joy, keycode, 0 );1.113 - input_event_keyup( (input_driver_t)joy, keycode+1, 0 );1.114 - } else if( event.value < 0 ) {1.115 - input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );1.116 - input_event_keyup( (input_driver_t)joy, keycode, 0 );1.117 - } else {1.118 - input_event_keydown( (input_driver_t)joy, keycode, event.value );1.119 - input_event_keyup( (input_driver_t)joy, keycode+1, 0 );1.120 - }1.121 - }1.122 - }1.123 + struct js_event event;1.124 + while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {1.125 + if( event.type == JS_EVENT_BUTTON ) {1.126 + int keycode = event.number+1;1.127 + if( event.value == 0 ) {1.128 + input_event_keyup( (input_driver_t)joy, keycode, 0 );1.129 + } else {1.130 + input_event_keydown( (input_driver_t)joy, keycode, event.value );1.131 + }1.132 + } else if( event.type == JS_EVENT_AXIS ) {1.133 + int keycode = (event.number*2) + joy->button_count + 1;1.134 + if( event.value == 0 ) {1.135 + input_event_keyup( (input_driver_t)joy, keycode, 0 );1.136 + input_event_keyup( (input_driver_t)joy, keycode+1, 0 );1.137 + } else if( event.value < 0 ) {1.138 + input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );1.139 + input_event_keyup( (input_driver_t)joy, keycode, 0 );1.140 + } else {1.141 + input_event_keydown( (input_driver_t)joy, keycode, event.value );1.142 + input_event_keyup( (input_driver_t)joy, keycode+1, 0 );1.143 + }1.144 + }1.145 + }1.146 }1.147 return TRUE;1.148 }1.149 @@ -183,17 +183,17 @@1.151 char *p = strrchr(filename, '/');1.152 if( p == NULL ) {1.153 - joy->driver.id = filename;1.154 + joy->driver.id = filename;1.155 } else {1.156 - joy->driver.id = p+1;1.157 + joy->driver.id = p+1;1.158 }1.160 if( ioctl( fd, JSIOCGNAME(128), joy->name ) == -1 ||1.161 - ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 ||1.162 - ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) {1.163 - ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) );1.164 - g_free(joy);1.165 - return NULL;1.166 + ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 ||1.167 + ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) {1.168 + ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) );1.169 + g_free(joy);1.170 + return NULL;1.171 }1.173 joy->channel = g_io_channel_unix_new(fd);1.174 @@ -208,24 +208,24 @@1.175 DIR *dir = opendir(INPUT_PATH);1.177 if( dir == NULL ) {1.178 - return 0;1.179 + return 0;1.180 }1.181 -1.182 +1.183 while( (ent = readdir(dir)) != NULL ) {1.184 - if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&1.185 - isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {1.186 - gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );1.187 - int fd = open(name, O_RDONLY|O_NONBLOCK);1.188 - if( fd == -1 ) {1.189 - g_free( name );1.190 - } else {1.191 - linux_joystick_t joy = linux_joystick_new( name, fd );1.192 - input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );1.193 - INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)",1.194 - joy->driver.id, joy->name, joy->button_count, joy->axis_count );1.195 - joysticks++;1.196 - }1.197 - }1.198 + if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&1.199 + isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {1.200 + gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );1.201 + int fd = open(name, O_RDONLY|O_NONBLOCK);1.202 + if( fd == -1 ) {1.203 + g_free( name );1.204 + } else {1.205 + linux_joystick_t joy = linux_joystick_new( name, fd );1.206 + input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );1.207 + INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)",1.208 + joy->driver.id, joy->name, joy->button_count, joy->axis_count );1.209 + joysticks++;1.210 + }1.211 + }1.212 }1.214 closedir(dir);1.215 @@ -235,7 +235,7 @@1.216 gboolean linux_joystick_init()1.217 {1.218 if( !linux_joystick_install_watch(INPUT_PATH) ) {1.219 - return FALSE;1.220 + return FALSE;1.221 }1.222 linux_joystick_scan();1.223 return TRUE;1.224 @@ -254,11 +254,11 @@1.225 static gboolean gtk_loop_check_input(gpointer data)1.226 {1.227 if( need_input_rescan == 1 ) {1.228 - need_input_rescan = 0;1.229 - int js = linux_joystick_scan();1.230 - if( js > 0 ) {1.231 - maple_reattach_all();1.232 - }1.233 + need_input_rescan = 0;1.234 + int js = linux_joystick_scan();1.235 + if( js > 0 ) {1.236 + maple_reattach_all();1.237 + }1.238 }1.239 return need_input_rescan != 2;1.240 }1.241 @@ -272,14 +272,14 @@1.242 {1.243 int fd = open( dir, O_RDONLY|O_NONBLOCK );1.244 if( fd == -1 ) {1.245 - return FALSE;1.246 + return FALSE;1.247 }1.248 -1.249 +1.250 signal( SIGRTMIN+1, dnotify_handler );1.251 fcntl(fd, F_SETSIG, SIGRTMIN + 1);1.252 if( fcntl(fd, F_NOTIFY, DN_CREATE|DN_MULTISHOT) == -1 ) {1.253 - close(fd);1.254 - return FALSE;1.255 + close(fd);1.256 + return FALSE;1.257 }1.258 watch_dir_fd = fd;1.259 g_timeout_add( 500, gtk_loop_check_input, NULL );
.