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