blob: a2e0ba06a2c2447743aff8a0159bdd4e78827f83 [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 Hackborn2e9f93e2010-06-28 15:27:30 -070046struct AInputEvent { };
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070047
Jeff Brown46b9ac02010-04-22 18:58:52 -070048namespace android {
49
50/*
51 * A raw event as retrieved from the EventHub.
52 */
53struct 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 Brown46b9ac02010-04-22 18:58:52 -070066 */
67enum {
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 Brown7fbdc842010-06-17 20:52:56 -070079 POLICY_FLAG_RAW_MASK = 0x0000ffff,
80
Jeff Brown9c3cda02010-06-15 01:31:58 -070081 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070082
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 Brown9c3cda02010-06-15 01:31:58 -070093 * Describes the basic configuration of input devices that are present.
94 */
95struct 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 Brown46b9ac02010-04-22 18:58:52 -0700124 * Pointer coordinate data.
125 */
126struct PointerCoords {
127 float x;
128 float y;
129 float pressure;
130 float size;
131};
132
133/*
134 * Input events.
135 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700136class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700137public:
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; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700145
Jeff Brown46b9ac02010-04-22 18:58:52 -0700146protected:
147 void initialize(int32_t deviceId, int32_t nature);
148
149private:
150 int32_t mDeviceId;
151 int32_t mNature;
152};
153
Jeff Brown5c225b12010-06-16 01:53:36 -0700154/*
155 * Key events.
156 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700157class KeyEvent : public InputEvent {
158public:
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
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700179 // Return true if this event may have a default action implementation.
180 static bool hasDefaultAction(int32_t keyCode);
181 bool hasDefaultAction() const;
182
183 // Return true if this event represents a system key.
184 static bool isSystemKey(int32_t keyCode);
185 bool isSystemKey() const;
186
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187 void initialize(
188 int32_t deviceId,
189 int32_t nature,
190 int32_t action,
191 int32_t flags,
192 int32_t keyCode,
193 int32_t scanCode,
194 int32_t metaState,
195 int32_t repeatCount,
196 nsecs_t downTime,
197 nsecs_t eventTime);
198
199private:
200 int32_t mAction;
201 int32_t mFlags;
202 int32_t mKeyCode;
203 int32_t mScanCode;
204 int32_t mMetaState;
205 int32_t mRepeatCount;
206 nsecs_t mDownTime;
207 nsecs_t mEventTime;
208};
209
Jeff Brown5c225b12010-06-16 01:53:36 -0700210/*
211 * Motion events.
212 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213class MotionEvent : public InputEvent {
214public:
215 virtual ~MotionEvent() { }
216
217 virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; }
218
219 inline int32_t getAction() const { return mAction; }
220
221 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
222
223 inline int32_t getMetaState() const { return mMetaState; }
224
Jeff Brown5c225b12010-06-16 01:53:36 -0700225 inline float getXOffset() const { return mXOffset; }
226
227 inline float getYOffset() const { return mYOffset; }
228
Jeff Brown46b9ac02010-04-22 18:58:52 -0700229 inline float getXPrecision() const { return mXPrecision; }
230
231 inline float getYPrecision() const { return mYPrecision; }
232
233 inline nsecs_t getDownTime() const { return mDownTime; }
234
235 inline size_t getPointerCount() const { return mPointerIds.size(); }
236
237 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
238
239 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
240
Jeff Brown5c225b12010-06-16 01:53:36 -0700241 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700242 return getCurrentPointerCoords(pointerIndex).x;
243 }
244
Jeff Brown5c225b12010-06-16 01:53:36 -0700245 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246 return getCurrentPointerCoords(pointerIndex).y;
247 }
248
Jeff Brown5c225b12010-06-16 01:53:36 -0700249 inline float getX(size_t pointerIndex) const {
250 return getRawX(pointerIndex) + mXOffset;
251 }
252
253 inline float getY(size_t pointerIndex) const {
254 return getRawY(pointerIndex) + mYOffset;
255 }
256
Jeff Brown46b9ac02010-04-22 18:58:52 -0700257 inline float getPressure(size_t pointerIndex) const {
258 return getCurrentPointerCoords(pointerIndex).pressure;
259 }
260
261 inline float getSize(size_t pointerIndex) const {
262 return getCurrentPointerCoords(pointerIndex).size;
263 }
264
265 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
266
267 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
268 return mSampleEventTimes[historicalIndex];
269 }
270
Jeff Brown5c225b12010-06-16 01:53:36 -0700271 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700272 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
273 }
274
Jeff Brown5c225b12010-06-16 01:53:36 -0700275 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700276 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
277 }
278
Jeff Brown5c225b12010-06-16 01:53:36 -0700279 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
280 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
281 }
282
283 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
284 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
285 }
286
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
288 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
289 }
290
291 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
292 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
293 }
294
295 void initialize(
296 int32_t deviceId,
297 int32_t nature,
298 int32_t action,
299 int32_t edgeFlags,
300 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700301 float xOffset,
302 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700303 float xPrecision,
304 float yPrecision,
305 nsecs_t downTime,
306 nsecs_t eventTime,
307 size_t pointerCount,
308 const int32_t* pointerIds,
309 const PointerCoords* pointerCoords);
310
311 void addSample(
312 nsecs_t eventTime,
313 const PointerCoords* pointerCoords);
314
315 void offsetLocation(float xOffset, float yOffset);
316
Jeff Brown5c225b12010-06-16 01:53:36 -0700317 // Low-level accessors.
318 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
319 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
320 inline const PointerCoords* getSamplePointerCoords() const {
321 return mSamplePointerCoords.array();
322 }
323
Jeff Brown46b9ac02010-04-22 18:58:52 -0700324private:
325 int32_t mAction;
326 int32_t mEdgeFlags;
327 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700328 float mXOffset;
329 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700330 float mXPrecision;
331 float mYPrecision;
332 nsecs_t mDownTime;
333 Vector<int32_t> mPointerIds;
334 Vector<nsecs_t> mSampleEventTimes;
335 Vector<PointerCoords> mSamplePointerCoords;
336
337 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
338 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
339 }
340
341 inline const PointerCoords& getHistoricalPointerCoords(
342 size_t pointerIndex, size_t historicalIndex) const {
343 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
344 }
345};
346
347/*
348 * Input event factory.
349 */
350class InputEventFactoryInterface {
351protected:
352 virtual ~InputEventFactoryInterface() { }
353
354public:
355 InputEventFactoryInterface() { }
356
357 virtual KeyEvent* createKeyEvent() = 0;
358 virtual MotionEvent* createMotionEvent() = 0;
359};
360
361/*
362 * A simple input event factory implementation that uses a single preallocated instance
363 * of each type of input event that are reused for each request.
364 */
365class PreallocatedInputEventFactory : public InputEventFactoryInterface {
366public:
367 PreallocatedInputEventFactory() { }
368 virtual ~PreallocatedInputEventFactory() { }
369
370 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
371 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
372
373private:
374 KeyEvent mKeyEvent;
375 MotionEvent mMotionEvent;
376};
377
378
379} // namespace android
380
381#endif // _UI_INPUT_H