blob: ee40b85d7b3399fc78699617b7b18b769cf4dc18 [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
41/*
42 * Maximum number of pointers supported per motion event.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070043 * Smallest number of pointers is 1.
Jeff Browne839a582010-04-22 18:58:52 -070044 */
45#define MAX_POINTERS 10
46
Dianne Hackborn4d96bb62010-06-18 18:09:33 -070047/*
Jeff Brownd1b0a2b2010-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 Hackborn4d96bb62010-06-18 18:09:33 -070055 * Declare a concrete type for the NDK's input event forward declaration.
56 */
Dianne Hackbornce838a22010-07-13 17:48:30 -070057struct AInputEvent {
58 virtual ~AInputEvent() { }
59};
Dianne Hackborn4d96bb62010-06-18 18:09:33 -070060
Jeff Browne839a582010-04-22 18:58:52 -070061/*
Jeff Browne57e8952010-07-23 21:28:06 -070062 * Declare a concrete type for the NDK's input device forward declaration.
Jeff Browne839a582010-04-22 18:58:52 -070063 */
Jeff Browne57e8952010-07-23 21:28:06 -070064struct AInputDevice {
65 virtual ~AInputDevice() { }
Jeff Browne839a582010-04-22 18:58:52 -070066};
67
Jeff Browne57e8952010-07-23 21:28:06 -070068
69namespace android {
70
Jeff Browne839a582010-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 Browne839a582010-04-22 18:58:52 -070074 */
75enum {
Jeff Brown956c0fb2010-10-01 14:55:30 -070076 /* These flags originate in RawEvents and are generally set in the key map.
77 * See also labels for policy flags in KeycodeLabels.h. */
Jeff Browne839a582010-04-22 18:58:52 -070078
79 POLICY_FLAG_WAKE = 0x00000001,
80 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
81 POLICY_FLAG_SHIFT = 0x00000004,
82 POLICY_FLAG_CAPS_LOCK = 0x00000008,
83 POLICY_FLAG_ALT = 0x00000010,
84 POLICY_FLAG_ALT_GR = 0x00000020,
85 POLICY_FLAG_MENU = 0x00000040,
86 POLICY_FLAG_LAUNCHER = 0x00000080,
Jeff Brown956c0fb2010-10-01 14:55:30 -070087 POLICY_FLAG_VIRTUAL = 0x00000100,
Jeff Browne839a582010-04-22 18:58:52 -070088
Jeff Brown51d45a72010-06-17 20:52:56 -070089 POLICY_FLAG_RAW_MASK = 0x0000ffff,
90
Jeff Brownaf30ff62010-09-01 17:01:00 -070091 /* These flags are set by the input dispatcher. */
92
93 // Indicates that the input event was injected.
94 POLICY_FLAG_INJECTED = 0x01000000,
95
Jeff Brown54bc2812010-06-15 01:31:58 -070096 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Browne839a582010-04-22 18:58:52 -070097
98 // Indicates that the screen was off when the event was received and the event
99 // should wake the device.
100 POLICY_FLAG_WOKE_HERE = 0x10000000,
101
102 // Indicates that the screen was dim when the event was received and the event
103 // should brighten the device.
104 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
105};
106
107/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700108 * Describes the basic configuration of input devices that are present.
109 */
110struct InputConfiguration {
111 enum {
112 TOUCHSCREEN_UNDEFINED = 0,
113 TOUCHSCREEN_NOTOUCH = 1,
114 TOUCHSCREEN_STYLUS = 2,
115 TOUCHSCREEN_FINGER = 3
116 };
117
118 enum {
119 KEYBOARD_UNDEFINED = 0,
120 KEYBOARD_NOKEYS = 1,
121 KEYBOARD_QWERTY = 2,
122 KEYBOARD_12KEY = 3
123 };
124
125 enum {
126 NAVIGATION_UNDEFINED = 0,
127 NAVIGATION_NONAV = 1,
128 NAVIGATION_DPAD = 2,
129 NAVIGATION_TRACKBALL = 3,
130 NAVIGATION_WHEEL = 4
131 };
132
133 int32_t touchScreen;
134 int32_t keyboard;
135 int32_t navigation;
136};
137
138/*
Jeff Browne839a582010-04-22 18:58:52 -0700139 * Pointer coordinate data.
140 */
141struct PointerCoords {
142 float x;
143 float y;
144 float pressure;
145 float size;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700146 float touchMajor;
147 float touchMinor;
148 float toolMajor;
149 float toolMinor;
150 float orientation;
Jeff Browne839a582010-04-22 18:58:52 -0700151};
152
153/*
154 * Input events.
155 */
Dianne Hackborn9c7f8182010-06-28 15:27:30 -0700156class InputEvent : public AInputEvent {
Jeff Browne839a582010-04-22 18:58:52 -0700157public:
158 virtual ~InputEvent() { }
159
160 virtual int32_t getType() const = 0;
161
162 inline int32_t getDeviceId() const { return mDeviceId; }
163
Jeff Brown5c1ed842010-07-14 18:48:53 -0700164 inline int32_t getSource() const { return mSource; }
Dianne Hackborn189ed232010-06-29 19:20:40 -0700165
Jeff Browne839a582010-04-22 18:58:52 -0700166protected:
Jeff Brown5c1ed842010-07-14 18:48:53 -0700167 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn0e885272010-07-15 17:44:53 -0700168 void initialize(const InputEvent& from);
Jeff Browne839a582010-04-22 18:58:52 -0700169
170private:
171 int32_t mDeviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700172 int32_t mSource;
Jeff Browne839a582010-04-22 18:58:52 -0700173};
174
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700175/*
176 * Key events.
177 */
Jeff Browne839a582010-04-22 18:58:52 -0700178class KeyEvent : public InputEvent {
179public:
180 virtual ~KeyEvent() { }
181
Jeff Brown5c1ed842010-07-14 18:48:53 -0700182 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Browne839a582010-04-22 18:58:52 -0700183
184 inline int32_t getAction() const { return mAction; }
185
186 inline int32_t getFlags() const { return mFlags; }
187
188 inline int32_t getKeyCode() const { return mKeyCode; }
189
190 inline int32_t getScanCode() const { return mScanCode; }
191
192 inline int32_t getMetaState() const { return mMetaState; }
193
194 inline int32_t getRepeatCount() const { return mRepeatCount; }
195
196 inline nsecs_t getDownTime() const { return mDownTime; }
197
198 inline nsecs_t getEventTime() const { return mEventTime; }
199
Dianne Hackborn189ed232010-06-29 19:20:40 -0700200 // Return true if this event may have a default action implementation.
201 static bool hasDefaultAction(int32_t keyCode);
202 bool hasDefaultAction() const;
203
204 // Return true if this event represents a system key.
205 static bool isSystemKey(int32_t keyCode);
206 bool isSystemKey() const;
207
Jeff Browne839a582010-04-22 18:58:52 -0700208 void initialize(
209 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700210 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700211 int32_t action,
212 int32_t flags,
213 int32_t keyCode,
214 int32_t scanCode,
215 int32_t metaState,
216 int32_t repeatCount,
217 nsecs_t downTime,
218 nsecs_t eventTime);
Dianne Hackborn0e885272010-07-15 17:44:53 -0700219 void initialize(const KeyEvent& from);
Jeff Browne839a582010-04-22 18:58:52 -0700220
221private:
222 int32_t mAction;
223 int32_t mFlags;
224 int32_t mKeyCode;
225 int32_t mScanCode;
226 int32_t mMetaState;
227 int32_t mRepeatCount;
228 nsecs_t mDownTime;
229 nsecs_t mEventTime;
230};
231
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700232/*
233 * Motion events.
234 */
Jeff Browne839a582010-04-22 18:58:52 -0700235class MotionEvent : public InputEvent {
236public:
237 virtual ~MotionEvent() { }
238
Jeff Brown5c1ed842010-07-14 18:48:53 -0700239 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Browne839a582010-04-22 18:58:52 -0700240
241 inline int32_t getAction() const { return mAction; }
242
Jeff Brownaf30ff62010-09-01 17:01:00 -0700243 inline int32_t getFlags() const { return mFlags; }
244
Jeff Browne839a582010-04-22 18:58:52 -0700245 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
246
247 inline int32_t getMetaState() const { return mMetaState; }
248
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700249 inline float getXOffset() const { return mXOffset; }
250
251 inline float getYOffset() const { return mYOffset; }
252
Jeff Browne839a582010-04-22 18:58:52 -0700253 inline float getXPrecision() const { return mXPrecision; }
254
255 inline float getYPrecision() const { return mYPrecision; }
256
257 inline nsecs_t getDownTime() const { return mDownTime; }
258
259 inline size_t getPointerCount() const { return mPointerIds.size(); }
260
261 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
262
263 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
264
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700265 inline float getRawX(size_t pointerIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700266 return getCurrentPointerCoords(pointerIndex).x;
267 }
268
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700269 inline float getRawY(size_t pointerIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700270 return getCurrentPointerCoords(pointerIndex).y;
271 }
272
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700273 inline float getX(size_t pointerIndex) const {
274 return getRawX(pointerIndex) + mXOffset;
275 }
276
277 inline float getY(size_t pointerIndex) const {
278 return getRawY(pointerIndex) + mYOffset;
279 }
280
Jeff Browne839a582010-04-22 18:58:52 -0700281 inline float getPressure(size_t pointerIndex) const {
282 return getCurrentPointerCoords(pointerIndex).pressure;
283 }
284
285 inline float getSize(size_t pointerIndex) const {
286 return getCurrentPointerCoords(pointerIndex).size;
287 }
288
Jeff Brown5c1ed842010-07-14 18:48:53 -0700289 inline float getTouchMajor(size_t pointerIndex) const {
290 return getCurrentPointerCoords(pointerIndex).touchMajor;
291 }
292
293 inline float getTouchMinor(size_t pointerIndex) const {
294 return getCurrentPointerCoords(pointerIndex).touchMinor;
295 }
296
297 inline float getToolMajor(size_t pointerIndex) const {
298 return getCurrentPointerCoords(pointerIndex).toolMajor;
299 }
300
301 inline float getToolMinor(size_t pointerIndex) const {
302 return getCurrentPointerCoords(pointerIndex).toolMinor;
303 }
304
305 inline float getOrientation(size_t pointerIndex) const {
306 return getCurrentPointerCoords(pointerIndex).orientation;
307 }
308
Jeff Browne839a582010-04-22 18:58:52 -0700309 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
310
311 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
312 return mSampleEventTimes[historicalIndex];
313 }
314
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700315 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700316 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
317 }
318
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700319 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Browne839a582010-04-22 18:58:52 -0700320 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
321 }
322
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700323 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
324 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
325 }
326
327 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
328 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
329 }
330
Jeff Browne839a582010-04-22 18:58:52 -0700331 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
332 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
333 }
334
335 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
336 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
337 }
338
Jeff Brown5c1ed842010-07-14 18:48:53 -0700339 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
340 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
341 }
342
343 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
344 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
345 }
346
347 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
348 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
349 }
350
351 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
352 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
353 }
354
355 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
356 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
357 }
358
Jeff Browne839a582010-04-22 18:58:52 -0700359 void initialize(
360 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700361 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700362 int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700363 int32_t flags,
Jeff Browne839a582010-04-22 18:58:52 -0700364 int32_t edgeFlags,
365 int32_t metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700366 float xOffset,
367 float yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700368 float xPrecision,
369 float yPrecision,
370 nsecs_t downTime,
371 nsecs_t eventTime,
372 size_t pointerCount,
373 const int32_t* pointerIds,
374 const PointerCoords* pointerCoords);
375
376 void addSample(
377 nsecs_t eventTime,
378 const PointerCoords* pointerCoords);
379
380 void offsetLocation(float xOffset, float yOffset);
381
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700382 // Low-level accessors.
383 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
384 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
385 inline const PointerCoords* getSamplePointerCoords() const {
386 return mSamplePointerCoords.array();
387 }
388
Jeff Browne839a582010-04-22 18:58:52 -0700389private:
390 int32_t mAction;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700391 int32_t mFlags;
Jeff Browne839a582010-04-22 18:58:52 -0700392 int32_t mEdgeFlags;
393 int32_t mMetaState;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700394 float mXOffset;
395 float mYOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700396 float mXPrecision;
397 float mYPrecision;
398 nsecs_t mDownTime;
399 Vector<int32_t> mPointerIds;
400 Vector<nsecs_t> mSampleEventTimes;
401 Vector<PointerCoords> mSamplePointerCoords;
402
403 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
404 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
405 }
406
407 inline const PointerCoords& getHistoricalPointerCoords(
408 size_t pointerIndex, size_t historicalIndex) const {
409 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
410 }
411};
412
413/*
414 * Input event factory.
415 */
416class InputEventFactoryInterface {
417protected:
418 virtual ~InputEventFactoryInterface() { }
419
420public:
421 InputEventFactoryInterface() { }
422
423 virtual KeyEvent* createKeyEvent() = 0;
424 virtual MotionEvent* createMotionEvent() = 0;
425};
426
427/*
428 * A simple input event factory implementation that uses a single preallocated instance
429 * of each type of input event that are reused for each request.
430 */
431class PreallocatedInputEventFactory : public InputEventFactoryInterface {
432public:
433 PreallocatedInputEventFactory() { }
434 virtual ~PreallocatedInputEventFactory() { }
435
436 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
437 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
438
439private:
440 KeyEvent mKeyEvent;
441 MotionEvent mMotionEvent;
442};
443
Jeff Browne57e8952010-07-23 21:28:06 -0700444/*
445 * Describes the characteristics and capabilities of an input device.
446 */
447class InputDeviceInfo {
448public:
449 InputDeviceInfo();
450 InputDeviceInfo(const InputDeviceInfo& other);
451 ~InputDeviceInfo();
452
453 struct MotionRange {
454 float min;
455 float max;
456 float flat;
457 float fuzz;
458 };
459
460 void initialize(int32_t id, const String8& name);
461
462 inline int32_t getId() const { return mId; }
463 inline const String8 getName() const { return mName; }
464 inline uint32_t getSources() const { return mSources; }
465
466 const MotionRange* getMotionRange(int32_t rangeType) const;
467
468 void addSource(uint32_t source);
469 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
470 void addMotionRange(int32_t rangeType, const MotionRange& range);
471
472 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
473 inline int32_t getKeyboardType() const { return mKeyboardType; }
474
Jeff Brown38a7fab2010-08-30 03:02:23 -0700475 inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
476 return mMotionRanges;
477 }
478
Jeff Browne57e8952010-07-23 21:28:06 -0700479private:
480 int32_t mId;
481 String8 mName;
482 uint32_t mSources;
483 int32_t mKeyboardType;
484
485 KeyedVector<int32_t, MotionRange> mMotionRanges;
486};
487
Jeff Browne839a582010-04-22 18:58:52 -0700488
489} // namespace android
490
491#endif // _UI_INPUT_H