1 /* 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> 21 22 #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> 28 29 namespace 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 */ 36 class KeyCharacterMap { 37 public: 38 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 47 // Substitute key code and meta state for fallback action. 48 struct FallbackAction { 49 int32_t keyCode; 50 int32_t metaState; 51 }; 52 53 ~KeyCharacterMap(); 54 55 static status_t load(const String8& filename, KeyCharacterMap** outMap); 56 static status_t loadByDeviceId(int32_t deviceId, KeyCharacterMap** outMap); 57 58 /* Gets the keyboard type. */ 59 int32_t getKeyboardType() const; 60 61 /* Gets the primary character for this key as in the label physically printed on it. 62 * Returns 0 if none (eg. for non-printing keys). */ 63 char16_t getDisplayLabel(int32_t keyCode) const; 64 65 /* Gets the Unicode character for the number or symbol generated by the key 66 * when the keyboard is used as a dialing pad. 67 * Returns 0 if no number or symbol is generated. 68 */ 69 char16_t getNumber(int32_t keyCode) const; 70 71 /* Gets the Unicode character generated by the key and meta key modifiers. 72 * Returns 0 if no character is generated. 73 */ 74 char16_t getCharacter(int32_t keyCode, int32_t metaState) const; 75 76 /* Gets the fallback action to use by default if the application does not 77 * handle the specified key. 78 * Returns true if an action was available, false if none. 79 */ 80 bool getFallbackAction(int32_t keyCode, int32_t metaState, 81 FallbackAction* outFallbackAction) const; 82 83 /* Gets the first matching Unicode character that can be generated by the key, 84 * preferring the one with the specified meta key modifiers. 85 * Returns 0 if no matching character is generated. 86 */ 87 char16_t getMatch(int32_t keyCode, const char16_t* chars, 88 size_t numChars, int32_t metaState) const; 89 90 /* Gets a sequence of key events that could plausibly generate the specified 91 * character sequence. Returns false if some of the characters cannot be generated. 92 */ 93 bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars, 94 Vector<KeyEvent>& outEvents) const; 95 96 private: 97 struct Behavior { 98 Behavior(); 99 100 /* The next behavior in the list, or NULL if none. */ 101 Behavior* next; 102 103 /* The meta key modifiers for this behavior. */ 104 int32_t metaState; 105 106 /* The character to insert. */ 107 char16_t character; 108 109 /* The fallback keycode if the key is not handled. */ 110 int32_t fallbackKeyCode; 111 }; 112 113 struct Key { 114 Key(); 115 ~Key(); 116 117 /* The single character label printed on the key, or 0 if none. */ 118 char16_t label; 119 120 /* The number or symbol character generated by the key, or 0 if none. */ 121 char16_t number; 122 123 /* The list of key behaviors sorted from most specific to least specific 124 * meta key binding. */ 125 Behavior* firstBehavior; 126 }; 127 128 class Parser { 129 enum State { 130 STATE_TOP = 0, 131 STATE_KEY = 1, 132 }; 133 134 enum { 135 PROPERTY_LABEL = 1, 136 PROPERTY_NUMBER = 2, 137 PROPERTY_META = 3, 138 }; 139 140 struct Property { 141 inline Property(int32_t property = 0, int32_t metaState = 0) : 142 property(property), metaState(metaState) { } 143 144 int32_t property; 145 int32_t metaState; 146 }; 147 148 KeyCharacterMap* mMap; 149 Tokenizer* mTokenizer; 150 State mState; 151 int32_t mKeyCode; 152 153 public: 154 Parser(KeyCharacterMap* map, Tokenizer* tokenizer); 155 ~Parser(); 156 status_t parse(); 157 158 private: 159 status_t parseType(); 160 status_t parseKey(); 161 status_t parseKeyProperty(); 162 status_t parseModifier(const String8& token, int32_t* outMetaState); 163 status_t parseCharacterLiteral(char16_t* outCharacter); 164 }; 165 166 KeyedVector<int32_t, Key*> mKeys; 167 int mType; 168 169 KeyCharacterMap(); 170 171 bool getKey(int32_t keyCode, const Key** outKey) const; 172 bool getKeyBehavior(int32_t keyCode, int32_t metaState, 173 const Key** outKey, const Behavior** outBehavior) const; 174 175 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const; 176 177 static void addKey(Vector<KeyEvent>& outEvents, 178 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time); 179 static void addMetaKeys(Vector<KeyEvent>& outEvents, 180 int32_t deviceId, int32_t metaState, bool down, nsecs_t time, 181 int32_t* currentMetaState); 182 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, 183 int32_t deviceId, int32_t metaState, bool down, nsecs_t time, 184 int32_t keyCode, int32_t keyMetaState, 185 int32_t* currentMetaState); 186 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, 187 int32_t deviceId, int32_t metaState, bool down, nsecs_t time, 188 int32_t leftKeyCode, int32_t leftKeyMetaState, 189 int32_t rightKeyCode, int32_t rightKeyMetaState, 190 int32_t eitherKeyMetaState, 191 int32_t* currentMetaState); 192 static void addLockedMetaKey(Vector<KeyEvent>& outEvents, 193 int32_t deviceId, int32_t metaState, nsecs_t time, 194 int32_t keyCode, int32_t keyMetaState, 195 int32_t* currentMetaState); 196 }; 197 198 } // namespace android 199 200 #endif // _UI_KEY_CHARACTER_MAP_H 201