| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #ifndef _LIBINPUT_KEY_CHARACTER_MAP_H | 
|  | 18 | #define _LIBINPUT_KEY_CHARACTER_MAP_H | 
|  | 19 |  | 
|  | 20 | #include <stdint.h> | 
|  | 21 |  | 
| Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 22 | #ifdef __ANDROID__ | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | #include <binder/IBinder.h> | 
|  | 24 | #endif | 
|  | 25 |  | 
|  | 26 | #include <input/Input.h> | 
|  | 27 | #include <utils/Errors.h> | 
|  | 28 | #include <utils/KeyedVector.h> | 
|  | 29 | #include <utils/Tokenizer.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | #include <utils/Unicode.h> | 
|  | 31 | #include <utils/RefBase.h> | 
|  | 32 |  | 
| Michael Wright | 4c971c0 | 2015-10-21 14:38:03 +0100 | [diff] [blame] | 33 | // Maximum number of keys supported by KeyCharacterMaps | 
|  | 34 | #define MAX_KEYS 8192 | 
|  | 35 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 36 | namespace android { | 
|  | 37 |  | 
|  | 38 | /** | 
|  | 39 | * Describes a mapping from Android key codes to characters. | 
|  | 40 | * Also specifies other functions of the keyboard such as the keyboard type | 
|  | 41 | * and key modifier semantics. | 
|  | 42 | * | 
|  | 43 | * This object is immutable after it has been loaded. | 
|  | 44 | */ | 
|  | 45 | class KeyCharacterMap : public RefBase { | 
|  | 46 | public: | 
|  | 47 | enum KeyboardType { | 
|  | 48 | KEYBOARD_TYPE_UNKNOWN = 0, | 
|  | 49 | KEYBOARD_TYPE_NUMERIC = 1, | 
|  | 50 | KEYBOARD_TYPE_PREDICTIVE = 2, | 
|  | 51 | KEYBOARD_TYPE_ALPHA = 3, | 
|  | 52 | KEYBOARD_TYPE_FULL = 4, | 
| Siarhei Vishniakou | 61da25a | 2018-02-15 21:04:49 -0600 | [diff] [blame] | 53 | /** | 
|  | 54 | * Deprecated. Set 'keyboard.specialFunction' to '1' in the device's IDC file instead. | 
|  | 55 | */ | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 56 | KEYBOARD_TYPE_SPECIAL_FUNCTION = 5, | 
|  | 57 | KEYBOARD_TYPE_OVERLAY = 6, | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | enum Format { | 
|  | 61 | // Base keyboard layout, may contain device-specific options, such as "type" declaration. | 
|  | 62 | FORMAT_BASE = 0, | 
|  | 63 | // Overlay keyboard layout, more restrictive, may be published by applications, | 
|  | 64 | // cannot override device-specific options. | 
|  | 65 | FORMAT_OVERLAY = 1, | 
|  | 66 | // Either base or overlay layout ok. | 
|  | 67 | FORMAT_ANY = 2, | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | // Substitute key code and meta state for fallback action. | 
|  | 71 | struct FallbackAction { | 
|  | 72 | int32_t keyCode; | 
|  | 73 | int32_t metaState; | 
|  | 74 | }; | 
|  | 75 |  | 
|  | 76 | /* Loads a key character map from a file. */ | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 77 | static status_t load(const std::string& filename, Format format, sp<KeyCharacterMap>* outMap); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | /* Loads a key character map from its string contents. */ | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 80 | static status_t loadContents(const std::string& filename, | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 81 | const char* contents, Format format, sp<KeyCharacterMap>* outMap); | 
|  | 82 |  | 
|  | 83 | /* Combines a base key character map and an overlay. */ | 
|  | 84 | static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base, | 
|  | 85 | const sp<KeyCharacterMap>& overlay); | 
|  | 86 |  | 
|  | 87 | /* Returns an empty key character map. */ | 
|  | 88 | static sp<KeyCharacterMap> empty(); | 
|  | 89 |  | 
|  | 90 | /* Gets the keyboard type. */ | 
|  | 91 | int32_t getKeyboardType() const; | 
|  | 92 |  | 
|  | 93 | /* Gets the primary character for this key as in the label physically printed on it. | 
|  | 94 | * Returns 0 if none (eg. for non-printing keys). */ | 
|  | 95 | char16_t getDisplayLabel(int32_t keyCode) const; | 
|  | 96 |  | 
|  | 97 | /* Gets the Unicode character for the number or symbol generated by the key | 
|  | 98 | * when the keyboard is used as a dialing pad. | 
|  | 99 | * Returns 0 if no number or symbol is generated. | 
|  | 100 | */ | 
|  | 101 | char16_t getNumber(int32_t keyCode) const; | 
|  | 102 |  | 
|  | 103 | /* Gets the Unicode character generated by the key and meta key modifiers. | 
|  | 104 | * Returns 0 if no character is generated. | 
|  | 105 | */ | 
|  | 106 | char16_t getCharacter(int32_t keyCode, int32_t metaState) const; | 
|  | 107 |  | 
|  | 108 | /* Gets the fallback action to use by default if the application does not | 
|  | 109 | * handle the specified key. | 
|  | 110 | * Returns true if an action was available, false if none. | 
|  | 111 | */ | 
|  | 112 | bool getFallbackAction(int32_t keyCode, int32_t metaState, | 
|  | 113 | FallbackAction* outFallbackAction) const; | 
|  | 114 |  | 
|  | 115 | /* Gets the first matching Unicode character that can be generated by the key, | 
|  | 116 | * preferring the one with the specified meta key modifiers. | 
|  | 117 | * Returns 0 if no matching character is generated. | 
|  | 118 | */ | 
|  | 119 | char16_t getMatch(int32_t keyCode, const char16_t* chars, | 
|  | 120 | size_t numChars, int32_t metaState) const; | 
|  | 121 |  | 
|  | 122 | /* Gets a sequence of key events that could plausibly generate the specified | 
|  | 123 | * character sequence.  Returns false if some of the characters cannot be generated. | 
|  | 124 | */ | 
|  | 125 | bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars, | 
|  | 126 | Vector<KeyEvent>& outEvents) const; | 
|  | 127 |  | 
|  | 128 | /* Maps a scan code and usage code to a key code, in case this key map overrides | 
|  | 129 | * the mapping in some way. */ | 
|  | 130 | status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const; | 
|  | 131 |  | 
| Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 132 | /* Tries to find a replacement key code for a given key code and meta state | 
|  | 133 | * in character map. */ | 
|  | 134 | void tryRemapKey(int32_t scanCode, int32_t metaState, | 
|  | 135 | int32_t* outKeyCode, int32_t* outMetaState) const; | 
|  | 136 |  | 
| Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 137 | #ifdef __ANDROID__ | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 138 | /* Reads a key map from a parcel. */ | 
|  | 139 | static sp<KeyCharacterMap> readFromParcel(Parcel* parcel); | 
|  | 140 |  | 
|  | 141 | /* Writes a key map to a parcel. */ | 
|  | 142 | void writeToParcel(Parcel* parcel) const; | 
|  | 143 | #endif | 
|  | 144 |  | 
|  | 145 | protected: | 
|  | 146 | virtual ~KeyCharacterMap(); | 
|  | 147 |  | 
|  | 148 | private: | 
|  | 149 | struct Behavior { | 
|  | 150 | Behavior(); | 
|  | 151 | Behavior(const Behavior& other); | 
|  | 152 |  | 
|  | 153 | /* The next behavior in the list, or NULL if none. */ | 
|  | 154 | Behavior* next; | 
|  | 155 |  | 
|  | 156 | /* The meta key modifiers for this behavior. */ | 
|  | 157 | int32_t metaState; | 
|  | 158 |  | 
|  | 159 | /* The character to insert. */ | 
|  | 160 | char16_t character; | 
|  | 161 |  | 
|  | 162 | /* The fallback keycode if the key is not handled. */ | 
|  | 163 | int32_t fallbackKeyCode; | 
| Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 164 |  | 
|  | 165 | /* The replacement keycode if the key has to be replaced outright. */ | 
|  | 166 | int32_t replacementKeyCode; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 167 | }; | 
|  | 168 |  | 
|  | 169 | struct Key { | 
|  | 170 | Key(); | 
|  | 171 | Key(const Key& other); | 
|  | 172 | ~Key(); | 
|  | 173 |  | 
|  | 174 | /* The single character label printed on the key, or 0 if none. */ | 
|  | 175 | char16_t label; | 
|  | 176 |  | 
|  | 177 | /* The number or symbol character generated by the key, or 0 if none. */ | 
|  | 178 | char16_t number; | 
|  | 179 |  | 
|  | 180 | /* The list of key behaviors sorted from most specific to least specific | 
|  | 181 | * meta key binding. */ | 
|  | 182 | Behavior* firstBehavior; | 
|  | 183 | }; | 
|  | 184 |  | 
|  | 185 | class Parser { | 
|  | 186 | enum State { | 
|  | 187 | STATE_TOP = 0, | 
|  | 188 | STATE_KEY = 1, | 
|  | 189 | }; | 
|  | 190 |  | 
|  | 191 | enum { | 
|  | 192 | PROPERTY_LABEL = 1, | 
|  | 193 | PROPERTY_NUMBER = 2, | 
|  | 194 | PROPERTY_META = 3, | 
|  | 195 | }; | 
|  | 196 |  | 
|  | 197 | struct Property { | 
| Chih-Hung Hsieh | f43b02c | 2018-12-20 15:45:56 -0800 | [diff] [blame] | 198 | inline explicit Property(int32_t property = 0, int32_t metaState = 0) : | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 199 | property(property), metaState(metaState) { } | 
|  | 200 |  | 
|  | 201 | int32_t property; | 
|  | 202 | int32_t metaState; | 
|  | 203 | }; | 
|  | 204 |  | 
|  | 205 | KeyCharacterMap* mMap; | 
|  | 206 | Tokenizer* mTokenizer; | 
|  | 207 | Format mFormat; | 
|  | 208 | State mState; | 
|  | 209 | int32_t mKeyCode; | 
|  | 210 |  | 
|  | 211 | public: | 
|  | 212 | Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format); | 
|  | 213 | ~Parser(); | 
|  | 214 | status_t parse(); | 
|  | 215 |  | 
|  | 216 | private: | 
|  | 217 | status_t parseType(); | 
|  | 218 | status_t parseMap(); | 
|  | 219 | status_t parseMapKey(); | 
|  | 220 | status_t parseKey(); | 
|  | 221 | status_t parseKeyProperty(); | 
|  | 222 | status_t finishKey(Key* key); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 223 | status_t parseModifier(const std::string& token, int32_t* outMetaState); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 224 | status_t parseCharacterLiteral(char16_t* outCharacter); | 
|  | 225 | }; | 
|  | 226 |  | 
|  | 227 | static sp<KeyCharacterMap> sEmpty; | 
|  | 228 |  | 
|  | 229 | KeyedVector<int32_t, Key*> mKeys; | 
|  | 230 | int mType; | 
|  | 231 |  | 
|  | 232 | KeyedVector<int32_t, int32_t> mKeysByScanCode; | 
|  | 233 | KeyedVector<int32_t, int32_t> mKeysByUsageCode; | 
|  | 234 |  | 
|  | 235 | KeyCharacterMap(); | 
|  | 236 | KeyCharacterMap(const KeyCharacterMap& other); | 
|  | 237 |  | 
|  | 238 | bool getKey(int32_t keyCode, const Key** outKey) const; | 
|  | 239 | bool getKeyBehavior(int32_t keyCode, int32_t metaState, | 
|  | 240 | const Key** outKey, const Behavior** outBehavior) const; | 
|  | 241 | static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState); | 
|  | 242 |  | 
|  | 243 | bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const; | 
|  | 244 |  | 
|  | 245 | static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap); | 
|  | 246 |  | 
|  | 247 | static void addKey(Vector<KeyEvent>& outEvents, | 
|  | 248 | int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time); | 
|  | 249 | static void addMetaKeys(Vector<KeyEvent>& outEvents, | 
|  | 250 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, | 
|  | 251 | int32_t* currentMetaState); | 
|  | 252 | static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, | 
|  | 253 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, | 
|  | 254 | int32_t keyCode, int32_t keyMetaState, | 
|  | 255 | int32_t* currentMetaState); | 
|  | 256 | static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, | 
|  | 257 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, | 
|  | 258 | int32_t leftKeyCode, int32_t leftKeyMetaState, | 
|  | 259 | int32_t rightKeyCode, int32_t rightKeyMetaState, | 
|  | 260 | int32_t eitherKeyMetaState, | 
|  | 261 | int32_t* currentMetaState); | 
|  | 262 | static void addLockedMetaKey(Vector<KeyEvent>& outEvents, | 
|  | 263 | int32_t deviceId, int32_t metaState, nsecs_t time, | 
|  | 264 | int32_t keyCode, int32_t keyMetaState, | 
|  | 265 | int32_t* currentMetaState); | 
|  | 266 | }; | 
|  | 267 |  | 
|  | 268 | } // namespace android | 
|  | 269 |  | 
|  | 270 | #endif // _LIBINPUT_KEY_CHARACTER_MAP_H |