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 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
63 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
64 SG_USING_STD(ifstream);
71 ////////////////////////////////////////////////////////////////////////
72 // Local data structures.
73 ////////////////////////////////////////////////////////////////////////
76 ////////////////////////////////////////////////////////////////////////
77 // Implementation of FGBinding.
78 ////////////////////////////////////////////////////////////////////////
80 FGBinding::FGBinding ()
82 _arg(new SGPropertyNode),
88 FGBinding::FGBinding (const SGPropertyNode * node)
90 _arg(new SGPropertyNode),
97 FGBinding::~FGBinding ()
99 // delete _arg; // Delete the saved arguments
100 // delete _command_state; // Delete the saved command state
104 FGBinding::read (const SGPropertyNode * node)
106 const SGPropertyNode * conditionNode = node->getChild("condition");
107 if (conditionNode != 0) {
108 cerr << "Adding condition to binding" << endl;
109 setCondition(fgReadCondition(conditionNode));
112 _command_name = node->getStringValue("command", "");
113 if (_command_name == "") {
114 SG_LOG(SG_INPUT, SG_ALERT, "No command supplied for binding.");
119 _command = globals->get_commands()->getCommand(_command_name);
121 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
127 _arg = new SGPropertyNode;
129 copyProperties(node, _arg); // FIXME: don't use whole node!!!
133 FGBinding::fire () const
137 SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
138 } else if (!(*_command)(_arg, &_command_state)) {
139 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
146 FGBinding::fire (double setting) const
149 // A value is automatically added to
151 if (_setting == 0) // save the setting node for efficiency
152 _setting = _arg->getChild("setting", 0, true);
153 _setting->setDoubleValue(setting);
160 ////////////////////////////////////////////////////////////////////////
161 // Implementation of FGInput.
162 ////////////////////////////////////////////////////////////////////////
165 extern void fgReshape( int width, int height );
167 FGInput current_input;
200 FGInput::update (int dt)
207 FGInput::doKey (int k, int modifiers, int x, int y)
209 // SG_LOG( SG_INPUT, SG_INFO, "User pressed key " << k
210 // << " with modifiers " << modifiers );
213 if (k < 0 || k >= MAX_KEYS) {
214 SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
218 button &b = _key_bindings[k];
221 if (modifiers&FG_MOD_UP == 0) {
222 // SG_LOG( SG_INPUT, SG_INFO, "User pressed key " << k
223 // << " with modifiers " << modifiers );
224 if (!b.last_state || b.is_repeatable) {
225 const binding_list_t &bindings =
226 _find_key_bindings(k, modifiers);
227 int max = bindings.size();
229 for (int i = 0; i < max; i++)
238 // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
239 // << " with modifiers " << modifiers);
241 const binding_list_t &bindings =
242 _find_key_bindings(k, modifiers);
243 int max = bindings.size();
245 for (int i = 0; i < max; i++)
253 // Use the old, default actions.
254 // SG_LOG( SG_INPUT, SG_INFO, "(No user binding.)" );
255 if (modifiers&FG_MOD_UP)
258 // everything after here will be removed sooner or later...
260 if (modifiers & FG_MOD_SHIFT) {
264 HUD_brightkey( true );
268 fgHUDInit2(¤t_aircraft);
274 SG_LOG( SG_INPUT, SG_DEBUG, "" );
277 HUD_masterswitch( true );
280 fgHUDInit(¤t_aircraft); // normal HUD
285 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
286 if ( current_autopilot->get_HeadingMode() !=
287 FGAutopilot::FG_HEADING_WAYPOINT ) {
288 current_autopilot->set_HeadingMode(
289 FGAutopilot::FG_HEADING_WAYPOINT );
290 current_autopilot->set_HeadingEnabled( true );
292 current_autopilot->set_HeadingMode(
293 FGAutopilot::FG_TC_HEADING_LOCK );
296 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
297 const string &fog = fgGetString("/sim/rendering/fog");
298 if (fog == "disabled") {
299 fgSetString("/sim/rendering/fog", "fastest");
300 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
301 } else if (fog == "fastest") {
302 fgSetString("/sim/rendering/fog", "nicest");
303 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
304 } else if (fog == "nicest") {
305 fgSetString("/sim/rendering/fog", "disabled");
306 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
308 fgSetString("/sim/rendering/fog", "disabled");
309 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
310 << fog << ", changed to 'disabled'");
314 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
315 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
318 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
319 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
322 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
323 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
335 FGInput::_init_keyboard ()
337 // TODO: zero the old bindings first.
338 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
339 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
340 if (key_nodes == 0) {
341 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
345 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
346 for (unsigned int i = 0; i < keys.size(); i++) {
347 int index = keys[i]->getIndex();
348 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
349 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
350 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
356 FGInput::_init_joystick ()
358 // TODO: zero the old bindings first.
359 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
360 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
362 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
366 for (int i = 0; i < MAX_JOYSTICKS; i++) {
367 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
369 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
372 jsJoystick * js = new jsJoystick(i);
373 _joystick_bindings[i].js = js;
374 if (js->notWorking()) {
375 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
380 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
381 int nbuttons = jsCaps.wNumButtons;
382 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
384 int nbuttons = MAX_BUTTONS;
387 int naxes = js->getNumAxes();
388 if (naxes > MAX_AXES) naxes = MAX_AXES;
389 _joystick_bindings[i].naxes = naxes;
390 _joystick_bindings[i].nbuttons = nbuttons;
392 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
394 // Set up range arrays
395 float minRange[MAX_AXES];
396 float maxRange[MAX_AXES];
397 float center[MAX_AXES];
399 // Initialize with default values
400 js->getMinRange(minRange);
401 js->getMaxRange(maxRange);
402 js->getCenter(center);
404 // Allocate axes and buttons
405 _joystick_bindings[i].axes = new axis[naxes];
406 _joystick_bindings[i].buttons = new button[nbuttons];
410 // Initialize the axes.
413 for (j = 0; j < naxes; j++) {
414 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
415 if (axis_node == 0) {
416 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
420 axis &a = _joystick_bindings[i].axes[j];
422 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
424 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
425 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
426 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
427 center[j] = axis_node->getDoubleValue("center", center[j]);
429 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
431 // Initialize the virtual axis buttons.
432 _init_button(axis_node->getChild("low"), a.low, "low");
433 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
435 _init_button(axis_node->getChild("high"), a.high, "high");
436 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
440 // Initialize the buttons.
443 for (j = 0; j < nbuttons; j++) {
444 sprintf(buf, "%d", j);
445 SG_LOG(SG_INPUT, SG_INFO, "Initializing button " << j);
446 _init_button(js_node->getChild("button", j),
447 _joystick_bindings[i].buttons[j],
452 js->setMinRange(minRange);
453 js->setMaxRange(maxRange);
454 js->setCenter(center);
460 FGInput::_init_button (const SGPropertyNode * node,
465 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << name);
467 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
469 // Get the bindings for the button
470 _read_bindings(node, b.bindings, FG_MOD_NONE);
476 FGInput::_update_keyboard ()
483 FGInput::_update_joystick ()
485 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
487 // float js_val, diff;
488 float axis_values[MAX_AXES];
493 for ( i = 0; i < MAX_JOYSTICKS; i++) {
495 jsJoystick * js = _joystick_bindings[i].js;
496 if (js == 0 || js->notWorking())
499 js->read(&buttons, axis_values);
502 // Fire bindings for the axes.
503 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
504 axis &a = _joystick_bindings[i].axes[j];
506 // Do nothing if the axis position
507 // is unchanged; only a change in
508 // position fires the bindings.
509 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
510 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
512 a.last_value = axis_values[j];
513 // SG_LOG(SG_INPUT, SG_INFO, "There are "
514 // << a.bindings[modifiers].size() << " bindings");
515 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
516 a.bindings[modifiers][k]->fire(axis_values[j]);
519 // do we have to emulate axis buttons?
520 if (a.low.bindings[modifiers].size())
521 _update_button(_joystick_bindings[i].axes[j].low,
523 axis_values[j] < a.low_threshold);
525 if (a.high.bindings[modifiers].size())
526 _update_button(_joystick_bindings[i].axes[j].high,
528 axis_values[j] > a.high_threshold);
531 // Fire bindings for the buttons.
532 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
533 _update_button(_joystick_bindings[i].buttons[j],
535 (buttons & (1 << j)) > 0);
542 FGInput::_update_button (button &b, int modifiers, bool pressed)
545 // The press event may be repeated.
546 if (!b.last_state || b.is_repeatable) {
547 // SG_LOG( SG_INPUT, SG_INFO, "Button has been pressed" );
548 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
549 b.bindings[modifiers][k]->fire();
552 // The release event is never repeated.
554 // SG_LOG( SG_INPUT, SG_INFO, "Button has been released" );
555 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
556 b.bindings[modifiers|FG_MOD_UP][k]->fire();
560 b.last_state = pressed;
565 FGInput::_read_bindings (const SGPropertyNode * node,
566 binding_list_t * binding_list,
569 SG_LOG(SG_INPUT, SG_INFO, "Reading all bindings");
570 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
571 for (unsigned int i = 0; i < bindings.size(); i++) {
572 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
573 << bindings[i]->getStringValue("command"));
574 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
577 // Read nested bindings for modifiers
578 if (node->getChild("mod-up") != 0)
579 _read_bindings(node->getChild("mod-up"), binding_list,
580 modifiers|FG_MOD_UP);
582 if (node->getChild("mod-shift") != 0)
583 _read_bindings(node->getChild("mod-shift"), binding_list,
584 modifiers|FG_MOD_SHIFT);
586 if (node->getChild("mod-ctrl") != 0)
587 _read_bindings(node->getChild("mod-ctrl"), binding_list,
588 modifiers|FG_MOD_CTRL);
590 if (node->getChild("mod-alt") != 0)
591 _read_bindings(node->getChild("mod-alt"), binding_list,
592 modifiers|FG_MOD_ALT);
596 const vector<FGBinding *> &
597 FGInput::_find_key_bindings (unsigned int k, int modifiers)
599 button &b = _key_bindings[k];
601 // Try it straight, first.
602 if (b.bindings[modifiers].size() > 0)
603 return b.bindings[modifiers];
605 // Try removing the control modifier
607 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
608 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
610 // Try removing shift modifier
611 // for upper case or any punctuation
612 // (since different keyboards will
613 // shift different punctuation types)
614 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
615 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
617 // Try removing alt modifier for
618 // high-bit characters.
619 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
620 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
622 // Give up and return the empty vector.
624 return b.bindings[modifiers];
629 ////////////////////////////////////////////////////////////////////////
630 // Implementation of FGInput::button.
631 ////////////////////////////////////////////////////////////////////////
633 FGInput::button::button ()
634 : is_repeatable(false),
639 FGInput::button::~button ()
641 // FIXME: memory leak
642 // for (int i = 0; i < FG_MOD_MAX; i++)
643 // for (int j = 0; i < bindings[i].size(); j++)
644 // delete bindings[i][j];
649 ////////////////////////////////////////////////////////////////////////
650 // Implementation of FGInput::axis.
651 ////////////////////////////////////////////////////////////////////////
653 FGInput::axis::axis ()
654 : last_value(9999999),
661 FGInput::axis::~axis ()
663 // for (int i = 0; i < FG_MOD_MAX; i++)
664 // for (int j = 0; i < bindings[i].size(); j++)
665 // delete bindings[i][j];
670 ////////////////////////////////////////////////////////////////////////
671 // Implementation of FGInput::joystick.
672 ////////////////////////////////////////////////////////////////////////
674 FGInput::joystick::joystick ()
678 FGInput::joystick::~joystick ()
687 ////////////////////////////////////////////////////////////////////////
688 // Implementation of GLUT callbacks.
689 ////////////////////////////////////////////////////////////////////////
693 * Construct the modifiers.
695 static inline int get_mods ()
697 int glut_modifiers = glutGetModifiers();
700 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
701 modifiers |= FGInput::FG_MOD_SHIFT;
702 if (glut_modifiers & GLUT_ACTIVE_CTRL)
703 modifiers |= FGInput::FG_MOD_CTRL;
704 if (glut_modifiers & GLUT_ACTIVE_ALT)
705 modifiers |= FGInput::FG_MOD_ALT;
712 * Key-down event handler for Glut.
714 * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
716 * @param k The integer value for the key pressed.
720 void GLUTkey(unsigned char k, int x, int y)
722 // Give PUI a chance to grab it first.
723 if (!puKeyboard(k, PU_DOWN))
724 current_input.doKey(k, get_mods(), x, y);
729 * Key-up event handler for GLUT.
731 * <p>PUI doesn't use this, so always pass it to the input manager.</p>
733 * @param k The integer value for the key pressed.
737 void GLUTkeyup(unsigned char k, int x, int y)
739 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
744 * Special key-down handler for Glut.
746 * <p>Pass the value on to the FGInput module unless PUI wants it.
747 * The key value will have 256 added to it.</p>
749 * @param k The integer value for the key pressed (will have 256 added
754 void GLUTspecialkey(int k, int x, int y)
756 // Give PUI a chance to grab it first.
757 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
758 current_input.doKey(k + 256, get_mods(), x, y);
763 * Special key-up handler for Glut.
765 * @param k The integer value for the key pressed (will have 256 added
770 void GLUTspecialkeyup(int k, int x, int y)
772 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);