InputReader.h revision 9626b14a283ef82d16636cf5fb5ba8bb4d30381e
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 amount of time to disable virtual keys after the screen is touched 82 * in order to filter out accidental virtual key presses due to swiping gestures 83 * or taps near the edge of the display. May be 0 to disable the feature. 84 */ 85 virtual nsecs_t getVirtualKeyQuietTime() = 0; 86 87 /* Gets the excluded device names for the platform. */ 88 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0; 89 90 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */ 91 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0; 92}; 93 94 95/* Processes raw input events and sends cooked event data to an input dispatcher. */ 96class InputReaderInterface : public virtual RefBase { 97protected: 98 InputReaderInterface() { } 99 virtual ~InputReaderInterface() { } 100 101public: 102 /* Dumps the state of the input reader. 103 * 104 * This method may be called on any thread (usually by the input manager). */ 105 virtual void dump(String8& dump) = 0; 106 107 /* Runs a single iteration of the processing loop. 108 * Nominally reads and processes one incoming message from the EventHub. 109 * 110 * This method should be called on the input reader thread. 111 */ 112 virtual void loopOnce() = 0; 113 114 /* Gets the current input device configuration. 115 * 116 * This method may be called on any thread (usually by the input manager). 117 */ 118 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0; 119 120 /* Gets information about the specified input device. 121 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there 122 * was no such device. 123 * 124 * This method may be called on any thread (usually by the input manager). 125 */ 126 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0; 127 128 /* Gets the list of all registered device ids. */ 129 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0; 130 131 /* Query current input state. */ 132 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, 133 int32_t scanCode) = 0; 134 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, 135 int32_t keyCode) = 0; 136 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, 137 int32_t sw) = 0; 138 139 /* Determine whether physical keys exist for the given framework-domain key codes. */ 140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, 141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; 142}; 143 144 145/* Internal interface used by individual input devices to access global input device state 146 * and parameters maintained by the input reader. 147 */ 148class InputReaderContext { 149public: 150 InputReaderContext() { } 151 virtual ~InputReaderContext() { } 152 153 virtual void updateGlobalMetaState() = 0; 154 virtual int32_t getGlobalMetaState() = 0; 155 156 virtual void disableVirtualKeysUntil(nsecs_t time) = 0; 157 virtual bool shouldDropVirtualKey(nsecs_t now, 158 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0; 159 160 virtual void fadePointer() = 0; 161 162 virtual InputReaderPolicyInterface* getPolicy() = 0; 163 virtual InputDispatcherInterface* getDispatcher() = 0; 164 virtual EventHubInterface* getEventHub() = 0; 165}; 166 167 168/* The input reader reads raw event data from the event hub and processes it into input events 169 * that it sends to the input dispatcher. Some functions of the input reader, such as early 170 * event filtering in low power states, are controlled by a separate policy object. 171 * 172 * IMPORTANT INVARIANT: 173 * Because the policy and dispatcher can potentially block or cause re-entrance into 174 * the input reader, the input reader never calls into other components while holding 175 * an exclusive internal lock whenever re-entrance can happen. 176 */ 177class InputReader : public InputReaderInterface, protected InputReaderContext { 178public: 179 InputReader(const sp<EventHubInterface>& eventHub, 180 const sp<InputReaderPolicyInterface>& policy, 181 const sp<InputDispatcherInterface>& dispatcher); 182 virtual ~InputReader(); 183 184 virtual void dump(String8& dump); 185 186 virtual void loopOnce(); 187 188 virtual void getInputConfiguration(InputConfiguration* outConfiguration); 189 190 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo); 191 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds); 192 193 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, 194 int32_t scanCode); 195 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, 196 int32_t keyCode); 197 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, 198 int32_t sw); 199 200 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, 201 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags); 202 203protected: 204 // These methods are protected virtual so they can be overridden and instrumented 205 // by test cases. 206 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes); 207 208private: 209 sp<EventHubInterface> mEventHub; 210 sp<InputReaderPolicyInterface> mPolicy; 211 sp<InputDispatcherInterface> mDispatcher; 212 213 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); } 214 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); } 215 virtual EventHubInterface* getEventHub() { return mEventHub.get(); } 216 217 // This reader/writer lock guards the list of input devices. 218 // The writer lock must be held whenever the list of input devices is modified 219 // and then promptly released. 220 // The reader lock must be held whenever the list of input devices is traversed or an 221 // input device in the list is accessed. 222 // This lock only protects the registry and prevents inadvertent deletion of device objects 223 // that are in use. Individual devices are responsible for guarding their own internal state 224 // as needed for concurrent operation. 225 RWLock mDeviceRegistryLock; 226 KeyedVector<int32_t, InputDevice*> mDevices; 227 228 // low-level input event decoding and device management 229 void process(const RawEvent* rawEvent); 230 231 void addDevice(int32_t deviceId); 232 void removeDevice(int32_t deviceId); 233 void configureExcludedDevices(); 234 235 void consumeEvent(const RawEvent* rawEvent); 236 237 void handleConfigurationChanged(nsecs_t when); 238 239 // state management for all devices 240 Mutex mStateLock; 241 242 int32_t mGlobalMetaState; 243 virtual void updateGlobalMetaState(); 244 virtual int32_t getGlobalMetaState(); 245 246 virtual void fadePointer(); 247 248 InputConfiguration mInputConfiguration; 249 void updateInputConfiguration(); 250 251 nsecs_t mDisableVirtualKeysTimeout; 252 virtual void disableVirtualKeysUntil(nsecs_t time); 253 virtual bool shouldDropVirtualKey(nsecs_t now, 254 InputDevice* device, int32_t keyCode, int32_t scanCode); 255 256 // state queries 257 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); 258 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code, 259 GetStateFunc getStateFunc); 260 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, 261 const int32_t* keyCodes, uint8_t* outFlags); 262}; 263 264 265/* Reads raw events from the event hub and processes them, endlessly. */ 266class InputReaderThread : public Thread { 267public: 268 InputReaderThread(const sp<InputReaderInterface>& reader); 269 virtual ~InputReaderThread(); 270 271private: 272 sp<InputReaderInterface> mReader; 273 274 virtual bool threadLoop(); 275}; 276 277 278/* Represents the state of a single input device. */ 279class InputDevice { 280public: 281 InputDevice(InputReaderContext* context, int32_t id, const String8& name); 282 ~InputDevice(); 283 284 inline InputReaderContext* getContext() { return mContext; } 285 inline int32_t getId() { return mId; } 286 inline const String8& getName() { return mName; } 287 inline uint32_t getSources() { return mSources; } 288 289 inline bool isExternal() { return mIsExternal; } 290 inline void setExternal(bool external) { mIsExternal = external; } 291 292 inline bool isIgnored() { return mMappers.isEmpty(); } 293 294 void dump(String8& dump); 295 void addMapper(InputMapper* mapper); 296 void configure(); 297 void reset(); 298 void process(const RawEvent* rawEvent); 299 300 void getDeviceInfo(InputDeviceInfo* outDeviceInfo); 301 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 302 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 303 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 304 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 305 const int32_t* keyCodes, uint8_t* outFlags); 306 307 int32_t getMetaState(); 308 309 void fadePointer(); 310 311 inline const PropertyMap& getConfiguration() { 312 return mConfiguration; 313 } 314 315private: 316 InputReaderContext* mContext; 317 int32_t mId; 318 319 Vector<InputMapper*> mMappers; 320 321 String8 mName; 322 uint32_t mSources; 323 bool mIsExternal; 324 325 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); 326 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); 327 328 PropertyMap mConfiguration; 329}; 330 331 332/* An input mapper transforms raw input events into cooked event data. 333 * A single input device can have multiple associated input mappers in order to interpret 334 * different classes of events. 335 */ 336class InputMapper { 337public: 338 InputMapper(InputDevice* device); 339 virtual ~InputMapper(); 340 341 inline InputDevice* getDevice() { return mDevice; } 342 inline int32_t getDeviceId() { return mDevice->getId(); } 343 inline const String8 getDeviceName() { return mDevice->getName(); } 344 inline InputReaderContext* getContext() { return mContext; } 345 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); } 346 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); } 347 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } 348 349 virtual uint32_t getSources() = 0; 350 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 351 virtual void dump(String8& dump); 352 virtual void configure(); 353 virtual void reset(); 354 virtual void process(const RawEvent* rawEvent) = 0; 355 356 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 357 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 358 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 359 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 360 const int32_t* keyCodes, uint8_t* outFlags); 361 362 virtual int32_t getMetaState(); 363 364 virtual void fadePointer(); 365 366protected: 367 InputDevice* mDevice; 368 InputReaderContext* mContext; 369 370 static void dumpRawAbsoluteAxisInfo(String8& dump, 371 const RawAbsoluteAxisInfo& axis, const char* name); 372}; 373 374 375class SwitchInputMapper : public InputMapper { 376public: 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 385private: 386 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue); 387}; 388 389 390class KeyboardInputMapper : public InputMapper { 391public: 392 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType); 393 virtual ~KeyboardInputMapper(); 394 395 virtual uint32_t getSources(); 396 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 397 virtual void dump(String8& dump); 398 virtual void configure(); 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 409private: 410 Mutex mLock; 411 412 struct KeyDown { 413 int32_t keyCode; 414 int32_t scanCode; 415 }; 416 417 uint32_t mSources; 418 int32_t mKeyboardType; 419 420 // Immutable configuration parameters. 421 struct Parameters { 422 int32_t associatedDisplayId; 423 bool orientationAware; 424 } mParameters; 425 426 struct LockedState { 427 Vector<KeyDown> keyDowns; // keys that are down 428 int32_t metaState; 429 nsecs_t downTime; // time of most recent key down 430 431 struct LedState { 432 bool avail; // led is available 433 bool on; // we think the led is currently on 434 }; 435 LedState capsLockLedState; 436 LedState numLockLedState; 437 LedState scrollLockLedState; 438 } mLocked; 439 440 void initializeLocked(); 441 442 void configureParameters(); 443 void dumpParameters(String8& dump); 444 445 bool isKeyboardOrGamepadKey(int32_t scanCode); 446 447 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode, 448 uint32_t policyFlags); 449 450 ssize_t findKeyDownLocked(int32_t scanCode); 451 452 void resetLedStateLocked(); 453 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led); 454 void updateLedStateLocked(bool reset); 455 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led, 456 int32_t modifier, bool reset); 457}; 458 459 460class CursorInputMapper : public InputMapper { 461public: 462 CursorInputMapper(InputDevice* device); 463 virtual ~CursorInputMapper(); 464 465 virtual uint32_t getSources(); 466 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 467 virtual void dump(String8& dump); 468 virtual void configure(); 469 virtual void reset(); 470 virtual void process(const RawEvent* rawEvent); 471 472 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 473 474 virtual void fadePointer(); 475 476private: 477 // Amount that trackball needs to move in order to generate a key event. 478 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; 479 480 Mutex mLock; 481 482 // Immutable configuration parameters. 483 struct Parameters { 484 enum Mode { 485 MODE_POINTER, 486 MODE_NAVIGATION, 487 }; 488 489 Mode mode; 490 int32_t associatedDisplayId; 491 bool orientationAware; 492 } mParameters; 493 494 struct Accumulator { 495 enum { 496 FIELD_BTN_MOUSE = 1, 497 FIELD_REL_X = 2, 498 FIELD_REL_Y = 4, 499 FIELD_REL_WHEEL = 8, 500 FIELD_REL_HWHEEL = 16, 501 }; 502 503 uint32_t fields; 504 505 bool btnMouse; 506 int32_t relX; 507 int32_t relY; 508 int32_t relWheel; 509 int32_t relHWheel; 510 511 inline void clear() { 512 fields = 0; 513 } 514 } mAccumulator; 515 516 int32_t mSources; 517 float mXScale; 518 float mYScale; 519 float mXPrecision; 520 float mYPrecision; 521 522 bool mHaveVWheel; 523 bool mHaveHWheel; 524 float mVWheelScale; 525 float mHWheelScale; 526 527 sp<PointerControllerInterface> mPointerController; 528 529 struct LockedState { 530 bool down; 531 nsecs_t downTime; 532 } mLocked; 533 534 void initializeLocked(); 535 536 void configureParameters(); 537 void dumpParameters(String8& dump); 538 539 void sync(nsecs_t when); 540}; 541 542 543class TouchInputMapper : public InputMapper { 544public: 545 TouchInputMapper(InputDevice* device); 546 virtual ~TouchInputMapper(); 547 548 virtual uint32_t getSources(); 549 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 550 virtual void dump(String8& dump); 551 virtual void configure(); 552 virtual void reset(); 553 554 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 555 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 556 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 557 const int32_t* keyCodes, uint8_t* outFlags); 558 559protected: 560 Mutex mLock; 561 562 struct VirtualKey { 563 int32_t keyCode; 564 int32_t scanCode; 565 uint32_t flags; 566 567 // computed hit box, specified in touch screen coords based on known display size 568 int32_t hitLeft; 569 int32_t hitTop; 570 int32_t hitRight; 571 int32_t hitBottom; 572 573 inline bool isHit(int32_t x, int32_t y) const { 574 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; 575 } 576 }; 577 578 // Raw data for a single pointer. 579 struct PointerData { 580 uint32_t id; 581 int32_t x; 582 int32_t y; 583 int32_t pressure; 584 int32_t touchMajor; 585 int32_t touchMinor; 586 int32_t toolMajor; 587 int32_t toolMinor; 588 int32_t orientation; 589 590 inline bool operator== (const PointerData& other) const { 591 return id == other.id 592 && x == other.x 593 && y == other.y 594 && pressure == other.pressure 595 && touchMajor == other.touchMajor 596 && touchMinor == other.touchMinor 597 && toolMajor == other.toolMajor 598 && toolMinor == other.toolMinor 599 && orientation == other.orientation; 600 } 601 inline bool operator!= (const PointerData& other) const { 602 return !(*this == other); 603 } 604 }; 605 606 // Raw data for a collection of pointers including a pointer id mapping table. 607 struct TouchData { 608 uint32_t pointerCount; 609 PointerData pointers[MAX_POINTERS]; 610 BitSet32 idBits; 611 uint32_t idToIndex[MAX_POINTER_ID + 1]; 612 613 void copyFrom(const TouchData& other) { 614 pointerCount = other.pointerCount; 615 idBits = other.idBits; 616 617 for (uint32_t i = 0; i < pointerCount; i++) { 618 pointers[i] = other.pointers[i]; 619 620 int id = pointers[i].id; 621 idToIndex[id] = other.idToIndex[id]; 622 } 623 } 624 625 inline void clear() { 626 pointerCount = 0; 627 idBits.clear(); 628 } 629 }; 630 631 // Input sources supported by the device. 632 int32_t mSources; 633 634 // Immutable configuration parameters. 635 struct Parameters { 636 enum DeviceType { 637 DEVICE_TYPE_TOUCH_SCREEN, 638 DEVICE_TYPE_TOUCH_PAD, 639 }; 640 641 DeviceType deviceType; 642 int32_t associatedDisplayId; 643 bool orientationAware; 644 645 bool useBadTouchFilter; 646 bool useJumpyTouchFilter; 647 bool useAveragingTouchFilter; 648 nsecs_t virtualKeyQuietTime; 649 } mParameters; 650 651 // Immutable calibration parameters in parsed form. 652 struct Calibration { 653 // Touch Size 654 enum TouchSizeCalibration { 655 TOUCH_SIZE_CALIBRATION_DEFAULT, 656 TOUCH_SIZE_CALIBRATION_NONE, 657 TOUCH_SIZE_CALIBRATION_GEOMETRIC, 658 TOUCH_SIZE_CALIBRATION_PRESSURE, 659 }; 660 661 TouchSizeCalibration touchSizeCalibration; 662 663 // Tool Size 664 enum ToolSizeCalibration { 665 TOOL_SIZE_CALIBRATION_DEFAULT, 666 TOOL_SIZE_CALIBRATION_NONE, 667 TOOL_SIZE_CALIBRATION_GEOMETRIC, 668 TOOL_SIZE_CALIBRATION_LINEAR, 669 TOOL_SIZE_CALIBRATION_AREA, 670 }; 671 672 ToolSizeCalibration toolSizeCalibration; 673 bool haveToolSizeLinearScale; 674 float toolSizeLinearScale; 675 bool haveToolSizeLinearBias; 676 float toolSizeLinearBias; 677 bool haveToolSizeAreaScale; 678 float toolSizeAreaScale; 679 bool haveToolSizeAreaBias; 680 float toolSizeAreaBias; 681 bool haveToolSizeIsSummed; 682 bool toolSizeIsSummed; 683 684 // Pressure 685 enum PressureCalibration { 686 PRESSURE_CALIBRATION_DEFAULT, 687 PRESSURE_CALIBRATION_NONE, 688 PRESSURE_CALIBRATION_PHYSICAL, 689 PRESSURE_CALIBRATION_AMPLITUDE, 690 }; 691 enum PressureSource { 692 PRESSURE_SOURCE_DEFAULT, 693 PRESSURE_SOURCE_PRESSURE, 694 PRESSURE_SOURCE_TOUCH, 695 }; 696 697 PressureCalibration pressureCalibration; 698 PressureSource pressureSource; 699 bool havePressureScale; 700 float pressureScale; 701 702 // Size 703 enum SizeCalibration { 704 SIZE_CALIBRATION_DEFAULT, 705 SIZE_CALIBRATION_NONE, 706 SIZE_CALIBRATION_NORMALIZED, 707 }; 708 709 SizeCalibration sizeCalibration; 710 711 // Orientation 712 enum OrientationCalibration { 713 ORIENTATION_CALIBRATION_DEFAULT, 714 ORIENTATION_CALIBRATION_NONE, 715 ORIENTATION_CALIBRATION_INTERPOLATED, 716 ORIENTATION_CALIBRATION_VECTOR, 717 }; 718 719 OrientationCalibration orientationCalibration; 720 } mCalibration; 721 722 // Raw axis information from the driver. 723 struct RawAxes { 724 RawAbsoluteAxisInfo x; 725 RawAbsoluteAxisInfo y; 726 RawAbsoluteAxisInfo pressure; 727 RawAbsoluteAxisInfo touchMajor; 728 RawAbsoluteAxisInfo touchMinor; 729 RawAbsoluteAxisInfo toolMajor; 730 RawAbsoluteAxisInfo toolMinor; 731 RawAbsoluteAxisInfo orientation; 732 } mRawAxes; 733 734 // Current and previous touch sample data. 735 TouchData mCurrentTouch; 736 TouchData mLastTouch; 737 738 // The time the primary pointer last went down. 739 nsecs_t mDownTime; 740 741 struct LockedState { 742 Vector<VirtualKey> virtualKeys; 743 744 // The surface orientation and width and height set by configureSurfaceLocked(). 745 int32_t surfaceOrientation; 746 int32_t surfaceWidth, surfaceHeight; 747 748 // Translation and scaling factors, orientation-independent. 749 float xScale; 750 float xPrecision; 751 752 float yScale; 753 float yPrecision; 754 755 float geometricScale; 756 757 float toolSizeLinearScale; 758 float toolSizeLinearBias; 759 float toolSizeAreaScale; 760 float toolSizeAreaBias; 761 762 float pressureScale; 763 764 float sizeScale; 765 766 float orientationScale; 767 768 // Oriented motion ranges for input device info. 769 struct OrientedRanges { 770 InputDeviceInfo::MotionRange x; 771 InputDeviceInfo::MotionRange y; 772 773 bool havePressure; 774 InputDeviceInfo::MotionRange pressure; 775 776 bool haveSize; 777 InputDeviceInfo::MotionRange size; 778 779 bool haveTouchSize; 780 InputDeviceInfo::MotionRange touchMajor; 781 InputDeviceInfo::MotionRange touchMinor; 782 783 bool haveToolSize; 784 InputDeviceInfo::MotionRange toolMajor; 785 InputDeviceInfo::MotionRange toolMinor; 786 787 bool haveOrientation; 788 InputDeviceInfo::MotionRange orientation; 789 } orientedRanges; 790 791 // Oriented dimensions and precision. 792 float orientedSurfaceWidth, orientedSurfaceHeight; 793 float orientedXPrecision, orientedYPrecision; 794 795 struct CurrentVirtualKeyState { 796 bool down; 797 nsecs_t downTime; 798 int32_t keyCode; 799 int32_t scanCode; 800 } currentVirtualKey; 801 } mLocked; 802 803 virtual void configureParameters(); 804 virtual void dumpParameters(String8& dump); 805 virtual void configureRawAxes(); 806 virtual void dumpRawAxes(String8& dump); 807 virtual bool configureSurfaceLocked(); 808 virtual void dumpSurfaceLocked(String8& dump); 809 virtual void configureVirtualKeysLocked(); 810 virtual void dumpVirtualKeysLocked(String8& dump); 811 virtual void parseCalibration(); 812 virtual void resolveCalibration(); 813 virtual void dumpCalibration(String8& dump); 814 815 enum TouchResult { 816 // Dispatch the touch normally. 817 DISPATCH_TOUCH, 818 // Do not dispatch the touch, but keep tracking the current stroke. 819 SKIP_TOUCH, 820 // Do not dispatch the touch, and drop all information associated with the current stoke 821 // so the next movement will appear as a new down. 822 DROP_STROKE 823 }; 824 825 void syncTouch(nsecs_t when, bool havePointerIds); 826 827private: 828 /* Maximum number of historical samples to average. */ 829 static const uint32_t AVERAGING_HISTORY_SIZE = 5; 830 831 /* Slop distance for jumpy pointer detection. 832 * The vertical range of the screen divided by this is our epsilon value. */ 833 static const uint32_t JUMPY_EPSILON_DIVISOR = 212; 834 835 /* Number of jumpy points to drop for touchscreens that need it. */ 836 static const uint32_t JUMPY_TRANSITION_DROPS = 3; 837 static const uint32_t JUMPY_DROP_LIMIT = 3; 838 839 /* Maximum squared distance for averaging. 840 * If moving farther than this, turn of averaging to avoid lag in response. */ 841 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75; 842 843 struct AveragingTouchFilterState { 844 // Individual history tracks are stored by pointer id 845 uint32_t historyStart[MAX_POINTERS]; 846 uint32_t historyEnd[MAX_POINTERS]; 847 struct { 848 struct { 849 int32_t x; 850 int32_t y; 851 int32_t pressure; 852 } pointers[MAX_POINTERS]; 853 } historyData[AVERAGING_HISTORY_SIZE]; 854 } mAveragingTouchFilter; 855 856 struct JumpyTouchFilterState { 857 uint32_t jumpyPointsDropped; 858 } mJumpyTouchFilter; 859 860 struct PointerDistanceHeapElement { 861 uint32_t currentPointerIndex : 8; 862 uint32_t lastPointerIndex : 8; 863 uint64_t distance : 48; // squared distance 864 }; 865 866 void initializeLocked(); 867 868 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags); 869 void dispatchTouches(nsecs_t when, uint32_t policyFlags); 870 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch, 871 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount, 872 int32_t motionEventAction); 873 void detectGestures(nsecs_t when); 874 875 bool isPointInsideSurfaceLocked(int32_t x, int32_t y); 876 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y); 877 878 bool applyBadTouchFilter(); 879 bool applyJumpyTouchFilter(); 880 void applyAveragingTouchFilter(); 881 void calculatePointerIds(); 882}; 883 884 885class SingleTouchInputMapper : public TouchInputMapper { 886public: 887 SingleTouchInputMapper(InputDevice* device); 888 virtual ~SingleTouchInputMapper(); 889 890 virtual void reset(); 891 virtual void process(const RawEvent* rawEvent); 892 893protected: 894 virtual void configureRawAxes(); 895 896private: 897 struct Accumulator { 898 enum { 899 FIELD_BTN_TOUCH = 1, 900 FIELD_ABS_X = 2, 901 FIELD_ABS_Y = 4, 902 FIELD_ABS_PRESSURE = 8, 903 FIELD_ABS_TOOL_WIDTH = 16 904 }; 905 906 uint32_t fields; 907 908 bool btnTouch; 909 int32_t absX; 910 int32_t absY; 911 int32_t absPressure; 912 int32_t absToolWidth; 913 914 inline void clear() { 915 fields = 0; 916 } 917 } mAccumulator; 918 919 bool mDown; 920 int32_t mX; 921 int32_t mY; 922 int32_t mPressure; 923 int32_t mToolWidth; 924 925 void initialize(); 926 927 void sync(nsecs_t when); 928}; 929 930 931class MultiTouchInputMapper : public TouchInputMapper { 932public: 933 MultiTouchInputMapper(InputDevice* device); 934 virtual ~MultiTouchInputMapper(); 935 936 virtual void reset(); 937 virtual void process(const RawEvent* rawEvent); 938 939protected: 940 virtual void configureRawAxes(); 941 942private: 943 struct Accumulator { 944 enum { 945 FIELD_ABS_MT_POSITION_X = 1, 946 FIELD_ABS_MT_POSITION_Y = 2, 947 FIELD_ABS_MT_TOUCH_MAJOR = 4, 948 FIELD_ABS_MT_TOUCH_MINOR = 8, 949 FIELD_ABS_MT_WIDTH_MAJOR = 16, 950 FIELD_ABS_MT_WIDTH_MINOR = 32, 951 FIELD_ABS_MT_ORIENTATION = 64, 952 FIELD_ABS_MT_TRACKING_ID = 128, 953 FIELD_ABS_MT_PRESSURE = 256, 954 }; 955 956 uint32_t pointerCount; 957 struct Pointer { 958 uint32_t fields; 959 960 int32_t absMTPositionX; 961 int32_t absMTPositionY; 962 int32_t absMTTouchMajor; 963 int32_t absMTTouchMinor; 964 int32_t absMTWidthMajor; 965 int32_t absMTWidthMinor; 966 int32_t absMTOrientation; 967 int32_t absMTTrackingId; 968 int32_t absMTPressure; 969 970 inline void clear() { 971 fields = 0; 972 } 973 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks 974 975 inline void clear() { 976 pointerCount = 0; 977 pointers[0].clear(); 978 } 979 } mAccumulator; 980 981 void initialize(); 982 983 void sync(nsecs_t when); 984}; 985 986 987class JoystickInputMapper : public InputMapper { 988public: 989 JoystickInputMapper(InputDevice* device); 990 virtual ~JoystickInputMapper(); 991 992 virtual uint32_t getSources(); 993 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 994 virtual void dump(String8& dump); 995 virtual void configure(); 996 virtual void reset(); 997 virtual void process(const RawEvent* rawEvent); 998 999private: 1000 struct Axis { 1001 RawAbsoluteAxisInfo rawAxisInfo; 1002 AxisInfo axisInfo; 1003 1004 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id 1005 1006 float scale; // scale factor from raw to normalized values 1007 float offset; // offset to add after scaling for normalization 1008 float highScale; // scale factor from raw to normalized values of high split 1009 float highOffset; // offset to add after scaling for normalization of high split 1010 1011 float min; // normalized inclusive minimum 1012 float max; // normalized inclusive maximum 1013 float flat; // normalized flat region size 1014 float fuzz; // normalized error tolerance 1015 1016 float filter; // filter out small variations of this size 1017 float currentValue; // current value 1018 float newValue; // most recent value 1019 float highCurrentValue; // current value of high split 1020 float highNewValue; // most recent value of high split 1021 1022 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo, 1023 bool explicitlyMapped, float scale, float offset, 1024 float highScale, float highOffset, 1025 float min, float max, float flat, float fuzz) { 1026 this->rawAxisInfo = rawAxisInfo; 1027 this->axisInfo = axisInfo; 1028 this->explicitlyMapped = explicitlyMapped; 1029 this->scale = scale; 1030 this->offset = offset; 1031 this->highScale = highScale; 1032 this->highOffset = highOffset; 1033 this->min = min; 1034 this->max = max; 1035 this->flat = flat; 1036 this->fuzz = fuzz; 1037 this->filter = 0; 1038 resetValue(); 1039 } 1040 1041 void resetValue() { 1042 this->currentValue = 0; 1043 this->newValue = 0; 1044 this->highCurrentValue = 0; 1045 this->highNewValue = 0; 1046 } 1047 }; 1048 1049 // Axes indexed by raw ABS_* axis index. 1050 KeyedVector<int32_t, Axis> mAxes; 1051 1052 void sync(nsecs_t when, bool force); 1053 1054 bool haveAxis(int32_t axisId); 1055 void pruneAxes(bool ignoreExplicitlyMappedAxes); 1056 bool filterAxes(bool force); 1057 1058 static bool hasValueChangedSignificantly(float filter, 1059 float newValue, float currentValue, float min, float max); 1060 static bool hasMovedNearerToValueWithinFilteredRange(float filter, 1061 float newValue, float currentValue, float thresholdValue); 1062 1063 static bool isCenteredAxis(int32_t axis); 1064}; 1065 1066} // namespace android 1067 1068#endif // _UI_INPUT_READER_H 1069