InputReader.h revision be1aa8250cee7819c49741e819e81659d1d03823
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 "PointerController.h" 22#include "InputListener.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 configuration. 44 * 45 * Specifies various options that modify the behavior of the input reader. 46 */ 47struct InputReaderConfiguration { 48 // Describes changes that have occurred. 49 enum { 50 // The pointer speed changed. 51 CHANGE_POINTER_SPEED = 1 << 0, 52 53 // The pointer gesture control changed. 54 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1, 55 56 // All devices must be reopened. 57 CHANGE_MUST_REOPEN = 1 << 31, 58 }; 59 60 // Gets the amount of time to disable virtual keys after the screen is touched 61 // in order to filter out accidental virtual key presses due to swiping gestures 62 // or taps near the edge of the display. May be 0 to disable the feature. 63 nsecs_t virtualKeyQuietTime; 64 65 // The excluded device names for the platform. 66 // Devices with these names will be ignored. 67 Vector<String8> excludedDeviceNames; 68 69 // Velocity control parameters for mouse pointer movements. 70 VelocityControlParameters pointerVelocityControlParameters; 71 72 // Velocity control parameters for mouse wheel movements. 73 VelocityControlParameters wheelVelocityControlParameters; 74 75 // True if pointer gestures are enabled. 76 bool pointerGesturesEnabled; 77 78 // Quiet time between certain pointer gesture transitions. 79 // Time to allow for all fingers or buttons to settle into a stable state before 80 // starting a new gesture. 81 nsecs_t pointerGestureQuietInterval; 82 83 // The minimum speed that a pointer must travel for us to consider switching the active 84 // touch pointer to it during a drag. This threshold is set to avoid switching due 85 // to noise from a finger resting on the touch pad (perhaps just pressing it down). 86 float pointerGestureDragMinSwitchSpeed; // in pixels per second 87 88 // Tap gesture delay time. 89 // The time between down and up must be less than this to be considered a tap. 90 nsecs_t pointerGestureTapInterval; 91 92 // Tap drag gesture delay time. 93 // The time between the previous tap's up and the next down must be less than 94 // this to be considered a drag. Otherwise, the previous tap is finished and a 95 // new tap begins. 96 // 97 // Note that the previous tap will be held down for this entire duration so this 98 // interval must be shorter than the long press timeout. 99 nsecs_t pointerGestureTapDragInterval; 100 101 // The distance in pixels that the pointer is allowed to move from initial down 102 // to up and still be called a tap. 103 float pointerGestureTapSlop; // in pixels 104 105 // Time after the first touch points go down to settle on an initial centroid. 106 // This is intended to be enough time to handle cases where the user puts down two 107 // fingers at almost but not quite exactly the same time. 108 nsecs_t pointerGestureMultitouchSettleInterval; 109 110 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when 111 // at least two pointers have moved at least this far from their starting place. 112 float pointerGestureMultitouchMinDistance; // in pixels 113 114 // The transition from PRESS to SWIPE gesture mode can only occur when the 115 // cosine of the angle between the two vectors is greater than or equal to than this value 116 // which indicates that the vectors are oriented in the same direction. 117 // When the vectors are oriented in the exactly same direction, the cosine is 1.0. 118 // (In exactly opposite directions, the cosine is -1.0.) 119 float pointerGestureSwipeTransitionAngleCosine; 120 121 // The transition from PRESS to SWIPE gesture mode can only occur when the 122 // fingers are no more than this far apart relative to the diagonal size of 123 // the touch pad. For example, a ratio of 0.5 means that the fingers must be 124 // no more than half the diagonal size of the touch pad apart. 125 float pointerGestureSwipeMaxWidthRatio; 126 127 // The gesture movement speed factor relative to the size of the display. 128 // Movement speed applies when the fingers are moving in the same direction. 129 // Without acceleration, a full swipe of the touch pad diagonal in movement mode 130 // will cover this portion of the display diagonal. 131 float pointerGestureMovementSpeedRatio; 132 133 // The gesture zoom speed factor relative to the size of the display. 134 // Zoom speed applies when the fingers are mostly moving relative to each other 135 // to execute a scale gesture or similar. 136 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode 137 // will cover this portion of the display diagonal. 138 float pointerGestureZoomSpeedRatio; 139 140 InputReaderConfiguration() : 141 virtualKeyQuietTime(0), 142 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f), 143 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f), 144 pointerGesturesEnabled(true), 145 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms 146 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second 147 pointerGestureTapInterval(150 * 1000000LL), // 150 ms 148 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms 149 pointerGestureTapSlop(10.0f), // 10 pixels 150 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms 151 pointerGestureMultitouchMinDistance(15), // 15 pixels 152 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees 153 pointerGestureSwipeMaxWidthRatio(0.25f), 154 pointerGestureMovementSpeedRatio(0.8f), 155 pointerGestureZoomSpeedRatio(0.3f) { } 156}; 157 158 159/* 160 * Input reader policy interface. 161 * 162 * The input reader policy is used by the input reader to interact with the Window Manager 163 * and other system components. 164 * 165 * The actual implementation is partially supported by callbacks into the DVM 166 * via JNI. This interface is also mocked in the unit tests. 167 * 168 * These methods must NOT re-enter the input reader since they may be called while 169 * holding the input reader lock. 170 */ 171class InputReaderPolicyInterface : public virtual RefBase { 172protected: 173 InputReaderPolicyInterface() { } 174 virtual ~InputReaderPolicyInterface() { } 175 176public: 177 /* Display orientations. */ 178 enum { 179 ROTATION_0 = 0, 180 ROTATION_90 = 1, 181 ROTATION_180 = 2, 182 ROTATION_270 = 3 183 }; 184 185 /* Gets information about the display with the specified id. 186 * If external is true, returns the size of the external mirrored 187 * counterpart of the specified display. 188 * Returns true if the display info is available, false otherwise. 189 */ 190 virtual bool getDisplayInfo(int32_t displayId, bool external, 191 int32_t* width, int32_t* height, int32_t* orientation) = 0; 192 193 /* Gets the input reader configuration. */ 194 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0; 195 196 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */ 197 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0; 198}; 199 200 201/* Processes raw input events and sends cooked event data to an input listener. */ 202class InputReaderInterface : public virtual RefBase { 203protected: 204 InputReaderInterface() { } 205 virtual ~InputReaderInterface() { } 206 207public: 208 /* Dumps the state of the input reader. 209 * 210 * This method may be called on any thread (usually by the input manager). */ 211 virtual void dump(String8& dump) = 0; 212 213 /* Runs a single iteration of the processing loop. 214 * Nominally reads and processes one incoming message from the EventHub. 215 * 216 * This method should be called on the input reader thread. 217 */ 218 virtual void loopOnce() = 0; 219 220 /* Gets the current input device configuration. 221 * 222 * This method may be called on any thread (usually by the input manager). 223 */ 224 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0; 225 226 /* Gets information about the specified input device. 227 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there 228 * was no such device. 229 * 230 * This method may be called on any thread (usually by the input manager). 231 */ 232 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0; 233 234 /* Gets the list of all registered device ids. */ 235 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0; 236 237 /* Query current input state. */ 238 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, 239 int32_t scanCode) = 0; 240 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, 241 int32_t keyCode) = 0; 242 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, 243 int32_t sw) = 0; 244 245 /* Determine whether physical keys exist for the given framework-domain key codes. */ 246 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, 247 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; 248 249 /* Requests that a reconfiguration of all input devices. 250 * The changes flag is a bitfield that indicates what has changed and whether 251 * the input devices must all be reopened. */ 252 virtual void requestRefreshConfiguration(uint32_t changes) = 0; 253}; 254 255 256/* Internal interface used by individual input devices to access global input device state 257 * and parameters maintained by the input reader. 258 */ 259class InputReaderContext { 260public: 261 InputReaderContext() { } 262 virtual ~InputReaderContext() { } 263 264 virtual void updateGlobalMetaState() = 0; 265 virtual int32_t getGlobalMetaState() = 0; 266 267 virtual void disableVirtualKeysUntil(nsecs_t time) = 0; 268 virtual bool shouldDropVirtualKey(nsecs_t now, 269 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0; 270 271 virtual void fadePointer() = 0; 272 273 virtual void requestTimeoutAtTime(nsecs_t when) = 0; 274 275 virtual InputReaderPolicyInterface* getPolicy() = 0; 276 virtual InputListenerInterface* getListener() = 0; 277 virtual EventHubInterface* getEventHub() = 0; 278}; 279 280 281/* The input reader reads raw event data from the event hub and processes it into input events 282 * that it sends to the input listener. Some functions of the input reader, such as early 283 * event filtering in low power states, are controlled by a separate policy object. 284 * 285 * The InputReader owns a collection of InputMappers. Most of the work it does happens 286 * on the input reader thread but the InputReader can receive queries from other system 287 * components running on arbitrary threads. To keep things manageable, the InputReader 288 * uses a single Mutex to guard its state. The Mutex may be held while calling into the 289 * EventHub or the InputReaderPolicy but it is never held while calling into the 290 * InputListener. 291 */ 292class InputReader : public InputReaderInterface { 293public: 294 InputReader(const sp<EventHubInterface>& eventHub, 295 const sp<InputReaderPolicyInterface>& policy, 296 const sp<InputListenerInterface>& listener); 297 virtual ~InputReader(); 298 299 virtual void dump(String8& dump); 300 301 virtual void loopOnce(); 302 303 virtual void getInputConfiguration(InputConfiguration* outConfiguration); 304 305 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo); 306 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds); 307 308 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, 309 int32_t scanCode); 310 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, 311 int32_t keyCode); 312 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, 313 int32_t sw); 314 315 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, 316 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags); 317 318 virtual void requestRefreshConfiguration(uint32_t changes); 319 320protected: 321 // These members are protected so they can be instrumented by test cases. 322 virtual InputDevice* createDeviceLocked(int32_t deviceId, 323 const String8& name, uint32_t classes); 324 325 class ContextImpl : public InputReaderContext { 326 InputReader* mReader; 327 328 public: 329 ContextImpl(InputReader* reader); 330 331 virtual void updateGlobalMetaState(); 332 virtual int32_t getGlobalMetaState(); 333 virtual void disableVirtualKeysUntil(nsecs_t time); 334 virtual bool shouldDropVirtualKey(nsecs_t now, 335 InputDevice* device, int32_t keyCode, int32_t scanCode); 336 virtual void fadePointer(); 337 virtual void requestTimeoutAtTime(nsecs_t when); 338 virtual InputReaderPolicyInterface* getPolicy(); 339 virtual InputListenerInterface* getListener(); 340 virtual EventHubInterface* getEventHub(); 341 } mContext; 342 343 friend class ContextImpl; 344 345private: 346 Mutex mLock; 347 348 sp<EventHubInterface> mEventHub; 349 sp<InputReaderPolicyInterface> mPolicy; 350 sp<QueuedInputListener> mQueuedListener; 351 352 InputReaderConfiguration mConfig; 353 354 // The event queue. 355 static const int EVENT_BUFFER_SIZE = 256; 356 RawEvent mEventBuffer[EVENT_BUFFER_SIZE]; 357 358 KeyedVector<int32_t, InputDevice*> mDevices; 359 360 // low-level input event decoding and device management 361 void processEventsLocked(const RawEvent* rawEvents, size_t count); 362 363 void addDeviceLocked(int32_t deviceId); 364 void removeDeviceLocked(int32_t deviceId); 365 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count); 366 void timeoutExpiredLocked(nsecs_t when); 367 368 void handleConfigurationChangedLocked(nsecs_t when); 369 370 int32_t mGlobalMetaState; 371 void updateGlobalMetaStateLocked(); 372 int32_t getGlobalMetaStateLocked(); 373 374 void fadePointerLocked(); 375 376 InputConfiguration mInputConfiguration; 377 void updateInputConfigurationLocked(); 378 379 nsecs_t mDisableVirtualKeysTimeout; 380 void disableVirtualKeysUntilLocked(nsecs_t time); 381 bool shouldDropVirtualKeyLocked(nsecs_t now, 382 InputDevice* device, int32_t keyCode, int32_t scanCode); 383 384 nsecs_t mNextTimeout; 385 void requestTimeoutAtTimeLocked(nsecs_t when); 386 387 uint32_t mConfigurationChangesToRefresh; 388 void refreshConfigurationLocked(uint32_t changes); 389 390 // state queries 391 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); 392 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, 393 GetStateFunc getStateFunc); 394 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes, 395 const int32_t* keyCodes, uint8_t* outFlags); 396}; 397 398 399/* Reads raw events from the event hub and processes them, endlessly. */ 400class InputReaderThread : public Thread { 401public: 402 InputReaderThread(const sp<InputReaderInterface>& reader); 403 virtual ~InputReaderThread(); 404 405private: 406 sp<InputReaderInterface> mReader; 407 408 virtual bool threadLoop(); 409}; 410 411 412/* Represents the state of a single input device. */ 413class InputDevice { 414public: 415 InputDevice(InputReaderContext* context, int32_t id, const String8& name); 416 ~InputDevice(); 417 418 inline InputReaderContext* getContext() { return mContext; } 419 inline int32_t getId() { return mId; } 420 inline const String8& getName() { return mName; } 421 inline uint32_t getSources() { return mSources; } 422 423 inline bool isExternal() { return mIsExternal; } 424 inline void setExternal(bool external) { mIsExternal = external; } 425 426 inline bool isIgnored() { return mMappers.isEmpty(); } 427 428 void dump(String8& dump); 429 void addMapper(InputMapper* mapper); 430 void configure(const InputReaderConfiguration* config, uint32_t changes); 431 void reset(); 432 void process(const RawEvent* rawEvents, size_t count); 433 void timeoutExpired(nsecs_t when); 434 435 void getDeviceInfo(InputDeviceInfo* outDeviceInfo); 436 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 437 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 438 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 439 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 440 const int32_t* keyCodes, uint8_t* outFlags); 441 442 int32_t getMetaState(); 443 444 void fadePointer(); 445 446 inline const PropertyMap& getConfiguration() { return mConfiguration; } 447 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } 448 449private: 450 InputReaderContext* mContext; 451 int32_t mId; 452 453 Vector<InputMapper*> mMappers; 454 455 String8 mName; 456 uint32_t mSources; 457 bool mIsExternal; 458 bool mDropUntilNextSync; 459 460 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); 461 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); 462 463 PropertyMap mConfiguration; 464}; 465 466 467/* Keeps track of the state of mouse or touch pad buttons. */ 468class CursorButtonAccumulator { 469public: 470 CursorButtonAccumulator(); 471 472 void clearButtons(); 473 void process(const RawEvent* rawEvent); 474 475 uint32_t getButtonState() const; 476 477private: 478 bool mBtnLeft; 479 bool mBtnRight; 480 bool mBtnMiddle; 481 bool mBtnBack; 482 bool mBtnSide; 483 bool mBtnForward; 484 bool mBtnExtra; 485 bool mBtnTask; 486}; 487 488 489/* Keeps track of cursor movements. */ 490 491class CursorMotionAccumulator { 492public: 493 CursorMotionAccumulator(); 494 void configure(InputDevice* device); 495 496 void clearRelativeAxes(); 497 void process(const RawEvent* rawEvent); 498 499 inline bool haveRelativeVWheel() const { return mHaveRelWheel; } 500 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; } 501 502 inline int32_t getRelativeX() const { return mRelX; } 503 inline int32_t getRelativeY() const { return mRelY; } 504 inline int32_t getRelativeVWheel() const { return mRelWheel; } 505 inline int32_t getRelativeHWheel() const { return mRelHWheel; } 506 507private: 508 bool mHaveRelWheel; 509 bool mHaveRelHWheel; 510 511 int32_t mRelX; 512 int32_t mRelY; 513 int32_t mRelWheel; 514 int32_t mRelHWheel; 515}; 516 517 518/* Keeps track of the state of touch, stylus and tool buttons. */ 519class TouchButtonAccumulator { 520public: 521 TouchButtonAccumulator(); 522 void configure(InputDevice* device); 523 524 void clearButtons(); 525 void process(const RawEvent* rawEvent); 526 527 uint32_t getButtonState() const; 528 int32_t getToolType() const; 529 bool isActive() const; 530 bool isHovering() const; 531 532private: 533 bool mHaveBtnTouch; 534 535 bool mBtnTouch; 536 bool mBtnStylus; 537 bool mBtnStylus2; 538 bool mBtnToolFinger; 539 bool mBtnToolPen; 540 bool mBtnToolRubber; 541}; 542 543 544/* Raw axis information from the driver. */ 545struct RawPointerAxes { 546 RawAbsoluteAxisInfo x; 547 RawAbsoluteAxisInfo y; 548 RawAbsoluteAxisInfo pressure; 549 RawAbsoluteAxisInfo touchMajor; 550 RawAbsoluteAxisInfo touchMinor; 551 RawAbsoluteAxisInfo toolMajor; 552 RawAbsoluteAxisInfo toolMinor; 553 RawAbsoluteAxisInfo orientation; 554 RawAbsoluteAxisInfo distance; 555 RawAbsoluteAxisInfo trackingId; 556 RawAbsoluteAxisInfo slot; 557 558 RawPointerAxes(); 559 void clear(); 560}; 561 562 563/* Raw data for a collection of pointers including a pointer id mapping table. */ 564struct RawPointerData { 565 struct Pointer { 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 int32_t distance; 576 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant 577 bool isHovering; 578 }; 579 580 uint32_t pointerCount; 581 Pointer pointers[MAX_POINTERS]; 582 BitSet32 hoveringIdBits, touchingIdBits; 583 uint32_t idToIndex[MAX_POINTER_ID + 1]; 584 585 RawPointerData(); 586 void clear(); 587 void copyFrom(const RawPointerData& other); 588 void getCentroidOfTouchingPointers(float* outX, float* outY) const; 589 590 inline void markIdBit(uint32_t id, bool isHovering) { 591 if (isHovering) { 592 hoveringIdBits.markBit(id); 593 } else { 594 touchingIdBits.markBit(id); 595 } 596 } 597 598 inline void clearIdBits() { 599 hoveringIdBits.clear(); 600 touchingIdBits.clear(); 601 } 602 603 inline const Pointer& pointerForId(uint32_t id) const { 604 return pointers[idToIndex[id]]; 605 } 606 607 inline bool isHovering(uint32_t pointerIndex) { 608 return pointers[pointerIndex].isHovering; 609 } 610}; 611 612 613/* Cooked data for a collection of pointers including a pointer id mapping table. */ 614struct CookedPointerData { 615 uint32_t pointerCount; 616 PointerProperties pointerProperties[MAX_POINTERS]; 617 PointerCoords pointerCoords[MAX_POINTERS]; 618 BitSet32 hoveringIdBits, touchingIdBits; 619 uint32_t idToIndex[MAX_POINTER_ID + 1]; 620 621 CookedPointerData(); 622 void clear(); 623 void copyFrom(const CookedPointerData& other); 624 625 inline bool isHovering(uint32_t pointerIndex) { 626 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id); 627 } 628}; 629 630 631/* Keeps track of the state of single-touch protocol. */ 632class SingleTouchMotionAccumulator { 633public: 634 SingleTouchMotionAccumulator(); 635 636 void clearAbsoluteAxes(); 637 void process(const RawEvent* rawEvent); 638 639 inline int32_t getAbsoluteX() const { return mAbsX; } 640 inline int32_t getAbsoluteY() const { return mAbsY; } 641 inline int32_t getAbsolutePressure() const { return mAbsPressure; } 642 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; } 643 inline int32_t getAbsoluteDistance() const { return mAbsDistance; } 644 645private: 646 int32_t mAbsX; 647 int32_t mAbsY; 648 int32_t mAbsPressure; 649 int32_t mAbsToolWidth; 650 int32_t mAbsDistance; 651}; 652 653 654/* Keeps track of the state of multi-touch protocol. */ 655class MultiTouchMotionAccumulator { 656public: 657 class Slot { 658 public: 659 inline bool isInUse() const { return mInUse; } 660 inline int32_t getX() const { return mAbsMTPositionX; } 661 inline int32_t getY() const { return mAbsMTPositionY; } 662 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; } 663 inline int32_t getTouchMinor() const { 664 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; } 665 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; } 666 inline int32_t getToolMinor() const { 667 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; } 668 inline int32_t getOrientation() const { return mAbsMTOrientation; } 669 inline int32_t getTrackingId() const { return mAbsMTTrackingId; } 670 inline int32_t getPressure() const { return mAbsMTPressure; } 671 inline int32_t getDistance() const { return mAbsMTDistance; } 672 inline int32_t getToolType() const; 673 674 private: 675 friend class MultiTouchMotionAccumulator; 676 677 bool mInUse; 678 bool mHaveAbsMTTouchMinor; 679 bool mHaveAbsMTWidthMinor; 680 bool mHaveAbsMTToolType; 681 682 int32_t mAbsMTPositionX; 683 int32_t mAbsMTPositionY; 684 int32_t mAbsMTTouchMajor; 685 int32_t mAbsMTTouchMinor; 686 int32_t mAbsMTWidthMajor; 687 int32_t mAbsMTWidthMinor; 688 int32_t mAbsMTOrientation; 689 int32_t mAbsMTTrackingId; 690 int32_t mAbsMTPressure; 691 int32_t mAbsMTDistance; 692 int32_t mAbsMTToolType; 693 694 Slot(); 695 void clearIfInUse(); 696 void clear(); 697 }; 698 699 MultiTouchMotionAccumulator(); 700 ~MultiTouchMotionAccumulator(); 701 702 void configure(size_t slotCount, bool usingSlotsProtocol); 703 void process(const RawEvent* rawEvent); 704 705 void clearSlots(int32_t initialSlot); 706 707 inline bool isUsingSlotsProtocol() const { return mUsingSlotsProtocol; } 708 inline size_t getSlotCount() const { return mSlotCount; } 709 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; } 710 711private: 712 int32_t mCurrentSlot; 713 Slot* mSlots; 714 size_t mSlotCount; 715 bool mUsingSlotsProtocol; 716}; 717 718 719/* An input mapper transforms raw input events into cooked event data. 720 * A single input device can have multiple associated input mappers in order to interpret 721 * different classes of events. 722 */ 723class InputMapper { 724public: 725 InputMapper(InputDevice* device); 726 virtual ~InputMapper(); 727 728 inline InputDevice* getDevice() { return mDevice; } 729 inline int32_t getDeviceId() { return mDevice->getId(); } 730 inline const String8 getDeviceName() { return mDevice->getName(); } 731 inline InputReaderContext* getContext() { return mContext; } 732 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); } 733 inline InputListenerInterface* getListener() { return mContext->getListener(); } 734 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } 735 736 virtual uint32_t getSources() = 0; 737 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 738 virtual void dump(String8& dump); 739 virtual void configure(const InputReaderConfiguration* config, uint32_t changes); 740 virtual void reset(); 741 virtual void process(const RawEvent* rawEvent) = 0; 742 virtual void timeoutExpired(nsecs_t when); 743 744 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 745 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 746 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 747 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 748 const int32_t* keyCodes, uint8_t* outFlags); 749 750 virtual int32_t getMetaState(); 751 752 virtual void fadePointer(); 753 754protected: 755 InputDevice* mDevice; 756 InputReaderContext* mContext; 757 758 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo); 759 760 static void dumpRawAbsoluteAxisInfo(String8& dump, 761 const RawAbsoluteAxisInfo& axis, const char* name); 762}; 763 764 765class SwitchInputMapper : public InputMapper { 766public: 767 SwitchInputMapper(InputDevice* device); 768 virtual ~SwitchInputMapper(); 769 770 virtual uint32_t getSources(); 771 virtual void process(const RawEvent* rawEvent); 772 773 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 774 775private: 776 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue); 777}; 778 779 780class KeyboardInputMapper : public InputMapper { 781public: 782 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType); 783 virtual ~KeyboardInputMapper(); 784 785 virtual uint32_t getSources(); 786 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 787 virtual void dump(String8& dump); 788 virtual void configure(const InputReaderConfiguration* config, uint32_t changes); 789 virtual void reset(); 790 virtual void process(const RawEvent* rawEvent); 791 792 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 793 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 794 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 795 const int32_t* keyCodes, uint8_t* outFlags); 796 797 virtual int32_t getMetaState(); 798 799private: 800 Mutex mLock; 801 802 struct KeyDown { 803 int32_t keyCode; 804 int32_t scanCode; 805 }; 806 807 uint32_t mSource; 808 int32_t mKeyboardType; 809 810 Vector<KeyDown> mKeyDowns; // keys that are down 811 int32_t mMetaState; 812 nsecs_t mDownTime; // time of most recent key down 813 814 struct LedState { 815 bool avail; // led is available 816 bool on; // we think the led is currently on 817 }; 818 LedState mCapsLockLedState; 819 LedState mNumLockLedState; 820 LedState mScrollLockLedState; 821 822 // Immutable configuration parameters. 823 struct Parameters { 824 int32_t associatedDisplayId; 825 bool orientationAware; 826 } mParameters; 827 828 void initialize(); 829 830 void configureParameters(); 831 void dumpParameters(String8& dump); 832 833 bool isKeyboardOrGamepadKey(int32_t scanCode); 834 835 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode, 836 uint32_t policyFlags); 837 838 ssize_t findKeyDown(int32_t scanCode); 839 840 void resetLedState(); 841 void initializeLedState(LedState& ledState, int32_t led); 842 void updateLedState(bool reset); 843 void updateLedStateForModifier(LedState& ledState, int32_t led, 844 int32_t modifier, bool reset); 845}; 846 847 848class CursorInputMapper : public InputMapper { 849public: 850 CursorInputMapper(InputDevice* device); 851 virtual ~CursorInputMapper(); 852 853 virtual uint32_t getSources(); 854 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 855 virtual void dump(String8& dump); 856 virtual void configure(const InputReaderConfiguration* config, uint32_t changes); 857 virtual void reset(); 858 virtual void process(const RawEvent* rawEvent); 859 860 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 861 862 virtual void fadePointer(); 863 864private: 865 // Amount that trackball needs to move in order to generate a key event. 866 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; 867 868 // Immutable configuration parameters. 869 struct Parameters { 870 enum Mode { 871 MODE_POINTER, 872 MODE_NAVIGATION, 873 }; 874 875 Mode mode; 876 int32_t associatedDisplayId; 877 bool orientationAware; 878 } mParameters; 879 880 CursorButtonAccumulator mCursorButtonAccumulator; 881 CursorMotionAccumulator mCursorMotionAccumulator; 882 883 int32_t mSource; 884 float mXScale; 885 float mYScale; 886 float mXPrecision; 887 float mYPrecision; 888 889 float mVWheelScale; 890 float mHWheelScale; 891 892 // Velocity controls for mouse pointer and wheel movements. 893 // The controls for X and Y wheel movements are separate to keep them decoupled. 894 VelocityControl mPointerVelocityControl; 895 VelocityControl mWheelXVelocityControl; 896 VelocityControl mWheelYVelocityControl; 897 898 sp<PointerControllerInterface> mPointerController; 899 900 int32_t mButtonState; 901 nsecs_t mDownTime; 902 903 void initialize(); 904 905 void configureParameters(); 906 void dumpParameters(String8& dump); 907 908 void sync(nsecs_t when); 909}; 910 911 912class TouchInputMapper : public InputMapper { 913public: 914 TouchInputMapper(InputDevice* device); 915 virtual ~TouchInputMapper(); 916 917 virtual uint32_t getSources(); 918 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 919 virtual void dump(String8& dump); 920 virtual void configure(const InputReaderConfiguration* config, uint32_t changes); 921 virtual void reset(); 922 923 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 924 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 925 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, 926 const int32_t* keyCodes, uint8_t* outFlags); 927 928 virtual void fadePointer(); 929 virtual void timeoutExpired(nsecs_t when); 930 931protected: 932 struct VirtualKey { 933 int32_t keyCode; 934 int32_t scanCode; 935 uint32_t flags; 936 937 // computed hit box, specified in touch screen coords based on known display size 938 int32_t hitLeft; 939 int32_t hitTop; 940 int32_t hitRight; 941 int32_t hitBottom; 942 943 inline bool isHit(int32_t x, int32_t y) const { 944 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; 945 } 946 }; 947 948 // Input sources supported by the device. 949 uint32_t mTouchSource; // sources when reporting touch data 950 uint32_t mPointerSource; // sources when reporting pointer gestures 951 952 // The reader's configuration. 953 InputReaderConfiguration mConfig; 954 955 // Immutable configuration parameters. 956 struct Parameters { 957 enum DeviceType { 958 DEVICE_TYPE_TOUCH_SCREEN, 959 DEVICE_TYPE_TOUCH_PAD, 960 DEVICE_TYPE_POINTER, 961 }; 962 963 DeviceType deviceType; 964 int32_t associatedDisplayId; 965 bool associatedDisplayIsExternal; 966 bool orientationAware; 967 968 enum GestureMode { 969 GESTURE_MODE_POINTER, 970 GESTURE_MODE_SPOTS, 971 }; 972 GestureMode gestureMode; 973 } mParameters; 974 975 // Immutable calibration parameters in parsed form. 976 struct Calibration { 977 // Touch Size 978 enum TouchSizeCalibration { 979 TOUCH_SIZE_CALIBRATION_DEFAULT, 980 TOUCH_SIZE_CALIBRATION_NONE, 981 TOUCH_SIZE_CALIBRATION_GEOMETRIC, 982 TOUCH_SIZE_CALIBRATION_PRESSURE, 983 }; 984 985 TouchSizeCalibration touchSizeCalibration; 986 987 // Tool Size 988 enum ToolSizeCalibration { 989 TOOL_SIZE_CALIBRATION_DEFAULT, 990 TOOL_SIZE_CALIBRATION_NONE, 991 TOOL_SIZE_CALIBRATION_GEOMETRIC, 992 TOOL_SIZE_CALIBRATION_LINEAR, 993 TOOL_SIZE_CALIBRATION_AREA, 994 }; 995 996 ToolSizeCalibration toolSizeCalibration; 997 bool haveToolSizeLinearScale; 998 float toolSizeLinearScale; 999 bool haveToolSizeLinearBias; 1000 float toolSizeLinearBias; 1001 bool haveToolSizeAreaScale; 1002 float toolSizeAreaScale; 1003 bool haveToolSizeAreaBias; 1004 float toolSizeAreaBias; 1005 bool haveToolSizeIsSummed; 1006 bool toolSizeIsSummed; 1007 1008 // Pressure 1009 enum PressureCalibration { 1010 PRESSURE_CALIBRATION_DEFAULT, 1011 PRESSURE_CALIBRATION_NONE, 1012 PRESSURE_CALIBRATION_PHYSICAL, 1013 PRESSURE_CALIBRATION_AMPLITUDE, 1014 }; 1015 enum PressureSource { 1016 PRESSURE_SOURCE_DEFAULT, 1017 PRESSURE_SOURCE_PRESSURE, 1018 PRESSURE_SOURCE_TOUCH, 1019 }; 1020 1021 PressureCalibration pressureCalibration; 1022 PressureSource pressureSource; 1023 bool havePressureScale; 1024 float pressureScale; 1025 1026 // Size 1027 enum SizeCalibration { 1028 SIZE_CALIBRATION_DEFAULT, 1029 SIZE_CALIBRATION_NONE, 1030 SIZE_CALIBRATION_NORMALIZED, 1031 }; 1032 1033 SizeCalibration sizeCalibration; 1034 1035 // Orientation 1036 enum OrientationCalibration { 1037 ORIENTATION_CALIBRATION_DEFAULT, 1038 ORIENTATION_CALIBRATION_NONE, 1039 ORIENTATION_CALIBRATION_INTERPOLATED, 1040 ORIENTATION_CALIBRATION_VECTOR, 1041 }; 1042 1043 OrientationCalibration orientationCalibration; 1044 1045 // Distance 1046 enum DistanceCalibration { 1047 DISTANCE_CALIBRATION_DEFAULT, 1048 DISTANCE_CALIBRATION_NONE, 1049 DISTANCE_CALIBRATION_SCALED, 1050 }; 1051 1052 DistanceCalibration distanceCalibration; 1053 bool haveDistanceScale; 1054 float distanceScale; 1055 } mCalibration; 1056 1057 // Raw pointer axis information from the driver. 1058 RawPointerAxes mRawPointerAxes; 1059 1060 // Raw pointer sample data. 1061 RawPointerData mCurrentRawPointerData; 1062 RawPointerData mLastRawPointerData; 1063 1064 // Cooked pointer sample data. 1065 CookedPointerData mCurrentCookedPointerData; 1066 CookedPointerData mLastCookedPointerData; 1067 1068 // Button state. 1069 int32_t mCurrentButtonState; 1070 int32_t mLastButtonState; 1071 1072 // True if we sent a HOVER_ENTER event. 1073 bool mSentHoverEnter; 1074 1075 // The time the primary pointer last went down. 1076 nsecs_t mDownTime; 1077 1078 // The pointer controller, or null if the device is not a pointer. 1079 sp<PointerControllerInterface> mPointerController; 1080 1081 Vector<VirtualKey> mVirtualKeys; 1082 1083 virtual void configureParameters(); 1084 virtual void dumpParameters(String8& dump); 1085 virtual void configureRawPointerAxes(); 1086 virtual void dumpRawPointerAxes(String8& dump); 1087 virtual bool configureSurface(); 1088 virtual void dumpSurface(String8& dump); 1089 virtual void configureVirtualKeys(); 1090 virtual void dumpVirtualKeys(String8& dump); 1091 virtual void parseCalibration(); 1092 virtual void resolveCalibration(); 1093 virtual void dumpCalibration(String8& dump); 1094 1095 void syncTouch(nsecs_t when, bool havePointerIds); 1096 1097private: 1098 // The surface orientation and width and height set by configureSurface(). 1099 int32_t mSurfaceOrientation; 1100 int32_t mSurfaceWidth; 1101 int32_t mSurfaceHeight; 1102 1103 // The associated display orientation and width and height set by configureSurface(). 1104 int32_t mAssociatedDisplayOrientation; 1105 int32_t mAssociatedDisplayWidth; 1106 int32_t mAssociatedDisplayHeight; 1107 1108 // Translation and scaling factors, orientation-independent. 1109 float mXScale; 1110 float mXPrecision; 1111 1112 float mYScale; 1113 float mYPrecision; 1114 1115 float mGeometricScale; 1116 1117 float mToolSizeLinearScale; 1118 float mToolSizeLinearBias; 1119 float mToolSizeAreaScale; 1120 float mToolSizeAreaBias; 1121 1122 float mPressureScale; 1123 1124 float mSizeScale; 1125 1126 float mOrientationScale; 1127 1128 float mDistanceScale; 1129 1130 // Oriented motion ranges for input device info. 1131 struct OrientedRanges { 1132 InputDeviceInfo::MotionRange x; 1133 InputDeviceInfo::MotionRange y; 1134 1135 bool havePressure; 1136 InputDeviceInfo::MotionRange pressure; 1137 1138 bool haveSize; 1139 InputDeviceInfo::MotionRange size; 1140 1141 bool haveTouchSize; 1142 InputDeviceInfo::MotionRange touchMajor; 1143 InputDeviceInfo::MotionRange touchMinor; 1144 1145 bool haveToolSize; 1146 InputDeviceInfo::MotionRange toolMajor; 1147 InputDeviceInfo::MotionRange toolMinor; 1148 1149 bool haveOrientation; 1150 InputDeviceInfo::MotionRange orientation; 1151 1152 bool haveDistance; 1153 InputDeviceInfo::MotionRange distance; 1154 } mOrientedRanges; 1155 1156 // Oriented dimensions and precision. 1157 float mOrientedSurfaceWidth; 1158 float mOrientedSurfaceHeight; 1159 float mOrientedXPrecision; 1160 float mOrientedYPrecision; 1161 1162 struct CurrentVirtualKeyState { 1163 bool down; 1164 bool ignored; 1165 nsecs_t downTime; 1166 int32_t keyCode; 1167 int32_t scanCode; 1168 } mCurrentVirtualKey; 1169 1170 // Scale factor for gesture based pointer movements. 1171 float mPointerGestureXMovementScale; 1172 float mPointerGestureYMovementScale; 1173 1174 // Scale factor for gesture based zooming and other freeform motions. 1175 float mPointerGestureXZoomScale; 1176 float mPointerGestureYZoomScale; 1177 1178 // The maximum swipe width. 1179 float mPointerGestureMaxSwipeWidth; 1180 1181 struct PointerDistanceHeapElement { 1182 uint32_t currentPointerIndex : 8; 1183 uint32_t lastPointerIndex : 8; 1184 uint64_t distance : 48; // squared distance 1185 }; 1186 1187 struct PointerGesture { 1188 enum Mode { 1189 // No fingers, button is not pressed. 1190 // Nothing happening. 1191 NEUTRAL, 1192 1193 // No fingers, button is not pressed. 1194 // Tap detected. 1195 // Emits DOWN and UP events at the pointer location. 1196 TAP, 1197 1198 // Exactly one finger dragging following a tap. 1199 // Pointer follows the active finger. 1200 // Emits DOWN, MOVE and UP events at the pointer location. 1201 // 1202 // Detect double-taps when the finger goes up while in TAP_DRAG mode. 1203 TAP_DRAG, 1204 1205 // Button is pressed. 1206 // Pointer follows the active finger if there is one. Other fingers are ignored. 1207 // Emits DOWN, MOVE and UP events at the pointer location. 1208 BUTTON_CLICK_OR_DRAG, 1209 1210 // Exactly one finger, button is not pressed. 1211 // Pointer follows the active finger. 1212 // Emits HOVER_MOVE events at the pointer location. 1213 // 1214 // Detect taps when the finger goes up while in HOVER mode. 1215 HOVER, 1216 1217 // Exactly two fingers but neither have moved enough to clearly indicate 1218 // whether a swipe or freeform gesture was intended. We consider the 1219 // pointer to be pressed so this enables clicking or long-pressing on buttons. 1220 // Pointer does not move. 1221 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate. 1222 PRESS, 1223 1224 // Exactly two fingers moving in the same direction, button is not pressed. 1225 // Pointer does not move. 1226 // Emits DOWN, MOVE and UP events with a single pointer coordinate that 1227 // follows the midpoint between both fingers. 1228 SWIPE, 1229 1230 // Two or more fingers moving in arbitrary directions, button is not pressed. 1231 // Pointer does not move. 1232 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow 1233 // each finger individually relative to the initial centroid of the finger. 1234 FREEFORM, 1235 1236 // Waiting for quiet time to end before starting the next gesture. 1237 QUIET, 1238 }; 1239 1240 // Time the first finger went down. 1241 nsecs_t firstTouchTime; 1242 1243 // The active pointer id from the raw touch data. 1244 int32_t activeTouchId; // -1 if none 1245 1246 // The active pointer id from the gesture last delivered to the application. 1247 int32_t activeGestureId; // -1 if none 1248 1249 // Pointer coords and ids for the current and previous pointer gesture. 1250 Mode currentGestureMode; 1251 BitSet32 currentGestureIdBits; 1252 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1]; 1253 PointerProperties currentGestureProperties[MAX_POINTERS]; 1254 PointerCoords currentGestureCoords[MAX_POINTERS]; 1255 1256 Mode lastGestureMode; 1257 BitSet32 lastGestureIdBits; 1258 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1]; 1259 PointerProperties lastGestureProperties[MAX_POINTERS]; 1260 PointerCoords lastGestureCoords[MAX_POINTERS]; 1261 1262 // Time the pointer gesture last went down. 1263 nsecs_t downTime; 1264 1265 // Time when the pointer went down for a TAP. 1266 nsecs_t tapDownTime; 1267 1268 // Time when the pointer went up for a TAP. 1269 nsecs_t tapUpTime; 1270 1271 // Location of initial tap. 1272 float tapX, tapY; 1273 1274 // Time we started waiting for quiescence. 1275 nsecs_t quietTime; 1276 1277 // Reference points for multitouch gestures. 1278 float referenceTouchX; // reference touch X/Y coordinates in surface units 1279 float referenceTouchY; 1280 float referenceGestureX; // reference gesture X/Y coordinates in pixels 1281 float referenceGestureY; 1282 1283 // Distance that each pointer has traveled which has not yet been 1284 // subsumed into the reference gesture position. 1285 BitSet32 referenceIdBits; 1286 struct Delta { 1287 float dx, dy; 1288 }; 1289 Delta referenceDeltas[MAX_POINTER_ID + 1]; 1290 1291 // Describes how touch ids are mapped to gesture ids for freeform gestures. 1292 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1]; 1293 1294 // A velocity tracker for determining whether to switch active pointers during drags. 1295 VelocityTracker velocityTracker; 1296 1297 // Velocity control for pointer movements. 1298 VelocityControl pointerVelocityControl; 1299 1300 void reset() { 1301 firstTouchTime = LLONG_MIN; 1302 activeTouchId = -1; 1303 activeGestureId = -1; 1304 currentGestureMode = NEUTRAL; 1305 currentGestureIdBits.clear(); 1306 lastGestureMode = NEUTRAL; 1307 lastGestureIdBits.clear(); 1308 downTime = 0; 1309 velocityTracker.clear(); 1310 resetTap(); 1311 resetQuietTime(); 1312 pointerVelocityControl.reset(); 1313 } 1314 1315 void resetTap() { 1316 tapDownTime = LLONG_MIN; 1317 tapUpTime = LLONG_MIN; 1318 } 1319 1320 void resetQuietTime() { 1321 quietTime = LLONG_MIN; 1322 } 1323 } mPointerGesture; 1324 1325 void initialize(); 1326 1327 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags); 1328 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, 1329 int32_t keyEventAction, int32_t keyEventFlags); 1330 1331 void dispatchTouches(nsecs_t when, uint32_t policyFlags); 1332 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags); 1333 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags); 1334 void cookPointerData(); 1335 1336 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout); 1337 bool preparePointerGestures(nsecs_t when, 1338 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout); 1339 1340 // Dispatches a motion event. 1341 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the 1342 // method will take care of setting the index and transmuting the action to DOWN or UP 1343 // it is the first / last pointer to go down / up. 1344 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, 1345 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, 1346 int32_t edgeFlags, 1347 const PointerProperties* properties, const PointerCoords* coords, 1348 const uint32_t* idToIndex, BitSet32 idBits, 1349 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime); 1350 1351 // Updates pointer coords and properties for pointers with specified ids that have moved. 1352 // Returns true if any of them changed. 1353 bool updateMovedPointers(const PointerProperties* inProperties, 1354 const PointerCoords* inCoords, const uint32_t* inIdToIndex, 1355 PointerProperties* outProperties, PointerCoords* outCoords, 1356 const uint32_t* outIdToIndex, BitSet32 idBits) const; 1357 1358 bool isPointInsideSurface(int32_t x, int32_t y); 1359 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y); 1360 1361 void assignPointerIds(); 1362}; 1363 1364 1365class SingleTouchInputMapper : public TouchInputMapper { 1366public: 1367 SingleTouchInputMapper(InputDevice* device); 1368 virtual ~SingleTouchInputMapper(); 1369 1370 virtual void reset(); 1371 virtual void process(const RawEvent* rawEvent); 1372 1373protected: 1374 virtual void configureRawPointerAxes(); 1375 1376private: 1377 CursorButtonAccumulator mCursorButtonAccumulator; 1378 TouchButtonAccumulator mTouchButtonAccumulator; 1379 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator; 1380 1381 void clearState(); 1382 1383 void sync(nsecs_t when); 1384}; 1385 1386 1387class MultiTouchInputMapper : public TouchInputMapper { 1388public: 1389 MultiTouchInputMapper(InputDevice* device); 1390 virtual ~MultiTouchInputMapper(); 1391 1392 virtual void reset(); 1393 virtual void process(const RawEvent* rawEvent); 1394 1395protected: 1396 virtual void configureRawPointerAxes(); 1397 1398private: 1399 CursorButtonAccumulator mCursorButtonAccumulator; 1400 TouchButtonAccumulator mTouchButtonAccumulator; 1401 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator; 1402 1403 // Specifies the pointer id bits that are in use, and their associated tracking id. 1404 BitSet32 mPointerIdBits; 1405 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1]; 1406 1407 void clearState(); 1408 1409 void sync(nsecs_t when); 1410}; 1411 1412 1413class JoystickInputMapper : public InputMapper { 1414public: 1415 JoystickInputMapper(InputDevice* device); 1416 virtual ~JoystickInputMapper(); 1417 1418 virtual uint32_t getSources(); 1419 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); 1420 virtual void dump(String8& dump); 1421 virtual void configure(const InputReaderConfiguration* config, uint32_t changes); 1422 virtual void reset(); 1423 virtual void process(const RawEvent* rawEvent); 1424 1425private: 1426 struct Axis { 1427 RawAbsoluteAxisInfo rawAxisInfo; 1428 AxisInfo axisInfo; 1429 1430 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id 1431 1432 float scale; // scale factor from raw to normalized values 1433 float offset; // offset to add after scaling for normalization 1434 float highScale; // scale factor from raw to normalized values of high split 1435 float highOffset; // offset to add after scaling for normalization of high split 1436 1437 float min; // normalized inclusive minimum 1438 float max; // normalized inclusive maximum 1439 float flat; // normalized flat region size 1440 float fuzz; // normalized error tolerance 1441 1442 float filter; // filter out small variations of this size 1443 float currentValue; // current value 1444 float newValue; // most recent value 1445 float highCurrentValue; // current value of high split 1446 float highNewValue; // most recent value of high split 1447 1448 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo, 1449 bool explicitlyMapped, float scale, float offset, 1450 float highScale, float highOffset, 1451 float min, float max, float flat, float fuzz) { 1452 this->rawAxisInfo = rawAxisInfo; 1453 this->axisInfo = axisInfo; 1454 this->explicitlyMapped = explicitlyMapped; 1455 this->scale = scale; 1456 this->offset = offset; 1457 this->highScale = highScale; 1458 this->highOffset = highOffset; 1459 this->min = min; 1460 this->max = max; 1461 this->flat = flat; 1462 this->fuzz = fuzz; 1463 this->filter = 0; 1464 resetValue(); 1465 } 1466 1467 void resetValue() { 1468 this->currentValue = 0; 1469 this->newValue = 0; 1470 this->highCurrentValue = 0; 1471 this->highNewValue = 0; 1472 } 1473 }; 1474 1475 // Axes indexed by raw ABS_* axis index. 1476 KeyedVector<int32_t, Axis> mAxes; 1477 1478 void sync(nsecs_t when, bool force); 1479 1480 bool haveAxis(int32_t axisId); 1481 void pruneAxes(bool ignoreExplicitlyMappedAxes); 1482 bool filterAxes(bool force); 1483 1484 static bool hasValueChangedSignificantly(float filter, 1485 float newValue, float currentValue, float min, float max); 1486 static bool hasMovedNearerToValueWithinFilteredRange(float filter, 1487 float newValue, float currentValue, float thresholdValue); 1488 1489 static bool isCenteredAxis(int32_t axis); 1490}; 1491 1492} // namespace android 1493 1494#endif // _UI_INPUT_READER_H 1495