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.
27 #include <simgear/compiler.h>
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/props.hxx>
36 #include <Aircraft/aircraft.hxx>
37 #include <Autopilot/auto_gui.hxx>
38 #include <Autopilot/newauto.hxx>
39 #include <Cockpit/hud.hxx>
40 #include <Cockpit/panel.hxx>
41 #include <Cockpit/panel_io.hxx>
43 #include <Scenery/tilemgr.hxx>
44 #include <Objects/matlib.hxx>
45 #include <Time/light.hxx>
46 #include <Time/tmp.hxx>
48 #ifndef FG_OLD_WEATHER
49 # include <WeatherCM/FGLocalWeatherDatabase.h>
51 # include <Weather/weather.hxx>
54 #include <Main/bfi.hxx>
55 #include <Main/globals.hxx>
56 #include <Main/keyboard.hxx>
57 #include <Main/fg_props.hxx>
58 #include <Main/options.hxx>
62 SG_USING_STD(ifstream);
67 ////////////////////////////////////////////////////////////////////////
68 // Implementation of FGBinding.
69 ////////////////////////////////////////////////////////////////////////
71 FGBinding::FGBinding ()
72 : _action(ACTION_NONE), _node(0), _adjust_step(0), _assign_value(0)
76 FGBinding::FGBinding (const SGPropertyNode * node)
77 : _action(ACTION_NONE), _node(0), _adjust_step(0), _assign_value(0)
82 FGBinding::~FGBinding ()
88 FGBinding::setAction (Action action)
94 FGBinding::setProperty (SGPropertyNode * node)
100 FGBinding::setAdjustStep (const SGValue * step)
106 FGBinding::setAssignValue (const SGValue * value)
108 _assign_value = value;
112 FGBinding::read (const SGPropertyNode * node)
114 if (node->hasValue("action")) {
115 string action = node->getStringValue("action");
116 if (action == "none")
117 _action = ACTION_NONE;
118 else if (action == "switch")
119 _action = ACTION_SWITCH;
120 else if (action == "adjust")
121 _action = ACTION_ADJUST;
122 else if (action == "assign")
123 _action = ACTION_ASSIGN;
125 SG_LOG(SG_INPUT, SG_ALERT, "Ignoring unrecognized action type "
129 if (node->hasValue("control"))
130 _node = fgGetNode(node->getStringValue("control"), true);
132 if (node->hasValue("step"))
133 _adjust_step = node->getChild("step")->getValue();
135 if (node->hasValue("value"))
136 _assign_value = node->getChild("value")->getValue();
140 FGBinding::fire () const
143 SG_LOG(SG_INPUT, SG_ALERT, "No control property attached to binding");
153 _node->setBoolValue(!_node->getBoolValue());
157 if (_adjust_step == 0) {
158 SG_LOG(SG_INPUT, SG_ALERT, "No step provided for adjust binding");
161 switch (_node->getType()) {
163 if (_adjust_step->getBoolValue())
164 _node->setBoolValue(!_node->getBoolValue());
167 _node->setIntValue(_node->getIntValue() + _adjust_step->getIntValue());
170 _node->setLongValue(_node->getLongValue() + _adjust_step->getLongValue());
173 _node->setFloatValue(_node->getFloatValue()
174 + _adjust_step->getFloatValue());
176 case SGValue::DOUBLE:
177 case SGValue::UNKNOWN: // force to double
178 _node->setDoubleValue(_node->getDoubleValue()
179 + _adjust_step->getDoubleValue());
181 case SGValue::STRING:
182 SG_LOG(SG_INPUT, SG_ALERT, "Cannot increment or decrement string value"
183 << _node->getStringValue());
189 if (_assign_value == 0) {
190 SG_LOG(SG_INPUT, SG_ALERT, "No value provided for assign binding");
193 switch (_node->getType()) {
195 _node->setBoolValue(_assign_value->getBoolValue());
198 _node->setIntValue(_assign_value->getIntValue());
201 _node->setLongValue(_assign_value->getLongValue());
204 _node->setFloatValue(_assign_value->getFloatValue());
206 case SGValue::DOUBLE:
207 _node->setDoubleValue(_assign_value->getDoubleValue());
209 case SGValue::STRING:
210 _node->setStringValue(_assign_value->getStringValue());
212 case SGValue::UNKNOWN:
213 _node->setUnknownValue(_assign_value->getStringValue());
222 ////////////////////////////////////////////////////////////////////////
223 // Implementation of FGInput.
224 ////////////////////////////////////////////////////////////////////////
227 extern void fgReshape( int width, int height );
229 FGInput current_input;
245 // Read the keyboard bindings.
246 // TODO: zero the old bindings first.
247 const SGPropertyNode * keyboard =
248 globals->get_props()->getNode("/input/keyboard", true);
249 vector<const SGPropertyNode *> keys = keyboard->getChildren("key");
251 for (unsigned int i = 0; i < keys.size(); i++) {
252 int index = keys[i]->getIndex();
253 int modifiers = FG_MOD_NONE;
254 if (keys[i]->getBoolValue("mod-shift"))
255 modifiers |= FG_MOD_SHIFT;
256 if (keys[i]->getBoolValue("mod-ctrl"))
257 modifiers |= FG_MOD_CTRL;
258 if (keys[i]->getBoolValue("mod-alt"))
259 modifiers |= FG_MOD_ALT;
260 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index
261 << " with modifiers " << modifiers);
263 vector<const SGPropertyNode *> bindings = keys[i]->getChildren("binding");
264 for (unsigned int j = 0; j < bindings.size(); j++) {
265 SG_LOG(SG_INPUT, SG_INFO, " Adding binding " << j);
266 _key_bindings[modifiers][index].push_back(FGBinding(bindings[j]));
286 // we'll do something here with the joystick
290 FGInput::doKey (int k, int modifiers, int x, int y)
293 static bool winding_ccw = true;
296 SG_LOG(SG_INPUT, SG_INFO, "User pressed key " << k
297 << " with modifiers " << modifiers);
299 if (_key_bindings[modifiers].find(k) != _key_bindings[modifiers].end()) {
300 const vector<FGBinding> &bindings = _key_bindings[modifiers][k];
301 for (unsigned int i = 0; i < bindings.size(); i++) {
307 SG_LOG(SG_INPUT, SG_INFO, "(No user binding.)");
309 // Use the old, default actions.
310 FGInterface *f = current_aircraft.fdm_state;
311 FGViewer *v = globals->get_current_view();
313 // everything after here will be removed sooner or later...
315 if (modifiers & FG_MOD_SHIFT) {
318 case 7: // Ctrl-G key
319 current_autopilot->set_AltitudeMode(
320 FGAutopilot::FG_ALTITUDE_GS1 );
321 current_autopilot->set_AltitudeEnabled(
322 ! current_autopilot->get_AltitudeEnabled()
325 case 18: // Ctrl-R key
327 winding_ccw = !winding_ccw;
329 glFrontFace ( GL_CCW );
331 glFrontFace ( GL_CW );
334 case 19: // Ctrl-S key
335 current_autopilot->set_AutoThrottleEnabled(
336 ! current_autopilot->get_AutoThrottleEnabled()
339 case 20: // Ctrl-T key
340 current_autopilot->set_AltitudeMode(
341 FGAutopilot::FG_ALTITUDE_TERRAIN );
342 current_autopilot->set_AltitudeEnabled(
343 ! current_autopilot->get_AltitudeEnabled()
346 case 49: // numeric keypad 1
347 v->set_goal_view_offset( SGD_PI * 0.75 );
349 case 50: // numeric keypad 2
350 v->set_goal_view_offset( SGD_PI );
352 case 51: // numeric keypad 3
353 v->set_goal_view_offset( SGD_PI * 1.25 );
355 case 52: // numeric keypad 4
356 v->set_goal_view_offset( SGD_PI * 0.50 );
358 case 54: // numeric keypad 6
359 v->set_goal_view_offset( SGD_PI * 1.50 );
361 case 55: // numeric keypad 7
362 v->set_goal_view_offset( SGD_PI * 0.25 );
364 case 56: // numeric keypad 8
365 v->set_goal_view_offset( 0.00 );
367 case 57: // numeric keypad 9
368 v->set_goal_view_offset( SGD_PI * 1.75 );
371 HUD_brightkey( true );
375 fgHUDInit2(¤t_aircraft);
378 globals->inc_warp( -60 );
379 fgUpdateSkyAndLightingParams();
382 globals->inc_warp_delta( -30 );
383 fgUpdateSkyAndLightingParams();
386 #if defined(FX) && !defined(WIN32)
387 global_fullscreen = ( !global_fullscreen );
388 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
389 XMesaSetFXmode( global_fullscreen ?
390 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
395 fov = globals->get_current_view()->get_fov();
397 if ( fov > FG_FOV_MAX ) {
400 globals->get_current_view()->set_fov(fov);
401 // v->force_update_fov_math();
404 #ifndef FG_OLD_WEATHER
405 tmp = WeatherDatabase->getWeatherVisibility();
407 WeatherDatabase->setWeatherVisibility( tmp );
409 tmp = current_weather.get_visibility(); // in meters
411 current_weather.set_visibility( tmp );
417 case 256+GLUT_KEY_F1: {
418 ifstream input("fgfs.sav");
419 if (input.good() && fgLoadFlight(input)) {
421 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from fgfs.sav");
423 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from fgfs.sav");
427 case 256+GLUT_KEY_F2: {
428 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
429 ofstream output("fgfs.sav");
430 if (output.good() && fgSaveFlight(output)) {
432 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to fgfs.sav");
434 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to fgfs.sav");
438 case 256+GLUT_KEY_F3: {
440 fgGetString("/sim/panel/path", "Panels/Default/default.xml");
441 FGPanel * new_panel = fgReadPanel(panel_path);
442 if (new_panel == 0) {
443 SG_LOG(SG_INPUT, SG_ALERT,
444 "Error reading new panel from " << panel_path);
447 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
448 current_panel->unbind();
449 delete current_panel;
450 current_panel = new_panel;
453 case 256+GLUT_KEY_F4: {
454 SGPath props_path(globals->get_fg_root());
455 props_path.append("preferences.xml");
456 SG_LOG(SG_INPUT, SG_INFO, "Rereading global preferences");
457 if (!readProperties(props_path.str(), globals->get_props())) {
458 SG_LOG(SG_INPUT, SG_ALERT,
459 "Failed to reread global preferences from "
460 << props_path.str());
462 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
466 case 256+GLUT_KEY_F5: {
467 current_panel->setYOffset(current_panel->getYOffset() - 5);
468 fgReshape(fgGetInt("/sim/startup/xsize"),
469 fgGetInt("/sim/startup/ysize"));
472 case 256+GLUT_KEY_F6: {
473 current_panel->setYOffset(current_panel->getYOffset() + 5);
474 fgReshape(fgGetInt("/sim/startup/xsize"),
475 fgGetInt("/sim/startup/ysize"));
478 case 256+GLUT_KEY_F7: {
479 current_panel->setXOffset(current_panel->getXOffset() - 5);
482 case 256+GLUT_KEY_F8: {
483 current_panel->setXOffset(current_panel->getXOffset() + 5);
486 // case 256+GLUT_KEY_F9: {
489 case 256+GLUT_KEY_F10: {
490 fgToggleFDMdataLogging();
493 // case 256+GLUT_KEY_F11: {
496 // case 256+GLUT_KEY_F12: {
499 case 256+GLUT_KEY_END: // numeric keypad 1
500 v->set_goal_view_offset( SGD_PI * 0.75 );
502 case 256+GLUT_KEY_DOWN: // numeric keypad 2
503 v->set_goal_view_offset( SGD_PI );
505 case 256+GLUT_KEY_PAGE_DOWN: // numeric keypad 3
506 v->set_goal_view_offset( SGD_PI * 1.25 );
508 case 256+GLUT_KEY_LEFT: // numeric keypad 4
509 v->set_goal_view_offset( SGD_PI * 0.50 );
511 case 256+GLUT_KEY_RIGHT: // numeric keypad 6
512 v->set_goal_view_offset( SGD_PI * 1.50 );
514 case 256+GLUT_KEY_HOME: // numeric keypad 7
515 v->set_goal_view_offset( SGD_PI * 0.25 );
517 case 256+GLUT_KEY_UP: // numeric keypad 8
518 v->set_goal_view_offset( 0.00 );
520 case 256+GLUT_KEY_PAGE_UP: // numeric keypad 9
521 v->set_goal_view_offset( SGD_PI * 1.75 );
530 SG_LOG( SG_INPUT, SG_DEBUG, "" );
535 b_ret = int( controls.get_brake( 0 ) );
536 b_set = double(!b_ret);
537 controls.set_brake( FGControls::ALL_WHEELS, b_set);
540 if (controls.get_brake(0) > 0.0) {
541 controls.set_brake(0, 0.0);
543 controls.set_brake(0, 1.0);
547 if (controls.get_brake(1) > 0.0) {
548 controls.set_brake(1, 0.0);
550 controls.set_brake(1, 1.0);
554 HUD_masterswitch( true );
557 fgHUDInit(¤t_aircraft); // normal HUD
560 globals->inc_warp( 60 );
561 fgUpdateSkyAndLightingParams();
564 globals->set_freeze( ! globals->get_freeze() );
567 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
568 f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
569 SGPath tile_path( globals->get_fg_root() );
570 tile_path.append( "Scenery" );
571 tile_path.append( p.gen_base_path() );
572 tile_path.append( p.gen_index_str() );
574 // printf position and attitude information
575 SG_LOG( SG_INPUT, SG_INFO,
576 "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
577 << " Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
578 << " Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
580 SG_LOG( SG_INPUT, SG_INFO,
581 "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES
582 << " Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
583 << " Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
584 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
588 globals->inc_warp_delta( 30 );
589 fgUpdateSkyAndLightingParams();
592 // handles GUI state as well as Viewer LookAt Direction
594 globals->set_current_view( globals->get_viewmgr()->next_view() );
595 fgReshape( fgGetInt("/sim/startup/xsize"),
596 fgGetInt("/sim/startup/ysize") );
599 fov = globals->get_current_view()->get_fov();
601 if ( fov < FG_FOV_MIN ) {
604 globals->get_current_view()->set_fov(fov);
605 // v->force_update_fov_math();
608 #ifndef FG_OLD_WEATHER
609 tmp = WeatherDatabase->getWeatherVisibility();
611 WeatherDatabase->setWeatherVisibility( tmp );
613 tmp = current_weather.get_visibility(); // in meters
615 current_weather.set_visibility( tmp );
619 // if( fg_DebugOutput ) {
620 // fclose( fg_DebugOutput );
622 SG_LOG( SG_INPUT, SG_ALERT,
623 "Program exit requested." );
629 case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
631 bool freeze = globals->get_freeze();
632 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
634 globals->set_freeze( true );
636 if ( global_tile_mgr.init() ) {
637 // Load the local scenery data
638 global_tile_mgr.update(
639 cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
640 cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
642 SG_LOG( SG_GENERAL, SG_ALERT,
643 "Error in Tile Manager initialization!" );
648 globals->set_freeze( false );
651 case 256+GLUT_KEY_F3: // F3 Take a screen shot
654 case 256+GLUT_KEY_F4: // F4 Update lighting manually
655 fgUpdateSkyAndLightingParams();
657 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
658 if ( current_autopilot->get_HeadingMode() !=
659 FGAutopilot::FG_HEADING_WAYPOINT ) {
660 current_autopilot->set_HeadingMode(
661 FGAutopilot::FG_HEADING_WAYPOINT );
662 current_autopilot->set_HeadingEnabled( true );
664 current_autopilot->set_HeadingMode(
665 FGAutopilot::FG_TC_HEADING_LOCK );
668 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
669 const string &fog = fgGetString("/sim/rendering/fog");
670 if (fog == "disabled") {
671 fgSetString("/sim/rendering/fog", "fastest");
672 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
673 } else if (fog == "fastest") {
674 fgSetString("/sim/rendering/fog", "nicest");
675 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
676 } else if (fog == "nicest") {
677 fgSetString("/sim/rendering/fog", "disabled");
678 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
680 fgSetString("/sim/rendering/fog", "disabled");
681 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
682 << fog << ", changed to 'disabled'");
686 case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
687 SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
688 if ( fgGetBool("/sim/rendering/textures")) {
689 fgSetBool("/sim/rendering/textures", false);
690 material_lib.set_step( 1 );
692 fgSetBool("/sim/rendering/textures", true);
693 material_lib.set_step( 0 );
696 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
697 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
700 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
701 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
704 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
705 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
717 FGInput::action (const SGPropertyNode * binding)
719 const string &action = binding->getStringValue("action", "");
720 const string &control = binding->getStringValue("control", "");
721 bool repeatable = binding->getBoolValue("repeatable", false);
722 int step = binding->getIntValue("step", 0.0);
725 SG_LOG(SG_INPUT, SG_ALERT, "No control specified for key "
726 << binding->getIndex());
730 else if (action == "") {
731 SG_LOG(SG_INPUT, SG_ALERT, "No action specified for key "
732 << binding->getIndex());
736 else if (action == "switch") {
737 fgSetBool(control, !fgGetBool(control));
740 else if (action == "adjust") {
741 const SGValue * step = binding->getValue("step");
743 SG_LOG(SG_INPUT, SG_ALERT, "No step supplied for adjust action for key "
744 << binding->getIndex());
747 SGValue * target = fgGetValue(control, true);
748 // Use the target's type...
749 switch (target->getType()) {
752 target->setIntValue(target->getIntValue() + step->getIntValue());
755 target->setLongValue(target->getLongValue() + step->getLongValue());
758 target->setFloatValue(target->getFloatValue() + step->getFloatValue());
760 case SGValue::DOUBLE:
761 case SGValue::UNKNOWN: // treat unknown as a double
762 target->setDoubleValue(target->getDoubleValue()
763 + step->getDoubleValue());
765 case SGValue::STRING:
766 SG_LOG(SG_INPUT, SG_ALERT, "Failed attempt to adjust string property "
772 else if (action == "assign") {
773 const SGValue * value = binding->getValue("value");
775 SG_LOG(SG_INPUT, SG_ALERT, "No value supplied for assign action for key "
776 << binding->getIndex());
779 SGValue * target = fgGetValue(control, true);
780 // Use the target's type...
781 switch (target->getType()) {
783 target->setBoolValue(value->getBoolValue());
786 target->setIntValue(value->getIntValue());
789 target->setLongValue(value->getLongValue());
792 target->setFloatValue(value->getFloatValue());
794 case SGValue::DOUBLE:
795 target->setDoubleValue(value->getDoubleValue());
797 case SGValue::STRING:
798 target->setStringValue(value->getStringValue());
800 case SGValue::UNKNOWN:
801 target->setUnknownValue(value->getStringValue());
807 SG_LOG(SG_INPUT, SG_ALERT, "Unknown action " << action
808 << " for key " << binding->getIndex());
814 FGViewer *v = globals->get_current_view();
816 SG_LOG( SG_INPUT, SG_DEBUG, "Special key hit = " << k );
818 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
819 SG_LOG( SG_INPUT, SG_DEBUG, " SHIFTED" );