1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/libui/KeyCharacterMap.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,257 @@ 1.4 +/* 1.5 + * Copyright (C) 2008 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#ifndef _ANDROIDFW_KEY_CHARACTER_MAP_H 1.21 +#define _ANDROIDFW_KEY_CHARACTER_MAP_H 1.22 + 1.23 +#include <stdint.h> 1.24 + 1.25 +#if HAVE_ANDROID_OS 1.26 +#include <binder/IBinder.h> 1.27 +#endif 1.28 + 1.29 +#include "Input.h" 1.30 +#include <utils/Errors.h> 1.31 +#include <utils/KeyedVector.h> 1.32 +#include "Tokenizer.h" 1.33 +#include <utils/String8.h> 1.34 +#include <utils/Unicode.h> 1.35 +#include <utils/RefBase.h> 1.36 + 1.37 +namespace android { 1.38 + 1.39 +/** 1.40 + * Describes a mapping from Android key codes to characters. 1.41 + * Also specifies other functions of the keyboard such as the keyboard type 1.42 + * and key modifier semantics. 1.43 + * 1.44 + * This object is immutable after it has been loaded. 1.45 + */ 1.46 +class KeyCharacterMap : public RefBase { 1.47 +public: 1.48 + enum KeyboardType { 1.49 + KEYBOARD_TYPE_UNKNOWN = 0, 1.50 + KEYBOARD_TYPE_NUMERIC = 1, 1.51 + KEYBOARD_TYPE_PREDICTIVE = 2, 1.52 + KEYBOARD_TYPE_ALPHA = 3, 1.53 + KEYBOARD_TYPE_FULL = 4, 1.54 + KEYBOARD_TYPE_SPECIAL_FUNCTION = 5, 1.55 + KEYBOARD_TYPE_OVERLAY = 6, 1.56 + }; 1.57 + 1.58 + enum Format { 1.59 + // Base keyboard layout, may contain device-specific options, such as "type" declaration. 1.60 + FORMAT_BASE = 0, 1.61 + // Overlay keyboard layout, more restrictive, may be published by applications, 1.62 + // cannot override device-specific options. 1.63 + FORMAT_OVERLAY = 1, 1.64 + // Either base or overlay layout ok. 1.65 + FORMAT_ANY = 2, 1.66 + }; 1.67 + 1.68 + // Substitute key code and meta state for fallback action. 1.69 + struct FallbackAction { 1.70 + int32_t keyCode; 1.71 + int32_t metaState; 1.72 + }; 1.73 + 1.74 + /* Loads a key character map from a file. */ 1.75 + static status_t load(const String8& filename, Format format, sp<KeyCharacterMap>* outMap); 1.76 + 1.77 + /* Loads a key character map from its string contents. */ 1.78 + static status_t loadContents(const String8& filename, 1.79 + const char* contents, Format format, sp<KeyCharacterMap>* outMap); 1.80 + 1.81 + /* Combines a base key character map and an overlay. */ 1.82 + static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base, 1.83 + const sp<KeyCharacterMap>& overlay); 1.84 + 1.85 + /* Returns an empty key character map. */ 1.86 + static sp<KeyCharacterMap> empty(); 1.87 + 1.88 + /* Gets the keyboard type. */ 1.89 + int32_t getKeyboardType() const; 1.90 + 1.91 + /* Gets the primary character for this key as in the label physically printed on it. 1.92 + * Returns 0 if none (eg. for non-printing keys). */ 1.93 + char16_t getDisplayLabel(int32_t keyCode) const; 1.94 + 1.95 + /* Gets the Unicode character for the number or symbol generated by the key 1.96 + * when the keyboard is used as a dialing pad. 1.97 + * Returns 0 if no number or symbol is generated. 1.98 + */ 1.99 + char16_t getNumber(int32_t keyCode) const; 1.100 + 1.101 + /* Gets the Unicode character generated by the key and meta key modifiers. 1.102 + * Returns 0 if no character is generated. 1.103 + */ 1.104 + char16_t getCharacter(int32_t keyCode, int32_t metaState) const; 1.105 + 1.106 + /* Gets the fallback action to use by default if the application does not 1.107 + * handle the specified key. 1.108 + * Returns true if an action was available, false if none. 1.109 + */ 1.110 + bool getFallbackAction(int32_t keyCode, int32_t metaState, 1.111 + FallbackAction* outFallbackAction) const; 1.112 + 1.113 + /* Gets the first matching Unicode character that can be generated by the key, 1.114 + * preferring the one with the specified meta key modifiers. 1.115 + * Returns 0 if no matching character is generated. 1.116 + */ 1.117 + char16_t getMatch(int32_t keyCode, const char16_t* chars, 1.118 + size_t numChars, int32_t metaState) const; 1.119 + 1.120 + /* Gets a sequence of key events that could plausibly generate the specified 1.121 + * character sequence. Returns false if some of the characters cannot be generated. 1.122 + */ 1.123 + bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars, 1.124 + Vector<KeyEvent>& outEvents) const; 1.125 + 1.126 + /* Maps a scan code and usage code to a key code, in case this key map overrides 1.127 + * the mapping in some way. */ 1.128 + status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const; 1.129 + 1.130 +#if HAVE_ANDROID_OS 1.131 + /* Reads a key map from a parcel. */ 1.132 + static sp<KeyCharacterMap> readFromParcel(Parcel* parcel); 1.133 + 1.134 + /* Writes a key map to a parcel. */ 1.135 + void writeToParcel(Parcel* parcel) const; 1.136 +#endif 1.137 + 1.138 +protected: 1.139 + virtual ~KeyCharacterMap(); 1.140 + 1.141 +private: 1.142 + struct Behavior { 1.143 + Behavior(); 1.144 + Behavior(const Behavior& other); 1.145 + 1.146 + /* The next behavior in the list, or NULL if none. */ 1.147 + Behavior* next; 1.148 + 1.149 + /* The meta key modifiers for this behavior. */ 1.150 + int32_t metaState; 1.151 + 1.152 + /* The character to insert. */ 1.153 + char16_t character; 1.154 + 1.155 + /* The fallback keycode if the key is not handled. */ 1.156 + int32_t fallbackKeyCode; 1.157 + }; 1.158 + 1.159 + struct Key { 1.160 + Key(); 1.161 + Key(const Key& other); 1.162 + ~Key(); 1.163 + 1.164 + /* The single character label printed on the key, or 0 if none. */ 1.165 + char16_t label; 1.166 + 1.167 + /* The number or symbol character generated by the key, or 0 if none. */ 1.168 + char16_t number; 1.169 + 1.170 + /* The list of key behaviors sorted from most specific to least specific 1.171 + * meta key binding. */ 1.172 + Behavior* firstBehavior; 1.173 + }; 1.174 + 1.175 + class Parser { 1.176 + enum State { 1.177 + STATE_TOP = 0, 1.178 + STATE_KEY = 1, 1.179 + }; 1.180 + 1.181 + enum { 1.182 + PROPERTY_LABEL = 1, 1.183 + PROPERTY_NUMBER = 2, 1.184 + PROPERTY_META = 3, 1.185 + }; 1.186 + 1.187 + struct Property { 1.188 + inline Property(int32_t property = 0, int32_t metaState = 0) : 1.189 + property(property), metaState(metaState) { } 1.190 + 1.191 + int32_t property; 1.192 + int32_t metaState; 1.193 + }; 1.194 + 1.195 + KeyCharacterMap* mMap; 1.196 + Tokenizer* mTokenizer; 1.197 + Format mFormat; 1.198 + State mState; 1.199 + int32_t mKeyCode; 1.200 + 1.201 + public: 1.202 + Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format); 1.203 + ~Parser(); 1.204 + status_t parse(); 1.205 + 1.206 + private: 1.207 + status_t parseType(); 1.208 + status_t parseMap(); 1.209 + status_t parseMapKey(); 1.210 + status_t parseKey(); 1.211 + status_t parseKeyProperty(); 1.212 + status_t finishKey(Key* key); 1.213 + status_t parseModifier(const String8& token, int32_t* outMetaState); 1.214 + status_t parseCharacterLiteral(char16_t* outCharacter); 1.215 + }; 1.216 + 1.217 + static sp<KeyCharacterMap> sEmpty; 1.218 + 1.219 + KeyedVector<int32_t, Key*> mKeys; 1.220 + int mType; 1.221 + 1.222 + KeyedVector<int32_t, int32_t> mKeysByScanCode; 1.223 + KeyedVector<int32_t, int32_t> mKeysByUsageCode; 1.224 + 1.225 + KeyCharacterMap(); 1.226 + KeyCharacterMap(const KeyCharacterMap& other); 1.227 + 1.228 + bool getKey(int32_t keyCode, const Key** outKey) const; 1.229 + bool getKeyBehavior(int32_t keyCode, int32_t metaState, 1.230 + const Key** outKey, const Behavior** outBehavior) const; 1.231 + static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState); 1.232 + 1.233 + bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const; 1.234 + 1.235 + static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap); 1.236 + 1.237 + static void addKey(Vector<KeyEvent>& outEvents, 1.238 + int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time); 1.239 + static void addMetaKeys(Vector<KeyEvent>& outEvents, 1.240 + int32_t deviceId, int32_t metaState, bool down, nsecs_t time, 1.241 + int32_t* currentMetaState); 1.242 + static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, 1.243 + int32_t deviceId, int32_t metaState, bool down, nsecs_t time, 1.244 + int32_t keyCode, int32_t keyMetaState, 1.245 + int32_t* currentMetaState); 1.246 + static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, 1.247 + int32_t deviceId, int32_t metaState, bool down, nsecs_t time, 1.248 + int32_t leftKeyCode, int32_t leftKeyMetaState, 1.249 + int32_t rightKeyCode, int32_t rightKeyMetaState, 1.250 + int32_t eitherKeyMetaState, 1.251 + int32_t* currentMetaState); 1.252 + static void addLockedMetaKey(Vector<KeyEvent>& outEvents, 1.253 + int32_t deviceId, int32_t metaState, nsecs_t time, 1.254 + int32_t keyCode, int32_t keyMetaState, 1.255 + int32_t* currentMetaState); 1.256 +}; 1.257 + 1.258 +} // namespace android 1.259 + 1.260 +#endif // _ANDROIDFW_KEY_CHARACTER_MAP_H