]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
349226a0302afae27b8d959e2b96119491ef3105
[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 (int x, int y) const
147 {
148   if (test()) {
149     if (x >= 0)
150       _arg->setIntValue("x-pos", x);
151     if (y >= 0)
152       _arg->setIntValue("y-pos", y);
153     fire();
154   }
155 }
156
157 void
158 FGBinding::fire (double setting) const
159 {
160   if (test()) {
161                                 // A value is automatically added to
162                                 // the args
163     if (_setting == 0)          // save the setting node for efficiency
164       _setting = _arg->getChild("setting", 0, true);
165     _setting->setDoubleValue(setting);
166     fire();
167   }
168 }
169
170
171 \f
172 ////////////////////////////////////////////////////////////////////////
173 // Implementation of FGInput.
174 ////////////////////////////////////////////////////////////////////////
175
176                                 // From main.cxx
177 extern void fgReshape( int width, int height );
178
179 FGInput current_input;
180
181
182 FGInput::FGInput ()
183   : _current_mouse_mode(-1),
184     _last_mouse_mode(-1)
185 {
186   // no op
187 }
188
189 FGInput::~FGInput ()
190 {
191   // no op
192 }
193
194 void
195 FGInput::init ()
196 {
197   _init_keyboard();
198   _init_joystick();
199   _init_mouse();
200 }
201
202 void
203 FGInput::bind ()
204 {
205   // no op
206 }
207
208 void
209 FGInput::unbind ()
210 {
211   // no op
212 }
213
214 void 
215 FGInput::update (int dt)
216 {
217   _update_keyboard();
218   _update_joystick();
219   _update_mouse();
220 }
221
222 void
223 FGInput::doKey (int k, int modifiers, int x, int y)
224 {
225   SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
226           << " with modifiers " << modifiers );
227
228                                 // Sanity check.
229   if (k < 0 || k >= MAX_KEYS) {
230     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
231     return;
232   }
233
234   button &b = _key_bindings[k];
235
236                                 // Key pressed.
237   if (modifiers&FG_MOD_UP == 0) {
238     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
239             << " with modifiers " << modifiers );
240     if (!b.last_state || b.is_repeatable) {
241       const binding_list_t &bindings =
242         _find_key_bindings(k, modifiers);
243       int max = bindings.size();
244       if (max > 0) {
245         for (int i = 0; i < max; i++)
246           bindings[i]->fire();
247         return;
248       }
249     }
250   }
251
252                                 // Key released.
253   else {
254     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
255            << " with modifiers " << modifiers);
256     if (b.last_state) {
257       const binding_list_t &bindings =
258         _find_key_bindings(k, modifiers);
259       int max = bindings.size();
260       if (max > 0) {
261         for (int i = 0; i < max; i++)
262           bindings[i]->fire();
263         return;
264       }
265     }
266   }
267
268
269                                 // Use the old, default actions.
270   SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
271   if (modifiers&FG_MOD_UP)
272     return;
273
274   // everything after here will be removed sooner or later...
275
276   if (modifiers & FG_MOD_SHIFT) {
277
278         switch (k) {
279         case 72: // H key
280             HUD_brightkey( true );
281             return;
282         case 73: // I key
283             // Minimal Hud
284             fgHUDInit2(&current_aircraft);
285             return;
286         }
287
288
289     } else {
290         SG_LOG( SG_INPUT, SG_DEBUG, "" );
291         switch (k) {
292         case 104: // h key
293             HUD_masterswitch( true );
294             return;
295         case 105: // i key
296             fgHUDInit(&current_aircraft);  // normal HUD
297             return;
298
299 // START SPECIALS
300
301         case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
302             if ( globals->get_autopilot()->get_HeadingMode() !=
303                  FGAutopilot::FG_HEADING_WAYPOINT ) {
304                 globals->get_autopilot()->set_HeadingMode(
305                     FGAutopilot::FG_HEADING_WAYPOINT );
306                 globals->get_autopilot()->set_HeadingEnabled( true );
307             } else {
308                 globals->get_autopilot()->set_HeadingMode(
309                     FGAutopilot::FG_TC_HEADING_LOCK );
310             }
311             return;
312         case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
313             const string &fog = fgGetString("/sim/rendering/fog");
314             if (fog == "disabled") {
315               fgSetString("/sim/rendering/fog", "fastest");
316               SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
317             } else if (fog == "fastest") {
318               fgSetString("/sim/rendering/fog", "nicest");
319               SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
320             } else if (fog == "nicest") {
321               fgSetString("/sim/rendering/fog", "disabled");
322               SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
323             } else {
324               fgSetString("/sim/rendering/fog", "disabled");
325               SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
326                      << fog << ", changed to 'disabled'");
327             }
328             return;
329         }
330         case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
331             SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
332             guiToggleMenu();
333             return;
334         case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
335             SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
336             NewAltitude( NULL );
337             return;
338         case 256+GLUT_KEY_F12: // F12 Heading Dialog...
339             SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
340             NewHeading( NULL );
341             return;
342         }
343
344 // END SPECIALS
345
346     }
347 }
348
349 void
350 FGInput::doMouseClick (int b, int updown, int x, int y)
351 {
352   std::cout << "Mouse click " << b << ',' << updown << std::endl;
353   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
354
355   if (b >= MAX_MOUSE_BUTTONS) {
356     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
357            << " where only " << MAX_MOUSE_BUTTONS << " expected");
358     return;
359   }
360
361   _update_button(_mouse_bindings[0].buttons[b], modifiers, updown, x, y);
362 }
363
364 void
365 FGInput::doMouseMotion (int x, int y)
366 {
367   // TODO
368 }
369
370 void
371 FGInput::_init_keyboard ()
372 {
373                                 // TODO: zero the old bindings first.
374   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
375   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
376   if (key_nodes == 0) {
377     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
378     key_nodes = fgGetNode("/input/keyboard", true);
379   }
380   
381   vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
382   for (unsigned int i = 0; i < keys.size(); i++) {
383     int index = keys[i]->getIndex();
384     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
385     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
386     _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
387   }
388 }
389
390
391 void
392 FGInput::_init_joystick ()
393 {
394                                 // TODO: zero the old bindings first.
395   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
396   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
397   if (js_nodes == 0) {
398     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
399     js_nodes = fgGetNode("/input/joysticks", true);
400   }
401
402   for (int i = 0; i < MAX_JOYSTICKS; i++) {
403     SGPropertyNode * js_node = js_nodes->getChild("js", i);
404     if (js_node == 0) {
405       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
406       js_node = js_nodes->getChild("js", i, true);
407     }
408     jsJoystick * js = new jsJoystick(i);
409     _joystick_bindings[i].js = js;
410     if (js->notWorking()) {
411       SG_LOG(SG_INPUT, SG_WARN, "Joystick " << i << " not found");
412       continue;
413     }
414 #ifdef WIN32
415     JOYCAPS jsCaps ;
416     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
417     int nbuttons = jsCaps.wNumButtons;
418     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
419 #else
420     int nbuttons = MAX_JOYSTICK_BUTTONS;
421 #endif
422         
423     int naxes = js->getNumAxes();
424     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
425     _joystick_bindings[i].naxes = naxes;
426     _joystick_bindings[i].nbuttons = nbuttons;
427
428     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
429
430                                 // Set up range arrays
431     float minRange[MAX_JOYSTICK_AXES];
432     float maxRange[MAX_JOYSTICK_AXES];
433     float center[MAX_JOYSTICK_AXES];
434
435                                 // Initialize with default values
436     js->getMinRange(minRange);
437     js->getMaxRange(maxRange);
438     js->getCenter(center);
439
440                                 // Allocate axes and buttons
441     _joystick_bindings[i].axes = new axis[naxes];
442     _joystick_bindings[i].buttons = new button[nbuttons];
443
444
445     //
446     // Initialize the axes.
447     //
448     int j;
449     for (j = 0; j < naxes; j++) {
450       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
451       if (axis_node == 0) {
452         SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
453         axis_node = js_node->getChild("axis", j, true);
454       }
455       
456       axis &a = _joystick_bindings[i].axes[j];
457
458       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
459
460       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
461       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
462       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
463       center[j] = axis_node->getDoubleValue("center", center[j]);
464
465       _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
466
467       // Initialize the virtual axis buttons.
468       _init_button(axis_node->getChild("low"), a.low, "low");
469       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
470       
471       _init_button(axis_node->getChild("high"), a.high, "high");
472       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
473     }
474
475     //
476     // Initialize the buttons.
477     //
478     char buf[8];
479     for (j = 0; j < nbuttons; j++) {
480       sprintf(buf, "%d", j);
481       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
482       _init_button(js_node->getChild("button", j),
483                    _joystick_bindings[i].buttons[j],
484                    buf);
485                    
486     }
487
488     js->setMinRange(minRange);
489     js->setMaxRange(maxRange);
490     js->setCenter(center);
491   }
492 }
493
494 // 
495 // Map of all known GLUT cursor names
496 //
497 struct {
498   const char * name;
499   int cursor;
500 } mouse_cursor_map[] = {
501   { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
502   { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
503   { "info", GLUT_CURSOR_INFO },
504   { "destroy", GLUT_CURSOR_DESTROY },
505   { "help", GLUT_CURSOR_HELP },
506   { "cycle", GLUT_CURSOR_CYCLE },
507   { "spray", GLUT_CURSOR_SPRAY },
508   { "wait", GLUT_CURSOR_WAIT },
509   { "text", GLUT_CURSOR_TEXT },
510   { "crosshair", GLUT_CURSOR_CROSSHAIR },
511   { "up-down", GLUT_CURSOR_UP_DOWN },
512   { "left-right", GLUT_CURSOR_LEFT_RIGHT },
513   { "top-side", GLUT_CURSOR_TOP_SIDE },
514   { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
515   { "left-side", GLUT_CURSOR_LEFT_SIDE },
516   { "right-side", GLUT_CURSOR_RIGHT_SIDE },
517   { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
518   { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
519   { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
520   { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
521   { "inherit", GLUT_CURSOR_INHERIT },
522   { "none", GLUT_CURSOR_NONE },
523   { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
524   { 0, 0 }
525 };
526
527
528
529 void
530 FGInput::_init_mouse ()
531 {
532   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
533
534   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
535   if (mouse_nodes == 0) {
536     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
537     mouse_nodes = fgGetNode("/input/mice", true);
538   }
539
540   int j;
541   for (int i = 0; i < MAX_MICE; i++) {
542     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i);
543
544                                 // Read the cursor type for each mode.
545     _mouse_bindings[i].nModes = mouse_node->getIntValue("mode-count", 1);
546     _mouse_bindings[i].cursors = new int[_mouse_bindings[i].nModes];
547     SGPropertyNode * cursor_nodes =
548       mouse_node->getChild("mode-cursors", 0, true);
549     for (j = 0; j < _mouse_bindings[i].nModes; j++) {
550       const char * name = cursor_nodes->getChild("cursor", j, true)
551         ->getStringValue();
552       if (name[0] == '\0')
553         name = "inherit";
554       _mouse_bindings[i].cursors[j] = GLUT_CURSOR_INHERIT;
555       for (int k = 0; mouse_cursor_map[k].name != 0; k++) {
556         if (!strcmp(mouse_cursor_map[k].name, name)) {
557           _mouse_bindings[i].cursors[j] = mouse_cursor_map[k].cursor;
558           break;
559         }
560       }
561     }
562
563                                 // Read the binding for each button
564     _mouse_bindings[i].buttons = new button[MAX_MOUSE_BUTTONS];
565     if (mouse_node == 0) {
566       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for mouse " << i);
567       mouse_node = mouse_nodes->getChild("mouse", i, true);
568     }
569     char buf[8];
570     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
571       sprintf(buf, "%d", j);
572       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << j);
573       _init_button(mouse_node->getChild("button", j),
574                    _mouse_bindings[i].buttons[j],
575                    buf);
576     }
577   }
578 }
579
580
581 void
582 FGInput::_init_button (const SGPropertyNode * node,
583                        button &b,
584                        const string name)
585 {       
586   if (node == 0) {
587     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
588   } else {
589     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
590     
591                 // Get the bindings for the button
592     _read_bindings(node, b.bindings, FG_MOD_NONE);
593   }
594 }
595
596
597 void
598 FGInput::_update_keyboard ()
599 {
600   // no-op
601 }
602
603
604 void
605 FGInput::_update_joystick ()
606 {
607   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
608   int buttons;
609   // float js_val, diff;
610   float axis_values[MAX_JOYSTICK_AXES];
611
612   int i;
613   int j;
614
615   for ( i = 0; i < MAX_JOYSTICKS; i++) {
616
617     jsJoystick * js = _joystick_bindings[i].js;
618     if (js == 0 || js->notWorking())
619       continue;
620
621     js->read(&buttons, axis_values);
622
623
624                                 // Fire bindings for the axes.
625     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
626       axis &a = _joystick_bindings[i].axes[j];
627       
628                                 // Do nothing if the axis position
629                                 // is unchanged; only a change in
630                                 // position fires the bindings.
631       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
632 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
633         SGPropertyNode node;
634         a.last_value = axis_values[j];
635 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
636 //             << a.bindings[modifiers].size() << " bindings");
637         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
638           a.bindings[modifiers][k]->fire(axis_values[j]);
639       }
640      
641                                 // do we have to emulate axis buttons?
642       if (a.low.bindings[modifiers].size())
643         _update_button(_joystick_bindings[i].axes[j].low,
644                        modifiers,
645                        axis_values[j] < a.low_threshold,
646                        -1, -1);
647       
648       if (a.high.bindings[modifiers].size())
649         _update_button(_joystick_bindings[i].axes[j].high,
650                        modifiers,
651                        axis_values[j] > a.high_threshold,
652                        -1, -1);
653     }
654
655                                 // Fire bindings for the buttons.
656     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
657       _update_button(_joystick_bindings[i].buttons[j],
658                      modifiers,
659                      (buttons & (1 << j)) > 0,
660                      -1, -1);
661     }
662   }
663 }
664
665 void
666 FGInput::_update_mouse ()
667 {
668   _current_mouse_mode = fgGetInt("/input/mice/mouse[0]/mode");
669   if (_current_mouse_mode != _last_mouse_mode) {
670     _last_mouse_mode = _current_mouse_mode;
671     if (_current_mouse_mode >= 0
672         && _current_mouse_mode < _mouse_bindings[0].nModes) {
673       glutSetCursor(_mouse_bindings[0].cursors[_current_mouse_mode]);
674     } else {
675       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode "
676              << _current_mouse_mode << " out of range");
677       glutSetCursor(GLUT_CURSOR_INHERIT);
678     }
679   }
680 }
681
682 void
683 FGInput::_update_button (button &b, int modifiers, bool pressed,
684                          int x, int y)
685 {
686   if (pressed) {
687                                 // The press event may be repeated.
688     if (!b.last_state || b.is_repeatable) {
689       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
690       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
691         b.bindings[modifiers][k]->fire(x, y);
692     }
693   } else {
694                                 // The release event is never repeated.
695     if (b.last_state) {
696       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
697       for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
698         b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
699     }
700   }
701           
702   b.last_state = pressed;
703 }  
704
705
706 void
707 FGInput::_read_bindings (const SGPropertyNode * node, 
708                          binding_list_t * binding_list,
709                          int modifiers)
710 {
711   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
712   vector<const SGPropertyNode *> bindings = node->getChildren("binding");
713   for (unsigned int i = 0; i < bindings.size(); i++) {
714     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
715            << bindings[i]->getStringValue("command"));
716     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
717   }
718
719                                 // Read nested bindings for modifiers
720   if (node->getChild("mod-up") != 0)
721     _read_bindings(node->getChild("mod-up"), binding_list,
722                    modifiers|FG_MOD_UP);
723
724   if (node->getChild("mod-shift") != 0)
725     _read_bindings(node->getChild("mod-shift"), binding_list,
726                    modifiers|FG_MOD_SHIFT);
727
728   if (node->getChild("mod-ctrl") != 0)
729     _read_bindings(node->getChild("mod-ctrl"), binding_list,
730                    modifiers|FG_MOD_CTRL);
731
732   if (node->getChild("mod-alt") != 0)
733     _read_bindings(node->getChild("mod-alt"), binding_list,
734                    modifiers|FG_MOD_ALT);
735 }
736
737
738 const vector<FGBinding *> &
739 FGInput::_find_key_bindings (unsigned int k, int modifiers)
740 {
741   button &b = _key_bindings[k];
742
743                                 // Try it straight, first.
744   if (b.bindings[modifiers].size() > 0)
745     return b.bindings[modifiers];
746
747                                 // Try removing the control modifier
748                                 // for control keys.
749   else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
750     return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
751
752                                 // Try removing shift modifier 
753                                 // for upper case or any punctuation
754                                 // (since different keyboards will
755                                 // shift different punctuation types)
756   else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
757     return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
758
759                                 // Try removing alt modifier for
760                                 // high-bit characters.
761   else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
762     return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
763
764                                 // Give up and return the empty vector.
765   else
766     return b.bindings[modifiers];
767 }
768
769
770 \f
771 ////////////////////////////////////////////////////////////////////////
772 // Implementation of FGInput::button.
773 ////////////////////////////////////////////////////////////////////////
774
775 FGInput::button::button ()
776   : is_repeatable(false),
777     last_state(-1)
778 {
779 }
780
781 FGInput::button::~button ()
782 {
783                                 // FIXME: memory leak
784 //   for (int i = 0; i < FG_MOD_MAX; i++)
785 //     for (int j = 0; i < bindings[i].size(); j++)
786 //       delete bindings[i][j];
787 }
788
789
790 \f
791 ////////////////////////////////////////////////////////////////////////
792 // Implementation of FGInput::axis.
793 ////////////////////////////////////////////////////////////////////////
794
795 FGInput::axis::axis ()
796   : last_value(9999999),
797     tolerance(0.002),
798     low_threshold(-0.9),
799     high_threshold(0.9)
800 {
801 }
802
803 FGInput::axis::~axis ()
804 {
805 //   for (int i = 0; i < FG_MOD_MAX; i++)
806 //     for (int j = 0; i < bindings[i].size(); j++)
807 //       delete bindings[i][j];
808 }
809
810
811 \f
812 ////////////////////////////////////////////////////////////////////////
813 // Implementation of FGInput::joystick.
814 ////////////////////////////////////////////////////////////////////////
815
816 FGInput::joystick::joystick ()
817 {
818 }
819
820 FGInput::joystick::~joystick ()
821 {
822 //   delete js;
823   delete[] axes;
824   delete[] buttons;
825 }
826
827
828 \f
829 ////////////////////////////////////////////////////////////////////////
830 // Implementation of FGInput::mouse
831 ////////////////////////////////////////////////////////////////////////
832
833 FGInput::mouse::mouse ()
834   : nModes(0),
835     cursors(0),
836     buttons(0)
837 {
838 }
839
840 FGInput::mouse::~mouse ()
841 {
842   delete [] cursors;
843   delete [] buttons;
844 }
845
846
847 \f
848 ////////////////////////////////////////////////////////////////////////
849 // Implementation of GLUT callbacks.
850 ////////////////////////////////////////////////////////////////////////
851
852
853 /**
854  * Construct the modifiers.
855  */
856 static inline int get_mods ()
857 {
858   int glut_modifiers = glutGetModifiers();
859   int modifiers = 0;
860
861   if (glut_modifiers & GLUT_ACTIVE_SHIFT)
862     modifiers |= FGInput::FG_MOD_SHIFT;
863   if (glut_modifiers & GLUT_ACTIVE_CTRL)
864     modifiers |= FGInput::FG_MOD_CTRL;
865   if (glut_modifiers & GLUT_ACTIVE_ALT)
866     modifiers |= FGInput::FG_MOD_ALT;
867
868   return modifiers;
869 }
870
871
872 \f
873 ////////////////////////////////////////////////////////////////////////
874 // GLUT C callbacks.
875 ////////////////////////////////////////////////////////////////////////
876
877 void
878 GLUTkey(unsigned char k, int x, int y)
879 {
880                                 // Give PUI a chance to grab it first.
881   if (!puKeyboard(k, PU_DOWN))
882     current_input.doKey(k, get_mods(), x, y);
883 }
884
885 void
886 GLUTkeyup(unsigned char k, int x, int y)
887 {
888   current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
889 }
890
891 void
892 GLUTspecialkey(int k, int x, int y)
893 {
894                                 // Give PUI a chance to grab it first.
895   if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
896     current_input.doKey(k + 256, get_mods(), x, y);
897 }
898
899 void
900 GLUTspecialkeyup(int k, int x, int y)
901 {
902   current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
903 }
904
905 void
906 GLUTmouse (int button, int updown, int x, int y)
907 {
908   current_input.doMouseClick(button, updown == GLUT_DOWN, x, y);
909 }
910
911 void
912 GLUTmotion (int x, int y)
913 {
914   puMouse(x, y);
915 //   glutPostRedisplay();
916   current_input.doMouseMotion(x, y);
917 }
918
919 // end of input.cxx