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);
118 ////////////////////////////////////////////////////////////////////////
119 // Implementation of FGInput.
120 ////////////////////////////////////////////////////////////////////////
123 FGInput::FGInput () :
124 _key_event(fgGetNode("/devices/status/keyboard/event", true))
126 if (default_input == 0)
127 default_input = this;
132 if (default_input == this)
142 fgRegisterKeyHandler(keyHandler);
143 fgRegisterMouseClickHandler(mouseClickHandler);
144 fgRegisterMouseMotionHandler(mouseMotionHandler);
156 _postinit_joystick();
157 _postinit_keyboard();
163 fgTie("/devices/status/keyboard/shift", getModShift);
164 fgTie("/devices/status/keyboard/ctrl", getModCtrl);
165 fgTie("/devices/status/keyboard/alt", getModAlt);
166 fgTie("/devices/status/keyboard/meta", getModMeta);
167 fgTie("/devices/status/keyboard/super", getModSuper);
169 _key_event->tie("key", SGRawValuePointer<int>(&_key_code));
170 _key_event->tie("pressed", SGRawValuePointer<bool>(&_key_pressed));
171 _key_event->tie("modifier", SGRawValuePointer<int>(&_key_modifiers));
172 _key_event->tie("modifier/shift", SGRawValuePointer<bool>(&_key_shift));
173 _key_event->tie("modifier/ctrl", SGRawValuePointer<bool>(&_key_ctrl));
174 _key_event->tie("modifier/alt", SGRawValuePointer<bool>(&_key_alt));
175 _key_event->tie("modifier/meta", SGRawValuePointer<bool>(&_key_meta));
176 _key_event->tie("modifier/super", SGRawValuePointer<bool>(&_key_super));
182 fgUntie("/devices/status/keyboard/shift");
183 fgUntie("/devices/status/keyboard/ctrl");
184 fgUntie("/devices/status/keyboard/alt");
185 fgUntie("/devices/status/keyboard/meta");
186 fgUntie("/devices/status/keyboard/super");
188 _key_event->untie("key");
189 _key_event->untie("pressed");
190 _key_event->untie("modifier");
191 _key_event->untie("modifier/shift");
192 _key_event->untie("modifier/ctrl");
193 _key_event->untie("modifier/alt");
194 _key_event->untie("modifier/meta");
195 _key_event->untie("modifier/super");
199 FGInput::update (double dt)
202 _update_joystick(dt);
219 FGInput::is_suspended () const
225 FGInput::makeDefault (bool status)
228 default_input = this;
229 else if (default_input == this)
234 FGInput::doKey (int k, int modifiers, int x, int y)
237 if (k < 0 || k >= MAX_KEYS) {
238 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
243 _key_modifiers = modifiers & ~KEYMOD_RELEASED;
244 _key_pressed = !bool(modifiers & KEYMOD_RELEASED);
245 _key_shift = bool(modifiers & KEYMOD_SHIFT);
246 _key_ctrl = bool(modifiers & KEYMOD_CTRL);
247 _key_alt = bool(modifiers & KEYMOD_ALT);
248 _key_meta = bool(modifiers & KEYMOD_META);
249 _key_super = bool(modifiers & KEYMOD_SUPER);
250 _key_event->fireValueChanged();
255 modifiers = _key_modifiers | (_key_pressed ? KEYMOD_NONE : KEYMOD_RELEASED);
256 button &b = _key_bindings[k];
259 if (!(modifiers & KEYMOD_RELEASED)) {
260 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
261 << " with modifiers " << modifiers );
262 if (!b.last_state || b.is_repeatable) {
263 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
265 for (unsigned int i = 0; i < bindings.size(); i++)
272 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
273 << " with modifiers " << modifiers);
275 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
276 for (unsigned int i = 0; i < bindings.size(); i++)
284 FGInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
286 int modifiers = fgGetKeyModifiers();
288 mouse &m = _mouse_bindings[0];
289 mouse_mode &mode = m.modes[m.current_mode];
291 // Let the property manager know.
292 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
293 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
295 // Pass on to PUI and the panel if
296 // requested, and return if one of
297 // them consumes the event.
299 if (updown != MOUSE_BUTTON_DOWN) {
300 // Execute the mouse up event in any case, may be we should
301 // stop processing here?
302 while (!_activePickCallbacks[b].empty()) {
303 _activePickCallbacks[b].front()->buttonReleased();
304 _activePickCallbacks[b].pop_front();
308 if (mode.pass_through) {
309 // The pu stuff seems to need that. May be it does opengl picking ...
311 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
313 else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
314 globals->get_current_panel()->getVisibility() &&
315 globals->get_current_panel()->doMouseAction(b, updown, x, y))
317 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
320 // pui didn't want the click event so compute a
321 // scenegraph intersection point corresponding to the mouse click
322 if (updown == MOUSE_BUTTON_DOWN) {
324 // Get the list of hit callbacks. Take the first callback that
325 // accepts the mouse button press and ignore the rest of them
326 // That is they get sorted by distance and by scenegraph depth.
327 // The nearest one is the first one and the deepest
328 // (the most specialized one in the scenegraph) is the first.
329 std::vector<SGSceneryPick> pickList;
330 if (FGRenderer::pick(x, y, pickList, ea)) {
331 std::vector<SGSceneryPick>::const_iterator i;
332 for (i = pickList.begin(); i != pickList.end(); ++i) {
333 if (i->callback->buttonPressed(b, i->info)) {
334 _activePickCallbacks[b].push_back(i->callback);
343 // OK, PUI and the panel didn't want the click
344 if (b >= MAX_MOUSE_BUTTONS) {
345 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
346 << " where only " << MAX_MOUSE_BUTTONS << " expected");
350 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
354 FGInput::doMouseMotion (int x, int y)
356 // Don't call fgGetKeyModifiers() here, until we are using a
357 // toolkit that supports getting the mods from outside a key
358 // callback. Glut doesn't.
359 int modifiers = KEYMOD_NONE;
361 int xsize = fgGetInt("/sim/startup/xsize", 800);
362 int ysize = fgGetInt("/sim/startup/ysize", 600);
364 mouse &m = _mouse_bindings[0];
366 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
371 mouse_mode &mode = m.modes[m.current_mode];
373 // Pass on to PUI if requested, and return
374 // if PUI consumed the event.
375 if (mode.pass_through && puMouse(x, y)) {
381 // OK, PUI didn't want the event,
382 // so we can play with it.
385 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
386 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
390 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
391 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
394 // Constrain the mouse if requested
395 if (mode.constrained) {
396 bool need_warp = false;
397 if (x <= (xsize * .25) || x >= (xsize * .75)) {
402 if (y <= (ysize * .25) || y >= (ysize * .75)) {
412 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
415 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
420 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
422 ulDir *dir = ulOpenDir(path->c_str());
425 while ((dent = ulReadDir(dir)) != 0) {
426 if (dent->d_name[0] == '.')
429 SGPath p(path->str());
430 p.append(dent->d_name);
431 _scan_joystick_dir(&p, node, index);
435 } else if (path->extension() == "xml") {
436 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
437 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
438 readProperties(path->str(), n);
439 n->setStringValue("source", path->c_str());
445 FGInput::_init_joystick ()
448 // TODO: zero the old bindings first.
449 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
450 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
452 // read all joystick xml files into /input/joysticks/js_named[1000++]
453 SGPath path(globals->get_fg_root());
454 path.append("Input/Joysticks");
455 int js_named_index = 1000;
456 _scan_joystick_dir(&path, js_nodes, &js_named_index);
458 // build name->node map with each <name> (reverse order)
459 map<string, SGPropertyNode_ptr> jsmap;
460 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
462 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
463 SGPropertyNode *n = js_named[k];
464 vector<SGPropertyNode_ptr> names = n->getChildren("name");
465 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
466 for (unsigned int j = 0; j < names.size(); j++)
467 jsmap[names[j]->getStringValue()] = n;
471 for (int i = 0; i < MAX_JOYSTICKS; i++) {
472 jsJoystick * js = new jsJoystick(i);
473 _joystick_bindings[i].js = js;
475 if (js->notWorking()) {
476 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
480 const char * name = js->getName();
481 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
484 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
487 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
488 SGPropertyNode_ptr named;
490 if ((named = jsmap[name])) {
491 string source = named->getStringValue("source", "user defined");
492 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
494 } else if ((named = jsmap["default"])) {
495 string source = named->getStringValue("source", "user defined");
496 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
497 << "\"\nUsing default: \"" << source << '"');
500 throw sg_throwable(string("No joystick configuration file with "
501 "<name>default</name> entry found!"));
504 js_node = js_nodes->getChild("js", i, true);
505 copyProperties(named, js_node);
506 js_node->setStringValue("id", name);
510 // get rid of unused config nodes
511 js_nodes->removeChildren("js-named", false);
516 FGInput::_postinit_keyboard()
518 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
520 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
521 if (key_nodes == 0) {
522 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
523 key_nodes = fgGetNode("/input/keyboard", true);
526 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
527 vector<SGPropertyNode_ptr> nasal = key_nodes->getChildren("nasal");
528 for (unsigned int j = 0; j < nasal.size(); j++) {
529 nasal[j]->setStringValue("module", _module.c_str());
530 nasalsys->handleCommand(nasal[j]);
533 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
534 for (unsigned int i = 0; i < keys.size(); i++) {
535 int index = keys[i]->getIndex();
536 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
538 _key_bindings[index].bindings->clear();
539 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
540 _key_bindings[index].last_state = 0;
541 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
547 FGInput::_postinit_joystick()
549 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
550 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
552 for (int i = 0; i < MAX_JOYSTICKS; i++) {
553 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
554 jsJoystick *js = _joystick_bindings[i].js;
555 if (!js_node || js->notWorking())
560 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
561 unsigned int nbuttons = jsCaps.wNumButtons;
562 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
564 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
567 int naxes = js->getNumAxes();
568 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
569 _joystick_bindings[i].naxes = naxes;
570 _joystick_bindings[i].nbuttons = nbuttons;
572 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
574 // Set up range arrays
575 float minRange[MAX_JOYSTICK_AXES];
576 float maxRange[MAX_JOYSTICK_AXES];
577 float center[MAX_JOYSTICK_AXES];
579 // Initialize with default values
580 js->getMinRange(minRange);
581 js->getMaxRange(maxRange);
582 js->getCenter(center);
584 // Allocate axes and buttons
585 _joystick_bindings[i].axes = new axis[naxes];
586 _joystick_bindings[i].buttons = new button[nbuttons];
589 // Initialize nasal groups.
594 nasalsys->createModule(_module.c_str(), _module.c_str(), "", 0);
596 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
598 for (j = 0; j < nasal.size(); j++) {
599 nasal[j]->setStringValue("module", _module.c_str());
600 nasalsys->handleCommand(nasal[j]);
604 // Initialize the axes.
606 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
607 size_t nb_axes = axes.size();
608 for (j = 0; j < nb_axes; j++ ) {
609 const SGPropertyNode * axis_node = axes[j];
610 const SGPropertyNode * num_node = axis_node->getChild("number");
611 int n_axis = axis_node->getIndex();
613 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
615 // Silently ignore platforms that are not specified within the
616 // <number></number> section
621 if (n_axis >= naxes) {
622 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
625 axis &a = _joystick_bindings[i].axes[n_axis];
627 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
629 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
630 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
631 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
632 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
634 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
636 // Initialize the virtual axis buttons.
637 _init_button(axis_node->getChild("low"), a.low, "low");
638 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
640 _init_button(axis_node->getChild("high"), a.high, "high");
641 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
642 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
647 // Initialize the buttons.
649 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
651 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
652 const SGPropertyNode * button_node = buttons[j];
653 const SGPropertyNode * num_node = button_node->getChild("number");
654 size_t n_but = button_node->getIndex();
656 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
659 if (n_but >= nbuttons) {
660 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
664 sprintf(buf, "%d", n_but);
665 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
666 _init_button(button_node,
667 _joystick_bindings[i].buttons[n_but],
670 // get interval-sec property
671 button &b = _joystick_bindings[i].buttons[n_but];
672 if (button_node != 0) {
673 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
678 js->setMinRange(minRange);
679 js->setMaxRange(maxRange);
680 js->setCenter(center);
686 // Map of all known cursor names
687 // This used to contain all the Glut cursors, but those are
688 // not defined by other toolkits. It now supports only the cursor
689 // images we actually use, in the interest of portability. Someday,
690 // it would be cool to write an OpenGL cursor renderer, with the
691 // cursors defined as textures referenced in the property tree. This
692 // list could then be eliminated. -Andy
697 } mouse_cursor_map[] = {
698 { "none", MOUSE_CURSOR_NONE },
699 { "inherit", MOUSE_CURSOR_POINTER },
700 { "wait", MOUSE_CURSOR_WAIT },
701 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
702 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
707 FGInput::_init_mouse ()
709 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
712 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
713 if (mouse_nodes == 0) {
714 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
715 mouse_nodes = fgGetNode("/input/mice", true);
719 for (int i = 0; i < MAX_MICE; i++) {
720 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
721 mouse &m = _mouse_bindings[i];
723 // Grab node pointers
725 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
726 m.mode_node = fgGetNode(buf);
727 if (m.mode_node == NULL) {
728 m.mode_node = fgGetNode(buf, true);
729 m.mode_node->setIntValue(0);
731 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
732 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
733 m.mouse_button_nodes[j] = fgGetNode(buf, true);
734 m.mouse_button_nodes[j]->setBoolValue(false);
737 // Read all the modes
738 m.nModes = mouse_node->getIntValue("mode-count", 1);
739 m.modes = new mouse_mode[m.nModes];
741 for (int j = 0; j < m.nModes; j++) {
744 // Read the mouse cursor for this mode
745 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
746 const char * cursor_name =
747 mode_node->getStringValue("cursor", "inherit");
748 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
749 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
750 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
751 m.modes[j].cursor = mouse_cursor_map[k].cursor;
756 // Read other properties for this mode
757 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
758 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
760 // Read the button bindings for this mode
761 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
763 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
764 sprintf(buf, "mouse button %d", k);
765 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
766 _init_button(mode_node->getChild("button", k),
767 m.modes[j].buttons[k],
771 // Read the axis bindings for this mode
772 _read_bindings(mode_node->getChild("x-axis", 0, true),
773 m.modes[j].x_bindings,
775 _read_bindings(mode_node->getChild("y-axis", 0, true),
776 m.modes[j].y_bindings,
784 FGInput::_init_button (const SGPropertyNode * node,
789 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
791 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
793 // Get the bindings for the button
794 _read_bindings(node, b.bindings, KEYMOD_NONE);
800 FGInput::_update_keyboard ()
807 FGInput::_update_joystick (double dt)
809 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
811 // float js_val, diff;
812 float axis_values[MAX_JOYSTICK_AXES];
817 for ( i = 0; i < MAX_JOYSTICKS; i++) {
819 jsJoystick * js = _joystick_bindings[i].js;
820 if (js == 0 || js->notWorking())
823 js->read(&buttons, axis_values);
825 // Fire bindings for the axes.
826 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
827 axis &a = _joystick_bindings[i].axes[j];
829 // Do nothing if the axis position
830 // is unchanged; only a change in
831 // position fires the bindings.
832 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
833 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
834 a.last_value = axis_values[j];
835 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
836 // << a.bindings[modifiers].size() << " bindings");
837 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
838 a.bindings[modifiers][k]->fire(axis_values[j]);
841 // do we have to emulate axis buttons?
843 if(a.last_dt >= a.interval_sec) {
844 if (a.low.bindings[modifiers].size())
845 _update_button(_joystick_bindings[i].axes[j].low,
847 axis_values[j] < a.low_threshold,
850 if (a.high.bindings[modifiers].size())
851 _update_button(_joystick_bindings[i].axes[j].high,
853 axis_values[j] > a.high_threshold,
855 a.last_dt -= a.interval_sec;
859 // Fire bindings for the buttons.
860 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
861 button &b = _joystick_bindings[i].buttons[j];
863 if(b.last_dt >= b.interval_sec) {
864 _update_button(_joystick_bindings[i].buttons[j],
866 (buttons & (1 << j)) > 0,
868 b.last_dt -= b.interval_sec;
875 FGInput::_update_mouse ( double dt )
877 mouse &m = _mouse_bindings[0];
878 int mode = m.mode_node->getIntValue();
879 if (mode != m.current_mode) {
880 m.current_mode = mode;
881 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
882 if (mode >= 0 && mode < m.nModes) {
883 fgSetMouseCursor(m.modes[mode].cursor);
884 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
885 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
886 fgWarpMouse(m.x, m.y);
888 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
889 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
893 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
894 if ( m.x != m.save_x || m.y != m.save_y ) {
895 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
896 fgSetMouseCursor(m.modes[mode].cursor);
899 if ( m.timeout <= 0.0 ) {
900 fgSetMouseCursor(MOUSE_CURSOR_NONE);
908 // handle repeatable mouse press events
909 std::map<int, std::list<SGSharedPtr<SGPickCallback> > >::iterator mi;
910 for (mi = _activePickCallbacks.begin();
911 mi != _activePickCallbacks.end(); ++mi) {
912 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
913 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
920 FGInput::_update_button (button &b, int modifiers, bool pressed,
924 // The press event may be repeated.
925 if (!b.last_state || b.is_repeatable) {
926 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
927 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++) {
928 b.bindings[modifiers][k]->fire(x, y);
932 // The release event is never repeated.
934 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
935 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
936 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
940 b.last_state = pressed;
945 FGInput::_read_bindings (const SGPropertyNode * node,
946 binding_list_t * binding_list,
949 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
950 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
951 for (unsigned int i = 0; i < bindings.size(); i++) {
952 const char *cmd = bindings[i]->getStringValue("command");
953 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
955 if (!strcmp(cmd, "nasal") && !_module.empty())
956 bindings[i]->setStringValue("module", _module.c_str());
957 binding_list[modifiers].push_back(new SGBinding(bindings[i], globals->get_props()));
960 // Read nested bindings for modifiers
961 if (node->getChild("mod-up") != 0)
962 _read_bindings(node->getChild("mod-up"), binding_list,
963 modifiers|KEYMOD_RELEASED);
965 if (node->getChild("mod-shift") != 0)
966 _read_bindings(node->getChild("mod-shift"), binding_list,
967 modifiers|KEYMOD_SHIFT);
969 if (node->getChild("mod-ctrl") != 0)
970 _read_bindings(node->getChild("mod-ctrl"), binding_list,
971 modifiers|KEYMOD_CTRL);
973 if (node->getChild("mod-alt") != 0)
974 _read_bindings(node->getChild("mod-alt"), binding_list,
975 modifiers|KEYMOD_ALT);
977 if (node->getChild("mod-meta") != 0)
978 _read_bindings(node->getChild("mod-meta"), binding_list,
979 modifiers|KEYMOD_META);
981 if (node->getChild("mod-super") != 0)
982 _read_bindings(node->getChild("mod-super"), binding_list,
983 modifiers|KEYMOD_SUPER);
987 const FGInput::binding_list_t&
988 FGInput::_find_key_bindings (unsigned int k, int modifiers)
990 unsigned char kc = (unsigned char)k;
991 button &b = _key_bindings[k];
993 // Try it straight, first.
994 if (b.bindings[modifiers].size() > 0)
995 return b.bindings[modifiers];
997 // Alt-Gr is CTRL+ALT
998 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
999 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
1001 // Try removing the control modifier
1002 // for control keys.
1003 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
1004 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
1006 // Try removing shift modifier
1007 // for upper case or any punctuation
1008 // (since different keyboards will
1009 // shift different punctuation types)
1010 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
1011 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
1013 // Try removing alt modifier for
1014 // high-bit characters.
1015 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
1016 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
1018 // Give up and return the empty vector.
1020 return b.bindings[modifiers];
1025 ////////////////////////////////////////////////////////////////////////
1026 // Implementation of FGInput::button.
1027 ////////////////////////////////////////////////////////////////////////
1029 FGInput::button::button ()
1030 : is_repeatable(false),
1037 FGInput::button::~button ()
1039 // FIXME: memory leak
1040 // for (int i = 0; i < KEYMOD_MAX; i++)
1041 // for (int j = 0; i < bindings[i].size(); j++)
1042 // delete bindings[i][j];
1047 ////////////////////////////////////////////////////////////////////////
1048 // Implementation of FGInput::axis.
1049 ////////////////////////////////////////////////////////////////////////
1051 FGInput::axis::axis ()
1052 : last_value(9999999),
1054 low_threshold(-0.9),
1055 high_threshold(0.9),
1061 FGInput::axis::~axis ()
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::joystick.
1072 ////////////////////////////////////////////////////////////////////////
1074 FGInput::joystick::joystick ()
1084 FGInput::joystick::~joystick ()
1093 ////////////////////////////////////////////////////////////////////////
1094 // Implementation of FGInput::mouse_mode
1095 ////////////////////////////////////////////////////////////////////////
1097 FGInput::mouse_mode::mouse_mode ()
1098 : cursor(MOUSE_CURSOR_POINTER),
1100 pass_through(false),
1105 FGInput::mouse_mode::~mouse_mode ()
1107 // FIXME: memory leak
1108 // for (int i = 0; i < KEYMOD_MAX; i++) {
1110 // for (j = 0; i < x_bindings[i].size(); j++)
1111 // delete bindings[i][j];
1112 // for (j = 0; j < y_bindings[i].size(); j++)
1113 // delete bindings[i][j];
1120 ////////////////////////////////////////////////////////////////////////
1121 // Implementation of FGInput::mouse
1122 ////////////////////////////////////////////////////////////////////////
1124 FGInput::mouse::mouse ()
1135 FGInput::mouse::~mouse ()
1140 ////////////////////////////////////////////////////////////////////////
1141 // Implementation of OS callbacks.
1142 ////////////////////////////////////////////////////////////////////////
1144 void keyHandler(int key, int keymod, int mousex, int mousey)
1146 if((keymod & KEYMOD_RELEASED) == 0)
1147 if(puKeyboard(key, PU_DOWN))
1151 default_input->doKey(key, keymod, mousex, mousey);
1154 void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
1157 default_input->doMouseClick(button, updown, x, y, mainWindow, ea);
1160 void mouseMotionHandler(int x, int y)
1162 if (default_input != 0)
1163 default_input->doMouseMotion(x, y);