]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGKeyboardInput.cxx
Merge branch 'jmt/reciprocal'
[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 static int getModifiers ()
31 {
32   return fgGetKeyModifiers() >> 1;
33 }
34
35 static bool getModShift ()
36 {
37   return (fgGetKeyModifiers() & KEYMOD_SHIFT) != 0;
38 }
39
40 static bool getModCtrl ()
41 {
42   return (fgGetKeyModifiers() & KEYMOD_CTRL) != 0;
43 }
44
45 static bool getModAlt ()
46 {
47   return (fgGetKeyModifiers() & KEYMOD_ALT) != 0;
48 }
49
50 static bool getModMeta ()
51 {
52   return (fgGetKeyModifiers() & KEYMOD_META) != 0;
53 }
54
55 static bool getModSuper ()
56 {
57   return (fgGetKeyModifiers() & KEYMOD_SUPER) != 0;
58 }
59
60 static bool getModHyper ()
61 {
62   return (fgGetKeyModifiers() & KEYMOD_HYPER) != 0;
63 }
64
65 FGKeyboardInput * FGKeyboardInput::keyboardInput = NULL;
66
67 FGKeyboardInput::FGKeyboardInput() :
68     _key_event(fgGetNode("/devices/status/keyboard/event", true))
69 {
70   if( keyboardInput == NULL )
71     keyboardInput = this;
72 }
73
74 FGKeyboardInput::~FGKeyboardInput()
75 {
76   if( keyboardInput == this )
77     keyboardInput = NULL;
78 }
79
80
81 void FGKeyboardInput::init()
82 {
83   fgRegisterKeyHandler(keyHandler);
84 }
85
86 void FGKeyboardInput::postinit()
87 {
88   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
89   string module = "__kbd";
90   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
91   if (key_nodes == NULL) {
92     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
93     key_nodes = fgGetNode("/input/keyboard", true);
94   }
95
96   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
97   vector<SGPropertyNode_ptr> nasal = key_nodes->getChildren("nasal");
98   for (unsigned int j = 0; j < nasal.size(); j++) {
99     nasal[j]->setStringValue("module", module.c_str());
100     nasalsys->handleCommand(nasal[j]);
101   }
102
103   vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
104   for (unsigned int i = 0; i < keys.size(); i++) {
105     int index = keys[i]->getIndex();
106     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
107     if( index >= MAX_KEYS ) {
108       SG_LOG(SG_INPUT, SG_WARN, "Key binding " << index << " out of range");
109       continue;
110     }
111
112     bindings[index].bindings->clear();
113     bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
114     bindings[index].last_state = 0;
115     read_bindings(keys[i], bindings[index].bindings, KEYMOD_NONE, module );
116   }
117 }
118
119 void FGKeyboardInput::bind()
120 {
121   fgTie("/devices/status/keyboard", getModifiers);
122   fgTie("/devices/status/keyboard/shift", getModShift);
123   fgTie("/devices/status/keyboard/ctrl", getModCtrl);
124   fgTie("/devices/status/keyboard/alt", getModAlt);
125   fgTie("/devices/status/keyboard/meta", getModMeta);
126   fgTie("/devices/status/keyboard/super", getModSuper);
127   fgTie("/devices/status/keyboard/hyper", getModHyper);
128
129   _key_event->tie("key", SGRawValuePointer<int>(&_key_code));
130   _key_event->tie("pressed", SGRawValuePointer<bool>(&_key_pressed));
131   _key_event->tie("modifier", SGRawValuePointer<int>(&_key_modifiers));
132   _key_event->tie("modifier/shift", SGRawValuePointer<bool>(&_key_shift));
133   _key_event->tie("modifier/ctrl", SGRawValuePointer<bool>(&_key_ctrl));
134   _key_event->tie("modifier/alt", SGRawValuePointer<bool>(&_key_alt));
135   _key_event->tie("modifier/meta", SGRawValuePointer<bool>(&_key_meta));
136   _key_event->tie("modifier/super", SGRawValuePointer<bool>(&_key_super));
137   _key_event->tie("modifier/hyper", SGRawValuePointer<bool>(&_key_hyper));
138 }
139
140 void FGKeyboardInput::unbind()
141 {
142   fgUntie("/devices/status/keyboard");
143   fgUntie("/devices/status/keyboard/shift");
144   fgUntie("/devices/status/keyboard/ctrl");
145   fgUntie("/devices/status/keyboard/alt");
146   fgUntie("/devices/status/keyboard/meta");
147   fgUntie("/devices/status/keyboard/super");
148   fgUntie("/devices/status/keyboard/hyper");
149
150   _key_event->untie("key");
151   _key_event->untie("pressed");
152   _key_event->untie("modifier");
153   _key_event->untie("modifier/shift");
154   _key_event->untie("modifier/ctrl");
155   _key_event->untie("modifier/alt");
156   _key_event->untie("modifier/meta");
157   _key_event->untie("modifier/super");
158   _key_event->untie("modifier/hyper");
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     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
206     return;
207   }
208
209   _key_code = k;
210   _key_modifiers = modifiers >> 1;
211   _key_pressed = (modifiers & KEYMOD_RELEASED) == 0;
212   _key_shift = getModShift();
213   _key_ctrl = getModCtrl();
214   _key_alt = getModAlt();
215   _key_meta = getModMeta();
216   _key_super = getModSuper();
217   _key_hyper = getModHyper();
218   _key_event->fireValueChanged();
219   if (_key_code < 0)
220     return;
221
222   k = _key_code;
223   modifiers = _key_modifiers << 1;
224   if (!_key_pressed)
225       modifiers |= KEYMOD_RELEASED;
226   FGButton &b = bindings[k];
227
228                                 // Key pressed.
229   if (!(modifiers & KEYMOD_RELEASED)) {
230     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k << " with modifiers " << modifiers );
231     if (!b.last_state || b.is_repeatable) {
232       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
233
234       for (unsigned int i = 0; i < bindings.size(); i++)
235         bindings[i]->fire();
236       b.last_state = 1;
237     }
238   }
239                                 // Key released.
240   else {
241     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k << " with modifiers " << modifiers);
242     if (b.last_state) {
243       const binding_list_t &bindings = _find_key_bindings(k, modifiers);
244       for (unsigned int i = 0; i < bindings.size(); i++)
245         bindings[i]->fire();
246       b.last_state = 0;
247     }
248   }
249 }
250
251 void FGKeyboardInput::keyHandler(int key, int keymod, int mousex, int mousey)
252 {
253   if((keymod & KEYMOD_RELEASED) == 0)
254       if(puKeyboard(key, PU_DOWN))
255           return;
256
257   if(keyboardInput)
258       keyboardInput->doKey(key, keymod, mousex, mousey);
259 }