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