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 SG_USING_STD(ifstream);
70 ////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////
74 static FGInput * default_input = 0;
78 ////////////////////////////////////////////////////////////////////////
79 // Implementation of FGBinding.
80 ////////////////////////////////////////////////////////////////////////
82 FGBinding::FGBinding ()
84 _arg(new SGPropertyNode),
89 FGBinding::FGBinding (const SGPropertyNode * node)
97 FGBinding::~FGBinding ()
99 delete _arg; // Delete the saved arguments
103 FGBinding::read (const SGPropertyNode * node)
105 const SGPropertyNode * conditionNode = node->getChild("condition");
106 if (conditionNode != 0)
107 setCondition(fgReadCondition(conditionNode));
109 _command_name = node->getStringValue("command", "");
110 if (_command_name.empty()) {
111 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
116 _command = globals->get_commands()->getCommand(_command_name);
118 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
124 _arg = new SGPropertyNode;
126 copyProperties(node, _arg); // FIXME: don't use whole node!!!
130 FGBinding::fire () const
134 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
135 } else if (!(*_command)(_arg)) {
136 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
143 FGBinding::fire (double offset, double max) const
146 _arg->setDoubleValue("offset", offset/max);
152 FGBinding::fire (double setting) const
155 // A value is automatically added to
157 if (_setting == 0) // save the setting node for efficiency
158 _setting = _arg->getChild("setting", 0, true);
159 _setting->setDoubleValue(setting);
166 ////////////////////////////////////////////////////////////////////////
167 // Implementation of FGInput.
168 ////////////////////////////////////////////////////////////////////////
171 extern void fgReshape( int width, int height );
176 if (default_input == 0)
177 default_input = this;
182 if (default_input == this)
193 glutKeyboardFunc(GLUTkey);
194 glutKeyboardUpFunc(GLUTkeyup);
195 glutSpecialFunc(GLUTspecialkey);
196 glutSpecialUpFunc(GLUTspecialkeyup);
197 glutMouseFunc (GLUTmouse);
198 glutMotionFunc (GLUTmotion);
199 glutPassiveMotionFunc (GLUTmotion);
215 FGInput::update (double dt)
223 FGInput::makeDefault (bool status)
226 default_input = this;
227 else if (default_input == this)
232 FGInput::doKey (int k, int modifiers, int x, int y)
235 if (k < 0 || k >= MAX_KEYS) {
236 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
240 button &b = _key_bindings[k];
243 if (modifiers&FG_MOD_UP == 0) {
244 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
245 << " with modifiers " << modifiers );
246 if (!b.last_state || b.is_repeatable) {
247 const binding_list_t &bindings =
248 _find_key_bindings(k, modifiers);
249 int max = bindings.size();
251 for (int i = 0; i < max; i++)
260 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
261 << " with modifiers " << modifiers);
263 const binding_list_t &bindings =
264 _find_key_bindings(k, modifiers);
265 int max = bindings.size();
267 for (int i = 0; i < max; i++)
275 // Use the old, default actions.
276 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
277 if (modifiers&FG_MOD_UP)
280 // everything after here will be removed sooner or later...
282 if (modifiers & FG_MOD_SHIFT) {
286 HUD_brightkey( true );
290 fgHUDInit2(¤t_aircraft);
296 SG_LOG( SG_INPUT, SG_DEBUG, "" );
299 HUD_masterswitch( true );
302 fgHUDInit(¤t_aircraft); // normal HUD
307 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
308 if ( globals->get_autopilot()->get_HeadingMode() !=
309 FGAutopilot::FG_HEADING_WAYPOINT ) {
310 globals->get_autopilot()->set_HeadingMode(
311 FGAutopilot::FG_HEADING_WAYPOINT );
312 globals->get_autopilot()->set_HeadingEnabled( true );
314 globals->get_autopilot()->set_HeadingMode(
315 FGAutopilot::FG_TC_HEADING_LOCK );
318 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
319 const string &fog = fgGetString("/sim/rendering/fog");
320 if (fog == "disabled") {
321 fgSetString("/sim/rendering/fog", "fastest");
322 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
323 } else if (fog == "fastest") {
324 fgSetString("/sim/rendering/fog", "nicest");
325 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
326 } else if (fog == "nicest") {
327 fgSetString("/sim/rendering/fog", "disabled");
328 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
330 fgSetString("/sim/rendering/fog", "disabled");
331 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
332 << fog << ", changed to 'disabled'");
336 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
337 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
338 #if defined(FG_OLD_MENUBAR)
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 bool found_js = false;
494 const char * name = js->getName();
495 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
497 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
498 for (unsigned int i = 0; i < nodes.size(); i++) {
499 SGPropertyNode_ptr node = nodes[i];
500 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
501 for (unsigned int j = 0; j < name_nodes.size(); j++) {
502 const char * js_name = name_nodes[j]->getStringValue();
503 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
504 if (!strcmp(js_name, name)) {
505 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
517 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
518 int nbuttons = jsCaps.wNumButtons;
519 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
521 int nbuttons = MAX_JOYSTICK_BUTTONS;
524 int naxes = js->getNumAxes();
525 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
526 _joystick_bindings[i].naxes = naxes;
527 _joystick_bindings[i].nbuttons = nbuttons;
529 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
531 // Set up range arrays
532 float minRange[MAX_JOYSTICK_AXES];
533 float maxRange[MAX_JOYSTICK_AXES];
534 float center[MAX_JOYSTICK_AXES];
536 // Initialize with default values
537 js->getMinRange(minRange);
538 js->getMaxRange(maxRange);
539 js->getCenter(center);
541 // Allocate axes and buttons
542 _joystick_bindings[i].axes = new axis[naxes];
543 _joystick_bindings[i].buttons = new button[nbuttons];
547 // Initialize the axes.
550 for (j = 0; j < naxes; j++) {
551 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
552 if (axis_node == 0) {
553 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
554 axis_node = js_node->getChild("axis", j, true);
557 axis &a = _joystick_bindings[i].axes[j];
559 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
561 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
562 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
563 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
564 center[j] = axis_node->getDoubleValue("center", center[j]);
566 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
568 // Initialize the virtual axis buttons.
569 _init_button(axis_node->getChild("low"), a.low, "low");
570 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
572 _init_button(axis_node->getChild("high"), a.high, "high");
573 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
577 // Initialize the buttons.
580 for (j = 0; j < nbuttons; j++) {
581 sprintf(buf, "%d", j);
582 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
583 _init_button(js_node->getChild("button", j),
584 _joystick_bindings[i].buttons[j],
589 js->setMinRange(minRange);
590 js->setMaxRange(maxRange);
591 js->setCenter(center);
596 // Map of all known GLUT cursor names
601 } mouse_cursor_map[] = {
602 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
603 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
604 { "info", GLUT_CURSOR_INFO },
605 { "destroy", GLUT_CURSOR_DESTROY },
606 { "help", GLUT_CURSOR_HELP },
607 { "cycle", GLUT_CURSOR_CYCLE },
608 { "spray", GLUT_CURSOR_SPRAY },
609 { "wait", GLUT_CURSOR_WAIT },
610 { "text", GLUT_CURSOR_TEXT },
611 { "crosshair", GLUT_CURSOR_CROSSHAIR },
612 { "up-down", GLUT_CURSOR_UP_DOWN },
613 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
614 { "top-side", GLUT_CURSOR_TOP_SIDE },
615 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
616 { "left-side", GLUT_CURSOR_LEFT_SIDE },
617 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
618 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
619 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
620 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
621 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
622 { "inherit", GLUT_CURSOR_INHERIT },
623 { "none", GLUT_CURSOR_NONE },
624 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
631 FGInput::_init_mouse ()
633 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
635 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
636 if (mouse_nodes == 0) {
637 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
638 mouse_nodes = fgGetNode("/input/mice", true);
642 for (int i = 0; i < MAX_MICE; i++) {
643 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
644 mouse &m = _mouse_bindings[i];
646 // Grab node pointers
648 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
649 m.mode_node = fgGetNode(buf, true);
650 m.mode_node->setIntValue(0);
651 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
652 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
653 m.mouse_button_nodes[j] = fgGetNode(buf, true);
654 m.mouse_button_nodes[j]->setBoolValue(false);
657 // Read all the modes
658 m.nModes = mouse_node->getIntValue("mode-count", 1);
659 m.modes = new mouse_mode[m.nModes];
661 for (int j = 0; j < m.nModes; j++) {
664 // Read the mouse cursor for this mode
665 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
666 const char * cursor_name =
667 mode_node->getStringValue("cursor", "inherit");
668 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
669 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
670 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
671 m.modes[j].cursor = mouse_cursor_map[k].cursor;
676 // Read other properties for this mode
677 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
678 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
680 // Read the button bindings for this mode
681 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
683 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
684 sprintf(buf, "mouse button %d", k);
685 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
686 _init_button(mode_node->getChild("button", k),
687 m.modes[j].buttons[k],
691 // Read the axis bindings for this mode
692 _read_bindings(mode_node->getChild("x-axis", 0, true),
693 m.modes[j].x_bindings,
695 _read_bindings(mode_node->getChild("y-axis", 0, true),
696 m.modes[j].y_bindings,
704 FGInput::_init_button (const SGPropertyNode * node,
709 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
711 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
713 // Get the bindings for the button
714 _read_bindings(node, b.bindings, FG_MOD_NONE);
720 FGInput::_update_keyboard ()
727 FGInput::_update_joystick ()
729 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
731 // float js_val, diff;
732 float axis_values[MAX_JOYSTICK_AXES];
737 for ( i = 0; i < MAX_JOYSTICKS; i++) {
739 jsJoystick * js = _joystick_bindings[i].js;
740 if (js == 0 || js->notWorking())
743 js->read(&buttons, axis_values);
746 // Fire bindings for the axes.
747 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
748 axis &a = _joystick_bindings[i].axes[j];
750 // Do nothing if the axis position
751 // is unchanged; only a change in
752 // position fires the bindings.
753 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
754 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
756 a.last_value = axis_values[j];
757 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
758 // << a.bindings[modifiers].size() << " bindings");
759 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
760 a.bindings[modifiers][k]->fire(axis_values[j]);
763 // do we have to emulate axis buttons?
764 if (a.low.bindings[modifiers].size())
765 _update_button(_joystick_bindings[i].axes[j].low,
767 axis_values[j] < a.low_threshold,
770 if (a.high.bindings[modifiers].size())
771 _update_button(_joystick_bindings[i].axes[j].high,
773 axis_values[j] > a.high_threshold,
777 // Fire bindings for the buttons.
778 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
779 _update_button(_joystick_bindings[i].buttons[j],
781 (buttons & (1 << j)) > 0,
788 FGInput::_update_mouse ()
790 mouse &m = _mouse_bindings[0];
791 int mode = m.mode_node->getIntValue();
792 if (mode != m.current_mode) {
793 m.current_mode = mode;
794 if (mode >= 0 && mode < m.nModes) {
795 glutSetCursor(m.modes[mode].cursor);
796 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
797 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
798 glutWarpPointer(m.x, m.y);
800 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
801 glutSetCursor(GLUT_CURSOR_INHERIT);
807 FGInput::_update_button (button &b, int modifiers, bool pressed,
811 // The press event may be repeated.
812 if (!b.last_state || b.is_repeatable) {
813 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
814 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
815 b.bindings[modifiers][k]->fire(x, y);
818 // The release event is never repeated.
820 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
821 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
822 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
826 b.last_state = pressed;
831 FGInput::_read_bindings (const SGPropertyNode * node,
832 binding_list_t * binding_list,
835 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
836 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
837 for (unsigned int i = 0; i < bindings.size(); i++) {
838 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
839 << bindings[i]->getStringValue("command"));
840 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
843 // Read nested bindings for modifiers
844 if (node->getChild("mod-up") != 0)
845 _read_bindings(node->getChild("mod-up"), binding_list,
846 modifiers|FG_MOD_UP);
848 if (node->getChild("mod-shift") != 0)
849 _read_bindings(node->getChild("mod-shift"), binding_list,
850 modifiers|FG_MOD_SHIFT);
852 if (node->getChild("mod-ctrl") != 0)
853 _read_bindings(node->getChild("mod-ctrl"), binding_list,
854 modifiers|FG_MOD_CTRL);
856 if (node->getChild("mod-alt") != 0)
857 _read_bindings(node->getChild("mod-alt"), binding_list,
858 modifiers|FG_MOD_ALT);
862 const vector<FGBinding *> &
863 FGInput::_find_key_bindings (unsigned int k, int modifiers)
865 button &b = _key_bindings[k];
867 // Try it straight, first.
868 if (b.bindings[modifiers].size() > 0)
869 return b.bindings[modifiers];
871 // Try removing the control modifier
873 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
874 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
876 // Try removing shift modifier
877 // for upper case or any punctuation
878 // (since different keyboards will
879 // shift different punctuation types)
880 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
881 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
883 // Try removing alt modifier for
884 // high-bit characters.
885 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
886 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
888 // Give up and return the empty vector.
890 return b.bindings[modifiers];
895 ////////////////////////////////////////////////////////////////////////
896 // Implementation of FGInput::button.
897 ////////////////////////////////////////////////////////////////////////
899 FGInput::button::button ()
900 : is_repeatable(false),
905 FGInput::button::~button ()
907 // FIXME: memory leak
908 // for (int i = 0; i < FG_MOD_MAX; i++)
909 // for (int j = 0; i < bindings[i].size(); j++)
910 // delete bindings[i][j];
915 ////////////////////////////////////////////////////////////////////////
916 // Implementation of FGInput::axis.
917 ////////////////////////////////////////////////////////////////////////
919 FGInput::axis::axis ()
920 : last_value(9999999),
927 FGInput::axis::~axis ()
929 // for (int i = 0; i < FG_MOD_MAX; i++)
930 // for (int j = 0; i < bindings[i].size(); j++)
931 // delete bindings[i][j];
936 ////////////////////////////////////////////////////////////////////////
937 // Implementation of FGInput::joystick.
938 ////////////////////////////////////////////////////////////////////////
940 FGInput::joystick::joystick ()
944 FGInput::joystick::~joystick ()
953 ////////////////////////////////////////////////////////////////////////
954 // Implementation of FGInput::mouse_mode
955 ////////////////////////////////////////////////////////////////////////
957 FGInput::mouse_mode::mouse_mode ()
958 : cursor(GLUT_CURSOR_INHERIT),
965 FGInput::mouse_mode::~mouse_mode ()
967 // FIXME: memory leak
968 // for (int i = 0; i < FG_MOD_MAX; i++) {
970 // for (j = 0; i < x_bindings[i].size(); j++)
971 // delete bindings[i][j];
972 // for (j = 0; j < y_bindings[i].size(); j++)
973 // delete bindings[i][j];
980 ////////////////////////////////////////////////////////////////////////
981 // Implementation of FGInput::mouse
982 ////////////////////////////////////////////////////////////////////////
984 FGInput::mouse::mouse ()
993 FGInput::mouse::~mouse ()
1000 ////////////////////////////////////////////////////////////////////////
1001 // Implementation of GLUT callbacks.
1002 ////////////////////////////////////////////////////////////////////////
1006 * Construct the modifiers.
1008 static inline int get_mods ()
1010 int glut_modifiers = glutGetModifiers();
1013 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1014 modifiers |= FGInput::FG_MOD_SHIFT;
1015 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1016 modifiers |= FGInput::FG_MOD_CTRL;
1017 if (glut_modifiers & GLUT_ACTIVE_ALT)
1018 modifiers |= FGInput::FG_MOD_ALT;
1025 ////////////////////////////////////////////////////////////////////////
1026 // GLUT C callbacks.
1027 ////////////////////////////////////////////////////////////////////////
1030 GLUTkey(unsigned char k, int x, int y)
1032 // Give PUI a chance to grab it first.
1033 if (!puKeyboard(k, PU_DOWN)) {
1034 if (default_input != 0)
1035 default_input->doKey(k, get_mods(), x, y);
1040 GLUTkeyup(unsigned char k, int x, int y)
1042 if (default_input != 0)
1043 default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1047 GLUTspecialkey(int k, int x, int y)
1049 // Give PUI a chance to grab it first.
1050 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
1051 if (default_input != 0)
1052 default_input->doKey(k + 256, get_mods(), x, y);
1057 GLUTspecialkeyup(int k, int x, int y)
1059 if (default_input != 0)
1060 default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1064 GLUTmouse (int button, int updown, int x, int y)
1066 if (default_input != 0)
1067 default_input->doMouseClick(button, updown, x, y);
1071 GLUTmotion (int x, int y)
1073 if (default_input != 0)
1074 default_input->doMouseMotion(x, y);