blob: d45bfcfda946cdb6faea6795a0cc253020cc9830 [file] [log] [blame]
Jeff Brown46b9ac0a2010-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.
61 *
62 * TODO This enumeration should probably be split up or relabeled for clarity.
63 */
64enum {
65 /* These flags originate in RawEvents and are generally set in the key map. */
66
67 POLICY_FLAG_WAKE = 0x00000001,
68 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
69 POLICY_FLAG_SHIFT = 0x00000004,
70 POLICY_FLAG_CAPS_LOCK = 0x00000008,
71 POLICY_FLAG_ALT = 0x00000010,
72 POLICY_FLAG_ALT_GR = 0x00000020,
73 POLICY_FLAG_MENU = 0x00000040,
74 POLICY_FLAG_LAUNCHER = 0x00000080,
75
Jeff Brown9c3cda02010-06-15 01:31:58 -070076 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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
151class KeyEvent : public InputEvent {
152public:
153 virtual ~KeyEvent() { }
154
155 virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; }
156
157 inline int32_t getAction() const { return mAction; }
158
159 inline int32_t getFlags() const { return mFlags; }
160
161 inline int32_t getKeyCode() const { return mKeyCode; }
162
163 inline int32_t getScanCode() const { return mScanCode; }
164
165 inline int32_t getMetaState() const { return mMetaState; }
166
167 inline int32_t getRepeatCount() const { return mRepeatCount; }
168
169 inline nsecs_t getDownTime() const { return mDownTime; }
170
171 inline nsecs_t getEventTime() const { return mEventTime; }
172
173 void initialize(
174 int32_t deviceId,
175 int32_t nature,
176 int32_t action,
177 int32_t flags,
178 int32_t keyCode,
179 int32_t scanCode,
180 int32_t metaState,
181 int32_t repeatCount,
182 nsecs_t downTime,
183 nsecs_t eventTime);
184
185private:
186 int32_t mAction;
187 int32_t mFlags;
188 int32_t mKeyCode;
189 int32_t mScanCode;
190 int32_t mMetaState;
191 int32_t mRepeatCount;
192 nsecs_t mDownTime;
193 nsecs_t mEventTime;
194};
195
196class MotionEvent : public InputEvent {
197public:
198 virtual ~MotionEvent() { }
199
200 virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; }
201
202 inline int32_t getAction() const { return mAction; }
203
204 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
205
206 inline int32_t getMetaState() const { return mMetaState; }
207
208 inline float getXPrecision() const { return mXPrecision; }
209
210 inline float getYPrecision() const { return mYPrecision; }
211
212 inline nsecs_t getDownTime() const { return mDownTime; }
213
214 inline size_t getPointerCount() const { return mPointerIds.size(); }
215
216 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
217
218 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
219
220 inline float getRawX() const { return mRawX; }
221
222 inline float getRawY() const { return mRawY; }
223
224 inline float getX(size_t pointerIndex) const {
225 return getCurrentPointerCoords(pointerIndex).x;
226 }
227
228 inline float getY(size_t pointerIndex) const {
229 return getCurrentPointerCoords(pointerIndex).y;
230 }
231
232 inline float getPressure(size_t pointerIndex) const {
233 return getCurrentPointerCoords(pointerIndex).pressure;
234 }
235
236 inline float getSize(size_t pointerIndex) const {
237 return getCurrentPointerCoords(pointerIndex).size;
238 }
239
240 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
241
242 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
243 return mSampleEventTimes[historicalIndex];
244 }
245
246 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
247 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
248 }
249
250 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
251 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
252 }
253
254 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
255 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
256 }
257
258 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
259 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
260 }
261
262 void initialize(
263 int32_t deviceId,
264 int32_t nature,
265 int32_t action,
266 int32_t edgeFlags,
267 int32_t metaState,
268 float rawX,
269 float rawY,
270 float xPrecision,
271 float yPrecision,
272 nsecs_t downTime,
273 nsecs_t eventTime,
274 size_t pointerCount,
275 const int32_t* pointerIds,
276 const PointerCoords* pointerCoords);
277
278 void addSample(
279 nsecs_t eventTime,
280 const PointerCoords* pointerCoords);
281
282 void offsetLocation(float xOffset, float yOffset);
283
284private:
285 int32_t mAction;
286 int32_t mEdgeFlags;
287 int32_t mMetaState;
288 float mRawX;
289 float mRawY;
290 float mXPrecision;
291 float mYPrecision;
292 nsecs_t mDownTime;
293 Vector<int32_t> mPointerIds;
294 Vector<nsecs_t> mSampleEventTimes;
295 Vector<PointerCoords> mSamplePointerCoords;
296
297 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
298 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
299 }
300
301 inline const PointerCoords& getHistoricalPointerCoords(
302 size_t pointerIndex, size_t historicalIndex) const {
303 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
304 }
305};
306
307/*
308 * Input event factory.
309 */
310class InputEventFactoryInterface {
311protected:
312 virtual ~InputEventFactoryInterface() { }
313
314public:
315 InputEventFactoryInterface() { }
316
317 virtual KeyEvent* createKeyEvent() = 0;
318 virtual MotionEvent* createMotionEvent() = 0;
319};
320
321/*
322 * A simple input event factory implementation that uses a single preallocated instance
323 * of each type of input event that are reused for each request.
324 */
325class PreallocatedInputEventFactory : public InputEventFactoryInterface {
326public:
327 PreallocatedInputEventFactory() { }
328 virtual ~PreallocatedInputEventFactory() { }
329
330 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
331 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
332
333private:
334 KeyEvent mKeyEvent;
335 MotionEvent mMotionEvent;
336};
337
338
339} // namespace android
340
341#endif // _UI_INPUT_H