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