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(), false);
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)) {
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 = _find_key_bindings(k, modifiers);
252 for (unsigned int i = 0; i < bindings.size(); i++)
259 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
260 << " with modifiers " << modifiers);
262 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
263 for (unsigned int i = 0; i < bindings.size(); i++)
271 FGInput::doMouseClick (int b, int updown, int x, int y)
273 int modifiers = fgGetKeyModifiers();
275 mouse &m = _mouse_bindings[0];
276 mouse_mode &mode = m.modes[m.current_mode];
278 // Let the property manager know.
279 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
280 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
282 // Pass on to PUI and the panel if
283 // requested, and return if one of
284 // them consumes the event.
285 if (mode.pass_through) {
286 if (puMouse(b, updown, x, y))
288 else if ((globals->get_current_panel() != 0) &&
289 globals->get_current_panel()->getVisibility() &&
290 globals->get_current_panel()->doMouseAction(b, updown, x, y))
292 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
296 // OK, PUI and the panel didn't want the click
297 if (b >= MAX_MOUSE_BUTTONS) {
298 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
299 << " where only " << MAX_MOUSE_BUTTONS << " expected");
303 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
307 FGInput::doMouseMotion (int x, int y)
309 // Don't call fgGetKeyModifiers() here, until we are using a
310 // toolkit that supports getting the mods from outside a key
311 // callback. Glut doesn't.
312 int modifiers = KEYMOD_NONE;
314 int xsize = fgGetInt("/sim/startup/xsize", 800);
315 int ysize = fgGetInt("/sim/startup/ysize", 600);
317 mouse &m = _mouse_bindings[0];
319 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
324 mouse_mode &mode = m.modes[m.current_mode];
326 // Pass on to PUI if requested, and return
327 // if PUI consumed the event.
328 if (mode.pass_through && puMouse(x, y)) {
334 // OK, PUI didn't want the event,
335 // so we can play with it.
338 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
339 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
343 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
344 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
347 // Constrain the mouse if requested
348 if (mode.constrained) {
349 bool need_warp = false;
353 } else if (x >= (xsize-1)) {
361 } else if (y >= (ysize-1)) {
374 FGInput::_init_keyboard ()
376 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
378 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
379 if (key_nodes == 0) {
380 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
381 key_nodes = fgGetNode("/input/keyboard", true);
384 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
385 for (unsigned int i = 0; i < keys.size(); i++) {
386 int index = keys[i]->getIndex();
387 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
389 _key_bindings[index].bindings->clear();
390 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
391 _key_bindings[index].last_state = 0;
392 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
398 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
400 ulDir *dir = ulOpenDir(path->c_str());
403 while ((dent = ulReadDir(dir)) != 0) {
404 if (dent->d_name[0] == '.')
407 SGPath p(path->str());
408 p.append(dent->d_name);
409 _scan_joystick_dir(&p, node, index);
413 } else if (path->extension() == "xml") {
414 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
415 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
416 readProperties(path->str(), n);
417 n->setStringValue("source", path->c_str());
423 FGInput::_init_joystick ()
426 // TODO: zero the old bindings first.
427 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
428 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
430 // read all joystick xml files into /input/joysticks/js_named[1000++]
431 SGPath path(globals->get_fg_root());
432 path.append("Input/Joysticks");
433 int js_named_index = 1000;
434 _scan_joystick_dir(&path, js_nodes, &js_named_index);
436 // build name->node map with each <name> (reverse order)
437 map<string, SGPropertyNode_ptr> jsmap;
438 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
440 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
441 SGPropertyNode *n = js_named[k];
442 vector<SGPropertyNode_ptr> names = n->getChildren("name");
443 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
444 for (unsigned int j = 0; j < names.size(); j++)
445 jsmap[names[j]->getStringValue()] = n;
449 for (int i = 0; i < MAX_JOYSTICKS; i++) {
450 jsJoystick * js = new jsJoystick(i);
451 _joystick_bindings[i].js = js;
453 if (js->notWorking()) {
454 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
458 const char * name = js->getName();
459 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
462 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
465 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
466 SGPropertyNode_ptr named;
468 if ((named = jsmap[name])) {
469 string source = named->getStringValue("source", "user defined");
470 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
472 } else if ((named = jsmap["default"])) {
473 string source = named->getStringValue("source", "user defined");
474 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
475 << "\"\nUsing default: \"" << source << '"');
478 throw sg_throwable(string("No joystick configuration file with "
479 "<name>default</name> entry found!"));
482 js_node = js_nodes->getChild("js", i, true);
483 copyProperties(named, js_node);
484 js_node->setStringValue("id", name);
488 // get rid of unused config nodes
489 for (unsigned int m = 0; m < js_named.size(); m++)
490 js_nodes->removeChild("js-named", js_named[m]->getIndex(), false);
495 FGInput::_postinit_joystick()
497 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
498 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
500 for (int i = 0; i < MAX_JOYSTICKS; i++) {
501 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
502 jsJoystick *js = _joystick_bindings[i].js;
503 if (!js_node || js->notWorking())
508 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
509 unsigned int nbuttons = jsCaps.wNumButtons;
510 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
512 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
515 int naxes = js->getNumAxes();
516 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
517 _joystick_bindings[i].naxes = naxes;
518 _joystick_bindings[i].nbuttons = nbuttons;
520 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
522 // Set up range arrays
523 float minRange[MAX_JOYSTICK_AXES];
524 float maxRange[MAX_JOYSTICK_AXES];
525 float center[MAX_JOYSTICK_AXES];
527 // Initialize with default values
528 js->getMinRange(minRange);
529 js->getMaxRange(maxRange);
530 js->getCenter(center);
532 // Allocate axes and buttons
533 _joystick_bindings[i].axes = new axis[naxes];
534 _joystick_bindings[i].buttons = new button[nbuttons];
537 // Initialize nasal groups.
540 init = "this=\"" + string(js_node->getPath()) + "\"";
541 sprintf(_module, "__js%d", i);
542 nasalsys->createModule(_module, _module, init.c_str(), init.size());
544 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
546 for (j = 0; j < nasal.size(); j++) {
547 nasal[j]->setStringValue("module", _module);
548 nasalsys->handleCommand(nasal[j]);
552 // Initialize the axes.
554 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
555 size_t nb_axes = axes.size();
556 for (j = 0; j < nb_axes; j++ ) {
557 const SGPropertyNode * axis_node = axes[j];
558 const SGPropertyNode * num_node = axis_node->getChild("number");
559 int n_axis = axis_node->getIndex();
561 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
563 // Silently ignore platforms that are not specified within the
564 // <number></number> section
569 if (n_axis >= naxes) {
570 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
573 axis &a = _joystick_bindings[i].axes[n_axis];
575 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
577 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
578 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
579 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
580 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
582 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
584 // Initialize the virtual axis buttons.
585 _init_button(axis_node->getChild("low"), a.low, "low");
586 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
588 _init_button(axis_node->getChild("high"), a.high, "high");
589 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
590 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
595 // Initialize the buttons.
597 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
599 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
600 const SGPropertyNode * button_node = buttons[j];
601 const SGPropertyNode * num_node = button_node->getChild("number");
602 size_t n_but = button_node->getIndex();
604 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
607 if (n_but >= nbuttons) {
608 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
612 sprintf(buf, "%d", n_but);
613 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
614 _init_button(button_node,
615 _joystick_bindings[i].buttons[n_but],
618 // get interval-sec property
619 button &b = _joystick_bindings[i].buttons[n_but];
620 if (button_node != 0) {
621 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
626 js->setMinRange(minRange);
627 js->setMaxRange(maxRange);
628 js->setCenter(center);
634 // Map of all known cursor names
635 // This used to contain all the Glut cursors, but those are
636 // not defined by other toolkits. It now supports only the cursor
637 // images we actually use, in the interest of portability. Someday,
638 // it would be cool to write an OpenGL cursor renderer, with the
639 // cursors defined as textures referenced in the property tree. This
640 // list could then be eliminated. -Andy
645 } mouse_cursor_map[] = {
646 { "none", MOUSE_CURSOR_NONE },
647 { "inherit", MOUSE_CURSOR_POINTER },
648 { "wait", MOUSE_CURSOR_WAIT },
649 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
650 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
655 FGInput::_init_mouse ()
657 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
660 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
661 if (mouse_nodes == 0) {
662 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
663 mouse_nodes = fgGetNode("/input/mice", true);
667 for (int i = 0; i < MAX_MICE; i++) {
668 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
669 mouse &m = _mouse_bindings[i];
671 // Grab node pointers
673 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
674 m.mode_node = fgGetNode(buf);
675 if (m.mode_node == NULL) {
676 m.mode_node = fgGetNode(buf, true);
677 m.mode_node->setIntValue(0);
679 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
680 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
681 m.mouse_button_nodes[j] = fgGetNode(buf, true);
682 m.mouse_button_nodes[j]->setBoolValue(false);
685 // Read all the modes
686 m.nModes = mouse_node->getIntValue("mode-count", 1);
687 m.modes = new mouse_mode[m.nModes];
689 for (int j = 0; j < m.nModes; j++) {
692 // Read the mouse cursor for this mode
693 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
694 const char * cursor_name =
695 mode_node->getStringValue("cursor", "inherit");
696 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
697 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
698 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
699 m.modes[j].cursor = mouse_cursor_map[k].cursor;
704 // Read other properties for this mode
705 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
706 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
708 // Read the button bindings for this mode
709 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
711 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
712 sprintf(buf, "mouse button %d", k);
713 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
714 _init_button(mode_node->getChild("button", k),
715 m.modes[j].buttons[k],
719 // Read the axis bindings for this mode
720 _read_bindings(mode_node->getChild("x-axis", 0, true),
721 m.modes[j].x_bindings,
723 _read_bindings(mode_node->getChild("y-axis", 0, true),
724 m.modes[j].y_bindings,
732 FGInput::_init_button (const SGPropertyNode * node,
737 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
739 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
741 // Get the bindings for the button
742 _read_bindings(node, b.bindings, KEYMOD_NONE);
748 FGInput::_update_keyboard ()
755 FGInput::_update_joystick (double dt)
757 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
759 // float js_val, diff;
760 float axis_values[MAX_JOYSTICK_AXES];
765 for ( i = 0; i < MAX_JOYSTICKS; i++) {
767 jsJoystick * js = _joystick_bindings[i].js;
768 if (js == 0 || js->notWorking())
771 js->read(&buttons, axis_values);
773 // Fire bindings for the axes.
774 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
775 axis &a = _joystick_bindings[i].axes[j];
777 // Do nothing if the axis position
778 // is unchanged; only a change in
779 // position fires the bindings.
780 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
781 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
782 a.last_value = axis_values[j];
783 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
784 // << a.bindings[modifiers].size() << " bindings");
785 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
786 a.bindings[modifiers][k]->fire(axis_values[j]);
789 // do we have to emulate axis buttons?
791 if(a.last_dt >= a.interval_sec) {
792 if (a.low.bindings[modifiers].size())
793 _update_button(_joystick_bindings[i].axes[j].low,
795 axis_values[j] < a.low_threshold,
798 if (a.high.bindings[modifiers].size())
799 _update_button(_joystick_bindings[i].axes[j].high,
801 axis_values[j] > a.high_threshold,
803 a.last_dt -= a.interval_sec;
807 // Fire bindings for the buttons.
808 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
809 button &b = _joystick_bindings[i].buttons[j];
811 if(b.last_dt >= b.interval_sec) {
812 _update_button(_joystick_bindings[i].buttons[j],
814 (buttons & (1 << j)) > 0,
816 b.last_dt -= b.interval_sec;
823 FGInput::_update_mouse ( double dt )
825 mouse &m = _mouse_bindings[0];
826 int mode = m.mode_node->getIntValue();
827 if (mode != m.current_mode) {
828 m.current_mode = mode;
829 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
830 if (mode >= 0 && mode < m.nModes) {
831 fgSetMouseCursor(m.modes[mode].cursor);
832 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
833 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
834 fgWarpMouse(m.x, m.y);
836 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
837 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
841 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
842 if ( m.x != m.save_x || m.y != m.save_y ) {
843 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
844 fgSetMouseCursor(m.modes[mode].cursor);
847 if ( m.timeout <= 0.0 ) {
848 fgSetMouseCursor(MOUSE_CURSOR_NONE);
858 FGInput::_update_button (button &b, int modifiers, bool pressed,
862 // The press event may be repeated.
863 if (!b.last_state || b.is_repeatable) {
864 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
865 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
866 b.bindings[modifiers][k]->fire(x, y);
869 // The release event is never repeated.
871 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
872 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
873 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
877 b.last_state = pressed;
882 FGInput::_read_bindings (const SGPropertyNode * node,
883 binding_list_t * binding_list,
886 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
887 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
888 for (unsigned int i = 0; i < bindings.size(); i++) {
889 const char *cmd = bindings[i]->getStringValue("command");
890 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
892 if (!strcmp(cmd, "nasal") && _module[0])
893 bindings[i]->setStringValue("module", _module);
894 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
897 // Read nested bindings for modifiers
898 if (node->getChild("mod-up") != 0)
899 _read_bindings(node->getChild("mod-up"), binding_list,
900 modifiers|KEYMOD_RELEASED);
902 if (node->getChild("mod-shift") != 0)
903 _read_bindings(node->getChild("mod-shift"), binding_list,
904 modifiers|KEYMOD_SHIFT);
906 if (node->getChild("mod-ctrl") != 0)
907 _read_bindings(node->getChild("mod-ctrl"), binding_list,
908 modifiers|KEYMOD_CTRL);
910 if (node->getChild("mod-alt") != 0)
911 _read_bindings(node->getChild("mod-alt"), binding_list,
912 modifiers|KEYMOD_ALT);
916 const vector<FGBinding *> &
917 FGInput::_find_key_bindings (unsigned int k, int modifiers)
919 unsigned char kc = (unsigned char)k;
920 button &b = _key_bindings[k];
922 // Try it straight, first.
923 if (b.bindings[modifiers].size() > 0)
924 return b.bindings[modifiers];
926 // Alt-Gr is CTRL+ALT
927 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
928 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
930 // Try removing the control modifier
932 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
933 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
935 // Try removing shift modifier
936 // for upper case or any punctuation
937 // (since different keyboards will
938 // shift different punctuation types)
939 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
940 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
942 // Try removing alt modifier for
943 // high-bit characters.
944 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
945 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
947 // Give up and return the empty vector.
949 return b.bindings[modifiers];
954 ////////////////////////////////////////////////////////////////////////
955 // Implementation of FGInput::button.
956 ////////////////////////////////////////////////////////////////////////
958 FGInput::button::button ()
959 : is_repeatable(false),
964 FGInput::button::~button ()
966 // FIXME: memory leak
967 // for (int i = 0; i < KEYMOD_MAX; i++)
968 // for (int j = 0; i < bindings[i].size(); j++)
969 // delete bindings[i][j];
974 ////////////////////////////////////////////////////////////////////////
975 // Implementation of FGInput::axis.
976 ////////////////////////////////////////////////////////////////////////
978 FGInput::axis::axis ()
979 : last_value(9999999),
986 FGInput::axis::~axis ()
988 // for (int i = 0; i < KEYMOD_MAX; i++)
989 // for (int j = 0; i < bindings[i].size(); j++)
990 // delete bindings[i][j];
995 ////////////////////////////////////////////////////////////////////////
996 // Implementation of FGInput::joystick.
997 ////////////////////////////////////////////////////////////////////////
999 FGInput::joystick::joystick ()
1003 FGInput::joystick::~joystick ()
1012 ////////////////////////////////////////////////////////////////////////
1013 // Implementation of FGInput::mouse_mode
1014 ////////////////////////////////////////////////////////////////////////
1016 FGInput::mouse_mode::mouse_mode ()
1017 : cursor(MOUSE_CURSOR_POINTER),
1019 pass_through(false),
1024 FGInput::mouse_mode::~mouse_mode ()
1026 // FIXME: memory leak
1027 // for (int i = 0; i < KEYMOD_MAX; i++) {
1029 // for (j = 0; i < x_bindings[i].size(); j++)
1030 // delete bindings[i][j];
1031 // for (j = 0; j < y_bindings[i].size(); j++)
1032 // delete bindings[i][j];
1039 ////////////////////////////////////////////////////////////////////////
1040 // Implementation of FGInput::mouse
1041 ////////////////////////////////////////////////////////////////////////
1043 FGInput::mouse::mouse ()
1052 FGInput::mouse::~mouse ()
1057 ////////////////////////////////////////////////////////////////////////
1058 // Implementation of OS callbacks.
1059 ////////////////////////////////////////////////////////////////////////
1061 void keyHandler(int key, int keymod, int mousex, int mousey)
1063 if((keymod & KEYMOD_RELEASED) == 0)
1064 if(puKeyboard(key, PU_DOWN))
1068 default_input->doKey(key, keymod, mousex, mousey);
1071 void mouseClickHandler(int button, int updown, int x, int y)
1074 default_input->doMouseClick(button, updown, x, y);
1077 void mouseMotionHandler(int x, int y)
1079 if (default_input != 0)
1080 default_input->doMouseMotion(x, y);