blob: a1ccb37b048fa094f34aee18bb7df792c7df2844 [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
17#ifndef _UI_KEY_CHARACTER_MAP_H
18#define _UI_KEY_CHARACTER_MAP_H
19
20#include <stdint.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
Jeff Brown6b53e8d2010-11-10 16:03:06 -080022#include <ui/Input.h>
23#include <utils/Errors.h>
24#include <utils/KeyedVector.h>
25#include <utils/Tokenizer.h>
26#include <utils/String8.h>
27#include <utils/Unicode.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Jeff Brown6b53e8d2010-11-10 16:03:06 -080029namespace android {
30
31/**
32 * Describes a mapping from Android key codes to characters.
33 * Also specifies other functions of the keyboard such as the keyboard type
34 * and key modifier semantics.
35 */
36class KeyCharacterMap {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037public:
Jeff Brown6b53e8d2010-11-10 16:03:06 -080038 enum KeyboardType {
39 KEYBOARD_TYPE_UNKNOWN = 0,
40 KEYBOARD_TYPE_NUMERIC = 1,
41 KEYBOARD_TYPE_PREDICTIVE = 2,
42 KEYBOARD_TYPE_ALPHA = 3,
43 KEYBOARD_TYPE_FULL = 4,
44 KEYBOARD_TYPE_SPECIAL_FUNCTION = 5,
45 };
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 ~KeyCharacterMap();
48
Jeff Brown6b53e8d2010-11-10 16:03:06 -080049 static status_t load(const String8& filename, KeyCharacterMap** outMap);
50 static status_t loadByDeviceId(int32_t deviceId, KeyCharacterMap** outMap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Jeff Brown6b53e8d2010-11-10 16:03:06 -080052 /* Gets the keyboard type. */
53 int32_t getKeyboardType() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Jeff Brown6b53e8d2010-11-10 16:03:06 -080055 /* Gets the primary character for this key as in the label physically printed on it.
56 * Returns 0 if none (eg. for non-printing keys). */
57 char16_t getDisplayLabel(int32_t keyCode) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Jeff Brown6b53e8d2010-11-10 16:03:06 -080059 /* Gets the Unicode character for the number or symbol generated by the key
60 * when the keyboard is used as a dialing pad.
61 * Returns 0 if no number or symbol is generated.
62 */
63 char16_t getNumber(int32_t keyCode) const;
64
65 /* Gets the Unicode character generated by the key and meta key modifiers.
66 * Returns 0 if no character is generated.
67 */
68 char16_t getCharacter(int32_t keyCode, int32_t metaState) const;
69
70 /* Gets the first matching Unicode character that can be generated by the key,
71 * preferring the one with the specified meta key modifiers.
72 * Returns 0 if no matching character is generated.
73 */
74 char16_t getMatch(int32_t keyCode, const char16_t* chars,
75 size_t numChars, int32_t metaState) const;
76
77 /* Gets a sequence of key events that could plausibly generate the specified
78 * character sequence. Returns false if some of the characters cannot be generated.
79 */
80 bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars,
81 Vector<KeyEvent>& outEvents) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83private:
Jeff Brown6b53e8d2010-11-10 16:03:06 -080084 struct Behavior {
85 Behavior();
86
87 /* The next behavior in the list, or NULL if none. */
88 Behavior* next;
89
90 /* The meta key modifiers for this behavior. */
91 int32_t metaState;
92
93 /* The character to insert. */
94 char16_t character;
95
96 /* The fallback keycode if the key is not handled. */
97 int32_t fallbackKeyCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 };
99
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800100 struct Key {
101 Key();
102 ~Key();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800104 /* The single character label printed on the key, or 0 if none. */
105 char16_t label;
106
107 /* The number or symbol character generated by the key, or 0 if none. */
108 char16_t number;
109
110 /* The list of key behaviors sorted from most specific to least specific
111 * meta key binding. */
112 Behavior* firstBehavior;
113 };
114
115 class Parser {
116 enum State {
117 STATE_TOP = 0,
118 STATE_KEY = 1,
119 };
120
121 enum {
122 PROPERTY_LABEL = 1,
123 PROPERTY_NUMBER = 2,
124 PROPERTY_META = 3,
125 };
126
127 struct Property {
128 inline Property(int32_t property = 0, int32_t metaState = 0) :
129 property(property), metaState(metaState) { }
130
131 int32_t property;
132 int32_t metaState;
133 };
134
135 KeyCharacterMap* mMap;
136 Tokenizer* mTokenizer;
137 State mState;
138 int32_t mKeyCode;
139
140 public:
141 Parser(KeyCharacterMap* map, Tokenizer* tokenizer);
142 ~Parser();
143 status_t parse();
144
145 private:
146 status_t parseType();
147 status_t parseKey();
148 status_t parseKeyProperty();
149 status_t parseModifier(const String8& token, int32_t* outMetaState);
150 status_t parseCharacterLiteral(char16_t* outCharacter);
151 };
152
153 KeyedVector<int32_t, Key*> mKeys;
154 int mType;
155
156 KeyCharacterMap();
157
158 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
159
160 static void addKey(Vector<KeyEvent>& outEvents,
161 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
162 static void addMetaKeys(Vector<KeyEvent>& outEvents,
163 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
164 int32_t* currentMetaState);
165 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
166 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
167 int32_t keyCode, int32_t keyMetaState,
168 int32_t* currentMetaState);
169 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
170 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
171 int32_t leftKeyCode, int32_t leftKeyMetaState,
172 int32_t rightKeyCode, int32_t rightKeyMetaState,
173 int32_t eitherKeyMetaState,
174 int32_t* currentMetaState);
175 static void addLockedMetaKey(Vector<KeyEvent>& outEvents,
176 int32_t deviceId, int32_t metaState, nsecs_t time,
177 int32_t keyCode, int32_t keyMetaState,
178 int32_t* currentMetaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179};
180
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800181} // namespace android
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183#endif // _UI_KEY_CHARACTER_MAP_H