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>
44 #include <simgear/compiler.h>
46 #include <simgear/constants.h>
47 #include <simgear/debug/logstream.hxx>
48 #include <simgear/misc/props.hxx>
50 #include <Aircraft/aircraft.hxx>
51 #include <Autopilot/auto_gui.hxx>
52 #include <Autopilot/newauto.hxx>
53 #include <Cockpit/hud.hxx>
54 #include <Cockpit/panel.hxx>
55 #include <Cockpit/panel_io.hxx>
57 #include <Scenery/tilemgr.hxx>
58 #include <Objects/matlib.hxx>
59 #include <Time/light.hxx>
60 #include <Time/tmp.hxx>
62 #ifndef FG_OLD_WEATHER
63 # include <WeatherCM/FGLocalWeatherDatabase.h>
65 # include <Weather/weather.hxx>
68 #include <Main/globals.hxx>
69 #include <Main/fg_props.hxx>
70 #include <Main/options.hxx>
74 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
75 SG_USING_STD(ifstream);
82 ////////////////////////////////////////////////////////////////////////
83 // Local data structures.
84 ////////////////////////////////////////////////////////////////////////
87 ////////////////////////////////////////////////////////////////////////
88 // Implementation of FGBinding.
89 ////////////////////////////////////////////////////////////////////////
91 FGBinding::FGBinding ()
92 : _command(0), _arg(0)
96 FGBinding::FGBinding (const SGPropertyNode * node)
97 : _command(0), _arg(0)
102 FGBinding::~FGBinding ()
108 FGBinding::read (const SGPropertyNode * node)
110 _command_name = node->getStringValue("command", "");
111 if (_command_name == "") {
112 SG_LOG(SG_INPUT, SG_ALERT, "No command supplied for binding.");
117 _command = globals->get_commands()->getCommand(_command_name);
119 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
123 _arg = node; // FIXME: don't use whole node!!!
127 FGBinding::fire () const
133 FGBinding::fire (double setting) const
137 copyProperties(_arg, &arg);
138 arg.setDoubleValue("setting", setting);
143 FGBinding::_fire(const SGPropertyNode * arg) const
146 SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
147 } else if (!(*_command)(arg)) {
148 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " << _command_name);
154 ////////////////////////////////////////////////////////////////////////
155 // Implementation of FGInput.
156 ////////////////////////////////////////////////////////////////////////
159 extern void fgReshape( int width, int height );
161 FGInput current_input;
201 FGInput::doKey (int k, int modifiers, int x, int y)
203 SG_LOG(SG_INPUT, SG_INFO, "User pressed key " << k
204 << " with modifiers " << modifiers);
207 if (k < 0 || k >= MAX_KEYS) {
208 SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
212 button &b = _key_bindings[k];
215 if (modifiers&FG_MOD_UP == 0) {
216 // SG_LOG( SG_INPUT, SG_INFO, "User pressed key " << k
217 // << " with modifiers " << modifiers );
218 if (!b.last_state || b.is_repeatable) {
219 const binding_list_t &bindings =
220 _find_key_bindings(k, modifiers);
221 int max = bindings.size();
223 for (int i = 0; i < max; i++)
232 // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
233 // << " with modifiers " << modifiers);
235 const binding_list_t &bindings =
236 _find_key_bindings(k, modifiers);
237 int max = bindings.size();
239 for (int i = 0; i < max; i++)
247 // Use the old, default actions.
248 SG_LOG(SG_INPUT, SG_INFO, "(No user binding.)");
249 if (modifiers&FG_MOD_UP)
253 static bool winding_ccw = true;
255 FGInterface *f = current_aircraft.fdm_state;
256 FGViewer *v = globals->get_current_view();
258 // everything after here will be removed sooner or later...
260 if (modifiers & FG_MOD_SHIFT) {
263 case 7: // Ctrl-G key
264 current_autopilot->set_AltitudeMode(
265 FGAutopilot::FG_ALTITUDE_GS1 );
266 current_autopilot->set_AltitudeEnabled(
267 ! current_autopilot->get_AltitudeEnabled()
270 case 18: // Ctrl-R key
272 winding_ccw = !winding_ccw;
274 glFrontFace ( GL_CCW );
276 glFrontFace ( GL_CW );
279 case 20: // Ctrl-T key
280 current_autopilot->set_AltitudeMode(
281 FGAutopilot::FG_ALTITUDE_TERRAIN );
282 current_autopilot->set_AltitudeEnabled(
283 ! current_autopilot->get_AltitudeEnabled()
287 HUD_brightkey( true );
291 fgHUDInit2(¤t_aircraft);
294 globals->inc_warp( -60 );
295 fgUpdateSkyAndLightingParams();
298 globals->inc_warp_delta( -30 );
299 fgUpdateSkyAndLightingParams();
302 #if defined(FX) && !defined(WIN32)
303 global_fullscreen = ( !global_fullscreen );
304 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
305 XMesaSetFXmode( global_fullscreen ?
306 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
311 fov = globals->get_current_view()->get_fov();
313 if ( fov > FG_FOV_MAX ) {
316 globals->get_current_view()->set_fov(fov);
317 // v->force_update_fov_math();
320 #ifndef FG_OLD_WEATHER
321 tmp = WeatherDatabase->getWeatherVisibility();
323 WeatherDatabase->setWeatherVisibility( tmp );
325 tmp = current_weather.get_visibility(); // in meters
327 current_weather.set_visibility( tmp );
333 case 256+GLUT_KEY_F10: {
334 fgToggleFDMdataLogging();
344 SG_LOG( SG_INPUT, SG_DEBUG, "" );
347 HUD_masterswitch( true );
350 fgHUDInit(¤t_aircraft); // normal HUD
353 globals->inc_warp( 60 );
354 fgUpdateSkyAndLightingParams();
357 globals->set_freeze( ! globals->get_freeze() );
360 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
361 f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
362 SGPath tile_path( globals->get_fg_root() );
363 tile_path.append( "Scenery" );
364 tile_path.append( p.gen_base_path() );
365 tile_path.append( p.gen_index_str() );
367 // printf position and attitude information
368 SG_LOG( SG_INPUT, SG_INFO,
369 "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
370 << " Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
371 << " Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
373 SG_LOG( SG_INPUT, SG_INFO,
374 "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES
375 << " Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
376 << " Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
377 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
381 globals->inc_warp_delta( 30 );
382 fgUpdateSkyAndLightingParams();
385 fov = globals->get_current_view()->get_fov();
387 if ( fov < FG_FOV_MIN ) {
390 globals->get_current_view()->set_fov(fov);
391 // v->force_update_fov_math();
394 #ifndef FG_OLD_WEATHER
395 tmp = WeatherDatabase->getWeatherVisibility();
397 WeatherDatabase->setWeatherVisibility( tmp );
399 tmp = current_weather.get_visibility(); // in meters
401 current_weather.set_visibility( tmp );
405 // if( fg_DebugOutput ) {
406 // fclose( fg_DebugOutput );
408 SG_LOG( SG_INPUT, SG_ALERT,
409 "Program exit requested." );
415 case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
417 bool freeze = globals->get_freeze();
418 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
420 globals->set_freeze( true );
422 if ( global_tile_mgr.init() ) {
423 // Load the local scenery data
424 global_tile_mgr.update(
425 cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
426 cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
428 SG_LOG( SG_GENERAL, SG_ALERT,
429 "Error in Tile Manager initialization!" );
434 globals->set_freeze( false );
437 case 256+GLUT_KEY_F4: // F4 Update lighting manually
438 fgUpdateSkyAndLightingParams();
440 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
441 if ( current_autopilot->get_HeadingMode() !=
442 FGAutopilot::FG_HEADING_WAYPOINT ) {
443 current_autopilot->set_HeadingMode(
444 FGAutopilot::FG_HEADING_WAYPOINT );
445 current_autopilot->set_HeadingEnabled( true );
447 current_autopilot->set_HeadingMode(
448 FGAutopilot::FG_TC_HEADING_LOCK );
451 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
452 const string &fog = fgGetString("/sim/rendering/fog");
453 if (fog == "disabled") {
454 fgSetString("/sim/rendering/fog", "fastest");
455 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
456 } else if (fog == "fastest") {
457 fgSetString("/sim/rendering/fog", "nicest");
458 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
459 } else if (fog == "nicest") {
460 fgSetString("/sim/rendering/fog", "disabled");
461 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
463 fgSetString("/sim/rendering/fog", "disabled");
464 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
465 << fog << ", changed to 'disabled'");
469 case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
470 SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
471 if ( fgGetBool("/sim/rendering/textures")) {
472 fgSetBool("/sim/rendering/textures", false);
473 material_lib.set_step( 1 );
475 fgSetBool("/sim/rendering/textures", true);
476 material_lib.set_step( 0 );
479 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
480 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
483 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
484 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
487 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
488 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
500 FGInput::_init_keyboard ()
502 // TODO: zero the old bindings first.
503 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
504 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
505 if (key_nodes == 0) {
506 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
510 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
511 for (unsigned int i = 0; i < keys.size(); i++) {
512 int index = keys[i]->getIndex();
513 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
514 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
515 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
521 FGInput::_init_joystick ()
523 // TODO: zero the old bindings first.
524 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
525 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
527 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
531 for (int i = 0; i < MAX_JOYSTICKS; i++) {
532 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
534 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
537 jsJoystick * js = new jsJoystick(i);
538 _joystick_bindings[i].js = js;
539 if (js->notWorking()) {
540 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
545 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
546 int nbuttons = jsCaps.wNumButtons;
547 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
549 int nbuttons = MAX_BUTTONS;
552 int naxes = js->getNumAxes();
553 if (naxes > MAX_AXES) naxes = MAX_AXES;
554 _joystick_bindings[i].naxes = naxes;
555 _joystick_bindings[i].nbuttons = nbuttons;
557 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
559 // Set up range arrays
560 float minRange[MAX_AXES];
561 float maxRange[MAX_AXES];
562 float center[MAX_AXES];
564 // Initialize with default values
565 js->getMinRange(minRange);
566 js->getMaxRange(maxRange);
567 js->getCenter(center);
569 // Allocate axes and buttons
570 _joystick_bindings[i].axes = new axis[naxes];
571 _joystick_bindings[i].buttons = new button[nbuttons];
575 // Initialize the axes.
578 for (j = 0; j < naxes; j++) {
579 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
580 if (axis_node == 0) {
581 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
585 axis &a = _joystick_bindings[i].axes[j];
587 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
589 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
590 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
591 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
592 center[j] = axis_node->getDoubleValue("center", center[j]);
594 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
598 // Initialize the buttons.
600 for (j = 0; j < nbuttons; j++) {
601 const SGPropertyNode * button_node = js_node->getChild("button", j);
602 if (button_node == 0) {
603 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
607 button &b = _joystick_bindings[i].buttons[j];
610 button_node->getBoolValue("repeatable", b.is_repeatable);
612 // Get the bindings for the button
613 _read_bindings(button_node, b.bindings, FG_MOD_NONE);
616 js->setMinRange(minRange);
617 js->setMaxRange(maxRange);
618 js->setCenter(center);
624 FGInput::_update_keyboard ()
631 FGInput::_update_joystick ()
633 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
636 float axis_values[MAX_AXES];
641 for ( i = 0; i < MAX_JOYSTICKS; i++) {
643 jsJoystick * js = _joystick_bindings[i].js;
644 if (js == 0 || js->notWorking())
647 js->read(&buttons, axis_values);
650 // Fire bindings for the axes.
651 for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
652 axis &a = _joystick_bindings[i].axes[j];
654 // Do nothing if the axis position
655 // is unchanged; only a change in
656 // position fires the bindings.
657 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
658 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
660 a.last_value = axis_values[j];
661 // SG_LOG(SG_INPUT, SG_INFO, "There are "
662 // << a.bindings[modifiers].size() << " bindings");
663 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
664 a.bindings[modifiers][k].fire(axis_values[j]);
668 // Fire bindings for the buttons.
669 for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
670 bool pressed = ((buttons & (1 << j)) > 0);
671 button &b = _joystick_bindings[i].buttons[j];
674 // The press event may be repeated.
675 if (!b.last_state || b.is_repeatable) {
676 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
677 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
678 b.bindings[modifiers][k].fire();
681 // The release event is never repeated.
683 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
684 for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
685 b.bindings[modifiers|FG_MOD_UP][k].fire();
688 b.last_state = pressed;
695 FGInput::_read_bindings (const SGPropertyNode * node,
696 binding_list_t * binding_list,
699 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
700 for (unsigned int i = 0; i < bindings.size(); i++) {
701 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
702 << bindings[i]->getStringValue("command"));
703 binding_list[modifiers].push_back(FGBinding(bindings[i]));
706 // Read nested bindings for modifiers
707 if (node->getChild("mod-up") != 0)
708 _read_bindings(node->getChild("mod-up"), binding_list,
709 modifiers|FG_MOD_UP);
711 if (node->getChild("mod-shift") != 0)
712 _read_bindings(node->getChild("mod-shift"), binding_list,
713 modifiers|FG_MOD_SHIFT);
715 if (node->getChild("mod-ctrl") != 0)
716 _read_bindings(node->getChild("mod-ctrl"), binding_list,
717 modifiers|FG_MOD_CTRL);
719 if (node->getChild("mod-alt") != 0)
720 _read_bindings(node->getChild("mod-alt"), binding_list,
721 modifiers|FG_MOD_ALT);
725 const vector<FGBinding> &
726 FGInput::_find_key_bindings (unsigned int k, int modifiers)
728 button &b = _key_bindings[k];
730 // Try it straight, first.
731 if (b.bindings[modifiers].size() > 0)
732 return b.bindings[modifiers];
734 // Try removing the control modifier
736 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
737 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
739 // Try removing shift modifier
740 // for upper case or any punctuation
741 // (since different keyboards will
742 // shift different punctuation types)
743 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
744 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
746 // Try removing alt modifier for
747 // high-bit characters.
748 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
749 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
751 // Give up and return the empty vector.
753 return b.bindings[modifiers];
758 * Construct the modifiers.
760 static inline int get_mods ()
762 int glut_modifiers = glutGetModifiers();
765 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
766 modifiers |= FGInput::FG_MOD_SHIFT;
767 if (glut_modifiers & GLUT_ACTIVE_CTRL)
768 modifiers |= FGInput::FG_MOD_CTRL;
769 if (glut_modifiers & GLUT_ACTIVE_ALT)
770 modifiers |= FGInput::FG_MOD_ALT;
777 * Key-down event handler for Glut.
779 * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
781 * @param k The integer value for the key pressed.
785 void GLUTkey(unsigned char k, int x, int y)
787 // Give PUI a chance to grab it first.
788 if (!puKeyboard(k, PU_DOWN))
789 current_input.doKey(k, get_mods(), x, y);
794 * Key-up event handler for GLUT.
796 * <p>PUI doesn't use this, so always pass it to the input manager.</p>
798 * @param k The integer value for the key pressed.
802 void GLUTkeyup(unsigned char k, int x, int y)
804 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
809 * Special key-down handler for Glut.
811 * <p>Pass the value on to the FGInput module unless PUI wants it.
812 * The key value will have 256 added to it.</p>
814 * @param k The integer value for the key pressed (will have 256 added
819 void GLUTspecialkey(int k, int x, int y)
821 // Give PUI a chance to grab it first.
822 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
823 current_input.doKey(k + 256, get_mods(), x, y);
828 * Special key-up handler for Glut.
830 * @param k The integer value for the key pressed (will have 256 added
835 void GLUTspecialkeyup(int k, int x, int y)
837 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);