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