]> git.mxchange.org Git - flightgear.git/blob - src/Input/fgjs.cxx
e9ed0aa72639f3f77482279780617f0cdf87f9f0
[flightgear.git] / src / Input / fgjs.cxx
1 // fgjs.cxx -- assign joystick axes to flightgear properties
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include <simgear/compiler.h>
22
23 #include <math.h>
24
25 #include STL_IOSTREAM
26 #include STL_FSTREAM
27 #include STL_STRING
28
29 #include <jssuper.h>
30 #include <jsinput.h>
31
32 SG_USING_STD(string);
33
34
35 string axes_humannames[8] = { "elevator", "ailerons", "rudder", "throttle", 
36                               "mixture","propller pitch", "lateral view", 
37                               "longitudinal view" 
38                             };
39
40 string axes_propnames[8]={ "/controls/elevator","/controls/aileron",
41                            "/controls/rudder","/controls/throttle",
42                            "/controls/mixture","/controls/pitch", 
43                            "/sim/views/axes/lat","/sim/views/axes/long" 
44                          };
45                       
46 bool half_range[8]={ false,false,false,true,true,true,false,false };
47
48
49 string button_humannames[7]= { "apply all brakes", "apply left brake", 
50                                "apply right brake", "step flaps up", 
51                                "step flaps down","apply nose-up trim",
52                                "apply nose-down trim"
53                              }; 
54
55 string button_propnames[7]={ "/controls/brakes/all", "/controls/brakes[0]",
56                              "/controls/brakes[1]", "/controls/flaps",
57                              "/controls/flaps","/controls/elevator-trim",
58                              "/controls/elevator-trim" 
59                            };                                                   
60  
61
62 float button_step[7]={ 1.0, 1.0, 1.0, 0.34, -0.34, 0.001, -0.001 };
63
64 string button_repeat[7]={ "false", "false", "false", "false", "false", 
65                           "true", "true" };
66
67
68 void waitForButton(jsSuper *jss, int wait_ms) {
69       int b,lastb;
70       float axes[_JS_MAX_AXES];
71       b=0;
72       ulMilliSecondSleep(wait_ms);
73       do {
74         lastb=b;
75         do {
76           jss->getJoystick()->read ( &b, axes ) ;
77         } while( jss->nextJoystick()); 
78         
79         ulMilliSecondSleep(1); 
80  
81       }while( lastb == b ); 
82       ulMilliSecondSleep(wait_ms);
83 }
84
85 void writeAxisProperties(fstream &fs, int control,int joystick, int axis) {
86      
87      char jsDesc[40];
88      snprintf(jsDesc,40,"--prop:/input/joysticks/js[%d]/axis[%d]",joystick,axis);
89      fs << jsDesc  << "/control=" << axes_propnames[control] << endl; 
90      
91      fs << jsDesc << "/dead-band=0.02"  << endl; 
92      
93      if( half_range[control] == true) {
94        fs << jsDesc << "/offset=-1.0" << endl; 
95        fs << jsDesc << "/factor=0.5" << endl;
96      } else {
97        fs << jsDesc << "/offset=0.0" << endl; 
98        fs << jsDesc << "/factor=1.0" << endl;
99      }
100      fs << endl;
101
102
103 void writeButtonProperties(fstream &fs, int property,int joystick, int button) {
104      
105      char jsDesc[40];
106      snprintf(jsDesc,40,"--prop:/input/joysticks/js[%d]/button[%d]",joystick,button);
107      
108      fs << jsDesc << "/action=adjust" << endl; 
109      fs << jsDesc << "/control=" << button_propnames[property] << endl;
110      fs << jsDesc << "/step=" << button_step[property] << endl; 
111      fs << jsDesc << "/repeatable=" << button_repeat[property] << endl;
112      fs << endl; 
113 }    
114
115         
116
117       
118 int main(void) {
119   jsSuper *jss=new jsSuper();
120   jsInput *jsi=new jsInput(jss);
121   jsi->displayValues(false);
122   int i;
123   int control=0;
124   
125   
126   cout << "Found " << jss->getNumJoysticks() << " joystick(s)" << endl;
127   
128   if(jss->getNumJoysticks() <= 0) { 
129     cout << "Can't find any joysticks ..." << endl;
130     exit(1);
131   }  
132   
133   jss->firstJoystick();
134   do {
135     cout << "Joystick " << jss->getCurrentJoystickId() << " has "
136          << jss->getJoystick()->getNumAxes() << " axes" << endl;
137   } while( jss->nextJoystick() ); 
138   
139   fstream fs("fgfsrc.js",ios::out);
140
141   
142   for(control=0;control<=7;control++) {
143       cout << "Move the control you wish to use for " << axes_humannames[control]
144            << endl;
145       fflush( stdout );
146       jsi->getInput();
147       
148       if(jsi->getInputAxis() != -1) {
149          cout << endl << "Assigned axis " << jsi->getInputAxis() 
150               << " on joystick " << jsi->getInputJoystick() 
151               << " to control " << axes_humannames[control]
152               << endl;
153       
154           writeAxisProperties( fs, control, jsi->getInputJoystick(), 
155                                jsi->getInputAxis() ); 
156       } else {
157           cout << "Skipping Axis" << endl;
158       }           
159       
160       cout << "Press any button for next control" << endl;
161       
162       waitForButton(jss,500);
163       cout << endl;
164   }
165   
166   for(control=0;control<=6;control++) {
167       cout << "Press the button you wish to use to " 
168            << button_humannames[control]
169            << endl;
170       fflush( stdout );
171       jsi->getInput();
172       if(jsi->getInputButton() != -1) {
173          
174          cout << endl << "Assigned button " << jsi->getInputButton() 
175               << " on joystick " << jsi->getInputJoystick() 
176               << " to control " << button_humannames[control]
177               << endl;
178       
179           writeButtonProperties( fs, control, jsi->getInputJoystick(), 
180                                jsi->getInputButton() ); 
181       } else {
182           cout << "Skipping..." << endl;
183       }           
184       
185       cout << "Press any button for next axis" << endl;
186       
187       waitForButton(jss,500);
188       cout << endl;
189   }
190  
191
192   delete jsi;
193   delete jss;
194       
195   cout << "Your joystick settings are in the file fgfsrc.js" << endl;
196   cout << "Check and edit as desired (especially important if you are"
197          << " using a hat switch" << endl;
198   cout << "as this program will, most likely, not get those right).  ";        
199   
200   cout << "Once you are happy, " << endl 
201        << "append its contents to your .fgfsrc or system.fgfsrc" << endl;     
202      
203   return 1;
204 }