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