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