blob: b587e94db5708b5dac66f562aedaf9f87fcc4dcd [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>
Jeff Brown6d0fec22010-07-23 21:28:06 -070026#include <utils/KeyedVector.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070027#include <utils/Timers.h>
Jeff Brown6d0fec22010-07-23 21:28:06 -070028#include <utils/RefBase.h>
29#include <utils/String8.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070030
31/*
32 * Additional private constants not defined in ndk/ui/input.h.
33 */
34enum {
35 /*
36 * Private control to determine when an app is tracking a key sequence.
37 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070038 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000
Jeff Brown46b9ac02010-04-22 18:58:52 -070039};
40
41/*
42 * Maximum number of pointers supported per motion event.
43 */
44#define MAX_POINTERS 10
45
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070046/*
47 * Declare a concrete type for the NDK's input event forward declaration.
48 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070049struct AInputEvent {
50 virtual ~AInputEvent() { }
51};
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070052
Jeff Brown46b9ac02010-04-22 18:58:52 -070053/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070054 * Declare a concrete type for the NDK's input device forward declaration.
Jeff Brown46b9ac02010-04-22 18:58:52 -070055 */
Jeff Brown6d0fec22010-07-23 21:28:06 -070056struct AInputDevice {
57 virtual ~AInputDevice() { }
Jeff Brown46b9ac02010-04-22 18:58:52 -070058};
59
Jeff Brown6d0fec22010-07-23 21:28:06 -070060
61namespace android {
62
Jeff Brown46b9ac02010-04-22 18:58:52 -070063/*
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 Brown85a31762010-09-01 17:01:00 -070081 /* These flags are set by the input dispatcher. */
82
83 // Indicates that the input event was injected.
84 POLICY_FLAG_INJECTED = 0x01000000,
85
Jeff Brown9c3cda02010-06-15 01:31:58 -070086 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070087
88 // Indicates that the screen was off when the event was received and the event
89 // should wake the device.
90 POLICY_FLAG_WOKE_HERE = 0x10000000,
91
92 // Indicates that the screen was dim when the event was received and the event
93 // should brighten the device.
94 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
95};
96
97/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070098 * Describes the basic configuration of input devices that are present.
99 */
100struct InputConfiguration {
101 enum {
102 TOUCHSCREEN_UNDEFINED = 0,
103 TOUCHSCREEN_NOTOUCH = 1,
104 TOUCHSCREEN_STYLUS = 2,
105 TOUCHSCREEN_FINGER = 3
106 };
107
108 enum {
109 KEYBOARD_UNDEFINED = 0,
110 KEYBOARD_NOKEYS = 1,
111 KEYBOARD_QWERTY = 2,
112 KEYBOARD_12KEY = 3
113 };
114
115 enum {
116 NAVIGATION_UNDEFINED = 0,
117 NAVIGATION_NONAV = 1,
118 NAVIGATION_DPAD = 2,
119 NAVIGATION_TRACKBALL = 3,
120 NAVIGATION_WHEEL = 4
121 };
122
123 int32_t touchScreen;
124 int32_t keyboard;
125 int32_t navigation;
126};
127
128/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700129 * Pointer coordinate data.
130 */
131struct PointerCoords {
132 float x;
133 float y;
134 float pressure;
135 float size;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700136 float touchMajor;
137 float touchMinor;
138 float toolMajor;
139 float toolMinor;
140 float orientation;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700141};
142
143/*
144 * Input events.
145 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700146class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700147public:
148 virtual ~InputEvent() { }
149
150 virtual int32_t getType() const = 0;
151
152 inline int32_t getDeviceId() const { return mDeviceId; }
153
Jeff Brownc5ed5912010-07-14 18:48:53 -0700154 inline int32_t getSource() const { return mSource; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700155
Jeff Brown46b9ac02010-04-22 18:58:52 -0700156protected:
Jeff Brownc5ed5912010-07-14 18:48:53 -0700157 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700158 void initialize(const InputEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700159
160private:
161 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700162 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700163};
164
Jeff Brown5c225b12010-06-16 01:53:36 -0700165/*
166 * Key events.
167 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700168class KeyEvent : public InputEvent {
169public:
170 virtual ~KeyEvent() { }
171
Jeff Brownc5ed5912010-07-14 18:48:53 -0700172 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700173
174 inline int32_t getAction() const { return mAction; }
175
176 inline int32_t getFlags() const { return mFlags; }
177
178 inline int32_t getKeyCode() const { return mKeyCode; }
179
180 inline int32_t getScanCode() const { return mScanCode; }
181
182 inline int32_t getMetaState() const { return mMetaState; }
183
184 inline int32_t getRepeatCount() const { return mRepeatCount; }
185
186 inline nsecs_t getDownTime() const { return mDownTime; }
187
188 inline nsecs_t getEventTime() const { return mEventTime; }
189
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700190 // Return true if this event may have a default action implementation.
191 static bool hasDefaultAction(int32_t keyCode);
192 bool hasDefaultAction() const;
193
194 // Return true if this event represents a system key.
195 static bool isSystemKey(int32_t keyCode);
196 bool isSystemKey() const;
197
Jeff Brown46b9ac02010-04-22 18:58:52 -0700198 void initialize(
199 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700200 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700201 int32_t action,
202 int32_t flags,
203 int32_t keyCode,
204 int32_t scanCode,
205 int32_t metaState,
206 int32_t repeatCount,
207 nsecs_t downTime,
208 nsecs_t eventTime);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700209 void initialize(const KeyEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700210
211private:
212 int32_t mAction;
213 int32_t mFlags;
214 int32_t mKeyCode;
215 int32_t mScanCode;
216 int32_t mMetaState;
217 int32_t mRepeatCount;
218 nsecs_t mDownTime;
219 nsecs_t mEventTime;
220};
221
Jeff Brown5c225b12010-06-16 01:53:36 -0700222/*
223 * Motion events.
224 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225class MotionEvent : public InputEvent {
226public:
227 virtual ~MotionEvent() { }
228
Jeff Brownc5ed5912010-07-14 18:48:53 -0700229 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700230
231 inline int32_t getAction() const { return mAction; }
232
Jeff Brown85a31762010-09-01 17:01:00 -0700233 inline int32_t getFlags() const { return mFlags; }
234
Jeff Brown46b9ac02010-04-22 18:58:52 -0700235 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
236
237 inline int32_t getMetaState() const { return mMetaState; }
238
Jeff Brown5c225b12010-06-16 01:53:36 -0700239 inline float getXOffset() const { return mXOffset; }
240
241 inline float getYOffset() const { return mYOffset; }
242
Jeff Brown46b9ac02010-04-22 18:58:52 -0700243 inline float getXPrecision() const { return mXPrecision; }
244
245 inline float getYPrecision() const { return mYPrecision; }
246
247 inline nsecs_t getDownTime() const { return mDownTime; }
248
249 inline size_t getPointerCount() const { return mPointerIds.size(); }
250
251 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
252
253 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
254
Jeff Brown5c225b12010-06-16 01:53:36 -0700255 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700256 return getCurrentPointerCoords(pointerIndex).x;
257 }
258
Jeff Brown5c225b12010-06-16 01:53:36 -0700259 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700260 return getCurrentPointerCoords(pointerIndex).y;
261 }
262
Jeff Brown5c225b12010-06-16 01:53:36 -0700263 inline float getX(size_t pointerIndex) const {
264 return getRawX(pointerIndex) + mXOffset;
265 }
266
267 inline float getY(size_t pointerIndex) const {
268 return getRawY(pointerIndex) + mYOffset;
269 }
270
Jeff Brown46b9ac02010-04-22 18:58:52 -0700271 inline float getPressure(size_t pointerIndex) const {
272 return getCurrentPointerCoords(pointerIndex).pressure;
273 }
274
275 inline float getSize(size_t pointerIndex) const {
276 return getCurrentPointerCoords(pointerIndex).size;
277 }
278
Jeff Brownc5ed5912010-07-14 18:48:53 -0700279 inline float getTouchMajor(size_t pointerIndex) const {
280 return getCurrentPointerCoords(pointerIndex).touchMajor;
281 }
282
283 inline float getTouchMinor(size_t pointerIndex) const {
284 return getCurrentPointerCoords(pointerIndex).touchMinor;
285 }
286
287 inline float getToolMajor(size_t pointerIndex) const {
288 return getCurrentPointerCoords(pointerIndex).toolMajor;
289 }
290
291 inline float getToolMinor(size_t pointerIndex) const {
292 return getCurrentPointerCoords(pointerIndex).toolMinor;
293 }
294
295 inline float getOrientation(size_t pointerIndex) const {
296 return getCurrentPointerCoords(pointerIndex).orientation;
297 }
298
Jeff Brown46b9ac02010-04-22 18:58:52 -0700299 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
300
301 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
302 return mSampleEventTimes[historicalIndex];
303 }
304
Jeff Brown5c225b12010-06-16 01:53:36 -0700305 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700306 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
307 }
308
Jeff Brown5c225b12010-06-16 01:53:36 -0700309 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700310 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
311 }
312
Jeff Brown5c225b12010-06-16 01:53:36 -0700313 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
314 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
315 }
316
317 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
318 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
319 }
320
Jeff Brown46b9ac02010-04-22 18:58:52 -0700321 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
322 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
323 }
324
325 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
326 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
327 }
328
Jeff Brownc5ed5912010-07-14 18:48:53 -0700329 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
330 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
331 }
332
333 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
334 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
335 }
336
337 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
338 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
339 }
340
341 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
342 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
343 }
344
345 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
346 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
347 }
348
Jeff Brown46b9ac02010-04-22 18:58:52 -0700349 void initialize(
350 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700351 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700352 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700353 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354 int32_t edgeFlags,
355 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700356 float xOffset,
357 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700358 float xPrecision,
359 float yPrecision,
360 nsecs_t downTime,
361 nsecs_t eventTime,
362 size_t pointerCount,
363 const int32_t* pointerIds,
364 const PointerCoords* pointerCoords);
365
366 void addSample(
367 nsecs_t eventTime,
368 const PointerCoords* pointerCoords);
369
370 void offsetLocation(float xOffset, float yOffset);
371
Jeff Brown5c225b12010-06-16 01:53:36 -0700372 // Low-level accessors.
373 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
374 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
375 inline const PointerCoords* getSamplePointerCoords() const {
376 return mSamplePointerCoords.array();
377 }
378
Jeff Brown46b9ac02010-04-22 18:58:52 -0700379private:
380 int32_t mAction;
Jeff Brown85a31762010-09-01 17:01:00 -0700381 int32_t mFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700382 int32_t mEdgeFlags;
383 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700384 float mXOffset;
385 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700386 float mXPrecision;
387 float mYPrecision;
388 nsecs_t mDownTime;
389 Vector<int32_t> mPointerIds;
390 Vector<nsecs_t> mSampleEventTimes;
391 Vector<PointerCoords> mSamplePointerCoords;
392
393 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
394 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
395 }
396
397 inline const PointerCoords& getHistoricalPointerCoords(
398 size_t pointerIndex, size_t historicalIndex) const {
399 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
400 }
401};
402
403/*
404 * Input event factory.
405 */
406class InputEventFactoryInterface {
407protected:
408 virtual ~InputEventFactoryInterface() { }
409
410public:
411 InputEventFactoryInterface() { }
412
413 virtual KeyEvent* createKeyEvent() = 0;
414 virtual MotionEvent* createMotionEvent() = 0;
415};
416
417/*
418 * A simple input event factory implementation that uses a single preallocated instance
419 * of each type of input event that are reused for each request.
420 */
421class PreallocatedInputEventFactory : public InputEventFactoryInterface {
422public:
423 PreallocatedInputEventFactory() { }
424 virtual ~PreallocatedInputEventFactory() { }
425
426 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
427 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
428
429private:
430 KeyEvent mKeyEvent;
431 MotionEvent mMotionEvent;
432};
433
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434/*
435 * Describes the characteristics and capabilities of an input device.
436 */
437class InputDeviceInfo {
438public:
439 InputDeviceInfo();
440 InputDeviceInfo(const InputDeviceInfo& other);
441 ~InputDeviceInfo();
442
443 struct MotionRange {
444 float min;
445 float max;
446 float flat;
447 float fuzz;
448 };
449
450 void initialize(int32_t id, const String8& name);
451
452 inline int32_t getId() const { return mId; }
453 inline const String8 getName() const { return mName; }
454 inline uint32_t getSources() const { return mSources; }
455
456 const MotionRange* getMotionRange(int32_t rangeType) const;
457
458 void addSource(uint32_t source);
459 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
460 void addMotionRange(int32_t rangeType, const MotionRange& range);
461
462 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
463 inline int32_t getKeyboardType() const { return mKeyboardType; }
464
Jeff Brown8d608662010-08-30 03:02:23 -0700465 inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
466 return mMotionRanges;
467 }
468
Jeff Brown6d0fec22010-07-23 21:28:06 -0700469private:
470 int32_t mId;
471 String8 mName;
472 uint32_t mSources;
473 int32_t mKeyboardType;
474
475 KeyedVector<int32_t, MotionRange> mMotionRanges;
476};
477
Jeff Brown46b9ac02010-04-22 18:58:52 -0700478
479} // namespace android
480
481#endif // _UI_INPUT_H