blob: 21baf325991334b47d226820b70f82ee15b7f425 [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 Brown46b9ac02010-04-22 18:58:52 -070074 */
75enum {
76 /* These flags originate in RawEvents and are generally set in the key map. */
77
78 POLICY_FLAG_WAKE = 0x00000001,
79 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
80 POLICY_FLAG_SHIFT = 0x00000004,
81 POLICY_FLAG_CAPS_LOCK = 0x00000008,
82 POLICY_FLAG_ALT = 0x00000010,
83 POLICY_FLAG_ALT_GR = 0x00000020,
84 POLICY_FLAG_MENU = 0x00000040,
85 POLICY_FLAG_LAUNCHER = 0x00000080,
86
Jeff Brown7fbdc842010-06-17 20:52:56 -070087 POLICY_FLAG_RAW_MASK = 0x0000ffff,
88
Jeff Brown85a31762010-09-01 17:01:00 -070089 /* These flags are set by the input dispatcher. */
90
91 // Indicates that the input event was injected.
92 POLICY_FLAG_INJECTED = 0x01000000,
93
Jeff Brown9c3cda02010-06-15 01:31:58 -070094 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070095
96 // Indicates that the screen was off when the event was received and the event
97 // should wake the device.
98 POLICY_FLAG_WOKE_HERE = 0x10000000,
99
100 // Indicates that the screen was dim when the event was received and the event
101 // should brighten the device.
102 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
103};
104
105/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700106 * Describes the basic configuration of input devices that are present.
107 */
108struct InputConfiguration {
109 enum {
110 TOUCHSCREEN_UNDEFINED = 0,
111 TOUCHSCREEN_NOTOUCH = 1,
112 TOUCHSCREEN_STYLUS = 2,
113 TOUCHSCREEN_FINGER = 3
114 };
115
116 enum {
117 KEYBOARD_UNDEFINED = 0,
118 KEYBOARD_NOKEYS = 1,
119 KEYBOARD_QWERTY = 2,
120 KEYBOARD_12KEY = 3
121 };
122
123 enum {
124 NAVIGATION_UNDEFINED = 0,
125 NAVIGATION_NONAV = 1,
126 NAVIGATION_DPAD = 2,
127 NAVIGATION_TRACKBALL = 3,
128 NAVIGATION_WHEEL = 4
129 };
130
131 int32_t touchScreen;
132 int32_t keyboard;
133 int32_t navigation;
134};
135
136/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700137 * Pointer coordinate data.
138 */
139struct PointerCoords {
140 float x;
141 float y;
142 float pressure;
143 float size;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700144 float touchMajor;
145 float touchMinor;
146 float toolMajor;
147 float toolMinor;
148 float orientation;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700149};
150
151/*
152 * Input events.
153 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700154class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700155public:
156 virtual ~InputEvent() { }
157
158 virtual int32_t getType() const = 0;
159
160 inline int32_t getDeviceId() const { return mDeviceId; }
161
Jeff Brownc5ed5912010-07-14 18:48:53 -0700162 inline int32_t getSource() const { return mSource; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700163
Jeff Brown46b9ac02010-04-22 18:58:52 -0700164protected:
Jeff Brownc5ed5912010-07-14 18:48:53 -0700165 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700166 void initialize(const InputEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700167
168private:
169 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700170 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171};
172
Jeff Brown5c225b12010-06-16 01:53:36 -0700173/*
174 * Key events.
175 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700176class KeyEvent : public InputEvent {
177public:
178 virtual ~KeyEvent() { }
179
Jeff Brownc5ed5912010-07-14 18:48:53 -0700180 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700181
182 inline int32_t getAction() const { return mAction; }
183
184 inline int32_t getFlags() const { return mFlags; }
185
186 inline int32_t getKeyCode() const { return mKeyCode; }
187
188 inline int32_t getScanCode() const { return mScanCode; }
189
190 inline int32_t getMetaState() const { return mMetaState; }
191
192 inline int32_t getRepeatCount() const { return mRepeatCount; }
193
194 inline nsecs_t getDownTime() const { return mDownTime; }
195
196 inline nsecs_t getEventTime() const { return mEventTime; }
197
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700198 // Return true if this event may have a default action implementation.
199 static bool hasDefaultAction(int32_t keyCode);
200 bool hasDefaultAction() const;
201
202 // Return true if this event represents a system key.
203 static bool isSystemKey(int32_t keyCode);
204 bool isSystemKey() const;
205
Jeff Brown46b9ac02010-04-22 18:58:52 -0700206 void initialize(
207 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700208 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700209 int32_t action,
210 int32_t flags,
211 int32_t keyCode,
212 int32_t scanCode,
213 int32_t metaState,
214 int32_t repeatCount,
215 nsecs_t downTime,
216 nsecs_t eventTime);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700217 void initialize(const KeyEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700218
219private:
220 int32_t mAction;
221 int32_t mFlags;
222 int32_t mKeyCode;
223 int32_t mScanCode;
224 int32_t mMetaState;
225 int32_t mRepeatCount;
226 nsecs_t mDownTime;
227 nsecs_t mEventTime;
228};
229
Jeff Brown5c225b12010-06-16 01:53:36 -0700230/*
231 * Motion events.
232 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700233class MotionEvent : public InputEvent {
234public:
235 virtual ~MotionEvent() { }
236
Jeff Brownc5ed5912010-07-14 18:48:53 -0700237 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700238
239 inline int32_t getAction() const { return mAction; }
240
Jeff Brown85a31762010-09-01 17:01:00 -0700241 inline int32_t getFlags() const { return mFlags; }
242
Jeff Brown46b9ac02010-04-22 18:58:52 -0700243 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
244
245 inline int32_t getMetaState() const { return mMetaState; }
246
Jeff Brown5c225b12010-06-16 01:53:36 -0700247 inline float getXOffset() const { return mXOffset; }
248
249 inline float getYOffset() const { return mYOffset; }
250
Jeff Brown46b9ac02010-04-22 18:58:52 -0700251 inline float getXPrecision() const { return mXPrecision; }
252
253 inline float getYPrecision() const { return mYPrecision; }
254
255 inline nsecs_t getDownTime() const { return mDownTime; }
256
257 inline size_t getPointerCount() const { return mPointerIds.size(); }
258
259 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
260
261 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
262
Jeff Brown5c225b12010-06-16 01:53:36 -0700263 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 return getCurrentPointerCoords(pointerIndex).x;
265 }
266
Jeff Brown5c225b12010-06-16 01:53:36 -0700267 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700268 return getCurrentPointerCoords(pointerIndex).y;
269 }
270
Jeff Brown5c225b12010-06-16 01:53:36 -0700271 inline float getX(size_t pointerIndex) const {
272 return getRawX(pointerIndex) + mXOffset;
273 }
274
275 inline float getY(size_t pointerIndex) const {
276 return getRawY(pointerIndex) + mYOffset;
277 }
278
Jeff Brown46b9ac02010-04-22 18:58:52 -0700279 inline float getPressure(size_t pointerIndex) const {
280 return getCurrentPointerCoords(pointerIndex).pressure;
281 }
282
283 inline float getSize(size_t pointerIndex) const {
284 return getCurrentPointerCoords(pointerIndex).size;
285 }
286
Jeff Brownc5ed5912010-07-14 18:48:53 -0700287 inline float getTouchMajor(size_t pointerIndex) const {
288 return getCurrentPointerCoords(pointerIndex).touchMajor;
289 }
290
291 inline float getTouchMinor(size_t pointerIndex) const {
292 return getCurrentPointerCoords(pointerIndex).touchMinor;
293 }
294
295 inline float getToolMajor(size_t pointerIndex) const {
296 return getCurrentPointerCoords(pointerIndex).toolMajor;
297 }
298
299 inline float getToolMinor(size_t pointerIndex) const {
300 return getCurrentPointerCoords(pointerIndex).toolMinor;
301 }
302
303 inline float getOrientation(size_t pointerIndex) const {
304 return getCurrentPointerCoords(pointerIndex).orientation;
305 }
306
Jeff Brown46b9ac02010-04-22 18:58:52 -0700307 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
308
309 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
310 return mSampleEventTimes[historicalIndex];
311 }
312
Jeff Brown5c225b12010-06-16 01:53:36 -0700313 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700314 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
315 }
316
Jeff Brown5c225b12010-06-16 01:53:36 -0700317 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700318 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
319 }
320
Jeff Brown5c225b12010-06-16 01:53:36 -0700321 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
322 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
323 }
324
325 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
326 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
327 }
328
Jeff Brown46b9ac02010-04-22 18:58:52 -0700329 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
330 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
331 }
332
333 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
334 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
335 }
336
Jeff Brownc5ed5912010-07-14 18:48:53 -0700337 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
338 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
339 }
340
341 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
342 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
343 }
344
345 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
346 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
347 }
348
349 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
350 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
351 }
352
353 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
354 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
355 }
356
Jeff Brown46b9ac02010-04-22 18:58:52 -0700357 void initialize(
358 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700359 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700360 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700361 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362 int32_t edgeFlags,
363 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700364 float xOffset,
365 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366 float xPrecision,
367 float yPrecision,
368 nsecs_t downTime,
369 nsecs_t eventTime,
370 size_t pointerCount,
371 const int32_t* pointerIds,
372 const PointerCoords* pointerCoords);
373
374 void addSample(
375 nsecs_t eventTime,
376 const PointerCoords* pointerCoords);
377
378 void offsetLocation(float xOffset, float yOffset);
379
Jeff Brown5c225b12010-06-16 01:53:36 -0700380 // Low-level accessors.
381 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
382 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
383 inline const PointerCoords* getSamplePointerCoords() const {
384 return mSamplePointerCoords.array();
385 }
386
Jeff Brown46b9ac02010-04-22 18:58:52 -0700387private:
388 int32_t mAction;
Jeff Brown85a31762010-09-01 17:01:00 -0700389 int32_t mFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700390 int32_t mEdgeFlags;
391 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700392 float mXOffset;
393 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700394 float mXPrecision;
395 float mYPrecision;
396 nsecs_t mDownTime;
397 Vector<int32_t> mPointerIds;
398 Vector<nsecs_t> mSampleEventTimes;
399 Vector<PointerCoords> mSamplePointerCoords;
400
401 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
402 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
403 }
404
405 inline const PointerCoords& getHistoricalPointerCoords(
406 size_t pointerIndex, size_t historicalIndex) const {
407 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
408 }
409};
410
411/*
412 * Input event factory.
413 */
414class InputEventFactoryInterface {
415protected:
416 virtual ~InputEventFactoryInterface() { }
417
418public:
419 InputEventFactoryInterface() { }
420
421 virtual KeyEvent* createKeyEvent() = 0;
422 virtual MotionEvent* createMotionEvent() = 0;
423};
424
425/*
426 * A simple input event factory implementation that uses a single preallocated instance
427 * of each type of input event that are reused for each request.
428 */
429class PreallocatedInputEventFactory : public InputEventFactoryInterface {
430public:
431 PreallocatedInputEventFactory() { }
432 virtual ~PreallocatedInputEventFactory() { }
433
434 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
435 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
436
437private:
438 KeyEvent mKeyEvent;
439 MotionEvent mMotionEvent;
440};
441
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442/*
443 * Describes the characteristics and capabilities of an input device.
444 */
445class InputDeviceInfo {
446public:
447 InputDeviceInfo();
448 InputDeviceInfo(const InputDeviceInfo& other);
449 ~InputDeviceInfo();
450
451 struct MotionRange {
452 float min;
453 float max;
454 float flat;
455 float fuzz;
456 };
457
458 void initialize(int32_t id, const String8& name);
459
460 inline int32_t getId() const { return mId; }
461 inline const String8 getName() const { return mName; }
462 inline uint32_t getSources() const { return mSources; }
463
464 const MotionRange* getMotionRange(int32_t rangeType) const;
465
466 void addSource(uint32_t source);
467 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
468 void addMotionRange(int32_t rangeType, const MotionRange& range);
469
470 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
471 inline int32_t getKeyboardType() const { return mKeyboardType; }
472
Jeff Brown8d608662010-08-30 03:02:23 -0700473 inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
474 return mMotionRanges;
475 }
476
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477private:
478 int32_t mId;
479 String8 mName;
480 uint32_t mSources;
481 int32_t mKeyboardType;
482
483 KeyedVector<int32_t, MotionRange> mMotionRanges;
484};
485
Jeff Brown46b9ac02010-04-22 18:58:52 -0700486
487} // namespace android
488
489#endif // _UI_INPUT_H