blob: 4e809d6a82d634e9a8daea7dfefe9f17158e2c86 [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>
Jeff Browne57e8952010-07-23 21:28:06 -070026#include <utils/KeyedVector.h>
Jeff Browne839a582010-04-22 18:58:52 -070027#include <utils/Timers.h>
Jeff Browne57e8952010-07-23 21:28:06 -070028#include <utils/RefBase.h>
29#include <utils/String8.h>
Jeff Browne839a582010-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 Brown5c1ed842010-07-14 18:48:53 -070038 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000
Jeff Browne839a582010-04-22 18:58:52 -070039};
40
Jeff Browncb203b22011-01-19 20:35:47 -080041enum {
42 /*
43 * Indicates that an input device has switches.
44 * This input source flag is hidden from the API because switches are only used by the system
45 * and applications have no way to interact with them.
46 */
47 AINPUT_SOURCE_SWITCH = 0x80000000,
48};
49
Jeff Browne839a582010-04-22 18:58:52 -070050/*
51 * Maximum number of pointers supported per motion event.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070052 * Smallest number of pointers is 1.
Jeff Browne839a582010-04-22 18:58:52 -070053 */
54#define MAX_POINTERS 10
55
Dianne Hackborn4d96bb62010-06-18 18:09:33 -070056/*
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070057 * Maximum pointer id value supported in a motion event.
58 * Smallest pointer id is 0.
59 * (This is limited by our use of BitSet32 to track pointer assignments.)
60 */
61#define MAX_POINTER_ID 31
62
63/*
Dianne Hackborn4d96bb62010-06-18 18:09:33 -070064 * Declare a concrete type for the NDK's input event forward declaration.
65 */
Dianne Hackbornce838a22010-07-13 17:48:30 -070066struct AInputEvent {
67 virtual ~AInputEvent() { }
68};
Dianne Hackborn4d96bb62010-06-18 18:09:33 -070069
Jeff Browne839a582010-04-22 18:58:52 -070070/*
Jeff Browne57e8952010-07-23 21:28:06 -070071 * Declare a concrete type for the NDK's input device forward declaration.
Jeff Browne839a582010-04-22 18:58:52 -070072 */
Jeff Browne57e8952010-07-23 21:28:06 -070073struct AInputDevice {
74 virtual ~AInputDevice() { }
Jeff Browne839a582010-04-22 18:58:52 -070075};
76
Jeff Browne57e8952010-07-23 21:28:06 -070077
78namespace android {
79
Jeff Browne839a582010-04-22 18:58:52 -070080/*
81 * Flags that flow alongside events in the input dispatch system to help with certain
82 * policy decisions such as waking from device sleep.
Jeff Brown90f0cee2010-10-08 22:31:17 -070083 *
84 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
Jeff Browne839a582010-04-22 18:58:52 -070085 */
86enum {
Jeff Brown956c0fb2010-10-01 14:55:30 -070087 /* These flags originate in RawEvents and are generally set in the key map.
88 * See also labels for policy flags in KeycodeLabels.h. */
Jeff Browne839a582010-04-22 18:58:52 -070089
90 POLICY_FLAG_WAKE = 0x00000001,
91 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
92 POLICY_FLAG_SHIFT = 0x00000004,
93 POLICY_FLAG_CAPS_LOCK = 0x00000008,
94 POLICY_FLAG_ALT = 0x00000010,
95 POLICY_FLAG_ALT_GR = 0x00000020,
96 POLICY_FLAG_MENU = 0x00000040,
97 POLICY_FLAG_LAUNCHER = 0x00000080,
Jeff Brown956c0fb2010-10-01 14:55:30 -070098 POLICY_FLAG_VIRTUAL = 0x00000100,
Jeff Browne839a582010-04-22 18:58:52 -070099
Jeff Brown51d45a72010-06-17 20:52:56 -0700100 POLICY_FLAG_RAW_MASK = 0x0000ffff,
101
Jeff Brownaf30ff62010-09-01 17:01:00 -0700102 /* These flags are set by the input dispatcher. */
103
104 // Indicates that the input event was injected.
105 POLICY_FLAG_INJECTED = 0x01000000,
106
Jeff Brown1fe6dec2010-10-11 14:20:19 -0700107 // Indicates that the input event is from a trusted source such as a directly attached
108 // input device or an application with system-wide event injection permission.
109 POLICY_FLAG_TRUSTED = 0x02000000,
110
Jeff Brown54bc2812010-06-15 01:31:58 -0700111 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Browne839a582010-04-22 18:58:52 -0700112
113 // Indicates that the screen was off when the event was received and the event
114 // should wake the device.
115 POLICY_FLAG_WOKE_HERE = 0x10000000,
116
117 // Indicates that the screen was dim when the event was received and the event
118 // should brighten the device.
119 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
Jeff Brown90f0cee2010-10-08 22:31:17 -0700120
121 // Indicates that the event should be dispatched to applications.
122 // The input event should still be sent to the InputDispatcher so that it can see all
123 // input events received include those that it will not deliver.
124 POLICY_FLAG_PASS_TO_USER = 0x40000000,
Jeff Browne839a582010-04-22 18:58:52 -0700125};
126
127/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700128 * Describes the basic configuration of input devices that are present.
129 */
130struct InputConfiguration {
131 enum {
132 TOUCHSCREEN_UNDEFINED = 0,
133 TOUCHSCREEN_NOTOUCH = 1,
134 TOUCHSCREEN_STYLUS = 2,
135 TOUCHSCREEN_FINGER = 3
136 };
137
138 enum {
139 KEYBOARD_UNDEFINED = 0,
140 KEYBOARD_NOKEYS = 1,
141 KEYBOARD_QWERTY = 2,
142 KEYBOARD_12KEY = 3
143 };
144
145 enum {
146 NAVIGATION_UNDEFINED = 0,
147 NAVIGATION_NONAV = 1,
148 NAVIGATION_DPAD = 2,
149 NAVIGATION_TRACKBALL = 3,
150 NAVIGATION_WHEEL = 4
151 };
152
153 int32_t touchScreen;
154 int32_t keyboard;
155 int32_t navigation;
156};
157
158/*
Jeff Browne839a582010-04-22 18:58:52 -0700159 * Pointer coordinate data.
160 */
161struct PointerCoords {
162 float x;
163 float y;
164 float pressure;
165 float size;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700166 float touchMajor;
167 float touchMinor;
168 float toolMajor;
169 float toolMinor;
170 float orientation;
Jeff Browne839a582010-04-22 18:58:52 -0700171};
172
173/*
174 * Input events.
175 */
Dianne Hackborn9c7f8182010-06-28 15:27:30 -0700176class InputEvent : public AInputEvent {
Jeff Browne839a582010-04-22 18:58:52 -0700177public:
178 virtual ~InputEvent() { }
179
180 virtual int32_t getType() const = 0;
181
182 inline int32_t getDeviceId() const { return mDeviceId; }
183
Jeff Brown5c1ed842010-07-14 18:48:53 -0700184 inline int32_t getSource() const { return mSource; }
Dianne Hackborn189ed232010-06-29 19:20:40 -0700185
Jeff Browne839a582010-04-22 18:58:52 -0700186protected:
Jeff Brown5c1ed842010-07-14 18:48:53 -0700187 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn0e885272010-07-15 17:44:53 -0700188 void initialize(const InputEvent& from);
Jeff Browne839a582010-04-22 18:58:52 -0700189
190private:
191 int32_t mDeviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700192 int32_t mSource;
Jeff Browne839a582010-04-22 18:58:52 -0700193};
194
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700195/*
196 * Key events.
197 */
Jeff Browne839a582010-04-22 18:58:52 -0700198class KeyEvent : public InputEvent {
199public:
200 virtual ~KeyEvent() { }
201
Jeff Brown5c1ed842010-07-14 18:48:53 -0700202 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Browne839a582010-04-22 18:58:52 -0700203
204 inline int32_t getAction() const { return mAction; }
205
206 inline int32_t getFlags() const { return mFlags; }
207
208 inline int32_t getKeyCode() const { return mKeyCode; }
209
210 inline int32_t getScanCode() const { return mScanCode; }
211
212 inline int32_t getMetaState() const { return mMetaState; }
213
214 inline int32_t getRepeatCount() const { return mRepeatCount; }
215
216 inline nsecs_t getDownTime() const { return mDownTime; }
217
218 inline nsecs_t getEventTime() const { return mEventTime; }
219
Dianne Hackborn189ed232010-06-29 19:20:40 -0700220 // Return true if this event may have a default action implementation.
221 static bool hasDefaultAction(int32_t keyCode);
222 bool hasDefaultAction() const;
223
224 // Return true if this event represents a system key.
225 static bool isSystemKey(int32_t keyCode);
226 bool isSystemKey() const;
227
Jeff Browne839a582010-04-22 18:58:52 -0700228 void initialize(
229 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700230 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700231 int32_t action,
232 int32_t flags,
233 int32_t keyCode,
234 int32_t scanCode,
235 int32_t metaState,
236 int32_t repeatCount,
237 nsecs_t downTime,
238 nsecs_t eventTime);
Dianne Hackborn0e885272010-07-15 17:44:53 -0700239 void initialize(const KeyEvent& from);
Jeff Browne839a582010-04-22 18:58:52 -0700240
241private:
242 int32_t mAction;
243 int32_t mFlags;
244 int32_t mKeyCode;
245 int32_t mScanCode;
246 int32_t mMetaState;
247 int32_t mRepeatCount;
248 nsecs_t mDownTime;
249 nsecs_t mEventTime;
250};
251
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700252/*
253 * Motion events.
254 */
Jeff Browne839a582010-04-22 18:58:52 -0700255class MotionEvent : public InputEvent {
256public:
257 virtual ~MotionEvent() { }
258
Jeff Brown5c1ed842010-07-14 18:48:53 -0700259 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Browne839a582010-04-22 18:58:52 -0700260
261 inline int32_t getAction() const { return mAction; }
262
Jeff Brownaf30ff62010-09-01 17:01:00 -0700263 inline int32_t getFlags() const { return mFlags; }
264
Jeff Browne839a582010-04-22 18:58:52 -0700265 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
266
267 inline int32_t getMetaState() const { return mMetaState; }
268
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700269 inline float getXOffset() const { return mXOffset; }
270
271 inline float getYOffset() const { return mYOffset; }
272
Jeff Browne839a582010-04-22 18:58:52 -0700273 inline float getXPrecision() const { return mXPrecision; }
274
275 inline float getYPrecision() const { return mYPrecision; }
276
277 inline nsecs_t getDownTime() const { return mDownTime; }
278
279 inline size_t getPointerCount() const { return mPointerIds.size(); }
280
281 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
282
283 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
284
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700285 inline float getRawX(size_t pointerIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700286 return getCurrentPointerCoords(pointerIndex).x;
287 }
288
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700289 inline float getRawY(size_t pointerIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700290 return getCurrentPointerCoords(pointerIndex).y;
291 }
292
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700293 inline float getX(size_t pointerIndex) const {
294 return getRawX(pointerIndex) + mXOffset;
295 }
296
297 inline float getY(size_t pointerIndex) const {
298 return getRawY(pointerIndex) + mYOffset;
299 }
300
Jeff Browne839a582010-04-22 18:58:52 -0700301 inline float getPressure(size_t pointerIndex) const {
302 return getCurrentPointerCoords(pointerIndex).pressure;
303 }
304
305 inline float getSize(size_t pointerIndex) const {
306 return getCurrentPointerCoords(pointerIndex).size;
307 }
308
Jeff Brown5c1ed842010-07-14 18:48:53 -0700309 inline float getTouchMajor(size_t pointerIndex) const {
310 return getCurrentPointerCoords(pointerIndex).touchMajor;
311 }
312
313 inline float getTouchMinor(size_t pointerIndex) const {
314 return getCurrentPointerCoords(pointerIndex).touchMinor;
315 }
316
317 inline float getToolMajor(size_t pointerIndex) const {
318 return getCurrentPointerCoords(pointerIndex).toolMajor;
319 }
320
321 inline float getToolMinor(size_t pointerIndex) const {
322 return getCurrentPointerCoords(pointerIndex).toolMinor;
323 }
324
325 inline float getOrientation(size_t pointerIndex) const {
326 return getCurrentPointerCoords(pointerIndex).orientation;
327 }
328
Jeff Browne839a582010-04-22 18:58:52 -0700329 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
330
331 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
332 return mSampleEventTimes[historicalIndex];
333 }
334
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700335 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700336 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
337 }
338
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700339 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700340 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
341 }
342
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700343 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
344 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
345 }
346
347 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
348 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
349 }
350
Jeff Browne839a582010-04-22 18:58:52 -0700351 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
352 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
353 }
354
355 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
356 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
357 }
358
Jeff Brown5c1ed842010-07-14 18:48:53 -0700359 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
360 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
361 }
362
363 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
364 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
365 }
366
367 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
368 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
369 }
370
371 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
372 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
373 }
374
375 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
376 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
377 }
378
Jeff Browne839a582010-04-22 18:58:52 -0700379 void initialize(
380 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700381 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700382 int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700383 int32_t flags,
Jeff Browne839a582010-04-22 18:58:52 -0700384 int32_t edgeFlags,
385 int32_t metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700386 float xOffset,
387 float yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700388 float xPrecision,
389 float yPrecision,
390 nsecs_t downTime,
391 nsecs_t eventTime,
392 size_t pointerCount,
393 const int32_t* pointerIds,
394 const PointerCoords* pointerCoords);
395
396 void addSample(
397 nsecs_t eventTime,
398 const PointerCoords* pointerCoords);
399
400 void offsetLocation(float xOffset, float yOffset);
401
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700402 // Low-level accessors.
403 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
404 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
405 inline const PointerCoords* getSamplePointerCoords() const {
406 return mSamplePointerCoords.array();
407 }
408
Jeff Browne839a582010-04-22 18:58:52 -0700409private:
410 int32_t mAction;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700411 int32_t mFlags;
Jeff Browne839a582010-04-22 18:58:52 -0700412 int32_t mEdgeFlags;
413 int32_t mMetaState;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700414 float mXOffset;
415 float mYOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700416 float mXPrecision;
417 float mYPrecision;
418 nsecs_t mDownTime;
419 Vector<int32_t> mPointerIds;
420 Vector<nsecs_t> mSampleEventTimes;
421 Vector<PointerCoords> mSamplePointerCoords;
422
423 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
424 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
425 }
426
427 inline const PointerCoords& getHistoricalPointerCoords(
428 size_t pointerIndex, size_t historicalIndex) const {
429 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
430 }
431};
432
433/*
434 * Input event factory.
435 */
436class InputEventFactoryInterface {
437protected:
438 virtual ~InputEventFactoryInterface() { }
439
440public:
441 InputEventFactoryInterface() { }
442
443 virtual KeyEvent* createKeyEvent() = 0;
444 virtual MotionEvent* createMotionEvent() = 0;
445};
446
447/*
448 * A simple input event factory implementation that uses a single preallocated instance
449 * of each type of input event that are reused for each request.
450 */
451class PreallocatedInputEventFactory : public InputEventFactoryInterface {
452public:
453 PreallocatedInputEventFactory() { }
454 virtual ~PreallocatedInputEventFactory() { }
455
456 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
457 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
458
459private:
460 KeyEvent mKeyEvent;
461 MotionEvent mMotionEvent;
462};
463
Jeff Browne57e8952010-07-23 21:28:06 -0700464/*
465 * Describes the characteristics and capabilities of an input device.
466 */
467class InputDeviceInfo {
468public:
469 InputDeviceInfo();
470 InputDeviceInfo(const InputDeviceInfo& other);
471 ~InputDeviceInfo();
472
473 struct MotionRange {
474 float min;
475 float max;
476 float flat;
477 float fuzz;
478 };
479
480 void initialize(int32_t id, const String8& name);
481
482 inline int32_t getId() const { return mId; }
483 inline const String8 getName() const { return mName; }
484 inline uint32_t getSources() const { return mSources; }
485
486 const MotionRange* getMotionRange(int32_t rangeType) const;
487
488 void addSource(uint32_t source);
489 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
490 void addMotionRange(int32_t rangeType, const MotionRange& range);
491
492 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
493 inline int32_t getKeyboardType() const { return mKeyboardType; }
494
Jeff Brown38a7fab2010-08-30 03:02:23 -0700495 inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
496 return mMotionRanges;
497 }
498
Jeff Browne57e8952010-07-23 21:28:06 -0700499private:
500 int32_t mId;
501 String8 mName;
502 uint32_t mSources;
503 int32_t mKeyboardType;
504
505 KeyedVector<int32_t, MotionRange> mMotionRanges;
506};
507
Jeff Browne839a582010-04-22 18:58:52 -0700508
509} // namespace android
510
511#endif // _UI_INPUT_H