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> |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame^] | 21 | #include <list> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 22 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 23 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 24 | #include <binder/IBinder.h> |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 25 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 26 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 27 | #include <android-base/result.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | #include <input/Input.h> |
| 29 | #include <utils/Errors.h> |
| 30 | #include <utils/KeyedVector.h> |
| 31 | #include <utils/Tokenizer.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | #include <utils/Unicode.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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 | */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 46 | class KeyCharacterMap { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 47 | public: |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 48 | enum class KeyboardType : int32_t { |
| 49 | UNKNOWN = 0, |
| 50 | NUMERIC = 1, |
| 51 | PREDICTIVE = 2, |
| 52 | ALPHA = 3, |
| 53 | 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 | */ |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 57 | SPECIAL_FUNCTION = 5, |
| 58 | OVERLAY = 6, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 61 | enum class Format { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 62 | // Base keyboard layout, may contain device-specific options, such as "type" declaration. |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 63 | BASE = 0, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 64 | // Overlay keyboard layout, more restrictive, may be published by applications, |
| 65 | // cannot override device-specific options. |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 66 | OVERLAY = 1, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 67 | // Either base or overlay layout ok. |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 68 | ANY = 2, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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. */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 78 | static base::Result<std::shared_ptr<KeyCharacterMap>> load(const std::string& filename, |
| 79 | Format format); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 80 | |
| 81 | /* Loads a key character map from its string contents. */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 82 | static base::Result<std::shared_ptr<KeyCharacterMap>> loadContents(const std::string& filename, |
| 83 | const char* contents, |
| 84 | Format format); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 85 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 86 | const std::string getLoadFileName() const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 87 | |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 88 | /* Combines this key character map with the provided overlay. */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 89 | void combine(const KeyCharacterMap& overlay); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 90 | |
| 91 | /* Gets the keyboard type. */ |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 92 | KeyboardType getKeyboardType() const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 138 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 139 | /* Reads a key map from a parcel. */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 140 | static std::shared_ptr<KeyCharacterMap> readFromParcel(Parcel* parcel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 141 | |
| 142 | /* Writes a key map to a parcel. */ |
| 143 | void writeToParcel(Parcel* parcel) const; |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 144 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 145 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 146 | bool operator==(const KeyCharacterMap& other) const; |
| 147 | |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 148 | bool operator!=(const KeyCharacterMap& other) const; |
| 149 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 150 | KeyCharacterMap(const KeyCharacterMap& other); |
| 151 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 152 | virtual ~KeyCharacterMap(); |
| 153 | |
| 154 | private: |
| 155 | struct Behavior { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 156 | /* The meta key modifiers for this behavior. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame^] | 157 | int32_t metaState = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 158 | |
| 159 | /* The character to insert. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame^] | 160 | char16_t character = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 161 | |
| 162 | /* The fallback keycode if the key is not handled. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame^] | 163 | int32_t fallbackKeyCode = 0; |
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. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame^] | 166 | int32_t replacementKeyCode = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | struct Key { |
| 170 | Key(); |
| 171 | Key(const Key& other); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 172 | |
| 173 | /* The single character label printed on the key, or 0 if none. */ |
| 174 | char16_t label; |
| 175 | |
| 176 | /* The number or symbol character generated by the key, or 0 if none. */ |
| 177 | char16_t number; |
| 178 | |
| 179 | /* The list of key behaviors sorted from most specific to least specific |
| 180 | * meta key binding. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame^] | 181 | std::list<Behavior> behaviors; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | class Parser { |
| 185 | enum State { |
| 186 | STATE_TOP = 0, |
| 187 | STATE_KEY = 1, |
| 188 | }; |
| 189 | |
| 190 | enum { |
| 191 | PROPERTY_LABEL = 1, |
| 192 | PROPERTY_NUMBER = 2, |
| 193 | PROPERTY_META = 3, |
| 194 | }; |
| 195 | |
| 196 | struct Property { |
Chih-Hung Hsieh | f43b02c | 2018-12-20 15:45:56 -0800 | [diff] [blame] | 197 | inline explicit Property(int32_t property = 0, int32_t metaState = 0) : |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 198 | property(property), metaState(metaState) { } |
| 199 | |
| 200 | int32_t property; |
| 201 | int32_t metaState; |
| 202 | }; |
| 203 | |
| 204 | KeyCharacterMap* mMap; |
| 205 | Tokenizer* mTokenizer; |
| 206 | Format mFormat; |
| 207 | State mState; |
| 208 | int32_t mKeyCode; |
| 209 | |
| 210 | public: |
| 211 | Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format); |
| 212 | ~Parser(); |
| 213 | status_t parse(); |
| 214 | |
| 215 | private: |
| 216 | status_t parseType(); |
| 217 | status_t parseMap(); |
| 218 | status_t parseMapKey(); |
| 219 | status_t parseKey(); |
| 220 | status_t parseKeyProperty(); |
| 221 | status_t finishKey(Key* key); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 222 | status_t parseModifier(const std::string& token, int32_t* outMetaState); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 223 | status_t parseCharacterLiteral(char16_t* outCharacter); |
| 224 | }; |
| 225 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 226 | KeyedVector<int32_t, Key*> mKeys; |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 227 | KeyboardType mType; |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 228 | std::string mLoadFileName; |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 229 | bool mLayoutOverlayApplied; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 230 | |
| 231 | KeyedVector<int32_t, int32_t> mKeysByScanCode; |
| 232 | KeyedVector<int32_t, int32_t> mKeysByUsageCode; |
| 233 | |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 234 | KeyCharacterMap(const std::string& filename); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 235 | |
| 236 | bool getKey(int32_t keyCode, const Key** outKey) const; |
Siarhei Vishniakou | 12300c1 | 2022-08-05 12:30:36 -0700 | [diff] [blame] | 237 | const Behavior* getKeyBehavior(int32_t keyCode, int32_t metaState) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 238 | static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState); |
| 239 | |
| 240 | bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const; |
| 241 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 242 | static void addKey(Vector<KeyEvent>& outEvents, |
| 243 | int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time); |
| 244 | static void addMetaKeys(Vector<KeyEvent>& outEvents, |
| 245 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 246 | int32_t* currentMetaState); |
| 247 | static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, |
| 248 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 249 | int32_t keyCode, int32_t keyMetaState, |
| 250 | int32_t* currentMetaState); |
| 251 | static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, |
| 252 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 253 | int32_t leftKeyCode, int32_t leftKeyMetaState, |
| 254 | int32_t rightKeyCode, int32_t rightKeyMetaState, |
| 255 | int32_t eitherKeyMetaState, |
| 256 | int32_t* currentMetaState); |
| 257 | static void addLockedMetaKey(Vector<KeyEvent>& outEvents, |
| 258 | int32_t deviceId, int32_t metaState, nsecs_t time, |
| 259 | int32_t keyCode, int32_t keyMetaState, |
| 260 | int32_t* currentMetaState); |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 261 | |
| 262 | /* Clears all data stored in this key character map */ |
| 263 | void clear(); |
| 264 | |
| 265 | /* Loads the KeyCharacterMap provided by the tokenizer into this instance. */ |
| 266 | status_t load(Tokenizer* tokenizer, Format format); |
| 267 | |
| 268 | /* Reloads the data from mLoadFileName and unapplies any overlay. */ |
| 269 | status_t reloadBaseFromFile(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | } // namespace android |
| 273 | |
| 274 | #endif // _LIBINPUT_KEY_CHARACTER_MAP_H |