blob: d29776d8950777a53aebafa5657b3e39e0b84582 [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 Brown46b9ac0a2010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown214eaf42011-05-26 19:17:02 -070043 * Input reader configuration.
44 *
45 * Specifies various options that modify the behavior of the input reader.
46 */
47struct InputReaderConfiguration {
Jeff Brown474dcb52011-06-14 20:22:50 -070048 // Describes changes that have occurred.
49 enum {
50 // The pointer speed changed.
51 CHANGE_POINTER_SPEED = 1 << 0,
52
53 // The pointer gesture control changed.
54 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
55
Jeff Brown65fd2512011-08-18 11:20:58 -070056 // The display size or orientation changed.
57 CHANGE_DISPLAY_INFO = 1 << 2,
58
Jeff Browndaf4a122011-08-26 17:14:14 -070059 // The visible touches option changed.
60 CHANGE_SHOW_TOUCHES = 1 << 3,
61
Jeff Brown474dcb52011-06-14 20:22:50 -070062 // All devices must be reopened.
63 CHANGE_MUST_REOPEN = 1 << 31,
64 };
65
Jeff Brown214eaf42011-05-26 19:17:02 -070066 // Gets the amount of time to disable virtual keys after the screen is touched
67 // in order to filter out accidental virtual key presses due to swiping gestures
68 // or taps near the edge of the display. May be 0 to disable the feature.
69 nsecs_t virtualKeyQuietTime;
70
71 // The excluded device names for the platform.
72 // Devices with these names will be ignored.
73 Vector<String8> excludedDeviceNames;
74
Jeff Brown19c97d462011-06-01 12:33:19 -070075 // Velocity control parameters for mouse pointer movements.
76 VelocityControlParameters pointerVelocityControlParameters;
77
78 // Velocity control parameters for mouse wheel movements.
79 VelocityControlParameters wheelVelocityControlParameters;
80
Jeff Brown474dcb52011-06-14 20:22:50 -070081 // True if pointer gestures are enabled.
82 bool pointerGesturesEnabled;
83
Jeff Brown214eaf42011-05-26 19:17:02 -070084 // Quiet time between certain pointer gesture transitions.
85 // Time to allow for all fingers or buttons to settle into a stable state before
86 // starting a new gesture.
87 nsecs_t pointerGestureQuietInterval;
88
89 // The minimum speed that a pointer must travel for us to consider switching the active
90 // touch pointer to it during a drag. This threshold is set to avoid switching due
91 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
92 float pointerGestureDragMinSwitchSpeed; // in pixels per second
93
94 // Tap gesture delay time.
95 // The time between down and up must be less than this to be considered a tap.
96 nsecs_t pointerGestureTapInterval;
97
98 // Tap drag gesture delay time.
99 // The time between the previous tap's up and the next down must be less than
100 // this to be considered a drag. Otherwise, the previous tap is finished and a
101 // new tap begins.
102 //
103 // Note that the previous tap will be held down for this entire duration so this
104 // interval must be shorter than the long press timeout.
105 nsecs_t pointerGestureTapDragInterval;
106
107 // The distance in pixels that the pointer is allowed to move from initial down
108 // to up and still be called a tap.
109 float pointerGestureTapSlop; // in pixels
110
111 // Time after the first touch points go down to settle on an initial centroid.
112 // This is intended to be enough time to handle cases where the user puts down two
113 // fingers at almost but not quite exactly the same time.
114 nsecs_t pointerGestureMultitouchSettleInterval;
115
116 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700117 // at least two pointers have moved at least this far from their starting place.
118 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700119
120 // The transition from PRESS to SWIPE gesture mode can only occur when the
121 // cosine of the angle between the two vectors is greater than or equal to than this value
122 // which indicates that the vectors are oriented in the same direction.
123 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
124 // (In exactly opposite directions, the cosine is -1.0.)
125 float pointerGestureSwipeTransitionAngleCosine;
126
127 // The transition from PRESS to SWIPE gesture mode can only occur when the
128 // fingers are no more than this far apart relative to the diagonal size of
129 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
130 // no more than half the diagonal size of the touch pad apart.
131 float pointerGestureSwipeMaxWidthRatio;
132
133 // The gesture movement speed factor relative to the size of the display.
134 // Movement speed applies when the fingers are moving in the same direction.
135 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
136 // will cover this portion of the display diagonal.
137 float pointerGestureMovementSpeedRatio;
138
139 // The gesture zoom speed factor relative to the size of the display.
140 // Zoom speed applies when the fingers are mostly moving relative to each other
141 // to execute a scale gesture or similar.
142 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
143 // will cover this portion of the display diagonal.
144 float pointerGestureZoomSpeedRatio;
145
Jeff Browndaf4a122011-08-26 17:14:14 -0700146 // True to show the location of touches on the touch screen as spots.
147 bool showTouches;
148
Jeff Brown214eaf42011-05-26 19:17:02 -0700149 InputReaderConfiguration() :
Jeff Brown214eaf42011-05-26 19:17:02 -0700150 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700151 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700152 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700153 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700154 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
155 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
156 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
157 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
158 pointerGestureTapSlop(10.0f), // 10 pixels
159 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700160 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700161 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700162 pointerGestureSwipeMaxWidthRatio(0.25f),
163 pointerGestureMovementSpeedRatio(0.8f),
Jeff Browndaf4a122011-08-26 17:14:14 -0700164 pointerGestureZoomSpeedRatio(0.3f),
165 showTouches(false) { }
Jeff Brown65fd2512011-08-18 11:20:58 -0700166
167 bool getDisplayInfo(int32_t displayId, bool external,
168 int32_t* width, int32_t* height, int32_t* orientation) const;
169
170 void setDisplayInfo(int32_t displayId, bool external,
171 int32_t width, int32_t height, int32_t orientation);
172
173private:
174 struct DisplayInfo {
175 int32_t width;
176 int32_t height;
177 int32_t orientation;
178
179 DisplayInfo() :
180 width(-1), height(-1), orientation(DISPLAY_ORIENTATION_0) {
181 }
182 };
183
184 DisplayInfo mInternalDisplay;
185 DisplayInfo mExternalDisplay;
Jeff Brown214eaf42011-05-26 19:17:02 -0700186};
187
188
189/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190 * Input reader policy interface.
191 *
192 * The input reader policy is used by the input reader to interact with the Window Manager
193 * and other system components.
194 *
195 * The actual implementation is partially supported by callbacks into the DVM
196 * via JNI. This interface is also mocked in the unit tests.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700197 *
198 * These methods must NOT re-enter the input reader since they may be called while
199 * holding the input reader lock.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200 */
201class InputReaderPolicyInterface : public virtual RefBase {
202protected:
203 InputReaderPolicyInterface() { }
204 virtual ~InputReaderPolicyInterface() { }
205
206public:
Jeff Brown214eaf42011-05-26 19:17:02 -0700207 /* Gets the input reader configuration. */
208 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800209
210 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
211 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700212
213 /* Notifies the input reader policy that some input devices have changed
214 * and provides information about all current input devices.
215 */
216 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700217};
218
219
Jeff Brownbe1aa822011-07-27 16:04:54 -0700220/* Processes raw input events and sends cooked event data to an input listener. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700221class InputReaderInterface : public virtual RefBase {
222protected:
223 InputReaderInterface() { }
224 virtual ~InputReaderInterface() { }
225
226public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700227 /* Dumps the state of the input reader.
228 *
229 * This method may be called on any thread (usually by the input manager). */
230 virtual void dump(String8& dump) = 0;
231
Jeff Brown89ef0722011-08-10 16:25:21 -0700232 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
233 virtual void monitor() = 0;
234
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235 /* Runs a single iteration of the processing loop.
236 * Nominally reads and processes one incoming message from the EventHub.
237 *
238 * This method should be called on the input reader thread.
239 */
240 virtual void loopOnce() = 0;
241
Jeff Brown9c3cda02010-06-15 01:31:58 -0700242 /* Gets the current input device configuration.
243 *
244 * This method may be called on any thread (usually by the input manager).
245 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700246 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700247
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700248 /* Gets information about all input devices.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249 *
250 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700251 */
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700252 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700253
254 /* Query current input state. */
255 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
256 int32_t scanCode) = 0;
257 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
258 int32_t keyCode) = 0;
259 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
260 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700261
262 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700263 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
264 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700265
Jeff Brown474dcb52011-06-14 20:22:50 -0700266 /* Requests that a reconfiguration of all input devices.
267 * The changes flag is a bitfield that indicates what has changed and whether
268 * the input devices must all be reopened. */
269 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700270};
271
272
273/* Internal interface used by individual input devices to access global input device state
274 * and parameters maintained by the input reader.
275 */
276class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700277public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700278 InputReaderContext() { }
279 virtual ~InputReaderContext() { }
280
Jeff Brown6d0fec22010-07-23 21:28:06 -0700281 virtual void updateGlobalMetaState() = 0;
282 virtual int32_t getGlobalMetaState() = 0;
283
Jeff Brownfe508922011-01-18 15:10:10 -0800284 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
285 virtual bool shouldDropVirtualKey(nsecs_t now,
286 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
287
Jeff Brown05dc66a2011-03-02 14:41:58 -0800288 virtual void fadePointer() = 0;
289
Jeff Brownaa3855d2011-03-17 01:34:19 -0700290 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700291 virtual int32_t bumpGeneration() = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700292
Jeff Brown6d0fec22010-07-23 21:28:06 -0700293 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700294 virtual InputListenerInterface* getListener() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296};
297
Jeff Brown9c3cda02010-06-15 01:31:58 -0700298
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700299/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brownbe1aa822011-07-27 16:04:54 -0700300 * that it sends to the input listener. Some functions of the input reader, such as early
Jeff Brown9c3cda02010-06-15 01:31:58 -0700301 * event filtering in low power states, are controlled by a separate policy object.
302 *
Jeff Brownbe1aa822011-07-27 16:04:54 -0700303 * The InputReader owns a collection of InputMappers. Most of the work it does happens
304 * on the input reader thread but the InputReader can receive queries from other system
305 * components running on arbitrary threads. To keep things manageable, the InputReader
306 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
307 * EventHub or the InputReaderPolicy but it is never held while calling into the
308 * InputListener.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700309 */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310class InputReader : public InputReaderInterface {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700311public:
312 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700313 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700314 const sp<InputListenerInterface>& listener);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700315 virtual ~InputReader();
316
Jeff Brownb88102f2010-09-08 11:49:43 -0700317 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700318 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700319
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700320 virtual void loopOnce();
321
Jeff Brown6d0fec22010-07-23 21:28:06 -0700322 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700323 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700324
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
326 int32_t scanCode);
327 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
328 int32_t keyCode);
329 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
330 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700331
Jeff Brown6d0fec22010-07-23 21:28:06 -0700332 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
333 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700334
Jeff Brown474dcb52011-06-14 20:22:50 -0700335 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700336
Jeff Brownc3db8582010-10-20 15:33:38 -0700337protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700338 // These members are protected so they can be instrumented by test cases.
339 virtual InputDevice* createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700340 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700341
342 class ContextImpl : public InputReaderContext {
343 InputReader* mReader;
344
345 public:
346 ContextImpl(InputReader* reader);
347
348 virtual void updateGlobalMetaState();
349 virtual int32_t getGlobalMetaState();
350 virtual void disableVirtualKeysUntil(nsecs_t time);
351 virtual bool shouldDropVirtualKey(nsecs_t now,
352 InputDevice* device, int32_t keyCode, int32_t scanCode);
353 virtual void fadePointer();
354 virtual void requestTimeoutAtTime(nsecs_t when);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700355 virtual int32_t bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700356 virtual InputReaderPolicyInterface* getPolicy();
357 virtual InputListenerInterface* getListener();
358 virtual EventHubInterface* getEventHub();
359 } mContext;
360
361 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700362
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700363private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700364 Mutex mLock;
365
Jeff Brown112b5f52012-01-27 17:32:06 -0800366 Condition mReaderIsAliveCondition;
367
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700368 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700371
Jeff Brown214eaf42011-05-26 19:17:02 -0700372 InputReaderConfiguration mConfig;
373
Jeff Brownb7198742011-03-18 18:14:26 -0700374 // The event queue.
375 static const int EVENT_BUFFER_SIZE = 256;
376 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
377
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700378 KeyedVector<int32_t, InputDevice*> mDevices;
379
Jeff Brown6d0fec22010-07-23 21:28:06 -0700380 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700381 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700382
Jeff Brown65fd2512011-08-18 11:20:58 -0700383 void addDeviceLocked(nsecs_t when, int32_t deviceId);
384 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700385 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
386 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700387
Jeff Brownbe1aa822011-07-27 16:04:54 -0700388 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700389
Jeff Brownbe1aa822011-07-27 16:04:54 -0700390 int32_t mGlobalMetaState;
391 void updateGlobalMetaStateLocked();
392 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700393
Jeff Brownbe1aa822011-07-27 16:04:54 -0700394 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700396 int32_t mGeneration;
397 int32_t bumpGenerationLocked();
398
Jeff Brownbe1aa822011-07-27 16:04:54 -0700399 InputConfiguration mInputConfiguration;
400 void updateInputConfigurationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800401
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700402 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
403
Jeff Brownbe1aa822011-07-27 16:04:54 -0700404 nsecs_t mDisableVirtualKeysTimeout;
405 void disableVirtualKeysUntilLocked(nsecs_t time);
406 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800407 InputDevice* device, int32_t keyCode, int32_t scanCode);
408
Jeff Brownbe1aa822011-07-27 16:04:54 -0700409 nsecs_t mNextTimeout;
410 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700411
Jeff Brownbe1aa822011-07-27 16:04:54 -0700412 uint32_t mConfigurationChangesToRefresh;
413 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700414
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415 // state queries
416 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700417 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700418 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700419 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700421};
422
423
424/* Reads raw events from the event hub and processes them, endlessly. */
425class InputReaderThread : public Thread {
426public:
427 InputReaderThread(const sp<InputReaderInterface>& reader);
428 virtual ~InputReaderThread();
429
430private:
431 sp<InputReaderInterface> mReader;
432
433 virtual bool threadLoop();
434};
435
Jeff Brown6d0fec22010-07-23 21:28:06 -0700436
437/* Represents the state of a single input device. */
438class InputDevice {
439public:
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700440 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700441 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 ~InputDevice();
443
444 inline InputReaderContext* getContext() { return mContext; }
445 inline int32_t getId() { return mId; }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700446 inline int32_t getGeneration() { return mGeneration; }
Jeff Browne38fdfa2012-04-06 14:51:01 -0700447 inline const String8& getName() { return mIdentifier.name; }
Jeff Brown9ee285af2011-08-31 12:56:34 -0700448 inline uint32_t getClasses() { return mClasses; }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449 inline uint32_t getSources() { return mSources; }
450
Jeff Brown56194eb2011-03-02 19:23:13 -0800451 inline bool isExternal() { return mIsExternal; }
452 inline void setExternal(bool external) { mIsExternal = external; }
453
Jeff Brown6d0fec22010-07-23 21:28:06 -0700454 inline bool isIgnored() { return mMappers.isEmpty(); }
455
Jeff Brownef3d7e82010-09-30 14:33:04 -0700456 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457 void addMapper(InputMapper* mapper);
Jeff Brown65fd2512011-08-18 11:20:58 -0700458 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
459 void reset(nsecs_t when);
Jeff Brownb7198742011-03-18 18:14:26 -0700460 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700461 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462
463 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
464 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
465 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
466 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
467 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
468 const int32_t* keyCodes, uint8_t* outFlags);
469
470 int32_t getMetaState();
471
Jeff Brown05dc66a2011-03-02 14:41:58 -0800472 void fadePointer();
473
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700474 void bumpGeneration();
475
Jeff Brown65fd2512011-08-18 11:20:58 -0700476 void notifyReset(nsecs_t when);
477
Jeff Brown49754db2011-07-01 17:37:58 -0700478 inline const PropertyMap& getConfiguration() { return mConfiguration; }
479 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700480
Jeff Brown65fd2512011-08-18 11:20:58 -0700481 bool hasKey(int32_t code) {
482 return getEventHub()->hasScanCode(mId, code);
483 }
484
485 bool isKeyPressed(int32_t code) {
486 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
487 }
488
489 int32_t getAbsoluteAxisValue(int32_t code) {
490 int32_t value;
491 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
492 return value;
493 }
494
Jeff Brown6d0fec22010-07-23 21:28:06 -0700495private:
496 InputReaderContext* mContext;
497 int32_t mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700498 int32_t mGeneration;
Jeff Browne38fdfa2012-04-06 14:51:01 -0700499 InputDeviceIdentifier mIdentifier;
Jeff Brown9ee285af2011-08-31 12:56:34 -0700500 uint32_t mClasses;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700501
502 Vector<InputMapper*> mMappers;
503
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800505 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700506 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507
508 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
509 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700510
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800511 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512};
513
514
Jeff Brown49754db2011-07-01 17:37:58 -0700515/* Keeps track of the state of mouse or touch pad buttons. */
516class CursorButtonAccumulator {
517public:
518 CursorButtonAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700519 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700520
Jeff Brown49754db2011-07-01 17:37:58 -0700521 void process(const RawEvent* rawEvent);
522
523 uint32_t getButtonState() const;
524
525private:
526 bool mBtnLeft;
527 bool mBtnRight;
528 bool mBtnMiddle;
529 bool mBtnBack;
530 bool mBtnSide;
531 bool mBtnForward;
532 bool mBtnExtra;
533 bool mBtnTask;
Jeff Brown65fd2512011-08-18 11:20:58 -0700534
535 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700536};
537
538
539/* Keeps track of cursor movements. */
540
541class CursorMotionAccumulator {
542public:
543 CursorMotionAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700544 void reset(InputDevice* device);
545
546 void process(const RawEvent* rawEvent);
547 void finishSync();
548
549 inline int32_t getRelativeX() const { return mRelX; }
550 inline int32_t getRelativeY() const { return mRelY; }
551
552private:
553 int32_t mRelX;
554 int32_t mRelY;
Jeff Brown49754db2011-07-01 17:37:58 -0700555
556 void clearRelativeAxes();
Jeff Brown65fd2512011-08-18 11:20:58 -0700557};
558
559
560/* Keeps track of cursor scrolling motions. */
561
562class CursorScrollAccumulator {
563public:
564 CursorScrollAccumulator();
565 void configure(InputDevice* device);
566 void reset(InputDevice* device);
567
Jeff Brown49754db2011-07-01 17:37:58 -0700568 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700569 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700570
571 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
572 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
573
574 inline int32_t getRelativeX() const { return mRelX; }
575 inline int32_t getRelativeY() const { return mRelY; }
576 inline int32_t getRelativeVWheel() const { return mRelWheel; }
577 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
578
579private:
580 bool mHaveRelWheel;
581 bool mHaveRelHWheel;
582
583 int32_t mRelX;
584 int32_t mRelY;
585 int32_t mRelWheel;
586 int32_t mRelHWheel;
Jeff Brown65fd2512011-08-18 11:20:58 -0700587
588 void clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700589};
590
591
592/* Keeps track of the state of touch, stylus and tool buttons. */
593class TouchButtonAccumulator {
594public:
595 TouchButtonAccumulator();
596 void configure(InputDevice* device);
Jeff Brown65fd2512011-08-18 11:20:58 -0700597 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700598
Jeff Brown49754db2011-07-01 17:37:58 -0700599 void process(const RawEvent* rawEvent);
600
601 uint32_t getButtonState() const;
602 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700603 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700604 bool isHovering() const;
605
606private:
607 bool mHaveBtnTouch;
608
609 bool mBtnTouch;
610 bool mBtnStylus;
611 bool mBtnStylus2;
612 bool mBtnToolFinger;
613 bool mBtnToolPen;
614 bool mBtnToolRubber;
Jeff Brown65fd2512011-08-18 11:20:58 -0700615 bool mBtnToolBrush;
616 bool mBtnToolPencil;
617 bool mBtnToolAirbrush;
618 bool mBtnToolMouse;
619 bool mBtnToolLens;
Jeff Brownea6892e2011-08-23 17:31:25 -0700620 bool mBtnToolDoubleTap;
621 bool mBtnToolTripleTap;
622 bool mBtnToolQuadTap;
Jeff Brown65fd2512011-08-18 11:20:58 -0700623
624 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700625};
626
627
Jeff Brownbe1aa822011-07-27 16:04:54 -0700628/* Raw axis information from the driver. */
629struct RawPointerAxes {
630 RawAbsoluteAxisInfo x;
631 RawAbsoluteAxisInfo y;
632 RawAbsoluteAxisInfo pressure;
633 RawAbsoluteAxisInfo touchMajor;
634 RawAbsoluteAxisInfo touchMinor;
635 RawAbsoluteAxisInfo toolMajor;
636 RawAbsoluteAxisInfo toolMinor;
637 RawAbsoluteAxisInfo orientation;
638 RawAbsoluteAxisInfo distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700639 RawAbsoluteAxisInfo tiltX;
640 RawAbsoluteAxisInfo tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700641 RawAbsoluteAxisInfo trackingId;
642 RawAbsoluteAxisInfo slot;
643
644 RawPointerAxes();
645 void clear();
646};
647
648
649/* Raw data for a collection of pointers including a pointer id mapping table. */
650struct RawPointerData {
651 struct Pointer {
652 uint32_t id;
653 int32_t x;
654 int32_t y;
655 int32_t pressure;
656 int32_t touchMajor;
657 int32_t touchMinor;
658 int32_t toolMajor;
659 int32_t toolMinor;
660 int32_t orientation;
661 int32_t distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700662 int32_t tiltX;
663 int32_t tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700664 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
665 bool isHovering;
666 };
667
668 uint32_t pointerCount;
669 Pointer pointers[MAX_POINTERS];
670 BitSet32 hoveringIdBits, touchingIdBits;
671 uint32_t idToIndex[MAX_POINTER_ID + 1];
672
673 RawPointerData();
674 void clear();
675 void copyFrom(const RawPointerData& other);
676 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
677
678 inline void markIdBit(uint32_t id, bool isHovering) {
679 if (isHovering) {
680 hoveringIdBits.markBit(id);
681 } else {
682 touchingIdBits.markBit(id);
683 }
684 }
685
686 inline void clearIdBits() {
687 hoveringIdBits.clear();
688 touchingIdBits.clear();
689 }
690
691 inline const Pointer& pointerForId(uint32_t id) const {
692 return pointers[idToIndex[id]];
693 }
694
695 inline bool isHovering(uint32_t pointerIndex) {
696 return pointers[pointerIndex].isHovering;
697 }
698};
699
700
701/* Cooked data for a collection of pointers including a pointer id mapping table. */
702struct CookedPointerData {
703 uint32_t pointerCount;
704 PointerProperties pointerProperties[MAX_POINTERS];
705 PointerCoords pointerCoords[MAX_POINTERS];
706 BitSet32 hoveringIdBits, touchingIdBits;
707 uint32_t idToIndex[MAX_POINTER_ID + 1];
708
709 CookedPointerData();
710 void clear();
711 void copyFrom(const CookedPointerData& other);
712
713 inline bool isHovering(uint32_t pointerIndex) {
714 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
715 }
716};
717
718
Jeff Brown49754db2011-07-01 17:37:58 -0700719/* Keeps track of the state of single-touch protocol. */
720class SingleTouchMotionAccumulator {
721public:
722 SingleTouchMotionAccumulator();
723
Jeff Brown49754db2011-07-01 17:37:58 -0700724 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700725 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700726
727 inline int32_t getAbsoluteX() const { return mAbsX; }
728 inline int32_t getAbsoluteY() const { return mAbsY; }
729 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
730 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
731 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
Jeff Brown65fd2512011-08-18 11:20:58 -0700732 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
733 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
Jeff Brown49754db2011-07-01 17:37:58 -0700734
735private:
736 int32_t mAbsX;
737 int32_t mAbsY;
738 int32_t mAbsPressure;
739 int32_t mAbsToolWidth;
740 int32_t mAbsDistance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700741 int32_t mAbsTiltX;
742 int32_t mAbsTiltY;
743
744 void clearAbsoluteAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700745};
746
747
748/* Keeps track of the state of multi-touch protocol. */
749class MultiTouchMotionAccumulator {
750public:
751 class Slot {
752 public:
753 inline bool isInUse() const { return mInUse; }
754 inline int32_t getX() const { return mAbsMTPositionX; }
755 inline int32_t getY() const { return mAbsMTPositionY; }
756 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
757 inline int32_t getTouchMinor() const {
758 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
759 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
760 inline int32_t getToolMinor() const {
761 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
762 inline int32_t getOrientation() const { return mAbsMTOrientation; }
763 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
764 inline int32_t getPressure() const { return mAbsMTPressure; }
765 inline int32_t getDistance() const { return mAbsMTDistance; }
766 inline int32_t getToolType() const;
767
768 private:
769 friend class MultiTouchMotionAccumulator;
770
771 bool mInUse;
772 bool mHaveAbsMTTouchMinor;
773 bool mHaveAbsMTWidthMinor;
774 bool mHaveAbsMTToolType;
775
776 int32_t mAbsMTPositionX;
777 int32_t mAbsMTPositionY;
778 int32_t mAbsMTTouchMajor;
779 int32_t mAbsMTTouchMinor;
780 int32_t mAbsMTWidthMajor;
781 int32_t mAbsMTWidthMinor;
782 int32_t mAbsMTOrientation;
783 int32_t mAbsMTTrackingId;
784 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700785 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700786 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700787
788 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700789 void clear();
790 };
791
792 MultiTouchMotionAccumulator();
793 ~MultiTouchMotionAccumulator();
794
795 void configure(size_t slotCount, bool usingSlotsProtocol);
Jeff Brown65fd2512011-08-18 11:20:58 -0700796 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700797 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700798 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700799
Jeff Brown49754db2011-07-01 17:37:58 -0700800 inline size_t getSlotCount() const { return mSlotCount; }
801 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
802
803private:
804 int32_t mCurrentSlot;
805 Slot* mSlots;
806 size_t mSlotCount;
807 bool mUsingSlotsProtocol;
Jeff Brown65fd2512011-08-18 11:20:58 -0700808
809 void clearSlots(int32_t initialSlot);
Jeff Brown49754db2011-07-01 17:37:58 -0700810};
811
812
Jeff Brown6d0fec22010-07-23 21:28:06 -0700813/* An input mapper transforms raw input events into cooked event data.
814 * A single input device can have multiple associated input mappers in order to interpret
815 * different classes of events.
Jeff Brown65fd2512011-08-18 11:20:58 -0700816 *
817 * InputMapper lifecycle:
818 * - create
819 * - configure with 0 changes
820 * - reset
821 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
822 * - reset
823 * - destroy
Jeff Brown6d0fec22010-07-23 21:28:06 -0700824 */
825class InputMapper {
826public:
827 InputMapper(InputDevice* device);
828 virtual ~InputMapper();
829
830 inline InputDevice* getDevice() { return mDevice; }
831 inline int32_t getDeviceId() { return mDevice->getId(); }
832 inline const String8 getDeviceName() { return mDevice->getName(); }
833 inline InputReaderContext* getContext() { return mContext; }
834 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700835 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700836 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
837
838 virtual uint32_t getSources() = 0;
839 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700840 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700841 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
842 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700843 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700844 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700845
846 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
847 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
848 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
849 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
850 const int32_t* keyCodes, uint8_t* outFlags);
851
852 virtual int32_t getMetaState();
853
Jeff Brown05dc66a2011-03-02 14:41:58 -0800854 virtual void fadePointer();
855
Jeff Brown6d0fec22010-07-23 21:28:06 -0700856protected:
857 InputDevice* mDevice;
858 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800859
Jeff Brownbe1aa822011-07-27 16:04:54 -0700860 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700861 void bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700862
Jeff Browncb1404e2011-01-15 18:14:15 -0800863 static void dumpRawAbsoluteAxisInfo(String8& dump,
864 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700865};
866
867
868class SwitchInputMapper : public InputMapper {
869public:
870 SwitchInputMapper(InputDevice* device);
871 virtual ~SwitchInputMapper();
872
873 virtual uint32_t getSources();
874 virtual void process(const RawEvent* rawEvent);
875
876 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
877
878private:
879 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
880};
881
882
883class KeyboardInputMapper : public InputMapper {
884public:
Jeff Brownefd32662011-03-08 15:13:06 -0800885 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886 virtual ~KeyboardInputMapper();
887
888 virtual uint32_t getSources();
889 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700890 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700891 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
892 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700893 virtual void process(const RawEvent* rawEvent);
894
895 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
896 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
897 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
898 const int32_t* keyCodes, uint8_t* outFlags);
899
900 virtual int32_t getMetaState();
901
902private:
903 struct KeyDown {
904 int32_t keyCode;
905 int32_t scanCode;
906 };
907
Jeff Brownefd32662011-03-08 15:13:06 -0800908 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700909 int32_t mKeyboardType;
910
Jeff Brown65fd2512011-08-18 11:20:58 -0700911 int32_t mOrientation; // orientation for dpad keys
912
Jeff Brownbe1aa822011-07-27 16:04:54 -0700913 Vector<KeyDown> mKeyDowns; // keys that are down
914 int32_t mMetaState;
915 nsecs_t mDownTime; // time of most recent key down
916
Jeff Brown49ccac52012-04-11 18:27:33 -0700917 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
918
Jeff Brownbe1aa822011-07-27 16:04:54 -0700919 struct LedState {
920 bool avail; // led is available
921 bool on; // we think the led is currently on
922 };
923 LedState mCapsLockLedState;
924 LedState mNumLockLedState;
925 LedState mScrollLockLedState;
926
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800927 // Immutable configuration parameters.
928 struct Parameters {
929 int32_t associatedDisplayId;
930 bool orientationAware;
931 } mParameters;
932
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800933 void configureParameters();
934 void dumpParameters(String8& dump);
935
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700937
Jeff Brown6d0fec22010-07-23 21:28:06 -0700938 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
939 uint32_t policyFlags);
940
Jeff Brownbe1aa822011-07-27 16:04:54 -0700941 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700942
Jeff Brownbe1aa822011-07-27 16:04:54 -0700943 void resetLedState();
944 void initializeLedState(LedState& ledState, int32_t led);
945 void updateLedState(bool reset);
946 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -0700947 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700948};
949
950
Jeff Brown83c09682010-12-23 17:50:18 -0800951class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700952public:
Jeff Brown83c09682010-12-23 17:50:18 -0800953 CursorInputMapper(InputDevice* device);
954 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700955
956 virtual uint32_t getSources();
957 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700958 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700959 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
960 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700961 virtual void process(const RawEvent* rawEvent);
962
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700963 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
964
Jeff Brown05dc66a2011-03-02 14:41:58 -0800965 virtual void fadePointer();
966
Jeff Brown6d0fec22010-07-23 21:28:06 -0700967private:
968 // Amount that trackball needs to move in order to generate a key event.
969 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
970
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800971 // Immutable configuration parameters.
972 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800973 enum Mode {
974 MODE_POINTER,
975 MODE_NAVIGATION,
976 };
977
978 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800979 int32_t associatedDisplayId;
980 bool orientationAware;
981 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700982
Jeff Brown49754db2011-07-01 17:37:58 -0700983 CursorButtonAccumulator mCursorButtonAccumulator;
984 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown65fd2512011-08-18 11:20:58 -0700985 CursorScrollAccumulator mCursorScrollAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700986
Jeff Brownefd32662011-03-08 15:13:06 -0800987 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700988 float mXScale;
989 float mYScale;
990 float mXPrecision;
991 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800992
Jeff Brown6f2fba42011-02-19 01:08:02 -0800993 float mVWheelScale;
994 float mHWheelScale;
995
Jeff Brown19c97d462011-06-01 12:33:19 -0700996 // Velocity controls for mouse pointer and wheel movements.
997 // The controls for X and Y wheel movements are separate to keep them decoupled.
998 VelocityControl mPointerVelocityControl;
999 VelocityControl mWheelXVelocityControl;
1000 VelocityControl mWheelYVelocityControl;
1001
Jeff Brown65fd2512011-08-18 11:20:58 -07001002 int32_t mOrientation;
1003
Jeff Brown83c09682010-12-23 17:50:18 -08001004 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005
Jeff Brownbe1aa822011-07-27 16:04:54 -07001006 int32_t mButtonState;
1007 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001008
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001009 void configureParameters();
1010 void dumpParameters(String8& dump);
1011
Jeff Brown6d0fec22010-07-23 21:28:06 -07001012 void sync(nsecs_t when);
1013};
1014
1015
1016class TouchInputMapper : public InputMapper {
1017public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001018 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019 virtual ~TouchInputMapper();
1020
1021 virtual uint32_t getSources();
1022 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001023 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001024 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1025 virtual void reset(nsecs_t when);
1026 virtual void process(const RawEvent* rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001027
1028 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1029 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1030 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1031 const int32_t* keyCodes, uint8_t* outFlags);
1032
Jeff Brownace13b12011-03-09 17:39:48 -08001033 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -07001034 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -08001035
Jeff Brown6d0fec22010-07-23 21:28:06 -07001036protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001037 CursorButtonAccumulator mCursorButtonAccumulator;
1038 CursorScrollAccumulator mCursorScrollAccumulator;
1039 TouchButtonAccumulator mTouchButtonAccumulator;
1040
Jeff Brown6d0fec22010-07-23 21:28:06 -07001041 struct VirtualKey {
1042 int32_t keyCode;
1043 int32_t scanCode;
1044 uint32_t flags;
1045
1046 // computed hit box, specified in touch screen coords based on known display size
1047 int32_t hitLeft;
1048 int32_t hitTop;
1049 int32_t hitRight;
1050 int32_t hitBottom;
1051
1052 inline bool isHit(int32_t x, int32_t y) const {
1053 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1054 }
1055 };
1056
Jeff Brown65fd2512011-08-18 11:20:58 -07001057 // Input sources and device mode.
1058 uint32_t mSource;
1059
1060 enum DeviceMode {
1061 DEVICE_MODE_DISABLED, // input is disabled
1062 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1063 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1064 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1065 };
1066 DeviceMode mDeviceMode;
Jeff Brown83c09682010-12-23 17:50:18 -08001067
Jeff Brown214eaf42011-05-26 19:17:02 -07001068 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001069 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -07001070
Jeff Brown6d0fec22010-07-23 21:28:06 -07001071 // Immutable configuration parameters.
1072 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001073 enum DeviceType {
1074 DEVICE_TYPE_TOUCH_SCREEN,
1075 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -08001076 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001077 };
1078
1079 DeviceType deviceType;
1080 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -07001081 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001082 bool orientationAware;
1083
Jeff Brown2352b972011-04-12 22:39:53 -07001084 enum GestureMode {
1085 GESTURE_MODE_POINTER,
1086 GESTURE_MODE_SPOTS,
1087 };
1088 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001089 } mParameters;
1090
Jeff Brown8d608662010-08-30 03:02:23 -07001091 // Immutable calibration parameters in parsed form.
1092 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -07001093 // Size
1094 enum SizeCalibration {
1095 SIZE_CALIBRATION_DEFAULT,
1096 SIZE_CALIBRATION_NONE,
1097 SIZE_CALIBRATION_GEOMETRIC,
1098 SIZE_CALIBRATION_DIAMETER,
1099 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -07001100 };
1101
Jeff Browna1f89ce2011-08-11 00:05:01 -07001102 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001103
Jeff Browna1f89ce2011-08-11 00:05:01 -07001104 bool haveSizeScale;
1105 float sizeScale;
1106 bool haveSizeBias;
1107 float sizeBias;
1108 bool haveSizeIsSummed;
1109 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001110
1111 // Pressure
1112 enum PressureCalibration {
1113 PRESSURE_CALIBRATION_DEFAULT,
1114 PRESSURE_CALIBRATION_NONE,
1115 PRESSURE_CALIBRATION_PHYSICAL,
1116 PRESSURE_CALIBRATION_AMPLITUDE,
1117 };
Jeff Brown8d608662010-08-30 03:02:23 -07001118
1119 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001120 bool havePressureScale;
1121 float pressureScale;
1122
Jeff Brown8d608662010-08-30 03:02:23 -07001123 // Orientation
1124 enum OrientationCalibration {
1125 ORIENTATION_CALIBRATION_DEFAULT,
1126 ORIENTATION_CALIBRATION_NONE,
1127 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001128 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001129 };
1130
1131 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001132
1133 // Distance
1134 enum DistanceCalibration {
1135 DISTANCE_CALIBRATION_DEFAULT,
1136 DISTANCE_CALIBRATION_NONE,
1137 DISTANCE_CALIBRATION_SCALED,
1138 };
1139
1140 DistanceCalibration distanceCalibration;
1141 bool haveDistanceScale;
1142 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001143
1144 inline void applySizeScaleAndBias(float* outSize) const {
1145 if (haveSizeScale) {
1146 *outSize *= sizeScale;
1147 }
1148 if (haveSizeBias) {
1149 *outSize += sizeBias;
1150 }
1151 }
Jeff Brown8d608662010-08-30 03:02:23 -07001152 } mCalibration;
1153
Jeff Brownbe1aa822011-07-27 16:04:54 -07001154 // Raw pointer axis information from the driver.
1155 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001156
Jeff Brownbe1aa822011-07-27 16:04:54 -07001157 // Raw pointer sample data.
1158 RawPointerData mCurrentRawPointerData;
1159 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001160
Jeff Brownbe1aa822011-07-27 16:04:54 -07001161 // Cooked pointer sample data.
1162 CookedPointerData mCurrentCookedPointerData;
1163 CookedPointerData mLastCookedPointerData;
1164
1165 // Button state.
1166 int32_t mCurrentButtonState;
1167 int32_t mLastButtonState;
1168
Jeff Brown65fd2512011-08-18 11:20:58 -07001169 // Scroll state.
1170 int32_t mCurrentRawVScroll;
1171 int32_t mCurrentRawHScroll;
1172
1173 // Id bits used to differentiate fingers, stylus and mouse tools.
1174 BitSet32 mCurrentFingerIdBits; // finger or unknown
1175 BitSet32 mLastFingerIdBits;
1176 BitSet32 mCurrentStylusIdBits; // stylus or eraser
1177 BitSet32 mLastStylusIdBits;
1178 BitSet32 mCurrentMouseIdBits; // mouse or lens
1179 BitSet32 mLastMouseIdBits;
1180
Jeff Brownbe1aa822011-07-27 16:04:54 -07001181 // True if we sent a HOVER_ENTER event.
1182 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001183
1184 // The time the primary pointer last went down.
1185 nsecs_t mDownTime;
1186
Jeff Brownace13b12011-03-09 17:39:48 -08001187 // The pointer controller, or null if the device is not a pointer.
1188 sp<PointerControllerInterface> mPointerController;
1189
Jeff Brownbe1aa822011-07-27 16:04:54 -07001190 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001191
Jeff Brown8d608662010-08-30 03:02:23 -07001192 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001193 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001194 virtual void configureRawPointerAxes();
1195 virtual void dumpRawPointerAxes(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001196 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001197 virtual void dumpSurface(String8& dump);
1198 virtual void configureVirtualKeys();
1199 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001200 virtual void parseCalibration();
1201 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001202 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001203
Jeff Brown65fd2512011-08-18 11:20:58 -07001204 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001205
1206private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001207 // The surface orientation and width and height set by configureSurface().
1208 int32_t mSurfaceOrientation;
1209 int32_t mSurfaceWidth;
1210 int32_t mSurfaceHeight;
1211
1212 // The associated display orientation and width and height set by configureSurface().
1213 int32_t mAssociatedDisplayOrientation;
1214 int32_t mAssociatedDisplayWidth;
1215 int32_t mAssociatedDisplayHeight;
1216
1217 // Translation and scaling factors, orientation-independent.
1218 float mXScale;
1219 float mXPrecision;
1220
1221 float mYScale;
1222 float mYPrecision;
1223
1224 float mGeometricScale;
1225
Jeff Brownbe1aa822011-07-27 16:04:54 -07001226 float mPressureScale;
1227
1228 float mSizeScale;
1229
Jeff Brown65fd2512011-08-18 11:20:58 -07001230 float mOrientationCenter;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001231 float mOrientationScale;
1232
1233 float mDistanceScale;
1234
Jeff Brown65fd2512011-08-18 11:20:58 -07001235 bool mHaveTilt;
1236 float mTiltXCenter;
1237 float mTiltXScale;
1238 float mTiltYCenter;
1239 float mTiltYScale;
1240
Jeff Brownbe1aa822011-07-27 16:04:54 -07001241 // Oriented motion ranges for input device info.
1242 struct OrientedRanges {
1243 InputDeviceInfo::MotionRange x;
1244 InputDeviceInfo::MotionRange y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001245 InputDeviceInfo::MotionRange pressure;
1246
1247 bool haveSize;
1248 InputDeviceInfo::MotionRange size;
1249
1250 bool haveTouchSize;
1251 InputDeviceInfo::MotionRange touchMajor;
1252 InputDeviceInfo::MotionRange touchMinor;
1253
1254 bool haveToolSize;
1255 InputDeviceInfo::MotionRange toolMajor;
1256 InputDeviceInfo::MotionRange toolMinor;
1257
1258 bool haveOrientation;
1259 InputDeviceInfo::MotionRange orientation;
1260
1261 bool haveDistance;
1262 InputDeviceInfo::MotionRange distance;
Jeff Brown65fd2512011-08-18 11:20:58 -07001263
1264 bool haveTilt;
1265 InputDeviceInfo::MotionRange tilt;
1266
1267 OrientedRanges() {
1268 clear();
1269 }
1270
1271 void clear() {
1272 haveSize = false;
1273 haveTouchSize = false;
1274 haveToolSize = false;
1275 haveOrientation = false;
1276 haveDistance = false;
1277 haveTilt = false;
1278 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001279 } mOrientedRanges;
1280
1281 // Oriented dimensions and precision.
1282 float mOrientedSurfaceWidth;
1283 float mOrientedSurfaceHeight;
1284 float mOrientedXPrecision;
1285 float mOrientedYPrecision;
1286
1287 struct CurrentVirtualKeyState {
1288 bool down;
1289 bool ignored;
1290 nsecs_t downTime;
1291 int32_t keyCode;
1292 int32_t scanCode;
1293 } mCurrentVirtualKey;
1294
Jeff Brown65fd2512011-08-18 11:20:58 -07001295 // Scale factor for gesture or mouse based pointer movements.
1296 float mPointerXMovementScale;
1297 float mPointerYMovementScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001298
1299 // Scale factor for gesture based zooming and other freeform motions.
Jeff Brown65fd2512011-08-18 11:20:58 -07001300 float mPointerXZoomScale;
1301 float mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001302
1303 // The maximum swipe width.
1304 float mPointerGestureMaxSwipeWidth;
1305
Jeff Brown6d0fec22010-07-23 21:28:06 -07001306 struct PointerDistanceHeapElement {
1307 uint32_t currentPointerIndex : 8;
1308 uint32_t lastPointerIndex : 8;
1309 uint64_t distance : 48; // squared distance
1310 };
1311
Jeff Brown65fd2512011-08-18 11:20:58 -07001312 enum PointerUsage {
1313 POINTER_USAGE_NONE,
1314 POINTER_USAGE_GESTURES,
1315 POINTER_USAGE_STYLUS,
1316 POINTER_USAGE_MOUSE,
1317 };
1318 PointerUsage mPointerUsage;
1319
Jeff Brownace13b12011-03-09 17:39:48 -08001320 struct PointerGesture {
1321 enum Mode {
1322 // No fingers, button is not pressed.
1323 // Nothing happening.
1324 NEUTRAL,
1325
1326 // No fingers, button is not pressed.
1327 // Tap detected.
1328 // Emits DOWN and UP events at the pointer location.
1329 TAP,
1330
Jeff Brown79ac9692011-04-19 21:20:10 -07001331 // Exactly one finger dragging following a tap.
1332 // Pointer follows the active finger.
1333 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001334 //
1335 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001336 TAP_DRAG,
1337
Jeff Brownace13b12011-03-09 17:39:48 -08001338 // Button is pressed.
1339 // Pointer follows the active finger if there is one. Other fingers are ignored.
1340 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001341 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001342
1343 // Exactly one finger, button is not pressed.
1344 // Pointer follows the active finger.
1345 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001346 //
1347 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001348 HOVER,
1349
Jeff Brown2352b972011-04-12 22:39:53 -07001350 // Exactly two fingers but neither have moved enough to clearly indicate
1351 // whether a swipe or freeform gesture was intended. We consider the
1352 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1353 // Pointer does not move.
1354 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1355 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001356
1357 // Exactly two fingers moving in the same direction, button is not pressed.
1358 // Pointer does not move.
1359 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1360 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001361 SWIPE,
1362
1363 // Two or more fingers moving in arbitrary directions, button is not pressed.
1364 // Pointer does not move.
1365 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1366 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001367 FREEFORM,
1368
1369 // Waiting for quiet time to end before starting the next gesture.
1370 QUIET,
1371 };
1372
Jeff Brown2352b972011-04-12 22:39:53 -07001373 // Time the first finger went down.
1374 nsecs_t firstTouchTime;
1375
Jeff Brownace13b12011-03-09 17:39:48 -08001376 // The active pointer id from the raw touch data.
1377 int32_t activeTouchId; // -1 if none
1378
1379 // The active pointer id from the gesture last delivered to the application.
1380 int32_t activeGestureId; // -1 if none
1381
1382 // Pointer coords and ids for the current and previous pointer gesture.
1383 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001384 BitSet32 currentGestureIdBits;
1385 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001386 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001387 PointerCoords currentGestureCoords[MAX_POINTERS];
1388
1389 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001390 BitSet32 lastGestureIdBits;
1391 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001392 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001393 PointerCoords lastGestureCoords[MAX_POINTERS];
1394
Jeff Brownace13b12011-03-09 17:39:48 -08001395 // Time the pointer gesture last went down.
1396 nsecs_t downTime;
1397
Jeff Brown79ac9692011-04-19 21:20:10 -07001398 // Time when the pointer went down for a TAP.
1399 nsecs_t tapDownTime;
1400
1401 // Time when the pointer went up for a TAP.
1402 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001403
Jeff Brown2352b972011-04-12 22:39:53 -07001404 // Location of initial tap.
1405 float tapX, tapY;
1406
Jeff Brownace13b12011-03-09 17:39:48 -08001407 // Time we started waiting for quiescence.
1408 nsecs_t quietTime;
1409
Jeff Brown2352b972011-04-12 22:39:53 -07001410 // Reference points for multitouch gestures.
1411 float referenceTouchX; // reference touch X/Y coordinates in surface units
1412 float referenceTouchY;
1413 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1414 float referenceGestureY;
1415
Jeff Brown538881e2011-05-25 18:23:38 -07001416 // Distance that each pointer has traveled which has not yet been
1417 // subsumed into the reference gesture position.
1418 BitSet32 referenceIdBits;
1419 struct Delta {
1420 float dx, dy;
1421 };
1422 Delta referenceDeltas[MAX_POINTER_ID + 1];
1423
Jeff Brown2352b972011-04-12 22:39:53 -07001424 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1425 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1426
Jeff Brownace13b12011-03-09 17:39:48 -08001427 // A velocity tracker for determining whether to switch active pointers during drags.
1428 VelocityTracker velocityTracker;
1429
1430 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001431 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001432 activeTouchId = -1;
1433 activeGestureId = -1;
1434 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001435 currentGestureIdBits.clear();
1436 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001437 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001438 downTime = 0;
1439 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001440 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001441 resetQuietTime();
1442 }
1443
Jeff Brown79ac9692011-04-19 21:20:10 -07001444 void resetTap() {
1445 tapDownTime = LLONG_MIN;
1446 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001447 }
1448
1449 void resetQuietTime() {
1450 quietTime = LLONG_MIN;
1451 }
1452 } mPointerGesture;
1453
Jeff Brown65fd2512011-08-18 11:20:58 -07001454 struct PointerSimple {
1455 PointerCoords currentCoords;
1456 PointerProperties currentProperties;
1457 PointerCoords lastCoords;
1458 PointerProperties lastProperties;
1459
1460 // True if the pointer is down.
1461 bool down;
1462
1463 // True if the pointer is hovering.
1464 bool hovering;
1465
1466 // Time the pointer last went down.
1467 nsecs_t downTime;
1468
1469 void reset() {
1470 currentCoords.clear();
1471 currentProperties.clear();
1472 lastCoords.clear();
1473 lastProperties.clear();
1474 down = false;
1475 hovering = false;
1476 downTime = 0;
1477 }
1478 } mPointerSimple;
1479
1480 // The pointer and scroll velocity controls.
1481 VelocityControl mPointerVelocityControl;
1482 VelocityControl mWheelXVelocityControl;
1483 VelocityControl mWheelYVelocityControl;
1484
1485 void sync(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001486
Jeff Brownbe1aa822011-07-27 16:04:54 -07001487 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1488 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1489 int32_t keyEventAction, int32_t keyEventFlags);
1490
Jeff Brown6d0fec22010-07-23 21:28:06 -07001491 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001492 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1493 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1494 void cookPointerData();
1495
Jeff Brown65fd2512011-08-18 11:20:58 -07001496 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1497 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1498
Jeff Brown79ac9692011-04-19 21:20:10 -07001499 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
Jeff Brown65fd2512011-08-18 11:20:58 -07001500 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Jeff Brown79ac9692011-04-19 21:20:10 -07001501 bool preparePointerGestures(nsecs_t when,
Jeff Brown65fd2512011-08-18 11:20:58 -07001502 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1503 bool isTimeout);
1504
1505 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1506 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1507
1508 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1509 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1510
1511 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1512 bool down, bool hovering);
1513 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001514
1515 // Dispatches a motion event.
1516 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1517 // method will take care of setting the index and transmuting the action to DOWN or UP
1518 // it is the first / last pointer to go down / up.
1519 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001520 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1521 int32_t edgeFlags,
1522 const PointerProperties* properties, const PointerCoords* coords,
1523 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001524 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1525
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001526 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001527 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001528 bool updateMovedPointers(const PointerProperties* inProperties,
1529 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1530 PointerProperties* outProperties, PointerCoords* outCoords,
1531 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001532
Jeff Brownbe1aa822011-07-27 16:04:54 -07001533 bool isPointInsideSurface(int32_t x, int32_t y);
1534 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001535
Jeff Brownbe1aa822011-07-27 16:04:54 -07001536 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001537};
1538
1539
1540class SingleTouchInputMapper : public TouchInputMapper {
1541public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001542 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001543 virtual ~SingleTouchInputMapper();
1544
Jeff Brown65fd2512011-08-18 11:20:58 -07001545 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001546 virtual void process(const RawEvent* rawEvent);
1547
1548protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001549 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001550 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001551
1552private:
Jeff Brown49754db2011-07-01 17:37:58 -07001553 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001554};
1555
1556
1557class MultiTouchInputMapper : public TouchInputMapper {
1558public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001559 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001560 virtual ~MultiTouchInputMapper();
1561
Jeff Brown65fd2512011-08-18 11:20:58 -07001562 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001563 virtual void process(const RawEvent* rawEvent);
1564
1565protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001566 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001567 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001568
1569private:
Jeff Brown49754db2011-07-01 17:37:58 -07001570 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001571
Jeff Brown6894a292011-07-01 17:59:27 -07001572 // Specifies the pointer id bits that are in use, and their associated tracking id.
1573 BitSet32 mPointerIdBits;
1574 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001575};
1576
Jeff Browncb1404e2011-01-15 18:14:15 -08001577
1578class JoystickInputMapper : public InputMapper {
1579public:
1580 JoystickInputMapper(InputDevice* device);
1581 virtual ~JoystickInputMapper();
1582
1583 virtual uint32_t getSources();
1584 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1585 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001586 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1587 virtual void reset(nsecs_t when);
Jeff Browncb1404e2011-01-15 18:14:15 -08001588 virtual void process(const RawEvent* rawEvent);
1589
1590private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001591 struct Axis {
1592 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001593 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001594
Jeff Brown6f2fba42011-02-19 01:08:02 -08001595 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001596
Jeff Brown6f2fba42011-02-19 01:08:02 -08001597 float scale; // scale factor from raw to normalized values
1598 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001599 float highScale; // scale factor from raw to normalized values of high split
1600 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001601
Jeff Brown6f2fba42011-02-19 01:08:02 -08001602 float min; // normalized inclusive minimum
1603 float max; // normalized inclusive maximum
1604 float flat; // normalized flat region size
1605 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001606
Jeff Brown6f2fba42011-02-19 01:08:02 -08001607 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001608 float currentValue; // current value
1609 float newValue; // most recent value
1610 float highCurrentValue; // current value of high split
1611 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001612
Jeff Brown85297452011-03-04 13:07:49 -08001613 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1614 bool explicitlyMapped, float scale, float offset,
1615 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001616 float min, float max, float flat, float fuzz) {
1617 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001618 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001619 this->explicitlyMapped = explicitlyMapped;
1620 this->scale = scale;
1621 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001622 this->highScale = highScale;
1623 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001624 this->min = min;
1625 this->max = max;
1626 this->flat = flat;
1627 this->fuzz = fuzz;
1628 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001629 resetValue();
1630 }
1631
1632 void resetValue() {
1633 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001634 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001635 this->highCurrentValue = 0;
1636 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001637 }
1638 };
1639
Jeff Brown6f2fba42011-02-19 01:08:02 -08001640 // Axes indexed by raw ABS_* axis index.
1641 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001642
Jeff Brown6f2fba42011-02-19 01:08:02 -08001643 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001644
Jeff Brown85297452011-03-04 13:07:49 -08001645 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001646 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001647 bool filterAxes(bool force);
1648
1649 static bool hasValueChangedSignificantly(float filter,
1650 float newValue, float currentValue, float min, float max);
1651 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1652 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001653
Jeff Brown6f2fba42011-02-19 01:08:02 -08001654 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001655};
1656
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001657} // namespace android
1658
1659#endif // _UI_INPUT_READER_H