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