Joystick driver:
+ Cleaned up slightly + Converted to use syslog instead of printf + Re-enabled free() in joy_close()
This commit is contained in:
+29
-21
@@ -21,6 +21,7 @@
|
|||||||
#include <sys/errno.h>
|
#include <sys/errno.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
#include <linux/joystick.h>
|
#include <linux/joystick.h>
|
||||||
#ifndef JSIOCGNAME
|
#ifndef JSIOCGNAME
|
||||||
@@ -31,6 +32,7 @@
|
|||||||
#include "shared/str.h"
|
#include "shared/str.h"
|
||||||
|
|
||||||
#define NAME_LENGTH 128
|
#define NAME_LENGTH 128
|
||||||
|
#define JOY_DEFAULT_DEVICE "/dev/js0"
|
||||||
|
|
||||||
#include "lcd.h"
|
#include "lcd.h"
|
||||||
#include "joy.h"
|
#include "joy.h"
|
||||||
@@ -38,6 +40,7 @@
|
|||||||
lcd_logical_driver *joy;
|
lcd_logical_driver *joy;
|
||||||
|
|
||||||
int fd;
|
int fd;
|
||||||
|
extern int debug_level;
|
||||||
|
|
||||||
struct js_event js;
|
struct js_event js;
|
||||||
|
|
||||||
@@ -65,7 +68,7 @@ joy_init (struct lcd_logical_driver *driver, char *args)
|
|||||||
|
|
||||||
joy = driver;
|
joy = driver;
|
||||||
|
|
||||||
strcpy (device, "/dev/js0");
|
strcpy (device, JOY_DEFAULT_DEVICE);
|
||||||
|
|
||||||
argc = get_args (argv, args, 64);
|
argc = get_args (argv, args, 64);
|
||||||
|
|
||||||
@@ -101,29 +104,30 @@ joy_init (struct lcd_logical_driver *driver, char *args)
|
|||||||
driver->getkey = joy_getkey;
|
driver->getkey = joy_getkey;
|
||||||
driver->close = joy_close;
|
driver->close = joy_close;
|
||||||
|
|
||||||
/* FIXME: This crashes!
|
if ((fd = open (device, O_RDONLY)) < 0)
|
||||||
if(args)
|
|
||||||
{
|
|
||||||
if(strlen(args) > 0)
|
|
||||||
strcpy(device, args);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
fd = open (device, O_RDONLY);
|
|
||||||
fcntl (fd, F_SETFL, O_NONBLOCK);
|
|
||||||
|
|
||||||
if (fd < 0)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
fcntl (fd, F_SETFL, O_NONBLOCK);
|
||||||
ioctl (fd, JSIOCGVERSION, &jsversion);
|
ioctl (fd, JSIOCGVERSION, &jsversion);
|
||||||
ioctl (fd, JSIOCGAXES, &axes);
|
ioctl (fd, JSIOCGAXES, &axes);
|
||||||
ioctl (fd, JSIOCGBUTTONS, &buttons);
|
ioctl (fd, JSIOCGBUTTONS, &buttons);
|
||||||
ioctl (fd, JSIOCGNAME (NAME_LENGTH), jsname);
|
ioctl (fd, JSIOCGNAME (NAME_LENGTH), jsname);
|
||||||
|
|
||||||
debug ("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);
|
if (debug_level > 2) {
|
||||||
|
syslog(LOG_DEBUG, "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);
|
||||||
|
}
|
||||||
|
|
||||||
axis = calloc (axes, sizeof (int));
|
if ((axis = calloc (axes, sizeof (int))) == NULL) {
|
||||||
button = calloc (buttons, sizeof (char));
|
syslog(LOG_ERR, "joystick: could not allocate memory for axes");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((button = calloc (buttons, sizeof (char))) == NULL) {
|
||||||
|
syslog(LOG_ERR, "joystick: could not allocate memory for buttons");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return fd; // 200 is arbitrary. (must be 1 or more)
|
return fd; // 200 is arbitrary. (must be 1 or more)
|
||||||
}
|
}
|
||||||
@@ -133,13 +137,16 @@ joy_close ()
|
|||||||
{
|
{
|
||||||
if (joy->framebuf != NULL)
|
if (joy->framebuf != NULL)
|
||||||
free (joy->framebuf);
|
free (joy->framebuf);
|
||||||
|
|
||||||
close (fd);
|
close (fd);
|
||||||
|
|
||||||
joy->framebuf = NULL;
|
joy->framebuf = NULL;
|
||||||
|
|
||||||
// Why do I have so much trouble getting memory freed without segfaults??
|
// Why do I have so much trouble getting memory freed without segfaults??
|
||||||
//if(axis) free(axis);
|
// Use gdb and find out :) In preliminary testing, this seemed to work...
|
||||||
//if(button) free(button);
|
|
||||||
|
if (axis) free(axis);
|
||||||
|
if (button) free(button);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,13 +161,14 @@ joy_getkey ()
|
|||||||
int i;
|
int i;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
err = read (fd, &js, sizeof (struct js_event));
|
if ((err = read (fd, &js, sizeof (struct js_event))) <= 0) {
|
||||||
if (err <= 0)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
} else
|
||||||
if (err != sizeof (struct js_event)) {
|
if (err != sizeof (struct js_event)) {
|
||||||
fprintf (stderr, "\nerror reading joystick\n");
|
syslog(LOG_ERR, "error reading joystick input");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if(js.type & JS_EVENT_INIT) return 0;
|
// if(js.type & JS_EVENT_INIT) return 0;
|
||||||
|
|
||||||
switch (js.type & ~JS_EVENT_INIT) {
|
switch (js.type & ~JS_EVENT_INIT) {
|
||||||
|
|||||||
Reference in New Issue
Block a user