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