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