]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
I was too agressive in the fg_os port. Glut doesn't support grabbing
[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 <simgear/compiler.h>
41
42 #include <simgear/constants.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/props/props.hxx>
45
46 #include <Aircraft/aircraft.hxx>
47 #include <Autopilot/xmlauto.hxx>
48 #include <Cockpit/hud.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <Cockpit/panel_io.hxx>
51 #include <GUI/gui.h>
52 #include <Model/panelnode.hxx>
53
54 #include <Main/globals.hxx>
55 #include <Main/fg_props.hxx>
56
57 #include "input.hxx"
58
59 SG_USING_STD(ifstream);
60 SG_USING_STD(string);
61 SG_USING_STD(vector);
62
63 void mouseClickHandler(int button, int updown, int x, int y);
64 void mouseMotionHandler(int x, int y);
65 void keyHandler(int key, int keymod, int mousex, int mousey);
66
67 \f
68 ////////////////////////////////////////////////////////////////////////
69 // Local variables.
70 ////////////////////////////////////////////////////////////////////////
71
72 static FGInput * default_input = 0;
73
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Implementation of FGBinding.
78 ////////////////////////////////////////////////////////////////////////
79
80 FGBinding::FGBinding ()
81   : _command(0),
82     _arg(new SGPropertyNode),
83     _setting(0)
84 {
85 }
86
87 FGBinding::FGBinding (const SGPropertyNode * node)
88   : _command(0),
89     _arg(0),
90     _setting(0)
91 {
92   read(node);
93 }
94
95 void
96 FGBinding::read (const SGPropertyNode * node)
97 {
98   const SGPropertyNode * conditionNode = node->getChild("condition");
99   if (conditionNode != 0)
100     setCondition(sgReadCondition(globals->get_props(), conditionNode));
101
102   _command_name = node->getStringValue("command", "");
103   if (_command_name.empty()) {
104     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
105     _command = 0;
106     return;
107   }
108
109   _arg = new SGPropertyNode;
110   _setting = 0;
111   copyProperties(node, _arg);  // FIXME: don't use whole node!!!
112 }
113
114 void
115 FGBinding::fire () const
116 {
117   if (test()) {
118     if (_command == 0)
119       _command = globals->get_commands()->getCommand(_command_name);
120     if (_command == 0) {
121       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
122     } else if (!(*_command)(_arg)) {
123       SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
124              << _command_name);
125     }
126   }
127 }
128
129 void
130 FGBinding::fire (double offset, double max) const
131 {
132   if (test()) {
133     _arg->setDoubleValue("offset", offset/max);
134     fire();
135   }
136 }
137
138 void
139 FGBinding::fire (double setting) const
140 {
141   if (test()) {
142                                 // A value is automatically added to
143                                 // the args
144     if (_setting == 0)          // save the setting node for efficiency
145       _setting = _arg->getChild("setting", 0, true);
146     _setting->setDoubleValue(setting);
147     fire();
148   }
149 }
150
151
152 \f
153 ////////////////////////////////////////////////////////////////////////
154 // Implementation of FGInput.
155 ////////////////////////////////////////////////////////////////////////
156
157
158 FGInput::FGInput ()
159 {
160     if (default_input == 0)
161         default_input = this;
162 }
163
164 FGInput::~FGInput ()
165 {
166     if (default_input == this)
167         default_input = 0;
168 }
169
170 void
171 FGInput::init ()
172 {
173   _init_keyboard();
174   _init_joystick();
175   _init_mouse();
176
177   fgRegisterKeyHandler(keyHandler);
178   fgRegisterMouseClickHandler(mouseClickHandler);
179   fgRegisterMouseMotionHandler(mouseMotionHandler);
180 }
181
182 void 
183 FGInput::update (double dt)
184 {
185   _update_keyboard();
186   _update_joystick(dt);
187   _update_mouse();
188 }
189
190 void
191 FGInput::suspend ()
192 {
193     // NO-OP
194 }
195
196 void
197 FGInput::resume ()
198 {
199     // NO-OP
200 }
201
202 bool
203 FGInput::is_suspended () const
204 {
205     return false;
206 }
207
208 void
209 FGInput::makeDefault (bool status)
210 {
211     if (status)
212         default_input = this;
213     else if (default_input == this)
214         default_input = 0;
215 }
216
217 void
218 FGInput::doKey (int k, int modifiers, int x, int y)
219 {
220   // static SGPropertyNode *heading_enabled
221   //     = fgGetNode("/autopilot/locks/heading", true);
222
223                                 // Sanity check.
224   if (k < 0 || k >= MAX_KEYS) {
225     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
226     return;
227   }
228
229   button &b = _key_bindings[k];
230
231                                 // Key pressed.
232   if (modifiers&KEYMOD_RELEASED == 0) {
233     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
234             << " with modifiers " << modifiers );
235     if (!b.last_state || b.is_repeatable) {
236       const binding_list_t &bindings =
237         _find_key_bindings(k, modifiers);
238       int max = bindings.size();
239       if (max > 0) {
240         for (int i = 0; i < max; i++)
241           bindings[i]->fire();
242         return;
243       }
244     }
245   }
246
247                                 // Key released.
248   else {
249     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
250            << " with modifiers " << modifiers);
251     if (b.last_state) {
252       const binding_list_t &bindings =
253         _find_key_bindings(k, modifiers);
254       int max = bindings.size();
255       if (max > 0) {
256         for (int i = 0; i < max; i++)
257           bindings[i]->fire();
258         return;
259       }
260     }
261   }
262
263
264                                 // Use the old, default actions.
265   SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
266 }
267
268 void
269 FGInput::doMouseClick (int b, int updown, int x, int y)
270 {
271   int modifiers = fgGetKeyModifiers();
272
273   mouse &m = _mouse_bindings[0];
274   mouse_mode &mode = m.modes[m.current_mode];
275
276                                 // Let the property manager know.
277   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
278     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
279
280                                 // Pass on to PUI and the panel if
281                                 // requested, and return if one of
282                                 // them consumes the event.
283   if (mode.pass_through) {
284     if (puMouse(b, updown, x, y))
285       return;
286     else if ((globals->get_current_panel() != 0) &&
287              globals->get_current_panel()->getVisibility() &&
288              globals->get_current_panel()->doMouseAction(b, updown, x, y))
289       return;
290     else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
291       return;
292   }
293
294                                 // OK, PUI and the panel didn't want the click
295   if (b >= MAX_MOUSE_BUTTONS) {
296     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
297            << " where only " << MAX_MOUSE_BUTTONS << " expected");
298     return;
299   }
300
301   _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
302 }
303
304 void
305 FGInput::doMouseMotion (int x, int y)
306 {
307   // Don't call fgGetKeyModifiers() here, until we are using a
308   // toolkit that supports getting the mods from outside a key
309   // callback.  Glut doesn't.
310   int modifiers = KEYMOD_NONE;
311
312   int xsize = fgGetInt("/sim/startup/xsize", 800);
313   int ysize = fgGetInt("/sim/startup/ysize", 600);
314   mouse &m = _mouse_bindings[0];
315   if (m.current_mode < 0 || m.current_mode >= m.nModes)
316     return;
317   mouse_mode &mode = m.modes[m.current_mode];
318
319                                 // Pass on to PUI if requested, and return
320                                 // if PUI consumed the event.
321   if (mode.pass_through && puMouse(x, y))
322     return;
323
324                                 // OK, PUI didn't want the event,
325                                 // so we can play with it.
326   if (x != m.x) {
327     int delta = x - m.x;
328     for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
329       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
330   }
331   if (y != m.y) {
332     int delta = y - m.y;
333     for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
334       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
335   }
336
337                                 // Constrain the mouse if requested
338   if (mode.constrained) {
339     bool need_warp = false;
340     if (x <= 0) {
341       x = xsize - 2;
342       need_warp = true;
343     } else if (x >= (xsize-1)) {
344       x = 1;
345       need_warp = true;
346     }
347
348     if (y <= 0) {
349       y = ysize - 2;
350       need_warp = true;
351     } else if (y >= (ysize-1)) {
352       y = 1;
353       need_warp = true;
354     }
355
356     if (need_warp)
357       fgWarpMouse(x, y);
358   }
359   m.x = x;
360   m.y = y;
361 }
362
363 void
364 FGInput::_init_keyboard ()
365 {
366   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
367   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
368   if (key_nodes == 0) {
369     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
370     key_nodes = fgGetNode("/input/keyboard", true);
371   }
372   
373   vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
374   for (unsigned int i = 0; i < keys.size(); i++) {
375     int index = keys[i]->getIndex();
376     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
377
378     _key_bindings[index].bindings->clear();
379     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
380     _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
381   }
382 }
383
384
385 void
386 FGInput::_init_joystick ()
387 {
388                                 // TODO: zero the old bindings first.
389   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
390   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
391   if (js_nodes == 0) {
392     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
393     js_nodes = fgGetNode("/input/joysticks", true);
394   }
395
396   for (int i = 0; i < MAX_JOYSTICKS; i++) {
397     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
398     if (js_node == 0) {
399       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
400       js_node = js_nodes->getChild("js", i, true);
401     }
402     jsJoystick * js = new jsJoystick(i);
403     _joystick_bindings[i].js = js;
404     if (js->notWorking()) {
405       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
406       continue;
407     } else {
408       bool found_js = false;
409       const char * name = js->getName();
410       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
411              << name << '"');
412       vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
413       for (unsigned int i = 0; i < nodes.size(); i++) {
414         SGPropertyNode_ptr node = nodes[i];
415         vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
416         for (unsigned int j = 0; j < name_nodes.size(); j++) {
417             const char * js_name = name_nodes[j]->getStringValue();
418             SG_LOG(SG_INPUT, SG_INFO, "  Trying \"" << js_name << '"');
419             if (!strcmp(js_name, name)) {
420                 SG_LOG(SG_INPUT, SG_INFO, "  Found bindings");
421                 js_node = node;
422                 found_js = true;
423                 break;
424             }
425         }
426         if (found_js)
427             break;
428       }
429     }
430 #ifdef WIN32
431     JOYCAPS jsCaps ;
432     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
433     int nbuttons = jsCaps.wNumButtons;
434     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
435 #else
436     int nbuttons = MAX_JOYSTICK_BUTTONS;
437 #endif
438         
439     int naxes = js->getNumAxes();
440     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
441     _joystick_bindings[i].naxes = naxes;
442     _joystick_bindings[i].nbuttons = nbuttons;
443
444     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
445
446                                 // Set up range arrays
447     float minRange[MAX_JOYSTICK_AXES];
448     float maxRange[MAX_JOYSTICK_AXES];
449     float center[MAX_JOYSTICK_AXES];
450
451                                 // Initialize with default values
452     js->getMinRange(minRange);
453     js->getMaxRange(maxRange);
454     js->getCenter(center);
455
456                                 // Allocate axes and buttons
457     _joystick_bindings[i].axes = new axis[naxes];
458     _joystick_bindings[i].buttons = new button[nbuttons];
459
460
461     //
462     // Initialize the axes.
463     //
464     int j;
465     for (j = 0; j < naxes; j++) {
466       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
467       if (axis_node == 0) {
468         SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
469         axis_node = js_node->getChild("axis", j, true);
470       }
471       
472       axis &a = _joystick_bindings[i].axes[j];
473
474       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
475
476       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
477       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
478       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
479       center[j] = axis_node->getDoubleValue("center", center[j]);
480
481       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
482
483       // Initialize the virtual axis buttons.
484       _init_button(axis_node->getChild("low"), a.low, "low");
485       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
486       
487       _init_button(axis_node->getChild("high"), a.high, "high");
488       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
489       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
490       a.last_dt = 0.0;
491     }
492
493     //
494     // Initialize the buttons.
495     //
496     char buf[32];
497     for (j = 0; j < nbuttons; j++) {
498       sprintf(buf, "%d", j);
499       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
500       _init_button(js_node->getChild("button", j),
501                    _joystick_bindings[i].buttons[j],
502                    buf);
503       
504       // get interval-sec property             
505       button &b = _joystick_bindings[i].buttons[j];
506       const SGPropertyNode * button_node = js_node->getChild("button", j);
507       if (button_node != 0) {
508         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
509         b.last_dt = 0.0;
510       }
511     }
512
513     js->setMinRange(minRange);
514     js->setMaxRange(maxRange);
515     js->setCenter(center);
516   }
517 }
518
519 // 
520 // Map of all known cursor names
521 // This used to contain all the Glut cursors, but those are
522 // not defined by other toolkits.  It now supports only the cursor
523 // images we actually use, in the interest of portability.  Someday,
524 // it would be cool to write an OpenGL cursor renderer, with the
525 // cursors defined as textures referenced in the property tree.  This
526 // list could then be eliminated. -Andy
527 //
528 struct {
529   const char * name;
530   int cursor;
531 } mouse_cursor_map[] = {
532   { "none", MOUSE_CURSOR_NONE },
533   { "inherit", MOUSE_CURSOR_POINTER },
534   { "wait", MOUSE_CURSOR_WAIT },
535   { "crosshair", MOUSE_CURSOR_CROSSHAIR },
536   { "left-right", MOUSE_CURSOR_LEFTRIGHT },
537   { 0, 0 }
538 };
539
540 void
541 FGInput::_init_mouse ()
542 {
543   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
544
545   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
546   if (mouse_nodes == 0) {
547     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
548     mouse_nodes = fgGetNode("/input/mice", true);
549   }
550
551   int j;
552   for (int i = 0; i < MAX_MICE; i++) {
553     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
554     mouse &m = _mouse_bindings[i];
555
556                                 // Grab node pointers
557     char buf[64];
558     sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
559     m.mode_node = fgGetNode(buf);
560     if (m.mode_node == NULL) {
561       m.mode_node = fgGetNode(buf, true);
562       m.mode_node->setIntValue(0);
563     }
564     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
565       sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
566       m.mouse_button_nodes[j] = fgGetNode(buf, true);
567       m.mouse_button_nodes[j]->setBoolValue(false);
568     }
569
570                                 // Read all the modes
571     m.nModes = mouse_node->getIntValue("mode-count", 1);
572     m.modes = new mouse_mode[m.nModes];
573
574     for (int j = 0; j < m.nModes; j++) {
575       int k;
576
577                                 // Read the mouse cursor for this mode
578       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
579       const char * cursor_name =
580         mode_node->getStringValue("cursor", "inherit");
581       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
582       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
583         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
584           m.modes[j].cursor = mouse_cursor_map[k].cursor;
585           break;
586         }
587       }
588
589                                 // Read other properties for this mode
590       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
591       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
592
593                                 // Read the button bindings for this mode
594       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
595       char buf[32];
596       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
597         sprintf(buf, "mouse button %d", k);
598         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
599         _init_button(mode_node->getChild("button", k),
600                      m.modes[j].buttons[k],
601                      buf);
602       }
603
604                                 // Read the axis bindings for this mode
605       _read_bindings(mode_node->getChild("x-axis", 0, true),
606                      m.modes[j].x_bindings,
607                      KEYMOD_NONE);
608       _read_bindings(mode_node->getChild("y-axis", 0, true),
609                      m.modes[j].y_bindings,
610                      KEYMOD_NONE);
611     }
612   }
613 }
614
615
616 void
617 FGInput::_init_button (const SGPropertyNode * node,
618                        button &b,
619                        const string name)
620 {       
621   if (node == 0) {
622     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
623   } else {
624     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
625     
626                 // Get the bindings for the button
627     _read_bindings(node, b.bindings, KEYMOD_NONE);
628   }
629 }
630
631
632 void
633 FGInput::_update_keyboard ()
634 {
635   // no-op
636 }
637
638
639 void
640 FGInput::_update_joystick (double dt)
641 {
642   int modifiers = KEYMOD_NONE;  // FIXME: any way to get the real ones?
643   int buttons;
644   // float js_val, diff;
645   float axis_values[MAX_JOYSTICK_AXES];
646
647   int i;
648   int j;
649
650   for ( i = 0; i < MAX_JOYSTICKS; i++) {
651
652     jsJoystick * js = _joystick_bindings[i].js;
653     if (js == 0 || js->notWorking())
654       continue;
655
656     js->read(&buttons, axis_values);
657
658
659                                 // Fire bindings for the axes.
660     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
661       axis &a = _joystick_bindings[i].axes[j];
662       
663                                 // Do nothing if the axis position
664                                 // is unchanged; only a change in
665                                 // position fires the bindings.
666       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
667 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
668         SGPropertyNode node;
669         a.last_value = axis_values[j];
670 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
671 //             << a.bindings[modifiers].size() << " bindings");
672         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
673           a.bindings[modifiers][k]->fire(axis_values[j]);
674       }
675      
676                                 // do we have to emulate axis buttons?
677       a.last_dt += dt;
678       if(a.last_dt >= a.interval_sec) {
679         if (a.low.bindings[modifiers].size())
680           _update_button(_joystick_bindings[i].axes[j].low,
681                          modifiers,
682                          axis_values[j] < a.low_threshold,
683                          -1, -1);
684       
685         if (a.high.bindings[modifiers].size())
686           _update_button(_joystick_bindings[i].axes[j].high,
687                          modifiers,
688                          axis_values[j] > a.high_threshold,
689                          -1, -1);
690          a.last_dt -= a.interval_sec;
691       }
692     }
693
694                                 // Fire bindings for the buttons.
695     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
696       button &b = _joystick_bindings[i].buttons[j];
697       b.last_dt += dt;
698       if(b.last_dt >= b.interval_sec) {
699         _update_button(_joystick_bindings[i].buttons[j],
700                        modifiers,
701                        (buttons & (1 << j)) > 0,
702                        -1, -1);
703         b.last_dt -= b.interval_sec;
704       }
705     }
706   }
707 }
708
709 void
710 FGInput::_update_mouse ()
711 {
712   mouse &m = _mouse_bindings[0];
713   int mode =  m.mode_node->getIntValue();
714   if (mode != m.current_mode) {
715     m.current_mode = mode;
716     if (mode >= 0 && mode < m.nModes) {
717       fgSetMouseCursor(m.modes[mode].cursor);
718       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
719       m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
720       fgWarpMouse(m.x, m.y);
721     } else {
722       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
723       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
724     }
725   }
726 }
727
728 void
729 FGInput::_update_button (button &b, int modifiers, bool pressed,
730                          int x, int y)
731 {
732   if (pressed) {
733                                 // The press event may be repeated.
734     if (!b.last_state || b.is_repeatable) {
735       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
736       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
737         b.bindings[modifiers][k]->fire(x, y);
738     }
739   } else {
740                                 // The release event is never repeated.
741     if (b.last_state) {
742       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
743       for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
744         b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
745     }
746   }
747           
748   b.last_state = pressed;
749 }  
750
751
752 void
753 FGInput::_read_bindings (const SGPropertyNode * node, 
754                          binding_list_t * binding_list,
755                          int modifiers)
756 {
757   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
758   vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
759   for (unsigned int i = 0; i < bindings.size(); i++) {
760     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
761            << bindings[i]->getStringValue("command"));
762     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
763   }
764
765                                 // Read nested bindings for modifiers
766   if (node->getChild("mod-up") != 0)
767     _read_bindings(node->getChild("mod-up"), binding_list,
768                    modifiers|KEYMOD_RELEASED);
769
770   if (node->getChild("mod-shift") != 0)
771     _read_bindings(node->getChild("mod-shift"), binding_list,
772                    modifiers|KEYMOD_SHIFT);
773
774   if (node->getChild("mod-ctrl") != 0)
775     _read_bindings(node->getChild("mod-ctrl"), binding_list,
776                    modifiers|KEYMOD_CTRL);
777
778   if (node->getChild("mod-alt") != 0)
779     _read_bindings(node->getChild("mod-alt"), binding_list,
780                    modifiers|KEYMOD_ALT);
781 }
782
783
784 const vector<FGBinding *> &
785 FGInput::_find_key_bindings (unsigned int k, int modifiers)
786 {
787   unsigned char kc = (unsigned char)k;
788   button &b = _key_bindings[k];
789
790                                 // Try it straight, first.
791   if (b.bindings[modifiers].size() > 0)
792     return b.bindings[modifiers];
793
794                                 // Alt-Gr is CTRL+ALT
795   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
796     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
797
798                                 // Try removing the control modifier
799                                 // for control keys.
800   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
801     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
802
803                                 // Try removing shift modifier 
804                                 // for upper case or any punctuation
805                                 // (since different keyboards will
806                                 // shift different punctuation types)
807   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
808     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
809
810                                 // Try removing alt modifier for
811                                 // high-bit characters.
812   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
813     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
814
815                                 // Give up and return the empty vector.
816   else
817     return b.bindings[modifiers];
818 }
819
820
821 \f
822 ////////////////////////////////////////////////////////////////////////
823 // Implementation of FGInput::button.
824 ////////////////////////////////////////////////////////////////////////
825
826 FGInput::button::button ()
827   : is_repeatable(false),
828     last_state(-1)
829 {
830 }
831
832 FGInput::button::~button ()
833 {
834                                 // FIXME: memory leak
835 //   for (int i = 0; i < KEYMOD_MAX; i++)
836 //     for (int j = 0; i < bindings[i].size(); j++)
837 //       delete bindings[i][j];
838 }
839
840
841 \f
842 ////////////////////////////////////////////////////////////////////////
843 // Implementation of FGInput::axis.
844 ////////////////////////////////////////////////////////////////////////
845
846 FGInput::axis::axis ()
847   : last_value(9999999),
848     tolerance(0.002),
849     low_threshold(-0.9),
850     high_threshold(0.9)
851 {
852 }
853
854 FGInput::axis::~axis ()
855 {
856 //   for (int i = 0; i < KEYMOD_MAX; i++)
857 //     for (int j = 0; i < bindings[i].size(); j++)
858 //       delete bindings[i][j];
859 }
860
861
862 \f
863 ////////////////////////////////////////////////////////////////////////
864 // Implementation of FGInput::joystick.
865 ////////////////////////////////////////////////////////////////////////
866
867 FGInput::joystick::joystick ()
868 {
869 }
870
871 FGInput::joystick::~joystick ()
872 {
873 //   delete js;
874   delete[] axes;
875   delete[] buttons;
876 }
877
878
879 \f
880 ////////////////////////////////////////////////////////////////////////
881 // Implementation of FGInput::mouse_mode
882 ////////////////////////////////////////////////////////////////////////
883
884 FGInput::mouse_mode::mouse_mode ()
885   : cursor(MOUSE_CURSOR_POINTER),
886     constrained(false),
887     pass_through(false),
888     buttons(0)
889 {
890 }
891
892 FGInput::mouse_mode::~mouse_mode ()
893 {
894                                 // FIXME: memory leak
895 //   for (int i = 0; i < KEYMOD_MAX; i++) {
896 //     int j;
897 //     for (j = 0; i < x_bindings[i].size(); j++)
898 //       delete bindings[i][j];
899 //     for (j = 0; j < y_bindings[i].size(); j++)
900 //       delete bindings[i][j];
901 //   }
902   delete [] buttons;
903 }
904
905
906 \f
907 ////////////////////////////////////////////////////////////////////////
908 // Implementation of FGInput::mouse
909 ////////////////////////////////////////////////////////////////////////
910
911 FGInput::mouse::mouse ()
912   : x(-1),
913     y(-1),
914     nModes(1),
915     current_mode(0),
916     modes(0)
917 {
918 }
919
920 FGInput::mouse::~mouse ()
921 {
922   delete [] modes;
923 }
924
925 ////////////////////////////////////////////////////////////////////////
926 // Implementation of OS callbacks.
927 ////////////////////////////////////////////////////////////////////////
928
929 void keyHandler(int key, int keymod, int mousex, int mousey)
930 {
931     if((keymod & KEYMOD_RELEASED) == 0)
932         if(puKeyboard(key, PU_DOWN))
933             return;
934
935     if(default_input)
936         default_input->doKey(key, keymod, mousex, mousey);
937 }
938
939 void mouseClickHandler(int button, int updown, int x, int y)
940 {
941     if(default_input)
942         default_input->doMouseClick(button, updown, x, y);
943 }
944
945 void mouseMotionHandler(int x, int y)
946 {
947     if (default_input != 0)
948         default_input->doMouseMotion(x, y);
949 }