]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGKeyboardInput.cxx
73c7c5b0d92e192602ed77cd37254748160ceaf2
[flightgear.git] / src / Input / FGKeyboardInput.cxx
1 // FGKeyboardInput.cxx -- handle user input from keyboard devices
2 //
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
5 //
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include "FGKeyboardInput.hxx"
30 #include <Main/fg_props.hxx>
31 #include <Scripting/NasalSys.hxx>
32 #include <plib/pu.h>
33
34 using simgear::PropertyList;
35
36 static int getModifiers ()
37 {
38   return fgGetKeyModifiers() >> 1;
39 }
40
41 static bool getModShift ()
42 {
43   return (fgGetKeyModifiers() & KEYMOD_SHIFT) != 0;
44 }
45
46 static bool getModCtrl ()
47 {
48   return (fgGetKeyModifiers() & KEYMOD_CTRL) != 0;
49 }
50
51 static bool getModAlt ()
52 {
53   return (fgGetKeyModifiers() & KEYMOD_ALT) != 0;
54 }
55
56 static bool getModMeta ()
57 {
58   return (fgGetKeyModifiers() & KEYMOD_META) != 0;
59 }
60
61 static bool getModSuper ()
62 {
63   return (fgGetKeyModifiers() & KEYMOD_SUPER) != 0;
64 }
65
66 static bool getModHyper ()
67 {
68   return (fgGetKeyModifiers() & KEYMOD_HYPER) != 0;
69 }
70
71 FGKeyboardInput * FGKeyboardInput::keyboardInput = NULL;
72
73 FGKeyboardInput::FGKeyboardInput() :
74     _key_event(fgGetNode("/devices/status/keyboard/event", true))
75 {
76   if( keyboardInput == NULL )
77     keyboardInput = this;
78 }
79
80 FGKeyboardInput::~FGKeyboardInput()
81 {
82   if( keyboardInput == this )
83     keyboardInput = NULL;
84 }
85
86
87 void FGKeyboardInput::init()
88 {
89   fgRegisterKeyHandler(keyHandler);
90 }
91
92 void FGKeyboardInput::postinit()
93 {
94   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
95   string module = "__kbd";
96   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
97   if (key_nodes == NULL) {
98     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
99     key_nodes = fgGetNode("/input/keyboard", true);
100   }
101
102   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
103   PropertyList nasal = key_nodes->getChildren("nasal");
104   for (unsigned int j = 0; j < nasal.size(); j++) {
105     nasal[j]->setStringValue("module", module.c_str());
106     nasalsys->handleCommand(nasal[j]);
107   }
108
109   PropertyList keys = key_nodes->getChildren("key");
110   for (unsigned int i = 0; i < keys.size(); i++) {
111     int index = keys[i]->getIndex();
112     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
113     if( index >= MAX_KEYS ) {
114       SG_LOG(SG_INPUT, SG_WARN, "Key binding " << index << " out of range");
115       continue;
116     }
117
118     bindings[index].bindings->clear();
119     bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
120     bindings[index].last_state = 0;
121     read_bindings(keys[i], bindings[index].bindings, KEYMOD_NONE, module );
122   }
123 }
124
125 void FGKeyboardInput::bind()
126 {
127   fgTie("/devices/status/keyboard", getModifiers);
128   fgTie("/devices/status/keyboard/shift", getModShift);
129   fgTie("/devices/status/keyboard/ctrl", getModCtrl);
130   fgTie("/devices/status/keyboard/alt", getModAlt);
131   fgTie("/devices/status/keyboard/meta", getModMeta);
132   fgTie("/devices/status/keyboard/super", getModSuper);
133   fgTie("/devices/status/keyboard/hyper", getModHyper);
134
135   _key_event->tie("key", SGRawValuePointer<int>(&_key_code));
136   _key_event->tie("pressed", SGRawValuePointer<bool>(&_key_pressed));
137   _key_event->tie("modifier", SGRawValuePointer<int>(&_key_modifiers));
138   _key_event->tie("modifier/shift", SGRawValuePointer<bool>(&_key_shift));
139   _key_event->tie("modifier/ctrl", SGRawValuePointer<bool>(&_key_ctrl));
140   _key_event->tie("modifier/alt", SGRawValuePointer<bool>(&_key_alt));
141   _key_event->tie("modifier/meta", SGRawValuePointer<bool>(&_key_meta));
142   _key_event->tie("modifier/super", SGRawValuePointer<bool>(&_key_super));
143   _key_event->tie("modifier/hyper", SGRawValuePointer<bool>(&_key_hyper));
144 }
145
146 void FGKeyboardInput::unbind()
147 {
148   fgUntie("/devices/status/keyboard");
149   fgUntie("/devices/status/keyboard/shift");
150   fgUntie("/devices/status/keyboard/ctrl");
151   fgUntie("/devices/status/keyboard/alt");
152   fgUntie("/devices/status/keyboard/meta");
153   fgUntie("/devices/status/keyboard/super");
154   fgUntie("/devices/status/keyboard/hyper");
155
156   _key_event->untie("key");
157   _key_event->untie("pressed");
158   _key_event->untie("modifier");
159   _key_event->untie("modifier/shift");
160   _key_event->untie("modifier/ctrl");
161   _key_event->untie("modifier/alt");
162   _key_event->untie("modifier/meta");
163   _key_event->untie("modifier/super");
164   _key_event->untie("modifier/hyper");
165 }
166
167 void FGKeyboardInput::update( double dt )
168 {
169   // nothing to do
170 }
171
172 const FGCommonInput::binding_list_t & FGKeyboardInput::_find_key_bindings (unsigned int k, int modifiers)
173 {
174   unsigned char kc = (unsigned char)k;
175   FGButton &b = bindings[k];
176
177                                 // Try it straight, first.
178   if (b.bindings[modifiers].size() > 0)
179     return b.bindings[modifiers];
180
181                                 // Alt-Gr is CTRL+ALT
182   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
183     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
184
185                                 // Try removing the control modifier
186                                 // for control keys.
187   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
188     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
189
190                                 // Try removing shift modifier 
191                                 // for upper case or any punctuation
192                                 // (since different keyboards will
193                                 // shift different punctuation types)
194   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
195     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
196
197                                 // Try removing alt modifier for
198                                 // high-bit characters.
199   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
200     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
201
202                                 // Give up and return the empty vector.
203   else
204     return b.bindings[modifiers];
205 }
206
207 void FGKeyboardInput::doKey (int k, int modifiers, int x, int y)
208 {
209   // Sanity check.
210   if (k < 0 || k >= MAX_KEYS) {
211     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
212     return;
213   }
214
215   _key_code = k;
216   _key_modifiers = modifiers >> 1;
217   _key_pressed = (modifiers & KEYMOD_RELEASED) == 0;
218   _key_shift = getModShift();
219   _key_ctrl = getModCtrl();
220   _key_alt = getModAlt();
221   _key_meta = getModMeta();
222   _key_super = getModSuper();
223   _key_hyper = getModHyper();
224   _key_event->fireValueChanged();
225   if (_key_code < 0)
226     return;
227
228   k = _key_code;
229   modifiers = _key_modifiers << 1;
230   if (!_key_pressed)
231       modifiers |= KEYMOD_RELEASED;
232   FGButton &b = bindings[k];
233
234                                 // Key pressed.
235   if (!(modifiers & KEYMOD_RELEASED)) {
236     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k << " with modifiers " << modifiers );
237     if (!b.last_state || b.is_repeatable) {
238       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
239
240       for (unsigned int i = 0; i < bindings.size(); i++)
241         bindings[i]->fire();
242       b.last_state = 1;
243     }
244   }
245                                 // Key released.
246   else {
247     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k << " with modifiers " << modifiers);
248     if (b.last_state) {
249       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
250       for (unsigned int i = 0; i < bindings.size(); i++)
251         bindings[i]->fire();
252       b.last_state = 0;
253     }
254   }
255 }
256
257 void FGKeyboardInput::keyHandler(int key, int keymod, int mousex, int mousey)
258 {
259   if((keymod & KEYMOD_RELEASED) == 0)
260       if(puKeyboard(key, PU_DOWN))
261           return;
262
263   if(keyboardInput)
264       keyboardInput->doKey(key, keymod, mousex, mousey);
265 }