| 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, [email protected] |
| 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 | |
| 33 | using simgear::PropertyList; |
| 34 | |
| 35 | static int getModifiers () |
| 36 | { |
| 37 | return fgGetKeyModifiers() >> 1; |
| 38 | } |
| 39 | |
| 40 | static bool getModShift () |
| 41 | { |
| 42 | return (fgGetKeyModifiers() & KEYMOD_SHIFT) != 0; |
| 43 | } |
| 44 | |
| 45 | static bool getModCtrl () |
| 46 | { |
| 47 | return (fgGetKeyModifiers() & KEYMOD_CTRL) != 0; |
| 48 | } |
| 49 | |
| 50 | static bool getModAlt () |
| 51 | { |
| 52 | return (fgGetKeyModifiers() & KEYMOD_ALT) != 0; |
| 53 | } |
| 54 | |
| 55 | static bool getModMeta () |
| 56 | { |
| 57 | return (fgGetKeyModifiers() & KEYMOD_META) != 0; |
| 58 | } |
| 59 | |
| 60 | static bool getModSuper () |
| 61 | { |
| 62 | return (fgGetKeyModifiers() & KEYMOD_SUPER) != 0; |
| 63 | } |
| 64 | |
| 65 | static bool getModHyper () |
| 66 | { |
| 67 | return (fgGetKeyModifiers() & KEYMOD_HYPER) != 0; |
| 68 | } |
| 69 | |
| 70 | FGKeyboardInput * FGKeyboardInput::keyboardInput = NULL; |
| 71 | |
| 72 | FGKeyboardInput::FGKeyboardInput() : |
| 73 | _key_event(fgGetNode("/devices/status/keyboard/event", true)), |
| 74 | _key_code(0), |
| 75 | _key_modifiers(0), |
| 76 | _key_pressed(0), |
| 77 | _key_shift(0), |
| 78 | _key_ctrl(0), |
| 79 | _key_alt(0), |
| 80 | _key_meta(0), |
| 81 | _key_super(0), |
| 82 | _key_hyper(0) |
| 83 | { |
| 84 | if( keyboardInput == NULL ) |
| 85 | keyboardInput = this; |
| 86 | } |
| 87 | |
| 88 | FGKeyboardInput::~FGKeyboardInput() |
| 89 | { |
| 90 | if( keyboardInput == this ) |
| 91 | keyboardInput = NULL; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void FGKeyboardInput::init() |
| 96 | { |
| 97 | fgRegisterKeyHandler(keyHandler); |
| 98 | } |
| 99 | |
| 100 | void FGKeyboardInput::postinit() |
| 101 | { |
| 102 | SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings"); |
| 103 | std::string module = "__kbd"; |
| 104 | SGPropertyNode * key_nodes = fgGetNode("/input/keyboard"); |
| 105 | if (key_nodes == NULL) { |
| 106 | SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!"); |
| 107 | key_nodes = fgGetNode("/input/keyboard", true); |
| 108 | } |
| 109 | |
| 110 | auto nasalsys = globals->get_subsystem<FGNasalSys>(); |
| 111 | PropertyList nasal = key_nodes->getChildren("nasal"); |
| 112 | for (unsigned int j = 0; j < nasal.size(); j++) { |
| 113 | nasal[j]->setStringValue("module", module.c_str()); |
| 114 | nasalsys->handleCommand(nasal[j], nullptr); |
| 115 | } |
| 116 | |
| 117 | PropertyList keys = key_nodes->getChildren("key"); |
| 118 | for (unsigned int i = 0; i < keys.size(); i++) { |
| 119 | int index = keys[i]->getIndex(); |
| 120 | SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index); |
| 121 | if( index >= MAX_KEYS ) { |
| 122 | SG_LOG(SG_INPUT, SG_WARN, "Key binding " << index << " out of range"); |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | bindings[index].bindings->clear(); |
| 127 | bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable"); |
| 128 | bindings[index].last_state = 0; |
| 129 | read_bindings(keys[i], bindings[index].bindings, KEYMOD_NONE, module ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void FGKeyboardInput::bind() |
| 134 | { |
| 135 | _tiedProperties.setRoot(fgGetNode("/devices/status", true)); |
| 136 | _tiedProperties.Tie<int>("keyboard", getModifiers); |
| 137 | _tiedProperties.Tie<bool>("keyboard/shift", getModShift); |
| 138 | _tiedProperties.Tie<bool>("keyboard/ctrl", getModCtrl); |
| 139 | _tiedProperties.Tie<bool>("keyboard/alt", getModAlt); |
| 140 | _tiedProperties.Tie<bool>("keyboard/meta", getModMeta); |
| 141 | _tiedProperties.Tie<bool>("keyboard/super", getModSuper); |
| 142 | _tiedProperties.Tie<bool>("keyboard/hyper", getModHyper); |
| 143 | |
| 144 | _tiedProperties.Tie(_key_event->getNode("key", true), SGRawValuePointer<int>(&_key_code)); |
| 145 | _tiedProperties.Tie(_key_event->getNode("pressed", true), SGRawValuePointer<bool>(&_key_pressed)); |
| 146 | _tiedProperties.Tie(_key_event->getNode("modifier", true), SGRawValuePointer<int>(&_key_modifiers)); |
| 147 | _tiedProperties.Tie(_key_event->getNode("modifier/shift", true), SGRawValuePointer<bool>(&_key_shift)); |
| 148 | _tiedProperties.Tie(_key_event->getNode("modifier/ctrl", true), SGRawValuePointer<bool>(&_key_ctrl)); |
| 149 | _tiedProperties.Tie(_key_event->getNode("modifier/alt", true), SGRawValuePointer<bool>(&_key_alt)); |
| 150 | _tiedProperties.Tie(_key_event->getNode("modifier/meta", true), SGRawValuePointer<bool>(&_key_meta)); |
| 151 | _tiedProperties.Tie(_key_event->getNode("modifier/super", true), SGRawValuePointer<bool>(&_key_super)); |
| 152 | _tiedProperties.Tie(_key_event->getNode("modifier/hyper", true), SGRawValuePointer<bool>(&_key_hyper)); |
| 153 | } |
| 154 | |
| 155 | void FGKeyboardInput::unbind() |
| 156 | { |
| 157 | _tiedProperties.Untie(); |
| 158 | } |
| 159 | |
| 160 | void FGKeyboardInput::update( double dt ) |
| 161 | { |
| 162 | // nothing to do |
| 163 | } |
| 164 | |
| 165 | const FGCommonInput::binding_list_t & FGKeyboardInput::_find_key_bindings (unsigned int k, int modifiers) |
| 166 | { |
| 167 | unsigned char kc = (unsigned char)k; |
| 168 | FGButton &b = bindings[k]; |
| 169 | |
| 170 | // Try it straight, first. |
| 171 | if (! b.bindings[modifiers].empty()) |
| 172 | return b.bindings[modifiers]; |
| 173 | |
| 174 | // Alt-Gr is CTRL+ALT |
| 175 | else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT)) |
| 176 | return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT)); |
| 177 | |
| 178 | // Try removing the control modifier |
| 179 | // for control keys. |
| 180 | else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc)) |
| 181 | return _find_key_bindings(k, modifiers&~KEYMOD_CTRL); |
| 182 | |
| 183 | // Try removing shift modifier |
| 184 | // for upper case or any punctuation |
| 185 | // (since different keyboards will |
| 186 | // shift different punctuation types) |
| 187 | else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc))) |
| 188 | return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT); |
| 189 | |
| 190 | // Try removing alt modifier for |
| 191 | // high-bit characters. |
| 192 | else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256) |
| 193 | return _find_key_bindings(k, modifiers&~KEYMOD_ALT); |
| 194 | |
| 195 | // Give up and return the empty vector. |
| 196 | else |
| 197 | return b.bindings[modifiers]; |
| 198 | } |
| 199 | |
| 200 | void FGKeyboardInput::doKey (int k, int modifiers, int x, int y) |
| 201 | { |
| 202 | // Sanity check. |
| 203 | if (k < 0 || k >= MAX_KEYS) { |
| 204 | // normal for unsupported keys (i.e. left/right shift key press events) |
| 205 | SG_LOG(SG_INPUT, SG_DEBUG, "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 | fireBindingList(bindings); |
| 234 | b.last_state = 1; |
| 235 | } |
| 236 | } |
| 237 | // Key released. |
| 238 | else { |
| 239 | SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k << " with modifiers " << modifiers); |
| 240 | if (b.last_state) { |
| 241 | const binding_list_t &bindings = _find_key_bindings(k, modifiers); |
| 242 | fireBindingList(bindings); |
| 243 | b.last_state = 0; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void FGKeyboardInput::keyHandler(int key, int keymod, int mousex, int mousey) |
| 249 | { |
| 250 | if( keyboardInput) |
| 251 | keyboardInput->doKey(key, keymod, mousex, mousey); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | // Register the subsystem. |
| 256 | SGSubsystemMgr::Registrant<FGKeyboardInput> registrantFGKeyboardInput( |
| 257 | SGSubsystemMgr::GENERAL, |
| 258 | {{"nasal", SGSubsystemMgr::Dependency::HARD}}); |