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>
33 * Additional private constants not defined in ndk/ui/input.h.
36 /* Signifies that the key is being predispatched */
37 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
39 /* Private control to determine when an app is tracking a key sequence. */
40 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
42 /* Key event is inconsistent with previously sent key events. */
43 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
47 /* Motion event is inconsistent with previously sent motion events. */
48 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
52 /* Used when a motion event is not associated with any display.
53 * Typically used for non-pointer events. */
54 ADISPLAY_ID_NONE = -1,
56 /* The default display id. */
57 ADISPLAY_ID_DEFAULT = 0,
62 * Indicates that an input device has switches.
63 * This input source flag is hidden from the API because switches are only used by the system
64 * and applications have no way to interact with them.
66 AINPUT_SOURCE_SWITCH = 0x80000000,
71 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
72 * with LEDs to developers
74 * NOTE: If you add LEDs here, you must also add them to KeycodeLabels.h
78 ALED_CAPS_LOCK = 0x01,
79 ALED_SCROLL_LOCK = 0x02,
88 ALED_CONTROLLER_1 = 0x10,
89 ALED_CONTROLLER_2 = 0x11,
90 ALED_CONTROLLER_3 = 0x12,
91 ALED_CONTROLLER_4 = 0x13,
94 /* Maximum number of controller LEDs we support */
95 #define MAX_CONTROLLER_LEDS 4
98 * SystemUiVisibility constants from View.
101 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
102 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
106 * Maximum number of pointers supported per motion event.
107 * Smallest number of pointers is 1.
108 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
109 * will occasionally emit 11. There is not much harm making this constant bigger.)
111 #define MAX_POINTERS 16
114 * Maximum pointer id value supported in a motion event.
115 * Smallest pointer id is 0.
116 * (This is limited by our use of BitSet32 to track pointer assignments.)
118 #define MAX_POINTER_ID 31
121 * Declare a concrete type for the NDK's input event forward declaration.
124 virtual ~AInputEvent() { }
128 * Declare a concrete type for the NDK's input device forward declaration.
130 struct AInputDevice {
131 virtual ~AInputDevice() { }
137 #ifdef HAVE_ANDROID_OS
142 * Flags that flow alongside events in the input dispatch system to help with certain
143 * policy decisions such as waking from device sleep.
145 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
148 /* These flags originate in RawEvents and are generally set in the key map.
149 * NOTE: If you edit these flags, also edit labels in KeycodeLabels.h. */
151 POLICY_FLAG_WAKE = 0x00000001,
152 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
153 POLICY_FLAG_SHIFT = 0x00000004,
154 POLICY_FLAG_CAPS_LOCK = 0x00000008,
155 POLICY_FLAG_ALT = 0x00000010,
156 POLICY_FLAG_ALT_GR = 0x00000020,
157 POLICY_FLAG_MENU = 0x00000040,
158 POLICY_FLAG_LAUNCHER = 0x00000080,
159 POLICY_FLAG_VIRTUAL = 0x00000100,
160 POLICY_FLAG_FUNCTION = 0x00000200,
162 POLICY_FLAG_RAW_MASK = 0x0000ffff,
164 /* These flags are set by the input dispatcher. */
166 // Indicates that the input event was injected.
167 POLICY_FLAG_INJECTED = 0x01000000,
169 // Indicates that the input event is from a trusted source such as a directly attached
170 // input device or an application with system-wide event injection permission.
171 POLICY_FLAG_TRUSTED = 0x02000000,
173 // Indicates that the input event has passed through an input filter.
174 POLICY_FLAG_FILTERED = 0x04000000,
176 // Disables automatic key repeating behavior.
177 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
179 /* These flags are set by the input reader policy as it intercepts each event. */
181 // Indicates that the device was in an interactive state when the
182 // event was intercepted.
183 POLICY_FLAG_INTERACTIVE = 0x20000000,
185 // Indicates that the event should be dispatched to applications.
186 // The input event should still be sent to the InputDispatcher so that it can see all
187 // input events received include those that it will not deliver.
188 POLICY_FLAG_PASS_TO_USER = 0x40000000,
192 * Pointer coordinate data.
194 struct PointerCoords {
195 enum { MAX_AXES = 14 }; // 14 so that sizeof(PointerCoords) == 64
197 // Bitfield of axes that are present in this structure.
200 // Values of axes that are stored in this structure packed in order by axis id
201 // for each axis that is present in the structure according to 'bits'.
202 float values[MAX_AXES];
204 inline void clear() {
205 BitSet64::clear(bits);
208 bool isEmpty() const {
209 return BitSet64::isEmpty(bits);
212 float getAxisValue(int32_t axis) const;
213 status_t setAxisValue(int32_t axis, float value);
215 void scale(float scale);
216 void applyOffset(float xOffset, float yOffset);
218 inline float getX() const {
219 return getAxisValue(AMOTION_EVENT_AXIS_X);
222 inline float getY() const {
223 return getAxisValue(AMOTION_EVENT_AXIS_Y);
226 #ifdef HAVE_ANDROID_OS
227 status_t readFromParcel(Parcel* parcel);
228 status_t writeToParcel(Parcel* parcel) const;
231 bool operator==(const PointerCoords& other) const;
232 inline bool operator!=(const PointerCoords& other) const {
233 return !(*this == other);
236 void copyFrom(const PointerCoords& other);
239 void tooManyAxes(int axis);
243 * Pointer property data.
245 struct PointerProperties {
246 // The id of the pointer.
249 // The pointer tool type.
252 inline void clear() {
257 bool operator==(const PointerProperties& other) const;
258 inline bool operator!=(const PointerProperties& other) const {
259 return !(*this == other);
262 void copyFrom(const PointerProperties& other);
268 class InputEvent : public AInputEvent {
270 virtual ~InputEvent() { }
272 virtual int32_t getType() const = 0;
274 inline int32_t getDeviceId() const { return mDeviceId; }
276 inline int32_t getSource() const { return mSource; }
278 inline void setSource(int32_t source) { mSource = source; }
281 void initialize(int32_t deviceId, int32_t source);
282 void initialize(const InputEvent& from);
291 class KeyEvent : public InputEvent {
293 virtual ~KeyEvent() { }
295 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
297 inline int32_t getAction() const { return mAction; }
299 inline int32_t getFlags() const { return mFlags; }
301 inline void setFlags(int32_t flags) { mFlags = flags; }
303 inline int32_t getKeyCode() const { return mKeyCode; }
305 inline int32_t getScanCode() const { return mScanCode; }
307 inline int32_t getMetaState() const { return mMetaState; }
309 inline int32_t getRepeatCount() const { return mRepeatCount; }
311 inline nsecs_t getDownTime() const { return mDownTime; }
313 inline nsecs_t getEventTime() const { return mEventTime; }
315 // Return true if this event may have a default action implementation.
316 static bool hasDefaultAction(int32_t keyCode);
317 bool hasDefaultAction() const;
319 // Return true if this event represents a system key.
320 static bool isSystemKey(int32_t keyCode);
321 bool isSystemKey() const;
334 void initialize(const KeyEvent& from);
342 int32_t mRepeatCount;
350 class MotionEvent : public InputEvent {
352 virtual ~MotionEvent() { }
354 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
356 inline int32_t getAction() const { return mAction; }
358 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
360 inline int32_t getActionIndex() const {
361 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
362 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
365 inline void setAction(int32_t action) { mAction = action; }
367 inline int32_t getFlags() const { return mFlags; }
369 inline void setFlags(int32_t flags) { mFlags = flags; }
371 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
373 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
375 inline int32_t getMetaState() const { return mMetaState; }
377 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
379 inline int32_t getButtonState() const { return mButtonState; }
381 inline float getXOffset() const { return mXOffset; }
383 inline float getYOffset() const { return mYOffset; }
385 inline float getXPrecision() const { return mXPrecision; }
387 inline float getYPrecision() const { return mYPrecision; }
389 inline nsecs_t getDownTime() const { return mDownTime; }
391 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
393 inline size_t getPointerCount() const { return mPointerProperties.size(); }
395 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
396 return &mPointerProperties[pointerIndex];
399 inline int32_t getPointerId(size_t pointerIndex) const {
400 return mPointerProperties[pointerIndex].id;
403 inline int32_t getToolType(size_t pointerIndex) const {
404 return mPointerProperties[pointerIndex].toolType;
407 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
409 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
411 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
413 inline float getRawX(size_t pointerIndex) const {
414 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
417 inline float getRawY(size_t pointerIndex) const {
418 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
421 float getAxisValue(int32_t axis, size_t pointerIndex) const;
423 inline float getX(size_t pointerIndex) const {
424 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
427 inline float getY(size_t pointerIndex) const {
428 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
431 inline float getPressure(size_t pointerIndex) const {
432 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
435 inline float getSize(size_t pointerIndex) const {
436 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
439 inline float getTouchMajor(size_t pointerIndex) const {
440 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
443 inline float getTouchMinor(size_t pointerIndex) const {
444 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
447 inline float getToolMajor(size_t pointerIndex) const {
448 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
451 inline float getToolMinor(size_t pointerIndex) const {
452 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
455 inline float getOrientation(size_t pointerIndex) const {
456 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
459 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
461 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
462 return mSampleEventTimes[historicalIndex];
465 const PointerCoords* getHistoricalRawPointerCoords(
466 size_t pointerIndex, size_t historicalIndex) const;
468 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
469 size_t historicalIndex) const;
471 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
472 return getHistoricalRawAxisValue(
473 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
476 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
477 return getHistoricalRawAxisValue(
478 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
481 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
483 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
484 return getHistoricalAxisValue(
485 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
488 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
489 return getHistoricalAxisValue(
490 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
493 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
494 return getHistoricalAxisValue(
495 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
498 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
499 return getHistoricalAxisValue(
500 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
503 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
504 return getHistoricalAxisValue(
505 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
508 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
509 return getHistoricalAxisValue(
510 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
513 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
514 return getHistoricalAxisValue(
515 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
518 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
519 return getHistoricalAxisValue(
520 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
523 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
524 return getHistoricalAxisValue(
525 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
528 ssize_t findPointerIndex(int32_t pointerId) const;
545 const PointerProperties* pointerProperties,
546 const PointerCoords* pointerCoords);
548 void copyFrom(const MotionEvent* other, bool keepHistory);
552 const PointerCoords* pointerCoords);
554 void offsetLocation(float xOffset, float yOffset);
556 void scale(float scaleFactor);
558 // Apply 3x3 perspective matrix transformation.
559 // Matrix is in row-major form and compatible with SkMatrix.
560 void transform(const float matrix[9]);
562 #ifdef HAVE_ANDROID_OS
563 status_t readFromParcel(Parcel* parcel);
564 status_t writeToParcel(Parcel* parcel) const;
567 static bool isTouchEvent(int32_t source, int32_t action);
568 inline bool isTouchEvent() const {
569 return isTouchEvent(mSource, mAction);
572 // Low-level accessors.
573 inline const PointerProperties* getPointerProperties() const {
574 return mPointerProperties.array();
576 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
577 inline const PointerCoords* getSamplePointerCoords() const {
578 return mSamplePointerCoords.array();
586 int32_t mButtonState;
592 Vector<PointerProperties> mPointerProperties;
593 Vector<nsecs_t> mSampleEventTimes;
594 Vector<PointerCoords> mSamplePointerCoords;
598 * Input event factory.
600 class InputEventFactoryInterface {
602 virtual ~InputEventFactoryInterface() { }
605 InputEventFactoryInterface() { }
607 virtual KeyEvent* createKeyEvent() = 0;
608 virtual MotionEvent* createMotionEvent() = 0;
612 * A simple input event factory implementation that uses a single preallocated instance
613 * of each type of input event that are reused for each request.
615 class PreallocatedInputEventFactory : public InputEventFactoryInterface {
617 PreallocatedInputEventFactory() { }
618 virtual ~PreallocatedInputEventFactory() { }
620 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
621 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
625 MotionEvent mMotionEvent;
629 * An input event factory implementation that maintains a pool of input events.
631 class PooledInputEventFactory : public InputEventFactoryInterface {
633 PooledInputEventFactory(size_t maxPoolSize = 20);
634 virtual ~PooledInputEventFactory();
636 virtual KeyEvent* createKeyEvent();
637 virtual MotionEvent* createMotionEvent();
639 void recycle(InputEvent* event);
642 const size_t mMaxPoolSize;
644 Vector<KeyEvent*> mKeyEventPool;
645 Vector<MotionEvent*> mMotionEventPool;
648 } // namespace android
650 #endif // _LIBINPUT_INPUT_H