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)
96 FGBinding::~FGBinding ()
98 _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex());
102 FGBinding::read (const SGPropertyNode * node)
104 const SGPropertyNode * conditionNode = node->getChild("condition");
105 if (conditionNode != 0)
106 setCondition(sgReadCondition(globals->get_props(), conditionNode));
108 _command_name = node->getStringValue("command", "");
109 if (_command_name.empty()) {
110 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
115 _arg = (SGPropertyNode *)node;
120 FGBinding::fire () const
124 _command = globals->get_commands()->getCommand(_command_name);
126 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
127 } else if (!(*_command)(_arg)) {
128 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
135 FGBinding::fire (double offset, double max) const
138 _arg->setDoubleValue("offset", offset/max);
144 FGBinding::fire (double setting) const
147 // A value is automatically added to
149 if (_setting == 0) // save the setting node for efficiency
150 _setting = _arg->getChild("setting", 0, true);
151 _setting->setDoubleValue(setting);
158 ////////////////////////////////////////////////////////////////////////
159 // Implementation of FGInput.
160 ////////////////////////////////////////////////////////////////////////
165 if (default_input == 0)
166 default_input = this;
171 if (default_input == this)
182 fgRegisterKeyHandler(keyHandler);
183 fgRegisterMouseClickHandler(mouseClickHandler);
184 fgRegisterMouseMotionHandler(mouseMotionHandler);
196 _postinit_joystick();
200 FGInput::update (double dt)
203 _update_joystick(dt);
220 FGInput::is_suspended () const
226 FGInput::makeDefault (bool status)
229 default_input = this;
230 else if (default_input == this)
235 FGInput::doKey (int k, int modifiers, int x, int y)
238 if (k < 0 || k >= MAX_KEYS) {
239 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
243 button &b = _key_bindings[k];
246 if (modifiers&KEYMOD_RELEASED == 0) {
247 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
248 << " with modifiers " << modifiers );
249 if (!b.last_state || b.is_repeatable) {
250 const binding_list_t &bindings =
251 _find_key_bindings(k, modifiers);
252 int max = bindings.size();
254 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++)
278 FGInput::doMouseClick (int b, int updown, int x, int y)
280 int modifiers = fgGetKeyModifiers();
282 mouse &m = _mouse_bindings[0];
283 mouse_mode &mode = m.modes[m.current_mode];
285 // Let the property manager know.
286 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
287 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
289 // Pass on to PUI and the panel if
290 // requested, and return if one of
291 // them consumes the event.
292 if (mode.pass_through) {
293 if (puMouse(b, updown, x, y))
295 else if ((globals->get_current_panel() != 0) &&
296 globals->get_current_panel()->getVisibility() &&
297 globals->get_current_panel()->doMouseAction(b, updown, x, y))
299 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
303 // OK, PUI and the panel didn't want the click
304 if (b >= MAX_MOUSE_BUTTONS) {
305 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
306 << " where only " << MAX_MOUSE_BUTTONS << " expected");
310 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
314 FGInput::doMouseMotion (int x, int y)
316 // Don't call fgGetKeyModifiers() here, until we are using a
317 // toolkit that supports getting the mods from outside a key
318 // callback. Glut doesn't.
319 int modifiers = KEYMOD_NONE;
321 int xsize = fgGetInt("/sim/startup/xsize", 800);
322 int ysize = fgGetInt("/sim/startup/ysize", 600);
324 mouse &m = _mouse_bindings[0];
326 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
331 mouse_mode &mode = m.modes[m.current_mode];
333 // Pass on to PUI if requested, and return
334 // if PUI consumed the event.
335 if (mode.pass_through && puMouse(x, y)) {
341 // OK, PUI didn't want the event,
342 // so we can play with it.
345 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
346 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
350 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
351 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
354 // Constrain the mouse if requested
355 if (mode.constrained) {
356 bool need_warp = false;
360 } else if (x >= (xsize-1)) {
368 } else if (y >= (ysize-1)) {
381 FGInput::_init_keyboard ()
383 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
385 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
386 if (key_nodes == 0) {
387 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
388 key_nodes = fgGetNode("/input/keyboard", true);
391 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
392 for (unsigned int i = 0; i < keys.size(); i++) {
393 int index = keys[i]->getIndex();
394 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
396 _key_bindings[index].bindings->clear();
397 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
398 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
404 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
406 ulDir *dir = ulOpenDir(path->c_str());
409 while ((dent = ulReadDir(dir)) != 0) {
410 if (dent->d_name[0] == '.')
413 SGPath p(path->str());
414 p.append(dent->d_name);
415 _scan_joystick_dir(&p, node, index);
419 } else if (path->extension() == "xml") {
420 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
421 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
422 readProperties(path->str(), n);
423 n->setStringValue("source", path->c_str());
429 FGInput::_init_joystick ()
432 // TODO: zero the old bindings first.
433 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
434 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
436 // read all joystick xml files into /input/joysticks/js_named[1000++]
437 SGPath path(globals->get_fg_root());
438 path.append("Input/Joysticks");
439 int js_named_index = 1000;
440 _scan_joystick_dir(&path, js_nodes, &js_named_index);
442 // build name->node map with each <name> (reverse order)
443 map<string, SGPropertyNode_ptr> jsmap;
444 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
446 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
447 SGPropertyNode *n = js_named[k];
448 vector<SGPropertyNode_ptr> names = n->getChildren("name");
449 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
450 for (unsigned int j = 0; j < names.size(); j++)
451 jsmap[names[j]->getStringValue()] = n;
455 for (int i = 0; i < MAX_JOYSTICKS; i++) {
456 jsJoystick * js = new jsJoystick(i);
457 _joystick_bindings[i].js = js;
459 if (js->notWorking()) {
460 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
464 const char * name = js->getName();
465 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
468 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
471 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
472 SGPropertyNode_ptr named;
474 if ((named = jsmap[name])) {
475 string source = named->getStringValue("source", "user defined");
476 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
478 } else if ((named = jsmap["default"])) {
479 string source = named->getStringValue("source", "user defined");
480 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
481 << "\"\nUsing default: \"" << source << '"');
484 throw sg_throwable(string("No joystick configuration file with "
485 "<name>default</name> entry found!"));
488 js_node = js_nodes->getChild("js", i, true);
489 copyProperties(named, js_node);
490 js_node->setStringValue("id", name);
494 // get rid of unused config nodes
495 for (unsigned int m = 0; m < js_named.size(); m++)
496 js_nodes->removeChild("js-named", js_named[m]->getIndex(), false);
501 FGInput::_postinit_joystick()
503 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
504 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
506 for (int i = 0; i < MAX_JOYSTICKS; i++) {
507 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
508 jsJoystick *js = _joystick_bindings[i].js;
509 if (!js_node || js->notWorking())
514 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
515 int nbuttons = jsCaps.wNumButtons;
516 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
518 int nbuttons = MAX_JOYSTICK_BUTTONS;
521 int naxes = js->getNumAxes();
522 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
523 _joystick_bindings[i].naxes = naxes;
524 _joystick_bindings[i].nbuttons = nbuttons;
526 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
528 // Set up range arrays
529 float minRange[MAX_JOYSTICK_AXES];
530 float maxRange[MAX_JOYSTICK_AXES];
531 float center[MAX_JOYSTICK_AXES];
533 // Initialize with default values
534 js->getMinRange(minRange);
535 js->getMaxRange(maxRange);
536 js->getCenter(center);
538 // Allocate axes and buttons
539 _joystick_bindings[i].axes = new axis[naxes];
540 _joystick_bindings[i].buttons = new button[nbuttons];
543 // Initialize nasal groups.
546 init = "this=\"" + string(js_node->getPath()) + "\"";
547 sprintf(_module, "__js%d", i);
548 nasalsys->createModule(_module, _module, init.c_str(), init.size());
550 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
551 for (unsigned int j = 0; j < nasal.size(); j++) {
552 nasal[j]->setStringValue("module", _module);
553 nasalsys->handleCommand(nasal[j]);
557 // Initialize the axes.
559 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
560 size_t nb_axes = axes.size();
562 for (j = 0; j < (int)nb_axes; j++) {
563 const SGPropertyNode * axis_node = axes[j];
564 const SGPropertyNode * num_node = axis_node->getChild("number");
565 int n_axis = axis_node->getIndex();
567 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
569 // Silently ignore platforms that are not specified within the
570 // <number></number> section
575 if (n_axis >= naxes) {
576 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
579 axis &a = _joystick_bindings[i].axes[n_axis];
581 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
583 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
584 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
585 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
586 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
588 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
590 // Initialize the virtual axis buttons.
591 _init_button(axis_node->getChild("low"), a.low, "low");
592 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
594 _init_button(axis_node->getChild("high"), a.high, "high");
595 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
596 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
601 // Initialize the buttons.
603 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
605 for (j = 0; (j < (int)buttons.size()) && (j < nbuttons); j++) {
606 const SGPropertyNode * button_node = buttons[j];
607 const SGPropertyNode * num_node = button_node->getChild("number");
608 size_t n_but = button_node->getIndex();
610 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
613 if (n_but >= (size_t)nbuttons) {
614 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
618 sprintf(buf, "%d", n_but);
619 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
620 _init_button(button_node,
621 _joystick_bindings[i].buttons[n_but],
624 // get interval-sec property
625 button &b = _joystick_bindings[i].buttons[n_but];
626 if (button_node != 0) {
627 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
632 js->setMinRange(minRange);
633 js->setMaxRange(maxRange);
634 js->setCenter(center);
640 // Map of all known cursor names
641 // This used to contain all the Glut cursors, but those are
642 // not defined by other toolkits. It now supports only the cursor
643 // images we actually use, in the interest of portability. Someday,
644 // it would be cool to write an OpenGL cursor renderer, with the
645 // cursors defined as textures referenced in the property tree. This
646 // list could then be eliminated. -Andy
651 } mouse_cursor_map[] = {
652 { "none", MOUSE_CURSOR_NONE },
653 { "inherit", MOUSE_CURSOR_POINTER },
654 { "wait", MOUSE_CURSOR_WAIT },
655 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
656 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
661 FGInput::_init_mouse ()
663 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
666 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
667 if (mouse_nodes == 0) {
668 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
669 mouse_nodes = fgGetNode("/input/mice", true);
673 for (int i = 0; i < MAX_MICE; i++) {
674 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
675 mouse &m = _mouse_bindings[i];
677 // Grab node pointers
679 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
680 m.mode_node = fgGetNode(buf);
681 if (m.mode_node == NULL) {
682 m.mode_node = fgGetNode(buf, true);
683 m.mode_node->setIntValue(0);
685 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
686 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
687 m.mouse_button_nodes[j] = fgGetNode(buf, true);
688 m.mouse_button_nodes[j]->setBoolValue(false);
691 // Read all the modes
692 m.nModes = mouse_node->getIntValue("mode-count", 1);
693 m.modes = new mouse_mode[m.nModes];
695 for (int j = 0; j < m.nModes; j++) {
698 // Read the mouse cursor for this mode
699 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
700 const char * cursor_name =
701 mode_node->getStringValue("cursor", "inherit");
702 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
703 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
704 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
705 m.modes[j].cursor = mouse_cursor_map[k].cursor;
710 // Read other properties for this mode
711 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
712 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
714 // Read the button bindings for this mode
715 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
717 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
718 sprintf(buf, "mouse button %d", k);
719 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
720 _init_button(mode_node->getChild("button", k),
721 m.modes[j].buttons[k],
725 // Read the axis bindings for this mode
726 _read_bindings(mode_node->getChild("x-axis", 0, true),
727 m.modes[j].x_bindings,
729 _read_bindings(mode_node->getChild("y-axis", 0, true),
730 m.modes[j].y_bindings,
738 FGInput::_init_button (const SGPropertyNode * node,
743 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
745 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
747 // Get the bindings for the button
748 _read_bindings(node, b.bindings, KEYMOD_NONE);
754 FGInput::_update_keyboard ()
761 FGInput::_update_joystick (double dt)
763 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
765 // float js_val, diff;
766 float axis_values[MAX_JOYSTICK_AXES];
771 for ( i = 0; i < MAX_JOYSTICKS; i++) {
773 jsJoystick * js = _joystick_bindings[i].js;
774 if (js == 0 || js->notWorking())
777 js->read(&buttons, axis_values);
779 // Fire bindings for the axes.
780 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
781 axis &a = _joystick_bindings[i].axes[j];
783 // Do nothing if the axis position
784 // is unchanged; only a change in
785 // position fires the bindings.
786 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
787 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
788 a.last_value = axis_values[j];
789 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
790 // << a.bindings[modifiers].size() << " bindings");
791 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
792 a.bindings[modifiers][k]->fire(axis_values[j]);
795 // do we have to emulate axis buttons?
797 if(a.last_dt >= a.interval_sec) {
798 if (a.low.bindings[modifiers].size())
799 _update_button(_joystick_bindings[i].axes[j].low,
801 axis_values[j] < a.low_threshold,
804 if (a.high.bindings[modifiers].size())
805 _update_button(_joystick_bindings[i].axes[j].high,
807 axis_values[j] > a.high_threshold,
809 a.last_dt -= a.interval_sec;
813 // Fire bindings for the buttons.
814 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
815 button &b = _joystick_bindings[i].buttons[j];
817 if(b.last_dt >= b.interval_sec) {
818 _update_button(_joystick_bindings[i].buttons[j],
820 (buttons & (1 << j)) > 0,
822 b.last_dt -= b.interval_sec;
829 FGInput::_update_mouse ( double dt )
831 mouse &m = _mouse_bindings[0];
832 int mode = m.mode_node->getIntValue();
833 if (mode != m.current_mode) {
834 m.current_mode = mode;
835 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
836 if (mode >= 0 && mode < m.nModes) {
837 fgSetMouseCursor(m.modes[mode].cursor);
838 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
839 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
840 fgWarpMouse(m.x, m.y);
842 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
843 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
847 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
848 if ( m.x != m.save_x || m.y != m.save_y ) {
849 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
850 fgSetMouseCursor(m.modes[mode].cursor);
853 if ( m.timeout <= 0.0 ) {
854 fgSetMouseCursor(MOUSE_CURSOR_NONE);
864 FGInput::_update_button (button &b, int modifiers, bool pressed,
868 // The press event may be repeated.
869 if (!b.last_state || b.is_repeatable) {
870 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
871 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
872 b.bindings[modifiers][k]->fire(x, y);
875 // The release event is never repeated.
877 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
878 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
879 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
883 b.last_state = pressed;
888 FGInput::_read_bindings (const SGPropertyNode * node,
889 binding_list_t * binding_list,
892 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
893 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
894 for (unsigned int i = 0; i < bindings.size(); i++) {
895 const char *cmd = bindings[i]->getStringValue("command");
896 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
898 if (!strcmp(cmd, "nasal") && _module[0])
899 bindings[i]->setStringValue("module", _module);
900 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
903 // Read nested bindings for modifiers
904 if (node->getChild("mod-up") != 0)
905 _read_bindings(node->getChild("mod-up"), binding_list,
906 modifiers|KEYMOD_RELEASED);
908 if (node->getChild("mod-shift") != 0)
909 _read_bindings(node->getChild("mod-shift"), binding_list,
910 modifiers|KEYMOD_SHIFT);
912 if (node->getChild("mod-ctrl") != 0)
913 _read_bindings(node->getChild("mod-ctrl"), binding_list,
914 modifiers|KEYMOD_CTRL);
916 if (node->getChild("mod-alt") != 0)
917 _read_bindings(node->getChild("mod-alt"), binding_list,
918 modifiers|KEYMOD_ALT);
922 const vector<FGBinding *> &
923 FGInput::_find_key_bindings (unsigned int k, int modifiers)
925 unsigned char kc = (unsigned char)k;
926 button &b = _key_bindings[k];
928 // Try it straight, first.
929 if (b.bindings[modifiers].size() > 0)
930 return b.bindings[modifiers];
932 // Alt-Gr is CTRL+ALT
933 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
934 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
936 // Try removing the control modifier
938 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
939 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
941 // Try removing shift modifier
942 // for upper case or any punctuation
943 // (since different keyboards will
944 // shift different punctuation types)
945 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
946 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
948 // Try removing alt modifier for
949 // high-bit characters.
950 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
951 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
953 // Give up and return the empty vector.
955 return b.bindings[modifiers];
960 ////////////////////////////////////////////////////////////////////////
961 // Implementation of FGInput::button.
962 ////////////////////////////////////////////////////////////////////////
964 FGInput::button::button ()
965 : is_repeatable(false),
970 FGInput::button::~button ()
972 // FIXME: memory leak
973 // for (int i = 0; i < KEYMOD_MAX; i++)
974 // for (int j = 0; i < bindings[i].size(); j++)
975 // delete bindings[i][j];
980 ////////////////////////////////////////////////////////////////////////
981 // Implementation of FGInput::axis.
982 ////////////////////////////////////////////////////////////////////////
984 FGInput::axis::axis ()
985 : last_value(9999999),
992 FGInput::axis::~axis ()
994 // for (int i = 0; i < KEYMOD_MAX; i++)
995 // for (int j = 0; i < bindings[i].size(); j++)
996 // delete bindings[i][j];
1001 ////////////////////////////////////////////////////////////////////////
1002 // Implementation of FGInput::joystick.
1003 ////////////////////////////////////////////////////////////////////////
1005 FGInput::joystick::joystick ()
1009 FGInput::joystick::~joystick ()
1018 ////////////////////////////////////////////////////////////////////////
1019 // Implementation of FGInput::mouse_mode
1020 ////////////////////////////////////////////////////////////////////////
1022 FGInput::mouse_mode::mouse_mode ()
1023 : cursor(MOUSE_CURSOR_POINTER),
1025 pass_through(false),
1030 FGInput::mouse_mode::~mouse_mode ()
1032 // FIXME: memory leak
1033 // for (int i = 0; i < KEYMOD_MAX; i++) {
1035 // for (j = 0; i < x_bindings[i].size(); j++)
1036 // delete bindings[i][j];
1037 // for (j = 0; j < y_bindings[i].size(); j++)
1038 // delete bindings[i][j];
1045 ////////////////////////////////////////////////////////////////////////
1046 // Implementation of FGInput::mouse
1047 ////////////////////////////////////////////////////////////////////////
1049 FGInput::mouse::mouse ()
1058 FGInput::mouse::~mouse ()
1063 ////////////////////////////////////////////////////////////////////////
1064 // Implementation of OS callbacks.
1065 ////////////////////////////////////////////////////////////////////////
1067 void keyHandler(int key, int keymod, int mousex, int mousey)
1069 if((keymod & KEYMOD_RELEASED) == 0)
1070 if(puKeyboard(key, PU_DOWN))
1074 default_input->doKey(key, keymod, mousex, mousey);
1077 void mouseClickHandler(int button, int updown, int x, int y)
1080 default_input->doMouseClick(button, updown, x, y);
1083 void mouseMotionHandler(int x, int y)
1085 if (default_input != 0)
1086 default_input->doMouseMotion(x, y);