blob: 979d6e8f42ba7c84cd78fdfd2268fec425d2fdee [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
43namespace android {
44
45/*
46 * A raw event as retrieved from the EventHub.
47 */
48struct RawEvent {
49 nsecs_t when;
50 int32_t deviceId;
51 int32_t type;
52 int32_t scanCode;
53 int32_t keyCode;
54 int32_t value;
55 uint32_t flags;
56};
57
58/*
59 * Flags that flow alongside events in the input dispatch system to help with certain
60 * policy decisions such as waking from device sleep.
Jeff Brown46b9ac02010-04-22 18:58:52 -070061 */
62enum {
63 /* These flags originate in RawEvents and are generally set in the key map. */
64
65 POLICY_FLAG_WAKE = 0x00000001,
66 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
67 POLICY_FLAG_SHIFT = 0x00000004,
68 POLICY_FLAG_CAPS_LOCK = 0x00000008,
69 POLICY_FLAG_ALT = 0x00000010,
70 POLICY_FLAG_ALT_GR = 0x00000020,
71 POLICY_FLAG_MENU = 0x00000040,
72 POLICY_FLAG_LAUNCHER = 0x00000080,
73
Jeff Brown7fbdc842010-06-17 20:52:56 -070074 POLICY_FLAG_RAW_MASK = 0x0000ffff,
75
Jeff Brown9c3cda02010-06-15 01:31:58 -070076 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070077
78 // Indicates that the screen was off when the event was received and the event
79 // should wake the device.
80 POLICY_FLAG_WOKE_HERE = 0x10000000,
81
82 // Indicates that the screen was dim when the event was received and the event
83 // should brighten the device.
84 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
85};
86
87/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070088 * Describes the basic configuration of input devices that are present.
89 */
90struct InputConfiguration {
91 enum {
92 TOUCHSCREEN_UNDEFINED = 0,
93 TOUCHSCREEN_NOTOUCH = 1,
94 TOUCHSCREEN_STYLUS = 2,
95 TOUCHSCREEN_FINGER = 3
96 };
97
98 enum {
99 KEYBOARD_UNDEFINED = 0,
100 KEYBOARD_NOKEYS = 1,
101 KEYBOARD_QWERTY = 2,
102 KEYBOARD_12KEY = 3
103 };
104
105 enum {
106 NAVIGATION_UNDEFINED = 0,
107 NAVIGATION_NONAV = 1,
108 NAVIGATION_DPAD = 2,
109 NAVIGATION_TRACKBALL = 3,
110 NAVIGATION_WHEEL = 4
111 };
112
113 int32_t touchScreen;
114 int32_t keyboard;
115 int32_t navigation;
116};
117
118/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700119 * Pointer coordinate data.
120 */
121struct PointerCoords {
122 float x;
123 float y;
124 float pressure;
125 float size;
126};
127
128/*
129 * Input events.
130 */
131struct input_event_t { };
132
133class InputEvent : public input_event_t {
134public:
135 virtual ~InputEvent() { }
136
137 virtual int32_t getType() const = 0;
138
139 inline int32_t getDeviceId() const { return mDeviceId; }
140
141 inline int32_t getNature() const { return mNature; }
142
143protected:
144 void initialize(int32_t deviceId, int32_t nature);
145
146private:
147 int32_t mDeviceId;
148 int32_t mNature;
149};
150
Jeff Brown5c225b12010-06-16 01:53:36 -0700151/*
152 * Key events.
153 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700154class KeyEvent : public InputEvent {
155public:
156 virtual ~KeyEvent() { }
157
158 virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; }
159
160 inline int32_t getAction() const { return mAction; }
161
162 inline int32_t getFlags() const { return mFlags; }
163
164 inline int32_t getKeyCode() const { return mKeyCode; }
165
166 inline int32_t getScanCode() const { return mScanCode; }
167
168 inline int32_t getMetaState() const { return mMetaState; }
169
170 inline int32_t getRepeatCount() const { return mRepeatCount; }
171
172 inline nsecs_t getDownTime() const { return mDownTime; }
173
174 inline nsecs_t getEventTime() const { return mEventTime; }
175
176 void initialize(
177 int32_t deviceId,
178 int32_t nature,
179 int32_t action,
180 int32_t flags,
181 int32_t keyCode,
182 int32_t scanCode,
183 int32_t metaState,
184 int32_t repeatCount,
185 nsecs_t downTime,
186 nsecs_t eventTime);
187
188private:
189 int32_t mAction;
190 int32_t mFlags;
191 int32_t mKeyCode;
192 int32_t mScanCode;
193 int32_t mMetaState;
194 int32_t mRepeatCount;
195 nsecs_t mDownTime;
196 nsecs_t mEventTime;
197};
198
Jeff Brown5c225b12010-06-16 01:53:36 -0700199/*
200 * Motion events.
201 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700202class MotionEvent : public InputEvent {
203public:
204 virtual ~MotionEvent() { }
205
206 virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; }
207
208 inline int32_t getAction() const { return mAction; }
209
210 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
211
212 inline int32_t getMetaState() const { return mMetaState; }
213
Jeff Brown5c225b12010-06-16 01:53:36 -0700214 inline float getXOffset() const { return mXOffset; }
215
216 inline float getYOffset() const { return mYOffset; }
217
Jeff Brown46b9ac02010-04-22 18:58:52 -0700218 inline float getXPrecision() const { return mXPrecision; }
219
220 inline float getYPrecision() const { return mYPrecision; }
221
222 inline nsecs_t getDownTime() const { return mDownTime; }
223
224 inline size_t getPointerCount() const { return mPointerIds.size(); }
225
226 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
227
228 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
229
Jeff Brown5c225b12010-06-16 01:53:36 -0700230 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231 return getCurrentPointerCoords(pointerIndex).x;
232 }
233
Jeff Brown5c225b12010-06-16 01:53:36 -0700234 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700235 return getCurrentPointerCoords(pointerIndex).y;
236 }
237
Jeff Brown5c225b12010-06-16 01:53:36 -0700238 inline float getX(size_t pointerIndex) const {
239 return getRawX(pointerIndex) + mXOffset;
240 }
241
242 inline float getY(size_t pointerIndex) const {
243 return getRawY(pointerIndex) + mYOffset;
244 }
245
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246 inline float getPressure(size_t pointerIndex) const {
247 return getCurrentPointerCoords(pointerIndex).pressure;
248 }
249
250 inline float getSize(size_t pointerIndex) const {
251 return getCurrentPointerCoords(pointerIndex).size;
252 }
253
254 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
255
256 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
257 return mSampleEventTimes[historicalIndex];
258 }
259
Jeff Brown5c225b12010-06-16 01:53:36 -0700260 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700261 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
262 }
263
Jeff Brown5c225b12010-06-16 01:53:36 -0700264 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700265 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
266 }
267
Jeff Brown5c225b12010-06-16 01:53:36 -0700268 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
269 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
270 }
271
272 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
273 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
274 }
275
Jeff Brown46b9ac02010-04-22 18:58:52 -0700276 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
277 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
278 }
279
280 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
281 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
282 }
283
284 void initialize(
285 int32_t deviceId,
286 int32_t nature,
287 int32_t action,
288 int32_t edgeFlags,
289 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700290 float xOffset,
291 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 float xPrecision,
293 float yPrecision,
294 nsecs_t downTime,
295 nsecs_t eventTime,
296 size_t pointerCount,
297 const int32_t* pointerIds,
298 const PointerCoords* pointerCoords);
299
300 void addSample(
301 nsecs_t eventTime,
302 const PointerCoords* pointerCoords);
303
304 void offsetLocation(float xOffset, float yOffset);
305
Jeff Brown5c225b12010-06-16 01:53:36 -0700306 // Low-level accessors.
307 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
308 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
309 inline const PointerCoords* getSamplePointerCoords() const {
310 return mSamplePointerCoords.array();
311 }
312
Jeff Brown46b9ac02010-04-22 18:58:52 -0700313private:
314 int32_t mAction;
315 int32_t mEdgeFlags;
316 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700317 float mXOffset;
318 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700319 float mXPrecision;
320 float mYPrecision;
321 nsecs_t mDownTime;
322 Vector<int32_t> mPointerIds;
323 Vector<nsecs_t> mSampleEventTimes;
324 Vector<PointerCoords> mSamplePointerCoords;
325
326 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
327 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
328 }
329
330 inline const PointerCoords& getHistoricalPointerCoords(
331 size_t pointerIndex, size_t historicalIndex) const {
332 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
333 }
334};
335
336/*
337 * Input event factory.
338 */
339class InputEventFactoryInterface {
340protected:
341 virtual ~InputEventFactoryInterface() { }
342
343public:
344 InputEventFactoryInterface() { }
345
346 virtual KeyEvent* createKeyEvent() = 0;
347 virtual MotionEvent* createMotionEvent() = 0;
348};
349
350/*
351 * A simple input event factory implementation that uses a single preallocated instance
352 * of each type of input event that are reused for each request.
353 */
354class PreallocatedInputEventFactory : public InputEventFactoryInterface {
355public:
356 PreallocatedInputEventFactory() { }
357 virtual ~PreallocatedInputEventFactory() { }
358
359 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
360 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
361
362private:
363 KeyEvent mKeyEvent;
364 MotionEvent mMotionEvent;
365};
366
367
368} // namespace android
369
370#endif // _UI_INPUT_H