Jeff Brown | 46b9ac0 | 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 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 20 | #include "EventHub.h" |
| 21 | #include "InputDispatcher.h" |
| 22 | #include "PointerController.h" |
Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 23 | #include "SpotController.h" |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 24 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 25 | #include <ui/Input.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 26 | #include <ui/DisplayInfo.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 27 | #include <utils/KeyedVector.h> |
| 28 | #include <utils/threads.h> |
| 29 | #include <utils/Timers.h> |
| 30 | #include <utils/RefBase.h> |
| 31 | #include <utils/String8.h> |
| 32 | #include <utils/BitSet.h> |
| 33 | |
| 34 | #include <stddef.h> |
| 35 | #include <unistd.h> |
| 36 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 39 | class InputDevice; |
| 40 | class InputMapper; |
| 41 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 42 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Input reader policy interface. |
| 45 | * |
| 46 | * The input reader policy is used by the input reader to interact with the Window Manager |
| 47 | * and other system components. |
| 48 | * |
| 49 | * The actual implementation is partially supported by callbacks into the DVM |
| 50 | * via JNI. This interface is also mocked in the unit tests. |
| 51 | */ |
| 52 | class InputReaderPolicyInterface : public virtual RefBase { |
| 53 | protected: |
| 54 | InputReaderPolicyInterface() { } |
| 55 | virtual ~InputReaderPolicyInterface() { } |
| 56 | |
| 57 | public: |
| 58 | /* Display orientations. */ |
| 59 | enum { |
| 60 | ROTATION_0 = 0, |
| 61 | ROTATION_90 = 1, |
| 62 | ROTATION_180 = 2, |
| 63 | ROTATION_270 = 3 |
| 64 | }; |
| 65 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 66 | /* Gets information about the display with the specified id. |
| 67 | * Returns true if the display info is available, false otherwise. |
| 68 | */ |
| 69 | virtual bool getDisplayInfo(int32_t displayId, |
| 70 | int32_t* width, int32_t* height, int32_t* orientation) = 0; |
| 71 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 72 | /* Determines whether to turn on some hacks we have to improve the touch interaction with a |
| 73 | * certain device whose screen currently is not all that good. |
| 74 | */ |
| 75 | virtual bool filterTouchEvents() = 0; |
| 76 | |
| 77 | /* Determines whether to turn on some hacks to improve touch interaction with another device |
| 78 | * where touch coordinate data can get corrupted. |
| 79 | */ |
| 80 | virtual bool filterJumpyTouchEvents() = 0; |
| 81 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 82 | /* Gets the amount of time to disable virtual keys after the screen is touched |
| 83 | * in order to filter out accidental virtual key presses due to swiping gestures |
| 84 | * or taps near the edge of the display. May be 0 to disable the feature. |
| 85 | */ |
| 86 | virtual nsecs_t getVirtualKeyQuietTime() = 0; |
| 87 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 88 | /* Gets the excluded device names for the platform. */ |
| 89 | virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 90 | |
| 91 | /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */ |
| 92 | virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0; |
Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 93 | |
| 94 | /* Gets a spot controller associated with the specified touch pad device. */ |
| 95 | virtual sp<SpotControllerInterface> obtainSpotController(int32_t deviceId) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
| 99 | /* Processes raw input events and sends cooked event data to an input dispatcher. */ |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 100 | class InputReaderInterface : public virtual RefBase { |
| 101 | protected: |
| 102 | InputReaderInterface() { } |
| 103 | virtual ~InputReaderInterface() { } |
| 104 | |
| 105 | public: |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 106 | /* Dumps the state of the input reader. |
| 107 | * |
| 108 | * This method may be called on any thread (usually by the input manager). */ |
| 109 | virtual void dump(String8& dump) = 0; |
| 110 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 111 | /* Runs a single iteration of the processing loop. |
| 112 | * Nominally reads and processes one incoming message from the EventHub. |
| 113 | * |
| 114 | * This method should be called on the input reader thread. |
| 115 | */ |
| 116 | virtual void loopOnce() = 0; |
| 117 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 118 | /* Gets the current input device configuration. |
| 119 | * |
| 120 | * This method may be called on any thread (usually by the input manager). |
| 121 | */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 122 | virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 123 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 124 | /* Gets information about the specified input device. |
| 125 | * Returns OK if the device information was obtained or NAME_NOT_FOUND if there |
| 126 | * was no such device. |
| 127 | * |
| 128 | * This method may be called on any thread (usually by the input manager). |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 129 | */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 130 | virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0; |
| 131 | |
| 132 | /* Gets the list of all registered device ids. */ |
| 133 | virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0; |
| 134 | |
| 135 | /* Query current input state. */ |
| 136 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 137 | int32_t scanCode) = 0; |
| 138 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 139 | int32_t keyCode) = 0; |
| 140 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, |
| 141 | int32_t sw) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 142 | |
| 143 | /* Determine whether physical keys exist for the given framework-domain key codes. */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 144 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 145 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | /* Internal interface used by individual input devices to access global input device state |
| 150 | * and parameters maintained by the input reader. |
| 151 | */ |
| 152 | class InputReaderContext { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 153 | public: |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 154 | InputReaderContext() { } |
| 155 | virtual ~InputReaderContext() { } |
| 156 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 157 | virtual void updateGlobalMetaState() = 0; |
| 158 | virtual int32_t getGlobalMetaState() = 0; |
| 159 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 160 | virtual void disableVirtualKeysUntil(nsecs_t time) = 0; |
| 161 | virtual bool shouldDropVirtualKey(nsecs_t now, |
| 162 | InputDevice* device, int32_t keyCode, int32_t scanCode) = 0; |
| 163 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 164 | virtual void fadePointer() = 0; |
| 165 | |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 166 | virtual void requestTimeoutAtTime(nsecs_t when) = 0; |
| 167 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 168 | virtual InputReaderPolicyInterface* getPolicy() = 0; |
| 169 | virtual InputDispatcherInterface* getDispatcher() = 0; |
| 170 | virtual EventHubInterface* getEventHub() = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 173 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 174 | /* The input reader reads raw event data from the event hub and processes it into input events |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 175 | * that it sends to the input dispatcher. Some functions of the input reader, such as early |
| 176 | * event filtering in low power states, are controlled by a separate policy object. |
| 177 | * |
| 178 | * IMPORTANT INVARIANT: |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 179 | * Because the policy and dispatcher can potentially block or cause re-entrance into |
| 180 | * the input reader, the input reader never calls into other components while holding |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 181 | * an exclusive internal lock whenever re-entrance can happen. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | */ |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 183 | class InputReader : public InputReaderInterface, protected InputReaderContext { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 184 | public: |
| 185 | InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 186 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 187 | const sp<InputDispatcherInterface>& dispatcher); |
| 188 | virtual ~InputReader(); |
| 189 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 190 | virtual void dump(String8& dump); |
| 191 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 192 | virtual void loopOnce(); |
| 193 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 194 | virtual void getInputConfiguration(InputConfiguration* outConfiguration); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 195 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 196 | virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo); |
| 197 | virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 198 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 199 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 200 | int32_t scanCode); |
| 201 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 202 | int32_t keyCode); |
| 203 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, |
| 204 | int32_t sw); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 205 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 206 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 207 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 208 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 209 | protected: |
| 210 | // These methods are protected virtual so they can be overridden and instrumented |
| 211 | // by test cases. |
| 212 | virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes); |
| 213 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 214 | private: |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 215 | sp<EventHubInterface> mEventHub; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 216 | sp<InputReaderPolicyInterface> mPolicy; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 217 | sp<InputDispatcherInterface> mDispatcher; |
| 218 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 219 | virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); } |
| 220 | virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); } |
| 221 | virtual EventHubInterface* getEventHub() { return mEventHub.get(); } |
| 222 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 223 | // The event queue. |
| 224 | static const int EVENT_BUFFER_SIZE = 256; |
| 225 | RawEvent mEventBuffer[EVENT_BUFFER_SIZE]; |
| 226 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 227 | // This reader/writer lock guards the list of input devices. |
| 228 | // The writer lock must be held whenever the list of input devices is modified |
| 229 | // and then promptly released. |
| 230 | // The reader lock must be held whenever the list of input devices is traversed or an |
| 231 | // input device in the list is accessed. |
| 232 | // This lock only protects the registry and prevents inadvertent deletion of device objects |
| 233 | // that are in use. Individual devices are responsible for guarding their own internal state |
| 234 | // as needed for concurrent operation. |
| 235 | RWLock mDeviceRegistryLock; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 236 | KeyedVector<int32_t, InputDevice*> mDevices; |
| 237 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 238 | // low-level input event decoding and device management |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 239 | void processEvents(const RawEvent* rawEvents, size_t count); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 240 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 241 | void addDevice(int32_t deviceId); |
| 242 | void removeDevice(int32_t deviceId); |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 243 | void processEventsForDevice(int32_t deviceId, const RawEvent* rawEvents, size_t count); |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 244 | void timeoutExpired(nsecs_t when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 245 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 246 | void handleConfigurationChanged(nsecs_t when); |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 247 | void configureExcludedDevices(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 248 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 249 | // state management for all devices |
| 250 | Mutex mStateLock; |
| 251 | |
| 252 | int32_t mGlobalMetaState; |
| 253 | virtual void updateGlobalMetaState(); |
| 254 | virtual int32_t getGlobalMetaState(); |
| 255 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 256 | virtual void fadePointer(); |
| 257 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 258 | InputConfiguration mInputConfiguration; |
| 259 | void updateInputConfiguration(); |
| 260 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 261 | nsecs_t mDisableVirtualKeysTimeout; // only accessed by reader thread |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 262 | virtual void disableVirtualKeysUntil(nsecs_t time); |
| 263 | virtual bool shouldDropVirtualKey(nsecs_t now, |
| 264 | InputDevice* device, int32_t keyCode, int32_t scanCode); |
| 265 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 266 | nsecs_t mNextTimeout; // only accessed by reader thread |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 267 | virtual void requestTimeoutAtTime(nsecs_t when); |
| 268 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 269 | // state queries |
| 270 | typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 271 | int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 272 | GetStateFunc getStateFunc); |
| 273 | bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 274 | const int32_t* keyCodes, uint8_t* outFlags); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | |
| 278 | /* Reads raw events from the event hub and processes them, endlessly. */ |
| 279 | class InputReaderThread : public Thread { |
| 280 | public: |
| 281 | InputReaderThread(const sp<InputReaderInterface>& reader); |
| 282 | virtual ~InputReaderThread(); |
| 283 | |
| 284 | private: |
| 285 | sp<InputReaderInterface> mReader; |
| 286 | |
| 287 | virtual bool threadLoop(); |
| 288 | }; |
| 289 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 290 | |
| 291 | /* Represents the state of a single input device. */ |
| 292 | class InputDevice { |
| 293 | public: |
| 294 | InputDevice(InputReaderContext* context, int32_t id, const String8& name); |
| 295 | ~InputDevice(); |
| 296 | |
| 297 | inline InputReaderContext* getContext() { return mContext; } |
| 298 | inline int32_t getId() { return mId; } |
| 299 | inline const String8& getName() { return mName; } |
| 300 | inline uint32_t getSources() { return mSources; } |
| 301 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 302 | inline bool isExternal() { return mIsExternal; } |
| 303 | inline void setExternal(bool external) { mIsExternal = external; } |
| 304 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 305 | inline bool isIgnored() { return mMappers.isEmpty(); } |
| 306 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 307 | void dump(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 308 | void addMapper(InputMapper* mapper); |
| 309 | void configure(); |
| 310 | void reset(); |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 311 | void process(const RawEvent* rawEvents, size_t count); |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 312 | void timeoutExpired(nsecs_t when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 313 | |
| 314 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 315 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 316 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 317 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 318 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 319 | const int32_t* keyCodes, uint8_t* outFlags); |
| 320 | |
| 321 | int32_t getMetaState(); |
| 322 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 323 | void fadePointer(); |
| 324 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 325 | inline const PropertyMap& getConfiguration() { |
| 326 | return mConfiguration; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 329 | private: |
| 330 | InputReaderContext* mContext; |
| 331 | int32_t mId; |
| 332 | |
| 333 | Vector<InputMapper*> mMappers; |
| 334 | |
| 335 | String8 mName; |
| 336 | uint32_t mSources; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 337 | bool mIsExternal; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 338 | |
| 339 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 340 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 341 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 342 | PropertyMap mConfiguration; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | |
| 346 | /* An input mapper transforms raw input events into cooked event data. |
| 347 | * A single input device can have multiple associated input mappers in order to interpret |
| 348 | * different classes of events. |
| 349 | */ |
| 350 | class InputMapper { |
| 351 | public: |
| 352 | InputMapper(InputDevice* device); |
| 353 | virtual ~InputMapper(); |
| 354 | |
| 355 | inline InputDevice* getDevice() { return mDevice; } |
| 356 | inline int32_t getDeviceId() { return mDevice->getId(); } |
| 357 | inline const String8 getDeviceName() { return mDevice->getName(); } |
| 358 | inline InputReaderContext* getContext() { return mContext; } |
| 359 | inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); } |
| 360 | inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); } |
| 361 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 362 | |
| 363 | virtual uint32_t getSources() = 0; |
| 364 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 365 | virtual void dump(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 366 | virtual void configure(); |
| 367 | virtual void reset(); |
| 368 | virtual void process(const RawEvent* rawEvent) = 0; |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 369 | virtual void timeoutExpired(nsecs_t when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 370 | |
| 371 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 372 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 373 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 374 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 375 | const int32_t* keyCodes, uint8_t* outFlags); |
| 376 | |
| 377 | virtual int32_t getMetaState(); |
| 378 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 379 | virtual void fadePointer(); |
| 380 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 381 | protected: |
| 382 | InputDevice* mDevice; |
| 383 | InputReaderContext* mContext; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 384 | |
| 385 | static void dumpRawAbsoluteAxisInfo(String8& dump, |
| 386 | const RawAbsoluteAxisInfo& axis, const char* name); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | |
| 390 | class SwitchInputMapper : public InputMapper { |
| 391 | public: |
| 392 | SwitchInputMapper(InputDevice* device); |
| 393 | virtual ~SwitchInputMapper(); |
| 394 | |
| 395 | virtual uint32_t getSources(); |
| 396 | virtual void process(const RawEvent* rawEvent); |
| 397 | |
| 398 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 399 | |
| 400 | private: |
| 401 | void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue); |
| 402 | }; |
| 403 | |
| 404 | |
| 405 | class KeyboardInputMapper : public InputMapper { |
| 406 | public: |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 407 | KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 408 | virtual ~KeyboardInputMapper(); |
| 409 | |
| 410 | virtual uint32_t getSources(); |
| 411 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 412 | virtual void dump(String8& dump); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 413 | virtual void configure(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 414 | virtual void reset(); |
| 415 | virtual void process(const RawEvent* rawEvent); |
| 416 | |
| 417 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 418 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 419 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 420 | const int32_t* keyCodes, uint8_t* outFlags); |
| 421 | |
| 422 | virtual int32_t getMetaState(); |
| 423 | |
| 424 | private: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 425 | Mutex mLock; |
| 426 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 427 | struct KeyDown { |
| 428 | int32_t keyCode; |
| 429 | int32_t scanCode; |
| 430 | }; |
| 431 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 432 | uint32_t mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 433 | int32_t mKeyboardType; |
| 434 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 435 | // Immutable configuration parameters. |
| 436 | struct Parameters { |
| 437 | int32_t associatedDisplayId; |
| 438 | bool orientationAware; |
| 439 | } mParameters; |
| 440 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 441 | struct LockedState { |
| 442 | Vector<KeyDown> keyDowns; // keys that are down |
| 443 | int32_t metaState; |
| 444 | nsecs_t downTime; // time of most recent key down |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 445 | |
| 446 | struct LedState { |
| 447 | bool avail; // led is available |
| 448 | bool on; // we think the led is currently on |
| 449 | }; |
| 450 | LedState capsLockLedState; |
| 451 | LedState numLockLedState; |
| 452 | LedState scrollLockLedState; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 453 | } mLocked; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 454 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 455 | void initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 456 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 457 | void configureParameters(); |
| 458 | void dumpParameters(String8& dump); |
| 459 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 460 | bool isKeyboardOrGamepadKey(int32_t scanCode); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 461 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 462 | void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode, |
| 463 | uint32_t policyFlags); |
| 464 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 465 | ssize_t findKeyDownLocked(int32_t scanCode); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 466 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 467 | void resetLedStateLocked(); |
| 468 | void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 469 | void updateLedStateLocked(bool reset); |
| 470 | void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led, |
| 471 | int32_t modifier, bool reset); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 475 | class CursorInputMapper : public InputMapper { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 476 | public: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 477 | CursorInputMapper(InputDevice* device); |
| 478 | virtual ~CursorInputMapper(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 479 | |
| 480 | virtual uint32_t getSources(); |
| 481 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 482 | virtual void dump(String8& dump); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 483 | virtual void configure(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 484 | virtual void reset(); |
| 485 | virtual void process(const RawEvent* rawEvent); |
| 486 | |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 487 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 488 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 489 | virtual void fadePointer(); |
| 490 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 491 | private: |
| 492 | // Amount that trackball needs to move in order to generate a key event. |
| 493 | static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; |
| 494 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 495 | Mutex mLock; |
| 496 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 497 | // Immutable configuration parameters. |
| 498 | struct Parameters { |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 499 | enum Mode { |
| 500 | MODE_POINTER, |
| 501 | MODE_NAVIGATION, |
| 502 | }; |
| 503 | |
| 504 | Mode mode; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 505 | int32_t associatedDisplayId; |
| 506 | bool orientationAware; |
| 507 | } mParameters; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 508 | |
| 509 | struct Accumulator { |
| 510 | enum { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 511 | FIELD_BUTTONS = 1, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 512 | FIELD_REL_X = 2, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 513 | FIELD_REL_Y = 4, |
| 514 | FIELD_REL_WHEEL = 8, |
| 515 | FIELD_REL_HWHEEL = 16, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | uint32_t fields; |
| 519 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 520 | uint32_t buttonDown; |
| 521 | uint32_t buttonUp; |
| 522 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 523 | int32_t relX; |
| 524 | int32_t relY; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 525 | int32_t relWheel; |
| 526 | int32_t relHWheel; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 527 | |
| 528 | inline void clear() { |
| 529 | fields = 0; |
| 530 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 531 | } mAccumulator; |
| 532 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 533 | int32_t mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 534 | float mXScale; |
| 535 | float mYScale; |
| 536 | float mXPrecision; |
| 537 | float mYPrecision; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 538 | |
| 539 | bool mHaveVWheel; |
| 540 | bool mHaveHWheel; |
| 541 | float mVWheelScale; |
| 542 | float mHWheelScale; |
| 543 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 544 | sp<PointerControllerInterface> mPointerController; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 545 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 546 | struct LockedState { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 547 | uint32_t buttonState; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 548 | nsecs_t downTime; |
| 549 | } mLocked; |
| 550 | |
| 551 | void initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 552 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 553 | void configureParameters(); |
| 554 | void dumpParameters(String8& dump); |
| 555 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 556 | void sync(nsecs_t when); |
| 557 | }; |
| 558 | |
| 559 | |
| 560 | class TouchInputMapper : public InputMapper { |
| 561 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 562 | TouchInputMapper(InputDevice* device); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 563 | virtual ~TouchInputMapper(); |
| 564 | |
| 565 | virtual uint32_t getSources(); |
| 566 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 567 | virtual void dump(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 568 | virtual void configure(); |
| 569 | virtual void reset(); |
| 570 | |
| 571 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 572 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 573 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 574 | const int32_t* keyCodes, uint8_t* outFlags); |
| 575 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 576 | virtual void fadePointer(); |
| 577 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 578 | protected: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 579 | Mutex mLock; |
| 580 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 581 | struct VirtualKey { |
| 582 | int32_t keyCode; |
| 583 | int32_t scanCode; |
| 584 | uint32_t flags; |
| 585 | |
| 586 | // computed hit box, specified in touch screen coords based on known display size |
| 587 | int32_t hitLeft; |
| 588 | int32_t hitTop; |
| 589 | int32_t hitRight; |
| 590 | int32_t hitBottom; |
| 591 | |
| 592 | inline bool isHit(int32_t x, int32_t y) const { |
| 593 | return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; |
| 594 | } |
| 595 | }; |
| 596 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 597 | // Raw data for a single pointer. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 598 | struct PointerData { |
| 599 | uint32_t id; |
| 600 | int32_t x; |
| 601 | int32_t y; |
| 602 | int32_t pressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 603 | int32_t touchMajor; |
| 604 | int32_t touchMinor; |
| 605 | int32_t toolMajor; |
| 606 | int32_t toolMinor; |
| 607 | int32_t orientation; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 608 | |
| 609 | inline bool operator== (const PointerData& other) const { |
| 610 | return id == other.id |
| 611 | && x == other.x |
| 612 | && y == other.y |
| 613 | && pressure == other.pressure |
| 614 | && touchMajor == other.touchMajor |
| 615 | && touchMinor == other.touchMinor |
| 616 | && toolMajor == other.toolMajor |
| 617 | && toolMinor == other.toolMinor |
| 618 | && orientation == other.orientation; |
| 619 | } |
| 620 | inline bool operator!= (const PointerData& other) const { |
| 621 | return !(*this == other); |
| 622 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 625 | // Raw data for a collection of pointers including a pointer id mapping table. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 626 | struct TouchData { |
| 627 | uint32_t pointerCount; |
| 628 | PointerData pointers[MAX_POINTERS]; |
| 629 | BitSet32 idBits; |
| 630 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 631 | uint32_t buttonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 632 | |
| 633 | void copyFrom(const TouchData& other) { |
| 634 | pointerCount = other.pointerCount; |
| 635 | idBits = other.idBits; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 636 | buttonState = other.buttonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 637 | |
| 638 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 639 | pointers[i] = other.pointers[i]; |
Jeff Brown | 9e2ad36 | 2010-07-30 19:20:11 -0700 | [diff] [blame] | 640 | |
| 641 | int id = pointers[i].id; |
| 642 | idToIndex[id] = other.idToIndex[id]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | inline void clear() { |
| 647 | pointerCount = 0; |
| 648 | idBits.clear(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 649 | buttonState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 650 | } |
| 651 | }; |
| 652 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 653 | // Input sources supported by the device. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 654 | uint32_t mTouchSource; // sources when reporting touch data |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 655 | uint32_t mPointerSource; // sources when reporting pointer gestures |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 656 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 657 | // Immutable configuration parameters. |
| 658 | struct Parameters { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 659 | enum DeviceType { |
| 660 | DEVICE_TYPE_TOUCH_SCREEN, |
| 661 | DEVICE_TYPE_TOUCH_PAD, |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 662 | DEVICE_TYPE_POINTER, |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 663 | }; |
| 664 | |
| 665 | DeviceType deviceType; |
| 666 | int32_t associatedDisplayId; |
| 667 | bool orientationAware; |
| 668 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 669 | bool useBadTouchFilter; |
| 670 | bool useJumpyTouchFilter; |
| 671 | bool useAveragingTouchFilter; |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 672 | nsecs_t virtualKeyQuietTime; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 673 | } mParameters; |
| 674 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 675 | // Immutable calibration parameters in parsed form. |
| 676 | struct Calibration { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 677 | // Touch Size |
| 678 | enum TouchSizeCalibration { |
| 679 | TOUCH_SIZE_CALIBRATION_DEFAULT, |
| 680 | TOUCH_SIZE_CALIBRATION_NONE, |
| 681 | TOUCH_SIZE_CALIBRATION_GEOMETRIC, |
| 682 | TOUCH_SIZE_CALIBRATION_PRESSURE, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 683 | }; |
| 684 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 685 | TouchSizeCalibration touchSizeCalibration; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 686 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 687 | // Tool Size |
| 688 | enum ToolSizeCalibration { |
| 689 | TOOL_SIZE_CALIBRATION_DEFAULT, |
| 690 | TOOL_SIZE_CALIBRATION_NONE, |
| 691 | TOOL_SIZE_CALIBRATION_GEOMETRIC, |
| 692 | TOOL_SIZE_CALIBRATION_LINEAR, |
| 693 | TOOL_SIZE_CALIBRATION_AREA, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 694 | }; |
| 695 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 696 | ToolSizeCalibration toolSizeCalibration; |
| 697 | bool haveToolSizeLinearScale; |
| 698 | float toolSizeLinearScale; |
| 699 | bool haveToolSizeLinearBias; |
| 700 | float toolSizeLinearBias; |
| 701 | bool haveToolSizeAreaScale; |
| 702 | float toolSizeAreaScale; |
| 703 | bool haveToolSizeAreaBias; |
| 704 | float toolSizeAreaBias; |
| 705 | bool haveToolSizeIsSummed; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 706 | bool toolSizeIsSummed; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 707 | |
| 708 | // Pressure |
| 709 | enum PressureCalibration { |
| 710 | PRESSURE_CALIBRATION_DEFAULT, |
| 711 | PRESSURE_CALIBRATION_NONE, |
| 712 | PRESSURE_CALIBRATION_PHYSICAL, |
| 713 | PRESSURE_CALIBRATION_AMPLITUDE, |
| 714 | }; |
| 715 | enum PressureSource { |
| 716 | PRESSURE_SOURCE_DEFAULT, |
| 717 | PRESSURE_SOURCE_PRESSURE, |
| 718 | PRESSURE_SOURCE_TOUCH, |
| 719 | }; |
| 720 | |
| 721 | PressureCalibration pressureCalibration; |
| 722 | PressureSource pressureSource; |
| 723 | bool havePressureScale; |
| 724 | float pressureScale; |
| 725 | |
| 726 | // Size |
| 727 | enum SizeCalibration { |
| 728 | SIZE_CALIBRATION_DEFAULT, |
| 729 | SIZE_CALIBRATION_NONE, |
| 730 | SIZE_CALIBRATION_NORMALIZED, |
| 731 | }; |
| 732 | |
| 733 | SizeCalibration sizeCalibration; |
| 734 | |
| 735 | // Orientation |
| 736 | enum OrientationCalibration { |
| 737 | ORIENTATION_CALIBRATION_DEFAULT, |
| 738 | ORIENTATION_CALIBRATION_NONE, |
| 739 | ORIENTATION_CALIBRATION_INTERPOLATED, |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 740 | ORIENTATION_CALIBRATION_VECTOR, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | OrientationCalibration orientationCalibration; |
| 744 | } mCalibration; |
| 745 | |
| 746 | // Raw axis information from the driver. |
| 747 | struct RawAxes { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 748 | RawAbsoluteAxisInfo x; |
| 749 | RawAbsoluteAxisInfo y; |
| 750 | RawAbsoluteAxisInfo pressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 751 | RawAbsoluteAxisInfo touchMajor; |
| 752 | RawAbsoluteAxisInfo touchMinor; |
| 753 | RawAbsoluteAxisInfo toolMajor; |
| 754 | RawAbsoluteAxisInfo toolMinor; |
| 755 | RawAbsoluteAxisInfo orientation; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 756 | } mRawAxes; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 757 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 758 | // Current and previous touch sample data. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 759 | TouchData mCurrentTouch; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 760 | PointerCoords mCurrentTouchCoords[MAX_POINTERS]; |
| 761 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 762 | TouchData mLastTouch; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 763 | PointerCoords mLastTouchCoords[MAX_POINTERS]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 764 | |
| 765 | // The time the primary pointer last went down. |
| 766 | nsecs_t mDownTime; |
| 767 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 768 | // The pointer controller, or null if the device is not a pointer. |
| 769 | sp<PointerControllerInterface> mPointerController; |
| 770 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 771 | struct LockedState { |
| 772 | Vector<VirtualKey> virtualKeys; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 773 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 774 | // The surface orientation and width and height set by configureSurfaceLocked(). |
| 775 | int32_t surfaceOrientation; |
| 776 | int32_t surfaceWidth, surfaceHeight; |
| 777 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 778 | // The associated display orientation and width and height set by configureSurfaceLocked(). |
| 779 | int32_t associatedDisplayOrientation; |
| 780 | int32_t associatedDisplayWidth, associatedDisplayHeight; |
| 781 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 782 | // Translation and scaling factors, orientation-independent. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 783 | float xScale; |
| 784 | float xPrecision; |
| 785 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 786 | float yScale; |
| 787 | float yPrecision; |
| 788 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 789 | float geometricScale; |
| 790 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 791 | float toolSizeLinearScale; |
| 792 | float toolSizeLinearBias; |
| 793 | float toolSizeAreaScale; |
| 794 | float toolSizeAreaBias; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 795 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 796 | float pressureScale; |
| 797 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 798 | float sizeScale; |
| 799 | |
| 800 | float orientationScale; |
| 801 | |
| 802 | // Oriented motion ranges for input device info. |
| 803 | struct OrientedRanges { |
| 804 | InputDeviceInfo::MotionRange x; |
| 805 | InputDeviceInfo::MotionRange y; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 806 | |
| 807 | bool havePressure; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 808 | InputDeviceInfo::MotionRange pressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 809 | |
| 810 | bool haveSize; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 811 | InputDeviceInfo::MotionRange size; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 812 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 813 | bool haveTouchSize; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 814 | InputDeviceInfo::MotionRange touchMajor; |
| 815 | InputDeviceInfo::MotionRange touchMinor; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 816 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 817 | bool haveToolSize; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 818 | InputDeviceInfo::MotionRange toolMajor; |
| 819 | InputDeviceInfo::MotionRange toolMinor; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 820 | |
| 821 | bool haveOrientation; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 822 | InputDeviceInfo::MotionRange orientation; |
| 823 | } orientedRanges; |
| 824 | |
| 825 | // Oriented dimensions and precision. |
| 826 | float orientedSurfaceWidth, orientedSurfaceHeight; |
| 827 | float orientedXPrecision, orientedYPrecision; |
| 828 | |
| 829 | struct CurrentVirtualKeyState { |
| 830 | bool down; |
| 831 | nsecs_t downTime; |
| 832 | int32_t keyCode; |
| 833 | int32_t scanCode; |
| 834 | } currentVirtualKey; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 835 | |
| 836 | // Scale factor for gesture based pointer movements. |
| 837 | float pointerGestureXMovementScale; |
| 838 | float pointerGestureYMovementScale; |
| 839 | |
| 840 | // Scale factor for gesture based zooming and other freeform motions. |
| 841 | float pointerGestureXZoomScale; |
| 842 | float pointerGestureYZoomScale; |
| 843 | |
| 844 | // The maximum swipe width squared. |
| 845 | int32_t pointerGestureMaxSwipeWidthSquared; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 846 | } mLocked; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 847 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 848 | virtual void configureParameters(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 849 | virtual void dumpParameters(String8& dump); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 850 | virtual void configureRawAxes(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 851 | virtual void dumpRawAxes(String8& dump); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 852 | virtual bool configureSurfaceLocked(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 853 | virtual void dumpSurfaceLocked(String8& dump); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 854 | virtual void configureVirtualKeysLocked(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 855 | virtual void dumpVirtualKeysLocked(String8& dump); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 856 | virtual void parseCalibration(); |
| 857 | virtual void resolveCalibration(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 858 | virtual void dumpCalibration(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 859 | |
| 860 | enum TouchResult { |
| 861 | // Dispatch the touch normally. |
| 862 | DISPATCH_TOUCH, |
| 863 | // Do not dispatch the touch, but keep tracking the current stroke. |
| 864 | SKIP_TOUCH, |
| 865 | // Do not dispatch the touch, and drop all information associated with the current stoke |
| 866 | // so the next movement will appear as a new down. |
| 867 | DROP_STROKE |
| 868 | }; |
| 869 | |
| 870 | void syncTouch(nsecs_t when, bool havePointerIds); |
| 871 | |
| 872 | private: |
| 873 | /* Maximum number of historical samples to average. */ |
| 874 | static const uint32_t AVERAGING_HISTORY_SIZE = 5; |
| 875 | |
| 876 | /* Slop distance for jumpy pointer detection. |
| 877 | * The vertical range of the screen divided by this is our epsilon value. */ |
| 878 | static const uint32_t JUMPY_EPSILON_DIVISOR = 212; |
| 879 | |
| 880 | /* Number of jumpy points to drop for touchscreens that need it. */ |
| 881 | static const uint32_t JUMPY_TRANSITION_DROPS = 3; |
| 882 | static const uint32_t JUMPY_DROP_LIMIT = 3; |
| 883 | |
| 884 | /* Maximum squared distance for averaging. |
| 885 | * If moving farther than this, turn of averaging to avoid lag in response. */ |
| 886 | static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75; |
| 887 | |
| 888 | struct AveragingTouchFilterState { |
| 889 | // Individual history tracks are stored by pointer id |
| 890 | uint32_t historyStart[MAX_POINTERS]; |
| 891 | uint32_t historyEnd[MAX_POINTERS]; |
| 892 | struct { |
| 893 | struct { |
| 894 | int32_t x; |
| 895 | int32_t y; |
| 896 | int32_t pressure; |
| 897 | } pointers[MAX_POINTERS]; |
| 898 | } historyData[AVERAGING_HISTORY_SIZE]; |
| 899 | } mAveragingTouchFilter; |
| 900 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 901 | struct JumpyTouchFilterState { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 902 | uint32_t jumpyPointsDropped; |
| 903 | } mJumpyTouchFilter; |
| 904 | |
| 905 | struct PointerDistanceHeapElement { |
| 906 | uint32_t currentPointerIndex : 8; |
| 907 | uint32_t lastPointerIndex : 8; |
| 908 | uint64_t distance : 48; // squared distance |
| 909 | }; |
| 910 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 911 | struct PointerGesture { |
| 912 | enum Mode { |
| 913 | // No fingers, button is not pressed. |
| 914 | // Nothing happening. |
| 915 | NEUTRAL, |
| 916 | |
| 917 | // No fingers, button is not pressed. |
| 918 | // Tap detected. |
| 919 | // Emits DOWN and UP events at the pointer location. |
| 920 | TAP, |
| 921 | |
| 922 | // Button is pressed. |
| 923 | // Pointer follows the active finger if there is one. Other fingers are ignored. |
| 924 | // Emits DOWN, MOVE and UP events at the pointer location. |
| 925 | CLICK_OR_DRAG, |
| 926 | |
| 927 | // Exactly one finger, button is not pressed. |
| 928 | // Pointer follows the active finger. |
| 929 | // Emits HOVER_MOVE events at the pointer location. |
| 930 | HOVER, |
| 931 | |
| 932 | // More than two fingers involved but they haven't moved enough for us |
| 933 | // to figure out what is intended. |
| 934 | INDETERMINATE_MULTITOUCH, |
| 935 | |
| 936 | // Exactly two fingers moving in the same direction, button is not pressed. |
| 937 | // Pointer does not move. |
| 938 | // Emits DOWN, MOVE and UP events with a single pointer coordinate that |
| 939 | // follows the midpoint between both fingers. |
| 940 | // The centroid is fixed when entering this state. |
| 941 | SWIPE, |
| 942 | |
| 943 | // Two or more fingers moving in arbitrary directions, button is not pressed. |
| 944 | // Pointer does not move. |
| 945 | // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow |
| 946 | // each finger individually relative to the initial centroid of the finger. |
| 947 | // The centroid is fixed when entering this state. |
| 948 | FREEFORM, |
| 949 | |
| 950 | // Waiting for quiet time to end before starting the next gesture. |
| 951 | QUIET, |
| 952 | }; |
| 953 | |
| 954 | // The active pointer id from the raw touch data. |
| 955 | int32_t activeTouchId; // -1 if none |
| 956 | |
| 957 | // The active pointer id from the gesture last delivered to the application. |
| 958 | int32_t activeGestureId; // -1 if none |
| 959 | |
| 960 | // Pointer coords and ids for the current and previous pointer gesture. |
| 961 | Mode currentGestureMode; |
| 962 | uint32_t currentGesturePointerCount; |
| 963 | BitSet32 currentGestureIdBits; |
| 964 | uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1]; |
| 965 | PointerCoords currentGestureCoords[MAX_POINTERS]; |
| 966 | |
| 967 | Mode lastGestureMode; |
| 968 | uint32_t lastGesturePointerCount; |
| 969 | BitSet32 lastGestureIdBits; |
| 970 | uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1]; |
| 971 | PointerCoords lastGestureCoords[MAX_POINTERS]; |
| 972 | |
| 973 | // Tracks for all pointers originally went down. |
| 974 | TouchData touchOrigin; |
| 975 | |
| 976 | // Describes how touch ids are mapped to gesture ids for freeform gestures. |
| 977 | uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1]; |
| 978 | |
| 979 | // Initial centroid of the movement. |
| 980 | // Used to calculate how far the touch pointers have moved since the gesture started. |
| 981 | int32_t initialCentroidX; |
| 982 | int32_t initialCentroidY; |
| 983 | |
| 984 | // Initial pointer location. |
| 985 | // Used to track where the pointer was when the gesture started. |
| 986 | float initialPointerX; |
| 987 | float initialPointerY; |
| 988 | |
| 989 | // Time the pointer gesture last went down. |
| 990 | nsecs_t downTime; |
| 991 | |
| 992 | // Time we started waiting for a tap gesture. |
| 993 | nsecs_t tapTime; |
| 994 | |
| 995 | // Time we started waiting for quiescence. |
| 996 | nsecs_t quietTime; |
| 997 | |
| 998 | // A velocity tracker for determining whether to switch active pointers during drags. |
| 999 | VelocityTracker velocityTracker; |
| 1000 | |
| 1001 | void reset() { |
| 1002 | activeTouchId = -1; |
| 1003 | activeGestureId = -1; |
| 1004 | currentGestureMode = NEUTRAL; |
| 1005 | currentGesturePointerCount = 0; |
| 1006 | currentGestureIdBits.clear(); |
| 1007 | lastGestureMode = NEUTRAL; |
| 1008 | lastGesturePointerCount = 0; |
| 1009 | lastGestureIdBits.clear(); |
| 1010 | touchOrigin.clear(); |
| 1011 | initialCentroidX = 0; |
| 1012 | initialCentroidY = 0; |
| 1013 | initialPointerX = 0; |
| 1014 | initialPointerY = 0; |
| 1015 | downTime = 0; |
| 1016 | velocityTracker.clear(); |
| 1017 | resetTapTime(); |
| 1018 | resetQuietTime(); |
| 1019 | } |
| 1020 | |
| 1021 | void resetTapTime() { |
| 1022 | tapTime = LLONG_MIN; |
| 1023 | } |
| 1024 | |
| 1025 | void resetQuietTime() { |
| 1026 | quietTime = LLONG_MIN; |
| 1027 | } |
| 1028 | } mPointerGesture; |
| 1029 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1030 | void initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1031 | |
| 1032 | TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags); |
| 1033 | void dispatchTouches(nsecs_t when, uint32_t policyFlags); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1034 | void prepareTouches(int32_t* outEdgeFlags, float* outXPrecision, float* outYPrecision); |
| 1035 | void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags); |
| 1036 | void preparePointerGestures(nsecs_t when, |
| 1037 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture); |
| 1038 | |
| 1039 | // Dispatches a motion event. |
| 1040 | // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the |
| 1041 | // method will take care of setting the index and transmuting the action to DOWN or UP |
| 1042 | // it is the first / last pointer to go down / up. |
| 1043 | void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
| 1044 | int32_t action, int32_t flags, uint32_t metaState, int32_t edgeFlags, |
| 1045 | const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits, |
| 1046 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime); |
| 1047 | |
| 1048 | // Updates pointer coords for pointers with specified ids that have moved. |
| 1049 | // Returns true if any of them changed. |
| 1050 | bool updateMovedPointerCoords(const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
| 1051 | PointerCoords* outCoords, const uint32_t* outIdToIndex, BitSet32 idBits) const; |
| 1052 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1053 | void suppressSwipeOntoVirtualKeys(nsecs_t when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1054 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1055 | bool isPointInsideSurfaceLocked(int32_t x, int32_t y); |
| 1056 | const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1057 | |
| 1058 | bool applyBadTouchFilter(); |
| 1059 | bool applyJumpyTouchFilter(); |
| 1060 | void applyAveragingTouchFilter(); |
| 1061 | void calculatePointerIds(); |
| 1062 | }; |
| 1063 | |
| 1064 | |
| 1065 | class SingleTouchInputMapper : public TouchInputMapper { |
| 1066 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1067 | SingleTouchInputMapper(InputDevice* device); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1068 | virtual ~SingleTouchInputMapper(); |
| 1069 | |
| 1070 | virtual void reset(); |
| 1071 | virtual void process(const RawEvent* rawEvent); |
| 1072 | |
| 1073 | protected: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1074 | virtual void configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1075 | |
| 1076 | private: |
| 1077 | struct Accumulator { |
| 1078 | enum { |
| 1079 | FIELD_BTN_TOUCH = 1, |
| 1080 | FIELD_ABS_X = 2, |
| 1081 | FIELD_ABS_Y = 4, |
| 1082 | FIELD_ABS_PRESSURE = 8, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1083 | FIELD_ABS_TOOL_WIDTH = 16, |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1084 | FIELD_BUTTONS = 32, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1085 | }; |
| 1086 | |
| 1087 | uint32_t fields; |
| 1088 | |
| 1089 | bool btnTouch; |
| 1090 | int32_t absX; |
| 1091 | int32_t absY; |
| 1092 | int32_t absPressure; |
| 1093 | int32_t absToolWidth; |
| 1094 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1095 | uint32_t buttonDown; |
| 1096 | uint32_t buttonUp; |
| 1097 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1098 | inline void clear() { |
| 1099 | fields = 0; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1100 | buttonDown = 0; |
| 1101 | buttonUp = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1102 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1103 | } mAccumulator; |
| 1104 | |
| 1105 | bool mDown; |
| 1106 | int32_t mX; |
| 1107 | int32_t mY; |
| 1108 | int32_t mPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1109 | int32_t mToolWidth; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1110 | uint32_t mButtonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1111 | |
| 1112 | void initialize(); |
| 1113 | |
| 1114 | void sync(nsecs_t when); |
| 1115 | }; |
| 1116 | |
| 1117 | |
| 1118 | class MultiTouchInputMapper : public TouchInputMapper { |
| 1119 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1120 | MultiTouchInputMapper(InputDevice* device); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1121 | virtual ~MultiTouchInputMapper(); |
| 1122 | |
| 1123 | virtual void reset(); |
| 1124 | virtual void process(const RawEvent* rawEvent); |
| 1125 | |
| 1126 | protected: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1127 | virtual void configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1128 | |
| 1129 | private: |
| 1130 | struct Accumulator { |
| 1131 | enum { |
| 1132 | FIELD_ABS_MT_POSITION_X = 1, |
| 1133 | FIELD_ABS_MT_POSITION_Y = 2, |
| 1134 | FIELD_ABS_MT_TOUCH_MAJOR = 4, |
| 1135 | FIELD_ABS_MT_TOUCH_MINOR = 8, |
| 1136 | FIELD_ABS_MT_WIDTH_MAJOR = 16, |
| 1137 | FIELD_ABS_MT_WIDTH_MINOR = 32, |
| 1138 | FIELD_ABS_MT_ORIENTATION = 64, |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1139 | FIELD_ABS_MT_TRACKING_ID = 128, |
| 1140 | FIELD_ABS_MT_PRESSURE = 256, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1141 | }; |
| 1142 | |
| 1143 | uint32_t pointerCount; |
| 1144 | struct Pointer { |
| 1145 | uint32_t fields; |
| 1146 | |
| 1147 | int32_t absMTPositionX; |
| 1148 | int32_t absMTPositionY; |
| 1149 | int32_t absMTTouchMajor; |
| 1150 | int32_t absMTTouchMinor; |
| 1151 | int32_t absMTWidthMajor; |
| 1152 | int32_t absMTWidthMinor; |
| 1153 | int32_t absMTOrientation; |
| 1154 | int32_t absMTTrackingId; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1155 | int32_t absMTPressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1156 | |
| 1157 | inline void clear() { |
| 1158 | fields = 0; |
| 1159 | } |
| 1160 | } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks |
| 1161 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1162 | // Bitfield of buttons that went down or up. |
| 1163 | uint32_t buttonDown; |
| 1164 | uint32_t buttonUp; |
| 1165 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1166 | inline void clear() { |
| 1167 | pointerCount = 0; |
| 1168 | pointers[0].clear(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1169 | buttonDown = 0; |
| 1170 | buttonUp = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1171 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1172 | } mAccumulator; |
| 1173 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1174 | uint32_t mButtonState; |
| 1175 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1176 | void initialize(); |
| 1177 | |
| 1178 | void sync(nsecs_t when); |
| 1179 | }; |
| 1180 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1181 | |
| 1182 | class JoystickInputMapper : public InputMapper { |
| 1183 | public: |
| 1184 | JoystickInputMapper(InputDevice* device); |
| 1185 | virtual ~JoystickInputMapper(); |
| 1186 | |
| 1187 | virtual uint32_t getSources(); |
| 1188 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
| 1189 | virtual void dump(String8& dump); |
| 1190 | virtual void configure(); |
| 1191 | virtual void reset(); |
| 1192 | virtual void process(const RawEvent* rawEvent); |
| 1193 | |
| 1194 | private: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1195 | struct Axis { |
| 1196 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1197 | AxisInfo axisInfo; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1198 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1199 | bool explicitlyMapped; // true if the axis was explicitly assigned an axis id |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1200 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1201 | float scale; // scale factor from raw to normalized values |
| 1202 | float offset; // offset to add after scaling for normalization |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1203 | float highScale; // scale factor from raw to normalized values of high split |
| 1204 | float highOffset; // offset to add after scaling for normalization of high split |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1205 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1206 | float min; // normalized inclusive minimum |
| 1207 | float max; // normalized inclusive maximum |
| 1208 | float flat; // normalized flat region size |
| 1209 | float fuzz; // normalized error tolerance |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1210 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1211 | float filter; // filter out small variations of this size |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1212 | float currentValue; // current value |
| 1213 | float newValue; // most recent value |
| 1214 | float highCurrentValue; // current value of high split |
| 1215 | float highNewValue; // most recent value of high split |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1216 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1217 | void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo, |
| 1218 | bool explicitlyMapped, float scale, float offset, |
| 1219 | float highScale, float highOffset, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1220 | float min, float max, float flat, float fuzz) { |
| 1221 | this->rawAxisInfo = rawAxisInfo; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1222 | this->axisInfo = axisInfo; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1223 | this->explicitlyMapped = explicitlyMapped; |
| 1224 | this->scale = scale; |
| 1225 | this->offset = offset; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1226 | this->highScale = highScale; |
| 1227 | this->highOffset = highOffset; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1228 | this->min = min; |
| 1229 | this->max = max; |
| 1230 | this->flat = flat; |
| 1231 | this->fuzz = fuzz; |
| 1232 | this->filter = 0; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1233 | resetValue(); |
| 1234 | } |
| 1235 | |
| 1236 | void resetValue() { |
| 1237 | this->currentValue = 0; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1238 | this->newValue = 0; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1239 | this->highCurrentValue = 0; |
| 1240 | this->highNewValue = 0; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1241 | } |
| 1242 | }; |
| 1243 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1244 | // Axes indexed by raw ABS_* axis index. |
| 1245 | KeyedVector<int32_t, Axis> mAxes; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1246 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1247 | void sync(nsecs_t when, bool force); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1248 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1249 | bool haveAxis(int32_t axisId); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1250 | void pruneAxes(bool ignoreExplicitlyMappedAxes); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 1251 | bool filterAxes(bool force); |
| 1252 | |
| 1253 | static bool hasValueChangedSignificantly(float filter, |
| 1254 | float newValue, float currentValue, float min, float max); |
| 1255 | static bool hasMovedNearerToValueWithinFilteredRange(float filter, |
| 1256 | float newValue, float currentValue, float thresholdValue); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1257 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1258 | static bool isCenteredAxis(int32_t axis); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1259 | }; |
| 1260 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1261 | } // namespace android |
| 1262 | |
| 1263 | #endif // _UI_INPUT_READER_H |