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 InputEventLabels.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 number of samples supported per motion event.
116 #define MAX_SAMPLES UINT16_MAX
119 * Maximum pointer id value supported in a motion event.
120 * Smallest pointer id is 0.
121 * (This is limited by our use of BitSet32 to track pointer assignments.)
123 #define MAX_POINTER_ID 31
126 * Declare a concrete type for the NDK's input event forward declaration.
129 virtual ~AInputEvent() { }
133 * Declare a concrete type for the NDK's input device forward declaration.
135 struct AInputDevice {
136 virtual ~AInputDevice() { }
142 #ifdef HAVE_ANDROID_OS
147 * Flags that flow alongside events in the input dispatch system to help with certain
148 * policy decisions such as waking from device sleep.
150 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
153 /* These flags originate in RawEvents and are generally set in the key map.
154 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
155 * InputEventLabels.h as well. */
157 // Indicates that the event should wake the device.
158 POLICY_FLAG_WAKE = 0x00000001,
160 // Indicates that the key is virtual, such as a capacitive button, and should
161 // generate haptic feedback. Virtual keys may be suppressed for some time
162 // after a recent touch to prevent accidental activation of virtual keys adjacent
163 // to the touch screen during an edge swipe.
164 POLICY_FLAG_VIRTUAL = 0x00000002,
166 // Indicates that the key is the special function modifier.
167 POLICY_FLAG_FUNCTION = 0x00000004,
169 // Indicates that the key represents a special gesture that has been detected by
170 // the touch firmware or driver. Causes touch events from the same device to be canceled.
171 POLICY_FLAG_GESTURE = 0x00000008,
173 POLICY_FLAG_RAW_MASK = 0x0000ffff,
175 /* These flags are set by the input dispatcher. */
177 // Indicates that the input event was injected.
178 POLICY_FLAG_INJECTED = 0x01000000,
180 // Indicates that the input event is from a trusted source such as a directly attached
181 // input device or an application with system-wide event injection permission.
182 POLICY_FLAG_TRUSTED = 0x02000000,
184 // Indicates that the input event has passed through an input filter.
185 POLICY_FLAG_FILTERED = 0x04000000,
187 // Disables automatic key repeating behavior.
188 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
190 /* These flags are set by the input reader policy as it intercepts each event. */
192 // Indicates that the device was in an interactive state when the
193 // event was intercepted.
194 POLICY_FLAG_INTERACTIVE = 0x20000000,
196 // Indicates that the event should be dispatched to applications.
197 // The input event should still be sent to the InputDispatcher so that it can see all
198 // input events received include those that it will not deliver.
199 POLICY_FLAG_PASS_TO_USER = 0x40000000,
203 * Pointer coordinate data.
205 struct PointerCoords {
206 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
208 // Bitfield of axes that are present in this structure.
209 uint64_t bits __attribute__((aligned(8)));
211 // Values of axes that are stored in this structure packed in order by axis id
212 // for each axis that is present in the structure according to 'bits'.
213 float values[MAX_AXES];
215 inline void clear() {
216 BitSet64::clear(bits);
219 bool isEmpty() const {
220 return BitSet64::isEmpty(bits);
223 float getAxisValue(int32_t axis) const;
224 status_t setAxisValue(int32_t axis, float value);
226 void scale(float scale);
227 void applyOffset(float xOffset, float yOffset);
229 inline float getX() const {
230 return getAxisValue(AMOTION_EVENT_AXIS_X);
233 inline float getY() const {
234 return getAxisValue(AMOTION_EVENT_AXIS_Y);
237 #ifdef HAVE_ANDROID_OS
238 status_t readFromParcel(Parcel* parcel);
239 status_t writeToParcel(Parcel* parcel) const;
242 bool operator==(const PointerCoords& other) const;
243 inline bool operator!=(const PointerCoords& other) const {
244 return !(*this == other);
247 void copyFrom(const PointerCoords& other);
250 void tooManyAxes(int axis);
254 * Pointer property data.
256 struct PointerProperties {
257 // The id of the pointer.
260 // The pointer tool type.
263 inline void clear() {
268 bool operator==(const PointerProperties& other) const;
269 inline bool operator!=(const PointerProperties& other) const {
270 return !(*this == other);
273 void copyFrom(const PointerProperties& other);
279 class InputEvent : public AInputEvent {
281 virtual ~InputEvent() { }
283 virtual int32_t getType() const = 0;
285 inline int32_t getDeviceId() const { return mDeviceId; }
287 inline int32_t getSource() const { return mSource; }
289 inline void setSource(int32_t source) { mSource = source; }
292 void initialize(int32_t deviceId, int32_t source);
293 void initialize(const InputEvent& from);
302 class KeyEvent : public InputEvent {
304 virtual ~KeyEvent() { }
306 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
308 inline int32_t getAction() const { return mAction; }
310 inline int32_t getFlags() const { return mFlags; }
312 inline void setFlags(int32_t flags) { mFlags = flags; }
314 inline int32_t getKeyCode() const { return mKeyCode; }
316 inline int32_t getScanCode() const { return mScanCode; }
318 inline int32_t getMetaState() const { return mMetaState; }
320 inline int32_t getRepeatCount() const { return mRepeatCount; }
322 inline nsecs_t getDownTime() const { return mDownTime; }
324 inline nsecs_t getEventTime() const { return mEventTime; }
326 static const char* getLabel(int32_t keyCode);
327 static int32_t getKeyCodeFromLabel(const char* label);
340 void initialize(const KeyEvent& from);
348 int32_t mRepeatCount;
356 class MotionEvent : public InputEvent {
358 virtual ~MotionEvent() { }
360 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
362 inline int32_t getAction() const { return mAction; }
364 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
366 inline int32_t getActionIndex() const {
367 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
368 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
371 inline void setAction(int32_t action) { mAction = action; }
373 inline int32_t getFlags() const { return mFlags; }
375 inline void setFlags(int32_t flags) { mFlags = flags; }
377 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
379 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
381 inline int32_t getMetaState() const { return mMetaState; }
383 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
385 inline int32_t getButtonState() const { return mButtonState; }
387 inline float getXOffset() const { return mXOffset; }
389 inline float getYOffset() const { return mYOffset; }
391 inline float getXPrecision() const { return mXPrecision; }
393 inline float getYPrecision() const { return mYPrecision; }
395 inline nsecs_t getDownTime() const { return mDownTime; }
397 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
399 inline size_t getPointerCount() const { return mPointerProperties.size(); }
401 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
402 return &mPointerProperties[pointerIndex];
405 inline int32_t getPointerId(size_t pointerIndex) const {
406 return mPointerProperties[pointerIndex].id;
409 inline int32_t getToolType(size_t pointerIndex) const {
410 return mPointerProperties[pointerIndex].toolType;
413 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
415 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
417 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
419 inline float getRawX(size_t pointerIndex) const {
420 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
423 inline float getRawY(size_t pointerIndex) const {
424 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
427 float getAxisValue(int32_t axis, size_t pointerIndex) const;
429 inline float getX(size_t pointerIndex) const {
430 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
433 inline float getY(size_t pointerIndex) const {
434 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
437 inline float getPressure(size_t pointerIndex) const {
438 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
441 inline float getSize(size_t pointerIndex) const {
442 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
445 inline float getTouchMajor(size_t pointerIndex) const {
446 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
449 inline float getTouchMinor(size_t pointerIndex) const {
450 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
453 inline float getToolMajor(size_t pointerIndex) const {
454 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
457 inline float getToolMinor(size_t pointerIndex) const {
458 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
461 inline float getOrientation(size_t pointerIndex) const {
462 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
465 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
467 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
468 return mSampleEventTimes[historicalIndex];
471 const PointerCoords* getHistoricalRawPointerCoords(
472 size_t pointerIndex, size_t historicalIndex) const;
474 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
475 size_t historicalIndex) const;
477 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
478 return getHistoricalRawAxisValue(
479 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
482 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
483 return getHistoricalRawAxisValue(
484 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
487 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
489 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
490 return getHistoricalAxisValue(
491 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
494 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
495 return getHistoricalAxisValue(
496 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
499 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
500 return getHistoricalAxisValue(
501 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
504 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
505 return getHistoricalAxisValue(
506 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
509 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
510 return getHistoricalAxisValue(
511 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
514 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
515 return getHistoricalAxisValue(
516 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
519 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
520 return getHistoricalAxisValue(
521 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
524 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
525 return getHistoricalAxisValue(
526 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
529 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
530 return getHistoricalAxisValue(
531 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
534 ssize_t findPointerIndex(int32_t pointerId) const;
551 const PointerProperties* pointerProperties,
552 const PointerCoords* pointerCoords);
554 void copyFrom(const MotionEvent* other, bool keepHistory);
558 const PointerCoords* pointerCoords);
560 void offsetLocation(float xOffset, float yOffset);
562 void scale(float scaleFactor);
564 // Apply 3x3 perspective matrix transformation.
565 // Matrix is in row-major form and compatible with SkMatrix.
566 void transform(const float matrix[9]);
568 #ifdef HAVE_ANDROID_OS
569 status_t readFromParcel(Parcel* parcel);
570 status_t writeToParcel(Parcel* parcel) const;
573 static bool isTouchEvent(int32_t source, int32_t action);
574 inline bool isTouchEvent() const {
575 return isTouchEvent(mSource, mAction);
578 // Low-level accessors.
579 inline const PointerProperties* getPointerProperties() const {
580 return mPointerProperties.array();
582 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
583 inline const PointerCoords* getSamplePointerCoords() const {
584 return mSamplePointerCoords.array();
587 static const char* getLabel(int32_t axis);
588 static int32_t getAxisFromLabel(const char* label);
595 int32_t mButtonState;
601 Vector<PointerProperties> mPointerProperties;
602 Vector<nsecs_t> mSampleEventTimes;
603 Vector<PointerCoords> mSamplePointerCoords;
607 * Input event factory.
609 class InputEventFactoryInterface {
611 virtual ~InputEventFactoryInterface() { }
614 InputEventFactoryInterface() { }
616 virtual KeyEvent* createKeyEvent() = 0;
617 virtual MotionEvent* createMotionEvent() = 0;
621 * A simple input event factory implementation that uses a single preallocated instance
622 * of each type of input event that are reused for each request.
624 class PreallocatedInputEventFactory : public InputEventFactoryInterface {
626 PreallocatedInputEventFactory() { }
627 virtual ~PreallocatedInputEventFactory() { }
629 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
630 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
634 MotionEvent mMotionEvent;
638 * An input event factory implementation that maintains a pool of input events.
640 class PooledInputEventFactory : public InputEventFactoryInterface {
642 PooledInputEventFactory(size_t maxPoolSize = 20);
643 virtual ~PooledInputEventFactory();
645 virtual KeyEvent* createKeyEvent();
646 virtual MotionEvent* createMotionEvent();
648 void recycle(InputEvent* event);
651 const size_t mMaxPoolSize;
653 Vector<KeyEvent*> mKeyEventPool;
654 Vector<MotionEvent*> mMotionEventPool;
657 } // namespace android
659 #endif // _LIBINPUT_INPUT_H