blob: a7d23d48548dbf4399ad85eef9270dec308e27fd [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 */
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 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;
133};
134
135/*
136 * Input events.
137 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700138class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700139public:
140 virtual ~InputEvent() { }
141
142 virtual int32_t getType() const = 0;
143
144 inline int32_t getDeviceId() const { return mDeviceId; }
145
146 inline int32_t getNature() const { return mNature; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700147
Jeff Brown46b9ac02010-04-22 18:58:52 -0700148protected:
149 void initialize(int32_t deviceId, int32_t nature);
150
151private:
152 int32_t mDeviceId;
153 int32_t mNature;
154};
155
Jeff Brown5c225b12010-06-16 01:53:36 -0700156/*
157 * Key events.
158 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700159class KeyEvent : public InputEvent {
160public:
161 virtual ~KeyEvent() { }
162
163 virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; }
164
165 inline int32_t getAction() const { return mAction; }
166
167 inline int32_t getFlags() const { return mFlags; }
168
169 inline int32_t getKeyCode() const { return mKeyCode; }
170
171 inline int32_t getScanCode() const { return mScanCode; }
172
173 inline int32_t getMetaState() const { return mMetaState; }
174
175 inline int32_t getRepeatCount() const { return mRepeatCount; }
176
177 inline nsecs_t getDownTime() const { return mDownTime; }
178
179 inline nsecs_t getEventTime() const { return mEventTime; }
180
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700181 // Return true if this event may have a default action implementation.
182 static bool hasDefaultAction(int32_t keyCode);
183 bool hasDefaultAction() const;
184
185 // Return true if this event represents a system key.
186 static bool isSystemKey(int32_t keyCode);
187 bool isSystemKey() const;
188
Jeff Brown46b9ac02010-04-22 18:58:52 -0700189 void initialize(
190 int32_t deviceId,
191 int32_t nature,
192 int32_t action,
193 int32_t flags,
194 int32_t keyCode,
195 int32_t scanCode,
196 int32_t metaState,
197 int32_t repeatCount,
198 nsecs_t downTime,
199 nsecs_t eventTime);
200
201private:
202 int32_t mAction;
203 int32_t mFlags;
204 int32_t mKeyCode;
205 int32_t mScanCode;
206 int32_t mMetaState;
207 int32_t mRepeatCount;
208 nsecs_t mDownTime;
209 nsecs_t mEventTime;
210};
211
Jeff Brown5c225b12010-06-16 01:53:36 -0700212/*
213 * Motion events.
214 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215class MotionEvent : public InputEvent {
216public:
217 virtual ~MotionEvent() { }
218
219 virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; }
220
221 inline int32_t getAction() const { return mAction; }
222
223 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
224
225 inline int32_t getMetaState() const { return mMetaState; }
226
Jeff Brown5c225b12010-06-16 01:53:36 -0700227 inline float getXOffset() const { return mXOffset; }
228
229 inline float getYOffset() const { return mYOffset; }
230
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231 inline float getXPrecision() const { return mXPrecision; }
232
233 inline float getYPrecision() const { return mYPrecision; }
234
235 inline nsecs_t getDownTime() const { return mDownTime; }
236
237 inline size_t getPointerCount() const { return mPointerIds.size(); }
238
239 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
240
241 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
242
Jeff Brown5c225b12010-06-16 01:53:36 -0700243 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700244 return getCurrentPointerCoords(pointerIndex).x;
245 }
246
Jeff Brown5c225b12010-06-16 01:53:36 -0700247 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700248 return getCurrentPointerCoords(pointerIndex).y;
249 }
250
Jeff Brown5c225b12010-06-16 01:53:36 -0700251 inline float getX(size_t pointerIndex) const {
252 return getRawX(pointerIndex) + mXOffset;
253 }
254
255 inline float getY(size_t pointerIndex) const {
256 return getRawY(pointerIndex) + mYOffset;
257 }
258
Jeff Brown46b9ac02010-04-22 18:58:52 -0700259 inline float getPressure(size_t pointerIndex) const {
260 return getCurrentPointerCoords(pointerIndex).pressure;
261 }
262
263 inline float getSize(size_t pointerIndex) const {
264 return getCurrentPointerCoords(pointerIndex).size;
265 }
266
267 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
268
269 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
270 return mSampleEventTimes[historicalIndex];
271 }
272
Jeff Brown5c225b12010-06-16 01:53:36 -0700273 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700274 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
275 }
276
Jeff Brown5c225b12010-06-16 01:53:36 -0700277 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700278 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
279 }
280
Jeff Brown5c225b12010-06-16 01:53:36 -0700281 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
282 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
283 }
284
285 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
286 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
287 }
288
Jeff Brown46b9ac02010-04-22 18:58:52 -0700289 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
290 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
291 }
292
293 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
294 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
295 }
296
297 void initialize(
298 int32_t deviceId,
299 int32_t nature,
300 int32_t action,
301 int32_t edgeFlags,
302 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700303 float xOffset,
304 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700305 float xPrecision,
306 float yPrecision,
307 nsecs_t downTime,
308 nsecs_t eventTime,
309 size_t pointerCount,
310 const int32_t* pointerIds,
311 const PointerCoords* pointerCoords);
312
313 void addSample(
314 nsecs_t eventTime,
315 const PointerCoords* pointerCoords);
316
317 void offsetLocation(float xOffset, float yOffset);
318
Jeff Brown5c225b12010-06-16 01:53:36 -0700319 // Low-level accessors.
320 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
321 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
322 inline const PointerCoords* getSamplePointerCoords() const {
323 return mSamplePointerCoords.array();
324 }
325
Jeff Brown46b9ac02010-04-22 18:58:52 -0700326private:
327 int32_t mAction;
328 int32_t mEdgeFlags;
329 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700330 float mXOffset;
331 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700332 float mXPrecision;
333 float mYPrecision;
334 nsecs_t mDownTime;
335 Vector<int32_t> mPointerIds;
336 Vector<nsecs_t> mSampleEventTimes;
337 Vector<PointerCoords> mSamplePointerCoords;
338
339 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
340 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
341 }
342
343 inline const PointerCoords& getHistoricalPointerCoords(
344 size_t pointerIndex, size_t historicalIndex) const {
345 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
346 }
347};
348
349/*
350 * Input event factory.
351 */
352class InputEventFactoryInterface {
353protected:
354 virtual ~InputEventFactoryInterface() { }
355
356public:
357 InputEventFactoryInterface() { }
358
359 virtual KeyEvent* createKeyEvent() = 0;
360 virtual MotionEvent* createMotionEvent() = 0;
361};
362
363/*
364 * A simple input event factory implementation that uses a single preallocated instance
365 * of each type of input event that are reused for each request.
366 */
367class PreallocatedInputEventFactory : public InputEventFactoryInterface {
368public:
369 PreallocatedInputEventFactory() { }
370 virtual ~PreallocatedInputEventFactory() { }
371
372 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
373 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
374
375private:
376 KeyEvent mKeyEvent;
377 MotionEvent mMotionEvent;
378};
379
380
381} // namespace android
382
383#endif // _UI_INPUT_H