2 * $Id: joy_linux.c,v 1.12 2007-11-08 11:54:16 nkeynes Exp $
4 * Linux joystick input device support
6 * Copyright (c) 2008 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
28 #include <linux/joystick.h>
29 #include <glib/giochannel.h>
35 #define INPUT_PATH "/dev/input"
37 typedef struct linux_joystick {
38 struct input_driver driver;
39 const gchar *filename;
42 int button_count, axis_count;
47 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,
49 static linux_joystick_t linux_joystick_add( const gchar *filename, int fd );
50 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str );
51 static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode );
52 static void linux_joystick_destroy( input_driver_t joy );
55 * Convert keysym to keycode. Keysyms are either Button%d or Axis%d[+-], with buttons
56 * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count*2.
57 * The first button is Button1. (no Button0)
58 * The first axis is Axis1+, then Axis1-, Axis2+ and so forth.
60 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str )
62 linux_joystick_t joy = (linux_joystick_t)dev;
63 if( strncasecmp( str, "Button", 6 ) == 0 ){
64 unsigned long button = strtoul( str+6, NULL, 10 );
65 if( button > joy->button_count ) {
68 return (uint16_t)button;
69 } else if( strncasecmp( str, "Axis", 4 ) == 0 ) {
71 unsigned long axis = strtoul( str+4, &endptr, 10 );
72 if( axis > joy->axis_count || axis == 0 ) {
75 int keycode = ((axis - 1) << 1) + joy->button_count + 1;
76 if( *endptr == '-' ) {
86 static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode )
88 linux_joystick_t joy = (linux_joystick_t)dev;
92 if( keycode <= joy->button_count ) {
93 return g_strdup_printf( "Button%d", keycode );
95 if( keycode <= joy->button_count + joy->axis_count*2 ) {
96 int axis = keycode - joy->button_count - 1;
97 if( (axis & 1) == 0 ) {
98 return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );
100 return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );
106 static void linux_joystick_destroy( input_driver_t dev )
108 linux_joystick_t joy = (linux_joystick_t)dev;
109 g_free( (gchar *)joy->filename );
110 g_io_channel_shutdown(joy->channel, FALSE, NULL );
111 g_io_channel_unref(joy->channel);
116 * Callback from the GIOChannel whenever data is available, or an error/disconnect
119 * On data, process all pending events and direct them to the input system.
120 * On error, close the channel and delete the device.
122 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,
125 linux_joystick_t joy = (linux_joystick_t)data;
127 if( condition & G_IO_HUP ) {
129 INFO( "Joystick '%s' disconnected\n", joy->name );
130 input_unregister_device((input_driver_t)joy);
133 if( condition & G_IO_IN ) {
134 struct js_event event;
135 while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {
136 if( event.type == JS_EVENT_BUTTON ) {
137 int keycode = event.number+1;
138 if( event.value == 0 ) {
139 input_event_keyup( (input_driver_t)joy, keycode, 0 );
141 input_event_keydown( (input_driver_t)joy, keycode, event.value );
143 } else if( event.type == JS_EVENT_AXIS ) {
144 int keycode = (event.number*2) + joy->button_count + 1;
145 if( event.value == 0 ) {
146 input_event_keyup( (input_driver_t)joy, keycode, 0 );
147 input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
148 } else if( event.value < 0 ) {
149 input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );
150 input_event_keyup( (input_driver_t)joy, keycode, 0 );
152 input_event_keydown( (input_driver_t)joy, keycode, event.value );
153 input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
162 * Create a new joystick device structure given filename and (open) file
163 * descriptor. The joystick is automatically added to the watch list.
164 * @return The new joystick, or NULL if an error occurred.
166 linux_joystick_t linux_joystick_new( const gchar *filename, int fd )
168 linux_joystick_t joy = g_malloc0(sizeof(struct linux_joystick));
169 joy->filename = filename;
172 joy->driver.resolve_keysym = linux_joystick_resolve_keysym;
173 joy->driver.get_keysym_for_keycode = linux_joystick_keysym_for_keycode;
174 joy->driver.destroy = linux_joystick_destroy;
176 char *p = strrchr(filename, '/');
178 joy->driver.id = filename;
180 joy->driver.id = p+1;
183 if( ioctl( fd, JSIOCGNAME(128), joy->name ) == -1 ||
184 ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 ||
185 ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) {
186 ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) );
191 joy->channel = g_io_channel_unix_new(fd);
192 g_io_add_watch( joy->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, linux_joystick_callback, joy );
196 int linux_joystick_init()
200 DIR *dir = opendir(INPUT_PATH);
206 while( (ent = readdir(dir)) != NULL ) {
207 if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&
208 isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {
209 gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );
210 int fd = open(name, O_RDONLY|O_NONBLOCK);
214 linux_joystick_t joy = linux_joystick_new( name, fd );
215 input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );
216 INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)",
217 joy->driver.id, joy->name, joy->button_count, joy->axis_count );
.