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