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 SG_USING_STD(ifstream);
76 ////////////////////////////////////////////////////////////////////////
77 // Local data structures.
78 ////////////////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////////////////
82 // Implementation of FGBinding.
83 ////////////////////////////////////////////////////////////////////////
85 FGBinding::FGBinding ()
86 : _command(0), _arg(0)
90 FGBinding::FGBinding (const SGPropertyNode * node)
91 : _command(0), _arg(0)
96 FGBinding::~FGBinding ()
102 FGBinding::read (const SGPropertyNode * node)
104 _command_name = node->getStringValue("command", "");
105 if (_command_name == "") {
106 SG_LOG(SG_INPUT, SG_ALERT, "No command supplied for binding.");
111 _command = globals->get_commands()->getCommand(_command_name);
113 SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
117 _arg = node; // FIXME: don't use whole node!!!
121 FGBinding::fire () const
127 FGBinding::fire (double setting) const
130 copyProperties(_arg, &arg);
131 arg.setDoubleValue("setting", setting);
136 FGBinding::_fire(const SGPropertyNode * arg) const
139 SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
140 } else if (!(*_command)(arg)) {
141 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " << _command_name);
147 ////////////////////////////////////////////////////////////////////////
148 // Implementation of FGInput.
149 ////////////////////////////////////////////////////////////////////////
152 extern void fgReshape( int width, int height );
154 FGInput current_input;
194 FGInput::doKey (int k, int modifiers, int x, int y)
197 if (k < 0 || k >= MAX_KEYS) {
198 SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
202 button &b = _key_bindings[k];
205 if (modifiers&FG_MOD_UP == 0) {
206 // SG_LOG(SG_INPUT, SG_INFO, "User pressed key " << k
207 // << " with modifiers " << modifiers);
208 if (!b.last_state || b.is_repeatable) {
209 const binding_list_t &bindings =
210 _find_key_bindings(k, modifiers);
211 int max = bindings.size();
213 for (int i = 0; i < max; i++)
222 // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
223 // << " with modifiers " << modifiers);
225 const binding_list_t &bindings =
226 _find_key_bindings(k, modifiers);
227 int max = bindings.size();
229 for (int i = 0; i < max; i++)
237 // Use the old, default actions.
238 SG_LOG(SG_INPUT, SG_INFO, "(No user binding.)");
239 if (modifiers&FG_MOD_UP)
243 static bool winding_ccw = true;
245 FGInterface *f = current_aircraft.fdm_state;
246 FGViewer *v = globals->get_current_view();
248 // everything after here will be removed sooner or later...
250 if (modifiers & FG_MOD_SHIFT) {
253 case 7: // Ctrl-G key
254 current_autopilot->set_AltitudeMode(
255 FGAutopilot::FG_ALTITUDE_GS1 );
256 current_autopilot->set_AltitudeEnabled(
257 ! current_autopilot->get_AltitudeEnabled()
260 case 18: // Ctrl-R key
262 winding_ccw = !winding_ccw;
264 glFrontFace ( GL_CCW );
266 glFrontFace ( GL_CW );
269 case 20: // Ctrl-T key
270 current_autopilot->set_AltitudeMode(
271 FGAutopilot::FG_ALTITUDE_TERRAIN );
272 current_autopilot->set_AltitudeEnabled(
273 ! current_autopilot->get_AltitudeEnabled()
277 HUD_brightkey( true );
281 fgHUDInit2(¤t_aircraft);
284 globals->inc_warp( -60 );
285 fgUpdateSkyAndLightingParams();
288 globals->inc_warp_delta( -30 );
289 fgUpdateSkyAndLightingParams();
292 #if defined(FX) && !defined(WIN32)
293 global_fullscreen = ( !global_fullscreen );
294 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
295 XMesaSetFXmode( global_fullscreen ?
296 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
301 fov = globals->get_current_view()->get_fov();
303 if ( fov > FG_FOV_MAX ) {
306 globals->get_current_view()->set_fov(fov);
307 // v->force_update_fov_math();
310 #ifndef FG_OLD_WEATHER
311 tmp = WeatherDatabase->getWeatherVisibility();
313 WeatherDatabase->setWeatherVisibility( tmp );
315 tmp = current_weather.get_visibility(); // in meters
317 current_weather.set_visibility( tmp );
323 case 256+GLUT_KEY_F1: {
324 ifstream input("fgfs.sav");
325 if (input.good() && fgLoadFlight(input)) {
327 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from fgfs.sav");
329 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from fgfs.sav");
333 case 256+GLUT_KEY_F2: {
334 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
335 ofstream output("fgfs.sav");
336 if (output.good() && fgSaveFlight(output)) {
338 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to fgfs.sav");
340 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to fgfs.sav");
344 case 256+GLUT_KEY_F3: {
346 fgGetString("/sim/panel/path", "Panels/Default/default.xml");
347 FGPanel * new_panel = fgReadPanel(panel_path);
348 if (new_panel == 0) {
349 SG_LOG(SG_INPUT, SG_ALERT,
350 "Error reading new panel from " << panel_path);
353 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
354 current_panel->unbind();
355 delete current_panel;
356 current_panel = new_panel;
357 current_panel->bind();
360 case 256+GLUT_KEY_F4: {
361 SGPath props_path(globals->get_fg_root());
362 props_path.append("preferences.xml");
363 SG_LOG(SG_INPUT, SG_INFO, "Rereading global preferences");
364 if (!readProperties(props_path.str(), globals->get_props())) {
365 SG_LOG(SG_INPUT, SG_ALERT,
366 "Failed to reread global preferences from "
367 << props_path.str());
369 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
373 case 256+GLUT_KEY_F5: {
374 current_panel->setYOffset(current_panel->getYOffset() - 5);
375 fgReshape(fgGetInt("/sim/startup/xsize"),
376 fgGetInt("/sim/startup/ysize"));
379 case 256+GLUT_KEY_F6: {
380 current_panel->setYOffset(current_panel->getYOffset() + 5);
381 fgReshape(fgGetInt("/sim/startup/xsize"),
382 fgGetInt("/sim/startup/ysize"));
385 case 256+GLUT_KEY_F7: {
386 current_panel->setXOffset(current_panel->getXOffset() - 5);
389 case 256+GLUT_KEY_F8: {
390 current_panel->setXOffset(current_panel->getXOffset() + 5);
393 case 256+GLUT_KEY_F10: {
394 fgToggleFDMdataLogging();
404 SG_LOG( SG_INPUT, SG_DEBUG, "" );
407 HUD_masterswitch( true );
410 fgHUDInit(¤t_aircraft); // normal HUD
413 globals->inc_warp( 60 );
414 fgUpdateSkyAndLightingParams();
417 globals->set_freeze( ! globals->get_freeze() );
420 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
421 f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
422 SGPath tile_path( globals->get_fg_root() );
423 tile_path.append( "Scenery" );
424 tile_path.append( p.gen_base_path() );
425 tile_path.append( p.gen_index_str() );
427 // printf position and attitude information
428 SG_LOG( SG_INPUT, SG_INFO,
429 "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
430 << " Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
431 << " Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
433 SG_LOG( SG_INPUT, SG_INFO,
434 "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES
435 << " Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
436 << " Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
437 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
441 globals->inc_warp_delta( 30 );
442 fgUpdateSkyAndLightingParams();
445 fov = globals->get_current_view()->get_fov();
447 if ( fov < FG_FOV_MIN ) {
450 globals->get_current_view()->set_fov(fov);
451 // v->force_update_fov_math();
454 #ifndef FG_OLD_WEATHER
455 tmp = WeatherDatabase->getWeatherVisibility();
457 WeatherDatabase->setWeatherVisibility( tmp );
459 tmp = current_weather.get_visibility(); // in meters
461 current_weather.set_visibility( tmp );
465 // if( fg_DebugOutput ) {
466 // fclose( fg_DebugOutput );
468 SG_LOG( SG_INPUT, SG_ALERT,
469 "Program exit requested." );
475 case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
477 bool freeze = globals->get_freeze();
478 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
480 globals->set_freeze( true );
482 if ( global_tile_mgr.init() ) {
483 // Load the local scenery data
484 global_tile_mgr.update(
485 cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
486 cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
488 SG_LOG( SG_GENERAL, SG_ALERT,
489 "Error in Tile Manager initialization!" );
494 globals->set_freeze( false );
497 case 256+GLUT_KEY_F4: // F4 Update lighting manually
498 fgUpdateSkyAndLightingParams();
500 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
501 if ( current_autopilot->get_HeadingMode() !=
502 FGAutopilot::FG_HEADING_WAYPOINT ) {
503 current_autopilot->set_HeadingMode(
504 FGAutopilot::FG_HEADING_WAYPOINT );
505 current_autopilot->set_HeadingEnabled( true );
507 current_autopilot->set_HeadingMode(
508 FGAutopilot::FG_TC_HEADING_LOCK );
511 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
512 const string &fog = fgGetString("/sim/rendering/fog");
513 if (fog == "disabled") {
514 fgSetString("/sim/rendering/fog", "fastest");
515 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
516 } else if (fog == "fastest") {
517 fgSetString("/sim/rendering/fog", "nicest");
518 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
519 } else if (fog == "nicest") {
520 fgSetString("/sim/rendering/fog", "disabled");
521 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
523 fgSetString("/sim/rendering/fog", "disabled");
524 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
525 << fog << ", changed to 'disabled'");
529 case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
530 SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
531 if ( fgGetBool("/sim/rendering/textures")) {
532 fgSetBool("/sim/rendering/textures", false);
533 material_lib.set_step( 1 );
535 fgSetBool("/sim/rendering/textures", true);
536 material_lib.set_step( 0 );
539 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
540 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
543 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
544 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
547 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
548 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
560 FGInput::_init_keyboard ()
562 // TODO: zero the old bindings first.
563 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
564 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
565 if (key_nodes == 0) {
566 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
570 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
571 for (unsigned int i = 0; i < keys.size(); i++) {
572 int index = keys[i]->getIndex();
573 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
574 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
575 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
581 FGInput::_init_joystick ()
583 // TODO: zero the old bindings first.
584 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
585 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
587 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
591 for (int i = 0; i < MAX_JOYSTICKS; i++) {
592 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
594 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
597 jsJoystick * js = new jsJoystick(i);
598 _joystick_bindings[i].js = js;
599 if (js->notWorking()) {
600 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
605 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
606 int nbuttons = jsCaps.wNumButtons;
607 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
609 int nbuttons = MAX_BUTTONS;
612 int naxes = js->getNumAxes();
613 if (naxes > MAX_AXES) naxes = MAX_AXES;
614 _joystick_bindings[i].naxes = naxes;
615 _joystick_bindings[i].nbuttons = nbuttons;
617 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
619 // Set up range arrays
620 float minRange[naxes];
621 float maxRange[naxes];
624 // Initialize with default values
625 js->getMinRange(minRange);
626 js->getMaxRange(maxRange);
627 js->getCenter(center);
629 // Allocate axes and buttons
630 _joystick_bindings[i].axes = new axis[naxes];
631 _joystick_bindings[i].buttons = new button[nbuttons];
635 // Initialize the axes.
637 for (int j = 0; j < naxes; j++) {
638 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
639 if (axis_node == 0) {
640 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
644 axis &a = _joystick_bindings[i].axes[j];
646 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
648 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
649 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
650 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
651 center[j] = axis_node->getDoubleValue("center", center[j]);
653 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
657 // Initialize the buttons.
659 for (int j = 0; j < nbuttons; j++) {
660 const SGPropertyNode * button_node = js_node->getChild("button", j);
661 if (button_node == 0) {
662 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
666 button &b = _joystick_bindings[i].buttons[j];
669 button_node->getBoolValue("repeatable", b.is_repeatable);
671 // Get the bindings for the button
672 _read_bindings(button_node, b.bindings, FG_MOD_NONE);
675 js->setMinRange(minRange);
676 js->setMaxRange(maxRange);
677 js->setCenter(center);
683 FGInput::_update_keyboard ()
690 FGInput::_update_joystick ()
692 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
695 float axis_values[MAX_AXES];
697 for (int i = 0; i < MAX_JOYSTICKS; i++) {
699 jsJoystick * js = _joystick_bindings[i].js;
700 if (js == 0 || js->notWorking())
703 js->read(&buttons, axis_values);
706 // Fire bindings for the axes.
707 for (int j = 0; j < _joystick_bindings[i].naxes; j++) {
708 axis &a = _joystick_bindings[i].axes[j];
710 // Do nothing if the axis position
711 // is unchanged; only a change in
712 // position fires the bindings.
713 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
714 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
716 a.last_value = axis_values[j];
717 // SG_LOG(SG_INPUT, SG_INFO, "There are "
718 // << a.bindings[modifiers].size() << " bindings");
719 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
720 a.bindings[modifiers][k].fire(axis_values[j]);
724 // Fire bindings for the buttons.
725 for (int j = 0; j < _joystick_bindings[i].nbuttons; j++) {
726 bool pressed = ((buttons & (1 << j)) > 0);
727 button &b = _joystick_bindings[i].buttons[j];
730 // The press event may be repeated.
731 if (!b.last_state || b.is_repeatable) {
732 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
733 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
734 b.bindings[modifiers][k].fire();
737 // The release event is never repeated.
739 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
740 for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
741 b.bindings[modifiers|FG_MOD_UP][k].fire();
744 b.last_state = pressed;
751 FGInput::_read_bindings (const SGPropertyNode * node,
752 binding_list_t * binding_list,
755 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
756 for (unsigned int i = 0; i < bindings.size(); i++) {
757 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
758 << bindings[i]->getStringValue("command"));
759 binding_list[modifiers].push_back(FGBinding(bindings[i]));
762 // Read nested bindings for modifiers
763 if (node->getChild("mod-up") != 0)
764 _read_bindings(node->getChild("mod-up"), binding_list,
765 modifiers|FG_MOD_UP);
767 if (node->getChild("mod-shift") != 0)
768 _read_bindings(node->getChild("mod-shift"), binding_list,
769 modifiers|FG_MOD_SHIFT);
771 if (node->getChild("mod-ctrl") != 0)
772 _read_bindings(node->getChild("mod-ctrl"), binding_list,
773 modifiers|FG_MOD_CTRL);
775 if (node->getChild("mod-alt") != 0)
776 _read_bindings(node->getChild("mod-alt"), binding_list,
777 modifiers|FG_MOD_ALT);
781 const vector<FGBinding> &
782 FGInput::_find_key_bindings (unsigned int k, int modifiers)
784 button &b = _key_bindings[k];
786 // Try it straight, first.
787 if (b.bindings[modifiers].size() > 0)
788 return b.bindings[modifiers];
790 // Try removing the control modifier
792 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
793 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
795 // Try removing shift modifier
796 // for upper case or any punctuation
797 // (since different keyboards will
798 // shift different punctuation types)
799 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
800 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
802 // Try removing alt modifier for
803 // high-bit characters.
804 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
805 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
807 // Give up and return the empty vector.
809 return b.bindings[modifiers];