InputReader.h revision cb1404e45639d20439d7700b06d57ca1a1aad1fa
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 static void dumpRawAbsoluteAxisInfo(String8& dump, 344 const RawAbsoluteAxisInfo& axis, const char* name); 345}; 346 347 348class SwitchInputMapper : public InputMapper { 349public: 350 SwitchInputMapper(InputDevice* device); 351 virtual ~SwitchInputMapper(); 352 353 virtual uint32_t getSources(); 354 virtual void process(const RawEvent* rawEvent); 355 356 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 357 358private: 359 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue); 360}; 361 362 363class KeyboardInputMapper : public InputMapper { 364public: 365 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType); 366 virtual ~KeyboardInputMapper(); 367 368 virtual uint32_t getSources(); 369 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 370 virtual void dump(String8& dump); 371 virtual void configure(); 372 virtual void reset(); 373 virtual void process(const RawEvent* rawEvent); 374 375 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 376 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 377 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 378 const int32_t* keyCodes, uint8_t* outFlags); 379 380 virtual int32_t getMetaState(); 381 382private: 383 Mutex mLock; 384 385 struct KeyDown { 386 int32_t keyCode; 387 int32_t scanCode; 388 }; 389 390 uint32_t mSources; 391 int32_t mKeyboardType; 392 393 // Immutable configuration parameters. 394 struct Parameters { 395 int32_t associatedDisplayId; 396 bool orientationAware; 397 } mParameters; 398 399 struct LockedState { 400 Vector<KeyDown> keyDowns; // keys that are down 401 int32_t metaState; 402 nsecs_t downTime; // time of most recent key down 403 404 struct LedState { 405 bool avail; // led is available 406 bool on; // we think the led is currently on 407 }; 408 LedState capsLockLedState; 409 LedState numLockLedState; 410 LedState scrollLockLedState; 411 } mLocked; 412 413 void initializeLocked(); 414 415 void configureParameters(); 416 void dumpParameters(String8& dump); 417 418 bool isKeyboardOrGamepadKey(int32_t scanCode); 419 420 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode, 421 uint32_t policyFlags); 422 423 ssize_t findKeyDownLocked(int32_t scanCode); 424 425 void resetLedStateLocked(); 426 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led); 427 void updateLedStateLocked(bool reset); 428 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led, 429 int32_t modifier, bool reset); 430}; 431 432 433class CursorInputMapper : public InputMapper { 434public: 435 CursorInputMapper(InputDevice* device); 436 virtual ~CursorInputMapper(); 437 438 virtual uint32_t getSources(); 439 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 440 virtual void dump(String8& dump); 441 virtual void configure(); 442 virtual void reset(); 443 virtual void process(const RawEvent* rawEvent); 444 445 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 446 447private: 448 // Amount that trackball needs to move in order to generate a key event. 449 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; 450 451 Mutex mLock; 452 453 // Immutable configuration parameters. 454 struct Parameters { 455 enum Mode { 456 MODE_POINTER, 457 MODE_NAVIGATION, 458 }; 459 460 Mode mode; 461 int32_t associatedDisplayId; 462 bool orientationAware; 463 } mParameters; 464 465 struct Accumulator { 466 enum { 467 FIELD_BTN_MOUSE = 1, 468 FIELD_REL_X = 2, 469 FIELD_REL_Y = 4 470 }; 471 472 uint32_t fields; 473 474 bool btnMouse; 475 int32_t relX; 476 int32_t relY; 477 478 inline void clear() { 479 fields = 0; 480 } 481 } mAccumulator; 482 483 int32_t mSources; 484 float mXScale; 485 float mYScale; 486 float mXPrecision; 487 float mYPrecision; 488 sp<PointerControllerInterface> mPointerController; 489 490 struct LockedState { 491 bool down; 492 nsecs_t downTime; 493 } mLocked; 494 495 void initializeLocked(); 496 497 void configureParameters(); 498 void dumpParameters(String8& dump); 499 500 void sync(nsecs_t when); 501}; 502 503 504class TouchInputMapper : public InputMapper { 505public: 506 TouchInputMapper(InputDevice* device); 507 virtual ~TouchInputMapper(); 508 509 virtual uint32_t getSources(); 510 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 511 virtual void dump(String8& dump); 512 virtual void configure(); 513 virtual void reset(); 514 515 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 516 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 517 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 518 const int32_t* keyCodes, uint8_t* outFlags); 519 520protected: 521 Mutex mLock; 522 523 struct VirtualKey { 524 int32_t keyCode; 525 int32_t scanCode; 526 uint32_t flags; 527 528 // computed hit box, specified in touch screen coords based on known display size 529 int32_t hitLeft; 530 int32_t hitTop; 531 int32_t hitRight; 532 int32_t hitBottom; 533 534 inline bool isHit(int32_t x, int32_t y) const { 535 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; 536 } 537 }; 538 539 // Raw data for a single pointer. 540 struct PointerData { 541 uint32_t id; 542 int32_t x; 543 int32_t y; 544 int32_t pressure; 545 int32_t touchMajor; 546 int32_t touchMinor; 547 int32_t toolMajor; 548 int32_t toolMinor; 549 int32_t orientation; 550 551 inline bool operator== (const PointerData& other) const { 552 return id == other.id 553 && x == other.x 554 && y == other.y 555 && pressure == other.pressure 556 && touchMajor == other.touchMajor 557 && touchMinor == other.touchMinor 558 && toolMajor == other.toolMajor 559 && toolMinor == other.toolMinor 560 && orientation == other.orientation; 561 } 562 inline bool operator!= (const PointerData& other) const { 563 return !(*this == other); 564 } 565 }; 566 567 // Raw data for a collection of pointers including a pointer id mapping table. 568 struct TouchData { 569 uint32_t pointerCount; 570 PointerData pointers[MAX_POINTERS]; 571 BitSet32 idBits; 572 uint32_t idToIndex[MAX_POINTER_ID + 1]; 573 574 void copyFrom(const TouchData& other) { 575 pointerCount = other.pointerCount; 576 idBits = other.idBits; 577 578 for (uint32_t i = 0; i < pointerCount; i++) { 579 pointers[i] = other.pointers[i]; 580 581 int id = pointers[i].id; 582 idToIndex[id] = other.idToIndex[id]; 583 } 584 } 585 586 inline void clear() { 587 pointerCount = 0; 588 idBits.clear(); 589 } 590 }; 591 592 // Input sources supported by the device. 593 int32_t mSources; 594 595 // Immutable configuration parameters. 596 struct Parameters { 597 enum DeviceType { 598 DEVICE_TYPE_TOUCH_SCREEN, 599 DEVICE_TYPE_TOUCH_PAD, 600 }; 601 602 DeviceType deviceType; 603 int32_t associatedDisplayId; 604 bool orientationAware; 605 606 bool useBadTouchFilter; 607 bool useJumpyTouchFilter; 608 bool useAveragingTouchFilter; 609 } mParameters; 610 611 // Immutable calibration parameters in parsed form. 612 struct Calibration { 613 // Position 614 bool haveXOrigin; 615 int32_t xOrigin; 616 bool haveYOrigin; 617 int32_t yOrigin; 618 bool haveXScale; 619 float xScale; 620 bool haveYScale; 621 float yScale; 622 623 // Touch Size 624 enum TouchSizeCalibration { 625 TOUCH_SIZE_CALIBRATION_DEFAULT, 626 TOUCH_SIZE_CALIBRATION_NONE, 627 TOUCH_SIZE_CALIBRATION_GEOMETRIC, 628 TOUCH_SIZE_CALIBRATION_PRESSURE, 629 }; 630 631 TouchSizeCalibration touchSizeCalibration; 632 633 // Tool Size 634 enum ToolSizeCalibration { 635 TOOL_SIZE_CALIBRATION_DEFAULT, 636 TOOL_SIZE_CALIBRATION_NONE, 637 TOOL_SIZE_CALIBRATION_GEOMETRIC, 638 TOOL_SIZE_CALIBRATION_LINEAR, 639 TOOL_SIZE_CALIBRATION_AREA, 640 }; 641 642 ToolSizeCalibration toolSizeCalibration; 643 bool haveToolSizeLinearScale; 644 float toolSizeLinearScale; 645 bool haveToolSizeLinearBias; 646 float toolSizeLinearBias; 647 bool haveToolSizeAreaScale; 648 float toolSizeAreaScale; 649 bool haveToolSizeAreaBias; 650 float toolSizeAreaBias; 651 bool haveToolSizeIsSummed; 652 bool toolSizeIsSummed; 653 654 // Pressure 655 enum PressureCalibration { 656 PRESSURE_CALIBRATION_DEFAULT, 657 PRESSURE_CALIBRATION_NONE, 658 PRESSURE_CALIBRATION_PHYSICAL, 659 PRESSURE_CALIBRATION_AMPLITUDE, 660 }; 661 enum PressureSource { 662 PRESSURE_SOURCE_DEFAULT, 663 PRESSURE_SOURCE_PRESSURE, 664 PRESSURE_SOURCE_TOUCH, 665 }; 666 667 PressureCalibration pressureCalibration; 668 PressureSource pressureSource; 669 bool havePressureScale; 670 float pressureScale; 671 672 // Size 673 enum SizeCalibration { 674 SIZE_CALIBRATION_DEFAULT, 675 SIZE_CALIBRATION_NONE, 676 SIZE_CALIBRATION_NORMALIZED, 677 }; 678 679 SizeCalibration sizeCalibration; 680 681 // Orientation 682 enum OrientationCalibration { 683 ORIENTATION_CALIBRATION_DEFAULT, 684 ORIENTATION_CALIBRATION_NONE, 685 ORIENTATION_CALIBRATION_INTERPOLATED, 686 ORIENTATION_CALIBRATION_VECTOR, 687 }; 688 689 OrientationCalibration orientationCalibration; 690 } mCalibration; 691 692 // Raw axis information from the driver. 693 struct RawAxes { 694 RawAbsoluteAxisInfo x; 695 RawAbsoluteAxisInfo y; 696 RawAbsoluteAxisInfo pressure; 697 RawAbsoluteAxisInfo touchMajor; 698 RawAbsoluteAxisInfo touchMinor; 699 RawAbsoluteAxisInfo toolMajor; 700 RawAbsoluteAxisInfo toolMinor; 701 RawAbsoluteAxisInfo orientation; 702 } mRawAxes; 703 704 // Current and previous touch sample data. 705 TouchData mCurrentTouch; 706 TouchData mLastTouch; 707 708 // The time the primary pointer last went down. 709 nsecs_t mDownTime; 710 711 struct LockedState { 712 Vector<VirtualKey> virtualKeys; 713 714 // The surface orientation and width and height set by configureSurfaceLocked(). 715 int32_t surfaceOrientation; 716 int32_t surfaceWidth, surfaceHeight; 717 718 // Translation and scaling factors, orientation-independent. 719 int32_t xOrigin; 720 float xScale; 721 float xPrecision; 722 723 int32_t yOrigin; 724 float yScale; 725 float yPrecision; 726 727 float geometricScale; 728 729 float toolSizeLinearScale; 730 float toolSizeLinearBias; 731 float toolSizeAreaScale; 732 float toolSizeAreaBias; 733 734 float pressureScale; 735 736 float sizeScale; 737 738 float orientationScale; 739 740 // Oriented motion ranges for input device info. 741 struct OrientedRanges { 742 InputDeviceInfo::MotionRange x; 743 InputDeviceInfo::MotionRange y; 744 745 bool havePressure; 746 InputDeviceInfo::MotionRange pressure; 747 748 bool haveSize; 749 InputDeviceInfo::MotionRange size; 750 751 bool haveTouchSize; 752 InputDeviceInfo::MotionRange touchMajor; 753 InputDeviceInfo::MotionRange touchMinor; 754 755 bool haveToolSize; 756 InputDeviceInfo::MotionRange toolMajor; 757 InputDeviceInfo::MotionRange toolMinor; 758 759 bool haveOrientation; 760 InputDeviceInfo::MotionRange orientation; 761 } orientedRanges; 762 763 // Oriented dimensions and precision. 764 float orientedSurfaceWidth, orientedSurfaceHeight; 765 float orientedXPrecision, orientedYPrecision; 766 767 struct CurrentVirtualKeyState { 768 bool down; 769 nsecs_t downTime; 770 int32_t keyCode; 771 int32_t scanCode; 772 } currentVirtualKey; 773 } mLocked; 774 775 virtual void configureParameters(); 776 virtual void dumpParameters(String8& dump); 777 virtual void configureRawAxes(); 778 virtual void dumpRawAxes(String8& dump); 779 virtual bool configureSurfaceLocked(); 780 virtual void dumpSurfaceLocked(String8& dump); 781 virtual void configureVirtualKeysLocked(); 782 virtual void dumpVirtualKeysLocked(String8& dump); 783 virtual void parseCalibration(); 784 virtual void resolveCalibration(); 785 virtual void dumpCalibration(String8& dump); 786 787 enum TouchResult { 788 // Dispatch the touch normally. 789 DISPATCH_TOUCH, 790 // Do not dispatch the touch, but keep tracking the current stroke. 791 SKIP_TOUCH, 792 // Do not dispatch the touch, and drop all information associated with the current stoke 793 // so the next movement will appear as a new down. 794 DROP_STROKE 795 }; 796 797 void syncTouch(nsecs_t when, bool havePointerIds); 798 799private: 800 /* Maximum number of historical samples to average. */ 801 static const uint32_t AVERAGING_HISTORY_SIZE = 5; 802 803 /* Slop distance for jumpy pointer detection. 804 * The vertical range of the screen divided by this is our epsilon value. */ 805 static const uint32_t JUMPY_EPSILON_DIVISOR = 212; 806 807 /* Number of jumpy points to drop for touchscreens that need it. */ 808 static const uint32_t JUMPY_TRANSITION_DROPS = 3; 809 static const uint32_t JUMPY_DROP_LIMIT = 3; 810 811 /* Maximum squared distance for averaging. 812 * If moving farther than this, turn of averaging to avoid lag in response. */ 813 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75; 814 815 struct AveragingTouchFilterState { 816 // Individual history tracks are stored by pointer id 817 uint32_t historyStart[MAX_POINTERS]; 818 uint32_t historyEnd[MAX_POINTERS]; 819 struct { 820 struct { 821 int32_t x; 822 int32_t y; 823 int32_t pressure; 824 } pointers[MAX_POINTERS]; 825 } historyData[AVERAGING_HISTORY_SIZE]; 826 } mAveragingTouchFilter; 827 828 struct JumpyTouchFilterState { 829 uint32_t jumpyPointsDropped; 830 } mJumpyTouchFilter; 831 832 struct PointerDistanceHeapElement { 833 uint32_t currentPointerIndex : 8; 834 uint32_t lastPointerIndex : 8; 835 uint64_t distance : 48; // squared distance 836 }; 837 838 void initializeLocked(); 839 840 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags); 841 void dispatchTouches(nsecs_t when, uint32_t policyFlags); 842 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch, 843 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount, 844 int32_t motionEventAction); 845 846 bool isPointInsideSurfaceLocked(int32_t x, int32_t y); 847 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y); 848 849 bool applyBadTouchFilter(); 850 bool applyJumpyTouchFilter(); 851 void applyAveragingTouchFilter(); 852 void calculatePointerIds(); 853}; 854 855 856class SingleTouchInputMapper : public TouchInputMapper { 857public: 858 SingleTouchInputMapper(InputDevice* device); 859 virtual ~SingleTouchInputMapper(); 860 861 virtual void reset(); 862 virtual void process(const RawEvent* rawEvent); 863 864protected: 865 virtual void configureRawAxes(); 866 867private: 868 struct Accumulator { 869 enum { 870 FIELD_BTN_TOUCH = 1, 871 FIELD_ABS_X = 2, 872 FIELD_ABS_Y = 4, 873 FIELD_ABS_PRESSURE = 8, 874 FIELD_ABS_TOOL_WIDTH = 16 875 }; 876 877 uint32_t fields; 878 879 bool btnTouch; 880 int32_t absX; 881 int32_t absY; 882 int32_t absPressure; 883 int32_t absToolWidth; 884 885 inline void clear() { 886 fields = 0; 887 } 888 } mAccumulator; 889 890 bool mDown; 891 int32_t mX; 892 int32_t mY; 893 int32_t mPressure; 894 int32_t mToolWidth; 895 896 void initialize(); 897 898 void sync(nsecs_t when); 899}; 900 901 902class MultiTouchInputMapper : public TouchInputMapper { 903public: 904 MultiTouchInputMapper(InputDevice* device); 905 virtual ~MultiTouchInputMapper(); 906 907 virtual void reset(); 908 virtual void process(const RawEvent* rawEvent); 909 910protected: 911 virtual void configureRawAxes(); 912 913private: 914 struct Accumulator { 915 enum { 916 FIELD_ABS_MT_POSITION_X = 1, 917 FIELD_ABS_MT_POSITION_Y = 2, 918 FIELD_ABS_MT_TOUCH_MAJOR = 4, 919 FIELD_ABS_MT_TOUCH_MINOR = 8, 920 FIELD_ABS_MT_WIDTH_MAJOR = 16, 921 FIELD_ABS_MT_WIDTH_MINOR = 32, 922 FIELD_ABS_MT_ORIENTATION = 64, 923 FIELD_ABS_MT_TRACKING_ID = 128, 924 FIELD_ABS_MT_PRESSURE = 256, 925 }; 926 927 uint32_t pointerCount; 928 struct Pointer { 929 uint32_t fields; 930 931 int32_t absMTPositionX; 932 int32_t absMTPositionY; 933 int32_t absMTTouchMajor; 934 int32_t absMTTouchMinor; 935 int32_t absMTWidthMajor; 936 int32_t absMTWidthMinor; 937 int32_t absMTOrientation; 938 int32_t absMTTrackingId; 939 int32_t absMTPressure; 940 941 inline void clear() { 942 fields = 0; 943 } 944 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks 945 946 inline void clear() { 947 pointerCount = 0; 948 pointers[0].clear(); 949 } 950 } mAccumulator; 951 952 void initialize(); 953 954 void sync(nsecs_t when); 955}; 956 957 958class JoystickInputMapper : public InputMapper { 959public: 960 JoystickInputMapper(InputDevice* device); 961 virtual ~JoystickInputMapper(); 962 963 virtual uint32_t getSources(); 964 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 965 virtual void dump(String8& dump); 966 virtual void configure(); 967 virtual void reset(); 968 virtual void process(const RawEvent* rawEvent); 969 970private: 971 struct RawAxes { 972 RawAbsoluteAxisInfo x; 973 RawAbsoluteAxisInfo y; 974 RawAbsoluteAxisInfo hat0X; 975 RawAbsoluteAxisInfo hat0Y; 976 } mRawAxes; 977 978 struct NormalizedAxis { 979 bool valid; 980 981 static const float min = -1.0f; 982 static const float max = -1.0f; 983 984 float scale; // scale factor 985 float center; // center offset after scaling 986 float precision; // precision 987 float flat; // size of flat region 988 float fuzz; // error tolerance 989 990 float value; // most recent value 991 992 NormalizedAxis() : valid(false), scale(0), center(0), precision(0), 993 flat(0), fuzz(0), value(0) { 994 } 995 996 void configure(const RawAbsoluteAxisInfo& rawAxis) { 997 if (rawAxis.valid && rawAxis.getRange() != 0) { 998 valid = true; 999 scale = 2.0f / rawAxis.getRange(); 1000 precision = rawAxis.getRange(); 1001 flat = rawAxis.flat * scale; 1002 fuzz = rawAxis.fuzz * scale; 1003 center = float(rawAxis.minValue + rawAxis.maxValue) / rawAxis.getRange(); 1004 } 1005 } 1006 1007 void resetState() { 1008 value = 0; 1009 } 1010 1011 bool updateValue(int32_t rawValue) { 1012 float newValue = rawValue * scale - center; 1013 if (value == newValue) { 1014 return false; 1015 } 1016 value = newValue; 1017 return true; 1018 } 1019 }; 1020 1021 struct DirectionalAxis : NormalizedAxis { 1022 int32_t direction; // most recent direction vector: value is one of -1, 0, 1. 1023 1024 int32_t lastKeyCode; // most recent key code produced 1025 1026 DirectionalAxis() : lastKeyCode(0) { 1027 } 1028 1029 void resetState() { 1030 NormalizedAxis::resetState(); 1031 direction = 0; 1032 lastKeyCode = 0; 1033 } 1034 1035 bool updateValueAndDirection(int32_t rawValue) { 1036 if (!updateValue(rawValue)) { 1037 return false; 1038 } 1039 if (value > flat) { 1040 direction = 1; 1041 } else if (value < -flat) { 1042 direction = -1; 1043 } else { 1044 direction = 0; 1045 } 1046 return true; 1047 } 1048 }; 1049 1050 struct Axes { 1051 NormalizedAxis x; 1052 NormalizedAxis y; 1053 DirectionalAxis hat0X; 1054 DirectionalAxis hat0Y; 1055 } mAxes; 1056 1057 struct Accumulator { 1058 enum { 1059 FIELD_ABS_X = 1, 1060 FIELD_ABS_Y = 2, 1061 FIELD_ABS_HAT0X = 4, 1062 FIELD_ABS_HAT0Y = 8, 1063 1064 FIELD_ALL = FIELD_ABS_X | FIELD_ABS_Y | FIELD_ABS_HAT0X | FIELD_ABS_HAT0Y, 1065 }; 1066 1067 uint32_t fields; 1068 1069 int32_t absX; 1070 int32_t absY; 1071 int32_t absHat0X; 1072 int32_t absHat0Y; 1073 1074 inline void clear() { 1075 fields = 0; 1076 } 1077 } mAccumulator; 1078 1079 void initialize(); 1080 1081 void sync(nsecs_t when); 1082 1083 void notifyDirectionalAxis(DirectionalAxis& axis, 1084 nsecs_t when, int32_t metaState, int32_t lowKeyCode, int32_t highKeyCode); 1085 1086 static void dumpNormalizedAxis(String8& dump, 1087 const NormalizedAxis& axis, const char* name); 1088}; 1089 1090} // namespace android 1091 1092#endif // _UI_INPUT_READER_H 1093