First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / bsd / bsd_jstk.c
blobb5b91b0c7eb6d871067814181bb7958f84786de0
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.
24 /* Modified for FreeBSD by David Dawes <dawes@XFree86.org> */
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 #include <machine/joystick.h>
36 #include <fcntl.h>
38 #include "misc.h"
39 #include "xf86.h"
41 #define JS_RETURN sizeof(struct joystick)
43 /***********************************************************************
45 * xf86JoystickOn --
47 * open the device and init timeout according to the device value.
49 ***********************************************************************
52 int
53 xf86JoystickOn(char * name, int *timeout, int *centerX, int *centerY)
55 int status;
56 int changed = 0;
57 int timeinmicros;
58 struct joystick js;
60 #ifdef DEBUG
61 ErrorF("xf86JoystickOn: %s\n", name);
62 #endif
64 if ((status = open(name, O_RDWR | O_NDELAY, 0)) < 0)
66 xf86Msg(X_WARNING, "xf86JoystickOn: Cannot open joystick '%s' (%s)\n",
67 name, strerror(errno));
68 return -1;
71 if (*timeout <= 0) {
72 /* Use the current setting */
73 ioctl(status, JOY_GETTIMEOUT, (char *)&timeinmicros);
74 *timeout = timeinmicros / 1000;
75 if (*timeout == 0)
76 *timeout = 1;
77 changed = 1;
79 /* Maximum allowed timeout in the FreeBSD driver is 10ms */
80 if (*timeout > 10) {
81 *timeout = 10;
82 changed = 1;
85 if (changed)
86 xf86Msg(X_PROBED, "Joystick: timeout value = %d\n", *timeout);
88 timeinmicros = *timeout * 1000;
90 /* Assume the joystick is centred when this is called */
91 read(status, &js, JS_RETURN);
92 if (*centerX < 0) {
93 *centerX = js.x;
94 xf86Msg(X_PROBED, "Joystick: CenterX set to %d\n", *centerX);
96 if (*centerY < 0) {
97 *centerY = js.y;
98 xf86Msg(X_PROBED, "Joystick: CenterY set to %d\n", *centerY);
101 return status;
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 joystick 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.b1 | (js.b2 << 1);
166 #ifdef DEBUG
167 ErrorF("xf86JoystickGetState: x = %d, y = %d, buttons = %d\n", *x, *y,
168 *buttons);
169 #endif
171 return 1;
175 * Entry point for XFree86 Loader
177 void
178 bsd_jstkModuleInit(pointer *data, INT32 *magic)
180 *magic = MAGIC_DONE;
181 *data = NULL;
183 /* end of bsd_jstk.c */