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 (double offset, double max) const
149 _arg->setDoubleValue("offset", offset/max);
155 FGBinding::fire (double setting) const
158 // A value is automatically added to
160 if (_setting == 0) // save the setting node for efficiency
161 _setting = _arg->getChild("setting", 0, true);
162 _setting->setDoubleValue(setting);
169 ////////////////////////////////////////////////////////////////////////
170 // Implementation of FGInput.
171 ////////////////////////////////////////////////////////////////////////
174 extern void fgReshape( int width, int height );
176 FGInput current_input;
196 glutKeyboardFunc(GLUTkey);
197 glutKeyboardUpFunc(GLUTkeyup);
198 glutSpecialFunc(GLUTspecialkey);
199 glutSpecialUpFunc(GLUTspecialkeyup);
201 glutMouseFunc (GLUTmouse);
202 glutMotionFunc (GLUTmotion);
203 glutPassiveMotionFunc (GLUTmotion);
220 FGInput::update (int dt)
228 FGInput::doKey (int k, int modifiers, int x, int y)
230 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
231 << " with modifiers " << modifiers );
234 if (k < 0 || k >= MAX_KEYS) {
235 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
239 button &b = _key_bindings[k];
242 if (modifiers&FG_MOD_UP == 0) {
243 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
244 << " with modifiers " << modifiers );
245 if (!b.last_state || b.is_repeatable) {
246 const binding_list_t &bindings =
247 _find_key_bindings(k, modifiers);
248 int max = bindings.size();
250 for (int i = 0; i < max; i++)
259 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
260 << " with modifiers " << modifiers);
262 const binding_list_t &bindings =
263 _find_key_bindings(k, modifiers);
264 int max = bindings.size();
266 for (int i = 0; i < max; i++)
274 // Use the old, default actions.
275 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
276 if (modifiers&FG_MOD_UP)
279 // everything after here will be removed sooner or later...
281 if (modifiers & FG_MOD_SHIFT) {
285 HUD_brightkey( true );
289 fgHUDInit2(¤t_aircraft);
295 SG_LOG( SG_INPUT, SG_DEBUG, "" );
298 HUD_masterswitch( true );
301 fgHUDInit(¤t_aircraft); // normal HUD
306 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
307 if ( globals->get_autopilot()->get_HeadingMode() !=
308 FGAutopilot::FG_HEADING_WAYPOINT ) {
309 globals->get_autopilot()->set_HeadingMode(
310 FGAutopilot::FG_HEADING_WAYPOINT );
311 globals->get_autopilot()->set_HeadingEnabled( true );
313 globals->get_autopilot()->set_HeadingMode(
314 FGAutopilot::FG_TC_HEADING_LOCK );
317 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
318 const string &fog = fgGetString("/sim/rendering/fog");
319 if (fog == "disabled") {
320 fgSetString("/sim/rendering/fog", "fastest");
321 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
322 } else if (fog == "fastest") {
323 fgSetString("/sim/rendering/fog", "nicest");
324 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
325 } else if (fog == "nicest") {
326 fgSetString("/sim/rendering/fog", "disabled");
327 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
329 fgSetString("/sim/rendering/fog", "disabled");
330 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
331 << fog << ", changed to 'disabled'");
335 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
336 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
339 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
340 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
343 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
344 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
355 FGInput::doMouseClick (int b, int updown, int x, int y)
357 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
359 mouse &m = _mouse_bindings[0];
360 mouse_mode &mode = m.modes[m.current_mode];
362 // Let the property manager know.
363 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
364 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
366 // Pass on to PUI and the panel if
367 // requested, and return if one of
368 // them consumes the event.
369 if (mode.pass_through) {
370 if (puMouse(b, updown, x, y))
372 else if ((current_panel != 0) &&
373 current_panel->doMouseAction(b, updown, x, y))
377 // OK, PUI and the panel didn't want the click
378 if (b >= MAX_MOUSE_BUTTONS) {
379 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
380 << " where only " << MAX_MOUSE_BUTTONS << " expected");
384 _update_button(m.modes[m.current_mode].buttons[b], modifiers, updown, x, y);
388 FGInput::doMouseMotion (int x, int y)
390 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
392 int xsize = fgGetInt("/sim/startup/xsize", 800);
393 int ysize = fgGetInt("/sim/startup/ysize", 600);
394 mouse &m = _mouse_bindings[0];
395 if (m.current_mode < 0 || m.current_mode >= m.nModes)
397 mouse_mode &mode = m.modes[m.current_mode];
399 // Pass on to PUI if requested, and return
400 // if PUI consumed the event.
401 if (mode.pass_through && puMouse(x, y))
404 // OK, PUI didn't want the event,
405 // so we can play with it.
408 for (int i = 0; i < mode.x_bindings[modifiers].size(); i++)
409 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
413 for (int i = 0; i < mode.y_bindings[modifiers].size(); i++)
414 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
417 // Constrain the mouse if requested
418 if (mode.constrained) {
419 bool need_warp = false;
423 } else if (x >= (xsize-1)) {
431 } else if (y >= (ysize-1)) {
437 glutWarpPointer(x, y);
444 FGInput::_init_keyboard ()
446 // TODO: zero the old bindings first.
447 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
448 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
449 if (key_nodes == 0) {
450 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
451 key_nodes = fgGetNode("/input/keyboard", true);
454 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
455 for (unsigned int i = 0; i < keys.size(); i++) {
456 int index = keys[i]->getIndex();
457 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
458 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
459 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
465 FGInput::_init_joystick ()
467 // TODO: zero the old bindings first.
468 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
469 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
471 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
472 js_nodes = fgGetNode("/input/joysticks", true);
475 for (int i = 0; i < MAX_JOYSTICKS; i++) {
476 SGPropertyNode * js_node = js_nodes->getChild("js", i);
478 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
479 js_node = js_nodes->getChild("js", i, true);
481 jsJoystick * js = new jsJoystick(i);
482 _joystick_bindings[i].js = js;
483 if (js->notWorking()) {
484 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
489 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
490 int nbuttons = jsCaps.wNumButtons;
491 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
493 int nbuttons = MAX_JOYSTICK_BUTTONS;
496 int naxes = js->getNumAxes();
497 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
498 _joystick_bindings[i].naxes = naxes;
499 _joystick_bindings[i].nbuttons = nbuttons;
501 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
503 // Set up range arrays
504 float minRange[MAX_JOYSTICK_AXES];
505 float maxRange[MAX_JOYSTICK_AXES];
506 float center[MAX_JOYSTICK_AXES];
508 // Initialize with default values
509 js->getMinRange(minRange);
510 js->getMaxRange(maxRange);
511 js->getCenter(center);
513 // Allocate axes and buttons
514 _joystick_bindings[i].axes = new axis[naxes];
515 _joystick_bindings[i].buttons = new button[nbuttons];
519 // Initialize the axes.
522 for (j = 0; j < naxes; j++) {
523 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
524 if (axis_node == 0) {
525 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
526 axis_node = js_node->getChild("axis", j, true);
529 axis &a = _joystick_bindings[i].axes[j];
531 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
533 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
534 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
535 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
536 center[j] = axis_node->getDoubleValue("center", center[j]);
538 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
540 // Initialize the virtual axis buttons.
541 _init_button(axis_node->getChild("low"), a.low, "low");
542 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
544 _init_button(axis_node->getChild("high"), a.high, "high");
545 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
549 // Initialize the buttons.
552 for (j = 0; j < nbuttons; j++) {
553 sprintf(buf, "%d", j);
554 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
555 _init_button(js_node->getChild("button", j),
556 _joystick_bindings[i].buttons[j],
561 js->setMinRange(minRange);
562 js->setMaxRange(maxRange);
563 js->setCenter(center);
568 // Map of all known GLUT cursor names
573 } mouse_cursor_map[] = {
574 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
575 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
576 { "info", GLUT_CURSOR_INFO },
577 { "destroy", GLUT_CURSOR_DESTROY },
578 { "help", GLUT_CURSOR_HELP },
579 { "cycle", GLUT_CURSOR_CYCLE },
580 { "spray", GLUT_CURSOR_SPRAY },
581 { "wait", GLUT_CURSOR_WAIT },
582 { "text", GLUT_CURSOR_TEXT },
583 { "crosshair", GLUT_CURSOR_CROSSHAIR },
584 { "up-down", GLUT_CURSOR_UP_DOWN },
585 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
586 { "top-side", GLUT_CURSOR_TOP_SIDE },
587 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
588 { "left-side", GLUT_CURSOR_LEFT_SIDE },
589 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
590 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
591 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
592 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
593 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
594 { "inherit", GLUT_CURSOR_INHERIT },
595 { "none", GLUT_CURSOR_NONE },
596 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
603 FGInput::_init_mouse ()
605 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
607 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
608 if (mouse_nodes == 0) {
609 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
610 mouse_nodes = fgGetNode("/input/mice", true);
614 for (int i = 0; i < MAX_MICE; i++) {
615 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
616 mouse &m = _mouse_bindings[i];
618 // Grab node pointers
620 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
621 m.mode_node = fgGetNode(buf, true);
622 m.mode_node->setIntValue(0);
623 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
624 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
625 m.mouse_button_nodes[j] = fgGetNode(buf, true);
626 m.mouse_button_nodes[j]->setBoolValue(false);
629 // Read all the modes
630 m.nModes = mouse_node->getIntValue("mode-count", 1);
631 m.modes = new mouse_mode[m.nModes];
633 for (int j = 0; j < m.nModes; j++) {
636 // Read the mouse cursor for this mode
637 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
638 const char * cursor_name =
639 mode_node->getStringValue("cursor", "inherit");
640 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
641 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
642 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
643 m.modes[j].cursor = mouse_cursor_map[k].cursor;
648 // Read other properties for this mode
649 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
650 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
652 // Read the button bindings for this mode
653 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
655 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
656 sprintf(buf, "mouse button %d", k);
657 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
658 _init_button(mode_node->getChild("button", k),
659 m.modes[j].buttons[k],
663 // Read the axis bindings for this mode
664 _read_bindings(mode_node->getChild("x-axis", 0, true),
665 m.modes[j].x_bindings,
667 _read_bindings(mode_node->getChild("y-axis", 0, true),
668 m.modes[j].y_bindings,
676 FGInput::_init_button (const SGPropertyNode * node,
681 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
683 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
685 // Get the bindings for the button
686 _read_bindings(node, b.bindings, FG_MOD_NONE);
692 FGInput::_update_keyboard ()
699 FGInput::_update_joystick ()
701 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
703 // float js_val, diff;
704 float axis_values[MAX_JOYSTICK_AXES];
709 for ( i = 0; i < MAX_JOYSTICKS; i++) {
711 jsJoystick * js = _joystick_bindings[i].js;
712 if (js == 0 || js->notWorking())
715 js->read(&buttons, axis_values);
718 // Fire bindings for the axes.
719 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
720 axis &a = _joystick_bindings[i].axes[j];
722 // Do nothing if the axis position
723 // is unchanged; only a change in
724 // position fires the bindings.
725 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
726 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
728 a.last_value = axis_values[j];
729 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
730 // << a.bindings[modifiers].size() << " bindings");
731 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
732 a.bindings[modifiers][k]->fire(axis_values[j]);
735 // do we have to emulate axis buttons?
736 if (a.low.bindings[modifiers].size())
737 _update_button(_joystick_bindings[i].axes[j].low,
739 axis_values[j] < a.low_threshold,
742 if (a.high.bindings[modifiers].size())
743 _update_button(_joystick_bindings[i].axes[j].high,
745 axis_values[j] > a.high_threshold,
749 // Fire bindings for the buttons.
750 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
751 _update_button(_joystick_bindings[i].buttons[j],
753 (buttons & (1 << j)) > 0,
760 FGInput::_update_mouse ()
762 mouse &m = _mouse_bindings[0];
763 int mode = m.mode_node->getIntValue();
764 if (mode != m.current_mode) {
765 m.current_mode = mode;
766 if (mode >= 0 && mode < m.nModes) {
767 glutSetCursor(m.modes[mode].cursor);
768 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
769 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
770 glutWarpPointer(m.x, m.y);
772 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
773 glutSetCursor(GLUT_CURSOR_INHERIT);
779 FGInput::_update_button (button &b, int modifiers, bool pressed,
783 // The press event may be repeated.
784 if (!b.last_state || b.is_repeatable) {
785 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
786 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
787 b.bindings[modifiers][k]->fire(x, y);
790 // The release event is never repeated.
792 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
793 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
794 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
798 b.last_state = pressed;
803 FGInput::_read_bindings (const SGPropertyNode * node,
804 binding_list_t * binding_list,
807 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
808 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
809 for (unsigned int i = 0; i < bindings.size(); i++) {
810 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
811 << bindings[i]->getStringValue("command"));
812 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
815 // Read nested bindings for modifiers
816 if (node->getChild("mod-up") != 0)
817 _read_bindings(node->getChild("mod-up"), binding_list,
818 modifiers|FG_MOD_UP);
820 if (node->getChild("mod-shift") != 0)
821 _read_bindings(node->getChild("mod-shift"), binding_list,
822 modifiers|FG_MOD_SHIFT);
824 if (node->getChild("mod-ctrl") != 0)
825 _read_bindings(node->getChild("mod-ctrl"), binding_list,
826 modifiers|FG_MOD_CTRL);
828 if (node->getChild("mod-alt") != 0)
829 _read_bindings(node->getChild("mod-alt"), binding_list,
830 modifiers|FG_MOD_ALT);
834 const vector<FGBinding *> &
835 FGInput::_find_key_bindings (unsigned int k, int modifiers)
837 button &b = _key_bindings[k];
839 // Try it straight, first.
840 if (b.bindings[modifiers].size() > 0)
841 return b.bindings[modifiers];
843 // Try removing the control modifier
845 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
846 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
848 // Try removing shift modifier
849 // for upper case or any punctuation
850 // (since different keyboards will
851 // shift different punctuation types)
852 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
853 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
855 // Try removing alt modifier for
856 // high-bit characters.
857 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
858 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
860 // Give up and return the empty vector.
862 return b.bindings[modifiers];
867 ////////////////////////////////////////////////////////////////////////
868 // Implementation of FGInput::button.
869 ////////////////////////////////////////////////////////////////////////
871 FGInput::button::button ()
872 : is_repeatable(false),
877 FGInput::button::~button ()
879 // FIXME: memory leak
880 // for (int i = 0; i < FG_MOD_MAX; i++)
881 // for (int j = 0; i < bindings[i].size(); j++)
882 // delete bindings[i][j];
887 ////////////////////////////////////////////////////////////////////////
888 // Implementation of FGInput::axis.
889 ////////////////////////////////////////////////////////////////////////
891 FGInput::axis::axis ()
892 : last_value(9999999),
899 FGInput::axis::~axis ()
901 // for (int i = 0; i < FG_MOD_MAX; i++)
902 // for (int j = 0; i < bindings[i].size(); j++)
903 // delete bindings[i][j];
908 ////////////////////////////////////////////////////////////////////////
909 // Implementation of FGInput::joystick.
910 ////////////////////////////////////////////////////////////////////////
912 FGInput::joystick::joystick ()
916 FGInput::joystick::~joystick ()
925 ////////////////////////////////////////////////////////////////////////
926 // Implementation of FGInput::mouse_mode
927 ////////////////////////////////////////////////////////////////////////
929 FGInput::mouse_mode::mouse_mode ()
930 : cursor(GLUT_CURSOR_INHERIT),
937 FGInput::mouse_mode::~mouse_mode ()
939 // FIXME: memory leak
940 // for (int i = 0; i < FG_MOD_MAX; i++) {
942 // for (j = 0; i < x_bindings[i].size(); j++)
943 // delete bindings[i][j];
944 // for (j = 0; j < y_bindings[i].size(); j++)
945 // delete bindings[i][j];
952 ////////////////////////////////////////////////////////////////////////
953 // Implementation of FGInput::mouse
954 ////////////////////////////////////////////////////////////////////////
956 FGInput::mouse::mouse ()
965 FGInput::mouse::~mouse ()
972 ////////////////////////////////////////////////////////////////////////
973 // Implementation of GLUT callbacks.
974 ////////////////////////////////////////////////////////////////////////
978 * Construct the modifiers.
980 static inline int get_mods ()
982 int glut_modifiers = glutGetModifiers();
985 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
986 modifiers |= FGInput::FG_MOD_SHIFT;
987 if (glut_modifiers & GLUT_ACTIVE_CTRL)
988 modifiers |= FGInput::FG_MOD_CTRL;
989 if (glut_modifiers & GLUT_ACTIVE_ALT)
990 modifiers |= FGInput::FG_MOD_ALT;
997 ////////////////////////////////////////////////////////////////////////
999 ////////////////////////////////////////////////////////////////////////
1002 GLUTkey(unsigned char k, int x, int y)
1004 // Give PUI a chance to grab it first.
1005 if (!puKeyboard(k, PU_DOWN))
1006 current_input.doKey(k, get_mods(), x, y);
1010 GLUTkeyup(unsigned char k, int x, int y)
1012 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1016 GLUTspecialkey(int k, int x, int y)
1018 // Give PUI a chance to grab it first.
1019 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
1020 current_input.doKey(k + 256, get_mods(), x, y);
1024 GLUTspecialkeyup(int k, int x, int y)
1026 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1030 GLUTmouse (int button, int updown, int x, int y)
1032 current_input.doMouseClick(button, updown, x, y);
1036 GLUTmotion (int x, int y)
1038 current_input.doMouseMotion(x, y);