Jeff Brown | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 160 | virtual InputReaderPolicyInterface* getPolicy() = 0; |
| 161 | virtual InputDispatcherInterface* getDispatcher() = 0; |
| 162 | virtual EventHubInterface* getEventHub() = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 165 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 166 | /* 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] | 167 | * that it sends to the input dispatcher. Some functions of the input reader, such as early |
| 168 | * event filtering in low power states, are controlled by a separate policy object. |
| 169 | * |
| 170 | * IMPORTANT INVARIANT: |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 171 | * Because the policy and dispatcher can potentially block or cause re-entrance into |
| 172 | * 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] | 173 | * an exclusive internal lock whenever re-entrance can happen. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 174 | */ |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 175 | class InputReader : public InputReaderInterface, protected InputReaderContext { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 176 | public: |
| 177 | InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 178 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 179 | const sp<InputDispatcherInterface>& dispatcher); |
| 180 | virtual ~InputReader(); |
| 181 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 182 | virtual void dump(String8& dump); |
| 183 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 184 | virtual void loopOnce(); |
| 185 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 186 | virtual void getInputConfiguration(InputConfiguration* outConfiguration); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 187 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 188 | virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo); |
| 189 | virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 190 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 191 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 192 | int32_t scanCode); |
| 193 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 194 | int32_t keyCode); |
| 195 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, |
| 196 | int32_t sw); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 197 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 198 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 199 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 200 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 201 | protected: |
| 202 | // These methods are protected virtual so they can be overridden and instrumented |
| 203 | // by test cases. |
| 204 | virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes); |
| 205 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 206 | private: |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 207 | sp<EventHubInterface> mEventHub; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 208 | sp<InputReaderPolicyInterface> mPolicy; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 209 | sp<InputDispatcherInterface> mDispatcher; |
| 210 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 211 | virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); } |
| 212 | virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); } |
| 213 | virtual EventHubInterface* getEventHub() { return mEventHub.get(); } |
| 214 | |
| 215 | // This reader/writer lock guards the list of input devices. |
| 216 | // The writer lock must be held whenever the list of input devices is modified |
| 217 | // and then promptly released. |
| 218 | // The reader lock must be held whenever the list of input devices is traversed or an |
| 219 | // input device in the list is accessed. |
| 220 | // This lock only protects the registry and prevents inadvertent deletion of device objects |
| 221 | // that are in use. Individual devices are responsible for guarding their own internal state |
| 222 | // as needed for concurrent operation. |
| 223 | RWLock mDeviceRegistryLock; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 224 | KeyedVector<int32_t, InputDevice*> mDevices; |
| 225 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 226 | // low-level input event decoding and device management |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 227 | void process(const RawEvent* rawEvent); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 229 | void addDevice(int32_t deviceId); |
| 230 | void removeDevice(int32_t deviceId); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 231 | void configureExcludedDevices(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 232 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 233 | void consumeEvent(const RawEvent* rawEvent); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 234 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 235 | void handleConfigurationChanged(nsecs_t when); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 236 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 237 | // state management for all devices |
| 238 | Mutex mStateLock; |
| 239 | |
| 240 | int32_t mGlobalMetaState; |
| 241 | virtual void updateGlobalMetaState(); |
| 242 | virtual int32_t getGlobalMetaState(); |
| 243 | |
| 244 | InputConfiguration mInputConfiguration; |
| 245 | void updateInputConfiguration(); |
| 246 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 247 | nsecs_t mDisableVirtualKeysTimeout; |
| 248 | virtual void disableVirtualKeysUntil(nsecs_t time); |
| 249 | virtual bool shouldDropVirtualKey(nsecs_t now, |
| 250 | InputDevice* device, int32_t keyCode, int32_t scanCode); |
| 251 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 252 | // state queries |
| 253 | typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 254 | int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 255 | GetStateFunc getStateFunc); |
| 256 | bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 257 | const int32_t* keyCodes, uint8_t* outFlags); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | |
| 261 | /* Reads raw events from the event hub and processes them, endlessly. */ |
| 262 | class InputReaderThread : public Thread { |
| 263 | public: |
| 264 | InputReaderThread(const sp<InputReaderInterface>& reader); |
| 265 | virtual ~InputReaderThread(); |
| 266 | |
| 267 | private: |
| 268 | sp<InputReaderInterface> mReader; |
| 269 | |
| 270 | virtual bool threadLoop(); |
| 271 | }; |
| 272 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 273 | |
| 274 | /* Represents the state of a single input device. */ |
| 275 | class InputDevice { |
| 276 | public: |
| 277 | InputDevice(InputReaderContext* context, int32_t id, const String8& name); |
| 278 | ~InputDevice(); |
| 279 | |
| 280 | inline InputReaderContext* getContext() { return mContext; } |
| 281 | inline int32_t getId() { return mId; } |
| 282 | inline const String8& getName() { return mName; } |
| 283 | inline uint32_t getSources() { return mSources; } |
| 284 | |
| 285 | inline bool isIgnored() { return mMappers.isEmpty(); } |
| 286 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 287 | void dump(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 288 | void addMapper(InputMapper* mapper); |
| 289 | void configure(); |
| 290 | void reset(); |
| 291 | void process(const RawEvent* rawEvent); |
| 292 | |
| 293 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 294 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 295 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 296 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 297 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 298 | const int32_t* keyCodes, uint8_t* outFlags); |
| 299 | |
| 300 | int32_t getMetaState(); |
| 301 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 302 | inline const PropertyMap& getConfiguration() { |
| 303 | return mConfiguration; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 306 | private: |
| 307 | InputReaderContext* mContext; |
| 308 | int32_t mId; |
| 309 | |
| 310 | Vector<InputMapper*> mMappers; |
| 311 | |
| 312 | String8 mName; |
| 313 | uint32_t mSources; |
| 314 | |
| 315 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 316 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 317 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 318 | PropertyMap mConfiguration; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | |
| 322 | /* An input mapper transforms raw input events into cooked event data. |
| 323 | * A single input device can have multiple associated input mappers in order to interpret |
| 324 | * different classes of events. |
| 325 | */ |
| 326 | class InputMapper { |
| 327 | public: |
| 328 | InputMapper(InputDevice* device); |
| 329 | virtual ~InputMapper(); |
| 330 | |
| 331 | inline InputDevice* getDevice() { return mDevice; } |
| 332 | inline int32_t getDeviceId() { return mDevice->getId(); } |
| 333 | inline const String8 getDeviceName() { return mDevice->getName(); } |
| 334 | inline InputReaderContext* getContext() { return mContext; } |
| 335 | inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); } |
| 336 | inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); } |
| 337 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 338 | |
| 339 | virtual uint32_t getSources() = 0; |
| 340 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 341 | virtual void dump(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 342 | virtual void configure(); |
| 343 | virtual void reset(); |
| 344 | virtual void process(const RawEvent* rawEvent) = 0; |
| 345 | |
| 346 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 347 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 348 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 349 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 350 | const int32_t* keyCodes, uint8_t* outFlags); |
| 351 | |
| 352 | virtual int32_t getMetaState(); |
| 353 | |
| 354 | protected: |
| 355 | InputDevice* mDevice; |
| 356 | InputReaderContext* mContext; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 357 | |
| 358 | static void dumpRawAbsoluteAxisInfo(String8& dump, |
| 359 | const RawAbsoluteAxisInfo& axis, const char* name); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | |
| 363 | class SwitchInputMapper : public InputMapper { |
| 364 | public: |
| 365 | SwitchInputMapper(InputDevice* device); |
| 366 | virtual ~SwitchInputMapper(); |
| 367 | |
| 368 | virtual uint32_t getSources(); |
| 369 | virtual void process(const RawEvent* rawEvent); |
| 370 | |
| 371 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 372 | |
| 373 | private: |
| 374 | void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue); |
| 375 | }; |
| 376 | |
| 377 | |
| 378 | class KeyboardInputMapper : public InputMapper { |
| 379 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 380 | KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 381 | virtual ~KeyboardInputMapper(); |
| 382 | |
| 383 | virtual uint32_t getSources(); |
| 384 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 385 | virtual void dump(String8& dump); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 386 | virtual void configure(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 387 | virtual void reset(); |
| 388 | virtual void process(const RawEvent* rawEvent); |
| 389 | |
| 390 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 391 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 392 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 393 | const int32_t* keyCodes, uint8_t* outFlags); |
| 394 | |
| 395 | virtual int32_t getMetaState(); |
| 396 | |
| 397 | private: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 398 | Mutex mLock; |
| 399 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 400 | struct KeyDown { |
| 401 | int32_t keyCode; |
| 402 | int32_t scanCode; |
| 403 | }; |
| 404 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 405 | uint32_t mSources; |
| 406 | int32_t mKeyboardType; |
| 407 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 408 | // Immutable configuration parameters. |
| 409 | struct Parameters { |
| 410 | int32_t associatedDisplayId; |
| 411 | bool orientationAware; |
| 412 | } mParameters; |
| 413 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 414 | struct LockedState { |
| 415 | Vector<KeyDown> keyDowns; // keys that are down |
| 416 | int32_t metaState; |
| 417 | nsecs_t downTime; // time of most recent key down |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 418 | |
| 419 | struct LedState { |
| 420 | bool avail; // led is available |
| 421 | bool on; // we think the led is currently on |
| 422 | }; |
| 423 | LedState capsLockLedState; |
| 424 | LedState numLockLedState; |
| 425 | LedState scrollLockLedState; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 426 | } mLocked; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 427 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 428 | void initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 429 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 430 | void configureParameters(); |
| 431 | void dumpParameters(String8& dump); |
| 432 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 433 | bool isKeyboardOrGamepadKey(int32_t scanCode); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 434 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 435 | void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode, |
| 436 | uint32_t policyFlags); |
| 437 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 438 | ssize_t findKeyDownLocked(int32_t scanCode); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 439 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 440 | void resetLedStateLocked(); |
| 441 | void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 442 | void updateLedStateLocked(bool reset); |
| 443 | void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led, |
| 444 | int32_t modifier, bool reset); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 445 | }; |
| 446 | |
| 447 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 448 | class CursorInputMapper : public InputMapper { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 449 | public: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 450 | CursorInputMapper(InputDevice* device); |
| 451 | virtual ~CursorInputMapper(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 452 | |
| 453 | virtual uint32_t getSources(); |
| 454 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 455 | virtual void dump(String8& dump); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 456 | virtual void configure(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 457 | virtual void reset(); |
| 458 | virtual void process(const RawEvent* rawEvent); |
| 459 | |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 460 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 461 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 462 | private: |
| 463 | // Amount that trackball needs to move in order to generate a key event. |
| 464 | static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; |
| 465 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 466 | Mutex mLock; |
| 467 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 468 | // Immutable configuration parameters. |
| 469 | struct Parameters { |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 470 | enum Mode { |
| 471 | MODE_POINTER, |
| 472 | MODE_NAVIGATION, |
| 473 | }; |
| 474 | |
| 475 | Mode mode; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 476 | int32_t associatedDisplayId; |
| 477 | bool orientationAware; |
| 478 | } mParameters; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 479 | |
| 480 | struct Accumulator { |
| 481 | enum { |
| 482 | FIELD_BTN_MOUSE = 1, |
| 483 | FIELD_REL_X = 2, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 484 | FIELD_REL_Y = 4, |
| 485 | FIELD_REL_WHEEL = 8, |
| 486 | FIELD_REL_HWHEEL = 16, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | uint32_t fields; |
| 490 | |
| 491 | bool btnMouse; |
| 492 | int32_t relX; |
| 493 | int32_t relY; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 494 | int32_t relWheel; |
| 495 | int32_t relHWheel; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 496 | |
| 497 | inline void clear() { |
| 498 | fields = 0; |
| 499 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 500 | } mAccumulator; |
| 501 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 502 | int32_t mSources; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 503 | float mXScale; |
| 504 | float mYScale; |
| 505 | float mXPrecision; |
| 506 | float mYPrecision; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 507 | |
| 508 | bool mHaveVWheel; |
| 509 | bool mHaveHWheel; |
| 510 | float mVWheelScale; |
| 511 | float mHWheelScale; |
| 512 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 513 | sp<PointerControllerInterface> mPointerController; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 514 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 515 | struct LockedState { |
| 516 | bool down; |
| 517 | nsecs_t downTime; |
| 518 | } mLocked; |
| 519 | |
| 520 | void initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 521 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 522 | void configureParameters(); |
| 523 | void dumpParameters(String8& dump); |
| 524 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 525 | void sync(nsecs_t when); |
| 526 | }; |
| 527 | |
| 528 | |
| 529 | class TouchInputMapper : public InputMapper { |
| 530 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 531 | TouchInputMapper(InputDevice* device); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 532 | virtual ~TouchInputMapper(); |
| 533 | |
| 534 | virtual uint32_t getSources(); |
| 535 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 536 | virtual void dump(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 537 | virtual void configure(); |
| 538 | virtual void reset(); |
| 539 | |
| 540 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 541 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 542 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 543 | const int32_t* keyCodes, uint8_t* outFlags); |
| 544 | |
| 545 | protected: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 546 | Mutex mLock; |
| 547 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 548 | struct VirtualKey { |
| 549 | int32_t keyCode; |
| 550 | int32_t scanCode; |
| 551 | uint32_t flags; |
| 552 | |
| 553 | // computed hit box, specified in touch screen coords based on known display size |
| 554 | int32_t hitLeft; |
| 555 | int32_t hitTop; |
| 556 | int32_t hitRight; |
| 557 | int32_t hitBottom; |
| 558 | |
| 559 | inline bool isHit(int32_t x, int32_t y) const { |
| 560 | return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; |
| 561 | } |
| 562 | }; |
| 563 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 564 | // Raw data for a single pointer. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 565 | struct PointerData { |
| 566 | uint32_t id; |
| 567 | int32_t x; |
| 568 | int32_t y; |
| 569 | int32_t pressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 570 | int32_t touchMajor; |
| 571 | int32_t touchMinor; |
| 572 | int32_t toolMajor; |
| 573 | int32_t toolMinor; |
| 574 | int32_t orientation; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 575 | |
| 576 | inline bool operator== (const PointerData& other) const { |
| 577 | return id == other.id |
| 578 | && x == other.x |
| 579 | && y == other.y |
| 580 | && pressure == other.pressure |
| 581 | && touchMajor == other.touchMajor |
| 582 | && touchMinor == other.touchMinor |
| 583 | && toolMajor == other.toolMajor |
| 584 | && toolMinor == other.toolMinor |
| 585 | && orientation == other.orientation; |
| 586 | } |
| 587 | inline bool operator!= (const PointerData& other) const { |
| 588 | return !(*this == other); |
| 589 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 590 | }; |
| 591 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 592 | // 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] | 593 | struct TouchData { |
| 594 | uint32_t pointerCount; |
| 595 | PointerData pointers[MAX_POINTERS]; |
| 596 | BitSet32 idBits; |
| 597 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
| 598 | |
| 599 | void copyFrom(const TouchData& other) { |
| 600 | pointerCount = other.pointerCount; |
| 601 | idBits = other.idBits; |
| 602 | |
| 603 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 604 | pointers[i] = other.pointers[i]; |
Jeff Brown | 9e2ad36 | 2010-07-30 19:20:11 -0700 | [diff] [blame] | 605 | |
| 606 | int id = pointers[i].id; |
| 607 | idToIndex[id] = other.idToIndex[id]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | inline void clear() { |
| 612 | pointerCount = 0; |
| 613 | idBits.clear(); |
| 614 | } |
| 615 | }; |
| 616 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 617 | // Input sources supported by the device. |
| 618 | int32_t mSources; |
| 619 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 620 | // Immutable configuration parameters. |
| 621 | struct Parameters { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 622 | enum DeviceType { |
| 623 | DEVICE_TYPE_TOUCH_SCREEN, |
| 624 | DEVICE_TYPE_TOUCH_PAD, |
| 625 | }; |
| 626 | |
| 627 | DeviceType deviceType; |
| 628 | int32_t associatedDisplayId; |
| 629 | bool orientationAware; |
| 630 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 631 | bool useBadTouchFilter; |
| 632 | bool useJumpyTouchFilter; |
| 633 | bool useAveragingTouchFilter; |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 634 | nsecs_t virtualKeyQuietTime; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 635 | } mParameters; |
| 636 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 637 | // Immutable calibration parameters in parsed form. |
| 638 | struct Calibration { |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 639 | // Position |
| 640 | bool haveXOrigin; |
| 641 | int32_t xOrigin; |
| 642 | bool haveYOrigin; |
| 643 | int32_t yOrigin; |
| 644 | bool haveXScale; |
| 645 | float xScale; |
| 646 | bool haveYScale; |
| 647 | float yScale; |
| 648 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 649 | // Touch Size |
| 650 | enum TouchSizeCalibration { |
| 651 | TOUCH_SIZE_CALIBRATION_DEFAULT, |
| 652 | TOUCH_SIZE_CALIBRATION_NONE, |
| 653 | TOUCH_SIZE_CALIBRATION_GEOMETRIC, |
| 654 | TOUCH_SIZE_CALIBRATION_PRESSURE, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 655 | }; |
| 656 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 657 | TouchSizeCalibration touchSizeCalibration; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 658 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 659 | // Tool Size |
| 660 | enum ToolSizeCalibration { |
| 661 | TOOL_SIZE_CALIBRATION_DEFAULT, |
| 662 | TOOL_SIZE_CALIBRATION_NONE, |
| 663 | TOOL_SIZE_CALIBRATION_GEOMETRIC, |
| 664 | TOOL_SIZE_CALIBRATION_LINEAR, |
| 665 | TOOL_SIZE_CALIBRATION_AREA, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 666 | }; |
| 667 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 668 | ToolSizeCalibration toolSizeCalibration; |
| 669 | bool haveToolSizeLinearScale; |
| 670 | float toolSizeLinearScale; |
| 671 | bool haveToolSizeLinearBias; |
| 672 | float toolSizeLinearBias; |
| 673 | bool haveToolSizeAreaScale; |
| 674 | float toolSizeAreaScale; |
| 675 | bool haveToolSizeAreaBias; |
| 676 | float toolSizeAreaBias; |
| 677 | bool haveToolSizeIsSummed; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 678 | bool toolSizeIsSummed; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 679 | |
| 680 | // Pressure |
| 681 | enum PressureCalibration { |
| 682 | PRESSURE_CALIBRATION_DEFAULT, |
| 683 | PRESSURE_CALIBRATION_NONE, |
| 684 | PRESSURE_CALIBRATION_PHYSICAL, |
| 685 | PRESSURE_CALIBRATION_AMPLITUDE, |
| 686 | }; |
| 687 | enum PressureSource { |
| 688 | PRESSURE_SOURCE_DEFAULT, |
| 689 | PRESSURE_SOURCE_PRESSURE, |
| 690 | PRESSURE_SOURCE_TOUCH, |
| 691 | }; |
| 692 | |
| 693 | PressureCalibration pressureCalibration; |
| 694 | PressureSource pressureSource; |
| 695 | bool havePressureScale; |
| 696 | float pressureScale; |
| 697 | |
| 698 | // Size |
| 699 | enum SizeCalibration { |
| 700 | SIZE_CALIBRATION_DEFAULT, |
| 701 | SIZE_CALIBRATION_NONE, |
| 702 | SIZE_CALIBRATION_NORMALIZED, |
| 703 | }; |
| 704 | |
| 705 | SizeCalibration sizeCalibration; |
| 706 | |
| 707 | // Orientation |
| 708 | enum OrientationCalibration { |
| 709 | ORIENTATION_CALIBRATION_DEFAULT, |
| 710 | ORIENTATION_CALIBRATION_NONE, |
| 711 | ORIENTATION_CALIBRATION_INTERPOLATED, |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 712 | ORIENTATION_CALIBRATION_VECTOR, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 713 | }; |
| 714 | |
| 715 | OrientationCalibration orientationCalibration; |
| 716 | } mCalibration; |
| 717 | |
| 718 | // Raw axis information from the driver. |
| 719 | struct RawAxes { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 720 | RawAbsoluteAxisInfo x; |
| 721 | RawAbsoluteAxisInfo y; |
| 722 | RawAbsoluteAxisInfo pressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 723 | RawAbsoluteAxisInfo touchMajor; |
| 724 | RawAbsoluteAxisInfo touchMinor; |
| 725 | RawAbsoluteAxisInfo toolMajor; |
| 726 | RawAbsoluteAxisInfo toolMinor; |
| 727 | RawAbsoluteAxisInfo orientation; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 728 | } mRawAxes; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 729 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 730 | // Current and previous touch sample data. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 731 | TouchData mCurrentTouch; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 732 | TouchData mLastTouch; |
| 733 | |
| 734 | // The time the primary pointer last went down. |
| 735 | nsecs_t mDownTime; |
| 736 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 737 | struct LockedState { |
| 738 | Vector<VirtualKey> virtualKeys; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 739 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 740 | // The surface orientation and width and height set by configureSurfaceLocked(). |
| 741 | int32_t surfaceOrientation; |
| 742 | int32_t surfaceWidth, surfaceHeight; |
| 743 | |
| 744 | // Translation and scaling factors, orientation-independent. |
| 745 | int32_t xOrigin; |
| 746 | float xScale; |
| 747 | float xPrecision; |
| 748 | |
| 749 | int32_t yOrigin; |
| 750 | float yScale; |
| 751 | float yPrecision; |
| 752 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 753 | float geometricScale; |
| 754 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 755 | float toolSizeLinearScale; |
| 756 | float toolSizeLinearBias; |
| 757 | float toolSizeAreaScale; |
| 758 | float toolSizeAreaBias; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 759 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 760 | float pressureScale; |
| 761 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 762 | float sizeScale; |
| 763 | |
| 764 | float orientationScale; |
| 765 | |
| 766 | // Oriented motion ranges for input device info. |
| 767 | struct OrientedRanges { |
| 768 | InputDeviceInfo::MotionRange x; |
| 769 | InputDeviceInfo::MotionRange y; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 770 | |
| 771 | bool havePressure; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 772 | InputDeviceInfo::MotionRange pressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 773 | |
| 774 | bool haveSize; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 775 | InputDeviceInfo::MotionRange size; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 776 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 777 | bool haveTouchSize; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 778 | InputDeviceInfo::MotionRange touchMajor; |
| 779 | InputDeviceInfo::MotionRange touchMinor; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 780 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 781 | bool haveToolSize; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 782 | InputDeviceInfo::MotionRange toolMajor; |
| 783 | InputDeviceInfo::MotionRange toolMinor; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 784 | |
| 785 | bool haveOrientation; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 786 | InputDeviceInfo::MotionRange orientation; |
| 787 | } orientedRanges; |
| 788 | |
| 789 | // Oriented dimensions and precision. |
| 790 | float orientedSurfaceWidth, orientedSurfaceHeight; |
| 791 | float orientedXPrecision, orientedYPrecision; |
| 792 | |
| 793 | struct CurrentVirtualKeyState { |
| 794 | bool down; |
| 795 | nsecs_t downTime; |
| 796 | int32_t keyCode; |
| 797 | int32_t scanCode; |
| 798 | } currentVirtualKey; |
| 799 | } mLocked; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 800 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 801 | virtual void configureParameters(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 802 | virtual void dumpParameters(String8& dump); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 803 | virtual void configureRawAxes(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 804 | virtual void dumpRawAxes(String8& dump); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 805 | virtual bool configureSurfaceLocked(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 806 | virtual void dumpSurfaceLocked(String8& dump); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 807 | virtual void configureVirtualKeysLocked(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 808 | virtual void dumpVirtualKeysLocked(String8& dump); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 809 | virtual void parseCalibration(); |
| 810 | virtual void resolveCalibration(); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 811 | virtual void dumpCalibration(String8& dump); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 812 | |
| 813 | enum TouchResult { |
| 814 | // Dispatch the touch normally. |
| 815 | DISPATCH_TOUCH, |
| 816 | // Do not dispatch the touch, but keep tracking the current stroke. |
| 817 | SKIP_TOUCH, |
| 818 | // Do not dispatch the touch, and drop all information associated with the current stoke |
| 819 | // so the next movement will appear as a new down. |
| 820 | DROP_STROKE |
| 821 | }; |
| 822 | |
| 823 | void syncTouch(nsecs_t when, bool havePointerIds); |
| 824 | |
| 825 | private: |
| 826 | /* Maximum number of historical samples to average. */ |
| 827 | static const uint32_t AVERAGING_HISTORY_SIZE = 5; |
| 828 | |
| 829 | /* Slop distance for jumpy pointer detection. |
| 830 | * The vertical range of the screen divided by this is our epsilon value. */ |
| 831 | static const uint32_t JUMPY_EPSILON_DIVISOR = 212; |
| 832 | |
| 833 | /* Number of jumpy points to drop for touchscreens that need it. */ |
| 834 | static const uint32_t JUMPY_TRANSITION_DROPS = 3; |
| 835 | static const uint32_t JUMPY_DROP_LIMIT = 3; |
| 836 | |
| 837 | /* Maximum squared distance for averaging. |
| 838 | * If moving farther than this, turn of averaging to avoid lag in response. */ |
| 839 | static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75; |
| 840 | |
| 841 | struct AveragingTouchFilterState { |
| 842 | // Individual history tracks are stored by pointer id |
| 843 | uint32_t historyStart[MAX_POINTERS]; |
| 844 | uint32_t historyEnd[MAX_POINTERS]; |
| 845 | struct { |
| 846 | struct { |
| 847 | int32_t x; |
| 848 | int32_t y; |
| 849 | int32_t pressure; |
| 850 | } pointers[MAX_POINTERS]; |
| 851 | } historyData[AVERAGING_HISTORY_SIZE]; |
| 852 | } mAveragingTouchFilter; |
| 853 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 854 | struct JumpyTouchFilterState { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 855 | uint32_t jumpyPointsDropped; |
| 856 | } mJumpyTouchFilter; |
| 857 | |
| 858 | struct PointerDistanceHeapElement { |
| 859 | uint32_t currentPointerIndex : 8; |
| 860 | uint32_t lastPointerIndex : 8; |
| 861 | uint64_t distance : 48; // squared distance |
| 862 | }; |
| 863 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 864 | void initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 865 | |
| 866 | TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags); |
| 867 | void dispatchTouches(nsecs_t when, uint32_t policyFlags); |
| 868 | void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 869 | BitSet32 idBits, uint32_t changedId, uint32_t pointerCount, |
| 870 | int32_t motionEventAction); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 871 | void detectGestures(nsecs_t when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 872 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 873 | bool isPointInsideSurfaceLocked(int32_t x, int32_t y); |
| 874 | const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 875 | |
| 876 | bool applyBadTouchFilter(); |
| 877 | bool applyJumpyTouchFilter(); |
| 878 | void applyAveragingTouchFilter(); |
| 879 | void calculatePointerIds(); |
| 880 | }; |
| 881 | |
| 882 | |
| 883 | class SingleTouchInputMapper : public TouchInputMapper { |
| 884 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 885 | SingleTouchInputMapper(InputDevice* device); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 886 | virtual ~SingleTouchInputMapper(); |
| 887 | |
| 888 | virtual void reset(); |
| 889 | virtual void process(const RawEvent* rawEvent); |
| 890 | |
| 891 | protected: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 892 | virtual void configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 893 | |
| 894 | private: |
| 895 | struct Accumulator { |
| 896 | enum { |
| 897 | FIELD_BTN_TOUCH = 1, |
| 898 | FIELD_ABS_X = 2, |
| 899 | FIELD_ABS_Y = 4, |
| 900 | FIELD_ABS_PRESSURE = 8, |
| 901 | FIELD_ABS_TOOL_WIDTH = 16 |
| 902 | }; |
| 903 | |
| 904 | uint32_t fields; |
| 905 | |
| 906 | bool btnTouch; |
| 907 | int32_t absX; |
| 908 | int32_t absY; |
| 909 | int32_t absPressure; |
| 910 | int32_t absToolWidth; |
| 911 | |
| 912 | inline void clear() { |
| 913 | fields = 0; |
| 914 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 915 | } mAccumulator; |
| 916 | |
| 917 | bool mDown; |
| 918 | int32_t mX; |
| 919 | int32_t mY; |
| 920 | int32_t mPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 921 | int32_t mToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 922 | |
| 923 | void initialize(); |
| 924 | |
| 925 | void sync(nsecs_t when); |
| 926 | }; |
| 927 | |
| 928 | |
| 929 | class MultiTouchInputMapper : public TouchInputMapper { |
| 930 | public: |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 931 | MultiTouchInputMapper(InputDevice* device); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 932 | virtual ~MultiTouchInputMapper(); |
| 933 | |
| 934 | virtual void reset(); |
| 935 | virtual void process(const RawEvent* rawEvent); |
| 936 | |
| 937 | protected: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 938 | virtual void configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 939 | |
| 940 | private: |
| 941 | struct Accumulator { |
| 942 | enum { |
| 943 | FIELD_ABS_MT_POSITION_X = 1, |
| 944 | FIELD_ABS_MT_POSITION_Y = 2, |
| 945 | FIELD_ABS_MT_TOUCH_MAJOR = 4, |
| 946 | FIELD_ABS_MT_TOUCH_MINOR = 8, |
| 947 | FIELD_ABS_MT_WIDTH_MAJOR = 16, |
| 948 | FIELD_ABS_MT_WIDTH_MINOR = 32, |
| 949 | FIELD_ABS_MT_ORIENTATION = 64, |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 950 | FIELD_ABS_MT_TRACKING_ID = 128, |
| 951 | FIELD_ABS_MT_PRESSURE = 256, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 952 | }; |
| 953 | |
| 954 | uint32_t pointerCount; |
| 955 | struct Pointer { |
| 956 | uint32_t fields; |
| 957 | |
| 958 | int32_t absMTPositionX; |
| 959 | int32_t absMTPositionY; |
| 960 | int32_t absMTTouchMajor; |
| 961 | int32_t absMTTouchMinor; |
| 962 | int32_t absMTWidthMajor; |
| 963 | int32_t absMTWidthMinor; |
| 964 | int32_t absMTOrientation; |
| 965 | int32_t absMTTrackingId; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 966 | int32_t absMTPressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 967 | |
| 968 | inline void clear() { |
| 969 | fields = 0; |
| 970 | } |
| 971 | } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks |
| 972 | |
| 973 | inline void clear() { |
| 974 | pointerCount = 0; |
| 975 | pointers[0].clear(); |
| 976 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 977 | } mAccumulator; |
| 978 | |
| 979 | void initialize(); |
| 980 | |
| 981 | void sync(nsecs_t when); |
| 982 | }; |
| 983 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 984 | |
| 985 | class JoystickInputMapper : public InputMapper { |
| 986 | public: |
| 987 | JoystickInputMapper(InputDevice* device); |
| 988 | virtual ~JoystickInputMapper(); |
| 989 | |
| 990 | virtual uint32_t getSources(); |
| 991 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
| 992 | virtual void dump(String8& dump); |
| 993 | virtual void configure(); |
| 994 | virtual void reset(); |
| 995 | virtual void process(const RawEvent* rawEvent); |
| 996 | |
| 997 | private: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 998 | struct Axis { |
| 999 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1000 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1001 | int32_t axis; // axis id |
| 1002 | bool explicitlyMapped; // true if the axis was explicitly assigned an axis id |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1003 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1004 | float scale; // scale factor from raw to normalized values |
| 1005 | float offset; // offset to add after scaling for normalization |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1006 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1007 | float min; // normalized inclusive minimum |
| 1008 | float max; // normalized inclusive maximum |
| 1009 | float flat; // normalized flat region size |
| 1010 | float fuzz; // normalized error tolerance |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1011 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1012 | float oldValue; // previous value |
| 1013 | float newValue; // most recent value |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1014 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1015 | float filter; // filter out small variations of this size |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1016 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1017 | void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, |
| 1018 | int32_t axis, bool explicitlyMapped, float scale, float offset, |
| 1019 | float min, float max, float flat, float fuzz) { |
| 1020 | this->rawAxisInfo = rawAxisInfo; |
| 1021 | this->axis = axis; |
| 1022 | this->explicitlyMapped = explicitlyMapped; |
| 1023 | this->scale = scale; |
| 1024 | this->offset = offset; |
| 1025 | this->min = min; |
| 1026 | this->max = max; |
| 1027 | this->flat = flat; |
| 1028 | this->fuzz = fuzz; |
| 1029 | this->filter = 0; |
| 1030 | this->oldValue = 0; |
| 1031 | this->newValue = 0; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1032 | } |
| 1033 | }; |
| 1034 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1035 | // Axes indexed by raw ABS_* axis index. |
| 1036 | KeyedVector<int32_t, Axis> mAxes; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1037 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1038 | void sync(nsecs_t when, bool force); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1039 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1040 | bool haveAxis(int32_t axis); |
| 1041 | void pruneAxes(bool ignoreExplicitlyMappedAxes); |
| 1042 | bool haveAxesChangedSignificantly(); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1043 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame^] | 1044 | static bool isCenteredAxis(int32_t axis); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1045 | }; |
| 1046 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1047 | } // namespace android |
| 1048 | |
| 1049 | #endif // _UI_INPUT_READER_H |