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