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., 675 Mass Ave, Cambridge, MA 02139, 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/props/props.hxx>
46 #include <Aircraft/aircraft.hxx>
47 #include <Autopilot/xmlauto.hxx>
48 #include <Cockpit/hud.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <Cockpit/panel_io.hxx>
52 #include <Model/panelnode.hxx>
53 #include <Scripting/NasalSys.hxx>
55 #include <Main/globals.hxx>
56 #include <Main/fg_props.hxx>
60 #include <Scenery/scenery.hxx>
61 #include <Main/renderer.hxx>
63 #include <simgear/math/sg_geodesy.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;
82 ////////////////////////////////////////////////////////////////////////
83 // Implementation of FGBinding.
84 ////////////////////////////////////////////////////////////////////////
86 FGBinding::FGBinding ()
88 _arg(new SGPropertyNode),
93 FGBinding::FGBinding (const SGPropertyNode * node)
101 FGBinding::~FGBinding ()
103 _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
107 FGBinding::read (const SGPropertyNode * node)
109 const SGPropertyNode * conditionNode = node->getChild("condition");
110 if (conditionNode != 0)
111 setCondition(sgReadCondition(globals->get_props(), conditionNode));
113 _command_name = node->getStringValue("command", "");
114 if (_command_name.empty()) {
115 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
120 _arg = (SGPropertyNode *)node;
125 FGBinding::fire () const
129 _command = globals->get_commands()->getCommand(_command_name);
131 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
132 } else if (!(*_command)(_arg)) {
133 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
140 FGBinding::fire (double offset, double max) const
143 _arg->setDoubleValue("offset", offset/max);
149 FGBinding::fire (double setting) const
152 // A value is automatically added to
154 if (_setting == 0) // save the setting node for efficiency
155 _setting = _arg->getChild("setting", 0, true);
156 _setting->setDoubleValue(setting);
163 ////////////////////////////////////////////////////////////////////////
164 // Implementation of FGInput.
165 ////////////////////////////////////////////////////////////////////////
170 if (default_input == 0)
171 default_input = this;
176 if (default_input == this)
187 fgRegisterKeyHandler(keyHandler);
188 fgRegisterMouseClickHandler(mouseClickHandler);
189 fgRegisterMouseMotionHandler(mouseMotionHandler);
201 _postinit_joystick();
205 FGInput::update (double dt)
208 _update_joystick(dt);
225 FGInput::is_suspended () const
231 FGInput::makeDefault (bool status)
234 default_input = this;
235 else if (default_input == this)
240 FGInput::doKey (int k, int modifiers, int x, int y)
243 if (k < 0 || k >= MAX_KEYS) {
244 SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
248 button &b = _key_bindings[k];
251 if (!(modifiers & KEYMOD_RELEASED)) {
252 SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
253 << " with modifiers " << modifiers );
254 if (!b.last_state || b.is_repeatable) {
255 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
257 for (unsigned int i = 0; i < bindings.size(); i++)
264 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
265 << " with modifiers " << modifiers);
267 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
268 for (unsigned int i = 0; i < bindings.size(); i++)
272 if (k >= 1 && k <= 26) {
273 if (_key_bindings[k + '@'].last_state)
274 doKey(k + '@', KEYMOD_RELEASED, x, y);
275 if (_key_bindings[k + '`'].last_state)
276 doKey(k + '`', KEYMOD_RELEASED, x, y);
277 } else if (k >= 'A' && k <= 'Z') {
278 if (_key_bindings[k - '@'].last_state)
279 doKey(k - '@', KEYMOD_RELEASED, x, y);
280 if (_key_bindings[tolower(k)].last_state)
281 doKey(tolower(k), KEYMOD_RELEASED, x, y);
282 } else if (k >= 'a' && k <= 'z') {
283 if (_key_bindings[k - '`'].last_state)
284 doKey(k - '`', KEYMOD_RELEASED, x, y);
285 if (_key_bindings[toupper(k)].last_state)
286 doKey(toupper(k), KEYMOD_RELEASED, x, y);
293 FGInput::doMouseClick (int b, int updown, int x, int y)
295 int modifiers = fgGetKeyModifiers();
297 mouse &m = _mouse_bindings[0];
298 mouse_mode &mode = m.modes[m.current_mode];
300 // Let the property manager know.
301 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
302 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
304 // Pass on to PUI and the panel if
305 // requested, and return if one of
306 // them consumes the event.
307 if (mode.pass_through) {
308 if (puMouse(b, updown, x, y))
310 else if ((globals->get_current_panel() != 0) &&
311 globals->get_current_panel()->getVisibility() &&
312 globals->get_current_panel()->doMouseAction(b, updown, x, y))
314 else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
317 // pui and the panel didn't want the click event so compute a
318 // terrain intersection point corresponding to the mouse click
320 FGScenery* scenery = globals->get_scenery();
321 sgdVec3 start, dir, hit;
322 if (MOUSE_BUTTON_DOWN && FGRenderer::getPickInfo(start, dir, x, y) &&
323 scenery->get_cart_ground_intersection(start, dir, hit)) {
325 Point3D geod = sgCartToGeod(Point3D(hit[0], hit[1], hit[2]));
327 static SGPropertyNode_ptr lon
328 = fgGetNode("/sim/input/click/longitude-deg", true);
329 static SGPropertyNode_ptr lat
330 = fgGetNode("/sim/input/click/latitude-deg", true);
331 static SGPropertyNode_ptr elev_m
332 = fgGetNode("/sim/input/click/elevation-m", true);
333 static SGPropertyNode_ptr elev_ft
334 = fgGetNode("/sim/input/click/elevation-ft", true);
336 lon->setDoubleValue(geod.lon() * SGD_RADIANS_TO_DEGREES);
337 lat->setDoubleValue(geod.lat() * SGD_RADIANS_TO_DEGREES);
338 elev_m->setDoubleValue(geod.elev());
339 elev_ft->setDoubleValue(geod.elev() * SG_METER_TO_FEET);
344 // OK, PUI and the panel didn't want the click
345 if (b >= MAX_MOUSE_BUTTONS) {
346 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
347 << " where only " << MAX_MOUSE_BUTTONS << " expected");
351 _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
355 FGInput::doMouseMotion (int x, int y)
357 // Don't call fgGetKeyModifiers() here, until we are using a
358 // toolkit that supports getting the mods from outside a key
359 // callback. Glut doesn't.
360 int modifiers = KEYMOD_NONE;
362 int xsize = fgGetInt("/sim/startup/xsize", 800);
363 int ysize = fgGetInt("/sim/startup/ysize", 600);
365 mouse &m = _mouse_bindings[0];
367 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
372 mouse_mode &mode = m.modes[m.current_mode];
374 // Pass on to PUI if requested, and return
375 // if PUI consumed the event.
376 if (mode.pass_through && puMouse(x, y)) {
382 // OK, PUI didn't want the event,
383 // so we can play with it.
386 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
387 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
391 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
392 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
395 // Constrain the mouse if requested
396 if (mode.constrained) {
397 bool need_warp = false;
401 } else if (x >= (xsize-1)) {
409 } else if (y >= (ysize-1)) {
422 FGInput::_init_keyboard ()
424 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
426 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
427 if (key_nodes == 0) {
428 SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
429 key_nodes = fgGetNode("/input/keyboard", true);
432 vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
433 for (unsigned int i = 0; i < keys.size(); i++) {
434 int index = keys[i]->getIndex();
435 SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
437 _key_bindings[index].bindings->clear();
438 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
439 _key_bindings[index].last_state = 0;
440 _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
446 FGInput::_scan_joystick_dir(SGPath *path, SGPropertyNode* node, int *index)
448 ulDir *dir = ulOpenDir(path->c_str());
451 while ((dent = ulReadDir(dir)) != 0) {
452 if (dent->d_name[0] == '.')
455 SGPath p(path->str());
456 p.append(dent->d_name);
457 _scan_joystick_dir(&p, node, index);
461 } else if (path->extension() == "xml") {
462 SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path->str());
463 SGPropertyNode *n = node->getChild("js-named", (*index)++, true);
464 readProperties(path->str(), n);
465 n->setStringValue("source", path->c_str());
471 FGInput::_init_joystick ()
474 // TODO: zero the old bindings first.
475 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
476 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
478 // read all joystick xml files into /input/joysticks/js_named[1000++]
479 SGPath path(globals->get_fg_root());
480 path.append("Input/Joysticks");
481 int js_named_index = 1000;
482 _scan_joystick_dir(&path, js_nodes, &js_named_index);
484 // build name->node map with each <name> (reverse order)
485 map<string, SGPropertyNode_ptr> jsmap;
486 vector<SGPropertyNode_ptr> js_named = js_nodes->getChildren("js-named");
488 for (int k = (int)js_named.size() - 1; k >= 0; k--) {
489 SGPropertyNode *n = js_named[k];
490 vector<SGPropertyNode_ptr> names = n->getChildren("name");
491 if (names.size() && (n->getChildren("axis").size() || n->getChildren("button").size()))
492 for (unsigned int j = 0; j < names.size(); j++)
493 jsmap[names[j]->getStringValue()] = n;
497 for (int i = 0; i < MAX_JOYSTICKS; i++) {
498 jsJoystick * js = new jsJoystick(i);
499 _joystick_bindings[i].js = js;
501 if (js->notWorking()) {
502 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
506 const char * name = js->getName();
507 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
510 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
513 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
514 SGPropertyNode_ptr named;
516 if ((named = jsmap[name])) {
517 string source = named->getStringValue("source", "user defined");
518 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
520 } else if ((named = jsmap["default"])) {
521 string source = named->getStringValue("source", "user defined");
522 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
523 << "\"\nUsing default: \"" << source << '"');
526 throw sg_throwable(string("No joystick configuration file with "
527 "<name>default</name> entry found!"));
530 js_node = js_nodes->getChild("js", i, true);
531 copyProperties(named, js_node);
532 js_node->setStringValue("id", name);
536 // get rid of unused config nodes
537 js_nodes->removeChildren("js-named", false);
542 FGInput::_postinit_joystick()
544 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
545 SGPropertyNode *js_nodes = fgGetNode("/input/joysticks");
547 for (int i = 0; i < MAX_JOYSTICKS; i++) {
548 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
549 jsJoystick *js = _joystick_bindings[i].js;
550 if (!js_node || js->notWorking())
555 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
556 unsigned int nbuttons = jsCaps.wNumButtons;
557 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
559 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
562 int naxes = js->getNumAxes();
563 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
564 _joystick_bindings[i].naxes = naxes;
565 _joystick_bindings[i].nbuttons = nbuttons;
567 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
569 // Set up range arrays
570 float minRange[MAX_JOYSTICK_AXES];
571 float maxRange[MAX_JOYSTICK_AXES];
572 float center[MAX_JOYSTICK_AXES];
574 // Initialize with default values
575 js->getMinRange(minRange);
576 js->getMaxRange(maxRange);
577 js->getCenter(center);
579 // Allocate axes and buttons
580 _joystick_bindings[i].axes = new axis[naxes];
581 _joystick_bindings[i].buttons = new button[nbuttons];
584 // Initialize nasal groups.
587 init = "this=\"" + string(js_node->getPath()) + "\"";
588 sprintf(_module, "__js%d", i);
589 nasalsys->createModule(_module, _module, init.c_str(), init.size());
591 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
593 for (j = 0; j < nasal.size(); j++) {
594 nasal[j]->setStringValue("module", _module);
595 nasalsys->handleCommand(nasal[j]);
599 // Initialize the axes.
601 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
602 size_t nb_axes = axes.size();
603 for (j = 0; j < nb_axes; j++ ) {
604 const SGPropertyNode * axis_node = axes[j];
605 const SGPropertyNode * num_node = axis_node->getChild("number");
606 int n_axis = axis_node->getIndex();
608 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
610 // Silently ignore platforms that are not specified within the
611 // <number></number> section
616 if (n_axis >= naxes) {
617 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
620 axis &a = _joystick_bindings[i].axes[n_axis];
622 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
624 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
625 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
626 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
627 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
629 _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
631 // Initialize the virtual axis buttons.
632 _init_button(axis_node->getChild("low"), a.low, "low");
633 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
635 _init_button(axis_node->getChild("high"), a.high, "high");
636 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
637 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
642 // Initialize the buttons.
644 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
646 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
647 const SGPropertyNode * button_node = buttons[j];
648 const SGPropertyNode * num_node = button_node->getChild("number");
649 size_t n_but = button_node->getIndex();
651 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
654 if (n_but >= nbuttons) {
655 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
659 sprintf(buf, "%d", n_but);
660 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
661 _init_button(button_node,
662 _joystick_bindings[i].buttons[n_but],
665 // get interval-sec property
666 button &b = _joystick_bindings[i].buttons[n_but];
667 if (button_node != 0) {
668 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
673 js->setMinRange(minRange);
674 js->setMaxRange(maxRange);
675 js->setCenter(center);
681 // Map of all known cursor names
682 // This used to contain all the Glut cursors, but those are
683 // not defined by other toolkits. It now supports only the cursor
684 // images we actually use, in the interest of portability. Someday,
685 // it would be cool to write an OpenGL cursor renderer, with the
686 // cursors defined as textures referenced in the property tree. This
687 // list could then be eliminated. -Andy
692 } mouse_cursor_map[] = {
693 { "none", MOUSE_CURSOR_NONE },
694 { "inherit", MOUSE_CURSOR_POINTER },
695 { "wait", MOUSE_CURSOR_WAIT },
696 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
697 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
702 FGInput::_init_mouse ()
704 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
707 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
708 if (mouse_nodes == 0) {
709 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
710 mouse_nodes = fgGetNode("/input/mice", true);
714 for (int i = 0; i < MAX_MICE; i++) {
715 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
716 mouse &m = _mouse_bindings[i];
718 // Grab node pointers
720 sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
721 m.mode_node = fgGetNode(buf);
722 if (m.mode_node == NULL) {
723 m.mode_node = fgGetNode(buf, true);
724 m.mode_node->setIntValue(0);
726 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
727 sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
728 m.mouse_button_nodes[j] = fgGetNode(buf, true);
729 m.mouse_button_nodes[j]->setBoolValue(false);
732 // Read all the modes
733 m.nModes = mouse_node->getIntValue("mode-count", 1);
734 m.modes = new mouse_mode[m.nModes];
736 for (int j = 0; j < m.nModes; j++) {
739 // Read the mouse cursor for this mode
740 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
741 const char * cursor_name =
742 mode_node->getStringValue("cursor", "inherit");
743 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
744 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
745 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
746 m.modes[j].cursor = mouse_cursor_map[k].cursor;
751 // Read other properties for this mode
752 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
753 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
755 // Read the button bindings for this mode
756 m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
758 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
759 sprintf(buf, "mouse button %d", k);
760 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
761 _init_button(mode_node->getChild("button", k),
762 m.modes[j].buttons[k],
766 // Read the axis bindings for this mode
767 _read_bindings(mode_node->getChild("x-axis", 0, true),
768 m.modes[j].x_bindings,
770 _read_bindings(mode_node->getChild("y-axis", 0, true),
771 m.modes[j].y_bindings,
779 FGInput::_init_button (const SGPropertyNode * node,
784 SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
786 b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
788 // Get the bindings for the button
789 _read_bindings(node, b.bindings, KEYMOD_NONE);
795 FGInput::_update_keyboard ()
802 FGInput::_update_joystick (double dt)
804 int modifiers = KEYMOD_NONE; // FIXME: any way to get the real ones?
806 // float js_val, diff;
807 float axis_values[MAX_JOYSTICK_AXES];
812 for ( i = 0; i < MAX_JOYSTICKS; i++) {
814 jsJoystick * js = _joystick_bindings[i].js;
815 if (js == 0 || js->notWorking())
818 js->read(&buttons, axis_values);
820 // Fire bindings for the axes.
821 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
822 axis &a = _joystick_bindings[i].axes[j];
824 // Do nothing if the axis position
825 // is unchanged; only a change in
826 // position fires the bindings.
827 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
828 // SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
829 a.last_value = axis_values[j];
830 // SG_LOG(SG_INPUT, SG_DEBUG, "There are "
831 // << a.bindings[modifiers].size() << " bindings");
832 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
833 a.bindings[modifiers][k]->fire(axis_values[j]);
836 // do we have to emulate axis buttons?
838 if(a.last_dt >= a.interval_sec) {
839 if (a.low.bindings[modifiers].size())
840 _update_button(_joystick_bindings[i].axes[j].low,
842 axis_values[j] < a.low_threshold,
845 if (a.high.bindings[modifiers].size())
846 _update_button(_joystick_bindings[i].axes[j].high,
848 axis_values[j] > a.high_threshold,
850 a.last_dt -= a.interval_sec;
854 // Fire bindings for the buttons.
855 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
856 button &b = _joystick_bindings[i].buttons[j];
858 if(b.last_dt >= b.interval_sec) {
859 _update_button(_joystick_bindings[i].buttons[j],
861 (buttons & (1 << j)) > 0,
863 b.last_dt -= b.interval_sec;
870 FGInput::_update_mouse ( double dt )
872 mouse &m = _mouse_bindings[0];
873 int mode = m.mode_node->getIntValue();
874 if (mode != m.current_mode) {
875 m.current_mode = mode;
876 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
877 if (mode >= 0 && mode < m.nModes) {
878 fgSetMouseCursor(m.modes[mode].cursor);
879 m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
880 m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
881 fgWarpMouse(m.x, m.y);
883 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
884 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
888 if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
889 if ( m.x != m.save_x || m.y != m.save_y ) {
890 m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
891 fgSetMouseCursor(m.modes[mode].cursor);
894 if ( m.timeout <= 0.0 ) {
895 fgSetMouseCursor(MOUSE_CURSOR_NONE);
905 FGInput::_update_button (button &b, int modifiers, bool pressed,
909 // The press event may be repeated.
910 if (!b.last_state || b.is_repeatable) {
911 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
912 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
913 b.bindings[modifiers][k]->fire(x, y);
916 // The release event is never repeated.
918 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
919 for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
920 b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
924 b.last_state = pressed;
929 FGInput::_read_bindings (const SGPropertyNode * node,
930 binding_list_t * binding_list,
933 SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
934 vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
935 for (unsigned int i = 0; i < bindings.size(); i++) {
936 const char *cmd = bindings[i]->getStringValue("command");
937 SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd);
939 if (!strcmp(cmd, "nasal") && _module[0])
940 bindings[i]->setStringValue("module", _module);
941 binding_list[modifiers].push_back(new FGBinding(bindings[i]));
944 // Read nested bindings for modifiers
945 if (node->getChild("mod-up") != 0)
946 _read_bindings(node->getChild("mod-up"), binding_list,
947 modifiers|KEYMOD_RELEASED);
949 if (node->getChild("mod-shift") != 0)
950 _read_bindings(node->getChild("mod-shift"), binding_list,
951 modifiers|KEYMOD_SHIFT);
953 if (node->getChild("mod-ctrl") != 0)
954 _read_bindings(node->getChild("mod-ctrl"), binding_list,
955 modifiers|KEYMOD_CTRL);
957 if (node->getChild("mod-alt") != 0)
958 _read_bindings(node->getChild("mod-alt"), binding_list,
959 modifiers|KEYMOD_ALT);
963 const vector<FGBinding *> &
964 FGInput::_find_key_bindings (unsigned int k, int modifiers)
966 unsigned char kc = (unsigned char)k;
967 button &b = _key_bindings[k];
969 // Try it straight, first.
970 if (b.bindings[modifiers].size() > 0)
971 return b.bindings[modifiers];
973 // Alt-Gr is CTRL+ALT
974 else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
975 return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
977 // Try removing the control modifier
979 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
980 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
982 // Try removing shift modifier
983 // for upper case or any punctuation
984 // (since different keyboards will
985 // shift different punctuation types)
986 else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
987 return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
989 // Try removing alt modifier for
990 // high-bit characters.
991 else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
992 return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
994 // Give up and return the empty vector.
996 return b.bindings[modifiers];
1001 ////////////////////////////////////////////////////////////////////////
1002 // Implementation of FGInput::button.
1003 ////////////////////////////////////////////////////////////////////////
1005 FGInput::button::button ()
1006 : is_repeatable(false),
1013 FGInput::button::~button ()
1015 // FIXME: memory leak
1016 // for (int i = 0; i < KEYMOD_MAX; i++)
1017 // for (int j = 0; i < bindings[i].size(); j++)
1018 // delete bindings[i][j];
1023 ////////////////////////////////////////////////////////////////////////
1024 // Implementation of FGInput::axis.
1025 ////////////////////////////////////////////////////////////////////////
1027 FGInput::axis::axis ()
1028 : last_value(9999999),
1030 low_threshold(-0.9),
1031 high_threshold(0.9),
1037 FGInput::axis::~axis ()
1039 // for (int i = 0; i < KEYMOD_MAX; i++)
1040 // for (int j = 0; i < bindings[i].size(); j++)
1041 // delete bindings[i][j];
1046 ////////////////////////////////////////////////////////////////////////
1047 // Implementation of FGInput::joystick.
1048 ////////////////////////////////////////////////////////////////////////
1050 FGInput::joystick::joystick ()
1060 FGInput::joystick::~joystick ()
1069 ////////////////////////////////////////////////////////////////////////
1070 // Implementation of FGInput::mouse_mode
1071 ////////////////////////////////////////////////////////////////////////
1073 FGInput::mouse_mode::mouse_mode ()
1074 : cursor(MOUSE_CURSOR_POINTER),
1076 pass_through(false),
1081 FGInput::mouse_mode::~mouse_mode ()
1083 // FIXME: memory leak
1084 // for (int i = 0; i < KEYMOD_MAX; i++) {
1086 // for (j = 0; i < x_bindings[i].size(); j++)
1087 // delete bindings[i][j];
1088 // for (j = 0; j < y_bindings[i].size(); j++)
1089 // delete bindings[i][j];
1096 ////////////////////////////////////////////////////////////////////////
1097 // Implementation of FGInput::mouse
1098 ////////////////////////////////////////////////////////////////////////
1100 FGInput::mouse::mouse ()
1109 FGInput::mouse::~mouse ()
1114 ////////////////////////////////////////////////////////////////////////
1115 // Implementation of OS callbacks.
1116 ////////////////////////////////////////////////////////////////////////
1118 void keyHandler(int key, int keymod, int mousex, int mousey)
1120 if((keymod & KEYMOD_RELEASED) == 0)
1121 if(puKeyboard(key, PU_DOWN))
1125 default_input->doKey(key, keymod, mousex, mousey);
1128 void mouseClickHandler(int button, int updown, int x, int y)
1131 default_input->doMouseClick(button, updown, x, y);
1134 void mouseMotionHandler(int x, int y)
1136 if (default_input != 0)
1137 default_input->doMouseMotion(x, y);