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);
106 ////////////////////////////////////////////////////////////////////////
107 // Implementation of FGInput.
108 ////////////////////////////////////////////////////////////////////////
113 if (default_input == 0)
114 default_input = this;
119 if (default_input == this)
129 fgRegisterKeyHandler(keyHandler);
130 fgRegisterMouseClickHandler(mouseClickHandler);
131 fgRegisterMouseMotionHandler(mouseMotionHandler);
143 _postinit_joystick();
144 _postinit_keyboard();
150 fgTie("/devices/status/keyboard/shift", getModShift);
151 fgTie("/devices/status/keyboard/ctrl", getModCtrl);
152 fgTie("/devices/status/keyboard/alt", getModAlt);
158 fgUntie("/devices/status/keyboard/shift");
159 fgUntie("/devices/status/keyboard/ctrl");
160 fgUntie("/devices/status/keyboard/alt");
164 FGInput::update (double dt)
167 _update_joystick(dt);
184 FGInput::is_suspended () const
190 FGInput::makeDefault (bool status)
193 default_input = this;
194 else if (default_input == this)
199 FGInput::doKey (int k, int modifiers, int x, int y)
202 if (k < 0 || k >= MAX_KEYS) {
203 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
207 button &b = _key_bindings[k];
210 if (!(modifiers & KEYMOD_RELEASED)) {
211 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
212 << " with modifiers " << modifiers );
213 if (!b.last_state || b.is_repeatable) {
214 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
216 for (unsigned int i = 0; i < bindings.size(); i++)
223 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
224 << " with modifiers " << modifiers);
226 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
227 for (unsigned int i = 0; i < bindings.size(); i++)
231 if (k >= 1 && k <= 26) {
232 if (_key_bindings[k + '@'].last_state)
233 doKey(k + '@', KEYMOD_RELEASED, x, y);
234 if (_key_bindings[k + '`'].last_state)
235 doKey(k + '`', KEYMOD_RELEASED, x, y);
236 } else if (k >= 'A' && k <= 'Z') {
237 if (_key_bindings[k - '@'].last_state)
238 doKey(k - '@', KEYMOD_RELEASED, x, y);
239 if (_key_bindings[tolower(k)].last_state)
240 doKey(tolower(k), KEYMOD_RELEASED, x, y);
241 } else if (k >= 'a' && k <= 'z') {
242 if (_key_bindings[k - '`'].last_state)
243 doKey(k - '`', KEYMOD_RELEASED, x, y);
244 if (_key_bindings[toupper(k)].last_state)
245 doKey(toupper(k), KEYMOD_RELEASED, x, y);
252 FGInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
254 int modifiers = fgGetKeyModifiers();
256 mouse &m = _mouse_bindings[0];
257 mouse_mode &mode = m.modes[m.current_mode];
259 // Let the property manager know.
260 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
261 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
263 // Pass on to PUI and the panel if
264 // requested, and return if one of
265 // them consumes the event.
267 if (updown != MOUSE_BUTTON_DOWN) {
268 // Execute the mouse up event in any case, may be we should
269 // stop processing here?
270 while (!_activePickCallbacks[b].empty()) {
271 _activePickCallbacks[b].front()->buttonReleased();
272 _activePickCallbacks[b].pop_front();
276 if (mode.pass_through) {
277 // The pu stuff seems to need that. May be it does opengl picking ...
279 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
281 else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
282 globals->get_current_panel()->getVisibility() &&
283 globals->get_current_panel()->doMouseAction(b, updown, x, y))
285 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
288 // pui didn't want the click event so compute a
289 // scenegraph intersection point corresponding to the mouse click
290 if (updown == MOUSE_BUTTON_DOWN) {
292 // Get the list of hit callbacks. Take the first callback that
293 // accepts the mouse button press and ignore the rest of them
294 // That is they get sorted by distance and by scenegraph depth.
295 // The nearest one is the first one and the deepest
296 // (the most specialized one in the scenegraph) is the first.
297 std::vector<SGSceneryPick> pickList;
298 if (FGRenderer::pick(x, y, pickList, ea)) {
299 std::vector<SGSceneryPick>::const_iterator i;
300 for (i = pickList.begin(); i != pickList.end(); ++i) {
301 if (i->callback->buttonPressed(b, i->info)) {
302 _activePickCallbacks[b].push_back(i->callback);
311 // OK, PUI and the panel didn't want the click
312 if (b >= MAX_MOUSE_BUTTONS) {
313 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
314 << " where only " << MAX_MOUSE_BUTTONS << " expected");
318 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
322 FGInput::doMouseMotion (int x, int y)
324 // Don't call fgGetKeyModifiers() here, until we are using a
325 // toolkit that supports getting the mods from outside a key
326 // callback. Glut doesn't.
327 int modifiers = KEYMOD_NONE;
329 int xsize = fgGetInt("/sim/startup/xsize", 800);
330 int ysize = fgGetInt("/sim/startup/ysize", 600);
332 mouse &m = _mouse_bindings[0];
334 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
339 mouse_mode &mode = m.modes[m.current_mode];
341 // Pass on to PUI if requested, and return
342 // if PUI consumed the event.
343 if (mode.pass_through && puMouse(x, y)) {
349 // OK, PUI didn't want the event,
350 // so we can play with it.
353 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
354 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
358 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
359 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
362 // Constrain the mouse if requested
363 if (mode.constrained) {
364 bool need_warp = false;
365 if (x <= (xsize * .25) || x >= (xsize * .75)) {
370 if (y <= (ysize * .25) || y >= (ysize * .75)) {
380 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
383 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
388 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
390 ulDir *dir = ulOpenDir(path->c_str());
393 while ((dent = ulReadDir(dir)) != 0) {
394 if (dent->d_name[0] == '.')
397 SGPath p(path->str());
398 p.append(dent->d_name);
399 _scan_joystick_dir(&p, node, index);
403 } else if (path->extension() == "xml") {
404 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
405 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
406 readProperties(path->str(), n);
407 n->setStringValue("source", path->c_str());
413 FGInput::_init_joystick ()
416 // TODO: zero the old bindings first.
417 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
418 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
420 // read all joystick xml files into /input/joysticks/js_named[1000++]
421 SGPath path(globals->get_fg_root());
422 path.append("Input/Joysticks");
423 int js_named_index = 1000;
424 _scan_joystick_dir(&path, js_nodes, &js_named_index);
426 // build name->node map with each <name> (reverse order)
427 map<string, SGPropertyNode_ptr> jsmap;
428 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
430 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
431 SGPropertyNode *n = js_named[k];
432 vector<SGPropertyNode_ptr> names = n->getChildren("name");
433 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
434 for (unsigned int j = 0; j < names.size(); j++)
435 jsmap[names[j]->getStringValue()] = n;
439 for (int i = 0; i < MAX_JOYSTICKS; i++) {
440 jsJoystick * js = new jsJoystick(i);
441 _joystick_bindings[i].js = js;
443 if (js->notWorking()) {
444 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
448 const char * name = js->getName();
449 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
452 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
455 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
456 SGPropertyNode_ptr named;
458 if ((named = jsmap[name])) {
459 string source = named->getStringValue("source", "user defined");
460 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
462 } else if ((named = jsmap["default"])) {
463 string source = named->getStringValue("source", "user defined");
464 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
465 << "\"\nUsing default: \"" << source << '"');
468 throw sg_throwable(string("No joystick configuration file with "
469 "<name>default</name> entry found!"));
472 js_node = js_nodes->getChild("js", i, true);
473 copyProperties(named, js_node);
474 js_node->setStringValue("id", name);
478 // get rid of unused config nodes
479 js_nodes->removeChildren("js-named", false);
484 FGInput::_postinit_keyboard()
486 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
488 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
489 if (key_nodes == 0) {
490 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
491 key_nodes = fgGetNode("/input/keyboard", true);
494 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
495 vector<SGPropertyNode_ptr> nasal = key_nodes->getChildren("nasal");
496 for (unsigned int j = 0; j < nasal.size(); j++) {
497 nasal[j]->setStringValue("module", _module.c_str());
498 nasalsys->handleCommand(nasal[j]);
501 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
502 for (unsigned int i = 0; i < keys.size(); i++) {
503 int index = keys[i]->getIndex();
504 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
506 _key_bindings[index].bindings->clear();
507 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
508 _key_bindings[index].last_state = 0;
509 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
515 FGInput::_postinit_joystick()
517 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
518 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
520 for (int i = 0; i < MAX_JOYSTICKS; i++) {
521 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
522 jsJoystick *js = _joystick_bindings[i].js;
523 if (!js_node || js->notWorking())
528 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
529 unsigned int nbuttons = jsCaps.wNumButtons;
530 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
532 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
535 int naxes = js->getNumAxes();
536 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
537 _joystick_bindings[i].naxes = naxes;
538 _joystick_bindings[i].nbuttons = nbuttons;
540 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
542 // Set up range arrays
543 float minRange[MAX_JOYSTICK_AXES];
544 float maxRange[MAX_JOYSTICK_AXES];
545 float center[MAX_JOYSTICK_AXES];
547 // Initialize with default values
548 js->getMinRange(minRange);
549 js->getMaxRange(maxRange);
550 js->getCenter(center);
552 // Allocate axes and buttons
553 _joystick_bindings[i].axes = new axis[naxes];
554 _joystick_bindings[i].buttons = new button[nbuttons];
557 // Initialize nasal groups.
562 nasalsys->createModule(_module.c_str(), _module.c_str(), "", 0);
564 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
566 for (j = 0; j < nasal.size(); j++) {
567 nasal[j]->setStringValue("module", _module.c_str());
568 nasalsys->handleCommand(nasal[j]);
572 // Initialize the axes.
574 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
575 size_t nb_axes = axes.size();
576 for (j = 0; j < nb_axes; j++ ) {
577 const SGPropertyNode * axis_node = axes[j];
578 const SGPropertyNode * num_node = axis_node->getChild("number");
579 int n_axis = axis_node->getIndex();
581 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
583 // Silently ignore platforms that are not specified within the
584 // <number></number> section
589 if (n_axis >= naxes) {
590 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
593 axis &a = _joystick_bindings[i].axes[n_axis];
595 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
597 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
598 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
599 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
600 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
602 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
604 // Initialize the virtual axis buttons.
605 _init_button(axis_node->getChild("low"), a.low, "low");
606 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
608 _init_button(axis_node->getChild("high"), a.high, "high");
609 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
610 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
615 // Initialize the buttons.
617 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
619 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
620 const SGPropertyNode * button_node = buttons[j];
621 const SGPropertyNode * num_node = button_node->getChild("number");
622 size_t n_but = button_node->getIndex();
624 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
627 if (n_but >= nbuttons) {
628 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
632 sprintf(buf, "%d", n_but);
633 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
634 _init_button(button_node,
635 _joystick_bindings[i].buttons[n_but],
638 // get interval-sec property
639 button &b = _joystick_bindings[i].buttons[n_but];
640 if (button_node != 0) {
641 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
646 js->setMinRange(minRange);
647 js->setMaxRange(maxRange);
648 js->setCenter(center);
654 // Map of all known cursor names
655 // This used to contain all the Glut cursors, but those are
656 // not defined by other toolkits. It now supports only the cursor
657 // images we actually use, in the interest of portability. Someday,
658 // it would be cool to write an OpenGL cursor renderer, with the
659 // cursors defined as textures referenced in the property tree. This
660 // list could then be eliminated. -Andy
665 } mouse_cursor_map[] = {
666 { "none", MOUSE_CURSOR_NONE },
667 { "inherit", MOUSE_CURSOR_POINTER },
668 { "wait", MOUSE_CURSOR_WAIT },
669 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
670 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
675 FGInput::_init_mouse ()
677 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
680 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
681 if (mouse_nodes == 0) {
682 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
683 mouse_nodes = fgGetNode("/input/mice", true);
687 for (int i = 0; i < MAX_MICE; i++) {
688 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
689 mouse &m = _mouse_bindings[i];
691 // Grab node pointers
693 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
694 m.mode_node = fgGetNode(buf);
695 if (m.mode_node == NULL) {
696 m.mode_node = fgGetNode(buf, true);
697 m.mode_node->setIntValue(0);
699 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
700 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
701 m.mouse_button_nodes[j] = fgGetNode(buf, true);
702 m.mouse_button_nodes[j]->setBoolValue(false);
705 // Read all the modes
706 m.nModes = mouse_node->getIntValue("mode-count", 1);
707 m.modes = new mouse_mode[m.nModes];
709 for (int j = 0; j < m.nModes; j++) {
712 // Read the mouse cursor for this mode
713 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
714 const char * cursor_name =
715 mode_node->getStringValue("cursor", "inherit");
716 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
717 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
718 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
719 m.modes[j].cursor = mouse_cursor_map[k].cursor;
724 // Read other properties for this mode
725 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
726 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
728 // Read the button bindings for this mode
729 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
731 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
732 sprintf(buf, "mouse button %d", k);
733 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
734 _init_button(mode_node->getChild("button", k),
735 m.modes[j].buttons[k],
739 // Read the axis bindings for this mode
740 _read_bindings(mode_node->getChild("x-axis", 0, true),
741 m.modes[j].x_bindings,
743 _read_bindings(mode_node->getChild("y-axis", 0, true),
744 m.modes[j].y_bindings,
752 FGInput::_init_button (const SGPropertyNode * node,
757 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
759 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
761 // Get the bindings for the button
762 _read_bindings(node, b.bindings, KEYMOD_NONE);
768 FGInput::_update_keyboard ()
775 FGInput::_update_joystick (double dt)
777 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
779 // float js_val, diff;
780 float axis_values[MAX_JOYSTICK_AXES];
785 for ( i = 0; i < MAX_JOYSTICKS; i++) {
787 jsJoystick * js = _joystick_bindings[i].js;
788 if (js == 0 || js->notWorking())
791 js->read(&buttons, axis_values);
793 // Fire bindings for the axes.
794 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
795 axis &a = _joystick_bindings[i].axes[j];
797 // Do nothing if the axis position
798 // is unchanged; only a change in
799 // position fires the bindings.
800 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
801 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
802 a.last_value = axis_values[j];
803 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
804 // << a.bindings[modifiers].size() << " bindings");
805 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
806 a.bindings[modifiers][k]->fire(axis_values[j]);
809 // do we have to emulate axis buttons?
811 if(a.last_dt >= a.interval_sec) {
812 if (a.low.bindings[modifiers].size())
813 _update_button(_joystick_bindings[i].axes[j].low,
815 axis_values[j] < a.low_threshold,
818 if (a.high.bindings[modifiers].size())
819 _update_button(_joystick_bindings[i].axes[j].high,
821 axis_values[j] > a.high_threshold,
823 a.last_dt -= a.interval_sec;
827 // Fire bindings for the buttons.
828 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
829 button &b = _joystick_bindings[i].buttons[j];
831 if(b.last_dt >= b.interval_sec) {
832 _update_button(_joystick_bindings[i].buttons[j],
834 (buttons & (1 << j)) > 0,
836 b.last_dt -= b.interval_sec;
843 FGInput::_update_mouse ( double dt )
845 mouse &m = _mouse_bindings[0];
846 int mode = m.mode_node->getIntValue();
847 if (mode != m.current_mode) {
848 m.current_mode = mode;
849 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
850 if (mode >= 0 && mode < m.nModes) {
851 fgSetMouseCursor(m.modes[mode].cursor);
852 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
853 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
854 fgWarpMouse(m.x, m.y);
856 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
857 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
861 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
862 if ( m.x != m.save_x || m.y != m.save_y ) {
863 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
864 fgSetMouseCursor(m.modes[mode].cursor);
867 if ( m.timeout <= 0.0 ) {
868 fgSetMouseCursor(MOUSE_CURSOR_NONE);
876 // handle repeatable mouse press events
877 std::map<int, std::list<SGSharedPtr<SGPickCallback> > >::iterator mi;
878 for (mi = _activePickCallbacks.begin();
879 mi != _activePickCallbacks.end(); ++mi) {
880 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
881 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
888 FGInput::_update_button (button &b, int modifiers, bool pressed,
892 // The press event may be repeated.
893 if (!b.last_state || b.is_repeatable) {
894 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
895 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++) {
896 b.bindings[modifiers][k]->fire(x, y);
900 // The release event is never repeated.
902 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
903 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
904 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
908 b.last_state = pressed;
913 FGInput::_read_bindings (const SGPropertyNode * node,
914 binding_list_t * binding_list,
917 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
918 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
919 for (unsigned int i = 0; i < bindings.size(); i++) {
920 const char *cmd = bindings[i]->getStringValue("command");
921 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
923 if (!strcmp(cmd, "nasal") && !_module.empty())
924 bindings[i]->setStringValue("module", _module.c_str());
925 binding_list[modifiers].push_back(new SGBinding(bindings[i], globals->get_props()));
928 // Read nested bindings for modifiers
929 if (node->getChild("mod-up") != 0)
930 _read_bindings(node->getChild("mod-up"), binding_list,
931 modifiers|KEYMOD_RELEASED);
933 if (node->getChild("mod-shift") != 0)
934 _read_bindings(node->getChild("mod-shift"), binding_list,
935 modifiers|KEYMOD_SHIFT);
937 if (node->getChild("mod-ctrl") != 0)
938 _read_bindings(node->getChild("mod-ctrl"), binding_list,
939 modifiers|KEYMOD_CTRL);
941 if (node->getChild("mod-alt") != 0)
942 _read_bindings(node->getChild("mod-alt"), binding_list,
943 modifiers|KEYMOD_ALT);
947 const FGInput::binding_list_t&
948 FGInput::_find_key_bindings (unsigned int k, int modifiers)
950 unsigned char kc = (unsigned char)k;
951 button &b = _key_bindings[k];
953 // Try it straight, first.
954 if (b.bindings[modifiers].size() > 0)
955 return b.bindings[modifiers];
957 // Alt-Gr is CTRL+ALT
958 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
959 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
961 // Try removing the control modifier
963 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
964 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
966 // Try removing shift modifier
967 // for upper case or any punctuation
968 // (since different keyboards will
969 // shift different punctuation types)
970 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
971 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
973 // Try removing alt modifier for
974 // high-bit characters.
975 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
976 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
978 // Give up and return the empty vector.
980 return b.bindings[modifiers];
985 ////////////////////////////////////////////////////////////////////////
986 // Implementation of FGInput::button.
987 ////////////////////////////////////////////////////////////////////////
989 FGInput::button::button ()
990 : is_repeatable(false),
997 FGInput::button::~button ()
999 // FIXME: memory leak
1000 // for (int i = 0; i < KEYMOD_MAX; i++)
1001 // for (int j = 0; i < bindings[i].size(); j++)
1002 // delete bindings[i][j];
1007 ////////////////////////////////////////////////////////////////////////
1008 // Implementation of FGInput::axis.
1009 ////////////////////////////////////////////////////////////////////////
1011 FGInput::axis::axis ()
1012 : last_value(9999999),
1014 low_threshold(-0.9),
1015 high_threshold(0.9),
1021 FGInput::axis::~axis ()
1023 // for (int i = 0; i < KEYMOD_MAX; i++)
1024 // for (int j = 0; i < bindings[i].size(); j++)
1025 // delete bindings[i][j];
1030 ////////////////////////////////////////////////////////////////////////
1031 // Implementation of FGInput::joystick.
1032 ////////////////////////////////////////////////////////////////////////
1034 FGInput::joystick::joystick ()
1044 FGInput::joystick::~joystick ()
1053 ////////////////////////////////////////////////////////////////////////
1054 // Implementation of FGInput::mouse_mode
1055 ////////////////////////////////////////////////////////////////////////
1057 FGInput::mouse_mode::mouse_mode ()
1058 : cursor(MOUSE_CURSOR_POINTER),
1060 pass_through(false),
1065 FGInput::mouse_mode::~mouse_mode ()
1067 // FIXME: memory leak
1068 // for (int i = 0; i < KEYMOD_MAX; i++) {
1070 // for (j = 0; i < x_bindings[i].size(); j++)
1071 // delete bindings[i][j];
1072 // for (j = 0; j < y_bindings[i].size(); j++)
1073 // delete bindings[i][j];
1080 ////////////////////////////////////////////////////////////////////////
1081 // Implementation of FGInput::mouse
1082 ////////////////////////////////////////////////////////////////////////
1084 FGInput::mouse::mouse ()
1093 FGInput::mouse::~mouse ()
1098 ////////////////////////////////////////////////////////////////////////
1099 // Implementation of OS callbacks.
1100 ////////////////////////////////////////////////////////////////////////
1102 void keyHandler(int key, int keymod, int mousex, int mousey)
1104 if((keymod & KEYMOD_RELEASED) == 0)
1105 if(puKeyboard(key, PU_DOWN))
1109 default_input->doKey(key, keymod, mousex, mousey);
1112 void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
1115 default_input->doMouseClick(button, updown, x, y, mainWindow, ea);
1118 void mouseMotionHandler(int x, int y)
1120 if (default_input != 0)
1121 default_input->doMouseMotion(x, y);