]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
Fixed bug reported by Norman Vine:
[flightgear.git] / src / Input / input.cxx
1 // input.cxx -- handle user input from various sources.
2 //
3 // Written by David Megginson, started May 2001.
4 //
5 // Copyright (C) 2001 David Megginson, david@megginson.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #ifdef HAVE_WINDOWS_H
28 #  include <windows.h>                     
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <math.h>
34 #include <ctype.h>
35
36 #include STL_FSTREAM
37 #include STL_STRING
38 #include <vector>
39
40 #include <GL/glut.h>
41
42 #include <plib/pu.h>
43
44 #include <simgear/compiler.h>
45
46 #include <simgear/constants.h>
47 #include <simgear/debug/logstream.hxx>
48 #include <simgear/misc/props.hxx>
49
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>
56 #include <GUI/gui.h>
57
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
60
61 #include "input.hxx"
62
63 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
64 SG_USING_STD(ifstream);
65 #endif
66 SG_USING_STD(string);
67 SG_USING_STD(vector);
68
69
70 \f
71 ////////////////////////////////////////////////////////////////////////
72 // Local data structures.
73 ////////////////////////////////////////////////////////////////////////
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Implementation of FGBinding.
78 ////////////////////////////////////////////////////////////////////////
79
80 FGBinding::FGBinding ()
81   : _command(0),
82     _arg(new SGPropertyNode),
83     _setting(0),
84     _command_state(0)
85 {
86 }
87
88 FGBinding::FGBinding (const SGPropertyNode * node)
89   : _command(0),
90     _arg(new SGPropertyNode),
91     _setting(0),
92     _command_state(0)
93 {
94   read(node);
95 }
96
97 FGBinding::~FGBinding ()
98 {
99 //   delete _arg;                       // Delete the saved arguments
100 //   delete _command_state;     // Delete the saved command state
101 }
102
103 void
104 FGBinding::read (const SGPropertyNode * node)
105 {
106   const SGPropertyNode * conditionNode = node->getChild("condition");
107   if (conditionNode != 0) {
108     cerr << "Adding condition to binding" << endl;
109     setCondition(fgReadCondition(conditionNode));
110   }
111
112   _command_name = node->getStringValue("command", "");
113   if (_command_name.empty()) {
114     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
115     _command = 0;
116     return;
117   }
118
119   _command = globals->get_commands()->getCommand(_command_name);
120   if (_command == 0) {
121     SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
122     _arg = 0;
123     return;
124   }
125
126   delete _arg;
127   _arg = new SGPropertyNode;
128   _setting = 0;
129   copyProperties(node, _arg);  // FIXME: don't use whole node!!!
130 }
131
132 void
133 FGBinding::fire () const
134 {
135   if (test()) {
136     if (_command == 0) {
137       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
138     } else if (!(*_command)(_arg, &_command_state)) {
139       SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
140              << _command_name);
141     }
142   }
143 }
144
145 void
146 FGBinding::fire (double offset, double max) const
147 {
148   if (test()) {
149     _arg->setDoubleValue("offset", offset/max);
150     fire();
151   }
152 }
153
154 void
155 FGBinding::fire (double setting) const
156 {
157   if (test()) {
158                                 // A value is automatically added to
159                                 // the args
160     if (_setting == 0)          // save the setting node for efficiency
161       _setting = _arg->getChild("setting", 0, true);
162     _setting->setDoubleValue(setting);
163     fire();
164   }
165 }
166
167
168 \f
169 ////////////////////////////////////////////////////////////////////////
170 // Implementation of FGInput.
171 ////////////////////////////////////////////////////////////////////////
172
173                                 // From main.cxx
174 extern void fgReshape( int width, int height );
175
176 FGInput current_input;
177
178
179 FGInput::FGInput ()
180 {
181   // no op
182 }
183
184 FGInput::~FGInput ()
185 {
186   // no op
187 }
188
189 void
190 FGInput::init ()
191 {
192   _init_keyboard();
193   _init_joystick();
194   _init_mouse();
195
196   glutKeyboardFunc(GLUTkey);
197   glutKeyboardUpFunc(GLUTkeyup);
198   glutSpecialFunc(GLUTspecialkey);
199   glutSpecialUpFunc(GLUTspecialkeyup);
200 #ifdef FG_NEW_MOUSE
201   glutMouseFunc (GLUTmouse);
202   glutMotionFunc (GLUTmotion);
203   glutPassiveMotionFunc (GLUTmotion);
204 #endif
205 }
206
207 void
208 FGInput::bind ()
209 {
210   // no op
211 }
212
213 void
214 FGInput::unbind ()
215 {
216   // no op
217 }
218
219 void 
220 FGInput::update (int dt)
221 {
222   _update_keyboard();
223   _update_joystick();
224   _update_mouse();
225 }
226
227 void
228 FGInput::doKey (int k, int modifiers, int x, int y)
229 {
230   SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
231           << " with modifiers " << modifiers );
232
233                                 // Sanity check.
234   if (k < 0 || k >= MAX_KEYS) {
235     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
236     return;
237   }
238
239   button &b = _key_bindings[k];
240
241                                 // Key pressed.
242   if (modifiers&FG_MOD_UP == 0) {
243     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
244             << " with modifiers " << modifiers );
245     if (!b.last_state || b.is_repeatable) {
246       const binding_list_t &bindings =
247         _find_key_bindings(k, modifiers);
248       int max = bindings.size();
249       if (max > 0) {
250         for (int i = 0; i < max; i++)
251           bindings[i]->fire();
252         return;
253       }
254     }
255   }
256
257                                 // Key released.
258   else {
259     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
260            << " with modifiers " << modifiers);
261     if (b.last_state) {
262       const binding_list_t &bindings =
263         _find_key_bindings(k, modifiers);
264       int max = bindings.size();
265       if (max > 0) {
266         for (int i = 0; i < max; i++)
267           bindings[i]->fire();
268         return;
269       }
270     }
271   }
272
273
274                                 // Use the old, default actions.
275   SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
276   if (modifiers&FG_MOD_UP)
277     return;
278
279   // everything after here will be removed sooner or later...
280
281   if (modifiers & FG_MOD_SHIFT) {
282
283         switch (k) {
284         case 72: // H key
285             HUD_brightkey( true );
286             return;
287         case 73: // I key
288             // Minimal Hud
289             fgHUDInit2(&current_aircraft);
290             return;
291         }
292
293
294     } else {
295         SG_LOG( SG_INPUT, SG_DEBUG, "" );
296         switch (k) {
297         case 104: // h key
298             HUD_masterswitch( true );
299             return;
300         case 105: // i key
301             fgHUDInit(&current_aircraft);  // normal HUD
302             return;
303
304 // START SPECIALS
305
306         case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
307             if ( globals->get_autopilot()->get_HeadingMode() !=
308                  FGAutopilot::FG_HEADING_WAYPOINT ) {
309                 globals->get_autopilot()->set_HeadingMode(
310                     FGAutopilot::FG_HEADING_WAYPOINT );
311                 globals->get_autopilot()->set_HeadingEnabled( true );
312             } else {
313                 globals->get_autopilot()->set_HeadingMode(
314                     FGAutopilot::FG_TC_HEADING_LOCK );
315             }
316             return;
317         case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
318             const string &fog = fgGetString("/sim/rendering/fog");
319             if (fog == "disabled") {
320               fgSetString("/sim/rendering/fog", "fastest");
321               SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
322             } else if (fog == "fastest") {
323               fgSetString("/sim/rendering/fog", "nicest");
324               SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
325             } else if (fog == "nicest") {
326               fgSetString("/sim/rendering/fog", "disabled");
327               SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
328             } else {
329               fgSetString("/sim/rendering/fog", "disabled");
330               SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
331                      << fog << ", changed to 'disabled'");
332             }
333             return;
334         }
335         case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
336             SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
337             guiToggleMenu();
338             return;
339         case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
340             SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
341             NewAltitude( NULL );
342             return;
343         case 256+GLUT_KEY_F12: // F12 Heading Dialog...
344             SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
345             NewHeading( NULL );
346             return;
347         }
348
349 // END SPECIALS
350
351     }
352 }
353
354 void
355 FGInput::doMouseClick (int b, int updown, int x, int y)
356 {
357   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
358
359   mouse &m = _mouse_bindings[0];
360   mouse_mode &mode = m.modes[m.current_mode];
361
362                                 // Pass on to PUI and the panel if
363                                 // requested, and return if one of
364                                 // them consumes the event.
365   if (mode.pass_through) {
366     if (puMouse(b, updown, x, y))
367       return;
368     else if ((current_panel != 0) &&
369              current_panel->doMouseAction(b, updown, x, y))
370       return;
371   }
372
373                                 // OK, PUI and the panel didn't want the click
374   if (b >= MAX_MOUSE_BUTTONS) {
375     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
376            << " where only " << MAX_MOUSE_BUTTONS << " expected");
377     return;
378   }
379
380   _update_button(m.modes[m.current_mode].buttons[b], modifiers, updown, x, y);
381 }
382
383 void
384 FGInput::doMouseMotion (int x, int y)
385 {
386   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
387
388   int xsize = fgGetInt("/sim/startup/xsize", 800);
389   int ysize = fgGetInt("/sim/startup/ysize", 600);
390   mouse &m = _mouse_bindings[0];
391   if (m.current_mode < 0 || m.current_mode >= m.nModes)
392     return;
393   mouse_mode &mode = m.modes[m.current_mode];
394
395                                 // Pass on to PUI if requested, and return
396                                 // if PUI consumed the event.
397   if (mode.pass_through && puMouse(x, y))
398     return;
399
400                                 // OK, PUI didn't want the event,
401                                 // so we can play with it.
402   if (x != m.x) {
403     int delta = x - m.x;
404     for (int i = 0; i < mode.x_bindings[modifiers].size(); i++)
405       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
406   }
407   if (y != m.y) {
408     int delta = y - m.y;
409     for (int i = 0; i < mode.y_bindings[modifiers].size(); i++)
410       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
411   }
412
413                                 // Constrain the mouse if requested
414   if (mode.constrained) {
415     bool need_warp = false;
416     if (x < 0) {
417       x = xsize - 1;
418       need_warp = true;
419     } else if (x >= xsize) {
420       x = 0;
421       need_warp = true;
422     }
423
424     if (y < 0) {
425       y = ysize - 1;
426       need_warp = true;
427     } else if (y >= ysize) {
428       y = 0;
429       need_warp = true;
430     }
431
432     if (need_warp)
433       glutWarpPointer(x, y);
434   }
435   m.x = x;
436   m.y = y;
437 }
438
439 void
440 FGInput::_init_keyboard ()
441 {
442                                 // TODO: zero the old bindings first.
443   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
444   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
445   if (key_nodes == 0) {
446     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
447     key_nodes = fgGetNode("/input/keyboard", true);
448   }
449   
450   vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
451   for (unsigned int i = 0; i < keys.size(); i++) {
452     int index = keys[i]->getIndex();
453     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
454     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
455     _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
456   }
457 }
458
459
460 void
461 FGInput::_init_joystick ()
462 {
463                                 // TODO: zero the old bindings first.
464   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
465   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
466   if (js_nodes == 0) {
467     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
468     js_nodes = fgGetNode("/input/joysticks", true);
469   }
470
471   for (int i = 0; i < MAX_JOYSTICKS; i++) {
472     SGPropertyNode * js_node = js_nodes->getChild("js", i);
473     if (js_node == 0) {
474       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
475       js_node = js_nodes->getChild("js", i, true);
476     }
477     jsJoystick * js = new jsJoystick(i);
478     _joystick_bindings[i].js = js;
479     if (js->notWorking()) {
480       SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
481       continue;
482     }
483 #ifdef WIN32
484     JOYCAPS jsCaps ;
485     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
486     int nbuttons = jsCaps.wNumButtons;
487     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
488 #else
489     int nbuttons = MAX_JOYSTICK_BUTTONS;
490 #endif
491         
492     int naxes = js->getNumAxes();
493     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
494     _joystick_bindings[i].naxes = naxes;
495     _joystick_bindings[i].nbuttons = nbuttons;
496
497     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
498
499                                 // Set up range arrays
500     float minRange[MAX_JOYSTICK_AXES];
501     float maxRange[MAX_JOYSTICK_AXES];
502     float center[MAX_JOYSTICK_AXES];
503
504                                 // Initialize with default values
505     js->getMinRange(minRange);
506     js->getMaxRange(maxRange);
507     js->getCenter(center);
508
509                                 // Allocate axes and buttons
510     _joystick_bindings[i].axes = new axis[naxes];
511     _joystick_bindings[i].buttons = new button[nbuttons];
512
513
514     //
515     // Initialize the axes.
516     //
517     int j;
518     for (j = 0; j < naxes; j++) {
519       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
520       if (axis_node == 0) {
521         SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
522         axis_node = js_node->getChild("axis", j, true);
523       }
524       
525       axis &a = _joystick_bindings[i].axes[j];
526
527       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
528
529       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
530       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
531       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
532       center[j] = axis_node->getDoubleValue("center", center[j]);
533
534       _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
535
536       // Initialize the virtual axis buttons.
537       _init_button(axis_node->getChild("low"), a.low, "low");
538       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
539       
540       _init_button(axis_node->getChild("high"), a.high, "high");
541       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
542     }
543
544     //
545     // Initialize the buttons.
546     //
547     char buf[32];
548     for (j = 0; j < nbuttons; j++) {
549       sprintf(buf, "%d", j);
550       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
551       _init_button(js_node->getChild("button", j),
552                    _joystick_bindings[i].buttons[j],
553                    buf);
554                    
555     }
556
557     js->setMinRange(minRange);
558     js->setMaxRange(maxRange);
559     js->setCenter(center);
560   }
561 }
562
563 // 
564 // Map of all known GLUT cursor names
565 //
566 struct {
567   const char * name;
568   int cursor;
569 } mouse_cursor_map[] = {
570   { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
571   { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
572   { "info", GLUT_CURSOR_INFO },
573   { "destroy", GLUT_CURSOR_DESTROY },
574   { "help", GLUT_CURSOR_HELP },
575   { "cycle", GLUT_CURSOR_CYCLE },
576   { "spray", GLUT_CURSOR_SPRAY },
577   { "wait", GLUT_CURSOR_WAIT },
578   { "text", GLUT_CURSOR_TEXT },
579   { "crosshair", GLUT_CURSOR_CROSSHAIR },
580   { "up-down", GLUT_CURSOR_UP_DOWN },
581   { "left-right", GLUT_CURSOR_LEFT_RIGHT },
582   { "top-side", GLUT_CURSOR_TOP_SIDE },
583   { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
584   { "left-side", GLUT_CURSOR_LEFT_SIDE },
585   { "right-side", GLUT_CURSOR_RIGHT_SIDE },
586   { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
587   { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
588   { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
589   { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
590   { "inherit", GLUT_CURSOR_INHERIT },
591   { "none", GLUT_CURSOR_NONE },
592   { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
593   { 0, 0 }
594 };
595
596
597
598 void
599 FGInput::_init_mouse ()
600 {
601   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
602
603   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
604   if (mouse_nodes == 0) {
605     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
606     mouse_nodes = fgGetNode("/input/mice", true);
607   }
608
609   int j;
610   for (int i = 0; i < MAX_MICE; i++) {
611     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
612     mouse &m = _mouse_bindings[i];
613
614                                 // Read all the modes
615     m.nModes = mouse_node->getIntValue("mode-count", 1);
616     m.modes = new mouse_mode[m.nModes];
617
618     for (int j = 0; j < m.nModes; j++) {
619       int k;
620
621                                 // Read the mouse cursor for this mode
622       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
623       const char * cursor_name =
624         mode_node->getStringValue("cursor", "inherit");
625       m.modes[j].cursor = GLUT_CURSOR_INHERIT;
626       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
627         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
628           m.modes[j].cursor = mouse_cursor_map[k].cursor;
629           break;
630         }
631       }
632
633                                 // Read other properties for this mode
634       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
635       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
636
637                                 // Read the button bindings for this mode
638       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
639       char buf[8];
640       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
641         sprintf(buf, "mouse button %d", k);
642         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
643         _init_button(mode_node->getChild("button", k),
644                      m.modes[j].buttons[k],
645                      buf);
646       }
647
648                                 // Read the axis bindings for this mode
649       _read_bindings(mode_node->getChild("x-axis", 0, true),
650                      m.modes[j].x_bindings,
651                      FG_MOD_NONE);
652       _read_bindings(mode_node->getChild("y-axis", 0, true),
653                      m.modes[j].y_bindings,
654                      FG_MOD_NONE);
655     }
656   }
657 }
658
659
660 void
661 FGInput::_init_button (const SGPropertyNode * node,
662                        button &b,
663                        const string name)
664 {       
665   if (node == 0) {
666     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
667   } else {
668     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
669     
670                 // Get the bindings for the button
671     _read_bindings(node, b.bindings, FG_MOD_NONE);
672   }
673 }
674
675
676 void
677 FGInput::_update_keyboard ()
678 {
679   // no-op
680 }
681
682
683 void
684 FGInput::_update_joystick ()
685 {
686   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
687   int buttons;
688   // float js_val, diff;
689   float axis_values[MAX_JOYSTICK_AXES];
690
691   int i;
692   int j;
693
694   for ( i = 0; i < MAX_JOYSTICKS; i++) {
695
696     jsJoystick * js = _joystick_bindings[i].js;
697     if (js == 0 || js->notWorking())
698       continue;
699
700     js->read(&buttons, axis_values);
701
702
703                                 // Fire bindings for the axes.
704     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
705       axis &a = _joystick_bindings[i].axes[j];
706       
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_DEBUG, "Axis " << j << " has moved");
712         SGPropertyNode node;
713         a.last_value = axis_values[j];
714 //      SG_LOG(SG_INPUT, SG_DEBUG, "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]);
718       }
719      
720                                 // do we have to emulate axis buttons?
721       if (a.low.bindings[modifiers].size())
722         _update_button(_joystick_bindings[i].axes[j].low,
723                        modifiers,
724                        axis_values[j] < a.low_threshold,
725                        -1, -1);
726       
727       if (a.high.bindings[modifiers].size())
728         _update_button(_joystick_bindings[i].axes[j].high,
729                        modifiers,
730                        axis_values[j] > a.high_threshold,
731                        -1, -1);
732     }
733
734                                 // Fire bindings for the buttons.
735     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
736       _update_button(_joystick_bindings[i].buttons[j],
737                      modifiers,
738                      (buttons & (1 << j)) > 0,
739                      -1, -1);
740     }
741   }
742 }
743
744 void
745 FGInput::_update_mouse ()
746 {
747   mouse &m = _mouse_bindings[0];
748   int mode =  fgGetInt("/input/mice/mouse[0]/mode");
749   if (mode != m.current_mode) {
750     m.current_mode = mode;
751     if (mode >= 0 && mode < m.nModes) {
752       glutSetCursor(m.modes[mode].cursor);
753     } else {
754       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
755       glutSetCursor(GLUT_CURSOR_INHERIT);
756     }
757   }
758 }
759
760 void
761 FGInput::_update_button (button &b, int modifiers, bool pressed,
762                          int x, int y)
763 {
764   if (pressed) {
765                                 // The press event may be repeated.
766     if (!b.last_state || b.is_repeatable) {
767       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
768       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
769         b.bindings[modifiers][k]->fire(x, y);
770     }
771   } else {
772                                 // The release event is never repeated.
773     if (b.last_state) {
774       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
775       for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
776         b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
777     }
778   }
779           
780   b.last_state = pressed;
781 }  
782
783
784 void
785 FGInput::_read_bindings (const SGPropertyNode * node, 
786                          binding_list_t * binding_list,
787                          int modifiers)
788 {
789   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
790   vector<const SGPropertyNode *> bindings = node->getChildren("binding");
791   for (unsigned int i = 0; i < bindings.size(); i++) {
792     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
793            << bindings[i]->getStringValue("command"));
794     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
795   }
796
797                                 // Read nested bindings for modifiers
798   if (node->getChild("mod-up") != 0)
799     _read_bindings(node->getChild("mod-up"), binding_list,
800                    modifiers|FG_MOD_UP);
801
802   if (node->getChild("mod-shift") != 0)
803     _read_bindings(node->getChild("mod-shift"), binding_list,
804                    modifiers|FG_MOD_SHIFT);
805
806   if (node->getChild("mod-ctrl") != 0)
807     _read_bindings(node->getChild("mod-ctrl"), binding_list,
808                    modifiers|FG_MOD_CTRL);
809
810   if (node->getChild("mod-alt") != 0)
811     _read_bindings(node->getChild("mod-alt"), binding_list,
812                    modifiers|FG_MOD_ALT);
813 }
814
815
816 const vector<FGBinding *> &
817 FGInput::_find_key_bindings (unsigned int k, int modifiers)
818 {
819   button &b = _key_bindings[k];
820
821                                 // Try it straight, first.
822   if (b.bindings[modifiers].size() > 0)
823     return b.bindings[modifiers];
824
825                                 // Try removing the control modifier
826                                 // for control keys.
827   else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
828     return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
829
830                                 // Try removing shift modifier 
831                                 // for upper case or any punctuation
832                                 // (since different keyboards will
833                                 // shift different punctuation types)
834   else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
835     return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
836
837                                 // Try removing alt modifier for
838                                 // high-bit characters.
839   else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
840     return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
841
842                                 // Give up and return the empty vector.
843   else
844     return b.bindings[modifiers];
845 }
846
847
848 \f
849 ////////////////////////////////////////////////////////////////////////
850 // Implementation of FGInput::button.
851 ////////////////////////////////////////////////////////////////////////
852
853 FGInput::button::button ()
854   : is_repeatable(false),
855     last_state(-1)
856 {
857 }
858
859 FGInput::button::~button ()
860 {
861                                 // FIXME: memory leak
862 //   for (int i = 0; i < FG_MOD_MAX; i++)
863 //     for (int j = 0; i < bindings[i].size(); j++)
864 //       delete bindings[i][j];
865 }
866
867
868 \f
869 ////////////////////////////////////////////////////////////////////////
870 // Implementation of FGInput::axis.
871 ////////////////////////////////////////////////////////////////////////
872
873 FGInput::axis::axis ()
874   : last_value(9999999),
875     tolerance(0.002),
876     low_threshold(-0.9),
877     high_threshold(0.9)
878 {
879 }
880
881 FGInput::axis::~axis ()
882 {
883 //   for (int i = 0; i < FG_MOD_MAX; i++)
884 //     for (int j = 0; i < bindings[i].size(); j++)
885 //       delete bindings[i][j];
886 }
887
888
889 \f
890 ////////////////////////////////////////////////////////////////////////
891 // Implementation of FGInput::joystick.
892 ////////////////////////////////////////////////////////////////////////
893
894 FGInput::joystick::joystick ()
895 {
896 }
897
898 FGInput::joystick::~joystick ()
899 {
900 //   delete js;
901   delete[] axes;
902   delete[] buttons;
903 }
904
905
906 \f
907 ////////////////////////////////////////////////////////////////////////
908 // Implementation of FGInput::mouse_mode
909 ////////////////////////////////////////////////////////////////////////
910
911 FGInput::mouse_mode::mouse_mode ()
912   : cursor(GLUT_CURSOR_INHERIT),
913     constrained(false),
914     pass_through(false),
915     buttons(0)
916 {
917 }
918
919 FGInput::mouse_mode::~mouse_mode ()
920 {
921                                 // FIXME: memory leak
922 //   for (int i = 0; i < FG_MOD_MAX; i++) {
923 //     int j;
924 //     for (j = 0; i < x_bindings[i].size(); j++)
925 //       delete bindings[i][j];
926 //     for (j = 0; j < y_bindings[i].size(); j++)
927 //       delete bindings[i][j];
928 //   }
929   delete [] buttons;
930 }
931
932
933 \f
934 ////////////////////////////////////////////////////////////////////////
935 // Implementation of FGInput::mouse
936 ////////////////////////////////////////////////////////////////////////
937
938 FGInput::mouse::mouse ()
939   : nModes(1),
940     current_mode(0),
941     x(-1),
942     y(-1),
943     modes(0)
944 {
945 }
946
947 FGInput::mouse::~mouse ()
948 {
949   delete [] modes;
950 }
951
952
953 \f
954 ////////////////////////////////////////////////////////////////////////
955 // Implementation of GLUT callbacks.
956 ////////////////////////////////////////////////////////////////////////
957
958
959 /**
960  * Construct the modifiers.
961  */
962 static inline int get_mods ()
963 {
964   int glut_modifiers = glutGetModifiers();
965   int modifiers = 0;
966
967   if (glut_modifiers & GLUT_ACTIVE_SHIFT)
968     modifiers |= FGInput::FG_MOD_SHIFT;
969   if (glut_modifiers & GLUT_ACTIVE_CTRL)
970     modifiers |= FGInput::FG_MOD_CTRL;
971   if (glut_modifiers & GLUT_ACTIVE_ALT)
972     modifiers |= FGInput::FG_MOD_ALT;
973
974   return modifiers;
975 }
976
977
978 \f
979 ////////////////////////////////////////////////////////////////////////
980 // GLUT C callbacks.
981 ////////////////////////////////////////////////////////////////////////
982
983 void
984 GLUTkey(unsigned char k, int x, int y)
985 {
986                                 // Give PUI a chance to grab it first.
987   if (!puKeyboard(k, PU_DOWN))
988     current_input.doKey(k, get_mods(), x, y);
989 }
990
991 void
992 GLUTkeyup(unsigned char k, int x, int y)
993 {
994   current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
995 }
996
997 void
998 GLUTspecialkey(int k, int x, int y)
999 {
1000                                 // Give PUI a chance to grab it first.
1001   if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
1002     current_input.doKey(k + 256, get_mods(), x, y);
1003 }
1004
1005 void
1006 GLUTspecialkeyup(int k, int x, int y)
1007 {
1008   current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
1009 }
1010
1011 void
1012 GLUTmouse (int button, int updown, int x, int y)
1013 {
1014   current_input.doMouseClick(button, updown, x, y);
1015 }
1016
1017 void
1018 GLUTmotion (int x, int y)
1019 {
1020   current_input.doMouseMotion(x, y);
1021 }
1022
1023 // end of input.cxx