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.)");
240 static bool winding_ccw = true;
242 FGInterface *f = current_aircraft.fdm_state;
243 FGViewer *v = globals->get_current_view();
245 // everything after here will be removed sooner or later...
247 if (modifiers & FG_MOD_SHIFT) {
250 case 7: // Ctrl-G key
251 current_autopilot->set_AltitudeMode(
252 FGAutopilot::FG_ALTITUDE_GS1 );
253 current_autopilot->set_AltitudeEnabled(
254 ! current_autopilot->get_AltitudeEnabled()
257 case 18: // Ctrl-R key
259 winding_ccw = !winding_ccw;
261 glFrontFace ( GL_CCW );
263 glFrontFace ( GL_CW );
266 case 20: // Ctrl-T key
267 current_autopilot->set_AltitudeMode(
268 FGAutopilot::FG_ALTITUDE_TERRAIN );
269 current_autopilot->set_AltitudeEnabled(
270 ! current_autopilot->get_AltitudeEnabled()
274 HUD_brightkey( true );
278 fgHUDInit2(¤t_aircraft);
281 globals->inc_warp( -60 );
282 fgUpdateSkyAndLightingParams();
285 globals->inc_warp_delta( -30 );
286 fgUpdateSkyAndLightingParams();
289 #if defined(FX) && !defined(WIN32)
290 global_fullscreen = ( !global_fullscreen );
291 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
292 XMesaSetFXmode( global_fullscreen ?
293 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
298 fov = globals->get_current_view()->get_fov();
300 if ( fov > FG_FOV_MAX ) {
303 globals->get_current_view()->set_fov(fov);
304 // v->force_update_fov_math();
307 #ifndef FG_OLD_WEATHER
308 tmp = WeatherDatabase->getWeatherVisibility();
310 WeatherDatabase->setWeatherVisibility( tmp );
312 tmp = current_weather.get_visibility(); // in meters
314 current_weather.set_visibility( tmp );
320 case 256+GLUT_KEY_F1: {
321 ifstream input("fgfs.sav");
322 if (input.good() && fgLoadFlight(input)) {
324 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from fgfs.sav");
326 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from fgfs.sav");
330 case 256+GLUT_KEY_F2: {
331 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
332 ofstream output("fgfs.sav");
333 if (output.good() && fgSaveFlight(output)) {
335 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to fgfs.sav");
337 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to fgfs.sav");
341 case 256+GLUT_KEY_F3: {
343 fgGetString("/sim/panel/path", "Panels/Default/default.xml");
344 FGPanel * new_panel = fgReadPanel(panel_path);
345 if (new_panel == 0) {
346 SG_LOG(SG_INPUT, SG_ALERT,
347 "Error reading new panel from " << panel_path);
350 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
351 current_panel->unbind();
352 delete current_panel;
353 current_panel = new_panel;
354 current_panel->bind();
357 case 256+GLUT_KEY_F4: {
358 SGPath props_path(globals->get_fg_root());
359 props_path.append("preferences.xml");
360 SG_LOG(SG_INPUT, SG_INFO, "Rereading global preferences");
361 if (!readProperties(props_path.str(), globals->get_props())) {
362 SG_LOG(SG_INPUT, SG_ALERT,
363 "Failed to reread global preferences from "
364 << props_path.str());
366 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
370 case 256+GLUT_KEY_F5: {
371 current_panel->setYOffset(current_panel->getYOffset() - 5);
372 fgReshape(fgGetInt("/sim/startup/xsize"),
373 fgGetInt("/sim/startup/ysize"));
376 case 256+GLUT_KEY_F6: {
377 current_panel->setYOffset(current_panel->getYOffset() + 5);
378 fgReshape(fgGetInt("/sim/startup/xsize"),
379 fgGetInt("/sim/startup/ysize"));
382 case 256+GLUT_KEY_F7: {
383 current_panel->setXOffset(current_panel->getXOffset() - 5);
386 case 256+GLUT_KEY_F8: {
387 current_panel->setXOffset(current_panel->getXOffset() + 5);
390 case 256+GLUT_KEY_F10: {
391 fgToggleFDMdataLogging();
401 SG_LOG( SG_INPUT, SG_DEBUG, "" );
404 HUD_masterswitch( true );
407 fgHUDInit(¤t_aircraft); // normal HUD
410 globals->inc_warp( 60 );
411 fgUpdateSkyAndLightingParams();
414 globals->set_freeze( ! globals->get_freeze() );
417 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
418 f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
419 SGPath tile_path( globals->get_fg_root() );
420 tile_path.append( "Scenery" );
421 tile_path.append( p.gen_base_path() );
422 tile_path.append( p.gen_index_str() );
424 // printf position and attitude information
425 SG_LOG( SG_INPUT, SG_INFO,
426 "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
427 << " Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
428 << " Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
430 SG_LOG( SG_INPUT, SG_INFO,
431 "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES
432 << " Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
433 << " Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
434 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
438 globals->inc_warp_delta( 30 );
439 fgUpdateSkyAndLightingParams();
442 fov = globals->get_current_view()->get_fov();
444 if ( fov < FG_FOV_MIN ) {
447 globals->get_current_view()->set_fov(fov);
448 // v->force_update_fov_math();
451 #ifndef FG_OLD_WEATHER
452 tmp = WeatherDatabase->getWeatherVisibility();
454 WeatherDatabase->setWeatherVisibility( tmp );
456 tmp = current_weather.get_visibility(); // in meters
458 current_weather.set_visibility( tmp );
462 // if( fg_DebugOutput ) {
463 // fclose( fg_DebugOutput );
465 SG_LOG( SG_INPUT, SG_ALERT,
466 "Program exit requested." );
472 case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
474 bool freeze = globals->get_freeze();
475 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
477 globals->set_freeze( true );
479 if ( global_tile_mgr.init() ) {
480 // Load the local scenery data
481 global_tile_mgr.update(
482 cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
483 cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
485 SG_LOG( SG_GENERAL, SG_ALERT,
486 "Error in Tile Manager initialization!" );
491 globals->set_freeze( false );
494 case 256+GLUT_KEY_F4: // F4 Update lighting manually
495 fgUpdateSkyAndLightingParams();
497 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
498 if ( current_autopilot->get_HeadingMode() !=
499 FGAutopilot::FG_HEADING_WAYPOINT ) {
500 current_autopilot->set_HeadingMode(
501 FGAutopilot::FG_HEADING_WAYPOINT );
502 current_autopilot->set_HeadingEnabled( true );
504 current_autopilot->set_HeadingMode(
505 FGAutopilot::FG_TC_HEADING_LOCK );
508 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
509 const string &fog = fgGetString("/sim/rendering/fog");
510 if (fog == "disabled") {
511 fgSetString("/sim/rendering/fog", "fastest");
512 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
513 } else if (fog == "fastest") {
514 fgSetString("/sim/rendering/fog", "nicest");
515 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
516 } else if (fog == "nicest") {
517 fgSetString("/sim/rendering/fog", "disabled");
518 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
520 fgSetString("/sim/rendering/fog", "disabled");
521 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
522 << fog << ", changed to 'disabled'");
526 case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
527 SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
528 if ( fgGetBool("/sim/rendering/textures")) {
529 fgSetBool("/sim/rendering/textures", false);
530 material_lib.set_step( 1 );
532 fgSetBool("/sim/rendering/textures", true);
533 material_lib.set_step( 0 );
536 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
537 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
540 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
541 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
544 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
545 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
557 FGInput::_init_keyboard ()
559 // TODO: zero the old bindings first.
560 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
561 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
562 if (key_nodes == 0) {
563 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
567 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
568 for (unsigned int i = 0; i < keys.size(); i++) {
569 int index = keys[i]->getIndex();
570 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
571 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
572 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
578 FGInput::_init_joystick ()
580 // TODO: zero the old bindings first.
581 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
582 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
584 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
588 for (int i = 0; i < MAX_JOYSTICKS; i++) {
589 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
591 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
594 jsJoystick * js = new jsJoystick(i);
595 _joystick_bindings[i].js = js;
596 if (js->notWorking()) {
597 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
602 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
603 int nbuttons = jsCaps.wNumButtons;
604 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
606 int nbuttons = MAX_BUTTONS;
609 int naxes = js->getNumAxes();
610 if (naxes > MAX_AXES) naxes = MAX_AXES;
611 _joystick_bindings[i].naxes = naxes;
612 _joystick_bindings[i].nbuttons = nbuttons;
614 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
616 // Set up range arrays
617 float minRange[naxes];
618 float maxRange[naxes];
621 // Initialize with default values
622 js->getMinRange(minRange);
623 js->getMaxRange(maxRange);
624 js->getCenter(center);
626 // Allocate axes and buttons
627 _joystick_bindings[i].axes = new axis[naxes];
628 _joystick_bindings[i].buttons = new button[nbuttons];
632 // Initialize the axes.
634 for (int j = 0; j < naxes; j++) {
635 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
636 if (axis_node == 0) {
637 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
641 axis &a = _joystick_bindings[i].axes[j];
643 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
645 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
646 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
647 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
648 center[j] = axis_node->getDoubleValue("center", center[j]);
650 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
654 // Initialize the buttons.
656 for (int j = 0; j < nbuttons; j++) {
657 const SGPropertyNode * button_node = js_node->getChild("button", j);
658 if (button_node == 0) {
659 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
663 button &b = _joystick_bindings[i].buttons[j];
666 button_node->getBoolValue("repeatable", b.is_repeatable);
668 // Get the bindings for the button
669 _read_bindings(button_node, b.bindings, FG_MOD_NONE);
672 js->setMinRange(minRange);
673 js->setMaxRange(maxRange);
674 js->setCenter(center);
680 FGInput::_update_keyboard ()
687 FGInput::_update_joystick ()
689 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
692 float axis_values[MAX_AXES];
694 for (int i = 0; i < MAX_JOYSTICKS; i++) {
696 jsJoystick * js = _joystick_bindings[i].js;
697 if (js == 0 || js->notWorking())
700 js->read(&buttons, axis_values);
703 // Fire bindings for the axes.
704 for (int j = 0; j < _joystick_bindings[i].naxes; j++) {
705 axis &a = _joystick_bindings[i].axes[j];
707 // Do nothing if the axis position
708 // is unchanged; only a change in
709 // position fires the bindings.
710 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
711 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
713 a.last_value = axis_values[j];
714 // SG_LOG(SG_INPUT, SG_INFO, "There are "
715 // << a.bindings[modifiers].size() << " bindings");
716 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
717 a.bindings[modifiers][k].fire(axis_values[j]);
721 // Fire bindings for the buttons.
722 for (int j = 0; j < _joystick_bindings[i].nbuttons; j++) {
723 bool pressed = ((buttons & (1 << j)) > 0);
724 button &b = _joystick_bindings[i].buttons[j];
727 // The press event may be repeated.
728 if (!b.last_state || b.is_repeatable) {
729 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
730 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
731 b.bindings[modifiers][k].fire();
734 // The release event is never repeated.
736 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
737 for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
738 b.bindings[modifiers|FG_MOD_UP][k].fire();
741 b.last_state = pressed;
748 FGInput::_read_bindings (const SGPropertyNode * node,
749 binding_list_t * binding_list,
752 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
753 for (unsigned int i = 0; i < bindings.size(); i++) {
754 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
755 << bindings[i]->getStringValue("command"));
756 binding_list[modifiers].push_back(FGBinding(bindings[i]));
759 // Read nested bindings for modifiers
760 if (node->getChild("mod-up") != 0)
761 _read_bindings(node->getChild("mod-up"), binding_list,
762 modifiers|FG_MOD_UP);
764 if (node->getChild("mod-shift") != 0)
765 _read_bindings(node->getChild("mod-shift"), binding_list,
766 modifiers|FG_MOD_SHIFT);
768 if (node->getChild("mod-ctrl") != 0)
769 _read_bindings(node->getChild("mod-ctrl"), binding_list,
770 modifiers|FG_MOD_CTRL);
772 if (node->getChild("mod-alt") != 0)
773 _read_bindings(node->getChild("mod-alt"), binding_list,
774 modifiers|FG_MOD_ALT);
778 const vector<FGBinding> &
779 FGInput::_find_key_bindings (unsigned int k, int modifiers)
781 button &b = _key_bindings[k];
783 // Try it straight, first.
784 if (b.bindings[modifiers].size() > 0)
785 return b.bindings[modifiers];
787 // Try removing the control modifier
789 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
790 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
792 // Try removing shift modifier
793 // for upper case or any punctuation
794 // (since different keyboards will
795 // shift different punctuation types)
796 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
797 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
799 // Try removing alt modifier for
800 // high-bit characters.
801 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
802 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
804 // Give up and return the empty vector.
806 return b.bindings[modifiers];