Input.h revision 8a90e6e3174083f274538567d851f98478fc83e9
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 _ANDROIDFW_INPUT_H 18#define _ANDROIDFW_INPUT_H 19 20/** 21 * Native input event structures. 22 */ 23 24#include <android/input.h> 25#include <utils/Vector.h> 26#include <utils/KeyedVector.h> 27#include <utils/Timers.h> 28#include <utils/RefBase.h> 29#include <utils/String8.h> 30 31#ifdef HAVE_ANDROID_OS 32class SkMatrix; 33#endif 34 35/* 36 * Additional private constants not defined in ndk/ui/input.h. 37 */ 38enum { 39 /* Private control to determine when an app is tracking a key sequence. */ 40 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000, 41 42 /* Key event is inconsistent with previously sent key events. */ 43 AKEY_EVENT_FLAG_TAINTED = 0x80000000, 44}; 45 46enum { 47 /* Motion event is inconsistent with previously sent motion events. */ 48 AMOTION_EVENT_FLAG_TAINTED = 0x80000000, 49}; 50 51enum { 52 /* 53 * Indicates that an input device has switches. 54 * This input source flag is hidden from the API because switches are only used by the system 55 * and applications have no way to interact with them. 56 */ 57 AINPUT_SOURCE_SWITCH = 0x80000000, 58}; 59 60/* 61 * SystemUiVisibility constants from View. 62 */ 63enum { 64 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0, 65 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001, 66}; 67 68/* 69 * Maximum number of pointers supported per motion event. 70 * Smallest number of pointers is 1. 71 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers 72 * will occasionally emit 11. There is not much harm making this constant bigger.) 73 */ 74#define MAX_POINTERS 16 75 76/* 77 * Maximum pointer id value supported in a motion event. 78 * Smallest pointer id is 0. 79 * (This is limited by our use of BitSet32 to track pointer assignments.) 80 */ 81#define MAX_POINTER_ID 31 82 83/* 84 * Declare a concrete type for the NDK's input event forward declaration. 85 */ 86struct AInputEvent { 87 virtual ~AInputEvent() { } 88}; 89 90/* 91 * Declare a concrete type for the NDK's input device forward declaration. 92 */ 93struct AInputDevice { 94 virtual ~AInputDevice() { } 95}; 96 97 98namespace android { 99 100#ifdef HAVE_ANDROID_OS 101class Parcel; 102#endif 103 104/* 105 * Flags that flow alongside events in the input dispatch system to help with certain 106 * policy decisions such as waking from device sleep. 107 * 108 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java. 109 */ 110enum { 111 /* These flags originate in RawEvents and are generally set in the key map. 112 * NOTE: If you edit these flags, also edit labels in KeycodeLabels.h. */ 113 114 POLICY_FLAG_WAKE = 0x00000001, 115 POLICY_FLAG_WAKE_DROPPED = 0x00000002, 116 POLICY_FLAG_SHIFT = 0x00000004, 117 POLICY_FLAG_CAPS_LOCK = 0x00000008, 118 POLICY_FLAG_ALT = 0x00000010, 119 POLICY_FLAG_ALT_GR = 0x00000020, 120 POLICY_FLAG_MENU = 0x00000040, 121 POLICY_FLAG_LAUNCHER = 0x00000080, 122 POLICY_FLAG_VIRTUAL = 0x00000100, 123 POLICY_FLAG_FUNCTION = 0x00000200, 124 125 POLICY_FLAG_RAW_MASK = 0x0000ffff, 126 127 /* These flags are set by the input dispatcher. */ 128 129 // Indicates that the input event was injected. 130 POLICY_FLAG_INJECTED = 0x01000000, 131 132 // Indicates that the input event is from a trusted source such as a directly attached 133 // input device or an application with system-wide event injection permission. 134 POLICY_FLAG_TRUSTED = 0x02000000, 135 136 // Indicates that the input event has passed through an input filter. 137 POLICY_FLAG_FILTERED = 0x04000000, 138 139 // Disables automatic key repeating behavior. 140 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000, 141 142 /* These flags are set by the input reader policy as it intercepts each event. */ 143 144 // Indicates that the screen was off when the event was received and the event 145 // should wake the device. 146 POLICY_FLAG_WOKE_HERE = 0x10000000, 147 148 // Indicates that the screen was dim when the event was received and the event 149 // should brighten the device. 150 POLICY_FLAG_BRIGHT_HERE = 0x20000000, 151 152 // Indicates that the event should be dispatched to applications. 153 // The input event should still be sent to the InputDispatcher so that it can see all 154 // input events received include those that it will not deliver. 155 POLICY_FLAG_PASS_TO_USER = 0x40000000, 156}; 157 158/* 159 * Pointer coordinate data. 160 */ 161struct PointerCoords { 162 enum { MAX_AXES = 14 }; // 14 so that sizeof(PointerCoords) == 64 163 164 // Bitfield of axes that are present in this structure. 165 uint64_t bits; 166 167 // Values of axes that are stored in this structure packed in order by axis id 168 // for each axis that is present in the structure according to 'bits'. 169 float values[MAX_AXES]; 170 171 inline void clear() { 172 bits = 0; 173 } 174 175 float getAxisValue(int32_t axis) const; 176 status_t setAxisValue(int32_t axis, float value); 177 178 void scale(float scale); 179 void lerp(const PointerCoords& a, const PointerCoords& b, float alpha); 180 181 inline float getX() const { 182 return getAxisValue(AMOTION_EVENT_AXIS_X); 183 } 184 185 inline float getY() const { 186 return getAxisValue(AMOTION_EVENT_AXIS_Y); 187 } 188 189#ifdef HAVE_ANDROID_OS 190 status_t readFromParcel(Parcel* parcel); 191 status_t writeToParcel(Parcel* parcel) const; 192#endif 193 194 bool operator==(const PointerCoords& other) const; 195 inline bool operator!=(const PointerCoords& other) const { 196 return !(*this == other); 197 } 198 199 void copyFrom(const PointerCoords& other); 200 201private: 202 void tooManyAxes(int axis); 203}; 204 205/* 206 * Pointer property data. 207 */ 208struct PointerProperties { 209 // The id of the pointer. 210 int32_t id; 211 212 // The pointer tool type. 213 int32_t toolType; 214 215 inline void clear() { 216 id = -1; 217 toolType = 0; 218 } 219 220 bool operator==(const PointerProperties& other) const; 221 inline bool operator!=(const PointerProperties& other) const { 222 return !(*this == other); 223 } 224 225 void copyFrom(const PointerProperties& other); 226}; 227 228/* 229 * Input events. 230 */ 231class InputEvent : public AInputEvent { 232public: 233 virtual ~InputEvent() { } 234 235 virtual int32_t getType() const = 0; 236 237 inline int32_t getDeviceId() const { return mDeviceId; } 238 239 inline int32_t getSource() const { return mSource; } 240 241 inline void setSource(int32_t source) { mSource = source; } 242 243protected: 244 void initialize(int32_t deviceId, int32_t source); 245 void initialize(const InputEvent& from); 246 247 int32_t mDeviceId; 248 int32_t mSource; 249}; 250 251/* 252 * Key events. 253 */ 254class KeyEvent : public InputEvent { 255public: 256 virtual ~KeyEvent() { } 257 258 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; } 259 260 inline int32_t getAction() const { return mAction; } 261 262 inline int32_t getFlags() const { return mFlags; } 263 264 inline void setFlags(int32_t flags) { mFlags = flags; } 265 266 inline int32_t getKeyCode() const { return mKeyCode; } 267 268 inline int32_t getScanCode() const { return mScanCode; } 269 270 inline int32_t getMetaState() const { return mMetaState; } 271 272 inline int32_t getRepeatCount() const { return mRepeatCount; } 273 274 inline nsecs_t getDownTime() const { return mDownTime; } 275 276 inline nsecs_t getEventTime() const { return mEventTime; } 277 278 // Return true if this event may have a default action implementation. 279 static bool hasDefaultAction(int32_t keyCode); 280 bool hasDefaultAction() const; 281 282 // Return true if this event represents a system key. 283 static bool isSystemKey(int32_t keyCode); 284 bool isSystemKey() const; 285 286 void initialize( 287 int32_t deviceId, 288 int32_t source, 289 int32_t action, 290 int32_t flags, 291 int32_t keyCode, 292 int32_t scanCode, 293 int32_t metaState, 294 int32_t repeatCount, 295 nsecs_t downTime, 296 nsecs_t eventTime); 297 void initialize(const KeyEvent& from); 298 299protected: 300 int32_t mAction; 301 int32_t mFlags; 302 int32_t mKeyCode; 303 int32_t mScanCode; 304 int32_t mMetaState; 305 int32_t mRepeatCount; 306 nsecs_t mDownTime; 307 nsecs_t mEventTime; 308}; 309 310/* 311 * Motion events. 312 */ 313class MotionEvent : public InputEvent { 314public: 315 virtual ~MotionEvent() { } 316 317 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; } 318 319 inline int32_t getAction() const { return mAction; } 320 321 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; } 322 323 inline int32_t getActionIndex() const { 324 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) 325 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; 326 } 327 328 inline void setAction(int32_t action) { mAction = action; } 329 330 inline int32_t getFlags() const { return mFlags; } 331 332 inline void setFlags(int32_t flags) { mFlags = flags; } 333 334 inline int32_t getEdgeFlags() const { return mEdgeFlags; } 335 336 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } 337 338 inline int32_t getMetaState() const { return mMetaState; } 339 340 inline void setMetaState(int32_t metaState) { mMetaState = metaState; } 341 342 inline int32_t getButtonState() const { return mButtonState; } 343 344 inline float getXOffset() const { return mXOffset; } 345 346 inline float getYOffset() const { return mYOffset; } 347 348 inline float getXPrecision() const { return mXPrecision; } 349 350 inline float getYPrecision() const { return mYPrecision; } 351 352 inline nsecs_t getDownTime() const { return mDownTime; } 353 354 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } 355 356 inline size_t getPointerCount() const { return mPointerProperties.size(); } 357 358 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { 359 return &mPointerProperties[pointerIndex]; 360 } 361 362 inline int32_t getPointerId(size_t pointerIndex) const { 363 return mPointerProperties[pointerIndex].id; 364 } 365 366 inline int32_t getToolType(size_t pointerIndex) const { 367 return mPointerProperties[pointerIndex].toolType; 368 } 369 370 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } 371 372 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; 373 374 float getRawAxisValue(int32_t axis, size_t pointerIndex) const; 375 376 inline float getRawX(size_t pointerIndex) const { 377 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 378 } 379 380 inline float getRawY(size_t pointerIndex) const { 381 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 382 } 383 384 float getAxisValue(int32_t axis, size_t pointerIndex) const; 385 386 inline float getX(size_t pointerIndex) const { 387 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 388 } 389 390 inline float getY(size_t pointerIndex) const { 391 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 392 } 393 394 inline float getPressure(size_t pointerIndex) const { 395 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); 396 } 397 398 inline float getSize(size_t pointerIndex) const { 399 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); 400 } 401 402 inline float getTouchMajor(size_t pointerIndex) const { 403 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); 404 } 405 406 inline float getTouchMinor(size_t pointerIndex) const { 407 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); 408 } 409 410 inline float getToolMajor(size_t pointerIndex) const { 411 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); 412 } 413 414 inline float getToolMinor(size_t pointerIndex) const { 415 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); 416 } 417 418 inline float getOrientation(size_t pointerIndex) const { 419 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); 420 } 421 422 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } 423 424 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { 425 return mSampleEventTimes[historicalIndex]; 426 } 427 428 const PointerCoords* getHistoricalRawPointerCoords( 429 size_t pointerIndex, size_t historicalIndex) const; 430 431 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, 432 size_t historicalIndex) const; 433 434 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { 435 return getHistoricalRawAxisValue( 436 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 437 } 438 439 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { 440 return getHistoricalRawAxisValue( 441 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 442 } 443 444 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; 445 446 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { 447 return getHistoricalAxisValue( 448 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 449 } 450 451 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { 452 return getHistoricalAxisValue( 453 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 454 } 455 456 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { 457 return getHistoricalAxisValue( 458 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); 459 } 460 461 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { 462 return getHistoricalAxisValue( 463 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); 464 } 465 466 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { 467 return getHistoricalAxisValue( 468 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); 469 } 470 471 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { 472 return getHistoricalAxisValue( 473 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); 474 } 475 476 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { 477 return getHistoricalAxisValue( 478 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); 479 } 480 481 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { 482 return getHistoricalAxisValue( 483 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); 484 } 485 486 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { 487 return getHistoricalAxisValue( 488 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); 489 } 490 491 ssize_t findPointerIndex(int32_t pointerId) const; 492 493 void initialize( 494 int32_t deviceId, 495 int32_t source, 496 int32_t action, 497 int32_t flags, 498 int32_t edgeFlags, 499 int32_t metaState, 500 int32_t buttonState, 501 float xOffset, 502 float yOffset, 503 float xPrecision, 504 float yPrecision, 505 nsecs_t downTime, 506 nsecs_t eventTime, 507 size_t pointerCount, 508 const PointerProperties* pointerProperties, 509 const PointerCoords* pointerCoords); 510 511 void copyFrom(const MotionEvent* other, bool keepHistory); 512 513 void addSample( 514 nsecs_t eventTime, 515 const PointerCoords* pointerCoords); 516 517 void offsetLocation(float xOffset, float yOffset); 518 519 void scale(float scaleFactor); 520 521#ifdef HAVE_ANDROID_OS 522 void transform(const SkMatrix* matrix); 523 524 status_t readFromParcel(Parcel* parcel); 525 status_t writeToParcel(Parcel* parcel) const; 526#endif 527 528 static bool isTouchEvent(int32_t source, int32_t action); 529 inline bool isTouchEvent() const { 530 return isTouchEvent(mSource, mAction); 531 } 532 533 // Low-level accessors. 534 inline const PointerProperties* getPointerProperties() const { 535 return mPointerProperties.array(); 536 } 537 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } 538 inline const PointerCoords* getSamplePointerCoords() const { 539 return mSamplePointerCoords.array(); 540 } 541 542protected: 543 int32_t mAction; 544 int32_t mFlags; 545 int32_t mEdgeFlags; 546 int32_t mMetaState; 547 int32_t mButtonState; 548 float mXOffset; 549 float mYOffset; 550 float mXPrecision; 551 float mYPrecision; 552 nsecs_t mDownTime; 553 Vector<PointerProperties> mPointerProperties; 554 Vector<nsecs_t> mSampleEventTimes; 555 Vector<PointerCoords> mSamplePointerCoords; 556}; 557 558/* 559 * Input event factory. 560 */ 561class InputEventFactoryInterface { 562protected: 563 virtual ~InputEventFactoryInterface() { } 564 565public: 566 InputEventFactoryInterface() { } 567 568 virtual KeyEvent* createKeyEvent() = 0; 569 virtual MotionEvent* createMotionEvent() = 0; 570}; 571 572/* 573 * A simple input event factory implementation that uses a single preallocated instance 574 * of each type of input event that are reused for each request. 575 */ 576class PreallocatedInputEventFactory : public InputEventFactoryInterface { 577public: 578 PreallocatedInputEventFactory() { } 579 virtual ~PreallocatedInputEventFactory() { } 580 581 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } 582 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } 583 584private: 585 KeyEvent mKeyEvent; 586 MotionEvent mMotionEvent; 587}; 588 589/* 590 * An input event factory implementation that maintains a pool of input events. 591 */ 592class PooledInputEventFactory : public InputEventFactoryInterface { 593public: 594 PooledInputEventFactory(size_t maxPoolSize = 20); 595 virtual ~PooledInputEventFactory(); 596 597 virtual KeyEvent* createKeyEvent(); 598 virtual MotionEvent* createMotionEvent(); 599 600 void recycle(InputEvent* event); 601 602private: 603 const size_t mMaxPoolSize; 604 605 Vector<KeyEvent*> mKeyEventPool; 606 Vector<MotionEvent*> mMotionEventPool; 607}; 608 609} // namespace android 610 611#endif // _ANDROIDFW_INPUT_H 612