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