blob: d9b109197c8b52390d987a9ef81783979747c897 [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);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700155 void initialize(const InputEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700156
157private:
158 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700159 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700160};
161
Jeff Brown5c225b12010-06-16 01:53:36 -0700162/*
163 * Key events.
164 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700165class KeyEvent : public InputEvent {
166public:
167 virtual ~KeyEvent() { }
168
Jeff Brownc5ed5912010-07-14 18:48:53 -0700169 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700170
171 inline int32_t getAction() const { return mAction; }
172
173 inline int32_t getFlags() const { return mFlags; }
174
175 inline int32_t getKeyCode() const { return mKeyCode; }
176
177 inline int32_t getScanCode() const { return mScanCode; }
178
179 inline int32_t getMetaState() const { return mMetaState; }
180
181 inline int32_t getRepeatCount() const { return mRepeatCount; }
182
183 inline nsecs_t getDownTime() const { return mDownTime; }
184
185 inline nsecs_t getEventTime() const { return mEventTime; }
186
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700187 // Return true if this event may have a default action implementation.
188 static bool hasDefaultAction(int32_t keyCode);
189 bool hasDefaultAction() const;
190
191 // Return true if this event represents a system key.
192 static bool isSystemKey(int32_t keyCode);
193 bool isSystemKey() const;
194
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195 void initialize(
196 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700197 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700198 int32_t action,
199 int32_t flags,
200 int32_t keyCode,
201 int32_t scanCode,
202 int32_t metaState,
203 int32_t repeatCount,
204 nsecs_t downTime,
205 nsecs_t eventTime);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700206 void initialize(const KeyEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700207
208private:
209 int32_t mAction;
210 int32_t mFlags;
211 int32_t mKeyCode;
212 int32_t mScanCode;
213 int32_t mMetaState;
214 int32_t mRepeatCount;
215 nsecs_t mDownTime;
216 nsecs_t mEventTime;
217};
218
Jeff Brown5c225b12010-06-16 01:53:36 -0700219/*
220 * Motion events.
221 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700222class MotionEvent : public InputEvent {
223public:
224 virtual ~MotionEvent() { }
225
Jeff Brownc5ed5912010-07-14 18:48:53 -0700226 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700227
228 inline int32_t getAction() const { return mAction; }
229
230 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
231
232 inline int32_t getMetaState() const { return mMetaState; }
233
Jeff Brown5c225b12010-06-16 01:53:36 -0700234 inline float getXOffset() const { return mXOffset; }
235
236 inline float getYOffset() const { return mYOffset; }
237
Jeff Brown46b9ac02010-04-22 18:58:52 -0700238 inline float getXPrecision() const { return mXPrecision; }
239
240 inline float getYPrecision() const { return mYPrecision; }
241
242 inline nsecs_t getDownTime() const { return mDownTime; }
243
244 inline size_t getPointerCount() const { return mPointerIds.size(); }
245
246 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
247
248 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
249
Jeff Brown5c225b12010-06-16 01:53:36 -0700250 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700251 return getCurrentPointerCoords(pointerIndex).x;
252 }
253
Jeff Brown5c225b12010-06-16 01:53:36 -0700254 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700255 return getCurrentPointerCoords(pointerIndex).y;
256 }
257
Jeff Brown5c225b12010-06-16 01:53:36 -0700258 inline float getX(size_t pointerIndex) const {
259 return getRawX(pointerIndex) + mXOffset;
260 }
261
262 inline float getY(size_t pointerIndex) const {
263 return getRawY(pointerIndex) + mYOffset;
264 }
265
Jeff Brown46b9ac02010-04-22 18:58:52 -0700266 inline float getPressure(size_t pointerIndex) const {
267 return getCurrentPointerCoords(pointerIndex).pressure;
268 }
269
270 inline float getSize(size_t pointerIndex) const {
271 return getCurrentPointerCoords(pointerIndex).size;
272 }
273
Jeff Brownc5ed5912010-07-14 18:48:53 -0700274 inline float getTouchMajor(size_t pointerIndex) const {
275 return getCurrentPointerCoords(pointerIndex).touchMajor;
276 }
277
278 inline float getTouchMinor(size_t pointerIndex) const {
279 return getCurrentPointerCoords(pointerIndex).touchMinor;
280 }
281
282 inline float getToolMajor(size_t pointerIndex) const {
283 return getCurrentPointerCoords(pointerIndex).toolMajor;
284 }
285
286 inline float getToolMinor(size_t pointerIndex) const {
287 return getCurrentPointerCoords(pointerIndex).toolMinor;
288 }
289
290 inline float getOrientation(size_t pointerIndex) const {
291 return getCurrentPointerCoords(pointerIndex).orientation;
292 }
293
Jeff Brown46b9ac02010-04-22 18:58:52 -0700294 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
295
296 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
297 return mSampleEventTimes[historicalIndex];
298 }
299
Jeff Brown5c225b12010-06-16 01:53:36 -0700300 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
302 }
303
Jeff Brown5c225b12010-06-16 01:53:36 -0700304 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700305 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
306 }
307
Jeff Brown5c225b12010-06-16 01:53:36 -0700308 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
309 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
310 }
311
312 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
313 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
314 }
315
Jeff Brown46b9ac02010-04-22 18:58:52 -0700316 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
317 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
318 }
319
320 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
321 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
322 }
323
Jeff Brownc5ed5912010-07-14 18:48:53 -0700324 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
325 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
326 }
327
328 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
329 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
330 }
331
332 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
333 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
334 }
335
336 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
337 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
338 }
339
340 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
341 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
342 }
343
Jeff Brown46b9ac02010-04-22 18:58:52 -0700344 void initialize(
345 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700346 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700347 int32_t action,
348 int32_t edgeFlags,
349 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700350 float xOffset,
351 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700352 float xPrecision,
353 float yPrecision,
354 nsecs_t downTime,
355 nsecs_t eventTime,
356 size_t pointerCount,
357 const int32_t* pointerIds,
358 const PointerCoords* pointerCoords);
359
360 void addSample(
361 nsecs_t eventTime,
362 const PointerCoords* pointerCoords);
363
364 void offsetLocation(float xOffset, float yOffset);
365
Jeff Brown5c225b12010-06-16 01:53:36 -0700366 // Low-level accessors.
367 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
368 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
369 inline const PointerCoords* getSamplePointerCoords() const {
370 return mSamplePointerCoords.array();
371 }
372
Jeff Brown46b9ac02010-04-22 18:58:52 -0700373private:
374 int32_t mAction;
375 int32_t mEdgeFlags;
376 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700377 float mXOffset;
378 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700379 float mXPrecision;
380 float mYPrecision;
381 nsecs_t mDownTime;
382 Vector<int32_t> mPointerIds;
383 Vector<nsecs_t> mSampleEventTimes;
384 Vector<PointerCoords> mSamplePointerCoords;
385
386 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
387 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
388 }
389
390 inline const PointerCoords& getHistoricalPointerCoords(
391 size_t pointerIndex, size_t historicalIndex) const {
392 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
393 }
394};
395
396/*
397 * Input event factory.
398 */
399class InputEventFactoryInterface {
400protected:
401 virtual ~InputEventFactoryInterface() { }
402
403public:
404 InputEventFactoryInterface() { }
405
406 virtual KeyEvent* createKeyEvent() = 0;
407 virtual MotionEvent* createMotionEvent() = 0;
408};
409
410/*
411 * A simple input event factory implementation that uses a single preallocated instance
412 * of each type of input event that are reused for each request.
413 */
414class PreallocatedInputEventFactory : public InputEventFactoryInterface {
415public:
416 PreallocatedInputEventFactory() { }
417 virtual ~PreallocatedInputEventFactory() { }
418
419 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
420 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
421
422private:
423 KeyEvent mKeyEvent;
424 MotionEvent mMotionEvent;
425};
426
427
428} // namespace android
429
430#endif // _UI_INPUT_H