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 | |
Dianne Hackborn | 4d96bb6 | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Declare a concrete type for the NDK's input event forward declaration. |
| 45 | */ |
Dianne Hackborn | 9c7f818 | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 46 | struct AInputEvent { }; |
Dianne Hackborn | 4d96bb6 | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 47 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | namespace android { |
| 49 | |
| 50 | /* |
| 51 | * A raw event as retrieved from the EventHub. |
| 52 | */ |
| 53 | struct RawEvent { |
| 54 | nsecs_t when; |
| 55 | int32_t deviceId; |
| 56 | int32_t type; |
| 57 | int32_t scanCode; |
| 58 | int32_t keyCode; |
| 59 | int32_t value; |
| 60 | uint32_t flags; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Flags that flow alongside events in the input dispatch system to help with certain |
| 65 | * policy decisions such as waking from device sleep. |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 66 | */ |
| 67 | enum { |
| 68 | /* These flags originate in RawEvents and are generally set in the key map. */ |
| 69 | |
| 70 | POLICY_FLAG_WAKE = 0x00000001, |
| 71 | POLICY_FLAG_WAKE_DROPPED = 0x00000002, |
| 72 | POLICY_FLAG_SHIFT = 0x00000004, |
| 73 | POLICY_FLAG_CAPS_LOCK = 0x00000008, |
| 74 | POLICY_FLAG_ALT = 0x00000010, |
| 75 | POLICY_FLAG_ALT_GR = 0x00000020, |
| 76 | POLICY_FLAG_MENU = 0x00000040, |
| 77 | POLICY_FLAG_LAUNCHER = 0x00000080, |
| 78 | |
Jeff Brown | 51d45a7 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 79 | POLICY_FLAG_RAW_MASK = 0x0000ffff, |
| 80 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 81 | /* These flags are set by the input reader policy as it intercepts each event. */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 82 | |
| 83 | // Indicates that the screen was off when the event was received and the event |
| 84 | // should wake the device. |
| 85 | POLICY_FLAG_WOKE_HERE = 0x10000000, |
| 86 | |
| 87 | // Indicates that the screen was dim when the event was received and the event |
| 88 | // should brighten the device. |
| 89 | POLICY_FLAG_BRIGHT_HERE = 0x20000000, |
Jeff Brown | 50de30a | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 90 | |
| 91 | // Indicates that the dispatcher should call back into the policy before dispatching. */ |
| 92 | POLICY_FLAG_INTERCEPT_DISPATCH = 0x40000000, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 96 | * Describes the basic configuration of input devices that are present. |
| 97 | */ |
| 98 | struct InputConfiguration { |
| 99 | enum { |
| 100 | TOUCHSCREEN_UNDEFINED = 0, |
| 101 | TOUCHSCREEN_NOTOUCH = 1, |
| 102 | TOUCHSCREEN_STYLUS = 2, |
| 103 | TOUCHSCREEN_FINGER = 3 |
| 104 | }; |
| 105 | |
| 106 | enum { |
| 107 | KEYBOARD_UNDEFINED = 0, |
| 108 | KEYBOARD_NOKEYS = 1, |
| 109 | KEYBOARD_QWERTY = 2, |
| 110 | KEYBOARD_12KEY = 3 |
| 111 | }; |
| 112 | |
| 113 | enum { |
| 114 | NAVIGATION_UNDEFINED = 0, |
| 115 | NAVIGATION_NONAV = 1, |
| 116 | NAVIGATION_DPAD = 2, |
| 117 | NAVIGATION_TRACKBALL = 3, |
| 118 | NAVIGATION_WHEEL = 4 |
| 119 | }; |
| 120 | |
| 121 | int32_t touchScreen; |
| 122 | int32_t keyboard; |
| 123 | int32_t navigation; |
| 124 | }; |
| 125 | |
| 126 | /* |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 127 | * Pointer coordinate data. |
| 128 | */ |
| 129 | struct PointerCoords { |
| 130 | float x; |
| 131 | float y; |
| 132 | float pressure; |
| 133 | float size; |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * Input events. |
| 138 | */ |
Dianne Hackborn | 9c7f818 | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 139 | class InputEvent : public AInputEvent { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 140 | public: |
| 141 | virtual ~InputEvent() { } |
| 142 | |
| 143 | virtual int32_t getType() const = 0; |
| 144 | |
| 145 | inline int32_t getDeviceId() const { return mDeviceId; } |
| 146 | |
| 147 | inline int32_t getNature() const { return mNature; } |
Dianne Hackborn | 189ed23 | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 148 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 149 | protected: |
| 150 | void initialize(int32_t deviceId, int32_t nature); |
| 151 | |
| 152 | private: |
| 153 | int32_t mDeviceId; |
| 154 | int32_t mNature; |
| 155 | }; |
| 156 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Key events. |
| 159 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 160 | class KeyEvent : public InputEvent { |
| 161 | public: |
| 162 | virtual ~KeyEvent() { } |
| 163 | |
| 164 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; } |
| 165 | |
| 166 | inline int32_t getAction() const { return mAction; } |
| 167 | |
| 168 | inline int32_t getFlags() const { return mFlags; } |
| 169 | |
| 170 | inline int32_t getKeyCode() const { return mKeyCode; } |
| 171 | |
| 172 | inline int32_t getScanCode() const { return mScanCode; } |
| 173 | |
| 174 | inline int32_t getMetaState() const { return mMetaState; } |
| 175 | |
| 176 | inline int32_t getRepeatCount() const { return mRepeatCount; } |
| 177 | |
| 178 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 179 | |
| 180 | inline nsecs_t getEventTime() const { return mEventTime; } |
| 181 | |
Dianne Hackborn | 189ed23 | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 182 | // Return true if this event may have a default action implementation. |
| 183 | static bool hasDefaultAction(int32_t keyCode); |
| 184 | bool hasDefaultAction() const; |
| 185 | |
| 186 | // Return true if this event represents a system key. |
| 187 | static bool isSystemKey(int32_t keyCode); |
| 188 | bool isSystemKey() const; |
| 189 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 190 | void initialize( |
| 191 | int32_t deviceId, |
| 192 | int32_t nature, |
| 193 | int32_t action, |
| 194 | int32_t flags, |
| 195 | int32_t keyCode, |
| 196 | int32_t scanCode, |
| 197 | int32_t metaState, |
| 198 | int32_t repeatCount, |
| 199 | nsecs_t downTime, |
| 200 | nsecs_t eventTime); |
| 201 | |
| 202 | private: |
| 203 | int32_t mAction; |
| 204 | int32_t mFlags; |
| 205 | int32_t mKeyCode; |
| 206 | int32_t mScanCode; |
| 207 | int32_t mMetaState; |
| 208 | int32_t mRepeatCount; |
| 209 | nsecs_t mDownTime; |
| 210 | nsecs_t mEventTime; |
| 211 | }; |
| 212 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 213 | /* |
| 214 | * Motion events. |
| 215 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 216 | class MotionEvent : public InputEvent { |
| 217 | public: |
| 218 | virtual ~MotionEvent() { } |
| 219 | |
| 220 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; } |
| 221 | |
| 222 | inline int32_t getAction() const { return mAction; } |
| 223 | |
| 224 | inline int32_t getEdgeFlags() const { return mEdgeFlags; } |
| 225 | |
| 226 | inline int32_t getMetaState() const { return mMetaState; } |
| 227 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 228 | inline float getXOffset() const { return mXOffset; } |
| 229 | |
| 230 | inline float getYOffset() const { return mYOffset; } |
| 231 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 232 | inline float getXPrecision() const { return mXPrecision; } |
| 233 | |
| 234 | inline float getYPrecision() const { return mYPrecision; } |
| 235 | |
| 236 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 237 | |
| 238 | inline size_t getPointerCount() const { return mPointerIds.size(); } |
| 239 | |
| 240 | inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; } |
| 241 | |
| 242 | inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } |
| 243 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 244 | inline float getRawX(size_t pointerIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 245 | return getCurrentPointerCoords(pointerIndex).x; |
| 246 | } |
| 247 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 248 | inline float getRawY(size_t pointerIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 249 | return getCurrentPointerCoords(pointerIndex).y; |
| 250 | } |
| 251 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 252 | inline float getX(size_t pointerIndex) const { |
| 253 | return getRawX(pointerIndex) + mXOffset; |
| 254 | } |
| 255 | |
| 256 | inline float getY(size_t pointerIndex) const { |
| 257 | return getRawY(pointerIndex) + mYOffset; |
| 258 | } |
| 259 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 260 | inline float getPressure(size_t pointerIndex) const { |
| 261 | return getCurrentPointerCoords(pointerIndex).pressure; |
| 262 | } |
| 263 | |
| 264 | inline float getSize(size_t pointerIndex) const { |
| 265 | return getCurrentPointerCoords(pointerIndex).size; |
| 266 | } |
| 267 | |
| 268 | inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } |
| 269 | |
| 270 | inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { |
| 271 | return mSampleEventTimes[historicalIndex]; |
| 272 | } |
| 273 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 274 | inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 275 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).x; |
| 276 | } |
| 277 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 278 | inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 279 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).y; |
| 280 | } |
| 281 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 282 | inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { |
| 283 | return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset; |
| 284 | } |
| 285 | |
| 286 | inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { |
| 287 | return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset; |
| 288 | } |
| 289 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 290 | inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { |
| 291 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure; |
| 292 | } |
| 293 | |
| 294 | inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { |
| 295 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).size; |
| 296 | } |
| 297 | |
| 298 | void initialize( |
| 299 | int32_t deviceId, |
| 300 | int32_t nature, |
| 301 | int32_t action, |
| 302 | int32_t edgeFlags, |
| 303 | int32_t metaState, |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 304 | float xOffset, |
| 305 | float yOffset, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 306 | float xPrecision, |
| 307 | float yPrecision, |
| 308 | nsecs_t downTime, |
| 309 | nsecs_t eventTime, |
| 310 | size_t pointerCount, |
| 311 | const int32_t* pointerIds, |
| 312 | const PointerCoords* pointerCoords); |
| 313 | |
| 314 | void addSample( |
| 315 | nsecs_t eventTime, |
| 316 | const PointerCoords* pointerCoords); |
| 317 | |
| 318 | void offsetLocation(float xOffset, float yOffset); |
| 319 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 320 | // Low-level accessors. |
| 321 | inline const int32_t* getPointerIds() const { return mPointerIds.array(); } |
| 322 | inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } |
| 323 | inline const PointerCoords* getSamplePointerCoords() const { |
| 324 | return mSamplePointerCoords.array(); |
| 325 | } |
| 326 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 327 | private: |
| 328 | int32_t mAction; |
| 329 | int32_t mEdgeFlags; |
| 330 | int32_t mMetaState; |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 331 | float mXOffset; |
| 332 | float mYOffset; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 333 | float mXPrecision; |
| 334 | float mYPrecision; |
| 335 | nsecs_t mDownTime; |
| 336 | Vector<int32_t> mPointerIds; |
| 337 | Vector<nsecs_t> mSampleEventTimes; |
| 338 | Vector<PointerCoords> mSamplePointerCoords; |
| 339 | |
| 340 | inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const { |
| 341 | return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 342 | } |
| 343 | |
| 344 | inline const PointerCoords& getHistoricalPointerCoords( |
| 345 | size_t pointerIndex, size_t historicalIndex) const { |
| 346 | return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | /* |
| 351 | * Input event factory. |
| 352 | */ |
| 353 | class InputEventFactoryInterface { |
| 354 | protected: |
| 355 | virtual ~InputEventFactoryInterface() { } |
| 356 | |
| 357 | public: |
| 358 | InputEventFactoryInterface() { } |
| 359 | |
| 360 | virtual KeyEvent* createKeyEvent() = 0; |
| 361 | virtual MotionEvent* createMotionEvent() = 0; |
| 362 | }; |
| 363 | |
| 364 | /* |
| 365 | * A simple input event factory implementation that uses a single preallocated instance |
| 366 | * of each type of input event that are reused for each request. |
| 367 | */ |
| 368 | class PreallocatedInputEventFactory : public InputEventFactoryInterface { |
| 369 | public: |
| 370 | PreallocatedInputEventFactory() { } |
| 371 | virtual ~PreallocatedInputEventFactory() { } |
| 372 | |
| 373 | virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } |
| 374 | virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } |
| 375 | |
| 376 | private: |
| 377 | KeyEvent mKeyEvent; |
| 378 | MotionEvent mMotionEvent; |
| 379 | }; |
| 380 | |
| 381 | |
| 382 | } // namespace android |
| 383 | |
| 384 | #endif // _UI_INPUT_H |