First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / linux / lnx_jstk.c
blobd77631ba4e8d39514e9ac092d3adbefec4087509
1 /*
2 * Copyright 1995 by Frederic Lepied, France. <fred@sugix.frmug.fr.net>
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Frederic Lepied not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Frederic Lepied makes no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
14 * FREDERIC LEPIED DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL FREDERIC LEPIED BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
25 static const char rcs_id[] = "Id: lnx_jstk.c,v 1.1 1995/12/20 14:06:09 lepied Exp";
27 #ifdef HAVE_XORG_CONFIG_H
28 #include <xorg-config.h>
29 #endif
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <errno.h>
35 #define inline __inline__
36 #include <linux/joystick.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
40 #include "xf86.h"
42 #if !defined(JSIOCGTIMELIMIT)
43 /* make 2.1.x joystick.h backward compatable */
44 #define JSIOCGTIMELIMIT JS_GET_TIMELIMIT
45 #define JSIOCSTIMELIMIT JS_SET_TIMELIMIT
46 #define js_status JS_DATA_TYPE
47 #endif
50 /***********************************************************************
52 * xf86JoystickOn --
54 * open the device and init timeout according to the device value.
56 ***********************************************************************
59 int
60 xf86JoystickOn(char *name, int *timeout, int *centerX, int *centerY)
62 int fd;
63 struct js_status js;
65 #ifdef DEBUG
66 ErrorF("xf86JoystickOn %s\n", name);
67 #endif
69 if ((fd = open(name, O_RDWR | O_NDELAY, 0)) < 0)
71 xf86Msg(X_WARNING, "Cannot open joystick '%s' (%s)\n", name,
72 strerror(errno));
73 return -1;
76 if (*timeout == 0) {
77 if (ioctl (fd, JSIOCGTIMELIMIT, timeout) == -1) {
78 Error("joystick JSIOCGTIMELIMIT ioctl");
80 else {
81 xf86Msg(X_CONFIG, "Joystick: timeout value = %d\n", *timeout);
84 else {
85 if (ioctl(fd, JSIOCSTIMELIMIT, timeout) == -1) {
86 Error("joystick JSIOCSTIMELIMIT ioctl");
90 /* Assume the joystick is centred when this is called */
91 read(fd, &js, JS_RETURN);
92 if (*centerX < 0) {
93 *centerX = js.x;
94 xf86Msg(X_CONFIG, "Joystick: CenterX set to %d\n", *centerX);
96 if (*centerY < 0) {
97 *centerY = js.y;
98 xf86Msg(X_CONFIG, "Joystick: CenterY set to %d\n", *centerY);
101 return fd;
104 /***********************************************************************
106 * xf86JoystickInit --
108 * called when X device is initialized.
110 ***********************************************************************
113 void
114 xf86JoystickInit()
116 return;
119 /***********************************************************************
121 * xf86JoystickOff --
123 * close the handle.
125 ***********************************************************************
129 xf86JoystickOff(int *fd, int doclose)
131 int oldfd;
133 if (((oldfd = *fd) >= 0) && doclose) {
134 close(*fd);
135 *fd = -1;
137 return oldfd;
140 /***********************************************************************
142 * xf86JoystickGetState --
144 * return the state of buttons and the position of the joystick.
146 ***********************************************************************
150 xf86JoystickGetState(int fd, int *x, int *y, int *buttons)
152 struct js_status js;
153 int status;
155 status = read(fd, &js, JS_RETURN);
157 if (status != JS_RETURN)
159 Error("Joystick read");
160 return 0;
163 *x = js.x;
164 *y = js.y;
165 *buttons = js.buttons;
167 return 1;
171 * Entry point for XFree86 Loader
173 void
174 linux_jstkModuleInit(pointer *data, INT32 *magic)
176 *magic = MAGIC_DONE;
177 *data = NULL;
180 /* end of lnx_jstk.c */