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