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 bool(fgGetKeyModifiers() & KEYMOD_SHIFT);
96 return bool(fgGetKeyModifiers() & KEYMOD_CTRL);
102 return bool(fgGetKeyModifiers() & KEYMOD_ALT);
108 return bool(fgGetKeyModifiers() & KEYMOD_META);
114 return bool(fgGetKeyModifiers() & KEYMOD_SUPER);
120 return bool(fgGetKeyModifiers() & KEYMOD_HYPER);
124 ////////////////////////////////////////////////////////////////////////
125 // Implementation of FGInput.
126 ////////////////////////////////////////////////////////////////////////
129 FGInput::FGInput () :
130 _key_event(fgGetNode("/devices/status/keyboard/event", true))
132 if (default_input == 0)
133 default_input = this;
138 if (default_input == this)
148 fgRegisterKeyHandler(keyHandler);
149 fgRegisterMouseClickHandler(mouseClickHandler);
150 fgRegisterMouseMotionHandler(mouseMotionHandler);
162 _postinit_joystick();
163 _postinit_keyboard();
169 fgTie("/devices/status/keyboard/shift", getModShift);
170 fgTie("/devices/status/keyboard/ctrl", getModCtrl);
171 fgTie("/devices/status/keyboard/alt", getModAlt);
172 fgTie("/devices/status/keyboard/meta", getModMeta);
173 fgTie("/devices/status/keyboard/super", getModSuper);
174 fgTie("/devices/status/keyboard/hyper", getModHyper);
176 _key_event->tie("key", SGRawValuePointer<int>(&_key_code));
177 _key_event->tie("pressed", SGRawValuePointer<bool>(&_key_pressed));
178 _key_event->tie("modifier", SGRawValuePointer<int>(&_key_modifiers));
179 _key_event->tie("modifier/shift", SGRawValuePointer<bool>(&_key_shift));
180 _key_event->tie("modifier/ctrl", SGRawValuePointer<bool>(&_key_ctrl));
181 _key_event->tie("modifier/alt", SGRawValuePointer<bool>(&_key_alt));
182 _key_event->tie("modifier/meta", SGRawValuePointer<bool>(&_key_meta));
183 _key_event->tie("modifier/super", SGRawValuePointer<bool>(&_key_super));
184 _key_event->tie("modifier/hyper", SGRawValuePointer<bool>(&_key_hyper));
190 fgUntie("/devices/status/keyboard/shift");
191 fgUntie("/devices/status/keyboard/ctrl");
192 fgUntie("/devices/status/keyboard/alt");
193 fgUntie("/devices/status/keyboard/meta");
194 fgUntie("/devices/status/keyboard/super");
195 fgUntie("/devices/status/keyboard/hyper");
197 _key_event->untie("key");
198 _key_event->untie("pressed");
199 _key_event->untie("modifier");
200 _key_event->untie("modifier/shift");
201 _key_event->untie("modifier/ctrl");
202 _key_event->untie("modifier/alt");
203 _key_event->untie("modifier/meta");
204 _key_event->untie("modifier/super");
205 _key_event->untie("modifier/hyper");
209 FGInput::update (double dt)
212 _update_joystick(dt);
229 FGInput::is_suspended () const
235 FGInput::makeDefault (bool status)
238 default_input = this;
239 else if (default_input == this)
244 FGInput::doKey (int k, int modifiers, int x, int y)
247 if (k < 0 || k >= MAX_KEYS) {
248 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
253 _key_modifiers = modifiers & ~KEYMOD_RELEASED;
254 _key_pressed = !bool(modifiers & KEYMOD_RELEASED);
255 _key_shift = bool(modifiers & KEYMOD_SHIFT);
256 _key_ctrl = bool(modifiers & KEYMOD_CTRL);
257 _key_alt = bool(modifiers & KEYMOD_ALT);
258 _key_meta = bool(modifiers & KEYMOD_META);
259 _key_super = bool(modifiers & KEYMOD_SUPER);
260 _key_hyper = bool(modifiers & KEYMOD_HYPER);
261 _key_event->fireValueChanged();
266 modifiers = _key_modifiers | (_key_pressed ? KEYMOD_NONE : KEYMOD_RELEASED);
267 button &b = _key_bindings[k];
270 if (!(modifiers & KEYMOD_RELEASED)) {
271 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
272 << " with modifiers " << modifiers );
273 if (!b.last_state || b.is_repeatable) {
274 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
276 for (unsigned int i = 0; i < bindings.size(); i++)
283 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
284 << " with modifiers " << modifiers);
286 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
287 for (unsigned int i = 0; i < bindings.size(); i++)
295 FGInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
297 int modifiers = fgGetKeyModifiers();
299 mouse &m = _mouse_bindings[0];
300 mouse_mode &mode = m.modes[m.current_mode];
302 // Let the property manager know.
303 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
304 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
306 // Pass on to PUI and the panel if
307 // requested, and return if one of
308 // them consumes the event.
310 if (updown != MOUSE_BUTTON_DOWN) {
311 // Execute the mouse up event in any case, may be we should
312 // stop processing here?
313 while (!_activePickCallbacks[b].empty()) {
314 _activePickCallbacks[b].front()->buttonReleased();
315 _activePickCallbacks[b].pop_front();
319 if (mode.pass_through) {
320 // The pu stuff seems to need that. May be it does opengl picking ...
322 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
324 else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
325 globals->get_current_panel()->getVisibility() &&
326 globals->get_current_panel()->doMouseAction(b, updown, x, y))
328 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
331 // pui didn't want the click event so compute a
332 // scenegraph intersection point corresponding to the mouse click
333 if (updown == MOUSE_BUTTON_DOWN) {
335 // Get the list of hit callbacks. Take the first callback that
336 // accepts the mouse button press and ignore the rest of them
337 // That is they get sorted by distance and by scenegraph depth.
338 // The nearest one is the first one and the deepest
339 // (the most specialized one in the scenegraph) is the first.
340 std::vector<SGSceneryPick> pickList;
341 if (FGRenderer::pick(x, y, pickList, ea)) {
342 std::vector<SGSceneryPick>::const_iterator i;
343 for (i = pickList.begin(); i != pickList.end(); ++i) {
344 if (i->callback->buttonPressed(b, i->info)) {
345 _activePickCallbacks[b].push_back(i->callback);
354 // OK, PUI and the panel didn't want the click
355 if (b >= MAX_MOUSE_BUTTONS) {
356 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
357 << " where only " << MAX_MOUSE_BUTTONS << " expected");
361 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
365 FGInput::doMouseMotion (int x, int y)
367 // Don't call fgGetKeyModifiers() here, until we are using a
368 // toolkit that supports getting the mods from outside a key
369 // callback. Glut doesn't.
370 int modifiers = KEYMOD_NONE;
372 int xsize = fgGetInt("/sim/startup/xsize", 800);
373 int ysize = fgGetInt("/sim/startup/ysize", 600);
375 mouse &m = _mouse_bindings[0];
377 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
382 mouse_mode &mode = m.modes[m.current_mode];
384 // Pass on to PUI if requested, and return
385 // if PUI consumed the event.
386 if (mode.pass_through && puMouse(x, y)) {
392 // OK, PUI didn't want the event,
393 // so we can play with it.
396 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
397 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
401 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
402 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
405 // Constrain the mouse if requested
406 if (mode.constrained) {
407 bool need_warp = false;
408 if (x <= (xsize * .25) || x >= (xsize * .75)) {
413 if (y <= (ysize * .25) || y >= (ysize * .75)) {
423 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
426 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
431 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
433 ulDir *dir = ulOpenDir(path->c_str());
436 while ((dent = ulReadDir(dir)) != 0) {
437 if (dent->d_name[0] == '.')
440 SGPath p(path->str());
441 p.append(dent->d_name);
442 _scan_joystick_dir(&p, node, index);
446 } else if (path->extension() == "xml") {
447 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
448 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
449 readProperties(path->str(), n);
450 n->setStringValue("source", path->c_str());
456 FGInput::_init_joystick ()
459 // TODO: zero the old bindings first.
460 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
461 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
463 // read all joystick xml files into /input/joysticks/js_named[1000++]
464 SGPath path(globals->get_fg_root());
465 path.append("Input/Joysticks");
466 int js_named_index = 1000;
467 _scan_joystick_dir(&path, js_nodes, &js_named_index);
469 // build name->node map with each <name> (reverse order)
470 map<string, SGPropertyNode_ptr> jsmap;
471 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
473 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
474 SGPropertyNode *n = js_named[k];
475 vector<SGPropertyNode_ptr> names = n->getChildren("name");
476 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
477 for (unsigned int j = 0; j < names.size(); j++)
478 jsmap[names[j]->getStringValue()] = n;
482 for (int i = 0; i < MAX_JOYSTICKS; i++) {
483 jsJoystick * js = new jsJoystick(i);
484 _joystick_bindings[i].js = js;
486 if (js->notWorking()) {
487 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
491 const char * name = js->getName();
492 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
495 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
498 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
499 SGPropertyNode_ptr named;
501 if ((named = jsmap[name])) {
502 string source = named->getStringValue("source", "user defined");
503 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
505 } else if ((named = jsmap["default"])) {
506 string source = named->getStringValue("source", "user defined");
507 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
508 << "\"\nUsing default: \"" << source << '"');
511 throw sg_throwable(string("No joystick configuration file with "
512 "<name>default</name> entry found!"));
515 js_node = js_nodes->getChild("js", i, true);
516 copyProperties(named, js_node);
517 js_node->setStringValue("id", name);
521 // get rid of unused config nodes
522 js_nodes->removeChildren("js-named", false);
527 FGInput::_postinit_keyboard()
529 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
531 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
532 if (key_nodes == 0) {
533 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
534 key_nodes = fgGetNode("/input/keyboard", true);
537 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
538 vector<SGPropertyNode_ptr> nasal = key_nodes->getChildren("nasal");
539 for (unsigned int j = 0; j < nasal.size(); j++) {
540 nasal[j]->setStringValue("module", _module.c_str());
541 nasalsys->handleCommand(nasal[j]);
544 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
545 for (unsigned int i = 0; i < keys.size(); i++) {
546 int index = keys[i]->getIndex();
547 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
549 _key_bindings[index].bindings->clear();
550 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
551 _key_bindings[index].last_state = 0;
552 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
558 FGInput::_postinit_joystick()
560 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
561 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
563 for (int i = 0; i < MAX_JOYSTICKS; i++) {
564 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
565 jsJoystick *js = _joystick_bindings[i].js;
566 if (!js_node || js->notWorking())
571 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
572 unsigned int nbuttons = jsCaps.wNumButtons;
573 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
575 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
578 int naxes = js->getNumAxes();
579 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
580 _joystick_bindings[i].naxes = naxes;
581 _joystick_bindings[i].nbuttons = nbuttons;
583 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
585 // Set up range arrays
586 float minRange[MAX_JOYSTICK_AXES];
587 float maxRange[MAX_JOYSTICK_AXES];
588 float center[MAX_JOYSTICK_AXES];
590 // Initialize with default values
591 js->getMinRange(minRange);
592 js->getMaxRange(maxRange);
593 js->getCenter(center);
595 // Allocate axes and buttons
596 _joystick_bindings[i].axes = new axis[naxes];
597 _joystick_bindings[i].buttons = new button[nbuttons];
600 // Initialize nasal groups.
605 nasalsys->createModule(_module.c_str(), _module.c_str(), "", 0);
607 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
609 for (j = 0; j < nasal.size(); j++) {
610 nasal[j]->setStringValue("module", _module.c_str());
611 nasalsys->handleCommand(nasal[j]);
615 // Initialize the axes.
617 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
618 size_t nb_axes = axes.size();
619 for (j = 0; j < nb_axes; j++ ) {
620 const SGPropertyNode * axis_node = axes[j];
621 const SGPropertyNode * num_node = axis_node->getChild("number");
622 int n_axis = axis_node->getIndex();
624 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
626 // Silently ignore platforms that are not specified within the
627 // <number></number> section
632 if (n_axis >= naxes) {
633 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
636 axis &a = _joystick_bindings[i].axes[n_axis];
638 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
640 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
641 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
642 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
643 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
645 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
647 // Initialize the virtual axis buttons.
648 _init_button(axis_node->getChild("low"), a.low, "low");
649 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
651 _init_button(axis_node->getChild("high"), a.high, "high");
652 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
653 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
658 // Initialize the buttons.
660 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
662 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
663 const SGPropertyNode * button_node = buttons[j];
664 const SGPropertyNode * num_node = button_node->getChild("number");
665 size_t n_but = button_node->getIndex();
667 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
670 if (n_but >= nbuttons) {
671 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
675 sprintf(buf, "%d", n_but);
676 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
677 _init_button(button_node,
678 _joystick_bindings[i].buttons[n_but],
681 // get interval-sec property
682 button &b = _joystick_bindings[i].buttons[n_but];
683 if (button_node != 0) {
684 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
689 js->setMinRange(minRange);
690 js->setMaxRange(maxRange);
691 js->setCenter(center);
697 // Map of all known cursor names
698 // This used to contain all the Glut cursors, but those are
699 // not defined by other toolkits. It now supports only the cursor
700 // images we actually use, in the interest of portability. Someday,
701 // it would be cool to write an OpenGL cursor renderer, with the
702 // cursors defined as textures referenced in the property tree. This
703 // list could then be eliminated. -Andy
708 } mouse_cursor_map[] = {
709 { "none", MOUSE_CURSOR_NONE },
710 { "inherit", MOUSE_CURSOR_POINTER },
711 { "wait", MOUSE_CURSOR_WAIT },
712 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
713 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
718 FGInput::_init_mouse ()
720 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
723 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
724 if (mouse_nodes == 0) {
725 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
726 mouse_nodes = fgGetNode("/input/mice", true);
730 for (int i = 0; i < MAX_MICE; i++) {
731 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
732 mouse &m = _mouse_bindings[i];
734 // Grab node pointers
736 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
737 m.mode_node = fgGetNode(buf);
738 if (m.mode_node == NULL) {
739 m.mode_node = fgGetNode(buf, true);
740 m.mode_node->setIntValue(0);
742 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
743 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
744 m.mouse_button_nodes[j] = fgGetNode(buf, true);
745 m.mouse_button_nodes[j]->setBoolValue(false);
748 // Read all the modes
749 m.nModes = mouse_node->getIntValue("mode-count", 1);
750 m.modes = new mouse_mode[m.nModes];
752 for (int j = 0; j < m.nModes; j++) {
755 // Read the mouse cursor for this mode
756 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
757 const char * cursor_name =
758 mode_node->getStringValue("cursor", "inherit");
759 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
760 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
761 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
762 m.modes[j].cursor = mouse_cursor_map[k].cursor;
767 // Read other properties for this mode
768 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
769 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
771 // Read the button bindings for this mode
772 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
774 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
775 sprintf(buf, "mouse button %d", k);
776 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
777 _init_button(mode_node->getChild("button", k),
778 m.modes[j].buttons[k],
782 // Read the axis bindings for this mode
783 _read_bindings(mode_node->getChild("x-axis", 0, true),
784 m.modes[j].x_bindings,
786 _read_bindings(mode_node->getChild("y-axis", 0, true),
787 m.modes[j].y_bindings,
795 FGInput::_init_button (const SGPropertyNode * node,
800 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
802 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
804 // Get the bindings for the button
805 _read_bindings(node, b.bindings, KEYMOD_NONE);
811 FGInput::_update_keyboard ()
818 FGInput::_update_joystick (double dt)
820 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
822 // float js_val, diff;
823 float axis_values[MAX_JOYSTICK_AXES];
828 for ( i = 0; i < MAX_JOYSTICKS; i++) {
830 jsJoystick * js = _joystick_bindings[i].js;
831 if (js == 0 || js->notWorking())
834 js->read(&buttons, axis_values);
836 // Fire bindings for the axes.
837 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
838 axis &a = _joystick_bindings[i].axes[j];
840 // Do nothing if the axis position
841 // is unchanged; only a change in
842 // position fires the bindings.
843 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
844 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
845 a.last_value = axis_values[j];
846 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
847 // << a.bindings[modifiers].size() << " bindings");
848 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
849 a.bindings[modifiers][k]->fire(axis_values[j]);
852 // do we have to emulate axis buttons?
854 if(a.last_dt >= a.interval_sec) {
855 if (a.low.bindings[modifiers].size())
856 _update_button(_joystick_bindings[i].axes[j].low,
858 axis_values[j] < a.low_threshold,
861 if (a.high.bindings[modifiers].size())
862 _update_button(_joystick_bindings[i].axes[j].high,
864 axis_values[j] > a.high_threshold,
866 a.last_dt -= a.interval_sec;
870 // Fire bindings for the buttons.
871 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
872 button &b = _joystick_bindings[i].buttons[j];
874 if(b.last_dt >= b.interval_sec) {
875 _update_button(_joystick_bindings[i].buttons[j],
877 (buttons & (1 << j)) > 0,
879 b.last_dt -= b.interval_sec;
886 FGInput::_update_mouse ( double dt )
888 mouse &m = _mouse_bindings[0];
889 int mode = m.mode_node->getIntValue();
890 if (mode != m.current_mode) {
891 m.current_mode = mode;
892 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
893 if (mode >= 0 && mode < m.nModes) {
894 fgSetMouseCursor(m.modes[mode].cursor);
895 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
896 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
897 fgWarpMouse(m.x, m.y);
899 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
900 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
904 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
905 if ( m.x != m.save_x || m.y != m.save_y ) {
906 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
907 fgSetMouseCursor(m.modes[mode].cursor);
910 if ( m.timeout <= 0.0 ) {
911 fgSetMouseCursor(MOUSE_CURSOR_NONE);
919 // handle repeatable mouse press events
920 std::map<int, std::list<SGSharedPtr<SGPickCallback> > >::iterator mi;
921 for (mi = _activePickCallbacks.begin();
922 mi != _activePickCallbacks.end(); ++mi) {
923 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
924 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
931 FGInput::_update_button (button &b, int modifiers, bool pressed,
935 // The press event may be repeated.
936 if (!b.last_state || b.is_repeatable) {
937 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
938 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++) {
939 b.bindings[modifiers][k]->fire(x, y);
943 // The release event is never repeated.
945 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
946 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
947 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
951 b.last_state = pressed;
956 FGInput::_read_bindings (const SGPropertyNode * node,
957 binding_list_t * binding_list,
960 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
961 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
962 for (unsigned int i = 0; i < bindings.size(); i++) {
963 const char *cmd = bindings[i]->getStringValue("command");
964 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
966 if (!strcmp(cmd, "nasal") && !_module.empty())
967 bindings[i]->setStringValue("module", _module.c_str());
968 binding_list[modifiers].push_back(new SGBinding(bindings[i], globals->get_props()));
971 // Read nested bindings for modifiers
972 if (node->getChild("mod-up") != 0)
973 _read_bindings(node->getChild("mod-up"), binding_list,
974 modifiers|KEYMOD_RELEASED);
976 if (node->getChild("mod-shift") != 0)
977 _read_bindings(node->getChild("mod-shift"), binding_list,
978 modifiers|KEYMOD_SHIFT);
980 if (node->getChild("mod-ctrl") != 0)
981 _read_bindings(node->getChild("mod-ctrl"), binding_list,
982 modifiers|KEYMOD_CTRL);
984 if (node->getChild("mod-alt") != 0)
985 _read_bindings(node->getChild("mod-alt"), binding_list,
986 modifiers|KEYMOD_ALT);
988 if (node->getChild("mod-meta") != 0)
989 _read_bindings(node->getChild("mod-meta"), binding_list,
990 modifiers|KEYMOD_META);
992 if (node->getChild("mod-super") != 0)
993 _read_bindings(node->getChild("mod-super"), binding_list,
994 modifiers|KEYMOD_SUPER);
996 if (node->getChild("mod-hyper") != 0)
997 _read_bindings(node->getChild("mod-hyper"), binding_list,
998 modifiers|KEYMOD_HYPER);
1002 const FGInput::binding_list_t&
1003 FGInput::_find_key_bindings (unsigned int k, int modifiers)
1005 unsigned char kc = (unsigned char)k;
1006 button &b = _key_bindings[k];
1008 // Try it straight, first.
1009 if (b.bindings[modifiers].size() > 0)
1010 return b.bindings[modifiers];
1012 // Alt-Gr is CTRL+ALT
1013 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
1014 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
1016 // Try removing the control modifier
1017 // for control keys.
1018 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
1019 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
1021 // Try removing shift modifier
1022 // for upper case or any punctuation
1023 // (since different keyboards will
1024 // shift different punctuation types)
1025 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
1026 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
1028 // Try removing alt modifier for
1029 // high-bit characters.
1030 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
1031 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
1033 // Give up and return the empty vector.
1035 return b.bindings[modifiers];
1040 ////////////////////////////////////////////////////////////////////////
1041 // Implementation of FGInput::button.
1042 ////////////////////////////////////////////////////////////////////////
1044 FGInput::button::button ()
1045 : is_repeatable(false),
1052 FGInput::button::~button ()
1054 // FIXME: memory leak
1055 // for (int i = 0; i < KEYMOD_MAX; i++)
1056 // for (int j = 0; i < bindings[i].size(); j++)
1057 // delete bindings[i][j];
1062 ////////////////////////////////////////////////////////////////////////
1063 // Implementation of FGInput::axis.
1064 ////////////////////////////////////////////////////////////////////////
1066 FGInput::axis::axis ()
1067 : last_value(9999999),
1069 low_threshold(-0.9),
1070 high_threshold(0.9),
1076 FGInput::axis::~axis ()
1078 // for (int i = 0; i < KEYMOD_MAX; i++)
1079 // for (int j = 0; i < bindings[i].size(); j++)
1080 // delete bindings[i][j];
1085 ////////////////////////////////////////////////////////////////////////
1086 // Implementation of FGInput::joystick.
1087 ////////////////////////////////////////////////////////////////////////
1089 FGInput::joystick::joystick ()
1099 FGInput::joystick::~joystick ()
1108 ////////////////////////////////////////////////////////////////////////
1109 // Implementation of FGInput::mouse_mode
1110 ////////////////////////////////////////////////////////////////////////
1112 FGInput::mouse_mode::mouse_mode ()
1113 : cursor(MOUSE_CURSOR_POINTER),
1115 pass_through(false),
1120 FGInput::mouse_mode::~mouse_mode ()
1122 // FIXME: memory leak
1123 // for (int i = 0; i < KEYMOD_MAX; i++) {
1125 // for (j = 0; i < x_bindings[i].size(); j++)
1126 // delete bindings[i][j];
1127 // for (j = 0; j < y_bindings[i].size(); j++)
1128 // delete bindings[i][j];
1135 ////////////////////////////////////////////////////////////////////////
1136 // Implementation of FGInput::mouse
1137 ////////////////////////////////////////////////////////////////////////
1139 FGInput::mouse::mouse ()
1150 FGInput::mouse::~mouse ()
1155 ////////////////////////////////////////////////////////////////////////
1156 // Implementation of OS callbacks.
1157 ////////////////////////////////////////////////////////////////////////
1159 void keyHandler(int key, int keymod, int mousex, int mousey)
1161 if((keymod & KEYMOD_RELEASED) == 0)
1162 if(puKeyboard(key, PU_DOWN))
1166 default_input->doKey(key, keymod, mousex, mousey);
1169 void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
1172 default_input->doMouseClick(button, updown, x, y, mainWindow, ea);
1175 void mouseMotionHandler(int x, int y)
1177 if (default_input != 0)
1178 default_input->doMouseMotion(x, y);