]> git.mxchange.org Git - flightgear.git/blob - src/Input/jsinput.cxx
Clean-up some SGMath dependencies.
[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
25 using std::cout;
26 using std::cin;
27 using std::endl;
28
29 #include "jsinput.h"
30
31 jsInput::jsInput(jsSuper *j) {
32     jss=j;
33     pretty_display=true;
34     joystick=axis=button=-1;
35     axis_threshold=0.2;
36 }
37
38 jsInput::~jsInput(void) {}
39
40 int jsInput::getInput() {
41
42     bool gotit=false;
43
44     float delta;
45     int i, current_button = 0, button_bits = 0;
46
47     joystick=axis=button=-1;
48     axis_positive=false;
49
50     if(pretty_display) {
51         printf ( "+----------------------------------------------\n" ) ;
52         printf ( "| Btns " ) ;
53
54         for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
55             printf ( "Ax:%3d ", i ) ;
56
57         for ( ; i < 8 ; i++ )
58             printf ( "     " ) ;
59
60         printf ( "|\n" ) ;
61
62         printf ( "+----------------------------------------------\n" ) ;
63     }
64
65
66     jss->firstJoystick();
67     do {
68         jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
69                 axes_iv[jss->getCurrentJoystickId()] ) ;
70     } while( jss->nextJoystick() );
71
72
73
74     while(!gotit) {
75         jss->firstJoystick();
76         do {
77
78             jss->getJoystick()->read ( &current_button, axes ) ;
79
80             if(pretty_display) printf ( "| %04x ", current_button ) ;
81
82             for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
83
84                 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
85                 if(pretty_display) printf ( "%+.3f ", delta ) ;
86                 if(!gotit) {
87                     if( fabs(delta) > axis_threshold ) {
88                         gotit=true;
89                         joystick=jss->getCurrentJoystickId();
90                         axis=i;
91                         axis_positive=(delta>0);
92                     } else if( current_button != 0 ) {
93                         gotit=true;
94                         joystick=jss->getCurrentJoystickId();
95                         button_bits=current_button;
96                     }
97                 }
98             }
99
100             if(pretty_display) {
101                 for ( ; i < 8 ; i++ )
102                     printf ( "  .  " ) ;
103             }
104
105
106         } while( jss->nextJoystick() && !gotit);
107         if(pretty_display) {
108             printf ( "|\r" ) ;
109             fflush ( stdout ) ;
110         }
111
112         ulMilliSecondSleep(1);
113     }
114     if(button_bits != 0) {
115         for(int i=0;i<=31;i++) {
116             if( ( button_bits & (1 << i) ) > 0 ) {
117                 button=i;
118                 break;
119             }
120         }
121     }
122
123     return 0;
124 }
125
126 void jsInput::findDeadBand() {
127
128     float delta;
129     int i;
130     float dead_band[MAX_JOYSTICKS][_JS_MAX_AXES];
131
132     jss->firstJoystick();
133     do {
134         jss->getJoystick()->read ( NULL,
135                 axes_iv[jss->getCurrentJoystickId()] ) ;
136         for ( i = 0; i <  jss->getJoystick()->getNumAxes(); i++ ) {
137             dead_band[jss->getCurrentJoystickId()][i] = 0;
138         }
139     } while( jss->nextJoystick() );
140
141     ulClock clock;
142     cout << 10;
143     cout.flush();
144
145     for (int j = 9; j >= 0; j--) {
146         double start_time = clock.getAbsTime();
147         do {
148             jss->firstJoystick();
149             do {
150
151                 jss->getJoystick()->read ( NULL, axes ) ;
152
153                 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
154
155                     delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
156                     if (fabs(delta) > dead_band[jss->getCurrentJoystickId()][i])
157                         dead_band[jss->getCurrentJoystickId()][i] = delta;
158                 }
159
160             } while( jss->nextJoystick());
161
162             ulMilliSecondSleep(1);
163             clock.update();
164         } while (clock.getAbsTime() - start_time < 1.0);
165
166         cout << " - " << j;
167         cout.flush();
168     }
169     cout << endl << endl;
170
171     jss->firstJoystick();
172     do {
173         for ( i = 0; i <  jss->getJoystick()->getNumAxes(); i++ ) {
174             jss->getJoystick()->setDeadBand(i, dead_band[jss->getCurrentJoystickId()][i]);
175             printf("Joystick %i, axis %i: %f\n", jss->getCurrentJoystickId(), i, dead_band[jss->getCurrentJoystickId()][i]);
176         }
177     } while( jss->nextJoystick() );
178 }