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),
91 FGBinding::FGBinding (const SGPropertyNode * node)
93 _arg(new SGPropertyNode),
100 FGBinding::~FGBinding ()
102 // delete _arg; // Delete the saved arguments
103 // delete _command_state; // Delete the saved command state
107 FGBinding::read (const SGPropertyNode * node)
109 const SGPropertyNode * conditionNode = node->getChild("condition");
110 if (conditionNode != 0) {
111 cerr << "Adding condition to binding" << endl;
112 setCondition(fgReadCondition(conditionNode));
115 _command_name = node->getStringValue("command", "");
116 if (_command_name.empty()) {
117 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
122 _command = globals->get_commands()->getCommand(_command_name);
124 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
130 _arg = new SGPropertyNode;
132 copyProperties(node, _arg); // FIXME: don't use whole node!!!
136 FGBinding::fire () const
140 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
141 } else if (!(*_command)(_arg, &_command_state)) {
142 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
149 FGBinding::fire (double offset, double max) const
152 _arg->setDoubleValue("offset", offset/max);
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 );
182 if (default_input == 0)
183 default_input = this;
188 if (default_input == this)
199 glutKeyboardFunc(GLUTkey);
200 glutKeyboardUpFunc(GLUTkeyup);
201 glutSpecialFunc(GLUTspecialkey);
202 glutSpecialUpFunc(GLUTspecialkeyup);
203 glutMouseFunc (GLUTmouse);
204 glutMotionFunc (GLUTmotion);
205 glutPassiveMotionFunc (GLUTmotion);
221 FGInput::update (double dt)
229 FGInput::makeDefault (bool status)
232 default_input = this;
233 else if (default_input == this)
238 FGInput::doKey (int k, int modifiers, int x, int y)
240 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
241 << " with modifiers " << modifiers );
244 if (k < 0 || k >= MAX_KEYS) {
245 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
249 button &b = _key_bindings[k];
252 if (modifiers&FG_MOD_UP == 0) {
253 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
254 << " with modifiers " << modifiers );
255 if (!b.last_state || b.is_repeatable) {
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++)
269 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
270 << " with modifiers " << modifiers);
272 const binding_list_t &bindings =
273 _find_key_bindings(k, modifiers);
274 int max = bindings.size();
276 for (int i = 0; i < max; i++)
284 // Use the old, default actions.
285 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
286 if (modifiers&FG_MOD_UP)
289 // everything after here will be removed sooner or later...
291 if (modifiers & FG_MOD_SHIFT) {
295 HUD_brightkey( true );
299 fgHUDInit2(¤t_aircraft);
305 SG_LOG( SG_INPUT, SG_DEBUG, "" );
308 HUD_masterswitch( true );
311 fgHUDInit(¤t_aircraft); // normal HUD
316 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
317 if ( globals->get_autopilot()->get_HeadingMode() !=
318 FGAutopilot::FG_HEADING_WAYPOINT ) {
319 globals->get_autopilot()->set_HeadingMode(
320 FGAutopilot::FG_HEADING_WAYPOINT );
321 globals->get_autopilot()->set_HeadingEnabled( true );
323 globals->get_autopilot()->set_HeadingMode(
324 FGAutopilot::FG_TC_HEADING_LOCK );
327 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
328 const string &fog = fgGetString("/sim/rendering/fog");
329 if (fog == "disabled") {
330 fgSetString("/sim/rendering/fog", "fastest");
331 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
332 } else if (fog == "fastest") {
333 fgSetString("/sim/rendering/fog", "nicest");
334 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
335 } else if (fog == "nicest") {
336 fgSetString("/sim/rendering/fog", "disabled");
337 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
339 fgSetString("/sim/rendering/fog", "disabled");
340 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
341 << fog << ", changed to 'disabled'");
345 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
346 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
349 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
350 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
353 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
354 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
365 FGInput::doMouseClick (int b, int updown, int x, int y)
367 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
369 mouse &m = _mouse_bindings[0];
370 mouse_mode &mode = m.modes[m.current_mode];
372 // Let the property manager know.
373 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
374 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
376 // Pass on to PUI and the panel if
377 // requested, and return if one of
378 // them consumes the event.
379 if (mode.pass_through) {
380 if (puMouse(b, updown, x, y))
382 else if ((current_panel != 0) &&
383 current_panel->doMouseAction(b, updown, x, y))
387 // OK, PUI and the panel didn't want the click
388 if (b >= MAX_MOUSE_BUTTONS) {
389 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
390 << " where only " << MAX_MOUSE_BUTTONS << " expected");
394 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
398 FGInput::doMouseMotion (int x, int y)
400 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
402 int xsize = fgGetInt("/sim/startup/xsize", 800);
403 int ysize = fgGetInt("/sim/startup/ysize", 600);
404 mouse &m = _mouse_bindings[0];
405 if (m.current_mode < 0 || m.current_mode >= m.nModes)
407 mouse_mode &mode = m.modes[m.current_mode];
409 // Pass on to PUI if requested, and return
410 // if PUI consumed the event.
411 if (mode.pass_through && puMouse(x, y))
414 // OK, PUI didn't want the event,
415 // so we can play with it.
418 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
419 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
423 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
424 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
427 // Constrain the mouse if requested
428 if (mode.constrained) {
429 bool need_warp = false;
433 } else if (x >= (xsize-1)) {
441 } else if (y >= (ysize-1)) {
447 glutWarpPointer(x, y);
454 FGInput::_init_keyboard ()
456 // TODO: zero the old bindings first.
457 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
458 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
459 if (key_nodes == 0) {
460 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
461 key_nodes = fgGetNode("/input/keyboard", true);
464 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
465 for (unsigned int i = 0; i < keys.size(); i++) {
466 int index = keys[i]->getIndex();
467 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
468 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
469 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
475 FGInput::_init_joystick ()
477 // TODO: zero the old bindings first.
478 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
479 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
481 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
482 js_nodes = fgGetNode("/input/joysticks", true);
485 for (int i = 0; i < MAX_JOYSTICKS; i++) {
486 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
488 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
489 js_node = js_nodes->getChild("js", i, true);
491 jsJoystick * js = new jsJoystick(i);
492 _joystick_bindings[i].js = js;
493 if (js->notWorking()) {
494 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
497 #ifdef FG_PLIB_JOYSTICK_GETNAME
498 bool found_js = false;
499 const char * name = js->getName();
500 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
502 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
503 for (unsigned int i = 0; i < nodes.size(); i++) {
504 SGPropertyNode_ptr node = nodes[i];
505 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
506 for (unsigned int j = 0; j < name_nodes.size(); j++) {
507 const char * js_name = name_nodes[j]->getStringValue();
508 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
509 if (!strcmp(js_name, name)) {
510 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
523 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
524 int nbuttons = jsCaps.wNumButtons;
525 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
527 int nbuttons = MAX_JOYSTICK_BUTTONS;
530 int naxes = js->getNumAxes();
531 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
532 _joystick_bindings[i].naxes = naxes;
533 _joystick_bindings[i].nbuttons = nbuttons;
535 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
537 // Set up range arrays
538 float minRange[MAX_JOYSTICK_AXES];
539 float maxRange[MAX_JOYSTICK_AXES];
540 float center[MAX_JOYSTICK_AXES];
542 // Initialize with default values
543 js->getMinRange(minRange);
544 js->getMaxRange(maxRange);
545 js->getCenter(center);
547 // Allocate axes and buttons
548 _joystick_bindings[i].axes = new axis[naxes];
549 _joystick_bindings[i].buttons = new button[nbuttons];
553 // Initialize the axes.
556 for (j = 0; j < naxes; j++) {
557 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
558 if (axis_node == 0) {
559 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
560 axis_node = js_node->getChild("axis", j, true);
563 axis &a = _joystick_bindings[i].axes[j];
565 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
567 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
568 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
569 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
570 center[j] = axis_node->getDoubleValue("center", center[j]);
572 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
574 // Initialize the virtual axis buttons.
575 _init_button(axis_node->getChild("low"), a.low, "low");
576 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
578 _init_button(axis_node->getChild("high"), a.high, "high");
579 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
583 // Initialize the buttons.
586 for (j = 0; j < nbuttons; j++) {
587 sprintf(buf, "%d", j);
588 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
589 _init_button(js_node->getChild("button", j),
590 _joystick_bindings[i].buttons[j],
595 js->setMinRange(minRange);
596 js->setMaxRange(maxRange);
597 js->setCenter(center);
602 // Map of all known GLUT cursor names
607 } mouse_cursor_map[] = {
608 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
609 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
610 { "info", GLUT_CURSOR_INFO },
611 { "destroy", GLUT_CURSOR_DESTROY },
612 { "help", GLUT_CURSOR_HELP },
613 { "cycle", GLUT_CURSOR_CYCLE },
614 { "spray", GLUT_CURSOR_SPRAY },
615 { "wait", GLUT_CURSOR_WAIT },
616 { "text", GLUT_CURSOR_TEXT },
617 { "crosshair", GLUT_CURSOR_CROSSHAIR },
618 { "up-down", GLUT_CURSOR_UP_DOWN },
619 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
620 { "top-side", GLUT_CURSOR_TOP_SIDE },
621 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
622 { "left-side", GLUT_CURSOR_LEFT_SIDE },
623 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
624 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
625 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
626 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
627 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
628 { "inherit", GLUT_CURSOR_INHERIT },
629 { "none", GLUT_CURSOR_NONE },
630 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
637 FGInput::_init_mouse ()
639 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
641 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
642 if (mouse_nodes == 0) {
643 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
644 mouse_nodes = fgGetNode("/input/mice", true);
648 for (int i = 0; i < MAX_MICE; i++) {
649 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
650 mouse &m = _mouse_bindings[i];
652 // Grab node pointers
654 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
655 m.mode_node = fgGetNode(buf, true);
656 m.mode_node->setIntValue(0);
657 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
658 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
659 m.mouse_button_nodes[j] = fgGetNode(buf, true);
660 m.mouse_button_nodes[j]->setBoolValue(false);
663 // Read all the modes
664 m.nModes = mouse_node->getIntValue("mode-count", 1);
665 m.modes = new mouse_mode[m.nModes];
667 for (int j = 0; j < m.nModes; j++) {
670 // Read the mouse cursor for this mode
671 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
672 const char * cursor_name =
673 mode_node->getStringValue("cursor", "inherit");
674 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
675 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
676 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
677 m.modes[j].cursor = mouse_cursor_map[k].cursor;
682 // Read other properties for this mode
683 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
684 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
686 // Read the button bindings for this mode
687 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
689 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
690 sprintf(buf, "mouse button %d", k);
691 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
692 _init_button(mode_node->getChild("button", k),
693 m.modes[j].buttons[k],
697 // Read the axis bindings for this mode
698 _read_bindings(mode_node->getChild("x-axis", 0, true),
699 m.modes[j].x_bindings,
701 _read_bindings(mode_node->getChild("y-axis", 0, true),
702 m.modes[j].y_bindings,
710 FGInput::_init_button (const SGPropertyNode * node,
715 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
717 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
719 // Get the bindings for the button
720 _read_bindings(node, b.bindings, FG_MOD_NONE);
726 FGInput::_update_keyboard ()
733 FGInput::_update_joystick ()
735 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
737 // float js_val, diff;
738 float axis_values[MAX_JOYSTICK_AXES];
743 for ( i = 0; i < MAX_JOYSTICKS; i++) {
745 jsJoystick * js = _joystick_bindings[i].js;
746 if (js == 0 || js->notWorking())
749 js->read(&buttons, axis_values);
752 // Fire bindings for the axes.
753 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
754 axis &a = _joystick_bindings[i].axes[j];
756 // Do nothing if the axis position
757 // is unchanged; only a change in
758 // position fires the bindings.
759 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
760 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
762 a.last_value = axis_values[j];
763 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
764 // << a.bindings[modifiers].size() << " bindings");
765 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
766 a.bindings[modifiers][k]->fire(axis_values[j]);
769 // do we have to emulate axis buttons?
770 if (a.low.bindings[modifiers].size())
771 _update_button(_joystick_bindings[i].axes[j].low,
773 axis_values[j] < a.low_threshold,
776 if (a.high.bindings[modifiers].size())
777 _update_button(_joystick_bindings[i].axes[j].high,
779 axis_values[j] > a.high_threshold,
783 // Fire bindings for the buttons.
784 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
785 _update_button(_joystick_bindings[i].buttons[j],
787 (buttons & (1 << j)) > 0,
794 FGInput::_update_mouse ()
796 mouse &m = _mouse_bindings[0];
797 int mode = m.mode_node->getIntValue();
798 if (mode != m.current_mode) {
799 m.current_mode = mode;
800 if (mode >= 0 && mode < m.nModes) {
801 glutSetCursor(m.modes[mode].cursor);
802 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
803 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
804 glutWarpPointer(m.x, m.y);
806 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
807 glutSetCursor(GLUT_CURSOR_INHERIT);
813 FGInput::_update_button (button &b, int modifiers, bool pressed,
817 // The press event may be repeated.
818 if (!b.last_state || b.is_repeatable) {
819 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
820 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
821 b.bindings[modifiers][k]->fire(x, y);
824 // The release event is never repeated.
826 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
827 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
828 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
832 b.last_state = pressed;
837 FGInput::_read_bindings (const SGPropertyNode * node,
838 binding_list_t * binding_list,
841 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
842 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
843 for (unsigned int i = 0; i < bindings.size(); i++) {
844 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
845 << bindings[i]->getStringValue("command"));
846 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
849 // Read nested bindings for modifiers
850 if (node->getChild("mod-up") != 0)
851 _read_bindings(node->getChild("mod-up"), binding_list,
852 modifiers|FG_MOD_UP);
854 if (node->getChild("mod-shift") != 0)
855 _read_bindings(node->getChild("mod-shift"), binding_list,
856 modifiers|FG_MOD_SHIFT);
858 if (node->getChild("mod-ctrl") != 0)
859 _read_bindings(node->getChild("mod-ctrl"), binding_list,
860 modifiers|FG_MOD_CTRL);
862 if (node->getChild("mod-alt") != 0)
863 _read_bindings(node->getChild("mod-alt"), binding_list,
864 modifiers|FG_MOD_ALT);
868 const vector<FGBinding *> &
869 FGInput::_find_key_bindings (unsigned int k, int modifiers)
871 button &b = _key_bindings[k];
873 // Try it straight, first.
874 if (b.bindings[modifiers].size() > 0)
875 return b.bindings[modifiers];
877 // Try removing the control modifier
879 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
880 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
882 // Try removing shift modifier
883 // for upper case or any punctuation
884 // (since different keyboards will
885 // shift different punctuation types)
886 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
887 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
889 // Try removing alt modifier for
890 // high-bit characters.
891 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
892 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
894 // Give up and return the empty vector.
896 return b.bindings[modifiers];
901 ////////////////////////////////////////////////////////////////////////
902 // Implementation of FGInput::button.
903 ////////////////////////////////////////////////////////////////////////
905 FGInput::button::button ()
906 : is_repeatable(false),
911 FGInput::button::~button ()
913 // FIXME: memory leak
914 // for (int i = 0; i < FG_MOD_MAX; i++)
915 // for (int j = 0; i < bindings[i].size(); j++)
916 // delete bindings[i][j];
921 ////////////////////////////////////////////////////////////////////////
922 // Implementation of FGInput::axis.
923 ////////////////////////////////////////////////////////////////////////
925 FGInput::axis::axis ()
926 : last_value(9999999),
933 FGInput::axis::~axis ()
935 // for (int i = 0; i < FG_MOD_MAX; i++)
936 // for (int j = 0; i < bindings[i].size(); j++)
937 // delete bindings[i][j];
942 ////////////////////////////////////////////////////////////////////////
943 // Implementation of FGInput::joystick.
944 ////////////////////////////////////////////////////////////////////////
946 FGInput::joystick::joystick ()
950 FGInput::joystick::~joystick ()
959 ////////////////////////////////////////////////////////////////////////
960 // Implementation of FGInput::mouse_mode
961 ////////////////////////////////////////////////////////////////////////
963 FGInput::mouse_mode::mouse_mode ()
964 : cursor(GLUT_CURSOR_INHERIT),
971 FGInput::mouse_mode::~mouse_mode ()
973 // FIXME: memory leak
974 // for (int i = 0; i < FG_MOD_MAX; i++) {
976 // for (j = 0; i < x_bindings[i].size(); j++)
977 // delete bindings[i][j];
978 // for (j = 0; j < y_bindings[i].size(); j++)
979 // delete bindings[i][j];
986 ////////////////////////////////////////////////////////////////////////
987 // Implementation of FGInput::mouse
988 ////////////////////////////////////////////////////////////////////////
990 FGInput::mouse::mouse ()
999 FGInput::mouse::~mouse ()
1006 ////////////////////////////////////////////////////////////////////////
1007 // Implementation of GLUT callbacks.
1008 ////////////////////////////////////////////////////////////////////////
1012 * Construct the modifiers.
1014 static inline int get_mods ()
1016 int glut_modifiers = glutGetModifiers();
1019 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1020 modifiers |= FGInput::FG_MOD_SHIFT;
1021 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1022 modifiers |= FGInput::FG_MOD_CTRL;
1023 if (glut_modifiers & GLUT_ACTIVE_ALT)
1024 modifiers |= FGInput::FG_MOD_ALT;
1031 ////////////////////////////////////////////////////////////////////////
1032 // GLUT C callbacks.
1033 ////////////////////////////////////////////////////////////////////////
1036 GLUTkey(unsigned char k, int x, int y)
1038 // Give PUI a chance to grab it first.
1039 if (!puKeyboard(k, PU_DOWN)) {
1040 if (default_input != 0)
1041 default_input->doKey(k, get_mods(), x, y);
1046 GLUTkeyup(unsigned char k, int x, int y)
1048 if (default_input != 0)
1049 default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1053 GLUTspecialkey(int k, int x, int y)
1055 // Give PUI a chance to grab it first.
1056 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
1057 if (default_input != 0)
1058 default_input->doKey(k + 256, get_mods(), x, y);
1063 GLUTspecialkeyup(int k, int x, int y)
1065 if (default_input != 0)
1066 default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1070 GLUTmouse (int button, int updown, int x, int y)
1072 if (default_input != 0)
1073 default_input->doMouseClick(button, updown, x, y);
1077 GLUTmotion (int x, int y)
1079 if (default_input != 0)
1080 default_input->doMouseMotion(x, y);