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 <Main/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", joy_x_dead_min, joy_x_dead_max, \
100 // joy_y_dead_min, joy_y_dead_max );
102 status = read(joystick_fd, &js, JS_RETURN);
103 if (status != JS_RETURN) {
108 fgPrintf( FG_INPUT, FG_DEBUG,
109 "\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n",
110 joy_x_dead_min, joy_x_dead_max );
111 fgPrintf( FG_INPUT, FG_DEBUG,
112 " Y_dead_min = %d, Y_dead_max = %d\n",
113 joy_y_dead_min, joy_y_dead_max );
117 fgPrintf( FG_INPUT, FG_DEBUG,
118 "\nCenter joystick and press any joystick button.\n" );
119 status = read(joystick_fd, &js, JS_RETURN);
120 if (status != JS_RETURN) {
124 button = js.buttons & 1 || js.buttons & 2;
125 while(button == 0 ) {
126 status = read(joystick_fd, &js, JS_RETURN);
127 if (status != JS_RETURN) {
131 button = js.buttons & 1 || js.buttons & 2;
133 status = read(joystick_fd, &js, JS_RETURN);
134 if (status != JS_RETURN) {
141 fgPrintf( FG_INPUT, FG_DEBUG,
142 "Joystick calibration: X_ctr = %d, Y_ctr = %d\n",
143 joy_x_ctr, joy_y_ctr );
147 fgPrintf( FG_INPUT, FG_DEBUG,
148 "\nMove joystick to upper left and press any joystick button.\n" );
149 status = read(joystick_fd, &js, JS_RETURN);
150 if (status != JS_RETURN) {
154 button = js.buttons & 1 || js.buttons & 2;
155 while(button == 0 ) {
156 status = read(joystick_fd, &js, JS_RETURN);
157 if (status != JS_RETURN) {
161 button = js.buttons & 1 || js.buttons & 2;
163 status = read(joystick_fd, &js, JS_RETURN);
164 if (status != JS_RETURN) {
170 fgPrintf( FG_INPUT, FG_DEBUG,
171 "Joystick calibration: X_min = %d, Y_min = %d\n",
172 joy_x_min, joy_y_min );
176 fgPrintf( FG_INPUT, FG_DEBUG,
177 "\nMove joystick to lower right and press any joystick button.\n" );
178 status = read(joystick_fd, &js, JS_RETURN);
179 if (status != JS_RETURN) {
183 button = js.buttons & 1 || js.buttons & 2;
184 while(button == 0 ) {
185 status = read(joystick_fd, &js, JS_RETURN);
186 if (status != JS_RETURN) {
190 button = js.buttons & 1 || js.buttons & 2;
192 status = read(joystick_fd, &js, JS_RETURN);
193 if (status != JS_RETURN) {
200 fgPrintf( FG_INPUT, FG_DEBUG,
201 "Joystick calibration: X_max = %d, Y_max = %d\n",
202 joy_x_max, joy_y_max );
204 // joy_x_ctr = (joy_x_max-joy_x_min)/2;
205 // joy_y_ctr = (joy_y_max-joy_y_min)/2;
206 // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
208 return( joystick_fd );
212 /* void fgJoystickCalibrate( int joy_fd )
217 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
220 struct JS_DATA_TYPE js;
223 status = read(joystick_fd, &js, JS_RETURN);
224 if (status != JS_RETURN) {
229 /* printf("\n button 0: %s button 1: %s X position: %4d Y position: %4d\n",
230 (js.buttons & 1) ? "on " : "off",
231 (js.buttons & 2) ? "on " : "off",
235 if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
238 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
241 if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
244 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
247 *joy_b1 = js.buttons & 1;
248 *joy_b2 = js.buttons & 2;
256 /* Revision 1.3 1998/01/27 00:47:54 curt
257 /* Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
258 /* system and commandline/config file processing code.
260 * Revision 1.2 1997/12/30 20:47:40 curt
261 * Integrated new event manager with subsystem initializations.
263 * Revision 1.1 1997/08/29 18:06:54 curt