]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
20024c4d291b5d43cf9c786719931bd77e9924bf
[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
361   if (b >= MAX_MOUSE_BUTTONS) {
362     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
363            << " where only " << MAX_MOUSE_BUTTONS << " expected");
364     return;
365   }
366
367   _update_button(m.modes[m.current_mode].buttons[b], modifiers, updown, x, y);
368 }
369
370 void
371 FGInput::doMouseMotion (int x, int y)
372 {
373   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
374
375   int xsize = fgGetInt("/sim/startup/xsize", 800);
376   int ysize = fgGetInt("/sim/startup/ysize", 600);
377   mouse &m = _mouse_bindings[0];
378   if (m.current_mode < 0 || m.current_mode >= m.nModes)
379     return;
380   mouse_mode &mode = m.modes[m.current_mode];
381   if (x != m.x) {
382     int delta = x - m.x;
383     for (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 (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 - 1;
397       need_warp = true;
398     } else if (x >= xsize) {
399       x = 0;
400       need_warp = true;
401     }
402
403     if (y < 0) {
404       y = ysize - 1;
405       need_warp = true;
406     } else if (y >= ysize) {
407       y = 0;
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 *> 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 * 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     }
462 #ifdef WIN32
463     JOYCAPS jsCaps ;
464     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
465     int nbuttons = jsCaps.wNumButtons;
466     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
467 #else
468     int nbuttons = MAX_JOYSTICK_BUTTONS;
469 #endif
470         
471     int naxes = js->getNumAxes();
472     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
473     _joystick_bindings[i].naxes = naxes;
474     _joystick_bindings[i].nbuttons = nbuttons;
475
476     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
477
478                                 // Set up range arrays
479     float minRange[MAX_JOYSTICK_AXES];
480     float maxRange[MAX_JOYSTICK_AXES];
481     float center[MAX_JOYSTICK_AXES];
482
483                                 // Initialize with default values
484     js->getMinRange(minRange);
485     js->getMaxRange(maxRange);
486     js->getCenter(center);
487
488                                 // Allocate axes and buttons
489     _joystick_bindings[i].axes = new axis[naxes];
490     _joystick_bindings[i].buttons = new button[nbuttons];
491
492
493     //
494     // Initialize the axes.
495     //
496     int j;
497     for (j = 0; j < naxes; j++) {
498       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
499       if (axis_node == 0) {
500         SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
501         axis_node = js_node->getChild("axis", j, true);
502       }
503       
504       axis &a = _joystick_bindings[i].axes[j];
505
506       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
507
508       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
509       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
510       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
511       center[j] = axis_node->getDoubleValue("center", center[j]);
512
513       _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
514
515       // Initialize the virtual axis buttons.
516       _init_button(axis_node->getChild("low"), a.low, "low");
517       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
518       
519       _init_button(axis_node->getChild("high"), a.high, "high");
520       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
521     }
522
523     //
524     // Initialize the buttons.
525     //
526     char buf[8];
527     for (j = 0; j < nbuttons; j++) {
528       sprintf(buf, "%d", j);
529       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
530       _init_button(js_node->getChild("button", j),
531                    _joystick_bindings[i].buttons[j],
532                    buf);
533                    
534     }
535
536     js->setMinRange(minRange);
537     js->setMaxRange(maxRange);
538     js->setCenter(center);
539   }
540 }
541
542 // 
543 // Map of all known GLUT cursor names
544 //
545 struct {
546   const char * name;
547   int cursor;
548 } mouse_cursor_map[] = {
549   { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
550   { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
551   { "info", GLUT_CURSOR_INFO },
552   { "destroy", GLUT_CURSOR_DESTROY },
553   { "help", GLUT_CURSOR_HELP },
554   { "cycle", GLUT_CURSOR_CYCLE },
555   { "spray", GLUT_CURSOR_SPRAY },
556   { "wait", GLUT_CURSOR_WAIT },
557   { "text", GLUT_CURSOR_TEXT },
558   { "crosshair", GLUT_CURSOR_CROSSHAIR },
559   { "up-down", GLUT_CURSOR_UP_DOWN },
560   { "left-right", GLUT_CURSOR_LEFT_RIGHT },
561   { "top-side", GLUT_CURSOR_TOP_SIDE },
562   { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
563   { "left-side", GLUT_CURSOR_LEFT_SIDE },
564   { "right-side", GLUT_CURSOR_RIGHT_SIDE },
565   { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
566   { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
567   { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
568   { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
569   { "inherit", GLUT_CURSOR_INHERIT },
570   { "none", GLUT_CURSOR_NONE },
571   { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
572   { 0, 0 }
573 };
574
575
576
577 void
578 FGInput::_init_mouse ()
579 {
580   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
581
582   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
583   if (mouse_nodes == 0) {
584     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
585     mouse_nodes = fgGetNode("/input/mice", true);
586   }
587
588   int j;
589   for (int i = 0; i < MAX_MICE; i++) {
590     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
591     mouse &m = _mouse_bindings[i];
592
593                                 // Read all the modes
594     m.nModes = mouse_node->getIntValue("mode-count", 1);
595     m.modes = new mouse_mode[m.nModes];
596
597     for (int j = 0; j < m.nModes; j++) {
598       int k;
599
600                                 // Read the mouse cursor for this mode
601       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
602       const char * cursor_name =
603         mode_node->getStringValue("cursor", "inherit");
604       m.modes[j].cursor = GLUT_CURSOR_INHERIT;
605       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
606         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
607           m.modes[j].cursor = mouse_cursor_map[k].cursor;
608           break;
609         }
610       }
611
612                                 // Read other properties for this mode
613       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
614
615                                 // Read the button bindings for this mode
616       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
617       char buf[8];
618       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
619         sprintf(buf, "mouse button %d", k);
620         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
621         _init_button(mode_node->getChild("button", k),
622                      m.modes[j].buttons[k],
623                      buf);
624       }
625
626                                 // Read the axis bindings for this mode
627       _read_bindings(mode_node->getChild("x-axis", 0, true),
628                      m.modes[j].x_bindings,
629                      FG_MOD_NONE);
630       _read_bindings(mode_node->getChild("y-axis", 0, true),
631                      m.modes[j].y_bindings,
632                      FG_MOD_NONE);
633     }
634   }
635 }
636
637
638 void
639 FGInput::_init_button (const SGPropertyNode * node,
640                        button &b,
641                        const string name)
642 {       
643   if (node == 0) {
644     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
645   } else {
646     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
647     
648                 // Get the bindings for the button
649     _read_bindings(node, b.bindings, FG_MOD_NONE);
650   }
651 }
652
653
654 void
655 FGInput::_update_keyboard ()
656 {
657   // no-op
658 }
659
660
661 void
662 FGInput::_update_joystick ()
663 {
664   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
665   int buttons;
666   // float js_val, diff;
667   float axis_values[MAX_JOYSTICK_AXES];
668
669   int i;
670   int j;
671
672   for ( i = 0; i < MAX_JOYSTICKS; i++) {
673
674     jsJoystick * js = _joystick_bindings[i].js;
675     if (js == 0 || js->notWorking())
676       continue;
677
678     js->read(&buttons, axis_values);
679
680
681                                 // Fire bindings for the axes.
682     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
683       axis &a = _joystick_bindings[i].axes[j];
684       
685                                 // Do nothing if the axis position
686                                 // is unchanged; only a change in
687                                 // position fires the bindings.
688       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
689 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
690         SGPropertyNode node;
691         a.last_value = axis_values[j];
692 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
693 //             << a.bindings[modifiers].size() << " bindings");
694         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
695           a.bindings[modifiers][k]->fire(axis_values[j]);
696       }
697      
698                                 // do we have to emulate axis buttons?
699       if (a.low.bindings[modifiers].size())
700         _update_button(_joystick_bindings[i].axes[j].low,
701                        modifiers,
702                        axis_values[j] < a.low_threshold,
703                        -1, -1);
704       
705       if (a.high.bindings[modifiers].size())
706         _update_button(_joystick_bindings[i].axes[j].high,
707                        modifiers,
708                        axis_values[j] > a.high_threshold,
709                        -1, -1);
710     }
711
712                                 // Fire bindings for the buttons.
713     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
714       _update_button(_joystick_bindings[i].buttons[j],
715                      modifiers,
716                      (buttons & (1 << j)) > 0,
717                      -1, -1);
718     }
719   }
720 }
721
722 void
723 FGInput::_update_mouse ()
724 {
725   mouse &m = _mouse_bindings[0];
726   int mode =  fgGetInt("/input/mice/mouse[0]/mode");
727   if (mode != m.current_mode) {
728     m.current_mode = mode;
729     if (mode >= 0 && mode < m.nModes) {
730       glutSetCursor(m.modes[mode].cursor);
731     } else {
732       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
733       glutSetCursor(GLUT_CURSOR_INHERIT);
734     }
735   }
736 }
737
738 void
739 FGInput::_update_button (button &b, int modifiers, bool pressed,
740                          int x, int y)
741 {
742   if (pressed) {
743                                 // The press event may be repeated.
744     if (!b.last_state || b.is_repeatable) {
745       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
746       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
747         b.bindings[modifiers][k]->fire(x, y);
748     }
749   } else {
750                                 // The release event is never repeated.
751     if (b.last_state) {
752       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
753       for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
754         b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
755     }
756   }
757           
758   b.last_state = pressed;
759 }  
760
761
762 void
763 FGInput::_read_bindings (const SGPropertyNode * node, 
764                          binding_list_t * binding_list,
765                          int modifiers)
766 {
767   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
768   vector<const SGPropertyNode *> bindings = node->getChildren("binding");
769   for (unsigned int i = 0; i < bindings.size(); i++) {
770     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
771            << bindings[i]->getStringValue("command"));
772     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
773   }
774
775                                 // Read nested bindings for modifiers
776   if (node->getChild("mod-up") != 0)
777     _read_bindings(node->getChild("mod-up"), binding_list,
778                    modifiers|FG_MOD_UP);
779
780   if (node->getChild("mod-shift") != 0)
781     _read_bindings(node->getChild("mod-shift"), binding_list,
782                    modifiers|FG_MOD_SHIFT);
783
784   if (node->getChild("mod-ctrl") != 0)
785     _read_bindings(node->getChild("mod-ctrl"), binding_list,
786                    modifiers|FG_MOD_CTRL);
787
788   if (node->getChild("mod-alt") != 0)
789     _read_bindings(node->getChild("mod-alt"), binding_list,
790                    modifiers|FG_MOD_ALT);
791 }
792
793
794 const vector<FGBinding *> &
795 FGInput::_find_key_bindings (unsigned int k, int modifiers)
796 {
797   button &b = _key_bindings[k];
798
799                                 // Try it straight, first.
800   if (b.bindings[modifiers].size() > 0)
801     return b.bindings[modifiers];
802
803                                 // Try removing the control modifier
804                                 // for control keys.
805   else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
806     return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
807
808                                 // Try removing shift modifier 
809                                 // for upper case or any punctuation
810                                 // (since different keyboards will
811                                 // shift different punctuation types)
812   else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
813     return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
814
815                                 // Try removing alt modifier for
816                                 // high-bit characters.
817   else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
818     return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
819
820                                 // Give up and return the empty vector.
821   else
822     return b.bindings[modifiers];
823 }
824
825
826 \f
827 ////////////////////////////////////////////////////////////////////////
828 // Implementation of FGInput::button.
829 ////////////////////////////////////////////////////////////////////////
830
831 FGInput::button::button ()
832   : is_repeatable(false),
833     last_state(-1)
834 {
835 }
836
837 FGInput::button::~button ()
838 {
839                                 // FIXME: memory leak
840 //   for (int i = 0; i < FG_MOD_MAX; i++)
841 //     for (int j = 0; i < bindings[i].size(); j++)
842 //       delete bindings[i][j];
843 }
844
845
846 \f
847 ////////////////////////////////////////////////////////////////////////
848 // Implementation of FGInput::axis.
849 ////////////////////////////////////////////////////////////////////////
850
851 FGInput::axis::axis ()
852   : last_value(9999999),
853     tolerance(0.002),
854     low_threshold(-0.9),
855     high_threshold(0.9)
856 {
857 }
858
859 FGInput::axis::~axis ()
860 {
861 //   for (int i = 0; i < FG_MOD_MAX; i++)
862 //     for (int j = 0; i < bindings[i].size(); j++)
863 //       delete bindings[i][j];
864 }
865
866
867 \f
868 ////////////////////////////////////////////////////////////////////////
869 // Implementation of FGInput::joystick.
870 ////////////////////////////////////////////////////////////////////////
871
872 FGInput::joystick::joystick ()
873 {
874 }
875
876 FGInput::joystick::~joystick ()
877 {
878 //   delete js;
879   delete[] axes;
880   delete[] buttons;
881 }
882
883
884 \f
885 ////////////////////////////////////////////////////////////////////////
886 // Implementation of FGInput::mouse_mode
887 ////////////////////////////////////////////////////////////////////////
888
889 FGInput::mouse_mode::mouse_mode ()
890   : cursor(GLUT_CURSOR_INHERIT),
891     constrained(false),
892     buttons(0)
893 {
894 }
895
896 FGInput::mouse_mode::~mouse_mode ()
897 {
898                                 // FIXME: memory leak
899 //   for (int i = 0; i < FG_MOD_MAX; i++) {
900 //     int j;
901 //     for (j = 0; i < x_bindings[i].size(); j++)
902 //       delete bindings[i][j];
903 //     for (j = 0; j < y_bindings[i].size(); j++)
904 //       delete bindings[i][j];
905 //   }
906   delete [] buttons;
907 }
908
909
910 \f
911 ////////////////////////////////////////////////////////////////////////
912 // Implementation of FGInput::mouse
913 ////////////////////////////////////////////////////////////////////////
914
915 FGInput::mouse::mouse ()
916   : nModes(1),
917     current_mode(0),
918     x(-1),
919     y(-1),
920     modes(0)
921 {
922 }
923
924 FGInput::mouse::~mouse ()
925 {
926   delete [] modes;
927 }
928
929
930 \f
931 ////////////////////////////////////////////////////////////////////////
932 // Implementation of GLUT callbacks.
933 ////////////////////////////////////////////////////////////////////////
934
935
936 /**
937  * Construct the modifiers.
938  */
939 static inline int get_mods ()
940 {
941   int glut_modifiers = glutGetModifiers();
942   int modifiers = 0;
943
944   if (glut_modifiers & GLUT_ACTIVE_SHIFT)
945     modifiers |= FGInput::FG_MOD_SHIFT;
946   if (glut_modifiers & GLUT_ACTIVE_CTRL)
947     modifiers |= FGInput::FG_MOD_CTRL;
948   if (glut_modifiers & GLUT_ACTIVE_ALT)
949     modifiers |= FGInput::FG_MOD_ALT;
950
951   return modifiers;
952 }
953
954
955 \f
956 ////////////////////////////////////////////////////////////////////////
957 // GLUT C callbacks.
958 ////////////////////////////////////////////////////////////////////////
959
960 void
961 GLUTkey(unsigned char k, int x, int y)
962 {
963                                 // Give PUI a chance to grab it first.
964   if (!puKeyboard(k, PU_DOWN))
965     current_input.doKey(k, get_mods(), x, y);
966 }
967
968 void
969 GLUTkeyup(unsigned char k, int x, int y)
970 {
971   current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
972 }
973
974 void
975 GLUTspecialkey(int k, int x, int y)
976 {
977                                 // Give PUI a chance to grab it first.
978   if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
979     current_input.doKey(k + 256, get_mods(), x, y);
980 }
981
982 void
983 GLUTspecialkeyup(int k, int x, int y)
984 {
985   current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
986 }
987
988 void
989 GLUTmouse (int button, int updown, int x, int y)
990 {
991   current_input.doMouseClick(button, updown == GLUT_DOWN, x, y);
992 }
993
994 void
995 GLUTmotion (int x, int y)
996 {
997 //   puMouse(x, y);
998 //   glutPostRedisplay();
999   current_input.doMouseMotion(x, y);
1000 }
1001
1002 // end of input.cxx