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