blob: 2385973e6926a912c953792c484a8d45f76b8682 [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.
43 */
44#define MAX_POINTERS 10
45
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070046/*
47 * Declare a concrete type for the NDK's input event forward declaration.
48 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070049struct AInputEvent {
50 virtual ~AInputEvent() { }
51};
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070052
Jeff Brown46b9ac02010-04-22 18:58:52 -070053/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070054 * Declare a concrete type for the NDK's input device forward declaration.
Jeff Brown46b9ac02010-04-22 18:58:52 -070055 */
Jeff Brown6d0fec22010-07-23 21:28:06 -070056struct AInputDevice {
57 virtual ~AInputDevice() { }
Jeff Brown46b9ac02010-04-22 18:58:52 -070058};
59
Jeff Brown6d0fec22010-07-23 21:28:06 -070060
61namespace android {
62
Jeff Brown46b9ac02010-04-22 18:58:52 -070063/*
64 * Flags that flow alongside events in the input dispatch system to help with certain
65 * policy decisions such as waking from device sleep.
Jeff Brown46b9ac02010-04-22 18:58:52 -070066 */
67enum {
68 /* These flags originate in RawEvents and are generally set in the key map. */
69
70 POLICY_FLAG_WAKE = 0x00000001,
71 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
72 POLICY_FLAG_SHIFT = 0x00000004,
73 POLICY_FLAG_CAPS_LOCK = 0x00000008,
74 POLICY_FLAG_ALT = 0x00000010,
75 POLICY_FLAG_ALT_GR = 0x00000020,
76 POLICY_FLAG_MENU = 0x00000040,
77 POLICY_FLAG_LAUNCHER = 0x00000080,
78
Jeff Brown7fbdc842010-06-17 20:52:56 -070079 POLICY_FLAG_RAW_MASK = 0x0000ffff,
80
Jeff Brown9c3cda02010-06-15 01:31:58 -070081 /* These flags are set by the input reader policy as it intercepts each event. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070082
83 // Indicates that the screen was off when the event was received and the event
84 // should wake the device.
85 POLICY_FLAG_WOKE_HERE = 0x10000000,
86
87 // Indicates that the screen was dim when the event was received and the event
88 // should brighten the device.
89 POLICY_FLAG_BRIGHT_HERE = 0x20000000,
90};
91
92/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070093 * Describes the basic configuration of input devices that are present.
94 */
95struct InputConfiguration {
96 enum {
97 TOUCHSCREEN_UNDEFINED = 0,
98 TOUCHSCREEN_NOTOUCH = 1,
99 TOUCHSCREEN_STYLUS = 2,
100 TOUCHSCREEN_FINGER = 3
101 };
102
103 enum {
104 KEYBOARD_UNDEFINED = 0,
105 KEYBOARD_NOKEYS = 1,
106 KEYBOARD_QWERTY = 2,
107 KEYBOARD_12KEY = 3
108 };
109
110 enum {
111 NAVIGATION_UNDEFINED = 0,
112 NAVIGATION_NONAV = 1,
113 NAVIGATION_DPAD = 2,
114 NAVIGATION_TRACKBALL = 3,
115 NAVIGATION_WHEEL = 4
116 };
117
118 int32_t touchScreen;
119 int32_t keyboard;
120 int32_t navigation;
121};
122
123/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700124 * Pointer coordinate data.
125 */
126struct PointerCoords {
127 float x;
128 float y;
129 float pressure;
130 float size;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700131 float touchMajor;
132 float touchMinor;
133 float toolMajor;
134 float toolMinor;
135 float orientation;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700136};
137
138/*
139 * Input events.
140 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700141class InputEvent : public AInputEvent {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700142public:
143 virtual ~InputEvent() { }
144
145 virtual int32_t getType() const = 0;
146
147 inline int32_t getDeviceId() const { return mDeviceId; }
148
Jeff Brownc5ed5912010-07-14 18:48:53 -0700149 inline int32_t getSource() const { return mSource; }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700150
Jeff Brown46b9ac02010-04-22 18:58:52 -0700151protected:
Jeff Brownc5ed5912010-07-14 18:48:53 -0700152 void initialize(int32_t deviceId, int32_t source);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700153 void initialize(const InputEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700154
155private:
156 int32_t mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700157 int32_t mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700158};
159
Jeff Brown5c225b12010-06-16 01:53:36 -0700160/*
161 * Key events.
162 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700163class KeyEvent : public InputEvent {
164public:
165 virtual ~KeyEvent() { }
166
Jeff Brownc5ed5912010-07-14 18:48:53 -0700167 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700168
169 inline int32_t getAction() const { return mAction; }
170
171 inline int32_t getFlags() const { return mFlags; }
172
173 inline int32_t getKeyCode() const { return mKeyCode; }
174
175 inline int32_t getScanCode() const { return mScanCode; }
176
177 inline int32_t getMetaState() const { return mMetaState; }
178
179 inline int32_t getRepeatCount() const { return mRepeatCount; }
180
181 inline nsecs_t getDownTime() const { return mDownTime; }
182
183 inline nsecs_t getEventTime() const { return mEventTime; }
184
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700185 // Return true if this event may have a default action implementation.
186 static bool hasDefaultAction(int32_t keyCode);
187 bool hasDefaultAction() const;
188
189 // Return true if this event represents a system key.
190 static bool isSystemKey(int32_t keyCode);
191 bool isSystemKey() const;
192
Jeff Brown46b9ac02010-04-22 18:58:52 -0700193 void initialize(
194 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700195 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700196 int32_t action,
197 int32_t flags,
198 int32_t keyCode,
199 int32_t scanCode,
200 int32_t metaState,
201 int32_t repeatCount,
202 nsecs_t downTime,
203 nsecs_t eventTime);
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700204 void initialize(const KeyEvent& from);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205
206private:
207 int32_t mAction;
208 int32_t mFlags;
209 int32_t mKeyCode;
210 int32_t mScanCode;
211 int32_t mMetaState;
212 int32_t mRepeatCount;
213 nsecs_t mDownTime;
214 nsecs_t mEventTime;
215};
216
Jeff Brown5c225b12010-06-16 01:53:36 -0700217/*
218 * Motion events.
219 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700220class MotionEvent : public InputEvent {
221public:
222 virtual ~MotionEvent() { }
223
Jeff Brownc5ed5912010-07-14 18:48:53 -0700224 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225
226 inline int32_t getAction() const { return mAction; }
227
228 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
229
230 inline int32_t getMetaState() const { return mMetaState; }
231
Jeff Brown5c225b12010-06-16 01:53:36 -0700232 inline float getXOffset() const { return mXOffset; }
233
234 inline float getYOffset() const { return mYOffset; }
235
Jeff Brown46b9ac02010-04-22 18:58:52 -0700236 inline float getXPrecision() const { return mXPrecision; }
237
238 inline float getYPrecision() const { return mYPrecision; }
239
240 inline nsecs_t getDownTime() const { return mDownTime; }
241
242 inline size_t getPointerCount() const { return mPointerIds.size(); }
243
244 inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
245
246 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
247
Jeff Brown5c225b12010-06-16 01:53:36 -0700248 inline float getRawX(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700249 return getCurrentPointerCoords(pointerIndex).x;
250 }
251
Jeff Brown5c225b12010-06-16 01:53:36 -0700252 inline float getRawY(size_t pointerIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700253 return getCurrentPointerCoords(pointerIndex).y;
254 }
255
Jeff Brown5c225b12010-06-16 01:53:36 -0700256 inline float getX(size_t pointerIndex) const {
257 return getRawX(pointerIndex) + mXOffset;
258 }
259
260 inline float getY(size_t pointerIndex) const {
261 return getRawY(pointerIndex) + mYOffset;
262 }
263
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 inline float getPressure(size_t pointerIndex) const {
265 return getCurrentPointerCoords(pointerIndex).pressure;
266 }
267
268 inline float getSize(size_t pointerIndex) const {
269 return getCurrentPointerCoords(pointerIndex).size;
270 }
271
Jeff Brownc5ed5912010-07-14 18:48:53 -0700272 inline float getTouchMajor(size_t pointerIndex) const {
273 return getCurrentPointerCoords(pointerIndex).touchMajor;
274 }
275
276 inline float getTouchMinor(size_t pointerIndex) const {
277 return getCurrentPointerCoords(pointerIndex).touchMinor;
278 }
279
280 inline float getToolMajor(size_t pointerIndex) const {
281 return getCurrentPointerCoords(pointerIndex).toolMajor;
282 }
283
284 inline float getToolMinor(size_t pointerIndex) const {
285 return getCurrentPointerCoords(pointerIndex).toolMinor;
286 }
287
288 inline float getOrientation(size_t pointerIndex) const {
289 return getCurrentPointerCoords(pointerIndex).orientation;
290 }
291
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
293
294 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
295 return mSampleEventTimes[historicalIndex];
296 }
297
Jeff Brown5c225b12010-06-16 01:53:36 -0700298 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700299 return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
300 }
301
Jeff Brown5c225b12010-06-16 01:53:36 -0700302 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700303 return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
304 }
305
Jeff Brown5c225b12010-06-16 01:53:36 -0700306 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
307 return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
308 }
309
310 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
311 return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
312 }
313
Jeff Brown46b9ac02010-04-22 18:58:52 -0700314 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
315 return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
316 }
317
318 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
319 return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
320 }
321
Jeff Brownc5ed5912010-07-14 18:48:53 -0700322 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
323 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
324 }
325
326 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
327 return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
328 }
329
330 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
331 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
332 }
333
334 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
335 return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
336 }
337
338 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
339 return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
340 }
341
Jeff Brown46b9ac02010-04-22 18:58:52 -0700342 void initialize(
343 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700344 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345 int32_t action,
346 int32_t edgeFlags,
347 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700348 float xOffset,
349 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700350 float xPrecision,
351 float yPrecision,
352 nsecs_t downTime,
353 nsecs_t eventTime,
354 size_t pointerCount,
355 const int32_t* pointerIds,
356 const PointerCoords* pointerCoords);
357
358 void addSample(
359 nsecs_t eventTime,
360 const PointerCoords* pointerCoords);
361
362 void offsetLocation(float xOffset, float yOffset);
363
Jeff Brown5c225b12010-06-16 01:53:36 -0700364 // Low-level accessors.
365 inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
366 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
367 inline const PointerCoords* getSamplePointerCoords() const {
368 return mSamplePointerCoords.array();
369 }
370
Jeff Brown46b9ac02010-04-22 18:58:52 -0700371private:
372 int32_t mAction;
373 int32_t mEdgeFlags;
374 int32_t mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700375 float mXOffset;
376 float mYOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700377 float mXPrecision;
378 float mYPrecision;
379 nsecs_t mDownTime;
380 Vector<int32_t> mPointerIds;
381 Vector<nsecs_t> mSampleEventTimes;
382 Vector<PointerCoords> mSamplePointerCoords;
383
384 inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
385 return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
386 }
387
388 inline const PointerCoords& getHistoricalPointerCoords(
389 size_t pointerIndex, size_t historicalIndex) const {
390 return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
391 }
392};
393
394/*
395 * Input event factory.
396 */
397class InputEventFactoryInterface {
398protected:
399 virtual ~InputEventFactoryInterface() { }
400
401public:
402 InputEventFactoryInterface() { }
403
404 virtual KeyEvent* createKeyEvent() = 0;
405 virtual MotionEvent* createMotionEvent() = 0;
406};
407
408/*
409 * A simple input event factory implementation that uses a single preallocated instance
410 * of each type of input event that are reused for each request.
411 */
412class PreallocatedInputEventFactory : public InputEventFactoryInterface {
413public:
414 PreallocatedInputEventFactory() { }
415 virtual ~PreallocatedInputEventFactory() { }
416
417 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
418 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
419
420private:
421 KeyEvent mKeyEvent;
422 MotionEvent mMotionEvent;
423};
424
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425/*
426 * Describes the characteristics and capabilities of an input device.
427 */
428class InputDeviceInfo {
429public:
430 InputDeviceInfo();
431 InputDeviceInfo(const InputDeviceInfo& other);
432 ~InputDeviceInfo();
433
434 struct MotionRange {
435 float min;
436 float max;
437 float flat;
438 float fuzz;
439 };
440
441 void initialize(int32_t id, const String8& name);
442
443 inline int32_t getId() const { return mId; }
444 inline const String8 getName() const { return mName; }
445 inline uint32_t getSources() const { return mSources; }
446
447 const MotionRange* getMotionRange(int32_t rangeType) const;
448
449 void addSource(uint32_t source);
450 void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
451 void addMotionRange(int32_t rangeType, const MotionRange& range);
452
453 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
454 inline int32_t getKeyboardType() const { return mKeyboardType; }
455
456private:
457 int32_t mId;
458 String8 mName;
459 uint32_t mSources;
460 int32_t mKeyboardType;
461
462 KeyedVector<int32_t, MotionRange> mMotionRanges;
463};
464
465/*
466 * Provides remote access to information about an input device.
467 *
468 * Note: This is essentially a wrapper for Binder calls into the Window Manager Service.
469 */
470class InputDeviceProxy : public RefBase, public AInputDevice {
471protected:
472 InputDeviceProxy();
473 virtual ~InputDeviceProxy();
474
475public:
476 static void getDeviceIds(Vector<int32_t>& outIds);
477
478 static sp<InputDeviceProxy> getDevice(int32_t id);
479
480 inline const InputDeviceInfo* getInfo() { return & mInfo; }
481
482 // TODO add hasKeys, keymap, etc...
483
484private:
485 InputDeviceInfo mInfo;
486};
487
Jeff Brown46b9ac02010-04-22 18:58:52 -0700488
489} // namespace android
490
491#endif // _UI_INPUT_H