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