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 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
332 else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
333 globals->get_current_panel()->getVisibility() &&
334 globals->get_current_panel()->doMouseAction(b, updown, x, y))
336 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
339 // pui didn't want the click event so compute a
340 // scenegraph intersection point corresponding to the mouse click
341 if (updown == MOUSE_BUTTON_DOWN) {
343 // Get the list of hit callbacks. Take the first callback that
344 // accepts the mouse button press and ignore the rest of them
345 // That is they get sorted by distance and by scenegraph depth.
346 // The nearest one is the first one and the deepest
347 // (the most specialized one in the scenegraph) is the first.
348 std::vector<SGSceneryPick> pickList;
349 if (FGRenderer::pick(x, y, pickList, ea)) {
350 std::vector<SGSceneryPick>::const_iterator i;
351 for (i = pickList.begin(); i != pickList.end(); ++i) {
352 if (i->callback->buttonPressed(b, i->info)) {
353 _activePickCallbacks[b].push_back(i->callback);
362 // OK, PUI and the panel didn't want the click
363 if (b >= MAX_MOUSE_BUTTONS) {
364 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
365 << " where only " << MAX_MOUSE_BUTTONS << " expected");
369 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
373 FGInput::doMouseMotion (int x, int y)
375 // Don't call fgGetKeyModifiers() here, until we are using a
376 // toolkit that supports getting the mods from outside a key
377 // callback. Glut doesn't.
378 int modifiers = KEYMOD_NONE;
380 int xsize = fgGetInt("/sim/startup/xsize", 800);
381 int ysize = fgGetInt("/sim/startup/ysize", 600);
383 mouse &m = _mouse_bindings[0];
385 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
390 mouse_mode &mode = m.modes[m.current_mode];
392 // Pass on to PUI if requested, and return
393 // if PUI consumed the event.
394 if (mode.pass_through && puMouse(x, y)) {
400 // OK, PUI didn't want the event,
401 // so we can play with it.
404 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
405 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
409 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
410 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
413 // Constrain the mouse if requested
414 if (mode.constrained) {
415 bool need_warp = false;
416 if (x <= (xsize * .25) || x >= (xsize * .75)) {
421 if (y <= (ysize * .25) || y >= (ysize * .75)) {
431 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
434 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
439 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
441 ulDir *dir = ulOpenDir(path->c_str());
444 while ((dent = ulReadDir(dir)) != 0) {
445 if (dent->d_name[0] == '.')
448 SGPath p(path->str());
449 p.append(dent->d_name);
450 _scan_joystick_dir(&p, node, index);
454 } else if (path->extension() == "xml") {
455 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
456 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
457 readProperties(path->str(), n);
458 n->setStringValue("source", path->c_str());
464 FGInput::_init_joystick ()
467 // TODO: zero the old bindings first.
468 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
469 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
471 // read all joystick xml files into /input/joysticks/js_named[1000++]
472 SGPath path(globals->get_fg_root());
473 path.append("Input/Joysticks");
474 int js_named_index = 1000;
475 _scan_joystick_dir(&path, js_nodes, &js_named_index);
477 // build name->node map with each <name> (reverse order)
478 map<string, SGPropertyNode_ptr> jsmap;
479 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
481 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
482 SGPropertyNode *n = js_named[k];
483 vector<SGPropertyNode_ptr> names = n->getChildren("name");
484 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
485 for (unsigned int j = 0; j < names.size(); j++)
486 jsmap[names[j]->getStringValue()] = n;
490 for (int i = 0; i < MAX_JOYSTICKS; i++) {
491 jsJoystick * js = new jsJoystick(i);
492 _joystick_bindings[i].js = js;
494 if (js->notWorking()) {
495 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
499 const char * name = js->getName();
500 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
503 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
506 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
507 SGPropertyNode_ptr named;
509 if ((named = jsmap[name])) {
510 string source = named->getStringValue("source", "user defined");
511 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
513 } else if ((named = jsmap["default"])) {
514 string source = named->getStringValue("source", "user defined");
515 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
516 << "\"\nUsing default: \"" << source << '"');
519 throw sg_throwable(string("No joystick configuration file with "
520 "<name>default</name> entry found!"));
523 js_node = js_nodes->getChild("js", i, true);
524 copyProperties(named, js_node);
525 js_node->setStringValue("id", name);
529 // get rid of unused config nodes
530 js_nodes->removeChildren("js-named", false);
535 FGInput::_postinit_keyboard()
537 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
539 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
540 if (key_nodes == 0) {
541 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
542 key_nodes = fgGetNode("/input/keyboard", true);
545 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
546 vector<SGPropertyNode_ptr> nasal = key_nodes->getChildren("nasal");
547 for (unsigned int j = 0; j < nasal.size(); j++) {
548 nasal[j]->setStringValue("module", _module.c_str());
549 nasalsys->handleCommand(nasal[j]);
552 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
553 for (unsigned int i = 0; i < keys.size(); i++) {
554 int index = keys[i]->getIndex();
555 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
557 _key_bindings[index].bindings->clear();
558 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
559 _key_bindings[index].last_state = 0;
560 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
566 FGInput::_postinit_joystick()
568 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
569 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
571 for (int i = 0; i < MAX_JOYSTICKS; i++) {
572 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
573 jsJoystick *js = _joystick_bindings[i].js;
574 if (!js_node || js->notWorking())
579 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
580 unsigned int nbuttons = jsCaps.wNumButtons;
581 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
583 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
586 int naxes = js->getNumAxes();
587 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
588 _joystick_bindings[i].naxes = naxes;
589 _joystick_bindings[i].nbuttons = nbuttons;
591 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
593 // Set up range arrays
594 float minRange[MAX_JOYSTICK_AXES];
595 float maxRange[MAX_JOYSTICK_AXES];
596 float center[MAX_JOYSTICK_AXES];
598 // Initialize with default values
599 js->getMinRange(minRange);
600 js->getMaxRange(maxRange);
601 js->getCenter(center);
603 // Allocate axes and buttons
604 _joystick_bindings[i].axes = new axis[naxes];
605 _joystick_bindings[i].buttons = new button[nbuttons];
608 // Initialize nasal groups.
613 nasalsys->createModule(_module.c_str(), _module.c_str(), "", 0);
615 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
617 for (j = 0; j < nasal.size(); j++) {
618 nasal[j]->setStringValue("module", _module.c_str());
619 nasalsys->handleCommand(nasal[j]);
623 // Initialize the axes.
625 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
626 size_t nb_axes = axes.size();
627 for (j = 0; j < nb_axes; j++ ) {
628 const SGPropertyNode * axis_node = axes[j];
629 const SGPropertyNode * num_node = axis_node->getChild("number");
630 int n_axis = axis_node->getIndex();
632 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
634 // Silently ignore platforms that are not specified within the
635 // <number></number> section
640 if (n_axis >= naxes) {
641 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
644 axis &a = _joystick_bindings[i].axes[n_axis];
646 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
648 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
649 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
650 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
651 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
653 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
655 // Initialize the virtual axis buttons.
656 _init_button(axis_node->getChild("low"), a.low, "low");
657 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
659 _init_button(axis_node->getChild("high"), a.high, "high");
660 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
661 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
666 // Initialize the buttons.
668 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
670 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
671 const SGPropertyNode * button_node = buttons[j];
672 const SGPropertyNode * num_node = button_node->getChild("number");
673 size_t n_but = button_node->getIndex();
675 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
678 if (n_but >= nbuttons) {
679 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
683 sprintf(buf, "%d", n_but);
684 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
685 _init_button(button_node,
686 _joystick_bindings[i].buttons[n_but],
689 // get interval-sec property
690 button &b = _joystick_bindings[i].buttons[n_but];
691 if (button_node != 0) {
692 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
697 js->setMinRange(minRange);
698 js->setMaxRange(maxRange);
699 js->setCenter(center);
705 // Map of all known cursor names
706 // This used to contain all the Glut cursors, but those are
707 // not defined by other toolkits. It now supports only the cursor
708 // images we actually use, in the interest of portability. Someday,
709 // it would be cool to write an OpenGL cursor renderer, with the
710 // cursors defined as textures referenced in the property tree. This
711 // list could then be eliminated. -Andy
716 } mouse_cursor_map[] = {
717 { "none", MOUSE_CURSOR_NONE },
718 { "inherit", MOUSE_CURSOR_POINTER },
719 { "wait", MOUSE_CURSOR_WAIT },
720 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
721 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
726 FGInput::_init_mouse ()
728 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
731 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
732 if (mouse_nodes == 0) {
733 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
734 mouse_nodes = fgGetNode("/input/mice", true);
738 for (int i = 0; i < MAX_MICE; i++) {
739 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
740 mouse &m = _mouse_bindings[i];
742 // Grab node pointers
744 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
745 m.mode_node = fgGetNode(buf);
746 if (m.mode_node == NULL) {
747 m.mode_node = fgGetNode(buf, true);
748 m.mode_node->setIntValue(0);
750 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
751 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
752 m.mouse_button_nodes[j] = fgGetNode(buf, true);
753 m.mouse_button_nodes[j]->setBoolValue(false);
756 // Read all the modes
757 m.nModes = mouse_node->getIntValue("mode-count", 1);
758 m.modes = new mouse_mode[m.nModes];
760 for (int j = 0; j < m.nModes; j++) {
763 // Read the mouse cursor for this mode
764 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
765 const char * cursor_name =
766 mode_node->getStringValue("cursor", "inherit");
767 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
768 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
769 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
770 m.modes[j].cursor = mouse_cursor_map[k].cursor;
775 // Read other properties for this mode
776 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
777 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
779 // Read the button bindings for this mode
780 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
782 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
783 sprintf(buf, "mouse button %d", k);
784 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
785 _init_button(mode_node->getChild("button", k),
786 m.modes[j].buttons[k],
790 // Read the axis bindings for this mode
791 _read_bindings(mode_node->getChild("x-axis", 0, true),
792 m.modes[j].x_bindings,
794 _read_bindings(mode_node->getChild("y-axis", 0, true),
795 m.modes[j].y_bindings,
803 FGInput::_init_button (const SGPropertyNode * node,
808 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
810 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
812 // Get the bindings for the button
813 _read_bindings(node, b.bindings, KEYMOD_NONE);
819 FGInput::_update_keyboard ()
826 FGInput::_update_joystick (double dt)
828 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
830 // float js_val, diff;
831 float axis_values[MAX_JOYSTICK_AXES];
836 for ( i = 0; i < MAX_JOYSTICKS; i++) {
838 jsJoystick * js = _joystick_bindings[i].js;
839 if (js == 0 || js->notWorking())
842 js->read(&buttons, axis_values);
844 // Fire bindings for the axes.
845 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
846 axis &a = _joystick_bindings[i].axes[j];
848 // Do nothing if the axis position
849 // is unchanged; only a change in
850 // position fires the bindings.
851 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
852 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
853 a.last_value = axis_values[j];
854 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
855 // << a.bindings[modifiers].size() << " bindings");
856 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
857 a.bindings[modifiers][k]->fire(axis_values[j]);
860 // do we have to emulate axis buttons?
862 if(a.last_dt >= a.interval_sec) {
863 if (a.low.bindings[modifiers].size())
864 _update_button(_joystick_bindings[i].axes[j].low,
866 axis_values[j] < a.low_threshold,
869 if (a.high.bindings[modifiers].size())
870 _update_button(_joystick_bindings[i].axes[j].high,
872 axis_values[j] > a.high_threshold,
874 a.last_dt -= a.interval_sec;
878 // Fire bindings for the buttons.
879 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
880 button &b = _joystick_bindings[i].buttons[j];
882 if(b.last_dt >= b.interval_sec) {
883 _update_button(_joystick_bindings[i].buttons[j],
885 (buttons & (1 << j)) > 0,
887 b.last_dt -= b.interval_sec;
894 FGInput::_update_mouse ( double dt )
896 mouse &m = _mouse_bindings[0];
897 int mode = m.mode_node->getIntValue();
898 if (mode != m.current_mode) {
899 m.current_mode = mode;
900 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
901 if (mode >= 0 && mode < m.nModes) {
902 fgSetMouseCursor(m.modes[mode].cursor);
903 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
904 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
905 fgWarpMouse(m.x, m.y);
907 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
908 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
912 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
913 if ( m.x != m.save_x || m.y != m.save_y ) {
914 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
915 fgSetMouseCursor(m.modes[mode].cursor);
918 if ( m.timeout <= 0.0 ) {
919 fgSetMouseCursor(MOUSE_CURSOR_NONE);
927 // handle repeatable mouse press events
928 std::map<int, std::list<SGSharedPtr<SGPickCallback> > >::iterator mi;
929 for (mi = _activePickCallbacks.begin();
930 mi != _activePickCallbacks.end(); ++mi) {
931 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
932 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
939 FGInput::_update_button (button &b, int modifiers, bool pressed,
943 // The press event may be repeated.
944 if (!b.last_state || b.is_repeatable) {
945 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
946 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++) {
947 b.bindings[modifiers][k]->fire(x, y);
951 // The release event is never repeated.
953 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
954 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
955 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
959 b.last_state = pressed;
964 FGInput::_read_bindings (const SGPropertyNode * node,
965 binding_list_t * binding_list,
968 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
969 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
970 for (unsigned int i = 0; i < bindings.size(); i++) {
971 const char *cmd = bindings[i]->getStringValue("command");
972 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
974 if (!strcmp(cmd, "nasal") && !_module.empty())
975 bindings[i]->setStringValue("module", _module.c_str());
976 binding_list[modifiers].push_back(new SGBinding(bindings[i], globals->get_props()));
979 // Read nested bindings for modifiers
980 if (node->getChild("mod-up") != 0)
981 _read_bindings(node->getChild("mod-up"), binding_list,
982 modifiers|KEYMOD_RELEASED);
984 if (node->getChild("mod-shift") != 0)
985 _read_bindings(node->getChild("mod-shift"), binding_list,
986 modifiers|KEYMOD_SHIFT);
988 if (node->getChild("mod-ctrl") != 0)
989 _read_bindings(node->getChild("mod-ctrl"), binding_list,
990 modifiers|KEYMOD_CTRL);
992 if (node->getChild("mod-alt") != 0)
993 _read_bindings(node->getChild("mod-alt"), binding_list,
994 modifiers|KEYMOD_ALT);
996 if (node->getChild("mod-meta") != 0)
997 _read_bindings(node->getChild("mod-meta"), binding_list,
998 modifiers|KEYMOD_META);
1000 if (node->getChild("mod-super") != 0)
1001 _read_bindings(node->getChild("mod-super"), binding_list,
1002 modifiers|KEYMOD_SUPER);
1004 if (node->getChild("mod-hyper") != 0)
1005 _read_bindings(node->getChild("mod-hyper"), binding_list,
1006 modifiers|KEYMOD_HYPER);
1010 const FGInput::binding_list_t&
1011 FGInput::_find_key_bindings (unsigned int k, int modifiers)
1013 unsigned char kc = (unsigned char)k;
1014 button &b = _key_bindings[k];
1016 // Try it straight, first.
1017 if (b.bindings[modifiers].size() > 0)
1018 return b.bindings[modifiers];
1020 // Alt-Gr is CTRL+ALT
1021 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
1022 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
1024 // Try removing the control modifier
1025 // for control keys.
1026 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
1027 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
1029 // Try removing shift modifier
1030 // for upper case or any punctuation
1031 // (since different keyboards will
1032 // shift different punctuation types)
1033 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
1034 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
1036 // Try removing alt modifier for
1037 // high-bit characters.
1038 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
1039 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
1041 // Give up and return the empty vector.
1043 return b.bindings[modifiers];
1048 ////////////////////////////////////////////////////////////////////////
1049 // Implementation of FGInput::button.
1050 ////////////////////////////////////////////////////////////////////////
1052 FGInput::button::button ()
1053 : is_repeatable(false),
1060 FGInput::button::~button ()
1062 // FIXME: memory leak
1063 // for (int i = 0; i < KEYMOD_MAX; i++)
1064 // for (int j = 0; i < bindings[i].size(); j++)
1065 // delete bindings[i][j];
1070 ////////////////////////////////////////////////////////////////////////
1071 // Implementation of FGInput::axis.
1072 ////////////////////////////////////////////////////////////////////////
1074 FGInput::axis::axis ()
1075 : last_value(9999999),
1077 low_threshold(-0.9),
1078 high_threshold(0.9),
1084 FGInput::axis::~axis ()
1086 // for (int i = 0; i < KEYMOD_MAX; i++)
1087 // for (int j = 0; i < bindings[i].size(); j++)
1088 // delete bindings[i][j];
1093 ////////////////////////////////////////////////////////////////////////
1094 // Implementation of FGInput::joystick.
1095 ////////////////////////////////////////////////////////////////////////
1097 FGInput::joystick::joystick ()
1107 FGInput::joystick::~joystick ()
1116 ////////////////////////////////////////////////////////////////////////
1117 // Implementation of FGInput::mouse_mode
1118 ////////////////////////////////////////////////////////////////////////
1120 FGInput::mouse_mode::mouse_mode ()
1121 : cursor(MOUSE_CURSOR_POINTER),
1123 pass_through(false),
1128 FGInput::mouse_mode::~mouse_mode ()
1130 // FIXME: memory leak
1131 // for (int i = 0; i < KEYMOD_MAX; i++) {
1133 // for (j = 0; i < x_bindings[i].size(); j++)
1134 // delete bindings[i][j];
1135 // for (j = 0; j < y_bindings[i].size(); j++)
1136 // delete bindings[i][j];
1143 ////////////////////////////////////////////////////////////////////////
1144 // Implementation of FGInput::mouse
1145 ////////////////////////////////////////////////////////////////////////
1147 FGInput::mouse::mouse ()
1158 FGInput::mouse::~mouse ()
1163 ////////////////////////////////////////////////////////////////////////
1164 // Implementation of OS callbacks.
1165 ////////////////////////////////////////////////////////////////////////
1167 void keyHandler(int key, int keymod, int mousex, int mousey)
1169 if((keymod & KEYMOD_RELEASED) == 0)
1170 if(puKeyboard(key, PU_DOWN))
1174 default_input->doKey(key, keymod, mousex, mousey);
1177 void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
1180 default_input->doMouseClick(button, updown, x, y, mainWindow, ea);
1183 void mouseMotionHandler(int x, int y)
1185 if (default_input != 0)
1186 default_input->doMouseMotion(x, y);