blob: 8c6018bfeeb2d0cc0231a2bb11df5dfd39570523 [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.
Jeff Brown01ce2e92010-09-26 22:20:12 -070043 * Smallest number of pointers is 1.
Jeff Brown46b9ac02010-04-22 18:58:52 -070044 */
45#define MAX_POINTERS 10
46
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070047/*
Jeff Brown01ce2e92010-09-26 22:20:12 -070048 * Maximum pointer id value supported in a motion event.
49 * Smallest pointer id is 0.
50 * (This is limited by our use of BitSet32 to track pointer assignments.)
51 */
52#define MAX_POINTER_ID 31
53
54/*
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070055 * Declare a concrete type for the NDK's input event forward declaration.
56 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070057struct AInputEvent {
58 virtual ~AInputEvent() { }
59};
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070060
Jeff Brown46b9ac02010-04-22 18:58:52 -070061/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070062 * Declare a concrete type for the NDK's input device forward declaration.
Jeff Brown46b9ac02010-04-22 18:58:52 -070063 */
Jeff Brown6d0fec22010-07-23 21:28:06 -070064struct AInputDevice {
65 virtual ~AInputDevice() { }
Jeff Brown46b9ac02010-04-22 18:58:52 -070066};
67
Jeff Brown6d0fec22010-07-23 21:28:06 -070068
69namespace android {
70
Jeff Brown46b9ac02010-04-22 18:58:52 -070071/*
72 * Flags that flow alongside events in the input dispatch system to help with certain
73 * policy decisions such as waking from device sleep.
Jeff Brownb6997262010-10-08 22:31:17 -070074 *
75 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
Jeff Brown46b9ac02010-04-22 18:58:52 -070076 */
77enum {
Jeff Brown0eaf3932010-10-01 14:55:30 -070078 /* These flags originate in RawEvents and are generally set in the key map.
79 * See also labels for policy flags in KeycodeLabels.h. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070080
81 POLICY_FLAG_WAKE = 0x00000001,
82 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
83 POLICY_FLAG_SHIFT = 0x00000004,
84 POLICY_FLAG_CAPS_LOCK = 0x00000008,
85 POLICY_FLAG_ALT = 0x00000010,
86 POLICY_FLAG_ALT_GR = 0x00000020,
87 POLICY_FLAG_MENU = 0x00000040,
88 POLICY_FLAG_LAUNCHER = 0x00000080,
Jeff Brown0eaf3932010-10-01 14:55:30 -070089 POLICY_FLAG_VIRTUAL = 0x00000100,
Jeff Brown46b9ac02010-04-22 18:58:52 -070090
Jeff Brown7fbdc842010-06-17 20:52:56 -070091 POLICY_FLAG_RAW_MASK = 0x0000ffff,
92
Jeff Brown85a31762010-09-01 17:01:00 -070093 /* These flags are set by the input dispatcher. */
94
95 // Indicates that the input event was injected.
96 POLICY_FLAG_INJECTED = 0x01000000,
97
Jeff Browne20c9e02010-10-11 14:20:19 -070098 // Indicates that the input event is from a trusted source such as a directly attached
99 // input device or an application with system-wide event injection permission.
100 POLICY_FLAG_TRUSTED = 0x02000000,
101
Jeff Brown9c3cda02010-06-15 01:31:58 -0700102 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700103
104 // Indicates that the screen was off when the event was received and the event
105 // should wake the device.
106 POLICY_FLAG_WOKE_HERE = 0x10000000,
107
108 // Indicates that the screen was dim when the event was received and the event
109 // should brighten the device.
110 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
Jeff Brownb6997262010-10-08 22:31:17 -0700111
112 // Indicates that the event should be dispatched to applications.
113 // The input event should still be sent to the InputDispatcher so that it can see all
114 // input events received include those that it will not deliver.
115 POLICY_FLAG_PASS_TO_USER = 0x40000000,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700116};
117
118/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700119 * Describes the basic configuration of input devices that are present.
120 */
121struct InputConfiguration {
122 enum {
123 TOUCHSCREEN_UNDEFINED = 0,
124 TOUCHSCREEN_NOTOUCH = 1,
125 TOUCHSCREEN_STYLUS = 2,
126 TOUCHSCREEN_FINGER = 3
127 };
128
129 enum {
130 KEYBOARD_UNDEFINED = 0,
131 KEYBOARD_NOKEYS = 1,
132 KEYBOARD_QWERTY = 2,
133 KEYBOARD_12KEY = 3
134 };
135
136 enum {
137 NAVIGATION_UNDEFINED = 0,
138 NAVIGATION_NONAV = 1,
139 NAVIGATION_DPAD = 2,
140 NAVIGATION_TRACKBALL = 3,
141 NAVIGATION_WHEEL = 4
142 };
143
144 int32_t touchScreen;
145 int32_t keyboard;
146 int32_t navigation;
147};
148
149/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700150 * Pointer coordinate data.
151 */
152struct PointerCoords {
153 float x;
154 float y;
155 float pressure;
156 float size;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700157 float touchMajor;
158 float touchMinor;
159 float toolMajor;
160 float toolMinor;
161 float orientation;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700162};
163
164/*
165 * Input events.
166 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700167class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700168public:
169 virtual ~InputEvent() { }
170
171 virtual int32_t getType() const = 0;
172
173 inline int32_t getDeviceId() const { return mDeviceId; }
174
Jeff Brownc5ed5912010-07-14 18:48:53 -0700175 inline int32_t getSource() const { return mSource; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700176
Jeff Brown46b9ac02010-04-22 18:58:52 -0700177protected:
Jeff Brownc5ed5912010-07-14 18:48:53 -0700178 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700179 void initialize(const InputEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700180
181private:
182 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700183 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184};
185
Jeff Brown5c225b12010-06-16 01:53:36 -0700186/*
187 * Key events.
188 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700189class KeyEvent : public InputEvent {
190public:
191 virtual ~KeyEvent() { }
192
Jeff Brownc5ed5912010-07-14 18:48:53 -0700193 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700194
195 inline int32_t getAction() const { return mAction; }
196
197 inline int32_t getFlags() const { return mFlags; }
198
199 inline int32_t getKeyCode() const { return mKeyCode; }
200
201 inline int32_t getScanCode() const { return mScanCode; }
202
203 inline int32_t getMetaState() const { return mMetaState; }
204
205 inline int32_t getRepeatCount() const { return mRepeatCount; }
206
207 inline nsecs_t getDownTime() const { return mDownTime; }
208
209 inline nsecs_t getEventTime() const { return mEventTime; }
210
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700211 // Return true if this event may have a default action implementation.
212 static bool hasDefaultAction(int32_t keyCode);
213 bool hasDefaultAction() const;
214
215 // Return true if this event represents a system key.
216 static bool isSystemKey(int32_t keyCode);
217 bool isSystemKey() const;
218
Jeff Brown46b9ac02010-04-22 18:58:52 -0700219 void initialize(
220 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700221 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700222 int32_t action,
223 int32_t flags,
224 int32_t keyCode,
225 int32_t scanCode,
226 int32_t metaState,
227 int32_t repeatCount,
228 nsecs_t downTime,
229 nsecs_t eventTime);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700230 void initialize(const KeyEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231
232private:
233 int32_t mAction;
234 int32_t mFlags;
235 int32_t mKeyCode;
236 int32_t mScanCode;
237 int32_t mMetaState;
238 int32_t mRepeatCount;
239 nsecs_t mDownTime;
240 nsecs_t mEventTime;
241};
242
Jeff Brown5c225b12010-06-16 01:53:36 -0700243/*
244 * Motion events.
245 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246class MotionEvent : public InputEvent {
247public:
248 virtual ~MotionEvent() { }
249
Jeff Brownc5ed5912010-07-14 18:48:53 -0700250 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700251
252 inline int32_t getAction() const { return mAction; }
253
Jeff Brown85a31762010-09-01 17:01:00 -0700254 inline int32_t getFlags() const { return mFlags; }
255
Jeff Brown46b9ac02010-04-22 18:58:52 -0700256 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
257
258 inline int32_t getMetaState() const { return mMetaState; }
259
Jeff Brown5c225b12010-06-16 01:53:36 -0700260 inline float getXOffset() const { return mXOffset; }
261
262 inline float getYOffset() const { return mYOffset; }
263
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 inline float getXPrecision() const { return mXPrecision; }
265
266 inline float getYPrecision() const { return mYPrecision; }
267
268 inline nsecs_t getDownTime() const { return mDownTime; }
269
270 inline size_t getPointerCount() const { return mPointerIds.size(); }
271
272 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
273
274 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
275
Jeff Brown5c225b12010-06-16 01:53:36 -0700276 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700277 return getCurrentPointerCoords(pointerIndex).x;
278 }
279
Jeff Brown5c225b12010-06-16 01:53:36 -0700280 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 return getCurrentPointerCoords(pointerIndex).y;
282 }
283
Jeff Brown5c225b12010-06-16 01:53:36 -0700284 inline float getX(size_t pointerIndex) const {
285 return getRawX(pointerIndex) + mXOffset;
286 }
287
288 inline float getY(size_t pointerIndex) const {
289 return getRawY(pointerIndex) + mYOffset;
290 }
291
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 inline float getPressure(size_t pointerIndex) const {
293 return getCurrentPointerCoords(pointerIndex).pressure;
294 }
295
296 inline float getSize(size_t pointerIndex) const {
297 return getCurrentPointerCoords(pointerIndex).size;
298 }
299
Jeff Brownc5ed5912010-07-14 18:48:53 -0700300 inline float getTouchMajor(size_t pointerIndex) const {
301 return getCurrentPointerCoords(pointerIndex).touchMajor;
302 }
303
304 inline float getTouchMinor(size_t pointerIndex) const {
305 return getCurrentPointerCoords(pointerIndex).touchMinor;
306 }
307
308 inline float getToolMajor(size_t pointerIndex) const {
309 return getCurrentPointerCoords(pointerIndex).toolMajor;
310 }
311
312 inline float getToolMinor(size_t pointerIndex) const {
313 return getCurrentPointerCoords(pointerIndex).toolMinor;
314 }
315
316 inline float getOrientation(size_t pointerIndex) const {
317 return getCurrentPointerCoords(pointerIndex).orientation;
318 }
319
Jeff Brown46b9ac02010-04-22 18:58:52 -0700320 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
321
322 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
323 return mSampleEventTimes[historicalIndex];
324 }
325
Jeff Brown5c225b12010-06-16 01:53:36 -0700326 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700327 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
328 }
329
Jeff Brown5c225b12010-06-16 01:53:36 -0700330 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700331 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
332 }
333
Jeff Brown5c225b12010-06-16 01:53:36 -0700334 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
335 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
336 }
337
338 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
339 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
340 }
341
Jeff Brown46b9ac02010-04-22 18:58:52 -0700342 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
343 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
344 }
345
346 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
347 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
348 }
349
Jeff Brownc5ed5912010-07-14 18:48:53 -0700350 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
351 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
352 }
353
354 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
355 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
356 }
357
358 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
359 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
360 }
361
362 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
363 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
364 }
365
366 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
367 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
368 }
369
Jeff Brown46b9ac02010-04-22 18:58:52 -0700370 void initialize(
371 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700372 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700373 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700374 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700375 int32_t edgeFlags,
376 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700377 float xOffset,
378 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700379 float xPrecision,
380 float yPrecision,
381 nsecs_t downTime,
382 nsecs_t eventTime,
383 size_t pointerCount,
384 const int32_t* pointerIds,
385 const PointerCoords* pointerCoords);
386
387 void addSample(
388 nsecs_t eventTime,
389 const PointerCoords* pointerCoords);
390
391 void offsetLocation(float xOffset, float yOffset);
392
Jeff Brown5c225b12010-06-16 01:53:36 -0700393 // Low-level accessors.
394 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
395 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
396 inline const PointerCoords* getSamplePointerCoords() const {
397 return mSamplePointerCoords.array();
398 }
399
Jeff Brown46b9ac02010-04-22 18:58:52 -0700400private:
401 int32_t mAction;
Jeff Brown85a31762010-09-01 17:01:00 -0700402 int32_t mFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700403 int32_t mEdgeFlags;
404 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700405 float mXOffset;
406 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700407 float mXPrecision;
408 float mYPrecision;
409 nsecs_t mDownTime;
410 Vector<int32_t> mPointerIds;
411 Vector<nsecs_t> mSampleEventTimes;
412 Vector<PointerCoords> mSamplePointerCoords;
413
414 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
415 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
416 }
417
418 inline const PointerCoords& getHistoricalPointerCoords(
419 size_t pointerIndex, size_t historicalIndex) const {
420 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
421 }
422};
423
424/*
425 * Input event factory.
426 */
427class InputEventFactoryInterface {
428protected:
429 virtual ~InputEventFactoryInterface() { }
430
431public:
432 InputEventFactoryInterface() { }
433
434 virtual KeyEvent* createKeyEvent() = 0;
435 virtual MotionEvent* createMotionEvent() = 0;
436};
437
438/*
439 * A simple input event factory implementation that uses a single preallocated instance
440 * of each type of input event that are reused for each request.
441 */
442class PreallocatedInputEventFactory : public InputEventFactoryInterface {
443public:
444 PreallocatedInputEventFactory() { }
445 virtual ~PreallocatedInputEventFactory() { }
446
447 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
448 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
449
450private:
451 KeyEvent mKeyEvent;
452 MotionEvent mMotionEvent;
453};
454
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455/*
456 * Describes the characteristics and capabilities of an input device.
457 */
458class InputDeviceInfo {
459public:
460 InputDeviceInfo();
461 InputDeviceInfo(const InputDeviceInfo& other);
462 ~InputDeviceInfo();
463
464 struct MotionRange {
465 float min;
466 float max;
467 float flat;
468 float fuzz;
469 };
470
471 void initialize(int32_t id, const String8& name);
472
473 inline int32_t getId() const { return mId; }
474 inline const String8 getName() const { return mName; }
475 inline uint32_t getSources() const { return mSources; }
476
477 const MotionRange* getMotionRange(int32_t rangeType) const;
478
479 void addSource(uint32_t source);
480 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
481 void addMotionRange(int32_t rangeType, const MotionRange& range);
482
483 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
484 inline int32_t getKeyboardType() const { return mKeyboardType; }
485
Jeff Brown8d608662010-08-30 03:02:23 -0700486 inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
487 return mMotionRanges;
488 }
489
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490private:
491 int32_t mId;
492 String8 mName;
493 uint32_t mSources;
494 int32_t mKeyboardType;
495
496 KeyedVector<int32_t, MotionRange> mMotionRanges;
497};
498
Jeff Brown46b9ac02010-04-22 18:58:52 -0700499
500} // namespace android
501
502#endif // _UI_INPUT_H