]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
89461511e34cec822a99b3cb2ec4a8f61cda66be
[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   int modifiers = fgGetKeyModifiers();
308
309   int xsize = fgGetInt("/sim/startup/xsize", 800);
310   int ysize = fgGetInt("/sim/startup/ysize", 600);
311   mouse &m = _mouse_bindings[0];
312   if (m.current_mode < 0 || m.current_mode >= m.nModes)
313     return;
314   mouse_mode &mode = m.modes[m.current_mode];
315
316                                 // Pass on to PUI if requested, and return
317                                 // if PUI consumed the event.
318   if (mode.pass_through && puMouse(x, y))
319     return;
320
321                                 // OK, PUI didn't want the event,
322                                 // so we can play with it.
323   if (x != m.x) {
324     int delta = x - m.x;
325     for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
326       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
327   }
328   if (y != m.y) {
329     int delta = y - m.y;
330     for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
331       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
332   }
333
334                                 // Constrain the mouse if requested
335   if (mode.constrained) {
336     bool need_warp = false;
337     if (x <= 0) {
338       x = xsize - 2;
339       need_warp = true;
340     } else if (x >= (xsize-1)) {
341       x = 1;
342       need_warp = true;
343     }
344
345     if (y <= 0) {
346       y = ysize - 2;
347       need_warp = true;
348     } else if (y >= (ysize-1)) {
349       y = 1;
350       need_warp = true;
351     }
352
353     if (need_warp)
354       fgWarpMouse(x, y);
355   }
356   m.x = x;
357   m.y = y;
358 }
359
360 void
361 FGInput::_init_keyboard ()
362 {
363   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
364   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
365   if (key_nodes == 0) {
366     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
367     key_nodes = fgGetNode("/input/keyboard", true);
368   }
369   
370   vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
371   for (unsigned int i = 0; i < keys.size(); i++) {
372     int index = keys[i]->getIndex();
373     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
374
375     _key_bindings[index].bindings->clear();
376     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
377     _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
378   }
379 }
380
381
382 void
383 FGInput::_init_joystick ()
384 {
385                                 // TODO: zero the old bindings first.
386   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
387   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
388   if (js_nodes == 0) {
389     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
390     js_nodes = fgGetNode("/input/joysticks", true);
391   }
392
393   for (int i = 0; i < MAX_JOYSTICKS; i++) {
394     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
395     if (js_node == 0) {
396       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
397       js_node = js_nodes->getChild("js", i, true);
398     }
399     jsJoystick * js = new jsJoystick(i);
400     _joystick_bindings[i].js = js;
401     if (js->notWorking()) {
402       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
403       continue;
404     } else {
405       bool found_js = false;
406       const char * name = js->getName();
407       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
408              << name << '"');
409       vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
410       for (unsigned int i = 0; i < nodes.size(); i++) {
411         SGPropertyNode_ptr node = nodes[i];
412         vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
413         for (unsigned int j = 0; j < name_nodes.size(); j++) {
414             const char * js_name = name_nodes[j]->getStringValue();
415             SG_LOG(SG_INPUT, SG_INFO, "  Trying \"" << js_name << '"');
416             if (!strcmp(js_name, name)) {
417                 SG_LOG(SG_INPUT, SG_INFO, "  Found bindings");
418                 js_node = node;
419                 found_js = true;
420                 break;
421             }
422         }
423         if (found_js)
424             break;
425       }
426     }
427 #ifdef WIN32
428     JOYCAPS jsCaps ;
429     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
430     int nbuttons = jsCaps.wNumButtons;
431     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
432 #else
433     int nbuttons = MAX_JOYSTICK_BUTTONS;
434 #endif
435         
436     int naxes = js->getNumAxes();
437     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
438     _joystick_bindings[i].naxes = naxes;
439     _joystick_bindings[i].nbuttons = nbuttons;
440
441     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
442
443                                 // Set up range arrays
444     float minRange[MAX_JOYSTICK_AXES];
445     float maxRange[MAX_JOYSTICK_AXES];
446     float center[MAX_JOYSTICK_AXES];
447
448                                 // Initialize with default values
449     js->getMinRange(minRange);
450     js->getMaxRange(maxRange);
451     js->getCenter(center);
452
453                                 // Allocate axes and buttons
454     _joystick_bindings[i].axes = new axis[naxes];
455     _joystick_bindings[i].buttons = new button[nbuttons];
456
457
458     //
459     // Initialize the axes.
460     //
461     int j;
462     for (j = 0; j < naxes; j++) {
463       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
464       if (axis_node == 0) {
465         SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
466         axis_node = js_node->getChild("axis", j, true);
467       }
468       
469       axis &a = _joystick_bindings[i].axes[j];
470
471       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
472
473       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
474       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
475       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
476       center[j] = axis_node->getDoubleValue("center", center[j]);
477
478       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
479
480       // Initialize the virtual axis buttons.
481       _init_button(axis_node->getChild("low"), a.low, "low");
482       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
483       
484       _init_button(axis_node->getChild("high"), a.high, "high");
485       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
486       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
487       a.last_dt = 0.0;
488     }
489
490     //
491     // Initialize the buttons.
492     //
493     char buf[32];
494     for (j = 0; j < nbuttons; j++) {
495       sprintf(buf, "%d", j);
496       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
497       _init_button(js_node->getChild("button", j),
498                    _joystick_bindings[i].buttons[j],
499                    buf);
500       
501       // get interval-sec property             
502       button &b = _joystick_bindings[i].buttons[j];
503       const SGPropertyNode * button_node = js_node->getChild("button", j);
504       if (button_node != 0) {
505         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
506         b.last_dt = 0.0;
507       }
508     }
509
510     js->setMinRange(minRange);
511     js->setMaxRange(maxRange);
512     js->setCenter(center);
513   }
514 }
515
516 // 
517 // Map of all known cursor names
518 // This used to contain all the Glut cursors, but those are
519 // not defined by other toolkits.  It now supports only the cursor
520 // images we actually use, in the interest of portability.  Someday,
521 // it would be cool to write an OpenGL cursor renderer, with the
522 // cursors defined as textures referenced in the property tree.  This
523 // list could then be eliminated. -Andy
524 //
525 struct {
526   const char * name;
527   int cursor;
528 } mouse_cursor_map[] = {
529   { "none", MOUSE_CURSOR_NONE },
530   { "inherit", MOUSE_CURSOR_POINTER },
531   { "wait", MOUSE_CURSOR_WAIT },
532   { "crosshair", MOUSE_CURSOR_CROSSHAIR },
533   { "left-right", MOUSE_CURSOR_LEFTRIGHT },
534   { 0, 0 }
535 };
536
537 void
538 FGInput::_init_mouse ()
539 {
540   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
541
542   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
543   if (mouse_nodes == 0) {
544     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
545     mouse_nodes = fgGetNode("/input/mice", true);
546   }
547
548   int j;
549   for (int i = 0; i < MAX_MICE; i++) {
550     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
551     mouse &m = _mouse_bindings[i];
552
553                                 // Grab node pointers
554     char buf[64];
555     sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
556     m.mode_node = fgGetNode(buf);
557     if (m.mode_node == NULL) {
558       m.mode_node = fgGetNode(buf, true);
559       m.mode_node->setIntValue(0);
560     }
561     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
562       sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
563       m.mouse_button_nodes[j] = fgGetNode(buf, true);
564       m.mouse_button_nodes[j]->setBoolValue(false);
565     }
566
567                                 // Read all the modes
568     m.nModes = mouse_node->getIntValue("mode-count", 1);
569     m.modes = new mouse_mode[m.nModes];
570
571     for (int j = 0; j < m.nModes; j++) {
572       int k;
573
574                                 // Read the mouse cursor for this mode
575       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
576       const char * cursor_name =
577         mode_node->getStringValue("cursor", "inherit");
578       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
579       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
580         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
581           m.modes[j].cursor = mouse_cursor_map[k].cursor;
582           break;
583         }
584       }
585
586                                 // Read other properties for this mode
587       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
588       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
589
590                                 // Read the button bindings for this mode
591       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
592       char buf[32];
593       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
594         sprintf(buf, "mouse button %d", k);
595         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
596         _init_button(mode_node->getChild("button", k),
597                      m.modes[j].buttons[k],
598                      buf);
599       }
600
601                                 // Read the axis bindings for this mode
602       _read_bindings(mode_node->getChild("x-axis", 0, true),
603                      m.modes[j].x_bindings,
604                      KEYMOD_NONE);
605       _read_bindings(mode_node->getChild("y-axis", 0, true),
606                      m.modes[j].y_bindings,
607                      KEYMOD_NONE);
608     }
609   }
610 }
611
612
613 void
614 FGInput::_init_button (const SGPropertyNode * node,
615                        button &b,
616                        const string name)
617 {       
618   if (node == 0) {
619     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
620   } else {
621     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
622     
623                 // Get the bindings for the button
624     _read_bindings(node, b.bindings, KEYMOD_NONE);
625   }
626 }
627
628
629 void
630 FGInput::_update_keyboard ()
631 {
632   // no-op
633 }
634
635
636 void
637 FGInput::_update_joystick (double dt)
638 {
639   int modifiers = KEYMOD_NONE;  // FIXME: any way to get the real ones?
640   int buttons;
641   // float js_val, diff;
642   float axis_values[MAX_JOYSTICK_AXES];
643
644   int i;
645   int j;
646
647   for ( i = 0; i < MAX_JOYSTICKS; i++) {
648
649     jsJoystick * js = _joystick_bindings[i].js;
650     if (js == 0 || js->notWorking())
651       continue;
652
653     js->read(&buttons, axis_values);
654
655
656                                 // Fire bindings for the axes.
657     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
658       axis &a = _joystick_bindings[i].axes[j];
659       
660                                 // Do nothing if the axis position
661                                 // is unchanged; only a change in
662                                 // position fires the bindings.
663       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
664 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
665         SGPropertyNode node;
666         a.last_value = axis_values[j];
667 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
668 //             << a.bindings[modifiers].size() << " bindings");
669         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
670           a.bindings[modifiers][k]->fire(axis_values[j]);
671       }
672      
673                                 // do we have to emulate axis buttons?
674       a.last_dt += dt;
675       if(a.last_dt >= a.interval_sec) {
676         if (a.low.bindings[modifiers].size())
677           _update_button(_joystick_bindings[i].axes[j].low,
678                          modifiers,
679                          axis_values[j] < a.low_threshold,
680                          -1, -1);
681       
682         if (a.high.bindings[modifiers].size())
683           _update_button(_joystick_bindings[i].axes[j].high,
684                          modifiers,
685                          axis_values[j] > a.high_threshold,
686                          -1, -1);
687          a.last_dt -= a.interval_sec;
688       }
689     }
690
691                                 // Fire bindings for the buttons.
692     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
693       button &b = _joystick_bindings[i].buttons[j];
694       b.last_dt += dt;
695       if(b.last_dt >= b.interval_sec) {
696         _update_button(_joystick_bindings[i].buttons[j],
697                        modifiers,
698                        (buttons & (1 << j)) > 0,
699                        -1, -1);
700         b.last_dt -= b.interval_sec;
701       }
702     }
703   }
704 }
705
706 void
707 FGInput::_update_mouse ()
708 {
709   mouse &m = _mouse_bindings[0];
710   int mode =  m.mode_node->getIntValue();
711   if (mode != m.current_mode) {
712     m.current_mode = mode;
713     if (mode >= 0 && mode < m.nModes) {
714       fgSetMouseCursor(m.modes[mode].cursor);
715       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
716       m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
717       fgWarpMouse(m.x, m.y);
718     } else {
719       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
720       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
721     }
722   }
723 }
724
725 void
726 FGInput::_update_button (button &b, int modifiers, bool pressed,
727                          int x, int y)
728 {
729   if (pressed) {
730                                 // The press event may be repeated.
731     if (!b.last_state || b.is_repeatable) {
732       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
733       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
734         b.bindings[modifiers][k]->fire(x, y);
735     }
736   } else {
737                                 // The release event is never repeated.
738     if (b.last_state) {
739       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
740       for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
741         b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
742     }
743   }
744           
745   b.last_state = pressed;
746 }  
747
748
749 void
750 FGInput::_read_bindings (const SGPropertyNode * node, 
751                          binding_list_t * binding_list,
752                          int modifiers)
753 {
754   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
755   vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
756   for (unsigned int i = 0; i < bindings.size(); i++) {
757     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
758            << bindings[i]->getStringValue("command"));
759     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
760   }
761
762                                 // Read nested bindings for modifiers
763   if (node->getChild("mod-up") != 0)
764     _read_bindings(node->getChild("mod-up"), binding_list,
765                    modifiers|KEYMOD_RELEASED);
766
767   if (node->getChild("mod-shift") != 0)
768     _read_bindings(node->getChild("mod-shift"), binding_list,
769                    modifiers|KEYMOD_SHIFT);
770
771   if (node->getChild("mod-ctrl") != 0)
772     _read_bindings(node->getChild("mod-ctrl"), binding_list,
773                    modifiers|KEYMOD_CTRL);
774
775   if (node->getChild("mod-alt") != 0)
776     _read_bindings(node->getChild("mod-alt"), binding_list,
777                    modifiers|KEYMOD_ALT);
778 }
779
780
781 const vector<FGBinding *> &
782 FGInput::_find_key_bindings (unsigned int k, int modifiers)
783 {
784   unsigned char kc = (unsigned char)k;
785   button &b = _key_bindings[k];
786
787                                 // Try it straight, first.
788   if (b.bindings[modifiers].size() > 0)
789     return b.bindings[modifiers];
790
791                                 // Alt-Gr is CTRL+ALT
792   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
793     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
794
795                                 // Try removing the control modifier
796                                 // for control keys.
797   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
798     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
799
800                                 // Try removing shift modifier 
801                                 // for upper case or any punctuation
802                                 // (since different keyboards will
803                                 // shift different punctuation types)
804   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
805     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
806
807                                 // Try removing alt modifier for
808                                 // high-bit characters.
809   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
810     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
811
812                                 // Give up and return the empty vector.
813   else
814     return b.bindings[modifiers];
815 }
816
817
818 \f
819 ////////////////////////////////////////////////////////////////////////
820 // Implementation of FGInput::button.
821 ////////////////////////////////////////////////////////////////////////
822
823 FGInput::button::button ()
824   : is_repeatable(false),
825     last_state(-1)
826 {
827 }
828
829 FGInput::button::~button ()
830 {
831                                 // FIXME: memory leak
832 //   for (int i = 0; i < KEYMOD_MAX; i++)
833 //     for (int j = 0; i < bindings[i].size(); j++)
834 //       delete bindings[i][j];
835 }
836
837
838 \f
839 ////////////////////////////////////////////////////////////////////////
840 // Implementation of FGInput::axis.
841 ////////////////////////////////////////////////////////////////////////
842
843 FGInput::axis::axis ()
844   : last_value(9999999),
845     tolerance(0.002),
846     low_threshold(-0.9),
847     high_threshold(0.9)
848 {
849 }
850
851 FGInput::axis::~axis ()
852 {
853 //   for (int i = 0; i < KEYMOD_MAX; i++)
854 //     for (int j = 0; i < bindings[i].size(); j++)
855 //       delete bindings[i][j];
856 }
857
858
859 \f
860 ////////////////////////////////////////////////////////////////////////
861 // Implementation of FGInput::joystick.
862 ////////////////////////////////////////////////////////////////////////
863
864 FGInput::joystick::joystick ()
865 {
866 }
867
868 FGInput::joystick::~joystick ()
869 {
870 //   delete js;
871   delete[] axes;
872   delete[] buttons;
873 }
874
875
876 \f
877 ////////////////////////////////////////////////////////////////////////
878 // Implementation of FGInput::mouse_mode
879 ////////////////////////////////////////////////////////////////////////
880
881 FGInput::mouse_mode::mouse_mode ()
882   : cursor(MOUSE_CURSOR_POINTER),
883     constrained(false),
884     pass_through(false),
885     buttons(0)
886 {
887 }
888
889 FGInput::mouse_mode::~mouse_mode ()
890 {
891                                 // FIXME: memory leak
892 //   for (int i = 0; i < KEYMOD_MAX; i++) {
893 //     int j;
894 //     for (j = 0; i < x_bindings[i].size(); j++)
895 //       delete bindings[i][j];
896 //     for (j = 0; j < y_bindings[i].size(); j++)
897 //       delete bindings[i][j];
898 //   }
899   delete [] buttons;
900 }
901
902
903 \f
904 ////////////////////////////////////////////////////////////////////////
905 // Implementation of FGInput::mouse
906 ////////////////////////////////////////////////////////////////////////
907
908 FGInput::mouse::mouse ()
909   : x(-1),
910     y(-1),
911     nModes(1),
912     current_mode(0),
913     modes(0)
914 {
915 }
916
917 FGInput::mouse::~mouse ()
918 {
919   delete [] modes;
920 }
921
922 ////////////////////////////////////////////////////////////////////////
923 // Implementation of OS callbacks.
924 ////////////////////////////////////////////////////////////////////////
925
926 void keyHandler(int key, int keymod, int mousex, int mousey)
927 {
928     if((keymod & KEYMOD_RELEASED) == 0)
929         if(puKeyboard(key, PU_DOWN))
930             return;
931
932     if(default_input)
933         default_input->doKey(key, keymod, mousex, mousey);
934 }
935
936 void mouseClickHandler(int button, int updown, int x, int y)
937 {
938     if(default_input)
939         default_input->doMouseClick(button, updown, x, y);
940 }
941
942 void mouseMotionHandler(int x, int y)
943 {
944     if (default_input != 0)
945         default_input->doMouseMotion(x, y);
946 }