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