Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 _UI_INPUT_H |
| 18 | #define _UI_INPUT_H |
| 19 | |
| 20 | /** |
| 21 | * Native input event structures. |
| 22 | */ |
| 23 | |
| 24 | #include <android/input.h> |
| 25 | #include <utils/Vector.h> |
| 26 | #include <utils/Timers.h> |
| 27 | |
| 28 | /* |
| 29 | * Additional private constants not defined in ndk/ui/input.h. |
| 30 | */ |
| 31 | enum { |
| 32 | /* |
| 33 | * Private control to determine when an app is tracking a key sequence. |
| 34 | */ |
| 35 | KEY_EVENT_FLAG_START_TRACKING = 0x40000000 |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * Maximum number of pointers supported per motion event. |
| 40 | */ |
| 41 | #define MAX_POINTERS 10 |
| 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | /* |
| 46 | * A raw event as retrieved from the EventHub. |
| 47 | */ |
| 48 | struct RawEvent { |
| 49 | nsecs_t when; |
| 50 | int32_t deviceId; |
| 51 | int32_t type; |
| 52 | int32_t scanCode; |
| 53 | int32_t keyCode; |
| 54 | int32_t value; |
| 55 | uint32_t flags; |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * Flags that flow alongside events in the input dispatch system to help with certain |
| 60 | * policy decisions such as waking from device sleep. |
| 61 | * |
| 62 | * TODO This enumeration should probably be split up or relabeled for clarity. |
| 63 | */ |
| 64 | enum { |
| 65 | /* These flags originate in RawEvents and are generally set in the key map. */ |
| 66 | |
| 67 | POLICY_FLAG_WAKE = 0x00000001, |
| 68 | POLICY_FLAG_WAKE_DROPPED = 0x00000002, |
| 69 | POLICY_FLAG_SHIFT = 0x00000004, |
| 70 | POLICY_FLAG_CAPS_LOCK = 0x00000008, |
| 71 | POLICY_FLAG_ALT = 0x00000010, |
| 72 | POLICY_FLAG_ALT_GR = 0x00000020, |
| 73 | POLICY_FLAG_MENU = 0x00000040, |
| 74 | POLICY_FLAG_LAUNCHER = 0x00000080, |
| 75 | |
| 76 | /* These flags are set by the input dispatch policy as it intercepts each event. */ |
| 77 | |
| 78 | // Indicates that the screen was off when the event was received and the event |
| 79 | // should wake the device. |
| 80 | POLICY_FLAG_WOKE_HERE = 0x10000000, |
| 81 | |
| 82 | // Indicates that the screen was dim when the event was received and the event |
| 83 | // should brighten the device. |
| 84 | POLICY_FLAG_BRIGHT_HERE = 0x20000000, |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * Pointer coordinate data. |
| 89 | */ |
| 90 | struct PointerCoords { |
| 91 | float x; |
| 92 | float y; |
| 93 | float pressure; |
| 94 | float size; |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Input events. |
| 99 | */ |
| 100 | struct input_event_t { }; |
| 101 | |
| 102 | class InputEvent : public input_event_t { |
| 103 | public: |
| 104 | virtual ~InputEvent() { } |
| 105 | |
| 106 | virtual int32_t getType() const = 0; |
| 107 | |
| 108 | inline int32_t getDeviceId() const { return mDeviceId; } |
| 109 | |
| 110 | inline int32_t getNature() const { return mNature; } |
| 111 | |
| 112 | protected: |
| 113 | void initialize(int32_t deviceId, int32_t nature); |
| 114 | |
| 115 | private: |
| 116 | int32_t mDeviceId; |
| 117 | int32_t mNature; |
| 118 | }; |
| 119 | |
| 120 | class KeyEvent : public InputEvent { |
| 121 | public: |
| 122 | virtual ~KeyEvent() { } |
| 123 | |
| 124 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; } |
| 125 | |
| 126 | inline int32_t getAction() const { return mAction; } |
| 127 | |
| 128 | inline int32_t getFlags() const { return mFlags; } |
| 129 | |
| 130 | inline int32_t getKeyCode() const { return mKeyCode; } |
| 131 | |
| 132 | inline int32_t getScanCode() const { return mScanCode; } |
| 133 | |
| 134 | inline int32_t getMetaState() const { return mMetaState; } |
| 135 | |
| 136 | inline int32_t getRepeatCount() const { return mRepeatCount; } |
| 137 | |
| 138 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 139 | |
| 140 | inline nsecs_t getEventTime() const { return mEventTime; } |
| 141 | |
| 142 | void initialize( |
| 143 | int32_t deviceId, |
| 144 | int32_t nature, |
| 145 | int32_t action, |
| 146 | int32_t flags, |
| 147 | int32_t keyCode, |
| 148 | int32_t scanCode, |
| 149 | int32_t metaState, |
| 150 | int32_t repeatCount, |
| 151 | nsecs_t downTime, |
| 152 | nsecs_t eventTime); |
| 153 | |
| 154 | private: |
| 155 | int32_t mAction; |
| 156 | int32_t mFlags; |
| 157 | int32_t mKeyCode; |
| 158 | int32_t mScanCode; |
| 159 | int32_t mMetaState; |
| 160 | int32_t mRepeatCount; |
| 161 | nsecs_t mDownTime; |
| 162 | nsecs_t mEventTime; |
| 163 | }; |
| 164 | |
| 165 | class MotionEvent : public InputEvent { |
| 166 | public: |
| 167 | virtual ~MotionEvent() { } |
| 168 | |
| 169 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; } |
| 170 | |
| 171 | inline int32_t getAction() const { return mAction; } |
| 172 | |
| 173 | inline int32_t getEdgeFlags() const { return mEdgeFlags; } |
| 174 | |
| 175 | inline int32_t getMetaState() const { return mMetaState; } |
| 176 | |
| 177 | inline float getXPrecision() const { return mXPrecision; } |
| 178 | |
| 179 | inline float getYPrecision() const { return mYPrecision; } |
| 180 | |
| 181 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 182 | |
| 183 | inline size_t getPointerCount() const { return mPointerIds.size(); } |
| 184 | |
| 185 | inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; } |
| 186 | |
| 187 | inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } |
| 188 | |
| 189 | inline float getRawX() const { return mRawX; } |
| 190 | |
| 191 | inline float getRawY() const { return mRawY; } |
| 192 | |
| 193 | inline float getX(size_t pointerIndex) const { |
| 194 | return getCurrentPointerCoords(pointerIndex).x; |
| 195 | } |
| 196 | |
| 197 | inline float getY(size_t pointerIndex) const { |
| 198 | return getCurrentPointerCoords(pointerIndex).y; |
| 199 | } |
| 200 | |
| 201 | inline float getPressure(size_t pointerIndex) const { |
| 202 | return getCurrentPointerCoords(pointerIndex).pressure; |
| 203 | } |
| 204 | |
| 205 | inline float getSize(size_t pointerIndex) const { |
| 206 | return getCurrentPointerCoords(pointerIndex).size; |
| 207 | } |
| 208 | |
| 209 | inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } |
| 210 | |
| 211 | inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { |
| 212 | return mSampleEventTimes[historicalIndex]; |
| 213 | } |
| 214 | |
| 215 | inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { |
| 216 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).x; |
| 217 | } |
| 218 | |
| 219 | inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { |
| 220 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).y; |
| 221 | } |
| 222 | |
| 223 | inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { |
| 224 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure; |
| 225 | } |
| 226 | |
| 227 | inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { |
| 228 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).size; |
| 229 | } |
| 230 | |
| 231 | void initialize( |
| 232 | int32_t deviceId, |
| 233 | int32_t nature, |
| 234 | int32_t action, |
| 235 | int32_t edgeFlags, |
| 236 | int32_t metaState, |
| 237 | float rawX, |
| 238 | float rawY, |
| 239 | float xPrecision, |
| 240 | float yPrecision, |
| 241 | nsecs_t downTime, |
| 242 | nsecs_t eventTime, |
| 243 | size_t pointerCount, |
| 244 | const int32_t* pointerIds, |
| 245 | const PointerCoords* pointerCoords); |
| 246 | |
| 247 | void addSample( |
| 248 | nsecs_t eventTime, |
| 249 | const PointerCoords* pointerCoords); |
| 250 | |
| 251 | void offsetLocation(float xOffset, float yOffset); |
| 252 | |
| 253 | private: |
| 254 | int32_t mAction; |
| 255 | int32_t mEdgeFlags; |
| 256 | int32_t mMetaState; |
| 257 | float mRawX; |
| 258 | float mRawY; |
| 259 | float mXPrecision; |
| 260 | float mYPrecision; |
| 261 | nsecs_t mDownTime; |
| 262 | Vector<int32_t> mPointerIds; |
| 263 | Vector<nsecs_t> mSampleEventTimes; |
| 264 | Vector<PointerCoords> mSamplePointerCoords; |
| 265 | |
| 266 | inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const { |
| 267 | return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 268 | } |
| 269 | |
| 270 | inline const PointerCoords& getHistoricalPointerCoords( |
| 271 | size_t pointerIndex, size_t historicalIndex) const { |
| 272 | return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 273 | } |
| 274 | }; |
| 275 | |
| 276 | /* |
| 277 | * Input event factory. |
| 278 | */ |
| 279 | class InputEventFactoryInterface { |
| 280 | protected: |
| 281 | virtual ~InputEventFactoryInterface() { } |
| 282 | |
| 283 | public: |
| 284 | InputEventFactoryInterface() { } |
| 285 | |
| 286 | virtual KeyEvent* createKeyEvent() = 0; |
| 287 | virtual MotionEvent* createMotionEvent() = 0; |
| 288 | }; |
| 289 | |
| 290 | /* |
| 291 | * A simple input event factory implementation that uses a single preallocated instance |
| 292 | * of each type of input event that are reused for each request. |
| 293 | */ |
| 294 | class PreallocatedInputEventFactory : public InputEventFactoryInterface { |
| 295 | public: |
| 296 | PreallocatedInputEventFactory() { } |
| 297 | virtual ~PreallocatedInputEventFactory() { } |
| 298 | |
| 299 | virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } |
| 300 | virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } |
| 301 | |
| 302 | private: |
| 303 | KeyEvent mKeyEvent; |
| 304 | MotionEvent mMotionEvent; |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | } // namespace android |
| 309 | |
| 310 | #endif // _UI_INPUT_H |