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)
99 FGBinding::~FGBinding ()
101 delete _arg; // Delete the saved arguments
105 FGBinding::read (const SGPropertyNode * node)
107 const SGPropertyNode * conditionNode = node->getChild("condition");
108 if (conditionNode != 0)
109 setCondition(fgReadCondition(conditionNode));
111 _command_name = node->getStringValue("command", "");
112 if (_command_name.empty()) {
113 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
118 _command = globals->get_commands()->getCommand(_command_name);
120 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
126 _arg = new SGPropertyNode;
128 copyProperties(node, _arg); // FIXME: don't use whole node!!!
132 FGBinding::fire () const
136 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
137 } else if (!(*_command)(_arg)) {
138 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
145 FGBinding::fire (double offset, double max) const
148 _arg->setDoubleValue("offset", offset/max);
154 FGBinding::fire (double setting) const
157 // A value is automatically added to
159 if (_setting == 0) // save the setting node for efficiency
160 _setting = _arg->getChild("setting", 0, true);
161 _setting->setDoubleValue(setting);
168 ////////////////////////////////////////////////////////////////////////
169 // Implementation of FGInput.
170 ////////////////////////////////////////////////////////////////////////
173 extern void fgReshape( int width, int height );
178 if (default_input == 0)
179 default_input = this;
184 if (default_input == this)
195 glutKeyboardFunc(GLUTkey);
196 glutKeyboardUpFunc(GLUTkeyup);
197 glutSpecialFunc(GLUTspecialkey);
198 glutSpecialUpFunc(GLUTspecialkeyup);
199 glutMouseFunc (GLUTmouse);
200 glutMotionFunc (GLUTmotion);
201 glutPassiveMotionFunc (GLUTmotion);
217 FGInput::update (double dt)
225 FGInput::makeDefault (bool status)
228 default_input = this;
229 else if (default_input == this)
234 FGInput::doKey (int k, int modifiers, int x, int y)
237 if (k < 0 || k >= MAX_KEYS) {
238 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
242 button &b = _key_bindings[k];
245 if (modifiers&FG_MOD_UP == 0) {
246 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
247 << " with modifiers " << modifiers );
248 if (!b.last_state || b.is_repeatable) {
249 const binding_list_t &bindings =
250 _find_key_bindings(k, modifiers);
251 int max = bindings.size();
253 for (int i = 0; i < max; i++)
262 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
263 << " with modifiers " << modifiers);
265 const binding_list_t &bindings =
266 _find_key_bindings(k, modifiers);
267 int max = bindings.size();
269 for (int i = 0; i < max; i++)
277 // Use the old, default actions.
278 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
279 if (modifiers&FG_MOD_UP)
282 // everything after here will be removed sooner or later...
284 if (modifiers & FG_MOD_SHIFT) {
288 HUD_brightkey( true );
292 fgHUDInit2(¤t_aircraft);
298 SG_LOG( SG_INPUT, SG_DEBUG, "" );
301 HUD_masterswitch( true );
304 fgHUDInit(¤t_aircraft); // normal HUD
309 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
310 if ( globals->get_autopilot()->get_HeadingMode() !=
311 FGAutopilot::FG_HEADING_WAYPOINT ) {
312 globals->get_autopilot()->set_HeadingMode(
313 FGAutopilot::FG_HEADING_WAYPOINT );
314 globals->get_autopilot()->set_HeadingEnabled( true );
316 globals->get_autopilot()->set_HeadingMode(
317 FGAutopilot::FG_TC_HEADING_LOCK );
320 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
321 const string &fog = fgGetString("/sim/rendering/fog");
322 if (fog == "disabled") {
323 fgSetString("/sim/rendering/fog", "fastest");
324 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
325 } else if (fog == "fastest") {
326 fgSetString("/sim/rendering/fog", "nicest");
327 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
328 } else if (fog == "nicest") {
329 fgSetString("/sim/rendering/fog", "disabled");
330 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
332 fgSetString("/sim/rendering/fog", "disabled");
333 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
334 << fog << ", changed to 'disabled'");
338 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
339 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
342 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
343 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
346 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
347 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
358 FGInput::doMouseClick (int b, int updown, int x, int y)
360 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
362 mouse &m = _mouse_bindings[0];
363 mouse_mode &mode = m.modes[m.current_mode];
365 // Let the property manager know.
366 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
367 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
369 // Pass on to PUI and the panel if
370 // requested, and return if one of
371 // them consumes the event.
372 if (mode.pass_through) {
373 if (puMouse(b, updown, x, y))
375 else if ((current_panel != 0) &&
376 current_panel->getVisibility() &&
377 current_panel->doMouseAction(b, updown, x, y))
379 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
383 // OK, PUI and the panel didn't want the click
384 if (b >= MAX_MOUSE_BUTTONS) {
385 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
386 << " where only " << MAX_MOUSE_BUTTONS << " expected");
390 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
394 FGInput::doMouseMotion (int x, int y)
396 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
398 int xsize = fgGetInt("/sim/startup/xsize", 800);
399 int ysize = fgGetInt("/sim/startup/ysize", 600);
400 mouse &m = _mouse_bindings[0];
401 if (m.current_mode < 0 || m.current_mode >= m.nModes)
403 mouse_mode &mode = m.modes[m.current_mode];
405 // Pass on to PUI if requested, and return
406 // if PUI consumed the event.
407 if (mode.pass_through && puMouse(x, y))
410 // OK, PUI didn't want the event,
411 // so we can play with it.
414 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
415 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
419 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
420 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
423 // Constrain the mouse if requested
424 if (mode.constrained) {
425 bool need_warp = false;
429 } else if (x >= (xsize-1)) {
437 } else if (y >= (ysize-1)) {
443 glutWarpPointer(x, y);
450 FGInput::_init_keyboard ()
452 // TODO: zero the old bindings first.
453 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
454 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
455 if (key_nodes == 0) {
456 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
457 key_nodes = fgGetNode("/input/keyboard", true);
460 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
461 for (unsigned int i = 0; i < keys.size(); i++) {
462 int index = keys[i]->getIndex();
463 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
464 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
465 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
471 FGInput::_init_joystick ()
473 // TODO: zero the old bindings first.
474 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
475 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
477 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
478 js_nodes = fgGetNode("/input/joysticks", true);
481 for (int i = 0; i < MAX_JOYSTICKS; i++) {
482 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
484 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
485 js_node = js_nodes->getChild("js", i, true);
487 jsJoystick * js = new jsJoystick(i);
488 _joystick_bindings[i].js = js;
489 if (js->notWorking()) {
490 SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
493 #ifdef FG_PLIB_JOYSTICK_GETNAME
494 bool found_js = false;
495 const char * name = js->getName();
496 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
498 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
499 for (unsigned int i = 0; i < nodes.size(); i++) {
500 SGPropertyNode_ptr node = nodes[i];
501 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
502 for (unsigned int j = 0; j < name_nodes.size(); j++) {
503 const char * js_name = name_nodes[j]->getStringValue();
504 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
505 if (!strcmp(js_name, name)) {
506 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
519 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
520 int nbuttons = jsCaps.wNumButtons;
521 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
523 int nbuttons = MAX_JOYSTICK_BUTTONS;
526 int naxes = js->getNumAxes();
527 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
528 _joystick_bindings[i].naxes = naxes;
529 _joystick_bindings[i].nbuttons = nbuttons;
531 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
533 // Set up range arrays
534 float minRange[MAX_JOYSTICK_AXES];
535 float maxRange[MAX_JOYSTICK_AXES];
536 float center[MAX_JOYSTICK_AXES];
538 // Initialize with default values
539 js->getMinRange(minRange);
540 js->getMaxRange(maxRange);
541 js->getCenter(center);
543 // Allocate axes and buttons
544 _joystick_bindings[i].axes = new axis[naxes];
545 _joystick_bindings[i].buttons = new button[nbuttons];
549 // Initialize the axes.
552 for (j = 0; j < naxes; j++) {
553 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
554 if (axis_node == 0) {
555 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
556 axis_node = js_node->getChild("axis", j, true);
559 axis &a = _joystick_bindings[i].axes[j];
561 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
563 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
564 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
565 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
566 center[j] = axis_node->getDoubleValue("center", center[j]);
568 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
570 // Initialize the virtual axis buttons.
571 _init_button(axis_node->getChild("low"), a.low, "low");
572 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
574 _init_button(axis_node->getChild("high"), a.high, "high");
575 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
579 // Initialize the buttons.
582 for (j = 0; j < nbuttons; j++) {
583 sprintf(buf, "%d", j);
584 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
585 _init_button(js_node->getChild("button", j),
586 _joystick_bindings[i].buttons[j],
591 js->setMinRange(minRange);
592 js->setMaxRange(maxRange);
593 js->setCenter(center);
598 // Map of all known GLUT cursor names
603 } mouse_cursor_map[] = {
604 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
605 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
606 { "info", GLUT_CURSOR_INFO },
607 { "destroy", GLUT_CURSOR_DESTROY },
608 { "help", GLUT_CURSOR_HELP },
609 { "cycle", GLUT_CURSOR_CYCLE },
610 { "spray", GLUT_CURSOR_SPRAY },
611 { "wait", GLUT_CURSOR_WAIT },
612 { "text", GLUT_CURSOR_TEXT },
613 { "crosshair", GLUT_CURSOR_CROSSHAIR },
614 { "up-down", GLUT_CURSOR_UP_DOWN },
615 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
616 { "top-side", GLUT_CURSOR_TOP_SIDE },
617 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
618 { "left-side", GLUT_CURSOR_LEFT_SIDE },
619 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
620 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
621 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
622 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
623 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
624 { "inherit", GLUT_CURSOR_INHERIT },
625 { "none", GLUT_CURSOR_NONE },
626 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
633 FGInput::_init_mouse ()
635 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
637 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
638 if (mouse_nodes == 0) {
639 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
640 mouse_nodes = fgGetNode("/input/mice", true);
644 for (int i = 0; i < MAX_MICE; i++) {
645 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
646 mouse &m = _mouse_bindings[i];
648 // Grab node pointers
650 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
651 m.mode_node = fgGetNode(buf, true);
652 m.mode_node->setIntValue(0);
653 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
654 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
655 m.mouse_button_nodes[j] = fgGetNode(buf, true);
656 m.mouse_button_nodes[j]->setBoolValue(false);
659 // Read all the modes
660 m.nModes = mouse_node->getIntValue("mode-count", 1);
661 m.modes = new mouse_mode[m.nModes];
663 for (int j = 0; j < m.nModes; j++) {
666 // Read the mouse cursor for this mode
667 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
668 const char * cursor_name =
669 mode_node->getStringValue("cursor", "inherit");
670 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
671 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
672 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
673 m.modes[j].cursor = mouse_cursor_map[k].cursor;
678 // Read other properties for this mode
679 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
680 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
682 // Read the button bindings for this mode
683 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
685 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
686 sprintf(buf, "mouse button %d", k);
687 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
688 _init_button(mode_node->getChild("button", k),
689 m.modes[j].buttons[k],
693 // Read the axis bindings for this mode
694 _read_bindings(mode_node->getChild("x-axis", 0, true),
695 m.modes[j].x_bindings,
697 _read_bindings(mode_node->getChild("y-axis", 0, true),
698 m.modes[j].y_bindings,
706 FGInput::_init_button (const SGPropertyNode * node,
711 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
713 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
715 // Get the bindings for the button
716 _read_bindings(node, b.bindings, FG_MOD_NONE);
722 FGInput::_update_keyboard ()
729 FGInput::_update_joystick ()
731 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
733 // float js_val, diff;
734 float axis_values[MAX_JOYSTICK_AXES];
739 for ( i = 0; i < MAX_JOYSTICKS; i++) {
741 jsJoystick * js = _joystick_bindings[i].js;
742 if (js == 0 || js->notWorking())
745 js->read(&buttons, axis_values);
748 // Fire bindings for the axes.
749 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
750 axis &a = _joystick_bindings[i].axes[j];
752 // Do nothing if the axis position
753 // is unchanged; only a change in
754 // position fires the bindings.
755 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
756 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
758 a.last_value = axis_values[j];
759 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
760 // << a.bindings[modifiers].size() << " bindings");
761 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
762 a.bindings[modifiers][k]->fire(axis_values[j]);
765 // do we have to emulate axis buttons?
766 if (a.low.bindings[modifiers].size())
767 _update_button(_joystick_bindings[i].axes[j].low,
769 axis_values[j] < a.low_threshold,
772 if (a.high.bindings[modifiers].size())
773 _update_button(_joystick_bindings[i].axes[j].high,
775 axis_values[j] > a.high_threshold,
779 // Fire bindings for the buttons.
780 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
781 _update_button(_joystick_bindings[i].buttons[j],
783 (buttons & (1 << j)) > 0,
790 FGInput::_update_mouse ()
792 mouse &m = _mouse_bindings[0];
793 int mode = m.mode_node->getIntValue();
794 if (mode != m.current_mode) {
795 m.current_mode = mode;
796 if (mode >= 0 && mode < m.nModes) {
797 glutSetCursor(m.modes[mode].cursor);
798 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
799 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
800 glutWarpPointer(m.x, m.y);
802 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
803 glutSetCursor(GLUT_CURSOR_INHERIT);
809 FGInput::_update_button (button &b, int modifiers, bool pressed,
813 // The press event may be repeated.
814 if (!b.last_state || b.is_repeatable) {
815 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
816 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
817 b.bindings[modifiers][k]->fire(x, y);
820 // The release event is never repeated.
822 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
823 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
824 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
828 b.last_state = pressed;
833 FGInput::_read_bindings (const SGPropertyNode * node,
834 binding_list_t * binding_list,
837 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
838 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
839 for (unsigned int i = 0; i < bindings.size(); i++) {
840 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
841 << bindings[i]->getStringValue("command"));
842 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
845 // Read nested bindings for modifiers
846 if (node->getChild("mod-up") != 0)
847 _read_bindings(node->getChild("mod-up"), binding_list,
848 modifiers|FG_MOD_UP);
850 if (node->getChild("mod-shift") != 0)
851 _read_bindings(node->getChild("mod-shift"), binding_list,
852 modifiers|FG_MOD_SHIFT);
854 if (node->getChild("mod-ctrl") != 0)
855 _read_bindings(node->getChild("mod-ctrl"), binding_list,
856 modifiers|FG_MOD_CTRL);
858 if (node->getChild("mod-alt") != 0)
859 _read_bindings(node->getChild("mod-alt"), binding_list,
860 modifiers|FG_MOD_ALT);
864 const vector<FGBinding *> &
865 FGInput::_find_key_bindings (unsigned int k, int modifiers)
867 button &b = _key_bindings[k];
869 // Try it straight, first.
870 if (b.bindings[modifiers].size() > 0)
871 return b.bindings[modifiers];
873 // Try removing the control modifier
875 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
876 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
878 // Try removing shift modifier
879 // for upper case or any punctuation
880 // (since different keyboards will
881 // shift different punctuation types)
882 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
883 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
885 // Try removing alt modifier for
886 // high-bit characters.
887 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
888 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
890 // Give up and return the empty vector.
892 return b.bindings[modifiers];
897 ////////////////////////////////////////////////////////////////////////
898 // Implementation of FGInput::button.
899 ////////////////////////////////////////////////////////////////////////
901 FGInput::button::button ()
902 : is_repeatable(false),
907 FGInput::button::~button ()
909 // FIXME: memory leak
910 // for (int i = 0; i < FG_MOD_MAX; i++)
911 // for (int j = 0; i < bindings[i].size(); j++)
912 // delete bindings[i][j];
917 ////////////////////////////////////////////////////////////////////////
918 // Implementation of FGInput::axis.
919 ////////////////////////////////////////////////////////////////////////
921 FGInput::axis::axis ()
922 : last_value(9999999),
929 FGInput::axis::~axis ()
931 // for (int i = 0; i < FG_MOD_MAX; i++)
932 // for (int j = 0; i < bindings[i].size(); j++)
933 // delete bindings[i][j];
938 ////////////////////////////////////////////////////////////////////////
939 // Implementation of FGInput::joystick.
940 ////////////////////////////////////////////////////////////////////////
942 FGInput::joystick::joystick ()
946 FGInput::joystick::~joystick ()
955 ////////////////////////////////////////////////////////////////////////
956 // Implementation of FGInput::mouse_mode
957 ////////////////////////////////////////////////////////////////////////
959 FGInput::mouse_mode::mouse_mode ()
960 : cursor(GLUT_CURSOR_INHERIT),
967 FGInput::mouse_mode::~mouse_mode ()
969 // FIXME: memory leak
970 // for (int i = 0; i < FG_MOD_MAX; i++) {
972 // for (j = 0; i < x_bindings[i].size(); j++)
973 // delete bindings[i][j];
974 // for (j = 0; j < y_bindings[i].size(); j++)
975 // delete bindings[i][j];
982 ////////////////////////////////////////////////////////////////////////
983 // Implementation of FGInput::mouse
984 ////////////////////////////////////////////////////////////////////////
986 FGInput::mouse::mouse ()
995 FGInput::mouse::~mouse ()
1002 ////////////////////////////////////////////////////////////////////////
1003 // Implementation of GLUT callbacks.
1004 ////////////////////////////////////////////////////////////////////////
1008 * Construct the modifiers.
1010 static inline int get_mods ()
1012 int glut_modifiers = glutGetModifiers();
1015 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1016 modifiers |= FGInput::FG_MOD_SHIFT;
1017 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1018 modifiers |= FGInput::FG_MOD_CTRL;
1019 if (glut_modifiers & GLUT_ACTIVE_ALT)
1020 modifiers |= FGInput::FG_MOD_ALT;
1027 ////////////////////////////////////////////////////////////////////////
1028 // GLUT C callbacks.
1029 ////////////////////////////////////////////////////////////////////////
1032 GLUTkey(unsigned char k, int x, int y)
1034 // Give PUI a chance to grab it first.
1035 if (!puKeyboard(k, PU_DOWN)) {
1036 if (default_input != 0)
1037 default_input->doKey(k, get_mods(), x, y);
1042 GLUTkeyup(unsigned char k, int x, int y)
1044 if (default_input != 0)
1045 default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1049 GLUTspecialkey(int k, int x, int y)
1051 // Give PUI a chance to grab it first.
1052 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
1053 if (default_input != 0)
1054 default_input->doKey(k + 256, get_mods(), x, y);
1059 GLUTspecialkeyup(int k, int x, int y)
1061 if (default_input != 0)
1062 default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1066 GLUTmouse (int button, int updown, int x, int y)
1068 if (default_input != 0)
1069 default_input->doMouseClick(button, updown, x, y);
1073 GLUTmotion (int x, int y)
1075 if (default_input != 0)
1076 default_input->doMouseMotion(x, y);