| 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 |  | 
| Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 22 | #ifdef __linux__ | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | #include <binder/IBinder.h> | 
| Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 24 | #endif | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 26 | #include <android-base/result.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | #include <input/Input.h> | 
|  | 28 | #include <utils/Errors.h> | 
|  | 29 | #include <utils/KeyedVector.h> | 
|  | 30 | #include <utils/Tokenizer.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 31 | #include <utils/Unicode.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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 | */ | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 45 | class KeyCharacterMap { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 46 | public: | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 47 | enum class KeyboardType : int32_t { | 
|  | 48 | UNKNOWN = 0, | 
|  | 49 | NUMERIC = 1, | 
|  | 50 | PREDICTIVE = 2, | 
|  | 51 | ALPHA = 3, | 
|  | 52 | 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 | */ | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 56 | SPECIAL_FUNCTION = 5, | 
|  | 57 | OVERLAY = 6, | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 58 | }; | 
|  | 59 |  | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 60 | enum class Format { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 61 | // Base keyboard layout, may contain device-specific options, such as "type" declaration. | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 62 | BASE = 0, | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 63 | // Overlay keyboard layout, more restrictive, may be published by applications, | 
|  | 64 | // cannot override device-specific options. | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 65 | OVERLAY = 1, | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 66 | // Either base or overlay layout ok. | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 67 | ANY = 2, | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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. */ | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 77 | static base::Result<std::shared_ptr<KeyCharacterMap>> load(const std::string& filename, | 
|  | 78 | Format format); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 79 |  | 
|  | 80 | /* Loads a key character map from its string contents. */ | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 81 | static base::Result<std::shared_ptr<KeyCharacterMap>> loadContents(const std::string& filename, | 
|  | 82 | const char* contents, | 
|  | 83 | Format format); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 84 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 85 | const std::string getLoadFileName() const; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 86 |  | 
| Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 87 | /* Combines this key character map with the provided overlay. */ | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 88 | void combine(const KeyCharacterMap& overlay); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 89 |  | 
| Vaibhav Devmurari | 23e8ae9 | 2022-12-29 12:07:56 +0000 | [diff] [blame] | 90 | /* Clears already applied layout overlay */ | 
|  | 91 | void clearLayoutOverlay(); | 
|  | 92 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 93 | /* Gets the keyboard type. */ | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 94 | KeyboardType getKeyboardType() const; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | /* Gets the primary character for this key as in the label physically printed on it. | 
|  | 97 | * Returns 0 if none (eg. for non-printing keys). */ | 
|  | 98 | char16_t getDisplayLabel(int32_t keyCode) const; | 
|  | 99 |  | 
|  | 100 | /* Gets the Unicode character for the number or symbol generated by the key | 
|  | 101 | * when the keyboard is used as a dialing pad. | 
|  | 102 | * Returns 0 if no number or symbol is generated. | 
|  | 103 | */ | 
|  | 104 | char16_t getNumber(int32_t keyCode) const; | 
|  | 105 |  | 
|  | 106 | /* Gets the Unicode character generated by the key and meta key modifiers. | 
|  | 107 | * Returns 0 if no character is generated. | 
|  | 108 | */ | 
|  | 109 | char16_t getCharacter(int32_t keyCode, int32_t metaState) const; | 
|  | 110 |  | 
|  | 111 | /* Gets the fallback action to use by default if the application does not | 
|  | 112 | * handle the specified key. | 
|  | 113 | * Returns true if an action was available, false if none. | 
|  | 114 | */ | 
|  | 115 | bool getFallbackAction(int32_t keyCode, int32_t metaState, | 
|  | 116 | FallbackAction* outFallbackAction) const; | 
|  | 117 |  | 
|  | 118 | /* Gets the first matching Unicode character that can be generated by the key, | 
|  | 119 | * preferring the one with the specified meta key modifiers. | 
|  | 120 | * Returns 0 if no matching character is generated. | 
|  | 121 | */ | 
|  | 122 | char16_t getMatch(int32_t keyCode, const char16_t* chars, | 
|  | 123 | size_t numChars, int32_t metaState) const; | 
|  | 124 |  | 
|  | 125 | /* Gets a sequence of key events that could plausibly generate the specified | 
|  | 126 | * character sequence.  Returns false if some of the characters cannot be generated. | 
|  | 127 | */ | 
|  | 128 | bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars, | 
|  | 129 | Vector<KeyEvent>& outEvents) const; | 
|  | 130 |  | 
| Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 131 | /* Maps an Android key code to another Android key code. This mapping is applied after scanCode | 
|  | 132 | * and usageCodes are mapped to corresponding Android Keycode */ | 
|  | 133 | void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode); | 
|  | 134 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 135 | /* Maps a scan code and usage code to a key code, in case this key map overrides | 
|  | 136 | * the mapping in some way. */ | 
|  | 137 | status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const; | 
|  | 138 |  | 
| Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 139 | /* Returns keycode after applying Android key code remapping defined in mKeyRemapping */ | 
|  | 140 | int32_t applyKeyRemapping(int32_t fromKeyCode) const; | 
|  | 141 |  | 
|  | 142 | /* Returns the <keyCode, metaState> pair after applying key behavior defined in the kcm file, | 
|  | 143 | * that tries to find a replacement key code based on current meta state */ | 
|  | 144 | std::pair<int32_t /*keyCode*/, int32_t /*metaState*/> applyKeyBehavior(int32_t keyCode, | 
|  | 145 | int32_t metaState) const; | 
| Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 146 |  | 
| Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 147 | #ifdef __linux__ | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 148 | /* Reads a key map from a parcel. */ | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 149 | static std::shared_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; | 
| Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 153 | #endif | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 154 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 155 | bool operator==(const KeyCharacterMap& other) const; | 
|  | 156 |  | 
| Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 157 | bool operator!=(const KeyCharacterMap& other) const; | 
|  | 158 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 159 | KeyCharacterMap(const KeyCharacterMap& other); | 
|  | 160 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 161 | virtual ~KeyCharacterMap(); | 
|  | 162 |  | 
|  | 163 | private: | 
|  | 164 | struct Behavior { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 165 | /* The meta key modifiers for this behavior. */ | 
| Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 166 | int32_t metaState = 0; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 167 |  | 
|  | 168 | /* The character to insert. */ | 
| Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 169 | char16_t character = 0; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 170 |  | 
|  | 171 | /* The fallback keycode if the key is not handled. */ | 
| Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 172 | int32_t fallbackKeyCode = 0; | 
| Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | /* The replacement keycode if the key has to be replaced outright. */ | 
| Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 175 | int32_t replacementKeyCode = 0; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 176 | }; | 
|  | 177 |  | 
|  | 178 | struct Key { | 
|  | 179 | Key(); | 
|  | 180 | Key(const Key& other); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 181 |  | 
|  | 182 | /* The single character label printed on the key, or 0 if none. */ | 
|  | 183 | char16_t label; | 
|  | 184 |  | 
|  | 185 | /* The number or symbol character generated by the key, or 0 if none. */ | 
|  | 186 | char16_t number; | 
|  | 187 |  | 
|  | 188 | /* The list of key behaviors sorted from most specific to least specific | 
|  | 189 | * meta key binding. */ | 
| Siarhei Vishniakou | aa9e9d2 | 2022-08-05 11:13:31 -0700 | [diff] [blame] | 190 | std::list<Behavior> behaviors; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 191 | }; | 
|  | 192 |  | 
|  | 193 | class Parser { | 
|  | 194 | enum State { | 
|  | 195 | STATE_TOP = 0, | 
|  | 196 | STATE_KEY = 1, | 
|  | 197 | }; | 
|  | 198 |  | 
|  | 199 | enum { | 
|  | 200 | PROPERTY_LABEL = 1, | 
|  | 201 | PROPERTY_NUMBER = 2, | 
|  | 202 | PROPERTY_META = 3, | 
|  | 203 | }; | 
|  | 204 |  | 
|  | 205 | struct Property { | 
| Chih-Hung Hsieh | f43b02c | 2018-12-20 15:45:56 -0800 | [diff] [blame] | 206 | inline explicit Property(int32_t property = 0, int32_t metaState = 0) : | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 207 | property(property), metaState(metaState) { } | 
|  | 208 |  | 
|  | 209 | int32_t property; | 
|  | 210 | int32_t metaState; | 
|  | 211 | }; | 
|  | 212 |  | 
|  | 213 | KeyCharacterMap* mMap; | 
|  | 214 | Tokenizer* mTokenizer; | 
|  | 215 | Format mFormat; | 
|  | 216 | State mState; | 
|  | 217 | int32_t mKeyCode; | 
|  | 218 |  | 
|  | 219 | public: | 
|  | 220 | Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format); | 
|  | 221 | ~Parser(); | 
|  | 222 | status_t parse(); | 
|  | 223 |  | 
|  | 224 | private: | 
|  | 225 | status_t parseType(); | 
|  | 226 | status_t parseMap(); | 
|  | 227 | status_t parseMapKey(); | 
|  | 228 | status_t parseKey(); | 
|  | 229 | status_t parseKeyProperty(); | 
|  | 230 | status_t finishKey(Key* key); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 231 | status_t parseModifier(const std::string& token, int32_t* outMetaState); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 232 | status_t parseCharacterLiteral(char16_t* outCharacter); | 
|  | 233 | }; | 
|  | 234 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 235 | KeyedVector<int32_t, Key*> mKeys; | 
| Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 236 | KeyboardType mType; | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 237 | std::string mLoadFileName; | 
| Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 238 | bool mLayoutOverlayApplied; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 239 |  | 
| Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 240 | std::map<int32_t /* fromAndroidKeyCode */, int32_t /* toAndroidKeyCode */> mKeyRemapping; | 
|  | 241 | std::map<int32_t /* fromScanCode */, int32_t /* toAndroidKeyCode */> mKeysByScanCode; | 
|  | 242 | std::map<int32_t /* fromHidUsageCode */, int32_t /* toAndroidKeyCode */> mKeysByUsageCode; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 243 |  | 
| Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 244 | KeyCharacterMap(const std::string& filename); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 245 |  | 
|  | 246 | bool getKey(int32_t keyCode, const Key** outKey) const; | 
| Siarhei Vishniakou | 12300c1 | 2022-08-05 12:30:36 -0700 | [diff] [blame] | 247 | const Behavior* getKeyBehavior(int32_t keyCode, int32_t metaState) const; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 248 | static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState); | 
|  | 249 |  | 
|  | 250 | bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const; | 
|  | 251 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 252 | static void addKey(Vector<KeyEvent>& outEvents, | 
|  | 253 | int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time); | 
|  | 254 | static void addMetaKeys(Vector<KeyEvent>& outEvents, | 
|  | 255 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, | 
|  | 256 | int32_t* currentMetaState); | 
|  | 257 | static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, | 
|  | 258 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, | 
|  | 259 | int32_t keyCode, int32_t keyMetaState, | 
|  | 260 | int32_t* currentMetaState); | 
|  | 261 | static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, | 
|  | 262 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, | 
|  | 263 | int32_t leftKeyCode, int32_t leftKeyMetaState, | 
|  | 264 | int32_t rightKeyCode, int32_t rightKeyMetaState, | 
|  | 265 | int32_t eitherKeyMetaState, | 
|  | 266 | int32_t* currentMetaState); | 
|  | 267 | static void addLockedMetaKey(Vector<KeyEvent>& outEvents, | 
|  | 268 | int32_t deviceId, int32_t metaState, nsecs_t time, | 
|  | 269 | int32_t keyCode, int32_t keyMetaState, | 
|  | 270 | int32_t* currentMetaState); | 
| Philip Junker | 90bc949 | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 271 |  | 
|  | 272 | /* Clears all data stored in this key character map */ | 
|  | 273 | void clear(); | 
|  | 274 |  | 
|  | 275 | /* Loads the KeyCharacterMap provided by the tokenizer into this instance. */ | 
|  | 276 | status_t load(Tokenizer* tokenizer, Format format); | 
|  | 277 |  | 
|  | 278 | /* Reloads the data from mLoadFileName and unapplies any overlay. */ | 
|  | 279 | status_t reloadBaseFromFile(); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 280 | }; | 
|  | 281 |  | 
|  | 282 | } // namespace android |