1 /**************************************************************************
2 * joystick.h -- joystick support
4 * Written by Michele America, started September 1997.
6 * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
24 **************************************************************************/
29 #include <linux/joystick.h>
36 #include <Debug/fg_debug.h>
38 static joy_x_min=0, joy_x_ctr=0, joy_x_max=0;
39 static joy_y_min=0, joy_y_ctr=0, joy_y_max=0;
40 static joy_x_dead_min=1000, joy_x_dead_max=-1000;
41 static joy_y_dead_min=1000, joy_y_dead_max=-1000;
44 static int joystick_fd;
46 int fgJoystickInit( int joy_num ) {
48 fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n");
53 struct JS_DATA_TYPE js;
56 /* argument should be 0 or 1 */
57 if( joy_num != 1 && joy_num != 0 ) {
62 /* pick appropriate device file */
68 /* open device file */
69 joystick_fd = open(fname, O_RDONLY);
70 if (joystick_fd < 0) {
75 fgPrintf( FG_INPUT,FG_ALERT,
76 "\nMove joystick around dead spot and press any joystick button.\n" );
77 status = read(joystick_fd, &js, JS_RETURN);
78 if (status != JS_RETURN) {
82 button = js.buttons & 1 || js.buttons & 2;
84 status = read(joystick_fd, &js, JS_RETURN);
85 if (status != JS_RETURN) {
89 button = js.buttons & 1 || js.buttons & 2;
90 if( js.x > joy_x_dead_max )
91 joy_x_dead_max = js.x;
92 if( js.x < joy_x_dead_min )
93 joy_x_dead_min = js.x;
94 if( js.y > joy_y_dead_max )
95 joy_y_dead_max = js.y;
96 if( js.y < joy_y_dead_min )
97 joy_y_dead_min = js.y;
99 /* printf( "Xmin %d Xmax %d Ymin %d Ymax %d",
100 joy_x_dead_min, joy_x_dead_max,
101 joy_y_dead_min, joy_y_dead_max ); */
103 status = read(joystick_fd, &js, JS_RETURN);
104 if (status != JS_RETURN) {
109 fgPrintf( FG_INPUT, FG_DEBUG,
110 "\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n",
111 joy_x_dead_min, joy_x_dead_max );
112 fgPrintf( FG_INPUT, FG_DEBUG,
113 " Y_dead_min = %d, Y_dead_max = %d\n",
114 joy_y_dead_min, joy_y_dead_max );
118 fgPrintf( FG_INPUT, FG_DEBUG,
119 "\nCenter joystick and press any joystick button.\n" );
120 status = read(joystick_fd, &js, JS_RETURN);
121 if (status != JS_RETURN) {
125 button = js.buttons & 1 || js.buttons & 2;
126 while(button == 0 ) {
127 status = read(joystick_fd, &js, JS_RETURN);
128 if (status != JS_RETURN) {
132 button = js.buttons & 1 || js.buttons & 2;
134 status = read(joystick_fd, &js, JS_RETURN);
135 if (status != JS_RETURN) {
142 fgPrintf( FG_INPUT, FG_DEBUG,
143 "Joystick calibration: X_ctr = %d, Y_ctr = %d\n",
144 joy_x_ctr, joy_y_ctr );
148 fgPrintf( FG_INPUT, FG_DEBUG,
149 "\nMove joystick to upper left and press any joystick button.\n" );
150 status = read(joystick_fd, &js, JS_RETURN);
151 if (status != JS_RETURN) {
155 button = js.buttons & 1 || js.buttons & 2;
156 while(button == 0 ) {
157 status = read(joystick_fd, &js, JS_RETURN);
158 if (status != JS_RETURN) {
162 button = js.buttons & 1 || js.buttons & 2;
164 status = read(joystick_fd, &js, JS_RETURN);
165 if (status != JS_RETURN) {
171 fgPrintf( FG_INPUT, FG_DEBUG,
172 "Joystick calibration: X_min = %d, Y_min = %d\n",
173 joy_x_min, joy_y_min );
177 fgPrintf( FG_INPUT, FG_DEBUG,
178 "\nMove joystick to lower right and press any joystick button.\n" );
179 status = read(joystick_fd, &js, JS_RETURN);
180 if (status != JS_RETURN) {
184 button = js.buttons & 1 || js.buttons & 2;
185 while(button == 0 ) {
186 status = read(joystick_fd, &js, JS_RETURN);
187 if (status != JS_RETURN) {
191 button = js.buttons & 1 || js.buttons & 2;
193 status = read(joystick_fd, &js, JS_RETURN);
194 if (status != JS_RETURN) {
201 fgPrintf( FG_INPUT, FG_DEBUG,
202 "Joystick calibration: X_max = %d, Y_max = %d\n",
203 joy_x_max, joy_y_max );
205 // joy_x_ctr = (joy_x_max-joy_x_min)/2;
206 // joy_y_ctr = (joy_y_max-joy_y_min)/2;
207 // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
209 return( joystick_fd );
215 /* void fgJoystickCalibrate( int joy_fd )
220 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
223 struct JS_DATA_TYPE js;
226 status = read(joystick_fd, &js, JS_RETURN);
227 if (status != JS_RETURN) {
232 /* printf("\n button 0: %s button 1: %s X position: %4d Y position: %4d\n",
233 (js.buttons & 1) ? "on " : "off",
234 (js.buttons & 2) ? "on " : "off",
238 if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
241 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
244 if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
247 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
250 *joy_b1 = js.buttons & 1;
251 *joy_b2 = js.buttons & 2;
259 /* Revision 1.6 1998/04/18 04:14:05 curt
260 /* Moved fg_debug.c to it's own library.
262 * Revision 1.5 1998/02/12 21:59:44 curt
263 * Incorporated code changes contributed by Charlie Hotchkiss
264 * <chotchkiss@namg.us.anritsu.com>
266 * Revision 1.4 1998/02/03 23:20:20 curt
267 * Lots of little tweaks to fix various consistency problems discovered by
268 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
269 * passed arguments along to the real printf(). Also incorporated HUD changes
270 * by Michele America.
272 * Revision 1.3 1998/01/27 00:47:54 curt
273 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
274 * system and commandline/config file processing code.
276 * Revision 1.2 1997/12/30 20:47:40 curt
277 * Integrated new event manager with subsystem initializations.
279 * Revision 1.1 1997/08/29 18:06:54 curt