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);
200 glutMouseFunc (GLUTmouse);
201 glutMotionFunc (GLUTmotion);
202 glutPassiveMotionFunc (GLUTmotion);
218 FGInput::update (double dt)
226 FGInput::doKey (int k, int modifiers, int x, int y)
228 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
229 << " with modifiers " << modifiers );
232 if (k < 0 || k >= MAX_KEYS) {
233 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
237 button &b = _key_bindings[k];
240 if (modifiers&FG_MOD_UP == 0) {
241 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
242 << " with modifiers " << modifiers );
243 if (!b.last_state || b.is_repeatable) {
244 const binding_list_t &bindings =
245 _find_key_bindings(k, modifiers);
246 int max = bindings.size();
248 for (int i = 0; i < max; i++)
257 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
258 << " with modifiers " << modifiers);
260 const binding_list_t &bindings =
261 _find_key_bindings(k, modifiers);
262 int max = bindings.size();
264 for (int i = 0; i < max; i++)
272 // Use the old, default actions.
273 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
274 if (modifiers&FG_MOD_UP)
277 // everything after here will be removed sooner or later...
279 if (modifiers & FG_MOD_SHIFT) {
283 HUD_brightkey( true );
287 fgHUDInit2(¤t_aircraft);
293 SG_LOG( SG_INPUT, SG_DEBUG, "" );
296 HUD_masterswitch( true );
299 fgHUDInit(¤t_aircraft); // normal HUD
304 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
305 if ( globals->get_autopilot()->get_HeadingMode() !=
306 FGAutopilot::FG_HEADING_WAYPOINT ) {
307 globals->get_autopilot()->set_HeadingMode(
308 FGAutopilot::FG_HEADING_WAYPOINT );
309 globals->get_autopilot()->set_HeadingEnabled( true );
311 globals->get_autopilot()->set_HeadingMode(
312 FGAutopilot::FG_TC_HEADING_LOCK );
315 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
316 const string &fog = fgGetString("/sim/rendering/fog");
317 if (fog == "disabled") {
318 fgSetString("/sim/rendering/fog", "fastest");
319 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
320 } else if (fog == "fastest") {
321 fgSetString("/sim/rendering/fog", "nicest");
322 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
323 } else if (fog == "nicest") {
324 fgSetString("/sim/rendering/fog", "disabled");
325 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
327 fgSetString("/sim/rendering/fog", "disabled");
328 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
329 << fog << ", changed to 'disabled'");
333 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
334 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
337 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
338 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
341 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
342 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
353 FGInput::doMouseClick (int b, int updown, int x, int y)
355 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
357 mouse &m = _mouse_bindings[0];
358 mouse_mode &mode = m.modes[m.current_mode];
360 // Let the property manager know.
361 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
362 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
364 // Pass on to PUI and the panel if
365 // requested, and return if one of
366 // them consumes the event.
367 if (mode.pass_through) {
368 if (puMouse(b, updown, x, y))
370 else if ((current_panel != 0) &&
371 current_panel->doMouseAction(b, updown, x, y))
375 // OK, PUI and the panel didn't want the click
376 if (b >= MAX_MOUSE_BUTTONS) {
377 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
378 << " where only " << MAX_MOUSE_BUTTONS << " expected");
382 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
386 FGInput::doMouseMotion (int x, int y)
388 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
390 int xsize = fgGetInt("/sim/startup/xsize", 800);
391 int ysize = fgGetInt("/sim/startup/ysize", 600);
392 mouse &m = _mouse_bindings[0];
393 if (m.current_mode < 0 || m.current_mode >= m.nModes)
395 mouse_mode &mode = m.modes[m.current_mode];
397 // Pass on to PUI if requested, and return
398 // if PUI consumed the event.
399 if (mode.pass_through && puMouse(x, y))
402 // OK, PUI didn't want the event,
403 // so we can play with it.
406 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
407 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
411 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
412 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
415 // Constrain the mouse if requested
416 if (mode.constrained) {
417 bool need_warp = false;
421 } else if (x >= (xsize-1)) {
429 } else if (y >= (ysize-1)) {
435 glutWarpPointer(x, y);
442 FGInput::_init_keyboard ()
444 // TODO: zero the old bindings first.
445 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
446 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
447 if (key_nodes == 0) {
448 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
449 key_nodes = fgGetNode("/input/keyboard", true);
452 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
453 for (unsigned int i = 0; i < keys.size(); i++) {
454 int index = keys[i]->getIndex();
455 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
456 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
457 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
463 FGInput::_init_joystick ()
465 // TODO: zero the old bindings first.
466 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
467 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
469 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
470 js_nodes = fgGetNode("/input/joysticks", true);
473 for (int i = 0; i < MAX_JOYSTICKS; i++) {
474 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
476 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
477 js_node = js_nodes->getChild("js", i, true);
479 jsJoystick * js = new jsJoystick(i);
480 _joystick_bindings[i].js = js;
481 if (js->notWorking()) {
482 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
485 #ifdef FG_PLIB_JOYSTICK_GETNAME
486 bool found_js = false;
487 const char * name = js->getName();
488 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
490 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
491 for (unsigned int i = 0; i < nodes.size(); i++) {
492 SGPropertyNode_ptr node = nodes[i];
493 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
494 for (unsigned int j = 0; j < name_nodes.size(); j++) {
495 const char * js_name = name_nodes[j]->getStringValue();
496 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
497 if (!strcmp(js_name, name)) {
498 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
511 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
512 int nbuttons = jsCaps.wNumButtons;
513 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
515 int nbuttons = MAX_JOYSTICK_BUTTONS;
518 int naxes = js->getNumAxes();
519 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
520 _joystick_bindings[i].naxes = naxes;
521 _joystick_bindings[i].nbuttons = nbuttons;
523 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
525 // Set up range arrays
526 float minRange[MAX_JOYSTICK_AXES];
527 float maxRange[MAX_JOYSTICK_AXES];
528 float center[MAX_JOYSTICK_AXES];
530 // Initialize with default values
531 js->getMinRange(minRange);
532 js->getMaxRange(maxRange);
533 js->getCenter(center);
535 // Allocate axes and buttons
536 _joystick_bindings[i].axes = new axis[naxes];
537 _joystick_bindings[i].buttons = new button[nbuttons];
541 // Initialize the axes.
544 for (j = 0; j < naxes; j++) {
545 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
546 if (axis_node == 0) {
547 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
548 axis_node = js_node->getChild("axis", j, true);
551 axis &a = _joystick_bindings[i].axes[j];
553 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
555 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
556 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
557 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
558 center[j] = axis_node->getDoubleValue("center", center[j]);
560 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
562 // Initialize the virtual axis buttons.
563 _init_button(axis_node->getChild("low"), a.low, "low");
564 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
566 _init_button(axis_node->getChild("high"), a.high, "high");
567 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
571 // Initialize the buttons.
574 for (j = 0; j < nbuttons; j++) {
575 sprintf(buf, "%d", j);
576 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
577 _init_button(js_node->getChild("button", j),
578 _joystick_bindings[i].buttons[j],
583 js->setMinRange(minRange);
584 js->setMaxRange(maxRange);
585 js->setCenter(center);
590 // Map of all known GLUT cursor names
595 } mouse_cursor_map[] = {
596 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
597 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
598 { "info", GLUT_CURSOR_INFO },
599 { "destroy", GLUT_CURSOR_DESTROY },
600 { "help", GLUT_CURSOR_HELP },
601 { "cycle", GLUT_CURSOR_CYCLE },
602 { "spray", GLUT_CURSOR_SPRAY },
603 { "wait", GLUT_CURSOR_WAIT },
604 { "text", GLUT_CURSOR_TEXT },
605 { "crosshair", GLUT_CURSOR_CROSSHAIR },
606 { "up-down", GLUT_CURSOR_UP_DOWN },
607 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
608 { "top-side", GLUT_CURSOR_TOP_SIDE },
609 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
610 { "left-side", GLUT_CURSOR_LEFT_SIDE },
611 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
612 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
613 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
614 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
615 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
616 { "inherit", GLUT_CURSOR_INHERIT },
617 { "none", GLUT_CURSOR_NONE },
618 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
625 FGInput::_init_mouse ()
627 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
629 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
630 if (mouse_nodes == 0) {
631 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
632 mouse_nodes = fgGetNode("/input/mice", true);
636 for (int i = 0; i < MAX_MICE; i++) {
637 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
638 mouse &m = _mouse_bindings[i];
640 // Grab node pointers
642 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
643 m.mode_node = fgGetNode(buf, true);
644 m.mode_node->setIntValue(0);
645 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
646 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
647 m.mouse_button_nodes[j] = fgGetNode(buf, true);
648 m.mouse_button_nodes[j]->setBoolValue(false);
651 // Read all the modes
652 m.nModes = mouse_node->getIntValue("mode-count", 1);
653 m.modes = new mouse_mode[m.nModes];
655 for (int j = 0; j < m.nModes; j++) {
658 // Read the mouse cursor for this mode
659 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
660 const char * cursor_name =
661 mode_node->getStringValue("cursor", "inherit");
662 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
663 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
664 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
665 m.modes[j].cursor = mouse_cursor_map[k].cursor;
670 // Read other properties for this mode
671 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
672 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
674 // Read the button bindings for this mode
675 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
677 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
678 sprintf(buf, "mouse button %d", k);
679 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
680 _init_button(mode_node->getChild("button", k),
681 m.modes[j].buttons[k],
685 // Read the axis bindings for this mode
686 _read_bindings(mode_node->getChild("x-axis", 0, true),
687 m.modes[j].x_bindings,
689 _read_bindings(mode_node->getChild("y-axis", 0, true),
690 m.modes[j].y_bindings,
698 FGInput::_init_button (const SGPropertyNode * node,
703 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
705 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
707 // Get the bindings for the button
708 _read_bindings(node, b.bindings, FG_MOD_NONE);
714 FGInput::_update_keyboard ()
721 FGInput::_update_joystick ()
723 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
725 // float js_val, diff;
726 float axis_values[MAX_JOYSTICK_AXES];
731 for ( i = 0; i < MAX_JOYSTICKS; i++) {
733 jsJoystick * js = _joystick_bindings[i].js;
734 if (js == 0 || js->notWorking())
737 js->read(&buttons, axis_values);
740 // Fire bindings for the axes.
741 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
742 axis &a = _joystick_bindings[i].axes[j];
744 // Do nothing if the axis position
745 // is unchanged; only a change in
746 // position fires the bindings.
747 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
748 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
750 a.last_value = axis_values[j];
751 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
752 // << a.bindings[modifiers].size() << " bindings");
753 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
754 a.bindings[modifiers][k]->fire(axis_values[j]);
757 // do we have to emulate axis buttons?
758 if (a.low.bindings[modifiers].size())
759 _update_button(_joystick_bindings[i].axes[j].low,
761 axis_values[j] < a.low_threshold,
764 if (a.high.bindings[modifiers].size())
765 _update_button(_joystick_bindings[i].axes[j].high,
767 axis_values[j] > a.high_threshold,
771 // Fire bindings for the buttons.
772 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
773 _update_button(_joystick_bindings[i].buttons[j],
775 (buttons & (1 << j)) > 0,
782 FGInput::_update_mouse ()
784 mouse &m = _mouse_bindings[0];
785 int mode = m.mode_node->getIntValue();
786 if (mode != m.current_mode) {
787 m.current_mode = mode;
788 if (mode >= 0 && mode < m.nModes) {
789 glutSetCursor(m.modes[mode].cursor);
790 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
791 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
792 glutWarpPointer(m.x, m.y);
794 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
795 glutSetCursor(GLUT_CURSOR_INHERIT);
801 FGInput::_update_button (button &b, int modifiers, bool pressed,
805 // The press event may be repeated.
806 if (!b.last_state || b.is_repeatable) {
807 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
808 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
809 b.bindings[modifiers][k]->fire(x, y);
812 // The release event is never repeated.
814 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
815 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
816 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
820 b.last_state = pressed;
825 FGInput::_read_bindings (const SGPropertyNode * node,
826 binding_list_t * binding_list,
829 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
830 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
831 for (unsigned int i = 0; i < bindings.size(); i++) {
832 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
833 << bindings[i]->getStringValue("command"));
834 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
837 // Read nested bindings for modifiers
838 if (node->getChild("mod-up") != 0)
839 _read_bindings(node->getChild("mod-up"), binding_list,
840 modifiers|FG_MOD_UP);
842 if (node->getChild("mod-shift") != 0)
843 _read_bindings(node->getChild("mod-shift"), binding_list,
844 modifiers|FG_MOD_SHIFT);
846 if (node->getChild("mod-ctrl") != 0)
847 _read_bindings(node->getChild("mod-ctrl"), binding_list,
848 modifiers|FG_MOD_CTRL);
850 if (node->getChild("mod-alt") != 0)
851 _read_bindings(node->getChild("mod-alt"), binding_list,
852 modifiers|FG_MOD_ALT);
856 const vector<FGBinding *> &
857 FGInput::_find_key_bindings (unsigned int k, int modifiers)
859 button &b = _key_bindings[k];
861 // Try it straight, first.
862 if (b.bindings[modifiers].size() > 0)
863 return b.bindings[modifiers];
865 // Try removing the control modifier
867 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
868 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
870 // Try removing shift modifier
871 // for upper case or any punctuation
872 // (since different keyboards will
873 // shift different punctuation types)
874 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
875 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
877 // Try removing alt modifier for
878 // high-bit characters.
879 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
880 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
882 // Give up and return the empty vector.
884 return b.bindings[modifiers];
889 ////////////////////////////////////////////////////////////////////////
890 // Implementation of FGInput::button.
891 ////////////////////////////////////////////////////////////////////////
893 FGInput::button::button ()
894 : is_repeatable(false),
899 FGInput::button::~button ()
901 // FIXME: memory leak
902 // for (int i = 0; i < FG_MOD_MAX; i++)
903 // for (int j = 0; i < bindings[i].size(); j++)
904 // delete bindings[i][j];
909 ////////////////////////////////////////////////////////////////////////
910 // Implementation of FGInput::axis.
911 ////////////////////////////////////////////////////////////////////////
913 FGInput::axis::axis ()
914 : last_value(9999999),
921 FGInput::axis::~axis ()
923 // for (int i = 0; i < FG_MOD_MAX; i++)
924 // for (int j = 0; i < bindings[i].size(); j++)
925 // delete bindings[i][j];
930 ////////////////////////////////////////////////////////////////////////
931 // Implementation of FGInput::joystick.
932 ////////////////////////////////////////////////////////////////////////
934 FGInput::joystick::joystick ()
938 FGInput::joystick::~joystick ()
947 ////////////////////////////////////////////////////////////////////////
948 // Implementation of FGInput::mouse_mode
949 ////////////////////////////////////////////////////////////////////////
951 FGInput::mouse_mode::mouse_mode ()
952 : cursor(GLUT_CURSOR_INHERIT),
959 FGInput::mouse_mode::~mouse_mode ()
961 // FIXME: memory leak
962 // for (int i = 0; i < FG_MOD_MAX; i++) {
964 // for (j = 0; i < x_bindings[i].size(); j++)
965 // delete bindings[i][j];
966 // for (j = 0; j < y_bindings[i].size(); j++)
967 // delete bindings[i][j];
974 ////////////////////////////////////////////////////////////////////////
975 // Implementation of FGInput::mouse
976 ////////////////////////////////////////////////////////////////////////
978 FGInput::mouse::mouse ()
987 FGInput::mouse::~mouse ()
994 ////////////////////////////////////////////////////////////////////////
995 // Implementation of GLUT callbacks.
996 ////////////////////////////////////////////////////////////////////////
1000 * Construct the modifiers.
1002 static inline int get_mods ()
1004 int glut_modifiers = glutGetModifiers();
1007 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1008 modifiers |= FGInput::FG_MOD_SHIFT;
1009 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1010 modifiers |= FGInput::FG_MOD_CTRL;
1011 if (glut_modifiers & GLUT_ACTIVE_ALT)
1012 modifiers |= FGInput::FG_MOD_ALT;
1019 ////////////////////////////////////////////////////////////////////////
1020 // GLUT C callbacks.
1021 ////////////////////////////////////////////////////////////////////////
1024 GLUTkey(unsigned char k, int x, int y)
1026 // Give PUI a chance to grab it first.
1027 if (!puKeyboard(k, PU_DOWN))
1028 current_input.doKey(k, get_mods(), x, y);
1032 GLUTkeyup(unsigned char k, int x, int y)
1034 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1038 GLUTspecialkey(int k, int x, int y)
1040 // Give PUI a chance to grab it first.
1041 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
1042 current_input.doKey(k + 256, get_mods(), x, y);
1046 GLUTspecialkeyup(int k, int x, int y)
1048 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1052 GLUTmouse (int button, int updown, int x, int y)
1054 current_input.doMouseClick(button, updown, x, y);
1058 GLUTmotion (int x, int y)
1060 current_input.doMouseMotion(x, y);