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