Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_INPUT_READER_H |
| 18 | #define _UI_INPUT_READER_H |
| 19 | |
| 20 | #include <ui/EventHub.h> |
| 21 | #include <ui/Input.h> |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 22 | #include <ui/InputDispatcher.h> |
| 23 | #include <utils/KeyedVector.h> |
| 24 | #include <utils/threads.h> |
| 25 | #include <utils/Timers.h> |
| 26 | #include <utils/RefBase.h> |
| 27 | #include <utils/String8.h> |
| 28 | #include <utils/BitSet.h> |
| 29 | |
| 30 | #include <stddef.h> |
| 31 | #include <unistd.h> |
| 32 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 35 | class InputDevice; |
| 36 | class InputMapper; |
| 37 | |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 38 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Input reader policy interface. |
| 41 | * |
| 42 | * The input reader policy is used by the input reader to interact with the Window Manager |
| 43 | * and other system components. |
| 44 | * |
| 45 | * The actual implementation is partially supported by callbacks into the DVM |
| 46 | * via JNI. This interface is also mocked in the unit tests. |
| 47 | */ |
| 48 | class InputReaderPolicyInterface : public virtual RefBase { |
| 49 | protected: |
| 50 | InputReaderPolicyInterface() { } |
| 51 | virtual ~InputReaderPolicyInterface() { } |
| 52 | |
| 53 | public: |
| 54 | /* Display orientations. */ |
| 55 | enum { |
| 56 | ROTATION_0 = 0, |
| 57 | ROTATION_90 = 1, |
| 58 | ROTATION_180 = 2, |
| 59 | ROTATION_270 = 3 |
| 60 | }; |
| 61 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 62 | /* Gets information about the display with the specified id. |
| 63 | * Returns true if the display info is available, false otherwise. |
| 64 | */ |
| 65 | virtual bool getDisplayInfo(int32_t displayId, |
| 66 | int32_t* width, int32_t* height, int32_t* orientation) = 0; |
| 67 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 68 | /* Determines whether to turn on some hacks we have to improve the touch interaction with a |
| 69 | * certain device whose screen currently is not all that good. |
| 70 | */ |
| 71 | virtual bool filterTouchEvents() = 0; |
| 72 | |
| 73 | /* Determines whether to turn on some hacks to improve touch interaction with another device |
| 74 | * where touch coordinate data can get corrupted. |
| 75 | */ |
| 76 | virtual bool filterJumpyTouchEvents() = 0; |
| 77 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 78 | /* Gets the excluded device names for the platform. */ |
| 79 | virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /* Processes raw input events and sends cooked event data to an input dispatcher. */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 84 | class InputReaderInterface : public virtual RefBase { |
| 85 | protected: |
| 86 | InputReaderInterface() { } |
| 87 | virtual ~InputReaderInterface() { } |
| 88 | |
| 89 | public: |
Jeff Brown | a665ca8 | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 90 | /* Dumps the state of the input reader. |
| 91 | * |
| 92 | * This method may be called on any thread (usually by the input manager). */ |
| 93 | virtual void dump(String8& dump) = 0; |
| 94 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 95 | /* Runs a single iteration of the processing loop. |
| 96 | * Nominally reads and processes one incoming message from the EventHub. |
| 97 | * |
| 98 | * This method should be called on the input reader thread. |
| 99 | */ |
| 100 | virtual void loopOnce() = 0; |
| 101 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 102 | /* Gets the current input device configuration. |
| 103 | * |
| 104 | * This method may be called on any thread (usually by the input manager). |
| 105 | */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 106 | virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0; |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 107 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 108 | /* Gets information about the specified input device. |
| 109 | * Returns OK if the device information was obtained or NAME_NOT_FOUND if there |
| 110 | * was no such device. |
| 111 | * |
| 112 | * This method may be called on any thread (usually by the input manager). |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 113 | */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 114 | virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0; |
| 115 | |
| 116 | /* Gets the list of all registered device ids. */ |
| 117 | virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0; |
| 118 | |
| 119 | /* Query current input state. */ |
| 120 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 121 | int32_t scanCode) = 0; |
| 122 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 123 | int32_t keyCode) = 0; |
| 124 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, |
| 125 | int32_t sw) = 0; |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 126 | |
| 127 | /* Determine whether physical keys exist for the given framework-domain key codes. */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 128 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 129 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | /* Internal interface used by individual input devices to access global input device state |
| 134 | * and parameters maintained by the input reader. |
| 135 | */ |
| 136 | class InputReaderContext { |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 137 | public: |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 138 | InputReaderContext() { } |
| 139 | virtual ~InputReaderContext() { } |
| 140 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 141 | virtual void updateGlobalMetaState() = 0; |
| 142 | virtual int32_t getGlobalMetaState() = 0; |
| 143 | |
| 144 | virtual InputReaderPolicyInterface* getPolicy() = 0; |
| 145 | virtual InputDispatcherInterface* getDispatcher() = 0; |
| 146 | virtual EventHubInterface* getEventHub() = 0; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 149 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 150 | /* The input reader reads raw event data from the event hub and processes it into input events |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 151 | * that it sends to the input dispatcher. Some functions of the input reader, such as early |
| 152 | * event filtering in low power states, are controlled by a separate policy object. |
| 153 | * |
| 154 | * IMPORTANT INVARIANT: |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 155 | * Because the policy and dispatcher can potentially block or cause re-entrance into |
| 156 | * the input reader, the input reader never calls into other components while holding |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 157 | * an exclusive internal lock whenever re-entrance can happen. |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 158 | */ |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 159 | class InputReader : public InputReaderInterface, protected InputReaderContext { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 160 | public: |
| 161 | InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 162 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | const sp<InputDispatcherInterface>& dispatcher); |
| 164 | virtual ~InputReader(); |
| 165 | |
Jeff Brown | a665ca8 | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 166 | virtual void dump(String8& dump); |
| 167 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 168 | virtual void loopOnce(); |
| 169 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 170 | virtual void getInputConfiguration(InputConfiguration* outConfiguration); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 171 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 172 | virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo); |
| 173 | virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds); |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 174 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 175 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 176 | int32_t scanCode); |
| 177 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 178 | int32_t keyCode); |
| 179 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, |
| 180 | int32_t sw); |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 181 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 182 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 183 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags); |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 184 | |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 185 | protected: |
| 186 | // These methods are protected virtual so they can be overridden and instrumented |
| 187 | // by test cases. |
| 188 | virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes); |
| 189 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 190 | private: |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 191 | sp<EventHubInterface> mEventHub; |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 192 | sp<InputReaderPolicyInterface> mPolicy; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 193 | sp<InputDispatcherInterface> mDispatcher; |
| 194 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 195 | virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); } |
| 196 | virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); } |
| 197 | virtual EventHubInterface* getEventHub() { return mEventHub.get(); } |
| 198 | |
| 199 | // This reader/writer lock guards the list of input devices. |
| 200 | // The writer lock must be held whenever the list of input devices is modified |
| 201 | // and then promptly released. |
| 202 | // The reader lock must be held whenever the list of input devices is traversed or an |
| 203 | // input device in the list is accessed. |
| 204 | // This lock only protects the registry and prevents inadvertent deletion of device objects |
| 205 | // that are in use. Individual devices are responsible for guarding their own internal state |
| 206 | // as needed for concurrent operation. |
| 207 | RWLock mDeviceRegistryLock; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 208 | KeyedVector<int32_t, InputDevice*> mDevices; |
| 209 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 210 | // low-level input event decoding and device management |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 211 | void process(const RawEvent* rawEvent); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 212 | |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 213 | void addDevice(int32_t deviceId); |
| 214 | void removeDevice(int32_t deviceId); |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 215 | void configureExcludedDevices(); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 216 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 217 | void consumeEvent(const RawEvent* rawEvent); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 218 | |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 219 | void handleConfigurationChanged(nsecs_t when); |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 220 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 221 | // state management for all devices |
| 222 | Mutex mStateLock; |
| 223 | |
| 224 | int32_t mGlobalMetaState; |
| 225 | virtual void updateGlobalMetaState(); |
| 226 | virtual int32_t getGlobalMetaState(); |
| 227 | |
| 228 | InputConfiguration mInputConfiguration; |
| 229 | void updateInputConfiguration(); |
| 230 | |
| 231 | // state queries |
| 232 | typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 233 | int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 234 | GetStateFunc getStateFunc); |
| 235 | bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 236 | const int32_t* keyCodes, uint8_t* outFlags); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | |
| 240 | /* Reads raw events from the event hub and processes them, endlessly. */ |
| 241 | class InputReaderThread : public Thread { |
| 242 | public: |
| 243 | InputReaderThread(const sp<InputReaderInterface>& reader); |
| 244 | virtual ~InputReaderThread(); |
| 245 | |
| 246 | private: |
| 247 | sp<InputReaderInterface> mReader; |
| 248 | |
| 249 | virtual bool threadLoop(); |
| 250 | }; |
| 251 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 252 | |
| 253 | /* Represents the state of a single input device. */ |
| 254 | class InputDevice { |
| 255 | public: |
| 256 | InputDevice(InputReaderContext* context, int32_t id, const String8& name); |
| 257 | ~InputDevice(); |
| 258 | |
| 259 | inline InputReaderContext* getContext() { return mContext; } |
| 260 | inline int32_t getId() { return mId; } |
| 261 | inline const String8& getName() { return mName; } |
| 262 | inline uint32_t getSources() { return mSources; } |
| 263 | |
| 264 | inline bool isIgnored() { return mMappers.isEmpty(); } |
| 265 | |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 266 | void dump(String8& dump); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 267 | void addMapper(InputMapper* mapper); |
| 268 | void configure(); |
| 269 | void reset(); |
| 270 | void process(const RawEvent* rawEvent); |
| 271 | |
| 272 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 273 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 274 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 275 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 276 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 277 | const int32_t* keyCodes, uint8_t* outFlags); |
| 278 | |
| 279 | int32_t getMetaState(); |
| 280 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 281 | inline const PropertyMap& getConfiguration() { |
| 282 | return mConfiguration; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 285 | private: |
| 286 | InputReaderContext* mContext; |
| 287 | int32_t mId; |
| 288 | |
| 289 | Vector<InputMapper*> mMappers; |
| 290 | |
| 291 | String8 mName; |
| 292 | uint32_t mSources; |
| 293 | |
| 294 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 295 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 296 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 297 | PropertyMap mConfiguration; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | |
| 301 | /* An input mapper transforms raw input events into cooked event data. |
| 302 | * A single input device can have multiple associated input mappers in order to interpret |
| 303 | * different classes of events. |
| 304 | */ |
| 305 | class InputMapper { |
| 306 | public: |
| 307 | InputMapper(InputDevice* device); |
| 308 | virtual ~InputMapper(); |
| 309 | |
| 310 | inline InputDevice* getDevice() { return mDevice; } |
| 311 | inline int32_t getDeviceId() { return mDevice->getId(); } |
| 312 | inline const String8 getDeviceName() { return mDevice->getName(); } |
| 313 | inline InputReaderContext* getContext() { return mContext; } |
| 314 | inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); } |
| 315 | inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); } |
| 316 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 317 | |
| 318 | virtual uint32_t getSources() = 0; |
| 319 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 320 | virtual void dump(String8& dump); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 321 | virtual void configure(); |
| 322 | virtual void reset(); |
| 323 | virtual void process(const RawEvent* rawEvent) = 0; |
| 324 | |
| 325 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 326 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 327 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 328 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 329 | const int32_t* keyCodes, uint8_t* outFlags); |
| 330 | |
| 331 | virtual int32_t getMetaState(); |
| 332 | |
| 333 | protected: |
| 334 | InputDevice* mDevice; |
| 335 | InputReaderContext* mContext; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | |
| 339 | class SwitchInputMapper : public InputMapper { |
| 340 | public: |
| 341 | SwitchInputMapper(InputDevice* device); |
| 342 | virtual ~SwitchInputMapper(); |
| 343 | |
| 344 | virtual uint32_t getSources(); |
| 345 | virtual void process(const RawEvent* rawEvent); |
| 346 | |
| 347 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 348 | |
| 349 | private: |
| 350 | void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue); |
| 351 | }; |
| 352 | |
| 353 | |
| 354 | class KeyboardInputMapper : public InputMapper { |
| 355 | public: |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 356 | KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 357 | virtual ~KeyboardInputMapper(); |
| 358 | |
| 359 | virtual uint32_t getSources(); |
| 360 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 361 | virtual void dump(String8& dump); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 362 | virtual void configure(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 363 | virtual void reset(); |
| 364 | virtual void process(const RawEvent* rawEvent); |
| 365 | |
| 366 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 367 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 368 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 369 | const int32_t* keyCodes, uint8_t* outFlags); |
| 370 | |
| 371 | virtual int32_t getMetaState(); |
| 372 | |
| 373 | private: |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 374 | Mutex mLock; |
| 375 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 376 | struct KeyDown { |
| 377 | int32_t keyCode; |
| 378 | int32_t scanCode; |
| 379 | }; |
| 380 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 381 | uint32_t mSources; |
| 382 | int32_t mKeyboardType; |
| 383 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 384 | // Immutable configuration parameters. |
| 385 | struct Parameters { |
| 386 | int32_t associatedDisplayId; |
| 387 | bool orientationAware; |
| 388 | } mParameters; |
| 389 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 390 | struct LockedState { |
| 391 | Vector<KeyDown> keyDowns; // keys that are down |
| 392 | int32_t metaState; |
| 393 | nsecs_t downTime; // time of most recent key down |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 394 | |
| 395 | struct LedState { |
| 396 | bool avail; // led is available |
| 397 | bool on; // we think the led is currently on |
| 398 | }; |
| 399 | LedState capsLockLedState; |
| 400 | LedState numLockLedState; |
| 401 | LedState scrollLockLedState; |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 402 | } mLocked; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 403 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 404 | void initializeLocked(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 405 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 406 | void configureParameters(); |
| 407 | void dumpParameters(String8& dump); |
| 408 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 409 | bool isKeyboardOrGamepadKey(int32_t scanCode); |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 410 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 411 | void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode, |
| 412 | uint32_t policyFlags); |
| 413 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 414 | ssize_t findKeyDownLocked(int32_t scanCode); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 415 | |
Jeff Brown | 02d85b5 | 2010-12-06 17:13:33 -0800 | [diff] [blame^] | 416 | void resetLedStateLocked(); |
| 417 | void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 418 | void updateLedStateLocked(bool reset); |
| 419 | void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led, |
| 420 | int32_t modifier, bool reset); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | |
| 424 | class TrackballInputMapper : public InputMapper { |
| 425 | public: |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 426 | TrackballInputMapper(InputDevice* device); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 427 | virtual ~TrackballInputMapper(); |
| 428 | |
| 429 | virtual uint32_t getSources(); |
| 430 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 431 | virtual void dump(String8& dump); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 432 | virtual void configure(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 433 | virtual void reset(); |
| 434 | virtual void process(const RawEvent* rawEvent); |
| 435 | |
Jeff Brown | 8d4dfd2 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 436 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 437 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 438 | private: |
| 439 | // Amount that trackball needs to move in order to generate a key event. |
| 440 | static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; |
| 441 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 442 | Mutex mLock; |
| 443 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 444 | // Immutable configuration parameters. |
| 445 | struct Parameters { |
| 446 | int32_t associatedDisplayId; |
| 447 | bool orientationAware; |
| 448 | } mParameters; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 449 | |
| 450 | struct Accumulator { |
| 451 | enum { |
| 452 | FIELD_BTN_MOUSE = 1, |
| 453 | FIELD_REL_X = 2, |
| 454 | FIELD_REL_Y = 4 |
| 455 | }; |
| 456 | |
| 457 | uint32_t fields; |
| 458 | |
| 459 | bool btnMouse; |
| 460 | int32_t relX; |
| 461 | int32_t relY; |
| 462 | |
| 463 | inline void clear() { |
| 464 | fields = 0; |
| 465 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 466 | } mAccumulator; |
| 467 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 468 | float mXScale; |
| 469 | float mYScale; |
| 470 | float mXPrecision; |
| 471 | float mYPrecision; |
| 472 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 473 | struct LockedState { |
| 474 | bool down; |
| 475 | nsecs_t downTime; |
| 476 | } mLocked; |
| 477 | |
| 478 | void initializeLocked(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 479 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 480 | void configureParameters(); |
| 481 | void dumpParameters(String8& dump); |
| 482 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 483 | void sync(nsecs_t when); |
| 484 | }; |
| 485 | |
| 486 | |
| 487 | class TouchInputMapper : public InputMapper { |
| 488 | public: |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 489 | TouchInputMapper(InputDevice* device); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 490 | virtual ~TouchInputMapper(); |
| 491 | |
| 492 | virtual uint32_t getSources(); |
| 493 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 494 | virtual void dump(String8& dump); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 495 | virtual void configure(); |
| 496 | virtual void reset(); |
| 497 | |
| 498 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 499 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 500 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 501 | const int32_t* keyCodes, uint8_t* outFlags); |
| 502 | |
| 503 | protected: |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 504 | Mutex mLock; |
| 505 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 506 | struct VirtualKey { |
| 507 | int32_t keyCode; |
| 508 | int32_t scanCode; |
| 509 | uint32_t flags; |
| 510 | |
| 511 | // computed hit box, specified in touch screen coords based on known display size |
| 512 | int32_t hitLeft; |
| 513 | int32_t hitTop; |
| 514 | int32_t hitRight; |
| 515 | int32_t hitBottom; |
| 516 | |
| 517 | inline bool isHit(int32_t x, int32_t y) const { |
| 518 | return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; |
| 519 | } |
| 520 | }; |
| 521 | |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 522 | // Raw data for a single pointer. |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 523 | struct PointerData { |
| 524 | uint32_t id; |
| 525 | int32_t x; |
| 526 | int32_t y; |
| 527 | int32_t pressure; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 528 | int32_t touchMajor; |
| 529 | int32_t touchMinor; |
| 530 | int32_t toolMajor; |
| 531 | int32_t toolMinor; |
| 532 | int32_t orientation; |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 533 | |
| 534 | inline bool operator== (const PointerData& other) const { |
| 535 | return id == other.id |
| 536 | && x == other.x |
| 537 | && y == other.y |
| 538 | && pressure == other.pressure |
| 539 | && touchMajor == other.touchMajor |
| 540 | && touchMinor == other.touchMinor |
| 541 | && toolMajor == other.toolMajor |
| 542 | && toolMinor == other.toolMinor |
| 543 | && orientation == other.orientation; |
| 544 | } |
| 545 | inline bool operator!= (const PointerData& other) const { |
| 546 | return !(*this == other); |
| 547 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 550 | // Raw data for a collection of pointers including a pointer id mapping table. |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 551 | struct TouchData { |
| 552 | uint32_t pointerCount; |
| 553 | PointerData pointers[MAX_POINTERS]; |
| 554 | BitSet32 idBits; |
| 555 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
| 556 | |
| 557 | void copyFrom(const TouchData& other) { |
| 558 | pointerCount = other.pointerCount; |
| 559 | idBits = other.idBits; |
| 560 | |
| 561 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 562 | pointers[i] = other.pointers[i]; |
Jeff Brown | b183fbd | 2010-07-30 19:20:11 -0700 | [diff] [blame] | 563 | |
| 564 | int id = pointers[i].id; |
| 565 | idToIndex[id] = other.idToIndex[id]; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | inline void clear() { |
| 570 | pointerCount = 0; |
| 571 | idBits.clear(); |
| 572 | } |
| 573 | }; |
| 574 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 575 | // Immutable configuration parameters. |
| 576 | struct Parameters { |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 577 | enum DeviceType { |
| 578 | DEVICE_TYPE_TOUCH_SCREEN, |
| 579 | DEVICE_TYPE_TOUCH_PAD, |
| 580 | }; |
| 581 | |
| 582 | DeviceType deviceType; |
| 583 | int32_t associatedDisplayId; |
| 584 | bool orientationAware; |
| 585 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 586 | bool useBadTouchFilter; |
| 587 | bool useJumpyTouchFilter; |
| 588 | bool useAveragingTouchFilter; |
| 589 | } mParameters; |
| 590 | |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 591 | // Immutable calibration parameters in parsed form. |
| 592 | struct Calibration { |
Jeff Brown | 60b5776 | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 593 | // Position |
| 594 | bool haveXOrigin; |
| 595 | int32_t xOrigin; |
| 596 | bool haveYOrigin; |
| 597 | int32_t yOrigin; |
| 598 | bool haveXScale; |
| 599 | float xScale; |
| 600 | bool haveYScale; |
| 601 | float yScale; |
| 602 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 603 | // Touch Size |
| 604 | enum TouchSizeCalibration { |
| 605 | TOUCH_SIZE_CALIBRATION_DEFAULT, |
| 606 | TOUCH_SIZE_CALIBRATION_NONE, |
| 607 | TOUCH_SIZE_CALIBRATION_GEOMETRIC, |
| 608 | TOUCH_SIZE_CALIBRATION_PRESSURE, |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 609 | }; |
| 610 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 611 | TouchSizeCalibration touchSizeCalibration; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 612 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 613 | // Tool Size |
| 614 | enum ToolSizeCalibration { |
| 615 | TOOL_SIZE_CALIBRATION_DEFAULT, |
| 616 | TOOL_SIZE_CALIBRATION_NONE, |
| 617 | TOOL_SIZE_CALIBRATION_GEOMETRIC, |
| 618 | TOOL_SIZE_CALIBRATION_LINEAR, |
| 619 | TOOL_SIZE_CALIBRATION_AREA, |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 620 | }; |
| 621 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 622 | ToolSizeCalibration toolSizeCalibration; |
| 623 | bool haveToolSizeLinearScale; |
| 624 | float toolSizeLinearScale; |
| 625 | bool haveToolSizeLinearBias; |
| 626 | float toolSizeLinearBias; |
| 627 | bool haveToolSizeAreaScale; |
| 628 | float toolSizeAreaScale; |
| 629 | bool haveToolSizeAreaBias; |
| 630 | float toolSizeAreaBias; |
| 631 | bool haveToolSizeIsSummed; |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 632 | bool toolSizeIsSummed; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 633 | |
| 634 | // Pressure |
| 635 | enum PressureCalibration { |
| 636 | PRESSURE_CALIBRATION_DEFAULT, |
| 637 | PRESSURE_CALIBRATION_NONE, |
| 638 | PRESSURE_CALIBRATION_PHYSICAL, |
| 639 | PRESSURE_CALIBRATION_AMPLITUDE, |
| 640 | }; |
| 641 | enum PressureSource { |
| 642 | PRESSURE_SOURCE_DEFAULT, |
| 643 | PRESSURE_SOURCE_PRESSURE, |
| 644 | PRESSURE_SOURCE_TOUCH, |
| 645 | }; |
| 646 | |
| 647 | PressureCalibration pressureCalibration; |
| 648 | PressureSource pressureSource; |
| 649 | bool havePressureScale; |
| 650 | float pressureScale; |
| 651 | |
| 652 | // Size |
| 653 | enum SizeCalibration { |
| 654 | SIZE_CALIBRATION_DEFAULT, |
| 655 | SIZE_CALIBRATION_NONE, |
| 656 | SIZE_CALIBRATION_NORMALIZED, |
| 657 | }; |
| 658 | |
| 659 | SizeCalibration sizeCalibration; |
| 660 | |
| 661 | // Orientation |
| 662 | enum OrientationCalibration { |
| 663 | ORIENTATION_CALIBRATION_DEFAULT, |
| 664 | ORIENTATION_CALIBRATION_NONE, |
| 665 | ORIENTATION_CALIBRATION_INTERPOLATED, |
| 666 | }; |
| 667 | |
| 668 | OrientationCalibration orientationCalibration; |
| 669 | } mCalibration; |
| 670 | |
| 671 | // Raw axis information from the driver. |
| 672 | struct RawAxes { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 673 | RawAbsoluteAxisInfo x; |
| 674 | RawAbsoluteAxisInfo y; |
| 675 | RawAbsoluteAxisInfo pressure; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 676 | RawAbsoluteAxisInfo touchMajor; |
| 677 | RawAbsoluteAxisInfo touchMinor; |
| 678 | RawAbsoluteAxisInfo toolMajor; |
| 679 | RawAbsoluteAxisInfo toolMinor; |
| 680 | RawAbsoluteAxisInfo orientation; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 681 | } mRawAxes; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 682 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 683 | // Current and previous touch sample data. |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 684 | TouchData mCurrentTouch; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 685 | TouchData mLastTouch; |
| 686 | |
| 687 | // The time the primary pointer last went down. |
| 688 | nsecs_t mDownTime; |
| 689 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 690 | struct LockedState { |
| 691 | Vector<VirtualKey> virtualKeys; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 692 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 693 | // The surface orientation and width and height set by configureSurfaceLocked(). |
| 694 | int32_t surfaceOrientation; |
| 695 | int32_t surfaceWidth, surfaceHeight; |
| 696 | |
| 697 | // Translation and scaling factors, orientation-independent. |
| 698 | int32_t xOrigin; |
| 699 | float xScale; |
| 700 | float xPrecision; |
| 701 | |
| 702 | int32_t yOrigin; |
| 703 | float yScale; |
| 704 | float yPrecision; |
| 705 | |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 706 | float geometricScale; |
| 707 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 708 | float toolSizeLinearScale; |
| 709 | float toolSizeLinearBias; |
| 710 | float toolSizeAreaScale; |
| 711 | float toolSizeAreaBias; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 712 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 713 | float pressureScale; |
| 714 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 715 | float sizeScale; |
| 716 | |
| 717 | float orientationScale; |
| 718 | |
| 719 | // Oriented motion ranges for input device info. |
| 720 | struct OrientedRanges { |
| 721 | InputDeviceInfo::MotionRange x; |
| 722 | InputDeviceInfo::MotionRange y; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 723 | |
| 724 | bool havePressure; |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 725 | InputDeviceInfo::MotionRange pressure; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 726 | |
| 727 | bool haveSize; |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 728 | InputDeviceInfo::MotionRange size; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 729 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 730 | bool haveTouchSize; |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 731 | InputDeviceInfo::MotionRange touchMajor; |
| 732 | InputDeviceInfo::MotionRange touchMinor; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 733 | |
Jeff Brown | 6b337e7 | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 734 | bool haveToolSize; |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 735 | InputDeviceInfo::MotionRange toolMajor; |
| 736 | InputDeviceInfo::MotionRange toolMinor; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 737 | |
| 738 | bool haveOrientation; |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 739 | InputDeviceInfo::MotionRange orientation; |
| 740 | } orientedRanges; |
| 741 | |
| 742 | // Oriented dimensions and precision. |
| 743 | float orientedSurfaceWidth, orientedSurfaceHeight; |
| 744 | float orientedXPrecision, orientedYPrecision; |
| 745 | |
| 746 | struct CurrentVirtualKeyState { |
| 747 | bool down; |
| 748 | nsecs_t downTime; |
| 749 | int32_t keyCode; |
| 750 | int32_t scanCode; |
| 751 | } currentVirtualKey; |
| 752 | } mLocked; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 753 | |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 754 | virtual void configureParameters(); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 755 | virtual void dumpParameters(String8& dump); |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 756 | virtual void configureRawAxes(); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 757 | virtual void dumpRawAxes(String8& dump); |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 758 | virtual bool configureSurfaceLocked(); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 759 | virtual void dumpSurfaceLocked(String8& dump); |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 760 | virtual void configureVirtualKeysLocked(); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 761 | virtual void dumpVirtualKeysLocked(String8& dump); |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 762 | virtual void parseCalibration(); |
| 763 | virtual void resolveCalibration(); |
Jeff Brown | 26c94ff | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 764 | virtual void dumpCalibration(String8& dump); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 765 | |
| 766 | enum TouchResult { |
| 767 | // Dispatch the touch normally. |
| 768 | DISPATCH_TOUCH, |
| 769 | // Do not dispatch the touch, but keep tracking the current stroke. |
| 770 | SKIP_TOUCH, |
| 771 | // Do not dispatch the touch, and drop all information associated with the current stoke |
| 772 | // so the next movement will appear as a new down. |
| 773 | DROP_STROKE |
| 774 | }; |
| 775 | |
| 776 | void syncTouch(nsecs_t when, bool havePointerIds); |
| 777 | |
| 778 | private: |
| 779 | /* Maximum number of historical samples to average. */ |
| 780 | static const uint32_t AVERAGING_HISTORY_SIZE = 5; |
| 781 | |
| 782 | /* Slop distance for jumpy pointer detection. |
| 783 | * The vertical range of the screen divided by this is our epsilon value. */ |
| 784 | static const uint32_t JUMPY_EPSILON_DIVISOR = 212; |
| 785 | |
| 786 | /* Number of jumpy points to drop for touchscreens that need it. */ |
| 787 | static const uint32_t JUMPY_TRANSITION_DROPS = 3; |
| 788 | static const uint32_t JUMPY_DROP_LIMIT = 3; |
| 789 | |
| 790 | /* Maximum squared distance for averaging. |
| 791 | * If moving farther than this, turn of averaging to avoid lag in response. */ |
| 792 | static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75; |
| 793 | |
| 794 | struct AveragingTouchFilterState { |
| 795 | // Individual history tracks are stored by pointer id |
| 796 | uint32_t historyStart[MAX_POINTERS]; |
| 797 | uint32_t historyEnd[MAX_POINTERS]; |
| 798 | struct { |
| 799 | struct { |
| 800 | int32_t x; |
| 801 | int32_t y; |
| 802 | int32_t pressure; |
| 803 | } pointers[MAX_POINTERS]; |
| 804 | } historyData[AVERAGING_HISTORY_SIZE]; |
| 805 | } mAveragingTouchFilter; |
| 806 | |
Jeff Brown | d64c855 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 807 | struct JumpyTouchFilterState { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 808 | uint32_t jumpyPointsDropped; |
| 809 | } mJumpyTouchFilter; |
| 810 | |
| 811 | struct PointerDistanceHeapElement { |
| 812 | uint32_t currentPointerIndex : 8; |
| 813 | uint32_t lastPointerIndex : 8; |
| 814 | uint64_t distance : 48; // squared distance |
| 815 | }; |
| 816 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 817 | void initializeLocked(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 818 | |
| 819 | TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags); |
| 820 | void dispatchTouches(nsecs_t when, uint32_t policyFlags); |
| 821 | void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch, |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 822 | BitSet32 idBits, uint32_t changedId, uint32_t pointerCount, |
| 823 | int32_t motionEventAction); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 824 | |
Jeff Brown | b51719b | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 825 | bool isPointInsideSurfaceLocked(int32_t x, int32_t y); |
| 826 | const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 827 | |
| 828 | bool applyBadTouchFilter(); |
| 829 | bool applyJumpyTouchFilter(); |
| 830 | void applyAveragingTouchFilter(); |
| 831 | void calculatePointerIds(); |
| 832 | }; |
| 833 | |
| 834 | |
| 835 | class SingleTouchInputMapper : public TouchInputMapper { |
| 836 | public: |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 837 | SingleTouchInputMapper(InputDevice* device); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 838 | virtual ~SingleTouchInputMapper(); |
| 839 | |
| 840 | virtual void reset(); |
| 841 | virtual void process(const RawEvent* rawEvent); |
| 842 | |
| 843 | protected: |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 844 | virtual void configureRawAxes(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 845 | |
| 846 | private: |
| 847 | struct Accumulator { |
| 848 | enum { |
| 849 | FIELD_BTN_TOUCH = 1, |
| 850 | FIELD_ABS_X = 2, |
| 851 | FIELD_ABS_Y = 4, |
| 852 | FIELD_ABS_PRESSURE = 8, |
| 853 | FIELD_ABS_TOOL_WIDTH = 16 |
| 854 | }; |
| 855 | |
| 856 | uint32_t fields; |
| 857 | |
| 858 | bool btnTouch; |
| 859 | int32_t absX; |
| 860 | int32_t absY; |
| 861 | int32_t absPressure; |
| 862 | int32_t absToolWidth; |
| 863 | |
| 864 | inline void clear() { |
| 865 | fields = 0; |
| 866 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 867 | } mAccumulator; |
| 868 | |
| 869 | bool mDown; |
| 870 | int32_t mX; |
| 871 | int32_t mY; |
| 872 | int32_t mPressure; |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 873 | int32_t mToolWidth; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 874 | |
| 875 | void initialize(); |
| 876 | |
| 877 | void sync(nsecs_t when); |
| 878 | }; |
| 879 | |
| 880 | |
| 881 | class MultiTouchInputMapper : public TouchInputMapper { |
| 882 | public: |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 883 | MultiTouchInputMapper(InputDevice* device); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 884 | virtual ~MultiTouchInputMapper(); |
| 885 | |
| 886 | virtual void reset(); |
| 887 | virtual void process(const RawEvent* rawEvent); |
| 888 | |
| 889 | protected: |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 890 | virtual void configureRawAxes(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 891 | |
| 892 | private: |
| 893 | struct Accumulator { |
| 894 | enum { |
| 895 | FIELD_ABS_MT_POSITION_X = 1, |
| 896 | FIELD_ABS_MT_POSITION_Y = 2, |
| 897 | FIELD_ABS_MT_TOUCH_MAJOR = 4, |
| 898 | FIELD_ABS_MT_TOUCH_MINOR = 8, |
| 899 | FIELD_ABS_MT_WIDTH_MAJOR = 16, |
| 900 | FIELD_ABS_MT_WIDTH_MINOR = 32, |
| 901 | FIELD_ABS_MT_ORIENTATION = 64, |
Jeff Brown | d64c855 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 902 | FIELD_ABS_MT_TRACKING_ID = 128, |
| 903 | FIELD_ABS_MT_PRESSURE = 256, |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 904 | }; |
| 905 | |
| 906 | uint32_t pointerCount; |
| 907 | struct Pointer { |
| 908 | uint32_t fields; |
| 909 | |
| 910 | int32_t absMTPositionX; |
| 911 | int32_t absMTPositionY; |
| 912 | int32_t absMTTouchMajor; |
| 913 | int32_t absMTTouchMinor; |
| 914 | int32_t absMTWidthMajor; |
| 915 | int32_t absMTWidthMinor; |
| 916 | int32_t absMTOrientation; |
| 917 | int32_t absMTTrackingId; |
Jeff Brown | d64c855 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 918 | int32_t absMTPressure; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 919 | |
| 920 | inline void clear() { |
| 921 | fields = 0; |
| 922 | } |
| 923 | } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks |
| 924 | |
| 925 | inline void clear() { |
| 926 | pointerCount = 0; |
| 927 | pointers[0].clear(); |
| 928 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 929 | } mAccumulator; |
| 930 | |
| 931 | void initialize(); |
| 932 | |
| 933 | void sync(nsecs_t when); |
| 934 | }; |
| 935 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 936 | } // namespace android |
| 937 | |
| 938 | #endif // _UI_INPUT_READER_H |