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.empty()) {
114 SG_LOG(SG_INPUT, SG_WARN, "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_WARN, "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 (int x, int y) const
150 _arg->setIntValue("x-pos", x);
152 _arg->setIntValue("y-pos", y);
158 FGBinding::fire (double setting) const
161 // A value is automatically added to
163 if (_setting == 0) // save the setting node for efficiency
164 _setting = _arg->getChild("setting", 0, true);
165 _setting->setDoubleValue(setting);
172 ////////////////////////////////////////////////////////////////////////
173 // Implementation of FGInput.
174 ////////////////////////////////////////////////////////////////////////
177 extern void fgReshape( int width, int height );
179 FGInput current_input;
214 FGInput::update (int dt)
222 FGInput::doKey (int k, int modifiers, int x, int y)
224 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
225 << " with modifiers " << modifiers );
228 if (k < 0 || k >= MAX_KEYS) {
229 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
233 button &b = _key_bindings[k];
236 if (modifiers&FG_MOD_UP == 0) {
237 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
238 << " with modifiers " << modifiers );
239 if (!b.last_state || b.is_repeatable) {
240 const binding_list_t &bindings =
241 _find_key_bindings(k, modifiers);
242 int max = bindings.size();
244 for (int i = 0; i < max; i++)
253 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
254 << " with modifiers " << modifiers);
256 const binding_list_t &bindings =
257 _find_key_bindings(k, modifiers);
258 int max = bindings.size();
260 for (int i = 0; i < max; i++)
268 // Use the old, default actions.
269 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
270 if (modifiers&FG_MOD_UP)
273 // everything after here will be removed sooner or later...
275 if (modifiers & FG_MOD_SHIFT) {
279 HUD_brightkey( true );
283 fgHUDInit2(¤t_aircraft);
289 SG_LOG( SG_INPUT, SG_DEBUG, "" );
292 HUD_masterswitch( true );
295 fgHUDInit(¤t_aircraft); // normal HUD
300 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
301 if ( globals->get_autopilot()->get_HeadingMode() !=
302 FGAutopilot::FG_HEADING_WAYPOINT ) {
303 globals->get_autopilot()->set_HeadingMode(
304 FGAutopilot::FG_HEADING_WAYPOINT );
305 globals->get_autopilot()->set_HeadingEnabled( true );
307 globals->get_autopilot()->set_HeadingMode(
308 FGAutopilot::FG_TC_HEADING_LOCK );
311 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
312 const string &fog = fgGetString("/sim/rendering/fog");
313 if (fog == "disabled") {
314 fgSetString("/sim/rendering/fog", "fastest");
315 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
316 } else if (fog == "fastest") {
317 fgSetString("/sim/rendering/fog", "nicest");
318 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
319 } else if (fog == "nicest") {
320 fgSetString("/sim/rendering/fog", "disabled");
321 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
323 fgSetString("/sim/rendering/fog", "disabled");
324 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
325 << fog << ", changed to 'disabled'");
329 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
330 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
333 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
334 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
337 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
338 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
349 FGInput::doMouseClick (int b, int updown, int x, int y)
351 std::cout << "Mouse click " << b << ',' << updown << std::endl;
352 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
354 _update_button(_mouse_bindings[0].buttons[b], modifiers, updown, x, y);
358 FGInput::doMouseMotion (int x, int y)
364 FGInput::_init_keyboard ()
366 // TODO: zero the old bindings first.
367 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
368 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
369 if (key_nodes == 0) {
370 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
371 key_nodes = fgGetNode("/input/keyboard", true);
374 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
375 for (unsigned int i = 0; i < keys.size(); i++) {
376 int index = keys[i]->getIndex();
377 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
378 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
379 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
385 FGInput::_init_joystick ()
387 // TODO: zero the old bindings first.
388 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
389 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
391 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
392 js_nodes = fgGetNode("/input/joysticks", true);
395 for (int i = 0; i < MAX_JOYSTICKS; i++) {
396 SGPropertyNode * js_node = js_nodes->getChild("js", i);
398 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
399 js_node = js_nodes->getChild("js", i, true);
401 jsJoystick * js = new jsJoystick(i);
402 _joystick_bindings[i].js = js;
403 if (js->notWorking()) {
404 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
409 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
410 int nbuttons = jsCaps.wNumButtons;
411 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
413 int nbuttons = MAX_JOYSTICK_BUTTONS;
416 int naxes = js->getNumAxes();
417 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
418 _joystick_bindings[i].naxes = naxes;
419 _joystick_bindings[i].nbuttons = nbuttons;
421 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
423 // Set up range arrays
424 float minRange[MAX_JOYSTICK_AXES];
425 float maxRange[MAX_JOYSTICK_AXES];
426 float center[MAX_JOYSTICK_AXES];
428 // Initialize with default values
429 js->getMinRange(minRange);
430 js->getMaxRange(maxRange);
431 js->getCenter(center);
433 // Allocate axes and buttons
434 _joystick_bindings[i].axes = new axis[naxes];
435 _joystick_bindings[i].buttons = new button[nbuttons];
439 // Initialize the axes.
442 for (j = 0; j < naxes; j++) {
443 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
444 if (axis_node == 0) {
445 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
446 axis_node = js_node->getChild("axis", j, true);
449 axis &a = _joystick_bindings[i].axes[j];
451 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
453 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
454 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
455 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
456 center[j] = axis_node->getDoubleValue("center", center[j]);
458 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
460 // Initialize the virtual axis buttons.
461 _init_button(axis_node->getChild("low"), a.low, "low");
462 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
464 _init_button(axis_node->getChild("high"), a.high, "high");
465 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
469 // Initialize the buttons.
472 for (j = 0; j < nbuttons; j++) {
473 sprintf(buf, "%d", j);
474 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
475 _init_button(js_node->getChild("button", j),
476 _joystick_bindings[i].buttons[j],
481 js->setMinRange(minRange);
482 js->setMaxRange(maxRange);
483 js->setCenter(center);
489 FGInput::_init_mouse ()
491 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
493 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
494 if (mouse_nodes == 0) {
495 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
496 mouse_nodes = fgGetNode("/input/mice", true);
499 for (int i = 0; i < MAX_MICE; i++) {
500 const SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i);
501 _mouse_bindings[i].buttons = new button[MAX_MOUSE_BUTTONS];
502 if (mouse_node == 0) {
503 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for mouse " << i);
504 mouse_node = mouse_nodes->getChild("mouse", i, true);
507 for (int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
508 sprintf(buf, "%d", j);
509 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << j);
510 _init_button(mouse_node->getChild("button", j),
511 _mouse_bindings[i].buttons[j],
519 FGInput::_init_button (const SGPropertyNode * node,
524 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
526 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
528 // Get the bindings for the button
529 _read_bindings(node, b.bindings, FG_MOD_NONE);
535 FGInput::_update_keyboard ()
542 FGInput::_update_joystick ()
544 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
546 // float js_val, diff;
547 float axis_values[MAX_JOYSTICK_AXES];
552 for ( i = 0; i < MAX_JOYSTICKS; i++) {
554 jsJoystick * js = _joystick_bindings[i].js;
555 if (js == 0 || js->notWorking())
558 js->read(&buttons, axis_values);
561 // Fire bindings for the axes.
562 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
563 axis &a = _joystick_bindings[i].axes[j];
565 // Do nothing if the axis position
566 // is unchanged; only a change in
567 // position fires the bindings.
568 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
569 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
571 a.last_value = axis_values[j];
572 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
573 // << a.bindings[modifiers].size() << " bindings");
574 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
575 a.bindings[modifiers][k]->fire(axis_values[j]);
578 // do we have to emulate axis buttons?
579 if (a.low.bindings[modifiers].size())
580 _update_button(_joystick_bindings[i].axes[j].low,
582 axis_values[j] < a.low_threshold,
585 if (a.high.bindings[modifiers].size())
586 _update_button(_joystick_bindings[i].axes[j].high,
588 axis_values[j] > a.high_threshold,
592 // Fire bindings for the buttons.
593 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
594 _update_button(_joystick_bindings[i].buttons[j],
596 (buttons & (1 << j)) > 0,
603 FGInput::_update_mouse ()
609 FGInput::_update_button (button &b, int modifiers, bool pressed,
613 // The press event may be repeated.
614 if (!b.last_state || b.is_repeatable) {
615 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
616 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
617 b.bindings[modifiers][k]->fire(x, y);
620 // The release event is never repeated.
622 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
623 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
624 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
628 b.last_state = pressed;
633 FGInput::_read_bindings (const SGPropertyNode * node,
634 binding_list_t * binding_list,
637 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
638 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
639 for (unsigned int i = 0; i < bindings.size(); i++) {
640 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
641 << bindings[i]->getStringValue("command"));
642 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
645 // Read nested bindings for modifiers
646 if (node->getChild("mod-up") != 0)
647 _read_bindings(node->getChild("mod-up"), binding_list,
648 modifiers|FG_MOD_UP);
650 if (node->getChild("mod-shift") != 0)
651 _read_bindings(node->getChild("mod-shift"), binding_list,
652 modifiers|FG_MOD_SHIFT);
654 if (node->getChild("mod-ctrl") != 0)
655 _read_bindings(node->getChild("mod-ctrl"), binding_list,
656 modifiers|FG_MOD_CTRL);
658 if (node->getChild("mod-alt") != 0)
659 _read_bindings(node->getChild("mod-alt"), binding_list,
660 modifiers|FG_MOD_ALT);
664 const vector<FGBinding *> &
665 FGInput::_find_key_bindings (unsigned int k, int modifiers)
667 button &b = _key_bindings[k];
669 // Try it straight, first.
670 if (b.bindings[modifiers].size() > 0)
671 return b.bindings[modifiers];
673 // Try removing the control modifier
675 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
676 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
678 // Try removing shift modifier
679 // for upper case or any punctuation
680 // (since different keyboards will
681 // shift different punctuation types)
682 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
683 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
685 // Try removing alt modifier for
686 // high-bit characters.
687 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
688 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
690 // Give up and return the empty vector.
692 return b.bindings[modifiers];
697 ////////////////////////////////////////////////////////////////////////
698 // Implementation of FGInput::button.
699 ////////////////////////////////////////////////////////////////////////
701 FGInput::button::button ()
702 : is_repeatable(false),
707 FGInput::button::~button ()
709 // FIXME: memory leak
710 // for (int i = 0; i < FG_MOD_MAX; i++)
711 // for (int j = 0; i < bindings[i].size(); j++)
712 // delete bindings[i][j];
717 ////////////////////////////////////////////////////////////////////////
718 // Implementation of FGInput::axis.
719 ////////////////////////////////////////////////////////////////////////
721 FGInput::axis::axis ()
722 : last_value(9999999),
729 FGInput::axis::~axis ()
731 // for (int i = 0; i < FG_MOD_MAX; i++)
732 // for (int j = 0; i < bindings[i].size(); j++)
733 // delete bindings[i][j];
738 ////////////////////////////////////////////////////////////////////////
739 // Implementation of FGInput::joystick.
740 ////////////////////////////////////////////////////////////////////////
742 FGInput::joystick::joystick ()
746 FGInput::joystick::~joystick ()
755 ////////////////////////////////////////////////////////////////////////
756 // Implementation of FGInput::mouse
757 ////////////////////////////////////////////////////////////////////////
759 FGInput::mouse::mouse ()
763 FGInput::mouse::~mouse ()
770 ////////////////////////////////////////////////////////////////////////
771 // Implementation of GLUT callbacks.
772 ////////////////////////////////////////////////////////////////////////
776 * Construct the modifiers.
778 static inline int get_mods ()
780 int glut_modifiers = glutGetModifiers();
783 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
784 modifiers |= FGInput::FG_MOD_SHIFT;
785 if (glut_modifiers & GLUT_ACTIVE_CTRL)
786 modifiers |= FGInput::FG_MOD_CTRL;
787 if (glut_modifiers & GLUT_ACTIVE_ALT)
788 modifiers |= FGInput::FG_MOD_ALT;
795 ////////////////////////////////////////////////////////////////////////
797 ////////////////////////////////////////////////////////////////////////
800 GLUTkey(unsigned char k, int x, int y)
802 // Give PUI a chance to grab it first.
803 if (!puKeyboard(k, PU_DOWN))
804 current_input.doKey(k, get_mods(), x, y);
808 GLUTkeyup(unsigned char k, int x, int y)
810 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
814 GLUTspecialkey(int k, int x, int y)
816 // Give PUI a chance to grab it first.
817 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
818 current_input.doKey(k + 256, get_mods(), x, y);
822 GLUTspecialkeyup(int k, int x, int y)
824 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
828 GLUTmouse (int button, int updown, int x, int y)
830 current_input.doMouseClick(button, updown == GLUT_DOWN, x, y);
834 GLUTmotion (int x, int y)
837 // glutPostRedisplay();
838 current_input.doMouseMotion(x, y);