blob: 06799f9658427087c88e053edf6b27a80478c310 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080017#ifndef _ANDROIDFW_KEY_CHARACTER_MAP_H
18#define _ANDROIDFW_KEY_CHARACTER_MAP_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20#include <stdint.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
Jeff Brown9f25b7f2012-04-10 14:30:49 -070022#if HAVE_ANDROID_OS
23#include <binder/IBinder.h>
24#endif
25
Mathias Agopianb93a03f82012-02-17 15:34:57 -080026#include <androidfw/Input.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080027#include <utils/Errors.h>
28#include <utils/KeyedVector.h>
29#include <utils/Tokenizer.h>
30#include <utils/String8.h>
31#include <utils/Unicode.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070032#include <utils/RefBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Jeff Brown6b53e8d2010-11-10 16:03:06 -080034namespace 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.
Jeff Brown9f25b7f2012-04-10 14:30:49 -070040 *
41 * This object is immutable after it has been loaded.
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -070043class KeyCharacterMap : public RefBase {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044public:
Jeff Brown6b53e8d2010-11-10 16:03:06 -080045 enum KeyboardType {
46 KEYBOARD_TYPE_UNKNOWN = 0,
47 KEYBOARD_TYPE_NUMERIC = 1,
48 KEYBOARD_TYPE_PREDICTIVE = 2,
49 KEYBOARD_TYPE_ALPHA = 3,
50 KEYBOARD_TYPE_FULL = 4,
51 KEYBOARD_TYPE_SPECIAL_FUNCTION = 5,
Jeff Brown6ec6f792012-04-17 16:52:41 -070052 KEYBOARD_TYPE_OVERLAY = 6,
53 };
54
55 enum Format {
56 // Base keyboard layout, may contain device-specific options, such as "type" declaration.
57 FORMAT_BASE = 0,
58 // Overlay keyboard layout, more restrictive, may be published by applications,
59 // cannot override device-specific options.
60 FORMAT_OVERLAY = 1,
61 // Either base or overlay layout ok.
62 FORMAT_ANY = 2,
Jeff Brown6b53e8d2010-11-10 16:03:06 -080063 };
64
Jeff Brown49ed71d2010-12-06 17:13:33 -080065 // Substitute key code and meta state for fallback action.
66 struct FallbackAction {
67 int32_t keyCode;
68 int32_t metaState;
69 };
70
Jeff Brown9f25b7f2012-04-10 14:30:49 -070071 /* Loads a key character map from a file. */
Jeff Brown6ec6f792012-04-17 16:52:41 -070072 static status_t load(const String8& filename, Format format, sp<KeyCharacterMap>* outMap);
73
74 /* Loads a key character map from its string contents. */
75 static status_t loadContents(const String8& filename,
76 const char* contents, Format format, sp<KeyCharacterMap>* outMap);
77
78 /* Combines a base key character map and an overlay. */
79 static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base,
80 const sp<KeyCharacterMap>& overlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
Jeff Brown9f25b7f2012-04-10 14:30:49 -070082 /* Returns an empty key character map. */
83 static sp<KeyCharacterMap> empty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Jeff Brown6b53e8d2010-11-10 16:03:06 -080085 /* Gets the keyboard type. */
86 int32_t getKeyboardType() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Jeff Brown6b53e8d2010-11-10 16:03:06 -080088 /* Gets the primary character for this key as in the label physically printed on it.
89 * Returns 0 if none (eg. for non-printing keys). */
90 char16_t getDisplayLabel(int32_t keyCode) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Jeff Brown6b53e8d2010-11-10 16:03:06 -080092 /* Gets the Unicode character for the number or symbol generated by the key
93 * when the keyboard is used as a dialing pad.
94 * Returns 0 if no number or symbol is generated.
95 */
96 char16_t getNumber(int32_t keyCode) const;
97
98 /* Gets the Unicode character generated by the key and meta key modifiers.
99 * Returns 0 if no character is generated.
100 */
101 char16_t getCharacter(int32_t keyCode, int32_t metaState) const;
102
Jeff Brown49ed71d2010-12-06 17:13:33 -0800103 /* Gets the fallback action to use by default if the application does not
104 * handle the specified key.
105 * Returns true if an action was available, false if none.
106 */
107 bool getFallbackAction(int32_t keyCode, int32_t metaState,
108 FallbackAction* outFallbackAction) const;
109
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800110 /* Gets the first matching Unicode character that can be generated by the key,
111 * preferring the one with the specified meta key modifiers.
112 * Returns 0 if no matching character is generated.
113 */
114 char16_t getMatch(int32_t keyCode, const char16_t* chars,
115 size_t numChars, int32_t metaState) const;
116
117 /* Gets a sequence of key events that could plausibly generate the specified
118 * character sequence. Returns false if some of the characters cannot be generated.
119 */
120 bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars,
121 Vector<KeyEvent>& outEvents) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Jeff Brown4a3862f2012-04-17 18:50:05 -0700123 /* Maps a scan code and usage code to a key code, in case this key map overrides
124 * the mapping in some way. */
125 status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const;
126
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700127#if HAVE_ANDROID_OS
128 /* Reads a key map from a parcel. */
129 static sp<KeyCharacterMap> readFromParcel(Parcel* parcel);
130
131 /* Writes a key map to a parcel. */
132 void writeToParcel(Parcel* parcel) const;
133#endif
134
135protected:
136 virtual ~KeyCharacterMap();
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138private:
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800139 struct Behavior {
140 Behavior();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700141 Behavior(const Behavior& other);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800142
143 /* The next behavior in the list, or NULL if none. */
144 Behavior* next;
145
146 /* The meta key modifiers for this behavior. */
147 int32_t metaState;
148
149 /* The character to insert. */
150 char16_t character;
151
152 /* The fallback keycode if the key is not handled. */
153 int32_t fallbackKeyCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 };
155
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800156 struct Key {
157 Key();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700158 Key(const Key& other);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800159 ~Key();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800161 /* The single character label printed on the key, or 0 if none. */
162 char16_t label;
163
164 /* The number or symbol character generated by the key, or 0 if none. */
165 char16_t number;
166
167 /* The list of key behaviors sorted from most specific to least specific
168 * meta key binding. */
169 Behavior* firstBehavior;
170 };
171
172 class Parser {
173 enum State {
174 STATE_TOP = 0,
175 STATE_KEY = 1,
176 };
177
178 enum {
179 PROPERTY_LABEL = 1,
180 PROPERTY_NUMBER = 2,
181 PROPERTY_META = 3,
182 };
183
184 struct Property {
185 inline Property(int32_t property = 0, int32_t metaState = 0) :
186 property(property), metaState(metaState) { }
187
188 int32_t property;
189 int32_t metaState;
190 };
191
192 KeyCharacterMap* mMap;
193 Tokenizer* mTokenizer;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700194 Format mFormat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800195 State mState;
196 int32_t mKeyCode;
197
198 public:
Jeff Brown6ec6f792012-04-17 16:52:41 -0700199 Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800200 ~Parser();
201 status_t parse();
202
203 private:
204 status_t parseType();
Jeff Brown4a3862f2012-04-17 18:50:05 -0700205 status_t parseMap();
206 status_t parseMapKey();
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800207 status_t parseKey();
208 status_t parseKeyProperty();
Jeff Brown9a2bbf62012-04-18 14:09:10 -0700209 status_t finishKey(Key* key);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800210 status_t parseModifier(const String8& token, int32_t* outMetaState);
211 status_t parseCharacterLiteral(char16_t* outCharacter);
212 };
213
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700214 static sp<KeyCharacterMap> sEmpty;
215
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800216 KeyedVector<int32_t, Key*> mKeys;
217 int mType;
218
Jeff Brown4a3862f2012-04-17 18:50:05 -0700219 KeyedVector<int32_t, int32_t> mKeysByScanCode;
220 KeyedVector<int32_t, int32_t> mKeysByUsageCode;
221
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800222 KeyCharacterMap();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700223 KeyCharacterMap(const KeyCharacterMap& other);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800224
Jeff Brown49ed71d2010-12-06 17:13:33 -0800225 bool getKey(int32_t keyCode, const Key** outKey) const;
226 bool getKeyBehavior(int32_t keyCode, int32_t metaState,
227 const Key** outKey, const Behavior** outBehavior) const;
Jeff Brown9a2bbf62012-04-18 14:09:10 -0700228 static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800229
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800230 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
231
Jeff Brown6ec6f792012-04-17 16:52:41 -0700232 static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap);
233
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800234 static void addKey(Vector<KeyEvent>& outEvents,
235 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
236 static void addMetaKeys(Vector<KeyEvent>& outEvents,
237 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
238 int32_t* currentMetaState);
239 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
240 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
241 int32_t keyCode, int32_t keyMetaState,
242 int32_t* currentMetaState);
243 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
244 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
245 int32_t leftKeyCode, int32_t leftKeyMetaState,
246 int32_t rightKeyCode, int32_t rightKeyMetaState,
247 int32_t eitherKeyMetaState,
248 int32_t* currentMetaState);
249 static void addLockedMetaKey(Vector<KeyEvent>& outEvents,
250 int32_t deviceId, int32_t metaState, nsecs_t time,
251 int32_t keyCode, int32_t keyMetaState,
252 int32_t* currentMetaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253};
254
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800255} // namespace android
256
Mathias Agopianb93a03f82012-02-17 15:34:57 -0800257#endif // _ANDROIDFW_KEY_CHARACTER_MAP_H