michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef _ANDROIDFW_INPUT_H michael@0: #define _ANDROIDFW_INPUT_H michael@0: michael@0: /** michael@0: * Native input event structures. michael@0: */ michael@0: michael@0: #include "android_input.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: class SkMatrix; michael@0: #endif michael@0: michael@0: /* michael@0: * Additional private constants not defined in ndk/ui/input.h. michael@0: */ michael@0: enum { michael@0: /* Signifies that the key is being predispatched */ michael@0: AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000, michael@0: michael@0: /* Private control to determine when an app is tracking a key sequence. */ michael@0: AKEY_EVENT_FLAG_START_TRACKING = 0x40000000, michael@0: michael@0: /* Key event is inconsistent with previously sent key events. */ michael@0: AKEY_EVENT_FLAG_TAINTED = 0x80000000, michael@0: }; michael@0: michael@0: enum { michael@0: /* Motion event is inconsistent with previously sent motion events. */ michael@0: AMOTION_EVENT_FLAG_TAINTED = 0x80000000, michael@0: }; michael@0: michael@0: enum { michael@0: /* Used when a motion event is not associated with any display. michael@0: * Typically used for non-pointer events. */ michael@0: ADISPLAY_ID_NONE = -1, michael@0: michael@0: /* The default display id. */ michael@0: ADISPLAY_ID_DEFAULT = 0, michael@0: }; michael@0: michael@0: enum { michael@0: /* michael@0: * Indicates that an input device has switches. michael@0: * This input source flag is hidden from the API because switches are only used by the system michael@0: * and applications have no way to interact with them. michael@0: */ michael@0: AINPUT_SOURCE_SWITCH = 0x80000000, michael@0: }; michael@0: michael@0: /* michael@0: * SystemUiVisibility constants from View. michael@0: */ michael@0: enum { michael@0: ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0, michael@0: ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001, michael@0: }; michael@0: michael@0: /* michael@0: * Maximum number of pointers supported per motion event. michael@0: * Smallest number of pointers is 1. michael@0: * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers michael@0: * will occasionally emit 11. There is not much harm making this constant bigger.) michael@0: */ michael@0: #define MAX_POINTERS 16 michael@0: michael@0: /* michael@0: * Maximum pointer id value supported in a motion event. michael@0: * Smallest pointer id is 0. michael@0: * (This is limited by our use of BitSet32 to track pointer assignments.) michael@0: */ michael@0: #define MAX_POINTER_ID 31 michael@0: michael@0: /* michael@0: * Declare a concrete type for the NDK's input event forward declaration. michael@0: */ michael@0: struct AInputEvent { michael@0: virtual ~AInputEvent() { } michael@0: }; michael@0: michael@0: /* michael@0: * Declare a concrete type for the NDK's input device forward declaration. michael@0: */ michael@0: struct AInputDevice { michael@0: virtual ~AInputDevice() { } michael@0: }; michael@0: michael@0: michael@0: namespace android { michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: class Parcel; michael@0: #endif michael@0: michael@0: /* michael@0: * Flags that flow alongside events in the input dispatch system to help with certain michael@0: * policy decisions such as waking from device sleep. michael@0: * michael@0: * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java. michael@0: */ michael@0: enum { michael@0: /* These flags originate in RawEvents and are generally set in the key map. michael@0: * NOTE: If you edit these flags, also edit labels in KeycodeLabels.h. */ michael@0: michael@0: POLICY_FLAG_WAKE = 0x00000001, michael@0: POLICY_FLAG_WAKE_DROPPED = 0x00000002, michael@0: POLICY_FLAG_SHIFT = 0x00000004, michael@0: POLICY_FLAG_CAPS_LOCK = 0x00000008, michael@0: POLICY_FLAG_ALT = 0x00000010, michael@0: POLICY_FLAG_ALT_GR = 0x00000020, michael@0: POLICY_FLAG_MENU = 0x00000040, michael@0: POLICY_FLAG_LAUNCHER = 0x00000080, michael@0: POLICY_FLAG_VIRTUAL = 0x00000100, michael@0: POLICY_FLAG_FUNCTION = 0x00000200, michael@0: michael@0: POLICY_FLAG_RAW_MASK = 0x0000ffff, michael@0: michael@0: /* These flags are set by the input dispatcher. */ michael@0: michael@0: // Indicates that the input event was injected. michael@0: POLICY_FLAG_INJECTED = 0x01000000, michael@0: michael@0: // Indicates that the input event is from a trusted source such as a directly attached michael@0: // input device or an application with system-wide event injection permission. michael@0: POLICY_FLAG_TRUSTED = 0x02000000, michael@0: michael@0: // Indicates that the input event has passed through an input filter. michael@0: POLICY_FLAG_FILTERED = 0x04000000, michael@0: michael@0: // Disables automatic key repeating behavior. michael@0: POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000, michael@0: michael@0: /* These flags are set by the input reader policy as it intercepts each event. */ michael@0: michael@0: // Indicates that the screen was off when the event was received and the event michael@0: // should wake the device. michael@0: POLICY_FLAG_WOKE_HERE = 0x10000000, michael@0: michael@0: // Indicates that the screen was dim when the event was received and the event michael@0: // should brighten the device. michael@0: POLICY_FLAG_BRIGHT_HERE = 0x20000000, michael@0: michael@0: // Indicates that the event should be dispatched to applications. michael@0: // The input event should still be sent to the InputDispatcher so that it can see all michael@0: // input events received include those that it will not deliver. michael@0: POLICY_FLAG_PASS_TO_USER = 0x40000000, michael@0: }; michael@0: michael@0: /* michael@0: * Pointer coordinate data. michael@0: */ michael@0: struct PointerCoords { michael@0: enum { MAX_AXES = 14 }; // 14 so that sizeof(PointerCoords) == 64 michael@0: michael@0: // Bitfield of axes that are present in this structure. michael@0: uint64_t bits; michael@0: michael@0: // Values of axes that are stored in this structure packed in order by axis id michael@0: // for each axis that is present in the structure according to 'bits'. michael@0: float values[MAX_AXES]; michael@0: michael@0: inline void clear() { michael@0: bits = 0; michael@0: } michael@0: michael@0: float getAxisValue(int32_t axis) const; michael@0: status_t setAxisValue(int32_t axis, float value); michael@0: michael@0: void scale(float scale); michael@0: michael@0: inline float getX() const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_X); michael@0: } michael@0: michael@0: inline float getY() const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_Y); michael@0: } michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: status_t readFromParcel(Parcel* parcel); michael@0: status_t writeToParcel(Parcel* parcel) const; michael@0: #endif michael@0: michael@0: bool operator==(const PointerCoords& other) const; michael@0: inline bool operator!=(const PointerCoords& other) const { michael@0: return !(*this == other); michael@0: } michael@0: michael@0: void copyFrom(const PointerCoords& other); michael@0: michael@0: private: michael@0: void tooManyAxes(int axis); michael@0: }; michael@0: michael@0: /* michael@0: * Pointer property data. michael@0: */ michael@0: struct PointerProperties { michael@0: // The id of the pointer. michael@0: int32_t id; michael@0: michael@0: // The pointer tool type. michael@0: int32_t toolType; michael@0: michael@0: inline void clear() { michael@0: id = -1; michael@0: toolType = 0; michael@0: } michael@0: michael@0: bool operator==(const PointerProperties& other) const; michael@0: inline bool operator!=(const PointerProperties& other) const { michael@0: return !(*this == other); michael@0: } michael@0: michael@0: void copyFrom(const PointerProperties& other); michael@0: }; michael@0: michael@0: /* michael@0: * Input events. michael@0: */ michael@0: class InputEvent : public AInputEvent { michael@0: public: michael@0: virtual ~InputEvent() { } michael@0: michael@0: virtual int32_t getType() const = 0; michael@0: michael@0: inline int32_t getDeviceId() const { return mDeviceId; } michael@0: michael@0: inline int32_t getSource() const { return mSource; } michael@0: michael@0: inline void setSource(int32_t source) { mSource = source; } michael@0: michael@0: protected: michael@0: void initialize(int32_t deviceId, int32_t source); michael@0: void initialize(const InputEvent& from); michael@0: michael@0: int32_t mDeviceId; michael@0: int32_t mSource; michael@0: }; michael@0: michael@0: /* michael@0: * Key events. michael@0: */ michael@0: class KeyEvent : public InputEvent { michael@0: public: michael@0: virtual ~KeyEvent() { } michael@0: michael@0: virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; } michael@0: michael@0: inline int32_t getAction() const { return mAction; } michael@0: michael@0: inline int32_t getFlags() const { return mFlags; } michael@0: michael@0: inline void setFlags(int32_t flags) { mFlags = flags; } michael@0: michael@0: inline int32_t getKeyCode() const { return mKeyCode; } michael@0: michael@0: inline int32_t getScanCode() const { return mScanCode; } michael@0: michael@0: inline int32_t getMetaState() const { return mMetaState; } michael@0: michael@0: inline int32_t getRepeatCount() const { return mRepeatCount; } michael@0: michael@0: inline nsecs_t getDownTime() const { return mDownTime; } michael@0: michael@0: inline nsecs_t getEventTime() const { return mEventTime; } michael@0: michael@0: // Return true if this event may have a default action implementation. michael@0: static bool hasDefaultAction(int32_t keyCode); michael@0: bool hasDefaultAction() const; michael@0: michael@0: // Return true if this event represents a system key. michael@0: static bool isSystemKey(int32_t keyCode); michael@0: bool isSystemKey() const; michael@0: michael@0: void initialize( michael@0: int32_t deviceId, michael@0: int32_t source, michael@0: int32_t action, michael@0: int32_t flags, michael@0: int32_t keyCode, michael@0: int32_t scanCode, michael@0: int32_t metaState, michael@0: int32_t repeatCount, michael@0: nsecs_t downTime, michael@0: nsecs_t eventTime); michael@0: void initialize(const KeyEvent& from); michael@0: michael@0: protected: michael@0: int32_t mAction; michael@0: int32_t mFlags; michael@0: int32_t mKeyCode; michael@0: int32_t mScanCode; michael@0: int32_t mMetaState; michael@0: int32_t mRepeatCount; michael@0: nsecs_t mDownTime; michael@0: nsecs_t mEventTime; michael@0: }; michael@0: michael@0: /* michael@0: * Motion events. michael@0: */ michael@0: class MotionEvent : public InputEvent { michael@0: public: michael@0: virtual ~MotionEvent() { } michael@0: michael@0: virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; } michael@0: michael@0: inline int32_t getAction() const { return mAction; } michael@0: michael@0: inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; } michael@0: michael@0: inline int32_t getActionIndex() const { michael@0: return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) michael@0: >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; michael@0: } michael@0: michael@0: inline void setAction(int32_t action) { mAction = action; } michael@0: michael@0: inline int32_t getFlags() const { return mFlags; } michael@0: michael@0: inline void setFlags(int32_t flags) { mFlags = flags; } michael@0: michael@0: inline int32_t getEdgeFlags() const { return mEdgeFlags; } michael@0: michael@0: inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } michael@0: michael@0: inline int32_t getMetaState() const { return mMetaState; } michael@0: michael@0: inline void setMetaState(int32_t metaState) { mMetaState = metaState; } michael@0: michael@0: inline int32_t getButtonState() const { return mButtonState; } michael@0: michael@0: inline float getXOffset() const { return mXOffset; } michael@0: michael@0: inline float getYOffset() const { return mYOffset; } michael@0: michael@0: inline float getXPrecision() const { return mXPrecision; } michael@0: michael@0: inline float getYPrecision() const { return mYPrecision; } michael@0: michael@0: inline nsecs_t getDownTime() const { return mDownTime; } michael@0: michael@0: inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } michael@0: michael@0: inline size_t getPointerCount() const { return mPointerProperties.size(); } michael@0: michael@0: inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { michael@0: return &mPointerProperties[pointerIndex]; michael@0: } michael@0: michael@0: inline int32_t getPointerId(size_t pointerIndex) const { michael@0: return mPointerProperties[pointerIndex].id; michael@0: } michael@0: michael@0: inline int32_t getToolType(size_t pointerIndex) const { michael@0: return mPointerProperties[pointerIndex].toolType; michael@0: } michael@0: michael@0: inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } michael@0: michael@0: const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; michael@0: michael@0: float getRawAxisValue(int32_t axis, size_t pointerIndex) const; michael@0: michael@0: inline float getRawX(size_t pointerIndex) const { michael@0: return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); michael@0: } michael@0: michael@0: inline float getRawY(size_t pointerIndex) const { michael@0: return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); michael@0: } michael@0: michael@0: float getAxisValue(int32_t axis, size_t pointerIndex) const; michael@0: michael@0: inline float getX(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); michael@0: } michael@0: michael@0: inline float getY(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); michael@0: } michael@0: michael@0: inline float getPressure(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); michael@0: } michael@0: michael@0: inline float getSize(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); michael@0: } michael@0: michael@0: inline float getTouchMajor(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); michael@0: } michael@0: michael@0: inline float getTouchMinor(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); michael@0: } michael@0: michael@0: inline float getToolMajor(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); michael@0: } michael@0: michael@0: inline float getToolMinor(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); michael@0: } michael@0: michael@0: inline float getOrientation(size_t pointerIndex) const { michael@0: return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); michael@0: } michael@0: michael@0: inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } michael@0: michael@0: inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { michael@0: return mSampleEventTimes[historicalIndex]; michael@0: } michael@0: michael@0: const PointerCoords* getHistoricalRawPointerCoords( michael@0: size_t pointerIndex, size_t historicalIndex) const; michael@0: michael@0: float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, michael@0: size_t historicalIndex) const; michael@0: michael@0: inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalRawAxisValue( michael@0: AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalRawAxisValue( michael@0: AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; michael@0: michael@0: inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { michael@0: return getHistoricalAxisValue( michael@0: AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); michael@0: } michael@0: michael@0: ssize_t findPointerIndex(int32_t pointerId) const; michael@0: michael@0: void initialize( michael@0: int32_t deviceId, michael@0: int32_t source, michael@0: int32_t action, michael@0: int32_t flags, michael@0: int32_t edgeFlags, michael@0: int32_t metaState, michael@0: int32_t buttonState, michael@0: float xOffset, michael@0: float yOffset, michael@0: float xPrecision, michael@0: float yPrecision, michael@0: nsecs_t downTime, michael@0: nsecs_t eventTime, michael@0: size_t pointerCount, michael@0: const PointerProperties* pointerProperties, michael@0: const PointerCoords* pointerCoords); michael@0: michael@0: void copyFrom(const MotionEvent* other, bool keepHistory); michael@0: michael@0: void addSample( michael@0: nsecs_t eventTime, michael@0: const PointerCoords* pointerCoords); michael@0: michael@0: void offsetLocation(float xOffset, float yOffset); michael@0: michael@0: void scale(float scaleFactor); michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: void transform(const SkMatrix* matrix); michael@0: michael@0: status_t readFromParcel(Parcel* parcel); michael@0: status_t writeToParcel(Parcel* parcel) const; michael@0: #endif michael@0: michael@0: static bool isTouchEvent(int32_t source, int32_t action); michael@0: inline bool isTouchEvent() const { michael@0: return isTouchEvent(mSource, mAction); michael@0: } michael@0: michael@0: // Low-level accessors. michael@0: inline const PointerProperties* getPointerProperties() const { michael@0: return mPointerProperties.array(); michael@0: } michael@0: inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } michael@0: inline const PointerCoords* getSamplePointerCoords() const { michael@0: return mSamplePointerCoords.array(); michael@0: } michael@0: michael@0: protected: michael@0: int32_t mAction; michael@0: int32_t mFlags; michael@0: int32_t mEdgeFlags; michael@0: int32_t mMetaState; michael@0: int32_t mButtonState; michael@0: float mXOffset; michael@0: float mYOffset; michael@0: float mXPrecision; michael@0: float mYPrecision; michael@0: nsecs_t mDownTime; michael@0: Vector mPointerProperties; michael@0: Vector mSampleEventTimes; michael@0: Vector mSamplePointerCoords; michael@0: }; michael@0: michael@0: /* michael@0: * Input event factory. michael@0: */ michael@0: class InputEventFactoryInterface { michael@0: protected: michael@0: virtual ~InputEventFactoryInterface() { } michael@0: michael@0: public: michael@0: InputEventFactoryInterface() { } michael@0: michael@0: virtual KeyEvent* createKeyEvent() = 0; michael@0: virtual MotionEvent* createMotionEvent() = 0; michael@0: }; michael@0: michael@0: /* michael@0: * A simple input event factory implementation that uses a single preallocated instance michael@0: * of each type of input event that are reused for each request. michael@0: */ michael@0: class PreallocatedInputEventFactory : public InputEventFactoryInterface { michael@0: public: michael@0: PreallocatedInputEventFactory() { } michael@0: virtual ~PreallocatedInputEventFactory() { } michael@0: michael@0: virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } michael@0: virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } michael@0: michael@0: private: michael@0: KeyEvent mKeyEvent; michael@0: MotionEvent mMotionEvent; michael@0: }; michael@0: michael@0: /* michael@0: * An input event factory implementation that maintains a pool of input events. michael@0: */ michael@0: class PooledInputEventFactory : public InputEventFactoryInterface { michael@0: public: michael@0: PooledInputEventFactory(size_t maxPoolSize = 20); michael@0: virtual ~PooledInputEventFactory(); michael@0: michael@0: virtual KeyEvent* createKeyEvent(); michael@0: virtual MotionEvent* createMotionEvent(); michael@0: michael@0: void recycle(InputEvent* event); michael@0: michael@0: private: michael@0: const size_t mMaxPoolSize; michael@0: michael@0: Vector mKeyEventPool; michael@0: Vector mMotionEventPool; michael@0: }; michael@0: michael@0: } // namespace android michael@0: michael@0: #endif // _ANDROIDFW_INPUT_H