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