2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _LIBINPUT_INPUT_H
18 #define _LIBINPUT_INPUT_H
21 * Native input event structures.
24 #include <android/input.h>
25 #include <utils/BitSet.h>
26 #include <utils/KeyedVector.h>
27 #include <utils/RefBase.h>
28 #include <utils/String8.h>
29 #include <utils/Timers.h>
30 #include <utils/Vector.h>
34 * Additional private constants not defined in ndk/ui/input.h.
37 /* Signifies that the key is being predispatched */
38 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
40 /* Private control to determine when an app is tracking a key sequence. */
41 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
43 /* Key event is inconsistent with previously sent key events. */
44 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
50 * This flag indicates that the window that received this motion event is partly
51 * or wholly obscured by another visible window above it. This flag is set to true
52 * even if the event did not directly pass through the obscured area.
53 * A security sensitive application can check this flag to identify situations in which
54 * a malicious application may have covered up part of its content for the purpose
55 * of misleading the user or hijacking touches. An appropriate response might be
56 * to drop the suspect touches or to take additional precautions to confirm the user's
59 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2,
61 /* Motion event is inconsistent with previously sent motion events. */
62 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
66 /* Used when a motion event is not associated with any display.
67 * Typically used for non-pointer events. */
68 ADISPLAY_ID_NONE = -1,
70 /* The default display id. */
71 ADISPLAY_ID_DEFAULT = 0,
76 * Indicates that an input device has switches.
77 * This input source flag is hidden from the API because switches are only used by the system
78 * and applications have no way to interact with them.
80 AINPUT_SOURCE_SWITCH = 0x80000000,
85 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
86 * with LEDs to developers
88 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
92 ALED_CAPS_LOCK = 0x01,
93 ALED_SCROLL_LOCK = 0x02,
101 ALED_CHARGING = 0x0a,
102 ALED_CONTROLLER_1 = 0x10,
103 ALED_CONTROLLER_2 = 0x11,
104 ALED_CONTROLLER_3 = 0x12,
105 ALED_CONTROLLER_4 = 0x13,
108 /* Maximum number of controller LEDs we support */
109 #define MAX_CONTROLLER_LEDS 4
112 * SystemUiVisibility constants from View.
115 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
116 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
120 * Maximum number of pointers supported per motion event.
121 * Smallest number of pointers is 1.
122 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
123 * will occasionally emit 11. There is not much harm making this constant bigger.)
125 #define MAX_POINTERS 16
128 * Maximum number of samples supported per motion event.
130 #define MAX_SAMPLES UINT16_MAX
133 * Maximum pointer id value supported in a motion event.
134 * Smallest pointer id is 0.
135 * (This is limited by our use of BitSet32 to track pointer assignments.)
137 #define MAX_POINTER_ID 31
140 * Declare a concrete type for the NDK's input event forward declaration.
143 virtual ~AInputEvent() { }
147 * Declare a concrete type for the NDK's input device forward declaration.
149 struct AInputDevice {
150 virtual ~AInputDevice() { }
156 #ifdef HAVE_ANDROID_OS
161 * Flags that flow alongside events in the input dispatch system to help with certain
162 * policy decisions such as waking from device sleep.
164 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
167 /* These flags originate in RawEvents and are generally set in the key map.
168 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
169 * InputEventLabels.h as well. */
171 // Indicates that the event should wake the device.
172 POLICY_FLAG_WAKE = 0x00000001,
174 // Indicates that the key is virtual, such as a capacitive button, and should
175 // generate haptic feedback. Virtual keys may be suppressed for some time
176 // after a recent touch to prevent accidental activation of virtual keys adjacent
177 // to the touch screen during an edge swipe.
178 POLICY_FLAG_VIRTUAL = 0x00000002,
180 // Indicates that the key is the special function modifier.
181 POLICY_FLAG_FUNCTION = 0x00000004,
183 // Indicates that the key represents a special gesture that has been detected by
184 // the touch firmware or driver. Causes touch events from the same device to be canceled.
185 POLICY_FLAG_GESTURE = 0x00000008,
187 POLICY_FLAG_RAW_MASK = 0x0000ffff,
189 /* These flags are set by the input dispatcher. */
191 // Indicates that the input event was injected.
192 POLICY_FLAG_INJECTED = 0x01000000,
194 // Indicates that the input event is from a trusted source such as a directly attached
195 // input device or an application with system-wide event injection permission.
196 POLICY_FLAG_TRUSTED = 0x02000000,
198 // Indicates that the input event has passed through an input filter.
199 POLICY_FLAG_FILTERED = 0x04000000,
201 // Disables automatic key repeating behavior.
202 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
204 /* These flags are set by the input reader policy as it intercepts each event. */
206 // Indicates that the device was in an interactive state when the
207 // event was intercepted.
208 POLICY_FLAG_INTERACTIVE = 0x20000000,
210 // Indicates that the event should be dispatched to applications.
211 // The input event should still be sent to the InputDispatcher so that it can see all
212 // input events received include those that it will not deliver.
213 POLICY_FLAG_PASS_TO_USER = 0x40000000,
217 * Pointer coordinate data.
219 struct PointerCoords {
220 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
222 // Bitfield of axes that are present in this structure.
223 uint64_t bits __attribute__((aligned(8)));
225 // Values of axes that are stored in this structure packed in order by axis id
226 // for each axis that is present in the structure according to 'bits'.
227 float values[MAX_AXES];
229 inline void clear() {
230 BitSet64::clear(bits);
233 bool isEmpty() const {
234 return BitSet64::isEmpty(bits);
237 float getAxisValue(int32_t axis) const;
238 status_t setAxisValue(int32_t axis, float value);
240 void scale(float scale);
241 void applyOffset(float xOffset, float yOffset);
243 inline float getX() const {
244 return getAxisValue(AMOTION_EVENT_AXIS_X);
247 inline float getY() const {
248 return getAxisValue(AMOTION_EVENT_AXIS_Y);
251 #ifdef HAVE_ANDROID_OS
252 status_t readFromParcel(Parcel* parcel);
253 status_t writeToParcel(Parcel* parcel) const;
256 bool operator==(const PointerCoords& other) const;
257 inline bool operator!=(const PointerCoords& other) const {
258 return !(*this == other);
261 void copyFrom(const PointerCoords& other);
264 void tooManyAxes(int axis);
268 * Pointer property data.
270 struct PointerProperties {
271 // The id of the pointer.
274 // The pointer tool type.
277 inline void clear() {
282 bool operator==(const PointerProperties& other) const;
283 inline bool operator!=(const PointerProperties& other) const {
284 return !(*this == other);
287 void copyFrom(const PointerProperties& other);
293 class InputEvent : public AInputEvent {
295 virtual ~InputEvent() { }
297 virtual int32_t getType() const = 0;
299 inline int32_t getDeviceId() const { return mDeviceId; }
301 inline int32_t getSource() const { return mSource; }
303 inline void setSource(int32_t source) { mSource = source; }
306 void initialize(int32_t deviceId, int32_t source);
307 void initialize(const InputEvent& from);
316 class KeyEvent : public InputEvent {
318 virtual ~KeyEvent() { }
320 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
322 inline int32_t getAction() const { return mAction; }
324 inline int32_t getFlags() const { return mFlags; }
326 inline void setFlags(int32_t flags) { mFlags = flags; }
328 inline int32_t getKeyCode() const { return mKeyCode; }
330 inline int32_t getScanCode() const { return mScanCode; }
332 inline int32_t getMetaState() const { return mMetaState; }
334 inline int32_t getRepeatCount() const { return mRepeatCount; }
336 inline nsecs_t getDownTime() const { return mDownTime; }
338 inline nsecs_t getEventTime() const { return mEventTime; }
340 static const char* getLabel(int32_t keyCode);
341 static int32_t getKeyCodeFromLabel(const char* label);
354 void initialize(const KeyEvent& from);
362 int32_t mRepeatCount;
370 class MotionEvent : public InputEvent {
372 virtual ~MotionEvent() { }
374 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
376 inline int32_t getAction() const { return mAction; }
378 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
380 inline int32_t getActionIndex() const {
381 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
382 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
385 inline void setAction(int32_t action) { mAction = action; }
387 inline int32_t getFlags() const { return mFlags; }
389 inline void setFlags(int32_t flags) { mFlags = flags; }
391 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
393 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
395 inline int32_t getMetaState() const { return mMetaState; }
397 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
399 inline int32_t getButtonState() const { return mButtonState; }
401 inline int32_t setButtonState(int32_t buttonState) { mButtonState = buttonState; }
403 inline int32_t getActionButton() const { return mActionButton; }
405 inline void setActionButton(int32_t button) { mActionButton = button; }
407 inline float getXOffset() const { return mXOffset; }
409 inline float getYOffset() const { return mYOffset; }
411 inline float getXPrecision() const { return mXPrecision; }
413 inline float getYPrecision() const { return mYPrecision; }
415 inline nsecs_t getDownTime() const { return mDownTime; }
417 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
419 inline size_t getPointerCount() const { return mPointerProperties.size(); }
421 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
422 return &mPointerProperties[pointerIndex];
425 inline int32_t getPointerId(size_t pointerIndex) const {
426 return mPointerProperties[pointerIndex].id;
429 inline int32_t getToolType(size_t pointerIndex) const {
430 return mPointerProperties[pointerIndex].toolType;
433 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
435 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
437 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
439 inline float getRawX(size_t pointerIndex) const {
440 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
443 inline float getRawY(size_t pointerIndex) const {
444 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
447 float getAxisValue(int32_t axis, size_t pointerIndex) const;
449 inline float getX(size_t pointerIndex) const {
450 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
453 inline float getY(size_t pointerIndex) const {
454 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
457 inline float getPressure(size_t pointerIndex) const {
458 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
461 inline float getSize(size_t pointerIndex) const {
462 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
465 inline float getTouchMajor(size_t pointerIndex) const {
466 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
469 inline float getTouchMinor(size_t pointerIndex) const {
470 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
473 inline float getToolMajor(size_t pointerIndex) const {
474 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
477 inline float getToolMinor(size_t pointerIndex) const {
478 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
481 inline float getOrientation(size_t pointerIndex) const {
482 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
485 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
487 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
488 return mSampleEventTimes[historicalIndex];
491 const PointerCoords* getHistoricalRawPointerCoords(
492 size_t pointerIndex, size_t historicalIndex) const;
494 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
495 size_t historicalIndex) const;
497 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
498 return getHistoricalRawAxisValue(
499 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
502 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
503 return getHistoricalRawAxisValue(
504 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
507 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
509 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
510 return getHistoricalAxisValue(
511 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
514 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
515 return getHistoricalAxisValue(
516 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
519 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
520 return getHistoricalAxisValue(
521 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
524 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
525 return getHistoricalAxisValue(
526 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
529 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
530 return getHistoricalAxisValue(
531 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
534 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
535 return getHistoricalAxisValue(
536 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
539 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
540 return getHistoricalAxisValue(
541 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
544 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
545 return getHistoricalAxisValue(
546 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
549 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
550 return getHistoricalAxisValue(
551 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
554 ssize_t findPointerIndex(int32_t pointerId) const;
560 int32_t actionButton,
572 const PointerProperties* pointerProperties,
573 const PointerCoords* pointerCoords);
575 void copyFrom(const MotionEvent* other, bool keepHistory);
579 const PointerCoords* pointerCoords);
581 void offsetLocation(float xOffset, float yOffset);
583 void scale(float scaleFactor);
585 // Apply 3x3 perspective matrix transformation.
586 // Matrix is in row-major form and compatible with SkMatrix.
587 void transform(const float matrix[9]);
589 #ifdef HAVE_ANDROID_OS
590 status_t readFromParcel(Parcel* parcel);
591 status_t writeToParcel(Parcel* parcel) const;
594 static bool isTouchEvent(int32_t source, int32_t action);
595 inline bool isTouchEvent() const {
596 return isTouchEvent(mSource, mAction);
599 // Low-level accessors.
600 inline const PointerProperties* getPointerProperties() const {
601 return mPointerProperties.array();
603 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
604 inline const PointerCoords* getSamplePointerCoords() const {
605 return mSamplePointerCoords.array();
608 static const char* getLabel(int32_t axis);
609 static int32_t getAxisFromLabel(const char* label);
613 int32_t mActionButton;
617 int32_t mButtonState;
623 Vector<PointerProperties> mPointerProperties;
624 Vector<nsecs_t> mSampleEventTimes;
625 Vector<PointerCoords> mSamplePointerCoords;
629 * Input event factory.
631 class InputEventFactoryInterface {
633 virtual ~InputEventFactoryInterface() { }
636 InputEventFactoryInterface() { }
638 virtual KeyEvent* createKeyEvent() = 0;
639 virtual MotionEvent* createMotionEvent() = 0;
643 * A simple input event factory implementation that uses a single preallocated instance
644 * of each type of input event that are reused for each request.
646 class PreallocatedInputEventFactory : public InputEventFactoryInterface {
648 PreallocatedInputEventFactory() { }
649 virtual ~PreallocatedInputEventFactory() { }
651 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
652 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
656 MotionEvent mMotionEvent;
660 * An input event factory implementation that maintains a pool of input events.
662 class PooledInputEventFactory : public InputEventFactoryInterface {
664 PooledInputEventFactory(size_t maxPoolSize = 20);
665 virtual ~PooledInputEventFactory();
667 virtual KeyEvent* createKeyEvent();
668 virtual MotionEvent* createMotionEvent();
670 void recycle(InputEvent* event);
673 const size_t mMaxPoolSize;
675 Vector<KeyEvent*> mKeyEventPool;
676 Vector<MotionEvent*> mMotionEventPool;
679 } // namespace android
681 #endif // _LIBINPUT_INPUT_H