Search
lxdream.org :: lxdream/src/drivers/joy_linux.c
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 (11 years ago)
permissions -rw-r--r--
last change Split axes into positive and negative sides. Works better now
file annotate diff log raw
nkeynes@614
     1
/**
nkeynes@614
     2
 * $Id: joy_linux.c,v 1.12 2007-11-08 11:54:16 nkeynes Exp $
nkeynes@614
     3
 *
nkeynes@614
     4
 * Linux joystick input device support
nkeynes@614
     5
 *
nkeynes@614
     6
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@614
     7
 *
nkeynes@614
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@614
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@614
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@614
    11
 * (at your option) any later version.
nkeynes@614
    12
 *
nkeynes@614
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@614
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@614
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@614
    16
 * GNU General Public License for more details.
nkeynes@614
    17
 */
nkeynes@614
    18
nkeynes@614
    19
#include <sys/types.h>
nkeynes@614
    20
#include <sys/ioctl.h>
nkeynes@614
    21
#include <errno.h>
nkeynes@614
    22
#include <stdio.h>
nkeynes@614
    23
#include <string.h>
nkeynes@614
    24
#include <fcntl.h>
nkeynes@614
    25
#include <dirent.h>
nkeynes@614
    26
#include <ctype.h>
nkeynes@614
    27
nkeynes@614
    28
#include <linux/joystick.h>
nkeynes@614
    29
#include <glib/giochannel.h>
nkeynes@614
    30
#include <glib.h>
nkeynes@614
    31
nkeynes@614
    32
#include "lxdream.h"
nkeynes@614
    33
#include "display.h"
nkeynes@614
    34
nkeynes@614
    35
#define INPUT_PATH "/dev/input"
nkeynes@614
    36
nkeynes@614
    37
typedef struct linux_joystick {
nkeynes@614
    38
    struct input_driver driver;
nkeynes@614
    39
    const gchar *filename;
nkeynes@614
    40
    char name[128];
nkeynes@614
    41
    int fd;
nkeynes@614
    42
    int button_count, axis_count;
nkeynes@614
    43
    GIOChannel *channel;
nkeynes@614
    44
nkeynes@614
    45
} *linux_joystick_t;
nkeynes@614
    46
nkeynes@614
    47
static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition, 
nkeynes@614
    48
					 gpointer data );
nkeynes@614
    49
static linux_joystick_t linux_joystick_add( const gchar *filename, int fd );
nkeynes@614
    50
static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str );
nkeynes@614
    51
static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode );
nkeynes@614
    52
static void linux_joystick_destroy( input_driver_t joy );
nkeynes@614
    53
nkeynes@614
    54
/**
nkeynes@615
    55
 * Convert keysym to keycode. Keysyms are either Button%d or Axis%d[+-], with buttons
nkeynes@615
    56
 * numbered 1 .. button_count, then axes from button_count+1 .. button_count + axis_count*2.
nkeynes@614
    57
 * The first button is Button1. (no Button0)
nkeynes@615
    58
 * The first axis is Axis1+, then Axis1-, Axis2+ and so forth.
nkeynes@614
    59
 */
nkeynes@614
    60
static uint16_t linux_joystick_resolve_keysym( input_driver_t dev, const gchar *str )
nkeynes@614
    61
{
nkeynes@614
    62
    linux_joystick_t joy = (linux_joystick_t)dev;
nkeynes@614
    63
    if( strncasecmp( str, "Button", 6 ) == 0 ){
nkeynes@614
    64
	unsigned long button = strtoul( str+6, NULL, 10 );
nkeynes@614
    65
	if( button > joy->button_count ) {
nkeynes@614
    66
	    return 0;
nkeynes@614
    67
	}
nkeynes@614
    68
	return (uint16_t)button;
nkeynes@614
    69
    } else if( strncasecmp( str, "Axis", 4 ) == 0 ) {
nkeynes@615
    70
	char *endptr;
nkeynes@615
    71
	unsigned long axis = strtoul( str+4, &endptr, 10 );
nkeynes@614
    72
	if( axis > joy->axis_count || axis == 0 ) {
nkeynes@614
    73
	    return 0;
nkeynes@614
    74
	}
nkeynes@615
    75
	int keycode = ((axis - 1) << 1) + joy->button_count + 1;
nkeynes@615
    76
	if( *endptr == '-' ) {
nkeynes@615
    77
	    return keycode + 1;
nkeynes@615
    78
	} else {
nkeynes@615
    79
	    return keycode;
nkeynes@615
    80
	}
nkeynes@614
    81
    } else {
nkeynes@614
    82
	return 0;
nkeynes@614
    83
    }
nkeynes@614
    84
}
nkeynes@614
    85
