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