]> git.mxchange.org Git - flightgear.git/blob - Joystick/joystick.c
Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
[flightgear.git] / Joystick / joystick.c
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 <Main/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", joy_x_dead_min, joy_x_dead_max, \
100                 //              joy_y_dead_min, joy_y_dead_max );
101         }
102         status = read(joystick_fd, &js, JS_RETURN);
103         if (status != JS_RETURN) {
104                 perror("js");
105                 return( 1 );
106         }
107         
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 );
114         
115         sleep( 1 );
116         
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) {
121                 perror("js");
122                 return( 1 );
123         }
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) {
128                         perror("js");
129                         return( 1 );
130                 }
131                 button = js.buttons & 1 || js.buttons & 2;
132         }
133         status = read(joystick_fd, &js, JS_RETURN);
134         if (status != JS_RETURN) {
135                 perror("js");
136                 return( 1 );
137         }
138         joy_x_ctr = js.x;
139         joy_y_ctr = js.y;
140         
141         fgPrintf( FG_INPUT, FG_DEBUG,
142                   "Joystick calibration: X_ctr = %d, Y_ctr = %d\n", 
143                   joy_x_ctr, joy_y_ctr );
144         
145         sleep( 1 );
146         
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) {
151                 perror("js");
152                 return( 1 );
153         }
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) {
158                         perror("js");
159                         return( 1 );
160                 }
161                 button = js.buttons & 1 || js.buttons & 2;
162         }
163         status = read(joystick_fd, &js, JS_RETURN);
164         if (status != JS_RETURN) {
165                 perror("js");
166                 return( 1 );
167         }
168         joy_x_min = js.x;
169         joy_y_min = js.y;
170         fgPrintf( FG_INPUT, FG_DEBUG,
171                   "Joystick calibration: X_min = %d, Y_min = %d\n", 
172                   joy_x_min, joy_y_min );
173         
174         sleep( 1 );
175         
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) {
180                 perror("js");
181                 return( 1 );
182         }
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) {
187                         perror("js");
188                         return( 1 );
189                 }
190                 button = js.buttons & 1 || js.buttons & 2;
191         }
192         status = read(joystick_fd, &js, JS_RETURN);
193         if (status != JS_RETURN) {
194                 perror("js");
195                 return( 1 );
196         }
197         joy_x_max = js.x;
198         joy_y_max = js.y;
199         
200         fgPrintf( FG_INPUT, FG_DEBUG,
201                   "Joystick calibration: X_max = %d, Y_max = %d\n", 
202                   joy_x_max, joy_y_max );
203         
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 );
207         
208         return( joystick_fd );
209 #endif
210 }
211
212 /* void fgJoystickCalibrate( int joy_fd )
213 {
214
215 } */
216
217 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
218 {
219 #ifdef HAVE_JOYSTICK
220         struct JS_DATA_TYPE js;
221         int status;
222         
223         status = read(joystick_fd, &js, JS_RETURN);
224         if (status != JS_RETURN) {
225                 perror("js");
226                 return( 1 );
227         }
228
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",
232                  js.x,
233                  js.y ); */
234                  
235         if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
236                 *joy_x = 0.5;
237         else
238                 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
239         *joy_x = *joy_x*2-1;
240         
241         if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
242                 *joy_y = 0.5;
243         else
244                 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
245         *joy_y = *joy_y*2-1;
246         
247         *joy_b1 = js.buttons & 1;
248         *joy_b2 = js.buttons & 2;
249
250         return( 0 );
251 #endif
252 }
253
254
255 /* $Log$
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.
259 /*
260  * Revision 1.2  1997/12/30 20:47:40  curt
261  * Integrated new event manager with subsystem initializations.
262  *
263  * Revision 1.1  1997/08/29 18:06:54  curt
264  * Initial revision.
265  *
266  */