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