nkeynes@614
    86
static gchar *linux_joystick_keysym_for_keycode( input_driver_t dev, uint16_t keycode )
nkeynes@614
    87
{
nkeynes@614
    88
    linux_joystick_t joy = (linux_joystick_t)dev;
nkeynes@614
    89
    if( keycode == 0 ) {
nkeynes@614
    90
	return NULL;
nkeynes@614
    91
    }
nkeynes@614
    92
    if( keycode <= joy->button_count ) {
nkeynes@614
    93
	return g_strdup_printf( "Button%d", keycode );
nkeynes@614
    94
    }
nkeynes@615
    95
    if( keycode <= joy->button_count + joy->axis_count*2 ) {
nkeynes@615
    96
	int axis = keycode - joy->button_count - 1;
nkeynes@615
    97
	if( (axis & 1) == 0 ) {
nkeynes@615
    98
	    return g_strdup_printf( "Axis%d+", (axis >> 1)+1 );
nkeynes@615
    99
	} else {
nkeynes@615
   100
	    return g_strdup_printf( "Axis%d-", (axis >> 1)+1 );
nkeynes@615
   101
	}
nkeynes@614
   102
    }
nkeynes@614
   103
    return NULL;
nkeynes@614
   104
}
nkeynes@614
   105
nkeynes@614
   106
static void linux_joystick_destroy( input_driver_t dev )
nkeynes@614
   107
{
nkeynes@614
   108
    linux_joystick_t joy = (linux_joystick_t)dev;
nkeynes@614
   109
    g_free( (gchar *)joy->filename );
nkeynes@614
   110
    g_io_channel_shutdown(joy->channel, FALSE, NULL );
nkeynes@614
   111
    g_io_channel_unref(joy->channel);
nkeynes@614
   112
    g_free( joy );
nkeynes@614
   113
}
nkeynes@614
   114
nkeynes@614
   115
/**
nkeynes@614
   116
 * Callback from the GIOChannel whenever data is available, or an error/disconnect
nkeynes@614
   117
 * occurs.
nkeynes@614
   118
 *
nkeynes@614
   119
 * On data, process all pending events and direct them to the input system.
nkeynes@614
   120
 * On error, close the channel and delete the device.
nkeynes@614
   121
 */
nkeynes@614
   122
static gboolean linux_joystick_callback( GIOChannel *source, GIOCondition condition, 
nkeynes@614
   123
					 gpointer data )
nkeynes@614
   124
{
nkeynes@614
   125
    linux_joystick_t joy = (linux_joystick_t)data;
nkeynes@614
   126
nkeynes@614
   127
    if( condition & G_IO_HUP ) {
nkeynes@614
   128
	close(joy->fd);
nkeynes@614
   129
	INFO( "Joystick '%s' disconnected\n", joy->name );
nkeynes@614
   130
	input_unregister_device((input_driver_t)joy);
nkeynes@614
   131
	return FALSE;
nkeynes@614
   132
    }
nkeynes@614
   133
    if( condition & G_IO_IN ) {
nkeynes@614
   134
	struct js_event event;
nkeynes@614
   135
	while( read( joy->fd, &event, sizeof(event) ) == sizeof(event) ) {
nkeynes@614
   136
	    if( event.type == JS_EVENT_BUTTON ) {
nkeynes@615
   137
		int keycode = event.number+1;
nkeynes@614
   138
		if( event.value == 0 ) {
nkeynes@615
   139
		    input_event_keyup( (input_driver_t)joy, keycode, 0 );
nkeynes@614
   140
		} else {
nkeynes@614
   141
		    input_event_keydown( (input_driver_t)joy, keycode, event.value );
nkeynes@614
   142
		}
nkeynes@615
   143
	    } else if( event.type == JS_EVENT_AXIS ) {
nkeynes@615
   144
		int keycode = (event.number*2) + joy->button_count + 1;
nkeynes@615
   145
		if( event.value == 0 ) {
nkeynes@615
   146
		    input_event_keyup( (input_driver_t)joy, keycode, 0 );
nkeynes@615
   147
		    input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
nkeynes@615
   148
		} else if( event.value < 0 ) {
nkeynes@615
   149
		    input_event_keydown( (input_driver_t)joy, keycode+1, -event.value );
nkeynes@615
   150
		    input_event_keyup( (input_driver_t)joy, keycode, 0 );
nkeynes@615
   151
		} else {
nkeynes@615
   152
		    input_event_keydown( (input_driver_t)joy, keycode, event.value );
nkeynes@615
   153
		    input_event_keyup( (input_driver_t)joy, keycode+1, 0 );
nkeynes@615
   154
		}
nkeynes@614
   155
	    }
nkeynes@614
   156
	}
nkeynes@614
   157
	return TRUE;
nkeynes@614
   158
    }
nkeynes@614
   159
}
nkeynes@614
   160
