blob: 867a08955c35af6859933b6b57b6eb66df0e9faa [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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 Pradhanc08b0db2022-09-10 00:57:15 +000017#pragma once
Jeff Brown5912f952013-07-01 19:10:31 -070018
19#include <stdint.h>
Siarhei Vishniakouaa9e9d22022-08-05 11:13:31 -070020#include <list>
Jeff Brown5912f952013-07-01 19:10:31 -070021
Brett Chabotfaa986c2020-11-04 17:39:36 -080022#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <binder/IBinder.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080024#endif
Jeff Brown5912f952013-07-01 19:10:31 -070025
Chris Ye3a1e4462020-08-12 10:13:15 -070026#include <android-base/result.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <input/Input.h>
28#include <utils/Errors.h>
29#include <utils/KeyedVector.h>
30#include <utils/Tokenizer.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <utils/Unicode.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032
Michael Wright4c971c02015-10-21 14:38:03 +010033// Maximum number of keys supported by KeyCharacterMaps
34#define MAX_KEYS 8192
35
Jeff Brown5912f952013-07-01 19:10:31 -070036namespace 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 Ye3a1e4462020-08-12 10:13:15 -070045class KeyCharacterMap {
Jeff Brown5912f952013-07-01 19:10:31 -070046public:
Michael Wright102936e2020-11-04 03:44:27 +000047 enum class KeyboardType : int32_t {
48 UNKNOWN = 0,
49 NUMERIC = 1,
50 PREDICTIVE = 2,
51 ALPHA = 3,
52 FULL = 4,
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -060053 /**
54 * Deprecated. Set 'keyboard.specialFunction' to '1' in the device's IDC file instead.
55 */
Michael Wright102936e2020-11-04 03:44:27 +000056 SPECIAL_FUNCTION = 5,
57 OVERLAY = 6,
Jeff Brown5912f952013-07-01 19:10:31 -070058 };
59
Michael Wright102936e2020-11-04 03:44:27 +000060 enum class Format {
Jeff Brown5912f952013-07-01 19:10:31 -070061 // Base keyboard layout, may contain device-specific options, such as "type" declaration.
Michael Wright102936e2020-11-04 03:44:27 +000062 BASE = 0,
Jeff Brown5912f952013-07-01 19:10:31 -070063 // Overlay keyboard layout, more restrictive, may be published by applications,
64 // cannot override device-specific options.
Michael Wright102936e2020-11-04 03:44:27 +000065 OVERLAY = 1,
Jeff Brown5912f952013-07-01 19:10:31 -070066 // Either base or overlay layout ok.
Michael Wright102936e2020-11-04 03:44:27 +000067 ANY = 2,
Jeff Brown5912f952013-07-01 19:10:31 -070068 };
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 Ye3a1e4462020-08-12 10:13:15 -070077 static base::Result<std::shared_ptr<KeyCharacterMap>> load(const std::string& filename,
78 Format format);
Jeff Brown5912f952013-07-01 19:10:31 -070079
80 /* Loads a key character map from its string contents. */
Chris Ye3a1e4462020-08-12 10:13:15 -070081 static base::Result<std::shared_ptr<KeyCharacterMap>> loadContents(const std::string& filename,
82 const char* contents,
83 Format format);
Jeff Brown5912f952013-07-01 19:10:31 -070084
Chris Ye3a1e4462020-08-12 10:13:15 -070085 const std::string getLoadFileName() const;
Jeff Brown5912f952013-07-01 19:10:31 -070086
Philip Junker90bc9492021-12-10 18:39:42 +010087 /* Combines this key character map with the provided overlay. */
Chris Ye3a1e4462020-08-12 10:13:15 -070088 void combine(const KeyCharacterMap& overlay);
Jeff Brown5912f952013-07-01 19:10:31 -070089
90 /* Gets the keyboard type. */
Michael Wright102936e2020-11-04 03:44:27 +000091 KeyboardType getKeyboardType() const;
Jeff Brown5912f952013-07-01 19:10:31 -070092
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
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000128 /* Maps an Android key code to another Android key code. This mapping is applied after scanCode
129 * and usageCodes are mapped to corresponding Android Keycode */
130 void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode);
131
Jeff Brown5912f952013-07-01 19:10:31 -0700132 /* Maps a scan code and usage code to a key code, in case this key map overrides
133 * the mapping in some way. */
134 status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const;
135
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000136 /* Returns keycode after applying Android key code remapping defined in mKeyRemapping */
137 int32_t applyKeyRemapping(int32_t fromKeyCode) const;
138
139 /* Returns the <keyCode, metaState> pair after applying key behavior defined in the kcm file,
140 * that tries to find a replacement key code based on current meta state */
141 std::pair<int32_t /*keyCode*/, int32_t /*metaState*/> applyKeyBehavior(int32_t keyCode,
142 int32_t metaState) const;
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700143
Brett Chabotfaa986c2020-11-04 17:39:36 -0800144#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700145 /* Reads a key map from a parcel. */
Chris Ye3a1e4462020-08-12 10:13:15 -0700146 static std::shared_ptr<KeyCharacterMap> readFromParcel(Parcel* parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700147
148 /* Writes a key map to a parcel. */
149 void writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800150#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700151
Chris Yef59a2f42020-10-16 12:55:26 -0700152 bool operator==(const KeyCharacterMap& other) const;
153
Philip Junker90bc9492021-12-10 18:39:42 +0100154 bool operator!=(const KeyCharacterMap& other) const;
155
Chris Ye3a1e4462020-08-12 10:13:15 -0700156 KeyCharacterMap(const KeyCharacterMap& other);
157
Jeff Brown5912f952013-07-01 19:10:31 -0700158 virtual ~KeyCharacterMap();
159
160private:
161 struct Behavior {
Jeff Brown5912f952013-07-01 19:10:31 -0700162 /* The meta key modifiers for this behavior. */
Siarhei Vishniakouaa9e9d22022-08-05 11:13:31 -0700163 int32_t metaState = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700164
165 /* The character to insert. */
Siarhei Vishniakouaa9e9d22022-08-05 11:13:31 -0700166 char16_t character = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700167
168 /* The fallback keycode if the key is not handled. */
Siarhei Vishniakouaa9e9d22022-08-05 11:13:31 -0700169 int32_t fallbackKeyCode = 0;
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700170
171 /* The replacement keycode if the key has to be replaced outright. */
Siarhei Vishniakouaa9e9d22022-08-05 11:13:31 -0700172 int32_t replacementKeyCode = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700173 };
174
175 struct Key {
176 Key();
177 Key(const Key& other);
Jeff Brown5912f952013-07-01 19:10:31 -0700178
179 /* The single character label printed on the key, or 0 if none. */
180 char16_t label;
181
182 /* The number or symbol character generated by the key, or 0 if none. */
183 char16_t number;
184
185 /* The list of key behaviors sorted from most specific to least specific
186 * meta key binding. */
Siarhei Vishniakouaa9e9d22022-08-05 11:13:31 -0700187 std::list<Behavior> behaviors;
Jeff Brown5912f952013-07-01 19:10:31 -0700188 };
189
190 class Parser {
191 enum State {
192 STATE_TOP = 0,
193 STATE_KEY = 1,
194 };
195
196 enum {
197 PROPERTY_LABEL = 1,
198 PROPERTY_NUMBER = 2,
199 PROPERTY_META = 3,
200 };
201
202 struct Property {
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800203 inline explicit Property(int32_t property = 0, int32_t metaState = 0) :
Jeff Brown5912f952013-07-01 19:10:31 -0700204 property(property), metaState(metaState) { }
205
206 int32_t property;
207 int32_t metaState;
208 };
209
210 KeyCharacterMap* mMap;
211 Tokenizer* mTokenizer;
212 Format mFormat;
213 State mState;
214 int32_t mKeyCode;
215
216 public:
217 Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format);
218 ~Parser();
219 status_t parse();
220
221 private:
222 status_t parseType();
223 status_t parseMap();
224 status_t parseMapKey();
225 status_t parseKey();
226 status_t parseKeyProperty();
227 status_t finishKey(Key* key);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100228 status_t parseModifier(const std::string& token, int32_t* outMetaState);
Jeff Brown5912f952013-07-01 19:10:31 -0700229 status_t parseCharacterLiteral(char16_t* outCharacter);
230 };
231
Jeff Brown5912f952013-07-01 19:10:31 -0700232 KeyedVector<int32_t, Key*> mKeys;
Michael Wright102936e2020-11-04 03:44:27 +0000233 KeyboardType mType;
Chris Ye3a1e4462020-08-12 10:13:15 -0700234 std::string mLoadFileName;
Philip Junker90bc9492021-12-10 18:39:42 +0100235 bool mLayoutOverlayApplied;
Jeff Brown5912f952013-07-01 19:10:31 -0700236
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000237 std::map<int32_t /* fromAndroidKeyCode */, int32_t /* toAndroidKeyCode */> mKeyRemapping;
238 std::map<int32_t /* fromScanCode */, int32_t /* toAndroidKeyCode */> mKeysByScanCode;
239 std::map<int32_t /* fromHidUsageCode */, int32_t /* toAndroidKeyCode */> mKeysByUsageCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700240
Philip Junker90bc9492021-12-10 18:39:42 +0100241 KeyCharacterMap(const std::string& filename);
Jeff Brown5912f952013-07-01 19:10:31 -0700242
243 bool getKey(int32_t keyCode, const Key** outKey) const;
Siarhei Vishniakou12300c12022-08-05 12:30:36 -0700244 const Behavior* getKeyBehavior(int32_t keyCode, int32_t metaState) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700245 static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState);
246
247 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
248
Jeff Brown5912f952013-07-01 19:10:31 -0700249 static void addKey(Vector<KeyEvent>& outEvents,
250 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
251 static void addMetaKeys(Vector<KeyEvent>& outEvents,
252 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
253 int32_t* currentMetaState);
254 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
255 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
256 int32_t keyCode, int32_t keyMetaState,
257 int32_t* currentMetaState);
258 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
259 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
260 int32_t leftKeyCode, int32_t leftKeyMetaState,
261 int32_t rightKeyCode, int32_t rightKeyMetaState,
262 int32_t eitherKeyMetaState,
263 int32_t* currentMetaState);
264 static void addLockedMetaKey(Vector<KeyEvent>& outEvents,
265 int32_t deviceId, int32_t metaState, nsecs_t time,
266 int32_t keyCode, int32_t keyMetaState,
267 int32_t* currentMetaState);
Philip Junker90bc9492021-12-10 18:39:42 +0100268
269 /* Clears all data stored in this key character map */
270 void clear();
271
272 /* Loads the KeyCharacterMap provided by the tokenizer into this instance. */
273 status_t load(Tokenizer* tokenizer, Format format);
274
275 /* Reloads the data from mLoadFileName and unapplies any overlay. */
276 status_t reloadBaseFromFile();
Jeff Brown5912f952013-07-01 19:10:31 -0700277};
278
279} // namespace android