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