blob: 66061fd7193589838d76e2e7eb860ec940b9bb88 [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 Brown9c3cda02010-06-15 01:31:58 -070098 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070099
100 // Indicates that the screen was off when the event was received and the event
101 // should wake the device.
102 POLICY_FLAG_WOKE_HERE = 0x10000000,
103
104 // Indicates that the screen was dim when the event was received and the event
105 // should brighten the device.
106 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
Jeff Brownb6997262010-10-08 22:31:17 -0700107
108 // Indicates that the event should be dispatched to applications.
109 // The input event should still be sent to the InputDispatcher so that it can see all
110 // input events received include those that it will not deliver.
111 POLICY_FLAG_PASS_TO_USER = 0x40000000,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112};
113
114/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700115 * Describes the basic configuration of input devices that are present.
116 */
117struct InputConfiguration {
118 enum {
119 TOUCHSCREEN_UNDEFINED = 0,
120 TOUCHSCREEN_NOTOUCH = 1,
121 TOUCHSCREEN_STYLUS = 2,
122 TOUCHSCREEN_FINGER = 3
123 };
124
125 enum {
126 KEYBOARD_UNDEFINED = 0,
127 KEYBOARD_NOKEYS = 1,
128 KEYBOARD_QWERTY = 2,
129 KEYBOARD_12KEY = 3
130 };
131
132 enum {
133 NAVIGATION_UNDEFINED = 0,
134 NAVIGATION_NONAV = 1,
135 NAVIGATION_DPAD = 2,
136 NAVIGATION_TRACKBALL = 3,
137 NAVIGATION_WHEEL = 4
138 };
139
140 int32_t touchScreen;
141 int32_t keyboard;
142 int32_t navigation;
143};
144
145/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700146 * Pointer coordinate data.
147 */
148struct PointerCoords {
149 float x;
150 float y;
151 float pressure;
152 float size;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700153 float touchMajor;
154 float touchMinor;
155 float toolMajor;
156 float toolMinor;
157 float orientation;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700158};
159
160/*
161 * Input events.
162 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700163class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700164public:
165 virtual ~InputEvent() { }
166
167 virtual int32_t getType() const = 0;
168
169 inline int32_t getDeviceId() const { return mDeviceId; }
170
Jeff Brownc5ed5912010-07-14 18:48:53 -0700171 inline int32_t getSource() const { return mSource; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700172
Jeff Brown46b9ac02010-04-22 18:58:52 -0700173protected:
Jeff Brownc5ed5912010-07-14 18:48:53 -0700174 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700175 void initialize(const InputEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700176
177private:
178 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700179 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700180};
181
Jeff Brown5c225b12010-06-16 01:53:36 -0700182/*
183 * Key events.
184 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700185class KeyEvent : public InputEvent {
186public:
187 virtual ~KeyEvent() { }
188
Jeff Brownc5ed5912010-07-14 18:48:53 -0700189 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700190
191 inline int32_t getAction() const { return mAction; }
192
193 inline int32_t getFlags() const { return mFlags; }
194
195 inline int32_t getKeyCode() const { return mKeyCode; }
196
197 inline int32_t getScanCode() const { return mScanCode; }
198
199 inline int32_t getMetaState() const { return mMetaState; }
200
201 inline int32_t getRepeatCount() const { return mRepeatCount; }
202
203 inline nsecs_t getDownTime() const { return mDownTime; }
204
205 inline nsecs_t getEventTime() const { return mEventTime; }
206
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700207 // Return true if this event may have a default action implementation.
208 static bool hasDefaultAction(int32_t keyCode);
209 bool hasDefaultAction() const;
210
211 // Return true if this event represents a system key.
212 static bool isSystemKey(int32_t keyCode);
213 bool isSystemKey() const;
214
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215 void initialize(
216 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700217 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700218 int32_t action,
219 int32_t flags,
220 int32_t keyCode,
221 int32_t scanCode,
222 int32_t metaState,
223 int32_t repeatCount,
224 nsecs_t downTime,
225 nsecs_t eventTime);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700226 void initialize(const KeyEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700227
228private:
229 int32_t mAction;
230 int32_t mFlags;
231 int32_t mKeyCode;
232 int32_t mScanCode;
233 int32_t mMetaState;
234 int32_t mRepeatCount;
235 nsecs_t mDownTime;
236 nsecs_t mEventTime;
237};
238
Jeff Brown5c225b12010-06-16 01:53:36 -0700239/*
240 * Motion events.
241 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700242class MotionEvent : public InputEvent {
243public:
244 virtual ~MotionEvent() { }
245
Jeff Brownc5ed5912010-07-14 18:48:53 -0700246 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700247
248 inline int32_t getAction() const { return mAction; }
249
Jeff Brown85a31762010-09-01 17:01:00 -0700250 inline int32_t getFlags() const { return mFlags; }
251
Jeff Brown46b9ac02010-04-22 18:58:52 -0700252 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
253
254 inline int32_t getMetaState() const { return mMetaState; }
255
Jeff Brown5c225b12010-06-16 01:53:36 -0700256 inline float getXOffset() const { return mXOffset; }
257
258 inline float getYOffset() const { return mYOffset; }
259
Jeff Brown46b9ac02010-04-22 18:58:52 -0700260 inline float getXPrecision() const { return mXPrecision; }
261
262 inline float getYPrecision() const { return mYPrecision; }
263
264 inline nsecs_t getDownTime() const { return mDownTime; }
265
266 inline size_t getPointerCount() const { return mPointerIds.size(); }
267
268 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
269
270 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
271
Jeff Brown5c225b12010-06-16 01:53:36 -0700272 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700273 return getCurrentPointerCoords(pointerIndex).x;
274 }
275
Jeff Brown5c225b12010-06-16 01:53:36 -0700276 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700277 return getCurrentPointerCoords(pointerIndex).y;
278 }
279
Jeff Brown5c225b12010-06-16 01:53:36 -0700280 inline float getX(size_t pointerIndex) const {
281 return getRawX(pointerIndex) + mXOffset;
282 }
283
284 inline float getY(size_t pointerIndex) const {
285 return getRawY(pointerIndex) + mYOffset;
286 }
287
Jeff Brown46b9ac02010-04-22 18:58:52 -0700288 inline float getPressure(size_t pointerIndex) const {
289 return getCurrentPointerCoords(pointerIndex).pressure;
290 }
291
292 inline float getSize(size_t pointerIndex) const {
293 return getCurrentPointerCoords(pointerIndex).size;
294 }
295
Jeff Brownc5ed5912010-07-14 18:48:53 -0700296 inline float getTouchMajor(size_t pointerIndex) const {
297 return getCurrentPointerCoords(pointerIndex).touchMajor;
298 }
299
300 inline float getTouchMinor(size_t pointerIndex) const {
301 return getCurrentPointerCoords(pointerIndex).touchMinor;
302 }
303
304 inline float getToolMajor(size_t pointerIndex) const {
305 return getCurrentPointerCoords(pointerIndex).toolMajor;
306 }
307
308 inline float getToolMinor(size_t pointerIndex) const {
309 return getCurrentPointerCoords(pointerIndex).toolMinor;
310 }
311
312 inline float getOrientation(size_t pointerIndex) const {
313 return getCurrentPointerCoords(pointerIndex).orientation;
314 }
315
Jeff Brown46b9ac02010-04-22 18:58:52 -0700316 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
317
318 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
319 return mSampleEventTimes[historicalIndex];
320 }
321
Jeff Brown5c225b12010-06-16 01:53:36 -0700322 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700323 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
324 }
325
Jeff Brown5c225b12010-06-16 01:53:36 -0700326 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700327 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
328 }
329
Jeff Brown5c225b12010-06-16 01:53:36 -0700330 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
331 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
332 }
333
334 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
335 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
336 }
337
Jeff Brown46b9ac02010-04-22 18:58:52 -0700338 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
339 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
340 }
341
342 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
343 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
344 }
345
Jeff Brownc5ed5912010-07-14 18:48:53 -0700346 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
347 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
348 }
349
350 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
351 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
352 }
353
354 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
355 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
356 }
357
358 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
359 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
360 }
361
362 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
363 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
364 }
365
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366 void initialize(
367 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700368 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700369 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700370 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700371 int32_t edgeFlags,
372 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700373 float xOffset,
374 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700375 float xPrecision,
376 float yPrecision,
377 nsecs_t downTime,
378 nsecs_t eventTime,
379 size_t pointerCount,
380 const int32_t* pointerIds,
381 const PointerCoords* pointerCoords);
382
383 void addSample(
384 nsecs_t eventTime,
385 const PointerCoords* pointerCoords);
386
387 void offsetLocation(float xOffset, float yOffset);
388
Jeff Brown5c225b12010-06-16 01:53:36 -0700389 // Low-level accessors.
390 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
391 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
392 inline const PointerCoords* getSamplePointerCoords() const {
393 return mSamplePointerCoords.array();
394 }
395
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396private:
397 int32_t mAction;
Jeff Brown85a31762010-09-01 17:01:00 -0700398 int32_t mFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700399 int32_t mEdgeFlags;
400 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700401 float mXOffset;
402 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700403 float mXPrecision;
404 float mYPrecision;
405 nsecs_t mDownTime;
406 Vector<int32_t> mPointerIds;
407 Vector<nsecs_t> mSampleEventTimes;
408 Vector<PointerCoords> mSamplePointerCoords;
409
410 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
411 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
412 }
413
414 inline const PointerCoords& getHistoricalPointerCoords(
415 size_t pointerIndex, size_t historicalIndex) const {
416 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
417 }
418};
419
420/*
421 * Input event factory.
422 */
423class InputEventFactoryInterface {
424protected:
425 virtual ~InputEventFactoryInterface() { }
426
427public:
428 InputEventFactoryInterface() { }
429
430 virtual KeyEvent* createKeyEvent() = 0;
431 virtual MotionEvent* createMotionEvent() = 0;
432};
433
434/*
435 * A simple input event factory implementation that uses a single preallocated instance
436 * of each type of input event that are reused for each request.
437 */
438class PreallocatedInputEventFactory : public InputEventFactoryInterface {
439public:
440 PreallocatedInputEventFactory() { }
441 virtual ~PreallocatedInputEventFactory() { }
442
443 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
444 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
445
446private:
447 KeyEvent mKeyEvent;
448 MotionEvent mMotionEvent;
449};
450
Jeff Brown6d0fec22010-07-23 21:28:06 -0700451/*
452 * Describes the characteristics and capabilities of an input device.
453 */
454class InputDeviceInfo {
455public:
456 InputDeviceInfo();
457 InputDeviceInfo(const InputDeviceInfo& other);
458 ~InputDeviceInfo();
459
460 struct MotionRange {
461 float min;
462 float max;
463 float flat;
464 float fuzz;
465 };
466
467 void initialize(int32_t id, const String8& name);
468
469 inline int32_t getId() const { return mId; }
470 inline const String8 getName() const { return mName; }
471 inline uint32_t getSources() const { return mSources; }
472
473 const MotionRange* getMotionRange(int32_t rangeType) const;
474
475 void addSource(uint32_t source);
476 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
477 void addMotionRange(int32_t rangeType, const MotionRange& range);
478
479 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
480 inline int32_t getKeyboardType() const { return mKeyboardType; }
481
Jeff Brown8d608662010-08-30 03:02:23 -0700482 inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
483 return mMotionRanges;
484 }
485
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486private:
487 int32_t mId;
488 String8 mName;
489 uint32_t mSources;
490 int32_t mKeyboardType;
491
492 KeyedVector<int32_t, MotionRange> mMotionRanges;
493};
494
Jeff Brown46b9ac02010-04-22 18:58:52 -0700495
496} // namespace android
497
498#endif // _UI_INPUT_H