]> git.mxchange.org Git - flightgear.git/blob - Joystick/joystick.c
Integrated new event manager with subsystem initializations.
[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 static joy_x_min=0, joy_x_ctr=0, joy_x_max=0;
37 static joy_y_min=0, joy_y_ctr=0, joy_y_max=0;
38 static joy_x_dead_min=1000, joy_x_dead_max=-1000;
39 static joy_y_dead_min=1000, joy_y_dead_max=-1000;
40
41
42 static int joystick_fd;
43
44 int fgJoystickInit( int joy_num ) {
45     printf("Initializing joystick\n");
46
47 #ifdef HAVE_JOYSTICK
48         int status;
49         char *fname;
50         struct JS_DATA_TYPE js;
51         int button;
52
53         /* argument should be 0 or 1 */
54         if( joy_num != 1 && joy_num != 0 ) {
55                 perror( "js" );
56                 return( 1 );
57         }
58
59         /* pick appropriate device file */
60         if (joy_num == 0)
61                 fname = "/dev/js0";
62         if (joy_num == 1)
63                 fname = "/dev/js1";
64
65         /* open device file */
66         joystick_fd = open(fname, O_RDONLY);
67         if (joystick_fd < 0) {
68                 perror ("js");
69                 return( 1 );
70         }
71         
72         printf( "\nMove joystick around dead spot and press any joystick button.\n" );
73         status = read(joystick_fd, &js, JS_RETURN);
74         if (status != JS_RETURN) {
75                 perror("js");
76                 return( 1 );
77         }
78         button = js.buttons & 1 || js.buttons & 2;
79         while(button == 0 ) {
80                 status = read(joystick_fd, &js, JS_RETURN);
81                 if (status != JS_RETURN) {
82                         perror("js");
83                         return( 1 );
84                 }
85                 button = js.buttons & 1 || js.buttons & 2;
86                 if( js.x > joy_x_dead_max )
87                         joy_x_dead_max = js.x;
88                 if( js.x < joy_x_dead_min )
89                         joy_x_dead_min = js.x;
90                 if( js.y > joy_y_dead_max )
91                         joy_y_dead_max = js.y;
92                 if( js.y < joy_y_dead_min )
93                         joy_y_dead_min = js.y;
94                 
95                 // printf( "Xmin %d Xmax %d Ymin %d Ymax %d", joy_x_dead_min, joy_x_dead_max, \
96                 //              joy_y_dead_min, joy_y_dead_max );
97         }
98         status = read(joystick_fd, &js, JS_RETURN);
99         if (status != JS_RETURN) {
100                 perror("js");
101                 return( 1 );
102         }
103         
104         printf("\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n", joy_x_dead_min, joy_x_dead_max );
105         printf("                      Y_dead_min = %d, Y_dead_max = %d\n", joy_y_dead_min, joy_y_dead_max );
106         
107         sleep( 1 );
108         
109         printf( "\nCenter joystick and press any joystick button.\n" );
110         status = read(joystick_fd, &js, JS_RETURN);
111         if (status != JS_RETURN) {
112                 perror("js");
113                 return( 1 );
114         }
115         button = js.buttons & 1 || js.buttons & 2;
116         while(button == 0 ) {
117                 status = read(joystick_fd, &js, JS_RETURN);
118                 if (status != JS_RETURN) {
119                         perror("js");
120                         return( 1 );
121                 }
122                 button = js.buttons & 1 || js.buttons & 2;
123         }
124         status = read(joystick_fd, &js, JS_RETURN);
125         if (status != JS_RETURN) {
126                 perror("js");
127                 return( 1 );
128         }
129         joy_x_ctr = js.x;
130         joy_y_ctr = js.y;
131         
132         printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
133         
134         sleep( 1 );
135         
136         printf( "\nMove joystick to upper left and press any joystick button.\n" );
137         status = read(joystick_fd, &js, JS_RETURN);
138         if (status != JS_RETURN) {
139                 perror("js");
140                 return( 1 );
141         }
142         button = js.buttons & 1 || js.buttons & 2;
143         while(button == 0 ) {
144                 status = read(joystick_fd, &js, JS_RETURN);
145                 if (status != JS_RETURN) {
146                         perror("js");
147                         return( 1 );
148                 }
149                 button = js.buttons & 1 || js.buttons & 2;
150         }
151         status = read(joystick_fd, &js, JS_RETURN);
152         if (status != JS_RETURN) {
153                 perror("js");
154                 return( 1 );
155         }
156         joy_x_min = js.x;
157         joy_y_min = js.y;
158         printf("Joystick calibration: X_min = %d, Y_min = %d\n", joy_x_min, joy_y_min );
159         
160         sleep( 1 );
161         
162         printf( "\nMove joystick to lower right and press any joystick button.\n" );
163         status = read(joystick_fd, &js, JS_RETURN);
164         if (status != JS_RETURN) {
165                 perror("js");
166                 return( 1 );
167         }
168         button = js.buttons & 1 || js.buttons & 2;
169         while(button == 0 ) {
170                 status = read(joystick_fd, &js, JS_RETURN);
171                 if (status != JS_RETURN) {
172                         perror("js");
173                         return( 1 );
174                 }
175                 button = js.buttons & 1 || js.buttons & 2;
176         }
177         status = read(joystick_fd, &js, JS_RETURN);
178         if (status != JS_RETURN) {
179                 perror("js");
180                 return( 1 );
181         }
182         joy_x_max = js.x;
183         joy_y_max = js.y;
184         
185         printf("Joystick calibration: X_max = %d, Y_max = %d\n", joy_x_max, joy_y_max );
186         
187         // joy_x_ctr = (joy_x_max-joy_x_min)/2;
188         // joy_y_ctr = (joy_y_max-joy_y_min)/2;
189         // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
190         
191         return( joystick_fd );
192 #endif
193 }
194
195 /* void fgJoystickCalibrate( int joy_fd )
196 {
197
198 } */
199
200 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
201 {
202 #ifdef HAVE_JOYSTICK
203         struct JS_DATA_TYPE js;
204         int status;
205         
206         status = read(joystick_fd, &js, JS_RETURN);
207         if (status != JS_RETURN) {
208                 perror("js");
209                 return( 1 );
210         }
211
212         /* printf("\n button 0: %s  button 1: %s  X position: %4d  Y position: %4d\n",
213                  (js.buttons & 1) ? "on " : "off",
214                  (js.buttons & 2) ? "on " : "off",
215                  js.x,
216                  js.y ); */
217                  
218         if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
219                 *joy_x = 0.5;
220         else
221                 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
222         *joy_x = *joy_x*2-1;
223         
224         if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
225                 *joy_y = 0.5;
226         else
227                 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
228         *joy_y = *joy_y*2-1;
229         
230         *joy_b1 = js.buttons & 1;
231         *joy_b2 = js.buttons & 2;
232
233         return( 0 );
234 #endif
235 }
236
237
238 /* $Log$
239 /* Revision 1.2  1997/12/30 20:47:40  curt
240 /* Integrated new event manager with subsystem initializations.
241 /*
242  * Revision 1.1  1997/08/29 18:06:54  curt
243  * Initial revision.
244  *
245  */