blob: 79359277c70374ecab17487bc47fc71147966857 [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
Elliott Hughes6071da72015-08-12 15:27:47 -070022#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <binder/IBinder.h>
24#endif
25
26#include <input/Input.h>
27#include <utils/Errors.h>
28#include <utils/KeyedVector.h>
29#include <utils/Tokenizer.h>
30#include <utils/String8.h>
31#include <utils/Unicode.h>
32#include <utils/RefBase.h>
33
Michael Wright4c971c02015-10-21 14:38:03 +010034// Maximum number of keys supported by KeyCharacterMaps
35#define MAX_KEYS 8192
36
Jeff Brown5912f952013-07-01 19:10:31 -070037namespace 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 */
46class KeyCharacterMap : public RefBase {
47public:
48 enum KeyboardType {
49 KEYBOARD_TYPE_UNKNOWN = 0,
50 KEYBOARD_TYPE_NUMERIC = 1,
51 KEYBOARD_TYPE_PREDICTIVE = 2,
52 KEYBOARD_TYPE_ALPHA = 3,
53 KEYBOARD_TYPE_FULL = 4,
54 KEYBOARD_TYPE_SPECIAL_FUNCTION = 5,
55 KEYBOARD_TYPE_OVERLAY = 6,
56 };
57
58 enum Format {
59 // Base keyboard layout, may contain device-specific options, such as "type" declaration.
60 FORMAT_BASE = 0,
61 // Overlay keyboard layout, more restrictive, may be published by applications,
62 // cannot override device-specific options.
63 FORMAT_OVERLAY = 1,
64 // Either base or overlay layout ok.
65 FORMAT_ANY = 2,
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. */
75 static status_t load(const String8& filename, Format format, sp<KeyCharacterMap>* outMap);
76
77 /* Loads a key character map from its string contents. */
78 static status_t loadContents(const String8& filename,
79 const char* contents, Format format, sp<KeyCharacterMap>* outMap);
80
81 /* Combines a base key character map and an overlay. */
82 static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base,
83 const sp<KeyCharacterMap>& overlay);
84
85 /* Returns an empty key character map. */
86 static sp<KeyCharacterMap> empty();
87
88 /* Gets the keyboard type. */
89 int32_t getKeyboardType() const;
90
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
Elliott Hughes6071da72015-08-12 15:27:47 -0700135#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700136 /* Reads a key map from a parcel. */
137 static sp<KeyCharacterMap> readFromParcel(Parcel* parcel);
138
139 /* Writes a key map to a parcel. */
140 void writeToParcel(Parcel* parcel) const;
141#endif
142
143protected:
144 virtual ~KeyCharacterMap();
145
146private:
147 struct Behavior {
148 Behavior();
149 Behavior(const Behavior& other);
150
151 /* The next behavior in the list, or NULL if none. */
152 Behavior* next;
153
154 /* The meta key modifiers for this behavior. */
155 int32_t metaState;
156
157 /* The character to insert. */
158 char16_t character;
159
160 /* The fallback keycode if the key is not handled. */
161 int32_t fallbackKeyCode;
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700162
163 /* The replacement keycode if the key has to be replaced outright. */
164 int32_t replacementKeyCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700165 };
166
167 struct Key {
168 Key();
169 Key(const Key& other);
170 ~Key();
171
172 /* The single character label printed on the key, or 0 if none. */
173 char16_t label;
174
175 /* The number or symbol character generated by the key, or 0 if none. */
176 char16_t number;
177
178 /* The list of key behaviors sorted from most specific to least specific
179 * meta key binding. */
180 Behavior* firstBehavior;
181 };
182
183 class Parser {
184 enum State {
185 STATE_TOP = 0,
186 STATE_KEY = 1,
187 };
188
189 enum {
190 PROPERTY_LABEL = 1,
191 PROPERTY_NUMBER = 2,
192 PROPERTY_META = 3,
193 };
194
195 struct Property {
196 inline Property(int32_t property = 0, int32_t metaState = 0) :
197 property(property), metaState(metaState) { }
198
199 int32_t property;
200 int32_t metaState;
201 };
202
203 KeyCharacterMap* mMap;
204 Tokenizer* mTokenizer;
205 Format mFormat;
206 State mState;
207 int32_t mKeyCode;
208
209 public:
210 Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format);
211 ~Parser();
212 status_t parse();
213
214 private:
215 status_t parseType();
216 status_t parseMap();
217 status_t parseMapKey();
218 status_t parseKey();
219 status_t parseKeyProperty();
220 status_t finishKey(Key* key);
221 status_t parseModifier(const String8& token, int32_t* outMetaState);
222 status_t parseCharacterLiteral(char16_t* outCharacter);
223 };
224
225 static sp<KeyCharacterMap> sEmpty;
226
227 KeyedVector<int32_t, Key*> mKeys;
228 int mType;
229
230 KeyedVector<int32_t, int32_t> mKeysByScanCode;
231 KeyedVector<int32_t, int32_t> mKeysByUsageCode;
232
233 KeyCharacterMap();
234 KeyCharacterMap(const KeyCharacterMap& other);
235
236 bool getKey(int32_t keyCode, const Key** outKey) const;
237 bool getKeyBehavior(int32_t keyCode, int32_t metaState,
238 const Key** outKey, const Behavior** outBehavior) const;
239 static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState);
240
241 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
242
243 static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap);
244
245 static void addKey(Vector<KeyEvent>& outEvents,
246 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
247 static void addMetaKeys(Vector<KeyEvent>& outEvents,
248 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
249 int32_t* currentMetaState);
250 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
251 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
252 int32_t keyCode, int32_t keyMetaState,
253 int32_t* currentMetaState);
254 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
255 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
256 int32_t leftKeyCode, int32_t leftKeyMetaState,
257 int32_t rightKeyCode, int32_t rightKeyMetaState,
258 int32_t eitherKeyMetaState,
259 int32_t* currentMetaState);
260 static void addLockedMetaKey(Vector<KeyEvent>& outEvents,
261 int32_t deviceId, int32_t metaState, nsecs_t time,
262 int32_t keyCode, int32_t keyMetaState,
263 int32_t* currentMetaState);
264};
265
266} // namespace android
267
268#endif // _LIBINPUT_KEY_CHARACTER_MAP_H