Files
lcdproc/server/drivers/joy.c
T

186 lines
4.3 KiB
C
Raw Normal View History

/*
* Joystick input driver for LCDd
*
* Only two unique functions are defined:
*
* joy_getkey
* joy_close
*
* All others are at their defaults.
*
* The code here is configured for a Gravis Gamepad (2 axis, 4 button)
*
*/
1999-12-09 23:15:14 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/joystick.h>
#ifndef JSIOCGNAME
#define JSIOCGNAME(len) _IOC(_IOC_READ, 'j', 0x13, len) /* get identifier string */
#endif
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "lcd.h"
#include "joy.h"
#include "report.h"
#include "shared/str.h"
1999-12-09 23:15:14 +00:00
2005-01-29 18:49:29 +00:00
#define JOY_NAMELENGTH 128
#define JOY_DEFAULT_DEVICE "/dev/js0"
#define JOY_MAPSIZE 16
#define JOY_DEFAULT_AXISMAP "EFGHIJKLMNOPQRST"
#define JOY_DEFAULT_BUTTONMAP "BDACEFGHIJKLMNOP"
1999-12-09 23:15:14 +00:00
int fd;
struct js_event js;
char axes = 2;
char buttons = 2;
2005-01-29 18:49:29 +00:00
int jsversion = 0x000801;
char jsname[JOY_NAMELENGTH] = "Unknown";
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
int *axis = NULL;
int *button = NULL;
1999-12-09 23:15:14 +00:00
// Configured for a Gravis Gamepad (2 axis, 4 button)
2005-01-29 18:49:29 +00:00
char axismap[JOY_MAPSIZE+1] = JOY_DEFAULT_AXISMAP;
char buttonmap[JOY_MAPSIZE+1] = JOY_DEFAULT_BUTTONMAP;
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
// Vars for the server core
MODULE_EXPORT char *api_version = API_VERSION;
MODULE_EXPORT int stay_in_foreground = 0;
MODULE_EXPORT int supports_multiple = 0;
MODULE_EXPORT char *symbol_prefix = "joy_";
1999-12-09 23:15:14 +00:00
////////////////////////////////////////////////////////////
// init() should set up any device-specific stuff, and
// point all the function pointers.
2002-02-21 22:50:12 +00:00
MODULE_EXPORT int
2001-12-30 00:15:25 +00:00
joy_init (Driver *drvthis, char *args)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
char device[256];
2005-01-29 18:49:29 +00:00
/* Read config file */
1999-12-09 23:15:14 +00:00
2005-01-29 18:49:29 +00:00
/* What device should be used */
strncpy(device, drvthis->config_get_string(drvthis->name, "Device", 0,
JOY_DEFAULT_DEVICE), sizeof(device));
device[sizeof(device)-1] = '\0';
2005-01-29 18:49:29 +00:00
/* How does the axis map look like */
strncpy(axismap, drvthis->config_get_string(drvthis->name, "AxisMap", 0,
JOY_DEFAULT_AXISMAP), sizeof(axismap));
axismap[sizeof(axismap)-1] = '\0';
2005-01-29 18:49:29 +00:00
/* How does the button map look like */
strncpy(buttonmap, drvthis->config_get_string(drvthis->name, "ButtonMap", 0,
JOY_DEFAULT_BUTTONMAP), sizeof(buttonmap));
buttonmap[sizeof(buttonmap)-1] = '\0';
2005-01-29 18:49:29 +00:00
/* End of config file parsing */
2001-09-25 06:20:27 +00:00
if ((fd = open (device, O_RDONLY)) < 0)
2000-04-03 22:13:57 +00:00
return -1;
1999-12-09 23:15:14 +00:00
2001-09-25 06:20:27 +00:00
fcntl (fd, F_SETFL, O_NONBLOCK);
2000-04-03 22:13:57 +00:00
ioctl (fd, JSIOCGVERSION, &jsversion);
ioctl (fd, JSIOCGAXES, &axes);
ioctl (fd, JSIOCGBUTTONS, &buttons);
2005-01-29 18:49:29 +00:00
ioctl (fd, JSIOCGNAME (JOY_NAMELENGTH), jsname);
2001-12-30 00:15:25 +00:00
report (RPT_NOTICE, "Joystick (%s) has %d axes and %d buttons. Driver version is %d.%d.%d.\n",
jsname, axes, buttons,
jsversion >> 16, (jsversion >> 8) & 0xff, jsversion & 0xff);
2001-09-25 06:20:27 +00:00
if ((axis = calloc (axes, sizeof (int))) == NULL) {
2001-12-30 00:15:25 +00:00
report (RPT_ERR, "joystick: could not allocate memory for axes");
2001-09-25 06:20:27 +00:00
return -1;
}
if ((button = calloc (buttons, sizeof (char))) == NULL) {
2001-12-30 00:15:25 +00:00
report (RPT_ERR, "joystick: could not allocate memory for buttons");
2001-09-25 06:20:27 +00:00
return -1;
}
2001-12-30 00:15:25 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
joy_close (Driver *drvthis)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
close (fd);
2000-04-03 22:13:57 +00:00
// Why do I have so much trouble getting memory freed without segfaults??
2001-09-25 06:20:27 +00:00
// Use gdb and find out :) In preliminary testing, this seemed to work...
2001-12-30 00:15:25 +00:00
if(axis) free(axis);
if(button) free(button);
1999-12-09 23:15:14 +00:00
}
//////////////////////////////////////////////////////////////////////
// Tries to read a character from an input device...
//
// Return 0 for "nothing available".
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT char
joy_getkey (Driver *drvthis)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int i;
int err;
2001-09-25 06:20:27 +00:00
if ((err = read (fd, &js, sizeof (struct js_event))) <= 0) {
2000-04-03 22:13:57 +00:00
return 0;
2001-09-25 06:20:27 +00:00
} else
if (err != sizeof (struct js_event)) {
2001-12-30 00:15:25 +00:00
report(RPT_ERR, "error reading joystick input");
2001-09-25 06:20:27 +00:00
return 0;
}
1999-12-09 23:15:14 +00:00
// if(js.type & JS_EVENT_INIT) return 0;
2000-04-03 22:13:57 +00:00
switch (js.type & ~JS_EVENT_INIT) {
2001-09-25 06:20:27 +00:00
case JS_EVENT_BUTTON:
button[js.number] = js.value;
break;
case JS_EVENT_AXIS:
axis[js.number] = js.value;
break;
2000-04-03 22:13:57 +00:00
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (buttons) {
//printf("Buttons: ");
for (i = 0; i < buttons; i++)
//printf("%2d:%s ", i, button[i] ? "on " : "off");
if (button[i])
return buttonmap[i];
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (axes) {
//printf("Axes: ");
for (i = 0; i < axes; i++) {
//printf("%2d:%6d ", i, axis[i]);
// Eliminate noise...
if (axis[i] > 20000)
return axismap[(2 * i) + 1];
if (axis[i] < -20000)
return axismap[(2 * i)];
}
}
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}