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