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