blob: f06988832d71bc7a3399cfff68a33c7281752da4 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
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 */
31enum {
32 /*
33 * Private control to determine when an app is tracking a key sequence.
34 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070035 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000
Jeff Brown46b9ac02010-04-22 18:58:52 -070036};
37
38/*
39 * Maximum number of pointers supported per motion event.
40 */
41#define MAX_POINTERS 10
42
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070043/*
44 * Declare a concrete type for the NDK's input event forward declaration.
45 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070046struct AInputEvent {
47 virtual ~AInputEvent() { }
48};
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070049
Jeff Brown46b9ac02010-04-22 18:58:52 -070050namespace android {
51
52/*
53 * A raw event as retrieved from the EventHub.
54 */
55struct RawEvent {
56 nsecs_t when;
57 int32_t deviceId;
58 int32_t type;
59 int32_t scanCode;
60 int32_t keyCode;
61 int32_t value;
62 uint32_t flags;
63};
64
65/*
66 * Flags that flow alongside events in the input dispatch system to help with certain
67 * policy decisions such as waking from device sleep.
Jeff Brown46b9ac02010-04-22 18:58:52 -070068 */
69enum {
70 /* These flags originate in RawEvents and are generally set in the key map. */
71
72 POLICY_FLAG_WAKE = 0x00000001,
73 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
74 POLICY_FLAG_SHIFT = 0x00000004,
75 POLICY_FLAG_CAPS_LOCK = 0x00000008,
76 POLICY_FLAG_ALT = 0x00000010,
77 POLICY_FLAG_ALT_GR = 0x00000020,
78 POLICY_FLAG_MENU = 0x00000040,
79 POLICY_FLAG_LAUNCHER = 0x00000080,
80
Jeff Brown7fbdc842010-06-17 20:52:56 -070081 POLICY_FLAG_RAW_MASK = 0x0000ffff,
82
Jeff Brown9c3cda02010-06-15 01:31:58 -070083 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070084
85 // Indicates that the screen was off when the event was received and the event
86 // should wake the device.
87 POLICY_FLAG_WOKE_HERE = 0x10000000,
88
89 // Indicates that the screen was dim when the event was received and the event
90 // should brighten the device.
91 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
92};
93
94/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070095 * Describes the basic configuration of input devices that are present.
96 */
97struct InputConfiguration {
98 enum {
99 TOUCHSCREEN_UNDEFINED = 0,
100 TOUCHSCREEN_NOTOUCH = 1,
101 TOUCHSCREEN_STYLUS = 2,
102 TOUCHSCREEN_FINGER = 3
103 };
104
105 enum {
106 KEYBOARD_UNDEFINED = 0,
107 KEYBOARD_NOKEYS = 1,
108 KEYBOARD_QWERTY = 2,
109 KEYBOARD_12KEY = 3
110 };
111
112 enum {
113 NAVIGATION_UNDEFINED = 0,
114 NAVIGATION_NONAV = 1,
115 NAVIGATION_DPAD = 2,
116 NAVIGATION_TRACKBALL = 3,
117 NAVIGATION_WHEEL = 4
118 };
119
120 int32_t touchScreen;
121 int32_t keyboard;
122 int32_t navigation;
123};
124
125/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700126 * Pointer coordinate data.
127 */
128struct PointerCoords {
129 float x;
130 float y;
131 float pressure;
132 float size;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700133 float touchMajor;
134 float touchMinor;
135 float toolMajor;
136 float toolMinor;
137 float orientation;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700138};
139
140/*
141 * Input events.
142 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700143class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700144public:
145 virtual ~InputEvent() { }
146
147 virtual int32_t getType() const = 0;
148
149 inline int32_t getDeviceId() const { return mDeviceId; }
150
Jeff Brownc5ed5912010-07-14 18:48:53 -0700151 inline int32_t getSource() const { return mSource; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700152
Jeff Brown46b9ac02010-04-22 18:58:52 -0700153protected:
Jeff Brownc5ed5912010-07-14 18:48:53 -0700154 void initialize(int32_t deviceId, int32_t source);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700155
156private:
157 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700158 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700159};
160
Jeff Brown5c225b12010-06-16 01:53:36 -0700161/*
162 * Key events.
163 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700164class KeyEvent : public InputEvent {
165public:
166 virtual ~KeyEvent() { }
167
Jeff Brownc5ed5912010-07-14 18:48:53 -0700168 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700169
170 inline int32_t getAction() const { return mAction; }
171
172 inline int32_t getFlags() const { return mFlags; }
173
174 inline int32_t getKeyCode() const { return mKeyCode; }
175
176 inline int32_t getScanCode() const { return mScanCode; }
177
178 inline int32_t getMetaState() const { return mMetaState; }
179
180 inline int32_t getRepeatCount() const { return mRepeatCount; }
181
182 inline nsecs_t getDownTime() const { return mDownTime; }
183
184 inline nsecs_t getEventTime() const { return mEventTime; }
185
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700186 // Return true if this event may have a default action implementation.
187 static bool hasDefaultAction(int32_t keyCode);
188 bool hasDefaultAction() const;
189
190 // Return true if this event represents a system key.
191 static bool isSystemKey(int32_t keyCode);
192 bool isSystemKey() const;
193
Jeff Brown46b9ac02010-04-22 18:58:52 -0700194 void initialize(
195 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700196 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700197 int32_t action,
198 int32_t flags,
199 int32_t keyCode,
200 int32_t scanCode,
201 int32_t metaState,
202 int32_t repeatCount,
203 nsecs_t downTime,
204 nsecs_t eventTime);
205
206private:
207 int32_t mAction;
208 int32_t mFlags;
209 int32_t mKeyCode;
210 int32_t mScanCode;
211 int32_t mMetaState;
212 int32_t mRepeatCount;
213 nsecs_t mDownTime;
214 nsecs_t mEventTime;
215};
216
Jeff Brown5c225b12010-06-16 01:53:36 -0700217/*
218 * Motion events.
219 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700220class MotionEvent : public InputEvent {
221public:
222 virtual ~MotionEvent() { }
223
Jeff Brownc5ed5912010-07-14 18:48:53 -0700224 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225
226 inline int32_t getAction() const { return mAction; }
227
228 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
229
230 inline int32_t getMetaState() const { return mMetaState; }
231
Jeff Brown5c225b12010-06-16 01:53:36 -0700232 inline float getXOffset() const { return mXOffset; }
233
234 inline float getYOffset() const { return mYOffset; }
235
Jeff Brown46b9ac02010-04-22 18:58:52 -0700236 inline float getXPrecision() const { return mXPrecision; }
237
238 inline float getYPrecision() const { return mYPrecision; }
239
240 inline nsecs_t getDownTime() const { return mDownTime; }
241
242 inline size_t getPointerCount() const { return mPointerIds.size(); }
243
244 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
245
246 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
247
Jeff Brown5c225b12010-06-16 01:53:36 -0700248 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700249 return getCurrentPointerCoords(pointerIndex).x;
250 }
251
Jeff Brown5c225b12010-06-16 01:53:36 -0700252 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700253 return getCurrentPointerCoords(pointerIndex).y;
254 }
255
Jeff Brown5c225b12010-06-16 01:53:36 -0700256 inline float getX(size_t pointerIndex) const {
257 return getRawX(pointerIndex) + mXOffset;
258 }
259
260 inline float getY(size_t pointerIndex) const {
261 return getRawY(pointerIndex) + mYOffset;
262 }
263
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 inline float getPressure(size_t pointerIndex) const {
265 return getCurrentPointerCoords(pointerIndex).pressure;
266 }
267
268 inline float getSize(size_t pointerIndex) const {
269 return getCurrentPointerCoords(pointerIndex).size;
270 }
271
Jeff Brownc5ed5912010-07-14 18:48:53 -0700272 inline float getTouchMajor(size_t pointerIndex) const {
273 return getCurrentPointerCoords(pointerIndex).touchMajor;
274 }
275
276 inline float getTouchMinor(size_t pointerIndex) const {
277 return getCurrentPointerCoords(pointerIndex).touchMinor;
278 }
279
280 inline float getToolMajor(size_t pointerIndex) const {
281 return getCurrentPointerCoords(pointerIndex).toolMajor;
282 }
283
284 inline float getToolMinor(size_t pointerIndex) const {
285 return getCurrentPointerCoords(pointerIndex).toolMinor;
286 }
287
288 inline float getOrientation(size_t pointerIndex) const {
289 return getCurrentPointerCoords(pointerIndex).orientation;
290 }
291
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
293
294 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
295 return mSampleEventTimes[historicalIndex];
296 }
297
Jeff Brown5c225b12010-06-16 01:53:36 -0700298 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700299 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
300 }
301
Jeff Brown5c225b12010-06-16 01:53:36 -0700302 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700303 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
304 }
305
Jeff Brown5c225b12010-06-16 01:53:36 -0700306 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
307 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
308 }
309
310 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
311 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
312 }
313
Jeff Brown46b9ac02010-04-22 18:58:52 -0700314 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
315 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
316 }
317
318 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
319 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
320 }
321
Jeff Brownc5ed5912010-07-14 18:48:53 -0700322 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
323 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
324 }
325
326 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
327 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
328 }
329
330 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
331 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
332 }
333
334 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
335 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
336 }
337
338 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
339 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
340 }
341
Jeff Brown46b9ac02010-04-22 18:58:52 -0700342 void initialize(
343 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700344 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345 int32_t action,
346 int32_t edgeFlags,
347 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700348 float xOffset,
349 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700350 float xPrecision,
351 float yPrecision,
352 nsecs_t downTime,
353 nsecs_t eventTime,
354 size_t pointerCount,
355 const int32_t* pointerIds,
356 const PointerCoords* pointerCoords);
357
358 void addSample(
359 nsecs_t eventTime,
360 const PointerCoords* pointerCoords);
361
362 void offsetLocation(float xOffset, float yOffset);
363
Jeff Brown5c225b12010-06-16 01:53:36 -0700364 // Low-level accessors.
365 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
366 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
367 inline const PointerCoords* getSamplePointerCoords() const {
368 return mSamplePointerCoords.array();
369 }
370
Jeff Brown46b9ac02010-04-22 18:58:52 -0700371private:
372 int32_t mAction;
373 int32_t mEdgeFlags;
374 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700375 float mXOffset;
376 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700377 float mXPrecision;
378 float mYPrecision;
379 nsecs_t mDownTime;
380 Vector<int32_t> mPointerIds;
381 Vector<nsecs_t> mSampleEventTimes;
382 Vector<PointerCoords> mSamplePointerCoords;
383
384 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
385 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
386 }
387
388 inline const PointerCoords& getHistoricalPointerCoords(
389 size_t pointerIndex, size_t historicalIndex) const {
390 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
391 }
392};
393
394/*
395 * Input event factory.
396 */
397class InputEventFactoryInterface {
398protected:
399 virtual ~InputEventFactoryInterface() { }
400
401public:
402 InputEventFactoryInterface() { }
403
404 virtual KeyEvent* createKeyEvent() = 0;
405 virtual MotionEvent* createMotionEvent() = 0;
406};
407
408/*
409 * A simple input event factory implementation that uses a single preallocated instance
410 * of each type of input event that are reused for each request.
411 */
412class PreallocatedInputEventFactory : public InputEventFactoryInterface {
413public:
414 PreallocatedInputEventFactory() { }
415 virtual ~PreallocatedInputEventFactory() { }
416
417 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
418 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
419
420private:
421 KeyEvent mKeyEvent;
422 MotionEvent mMotionEvent;
423};
424
425
426} // namespace android
427
428#endif // _UI_INPUT_H