blob: 8542577a8bbf9b64a1538aeba06141f1af0e7ceb [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
17#ifndef _LIBINPUT_KEY_CHARACTER_MAP_H
18#define _LIBINPUT_KEY_CHARACTER_MAP_H
19
20#include <stdint.h>
21
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <binder/IBinder.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023
Chris Ye3a1e4462020-08-12 10:13:15 -070024#include <android-base/result.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <input/Input.h>
26#include <utils/Errors.h>
27#include <utils/KeyedVector.h>
28#include <utils/Tokenizer.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <utils/Unicode.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
Michael Wright4c971c02015-10-21 14:38:03 +010031// Maximum number of keys supported by KeyCharacterMaps
32#define MAX_KEYS 8192
33
Jeff Brown5912f952013-07-01 19:10:31 -070034namespace 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 Ye3a1e4462020-08-12 10:13:15 -070043class KeyCharacterMap {
Jeff Brown5912f952013-07-01 19:10:31 -070044public:
Michael Wright102936e2020-11-04 03:44:27 +000045 enum class KeyboardType : int32_t {
46 UNKNOWN = 0,
47 NUMERIC = 1,
48 PREDICTIVE = 2,
49 ALPHA = 3,
50 FULL = 4,
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -060051 /**
52 * Deprecated. Set 'keyboard.specialFunction' to '1' in the device's IDC file instead.
53 */
Michael Wright102936e2020-11-04 03:44:27 +000054 SPECIAL_FUNCTION = 5,
55 OVERLAY = 6,
Jeff Brown5912f952013-07-01 19:10:31 -070056 };
57
Michael Wright102936e2020-11-04 03:44:27 +000058 enum class Format {
Jeff Brown5912f952013-07-01 19:10:31 -070059 // Base keyboard layout, may contain device-specific options, such as "type" declaration.
Michael Wright102936e2020-11-04 03:44:27 +000060 BASE = 0,
Jeff Brown5912f952013-07-01 19:10:31 -070061 // Overlay keyboard layout, more restrictive, may be published by applications,
62 // cannot override device-specific options.
Michael Wright102936e2020-11-04 03:44:27 +000063 OVERLAY = 1,
Jeff Brown5912f952013-07-01 19:10:31 -070064 // Either base or overlay layout ok.
Michael Wright102936e2020-11-04 03:44:27 +000065 ANY = 2,
Jeff Brown5912f952013-07-01 19:10:31 -070066 };
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. */
Chris Ye3a1e4462020-08-12 10:13:15 -070075 static base::Result<std::shared_ptr<KeyCharacterMap>> load(const std::string& filename,
76 Format format);
Jeff Brown5912f952013-07-01 19:10:31 -070077
78 /* Loads a key character map from its string contents. */
Chris Ye3a1e4462020-08-12 10:13:15 -070079 static base::Result<std::shared_ptr<KeyCharacterMap>> loadContents(const std::string& filename,
80 const char* contents,
81 Format format);
Jeff Brown5912f952013-07-01 19:10:31 -070082
Chris Ye3a1e4462020-08-12 10:13:15 -070083 const std::string getLoadFileName() const;
Jeff Brown5912f952013-07-01 19:10:31 -070084
Chris Ye3a1e4462020-08-12 10:13:15 -070085 /* Combines this key character map with an overlay. */
86 void combine(const KeyCharacterMap& overlay);
Jeff Brown5912f952013-07-01 19:10:31 -070087
88 /* Gets the keyboard type. */
Michael Wright102936e2020-11-04 03:44:27 +000089 KeyboardType getKeyboardType() const;
Jeff Brown5912f952013-07-01 19:10:31 -070090
91 /* Gets the primary character for this key as in the label physically printed on it.
92 * Returns 0 if none (eg. for non-printing keys). */
93 char16_t getDisplayLabel(int32_t keyCode) const;
94
95 /* Gets the Unicode character for the number or symbol generated by the key
96 * when the keyboard is used as a dialing pad.
97 * Returns 0 if no number or symbol is generated.
98 */
99 char16_t getNumber(int32_t keyCode) const;
100
101 /* Gets the Unicode character generated by the key and meta key modifiers.
102 * Returns 0 if no character is generated.
103 */
104 char16_t getCharacter(int32_t keyCode, int32_t metaState) const;
105
106 /* Gets the fallback action to use by default if the application does not
107 * handle the specified key.
108 * Returns true if an action was available, false if none.
109 */
110 bool getFallbackAction(int32_t keyCode, int32_t metaState,
111 FallbackAction* outFallbackAction) const;
112
113 /* Gets the first matching Unicode character that can be generated by the key,
114 * preferring the one with the specified meta key modifiers.
115 * Returns 0 if no matching character is generated.
116 */
117 char16_t getMatch(int32_t keyCode, const char16_t* chars,
118 size_t numChars, int32_t metaState) const;
119
120 /* Gets a sequence of key events that could plausibly generate the specified
121 * character sequence. Returns false if some of the characters cannot be generated.
122 */
123 bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars,
124 Vector<KeyEvent>& outEvents) const;
125
126 /* Maps a scan code and usage code to a key code, in case this key map overrides
127 * the mapping in some way. */
128 status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const;
129
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700130 /* Tries to find a replacement key code for a given key code and meta state
131 * in character map. */
132 void tryRemapKey(int32_t scanCode, int32_t metaState,
133 int32_t* outKeyCode, int32_t* outMetaState) const;
134
Jeff Brown5912f952013-07-01 19:10:31 -0700135 /* Reads a key map from a parcel. */
Chris Ye3a1e4462020-08-12 10:13:15 -0700136 static std::shared_ptr<KeyCharacterMap> readFromParcel(Parcel* parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700137
138 /* Writes a key map to a parcel. */
139 void writeToParcel(Parcel* parcel) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700140
Chris Ye3a1e4462020-08-12 10:13:15 -0700141 KeyCharacterMap(const KeyCharacterMap& other);
142
Jeff Brown5912f952013-07-01 19:10:31 -0700143 virtual ~KeyCharacterMap();
144
145private:
146 struct Behavior {
147 Behavior();
148 Behavior(const Behavior& other);
149
150 /* The next behavior in the list, or NULL if none. */
151 Behavior* next;
152
153 /* The meta key modifiers for this behavior. */
154 int32_t metaState;
155
156 /* The character to insert. */
157 char16_t character;
158
159 /* The fallback keycode if the key is not handled. */
160 int32_t fallbackKeyCode;
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700161
162 /* The replacement keycode if the key has to be replaced outright. */
163 int32_t replacementKeyCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700164 };
165
166 struct Key {
167 Key();
168 Key(const Key& other);
169 ~Key();
170
171 /* The single character label printed on the key, or 0 if none. */
172 char16_t label;
173
174 /* The number or symbol character generated by the key, or 0 if none. */
175 char16_t number;
176
177 /* The list of key behaviors sorted from most specific to least specific
178 * meta key binding. */
179 Behavior* firstBehavior;
180 };
181
182 class Parser {
183 enum State {
184 STATE_TOP = 0,
185 STATE_KEY = 1,
186 };
187
188 enum {
189 PROPERTY_LABEL = 1,
190 PROPERTY_NUMBER = 2,
191 PROPERTY_META = 3,
192 };
193
194 struct Property {
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800195 inline explicit Property(int32_t property = 0, int32_t metaState = 0) :
Jeff Brown5912f952013-07-01 19:10:31 -0700196 property(property), metaState(metaState) { }
197
198 int32_t property;
199 int32_t metaState;
200 };
201
202 KeyCharacterMap* mMap;
203 Tokenizer* mTokenizer;
204 Format mFormat;
205 State mState;
206 int32_t mKeyCode;
207
208 public:
209 Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format);
210 ~Parser();
211 status_t parse();
212
213 private:
214 status_t parseType();
215 status_t parseMap();
216 status_t parseMapKey();
217 status_t parseKey();
218 status_t parseKeyProperty();
219 status_t finishKey(Key* key);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100220 status_t parseModifier(const std::string& token, int32_t* outMetaState);
Jeff Brown5912f952013-07-01 19:10:31 -0700221 status_t parseCharacterLiteral(char16_t* outCharacter);
222 };
223
Jeff Brown5912f952013-07-01 19:10:31 -0700224 KeyedVector<int32_t, Key*> mKeys;
Michael Wright102936e2020-11-04 03:44:27 +0000225 KeyboardType mType;
Chris Ye3a1e4462020-08-12 10:13:15 -0700226 std::string mLoadFileName;
Jeff Brown5912f952013-07-01 19:10:31 -0700227
228 KeyedVector<int32_t, int32_t> mKeysByScanCode;
229 KeyedVector<int32_t, int32_t> mKeysByUsageCode;
230
231 KeyCharacterMap();
Jeff Brown5912f952013-07-01 19:10:31 -0700232
233 bool getKey(int32_t keyCode, const Key** outKey) const;
234 bool getKeyBehavior(int32_t keyCode, int32_t metaState,
235 const Key** outKey, const Behavior** outBehavior) const;
236 static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState);
237
238 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
239
Chris Ye3a1e4462020-08-12 10:13:15 -0700240 static base::Result<std::shared_ptr<KeyCharacterMap>> load(Tokenizer* tokenizer, Format format);
Jeff Brown5912f952013-07-01 19:10:31 -0700241
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);
261};
262
263} // namespace android
264
265#endif // _LIBINPUT_KEY_CHARACTER_MAP_H