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