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/props/props.hxx>
50 #include <Aircraft/aircraft.hxx>
51 // #include <Autopilot/auto_gui.hxx>
52 #include <Autopilot/xmlauto.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)
98 FGBinding::read (const SGPropertyNode * node)
100 const SGPropertyNode * conditionNode = node->getChild("condition");
101 if (conditionNode != 0)
102 setCondition(sgReadCondition(globals->get_props(), conditionNode));
104 _command_name = node->getStringValue("command", "");
105 if (_command_name.empty()) {
106 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
111 _arg = new SGPropertyNode;
113 copyProperties(node, _arg); // FIXME: don't use whole node!!!
117 FGBinding::fire () const
121 _command = globals->get_commands()->getCommand(_command_name);
123 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
124 } else if (!(*_command)(_arg)) {
125 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
132 FGBinding::fire (double offset, double max) const
135 _arg->setDoubleValue("offset", offset/max);
141 FGBinding::fire (double setting) const
144 // A value is automatically added to
146 if (_setting == 0) // save the setting node for efficiency
147 _setting = _arg->getChild("setting", 0, true);
148 _setting->setDoubleValue(setting);
155 ////////////////////////////////////////////////////////////////////////
156 // Implementation of FGInput.
157 ////////////////////////////////////////////////////////////////////////
162 if (default_input == 0)
163 default_input = this;
168 if (default_input == this)
179 glutKeyboardFunc(GLUTkey);
180 glutKeyboardUpFunc(GLUTkeyup);
181 glutSpecialFunc(GLUTspecialkey);
182 glutSpecialUpFunc(GLUTspecialkeyup);
183 glutMouseFunc (GLUTmouse);
184 glutMotionFunc (GLUTmotion);
185 glutPassiveMotionFunc (GLUTmotion);
189 FGInput::update (double dt)
192 _update_joystick(dt);
209 FGInput::is_suspended () const
215 FGInput::makeDefault (bool status)
218 default_input = this;
219 else if (default_input == this)
224 FGInput::doKey (int k, int modifiers, int x, int y)
226 static SGPropertyNode *heading_enabled
227 = fgGetNode("/autopilot/locks/heading", true);
230 if (k < 0 || k >= MAX_KEYS) {
231 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
235 button &b = _key_bindings[k];
238 if (modifiers&FG_MOD_UP == 0) {
239 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
240 << " with modifiers " << modifiers );
241 if (!b.last_state || b.is_repeatable) {
242 const binding_list_t &bindings =
243 _find_key_bindings(k, modifiers);
244 int max = bindings.size();
246 for (int i = 0; i < max; i++)
255 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
256 << " with modifiers " << modifiers);
258 const binding_list_t &bindings =
259 _find_key_bindings(k, modifiers);
260 int max = bindings.size();
262 for (int i = 0; i < max; i++)
270 // Use the old, default actions.
271 SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
272 if (modifiers&FG_MOD_UP)
275 // everything after here will be removed sooner or later...
277 if (modifiers & FG_MOD_SHIFT) {
281 HUD_brightkey( true );
285 fgHUDInit2(¤t_aircraft);
291 SG_LOG( SG_INPUT, SG_DEBUG, "" );
294 HUD_masterswitch( true );
297 fgHUDInit(¤t_aircraft); // normal HUD
302 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
303 if ( strcmp( heading_enabled->getStringValue(),
304 "true-heading-hold" ) != 0 ) {
305 heading_enabled->setStringValue( "true-heading-hold" );
307 heading_enabled->setStringValue( "" );
318 FGInput::doMouseClick (int b, int updown, int x, int y)
320 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
322 mouse &m = _mouse_bindings[0];
323 mouse_mode &mode = m.modes[m.current_mode];
325 // Let the property manager know.
326 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
327 m.mouse_button_nodes[b]->setBoolValue(updown == GLUT_DOWN);
329 // Pass on to PUI and the panel if
330 // requested, and return if one of
331 // them consumes the event.
332 if (mode.pass_through) {
333 if (puMouse(b, updown, x, y))
335 else if ((globals->get_current_panel() != 0) &&
336 globals->get_current_panel()->getVisibility() &&
337 globals->get_current_panel()->doMouseAction(b, updown, x, y))
339 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
343 // OK, PUI and the panel didn't want the click
344 if (b >= MAX_MOUSE_BUTTONS) {
345 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
346 << " where only " << MAX_MOUSE_BUTTONS << " expected");
350 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
354 FGInput::doMouseMotion (int x, int y)
356 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
358 int xsize = fgGetInt("/sim/startup/xsize", 800);
359 int ysize = fgGetInt("/sim/startup/ysize", 600);
360 mouse &m = _mouse_bindings[0];
361 if (m.current_mode < 0 || m.current_mode >= m.nModes)
363 mouse_mode &mode = m.modes[m.current_mode];
365 // Pass on to PUI if requested, and return
366 // if PUI consumed the event.
367 if (mode.pass_through && puMouse(x, y))
370 // OK, PUI didn't want the event,
371 // so we can play with it.
374 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
375 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
379 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
380 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
383 // Constrain the mouse if requested
384 if (mode.constrained) {
385 bool need_warp = false;
389 } else if (x >= (xsize-1)) {
397 } else if (y >= (ysize-1)) {
403 glutWarpPointer(x, y);
410 FGInput::_init_keyboard ()
412 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
413 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
414 if (key_nodes == 0) {
415 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
416 key_nodes = fgGetNode("/input/keyboard", true);
419 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
420 for (unsigned int i = 0; i < keys.size(); i++) {
421 int index = keys[i]->getIndex();
422 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
424 _key_bindings[index].bindings->clear();
425 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
426 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
432 FGInput::_init_joystick ()
434 // TODO: zero the old bindings first.
435 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
436 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
438 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
439 js_nodes = fgGetNode("/input/joysticks", true);
442 for (int i = 0; i < MAX_JOYSTICKS; i++) {
443 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
445 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
446 js_node = js_nodes->getChild("js", i, true);
448 jsJoystick * js = new jsJoystick(i);
449 _joystick_bindings[i].js = js;
450 if (js->notWorking()) {
451 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
454 bool found_js = false;
455 const char * name = js->getName();
456 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
458 vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
459 for (unsigned int i = 0; i < nodes.size(); i++) {
460 SGPropertyNode_ptr node = nodes[i];
461 vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
462 for (unsigned int j = 0; j < name_nodes.size(); j++) {
463 const char * js_name = name_nodes[j]->getStringValue();
464 SG_LOG(SG_INPUT, SG_INFO, " Trying \"" << js_name << '"');
465 if (!strcmp(js_name, name)) {
466 SG_LOG(SG_INPUT, SG_INFO, " Found bindings");
478 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
479 int nbuttons = jsCaps.wNumButtons;
480 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
482 int nbuttons = MAX_JOYSTICK_BUTTONS;
485 int naxes = js->getNumAxes();
486 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
487 _joystick_bindings[i].naxes = naxes;
488 _joystick_bindings[i].nbuttons = nbuttons;
490 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
492 // Set up range arrays
493 float minRange[MAX_JOYSTICK_AXES];
494 float maxRange[MAX_JOYSTICK_AXES];
495 float center[MAX_JOYSTICK_AXES];
497 // Initialize with default values
498 js->getMinRange(minRange);
499 js->getMaxRange(maxRange);
500 js->getCenter(center);
502 // Allocate axes and buttons
503 _joystick_bindings[i].axes = new axis[naxes];
504 _joystick_bindings[i].buttons = new button[nbuttons];
508 // Initialize the axes.
511 for (j = 0; j < naxes; j++) {
512 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
513 if (axis_node == 0) {
514 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
515 axis_node = js_node->getChild("axis", j, true);
518 axis &a = _joystick_bindings[i].axes[j];
520 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
522 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
523 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
524 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
525 center[j] = axis_node->getDoubleValue("center", center[j]);
527 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
529 // Initialize the virtual axis buttons.
530 _init_button(axis_node->getChild("low"), a.low, "low");
531 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
533 _init_button(axis_node->getChild("high"), a.high, "high");
534 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
535 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
540 // Initialize the buttons.
543 for (j = 0; j < nbuttons; j++) {
544 sprintf(buf, "%d", j);
545 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
546 _init_button(js_node->getChild("button", j),
547 _joystick_bindings[i].buttons[j],
550 // get interval-sec property
551 button &b = _joystick_bindings[i].buttons[j];
552 const SGPropertyNode * button_node = js_node->getChild("button", j);
553 if (button_node != 0) {
554 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
559 js->setMinRange(minRange);
560 js->setMaxRange(maxRange);
561 js->setCenter(center);
566 // Map of all known GLUT cursor names
571 } mouse_cursor_map[] = {
572 { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
573 { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
574 { "info", GLUT_CURSOR_INFO },
575 { "destroy", GLUT_CURSOR_DESTROY },
576 { "help", GLUT_CURSOR_HELP },
577 { "cycle", GLUT_CURSOR_CYCLE },
578 { "spray", GLUT_CURSOR_SPRAY },
579 { "wait", GLUT_CURSOR_WAIT },
580 { "text", GLUT_CURSOR_TEXT },
581 { "crosshair", GLUT_CURSOR_CROSSHAIR },
582 { "up-down", GLUT_CURSOR_UP_DOWN },
583 { "left-right", GLUT_CURSOR_LEFT_RIGHT },
584 { "top-side", GLUT_CURSOR_TOP_SIDE },
585 { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
586 { "left-side", GLUT_CURSOR_LEFT_SIDE },
587 { "right-side", GLUT_CURSOR_RIGHT_SIDE },
588 { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
589 { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
590 { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
591 { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
592 { "inherit", GLUT_CURSOR_INHERIT },
593 { "none", GLUT_CURSOR_NONE },
594 { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
601 FGInput::_init_mouse ()
603 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
605 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
606 if (mouse_nodes == 0) {
607 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
608 mouse_nodes = fgGetNode("/input/mice", true);
612 for (int i = 0; i < MAX_MICE; i++) {
613 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
614 mouse &m = _mouse_bindings[i];
616 // Grab node pointers
618 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
619 m.mode_node = fgGetNode(buf);
620 if (m.mode_node == NULL) {
621 m.mode_node = fgGetNode(buf, true);
622 m.mode_node->setIntValue(0);
624 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
625 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
626 m.mouse_button_nodes[j] = fgGetNode(buf, true);
627 m.mouse_button_nodes[j]->setBoolValue(false);
630 // Read all the modes
631 m.nModes = mouse_node->getIntValue("mode-count", 1);
632 m.modes = new mouse_mode[m.nModes];
634 for (int j = 0; j < m.nModes; j++) {
637 // Read the mouse cursor for this mode
638 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
639 const char * cursor_name =
640 mode_node->getStringValue("cursor", "inherit");
641 m.modes[j].cursor = GLUT_CURSOR_INHERIT;
642 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
643 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
644 m.modes[j].cursor = mouse_cursor_map[k].cursor;
649 // Read other properties for this mode
650 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
651 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
653 // Read the button bindings for this mode
654 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
656 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
657 sprintf(buf, "mouse button %d", k);
658 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
659 _init_button(mode_node->getChild("button", k),
660 m.modes[j].buttons[k],
664 // Read the axis bindings for this mode
665 _read_bindings(mode_node->getChild("x-axis", 0, true),
666 m.modes[j].x_bindings,
668 _read_bindings(mode_node->getChild("y-axis", 0, true),
669 m.modes[j].y_bindings,
677 FGInput::_init_button (const SGPropertyNode * node,
682 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
684 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
686 // Get the bindings for the button
687 _read_bindings(node, b.bindings, FG_MOD_NONE);
693 FGInput::_update_keyboard ()
700 FGInput::_update_joystick (double dt)
702 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
704 // float js_val, diff;
705 float axis_values[MAX_JOYSTICK_AXES];
710 for ( i = 0; i < MAX_JOYSTICKS; i++) {
712 jsJoystick * js = _joystick_bindings[i].js;
713 if (js == 0 || js->notWorking())
716 js->read(&buttons, axis_values);
719 // Fire bindings for the axes.
720 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
721 axis &a = _joystick_bindings[i].axes[j];
723 // Do nothing if the axis position
724 // is unchanged; only a change in
725 // position fires the bindings.
726 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
727 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
729 a.last_value = axis_values[j];
730 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
731 // << a.bindings[modifiers].size() << " bindings");
732 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
733 a.bindings[modifiers][k]->fire(axis_values[j]);
736 // do we have to emulate axis buttons?
738 if(a.last_dt >= a.interval_sec) {
739 if (a.low.bindings[modifiers].size())
740 _update_button(_joystick_bindings[i].axes[j].low,
742 axis_values[j] < a.low_threshold,
745 if (a.high.bindings[modifiers].size())
746 _update_button(_joystick_bindings[i].axes[j].high,
748 axis_values[j] > a.high_threshold,
750 a.last_dt -= a.interval_sec;
754 // Fire bindings for the buttons.
755 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
756 button &b = _joystick_bindings[i].buttons[j];
758 if(b.last_dt >= b.interval_sec) {
759 _update_button(_joystick_bindings[i].buttons[j],
761 (buttons & (1 << j)) > 0,
763 b.last_dt -= b.interval_sec;
770 FGInput::_update_mouse ()
772 mouse &m = _mouse_bindings[0];
773 int mode = m.mode_node->getIntValue();
774 if (mode != m.current_mode) {
775 m.current_mode = mode;
776 if (mode >= 0 && mode < m.nModes) {
777 glutSetCursor(m.modes[mode].cursor);
778 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
779 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
780 glutWarpPointer(m.x, m.y);
782 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
783 glutSetCursor(GLUT_CURSOR_INHERIT);
789 FGInput::_update_button (button &b, int modifiers, bool pressed,
793 // The press event may be repeated.
794 if (!b.last_state || b.is_repeatable) {
795 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
796 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
797 b.bindings[modifiers][k]->fire(x, y);
800 // The release event is never repeated.
802 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
803 for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
804 b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
808 b.last_state = pressed;
813 FGInput::_read_bindings (const SGPropertyNode * node,
814 binding_list_t * binding_list,
817 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
818 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
819 for (unsigned int i = 0; i < bindings.size(); i++) {
820 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
821 << bindings[i]->getStringValue("command"));
822 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
825 // Read nested bindings for modifiers
826 if (node->getChild("mod-up") != 0)
827 _read_bindings(node->getChild("mod-up"), binding_list,
828 modifiers|FG_MOD_UP);
830 if (node->getChild("mod-shift") != 0)
831 _read_bindings(node->getChild("mod-shift"), binding_list,
832 modifiers|FG_MOD_SHIFT);
834 if (node->getChild("mod-ctrl") != 0)
835 _read_bindings(node->getChild("mod-ctrl"), binding_list,
836 modifiers|FG_MOD_CTRL);
838 if (node->getChild("mod-alt") != 0)
839 _read_bindings(node->getChild("mod-alt"), binding_list,
840 modifiers|FG_MOD_ALT);
844 const vector<FGBinding *> &
845 FGInput::_find_key_bindings (unsigned int k, int modifiers)
847 unsigned char kc = (unsigned char)k;
848 button &b = _key_bindings[k];
850 // Try it straight, first.
851 if (b.bindings[modifiers].size() > 0)
852 return b.bindings[modifiers];
854 // Alt-Gr is CTRL+ALT
855 else if (modifiers&(FG_MOD_CTRL|FG_MOD_ALT))
856 return _find_key_bindings(k, modifiers&~(FG_MOD_CTRL|FG_MOD_ALT));
858 // Try removing the control modifier
860 else if ((modifiers&FG_MOD_CTRL) && iscntrl(kc))
861 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
863 // Try removing shift modifier
864 // for upper case or any punctuation
865 // (since different keyboards will
866 // shift different punctuation types)
867 else if ((modifiers&FG_MOD_SHIFT) && (isupper(kc) || ispunct(kc)))
868 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
870 // Try removing alt modifier for
871 // high-bit characters.
872 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
873 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
875 // Give up and return the empty vector.
877 return b.bindings[modifiers];
882 ////////////////////////////////////////////////////////////////////////
883 // Implementation of FGInput::button.
884 ////////////////////////////////////////////////////////////////////////
886 FGInput::button::button ()
887 : is_repeatable(false),
892 FGInput::button::~button ()
894 // FIXME: memory leak
895 // for (int i = 0; i < FG_MOD_MAX; i++)
896 // for (int j = 0; i < bindings[i].size(); j++)
897 // delete bindings[i][j];
902 ////////////////////////////////////////////////////////////////////////
903 // Implementation of FGInput::axis.
904 ////////////////////////////////////////////////////////////////////////
906 FGInput::axis::axis ()
907 : last_value(9999999),
914 FGInput::axis::~axis ()
916 // for (int i = 0; i < FG_MOD_MAX; i++)
917 // for (int j = 0; i < bindings[i].size(); j++)
918 // delete bindings[i][j];
923 ////////////////////////////////////////////////////////////////////////
924 // Implementation of FGInput::joystick.
925 ////////////////////////////////////////////////////////////////////////
927 FGInput::joystick::joystick ()
931 FGInput::joystick::~joystick ()
940 ////////////////////////////////////////////////////////////////////////
941 // Implementation of FGInput::mouse_mode
942 ////////////////////////////////////////////////////////////////////////
944 FGInput::mouse_mode::mouse_mode ()
945 : cursor(GLUT_CURSOR_INHERIT),
952 FGInput::mouse_mode::~mouse_mode ()
954 // FIXME: memory leak
955 // for (int i = 0; i < FG_MOD_MAX; i++) {
957 // for (j = 0; i < x_bindings[i].size(); j++)
958 // delete bindings[i][j];
959 // for (j = 0; j < y_bindings[i].size(); j++)
960 // delete bindings[i][j];
967 ////////////////////////////////////////////////////////////////////////
968 // Implementation of FGInput::mouse
969 ////////////////////////////////////////////////////////////////////////
971 FGInput::mouse::mouse ()
980 FGInput::mouse::~mouse ()
987 ////////////////////////////////////////////////////////////////////////
988 // Implementation of GLUT callbacks.
989 ////////////////////////////////////////////////////////////////////////
993 * Construct the modifiers.
995 static inline int get_mods ()
997 int glut_modifiers = glutGetModifiers();
1000 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
1001 modifiers |= FGInput::FG_MOD_SHIFT;
1002 if (glut_modifiers & GLUT_ACTIVE_CTRL)
1003 modifiers |= FGInput::FG_MOD_CTRL;
1004 if (glut_modifiers & GLUT_ACTIVE_ALT)
1005 modifiers |= FGInput::FG_MOD_ALT;
1012 ////////////////////////////////////////////////////////////////////////
1013 // GLUT C callbacks.
1014 ////////////////////////////////////////////////////////////////////////
1017 GLUTkey(unsigned char k, int x, int y)
1019 // Give PUI a chance to grab it first.
1020 if (!puKeyboard(k, PU_DOWN)) {
1021 if (default_input != 0)
1022 default_input->doKey(k, get_mods(), x, y);
1027 GLUTkeyup(unsigned char k, int x, int y)
1029 if (default_input != 0)
1030 default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
1034 GLUTspecialkey(int k, int x, int y)
1036 // Give PUI a chance to grab it first.
1037 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
1038 if (default_input != 0)
1039 default_input->doKey(k + 256, get_mods(), x, y);
1044 GLUTspecialkeyup(int k, int x, int y)
1046 if (default_input != 0)
1047 default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1051 GLUTmouse (int button, int updown, int x, int y)
1053 if (default_input != 0)
1054 default_input->doMouseClick(button, updown, x, y);
1058 GLUTmotion (int x, int y)
1060 if (default_input != 0)
1061 default_input->doMouseMotion(x, y);