]> git.mxchange.org Git - flightgear.git/blob - Joystick/joystick.cxx
Renamed joystick.[ch] to joystick.[ch]xx
[flightgear.git] / Joystick / joystick.cxx
1 /**************************************************************************
2  * joystick.h -- joystick support
3  *
4  * Written by Michele America, started September 1997.
5  *
6  * Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef HAVE_JOYSTICK
28
29 #include <linux/joystick.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <unistd.h>
33
34 #endif
35
36 #include <Debug/fg_debug.h>
37
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;
42
43
44 static int joystick_fd;
45
46 int fgJoystickInit( int joy_num ) {
47
48     fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n");
49
50 #ifdef HAVE_JOYSTICK
51         int status;
52         char *fname;
53         struct JS_DATA_TYPE js;
54         int button;
55
56         /* argument should be 0 or 1 */
57         if( joy_num != 1 && joy_num != 0 ) {
58                 perror( "js" );
59                 return( 1 );
60         }
61
62         /* pick appropriate device file */
63         if (joy_num == 0)
64                 fname = "/dev/js0";
65         if (joy_num == 1)
66                 fname = "/dev/js1";
67
68         /* open device file */
69         joystick_fd = open(fname, O_RDONLY);
70         if (joystick_fd < 0) {
71                 perror ("js");
72                 return( 1 );
73         }
74         
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) {
79                 perror("js");
80                 return( 1 );
81         }
82         button = js.buttons & 1 || js.buttons & 2;
83         while(button == 0 ) {
84                 status = read(joystick_fd, &js, JS_RETURN);
85                 if (status != JS_RETURN) {
86                         perror("js");
87                         return( 1 );
88                 }
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;
98                 
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 ); */
102         }
103         status = read(joystick_fd, &js, JS_RETURN);
104         if (status != JS_RETURN) {
105                 perror("js");
106                 return( 1 );
107         }
108         
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 );
115         
116         sleep( 1 );
117         
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) {
122                 perror("js");
123                 return( 1 );
124         }
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) {
129                         perror("js");
130                         return( 1 );
131                 }
132                 button = js.buttons & 1 || js.buttons & 2;
133         }
134         status = read(joystick_fd, &js, JS_RETURN);
135         if (status != JS_RETURN) {
136                 perror("js");
137                 return( 1 );
138         }
139         joy_x_ctr = js.x;
140         joy_y_ctr = js.y;
141         
142         fgPrintf( FG_INPUT, FG_DEBUG,
143                   "Joystick calibration: X_ctr = %d, Y_ctr = %d\n", 
144                   joy_x_ctr, joy_y_ctr );
145         
146         sleep( 1 );
147         
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) {
152                 perror("js");
153                 return( 1 );
154         }
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) {
159                         perror("js");
160                         return( 1 );
161                 }
162                 button = js.buttons & 1 || js.buttons & 2;
163         }
164         status = read(joystick_fd, &js, JS_RETURN);
165         if (status != JS_RETURN) {
166                 perror("js");
167                 return( 1 );
168         }
169         joy_x_min = js.x;
170         joy_y_min = js.y;
171         fgPrintf( FG_INPUT, FG_DEBUG,
172                   "Joystick calibration: X_min = %d, Y_min = %d\n", 
173                   joy_x_min, joy_y_min );
174         
175         sleep( 1 );
176         
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) {
181                 perror("js");
182                 return( 1 );
183         }
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) {
188                         perror("js");
189                         return( 1 );
190                 }
191                 button = js.buttons & 1 || js.buttons & 2;
192         }
193         status = read(joystick_fd, &js, JS_RETURN);
194         if (status != JS_RETURN) {
195                 perror("js");
196                 return( 1 );
197         }
198         joy_x_max = js.x;
199         joy_y_max = js.y;
200         
201         fgPrintf( FG_INPUT, FG_DEBUG,
202                   "Joystick calibration: X_max = %d, Y_max = %d\n", 
203                   joy_x_max, joy_y_max );
204         
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 );
208         
209         return( joystick_fd );
210 #else
211         return( 1 );
212 #endif
213 }
214
215 /* void fgJoystickCalibrate( int joy_fd )
216 {
217
218 } */
219
220 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
221 {
222 #ifdef HAVE_JOYSTICK
223         struct JS_DATA_TYPE js;
224         int status;
225         
226         status = read(joystick_fd, &js, JS_RETURN);
227         if (status != JS_RETURN) {
228                 perror("js");
229                 return( 1 );
230         }
231
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",
235                  js.x,
236                  js.y ); */
237                  
238         if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
239                 *joy_x = 0.5;
240         else
241                 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
242         *joy_x = *joy_x*2-1;
243         
244         if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
245                 *joy_y = 0.5;
246         else
247                 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
248         *joy_y = *joy_y*2-1;
249         
250         *joy_b1 = js.buttons & 1;
251         *joy_b2 = js.buttons & 2;
252
253 #endif
254         return( 0 );
255 }
256
257
258 /* $Log$
259 /* Revision 1.1  1998/10/24 22:28:16  curt
260 /* Renamed joystick.[ch] to joystick.[ch]xx
261 /* Added js.hxx which is Steve's joystick interface class.
262 /*
263  * Revision 1.7  1998/04/25 22:06:29  curt
264  * Edited cvs log messages in source files ... bad bad bad!
265  *
266  * Revision 1.6  1998/04/18 04:14:05  curt
267  * Moved fg_debug.c to it's own library.
268  *
269  * Revision 1.5  1998/02/12 21:59:44  curt
270  * Incorporated code changes contributed by Charlie Hotchkiss
271  * <chotchkiss@namg.us.anritsu.com>
272  *
273  * Revision 1.4  1998/02/03 23:20:20  curt
274  * Lots of little tweaks to fix various consistency problems discovered by
275  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
276  * passed arguments along to the real printf().  Also incorporated HUD changes
277  * by Michele America.
278  *
279  * Revision 1.3  1998/01/27 00:47:54  curt
280  * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
281  * system and commandline/config file processing code.
282  *
283  * Revision 1.2  1997/12/30 20:47:40  curt
284  * Integrated new event manager with subsystem initializations.
285  *
286  * Revision 1.1  1997/08/29 18:06:54  curt
287  * Initial revision.
288  *
289  */