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 ////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////
75 static FGInput * default_input = 0;
79 ////////////////////////////////////////////////////////////////////////
80 // Implementation of FGBinding.
81 ////////////////////////////////////////////////////////////////////////
83 FGBinding::FGBinding ()
85 _arg(new SGPropertyNode),
90 FGBinding::FGBinding (const SGPropertyNode * node)
92 _arg(new SGPropertyNode),
98 FGBinding::~FGBinding ()
100 // delete _arg; // Delete the saved arguments
101 // delete _command_state; // Delete the saved command state
105 FGBinding::read (const SGPropertyNode * node)
107 const SGPropertyNode * conditionNode = node->getChild("condition");
108 if (conditionNode != 0) {
109 cerr << "Adding condition to binding" << endl;
110 setCondition(fgReadCondition(conditionNode));
113 _command_name = node->getStringValue("command", "");
114 if (_command_name.empty()) {
115 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
120 _command = globals->get_commands()->getCommand(_command_name);
122 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
128 _arg = new SGPropertyNode;
130 copyProperties(node, _arg); // FIXME: don't use whole node!!!
134 FGBinding::fire () const
138 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
139 } else if (!(*_command)(_arg)) {
140 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
147 FGBinding::fire (double offset, double max) const
150 _arg->setDoubleValue("offset", offset/max);
156 FGBinding::fire (double setting) const
159 // A value is automatically added to
161 if (_setting == 0) // save the setting node for efficiency
162 _setting = _arg->getChild("setting", 0, true);
163 _setting->setDoubleValue(setting);
170 ////////////////////////////////////////////////////////////////////////
171 // Implementation of FGInput.
172 ////////////////////////////////////////////////////////////////////////
175 extern void fgReshape( int width, int height );
180 if (default_input == 0)
181 default_input = this;
186 if (default_input == this)
197 glutKeyboardFunc(GLUTkey);
198 glutKeyboardUpFunc(GLUTkeyup);
199 glutSpecialFunc(GLUTspecialkey);
200 glutSpecialUpFunc(GLUTspecialkeyup);
201 glutMouseFunc (GLUTmouse);
202 glutMotionFunc (GLUTmotion);
203 glutPassiveMotionFunc (GLUTmotion);
219 FGInput::update (double dt)
227 FGInput::makeDefault (bool status)
230 default_input = this;
231 else if (default_input == this)
236 FGInput::doKey (int k, int modifiers, int x, int y)
238 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
239 << " with modifiers " << modifiers );
242 if (k < 0 || k >= MAX_KEYS) {
243 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
247 button &b = _key_bindings[k];
250 if (modifiers&FG_MOD_UP == 0) {
251 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
252 << " with modifiers " << modifiers );
253 if (!b.last_state || b.is_repeatable) {
254 const binding_list_t &bindings =
255 _find_key_bindings(k, modifiers);
256 int max = bindings.size();
258 for (int i = 0; i < max; i++)
267 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
268 << " with modifiers " << modifiers);
270 const binding_list_t &bindings =
271 _find_key_bindings(k, modifiers);
272 int max = bindings.size();
274 for (int i = 0; i < max; i++)
282 // Use the old, default actions.
283 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
284 if (modifiers&FG_MOD_UP)
287 // everything after here will be removed sooner or later...
289 if (modifiers & FG_MOD_SHIFT) {
293 HUD_brightkey( true );
297 fgHUDInit2(¤t_aircraft);
303 SG_LOG( SG_INPUT, SG_DEBUG, "" );
306 HUD_masterswitch( true );
309 fgHUDInit(¤t_aircraft); // normal HUD
314 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
315 if ( globals->get_autopilot()->get_HeadingMode() !=
316 FGAutopilot::FG_HEADING_WAYPOINT ) {
317 globals->get_autopilot()->set_HeadingMode(
318 FGAutopilot::FG_HEADING_WAYPOINT );
319 globals->get_autopilot()->set_HeadingEnabled( true );
321 globals->get_autopilot()->set_HeadingMode(
322 FGAutopilot::FG_TC_HEADING_LOCK );
325 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
326 const string &fog = fgGetString("/sim/rendering/fog");
327 if (fog == "disabled") {
328 fgSetString("/sim/rendering/fog", "fastest");
329 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
330 } else if (fog == "fastest") {
331 fgSetString("/sim/rendering/fog", "nicest");
332 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
333 } else if (fog == "nicest") {
334 fgSetString("/sim/rendering/fog", "disabled");
335 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
337 fgSetString("/sim/rendering/fog", "disabled");
338 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
339 << fog << ", changed to 'disabled'");
343 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
344 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
347 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
348 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
351 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
352 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
363 FGInput::doMouseClick (int b, int updown, int x, int y)
365 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
367 mouse &m = _mouse_bindings[0];
368 mouse_mode &mode = m.modes[m.current_mode];
370 // Let the property manager know.
371 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
372 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
374 // Pass on to PUI and the panel if
375 // requested, and return if one of
376 // them consumes the event.
377 if (mode.pass_through) {
378 if (puMouse(b, updown, x, y))
380 else if ((current_panel != 0) &&
381 current_panel->doMouseAction(b, updown, x, y))
385 // OK, PUI and the panel didn't want the click
386 if (b >= MAX_MOUSE_BUTTONS) {
387 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
388 << " where only " << MAX_MOUSE_BUTTONS << " expected");
392 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
396 FGInput::doMouseMotion (int x, int y)
398 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
400 int xsize = fgGetInt("/sim/startup/xsize", 800);
401 int ysize = fgGetInt("/sim/startup/ysize", 600);
402 mouse &m = _mouse_bindings[0];
403 if (m.current_mode < 0 || m.current_mode >= m.nModes)
405 mouse_mode &mode = m.modes[m.current_mode];
407 // Pass on to PUI if requested, and return
408 // if PUI consumed the event.
409 if (mode.pass_through && puMouse(x, y))
412 // OK, PUI didn't want the event,
413 // so we can play with it.
416 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
417 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
421 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
422 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
425 // Constrain the mouse if requested
426 if (mode.constrained) {
427 bool need_warp = false;
431 } else if (x >= (xsize-1)) {
439 } else if (y >= (ysize-1)) {
445 glutWarpPointer(x, y);
452 FGInput::_init_keyboard ()
454 // TODO: zero the old bindings first.
455 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
456 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
457 if (key_nodes == 0) {
458 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
459 key_nodes = fgGetNode("/input/keyboard", true);
462 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
463 for (unsigned int i = 0; i < keys.size(); i++) {
464 int index = keys[i]->getIndex();
465 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
466 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
467 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
473 FGInput::_init_joystick ()
475 // TODO: zero the old bindings first.
476 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
477 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
479 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
480 js_nodes = fgGetNode("/input/joysticks", true);
483 for (int i = 0; i < MAX_JOYSTICKS; i++) {
484 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
486 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
487 js_node = js_nodes->getChild("js", i, true);
489 jsJoystick * js = new jsJoystick(i);
490 _joystick_bindings[i].js = js;
491 if (js->notWorking()) {
492 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
495 #ifdef FG_PLIB_JOYSTICK_GETNAME
496 bool found_js = false;
497 const char * name = js->getName();
498 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
500 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
501 for (unsigned int i = 0; i < nodes.size(); i++) {
502 SGPropertyNode_ptr node = nodes[i];
503 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
504 for (unsigned int j = 0; j < name_nodes.size(); j++) {
505 const char * js_name = name_nodes[j]->getStringValue();
506 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
507 if (!strcmp(js_name, name)) {
508 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
521 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
522 int nbuttons = jsCaps.wNumButtons;
523 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
525 int nbuttons = MAX_JOYSTICK_BUTTONS;
528 int naxes = js->getNumAxes();
529 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
530 _joystick_bindings[i].naxes = naxes;
531 _joystick_bindings[i].nbuttons = nbuttons;
533 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
535 // Set up range arrays
536 float minRange[MAX_JOYSTICK_AXES];
537 float maxRange[MAX_JOYSTICK_AXES];
538 float center[MAX_JOYSTICK_AXES];
540 // Initialize with default values
541 js->getMinRange(minRange);
542 js->getMaxRange(maxRange);
543 js->getCenter(center);
545 // Allocate axes and buttons
546 _joystick_bindings[i].axes = new axis[naxes];
547 _joystick_bindings[i].buttons = new button[nbuttons];
551 // Initialize the axes.
554 for (j = 0; j < naxes; j++) {
555 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
556 if (axis_node == 0) {
557 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
558 axis_node = js_node->getChild("axis", j, true);
561 axis &a = _joystick_bindings[i].axes[j];
563 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
565 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
566 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
567 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
568 center[j] = axis_node->getDoubleValue("center", center[j]);
570 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
572 // Initialize the virtual axis buttons.
573 _init_button(axis_node->getChild("low"), a.low, "low");
574 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
576 _init_button(axis_node->getChild("high"), a.high, "high");
577 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
581 // Initialize the buttons.
584 for (j = 0; j < nbuttons; j++) {
585 sprintf(buf, "%d", j);
586 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
587 _init_button(js_node->getChild("button", j),
588 _joystick_bindings[i].buttons[j],
593 js->setMinRange(minRange);
594 js->setMaxRange(maxRange);
595 js->setCenter(center);
600 // Map of all known GLUT cursor names
605 } mouse_cursor_map[] = {
606 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
607 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
608 { "info", GLUT_CURSOR_INFO },
609 { "destroy", GLUT_CURSOR_DESTROY },
610 { "help", GLUT_CURSOR_HELP },
611 { "cycle", GLUT_CURSOR_CYCLE },
612 { "spray", GLUT_CURSOR_SPRAY },
613 { "wait", GLUT_CURSOR_WAIT },
614 { "text", GLUT_CURSOR_TEXT },
615 { "crosshair", GLUT_CURSOR_CROSSHAIR },
616 { "up-down", GLUT_CURSOR_UP_DOWN },
617 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
618 { "top-side", GLUT_CURSOR_TOP_SIDE },
619 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
620 { "left-side", GLUT_CURSOR_LEFT_SIDE },
621 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
622 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
623 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
624 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
625 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
626 { "inherit", GLUT_CURSOR_INHERIT },
627 { "none", GLUT_CURSOR_NONE },
628 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
635 FGInput::_init_mouse ()
637 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
639 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
640 if (mouse_nodes == 0) {
641 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
642 mouse_nodes = fgGetNode("/input/mice", true);
646 for (int i = 0; i < MAX_MICE; i++) {
647 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
648 mouse &m = _mouse_bindings[i];
650 // Grab node pointers
652 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
653 m.mode_node = fgGetNode(buf, true);
654 m.mode_node->setIntValue(0);
655 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
656 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
657 m.mouse_button_nodes[j] = fgGetNode(buf, true);
658 m.mouse_button_nodes[j]->setBoolValue(false);
661 // Read all the modes
662 m.nModes = mouse_node->getIntValue("mode-count", 1);
663 m.modes = new mouse_mode[m.nModes];
665 for (int j = 0; j < m.nModes; j++) {
668 // Read the mouse cursor for this mode
669 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
670 const char * cursor_name =
671 mode_node->getStringValue("cursor", "inherit");
672 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
673 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
674 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
675 m.modes[j].cursor = mouse_cursor_map[k].cursor;
680 // Read other properties for this mode
681 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
682 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
684 // Read the button bindings for this mode
685 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
687 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
688 sprintf(buf, "mouse button %d", k);
689 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
690 _init_button(mode_node->getChild("button", k),
691 m.modes[j].buttons[k],
695 // Read the axis bindings for this mode
696 _read_bindings(mode_node->getChild("x-axis", 0, true),
697 m.modes[j].x_bindings,
699 _read_bindings(mode_node->getChild("y-axis", 0, true),
700 m.modes[j].y_bindings,
708 FGInput::_init_button (const SGPropertyNode * node,
713 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
715 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
717 // Get the bindings for the button
718 _read_bindings(node, b.bindings, FG_MOD_NONE);
724 FGInput::_update_keyboard ()
731 FGInput::_update_joystick ()
733 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
735 // float js_val, diff;
736 float axis_values[MAX_JOYSTICK_AXES];
741 for ( i = 0; i < MAX_JOYSTICKS; i++) {
743 jsJoystick * js = _joystick_bindings[i].js;
744 if (js == 0 || js->notWorking())
747 js->read(&buttons, axis_values);
750 // Fire bindings for the axes.
751 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
752 axis &a = _joystick_bindings[i].axes[j];
754 // Do nothing if the axis position
755 // is unchanged; only a change in
756 // position fires the bindings.
757 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
758 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
760 a.last_value = axis_values[j];
761 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
762 // << a.bindings[modifiers].size() << " bindings");
763 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
764 a.bindings[modifiers][k]->fire(axis_values[j]);
767 // do we have to emulate axis buttons?
768 if (a.low.bindings[modifiers].size())
769 _update_button(_joystick_bindings[i].axes[j].low,
771 axis_values[j] < a.low_threshold,
774 if (a.high.bindings[modifiers].size())
775 _update_button(_joystick_bindings[i].axes[j].high,
777 axis_values[j] > a.high_threshold,
781 // Fire bindings for the buttons.
782 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
783 _update_button(_joystick_bindings[i].buttons[j],
785 (buttons & (1 << j)) > 0,
792 FGInput::_update_mouse ()
794 mouse &m = _mouse_bindings[0];
795 int mode = m.mode_node->getIntValue();
796 if (mode != m.current_mode) {
797 m.current_mode = mode;
798 if (mode >= 0 && mode < m.nModes) {
799 glutSetCursor(m.modes[mode].cursor);
800 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
801 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
802 glutWarpPointer(m.x, m.y);
804 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
805 glutSetCursor(GLUT_CURSOR_INHERIT);
811 FGInput::_update_button (button &b, int modifiers, bool pressed,
815 // The press event may be repeated.
816 if (!b.last_state || b.is_repeatable) {
817 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
818 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
819 b.bindings[modifiers][k]->fire(x, y);
822 // The release event is never repeated.
824 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
825 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
826 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
830 b.last_state = pressed;
835 FGInput::_read_bindings (const SGPropertyNode * node,
836 binding_list_t * binding_list,
839 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
840 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
841 for (unsigned int i = 0; i < bindings.size(); i++) {
842 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
843 << bindings[i]->getStringValue("command"));
844 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
847 // Read nested bindings for modifiers
848 if (node->getChild("mod-up") != 0)
849 _read_bindings(node->getChild("mod-up"), binding_list,
850 modifiers|FG_MOD_UP);
852 if (node->getChild("mod-shift") != 0)
853 _read_bindings(node->getChild("mod-shift"), binding_list,
854 modifiers|FG_MOD_SHIFT);
856 if (node->getChild("mod-ctrl") != 0)
857 _read_bindings(node->getChild("mod-ctrl"), binding_list,
858 modifiers|FG_MOD_CTRL);
860 if (node->getChild("mod-alt") != 0)
861 _read_bindings(node->getChild("mod-alt"), binding_list,
862 modifiers|FG_MOD_ALT);
866 const vector<FGBinding *> &
867 FGInput::_find_key_bindings (unsigned int k, int modifiers)
869 button &b = _key_bindings[k];
871 // Try it straight, first.
872 if (b.bindings[modifiers].size() > 0)
873 return b.bindings[modifiers];
875 // Try removing the control modifier
877 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
878 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
880 // Try removing shift modifier
881 // for upper case or any punctuation
882 // (since different keyboards will
883 // shift different punctuation types)
884 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
885 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
887 // Try removing alt modifier for
888 // high-bit characters.
889 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
890 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
892 // Give up and return the empty vector.
894 return b.bindings[modifiers];
899 ////////////////////////////////////////////////////////////////////////
900 // Implementation of FGInput::button.
901 ////////////////////////////////////////////////////////////////////////
903 FGInput::button::button ()
904 : is_repeatable(false),
909 FGInput::button::~button ()
911 // FIXME: memory leak
912 // for (int i = 0; i < FG_MOD_MAX; i++)
913 // for (int j = 0; i < bindings[i].size(); j++)
914 // delete bindings[i][j];
919 ////////////////////////////////////////////////////////////////////////
920 // Implementation of FGInput::axis.
921 ////////////////////////////////////////////////////////////////////////
923 FGInput::axis::axis ()
924 : last_value(9999999),
931 FGInput::axis::~axis ()
933 // for (int i = 0; i < FG_MOD_MAX; i++)
934 // for (int j = 0; i < bindings[i].size(); j++)
935 // delete bindings[i][j];
940 ////////////////////////////////////////////////////////////////////////
941 // Implementation of FGInput::joystick.
942 ////////////////////////////////////////////////////////////////////////
944 FGInput::joystick::joystick ()
948 FGInput::joystick::~joystick ()
957 ////////////////////////////////////////////////////////////////////////
958 // Implementation of FGInput::mouse_mode
959 ////////////////////////////////////////////////////////////////////////
961 FGInput::mouse_mode::mouse_mode ()
962 : cursor(GLUT_CURSOR_INHERIT),
969 FGInput::mouse_mode::~mouse_mode ()
971 // FIXME: memory leak
972 // for (int i = 0; i < FG_MOD_MAX; i++) {
974 // for (j = 0; i < x_bindings[i].size(); j++)
975 // delete bindings[i][j];
976 // for (j = 0; j < y_bindings[i].size(); j++)
977 // delete bindings[i][j];
984 ////////////////////////////////////////////////////////////////////////
985 // Implementation of FGInput::mouse
986 ////////////////////////////////////////////////////////////////////////
988 FGInput::mouse::mouse ()
997 FGInput::mouse::~mouse ()
1004 ////////////////////////////////////////////////////////////////////////
1005 // Implementation of GLUT callbacks.
1006 ////////////////////////////////////////////////////////////////////////
1010 * Construct the modifiers.
1012 static inline int get_mods ()
1014 int glut_modifiers = glutGetModifiers();
1017 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1018 modifiers |= FGInput::FG_MOD_SHIFT;
1019 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1020 modifiers |= FGInput::FG_MOD_CTRL;
1021 if (glut_modifiers & GLUT_ACTIVE_ALT)
1022 modifiers |= FGInput::FG_MOD_ALT;
1029 ////////////////////////////////////////////////////////////////////////
1030 // GLUT C callbacks.
1031 ////////////////////////////////////////////////////////////////////////
1034 GLUTkey(unsigned char k, int x, int y)
1036 // Give PUI a chance to grab it first.
1037 if (!puKeyboard(k, PU_DOWN)) {
1038 if (default_input != 0)
1039 default_input->doKey(k, get_mods(), x, y);
1044 GLUTkeyup(unsigned char k, int x, int y)
1046 if (default_input != 0)
1047 default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1051 GLUTspecialkey(int k, int x, int y)
1053 // Give PUI a chance to grab it first.
1054 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
1055 if (default_input != 0)
1056 default_input->doKey(k + 256, get_mods(), x, y);
1061 GLUTspecialkeyup(int k, int x, int y)
1063 if (default_input != 0)
1064 default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1068 GLUTmouse (int button, int updown, int x, int y)
1070 if (default_input != 0)
1071 default_input->doMouseClick(button, updown, x, y);
1075 GLUTmotion (int x, int y)
1077 if (default_input != 0)
1078 default_input->doMouseMotion(x, y);