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 | */ |
| 46 | struct input_event_t { }; |
| 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, |
| 90 | }; |
| 91 | |
| 92 | /* |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 93 | * Describes the basic configuration of input devices that are present. |
| 94 | */ |
| 95 | struct InputConfiguration { |
| 96 | enum { |
| 97 | TOUCHSCREEN_UNDEFINED = 0, |
| 98 | TOUCHSCREEN_NOTOUCH = 1, |
| 99 | TOUCHSCREEN_STYLUS = 2, |
| 100 | TOUCHSCREEN_FINGER = 3 |
| 101 | }; |
| 102 | |
| 103 | enum { |
| 104 | KEYBOARD_UNDEFINED = 0, |
| 105 | KEYBOARD_NOKEYS = 1, |
| 106 | KEYBOARD_QWERTY = 2, |
| 107 | KEYBOARD_12KEY = 3 |
| 108 | }; |
| 109 | |
| 110 | enum { |
| 111 | NAVIGATION_UNDEFINED = 0, |
| 112 | NAVIGATION_NONAV = 1, |
| 113 | NAVIGATION_DPAD = 2, |
| 114 | NAVIGATION_TRACKBALL = 3, |
| 115 | NAVIGATION_WHEEL = 4 |
| 116 | }; |
| 117 | |
| 118 | int32_t touchScreen; |
| 119 | int32_t keyboard; |
| 120 | int32_t navigation; |
| 121 | }; |
| 122 | |
| 123 | /* |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 124 | * Pointer coordinate data. |
| 125 | */ |
| 126 | struct PointerCoords { |
| 127 | float x; |
| 128 | float y; |
| 129 | float pressure; |
| 130 | float size; |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * Input events. |
| 135 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 136 | class InputEvent : public input_event_t { |
| 137 | public: |
| 138 | virtual ~InputEvent() { } |
| 139 | |
| 140 | virtual int32_t getType() const = 0; |
| 141 | |
| 142 | inline int32_t getDeviceId() const { return mDeviceId; } |
| 143 | |
| 144 | inline int32_t getNature() const { return mNature; } |
| 145 | |
| 146 | protected: |
| 147 | void initialize(int32_t deviceId, int32_t nature); |
| 148 | |
| 149 | private: |
| 150 | int32_t mDeviceId; |
| 151 | int32_t mNature; |
| 152 | }; |
| 153 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 154 | /* |
| 155 | * Key events. |
| 156 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 157 | class KeyEvent : public InputEvent { |
| 158 | public: |
| 159 | virtual ~KeyEvent() { } |
| 160 | |
| 161 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; } |
| 162 | |
| 163 | inline int32_t getAction() const { return mAction; } |
| 164 | |
| 165 | inline int32_t getFlags() const { return mFlags; } |
| 166 | |
| 167 | inline int32_t getKeyCode() const { return mKeyCode; } |
| 168 | |
| 169 | inline int32_t getScanCode() const { return mScanCode; } |
| 170 | |
| 171 | inline int32_t getMetaState() const { return mMetaState; } |
| 172 | |
| 173 | inline int32_t getRepeatCount() const { return mRepeatCount; } |
| 174 | |
| 175 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 176 | |
| 177 | inline nsecs_t getEventTime() const { return mEventTime; } |
| 178 | |
| 179 | void initialize( |
| 180 | int32_t deviceId, |
| 181 | int32_t nature, |
| 182 | int32_t action, |
| 183 | int32_t flags, |
| 184 | int32_t keyCode, |
| 185 | int32_t scanCode, |
| 186 | int32_t metaState, |
| 187 | int32_t repeatCount, |
| 188 | nsecs_t downTime, |
| 189 | nsecs_t eventTime); |
| 190 | |
| 191 | private: |
| 192 | int32_t mAction; |
| 193 | int32_t mFlags; |
| 194 | int32_t mKeyCode; |
| 195 | int32_t mScanCode; |
| 196 | int32_t mMetaState; |
| 197 | int32_t mRepeatCount; |
| 198 | nsecs_t mDownTime; |
| 199 | nsecs_t mEventTime; |
| 200 | }; |
| 201 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Motion events. |
| 204 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 205 | class MotionEvent : public InputEvent { |
| 206 | public: |
| 207 | virtual ~MotionEvent() { } |
| 208 | |
| 209 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; } |
| 210 | |
| 211 | inline int32_t getAction() const { return mAction; } |
| 212 | |
| 213 | inline int32_t getEdgeFlags() const { return mEdgeFlags; } |
| 214 | |
| 215 | inline int32_t getMetaState() const { return mMetaState; } |
| 216 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 217 | inline float getXOffset() const { return mXOffset; } |
| 218 | |
| 219 | inline float getYOffset() const { return mYOffset; } |
| 220 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 221 | inline float getXPrecision() const { return mXPrecision; } |
| 222 | |
| 223 | inline float getYPrecision() const { return mYPrecision; } |
| 224 | |
| 225 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 226 | |
| 227 | inline size_t getPointerCount() const { return mPointerIds.size(); } |
| 228 | |
| 229 | inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; } |
| 230 | |
| 231 | inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } |
| 232 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 233 | inline float getRawX(size_t pointerIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 234 | return getCurrentPointerCoords(pointerIndex).x; |
| 235 | } |
| 236 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 237 | inline float getRawY(size_t pointerIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 238 | return getCurrentPointerCoords(pointerIndex).y; |
| 239 | } |
| 240 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 241 | inline float getX(size_t pointerIndex) const { |
| 242 | return getRawX(pointerIndex) + mXOffset; |
| 243 | } |
| 244 | |
| 245 | inline float getY(size_t pointerIndex) const { |
| 246 | return getRawY(pointerIndex) + mYOffset; |
| 247 | } |
| 248 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 249 | inline float getPressure(size_t pointerIndex) const { |
| 250 | return getCurrentPointerCoords(pointerIndex).pressure; |
| 251 | } |
| 252 | |
| 253 | inline float getSize(size_t pointerIndex) const { |
| 254 | return getCurrentPointerCoords(pointerIndex).size; |
| 255 | } |
| 256 | |
| 257 | inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } |
| 258 | |
| 259 | inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { |
| 260 | return mSampleEventTimes[historicalIndex]; |
| 261 | } |
| 262 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 263 | inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 264 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).x; |
| 265 | } |
| 266 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 267 | inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 268 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).y; |
| 269 | } |
| 270 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 271 | inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { |
| 272 | return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset; |
| 273 | } |
| 274 | |
| 275 | inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { |
| 276 | return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset; |
| 277 | } |
| 278 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 279 | inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { |
| 280 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure; |
| 281 | } |
| 282 | |
| 283 | inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { |
| 284 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).size; |
| 285 | } |
| 286 | |
| 287 | void initialize( |
| 288 | int32_t deviceId, |
| 289 | int32_t nature, |
| 290 | int32_t action, |
| 291 | int32_t edgeFlags, |
| 292 | int32_t metaState, |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 293 | float xOffset, |
| 294 | float yOffset, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 295 | float xPrecision, |
| 296 | float yPrecision, |
| 297 | nsecs_t downTime, |
| 298 | nsecs_t eventTime, |
| 299 | size_t pointerCount, |
| 300 | const int32_t* pointerIds, |
| 301 | const PointerCoords* pointerCoords); |
| 302 | |
| 303 | void addSample( |
| 304 | nsecs_t eventTime, |
| 305 | const PointerCoords* pointerCoords); |
| 306 | |
| 307 | void offsetLocation(float xOffset, float yOffset); |
| 308 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 309 | // Low-level accessors. |
| 310 | inline const int32_t* getPointerIds() const { return mPointerIds.array(); } |
| 311 | inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } |
| 312 | inline const PointerCoords* getSamplePointerCoords() const { |
| 313 | return mSamplePointerCoords.array(); |
| 314 | } |
| 315 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 316 | private: |
| 317 | int32_t mAction; |
| 318 | int32_t mEdgeFlags; |
| 319 | int32_t mMetaState; |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 320 | float mXOffset; |
| 321 | float mYOffset; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 322 | float mXPrecision; |
| 323 | float mYPrecision; |
| 324 | nsecs_t mDownTime; |
| 325 | Vector<int32_t> mPointerIds; |
| 326 | Vector<nsecs_t> mSampleEventTimes; |
| 327 | Vector<PointerCoords> mSamplePointerCoords; |
| 328 | |
| 329 | inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const { |
| 330 | return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 331 | } |
| 332 | |
| 333 | inline const PointerCoords& getHistoricalPointerCoords( |
| 334 | size_t pointerIndex, size_t historicalIndex) const { |
| 335 | return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | /* |
| 340 | * Input event factory. |
| 341 | */ |
| 342 | class InputEventFactoryInterface { |
| 343 | protected: |
| 344 | virtual ~InputEventFactoryInterface() { } |
| 345 | |
| 346 | public: |
| 347 | InputEventFactoryInterface() { } |
| 348 | |
| 349 | virtual KeyEvent* createKeyEvent() = 0; |
| 350 | virtual MotionEvent* createMotionEvent() = 0; |
| 351 | }; |
| 352 | |
| 353 | /* |
| 354 | * A simple input event factory implementation that uses a single preallocated instance |
| 355 | * of each type of input event that are reused for each request. |
| 356 | */ |
| 357 | class PreallocatedInputEventFactory : public InputEventFactoryInterface { |
| 358 | public: |
| 359 | PreallocatedInputEventFactory() { } |
| 360 | virtual ~PreallocatedInputEventFactory() { } |
| 361 | |
| 362 | virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } |
| 363 | virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } |
| 364 | |
| 365 | private: |
| 366 | KeyEvent mKeyEvent; |
| 367 | MotionEvent mMotionEvent; |
| 368 | }; |
| 369 | |
| 370 | |
| 371 | } // namespace android |
| 372 | |
| 373 | #endif // _UI_INPUT_H |