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