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.
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
30 #include <linux/joystick.h>
31 #include <glib/giochannel.h>
37 #define INPUT_PATH "/dev/input"
39 typedef struct linux_joystick {
40 struct input_driver driver;
41 const gchar *filename;
44 int button_count, axis_count;
49 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,
51 static linux_joystick_t linux_joystick_add( const gchar *filename, int fd );
52 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str );
53 static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode );
54 static void linux_joystick_destroy( input_driver_t joy );
55 static gboolean linux_joystick_install_watch( const gchar *dir );
56 static void linux_joystick_uninstall_watch( void );
59 * Convert keysym to keycode. Keysyms are either Button%d or Axis%d[+-], with buttons
60 * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count*2.
61 * The first button is Button1. (no Button0)
62 * The first axis is Axis1+, then Axis1-, Axis2+ and so forth.
64 static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str )
66 linux_joystick_t joy = (linux_joystick_t)dev;
67 if( strncasecmp( str, "Button", 6 ) == 0 ){
68 unsigned long button = strtoul( str+6, NULL, 10 );
69 if( button > joy->button_count ) {
72 return (uint16_t)button;
73 } else if( strncasecmp( str, "Axis", 4 ) == 0 ) {
75 unsigned long axis = strtoul( str+4, &endptr, 10 );
76 if( axis > joy->axis_count || axis == 0 ) {
79 int keycode = ((axis - 1) << 1) + joy->button_count + 1;
80 if( *endptr == '-' ) {
90 static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode )
92 linux_joystick_t joy = (linux_joystick_t)dev;
96 if( keycode <= joy->button_count ) {
97 return g_strdup_printf( "Button%d", keycode );
99 if( keycode <= joy->button_count + joy->axis_count*2 ) {
100 int axis = keycode - joy->button_count - 1;
101 if( (axis & 1) == 0 ) {
102 return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );
104 return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );
110 static void linux_joystick_destroy( input_driver_t dev )
112 linux_joystick_t joy = (linux_joystick_t)dev;
113 g_free( (gchar *)joy->filename );
114 g_io_channel_shutdown(joy->channel, FALSE, NULL );
115 g_io_channel_unref(joy->channel);
120 * Callback from the GIOChannel whenever data is available, or an error/disconnect
123 * On data, process all pending events and direct them to the input system.
124 * On error, close the channel and delete the device.
126 static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition,
129 linux_joystick_t joy = (linux_joystick_t)data;
131 if( condition & G_IO_HUP ) {
132 INFO( "Joystick '%s' disconnected\n", joy->name );
133 input_unregister_device((input_driver_t)joy);
136 if( condition & G_IO_IN ) {
137 struct js_event event;
138 while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {
139 if( event.type == JS_EVENT_BUTTON ) {
140 int keycode = event.number+1;
141 if( event.value == 0 ) {
142 input_event_keyup( (input_driver_t)joy, keycode, 0 );
144 input_event_keydown( (input_driver_t)joy, keycode, event.value );
146 } else if( event.type == JS_EVENT_AXIS ) {
147 int keycode = (event.number*2) + joy->button_count + 1;
148 if( event.value == 0 ) {
149 input_event_keyup( (input_driver_t)joy, keycode, 0 );
150 input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
151 } else if( event.value < 0 ) {
152 input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );
153 input_event_keyup( (input_driver_t)joy, keycode, 0 );
155 input_event_keydown( (input_driver_t)joy, keycode, event.value );
156 input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
165 * Create a new joystick device structure given filename and (open) file
166 * descriptor. The joystick is automatically added to the watch list.
167 * @return The new joystick, or NULL if an error occurred.
169 linux_joystick_t linux_joystick_new( const gchar *filename, int fd )
171 linux_joystick_t joy = g_malloc0(sizeof(struct linux_joystick));
172 joy->filename = filename;
175 joy->driver.resolve_keysym = linux_joystick_resolve_keysym;
176 joy->driver.get_keysym_for_keycode = linux_joystick_keysym_for_keycode;
177 joy->driver.destroy = linux_joystick_destroy;
179 char *p = strrchr(filename, '/');
181 joy->driver.id = filename;
183 joy->driver.id = p+1;
186 if( ioctl( fd, JSIOCGNAME(128), joy->name ) == -1 ||
187 ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 ||
188 ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) {
189 ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) );
194 joy->channel = g_io_channel_unix_new(fd);
195 g_io_add_watch( joy->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, linux_joystick_callback, joy );
199 int linux_joystick_init()
201 linux_joystick_install_watch(INPUT_PATH);
202 linux_joystick_scan();
205 int linux_joystick_scan()
209 DIR *dir = opendir(INPUT_PATH);
215 while( (ent = readdir(dir)) != NULL ) {
216 if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&
217 isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {
218 gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );
219 int fd = open(name, O_RDONLY|O_NONBLOCK);
223 linux_joystick_t joy = linux_joystick_new( name, fd );
224 input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );
225 INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)",
226 joy->driver.id, joy->name, joy->button_count, joy->axis_count );
236 void linux_joystick_shutdown(void)
238 linux_joystick_uninstall_watch();
241 /*************************** dnotify support **********************/
243 static volatile gboolean need_input_rescan = FALSE;
244 static int watch_dir_fd;
246 static gboolean gtk_loop_check_input(gpointer data)
248 if( need_input_rescan ) {
249 int js = linux_joystick_scan();
251 maple_reattach_all();
257 static void dnotify_handler(int sig )
259 need_input_rescan = TRUE;
262 static gboolean linux_joystick_install_watch( const gchar *dir )
264 int fd = open( dir, O_RDONLY|O_NONBLOCK );
269 signal( SIGRTMIN+1, dnotify_handler );
270 fcntl(fd, F_SETSIG, SIGRTMIN + 1);
271 if( fcntl(fd, F_NOTIFY, DN_CREATE|DN_MULTISHOT) == -1 ) {
276 g_timeout_add( 500, gtk_loop_check_input, NULL );
279 static void linux_joystick_uninstall_watch(void)
281 signal( SIGRTMIN+1, SIG_IGN );
282 close( watch_dir_fd );
.