]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGKeyboardInput.cxx
Merge branch 'ehofman/atc'
[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 #include "FGKeyboardInput.hxx"
26 #include <Main/fg_props.hxx>
27 #include <Scripting/NasalSys.hxx>
28 #include <plib/pu.h>
29
30 using simgear::PropertyList;
31
32 static int getModifiers ()
33 {
34   return fgGetKeyModifiers() >> 1;
35 }
36
37 static bool getModShift ()
38 {
39   return (fgGetKeyModifiers() & KEYMOD_SHIFT) != 0;
40 }
41
42 static bool getModCtrl ()
43 {
44   return (fgGetKeyModifiers() & KEYMOD_CTRL) != 0;
45 }
46
47 static bool getModAlt ()
48 {
49   return (fgGetKeyModifiers() & KEYMOD_ALT) != 0;
50 }
51
52 static bool getModMeta ()
53 {
54   return (fgGetKeyModifiers() & KEYMOD_META) != 0;
55 }
56
57 static bool getModSuper ()
58 {
59   return (fgGetKeyModifiers() & KEYMOD_SUPER) != 0;
60 }
61
62 static bool getModHyper ()
63 {
64   return (fgGetKeyModifiers() & KEYMOD_HYPER) != 0;
65 }
66
67 FGKeyboardInput * FGKeyboardInput::keyboardInput = NULL;
68
69 FGKeyboardInput::FGKeyboardInput() :
70     _key_event(fgGetNode("/devices/status/keyboard/event", true))
71 {
72   if( keyboardInput == NULL )
73     keyboardInput = this;
74 }
75
76 FGKeyboardInput::~FGKeyboardInput()
77 {
78   if( keyboardInput == this )
79     keyboardInput = NULL;
80 }
81
82
83 void FGKeyboardInput::init()
84 {
85   fgRegisterKeyHandler(keyHandler);
86 }
87
88 void FGKeyboardInput::postinit()
89 {
90   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
91   string module = "__kbd";
92   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
93   if (key_nodes == NULL) {
94     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
95     key_nodes = fgGetNode("/input/keyboard", true);
96   }
97
98   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
99   PropertyList nasal = key_nodes->getChildren("nasal");
100   for (unsigned int j = 0; j < nasal.size(); j++) {
101     nasal[j]->setStringValue("module", module.c_str());
102     nasalsys->handleCommand(nasal[j]);
103   }
104
105   PropertyList keys = key_nodes->getChildren("key");
106   for (unsigned int i = 0; i < keys.size(); i++) {
107     int index = keys[i]->getIndex();
108     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
109     if( index >= MAX_KEYS ) {
110       SG_LOG(SG_INPUT, SG_WARN, "Key binding " << index << " out of range");
111       continue;
112     }
113
114     bindings[index].bindings->clear();
115     bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
116     bindings[index].last_state = 0;
117     read_bindings(keys[i], bindings[index].bindings, KEYMOD_NONE, module );
118   }
119 }
120
121 void FGKeyboardInput::bind()
122 {
123   fgTie("/devices/status/keyboard", getModifiers);
124   fgTie("/devices/status/keyboard/shift", getModShift);
125   fgTie("/devices/status/keyboard/ctrl", getModCtrl);
126   fgTie("/devices/status/keyboard/alt", getModAlt);
127   fgTie("/devices/status/keyboard/meta", getModMeta);
128   fgTie("/devices/status/keyboard/super", getModSuper);
129   fgTie("/devices/status/keyboard/hyper", getModHyper);
130
131   _key_event->tie("key", SGRawValuePointer<int>(&_key_code));
132   _key_event->tie("pressed", SGRawValuePointer<bool>(&_key_pressed));
133   _key_event->tie("modifier", SGRawValuePointer<int>(&_key_modifiers));
134   _key_event->tie("modifier/shift", SGRawValuePointer<bool>(&_key_shift));
135   _key_event->tie("modifier/ctrl", SGRawValuePointer<bool>(&_key_ctrl));
136   _key_event->tie("modifier/alt", SGRawValuePointer<bool>(&_key_alt));
137   _key_event->tie("modifier/meta", SGRawValuePointer<bool>(&_key_meta));
138   _key_event->tie("modifier/super", SGRawValuePointer<bool>(&_key_super));
139   _key_event->tie("modifier/hyper", SGRawValuePointer<bool>(&_key_hyper));
140 }
141
142 void FGKeyboardInput::unbind()
143 {
144   fgUntie("/devices/status/keyboard");
145   fgUntie("/devices/status/keyboard/shift");
146   fgUntie("/devices/status/keyboard/ctrl");
147   fgUntie("/devices/status/keyboard/alt");
148   fgUntie("/devices/status/keyboard/meta");
149   fgUntie("/devices/status/keyboard/super");
150   fgUntie("/devices/status/keyboard/hyper");
151
152   _key_event->untie("key");
153   _key_event->untie("pressed");
154   _key_event->untie("modifier");
155   _key_event->untie("modifier/shift");
156   _key_event->untie("modifier/ctrl");
157   _key_event->untie("modifier/alt");
158   _key_event->untie("modifier/meta");
159   _key_event->untie("modifier/super");
160   _key_event->untie("modifier/hyper");
161 }
162
163 void FGKeyboardInput::update( double dt )
164 {
165   // nothing to do
166 }
167
168 const FGCommonInput::binding_list_t & FGKeyboardInput::_find_key_bindings (unsigned int k, int modifiers)
169 {
170   unsigned char kc = (unsigned char)k;
171   FGButton &b = bindings[k];
172
173                                 // Try it straight, first.
174   if (b.bindings[modifiers].size() > 0)
175     return b.bindings[modifiers];
176
177                                 // Alt-Gr is CTRL+ALT
178   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
179     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
180
181                                 // Try removing the control modifier
182                                 // for control keys.
183   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
184     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
185
186                                 // Try removing shift modifier 
187                                 // for upper case or any punctuation
188                                 // (since different keyboards will
189                                 // shift different punctuation types)
190   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
191     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
192
193                                 // Try removing alt modifier for
194                                 // high-bit characters.
195   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
196     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
197
198                                 // Give up and return the empty vector.
199   else
200     return b.bindings[modifiers];
201 }
202
203 void FGKeyboardInput::doKey (int k, int modifiers, int x, int y)
204 {
205   // Sanity check.
206   if (k < 0 || k >= MAX_KEYS) {
207     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
208     return;
209   }
210
211   _key_code = k;
212   _key_modifiers = modifiers >> 1;
213   _key_pressed = (modifiers & KEYMOD_RELEASED) == 0;
214   _key_shift = getModShift();
215   _key_ctrl = getModCtrl();
216   _key_alt = getModAlt();
217   _key_meta = getModMeta();
218   _key_super = getModSuper();
219   _key_hyper = getModHyper();
220   _key_event->fireValueChanged();
221   if (_key_code < 0)
222     return;
223
224   k = _key_code;
225   modifiers = _key_modifiers << 1;
226   if (!_key_pressed)
227       modifiers |= KEYMOD_RELEASED;
228   FGButton &b = bindings[k];
229
230                                 // Key pressed.
231   if (!(modifiers & KEYMOD_RELEASED)) {
232     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k << " with modifiers " << modifiers );
233     if (!b.last_state || b.is_repeatable) {
234       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
235
236       for (unsigned int i = 0; i < bindings.size(); i++)
237         bindings[i]->fire();
238       b.last_state = 1;
239     }
240   }
241                                 // Key released.
242   else {
243     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k << " with modifiers " << modifiers);
244     if (b.last_state) {
245       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
246       for (unsigned int i = 0; i < bindings.size(); i++)
247         bindings[i]->fire();
248       b.last_state = 0;
249     }
250   }
251 }
252
253 void FGKeyboardInput::keyHandler(int key, int keymod, int mousex, int mousey)
254 {
255   if((keymod & KEYMOD_RELEASED) == 0)
256       if(puKeyboard(key, PU_DOWN))
257           return;
258
259   if(keyboardInput)
260       keyboardInput->doKey(key, keymod, mousex, mousey);
261 }