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