Stripped everything down to the minimum

Added generic data structs
Splitted to multiple files
Moved to newer joystick.h interface
Added read_input function
This commit is contained in:
Sascha Hlusiak
2007-03-18 01:53:43 -04:00
parent 3ca9b73dd7
commit af7edfd136
3 changed files with 282 additions and 0 deletions

166
src/linux_jstk.c Normal file
View File

@@ -0,0 +1,166 @@
/*
* Copyright 2007 by Sascha Hlusiak. <saschahlusiak@freedesktop.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Sascha Hlusiak not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Sascha Hlusiak makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* SASCHA HLUSIAK DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL SASCHA HLUSIAK BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <linux/joystick.h>
#include <fcntl.h>
#include <errno.h>
#include "linux_jstk.h"
#include "xf86Jstk.h"
/***********************************************************************
*
* xf86JoystickOn --
*
* Open and initialize a joystick device
*
***********************************************************************
*/
int
xf86JoystickOn(JoystickDevPtr joystick,int init)
{
char joy_name[128];
if ((joystick->fd = open(joystick->device, O_RDWR | O_NDELAY, 0)) < 0) {
xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", joystick->device,
strerror(errno));
return -1;
}
/* ioctl(fd, JSIOCGVERSION, &version);*/
if (ioctl(joystick->fd, JSIOCGAXES, &joystick->axes)==-1) {
xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed (%s)\n", joystick->device,
strerror(errno));
return -1;
}
if (ioctl(joystick->fd, JSIOCGBUTTONS, &joystick->buttons)==-1) {
xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed (%s)\n", joystick->device,
strerror(errno));
return -1;
}
if (init != 0) {
if (ioctl(joystick->fd, JSIOCGNAME(128), joy_name)==-1) {
xf86Msg(X_ERROR, "Joystick: ioctl on '%s' failed (%s)\n", joystick->device,
strerror(errno));
return -1;
}
xf86Msg(X_INFO, "Joystick: %s. %d buttons, %d axes\n",
joy_name, joystick->axes, joystick->buttons);
}
return joystick->fd;
}
/***********************************************************************
*
* xf86JoystickOff --
*
* close the handle.
*
***********************************************************************
*/
void
xf86JoystickOff(JoystickDevPtr joystick)
{
if ((joystick->fd >= 0)) {
close(joystick->fd);
joystick->fd = -1;
}
}
/***********************************************************************
*
* xf86ReadJoystickData --
*
* Reads data from fd and stores it in the JoystickDevRec struct
* return 1 if success, 0 otherwise
*
***********************************************************************
*/
int
xf86ReadJoystickData(JoystickDevPtr joystick)
{
struct js_event js;
if (xf86ReadSerial(joystick->fd, &js, sizeof(struct js_event)) != sizeof(struct js_event))
return 0;
switch(js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
if (js.number<32)
joystick->button[js.number].pressed=js.value;
break;
case JS_EVENT_AXIS:
if (js.number<32) {
joystick->axis[js.number].value=js.value;
if (abs(js.value)<joystick->axis[js.number].deadzone) {
joystick->axis[js.number].value=0;
}
}
break;
}
return 1;
}
/***********************************************************************
*
* xf86JoystickGetState --
*
* return the state of buttons and the position of the joystick.
*
***********************************************************************
*/
// int
// xf86JoystickGetState(int fd, int *x, int *y, int *buttons)
// {
// struct js_status js;
// int status;
//
// status = read(fd, &js, JS_RETURN);
//
// if (status != JS_RETURN)
// {
// Error("Joystick read");
// return 0;
// }
//
// *x = js.x;
// *y = js.y;
// *buttons = js.buttons;
//
// return 1;
// }

33
src/linux_jstk.h Normal file
View File

@@ -0,0 +1,33 @@
/*
* Copyright 2007 by Sascha Hlusiak. <saschahlusiak@freedesktop.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Sascha Hlusiak not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Sascha Hlusiak makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* SASCHA HLUSIAK DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL SASCHA HLUSIAK BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef _LINUX_JSTK_H_INCLUDED_
#define _LINUX_JSTK_H_INCLUDED_
#include "xf86Jstk.h"
int xf86JoystickOn(JoystickDevPtr joystick, int init);
void xf86JoystickOff(JoystickDevPtr joystick);
int xf86ReadJoystickData(JoystickDevPtr joystick);
#endif

83
src/xf86Jstk.h Normal file
View File

@@ -0,0 +1,83 @@
/*
* Copyright 2007 by Sascha Hlusiak. <saschahlusiak@freedesktop.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Sascha Hlusiak not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Sascha Hlusiak makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* SASCHA HLUSIAK DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL SASCHA HLUSIAK BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef _XF86JSTK_H_INCLUDED_
#define _XF86JSTK_H_INCLUDED_
#include <xf86.h>
#define MAXBUTTONS 32
#define MAXAXES MAXBUTTONS
enum JOYSTICKTYPE {
TYPE_NONE,
TYPE_BYVALUE, /* Speed of cursor is relative to amplitude */
TYPE_ACCELERATED, /* Speed is accelerated */
TYPE_ABSOLUTE /* The amplitude defines the cursor position */
};
enum JOYSTICKMAPPING {
MAPPING_NONE=0,
MAPPING_X,
MAPPING_Y,
MAPPING_ZX,
MAPPING_ZY,
MAPPING_BUTTON,
MAPPING_KEY,
MAPPING_SPEED_MULTIPLY,
MAPPING_DISABLE,
MAPPING_DISABLE_MOUSE,
MAPPING_DISABLE_KEYS
};
typedef struct
{
int fd; /* Actual file descriptor */
OsTimerPtr timer;
int timeout;
char *device; /* Name of the device */
struct AXIS
{
int value;
int deadzone;
enum JOYSTICKTYPE type;
enum JOYSTICKMAPPING mapping;
}axis[32]; /* Configuration per axis */
struct BUTTON
{
char pressed;
int value;
enum JOYSTICKMAPPING mapping;
}button[32]; /* Configuration per button */
int axes, buttons; /* Number of axes and buttons */
} JoystickDevRec, *JoystickDevPtr;
#endif