blob: 8257dbcef0977812170be4f763ab15d7837ad8a5 [file] [log] [blame]
Jeff Brown46b9ac0a2010-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_READER_H
18#define _UI_INPUT_READER_H
19
Jeff Brownb4ff35d2011-01-02 16:37:43 -080020#include "EventHub.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080021#include "PointerController.h"
Jeff Brownbe1aa822011-07-27 16:04:54 -070022#include "InputListener.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080023
Mathias Agopianb93a03f82012-02-17 15:34:57 -080024#include <androidfw/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31#include <utils/BitSet.h>
32
33#include <stddef.h>
34#include <unistd.h>
35
Jeff Browna47425a2012-04-13 04:09:27 -070036// Maximum supported size of a vibration pattern.
37// Must be at least 2.
38#define MAX_VIBRATE_PATTERN_SIZE 100
39
40// Maximum allowable delay value in a vibration pattern before
41// which the delay will be truncated.
42#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
43
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044namespace android {
45
Jeff Brown6d0fec22010-07-23 21:28:06 -070046class InputDevice;
47class InputMapper;
48
Jeff Brown8d608662010-08-30 03:02:23 -070049
Jeff Brown9c3cda02010-06-15 01:31:58 -070050/*
Jeff Brown214eaf42011-05-26 19:17:02 -070051 * Input reader configuration.
52 *
53 * Specifies various options that modify the behavior of the input reader.
54 */
55struct InputReaderConfiguration {
Jeff Brown474dcb52011-06-14 20:22:50 -070056 // Describes changes that have occurred.
57 enum {
58 // The pointer speed changed.
59 CHANGE_POINTER_SPEED = 1 << 0,
60
61 // The pointer gesture control changed.
62 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
63
Jeff Brown65fd2512011-08-18 11:20:58 -070064 // The display size or orientation changed.
65 CHANGE_DISPLAY_INFO = 1 << 2,
66
Jeff Browndaf4a122011-08-26 17:14:14 -070067 // The visible touches option changed.
68 CHANGE_SHOW_TOUCHES = 1 << 3,
69
Jeff Brown6ec6f792012-04-17 16:52:41 -070070 // The keyboard layouts must be reloaded.
71 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
72
Jeff Brown5bbd4b42012-04-20 19:28:00 -070073 // The device name alias supplied by the may have changed for some devices.
74 CHANGE_DEVICE_ALIAS = 1 << 5,
75
Jeff Brown474dcb52011-06-14 20:22:50 -070076 // All devices must be reopened.
77 CHANGE_MUST_REOPEN = 1 << 31,
78 };
79
Jeff Brown214eaf42011-05-26 19:17:02 -070080 // Gets the amount of time to disable virtual keys after the screen is touched
81 // in order to filter out accidental virtual key presses due to swiping gestures
82 // or taps near the edge of the display. May be 0 to disable the feature.
83 nsecs_t virtualKeyQuietTime;
84
85 // The excluded device names for the platform.
86 // Devices with these names will be ignored.
87 Vector<String8> excludedDeviceNames;
88
Jeff Brown19c97d462011-06-01 12:33:19 -070089 // Velocity control parameters for mouse pointer movements.
90 VelocityControlParameters pointerVelocityControlParameters;
91
92 // Velocity control parameters for mouse wheel movements.
93 VelocityControlParameters wheelVelocityControlParameters;
94
Jeff Brown474dcb52011-06-14 20:22:50 -070095 // True if pointer gestures are enabled.
96 bool pointerGesturesEnabled;
97
Jeff Brown214eaf42011-05-26 19:17:02 -070098 // Quiet time between certain pointer gesture transitions.
99 // Time to allow for all fingers or buttons to settle into a stable state before
100 // starting a new gesture.
101 nsecs_t pointerGestureQuietInterval;
102
103 // The minimum speed that a pointer must travel for us to consider switching the active
104 // touch pointer to it during a drag. This threshold is set to avoid switching due
105 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
106 float pointerGestureDragMinSwitchSpeed; // in pixels per second
107
108 // Tap gesture delay time.
109 // The time between down and up must be less than this to be considered a tap.
110 nsecs_t pointerGestureTapInterval;
111
112 // Tap drag gesture delay time.
113 // The time between the previous tap's up and the next down must be less than
114 // this to be considered a drag. Otherwise, the previous tap is finished and a
115 // new tap begins.
116 //
117 // Note that the previous tap will be held down for this entire duration so this
118 // interval must be shorter than the long press timeout.
119 nsecs_t pointerGestureTapDragInterval;
120
121 // The distance in pixels that the pointer is allowed to move from initial down
122 // to up and still be called a tap.
123 float pointerGestureTapSlop; // in pixels
124
125 // Time after the first touch points go down to settle on an initial centroid.
126 // This is intended to be enough time to handle cases where the user puts down two
127 // fingers at almost but not quite exactly the same time.
128 nsecs_t pointerGestureMultitouchSettleInterval;
129
130 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700131 // at least two pointers have moved at least this far from their starting place.
132 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700133
134 // The transition from PRESS to SWIPE gesture mode can only occur when the
135 // cosine of the angle between the two vectors is greater than or equal to than this value
136 // which indicates that the vectors are oriented in the same direction.
137 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
138 // (In exactly opposite directions, the cosine is -1.0.)
139 float pointerGestureSwipeTransitionAngleCosine;
140
141 // The transition from PRESS to SWIPE gesture mode can only occur when the
142 // fingers are no more than this far apart relative to the diagonal size of
143 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
144 // no more than half the diagonal size of the touch pad apart.
145 float pointerGestureSwipeMaxWidthRatio;
146
147 // The gesture movement speed factor relative to the size of the display.
148 // Movement speed applies when the fingers are moving in the same direction.
149 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
150 // will cover this portion of the display diagonal.
151 float pointerGestureMovementSpeedRatio;
152
153 // The gesture zoom speed factor relative to the size of the display.
154 // Zoom speed applies when the fingers are mostly moving relative to each other
155 // to execute a scale gesture or similar.
156 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
157 // will cover this portion of the display diagonal.
158 float pointerGestureZoomSpeedRatio;
159
Jeff Browndaf4a122011-08-26 17:14:14 -0700160 // True to show the location of touches on the touch screen as spots.
161 bool showTouches;
162
Jeff Brown214eaf42011-05-26 19:17:02 -0700163 InputReaderConfiguration() :
Jeff Brown214eaf42011-05-26 19:17:02 -0700164 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700165 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700166 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700167 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700168 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
169 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
170 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
171 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
172 pointerGestureTapSlop(10.0f), // 10 pixels
173 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700174 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700175 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700176 pointerGestureSwipeMaxWidthRatio(0.25f),
177 pointerGestureMovementSpeedRatio(0.8f),
Jeff Browndaf4a122011-08-26 17:14:14 -0700178 pointerGestureZoomSpeedRatio(0.3f),
179 showTouches(false) { }
Jeff Brown65fd2512011-08-18 11:20:58 -0700180
181 bool getDisplayInfo(int32_t displayId, bool external,
182 int32_t* width, int32_t* height, int32_t* orientation) const;
183
184 void setDisplayInfo(int32_t displayId, bool external,
185 int32_t width, int32_t height, int32_t orientation);
186
187private:
188 struct DisplayInfo {
189 int32_t width;
190 int32_t height;
191 int32_t orientation;
192
193 DisplayInfo() :
194 width(-1), height(-1), orientation(DISPLAY_ORIENTATION_0) {
195 }
196 };
197
198 DisplayInfo mInternalDisplay;
199 DisplayInfo mExternalDisplay;
Jeff Brown214eaf42011-05-26 19:17:02 -0700200};
201
202
203/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204 * Input reader policy interface.
205 *
206 * The input reader policy is used by the input reader to interact with the Window Manager
207 * and other system components.
208 *
209 * The actual implementation is partially supported by callbacks into the DVM
210 * via JNI. This interface is also mocked in the unit tests.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700211 *
212 * These methods must NOT re-enter the input reader since they may be called while
213 * holding the input reader lock.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700214 */
215class InputReaderPolicyInterface : public virtual RefBase {
216protected:
217 InputReaderPolicyInterface() { }
218 virtual ~InputReaderPolicyInterface() { }
219
220public:
Jeff Brown214eaf42011-05-26 19:17:02 -0700221 /* Gets the input reader configuration. */
222 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800223
224 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
225 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700226
227 /* Notifies the input reader policy that some input devices have changed
228 * and provides information about all current input devices.
229 */
230 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700231
232 /* Gets the keyboard layout for a particular input device. */
233 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor) = 0;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700234
235 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
236 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237};
238
239
Jeff Brownbe1aa822011-07-27 16:04:54 -0700240/* Processes raw input events and sends cooked event data to an input listener. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700241class InputReaderInterface : public virtual RefBase {
242protected:
243 InputReaderInterface() { }
244 virtual ~InputReaderInterface() { }
245
246public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700247 /* Dumps the state of the input reader.
248 *
249 * This method may be called on any thread (usually by the input manager). */
250 virtual void dump(String8& dump) = 0;
251
Jeff Brown89ef0722011-08-10 16:25:21 -0700252 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
253 virtual void monitor() = 0;
254
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700255 /* Runs a single iteration of the processing loop.
256 * Nominally reads and processes one incoming message from the EventHub.
257 *
258 * This method should be called on the input reader thread.
259 */
260 virtual void loopOnce() = 0;
261
Jeff Brown9c3cda02010-06-15 01:31:58 -0700262 /* Gets the current input device configuration.
263 *
264 * This method may be called on any thread (usually by the input manager).
265 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700266 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700267
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700268 /* Gets information about all input devices.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700269 *
270 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271 */
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700272 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700273
274 /* Query current input state. */
275 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
276 int32_t scanCode) = 0;
277 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
278 int32_t keyCode) = 0;
279 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
280 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700281
282 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700283 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
284 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700285
Jeff Brown474dcb52011-06-14 20:22:50 -0700286 /* Requests that a reconfiguration of all input devices.
287 * The changes flag is a bitfield that indicates what has changed and whether
288 * the input devices must all be reopened. */
289 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Browna47425a2012-04-13 04:09:27 -0700290
291 /* Controls the vibrator of a particular input device. */
292 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
293 ssize_t repeat, int32_t token) = 0;
294 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295};
296
297
298/* Internal interface used by individual input devices to access global input device state
299 * and parameters maintained by the input reader.
300 */
301class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700302public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700303 InputReaderContext() { }
304 virtual ~InputReaderContext() { }
305
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306 virtual void updateGlobalMetaState() = 0;
307 virtual int32_t getGlobalMetaState() = 0;
308
Jeff Brownfe508922011-01-18 15:10:10 -0800309 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
310 virtual bool shouldDropVirtualKey(nsecs_t now,
311 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
312
Jeff Brown05dc66a2011-03-02 14:41:58 -0800313 virtual void fadePointer() = 0;
314
Jeff Brownaa3855d2011-03-17 01:34:19 -0700315 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700316 virtual int32_t bumpGeneration() = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700317
Jeff Brown6d0fec22010-07-23 21:28:06 -0700318 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700319 virtual InputListenerInterface* getListener() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700320 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700321};
322
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700324/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brownbe1aa822011-07-27 16:04:54 -0700325 * that it sends to the input listener. Some functions of the input reader, such as early
Jeff Brown9c3cda02010-06-15 01:31:58 -0700326 * event filtering in low power states, are controlled by a separate policy object.
327 *
Jeff Brownbe1aa822011-07-27 16:04:54 -0700328 * The InputReader owns a collection of InputMappers. Most of the work it does happens
329 * on the input reader thread but the InputReader can receive queries from other system
330 * components running on arbitrary threads. To keep things manageable, the InputReader
331 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
332 * EventHub or the InputReaderPolicy but it is never held while calling into the
333 * InputListener.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700334 */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700335class InputReader : public InputReaderInterface {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700336public:
337 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700338 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700339 const sp<InputListenerInterface>& listener);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700340 virtual ~InputReader();
341
Jeff Brownb88102f2010-09-08 11:49:43 -0700342 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700343 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700344
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700345 virtual void loopOnce();
346
Jeff Brown6d0fec22010-07-23 21:28:06 -0700347 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700348 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700349
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
351 int32_t scanCode);
352 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
353 int32_t keyCode);
354 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
355 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700356
Jeff Brown6d0fec22010-07-23 21:28:06 -0700357 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
358 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700359
Jeff Brown474dcb52011-06-14 20:22:50 -0700360 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700361
Jeff Browna47425a2012-04-13 04:09:27 -0700362 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
363 ssize_t repeat, int32_t token);
364 virtual void cancelVibrate(int32_t deviceId, int32_t token);
365
Jeff Brownc3db8582010-10-20 15:33:38 -0700366protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700367 // These members are protected so they can be instrumented by test cases.
368 virtual InputDevice* createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700369 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370
371 class ContextImpl : public InputReaderContext {
372 InputReader* mReader;
373
374 public:
375 ContextImpl(InputReader* reader);
376
377 virtual void updateGlobalMetaState();
378 virtual int32_t getGlobalMetaState();
379 virtual void disableVirtualKeysUntil(nsecs_t time);
380 virtual bool shouldDropVirtualKey(nsecs_t now,
381 InputDevice* device, int32_t keyCode, int32_t scanCode);
382 virtual void fadePointer();
383 virtual void requestTimeoutAtTime(nsecs_t when);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700384 virtual int32_t bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700385 virtual InputReaderPolicyInterface* getPolicy();
386 virtual InputListenerInterface* getListener();
387 virtual EventHubInterface* getEventHub();
388 } mContext;
389
390 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700391
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700392private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700393 Mutex mLock;
394
Jeff Brown112b5f52012-01-27 17:32:06 -0800395 Condition mReaderIsAliveCondition;
396
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700397 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700398 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700399 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700400
Jeff Brown214eaf42011-05-26 19:17:02 -0700401 InputReaderConfiguration mConfig;
402
Jeff Brownb7198742011-03-18 18:14:26 -0700403 // The event queue.
404 static const int EVENT_BUFFER_SIZE = 256;
405 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
406
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700407 KeyedVector<int32_t, InputDevice*> mDevices;
408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700410 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700411
Jeff Brown65fd2512011-08-18 11:20:58 -0700412 void addDeviceLocked(nsecs_t when, int32_t deviceId);
413 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700414 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
415 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700416
Jeff Brownbe1aa822011-07-27 16:04:54 -0700417 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700418
Jeff Brownbe1aa822011-07-27 16:04:54 -0700419 int32_t mGlobalMetaState;
420 void updateGlobalMetaStateLocked();
421 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700422
Jeff Brownbe1aa822011-07-27 16:04:54 -0700423 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700424
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700425 int32_t mGeneration;
426 int32_t bumpGenerationLocked();
427
Jeff Brownbe1aa822011-07-27 16:04:54 -0700428 InputConfiguration mInputConfiguration;
429 void updateInputConfigurationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800430
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700431 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
432
Jeff Brownbe1aa822011-07-27 16:04:54 -0700433 nsecs_t mDisableVirtualKeysTimeout;
434 void disableVirtualKeysUntilLocked(nsecs_t time);
435 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800436 InputDevice* device, int32_t keyCode, int32_t scanCode);
437
Jeff Brownbe1aa822011-07-27 16:04:54 -0700438 nsecs_t mNextTimeout;
439 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700440
Jeff Brownbe1aa822011-07-27 16:04:54 -0700441 uint32_t mConfigurationChangesToRefresh;
442 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700443
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444 // state queries
445 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700446 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700448 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700450};
451
452
453/* Reads raw events from the event hub and processes them, endlessly. */
454class InputReaderThread : public Thread {
455public:
456 InputReaderThread(const sp<InputReaderInterface>& reader);
457 virtual ~InputReaderThread();
458
459private:
460 sp<InputReaderInterface> mReader;
461
462 virtual bool threadLoop();
463};
464
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465
466/* Represents the state of a single input device. */
467class InputDevice {
468public:
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700469 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700470 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471 ~InputDevice();
472
473 inline InputReaderContext* getContext() { return mContext; }
474 inline int32_t getId() { return mId; }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700475 inline int32_t getGeneration() { return mGeneration; }
Jeff Browne38fdfa2012-04-06 14:51:01 -0700476 inline const String8& getName() { return mIdentifier.name; }
Jeff Brown9ee285af2011-08-31 12:56:34 -0700477 inline uint32_t getClasses() { return mClasses; }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700478 inline uint32_t getSources() { return mSources; }
479
Jeff Brown56194eb2011-03-02 19:23:13 -0800480 inline bool isExternal() { return mIsExternal; }
481 inline void setExternal(bool external) { mIsExternal = external; }
482
Jeff Brown6d0fec22010-07-23 21:28:06 -0700483 inline bool isIgnored() { return mMappers.isEmpty(); }
484
Jeff Brownef3d7e82010-09-30 14:33:04 -0700485 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486 void addMapper(InputMapper* mapper);
Jeff Brown65fd2512011-08-18 11:20:58 -0700487 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
488 void reset(nsecs_t when);
Jeff Brownb7198742011-03-18 18:14:26 -0700489 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700490 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700491
492 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
493 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
494 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
495 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
496 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
497 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700498 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
499 void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700500
501 int32_t getMetaState();
502
Jeff Brown05dc66a2011-03-02 14:41:58 -0800503 void fadePointer();
504
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700505 void bumpGeneration();
506
Jeff Brown65fd2512011-08-18 11:20:58 -0700507 void notifyReset(nsecs_t when);
508
Jeff Brown49754db2011-07-01 17:37:58 -0700509 inline const PropertyMap& getConfiguration() { return mConfiguration; }
510 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700511
Jeff Brown65fd2512011-08-18 11:20:58 -0700512 bool hasKey(int32_t code) {
513 return getEventHub()->hasScanCode(mId, code);
514 }
515
Jeff Brown00710e92012-04-19 15:18:26 -0700516 bool hasAbsoluteAxis(int32_t code) {
517 RawAbsoluteAxisInfo info;
518 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
519 return info.valid;
520 }
521
Jeff Brown65fd2512011-08-18 11:20:58 -0700522 bool isKeyPressed(int32_t code) {
523 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
524 }
525
526 int32_t getAbsoluteAxisValue(int32_t code) {
527 int32_t value;
528 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
529 return value;
530 }
531
Jeff Brown6d0fec22010-07-23 21:28:06 -0700532private:
533 InputReaderContext* mContext;
534 int32_t mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700535 int32_t mGeneration;
Jeff Browne38fdfa2012-04-06 14:51:01 -0700536 InputDeviceIdentifier mIdentifier;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700537 String8 mAlias;
Jeff Brown9ee285af2011-08-31 12:56:34 -0700538 uint32_t mClasses;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700539
540 Vector<InputMapper*> mMappers;
541
Jeff Brown6d0fec22010-07-23 21:28:06 -0700542 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800543 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700544 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700545
546 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
547 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700548
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800549 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700550};
551
552
Jeff Brown49754db2011-07-01 17:37:58 -0700553/* Keeps track of the state of mouse or touch pad buttons. */
554class CursorButtonAccumulator {
555public:
556 CursorButtonAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700557 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700558
Jeff Brown49754db2011-07-01 17:37:58 -0700559 void process(const RawEvent* rawEvent);
560
561 uint32_t getButtonState() const;
562
563private:
564 bool mBtnLeft;
565 bool mBtnRight;
566 bool mBtnMiddle;
567 bool mBtnBack;
568 bool mBtnSide;
569 bool mBtnForward;
570 bool mBtnExtra;
571 bool mBtnTask;
Jeff Brown65fd2512011-08-18 11:20:58 -0700572
573 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700574};
575
576
577/* Keeps track of cursor movements. */
578
579class CursorMotionAccumulator {
580public:
581 CursorMotionAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700582 void reset(InputDevice* device);
583
584 void process(const RawEvent* rawEvent);
585 void finishSync();
586
587 inline int32_t getRelativeX() const { return mRelX; }
588 inline int32_t getRelativeY() const { return mRelY; }
589
590private:
591 int32_t mRelX;
592 int32_t mRelY;
Jeff Brown49754db2011-07-01 17:37:58 -0700593
594 void clearRelativeAxes();
Jeff Brown65fd2512011-08-18 11:20:58 -0700595};
596
597
598/* Keeps track of cursor scrolling motions. */
599
600class CursorScrollAccumulator {
601public:
602 CursorScrollAccumulator();
603 void configure(InputDevice* device);
604 void reset(InputDevice* device);
605
Jeff Brown49754db2011-07-01 17:37:58 -0700606 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700607 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700608
609 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
610 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
611
612 inline int32_t getRelativeX() const { return mRelX; }
613 inline int32_t getRelativeY() const { return mRelY; }
614 inline int32_t getRelativeVWheel() const { return mRelWheel; }
615 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
616
617private:
618 bool mHaveRelWheel;
619 bool mHaveRelHWheel;
620
621 int32_t mRelX;
622 int32_t mRelY;
623 int32_t mRelWheel;
624 int32_t mRelHWheel;
Jeff Brown65fd2512011-08-18 11:20:58 -0700625
626 void clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700627};
628
629
630/* Keeps track of the state of touch, stylus and tool buttons. */
631class TouchButtonAccumulator {
632public:
633 TouchButtonAccumulator();
634 void configure(InputDevice* device);
Jeff Brown65fd2512011-08-18 11:20:58 -0700635 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700636
Jeff Brown49754db2011-07-01 17:37:58 -0700637 void process(const RawEvent* rawEvent);
638
639 uint32_t getButtonState() const;
640 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700641 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700642 bool isHovering() const;
Jeff Brown00710e92012-04-19 15:18:26 -0700643 bool hasStylus() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700644
645private:
646 bool mHaveBtnTouch;
Jeff Brown00710e92012-04-19 15:18:26 -0700647 bool mHaveStylus;
Jeff Brown49754db2011-07-01 17:37:58 -0700648
649 bool mBtnTouch;
650 bool mBtnStylus;
651 bool mBtnStylus2;
652 bool mBtnToolFinger;
653 bool mBtnToolPen;
654 bool mBtnToolRubber;
Jeff Brown65fd2512011-08-18 11:20:58 -0700655 bool mBtnToolBrush;
656 bool mBtnToolPencil;
657 bool mBtnToolAirbrush;
658 bool mBtnToolMouse;
659 bool mBtnToolLens;
Jeff Brownea6892e2011-08-23 17:31:25 -0700660 bool mBtnToolDoubleTap;
661 bool mBtnToolTripleTap;
662 bool mBtnToolQuadTap;
Jeff Brown65fd2512011-08-18 11:20:58 -0700663
664 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700665};
666
667
Jeff Brownbe1aa822011-07-27 16:04:54 -0700668/* Raw axis information from the driver. */
669struct RawPointerAxes {
670 RawAbsoluteAxisInfo x;
671 RawAbsoluteAxisInfo y;
672 RawAbsoluteAxisInfo pressure;
673 RawAbsoluteAxisInfo touchMajor;
674 RawAbsoluteAxisInfo touchMinor;
675 RawAbsoluteAxisInfo toolMajor;
676 RawAbsoluteAxisInfo toolMinor;
677 RawAbsoluteAxisInfo orientation;
678 RawAbsoluteAxisInfo distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700679 RawAbsoluteAxisInfo tiltX;
680 RawAbsoluteAxisInfo tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700681 RawAbsoluteAxisInfo trackingId;
682 RawAbsoluteAxisInfo slot;
683
684 RawPointerAxes();
685 void clear();
686};
687
688
689/* Raw data for a collection of pointers including a pointer id mapping table. */
690struct RawPointerData {
691 struct Pointer {
692 uint32_t id;
693 int32_t x;
694 int32_t y;
695 int32_t pressure;
696 int32_t touchMajor;
697 int32_t touchMinor;
698 int32_t toolMajor;
699 int32_t toolMinor;
700 int32_t orientation;
701 int32_t distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700702 int32_t tiltX;
703 int32_t tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700704 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
705 bool isHovering;
706 };
707
708 uint32_t pointerCount;
709 Pointer pointers[MAX_POINTERS];
710 BitSet32 hoveringIdBits, touchingIdBits;
711 uint32_t idToIndex[MAX_POINTER_ID + 1];
712
713 RawPointerData();
714 void clear();
715 void copyFrom(const RawPointerData& other);
716 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
717
718 inline void markIdBit(uint32_t id, bool isHovering) {
719 if (isHovering) {
720 hoveringIdBits.markBit(id);
721 } else {
722 touchingIdBits.markBit(id);
723 }
724 }
725
726 inline void clearIdBits() {
727 hoveringIdBits.clear();
728 touchingIdBits.clear();
729 }
730
731 inline const Pointer& pointerForId(uint32_t id) const {
732 return pointers[idToIndex[id]];
733 }
734
735 inline bool isHovering(uint32_t pointerIndex) {
736 return pointers[pointerIndex].isHovering;
737 }
738};
739
740
741/* Cooked data for a collection of pointers including a pointer id mapping table. */
742struct CookedPointerData {
743 uint32_t pointerCount;
744 PointerProperties pointerProperties[MAX_POINTERS];
745 PointerCoords pointerCoords[MAX_POINTERS];
746 BitSet32 hoveringIdBits, touchingIdBits;
747 uint32_t idToIndex[MAX_POINTER_ID + 1];
748
749 CookedPointerData();
750 void clear();
751 void copyFrom(const CookedPointerData& other);
752
753 inline bool isHovering(uint32_t pointerIndex) {
754 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
755 }
756};
757
758
Jeff Brown49754db2011-07-01 17:37:58 -0700759/* Keeps track of the state of single-touch protocol. */
760class SingleTouchMotionAccumulator {
761public:
762 SingleTouchMotionAccumulator();
763
Jeff Brown49754db2011-07-01 17:37:58 -0700764 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700765 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700766
767 inline int32_t getAbsoluteX() const { return mAbsX; }
768 inline int32_t getAbsoluteY() const { return mAbsY; }
769 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
770 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
771 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
Jeff Brown65fd2512011-08-18 11:20:58 -0700772 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
773 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
Jeff Brown49754db2011-07-01 17:37:58 -0700774
775private:
776 int32_t mAbsX;
777 int32_t mAbsY;
778 int32_t mAbsPressure;
779 int32_t mAbsToolWidth;
780 int32_t mAbsDistance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700781 int32_t mAbsTiltX;
782 int32_t mAbsTiltY;
783
784 void clearAbsoluteAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700785};
786
787
788/* Keeps track of the state of multi-touch protocol. */
789class MultiTouchMotionAccumulator {
790public:
791 class Slot {
792 public:
793 inline bool isInUse() const { return mInUse; }
794 inline int32_t getX() const { return mAbsMTPositionX; }
795 inline int32_t getY() const { return mAbsMTPositionY; }
796 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
797 inline int32_t getTouchMinor() const {
798 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
799 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
800 inline int32_t getToolMinor() const {
801 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
802 inline int32_t getOrientation() const { return mAbsMTOrientation; }
803 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
804 inline int32_t getPressure() const { return mAbsMTPressure; }
805 inline int32_t getDistance() const { return mAbsMTDistance; }
806 inline int32_t getToolType() const;
807
808 private:
809 friend class MultiTouchMotionAccumulator;
810
811 bool mInUse;
812 bool mHaveAbsMTTouchMinor;
813 bool mHaveAbsMTWidthMinor;
814 bool mHaveAbsMTToolType;
815
816 int32_t mAbsMTPositionX;
817 int32_t mAbsMTPositionY;
818 int32_t mAbsMTTouchMajor;
819 int32_t mAbsMTTouchMinor;
820 int32_t mAbsMTWidthMajor;
821 int32_t mAbsMTWidthMinor;
822 int32_t mAbsMTOrientation;
823 int32_t mAbsMTTrackingId;
824 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700825 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700826 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700827
828 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700829 void clear();
830 };
831
832 MultiTouchMotionAccumulator();
833 ~MultiTouchMotionAccumulator();
834
Jeff Brown00710e92012-04-19 15:18:26 -0700835 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
Jeff Brown65fd2512011-08-18 11:20:58 -0700836 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700837 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700838 void finishSync();
Jeff Brown00710e92012-04-19 15:18:26 -0700839 bool hasStylus() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700840
Jeff Brown49754db2011-07-01 17:37:58 -0700841 inline size_t getSlotCount() const { return mSlotCount; }
842 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
843
844private:
845 int32_t mCurrentSlot;
846 Slot* mSlots;
847 size_t mSlotCount;
848 bool mUsingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -0700849 bool mHaveStylus;
Jeff Brown65fd2512011-08-18 11:20:58 -0700850
851 void clearSlots(int32_t initialSlot);
Jeff Brown49754db2011-07-01 17:37:58 -0700852};
853
854
Jeff Brown6d0fec22010-07-23 21:28:06 -0700855/* An input mapper transforms raw input events into cooked event data.
856 * A single input device can have multiple associated input mappers in order to interpret
857 * different classes of events.
Jeff Brown65fd2512011-08-18 11:20:58 -0700858 *
859 * InputMapper lifecycle:
860 * - create
861 * - configure with 0 changes
862 * - reset
863 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
864 * - reset
865 * - destroy
Jeff Brown6d0fec22010-07-23 21:28:06 -0700866 */
867class InputMapper {
868public:
869 InputMapper(InputDevice* device);
870 virtual ~InputMapper();
871
872 inline InputDevice* getDevice() { return mDevice; }
873 inline int32_t getDeviceId() { return mDevice->getId(); }
874 inline const String8 getDeviceName() { return mDevice->getName(); }
875 inline InputReaderContext* getContext() { return mContext; }
876 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700877 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700878 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
879
880 virtual uint32_t getSources() = 0;
881 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700882 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700883 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
884 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700885 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700886 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700887
888 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
889 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
890 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
891 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
892 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700893 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
894 int32_t token);
895 virtual void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700896
897 virtual int32_t getMetaState();
898
Jeff Brown05dc66a2011-03-02 14:41:58 -0800899 virtual void fadePointer();
900
Jeff Brown6d0fec22010-07-23 21:28:06 -0700901protected:
902 InputDevice* mDevice;
903 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800904
Jeff Brownbe1aa822011-07-27 16:04:54 -0700905 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700906 void bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700907
Jeff Browncb1404e2011-01-15 18:14:15 -0800908 static void dumpRawAbsoluteAxisInfo(String8& dump,
909 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700910};
911
912
913class SwitchInputMapper : public InputMapper {
914public:
915 SwitchInputMapper(InputDevice* device);
916 virtual ~SwitchInputMapper();
917
918 virtual uint32_t getSources();
919 virtual void process(const RawEvent* rawEvent);
920
921 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
922
923private:
924 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
925};
926
927
Jeff Browna47425a2012-04-13 04:09:27 -0700928class VibratorInputMapper : public InputMapper {
929public:
930 VibratorInputMapper(InputDevice* device);
931 virtual ~VibratorInputMapper();
932
933 virtual uint32_t getSources();
934 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
935 virtual void process(const RawEvent* rawEvent);
936
937 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
938 int32_t token);
939 virtual void cancelVibrate(int32_t token);
940 virtual void timeoutExpired(nsecs_t when);
941 virtual void dump(String8& dump);
942
943private:
944 bool mVibrating;
945 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
946 size_t mPatternSize;
947 ssize_t mRepeat;
948 int32_t mToken;
949 ssize_t mIndex;
950 nsecs_t mNextStepTime;
951
952 void nextStep();
953 void stopVibrating();
954};
955
956
Jeff Brown6d0fec22010-07-23 21:28:06 -0700957class KeyboardInputMapper : public InputMapper {
958public:
Jeff Brownefd32662011-03-08 15:13:06 -0800959 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700960 virtual ~KeyboardInputMapper();
961
962 virtual uint32_t getSources();
963 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700964 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700965 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
966 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700967 virtual void process(const RawEvent* rawEvent);
968
969 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
970 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
971 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
972 const int32_t* keyCodes, uint8_t* outFlags);
973
974 virtual int32_t getMetaState();
975
976private:
977 struct KeyDown {
978 int32_t keyCode;
979 int32_t scanCode;
980 };
981
Jeff Brownefd32662011-03-08 15:13:06 -0800982 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700983 int32_t mKeyboardType;
984
Jeff Brown65fd2512011-08-18 11:20:58 -0700985 int32_t mOrientation; // orientation for dpad keys
986
Jeff Brownbe1aa822011-07-27 16:04:54 -0700987 Vector<KeyDown> mKeyDowns; // keys that are down
988 int32_t mMetaState;
989 nsecs_t mDownTime; // time of most recent key down
990
Jeff Brown49ccac52012-04-11 18:27:33 -0700991 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
992
Jeff Brownbe1aa822011-07-27 16:04:54 -0700993 struct LedState {
994 bool avail; // led is available
995 bool on; // we think the led is currently on
996 };
997 LedState mCapsLockLedState;
998 LedState mNumLockLedState;
999 LedState mScrollLockLedState;
1000
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001001 // Immutable configuration parameters.
1002 struct Parameters {
1003 int32_t associatedDisplayId;
1004 bool orientationAware;
1005 } mParameters;
1006
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001007 void configureParameters();
1008 void dumpParameters(String8& dump);
1009
Jeff Brown6d0fec22010-07-23 21:28:06 -07001010 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001011
Jeff Brown6d0fec22010-07-23 21:28:06 -07001012 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
1013 uint32_t policyFlags);
1014
Jeff Brownbe1aa822011-07-27 16:04:54 -07001015 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001016
Jeff Brownbe1aa822011-07-27 16:04:54 -07001017 void resetLedState();
1018 void initializeLedState(LedState& ledState, int32_t led);
1019 void updateLedState(bool reset);
1020 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -07001021 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001022};
1023
1024
Jeff Brown83c09682010-12-23 17:50:18 -08001025class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001026public:
Jeff Brown83c09682010-12-23 17:50:18 -08001027 CursorInputMapper(InputDevice* device);
1028 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001029
1030 virtual uint32_t getSources();
1031 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001032 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001033 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1034 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001035 virtual void process(const RawEvent* rawEvent);
1036
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001037 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1038
Jeff Brown05dc66a2011-03-02 14:41:58 -08001039 virtual void fadePointer();
1040
Jeff Brown6d0fec22010-07-23 21:28:06 -07001041private:
1042 // Amount that trackball needs to move in order to generate a key event.
1043 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1044
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001045 // Immutable configuration parameters.
1046 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -08001047 enum Mode {
1048 MODE_POINTER,
1049 MODE_NAVIGATION,
1050 };
1051
1052 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001053 int32_t associatedDisplayId;
1054 bool orientationAware;
1055 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001056
Jeff Brown49754db2011-07-01 17:37:58 -07001057 CursorButtonAccumulator mCursorButtonAccumulator;
1058 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown65fd2512011-08-18 11:20:58 -07001059 CursorScrollAccumulator mCursorScrollAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001060
Jeff Brownefd32662011-03-08 15:13:06 -08001061 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001062 float mXScale;
1063 float mYScale;
1064 float mXPrecision;
1065 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001066
Jeff Brown6f2fba42011-02-19 01:08:02 -08001067 float mVWheelScale;
1068 float mHWheelScale;
1069
Jeff Brown19c97d462011-06-01 12:33:19 -07001070 // Velocity controls for mouse pointer and wheel movements.
1071 // The controls for X and Y wheel movements are separate to keep them decoupled.
1072 VelocityControl mPointerVelocityControl;
1073 VelocityControl mWheelXVelocityControl;
1074 VelocityControl mWheelYVelocityControl;
1075
Jeff Brown65fd2512011-08-18 11:20:58 -07001076 int32_t mOrientation;
1077
Jeff Brown83c09682010-12-23 17:50:18 -08001078 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001079
Jeff Brownbe1aa822011-07-27 16:04:54 -07001080 int32_t mButtonState;
1081 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001082
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001083 void configureParameters();
1084 void dumpParameters(String8& dump);
1085
Jeff Brown6d0fec22010-07-23 21:28:06 -07001086 void sync(nsecs_t when);
1087};
1088
1089
1090class TouchInputMapper : public InputMapper {
1091public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001092 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001093 virtual ~TouchInputMapper();
1094
1095 virtual uint32_t getSources();
1096 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001097 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001098 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1099 virtual void reset(nsecs_t when);
1100 virtual void process(const RawEvent* rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001101
1102 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1103 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1104 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1105 const int32_t* keyCodes, uint8_t* outFlags);
1106
Jeff Brownace13b12011-03-09 17:39:48 -08001107 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -07001108 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -08001109
Jeff Brown6d0fec22010-07-23 21:28:06 -07001110protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001111 CursorButtonAccumulator mCursorButtonAccumulator;
1112 CursorScrollAccumulator mCursorScrollAccumulator;
1113 TouchButtonAccumulator mTouchButtonAccumulator;
1114
Jeff Brown6d0fec22010-07-23 21:28:06 -07001115 struct VirtualKey {
1116 int32_t keyCode;
1117 int32_t scanCode;
1118 uint32_t flags;
1119
1120 // computed hit box, specified in touch screen coords based on known display size
1121 int32_t hitLeft;
1122 int32_t hitTop;
1123 int32_t hitRight;
1124 int32_t hitBottom;
1125
1126 inline bool isHit(int32_t x, int32_t y) const {
1127 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1128 }
1129 };
1130
Jeff Brown65fd2512011-08-18 11:20:58 -07001131 // Input sources and device mode.
1132 uint32_t mSource;
1133
1134 enum DeviceMode {
1135 DEVICE_MODE_DISABLED, // input is disabled
1136 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1137 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1138 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1139 };
1140 DeviceMode mDeviceMode;
Jeff Brown83c09682010-12-23 17:50:18 -08001141
Jeff Brown214eaf42011-05-26 19:17:02 -07001142 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001143 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -07001144
Jeff Brown6d0fec22010-07-23 21:28:06 -07001145 // Immutable configuration parameters.
1146 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001147 enum DeviceType {
1148 DEVICE_TYPE_TOUCH_SCREEN,
1149 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -08001150 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001151 };
1152
1153 DeviceType deviceType;
1154 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -07001155 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001156 bool orientationAware;
1157
Jeff Brown2352b972011-04-12 22:39:53 -07001158 enum GestureMode {
1159 GESTURE_MODE_POINTER,
1160 GESTURE_MODE_SPOTS,
1161 };
1162 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001163 } mParameters;
1164
Jeff Brown8d608662010-08-30 03:02:23 -07001165 // Immutable calibration parameters in parsed form.
1166 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -07001167 // Size
1168 enum SizeCalibration {
1169 SIZE_CALIBRATION_DEFAULT,
1170 SIZE_CALIBRATION_NONE,
1171 SIZE_CALIBRATION_GEOMETRIC,
1172 SIZE_CALIBRATION_DIAMETER,
1173 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -07001174 };
1175
Jeff Browna1f89ce2011-08-11 00:05:01 -07001176 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001177
Jeff Browna1f89ce2011-08-11 00:05:01 -07001178 bool haveSizeScale;
1179 float sizeScale;
1180 bool haveSizeBias;
1181 float sizeBias;
1182 bool haveSizeIsSummed;
1183 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001184
1185 // Pressure
1186 enum PressureCalibration {
1187 PRESSURE_CALIBRATION_DEFAULT,
1188 PRESSURE_CALIBRATION_NONE,
1189 PRESSURE_CALIBRATION_PHYSICAL,
1190 PRESSURE_CALIBRATION_AMPLITUDE,
1191 };
Jeff Brown8d608662010-08-30 03:02:23 -07001192
1193 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001194 bool havePressureScale;
1195 float pressureScale;
1196
Jeff Brown8d608662010-08-30 03:02:23 -07001197 // Orientation
1198 enum OrientationCalibration {
1199 ORIENTATION_CALIBRATION_DEFAULT,
1200 ORIENTATION_CALIBRATION_NONE,
1201 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001202 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001203 };
1204
1205 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001206
1207 // Distance
1208 enum DistanceCalibration {
1209 DISTANCE_CALIBRATION_DEFAULT,
1210 DISTANCE_CALIBRATION_NONE,
1211 DISTANCE_CALIBRATION_SCALED,
1212 };
1213
1214 DistanceCalibration distanceCalibration;
1215 bool haveDistanceScale;
1216 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001217
1218 inline void applySizeScaleAndBias(float* outSize) const {
1219 if (haveSizeScale) {
1220 *outSize *= sizeScale;
1221 }
1222 if (haveSizeBias) {
1223 *outSize += sizeBias;
1224 }
1225 }
Jeff Brown8d608662010-08-30 03:02:23 -07001226 } mCalibration;
1227
Jeff Brownbe1aa822011-07-27 16:04:54 -07001228 // Raw pointer axis information from the driver.
1229 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001230
Jeff Brownbe1aa822011-07-27 16:04:54 -07001231 // Raw pointer sample data.
1232 RawPointerData mCurrentRawPointerData;
1233 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001234
Jeff Brownbe1aa822011-07-27 16:04:54 -07001235 // Cooked pointer sample data.
1236 CookedPointerData mCurrentCookedPointerData;
1237 CookedPointerData mLastCookedPointerData;
1238
1239 // Button state.
1240 int32_t mCurrentButtonState;
1241 int32_t mLastButtonState;
1242
Jeff Brown65fd2512011-08-18 11:20:58 -07001243 // Scroll state.
1244 int32_t mCurrentRawVScroll;
1245 int32_t mCurrentRawHScroll;
1246
1247 // Id bits used to differentiate fingers, stylus and mouse tools.
1248 BitSet32 mCurrentFingerIdBits; // finger or unknown
1249 BitSet32 mLastFingerIdBits;
1250 BitSet32 mCurrentStylusIdBits; // stylus or eraser
1251 BitSet32 mLastStylusIdBits;
1252 BitSet32 mCurrentMouseIdBits; // mouse or lens
1253 BitSet32 mLastMouseIdBits;
1254
Jeff Brownbe1aa822011-07-27 16:04:54 -07001255 // True if we sent a HOVER_ENTER event.
1256 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001257
1258 // The time the primary pointer last went down.
1259 nsecs_t mDownTime;
1260
Jeff Brownace13b12011-03-09 17:39:48 -08001261 // The pointer controller, or null if the device is not a pointer.
1262 sp<PointerControllerInterface> mPointerController;
1263
Jeff Brownbe1aa822011-07-27 16:04:54 -07001264 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001265
Jeff Brown8d608662010-08-30 03:02:23 -07001266 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001267 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001268 virtual void configureRawPointerAxes();
1269 virtual void dumpRawPointerAxes(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001270 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001271 virtual void dumpSurface(String8& dump);
1272 virtual void configureVirtualKeys();
1273 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001274 virtual void parseCalibration();
1275 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001276 virtual void dumpCalibration(String8& dump);
Jeff Brown00710e92012-04-19 15:18:26 -07001277 virtual bool hasStylus() const = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001278
Jeff Brown65fd2512011-08-18 11:20:58 -07001279 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001280
1281private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001282 // The surface orientation and width and height set by configureSurface().
1283 int32_t mSurfaceOrientation;
1284 int32_t mSurfaceWidth;
1285 int32_t mSurfaceHeight;
1286
1287 // The associated display orientation and width and height set by configureSurface().
1288 int32_t mAssociatedDisplayOrientation;
1289 int32_t mAssociatedDisplayWidth;
1290 int32_t mAssociatedDisplayHeight;
1291
1292 // Translation and scaling factors, orientation-independent.
1293 float mXScale;
1294 float mXPrecision;
1295
1296 float mYScale;
1297 float mYPrecision;
1298
1299 float mGeometricScale;
1300
Jeff Brownbe1aa822011-07-27 16:04:54 -07001301 float mPressureScale;
1302
1303 float mSizeScale;
1304
Jeff Brown65fd2512011-08-18 11:20:58 -07001305 float mOrientationCenter;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001306 float mOrientationScale;
1307
1308 float mDistanceScale;
1309
Jeff Brown65fd2512011-08-18 11:20:58 -07001310 bool mHaveTilt;
1311 float mTiltXCenter;
1312 float mTiltXScale;
1313 float mTiltYCenter;
1314 float mTiltYScale;
1315
Jeff Brownbe1aa822011-07-27 16:04:54 -07001316 // Oriented motion ranges for input device info.
1317 struct OrientedRanges {
1318 InputDeviceInfo::MotionRange x;
1319 InputDeviceInfo::MotionRange y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001320 InputDeviceInfo::MotionRange pressure;
1321
1322 bool haveSize;
1323 InputDeviceInfo::MotionRange size;
1324
1325 bool haveTouchSize;
1326 InputDeviceInfo::MotionRange touchMajor;
1327 InputDeviceInfo::MotionRange touchMinor;
1328
1329 bool haveToolSize;
1330 InputDeviceInfo::MotionRange toolMajor;
1331 InputDeviceInfo::MotionRange toolMinor;
1332
1333 bool haveOrientation;
1334 InputDeviceInfo::MotionRange orientation;
1335
1336 bool haveDistance;
1337 InputDeviceInfo::MotionRange distance;
Jeff Brown65fd2512011-08-18 11:20:58 -07001338
1339 bool haveTilt;
1340 InputDeviceInfo::MotionRange tilt;
1341
1342 OrientedRanges() {
1343 clear();
1344 }
1345
1346 void clear() {
1347 haveSize = false;
1348 haveTouchSize = false;
1349 haveToolSize = false;
1350 haveOrientation = false;
1351 haveDistance = false;
1352 haveTilt = false;
1353 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001354 } mOrientedRanges;
1355
1356 // Oriented dimensions and precision.
1357 float mOrientedSurfaceWidth;
1358 float mOrientedSurfaceHeight;
1359 float mOrientedXPrecision;
1360 float mOrientedYPrecision;
1361
1362 struct CurrentVirtualKeyState {
1363 bool down;
1364 bool ignored;
1365 nsecs_t downTime;
1366 int32_t keyCode;
1367 int32_t scanCode;
1368 } mCurrentVirtualKey;
1369
Jeff Brown65fd2512011-08-18 11:20:58 -07001370 // Scale factor for gesture or mouse based pointer movements.
1371 float mPointerXMovementScale;
1372 float mPointerYMovementScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001373
1374 // Scale factor for gesture based zooming and other freeform motions.
Jeff Brown65fd2512011-08-18 11:20:58 -07001375 float mPointerXZoomScale;
1376 float mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001377
1378 // The maximum swipe width.
1379 float mPointerGestureMaxSwipeWidth;
1380
Jeff Brown6d0fec22010-07-23 21:28:06 -07001381 struct PointerDistanceHeapElement {
1382 uint32_t currentPointerIndex : 8;
1383 uint32_t lastPointerIndex : 8;
1384 uint64_t distance : 48; // squared distance
1385 };
1386
Jeff Brown65fd2512011-08-18 11:20:58 -07001387 enum PointerUsage {
1388 POINTER_USAGE_NONE,
1389 POINTER_USAGE_GESTURES,
1390 POINTER_USAGE_STYLUS,
1391 POINTER_USAGE_MOUSE,
1392 };
1393 PointerUsage mPointerUsage;
1394
Jeff Brownace13b12011-03-09 17:39:48 -08001395 struct PointerGesture {
1396 enum Mode {
1397 // No fingers, button is not pressed.
1398 // Nothing happening.
1399 NEUTRAL,
1400
1401 // No fingers, button is not pressed.
1402 // Tap detected.
1403 // Emits DOWN and UP events at the pointer location.
1404 TAP,
1405
Jeff Brown79ac9692011-04-19 21:20:10 -07001406 // Exactly one finger dragging following a tap.
1407 // Pointer follows the active finger.
1408 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001409 //
1410 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001411 TAP_DRAG,
1412
Jeff Brownace13b12011-03-09 17:39:48 -08001413 // Button is pressed.
1414 // Pointer follows the active finger if there is one. Other fingers are ignored.
1415 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001416 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001417
1418 // Exactly one finger, button is not pressed.
1419 // Pointer follows the active finger.
1420 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001421 //
1422 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001423 HOVER,
1424
Jeff Brown2352b972011-04-12 22:39:53 -07001425 // Exactly two fingers but neither have moved enough to clearly indicate
1426 // whether a swipe or freeform gesture was intended. We consider the
1427 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1428 // Pointer does not move.
1429 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1430 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001431
1432 // Exactly two fingers moving in the same direction, button is not pressed.
1433 // Pointer does not move.
1434 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1435 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001436 SWIPE,
1437
1438 // Two or more fingers moving in arbitrary directions, button is not pressed.
1439 // Pointer does not move.
1440 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1441 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001442 FREEFORM,
1443
1444 // Waiting for quiet time to end before starting the next gesture.
1445 QUIET,
1446 };
1447
Jeff Brown2352b972011-04-12 22:39:53 -07001448 // Time the first finger went down.
1449 nsecs_t firstTouchTime;
1450
Jeff Brownace13b12011-03-09 17:39:48 -08001451 // The active pointer id from the raw touch data.
1452 int32_t activeTouchId; // -1 if none
1453
1454 // The active pointer id from the gesture last delivered to the application.
1455 int32_t activeGestureId; // -1 if none
1456
1457 // Pointer coords and ids for the current and previous pointer gesture.
1458 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001459 BitSet32 currentGestureIdBits;
1460 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001461 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001462 PointerCoords currentGestureCoords[MAX_POINTERS];
1463
1464 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001465 BitSet32 lastGestureIdBits;
1466 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001467 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001468 PointerCoords lastGestureCoords[MAX_POINTERS];
1469
Jeff Brownace13b12011-03-09 17:39:48 -08001470 // Time the pointer gesture last went down.
1471 nsecs_t downTime;
1472
Jeff Brown79ac9692011-04-19 21:20:10 -07001473 // Time when the pointer went down for a TAP.
1474 nsecs_t tapDownTime;
1475
1476 // Time when the pointer went up for a TAP.
1477 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001478
Jeff Brown2352b972011-04-12 22:39:53 -07001479 // Location of initial tap.
1480 float tapX, tapY;
1481
Jeff Brownace13b12011-03-09 17:39:48 -08001482 // Time we started waiting for quiescence.
1483 nsecs_t quietTime;
1484
Jeff Brown2352b972011-04-12 22:39:53 -07001485 // Reference points for multitouch gestures.
1486 float referenceTouchX; // reference touch X/Y coordinates in surface units
1487 float referenceTouchY;
1488 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1489 float referenceGestureY;
1490
Jeff Brown538881e2011-05-25 18:23:38 -07001491 // Distance that each pointer has traveled which has not yet been
1492 // subsumed into the reference gesture position.
1493 BitSet32 referenceIdBits;
1494 struct Delta {
1495 float dx, dy;
1496 };
1497 Delta referenceDeltas[MAX_POINTER_ID + 1];
1498
Jeff Brown2352b972011-04-12 22:39:53 -07001499 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1500 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1501
Jeff Brownace13b12011-03-09 17:39:48 -08001502 // A velocity tracker for determining whether to switch active pointers during drags.
1503 VelocityTracker velocityTracker;
1504
1505 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001506 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001507 activeTouchId = -1;
1508 activeGestureId = -1;
1509 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001510 currentGestureIdBits.clear();
1511 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001512 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001513 downTime = 0;
1514 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001515 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001516 resetQuietTime();
1517 }
1518
Jeff Brown79ac9692011-04-19 21:20:10 -07001519 void resetTap() {
1520 tapDownTime = LLONG_MIN;
1521 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001522 }
1523
1524 void resetQuietTime() {
1525 quietTime = LLONG_MIN;
1526 }
1527 } mPointerGesture;
1528
Jeff Brown65fd2512011-08-18 11:20:58 -07001529 struct PointerSimple {
1530 PointerCoords currentCoords;
1531 PointerProperties currentProperties;
1532 PointerCoords lastCoords;
1533 PointerProperties lastProperties;
1534
1535 // True if the pointer is down.
1536 bool down;
1537
1538 // True if the pointer is hovering.
1539 bool hovering;
1540
1541 // Time the pointer last went down.
1542 nsecs_t downTime;
1543
1544 void reset() {
1545 currentCoords.clear();
1546 currentProperties.clear();
1547 lastCoords.clear();
1548 lastProperties.clear();
1549 down = false;
1550 hovering = false;
1551 downTime = 0;
1552 }
1553 } mPointerSimple;
1554
1555 // The pointer and scroll velocity controls.
1556 VelocityControl mPointerVelocityControl;
1557 VelocityControl mWheelXVelocityControl;
1558 VelocityControl mWheelYVelocityControl;
1559
1560 void sync(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001561
Jeff Brownbe1aa822011-07-27 16:04:54 -07001562 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1563 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1564 int32_t keyEventAction, int32_t keyEventFlags);
1565
Jeff Brown6d0fec22010-07-23 21:28:06 -07001566 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001567 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1568 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1569 void cookPointerData();
1570
Jeff Brown65fd2512011-08-18 11:20:58 -07001571 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1572 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1573
Jeff Brown79ac9692011-04-19 21:20:10 -07001574 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
Jeff Brown65fd2512011-08-18 11:20:58 -07001575 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Jeff Brown79ac9692011-04-19 21:20:10 -07001576 bool preparePointerGestures(nsecs_t when,
Jeff Brown65fd2512011-08-18 11:20:58 -07001577 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1578 bool isTimeout);
1579
1580 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1581 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1582
1583 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1584 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1585
1586 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1587 bool down, bool hovering);
1588 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001589
1590 // Dispatches a motion event.
1591 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1592 // method will take care of setting the index and transmuting the action to DOWN or UP
1593 // it is the first / last pointer to go down / up.
1594 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001595 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1596 int32_t edgeFlags,
1597 const PointerProperties* properties, const PointerCoords* coords,
1598 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001599 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1600
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001601 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001602 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001603 bool updateMovedPointers(const PointerProperties* inProperties,
1604 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1605 PointerProperties* outProperties, PointerCoords* outCoords,
1606 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001607
Jeff Brownbe1aa822011-07-27 16:04:54 -07001608 bool isPointInsideSurface(int32_t x, int32_t y);
1609 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001610
Jeff Brownbe1aa822011-07-27 16:04:54 -07001611 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001612};
1613
1614
1615class SingleTouchInputMapper : public TouchInputMapper {
1616public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001617 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001618 virtual ~SingleTouchInputMapper();
1619
Jeff Brown65fd2512011-08-18 11:20:58 -07001620 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001621 virtual void process(const RawEvent* rawEvent);
1622
1623protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001624 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001625 virtual void configureRawPointerAxes();
Jeff Brown00710e92012-04-19 15:18:26 -07001626 virtual bool hasStylus() const;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001627
1628private:
Jeff Brown49754db2011-07-01 17:37:58 -07001629 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001630};
1631
1632
1633class MultiTouchInputMapper : public TouchInputMapper {
1634public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001635 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001636 virtual ~MultiTouchInputMapper();
1637
Jeff Brown65fd2512011-08-18 11:20:58 -07001638 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001639 virtual void process(const RawEvent* rawEvent);
1640
1641protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001642 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001643 virtual void configureRawPointerAxes();
Jeff Brown00710e92012-04-19 15:18:26 -07001644 virtual bool hasStylus() const;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001645
1646private:
Jeff Brown49754db2011-07-01 17:37:58 -07001647 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001648
Jeff Brown6894a292011-07-01 17:59:27 -07001649 // Specifies the pointer id bits that are in use, and their associated tracking id.
1650 BitSet32 mPointerIdBits;
1651 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001652};
1653
Jeff Browncb1404e2011-01-15 18:14:15 -08001654
1655class JoystickInputMapper : public InputMapper {
1656public:
1657 JoystickInputMapper(InputDevice* device);
1658 virtual ~JoystickInputMapper();
1659
1660 virtual uint32_t getSources();
1661 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1662 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001663 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1664 virtual void reset(nsecs_t when);
Jeff Browncb1404e2011-01-15 18:14:15 -08001665 virtual void process(const RawEvent* rawEvent);
1666
1667private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001668 struct Axis {
1669 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001670 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001671
Jeff Brown6f2fba42011-02-19 01:08:02 -08001672 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001673
Jeff Brown6f2fba42011-02-19 01:08:02 -08001674 float scale; // scale factor from raw to normalized values
1675 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001676 float highScale; // scale factor from raw to normalized values of high split
1677 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001678
Jeff Brown6f2fba42011-02-19 01:08:02 -08001679 float min; // normalized inclusive minimum
1680 float max; // normalized inclusive maximum
1681 float flat; // normalized flat region size
1682 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001683
Jeff Brown6f2fba42011-02-19 01:08:02 -08001684 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001685 float currentValue; // current value
1686 float newValue; // most recent value
1687 float highCurrentValue; // current value of high split
1688 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001689
Jeff Brown85297452011-03-04 13:07:49 -08001690 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1691 bool explicitlyMapped, float scale, float offset,
1692 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001693 float min, float max, float flat, float fuzz) {
1694 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001695 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001696 this->explicitlyMapped = explicitlyMapped;
1697 this->scale = scale;
1698 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001699 this->highScale = highScale;
1700 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001701 this->min = min;
1702 this->max = max;
1703 this->flat = flat;
1704 this->fuzz = fuzz;
1705 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001706 resetValue();
1707 }
1708
1709 void resetValue() {
1710 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001711 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001712 this->highCurrentValue = 0;
1713 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001714 }
1715 };
1716
Jeff Brown6f2fba42011-02-19 01:08:02 -08001717 // Axes indexed by raw ABS_* axis index.
1718 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001719
Jeff Brown6f2fba42011-02-19 01:08:02 -08001720 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001721
Jeff Brown85297452011-03-04 13:07:49 -08001722 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001723 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001724 bool filterAxes(bool force);
1725
1726 static bool hasValueChangedSignificantly(float filter,
1727 float newValue, float currentValue, float min, float max);
1728 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1729 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001730
Jeff Brown6f2fba42011-02-19 01:08:02 -08001731 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001732};
1733
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001734} // namespace android
1735
1736#endif // _UI_INPUT_READER_H