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