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/elevator","/controls/aileron",
43 "/controls/rudder","/controls/throttle",
44 "/controls/mixture","/controls/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[7]= { "apply all brakes", "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[7]={ "/controls/brakes/all", "/controls/brakes[0]",
58 "/controls/brakes[1]", "/controls/flaps",
59 "/controls/flaps","/controls/elevator-trim",
60 "/controls/elevator-trim"
64 float button_step[7]={ 1.0, 1.0, 1.0, 0.34, -0.34, 0.001, -0.001 };
66 string button_repeat[7]={ "false", "false", "false", "false", "false",
70 void waitForButton(jsSuper *jss, int wait_ms) {
72 float axes[_JS_MAX_AXES];
74 ulMilliSecondSleep(wait_ms);
78 jss->getJoystick()->read ( &b, axes ) ;
79 } while( jss->nextJoystick());
81 ulMilliSecondSleep(1);
84 ulMilliSecondSleep(wait_ms);
87 void writeAxisProperties(fstream &fs, int control,int joystick, int axis) {
90 snprintf(jsDesc,80,"--prop:/input/joysticks/js[%d]/axis[%d]/binding",joystick,axis);
91 fs << jsDesc << "/command=property-scale" << endl;
92 fs << jsDesc << "/property=" << axes_propnames[control] << endl;
94 fs << jsDesc << "/dead-band=0.02" << endl;
96 if( half_range[control] == true) {
97 fs << jsDesc << "/offset=-1.0" << endl;
98 fs << jsDesc << "/factor=-0.5" << endl;
100 fs << jsDesc << "/offset=0.0" << endl;
101 fs << jsDesc << "/factor=1.0" << endl;
106 void writeButtonProperties(fstream &fs, int property,int joystick, int button) {
109 snprintf(jsDesc,80,"--prop:/input/joysticks/js[%d]/button[%d]/binding",joystick,button);
111 fs << jsDesc << "/repeatable=" << button_repeat[property] << endl;
112 fs << jsDesc << "/command=property-adjust" << endl;
113 fs << jsDesc << "/property=" << button_propnames[property] << endl;
114 fs << jsDesc << "/step=" << button_step[property] << endl;
122 jsSuper *jss=new jsSuper();
123 jsInput *jsi=new jsInput(jss);
124 jsi->displayValues(false);
129 cout << "Found " << jss->getNumJoysticks() << " joystick(s)" << endl;
131 if(jss->getNumJoysticks() <= 0) {
132 cout << "Can't find any joysticks ..." << endl;
136 jss->firstJoystick();
138 cout << "Joystick " << jss->getCurrentJoystickId() << " has "
139 << jss->getJoystick()->getNumAxes() << " axes" << endl;
140 } while( jss->nextJoystick() );
142 fstream fs("fgfsrc.js",ios::out);
145 for(control=0;control<=7;control++) {
146 cout << "Move the control you wish to use for " << axes_humannames[control]
151 if(jsi->getInputAxis() != -1) {
152 cout << endl << "Assigned axis " << jsi->getInputAxis()
153 << " on joystick " << jsi->getInputJoystick()
154 << " to control " << axes_humannames[control]
157 writeAxisProperties( fs, control, jsi->getInputJoystick(),
158 jsi->getInputAxis() );
160 cout << "Skipping Axis" << endl;
163 cout << "Press any button for next control" << endl;
165 waitForButton(jss,500);
169 for(control=0;control<=6;control++) {
170 cout << "Press the button you wish to use to "
171 << button_humannames[control]
175 if(jsi->getInputButton() != -1) {
177 cout << endl << "Assigned button " << jsi->getInputButton()
178 << " on joystick " << jsi->getInputJoystick()
179 << " to control " << button_humannames[control]
182 writeButtonProperties( fs, control, jsi->getInputJoystick(),
183 jsi->getInputButton() );
185 cout << "Skipping..." << endl;
188 cout << "Press any button for next axis" << endl;
190 waitForButton(jss,500);
198 cout << "Your joystick settings are in the file fgfsrc.js" << endl;
199 cout << "Check and edit as desired (especially important if you are"
200 << " using a hat switch" << endl;
201 cout << "as this program will, most likely, not get those right). ";
203 cout << "Once you are happy, " << endl
204 << "append its contents to your .fgfsrc or system.fgfsrc" << endl;