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
136 copyProperties(_arg, &arg);
137 arg.setDoubleValue("setting", setting);
142 FGBinding::_fire(const SGPropertyNode * arg) const
145 SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
146 } else if (!(*_command)(arg)) {
147 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " << _command_name);
153 ////////////////////////////////////////////////////////////////////////
154 // Implementation of FGInput.
155 ////////////////////////////////////////////////////////////////////////
158 extern void fgReshape( int width, int height );
160 FGInput current_input;
200 FGInput::doKey (int k, int modifiers, int x, int y)
203 if (k < 0 || k >= MAX_KEYS) {
204 SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
208 button &b = _key_bindings[k];
211 if (modifiers&FG_MOD_UP == 0) {
212 // SG_LOG(SG_INPUT, SG_INFO, "User pressed key " << k
213 // << " with modifiers " << modifiers);
214 if (!b.last_state || b.is_repeatable) {
215 const binding_list_t &bindings =
216 _find_key_bindings(k, modifiers);
217 int max = bindings.size();
219 for (int i = 0; i < max; i++)
228 // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
229 // << " with modifiers " << modifiers);
231 const binding_list_t &bindings =
232 _find_key_bindings(k, modifiers);
233 int max = bindings.size();
235 for (int i = 0; i < max; i++)
243 // Use the old, default actions.
244 SG_LOG(SG_INPUT, SG_INFO, "(No user binding.)");
245 if (modifiers&FG_MOD_UP)
249 static bool winding_ccw = true;
251 FGInterface *f = current_aircraft.fdm_state;
252 FGViewer *v = globals->get_current_view();
254 // everything after here will be removed sooner or later...
256 if (modifiers & FG_MOD_SHIFT) {
259 case 7: // Ctrl-G key
260 current_autopilot->set_AltitudeMode(
261 FGAutopilot::FG_ALTITUDE_GS1 );
262 current_autopilot->set_AltitudeEnabled(
263 ! current_autopilot->get_AltitudeEnabled()
266 case 18: // Ctrl-R key
268 winding_ccw = !winding_ccw;
270 glFrontFace ( GL_CCW );
272 glFrontFace ( GL_CW );
275 case 20: // Ctrl-T key
276 current_autopilot->set_AltitudeMode(
277 FGAutopilot::FG_ALTITUDE_TERRAIN );
278 current_autopilot->set_AltitudeEnabled(
279 ! current_autopilot->get_AltitudeEnabled()
283 HUD_brightkey( true );
287 fgHUDInit2(¤t_aircraft);
290 globals->inc_warp( -60 );
291 fgUpdateSkyAndLightingParams();
294 globals->inc_warp_delta( -30 );
295 fgUpdateSkyAndLightingParams();
298 #if defined(FX) && !defined(WIN32)
299 global_fullscreen = ( !global_fullscreen );
300 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
301 XMesaSetFXmode( global_fullscreen ?
302 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
307 fov = globals->get_current_view()->get_fov();
309 if ( fov > FG_FOV_MAX ) {
312 globals->get_current_view()->set_fov(fov);
313 // v->force_update_fov_math();
316 #ifndef FG_OLD_WEATHER
317 tmp = WeatherDatabase->getWeatherVisibility();
319 WeatherDatabase->setWeatherVisibility( tmp );
321 tmp = current_weather.get_visibility(); // in meters
323 current_weather.set_visibility( tmp );
329 case 256+GLUT_KEY_F10: {
330 fgToggleFDMdataLogging();
340 SG_LOG( SG_INPUT, SG_DEBUG, "" );
343 HUD_masterswitch( true );
346 fgHUDInit(¤t_aircraft); // normal HUD
349 globals->inc_warp( 60 );
350 fgUpdateSkyAndLightingParams();
353 globals->set_freeze( ! globals->get_freeze() );
356 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
357 f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
358 SGPath tile_path( globals->get_fg_root() );
359 tile_path.append( "Scenery" );
360 tile_path.append( p.gen_base_path() );
361 tile_path.append( p.gen_index_str() );
363 // printf position and attitude information
364 SG_LOG( SG_INPUT, SG_INFO,
365 "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
366 << " Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
367 << " Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
369 SG_LOG( SG_INPUT, SG_INFO,
370 "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES
371 << " Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
372 << " Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
373 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
377 globals->inc_warp_delta( 30 );
378 fgUpdateSkyAndLightingParams();
381 fov = globals->get_current_view()->get_fov();
383 if ( fov < FG_FOV_MIN ) {
386 globals->get_current_view()->set_fov(fov);
387 // v->force_update_fov_math();
390 #ifndef FG_OLD_WEATHER
391 tmp = WeatherDatabase->getWeatherVisibility();
393 WeatherDatabase->setWeatherVisibility( tmp );
395 tmp = current_weather.get_visibility(); // in meters
397 current_weather.set_visibility( tmp );
401 // if( fg_DebugOutput ) {
402 // fclose( fg_DebugOutput );
404 SG_LOG( SG_INPUT, SG_ALERT,
405 "Program exit requested." );
411 case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
413 bool freeze = globals->get_freeze();
414 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
416 globals->set_freeze( true );
418 if ( global_tile_mgr.init() ) {
419 // Load the local scenery data
420 global_tile_mgr.update(
421 cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
422 cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
424 SG_LOG( SG_GENERAL, SG_ALERT,
425 "Error in Tile Manager initialization!" );
430 globals->set_freeze( false );
433 case 256+GLUT_KEY_F4: // F4 Update lighting manually
434 fgUpdateSkyAndLightingParams();
436 case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
437 if ( current_autopilot->get_HeadingMode() !=
438 FGAutopilot::FG_HEADING_WAYPOINT ) {
439 current_autopilot->set_HeadingMode(
440 FGAutopilot::FG_HEADING_WAYPOINT );
441 current_autopilot->set_HeadingEnabled( true );
443 current_autopilot->set_HeadingMode(
444 FGAutopilot::FG_TC_HEADING_LOCK );
447 case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
448 const string &fog = fgGetString("/sim/rendering/fog");
449 if (fog == "disabled") {
450 fgSetString("/sim/rendering/fog", "fastest");
451 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
452 } else if (fog == "fastest") {
453 fgSetString("/sim/rendering/fog", "nicest");
454 SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
455 } else if (fog == "nicest") {
456 fgSetString("/sim/rendering/fog", "disabled");
457 SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
459 fgSetString("/sim/rendering/fog", "disabled");
460 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
461 << fog << ", changed to 'disabled'");
465 case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
466 SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
467 if ( fgGetBool("/sim/rendering/textures")) {
468 fgSetBool("/sim/rendering/textures", false);
469 material_lib.set_step( 1 );
471 fgSetBool("/sim/rendering/textures", true);
472 material_lib.set_step( 0 );
475 case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
476 SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
479 case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
480 SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
483 case 256+GLUT_KEY_F12: // F12 Heading Dialog...
484 SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
496 FGInput::_init_keyboard ()
498 // TODO: zero the old bindings first.
499 SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
500 SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
501 if (key_nodes == 0) {
502 SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
506 vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
507 for (unsigned int i = 0; i < keys.size(); i++) {
508 int index = keys[i]->getIndex();
509 SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
510 _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
511 _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
517 FGInput::_init_joystick ()
519 // TODO: zero the old bindings first.
520 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
521 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
523 SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
527 for (int i = 0; i < MAX_JOYSTICKS; i++) {
528 const SGPropertyNode * js_node = js_nodes->getChild("js", i);
530 SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
533 jsJoystick * js = new jsJoystick(i);
534 _joystick_bindings[i].js = js;
535 if (js->notWorking()) {
536 SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
541 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
542 int nbuttons = jsCaps.wNumButtons;
543 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
545 int nbuttons = MAX_BUTTONS;
548 int naxes = js->getNumAxes();
549 if (naxes > MAX_AXES) naxes = MAX_AXES;
550 _joystick_bindings[i].naxes = naxes;
551 _joystick_bindings[i].nbuttons = nbuttons;
553 SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
555 // Set up range arrays
556 float minRange[naxes];
557 float maxRange[naxes];
560 // Initialize with default values
561 js->getMinRange(minRange);
562 js->getMaxRange(maxRange);
563 js->getCenter(center);
565 // Allocate axes and buttons
566 _joystick_bindings[i].axes = new axis[naxes];
567 _joystick_bindings[i].buttons = new button[nbuttons];
571 // Initialize the axes.
573 for (int j = 0; j < naxes; j++) {
574 const SGPropertyNode * axis_node = js_node->getChild("axis", j);
575 if (axis_node == 0) {
576 SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
580 axis &a = _joystick_bindings[i].axes[j];
582 js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
584 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
585 minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
586 maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
587 center[j] = axis_node->getDoubleValue("center", center[j]);
589 _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
593 // Initialize the buttons.
595 for (int j = 0; j < nbuttons; j++) {
596 const SGPropertyNode * button_node = js_node->getChild("button", j);
597 if (button_node == 0) {
598 SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
602 button &b = _joystick_bindings[i].buttons[j];
605 button_node->getBoolValue("repeatable", b.is_repeatable);
607 // Get the bindings for the button
608 _read_bindings(button_node, b.bindings, FG_MOD_NONE);
611 js->setMinRange(minRange);
612 js->setMaxRange(maxRange);
613 js->setCenter(center);
619 FGInput::_update_keyboard ()
626 FGInput::_update_joystick ()
628 int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
631 float axis_values[MAX_AXES];
633 for (int i = 0; i < MAX_JOYSTICKS; i++) {
635 jsJoystick * js = _joystick_bindings[i].js;
636 if (js == 0 || js->notWorking())
639 js->read(&buttons, axis_values);
642 // Fire bindings for the axes.
643 for (int j = 0; j < _joystick_bindings[i].naxes; j++) {
644 axis &a = _joystick_bindings[i].axes[j];
646 // Do nothing if the axis position
647 // is unchanged; only a change in
648 // position fires the bindings.
649 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
650 // SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
652 a.last_value = axis_values[j];
653 // SG_LOG(SG_INPUT, SG_INFO, "There are "
654 // << a.bindings[modifiers].size() << " bindings");
655 for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
656 a.bindings[modifiers][k].fire(axis_values[j]);
660 // Fire bindings for the buttons.
661 for (int j = 0; j < _joystick_bindings[i].nbuttons; j++) {
662 bool pressed = ((buttons & (1 << j)) > 0);
663 button &b = _joystick_bindings[i].buttons[j];
666 // The press event may be repeated.
667 if (!b.last_state || b.is_repeatable) {
668 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
669 for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
670 b.bindings[modifiers][k].fire();
673 // The release event is never repeated.
675 // SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
676 for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
677 b.bindings[modifiers|FG_MOD_UP][k].fire();
680 b.last_state = pressed;
687 FGInput::_read_bindings (const SGPropertyNode * node,
688 binding_list_t * binding_list,
691 vector<const SGPropertyNode *> bindings = node->getChildren("binding");
692 for (unsigned int i = 0; i < bindings.size(); i++) {
693 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
694 << bindings[i]->getStringValue("command"));
695 binding_list[modifiers].push_back(FGBinding(bindings[i]));
698 // Read nested bindings for modifiers
699 if (node->getChild("mod-up") != 0)
700 _read_bindings(node->getChild("mod-up"), binding_list,
701 modifiers|FG_MOD_UP);
703 if (node->getChild("mod-shift") != 0)
704 _read_bindings(node->getChild("mod-shift"), binding_list,
705 modifiers|FG_MOD_SHIFT);
707 if (node->getChild("mod-ctrl") != 0)
708 _read_bindings(node->getChild("mod-ctrl"), binding_list,
709 modifiers|FG_MOD_CTRL);
711 if (node->getChild("mod-alt") != 0)
712 _read_bindings(node->getChild("mod-alt"), binding_list,
713 modifiers|FG_MOD_ALT);
717 const vector<FGBinding> &
718 FGInput::_find_key_bindings (unsigned int k, int modifiers)
720 button &b = _key_bindings[k];
722 // Try it straight, first.
723 if (b.bindings[modifiers].size() > 0)
724 return b.bindings[modifiers];
726 // Try removing the control modifier
728 else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
729 return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
731 // Try removing shift modifier
732 // for upper case or any punctuation
733 // (since different keyboards will
734 // shift different punctuation types)
735 else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
736 return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
738 // Try removing alt modifier for
739 // high-bit characters.
740 else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
741 return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
743 // Give up and return the empty vector.
745 return b.bindings[modifiers];
750 * Construct the modifiers.
752 static inline int get_mods ()
754 int glut_modifiers = glutGetModifiers();
757 if (glut_modifiers & GLUT_ACTIVE_SHIFT)
758 modifiers |= FGInput::FG_MOD_SHIFT;
759 if (glut_modifiers & GLUT_ACTIVE_CTRL)
760 modifiers |= FGInput::FG_MOD_CTRL;
761 if (glut_modifiers & GLUT_ACTIVE_ALT)
762 modifiers |= FGInput::FG_MOD_ALT;
769 * Key-down event handler for Glut.
771 * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
773 * @param k The integer value for the key pressed.
777 void GLUTkey(unsigned char k, int x, int y)
779 // Give PUI a chance to grab it first.
780 if (!puKeyboard(k, PU_DOWN))
781 current_input.doKey(k, get_mods(), x, y);
786 * Key-up event handler for GLUT.
788 * <p>PUI doesn't use this, so always pass it to the input manager.</p>
790 * @param k The integer value for the key pressed.
794 void GLUTkeyup(unsigned char k, int x, int y)
796 current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
801 * Special key-down handler for Glut.
803 * <p>Pass the value on to the FGInput module unless PUI wants it.
804 * The key value will have 256 added to it.</p>
806 * @param k The integer value for the key pressed (will have 256 added
811 void GLUTspecialkey(int k, int x, int y)
813 // Give PUI a chance to grab it first.
814 if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
815 current_input.doKey(k + 256, get_mods(), x, y);
820 * Special key-up handler for Glut.
822 * @param k The integer value for the key pressed (will have 256 added
827 void GLUTspecialkeyup(int k, int x, int y)
829 current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);