1 // fgjs.cxx -- assign joystick axes to flightgear properties
3 // Written by Tony Peden, started May 2001
5 // Copyright (C) 2001 Tony Peden (apeden@earthlink.net)
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.
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.
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.
21 #include <simgear/compiler.h>
31 SG_USING_STD(fstream);
37 string axes_humannames[8] = { "elevator", "ailerons", "rudder", "throttle",
38 "mixture","propller pitch", "lateral view",
42 string axes_propnames[8]={ "/controls/flight/elevator","/controls/flight/aileron",
43 "/controls/flight/rudder","/controls/engines/engine/throttle",
44 "/controls/engines/engine/mixture","/controls/engines/engine/pitch",
45 "/sim/views/axes/lat","/sim/views/axes/long"
48 bool half_range[8]={ false,false,false,true,true,true,false,false };
51 string button_humannames[6]= { "apply left brake",
52 "apply right brake", "step flaps up",
53 "step flaps down","apply nose-up trim",
54 "apply nose-down trim"
57 string button_propnames[6]={ "/controls/gear/brake-left",
58 "/controls/gear/brake-right",
59 "/controls/flight/flaps",
60 "/controls/flight/flaps",
61 "/controls/flight/elevator-trim",
62 "/controls/flight/elevator-trim"
66 float button_step[6]={ 1.0, 1.0, 0.34, -0.34, 0.001, -0.001 };
68 string button_repeat[6]={ "false", "false", "false", "false",
72 void waitForButton(jsSuper *jss, int wait_ms) {
74 float axes[_JS_MAX_AXES];
76 ulMilliSecondSleep(wait_ms);
80 jss->getJoystick()->read ( &b, axes ) ;
81 } while( jss->nextJoystick());
83 ulMilliSecondSleep(1);
86 ulMilliSecondSleep(wait_ms);
89 void writeAxisProperties(fstream &fs, int control,int joystick, int axis) {
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;
96 fs << jsDesc << "/dead-band=0.02" << endl;
98 if( half_range[control] == true) {
99 fs << jsDesc << "/offset=-1.0" << endl;
100 fs << jsDesc << "/factor=-0.5" << endl;
102 fs << jsDesc << "/offset=0.0" << endl;
103 fs << jsDesc << "/factor=1.0" << endl;
108 void writeButtonProperties(fstream &fs, int property,int joystick, int button) {
111 snprintf(jsDesc,80,"--prop:/input/joysticks/js[%d]/button[%d]/binding",joystick,button);
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;
126 jsSuper *jss=new jsSuper();
127 jsInput *jsi=new jsInput(jss);
128 jsi->displayValues(false);
133 cout << "Found " << jss->getNumJoysticks() << " joystick(s)" << endl;
135 if(jss->getNumJoysticks() <= 0) {
136 cout << "Can't find any joysticks ..." << endl;
140 jss->firstJoystick();
142 cout << "Joystick " << jss->getCurrentJoystickId() << " has "
143 << jss->getJoystick()->getNumAxes() << " axes" << endl;
144 } while( jss->nextJoystick() );
146 fstream fs("fgfsrc.js",ios::out);
149 for(control=0;control<=7;control++) {
150 cout << "Move the control you wish to use for " << axes_humannames[control]
155 if(jsi->getInputAxis() != -1) {
156 cout << endl << "Assigned axis " << jsi->getInputAxis()
157 << " on joystick " << jsi->getInputJoystick()
158 << " to control " << axes_humannames[control]
161 writeAxisProperties( fs, control, jsi->getInputJoystick(),
162 jsi->getInputAxis() );
164 cout << "Skipping Axis" << endl;
167 cout << "Press any button for next control" << endl;
169 waitForButton(jss,500);
173 for(control=0;control<=5;control++) {
174 cout << "Press the button you wish to use to "
175 << button_humannames[control]
179 if(jsi->getInputButton() != -1) {
181 cout << endl << "Assigned button " << jsi->getInputButton()
182 << " on joystick " << jsi->getInputJoystick()
183 << " to control " << button_humannames[control]
186 writeButtonProperties( fs, control, jsi->getInputJoystick(),
187 jsi->getInputButton() );
189 cout << "Skipping..." << endl;
192 cout << "Press any button for next axis" << endl;
194 waitForButton(jss,500);
202 cout << "Your joystick settings are in the file fgfsrc.js" << endl;
203 cout << "Check and edit as desired (especially important if you are"
204 << " using a hat switch" << endl;
205 cout << "as this program will, most likely, not get those right). ";
207 cout << "Once you are happy, " << endl
208 << "append its contents to your .fgfsrc or system.fgfsrc" << endl;