]> git.mxchange.org Git - flightgear.git/blob - src/Input/jsinput.cxx
Olaf Flebbe: incorporate plib js code to fix problems with MS joysticks under Vista...
[flightgear.git] / src / Input / jsinput.cxx
1 // jsinput.cxx -- wait for and identify input from joystick
2 //
3 // Written by Tony Peden, started May 2001
4 //
5 // Copyright (C) 2001  Tony Peden (apeden@earthlink.net)
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #include <simgear/compiler.h>
22
23 #include <iostream>
24 #include <math.h>
25 using std::cout;
26 using std::cin;
27 using std::endl;
28
29 #include "jsinput.h"
30 #include <plib/ul.h>
31
32 jsInput::jsInput(jsSuper *j) {
33     jss=j;
34     pretty_display=true;
35     joystick=axis=button=-1;
36     axis_threshold=0.2;
37 }
38
39 jsInput::~jsInput(void) {}
40
41 int jsInput::getInput() {
42
43     bool gotit=false;
44
45     float delta;
46     int i, current_button = 0, button_bits = 0;
47
48     joystick=axis=button=-1;
49     axis_positive=false;
50
51     if(pretty_display) {
52         printf ( "+----------------------------------------------\n" ) ;
53         printf ( "| Btns " ) ;
54
55         for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
56             printf ( "Ax:%3d ", i ) ;
57
58         for ( ; i < 8 ; i++ )
59             printf ( "     " ) ;
60
61         printf ( "|\n" ) ;
62
63         printf ( "+----------------------------------------------\n" ) ;
64     }
65
66
67     jss->firstJoystick();
68     do {
69         jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
70                 axes_iv[jss->getCurrentJoystickId()] ) ;
71     } while( jss->nextJoystick() );
72
73
74
75     while(!gotit) {
76         jss->firstJoystick();
77         do {
78
79             jss->getJoystick()->read ( &current_button, axes ) ;
80
81             if(pretty_display) printf ( "| %04x ", current_button ) ;
82
83             for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
84
85                 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
86                 if(pretty_display) printf ( "%+.3f ", delta ) ;
87                 if(!gotit) {
88                     if( fabs(delta) > axis_threshold ) {
89                         gotit=true;
90                         joystick=jss->getCurrentJoystickId();
91                         axis=i;
92                         axis_positive=(delta>0);
93                     } else if( current_button != 0 ) {
94                         gotit=true;
95                         joystick=jss->getCurrentJoystickId();
96                         button_bits=current_button;
97                     }
98                 }
99             }
100
101             if(pretty_display) {
102                 for ( ; i < 8 ; i++ )
103                     printf ( "  .  " ) ;
104             }
105
106
107         } while( jss->nextJoystick() && !gotit);
108         if(pretty_display) {
109             printf ( "|\r" ) ;
110             fflush ( stdout ) ;
111         }
112
113         ulMilliSecondSleep(1);
114     }
115     if(button_bits != 0) {
116         for(int i=0;i<=31;i++) {
117             if( ( button_bits & (1 << i) ) > 0 ) {
118                 button=i;
119                 break;
120             }
121         }
122     }
123
124     return 0;
125 }
126
127 void jsInput::findDeadBand() {
128
129     float delta;
130     int i;
131     float dead_band[MAX_JOYSTICKS][_JS_MAX_AXES];
132
133     jss->firstJoystick();
134     do {
135         jss->getJoystick()->read ( NULL,
136                 axes_iv[jss->getCurrentJoystickId()] ) ;
137         for ( i = 0; i <  jss->getJoystick()->getNumAxes(); i++ ) {
138             dead_band[jss->getCurrentJoystickId()][i] = 0;
139         }
140     } while( jss->nextJoystick() );
141
142     ulClock clock;
143     cout << 10;
144     cout.flush();
145
146     for (int j = 9; j >= 0; j--) {
147         double start_time = clock.getAbsTime();
148         do {
149             jss->firstJoystick();
150             do {
151
152                 jss->getJoystick()->read ( NULL, axes ) ;
153
154                 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
155
156                     delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
157                     if (fabs(delta) > dead_band[jss->getCurrentJoystickId()][i])
158                         dead_band[jss->getCurrentJoystickId()][i] = delta;
159                 }
160
161             } while( jss->nextJoystick());
162
163             ulMilliSecondSleep(1);
164             clock.update();
165         } while (clock.getAbsTime() - start_time < 1.0);
166
167         cout << " - " << j;
168         cout.flush();
169     }
170     cout << endl << endl;
171
172     jss->firstJoystick();
173     do {
174         for ( i = 0; i <  jss->getJoystick()->getNumAxes(); i++ ) {
175             jss->getJoystick()->setDeadBand(i, dead_band[jss->getCurrentJoystickId()][i]);
176             printf("Joystick %i, axis %i: %f\n", jss->getCurrentJoystickId(), i, dead_band[jss->getCurrentJoystickId()][i]);
177         }
178     } while( jss->nextJoystick() );
179 }