]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGKeyboardInput.cxx
Spring-cleaning: some minor optimization
[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     _key_code(0),
76     _key_modifiers(0),
77     _key_pressed(0),
78     _key_shift(0),
79     _key_ctrl(0),
80     _key_alt(0),
81     _key_meta(0),
82     _key_super(0),
83     _key_hyper(0)
84 {
85   if( keyboardInput == NULL )
86     keyboardInput = this;
87 }
88
89 FGKeyboardInput::~FGKeyboardInput()
90 {
91   if( keyboardInput == this )
92     keyboardInput = NULL;
93 }
94
95
96 void FGKeyboardInput::init()
97 {
98   fgRegisterKeyHandler(keyHandler);
99 }
100
101 void FGKeyboardInput::postinit()
102 {
103   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
104   string module = "__kbd";
105   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
106   if (key_nodes == NULL) {
107     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
108     key_nodes = fgGetNode("/input/keyboard", true);
109   }
110
111   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
112   PropertyList nasal = key_nodes->getChildren("nasal");
113   for (unsigned int j = 0; j < nasal.size(); j++) {
114     nasal[j]->setStringValue("module", module.c_str());
115     nasalsys->handleCommand(nasal[j]);
116   }
117
118   PropertyList keys = key_nodes->getChildren("key");
119   for (unsigned int i = 0; i < keys.size(); i++) {
120     int index = keys[i]->getIndex();
121     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
122     if( index >= MAX_KEYS ) {
123       SG_LOG(SG_INPUT, SG_WARN, "Key binding " << index << " out of range");
124       continue;
125     }
126
127     bindings[index].bindings->clear();
128     bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
129     bindings[index].last_state = 0;
130     read_bindings(keys[i], bindings[index].bindings, KEYMOD_NONE, module );
131   }
132 }
133
134 void FGKeyboardInput::bind()
135 {
136   _tiedProperties.setRoot(fgGetNode("/device/status", true));
137   _tiedProperties.Tie("keyboard",       getModifiers);
138   _tiedProperties.Tie("keyboard/shift", getModShift);
139   _tiedProperties.Tie("keyboard/ctrl",  getModCtrl);
140   _tiedProperties.Tie("keyboard/alt",   getModAlt);
141   _tiedProperties.Tie("keyboard/meta",  getModMeta);
142   _tiedProperties.Tie("keyboard/super", getModSuper);
143   _tiedProperties.Tie("keyboard/hyper", getModHyper);
144
145   _tiedProperties.Tie(_key_event->getNode("key", true),            SGRawValuePointer<int>(&_key_code));
146   _tiedProperties.Tie(_key_event->getNode("pressed", true),        SGRawValuePointer<bool>(&_key_pressed));
147   _tiedProperties.Tie(_key_event->getNode("modifier", true),       SGRawValuePointer<int>(&_key_modifiers));
148   _tiedProperties.Tie(_key_event->getNode("modifier/shift", true), SGRawValuePointer<bool>(&_key_shift));
149   _tiedProperties.Tie(_key_event->getNode("modifier/ctrl", true),  SGRawValuePointer<bool>(&_key_ctrl));
150   _tiedProperties.Tie(_key_event->getNode("modifier/alt", true),   SGRawValuePointer<bool>(&_key_alt));
151   _tiedProperties.Tie(_key_event->getNode("modifier/meta", true),  SGRawValuePointer<bool>(&_key_meta));
152   _tiedProperties.Tie(_key_event->getNode("modifier/super", true), SGRawValuePointer<bool>(&_key_super));
153   _tiedProperties.Tie(_key_event->getNode("modifier/hyper", true), SGRawValuePointer<bool>(&_key_hyper));
154 }
155
156 void FGKeyboardInput::unbind()
157 {
158   _tiedProperties.Untie();
159 }
160
161 void FGKeyboardInput::update( double dt )
162 {
163   // nothing to do
164 }
165
166 const FGCommonInput::binding_list_t & FGKeyboardInput::_find_key_bindings (unsigned int k, int modifiers)
167 {
168   unsigned char kc = (unsigned char)k;
169   FGButton &b = bindings[k];
170
171                                 // Try it straight, first.
172   if (b.bindings[modifiers].size() > 0)
173     return b.bindings[modifiers];
174
175                                 // Alt-Gr is CTRL+ALT
176   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
177     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
178
179                                 // Try removing the control modifier
180                                 // for control keys.
181   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
182     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
183
184                                 // Try removing shift modifier 
185                                 // for upper case or any punctuation
186                                 // (since different keyboards will
187                                 // shift different punctuation types)
188   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
189     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
190
191                                 // Try removing alt modifier for
192                                 // high-bit characters.
193   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
194     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
195
196                                 // Give up and return the empty vector.
197   else
198     return b.bindings[modifiers];
199 }
200
201 void FGKeyboardInput::doKey (int k, int modifiers, int x, int y)
202 {
203   // Sanity check.
204   if (k < 0 || k >= MAX_KEYS) {
205     // normal for unsupported keys (i.e. left/right shift key press events)
206     SG_LOG(SG_INPUT, SG_DEBUG, "Key value " << k << " out of range");
207     return;
208   }
209
210   _key_code = k;
211   _key_modifiers = modifiers >> 1;
212   _key_pressed = (modifiers & KEYMOD_RELEASED) == 0;
213   _key_shift = getModShift();
214   _key_ctrl = getModCtrl();
215   _key_alt = getModAlt();
216   _key_meta = getModMeta();
217   _key_super = getModSuper();
218   _key_hyper = getModHyper();
219   _key_event->fireValueChanged();
220   if (_key_code < 0)
221     return;
222
223   k = _key_code;
224   modifiers = _key_modifiers << 1;
225   if (!_key_pressed)
226       modifiers |= KEYMOD_RELEASED;
227   FGButton &b = bindings[k];
228
229                                 // Key pressed.
230   if (!(modifiers & KEYMOD_RELEASED)) {
231     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k << " with modifiers " << modifiers );
232     if (!b.last_state || b.is_repeatable) {
233       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
234
235       for (unsigned int i = 0; i < bindings.size(); i++)
236         bindings[i]->fire();
237       b.last_state = 1;
238     }
239   }
240                                 // Key released.
241   else {
242     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k << " with modifiers " << modifiers);
243     if (b.last_state) {
244       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
245       for (unsigned int i = 0; i < bindings.size(); i++)
246         bindings[i]->fire();
247       b.last_state = 0;
248     }
249   }
250 }
251
252 void FGKeyboardInput::keyHandler(int key, int keymod, int mousex, int mousey)
253 {
254   if((keymod & KEYMOD_RELEASED) == 0)
255       if(puKeyboard(key, PU_DOWN))
256           return;
257
258   if(keyboardInput)
259       keyboardInput->doKey(key, keymod, mousex, mousey);
260 }