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>
40 #include <simgear/compiler.h>
42 #include <simgear/constants.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/props/props.hxx>
46 #include <Aircraft/aircraft.hxx>
47 #include <Autopilot/xmlauto.hxx>
48 #include <Cockpit/hud.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <Cockpit/panel_io.hxx>
52 #include <Model/panelnode.hxx>
53 #include <Scripting/NasalSys.hxx>
55 #include <Main/globals.hxx>
56 #include <Main/fg_props.hxx>
60 SG_USING_STD(ifstream);
64 void mouseClickHandler(int button, int updown, int x, int y);
65 void mouseMotionHandler(int x, int y);
66 void keyHandler(int key, int keymod, int mousex, int mousey);
69 ////////////////////////////////////////////////////////////////////////
71 ////////////////////////////////////////////////////////////////////////
73 static FGInput * default_input = 0;
77 ////////////////////////////////////////////////////////////////////////
78 // Implementation of FGBinding.
79 ////////////////////////////////////////////////////////////////////////
81 FGBinding::FGBinding ()
83 _arg(new SGPropertyNode),
88 FGBinding::FGBinding (const SGPropertyNode * node)
97 FGBinding::read (const SGPropertyNode * node)
99 const SGPropertyNode * conditionNode = node->getChild("condition");
100 if (conditionNode != 0)
101 setCondition(sgReadCondition(globals->get_props(), conditionNode));
103 _command_name = node->getStringValue("command", "");
104 if (_command_name.empty()) {
105 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
110 _arg = new SGPropertyNode;
112 copyProperties(node, _arg); // FIXME: don't use whole node!!!
116 FGBinding::fire () const
120 _command = globals->get_commands()->getCommand(_command_name);
122 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
123 } else if (!(*_command)(_arg)) {
124 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
131 FGBinding::fire (double offset, double max) const
134 _arg->setDoubleValue("offset", offset/max);
140 FGBinding::fire (double setting) const
143 // A value is automatically added to
145 if (_setting == 0) // save the setting node for efficiency
146 _setting = _arg->getChild("setting", 0, true);
147 _setting->setDoubleValue(setting);
154 ////////////////////////////////////////////////////////////////////////
155 // Implementation of FGInput.
156 ////////////////////////////////////////////////////////////////////////
161 if (default_input == 0)
162 default_input = this;
167 if (default_input == this)
178 fgRegisterKeyHandler(keyHandler);
179 fgRegisterMouseClickHandler(mouseClickHandler);
180 fgRegisterMouseMotionHandler(mouseMotionHandler);
192 _postinit_joystick();
196 FGInput::update (double dt)
199 _update_joystick(dt);
216 FGInput::is_suspended () const
222 FGInput::makeDefault (bool status)
225 default_input = this;
226 else if (default_input == this)
231 FGInput::doKey (int k, int modifiers, int x, int y)
234 if (k < 0 || k >= MAX_KEYS) {
235 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
239 button &b = _key_bindings[k];
242 if (modifiers&KEYMOD_RELEASED == 0) {
243 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
244 << " with modifiers " << modifiers );
245 if (!b.last_state || b.is_repeatable) {
246 const binding_list_t &bindings =
247 _find_key_bindings(k, modifiers);
248 int max = bindings.size();
250 for (int i = 0; i < max; i++)
258 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
259 << " with modifiers " << modifiers);
261 const binding_list_t &bindings =
262 _find_key_bindings(k, modifiers);
263 int max = bindings.size();
265 for (int i = 0; i < max; i++)
274 FGInput::doMouseClick (int b, int updown, int x, int y)
276 int modifiers = fgGetKeyModifiers();
278 mouse &m = _mouse_bindings[0];
279 mouse_mode &mode = m.modes[m.current_mode];
281 // Let the property manager know.
282 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
283 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
285 // Pass on to PUI and the panel if
286 // requested, and return if one of
287 // them consumes the event.
288 if (mode.pass_through) {
289 if (puMouse(b, updown, x, y))
291 else if ((globals->get_current_panel() != 0) &&
292 globals->get_current_panel()->getVisibility() &&
293 globals->get_current_panel()->doMouseAction(b, updown, x, y))
295 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
299 // OK, PUI and the panel didn't want the click
300 if (b >= MAX_MOUSE_BUTTONS) {
301 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
302 << " where only " << MAX_MOUSE_BUTTONS << " expected");
306 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
310 FGInput::doMouseMotion (int x, int y)
312 // Don't call fgGetKeyModifiers() here, until we are using a
313 // toolkit that supports getting the mods from outside a key
314 // callback. Glut doesn't.
315 int modifiers = KEYMOD_NONE;
317 int xsize = fgGetInt("/sim/startup/xsize", 800);
318 int ysize = fgGetInt("/sim/startup/ysize", 600);
320 mouse &m = _mouse_bindings[0];
322 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
327 mouse_mode &mode = m.modes[m.current_mode];
329 // Pass on to PUI if requested, and return
330 // if PUI consumed the event.
331 if (mode.pass_through && puMouse(x, y)) {
337 // OK, PUI didn't want the event,
338 // so we can play with it.
341 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
342 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
346 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
347 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
350 // Constrain the mouse if requested
351 if (mode.constrained) {
352 bool need_warp = false;
356 } else if (x >= (xsize-1)) {
364 } else if (y >= (ysize-1)) {
377 FGInput::_init_keyboard ()
379 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
380 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
381 if (key_nodes == 0) {
382 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
383 key_nodes = fgGetNode("/input/keyboard", true);
386 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
387 for (unsigned int i = 0; i < keys.size(); i++) {
388 int index = keys[i]->getIndex();
389 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
391 _key_bindings[index].bindings->clear();
392 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
393 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
399 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
401 ulDir *dir = ulOpenDir(path->c_str());
404 while ((dent = ulReadDir(dir)) != 0) {
405 if (dent->d_name[0] == '.')
408 SGPath p(path->str());
409 p.append(dent->d_name);
410 _scan_joystick_dir(&p, node, index);
414 } else if (path->extension() == "xml") {
415 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
416 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
417 readProperties(path->str(), n);
418 n->setStringValue("source", path->c_str());
424 FGInput::_init_joystick ()
427 // TODO: zero the old bindings first.
428 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
429 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
431 SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
432 js_nodes = fgGetNode("/input/joysticks", true);
435 // read all joystick xml files into /input/joysticks/js_named[1000++]
436 SGPath path(globals->get_fg_root());
437 path.append("Input/Joysticks");
438 int js_named_index = 1000;
439 _scan_joystick_dir(&path, js_nodes, &js_named_index);
441 // build name->node map with each <name> (reverse order)
442 map<string, SGPropertyNode_ptr> jsmap;
443 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
445 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
446 SGPropertyNode *n = js_named[k];
447 vector<SGPropertyNode_ptr> names = n->getChildren("name");
448 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
449 for (unsigned int j = 0; j < names.size(); j++)
450 jsmap[names[j]->getStringValue()] = n;
453 for (int i = 0; i < MAX_JOYSTICKS; i++) {
454 jsJoystick * js = new jsJoystick(i);
455 _joystick_bindings[i].js = js;
456 if (js->notWorking()) {
457 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
461 const char * name = js->getName();
462 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
465 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
468 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
469 SGPropertyNode_ptr named;
471 if ((named = jsmap[name])) {
472 string source = named->getStringValue("source", "user defined");
473 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: \"" << source << '"');
475 } else if ((named = jsmap["default"])) {
476 string source = named->getStringValue("source", "user defined");
477 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
478 << "\"\nUsing default: \"" << source << '"');
481 throw sg_throwable(string("No default joystick found! (<name>default</name>)"));
484 js_node = js_nodes->getChild("js", i, true);
485 copyProperties(named, js_node);
486 js_node->setStringValue("id", name);
492 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
493 int nbuttons = jsCaps.wNumButtons;
494 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
496 int nbuttons = MAX_JOYSTICK_BUTTONS;
499 int naxes = js->getNumAxes();
500 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
501 _joystick_bindings[i].naxes = naxes;
502 _joystick_bindings[i].nbuttons = nbuttons;
504 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
506 // Set up range arrays
507 float minRange[MAX_JOYSTICK_AXES];
508 float maxRange[MAX_JOYSTICK_AXES];
509 float center[MAX_JOYSTICK_AXES];
511 // Initialize with default values
512 js->getMinRange(minRange);
513 js->getMaxRange(maxRange);
514 js->getCenter(center);
516 // Allocate axes and buttons
517 _joystick_bindings[i].axes = new axis[naxes];
518 _joystick_bindings[i].buttons = new button[nbuttons];
522 // Initialize the axes.
524 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
525 size_t nb_axes = axes.size();
527 for (j = 0; j < (int)nb_axes; j++) {
528 const SGPropertyNode * axis_node = axes[j];
529 const SGPropertyNode * num_node = axis_node->getChild("number");
530 int n_axis = axis_node->getIndex();
532 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
534 // Silently ignore platforms that are not specified within the
535 // <number></number> section
540 if (n_axis >= naxes) {
541 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
544 axis &a = _joystick_bindings[i].axes[n_axis];
546 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
548 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
549 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
550 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
551 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
553 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
555 // Initialize the virtual axis buttons.
556 _init_button(axis_node->getChild("low"), a.low, "low");
557 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
559 _init_button(axis_node->getChild("high"), a.high, "high");
560 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
561 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
566 // Initialize the buttons.
568 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
570 for (j = 0; (j < (int)buttons.size()) && (j < nbuttons); j++) {
571 const SGPropertyNode * button_node = buttons[j];
572 const SGPropertyNode * num_node = button_node->getChild("number");
573 size_t n_but = button_node->getIndex();
575 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
578 if (n_but >= (size_t)nbuttons) {
579 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
583 sprintf(buf, "%d", n_but);
584 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
585 _init_button(button_node,
586 _joystick_bindings[i].buttons[n_but],
589 // get interval-sec property
590 button &b = _joystick_bindings[i].buttons[n_but];
591 if (button_node != 0) {
592 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
597 js->setMinRange(minRange);
598 js->setMaxRange(maxRange);
599 js->setCenter(center);
602 for (unsigned int m = 0; m < js_named.size(); m++)
603 js_nodes->removeChild("js-named", js_named[m]->getIndex(), false);
608 FGInput::_postinit_joystick()
610 vector<SGPropertyNode_ptr> js = fgGetNode("/input/joysticks")->getChildren("js");
611 for (unsigned int i = 0; i < js.size(); i++) {
612 vector<SGPropertyNode_ptr> nasal = js[i]->getChildren("nasal");
613 for (unsigned int j = 0; j < nasal.size(); j++)
614 ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(nasal[j]);
620 // Map of all known cursor names
621 // This used to contain all the Glut cursors, but those are
622 // not defined by other toolkits. It now supports only the cursor
623 // images we actually use, in the interest of portability. Someday,
624 // it would be cool to write an OpenGL cursor renderer, with the
625 // cursors defined as textures referenced in the property tree. This
626 // list could then be eliminated. -Andy
631 } mouse_cursor_map[] = {
632 { "none", MOUSE_CURSOR_NONE },
633 { "inherit", MOUSE_CURSOR_POINTER },
634 { "wait", MOUSE_CURSOR_WAIT },
635 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
636 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
641 FGInput::_init_mouse ()
643 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
645 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
646 if (mouse_nodes == 0) {
647 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
648 mouse_nodes = fgGetNode("/input/mice", true);
652 for (int i = 0; i < MAX_MICE; i++) {
653 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
654 mouse &m = _mouse_bindings[i];
656 // Grab node pointers
658 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
659 m.mode_node = fgGetNode(buf);
660 if (m.mode_node == NULL) {
661 m.mode_node = fgGetNode(buf, true);
662 m.mode_node->setIntValue(0);
664 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
665 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
666 m.mouse_button_nodes[j] = fgGetNode(buf, true);
667 m.mouse_button_nodes[j]->setBoolValue(false);
670 // Read all the modes
671 m.nModes = mouse_node->getIntValue("mode-count", 1);
672 m.modes = new mouse_mode[m.nModes];
674 for (int j = 0; j < m.nModes; j++) {
677 // Read the mouse cursor for this mode
678 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
679 const char * cursor_name =
680 mode_node->getStringValue("cursor", "inherit");
681 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
682 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
683 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
684 m.modes[j].cursor = mouse_cursor_map[k].cursor;
689 // Read other properties for this mode
690 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
691 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
693 // Read the button bindings for this mode
694 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
696 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
697 sprintf(buf, "mouse button %d", k);
698 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
699 _init_button(mode_node->getChild("button", k),
700 m.modes[j].buttons[k],
704 // Read the axis bindings for this mode
705 _read_bindings(mode_node->getChild("x-axis", 0, true),
706 m.modes[j].x_bindings,
708 _read_bindings(mode_node->getChild("y-axis", 0, true),
709 m.modes[j].y_bindings,
717 FGInput::_init_button (const SGPropertyNode * node,
722 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
724 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
726 // Get the bindings for the button
727 _read_bindings(node, b.bindings, KEYMOD_NONE);
733 FGInput::_update_keyboard ()
740 FGInput::_update_joystick (double dt)
742 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
744 // float js_val, diff;
745 float axis_values[MAX_JOYSTICK_AXES];
750 for ( i = 0; i < MAX_JOYSTICKS; i++) {
752 jsJoystick * js = _joystick_bindings[i].js;
753 if (js == 0 || js->notWorking())
756 js->read(&buttons, axis_values);
759 // Fire bindings for the axes.
760 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
761 axis &a = _joystick_bindings[i].axes[j];
763 // Do nothing if the axis position
764 // is unchanged; only a change in
765 // position fires the bindings.
766 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
767 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
768 a.last_value = axis_values[j];
769 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
770 // << a.bindings[modifiers].size() << " bindings");
771 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
772 a.bindings[modifiers][k]->fire(axis_values[j]);
775 // do we have to emulate axis buttons?
777 if(a.last_dt >= a.interval_sec) {
778 if (a.low.bindings[modifiers].size())
779 _update_button(_joystick_bindings[i].axes[j].low,
781 axis_values[j] < a.low_threshold,
784 if (a.high.bindings[modifiers].size())
785 _update_button(_joystick_bindings[i].axes[j].high,
787 axis_values[j] > a.high_threshold,
789 a.last_dt -= a.interval_sec;
793 // Fire bindings for the buttons.
794 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
795 button &b = _joystick_bindings[i].buttons[j];
797 if(b.last_dt >= b.interval_sec) {
798 _update_button(_joystick_bindings[i].buttons[j],
800 (buttons & (1 << j)) > 0,
802 b.last_dt -= b.interval_sec;
809 FGInput::_update_mouse ( double dt )
811 mouse &m = _mouse_bindings[0];
812 int mode = m.mode_node->getIntValue();
813 if (mode != m.current_mode) {
814 m.current_mode = mode;
815 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
816 if (mode >= 0 && mode < m.nModes) {
817 fgSetMouseCursor(m.modes[mode].cursor);
818 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
819 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
820 fgWarpMouse(m.x, m.y);
822 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
823 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
827 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
828 if ( m.x != m.save_x || m.y != m.save_y ) {
829 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
830 fgSetMouseCursor(m.modes[mode].cursor);
833 if ( m.timeout <= 0.0 ) {
834 fgSetMouseCursor(MOUSE_CURSOR_NONE);
844 FGInput::_update_button (button &b, int modifiers, bool pressed,
848 // The press event may be repeated.
849 if (!b.last_state || b.is_repeatable) {
850 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
851 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
852 b.bindings[modifiers][k]->fire(x, y);
855 // The release event is never repeated.
857 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
858 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
859 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
863 b.last_state = pressed;
868 FGInput::_read_bindings (const SGPropertyNode * node,
869 binding_list_t * binding_list,
872 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
873 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
874 for (unsigned int i = 0; i < bindings.size(); i++) {
875 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
876 << bindings[i]->getStringValue("command"));
877 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
880 // Read nested bindings for modifiers
881 if (node->getChild("mod-up") != 0)
882 _read_bindings(node->getChild("mod-up"), binding_list,
883 modifiers|KEYMOD_RELEASED);
885 if (node->getChild("mod-shift") != 0)
886 _read_bindings(node->getChild("mod-shift"), binding_list,
887 modifiers|KEYMOD_SHIFT);
889 if (node->getChild("mod-ctrl") != 0)
890 _read_bindings(node->getChild("mod-ctrl"), binding_list,
891 modifiers|KEYMOD_CTRL);
893 if (node->getChild("mod-alt") != 0)
894 _read_bindings(node->getChild("mod-alt"), binding_list,
895 modifiers|KEYMOD_ALT);
899 const vector<FGBinding *> &
900 FGInput::_find_key_bindings (unsigned int k, int modifiers)
902 unsigned char kc = (unsigned char)k;
903 button &b = _key_bindings[k];
905 // Try it straight, first.
906 if (b.bindings[modifiers].size() > 0)
907 return b.bindings[modifiers];
909 // Alt-Gr is CTRL+ALT
910 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
911 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
913 // Try removing the control modifier
915 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
916 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
918 // Try removing shift modifier
919 // for upper case or any punctuation
920 // (since different keyboards will
921 // shift different punctuation types)
922 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
923 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
925 // Try removing alt modifier for
926 // high-bit characters.
927 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
928 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
930 // Give up and return the empty vector.
932 return b.bindings[modifiers];
937 ////////////////////////////////////////////////////////////////////////
938 // Implementation of FGInput::button.
939 ////////////////////////////////////////////////////////////////////////
941 FGInput::button::button ()
942 : is_repeatable(false),
947 FGInput::button::~button ()
949 // FIXME: memory leak
950 // for (int i = 0; i < KEYMOD_MAX; i++)
951 // for (int j = 0; i < bindings[i].size(); j++)
952 // delete bindings[i][j];
957 ////////////////////////////////////////////////////////////////////////
958 // Implementation of FGInput::axis.
959 ////////////////////////////////////////////////////////////////////////
961 FGInput::axis::axis ()
962 : last_value(9999999),
969 FGInput::axis::~axis ()
971 // for (int i = 0; i < KEYMOD_MAX; i++)
972 // for (int j = 0; i < bindings[i].size(); j++)
973 // delete bindings[i][j];
978 ////////////////////////////////////////////////////////////////////////
979 // Implementation of FGInput::joystick.
980 ////////////////////////////////////////////////////////////////////////
982 FGInput::joystick::joystick ()
986 FGInput::joystick::~joystick ()
995 ////////////////////////////////////////////////////////////////////////
996 // Implementation of FGInput::mouse_mode
997 ////////////////////////////////////////////////////////////////////////
999 FGInput::mouse_mode::mouse_mode ()
1000 : cursor(MOUSE_CURSOR_POINTER),
1002 pass_through(false),
1007 FGInput::mouse_mode::~mouse_mode ()
1009 // FIXME: memory leak
1010 // for (int i = 0; i < KEYMOD_MAX; i++) {
1012 // for (j = 0; i < x_bindings[i].size(); j++)
1013 // delete bindings[i][j];
1014 // for (j = 0; j < y_bindings[i].size(); j++)
1015 // delete bindings[i][j];
1022 ////////////////////////////////////////////////////////////////////////
1023 // Implementation of FGInput::mouse
1024 ////////////////////////////////////////////////////////////////////////
1026 FGInput::mouse::mouse ()
1035 FGInput::mouse::~mouse ()
1040 ////////////////////////////////////////////////////////////////////////
1041 // Implementation of OS callbacks.
1042 ////////////////////////////////////////////////////////////////////////
1044 void keyHandler(int key, int keymod, int mousex, int mousey)
1046 if((keymod & KEYMOD_RELEASED) == 0)
1047 if(puKeyboard(key, PU_DOWN))
1051 default_input->doKey(key, keymod, mousex, mousey);
1054 void mouseClickHandler(int button, int updown, int x, int y)
1057 default_input->doMouseClick(button, updown, x, y);
1060 void mouseMotionHandler(int x, int y)
1062 if (default_input != 0)
1063 default_input->doMouseMotion(x, y);