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, |
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 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 139 | class InputEvent : public input_event_t { |
| 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; } |
| 148 | |
| 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 | |
| 182 | void initialize( |
| 183 | int32_t deviceId, |
| 184 | int32_t nature, |
| 185 | int32_t action, |
| 186 | int32_t flags, |
| 187 | int32_t keyCode, |
| 188 | int32_t scanCode, |
| 189 | int32_t metaState, |
| 190 | int32_t repeatCount, |
| 191 | nsecs_t downTime, |
| 192 | nsecs_t eventTime); |
| 193 | |
| 194 | private: |
| 195 | int32_t mAction; |
| 196 | int32_t mFlags; |
| 197 | int32_t mKeyCode; |
| 198 | int32_t mScanCode; |
| 199 | int32_t mMetaState; |
| 200 | int32_t mRepeatCount; |
| 201 | nsecs_t mDownTime; |
| 202 | nsecs_t mEventTime; |
| 203 | }; |
| 204 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Motion events. |
| 207 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 208 | class MotionEvent : public InputEvent { |
| 209 | public: |
| 210 | virtual ~MotionEvent() { } |
| 211 | |
| 212 | virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; } |
| 213 | |
| 214 | inline int32_t getAction() const { return mAction; } |
| 215 | |
| 216 | inline int32_t getEdgeFlags() const { return mEdgeFlags; } |
| 217 | |
| 218 | inline int32_t getMetaState() const { return mMetaState; } |
| 219 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 220 | inline float getXOffset() const { return mXOffset; } |
| 221 | |
| 222 | inline float getYOffset() const { return mYOffset; } |
| 223 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 224 | inline float getXPrecision() const { return mXPrecision; } |
| 225 | |
| 226 | inline float getYPrecision() const { return mYPrecision; } |
| 227 | |
| 228 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 229 | |
| 230 | inline size_t getPointerCount() const { return mPointerIds.size(); } |
| 231 | |
| 232 | inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; } |
| 233 | |
| 234 | inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } |
| 235 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 236 | inline float getRawX(size_t pointerIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 237 | return getCurrentPointerCoords(pointerIndex).x; |
| 238 | } |
| 239 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 240 | inline float getRawY(size_t pointerIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 241 | return getCurrentPointerCoords(pointerIndex).y; |
| 242 | } |
| 243 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 244 | inline float getX(size_t pointerIndex) const { |
| 245 | return getRawX(pointerIndex) + mXOffset; |
| 246 | } |
| 247 | |
| 248 | inline float getY(size_t pointerIndex) const { |
| 249 | return getRawY(pointerIndex) + mYOffset; |
| 250 | } |
| 251 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 252 | inline float getPressure(size_t pointerIndex) const { |
| 253 | return getCurrentPointerCoords(pointerIndex).pressure; |
| 254 | } |
| 255 | |
| 256 | inline float getSize(size_t pointerIndex) const { |
| 257 | return getCurrentPointerCoords(pointerIndex).size; |
| 258 | } |
| 259 | |
| 260 | inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } |
| 261 | |
| 262 | inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { |
| 263 | return mSampleEventTimes[historicalIndex]; |
| 264 | } |
| 265 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 266 | inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 267 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).x; |
| 268 | } |
| 269 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 270 | inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 271 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).y; |
| 272 | } |
| 273 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 274 | inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { |
| 275 | return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset; |
| 276 | } |
| 277 | |
| 278 | inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { |
| 279 | return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset; |
| 280 | } |
| 281 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 282 | inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { |
| 283 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure; |
| 284 | } |
| 285 | |
| 286 | inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { |
| 287 | return getHistoricalPointerCoords(pointerIndex, historicalIndex).size; |
| 288 | } |
| 289 | |
| 290 | void initialize( |
| 291 | int32_t deviceId, |
| 292 | int32_t nature, |
| 293 | int32_t action, |
| 294 | int32_t edgeFlags, |
| 295 | int32_t metaState, |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 296 | float xOffset, |
| 297 | float yOffset, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 298 | float xPrecision, |
| 299 | float yPrecision, |
| 300 | nsecs_t downTime, |
| 301 | nsecs_t eventTime, |
| 302 | size_t pointerCount, |
| 303 | const int32_t* pointerIds, |
| 304 | const PointerCoords* pointerCoords); |
| 305 | |
| 306 | void addSample( |
| 307 | nsecs_t eventTime, |
| 308 | const PointerCoords* pointerCoords); |
| 309 | |
| 310 | void offsetLocation(float xOffset, float yOffset); |
| 311 | |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 312 | // Low-level accessors. |
| 313 | inline const int32_t* getPointerIds() const { return mPointerIds.array(); } |
| 314 | inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } |
| 315 | inline const PointerCoords* getSamplePointerCoords() const { |
| 316 | return mSamplePointerCoords.array(); |
| 317 | } |
| 318 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 319 | private: |
| 320 | int32_t mAction; |
| 321 | int32_t mEdgeFlags; |
| 322 | int32_t mMetaState; |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 323 | float mXOffset; |
| 324 | float mYOffset; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 325 | float mXPrecision; |
| 326 | float mYPrecision; |
| 327 | nsecs_t mDownTime; |
| 328 | Vector<int32_t> mPointerIds; |
| 329 | Vector<nsecs_t> mSampleEventTimes; |
| 330 | Vector<PointerCoords> mSamplePointerCoords; |
| 331 | |
| 332 | inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const { |
| 333 | return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 334 | } |
| 335 | |
| 336 | inline const PointerCoords& getHistoricalPointerCoords( |
| 337 | size_t pointerIndex, size_t historicalIndex) const { |
| 338 | return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | /* |
| 343 | * Input event factory. |
| 344 | */ |
| 345 | class InputEventFactoryInterface { |
| 346 | protected: |
| 347 | virtual ~InputEventFactoryInterface() { } |
| 348 | |
| 349 | public: |
| 350 | InputEventFactoryInterface() { } |
| 351 | |
| 352 | virtual KeyEvent* createKeyEvent() = 0; |
| 353 | virtual MotionEvent* createMotionEvent() = 0; |
| 354 | }; |
| 355 | |
| 356 | /* |
| 357 | * A simple input event factory implementation that uses a single preallocated instance |
| 358 | * of each type of input event that are reused for each request. |
| 359 | */ |
| 360 | class PreallocatedInputEventFactory : public InputEventFactoryInterface { |
| 361 | public: |
| 362 | PreallocatedInputEventFactory() { } |
| 363 | virtual ~PreallocatedInputEventFactory() { } |
| 364 | |
| 365 | virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } |
| 366 | virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } |
| 367 | |
| 368 | private: |
| 369 | KeyEvent mKeyEvent; |
| 370 | MotionEvent mMotionEvent; |
| 371 | }; |
| 372 | |
| 373 | |
| 374 | } // namespace android |
| 375 | |
| 376 | #endif // _UI_INPUT_H |