widget/gonk/libui/KeyCharacterMap.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright (C) 2008 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef _ANDROIDFW_KEY_CHARACTER_MAP_H
michael@0 18 #define _ANDROIDFW_KEY_CHARACTER_MAP_H
michael@0 19
michael@0 20 #include <stdint.h>
michael@0 21
michael@0 22 #if HAVE_ANDROID_OS
michael@0 23 #include <binder/IBinder.h>
michael@0 24 #endif
michael@0 25
michael@0 26 #include "Input.h"
michael@0 27 #include <utils/Errors.h>
michael@0 28 #include <utils/KeyedVector.h>
michael@0 29 #include "Tokenizer.h"
michael@0 30 #include <utils/String8.h>
michael@0 31 #include <utils/Unicode.h>
michael@0 32 #include <utils/RefBase.h>
michael@0 33
michael@0 34 namespace android {
michael@0 35
michael@0 36 /**
michael@0 37 * Describes a mapping from Android key codes to characters.
michael@0 38 * Also specifies other functions of the keyboard such as the keyboard type
michael@0 39 * and key modifier semantics.
michael@0 40 *
michael@0 41 * This object is immutable after it has been loaded.
michael@0 42 */
michael@0 43 class KeyCharacterMap : public RefBase {
michael@0 44 public:
michael@0 45 enum KeyboardType {
michael@0 46 KEYBOARD_TYPE_UNKNOWN = 0,
michael@0 47 KEYBOARD_TYPE_NUMERIC = 1,
michael@0 48 KEYBOARD_TYPE_PREDICTIVE = 2,
michael@0 49 KEYBOARD_TYPE_ALPHA = 3,
michael@0 50 KEYBOARD_TYPE_FULL = 4,
michael@0 51 KEYBOARD_TYPE_SPECIAL_FUNCTION = 5,
michael@0 52 KEYBOARD_TYPE_OVERLAY = 6,
michael@0 53 };
michael@0 54
michael@0 55 enum Format {
michael@0 56 // Base keyboard layout, may contain device-specific options, such as "type" declaration.
michael@0 57 FORMAT_BASE = 0,
michael@0 58 // Overlay keyboard layout, more restrictive, may be published by applications,
michael@0 59 // cannot override device-specific options.
michael@0 60 FORMAT_OVERLAY = 1,
michael@0 61 // Either base or overlay layout ok.
michael@0 62 FORMAT_ANY = 2,
michael@0 63 };
michael@0 64
michael@0 65 // Substitute key code and meta state for fallback action.
michael@0 66 struct FallbackAction {
michael@0 67 int32_t keyCode;
michael@0 68 int32_t metaState;
michael@0 69 };
michael@0 70
michael@0 71 /* Loads a key character map from a file. */
michael@0 72 static status_t load(const String8& filename, Format format, sp<KeyCharacterMap>* outMap);
michael@0 73
michael@0 74 /* Loads a key character map from its string contents. */
michael@0 75 static status_t loadContents(const String8& filename,
michael@0 76 const char* contents, Format format, sp<KeyCharacterMap>* outMap);
michael@0 77
michael@0 78 /* Combines a base key character map and an overlay. */
michael@0 79 static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base,
michael@0 80 const sp<KeyCharacterMap>& overlay);
michael@0 81
michael@0 82 /* Returns an empty key character map. */
michael@0 83 static sp<KeyCharacterMap> empty();
michael@0 84
michael@0 85 /* Gets the keyboard type. */
michael@0 86 int32_t getKeyboardType() const;
michael@0 87
michael@0 88 /* Gets the primary character for this key as in the label physically printed on it.
michael@0 89 * Returns 0 if none (eg. for non-printing keys). */
michael@0 90 char16_t getDisplayLabel(int32_t keyCode) const;
michael@0 91
michael@0 92 /* Gets the Unicode character for the number or symbol generated by the key
michael@0 93 * when the keyboard is used as a dialing pad.
michael@0 94 * Returns 0 if no number or symbol is generated.
michael@0 95 */
michael@0 96 char16_t getNumber(int32_t keyCode) const;
michael@0 97
michael@0 98 /* Gets the Unicode character generated by the key and meta key modifiers.
michael@0 99 * Returns 0 if no character is generated.
michael@0 100 */
michael@0 101 char16_t getCharacter(int32_t keyCode, int32_t metaState) const;
michael@0 102
michael@0 103 /* Gets the fallback action to use by default if the application does not
michael@0 104 * handle the specified key.
michael@0 105 * Returns true if an action was available, false if none.
michael@0 106 */
michael@0 107 bool getFallbackAction(int32_t keyCode, int32_t metaState,
michael@0 108 FallbackAction* outFallbackAction) const;
michael@0 109
michael@0 110 /* Gets the first matching Unicode character that can be generated by the key,
michael@0 111 * preferring the one with the specified meta key modifiers.
michael@0 112 * Returns 0 if no matching character is generated.
michael@0 113 */
michael@0 114 char16_t getMatch(int32_t keyCode, const char16_t* chars,
michael@0 115 size_t numChars, int32_t metaState) const;
michael@0 116
michael@0 117 /* Gets a sequence of key events that could plausibly generate the specified
michael@0 118 * character sequence. Returns false if some of the characters cannot be generated.
michael@0 119 */
michael@0 120 bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars,
michael@0 121 Vector<KeyEvent>& outEvents) const;
michael@0 122
michael@0 123 /* Maps a scan code and usage code to a key code, in case this key map overrides
michael@0 124 * the mapping in some way. */
michael@0 125 status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const;
michael@0 126
michael@0 127 #if HAVE_ANDROID_OS
michael@0 128 /* Reads a key map from a parcel. */
michael@0 129 static sp<KeyCharacterMap> readFromParcel(Parcel* parcel);
michael@0 130
michael@0 131 /* Writes a key map to a parcel. */
michael@0 132 void writeToParcel(Parcel* parcel) const;
michael@0 133 #endif
michael@0 134
michael@0 135 protected:
michael@0 136 virtual ~KeyCharacterMap();
michael@0 137
michael@0 138 private:
michael@0 139 struct Behavior {
michael@0 140 Behavior();
michael@0 141 Behavior(const Behavior& other);
michael@0 142
michael@0 143 /* The next behavior in the list, or NULL if none. */
michael@0 144 Behavior* next;
michael@0 145
michael@0 146 /* The meta key modifiers for this behavior. */
michael@0 147 int32_t metaState;
michael@0 148
michael@0 149 /* The character to insert. */
michael@0 150 char16_t character;
michael@0 151
michael@0 152 /* The fallback keycode if the key is not handled. */
michael@0 153 int32_t fallbackKeyCode;
michael@0 154 };
michael@0 155
michael@0 156 struct Key {
michael@0 157 Key();
michael@0 158 Key(const Key& other);
michael@0 159 ~Key();
michael@0 160
michael@0 161 /* The single character label printed on the key, or 0 if none. */
michael@0 162 char16_t label;
michael@0 163
michael@0 164 /* The number or symbol character generated by the key, or 0 if none. */
michael@0 165 char16_t number;
michael@0 166
michael@0 167 /* The list of key behaviors sorted from most specific to least specific
michael@0 168 * meta key binding. */
michael@0 169 Behavior* firstBehavior;
michael@0 170 };
michael@0 171
michael@0 172 class Parser {
michael@0 173 enum State {
michael@0 174 STATE_TOP = 0,
michael@0 175 STATE_KEY = 1,
michael@0 176 };
michael@0 177
michael@0 178 enum {
michael@0 179 PROPERTY_LABEL = 1,
michael@0 180 PROPERTY_NUMBER = 2,
michael@0 181 PROPERTY_META = 3,
michael@0 182 };
michael@0 183
michael@0 184 struct Property {
michael@0 185 inline Property(int32_t property = 0, int32_t metaState = 0) :
michael@0 186 property(property), metaState(metaState) { }
michael@0 187
michael@0 188 int32_t property;
michael@0 189 int32_t metaState;
michael@0 190 };
michael@0 191
michael@0 192 KeyCharacterMap* mMap;
michael@0 193 Tokenizer* mTokenizer;
michael@0 194 Format mFormat;
michael@0 195 State mState;
michael@0 196 int32_t mKeyCode;
michael@0 197
michael@0 198 public:
michael@0 199 Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format);
michael@0 200 ~Parser();
michael@0 201 status_t parse();
michael@0 202
michael@0 203 private:
michael@0 204 status_t parseType();
michael@0 205 status_t parseMap();
michael@0 206 status_t parseMapKey();
michael@0 207 status_t parseKey();
michael@0 208 status_t parseKeyProperty();
michael@0 209 status_t finishKey(Key* key);
michael@0 210 status_t parseModifier(const String8& token, int32_t* outMetaState);
michael@0 211 status_t parseCharacterLiteral(char16_t* outCharacter);
michael@0 212 };
michael@0 213
michael@0 214 static sp<KeyCharacterMap> sEmpty;
michael@0 215
michael@0 216 KeyedVector<int32_t, Key*> mKeys;
michael@0 217 int mType;
michael@0 218
michael@0 219 KeyedVector<int32_t, int32_t> mKeysByScanCode;
michael@0 220 KeyedVector<int32_t, int32_t> mKeysByUsageCode;
michael@0 221
michael@0 222 KeyCharacterMap();
michael@0 223 KeyCharacterMap(const KeyCharacterMap& other);
michael@0 224
michael@0 225 bool getKey(int32_t keyCode, const Key** outKey) const;
michael@0 226 bool getKeyBehavior(int32_t keyCode, int32_t metaState,
michael@0 227 const Key** outKey, const Behavior** outBehavior) const;
michael@0 228 static bool matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState);
michael@0 229
michael@0 230 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
michael@0 231
michael@0 232 static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap);
michael@0 233
michael@0 234 static void addKey(Vector<KeyEvent>& outEvents,
michael@0 235 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
michael@0 236 static void addMetaKeys(Vector<KeyEvent>& outEvents,
michael@0 237 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
michael@0 238 int32_t* currentMetaState);
michael@0 239 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
michael@0 240 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
michael@0 241 int32_t keyCode, int32_t keyMetaState,
michael@0 242 int32_t* currentMetaState);
michael@0 243 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
michael@0 244 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
michael@0 245 int32_t leftKeyCode, int32_t leftKeyMetaState,
michael@0 246 int32_t rightKeyCode, int32_t rightKeyMetaState,
michael@0 247 int32_t eitherKeyMetaState,
michael@0 248 int32_t* currentMetaState);
michael@0 249 static void addLockedMetaKey(Vector<KeyEvent>& outEvents,
michael@0 250 int32_t deviceId, int32_t metaState, nsecs_t time,
michael@0 251 int32_t keyCode, int32_t keyMetaState,
michael@0 252 int32_t* currentMetaState);
michael@0 253 };
michael@0 254
michael@0 255 } // namespace android
michael@0 256
michael@0 257 #endif // _ANDROIDFW_KEY_CHARACTER_MAP_H

mercurial