1 // FGKeyboardInput.cxx -- handle user input from keyboard devices
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
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.
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.
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.
29 #include "FGKeyboardInput.hxx"
30 #include <Main/fg_props.hxx>
31 #include <Scripting/NasalSys.hxx>
34 using simgear::PropertyList;
36 static int getModifiers ()
38 return fgGetKeyModifiers() >> 1;
41 static bool getModShift ()
43 return (fgGetKeyModifiers() & KEYMOD_SHIFT) != 0;
46 static bool getModCtrl ()
48 return (fgGetKeyModifiers() & KEYMOD_CTRL) != 0;
51 static bool getModAlt ()
53 return (fgGetKeyModifiers() & KEYMOD_ALT) != 0;
56 static bool getModMeta ()
58 return (fgGetKeyModifiers() & KEYMOD_META) != 0;
61 static bool getModSuper ()
63 return (fgGetKeyModifiers() & KEYMOD_SUPER) != 0;
66 static bool getModHyper ()
68 return (fgGetKeyModifiers() & KEYMOD_HYPER) != 0;
71 FGKeyboardInput * FGKeyboardInput::keyboardInput = NULL;
73 FGKeyboardInput::FGKeyboardInput() :
74 _key_event(fgGetNode("/devices/status/keyboard/event", true)),
85 if( keyboardInput == NULL )
89 FGKeyboardInput::~FGKeyboardInput()
91 if( keyboardInput == this )
96 void FGKeyboardInput::init()
98 fgRegisterKeyHandler(keyHandler);
101 void FGKeyboardInput::postinit()
103 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
104 std::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);
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]);
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");
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 );
134 void FGKeyboardInput::bind()
136 _tiedProperties.setRoot(fgGetNode("/devices/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);
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));
156 void FGKeyboardInput::unbind()
158 _tiedProperties.Untie();
161 void FGKeyboardInput::update( double dt )
166 const FGCommonInput::binding_list_t & FGKeyboardInput::_find_key_bindings (unsigned int k, int modifiers)
168 unsigned char kc = (unsigned char)k;
169 FGButton &b = bindings[k];
171 // Try it straight, first.
172 if (! b.bindings[modifiers].empty())
173 return b.bindings[modifiers];
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));
179 // Try removing the control modifier
181 else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
182 return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
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);
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);
196 // Give up and return the empty vector.
198 return b.bindings[modifiers];
201 void FGKeyboardInput::doKey (int k, int modifiers, int x, int y)
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");
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();
224 modifiers = _key_modifiers << 1;
226 modifiers |= KEYMOD_RELEASED;
227 FGButton &b = bindings[k];
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 fireBindingList(bindings);
240 SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k << " with modifiers " << modifiers);
242 const binding_list_t &bindings = _find_key_bindings(k, modifiers);
243 fireBindingList(bindings);
249 void FGKeyboardInput::keyHandler(int key, int keymod, int mousex, int mousey)
251 if((keymod & KEYMOD_RELEASED) == 0)
252 if(puKeyboard(key, PU_DOWN))
256 keyboardInput->doKey(key, keymod, mousex, mousey);