blob: 214f587c783599afd69113abb933559fbfb3b6a4 [file] [log] [blame]
Jeff Browne839a582010-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 Hackborn4d96bb62010-06-18 18:09:33 -070043/*
44 * Declare a concrete type for the NDK's input event forward declaration.
45 */
Dianne Hackborn9c7f8182010-06-28 15:27:30 -070046struct AInputEvent { };
Dianne Hackborn4d96bb62010-06-18 18:09:33 -070047
Jeff Browne839a582010-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 Browne839a582010-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 Brown51d45a72010-06-17 20:52:56 -070079 POLICY_FLAG_RAW_MASK = 0x0000ffff,
80
Jeff Brown54bc2812010-06-15 01:31:58 -070081 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Browne839a582010-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,
Jeff Brown50de30a2010-06-22 01:27:15 -070090
91 // Indicates that the dispatcher should call back into the policy before dispatching. */
92 POLICY_FLAG_INTERCEPT_DISPATCH = 0x40000000,
Jeff Browne839a582010-04-22 18:58:52 -070093};
94
95/*
Jeff Brown54bc2812010-06-15 01:31:58 -070096 * Describes the basic configuration of input devices that are present.
97 */
98struct InputConfiguration {
99 enum {
100 TOUCHSCREEN_UNDEFINED = 0,
101 TOUCHSCREEN_NOTOUCH = 1,
102 TOUCHSCREEN_STYLUS = 2,
103 TOUCHSCREEN_FINGER = 3
104 };
105
106 enum {
107 KEYBOARD_UNDEFINED = 0,
108 KEYBOARD_NOKEYS = 1,
109 KEYBOARD_QWERTY = 2,
110 KEYBOARD_12KEY = 3
111 };
112
113 enum {
114 NAVIGATION_UNDEFINED = 0,
115 NAVIGATION_NONAV = 1,
116 NAVIGATION_DPAD = 2,
117 NAVIGATION_TRACKBALL = 3,
118 NAVIGATION_WHEEL = 4
119 };
120
121 int32_t touchScreen;
122 int32_t keyboard;
123 int32_t navigation;
124};
125
126/*
Jeff Browne839a582010-04-22 18:58:52 -0700127 * Pointer coordinate data.
128 */
129struct PointerCoords {
130 float x;
131 float y;
132 float pressure;
133 float size;
134};
135
136/*
137 * Input events.
138 */
Dianne Hackborn9c7f8182010-06-28 15:27:30 -0700139class InputEvent : public AInputEvent {
Jeff Browne839a582010-04-22 18:58:52 -0700140public:
141 virtual ~InputEvent() { }
142
143 virtual int32_t getType() const = 0;
144
145 inline int32_t getDeviceId() const { return mDeviceId; }
146
147 inline int32_t getNature() const { return mNature; }
Dianne Hackborn189ed232010-06-29 19:20:40 -0700148
Jeff Browne839a582010-04-22 18:58:52 -0700149protected:
150 void initialize(int32_t deviceId, int32_t nature);
151
152private:
153 int32_t mDeviceId;
154 int32_t mNature;
155};
156
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700157/*
158 * Key events.
159 */
Jeff Browne839a582010-04-22 18:58:52 -0700160class KeyEvent : public InputEvent {
161public:
162 virtual ~KeyEvent() { }
163
164 virtual int32_t getType() const { return INPUT_EVENT_TYPE_KEY; }
165
166 inline int32_t getAction() const { return mAction; }
167
168 inline int32_t getFlags() const { return mFlags; }
169
170 inline int32_t getKeyCode() const { return mKeyCode; }
171
172 inline int32_t getScanCode() const { return mScanCode; }
173
174 inline int32_t getMetaState() const { return mMetaState; }
175
176 inline int32_t getRepeatCount() const { return mRepeatCount; }
177
178 inline nsecs_t getDownTime() const { return mDownTime; }
179
180 inline nsecs_t getEventTime() const { return mEventTime; }
181
Dianne Hackborn189ed232010-06-29 19:20:40 -0700182 // Return true if this event may have a default action implementation.
183 static bool hasDefaultAction(int32_t keyCode);
184 bool hasDefaultAction() const;
185
186 // Return true if this event represents a system key.
187 static bool isSystemKey(int32_t keyCode);
188 bool isSystemKey() const;
189
Jeff Browne839a582010-04-22 18:58:52 -0700190 void initialize(
191 int32_t deviceId,
192 int32_t nature,
193 int32_t action,
194 int32_t flags,
195 int32_t keyCode,
196 int32_t scanCode,
197 int32_t metaState,
198 int32_t repeatCount,
199 nsecs_t downTime,
200 nsecs_t eventTime);
201
202private:
203 int32_t mAction;
204 int32_t mFlags;
205 int32_t mKeyCode;
206 int32_t mScanCode;
207 int32_t mMetaState;
208 int32_t mRepeatCount;
209 nsecs_t mDownTime;
210 nsecs_t mEventTime;
211};
212
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700213/*
214 * Motion events.
215 */
Jeff Browne839a582010-04-22 18:58:52 -0700216class MotionEvent : public InputEvent {
217public:
218 virtual ~MotionEvent() { }
219
220 virtual int32_t getType() const { return INPUT_EVENT_TYPE_MOTION; }
221
222 inline int32_t getAction() const { return mAction; }
223
224 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
225
226 inline int32_t getMetaState() const { return mMetaState; }
227
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700228 inline float getXOffset() const { return mXOffset; }
229
230 inline float getYOffset() const { return mYOffset; }
231
Jeff Browne839a582010-04-22 18:58:52 -0700232 inline float getXPrecision() const { return mXPrecision; }
233
234 inline float getYPrecision() const { return mYPrecision; }
235
236 inline nsecs_t getDownTime() const { return mDownTime; }
237
238 inline size_t getPointerCount() const { return mPointerIds.size(); }
239
240 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
241
242 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
243
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700244 inline float getRawX(size_t pointerIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700245 return getCurrentPointerCoords(pointerIndex).x;
246 }
247
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700248 inline float getRawY(size_t pointerIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700249 return getCurrentPointerCoords(pointerIndex).y;
250 }
251
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700252 inline float getX(size_t pointerIndex) const {
253 return getRawX(pointerIndex) + mXOffset;
254 }
255
256 inline float getY(size_t pointerIndex) const {
257 return getRawY(pointerIndex) + mYOffset;
258 }
259
Jeff Browne839a582010-04-22 18:58:52 -0700260 inline float getPressure(size_t pointerIndex) const {
261 return getCurrentPointerCoords(pointerIndex).pressure;
262 }
263
264 inline float getSize(size_t pointerIndex) const {
265 return getCurrentPointerCoords(pointerIndex).size;
266 }
267
268 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
269
270 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
271 return mSampleEventTimes[historicalIndex];
272 }
273
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700274 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700275 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
276 }
277
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700278 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700279 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
280 }
281
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700282 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
283 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
284 }
285
286 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
287 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
288 }
289
Jeff Browne839a582010-04-22 18:58:52 -0700290 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
291 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
292 }
293
294 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
295 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
296 }
297
298 void initialize(
299 int32_t deviceId,
300 int32_t nature,
301 int32_t action,
302 int32_t edgeFlags,
303 int32_t metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700304 float xOffset,
305 float yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700306 float xPrecision,
307 float yPrecision,
308 nsecs_t downTime,
309 nsecs_t eventTime,
310 size_t pointerCount,
311 const int32_t* pointerIds,
312 const PointerCoords* pointerCoords);
313
314 void addSample(
315 nsecs_t eventTime,
316 const PointerCoords* pointerCoords);
317
318 void offsetLocation(float xOffset, float yOffset);
319
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700320 // Low-level accessors.
321 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
322 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
323 inline const PointerCoords* getSamplePointerCoords() const {
324 return mSamplePointerCoords.array();
325 }
326
Jeff Browne839a582010-04-22 18:58:52 -0700327private:
328 int32_t mAction;
329 int32_t mEdgeFlags;
330 int32_t mMetaState;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700331 float mXOffset;
332 float mYOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700333 float mXPrecision;
334 float mYPrecision;
335 nsecs_t mDownTime;
336 Vector<int32_t> mPointerIds;
337 Vector<nsecs_t> mSampleEventTimes;
338 Vector<PointerCoords> mSamplePointerCoords;
339
340 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
341 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
342 }
343
344 inline const PointerCoords& getHistoricalPointerCoords(
345 size_t pointerIndex, size_t historicalIndex) const {
346 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
347 }
348};
349
350/*
351 * Input event factory.
352 */
353class InputEventFactoryInterface {
354protected:
355 virtual ~InputEventFactoryInterface() { }
356
357public:
358 InputEventFactoryInterface() { }
359
360 virtual KeyEvent* createKeyEvent() = 0;
361 virtual MotionEvent* createMotionEvent() = 0;
362};
363
364/*
365 * A simple input event factory implementation that uses a single preallocated instance
366 * of each type of input event that are reused for each request.
367 */
368class PreallocatedInputEventFactory : public InputEventFactoryInterface {
369public:
370 PreallocatedInputEventFactory() { }
371 virtual ~PreallocatedInputEventFactory() { }
372
373 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
374 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
375
376private:
377 KeyEvent mKeyEvent;
378 MotionEvent mMotionEvent;
379};
380
381
382} // namespace android
383
384#endif // _UI_INPUT_H