1 // input.cxx -- handle user input from various sources.
3 // Written by David Megginson, started May 2001.
5 // Copyright (C) 2001 David Megginson, david@megginson.com
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.
31 #include <simgear/compiler.h>
44 #include <simgear/compiler.h>
46 #include <simgear/constants.h>
47 #include <simgear/debug/logstream.hxx>
48 #include <simgear/misc/props.hxx>
50 #include <Aircraft/aircraft.hxx>
51 #include <Autopilot/auto_gui.hxx>
52 #include <Autopilot/newauto.hxx>
53 #include <Cockpit/hud.hxx>
54 #include <Cockpit/panel.hxx>
55 #include <Cockpit/panel_io.hxx>
58 #ifndef FG_OLD_WEATHER
59 # include <WeatherCM/FGLocalWeatherDatabase.h>
61 # include <Weather/weather.hxx>
64 #include <Main/globals.hxx>
65 #include <Main/fg_props.hxx>
66 #include <Main/options.hxx>
70 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
71 SG_USING_STD(ifstream);
78 ////////////////////////////////////////////////////////////////////////
79 // Local data structures.
80 ////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////
84 // Implementation of FGBinding.
85 ////////////////////////////////////////////////////////////////////////
87 FGBinding::FGBinding ()
89 _arg(new SGPropertyNode),
95 FGBinding::FGBinding (const SGPropertyNode * node)
97 _arg(new SGPropertyNode),
104 FGBinding::~FGBinding ()
106 delete _arg; // Delete the saved arguments
107 delete _command_state; // Delete the saved command state
111 FGBinding::read (const SGPropertyNode * node)
113 const SGPropertyNode * conditionNode = node->getChild("condition");
114 if (conditionNode != 0) {
115 cerr << "Adding condition to binding" << endl;
116 setCondition(fgReadCondition(conditionNode));
119 _command_name = node->getStringValue("command", "");
120 if (_command_name == "") {
121 SG_LOG(SG_INPUT, SG_ALERT, "No command supplied for binding.");
126 _command = globals->get_commands()->getCommand(_command_name);
128 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
134 _arg = new SGPropertyNode;
136 copyProperties(node, _arg); // FIXME: don't use whole node!!!
140 FGBinding::fire () const
144 SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
145 } else if (!(*_command)(_arg, &_command_state)) {
146 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
153 FGBinding::fire (double setting) const
156 // A value is automatically added to
158 if (_setting == 0) // save the setting node for efficiency
159 _setting = _arg->getChild("setting", 0, true);
160 _setting->setDoubleValue(setting);
167 ////////////////////////////////////////////////////////////////////////
168 // Implementation of FGInput.
169 ////////////////////////////////////////////////////////////////////////
172 extern void fgReshape( int width, int height );
174 FGInput current_input;
214 FGInput::doKey (int k, int modifiers, int x, int y)
216 // SG_LOG( SG_INPUT, SG_INFO, "User pressed key " << k
217 // << " with modifiers " << modifiers );
220 if (k < 0 || k >= MAX_KEYS) {
221 SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
225 button &b = _key_bindings[k];
228 if (modifiers&FG_MOD_UP == 0) {
229 // SG_LOG( SG_INPUT, SG_INFO, "User pressed key " << k
230 // << " with modifiers " << modifiers );
231 if (!b.last_state || b.is_repeatable) {
232 const binding_list_t &bindings =
233 _find_key_bindings(k, modifiers);
234 int max = bindings.size();
236 for (int i = 0; i < max; i++)
245 // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
246 // << " with modifiers " << modifiers);
248 const binding_list_t &bindings =
249 _find_key_bindings(k, modifiers);
250 int max = bindings.size();
252 for (int i = 0; i < max; i++)
260 // Use the old, default actions.
261 // SG_LOG( SG_INPUT, SG_INFO, "(No user binding.)" );
262 if (modifiers&FG_MOD_UP)
265 // everything after here will be removed sooner or later...
267 if (modifiers & FG_MOD_SHIFT) {
271 HUD_brightkey( true );
275 fgHUDInit2(¤t_aircraft);
281 SG_LOG( SG_INPUT, SG_DEBUG, "" );
284 HUD_masterswitch( true );
287 fgHUDInit(¤t_aircraft); // normal HUD
292 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
293 if ( current_autopilot->get_HeadingMode() !=
294 FGAutopilot::FG_HEADING_WAYPOINT ) {
295 current_autopilot->set_HeadingMode(
296 FGAutopilot::FG_HEADING_WAYPOINT );
297 current_autopilot->set_HeadingEnabled( true );
299 current_autopilot->set_HeadingMode(
300 FGAutopilot::FG_TC_HEADING_LOCK );
303 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
304 const string &fog = fgGetString("/sim/rendering/fog");
305 if (fog == "disabled") {
306 fgSetString("/sim/rendering/fog", "fastest");
307 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
308 } else if (fog == "fastest") {
309 fgSetString("/sim/rendering/fog", "nicest");
310 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
311 } else if (fog == "nicest") {
312 fgSetString("/sim/rendering/fog", "disabled");
313 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
315 fgSetString("/sim/rendering/fog", "disabled");
316 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
317 << fog << ", changed to 'disabled'");
321 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
322 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
325 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
326 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
329 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
330 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
342 FGInput::_init_keyboard ()
344 // TODO: zero the old bindings first.
345 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
346 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
347 if (key_nodes == 0) {
348 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
352 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
353 for (unsigned int i = 0; i < keys.size(); i++) {
354 int index = keys[i]->getIndex();
355 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
356 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
357 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
363 FGInput::_init_joystick ()
365 // TODO: zero the old bindings first.
366 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
367 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
369 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
373 for (int i = 0; i < MAX_JOYSTICKS; i++) {
374 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
376 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
379 jsJoystick * js = new jsJoystick(i);
380 _joystick_bindings[i].js = js;
381 if (js->notWorking()) {
382 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
387 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
388 int nbuttons = jsCaps.wNumButtons;
389 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
391 int nbuttons = MAX_BUTTONS;
394 int naxes = js->getNumAxes();
395 if (naxes > MAX_AXES) naxes = MAX_AXES;
396 _joystick_bindings[i].naxes = naxes;
397 _joystick_bindings[i].nbuttons = nbuttons;
399 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
401 // Set up range arrays
402 float minRange[MAX_AXES];
403 float maxRange[MAX_AXES];
404 float center[MAX_AXES];
406 // Initialize with default values
407 js->getMinRange(minRange);
408 js->getMaxRange(maxRange);
409 js->getCenter(center);
411 // Allocate axes and buttons
412 _joystick_bindings[i].axes = new axis[naxes];
413 _joystick_bindings[i].buttons = new button[nbuttons];
417 // Initialize the axes.
420 for (j = 0; j < naxes; j++) {
421 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
422 if (axis_node == 0) {
423 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
427 axis &a = _joystick_bindings[i].axes[j];
429 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
431 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
432 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
433 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
434 center[j] = axis_node->getDoubleValue("center", center[j]);
436 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
438 // Initialize the virtual axis buttons.
439 _init_button(axis_node->getChild("low"), a.low, "low");
440 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
442 _init_button(axis_node->getChild("high"), a.high, "high");
443 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
447 // Initialize the buttons.
450 for (j = 0; j < nbuttons; j++) {
451 sprintf(buf, "%d", j);
452 SG_LOG(SG_INPUT, SG_INFO, "Initializing button " << j);
453 _init_button(js_node->getChild("button", j),
454 _joystick_bindings[i].buttons[j],
459 js->setMinRange(minRange);
460 js->setMaxRange(maxRange);
461 js->setCenter(center);
467 FGInput::_init_button (const SGPropertyNode * node,
472 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << name);
474 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
476 // Get the bindings for the button
477 _read_bindings(node, b.bindings, FG_MOD_NONE);
483 FGInput::_update_keyboard ()
490 FGInput::_update_joystick ()
492 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
494 // float js_val, diff;
495 float axis_values[MAX_AXES];
500 for ( i = 0; i < MAX_JOYSTICKS; i++) {
502 jsJoystick * js = _joystick_bindings[i].js;
503 if (js == 0 || js->notWorking())
506 js->read(&buttons, axis_values);
509 // Fire bindings for the axes.
510 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
511 axis &a = _joystick_bindings[i].axes[j];
513 // Do nothing if the axis position
514 // is unchanged; only a change in
515 // position fires the bindings.
516 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
517 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
519 a.last_value = axis_values[j];
520 // SG_LOG(SG_INPUT, SG_INFO, "There are "
521 // << a.bindings[modifiers].size() << " bindings");
522 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
523 a.bindings[modifiers][k]->fire(axis_values[j]);
526 // do we have to emulate axis buttons?
527 if (a.low.bindings[modifiers].size())
528 _update_button(_joystick_bindings[i].axes[j].low,
530 axis_values[j] < a.low_threshold);
532 if (a.high.bindings[modifiers].size())
533 _update_button(_joystick_bindings[i].axes[j].high,
535 axis_values[j] > a.high_threshold);
538 // Fire bindings for the buttons.
539 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
540 _update_button(_joystick_bindings[i].buttons[j],
542 (buttons & (1 << j)) > 0);
549 FGInput::_update_button (button &b, int modifiers, bool pressed)
552 // The press event may be repeated.
553 if (!b.last_state || b.is_repeatable) {
554 // SG_LOG( SG_INPUT, SG_INFO, "Button has been pressed" );
555 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
556 b.bindings[modifiers][k]->fire();
559 // The release event is never repeated.
561 // SG_LOG( SG_INPUT, SG_INFO, "Button has been released" );
562 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
563 b.bindings[modifiers|FG_MOD_UP][k]->fire();
567 b.last_state = pressed;
572 FGInput::_read_bindings (const SGPropertyNode * node,
573 binding_list_t * binding_list,
576 SG_LOG(SG_INPUT, SG_INFO, "Reading all bindings");
577 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
578 for (unsigned int i = 0; i < bindings.size(); i++) {
579 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
580 << bindings[i]->getStringValue("command"));
581 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
584 // Read nested bindings for modifiers
585 if (node->getChild("mod-up") != 0)
586 _read_bindings(node->getChild("mod-up"), binding_list,
587 modifiers|FG_MOD_UP);
589 if (node->getChild("mod-shift") != 0)
590 _read_bindings(node->getChild("mod-shift"), binding_list,
591 modifiers|FG_MOD_SHIFT);
593 if (node->getChild("mod-ctrl") != 0)
594 _read_bindings(node->getChild("mod-ctrl"), binding_list,
595 modifiers|FG_MOD_CTRL);
597 if (node->getChild("mod-alt") != 0)
598 _read_bindings(node->getChild("mod-alt"), binding_list,
599 modifiers|FG_MOD_ALT);
603 const vector<FGBinding *> &
604 FGInput::_find_key_bindings (unsigned int k, int modifiers)
606 button &b = _key_bindings[k];
608 // Try it straight, first.
609 if (b.bindings[modifiers].size() > 0)
610 return b.bindings[modifiers];
612 // Try removing the control modifier
614 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
615 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
617 // Try removing shift modifier
618 // for upper case or any punctuation
619 // (since different keyboards will
620 // shift different punctuation types)
621 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
622 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
624 // Try removing alt modifier for
625 // high-bit characters.
626 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
627 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
629 // Give up and return the empty vector.
631 return b.bindings[modifiers];
636 ////////////////////////////////////////////////////////////////////////
637 // Implementation of FGInput::button.
638 ////////////////////////////////////////////////////////////////////////
640 FGInput::button::button ()
641 : is_repeatable(false),
646 FGInput::button::~button ()
648 // FIXME: memory leak
649 // for (int i = 0; i < FG_MOD_MAX; i++)
650 // for (int j = 0; i < bindings[i].size(); j++)
651 // delete bindings[i][j];
656 ////////////////////////////////////////////////////////////////////////
657 // Implementation of FGInput::axis.
658 ////////////////////////////////////////////////////////////////////////
660 FGInput::axis::axis ()
661 : last_value(9999999),
668 FGInput::axis::~axis ()
670 for (int i = 0; i < FG_MOD_MAX; i++)
671 for (unsigned int j = 0; i < bindings[i].size(); j++)
672 delete bindings[i][j];
677 ////////////////////////////////////////////////////////////////////////
678 // Implementation of FGInput::joystick.
679 ////////////////////////////////////////////////////////////////////////
681 FGInput::joystick::joystick ()
685 FGInput::joystick::~joystick ()
694 ////////////////////////////////////////////////////////////////////////
695 // Implementation of GLUT callbacks.
696 ////////////////////////////////////////////////////////////////////////
700 * Construct the modifiers.
702 static inline int get_mods ()
704 int glut_modifiers = glutGetModifiers();
707 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
708 modifiers |= FGInput::FG_MOD_SHIFT;
709 if (glut_modifiers & GLUT_ACTIVE_CTRL)
710 modifiers |= FGInput::FG_MOD_CTRL;
711 if (glut_modifiers & GLUT_ACTIVE_ALT)
712 modifiers |= FGInput::FG_MOD_ALT;
719 * Key-down event handler for Glut.
721 * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
723 * @param k The integer value for the key pressed.
727 void GLUTkey(unsigned char k, int x, int y)
729 // Give PUI a chance to grab it first.
730 if (!puKeyboard(k, PU_DOWN))
731 current_input.doKey(k, get_mods(), x, y);
736 * Key-up event handler for GLUT.
738 * <p>PUI doesn't use this, so always pass it to the input manager.</p>
740 * @param k The integer value for the key pressed.
744 void GLUTkeyup(unsigned char k, int x, int y)
746 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
751 * Special key-down handler for Glut.
753 * <p>Pass the value on to the FGInput module unless PUI wants it.
754 * The key value will have 256 added to it.</p>
756 * @param k The integer value for the key pressed (will have 256 added
761 void GLUTspecialkey(int k, int x, int y)
763 // Give PUI a chance to grab it first.
764 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
765 current_input.doKey(k + 256, get_mods(), x, y);
770 * Special key-up handler for Glut.
772 * @param k The integer value for the key pressed (will have 256 added
777 void GLUTspecialkeyup(int k, int x, int y)
779 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);