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>
57 #include <Model/panelnode.hxx>
59 #include <Main/globals.hxx>
60 #include <Main/fg_props.hxx>
64 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
65 SG_USING_STD(ifstream);
72 ////////////////////////////////////////////////////////////////////////
74 ////////////////////////////////////////////////////////////////////////
76 static FGInput * default_input = 0;
80 ////////////////////////////////////////////////////////////////////////
81 // Implementation of FGBinding.
82 ////////////////////////////////////////////////////////////////////////
84 FGBinding::FGBinding ()
86 _arg(new SGPropertyNode),
91 FGBinding::FGBinding (const SGPropertyNode * node)
93 _arg(new SGPropertyNode),
99 FGBinding::~FGBinding ()
101 // delete _arg; // Delete the saved arguments
102 // delete _command_state; // Delete the saved command state
106 FGBinding::read (const SGPropertyNode * node)
108 const SGPropertyNode * conditionNode = node->getChild("condition");
109 if (conditionNode != 0) {
110 cerr << "Adding condition to binding" << endl;
111 setCondition(fgReadCondition(conditionNode));
114 _command_name = node->getStringValue("command", "");
115 if (_command_name.empty()) {
116 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
121 _command = globals->get_commands()->getCommand(_command_name);
123 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
129 _arg = new SGPropertyNode;
131 copyProperties(node, _arg); // FIXME: don't use whole node!!!
135 FGBinding::fire () const
139 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
140 } else if (!(*_command)(_arg)) {
141 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
148 FGBinding::fire (double offset, double max) const
151 _arg->setDoubleValue("offset", offset/max);
157 FGBinding::fire (double setting) const
160 // A value is automatically added to
162 if (_setting == 0) // save the setting node for efficiency
163 _setting = _arg->getChild("setting", 0, true);
164 _setting->setDoubleValue(setting);
171 ////////////////////////////////////////////////////////////////////////
172 // Implementation of FGInput.
173 ////////////////////////////////////////////////////////////////////////
176 extern void fgReshape( int width, int height );
181 if (default_input == 0)
182 default_input = this;
187 if (default_input == this)
198 glutKeyboardFunc(GLUTkey);
199 glutKeyboardUpFunc(GLUTkeyup);
200 glutSpecialFunc(GLUTspecialkey);
201 glutSpecialUpFunc(GLUTspecialkeyup);
202 glutMouseFunc (GLUTmouse);
203 glutMotionFunc (GLUTmotion);
204 glutPassiveMotionFunc (GLUTmotion);
220 FGInput::update (double dt)
228 FGInput::makeDefault (bool status)
231 default_input = this;
232 else if (default_input == this)
237 FGInput::doKey (int k, int modifiers, int x, int y)
239 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
240 << " with modifiers " << modifiers );
243 if (k < 0 || k >= MAX_KEYS) {
244 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
248 button &b = _key_bindings[k];
251 if (modifiers&FG_MOD_UP == 0) {
252 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
253 << " with modifiers " << modifiers );
254 if (!b.last_state || b.is_repeatable) {
255 const binding_list_t &bindings =
256 _find_key_bindings(k, modifiers);
257 int max = bindings.size();
259 for (int i = 0; i < max; i++)
268 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
269 << " with modifiers " << modifiers);
271 const binding_list_t &bindings =
272 _find_key_bindings(k, modifiers);
273 int max = bindings.size();
275 for (int i = 0; i < max; i++)
283 // Use the old, default actions.
284 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
285 if (modifiers&FG_MOD_UP)
288 // everything after here will be removed sooner or later...
290 if (modifiers & FG_MOD_SHIFT) {
294 HUD_brightkey( true );
298 fgHUDInit2(¤t_aircraft);
304 SG_LOG( SG_INPUT, SG_DEBUG, "" );
307 HUD_masterswitch( true );
310 fgHUDInit(¤t_aircraft); // normal HUD
315 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
316 if ( globals->get_autopilot()->get_HeadingMode() !=
317 FGAutopilot::FG_HEADING_WAYPOINT ) {
318 globals->get_autopilot()->set_HeadingMode(
319 FGAutopilot::FG_HEADING_WAYPOINT );
320 globals->get_autopilot()->set_HeadingEnabled( true );
322 globals->get_autopilot()->set_HeadingMode(
323 FGAutopilot::FG_TC_HEADING_LOCK );
326 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
327 const string &fog = fgGetString("/sim/rendering/fog");
328 if (fog == "disabled") {
329 fgSetString("/sim/rendering/fog", "fastest");
330 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
331 } else if (fog == "fastest") {
332 fgSetString("/sim/rendering/fog", "nicest");
333 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
334 } else if (fog == "nicest") {
335 fgSetString("/sim/rendering/fog", "disabled");
336 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
338 fgSetString("/sim/rendering/fog", "disabled");
339 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
340 << fog << ", changed to 'disabled'");
344 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
345 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
348 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
349 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
352 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
353 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
364 FGInput::doMouseClick (int b, int updown, int x, int y)
366 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
368 mouse &m = _mouse_bindings[0];
369 mouse_mode &mode = m.modes[m.current_mode];
371 // Let the property manager know.
372 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
373 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
375 // Pass on to PUI and the panel if
376 // requested, and return if one of
377 // them consumes the event.
378 if (mode.pass_through) {
379 if (puMouse(b, updown, x, y))
381 else if ((current_panel != 0) &&
382 current_panel->doMouseAction(b, updown, x, y))
384 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
388 // OK, PUI and the panel didn't want the click
389 if (b >= MAX_MOUSE_BUTTONS) {
390 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
391 << " where only " << MAX_MOUSE_BUTTONS << " expected");
395 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
399 FGInput::doMouseMotion (int x, int y)
401 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
403 int xsize = fgGetInt("/sim/startup/xsize", 800);
404 int ysize = fgGetInt("/sim/startup/ysize", 600);
405 mouse &m = _mouse_bindings[0];
406 if (m.current_mode < 0 || m.current_mode >= m.nModes)
408 mouse_mode &mode = m.modes[m.current_mode];
410 // Pass on to PUI if requested, and return
411 // if PUI consumed the event.
412 if (mode.pass_through && puMouse(x, y))
415 // OK, PUI didn't want the event,
416 // so we can play with it.
419 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
420 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
424 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
425 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
428 // Constrain the mouse if requested
429 if (mode.constrained) {
430 bool need_warp = false;
434 } else if (x >= (xsize-1)) {
442 } else if (y >= (ysize-1)) {
448 glutWarpPointer(x, y);
455 FGInput::_init_keyboard ()
457 // TODO: zero the old bindings first.
458 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
459 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
460 if (key_nodes == 0) {
461 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
462 key_nodes = fgGetNode("/input/keyboard", true);
465 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
466 for (unsigned int i = 0; i < keys.size(); i++) {
467 int index = keys[i]->getIndex();
468 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
469 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
470 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
476 FGInput::_init_joystick ()
478 // TODO: zero the old bindings first.
479 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
480 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
482 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
483 js_nodes = fgGetNode("/input/joysticks", true);
486 for (int i = 0; i < MAX_JOYSTICKS; i++) {
487 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
489 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
490 js_node = js_nodes->getChild("js", i, true);
492 jsJoystick * js = new jsJoystick(i);
493 _joystick_bindings[i].js = js;
494 if (js->notWorking()) {
495 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
498 #ifdef FG_PLIB_JOYSTICK_GETNAME
499 bool found_js = false;
500 const char * name = js->getName();
501 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
503 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
504 for (unsigned int i = 0; i < nodes.size(); i++) {
505 SGPropertyNode_ptr node = nodes[i];
506 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
507 for (unsigned int j = 0; j < name_nodes.size(); j++) {
508 const char * js_name = name_nodes[j]->getStringValue();
509 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
510 if (!strcmp(js_name, name)) {
511 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
524 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
525 int nbuttons = jsCaps.wNumButtons;
526 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
528 int nbuttons = MAX_JOYSTICK_BUTTONS;
531 int naxes = js->getNumAxes();
532 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
533 _joystick_bindings[i].naxes = naxes;
534 _joystick_bindings[i].nbuttons = nbuttons;
536 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
538 // Set up range arrays
539 float minRange[MAX_JOYSTICK_AXES];
540 float maxRange[MAX_JOYSTICK_AXES];
541 float center[MAX_JOYSTICK_AXES];
543 // Initialize with default values
544 js->getMinRange(minRange);
545 js->getMaxRange(maxRange);
546 js->getCenter(center);
548 // Allocate axes and buttons
549 _joystick_bindings[i].axes = new axis[naxes];
550 _joystick_bindings[i].buttons = new button[nbuttons];
554 // Initialize the axes.
557 for (j = 0; j < naxes; j++) {
558 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
559 if (axis_node == 0) {
560 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
561 axis_node = js_node->getChild("axis", j, true);
564 axis &a = _joystick_bindings[i].axes[j];
566 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
568 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
569 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
570 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
571 center[j] = axis_node->getDoubleValue("center", center[j]);
573 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
575 // Initialize the virtual axis buttons.
576 _init_button(axis_node->getChild("low"), a.low, "low");
577 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
579 _init_button(axis_node->getChild("high"), a.high, "high");
580 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
584 // Initialize the buttons.
587 for (j = 0; j < nbuttons; j++) {
588 sprintf(buf, "%d", j);
589 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
590 _init_button(js_node->getChild("button", j),
591 _joystick_bindings[i].buttons[j],
596 js->setMinRange(minRange);
597 js->setMaxRange(maxRange);
598 js->setCenter(center);
603 // Map of all known GLUT cursor names
608 } mouse_cursor_map[] = {
609 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
610 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
611 { "info", GLUT_CURSOR_INFO },
612 { "destroy", GLUT_CURSOR_DESTROY },
613 { "help", GLUT_CURSOR_HELP },
614 { "cycle", GLUT_CURSOR_CYCLE },
615 { "spray", GLUT_CURSOR_SPRAY },
616 { "wait", GLUT_CURSOR_WAIT },
617 { "text", GLUT_CURSOR_TEXT },
618 { "crosshair", GLUT_CURSOR_CROSSHAIR },
619 { "up-down", GLUT_CURSOR_UP_DOWN },
620 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
621 { "top-side", GLUT_CURSOR_TOP_SIDE },
622 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
623 { "left-side", GLUT_CURSOR_LEFT_SIDE },
624 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
625 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
626 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
627 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
628 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
629 { "inherit", GLUT_CURSOR_INHERIT },
630 { "none", GLUT_CURSOR_NONE },
631 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
638 FGInput::_init_mouse ()
640 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
642 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
643 if (mouse_nodes == 0) {
644 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
645 mouse_nodes = fgGetNode("/input/mice", true);
649 for (int i = 0; i < MAX_MICE; i++) {
650 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
651 mouse &m = _mouse_bindings[i];
653 // Grab node pointers
655 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
656 m.mode_node = fgGetNode(buf, true);
657 m.mode_node->setIntValue(0);
658 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
659 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
660 m.mouse_button_nodes[j] = fgGetNode(buf, true);
661 m.mouse_button_nodes[j]->setBoolValue(false);
664 // Read all the modes
665 m.nModes = mouse_node->getIntValue("mode-count", 1);
666 m.modes = new mouse_mode[m.nModes];
668 for (int j = 0; j < m.nModes; j++) {
671 // Read the mouse cursor for this mode
672 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
673 const char * cursor_name =
674 mode_node->getStringValue("cursor", "inherit");
675 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
676 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
677 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
678 m.modes[j].cursor = mouse_cursor_map[k].cursor;
683 // Read other properties for this mode
684 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
685 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
687 // Read the button bindings for this mode
688 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
690 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
691 sprintf(buf, "mouse button %d", k);
692 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
693 _init_button(mode_node->getChild("button", k),
694 m.modes[j].buttons[k],
698 // Read the axis bindings for this mode
699 _read_bindings(mode_node->getChild("x-axis", 0, true),
700 m.modes[j].x_bindings,
702 _read_bindings(mode_node->getChild("y-axis", 0, true),
703 m.modes[j].y_bindings,
711 FGInput::_init_button (const SGPropertyNode * node,
716 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
718 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
720 // Get the bindings for the button
721 _read_bindings(node, b.bindings, FG_MOD_NONE);
727 FGInput::_update_keyboard ()
734 FGInput::_update_joystick ()
736 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
738 // float js_val, diff;
739 float axis_values[MAX_JOYSTICK_AXES];
744 for ( i = 0; i < MAX_JOYSTICKS; i++) {
746 jsJoystick * js = _joystick_bindings[i].js;
747 if (js == 0 || js->notWorking())
750 js->read(&buttons, axis_values);
753 // Fire bindings for the axes.
754 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
755 axis &a = _joystick_bindings[i].axes[j];
757 // Do nothing if the axis position
758 // is unchanged; only a change in
759 // position fires the bindings.
760 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
761 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
763 a.last_value = axis_values[j];
764 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
765 // << a.bindings[modifiers].size() << " bindings");
766 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
767 a.bindings[modifiers][k]->fire(axis_values[j]);
770 // do we have to emulate axis buttons?
771 if (a.low.bindings[modifiers].size())
772 _update_button(_joystick_bindings[i].axes[j].low,
774 axis_values[j] < a.low_threshold,
777 if (a.high.bindings[modifiers].size())
778 _update_button(_joystick_bindings[i].axes[j].high,
780 axis_values[j] > a.high_threshold,
784 // Fire bindings for the buttons.
785 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
786 _update_button(_joystick_bindings[i].buttons[j],
788 (buttons & (1 << j)) > 0,
795 FGInput::_update_mouse ()
797 mouse &m = _mouse_bindings[0];
798 int mode = m.mode_node->getIntValue();
799 if (mode != m.current_mode) {
800 m.current_mode = mode;
801 if (mode >= 0 && mode < m.nModes) {
802 glutSetCursor(m.modes[mode].cursor);
803 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
804 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
805 glutWarpPointer(m.x, m.y);
807 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
808 glutSetCursor(GLUT_CURSOR_INHERIT);
814 FGInput::_update_button (button &b, int modifiers, bool pressed,
818 // The press event may be repeated.
819 if (!b.last_state || b.is_repeatable) {
820 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
821 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
822 b.bindings[modifiers][k]->fire(x, y);
825 // The release event is never repeated.
827 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
828 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
829 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
833 b.last_state = pressed;
838 FGInput::_read_bindings (const SGPropertyNode * node,
839 binding_list_t * binding_list,
842 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
843 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
844 for (unsigned int i = 0; i < bindings.size(); i++) {
845 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
846 << bindings[i]->getStringValue("command"));
847 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
850 // Read nested bindings for modifiers
851 if (node->getChild("mod-up") != 0)
852 _read_bindings(node->getChild("mod-up"), binding_list,
853 modifiers|FG_MOD_UP);
855 if (node->getChild("mod-shift") != 0)
856 _read_bindings(node->getChild("mod-shift"), binding_list,
857 modifiers|FG_MOD_SHIFT);
859 if (node->getChild("mod-ctrl") != 0)
860 _read_bindings(node->getChild("mod-ctrl"), binding_list,
861 modifiers|FG_MOD_CTRL);
863 if (node->getChild("mod-alt") != 0)
864 _read_bindings(node->getChild("mod-alt"), binding_list,
865 modifiers|FG_MOD_ALT);
869 const vector<FGBinding *> &
870 FGInput::_find_key_bindings (unsigned int k, int modifiers)
872 button &b = _key_bindings[k];
874 // Try it straight, first.
875 if (b.bindings[modifiers].size() > 0)
876 return b.bindings[modifiers];
878 // Try removing the control modifier
880 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
881 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
883 // Try removing shift modifier
884 // for upper case or any punctuation
885 // (since different keyboards will
886 // shift different punctuation types)
887 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
888 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
890 // Try removing alt modifier for
891 // high-bit characters.
892 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
893 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
895 // Give up and return the empty vector.
897 return b.bindings[modifiers];
902 ////////////////////////////////////////////////////////////////////////
903 // Implementation of FGInput::button.
904 ////////////////////////////////////////////////////////////////////////
906 FGInput::button::button ()
907 : is_repeatable(false),
912 FGInput::button::~button ()
914 // FIXME: memory leak
915 // for (int i = 0; i < FG_MOD_MAX; i++)
916 // for (int j = 0; i < bindings[i].size(); j++)
917 // delete bindings[i][j];
922 ////////////////////////////////////////////////////////////////////////
923 // Implementation of FGInput::axis.
924 ////////////////////////////////////////////////////////////////////////
926 FGInput::axis::axis ()
927 : last_value(9999999),
934 FGInput::axis::~axis ()
936 // for (int i = 0; i < FG_MOD_MAX; i++)
937 // for (int j = 0; i < bindings[i].size(); j++)
938 // delete bindings[i][j];
943 ////////////////////////////////////////////////////////////////////////
944 // Implementation of FGInput::joystick.
945 ////////////////////////////////////////////////////////////////////////
947 FGInput::joystick::joystick ()
951 FGInput::joystick::~joystick ()
960 ////////////////////////////////////////////////////////////////////////
961 // Implementation of FGInput::mouse_mode
962 ////////////////////////////////////////////////////////////////////////
964 FGInput::mouse_mode::mouse_mode ()
965 : cursor(GLUT_CURSOR_INHERIT),
972 FGInput::mouse_mode::~mouse_mode ()
974 // FIXME: memory leak
975 // for (int i = 0; i < FG_MOD_MAX; i++) {
977 // for (j = 0; i < x_bindings[i].size(); j++)
978 // delete bindings[i][j];
979 // for (j = 0; j < y_bindings[i].size(); j++)
980 // delete bindings[i][j];
987 ////////////////////////////////////////////////////////////////////////
988 // Implementation of FGInput::mouse
989 ////////////////////////////////////////////////////////////////////////
991 FGInput::mouse::mouse ()
1000 FGInput::mouse::~mouse ()
1007 ////////////////////////////////////////////////////////////////////////
1008 // Implementation of GLUT callbacks.
1009 ////////////////////////////////////////////////////////////////////////
1013 * Construct the modifiers.
1015 static inline int get_mods ()
1017 int glut_modifiers = glutGetModifiers();
1020 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1021 modifiers |= FGInput::FG_MOD_SHIFT;
1022 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1023 modifiers |= FGInput::FG_MOD_CTRL;
1024 if (glut_modifiers & GLUT_ACTIVE_ALT)
1025 modifiers |= FGInput::FG_MOD_ALT;
1032 ////////////////////////////////////////////////////////////////////////
1033 // GLUT C callbacks.
1034 ////////////////////////////////////////////////////////////////////////
1037 GLUTkey(unsigned char k, int x, int y)
1039 // Give PUI a chance to grab it first.
1040 if (!puKeyboard(k, PU_DOWN)) {
1041 if (default_input != 0)
1042 default_input->doKey(k, get_mods(), x, y);
1047 GLUTkeyup(unsigned char k, int x, int y)
1049 if (default_input != 0)
1050 default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1054 GLUTspecialkey(int k, int x, int y)
1056 // Give PUI a chance to grab it first.
1057 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
1058 if (default_input != 0)
1059 default_input->doKey(k + 256, get_mods(), x, y);
1064 GLUTspecialkeyup(int k, int x, int y)
1066 if (default_input != 0)
1067 default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1071 GLUTmouse (int button, int updown, int x, int y)
1073 if (default_input != 0)
1074 default_input->doMouseClick(button, updown, x, y);
1078 GLUTmotion (int x, int y)
1080 if (default_input != 0)
1081 default_input->doMouseMotion(x, y);