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 | |
Prabir Pradhan | c08b0db | 2022-09-10 00:57:15 +0000 | [diff] [blame] | 17 | #pragma once |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 20 | #include <list> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 21 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 22 | #include <binder/IBinder.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 24 | #include <android-base/result.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | #include <input/Input.h> |
| 26 | #include <utils/Errors.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | #include <utils/Tokenizer.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | #include <utils/Unicode.h> |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 29 | #include <map> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | |
Michael Wright | 4c971c0 | 2015-10-21 14:38:03 +0100 | [diff] [blame] | 31 | // Maximum number of keys supported by KeyCharacterMaps |
| 32 | #define MAX_KEYS 8192 |
| 33 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | /** |
| 37 | * Describes a mapping from Android key codes to characters. |
| 38 | * Also specifies other functions of the keyboard such as the keyboard type |
| 39 | * and key modifier semantics. |
| 40 | * |
| 41 | * This object is immutable after it has been loaded. |
| 42 | */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 43 | class KeyCharacterMap { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 44 | public: |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 45 | enum class KeyboardType : int32_t { |
| 46 | UNKNOWN = 0, |
| 47 | NUMERIC = 1, |
| 48 | PREDICTIVE = 2, |
| 49 | ALPHA = 3, |
| 50 | FULL = 4, |
Siarhei Vishniakou | 61da25a | 2018-02-15 21:04:49 -0600 | [diff] [blame] | 51 | /** |
| 52 | * Deprecated. Set 'keyboard.specialFunction' to '1' in the device's IDC file instead. |
| 53 | */ |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 54 | SPECIAL_FUNCTION = 5, |
| 55 | OVERLAY = 6, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 58 | enum class Format { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 59 | // Base keyboard layout, may contain device-specific options, such as "type" declaration. |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 60 | BASE = 0, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 61 | // Overlay keyboard layout, more restrictive, may be published by applications, |
| 62 | // cannot override device-specific options. |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 63 | OVERLAY = 1, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 64 | // Either base or overlay layout ok. |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 65 | ANY = 2, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // Substitute key code and meta state for fallback action. |
| 69 | struct FallbackAction { |
| 70 | int32_t keyCode; |
| 71 | int32_t metaState; |
| 72 | }; |
| 73 | |
| 74 | /* Loads a key character map from a file. */ |
Linnan Li | d78dd9b | 2024-10-15 15:50:01 +0000 | [diff] [blame] | 75 | static base::Result<std::unique_ptr<KeyCharacterMap>> load(const std::string& filename, |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 76 | Format format); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 77 | |
| 78 | /* Loads a key character map from its string contents. */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 79 | static base::Result<std::shared_ptr<KeyCharacterMap>> loadContents(const std::string& filename, |
| 80 | const char* contents, |
| 81 | Format format); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 82 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 83 | const std::string getLoadFileName() const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 84 | |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 85 | /* Combines this key character map with the provided overlay. */ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 86 | void combine(const KeyCharacterMap& overlay); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 87 | |
Vaibhav Devmurari | 23e8ae9 | 2022-12-29 12:07:56 +0000 | [diff] [blame] | 88 | /* Clears already applied layout overlay */ |
| 89 | void clearLayoutOverlay(); |
| 90 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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 | |
Linnan Li | e5657f2 | 2024-09-13 21:54:37 +0800 | [diff] [blame] | 129 | /* Maps some Android key code to another Android key code. This mapping is applied after |
| 130 | * scanCode and usageCodes are mapped to corresponding Android Keycode */ |
| 131 | void setKeyRemapping(const std::map<int32_t, int32_t>& keyRemapping); |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 132 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 133 | /* Maps a scan code and usage code to a key code, in case this key map overrides |
| 134 | * the mapping in some way. */ |
| 135 | status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const; |
| 136 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 137 | /* Returns keycode after applying Android key code remapping defined in mKeyRemapping */ |
| 138 | int32_t applyKeyRemapping(int32_t fromKeyCode) const; |
| 139 | |
Vaibhav Devmurari | 5a71e88 | 2024-09-30 15:54:41 +0000 | [diff] [blame] | 140 | /** Returns list of keycodes that remap to provided keycode (@see setKeyRemapping()) */ |
| 141 | std::vector<int32_t> findKeyCodesMappedToKeyCode(int32_t toKeyCode) const; |
| 142 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 143 | /* Returns the <keyCode, metaState> pair after applying key behavior defined in the kcm file, |
| 144 | * that tries to find a replacement key code based on current meta state */ |
| 145 | std::pair<int32_t /*keyCode*/, int32_t /*metaState*/> applyKeyBehavior(int32_t keyCode, |
| 146 | int32_t metaState) const; |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 147 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 148 | /* Reads a key map from a parcel. */ |
Siarhei Vishniakou | c62948e | 2023-10-23 07:22:01 -0700 | [diff] [blame] | 149 | static std::unique_ptr<KeyCharacterMap> readFromParcel(Parcel* parcel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 150 | |
| 151 | /* Writes a key map to a parcel. */ |
| 152 | void writeToParcel(Parcel* parcel) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 153 | |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 154 | bool operator==(const KeyCharacterMap& other) const = default; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 155 | |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 156 | KeyCharacterMap(const KeyCharacterMap& other) = default; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 157 | |
| 158 | private: |
| 159 | struct Behavior { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 160 | /* The meta key modifiers for this behavior. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 161 | int32_t metaState = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 162 | |
| 163 | /* The character to insert. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 164 | char16_t character = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 165 | |
| 166 | /* The fallback keycode if the key is not handled. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 167 | int32_t fallbackKeyCode = 0; |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 168 | |
| 169 | /* The replacement keycode if the key has to be replaced outright. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 170 | int32_t replacementKeyCode = 0; |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 171 | |
| 172 | bool operator==(const Behavior&) const = default; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | struct Key { |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 176 | bool operator==(const Key&) const = default; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 177 | |
| 178 | /* The single character label printed on the key, or 0 if none. */ |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 179 | char16_t label = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 180 | |
| 181 | /* The number or symbol character generated by the key, or 0 if none. */ |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 182 | char16_t number = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 183 | |
| 184 | /* The list of key behaviors sorted from most specific to least specific |
| 185 | * meta key binding. */ |
Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 186 | std::list<Behavior> behaviors; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | class Parser { |
| 190 | enum State { |
| 191 | STATE_TOP = 0, |
| 192 | STATE_KEY = 1, |
| 193 | }; |
| 194 | |
| 195 | enum { |
| 196 | PROPERTY_LABEL = 1, |
| 197 | PROPERTY_NUMBER = 2, |
| 198 | PROPERTY_META = 3, |
| 199 | }; |
| 200 | |
| 201 | struct Property { |
Chih-Hung Hsieh | f43b02c | 2018-12-20 15:45:56 -0800 | [diff] [blame] | 202 | inline explicit Property(int32_t property = 0, int32_t metaState = 0) : |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 203 | property(property), metaState(metaState) { } |
| 204 | |
| 205 | int32_t property; |
| 206 | int32_t metaState; |
| 207 | }; |
| 208 | |
| 209 | KeyCharacterMap* mMap; |
| 210 | Tokenizer* mTokenizer; |
| 211 | Format mFormat; |
| 212 | State mState; |
| 213 | int32_t mKeyCode; |
| 214 | |
| 215 | public: |
| 216 | Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 217 | status_t parse(); |
| 218 | |
| 219 | private: |
| 220 | status_t parseType(); |
| 221 | status_t parseMap(); |
| 222 | status_t parseMapKey(); |
| 223 | status_t parseKey(); |
| 224 | status_t parseKeyProperty(); |
Siarhei Vishniakou | 5af2834 | 2023-04-17 08:45:31 -0700 | [diff] [blame] | 225 | status_t finishKey(Key& key); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 226 | status_t parseModifier(const std::string& token, int32_t* outMetaState); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 227 | status_t parseCharacterLiteral(char16_t* outCharacter); |
| 228 | }; |
| 229 | |
Siarhei Vishniakou | 2a916c4 | 2023-04-17 10:37:05 -0700 | [diff] [blame] | 230 | std::map<int32_t, Key> mKeys; |
| 231 | KeyboardType mType = KeyboardType::UNKNOWN; |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 232 | std::string mLoadFileName; |
Siarhei Vishniakou | 1e5ec0a | 2023-03-01 17:21:51 -0800 | [diff] [blame] | 233 | bool mLayoutOverlayApplied = false; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 234 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 235 | std::map<int32_t /* fromAndroidKeyCode */, int32_t /* toAndroidKeyCode */> mKeyRemapping; |
| 236 | std::map<int32_t /* fromScanCode */, int32_t /* toAndroidKeyCode */> mKeysByScanCode; |
| 237 | std::map<int32_t /* fromHidUsageCode */, int32_t /* toAndroidKeyCode */> mKeysByUsageCode; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 238 | |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 239 | KeyCharacterMap(const std::string& filename); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 240 | |
Siarhei Vishniakou | 0bae1a0 | 2023-04-17 09:00:59 -0700 | [diff] [blame] | 241 | const Key* getKey(int32_t keyCode) const; |
Siarhei Vishniakou | 12300c1 | 2022-08-05 12:30:36 -0700 | [diff] [blame] | 242 | const Behavior* getKeyBehavior(int32_t keyCode, int32_t metaState) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 243 | static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState); |
| 244 | |
| 245 | bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const; |
| 246 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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); |
Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 266 | |
| 267 | /* Clears all data stored in this key character map */ |
| 268 | void clear(); |
| 269 | |
| 270 | /* Loads the KeyCharacterMap provided by the tokenizer into this instance. */ |
| 271 | status_t load(Tokenizer* tokenizer, Format format); |
| 272 | |
| 273 | /* Reloads the data from mLoadFileName and unapplies any overlay. */ |
| 274 | status_t reloadBaseFromFile(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | } // namespace android |