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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <simgear/compiler.h>
41 #include <simgear/compiler.h>
43 #include <simgear/constants.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/math/SGMath.hxx>
46 #include <simgear/props/props.hxx>
47 #include <simgear/scene/util/SGSceneUserData.hxx>
49 #include <Aircraft/aircraft.hxx>
50 #include <Autopilot/xmlauto.hxx>
51 #include <Cockpit/hud.hxx>
52 #include <Cockpit/panel.hxx>
53 #include <Cockpit/panel_io.hxx>
55 #include <Model/panelnode.hxx>
56 #include <Scripting/NasalSys.hxx>
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
63 #include <Scenery/scenery.hxx>
64 #include <Main/renderer.hxx>
66 SG_USING_STD(ifstream);
67 SG_USING_STD(ostringstream);
71 void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter*);
72 void mouseMotionHandler(int x, int y);
73 void keyHandler(int key, int keymod, int mousex, int mousey);
76 ////////////////////////////////////////////////////////////////////////
78 ////////////////////////////////////////////////////////////////////////
80 static FGInput * default_input = 0;
83 ////////////////////////////////////////////////////////////////////////
85 ////////////////////////////////////////////////////////////////////////
90 return fgGetKeyModifiers() >> 1;
96 return bool(fgGetKeyModifiers() & KEYMOD_SHIFT);
102 return bool(fgGetKeyModifiers() & KEYMOD_CTRL);
108 return bool(fgGetKeyModifiers() & KEYMOD_ALT);
114 return bool(fgGetKeyModifiers() & KEYMOD_META);
120 return bool(fgGetKeyModifiers() & KEYMOD_SUPER);
126 return bool(fgGetKeyModifiers() & KEYMOD_HYPER);
130 ////////////////////////////////////////////////////////////////////////
131 // Implementation of FGInput.
132 ////////////////////////////////////////////////////////////////////////
135 FGInput::FGInput () :
136 _key_event(fgGetNode("/devices/status/keyboard/event", true))
138 if (default_input == 0)
139 default_input = this;
144 if (default_input == this)
154 fgRegisterKeyHandler(keyHandler);
155 fgRegisterMouseClickHandler(mouseClickHandler);
156 fgRegisterMouseMotionHandler(mouseMotionHandler);
168 _postinit_joystick();
169 _postinit_keyboard();
175 fgTie("/devices/status/keyboard", getModifiers);
176 fgTie("/devices/status/keyboard/shift", getModShift);
177 fgTie("/devices/status/keyboard/ctrl", getModCtrl);
178 fgTie("/devices/status/keyboard/alt", getModAlt);
179 fgTie("/devices/status/keyboard/meta", getModMeta);
180 fgTie("/devices/status/keyboard/super", getModSuper);
181 fgTie("/devices/status/keyboard/hyper", getModHyper);
183 _key_event->tie("key", SGRawValuePointer<int>(&_key_code));
184 _key_event->tie("pressed", SGRawValuePointer<bool>(&_key_pressed));
185 _key_event->tie("modifier", SGRawValuePointer<int>(&_key_modifiers));
186 _key_event->tie("modifier/shift", SGRawValuePointer<bool>(&_key_shift));
187 _key_event->tie("modifier/ctrl", SGRawValuePointer<bool>(&_key_ctrl));
188 _key_event->tie("modifier/alt", SGRawValuePointer<bool>(&_key_alt));
189 _key_event->tie("modifier/meta", SGRawValuePointer<bool>(&_key_meta));
190 _key_event->tie("modifier/super", SGRawValuePointer<bool>(&_key_super));
191 _key_event->tie("modifier/hyper", SGRawValuePointer<bool>(&_key_hyper));
197 fgUntie("/devices/status/keyboard");
198 fgUntie("/devices/status/keyboard/shift");
199 fgUntie("/devices/status/keyboard/ctrl");
200 fgUntie("/devices/status/keyboard/alt");
201 fgUntie("/devices/status/keyboard/meta");
202 fgUntie("/devices/status/keyboard/super");
203 fgUntie("/devices/status/keyboard/hyper");
205 _key_event->untie("key");
206 _key_event->untie("pressed");
207 _key_event->untie("modifier");
208 _key_event->untie("modifier/shift");
209 _key_event->untie("modifier/ctrl");
210 _key_event->untie("modifier/alt");
211 _key_event->untie("modifier/meta");
212 _key_event->untie("modifier/super");
213 _key_event->untie("modifier/hyper");
217 FGInput::update (double dt)
220 _update_joystick(dt);
237 FGInput::is_suspended () const
243 FGInput::makeDefault (bool status)
246 default_input = this;
247 else if (default_input == this)
252 FGInput::doKey (int k, int modifiers, int x, int y)
255 if (k < 0 || k >= MAX_KEYS) {
256 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
261 _key_modifiers = modifiers >> 1;
262 _key_pressed = !bool(modifiers & KEYMOD_RELEASED);
263 _key_shift = bool(modifiers & KEYMOD_SHIFT);
264 _key_ctrl = bool(modifiers & KEYMOD_CTRL);
265 _key_alt = bool(modifiers & KEYMOD_ALT);
266 _key_meta = bool(modifiers & KEYMOD_META);
267 _key_super = bool(modifiers & KEYMOD_SUPER);
268 _key_hyper = bool(modifiers & KEYMOD_HYPER);
269 _key_event->fireValueChanged();
274 modifiers = _key_modifiers << 1;
276 modifiers |= KEYMOD_RELEASED;
277 button &b = _key_bindings[k];
280 if (!(modifiers & KEYMOD_RELEASED)) {
281 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
282 << " with modifiers " << modifiers );
283 if (!b.last_state || b.is_repeatable) {
284 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
286 for (unsigned int i = 0; i < bindings.size(); i++)
293 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
294 << " with modifiers " << modifiers);
296 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
297 for (unsigned int i = 0; i < bindings.size(); i++)
305 FGInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
307 int modifiers = fgGetKeyModifiers();
309 mouse &m = _mouse_bindings[0];
310 mouse_mode &mode = m.modes[m.current_mode];
312 // Let the property manager know.
313 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
314 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
316 // Pass on to PUI and the panel if
317 // requested, and return if one of
318 // them consumes the event.
320 if (updown != MOUSE_BUTTON_DOWN) {
321 // Execute the mouse up event in any case, may be we should
322 // stop processing here?
323 while (!_activePickCallbacks[b].empty()) {
324 _activePickCallbacks[b].front()->buttonReleased();
325 _activePickCallbacks[b].pop_front();
329 if (mode.pass_through) {
330 // The pu stuff seems to need that. May be it does opengl picking ...
332 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
334 else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
335 globals->get_current_panel()->getVisibility() &&
336 globals->get_current_panel()->doMouseAction(b, updown, x, y))
338 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
341 // pui didn't want the click event so compute a
342 // scenegraph intersection point corresponding to the mouse click
343 if (updown == MOUSE_BUTTON_DOWN) {
345 // Get the list of hit callbacks. Take the first callback that
346 // accepts the mouse button press and ignore the rest of them
347 // That is they get sorted by distance and by scenegraph depth.
348 // The nearest one is the first one and the deepest
349 // (the most specialized one in the scenegraph) is the first.
350 std::vector<SGSceneryPick> pickList;
351 if (FGRenderer::pick(x, y, pickList, ea)) {
352 std::vector<SGSceneryPick>::const_iterator i;
353 for (i = pickList.begin(); i != pickList.end(); ++i) {
354 if (i->callback->buttonPressed(b, i->info)) {
355 _activePickCallbacks[b].push_back(i->callback);
364 // OK, PUI and the panel didn't want the click
365 if (b >= MAX_MOUSE_BUTTONS) {
366 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
367 << " where only " << MAX_MOUSE_BUTTONS << " expected");
371 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
375 FGInput::doMouseMotion (int x, int y)
377 // Don't call fgGetKeyModifiers() here, until we are using a
378 // toolkit that supports getting the mods from outside a key
379 // callback. Glut doesn't.
380 int modifiers = KEYMOD_NONE;
382 int xsize = fgGetInt("/sim/startup/xsize", 800);
383 int ysize = fgGetInt("/sim/startup/ysize", 600);
385 mouse &m = _mouse_bindings[0];
387 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
392 mouse_mode &mode = m.modes[m.current_mode];
394 // Pass on to PUI if requested, and return
395 // if PUI consumed the event.
396 if (mode.pass_through && puMouse(x, y)) {
402 // OK, PUI didn't want the event,
403 // so we can play with it.
406 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
407 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
411 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
412 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
415 // Constrain the mouse if requested
416 if (mode.constrained) {
417 bool need_warp = false;
418 if (x <= (xsize * .25) || x >= (xsize * .75)) {
423 if (y <= (ysize * .25) || y >= (ysize * .75)) {
433 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
436 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
441 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
443 ulDir *dir = ulOpenDir(path->c_str());
446 while ((dent = ulReadDir(dir)) != 0) {
447 if (dent->d_name[0] == '.')
450 SGPath p(path->str());
451 p.append(dent->d_name);
452 _scan_joystick_dir(&p, node, index);
456 } else if (path->extension() == "xml") {
457 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
458 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
459 readProperties(path->str(), n);
460 n->setStringValue("source", path->c_str());
466 FGInput::_init_joystick ()
469 // TODO: zero the old bindings first.
470 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
471 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
473 // read all joystick xml files into /input/joysticks/js_named[1000++]
474 SGPath path(globals->get_fg_root());
475 path.append("Input/Joysticks");
476 int js_named_index = 1000;
477 _scan_joystick_dir(&path, js_nodes, &js_named_index);
479 // build name->node map with each <name> (reverse order)
480 map<string, SGPropertyNode_ptr> jsmap;
481 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
483 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
484 SGPropertyNode *n = js_named[k];
485 vector<SGPropertyNode_ptr> names = n->getChildren("name");
486 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
487 for (unsigned int j = 0; j < names.size(); j++)
488 jsmap[names[j]->getStringValue()] = n;
492 for (int i = 0; i < MAX_JOYSTICKS; i++) {
493 jsJoystick * js = new jsJoystick(i);
494 _joystick_bindings[i].js = js;
496 if (js->notWorking()) {
497 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
501 const char * name = js->getName();
502 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
505 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
508 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
509 SGPropertyNode_ptr named;
511 if ((named = jsmap[name])) {
512 string source = named->getStringValue("source", "user defined");
513 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
515 } else if ((named = jsmap["default"])) {
516 string source = named->getStringValue("source", "user defined");
517 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
518 << "\"\nUsing default: \"" << source << '"');
521 throw sg_throwable(string("No joystick configuration file with "
522 "<name>default</name> entry found!"));
525 js_node = js_nodes->getChild("js", i, true);
526 copyProperties(named, js_node);
527 js_node->setStringValue("id", name);
531 // get rid of unused config nodes
532 js_nodes->removeChildren("js-named", false);
537 FGInput::_postinit_keyboard()
539 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
541 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
542 if (key_nodes == 0) {
543 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
544 key_nodes = fgGetNode("/input/keyboard", true);
547 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
548 vector<SGPropertyNode_ptr> nasal = key_nodes->getChildren("nasal");
549 for (unsigned int j = 0; j < nasal.size(); j++) {
550 nasal[j]->setStringValue("module", _module.c_str());
551 nasalsys->handleCommand(nasal[j]);
554 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
555 for (unsigned int i = 0; i < keys.size(); i++) {
556 int index = keys[i]->getIndex();
557 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
559 _key_bindings[index].bindings->clear();
560 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
561 _key_bindings[index].last_state = 0;
562 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
568 FGInput::_postinit_joystick()
570 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
571 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
573 for (int i = 0; i < MAX_JOYSTICKS; i++) {
574 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
575 jsJoystick *js = _joystick_bindings[i].js;
576 if (!js_node || js->notWorking())
581 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
582 unsigned int nbuttons = jsCaps.wNumButtons;
583 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
585 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
588 int naxes = js->getNumAxes();
589 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
590 _joystick_bindings[i].naxes = naxes;
591 _joystick_bindings[i].nbuttons = nbuttons;
593 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
595 // Set up range arrays
596 float minRange[MAX_JOYSTICK_AXES];
597 float maxRange[MAX_JOYSTICK_AXES];
598 float center[MAX_JOYSTICK_AXES];
600 // Initialize with default values
601 js->getMinRange(minRange);
602 js->getMaxRange(maxRange);
603 js->getCenter(center);
605 // Allocate axes and buttons
606 _joystick_bindings[i].axes = new axis[naxes];
607 _joystick_bindings[i].buttons = new button[nbuttons];
610 // Initialize nasal groups.
615 nasalsys->createModule(_module.c_str(), _module.c_str(), "", 0);
617 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
619 for (j = 0; j < nasal.size(); j++) {
620 nasal[j]->setStringValue("module", _module.c_str());
621 nasalsys->handleCommand(nasal[j]);
625 // Initialize the axes.
627 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
628 size_t nb_axes = axes.size();
629 for (j = 0; j < nb_axes; j++ ) {
630 const SGPropertyNode * axis_node = axes[j];
631 const SGPropertyNode * num_node = axis_node->getChild("number");
632 int n_axis = axis_node->getIndex();
634 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
636 // Silently ignore platforms that are not specified within the
637 // <number></number> section
642 if (n_axis >= naxes) {
643 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
646 axis &a = _joystick_bindings[i].axes[n_axis];
648 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
650 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
651 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
652 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
653 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
655 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
657 // Initialize the virtual axis buttons.
658 _init_button(axis_node->getChild("low"), a.low, "low");
659 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
661 _init_button(axis_node->getChild("high"), a.high, "high");
662 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
663 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
668 // Initialize the buttons.
670 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
672 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
673 const SGPropertyNode * button_node = buttons[j];
674 const SGPropertyNode * num_node = button_node->getChild("number");
675 size_t n_but = button_node->getIndex();
677 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
680 if (n_but >= nbuttons) {
681 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
685 sprintf(buf, "%d", n_but);
686 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
687 _init_button(button_node,
688 _joystick_bindings[i].buttons[n_but],
691 // get interval-sec property
692 button &b = _joystick_bindings[i].buttons[n_but];
693 if (button_node != 0) {
694 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
699 js->setMinRange(minRange);
700 js->setMaxRange(maxRange);
701 js->setCenter(center);
707 // Map of all known cursor names
708 // This used to contain all the Glut cursors, but those are
709 // not defined by other toolkits. It now supports only the cursor
710 // images we actually use, in the interest of portability. Someday,
711 // it would be cool to write an OpenGL cursor renderer, with the
712 // cursors defined as textures referenced in the property tree. This
713 // list could then be eliminated. -Andy
718 } mouse_cursor_map[] = {
719 { "none", MOUSE_CURSOR_NONE },
720 { "inherit", MOUSE_CURSOR_POINTER },
721 { "wait", MOUSE_CURSOR_WAIT },
722 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
723 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
728 FGInput::_init_mouse ()
730 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
733 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
734 if (mouse_nodes == 0) {
735 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
736 mouse_nodes = fgGetNode("/input/mice", true);
740 for (int i = 0; i < MAX_MICE; i++) {
741 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
742 mouse &m = _mouse_bindings[i];
744 // Grab node pointers
746 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
747 m.mode_node = fgGetNode(buf);
748 if (m.mode_node == NULL) {
749 m.mode_node = fgGetNode(buf, true);
750 m.mode_node->setIntValue(0);
752 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
753 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
754 m.mouse_button_nodes[j] = fgGetNode(buf, true);
755 m.mouse_button_nodes[j]->setBoolValue(false);
758 // Read all the modes
759 m.nModes = mouse_node->getIntValue("mode-count", 1);
760 m.modes = new mouse_mode[m.nModes];
762 for (int j = 0; j < m.nModes; j++) {
765 // Read the mouse cursor for this mode
766 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
767 const char * cursor_name =
768 mode_node->getStringValue("cursor", "inherit");
769 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
770 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
771 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
772 m.modes[j].cursor = mouse_cursor_map[k].cursor;
777 // Read other properties for this mode
778 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
779 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
781 // Read the button bindings for this mode
782 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
784 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
785 sprintf(buf, "mouse button %d", k);
786 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
787 _init_button(mode_node->getChild("button", k),
788 m.modes[j].buttons[k],
792 // Read the axis bindings for this mode
793 _read_bindings(mode_node->getChild("x-axis", 0, true),
794 m.modes[j].x_bindings,
796 _read_bindings(mode_node->getChild("y-axis", 0, true),
797 m.modes[j].y_bindings,
805 FGInput::_init_button (const SGPropertyNode * node,
810 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
812 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
814 // Get the bindings for the button
815 _read_bindings(node, b.bindings, KEYMOD_NONE);
821 FGInput::_update_keyboard ()
828 FGInput::_update_joystick (double dt)
830 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
832 // float js_val, diff;
833 float axis_values[MAX_JOYSTICK_AXES];
838 for ( i = 0; i < MAX_JOYSTICKS; i++) {
840 jsJoystick * js = _joystick_bindings[i].js;
841 if (js == 0 || js->notWorking())
844 js->read(&buttons, axis_values);
846 // Fire bindings for the axes.
847 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
848 axis &a = _joystick_bindings[i].axes[j];
850 // Do nothing if the axis position
851 // is unchanged; only a change in
852 // position fires the bindings.
853 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
854 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
855 a.last_value = axis_values[j];
856 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
857 // << a.bindings[modifiers].size() << " bindings");
858 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
859 a.bindings[modifiers][k]->fire(axis_values[j]);
862 // do we have to emulate axis buttons?
864 if(a.last_dt >= a.interval_sec) {
865 if (a.low.bindings[modifiers].size())
866 _update_button(_joystick_bindings[i].axes[j].low,
868 axis_values[j] < a.low_threshold,
871 if (a.high.bindings[modifiers].size())
872 _update_button(_joystick_bindings[i].axes[j].high,
874 axis_values[j] > a.high_threshold,
876 a.last_dt -= a.interval_sec;
880 // Fire bindings for the buttons.
881 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
882 button &b = _joystick_bindings[i].buttons[j];
884 if(b.last_dt >= b.interval_sec) {
885 _update_button(_joystick_bindings[i].buttons[j],
887 (buttons & (1 << j)) > 0,
889 b.last_dt -= b.interval_sec;
896 FGInput::_update_mouse ( double dt )
898 mouse &m = _mouse_bindings[0];
899 int mode = m.mode_node->getIntValue();
900 if (mode != m.current_mode) {
901 m.current_mode = mode;
902 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
903 if (mode >= 0 && mode < m.nModes) {
904 fgSetMouseCursor(m.modes[mode].cursor);
905 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
906 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
907 fgWarpMouse(m.x, m.y);
909 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
910 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
914 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
915 if ( m.x != m.save_x || m.y != m.save_y ) {
916 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
917 fgSetMouseCursor(m.modes[mode].cursor);
920 if ( m.timeout <= 0.0 ) {
921 fgSetMouseCursor(MOUSE_CURSOR_NONE);
929 // handle repeatable mouse press events
930 std::map<int, std::list<SGSharedPtr<SGPickCallback> > >::iterator mi;
931 for (mi = _activePickCallbacks.begin();
932 mi != _activePickCallbacks.end(); ++mi) {
933 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
934 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
941 FGInput::_update_button (button &b, int modifiers, bool pressed,
945 // The press event may be repeated.
946 if (!b.last_state || b.is_repeatable) {
947 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
948 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++) {
949 b.bindings[modifiers][k]->fire(x, y);
953 // The release event is never repeated.
955 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
956 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
957 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
961 b.last_state = pressed;
966 FGInput::_read_bindings (const SGPropertyNode * node,
967 binding_list_t * binding_list,
970 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
971 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
972 for (unsigned int i = 0; i < bindings.size(); i++) {
973 const char *cmd = bindings[i]->getStringValue("command");
974 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
976 if (!strcmp(cmd, "nasal") && !_module.empty())
977 bindings[i]->setStringValue("module", _module.c_str());
978 binding_list[modifiers].push_back(new SGBinding(bindings[i], globals->get_props()));
981 // Read nested bindings for modifiers
982 if (node->getChild("mod-up") != 0)
983 _read_bindings(node->getChild("mod-up"), binding_list,
984 modifiers|KEYMOD_RELEASED);
986 if (node->getChild("mod-shift") != 0)
987 _read_bindings(node->getChild("mod-shift"), binding_list,
988 modifiers|KEYMOD_SHIFT);
990 if (node->getChild("mod-ctrl") != 0)
991 _read_bindings(node->getChild("mod-ctrl"), binding_list,
992 modifiers|KEYMOD_CTRL);
994 if (node->getChild("mod-alt") != 0)
995 _read_bindings(node->getChild("mod-alt"), binding_list,
996 modifiers|KEYMOD_ALT);
998 if (node->getChild("mod-meta") != 0)
999 _read_bindings(node->getChild("mod-meta"), binding_list,
1000 modifiers|KEYMOD_META);
1002 if (node->getChild("mod-super") != 0)
1003 _read_bindings(node->getChild("mod-super"), binding_list,
1004 modifiers|KEYMOD_SUPER);
1006 if (node->getChild("mod-hyper") != 0)
1007 _read_bindings(node->getChild("mod-hyper"), binding_list,
1008 modifiers|KEYMOD_HYPER);
1012 const FGInput::binding_list_t&
1013 FGInput::_find_key_bindings (unsigned int k, int modifiers)
1015 unsigned char kc = (unsigned char)k;
1016 button &b = _key_bindings[k];
1018 // Try it straight, first.
1019 if (b.bindings[modifiers].size() > 0)
1020 return b.bindings[modifiers];
1022 // Alt-Gr is CTRL+ALT
1023 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
1024 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
1026 // Try removing the control modifier
1027 // for control keys.
1028 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
1029 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
1031 // Try removing shift modifier
1032 // for upper case or any punctuation
1033 // (since different keyboards will
1034 // shift different punctuation types)
1035 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
1036 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
1038 // Try removing alt modifier for
1039 // high-bit characters.
1040 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
1041 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
1043 // Give up and return the empty vector.
1045 return b.bindings[modifiers];
1050 ////////////////////////////////////////////////////////////////////////
1051 // Implementation of FGInput::button.
1052 ////////////////////////////////////////////////////////////////////////
1054 FGInput::button::button ()
1055 : is_repeatable(false),
1062 FGInput::button::~button ()
1064 // FIXME: memory leak
1065 // for (int i = 0; i < KEYMOD_MAX; i++)
1066 // for (int j = 0; i < bindings[i].size(); j++)
1067 // delete bindings[i][j];
1072 ////////////////////////////////////////////////////////////////////////
1073 // Implementation of FGInput::axis.
1074 ////////////////////////////////////////////////////////////////////////
1076 FGInput::axis::axis ()
1077 : last_value(9999999),
1079 low_threshold(-0.9),
1080 high_threshold(0.9),
1086 FGInput::axis::~axis ()
1088 // for (int i = 0; i < KEYMOD_MAX; i++)
1089 // for (int j = 0; i < bindings[i].size(); j++)
1090 // delete bindings[i][j];
1095 ////////////////////////////////////////////////////////////////////////
1096 // Implementation of FGInput::joystick.
1097 ////////////////////////////////////////////////////////////////////////
1099 FGInput::joystick::joystick ()
1109 FGInput::joystick::~joystick ()
1118 ////////////////////////////////////////////////////////////////////////
1119 // Implementation of FGInput::mouse_mode
1120 ////////////////////////////////////////////////////////////////////////
1122 FGInput::mouse_mode::mouse_mode ()
1123 : cursor(MOUSE_CURSOR_POINTER),
1125 pass_through(false),
1130 FGInput::mouse_mode::~mouse_mode ()
1132 // FIXME: memory leak
1133 // for (int i = 0; i < KEYMOD_MAX; i++) {
1135 // for (j = 0; i < x_bindings[i].size(); j++)
1136 // delete bindings[i][j];
1137 // for (j = 0; j < y_bindings[i].size(); j++)
1138 // delete bindings[i][j];
1145 ////////////////////////////////////////////////////////////////////////
1146 // Implementation of FGInput::mouse
1147 ////////////////////////////////////////////////////////////////////////
1149 FGInput::mouse::mouse ()
1160 FGInput::mouse::~mouse ()
1165 ////////////////////////////////////////////////////////////////////////
1166 // Implementation of OS callbacks.
1167 ////////////////////////////////////////////////////////////////////////
1169 void keyHandler(int key, int keymod, int mousex, int mousey)
1171 if((keymod & KEYMOD_RELEASED) == 0)
1172 if(puKeyboard(key, PU_DOWN))
1176 default_input->doKey(key, keymod, mousex, mousey);
1179 void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
1182 default_input->doMouseClick(button, updown, x, y, mainWindow, ea);
1185 void mouseMotionHandler(int x, int y)
1187 if (default_input != 0)
1188 default_input->doMouseMotion(x, y);