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