nkeynes@614
   161
/**
nkeynes@614
   162
 * Create a new joystick device structure given filename and (open) file
nkeynes@614
   163
 * descriptor. The joystick is automatically added to the watch list.
nkeynes@614
   164
 * @return The new joystick, or NULL if an error occurred.
nkeynes@614
   165
 */
nkeynes@615
   166
linux_joystick_t linux_joystick_new( const gchar *filename, int fd )
nkeynes@614
   167
{
nkeynes@614
   168
    linux_joystick_t joy = g_malloc0(sizeof(struct linux_joystick));
nkeynes@614
   169
    joy->filename = filename;
nkeynes@614
   170
    joy->fd = fd;
nkeynes@614
   171
    joy->name[0] = '\0';
nkeynes@614
   172
    joy->driver.resolve_keysym = linux_joystick_resolve_keysym;
nkeynes@614
   173
    joy->driver.get_keysym_for_keycode = linux_joystick_keysym_for_keycode;
nkeynes@614
   174
    joy->driver.destroy = linux_joystick_destroy;
nkeynes@614
   175
nkeynes@614
   176
    char *p = strrchr(filename, '/');
nkeynes@614
   177
    if( p == NULL ) {
nkeynes@614
   178
	joy->driver.id = filename;
nkeynes@614
   179
    } else {
nkeynes@614
   180
	joy->driver.id = p+1;
nkeynes@614
   181
    }
nkeynes@614
   182
nkeynes@614
   183
    if( ioctl( fd, JSIOCGNAME(128), joy->name ) == -1 ||
nkeynes@614
   184
	ioctl( fd, JSIOCGAXES, &joy->axis_count ) == -1 || 
nkeynes@614
   185
	ioctl( fd, JSIOCGBUTTONS, &joy->button_count ) == -1 ) {
nkeynes@614
   186
	ERROR( "Error reading joystick data from %s (%s)\n", filename, strerror(errno) );
nkeynes@614
   187
	g_free(joy);
nkeynes@614
   188
	return NULL;
nkeynes@614
   189
    }
nkeynes@614
   190
nkeynes@614
   191
    joy->channel = g_io_channel_unix_new(fd);
nkeynes@614
   192
    g_io_add_watch( joy->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, linux_joystick_callback, joy );
nkeynes@614
   193
    return joy;
nkeynes@614
   194
}
nkeynes@614
   195
nkeynes@614
   196
int linux_joystick_init()
nkeynes@614
   197
{
nkeynes@615
   198
    int joysticks = 0;
nkeynes@614
   199
    struct dirent *ent;
nkeynes@614
   200
    DIR *dir = opendir(INPUT_PATH);
nkeynes@614
   201
nkeynes@614
   202
    if( dir == NULL ) {
nkeynes@614
   203
	return 0;
nkeynes@614
   204
    }
nkeynes@614
   205
    
nkeynes@614
   206
    while( (ent = readdir(dir)) != NULL ) {
nkeynes@614
   207
	if( ent->d_name[0] == 'j' && ent->d_name[1] == 's' &&
nkeynes@615
   208
	    isdigit(ent->d_name[2]) && !input_has_device(ent->d_name) ) {
nkeynes@614
   209
	    gchar *name = g_strdup_printf( "%s/%s", INPUT_PATH, ent->d_name );
nkeynes@614
   210
	    int fd = open(name, O_RDONLY|O_NONBLOCK);
nkeynes@614
   211
	    if( fd == -1 ) {
nkeynes@614
   212
		g_free( name );
nkeynes@614
   213
	    } else {
nkeynes@615
   214
		linux_joystick_t joy = linux_joystick_new( name, fd );
nkeynes@615
   215
		input_register_device( (input_driver_t)joy, (joy->axis_count*2) + joy->button_count );
nkeynes@615
   216
		INFO( "Attached joystick %s named '%s', (%d buttons, %d axes)", 
nkeynes@615
   217
		      joy->driver.id, joy->name, joy->button_count, joy->axis_count );
nkeynes@615
   218
		joysticks++;
nkeynes@614
   219
	    }
nkeynes@614
   220
	}
nkeynes@614
   221
    }
nkeynes@614
   222
nkeynes@614
   223
    closedir(dir);
nkeynes@615
   224
    return joysticks;
nkeynes@614
   225
}
nkeynes@615
   226
.