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>
40 #include <simgear/compiler.h>
42 #include <simgear/constants.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/math/SGMath.hxx>
45 #include <simgear/props/props.hxx>
46 #include <simgear/scene/util/SGSceneUserData.hxx>
48 #include <Aircraft/aircraft.hxx>
49 #include <Autopilot/xmlauto.hxx>
50 #include <Cockpit/hud.hxx>
51 #include <Cockpit/panel.hxx>
52 #include <Cockpit/panel_io.hxx>
54 #include <Model/panelnode.hxx>
55 #include <Scripting/NasalSys.hxx>
57 #include <Main/globals.hxx>
58 #include <Main/fg_props.hxx>
62 #include <Scenery/scenery.hxx>
63 #include <Main/renderer.hxx>
65 SG_USING_STD(ifstream);
69 void mouseClickHandler(int button, int updown, int x, int y);
70 void mouseMotionHandler(int x, int y);
71 void keyHandler(int key, int keymod, int mousex, int mousey);
74 ////////////////////////////////////////////////////////////////////////
76 ////////////////////////////////////////////////////////////////////////
78 static FGInput * default_input = 0;
81 ////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////
88 return bool(fgGetKeyModifiers() & KEYMOD_SHIFT);
94 return bool(fgGetKeyModifiers() & KEYMOD_CTRL);
100 return bool(fgGetKeyModifiers() & KEYMOD_ALT);
104 ////////////////////////////////////////////////////////////////////////
105 // Implementation of FGInput.
106 ////////////////////////////////////////////////////////////////////////
111 if (default_input == 0)
112 default_input = this;
117 if (default_input == this)
128 fgRegisterKeyHandler(keyHandler);
129 fgRegisterMouseClickHandler(mouseClickHandler);
130 fgRegisterMouseMotionHandler(mouseMotionHandler);
142 _postinit_joystick();
148 fgTie("/devices/status/keyboard/shift", getModShift);
149 fgTie("/devices/status/keyboard/ctrl", getModCtrl);
150 fgTie("/devices/status/keyboard/alt", getModAlt);
156 fgUntie("/devices/status/keyboard/shift");
157 fgUntie("/devices/status/keyboard/ctrl");
158 fgUntie("/devices/status/keyboard/alt");
162 FGInput::update (double dt)
165 _update_joystick(dt);
182 FGInput::is_suspended () const
188 FGInput::makeDefault (bool status)
191 default_input = this;
192 else if (default_input == this)
197 FGInput::doKey (int k, int modifiers, int x, int y)
200 if (k < 0 || k >= MAX_KEYS) {
201 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
205 button &b = _key_bindings[k];
208 if (!(modifiers & KEYMOD_RELEASED)) {
209 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
210 << " with modifiers " << modifiers );
211 if (!b.last_state || b.is_repeatable) {
212 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
214 for (unsigned int i = 0; i < bindings.size(); i++)
221 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
222 << " with modifiers " << modifiers);
224 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
225 for (unsigned int i = 0; i < bindings.size(); i++)
229 if (k >= 1 && k <= 26) {
230 if (_key_bindings[k + '@'].last_state)
231 doKey(k + '@', KEYMOD_RELEASED, x, y);
232 if (_key_bindings[k + '`'].last_state)
233 doKey(k + '`', KEYMOD_RELEASED, x, y);
234 } else if (k >= 'A' && k <= 'Z') {
235 if (_key_bindings[k - '@'].last_state)
236 doKey(k - '@', KEYMOD_RELEASED, x, y);
237 if (_key_bindings[tolower(k)].last_state)
238 doKey(tolower(k), KEYMOD_RELEASED, x, y);
239 } else if (k >= 'a' && k <= 'z') {
240 if (_key_bindings[k - '`'].last_state)
241 doKey(k - '`', KEYMOD_RELEASED, x, y);
242 if (_key_bindings[toupper(k)].last_state)
243 doKey(toupper(k), KEYMOD_RELEASED, x, y);
250 FGInput::doMouseClick (int b, int updown, int x, int y)
252 int modifiers = fgGetKeyModifiers();
254 mouse &m = _mouse_bindings[0];
255 mouse_mode &mode = m.modes[m.current_mode];
257 // Let the property manager know.
258 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
259 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
261 // Pass on to PUI and the panel if
262 // requested, and return if one of
263 // them consumes the event.
265 if (updown != MOUSE_BUTTON_DOWN) {
266 // Execute the mouse up event in any case, may be we should
267 // stop processing here?
268 while (!_activePickCallbacks[b].empty()) {
269 _activePickCallbacks[b].front()->buttonReleased();
270 _activePickCallbacks[b].pop_front();
274 if (mode.pass_through) {
275 if (puMouse(b, updown, x, y))
277 else if ((globals->get_current_panel() != 0) &&
278 globals->get_current_panel()->getVisibility() &&
279 globals->get_current_panel()->doMouseAction(b, updown, x, y))
281 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
284 // pui didn't want the click event so compute a
285 // scenegraph intersection point corresponding to the mouse click
286 if (updown == MOUSE_BUTTON_DOWN) {
287 FGScenery* scenery = globals->get_scenery();
290 // Get the list of hit callbacks. Take the first callback that
291 // accepts the mouse button press and ignore the rest of them
292 // That is they get sorted by distance and by scenegraph depth.
293 // The nearest one is the first one and the deepest
294 // (the most specialized one in the scenegraph) is the first.
295 if (FGRenderer::getPickInfo(start, dir, x, y)) {
296 std::vector<SGSceneryPick> pickList;
297 scenery->pick(start, dir, pickList);
298 std::vector<SGSceneryPick>::const_iterator i;
299 for (i = pickList.begin(); i != pickList.end(); ++i) {
300 if (i->callback->buttonPressed(b, i->info)) {
301 _activePickCallbacks[b].push_back(i->callback);
310 // OK, PUI and the panel didn't want the click
311 if (b >= MAX_MOUSE_BUTTONS) {
312 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
313 << " where only " << MAX_MOUSE_BUTTONS << " expected");
317 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
321 FGInput::doMouseMotion (int x, int y)
323 // Don't call fgGetKeyModifiers() here, until we are using a
324 // toolkit that supports getting the mods from outside a key
325 // callback. Glut doesn't.
326 int modifiers = KEYMOD_NONE;
328 int xsize = fgGetInt("/sim/startup/xsize", 800);
329 int ysize = fgGetInt("/sim/startup/ysize", 600);
331 mouse &m = _mouse_bindings[0];
333 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
338 mouse_mode &mode = m.modes[m.current_mode];
340 // Pass on to PUI if requested, and return
341 // if PUI consumed the event.
342 if (mode.pass_through && puMouse(x, y)) {
348 // OK, PUI didn't want the event,
349 // so we can play with it.
352 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
353 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
357 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
358 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
361 // Constrain the mouse if requested
362 if (mode.constrained) {
363 bool need_warp = false;
364 if (x <= (xsize * .25) || x >= (xsize * .75)) {
369 if (y <= (ysize * .25) || y >= (ysize * .75)) {
382 FGInput::_init_keyboard ()
384 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
386 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
387 if (key_nodes == 0) {
388 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
389 key_nodes = fgGetNode("/input/keyboard", true);
392 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
393 for (unsigned int i = 0; i < keys.size(); i++) {
394 int index = keys[i]->getIndex();
395 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
397 _key_bindings[index].bindings->clear();
398 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
399 _key_bindings[index].last_state = 0;
400 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
406 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
408 ulDir *dir = ulOpenDir(path->c_str());
411 while ((dent = ulReadDir(dir)) != 0) {
412 if (dent->d_name[0] == '.')
415 SGPath p(path->str());
416 p.append(dent->d_name);
417 _scan_joystick_dir(&p, node, index);
421 } else if (path->extension() == "xml") {
422 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
423 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
424 readProperties(path->str(), n);
425 n->setStringValue("source", path->c_str());
431 FGInput::_init_joystick ()
434 // TODO: zero the old bindings first.
435 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
436 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
438 // read all joystick xml files into /input/joysticks/js_named[1000++]
439 SGPath path(globals->get_fg_root());
440 path.append("Input/Joysticks");
441 int js_named_index = 1000;
442 _scan_joystick_dir(&path, js_nodes, &js_named_index);
444 // build name->node map with each <name> (reverse order)
445 map<string, SGPropertyNode_ptr> jsmap;
446 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
448 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
449 SGPropertyNode *n = js_named[k];
450 vector<SGPropertyNode_ptr> names = n->getChildren("name");
451 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
452 for (unsigned int j = 0; j < names.size(); j++)
453 jsmap[names[j]->getStringValue()] = n;
457 for (int i = 0; i < MAX_JOYSTICKS; i++) {
458 jsJoystick * js = new jsJoystick(i);
459 _joystick_bindings[i].js = js;
461 if (js->notWorking()) {
462 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
466 const char * name = js->getName();
467 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
470 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
473 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
474 SGPropertyNode_ptr named;
476 if ((named = jsmap[name])) {
477 string source = named->getStringValue("source", "user defined");
478 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
480 } else if ((named = jsmap["default"])) {
481 string source = named->getStringValue("source", "user defined");
482 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
483 << "\"\nUsing default: \"" << source << '"');
486 throw sg_throwable(string("No joystick configuration file with "
487 "<name>default</name> entry found!"));
490 js_node = js_nodes->getChild("js", i, true);
491 copyProperties(named, js_node);
492 js_node->setStringValue("id", name);
496 // get rid of unused config nodes
497 js_nodes->removeChildren("js-named", false);
502 FGInput::_postinit_joystick()
504 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
505 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
507 for (int i = 0; i < MAX_JOYSTICKS; i++) {
508 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
509 jsJoystick *js = _joystick_bindings[i].js;
510 if (!js_node || js->notWorking())
515 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
516 unsigned int nbuttons = jsCaps.wNumButtons;
517 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
519 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
522 int naxes = js->getNumAxes();
523 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
524 _joystick_bindings[i].naxes = naxes;
525 _joystick_bindings[i].nbuttons = nbuttons;
527 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
529 // Set up range arrays
530 float minRange[MAX_JOYSTICK_AXES];
531 float maxRange[MAX_JOYSTICK_AXES];
532 float center[MAX_JOYSTICK_AXES];
534 // Initialize with default values
535 js->getMinRange(minRange);
536 js->getMaxRange(maxRange);
537 js->getCenter(center);
539 // Allocate axes and buttons
540 _joystick_bindings[i].axes = new axis[naxes];
541 _joystick_bindings[i].buttons = new button[nbuttons];
544 // Initialize nasal groups.
547 init = "this=\"" + string(js_node->getPath()) + "\"";
548 sprintf(_module, "__js%d", i);
549 nasalsys->createModule(_module, _module, init.c_str(), init.size());
551 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
553 for (j = 0; j < nasal.size(); j++) {
554 nasal[j]->setStringValue("module", _module);
555 nasalsys->handleCommand(nasal[j]);
559 // Initialize the axes.
561 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
562 size_t nb_axes = axes.size();
563 for (j = 0; j < nb_axes; j++ ) {
564 const SGPropertyNode * axis_node = axes[j];
565 const SGPropertyNode * num_node = axis_node->getChild("number");
566 int n_axis = axis_node->getIndex();
568 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
570 // Silently ignore platforms that are not specified within the
571 // <number></number> section
576 if (n_axis >= naxes) {
577 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
580 axis &a = _joystick_bindings[i].axes[n_axis];
582 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
584 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
585 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
586 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
587 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
589 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
591 // Initialize the virtual axis buttons.
592 _init_button(axis_node->getChild("low"), a.low, "low");
593 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
595 _init_button(axis_node->getChild("high"), a.high, "high");
596 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
597 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
602 // Initialize the buttons.
604 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
606 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
607 const SGPropertyNode * button_node = buttons[j];
608 const SGPropertyNode * num_node = button_node->getChild("number");
609 size_t n_but = button_node->getIndex();
611 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
614 if (n_but >= nbuttons) {
615 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
619 sprintf(buf, "%d", n_but);
620 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
621 _init_button(button_node,
622 _joystick_bindings[i].buttons[n_but],
625 // get interval-sec property
626 button &b = _joystick_bindings[i].buttons[n_but];
627 if (button_node != 0) {
628 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
633 js->setMinRange(minRange);
634 js->setMaxRange(maxRange);
635 js->setCenter(center);
641 // Map of all known cursor names
642 // This used to contain all the Glut cursors, but those are
643 // not defined by other toolkits. It now supports only the cursor
644 // images we actually use, in the interest of portability. Someday,
645 // it would be cool to write an OpenGL cursor renderer, with the
646 // cursors defined as textures referenced in the property tree. This
647 // list could then be eliminated. -Andy
652 } mouse_cursor_map[] = {
653 { "none", MOUSE_CURSOR_NONE },
654 { "inherit", MOUSE_CURSOR_POINTER },
655 { "wait", MOUSE_CURSOR_WAIT },
656 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
657 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
662 FGInput::_init_mouse ()
664 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
667 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
668 if (mouse_nodes == 0) {
669 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
670 mouse_nodes = fgGetNode("/input/mice", true);
674 for (int i = 0; i < MAX_MICE; i++) {
675 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
676 mouse &m = _mouse_bindings[i];
678 // Grab node pointers
680 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
681 m.mode_node = fgGetNode(buf);
682 if (m.mode_node == NULL) {
683 m.mode_node = fgGetNode(buf, true);
684 m.mode_node->setIntValue(0);
686 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
687 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
688 m.mouse_button_nodes[j] = fgGetNode(buf, true);
689 m.mouse_button_nodes[j]->setBoolValue(false);
692 // Read all the modes
693 m.nModes = mouse_node->getIntValue("mode-count", 1);
694 m.modes = new mouse_mode[m.nModes];
696 for (int j = 0; j < m.nModes; j++) {
699 // Read the mouse cursor for this mode
700 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
701 const char * cursor_name =
702 mode_node->getStringValue("cursor", "inherit");
703 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
704 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
705 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
706 m.modes[j].cursor = mouse_cursor_map[k].cursor;
711 // Read other properties for this mode
712 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
713 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
715 // Read the button bindings for this mode
716 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
718 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
719 sprintf(buf, "mouse button %d", k);
720 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
721 _init_button(mode_node->getChild("button", k),
722 m.modes[j].buttons[k],
726 // Read the axis bindings for this mode
727 _read_bindings(mode_node->getChild("x-axis", 0, true),
728 m.modes[j].x_bindings,
730 _read_bindings(mode_node->getChild("y-axis", 0, true),
731 m.modes[j].y_bindings,
739 FGInput::_init_button (const SGPropertyNode * node,
744 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
746 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
748 // Get the bindings for the button
749 _read_bindings(node, b.bindings, KEYMOD_NONE);
755 FGInput::_update_keyboard ()
762 FGInput::_update_joystick (double dt)
764 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
766 // float js_val, diff;
767 float axis_values[MAX_JOYSTICK_AXES];
772 for ( i = 0; i < MAX_JOYSTICKS; i++) {
774 jsJoystick * js = _joystick_bindings[i].js;
775 if (js == 0 || js->notWorking())
778 js->read(&buttons, axis_values);
780 // Fire bindings for the axes.
781 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
782 axis &a = _joystick_bindings[i].axes[j];
784 // Do nothing if the axis position
785 // is unchanged; only a change in
786 // position fires the bindings.
787 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
788 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
789 a.last_value = axis_values[j];
790 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
791 // << a.bindings[modifiers].size() << " bindings");
792 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
793 a.bindings[modifiers][k]->fire(axis_values[j]);
796 // do we have to emulate axis buttons?
798 if(a.last_dt >= a.interval_sec) {
799 if (a.low.bindings[modifiers].size())
800 _update_button(_joystick_bindings[i].axes[j].low,
802 axis_values[j] < a.low_threshold,
805 if (a.high.bindings[modifiers].size())
806 _update_button(_joystick_bindings[i].axes[j].high,
808 axis_values[j] > a.high_threshold,
810 a.last_dt -= a.interval_sec;
814 // Fire bindings for the buttons.
815 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
816 button &b = _joystick_bindings[i].buttons[j];
818 if(b.last_dt >= b.interval_sec) {
819 _update_button(_joystick_bindings[i].buttons[j],
821 (buttons & (1 << j)) > 0,
823 b.last_dt -= b.interval_sec;
830 FGInput::_update_mouse ( double dt )
832 mouse &m = _mouse_bindings[0];
833 int mode = m.mode_node->getIntValue();
834 if (mode != m.current_mode) {
835 m.current_mode = mode;
836 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
837 if (mode >= 0 && mode < m.nModes) {
838 fgSetMouseCursor(m.modes[mode].cursor);
839 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
840 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
841 fgWarpMouse(m.x, m.y);
843 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
844 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
848 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
849 if ( m.x != m.save_x || m.y != m.save_y ) {
850 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
851 fgSetMouseCursor(m.modes[mode].cursor);
854 if ( m.timeout <= 0.0 ) {
855 fgSetMouseCursor(MOUSE_CURSOR_NONE);
863 // handle repeatable mouse press events
864 std::map<int, std::list<SGSharedPtr<SGPickCallback> > >::iterator mi;
865 for (mi = _activePickCallbacks.begin();
866 mi != _activePickCallbacks.end(); ++mi) {
867 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
868 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
875 FGInput::_update_button (button &b, int modifiers, bool pressed,
879 // The press event may be repeated.
880 if (!b.last_state || b.is_repeatable) {
881 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
882 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++) {
883 b.bindings[modifiers][k]->fire(x, y);
887 // The release event is never repeated.
889 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
890 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
891 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
895 b.last_state = pressed;
900 FGInput::_read_bindings (const SGPropertyNode * node,
901 binding_list_t * binding_list,
904 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
905 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
906 for (unsigned int i = 0; i < bindings.size(); i++) {
907 const char *cmd = bindings[i]->getStringValue("command");
908 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
910 if (!strcmp(cmd, "nasal") && _module[0])
911 bindings[i]->setStringValue("module", _module);
912 binding_list[modifiers].push_back(new SGBinding(bindings[i], globals->get_props()));
915 // Read nested bindings for modifiers
916 if (node->getChild("mod-up") != 0)
917 _read_bindings(node->getChild("mod-up"), binding_list,
918 modifiers|KEYMOD_RELEASED);
920 if (node->getChild("mod-shift") != 0)
921 _read_bindings(node->getChild("mod-shift"), binding_list,
922 modifiers|KEYMOD_SHIFT);
924 if (node->getChild("mod-ctrl") != 0)
925 _read_bindings(node->getChild("mod-ctrl"), binding_list,
926 modifiers|KEYMOD_CTRL);
928 if (node->getChild("mod-alt") != 0)
929 _read_bindings(node->getChild("mod-alt"), binding_list,
930 modifiers|KEYMOD_ALT);
934 const FGInput::binding_list_t&
935 FGInput::_find_key_bindings (unsigned int k, int modifiers)
937 unsigned char kc = (unsigned char)k;
938 button &b = _key_bindings[k];
940 // Try it straight, first.
941 if (b.bindings[modifiers].size() > 0)
942 return b.bindings[modifiers];
944 // Alt-Gr is CTRL+ALT
945 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
946 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
948 // Try removing the control modifier
950 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
951 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
953 // Try removing shift modifier
954 // for upper case or any punctuation
955 // (since different keyboards will
956 // shift different punctuation types)
957 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
958 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
960 // Try removing alt modifier for
961 // high-bit characters.
962 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
963 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
965 // Give up and return the empty vector.
967 return b.bindings[modifiers];
972 ////////////////////////////////////////////////////////////////////////
973 // Implementation of FGInput::button.
974 ////////////////////////////////////////////////////////////////////////
976 FGInput::button::button ()
977 : is_repeatable(false),
984 FGInput::button::~button ()
986 // FIXME: memory leak
987 // for (int i = 0; i < KEYMOD_MAX; i++)
988 // for (int j = 0; i < bindings[i].size(); j++)
989 // delete bindings[i][j];
994 ////////////////////////////////////////////////////////////////////////
995 // Implementation of FGInput::axis.
996 ////////////////////////////////////////////////////////////////////////
998 FGInput::axis::axis ()
999 : last_value(9999999),
1001 low_threshold(-0.9),
1002 high_threshold(0.9),
1008 FGInput::axis::~axis ()
1010 // for (int i = 0; i < KEYMOD_MAX; i++)
1011 // for (int j = 0; i < bindings[i].size(); j++)
1012 // delete bindings[i][j];
1017 ////////////////////////////////////////////////////////////////////////
1018 // Implementation of FGInput::joystick.
1019 ////////////////////////////////////////////////////////////////////////
1021 FGInput::joystick::joystick ()
1031 FGInput::joystick::~joystick ()
1040 ////////////////////////////////////////////////////////////////////////
1041 // Implementation of FGInput::mouse_mode
1042 ////////////////////////////////////////////////////////////////////////
1044 FGInput::mouse_mode::mouse_mode ()
1045 : cursor(MOUSE_CURSOR_POINTER),
1047 pass_through(false),
1052 FGInput::mouse_mode::~mouse_mode ()
1054 // FIXME: memory leak
1055 // for (int i = 0; i < KEYMOD_MAX; i++) {
1057 // for (j = 0; i < x_bindings[i].size(); j++)
1058 // delete bindings[i][j];
1059 // for (j = 0; j < y_bindings[i].size(); j++)
1060 // delete bindings[i][j];
1067 ////////////////////////////////////////////////////////////////////////
1068 // Implementation of FGInput::mouse
1069 ////////////////////////////////////////////////////////////////////////
1071 FGInput::mouse::mouse ()
1080 FGInput::mouse::~mouse ()
1085 ////////////////////////////////////////////////////////////////////////
1086 // Implementation of OS callbacks.
1087 ////////////////////////////////////////////////////////////////////////
1089 void keyHandler(int key, int keymod, int mousex, int mousey)
1091 if((keymod & KEYMOD_RELEASED) == 0)
1092 if(puKeyboard(key, PU_DOWN))
1096 default_input->doKey(key, keymod, mousex, mousey);
1099 void mouseClickHandler(int button, int updown, int x, int y)
1102 default_input->doMouseClick(button, updown, x, y);
1105 void mouseMotionHandler(int x, int y)
1107 if (default_input != 0)
1108 default_input->doMouseMotion(x, y);