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/constants.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/misc/props.hxx>
44 #include <Aircraft/aircraft.hxx>
45 #include <Autopilot/auto_gui.hxx>
46 #include <Autopilot/newauto.hxx>
47 #include <Cockpit/hud.hxx>
48 #include <Cockpit/panel.hxx>
49 #include <Cockpit/panel_io.hxx>
51 #include <Scenery/tilemgr.hxx>
52 #include <Objects/matlib.hxx>
53 #include <Time/light.hxx>
54 #include <Time/tmp.hxx>
56 #ifndef FG_OLD_WEATHER
57 # include <WeatherCM/FGLocalWeatherDatabase.h>
59 # include <Weather/weather.hxx>
62 #include <Main/bfi.hxx>
63 #include <Main/globals.hxx>
64 #include <Main/keyboard.hxx>
65 #include <Main/fg_props.hxx>
66 #include <Main/options.hxx>
70 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
71 SG_USING_STD(ifstream);
78 ////////////////////////////////////////////////////////////////////////
79 // Local data structures.
80 ////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////
84 // Implementation of FGBinding.
85 ////////////////////////////////////////////////////////////////////////
87 FGBinding::FGBinding ()
88 : _command(0), _arg(0)
92 FGBinding::FGBinding (const SGPropertyNode * node)
93 : _command(0), _arg(0)
98 FGBinding::~FGBinding ()
104 FGBinding::read (const SGPropertyNode * node)
106 _command_name = node->getStringValue("command", "");
107 if (_command_name == "") {
108 SG_LOG(SG_INPUT, SG_ALERT, "No command supplied for binding.");
113 _command = globals->get_commands()->getCommand(_command_name);
115 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
119 _arg = node; // FIXME: don't use whole node!!!
123 FGBinding::fire () const
129 FGBinding::fire (double setting) const
132 copyProperties(_arg, &arg);
133 arg.setDoubleValue("setting", setting);
138 FGBinding::_fire(const SGPropertyNode * arg) const
141 SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
142 } else if (!(*_command)(arg)) {
143 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " << _command_name);
149 ////////////////////////////////////////////////////////////////////////
150 // Implementation of FGInput.
151 ////////////////////////////////////////////////////////////////////////
154 extern void fgReshape( int width, int height );
156 FGInput current_input;
196 FGInput::doKey (int k, int modifiers, int x, int y)
199 if (k < 0 || k >= MAX_KEYS) {
200 SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
204 button &b = _key_bindings[k];
207 if (modifiers&FG_MOD_UP == 0) {
208 // SG_LOG(SG_INPUT, SG_INFO, "User pressed key " << k
209 // << " with modifiers " << modifiers);
210 if (!b.last_state || b.is_repeatable) {
211 const binding_list_t &bindings =
212 _find_key_bindings(k, modifiers);
213 int max = bindings.size();
215 for (int i = 0; i < max; i++)
224 // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
225 // << " with modifiers " << modifiers);
227 const binding_list_t &bindings =
228 _find_key_bindings(k, modifiers);
229 int max = bindings.size();
231 for (int i = 0; i < max; i++)
239 // Use the old, default actions.
240 SG_LOG(SG_INPUT, SG_INFO, "(No user binding.)");
241 if (modifiers&FG_MOD_UP)
245 static bool winding_ccw = true;
247 FGInterface *f = current_aircraft.fdm_state;
248 FGViewer *v = globals->get_current_view();
250 // everything after here will be removed sooner or later...
252 if (modifiers & FG_MOD_SHIFT) {
255 case 7: // Ctrl-G key
256 current_autopilot->set_AltitudeMode(
257 FGAutopilot::FG_ALTITUDE_GS1 );
258 current_autopilot->set_AltitudeEnabled(
259 ! current_autopilot->get_AltitudeEnabled()
262 case 18: // Ctrl-R key
264 winding_ccw = !winding_ccw;
266 glFrontFace ( GL_CCW );
268 glFrontFace ( GL_CW );
271 case 20: // Ctrl-T key
272 current_autopilot->set_AltitudeMode(
273 FGAutopilot::FG_ALTITUDE_TERRAIN );
274 current_autopilot->set_AltitudeEnabled(
275 ! current_autopilot->get_AltitudeEnabled()
279 HUD_brightkey( true );
283 fgHUDInit2(¤t_aircraft);
286 globals->inc_warp( -60 );
287 fgUpdateSkyAndLightingParams();
290 globals->inc_warp_delta( -30 );
291 fgUpdateSkyAndLightingParams();
294 #if defined(FX) && !defined(WIN32)
295 global_fullscreen = ( !global_fullscreen );
296 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
297 XMesaSetFXmode( global_fullscreen ?
298 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
303 fov = globals->get_current_view()->get_fov();
305 if ( fov > FG_FOV_MAX ) {
308 globals->get_current_view()->set_fov(fov);
309 // v->force_update_fov_math();
312 #ifndef FG_OLD_WEATHER
313 tmp = WeatherDatabase->getWeatherVisibility();
315 WeatherDatabase->setWeatherVisibility( tmp );
317 tmp = current_weather.get_visibility(); // in meters
319 current_weather.set_visibility( tmp );
325 case 256+GLUT_KEY_F1: {
326 ifstream input("fgfs.sav");
327 if (input.good() && fgLoadFlight(input)) {
329 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from fgfs.sav");
331 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from fgfs.sav");
335 case 256+GLUT_KEY_F2: {
336 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
337 ofstream output("fgfs.sav");
338 if (output.good() && fgSaveFlight(output)) {
340 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to fgfs.sav");
342 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to fgfs.sav");
346 case 256+GLUT_KEY_F3: {
348 fgGetString("/sim/panel/path", "Panels/Default/default.xml");
349 FGPanel * new_panel = fgReadPanel(panel_path);
350 if (new_panel == 0) {
351 SG_LOG(SG_INPUT, SG_ALERT,
352 "Error reading new panel from " << panel_path);
355 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
356 current_panel->unbind();
357 delete current_panel;
358 current_panel = new_panel;
359 current_panel->bind();
362 case 256+GLUT_KEY_F4: {
363 SGPath props_path(globals->get_fg_root());
364 props_path.append("preferences.xml");
365 SG_LOG(SG_INPUT, SG_INFO, "Rereading global preferences");
366 if (!readProperties(props_path.str(), globals->get_props())) {
367 SG_LOG(SG_INPUT, SG_ALERT,
368 "Failed to reread global preferences from "
369 << props_path.str());
371 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
375 case 256+GLUT_KEY_F5: {
376 current_panel->setYOffset(current_panel->getYOffset() - 5);
377 fgReshape(fgGetInt("/sim/startup/xsize"),
378 fgGetInt("/sim/startup/ysize"));
381 case 256+GLUT_KEY_F6: {
382 current_panel->setYOffset(current_panel->getYOffset() + 5);
383 fgReshape(fgGetInt("/sim/startup/xsize"),
384 fgGetInt("/sim/startup/ysize"));
387 case 256+GLUT_KEY_F7: {
388 current_panel->setXOffset(current_panel->getXOffset() - 5);
391 case 256+GLUT_KEY_F8: {
392 current_panel->setXOffset(current_panel->getXOffset() + 5);
395 case 256+GLUT_KEY_F10: {
396 fgToggleFDMdataLogging();
406 SG_LOG( SG_INPUT, SG_DEBUG, "" );
409 HUD_masterswitch( true );
412 fgHUDInit(¤t_aircraft); // normal HUD
415 globals->inc_warp( 60 );
416 fgUpdateSkyAndLightingParams();
419 globals->set_freeze( ! globals->get_freeze() );
422 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
423 f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
424 SGPath tile_path( globals->get_fg_root() );
425 tile_path.append( "Scenery" );
426 tile_path.append( p.gen_base_path() );
427 tile_path.append( p.gen_index_str() );
429 // printf position and attitude information
430 SG_LOG( SG_INPUT, SG_INFO,
431 "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
432 << " Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
433 << " Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
435 SG_LOG( SG_INPUT, SG_INFO,
436 "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES
437 << " Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
438 << " Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
439 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
443 globals->inc_warp_delta( 30 );
444 fgUpdateSkyAndLightingParams();
447 fov = globals->get_current_view()->get_fov();
449 if ( fov < FG_FOV_MIN ) {
452 globals->get_current_view()->set_fov(fov);
453 // v->force_update_fov_math();
456 #ifndef FG_OLD_WEATHER
457 tmp = WeatherDatabase->getWeatherVisibility();
459 WeatherDatabase->setWeatherVisibility( tmp );
461 tmp = current_weather.get_visibility(); // in meters
463 current_weather.set_visibility( tmp );
467 // if( fg_DebugOutput ) {
468 // fclose( fg_DebugOutput );
470 SG_LOG( SG_INPUT, SG_ALERT,
471 "Program exit requested." );
477 case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
479 bool freeze = globals->get_freeze();
480 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
482 globals->set_freeze( true );
484 if ( global_tile_mgr.init() ) {
485 // Load the local scenery data
486 global_tile_mgr.update(
487 cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
488 cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
490 SG_LOG( SG_GENERAL, SG_ALERT,
491 "Error in Tile Manager initialization!" );
496 globals->set_freeze( false );
499 case 256+GLUT_KEY_F4: // F4 Update lighting manually
500 fgUpdateSkyAndLightingParams();
502 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
503 if ( current_autopilot->get_HeadingMode() !=
504 FGAutopilot::FG_HEADING_WAYPOINT ) {
505 current_autopilot->set_HeadingMode(
506 FGAutopilot::FG_HEADING_WAYPOINT );
507 current_autopilot->set_HeadingEnabled( true );
509 current_autopilot->set_HeadingMode(
510 FGAutopilot::FG_TC_HEADING_LOCK );
513 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
514 const string &fog = fgGetString("/sim/rendering/fog");
515 if (fog == "disabled") {
516 fgSetString("/sim/rendering/fog", "fastest");
517 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
518 } else if (fog == "fastest") {
519 fgSetString("/sim/rendering/fog", "nicest");
520 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
521 } else if (fog == "nicest") {
522 fgSetString("/sim/rendering/fog", "disabled");
523 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
525 fgSetString("/sim/rendering/fog", "disabled");
526 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
527 << fog << ", changed to 'disabled'");
531 case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
532 SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
533 if ( fgGetBool("/sim/rendering/textures")) {
534 fgSetBool("/sim/rendering/textures", false);
535 material_lib.set_step( 1 );
537 fgSetBool("/sim/rendering/textures", true);
538 material_lib.set_step( 0 );
541 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
542 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
545 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
546 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
549 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
550 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
562 FGInput::_init_keyboard ()
564 // TODO: zero the old bindings first.
565 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
566 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
567 if (key_nodes == 0) {
568 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
572 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
573 for (unsigned int i = 0; i < keys.size(); i++) {
574 int index = keys[i]->getIndex();
575 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
576 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
577 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
583 FGInput::_init_joystick ()
585 // TODO: zero the old bindings first.
586 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
587 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
589 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
593 for (int i = 0; i < MAX_JOYSTICKS; i++) {
594 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
596 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
599 jsJoystick * js = new jsJoystick(i);
600 _joystick_bindings[i].js = js;
601 if (js->notWorking()) {
602 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
607 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
608 int nbuttons = jsCaps.wNumButtons;
609 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
611 int nbuttons = MAX_BUTTONS;
614 int naxes = js->getNumAxes();
615 if (naxes > MAX_AXES) naxes = MAX_AXES;
616 _joystick_bindings[i].naxes = naxes;
617 _joystick_bindings[i].nbuttons = nbuttons;
619 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
621 // Set up range arrays
622 float minRange[naxes];
623 float maxRange[naxes];
626 // Initialize with default values
627 js->getMinRange(minRange);
628 js->getMaxRange(maxRange);
629 js->getCenter(center);
631 // Allocate axes and buttons
632 _joystick_bindings[i].axes = new axis[naxes];
633 _joystick_bindings[i].buttons = new button[nbuttons];
637 // Initialize the axes.
639 for (int j = 0; j < naxes; j++) {
640 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
641 if (axis_node == 0) {
642 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
646 axis &a = _joystick_bindings[i].axes[j];
648 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
650 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
651 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
652 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
653 center[j] = axis_node->getDoubleValue("center", center[j]);
655 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
659 // Initialize the buttons.
661 for (int j = 0; j < nbuttons; j++) {
662 const SGPropertyNode * button_node = js_node->getChild("button", j);
663 if (button_node == 0) {
664 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
668 button &b = _joystick_bindings[i].buttons[j];
671 button_node->getBoolValue("repeatable", b.is_repeatable);
673 // Get the bindings for the button
674 _read_bindings(button_node, b.bindings, FG_MOD_NONE);
677 js->setMinRange(minRange);
678 js->setMaxRange(maxRange);
679 js->setCenter(center);
685 FGInput::_update_keyboard ()
692 FGInput::_update_joystick ()
694 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
697 float axis_values[MAX_AXES];
699 for (int i = 0; i < MAX_JOYSTICKS; i++) {
701 jsJoystick * js = _joystick_bindings[i].js;
702 if (js == 0 || js->notWorking())
705 js->read(&buttons, axis_values);
708 // Fire bindings for the axes.
709 for (int j = 0; j < _joystick_bindings[i].naxes; j++) {
710 axis &a = _joystick_bindings[i].axes[j];
712 // Do nothing if the axis position
713 // is unchanged; only a change in
714 // position fires the bindings.
715 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
716 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
718 a.last_value = axis_values[j];
719 // SG_LOG(SG_INPUT, SG_INFO, "There are "
720 // << a.bindings[modifiers].size() << " bindings");
721 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
722 a.bindings[modifiers][k].fire(axis_values[j]);
726 // Fire bindings for the buttons.
727 for (int j = 0; j < _joystick_bindings[i].nbuttons; j++) {
728 bool pressed = ((buttons & (1 << j)) > 0);
729 button &b = _joystick_bindings[i].buttons[j];
732 // The press event may be repeated.
733 if (!b.last_state || b.is_repeatable) {
734 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
735 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
736 b.bindings[modifiers][k].fire();
739 // The release event is never repeated.
741 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
742 for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
743 b.bindings[modifiers|FG_MOD_UP][k].fire();
746 b.last_state = pressed;
753 FGInput::_read_bindings (const SGPropertyNode * node,
754 binding_list_t * binding_list,
757 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
758 for (unsigned int i = 0; i < bindings.size(); i++) {
759 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
760 << bindings[i]->getStringValue("command"));
761 binding_list[modifiers].push_back(FGBinding(bindings[i]));
764 // Read nested bindings for modifiers
765 if (node->getChild("mod-up") != 0)
766 _read_bindings(node->getChild("mod-up"), binding_list,
767 modifiers|FG_MOD_UP);
769 if (node->getChild("mod-shift") != 0)
770 _read_bindings(node->getChild("mod-shift"), binding_list,
771 modifiers|FG_MOD_SHIFT);
773 if (node->getChild("mod-ctrl") != 0)
774 _read_bindings(node->getChild("mod-ctrl"), binding_list,
775 modifiers|FG_MOD_CTRL);
777 if (node->getChild("mod-alt") != 0)
778 _read_bindings(node->getChild("mod-alt"), binding_list,
779 modifiers|FG_MOD_ALT);
783 const vector<FGBinding> &
784 FGInput::_find_key_bindings (unsigned int k, int modifiers)
786 button &b = _key_bindings[k];
788 // Try it straight, first.
789 if (b.bindings[modifiers].size() > 0)
790 return b.bindings[modifiers];
792 // Try removing the control modifier
794 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
795 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
797 // Try removing shift modifier
798 // for upper case or any punctuation
799 // (since different keyboards will
800 // shift different punctuation types)
801 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
802 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
804 // Try removing alt modifier for
805 // high-bit characters.
806 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
807 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
809 // Give up and return the empty vector.
811 return b.bindings[modifiers];