blob: 03198a68484dc432c9b99648fdef049fe9465848 [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 Brownaf9e8d32012-04-12 17:32:48 -0700262 /* Gets information about all input devices.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700263 *
264 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700265 */
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700266 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700267
268 /* Query current input state. */
269 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
270 int32_t scanCode) = 0;
271 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
272 int32_t keyCode) = 0;
273 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
274 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700275
276 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700277 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
278 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700279
Jeff Brown474dcb52011-06-14 20:22:50 -0700280 /* Requests that a reconfiguration of all input devices.
281 * The changes flag is a bitfield that indicates what has changed and whether
282 * the input devices must all be reopened. */
283 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Browna47425a2012-04-13 04:09:27 -0700284
285 /* Controls the vibrator of a particular input device. */
286 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
287 ssize_t repeat, int32_t token) = 0;
288 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289};
290
291
292/* Internal interface used by individual input devices to access global input device state
293 * and parameters maintained by the input reader.
294 */
295class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700296public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700297 InputReaderContext() { }
298 virtual ~InputReaderContext() { }
299
Jeff Brown6d0fec22010-07-23 21:28:06 -0700300 virtual void updateGlobalMetaState() = 0;
301 virtual int32_t getGlobalMetaState() = 0;
302
Jeff Brownfe508922011-01-18 15:10:10 -0800303 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
304 virtual bool shouldDropVirtualKey(nsecs_t now,
305 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
306
Jeff Brown05dc66a2011-03-02 14:41:58 -0800307 virtual void fadePointer() = 0;
308
Jeff Brownaa3855d2011-03-17 01:34:19 -0700309 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700310 virtual int32_t bumpGeneration() = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700311
Jeff Brown6d0fec22010-07-23 21:28:06 -0700312 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700313 virtual InputListenerInterface* getListener() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700314 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700315};
316
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700318/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brownbe1aa822011-07-27 16:04:54 -0700319 * that it sends to the input listener. Some functions of the input reader, such as early
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 * event filtering in low power states, are controlled by a separate policy object.
321 *
Jeff Brownbe1aa822011-07-27 16:04:54 -0700322 * The InputReader owns a collection of InputMappers. Most of the work it does happens
323 * on the input reader thread but the InputReader can receive queries from other system
324 * components running on arbitrary threads. To keep things manageable, the InputReader
325 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
326 * EventHub or the InputReaderPolicy but it is never held while calling into the
327 * InputListener.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700328 */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700329class InputReader : public InputReaderInterface {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700330public:
331 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700332 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700333 const sp<InputListenerInterface>& listener);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700334 virtual ~InputReader();
335
Jeff Brownb88102f2010-09-08 11:49:43 -0700336 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700337 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700338
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700339 virtual void loopOnce();
340
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700341 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700342
Jeff Brown6d0fec22010-07-23 21:28:06 -0700343 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
344 int32_t scanCode);
345 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
346 int32_t keyCode);
347 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
348 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700349
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
351 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352
Jeff Brown474dcb52011-06-14 20:22:50 -0700353 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700354
Jeff Browna47425a2012-04-13 04:09:27 -0700355 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
356 ssize_t repeat, int32_t token);
357 virtual void cancelVibrate(int32_t deviceId, int32_t token);
358
Jeff Brownc3db8582010-10-20 15:33:38 -0700359protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700360 // These members are protected so they can be instrumented by test cases.
361 virtual InputDevice* createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700362 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700363
364 class ContextImpl : public InputReaderContext {
365 InputReader* mReader;
366
367 public:
368 ContextImpl(InputReader* reader);
369
370 virtual void updateGlobalMetaState();
371 virtual int32_t getGlobalMetaState();
372 virtual void disableVirtualKeysUntil(nsecs_t time);
373 virtual bool shouldDropVirtualKey(nsecs_t now,
374 InputDevice* device, int32_t keyCode, int32_t scanCode);
375 virtual void fadePointer();
376 virtual void requestTimeoutAtTime(nsecs_t when);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700377 virtual int32_t bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700378 virtual InputReaderPolicyInterface* getPolicy();
379 virtual InputListenerInterface* getListener();
380 virtual EventHubInterface* getEventHub();
381 } mContext;
382
383 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700384
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700385private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700386 Mutex mLock;
387
Jeff Brown112b5f52012-01-27 17:32:06 -0800388 Condition mReaderIsAliveCondition;
389
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700390 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700391 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700392 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700393
Jeff Brown214eaf42011-05-26 19:17:02 -0700394 InputReaderConfiguration mConfig;
395
Jeff Brownb7198742011-03-18 18:14:26 -0700396 // The event queue.
397 static const int EVENT_BUFFER_SIZE = 256;
398 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
399
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700400 KeyedVector<int32_t, InputDevice*> mDevices;
401
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700403 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700404
Jeff Brown65fd2512011-08-18 11:20:58 -0700405 void addDeviceLocked(nsecs_t when, int32_t deviceId);
406 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700407 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
408 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700409
Jeff Brownbe1aa822011-07-27 16:04:54 -0700410 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700411
Jeff Brownbe1aa822011-07-27 16:04:54 -0700412 int32_t mGlobalMetaState;
413 void updateGlobalMetaStateLocked();
414 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415
Jeff Brownbe1aa822011-07-27 16:04:54 -0700416 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700417
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700418 int32_t mGeneration;
419 int32_t bumpGenerationLocked();
420
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700421 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
422
Jeff Brownbe1aa822011-07-27 16:04:54 -0700423 nsecs_t mDisableVirtualKeysTimeout;
424 void disableVirtualKeysUntilLocked(nsecs_t time);
425 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800426 InputDevice* device, int32_t keyCode, int32_t scanCode);
427
Jeff Brownbe1aa822011-07-27 16:04:54 -0700428 nsecs_t mNextTimeout;
429 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700430
Jeff Brownbe1aa822011-07-27 16:04:54 -0700431 uint32_t mConfigurationChangesToRefresh;
432 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700433
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434 // state queries
435 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700436 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700438 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700440};
441
442
443/* Reads raw events from the event hub and processes them, endlessly. */
444class InputReaderThread : public Thread {
445public:
446 InputReaderThread(const sp<InputReaderInterface>& reader);
447 virtual ~InputReaderThread();
448
449private:
450 sp<InputReaderInterface> mReader;
451
452 virtual bool threadLoop();
453};
454
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455
456/* Represents the state of a single input device. */
457class InputDevice {
458public:
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700459 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700460 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461 ~InputDevice();
462
463 inline InputReaderContext* getContext() { return mContext; }
464 inline int32_t getId() { return mId; }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700465 inline int32_t getGeneration() { return mGeneration; }
Jeff Browne38fdfa2012-04-06 14:51:01 -0700466 inline const String8& getName() { return mIdentifier.name; }
Jeff Brown9ee285af2011-08-31 12:56:34 -0700467 inline uint32_t getClasses() { return mClasses; }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700468 inline uint32_t getSources() { return mSources; }
469
Jeff Brown56194eb2011-03-02 19:23:13 -0800470 inline bool isExternal() { return mIsExternal; }
471 inline void setExternal(bool external) { mIsExternal = external; }
472
Jeff Brown6d0fec22010-07-23 21:28:06 -0700473 inline bool isIgnored() { return mMappers.isEmpty(); }
474
Jeff Brownef3d7e82010-09-30 14:33:04 -0700475 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476 void addMapper(InputMapper* mapper);
Jeff Brown65fd2512011-08-18 11:20:58 -0700477 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
478 void reset(nsecs_t when);
Jeff Brownb7198742011-03-18 18:14:26 -0700479 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700480 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481
482 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
483 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
484 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
485 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
486 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
487 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700488 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
489 void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490
491 int32_t getMetaState();
492
Jeff Brown05dc66a2011-03-02 14:41:58 -0800493 void fadePointer();
494
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700495 void bumpGeneration();
496
Jeff Brown65fd2512011-08-18 11:20:58 -0700497 void notifyReset(nsecs_t when);
498
Jeff Brown49754db2011-07-01 17:37:58 -0700499 inline const PropertyMap& getConfiguration() { return mConfiguration; }
500 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700501
Jeff Brown65fd2512011-08-18 11:20:58 -0700502 bool hasKey(int32_t code) {
503 return getEventHub()->hasScanCode(mId, code);
504 }
505
Jeff Brown00710e92012-04-19 15:18:26 -0700506 bool hasAbsoluteAxis(int32_t code) {
507 RawAbsoluteAxisInfo info;
508 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
509 return info.valid;
510 }
511
Jeff Brown65fd2512011-08-18 11:20:58 -0700512 bool isKeyPressed(int32_t code) {
513 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
514 }
515
516 int32_t getAbsoluteAxisValue(int32_t code) {
517 int32_t value;
518 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
519 return value;
520 }
521
Jeff Brown6d0fec22010-07-23 21:28:06 -0700522private:
523 InputReaderContext* mContext;
524 int32_t mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700525 int32_t mGeneration;
Jeff Browne38fdfa2012-04-06 14:51:01 -0700526 InputDeviceIdentifier mIdentifier;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700527 String8 mAlias;
Jeff Brown9ee285af2011-08-31 12:56:34 -0700528 uint32_t mClasses;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700529
530 Vector<InputMapper*> mMappers;
531
Jeff Brown6d0fec22010-07-23 21:28:06 -0700532 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800533 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700534 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535
536 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
537 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700538
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800539 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700540};
541
542
Jeff Brown49754db2011-07-01 17:37:58 -0700543/* Keeps track of the state of mouse or touch pad buttons. */
544class CursorButtonAccumulator {
545public:
546 CursorButtonAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700547 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700548
Jeff Brown49754db2011-07-01 17:37:58 -0700549 void process(const RawEvent* rawEvent);
550
551 uint32_t getButtonState() const;
552
553private:
554 bool mBtnLeft;
555 bool mBtnRight;
556 bool mBtnMiddle;
557 bool mBtnBack;
558 bool mBtnSide;
559 bool mBtnForward;
560 bool mBtnExtra;
561 bool mBtnTask;
Jeff Brown65fd2512011-08-18 11:20:58 -0700562
563 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700564};
565
566
567/* Keeps track of cursor movements. */
568
569class CursorMotionAccumulator {
570public:
571 CursorMotionAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700572 void reset(InputDevice* device);
573
574 void process(const RawEvent* rawEvent);
575 void finishSync();
576
577 inline int32_t getRelativeX() const { return mRelX; }
578 inline int32_t getRelativeY() const { return mRelY; }
579
580private:
581 int32_t mRelX;
582 int32_t mRelY;
Jeff Brown49754db2011-07-01 17:37:58 -0700583
584 void clearRelativeAxes();
Jeff Brown65fd2512011-08-18 11:20:58 -0700585};
586
587
588/* Keeps track of cursor scrolling motions. */
589
590class CursorScrollAccumulator {
591public:
592 CursorScrollAccumulator();
593 void configure(InputDevice* device);
594 void reset(InputDevice* device);
595
Jeff Brown49754db2011-07-01 17:37:58 -0700596 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700597 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700598
599 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
600 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
601
602 inline int32_t getRelativeX() const { return mRelX; }
603 inline int32_t getRelativeY() const { return mRelY; }
604 inline int32_t getRelativeVWheel() const { return mRelWheel; }
605 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
606
607private:
608 bool mHaveRelWheel;
609 bool mHaveRelHWheel;
610
611 int32_t mRelX;
612 int32_t mRelY;
613 int32_t mRelWheel;
614 int32_t mRelHWheel;
Jeff Brown65fd2512011-08-18 11:20:58 -0700615
616 void clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700617};
618
619
620/* Keeps track of the state of touch, stylus and tool buttons. */
621class TouchButtonAccumulator {
622public:
623 TouchButtonAccumulator();
624 void configure(InputDevice* device);
Jeff Brown65fd2512011-08-18 11:20:58 -0700625 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700626
Jeff Brown49754db2011-07-01 17:37:58 -0700627 void process(const RawEvent* rawEvent);
628
629 uint32_t getButtonState() const;
630 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700631 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700632 bool isHovering() const;
Jeff Brown00710e92012-04-19 15:18:26 -0700633 bool hasStylus() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700634
635private:
636 bool mHaveBtnTouch;
Jeff Brown00710e92012-04-19 15:18:26 -0700637 bool mHaveStylus;
Jeff Brown49754db2011-07-01 17:37:58 -0700638
639 bool mBtnTouch;
640 bool mBtnStylus;
641 bool mBtnStylus2;
642 bool mBtnToolFinger;
643 bool mBtnToolPen;
644 bool mBtnToolRubber;
Jeff Brown65fd2512011-08-18 11:20:58 -0700645 bool mBtnToolBrush;
646 bool mBtnToolPencil;
647 bool mBtnToolAirbrush;
648 bool mBtnToolMouse;
649 bool mBtnToolLens;
Jeff Brownea6892e2011-08-23 17:31:25 -0700650 bool mBtnToolDoubleTap;
651 bool mBtnToolTripleTap;
652 bool mBtnToolQuadTap;
Jeff Brown65fd2512011-08-18 11:20:58 -0700653
654 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700655};
656
657
Jeff Brownbe1aa822011-07-27 16:04:54 -0700658/* Raw axis information from the driver. */
659struct RawPointerAxes {
660 RawAbsoluteAxisInfo x;
661 RawAbsoluteAxisInfo y;
662 RawAbsoluteAxisInfo pressure;
663 RawAbsoluteAxisInfo touchMajor;
664 RawAbsoluteAxisInfo touchMinor;
665 RawAbsoluteAxisInfo toolMajor;
666 RawAbsoluteAxisInfo toolMinor;
667 RawAbsoluteAxisInfo orientation;
668 RawAbsoluteAxisInfo distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700669 RawAbsoluteAxisInfo tiltX;
670 RawAbsoluteAxisInfo tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700671 RawAbsoluteAxisInfo trackingId;
672 RawAbsoluteAxisInfo slot;
673
674 RawPointerAxes();
675 void clear();
676};
677
678
679/* Raw data for a collection of pointers including a pointer id mapping table. */
680struct RawPointerData {
681 struct Pointer {
682 uint32_t id;
683 int32_t x;
684 int32_t y;
685 int32_t pressure;
686 int32_t touchMajor;
687 int32_t touchMinor;
688 int32_t toolMajor;
689 int32_t toolMinor;
690 int32_t orientation;
691 int32_t distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700692 int32_t tiltX;
693 int32_t tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700694 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
695 bool isHovering;
696 };
697
698 uint32_t pointerCount;
699 Pointer pointers[MAX_POINTERS];
700 BitSet32 hoveringIdBits, touchingIdBits;
701 uint32_t idToIndex[MAX_POINTER_ID + 1];
702
703 RawPointerData();
704 void clear();
705 void copyFrom(const RawPointerData& other);
706 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
707
708 inline void markIdBit(uint32_t id, bool isHovering) {
709 if (isHovering) {
710 hoveringIdBits.markBit(id);
711 } else {
712 touchingIdBits.markBit(id);
713 }
714 }
715
716 inline void clearIdBits() {
717 hoveringIdBits.clear();
718 touchingIdBits.clear();
719 }
720
721 inline const Pointer& pointerForId(uint32_t id) const {
722 return pointers[idToIndex[id]];
723 }
724
725 inline bool isHovering(uint32_t pointerIndex) {
726 return pointers[pointerIndex].isHovering;
727 }
728};
729
730
731/* Cooked data for a collection of pointers including a pointer id mapping table. */
732struct CookedPointerData {
733 uint32_t pointerCount;
734 PointerProperties pointerProperties[MAX_POINTERS];
735 PointerCoords pointerCoords[MAX_POINTERS];
736 BitSet32 hoveringIdBits, touchingIdBits;
737 uint32_t idToIndex[MAX_POINTER_ID + 1];
738
739 CookedPointerData();
740 void clear();
741 void copyFrom(const CookedPointerData& other);
742
743 inline bool isHovering(uint32_t pointerIndex) {
744 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
745 }
746};
747
748
Jeff Brown49754db2011-07-01 17:37:58 -0700749/* Keeps track of the state of single-touch protocol. */
750class SingleTouchMotionAccumulator {
751public:
752 SingleTouchMotionAccumulator();
753
Jeff Brown49754db2011-07-01 17:37:58 -0700754 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700755 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700756
757 inline int32_t getAbsoluteX() const { return mAbsX; }
758 inline int32_t getAbsoluteY() const { return mAbsY; }
759 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
760 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
761 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
Jeff Brown65fd2512011-08-18 11:20:58 -0700762 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
763 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
Jeff Brown49754db2011-07-01 17:37:58 -0700764
765private:
766 int32_t mAbsX;
767 int32_t mAbsY;
768 int32_t mAbsPressure;
769 int32_t mAbsToolWidth;
770 int32_t mAbsDistance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700771 int32_t mAbsTiltX;
772 int32_t mAbsTiltY;
773
774 void clearAbsoluteAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700775};
776
777
778/* Keeps track of the state of multi-touch protocol. */
779class MultiTouchMotionAccumulator {
780public:
781 class Slot {
782 public:
783 inline bool isInUse() const { return mInUse; }
784 inline int32_t getX() const { return mAbsMTPositionX; }
785 inline int32_t getY() const { return mAbsMTPositionY; }
786 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
787 inline int32_t getTouchMinor() const {
788 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
789 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
790 inline int32_t getToolMinor() const {
791 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
792 inline int32_t getOrientation() const { return mAbsMTOrientation; }
793 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
794 inline int32_t getPressure() const { return mAbsMTPressure; }
795 inline int32_t getDistance() const { return mAbsMTDistance; }
796 inline int32_t getToolType() const;
797
798 private:
799 friend class MultiTouchMotionAccumulator;
800
801 bool mInUse;
802 bool mHaveAbsMTTouchMinor;
803 bool mHaveAbsMTWidthMinor;
804 bool mHaveAbsMTToolType;
805
806 int32_t mAbsMTPositionX;
807 int32_t mAbsMTPositionY;
808 int32_t mAbsMTTouchMajor;
809 int32_t mAbsMTTouchMinor;
810 int32_t mAbsMTWidthMajor;
811 int32_t mAbsMTWidthMinor;
812 int32_t mAbsMTOrientation;
813 int32_t mAbsMTTrackingId;
814 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700815 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700816 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700817
818 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700819 void clear();
820 };
821
822 MultiTouchMotionAccumulator();
823 ~MultiTouchMotionAccumulator();
824
Jeff Brown00710e92012-04-19 15:18:26 -0700825 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
Jeff Brown65fd2512011-08-18 11:20:58 -0700826 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700827 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700828 void finishSync();
Jeff Brown00710e92012-04-19 15:18:26 -0700829 bool hasStylus() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700830
Jeff Brown49754db2011-07-01 17:37:58 -0700831 inline size_t getSlotCount() const { return mSlotCount; }
832 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
833
834private:
835 int32_t mCurrentSlot;
836 Slot* mSlots;
837 size_t mSlotCount;
838 bool mUsingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -0700839 bool mHaveStylus;
Jeff Brown65fd2512011-08-18 11:20:58 -0700840
841 void clearSlots(int32_t initialSlot);
Jeff Brown49754db2011-07-01 17:37:58 -0700842};
843
844
Jeff Brown6d0fec22010-07-23 21:28:06 -0700845/* An input mapper transforms raw input events into cooked event data.
846 * A single input device can have multiple associated input mappers in order to interpret
847 * different classes of events.
Jeff Brown65fd2512011-08-18 11:20:58 -0700848 *
849 * InputMapper lifecycle:
850 * - create
851 * - configure with 0 changes
852 * - reset
853 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
854 * - reset
855 * - destroy
Jeff Brown6d0fec22010-07-23 21:28:06 -0700856 */
857class InputMapper {
858public:
859 InputMapper(InputDevice* device);
860 virtual ~InputMapper();
861
862 inline InputDevice* getDevice() { return mDevice; }
863 inline int32_t getDeviceId() { return mDevice->getId(); }
864 inline const String8 getDeviceName() { return mDevice->getName(); }
865 inline InputReaderContext* getContext() { return mContext; }
866 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700867 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700868 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
869
870 virtual uint32_t getSources() = 0;
871 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700872 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700873 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
874 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700875 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700876 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700877
878 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
879 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
880 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
881 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
882 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700883 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
884 int32_t token);
885 virtual void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886
887 virtual int32_t getMetaState();
888
Jeff Brown05dc66a2011-03-02 14:41:58 -0800889 virtual void fadePointer();
890
Jeff Brown6d0fec22010-07-23 21:28:06 -0700891protected:
892 InputDevice* mDevice;
893 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800894
Jeff Brownbe1aa822011-07-27 16:04:54 -0700895 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700896 void bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700897
Jeff Browncb1404e2011-01-15 18:14:15 -0800898 static void dumpRawAbsoluteAxisInfo(String8& dump,
899 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700900};
901
902
903class SwitchInputMapper : public InputMapper {
904public:
905 SwitchInputMapper(InputDevice* device);
906 virtual ~SwitchInputMapper();
907
908 virtual uint32_t getSources();
909 virtual void process(const RawEvent* rawEvent);
910
911 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
912
913private:
914 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
915};
916
917
Jeff Browna47425a2012-04-13 04:09:27 -0700918class VibratorInputMapper : public InputMapper {
919public:
920 VibratorInputMapper(InputDevice* device);
921 virtual ~VibratorInputMapper();
922
923 virtual uint32_t getSources();
924 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
925 virtual void process(const RawEvent* rawEvent);
926
927 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
928 int32_t token);
929 virtual void cancelVibrate(int32_t token);
930 virtual void timeoutExpired(nsecs_t when);
931 virtual void dump(String8& dump);
932
933private:
934 bool mVibrating;
935 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
936 size_t mPatternSize;
937 ssize_t mRepeat;
938 int32_t mToken;
939 ssize_t mIndex;
940 nsecs_t mNextStepTime;
941
942 void nextStep();
943 void stopVibrating();
944};
945
946
Jeff Brown6d0fec22010-07-23 21:28:06 -0700947class KeyboardInputMapper : public InputMapper {
948public:
Jeff Brownefd32662011-03-08 15:13:06 -0800949 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700950 virtual ~KeyboardInputMapper();
951
952 virtual uint32_t getSources();
953 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700954 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700955 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
956 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700957 virtual void process(const RawEvent* rawEvent);
958
959 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
960 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
961 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
962 const int32_t* keyCodes, uint8_t* outFlags);
963
964 virtual int32_t getMetaState();
965
966private:
967 struct KeyDown {
968 int32_t keyCode;
969 int32_t scanCode;
970 };
971
Jeff Brownefd32662011-03-08 15:13:06 -0800972 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700973 int32_t mKeyboardType;
974
Jeff Brown65fd2512011-08-18 11:20:58 -0700975 int32_t mOrientation; // orientation for dpad keys
976
Jeff Brownbe1aa822011-07-27 16:04:54 -0700977 Vector<KeyDown> mKeyDowns; // keys that are down
978 int32_t mMetaState;
979 nsecs_t mDownTime; // time of most recent key down
980
Jeff Brown49ccac52012-04-11 18:27:33 -0700981 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
982
Jeff Brownbe1aa822011-07-27 16:04:54 -0700983 struct LedState {
984 bool avail; // led is available
985 bool on; // we think the led is currently on
986 };
987 LedState mCapsLockLedState;
988 LedState mNumLockLedState;
989 LedState mScrollLockLedState;
990
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800991 // Immutable configuration parameters.
992 struct Parameters {
993 int32_t associatedDisplayId;
994 bool orientationAware;
995 } mParameters;
996
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800997 void configureParameters();
998 void dumpParameters(String8& dump);
999
Jeff Brown6d0fec22010-07-23 21:28:06 -07001000 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001001
Jeff Brown6d0fec22010-07-23 21:28:06 -07001002 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
1003 uint32_t policyFlags);
1004
Jeff Brownbe1aa822011-07-27 16:04:54 -07001005 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001006
Jeff Brownbe1aa822011-07-27 16:04:54 -07001007 void resetLedState();
1008 void initializeLedState(LedState& ledState, int32_t led);
1009 void updateLedState(bool reset);
1010 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -07001011 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001012};
1013
1014
Jeff Brown83c09682010-12-23 17:50:18 -08001015class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001016public:
Jeff Brown83c09682010-12-23 17:50:18 -08001017 CursorInputMapper(InputDevice* device);
1018 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019
1020 virtual uint32_t getSources();
1021 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001022 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001023 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1024 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001025 virtual void process(const RawEvent* rawEvent);
1026
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001027 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1028
Jeff Brown05dc66a2011-03-02 14:41:58 -08001029 virtual void fadePointer();
1030
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031private:
1032 // Amount that trackball needs to move in order to generate a key event.
1033 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1034
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001035 // Immutable configuration parameters.
1036 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -08001037 enum Mode {
1038 MODE_POINTER,
1039 MODE_NAVIGATION,
1040 };
1041
1042 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001043 int32_t associatedDisplayId;
1044 bool orientationAware;
1045 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001046
Jeff Brown49754db2011-07-01 17:37:58 -07001047 CursorButtonAccumulator mCursorButtonAccumulator;
1048 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown65fd2512011-08-18 11:20:58 -07001049 CursorScrollAccumulator mCursorScrollAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001050
Jeff Brownefd32662011-03-08 15:13:06 -08001051 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001052 float mXScale;
1053 float mYScale;
1054 float mXPrecision;
1055 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001056
Jeff Brown6f2fba42011-02-19 01:08:02 -08001057 float mVWheelScale;
1058 float mHWheelScale;
1059
Jeff Brown19c97d462011-06-01 12:33:19 -07001060 // Velocity controls for mouse pointer and wheel movements.
1061 // The controls for X and Y wheel movements are separate to keep them decoupled.
1062 VelocityControl mPointerVelocityControl;
1063 VelocityControl mWheelXVelocityControl;
1064 VelocityControl mWheelYVelocityControl;
1065
Jeff Brown65fd2512011-08-18 11:20:58 -07001066 int32_t mOrientation;
1067
Jeff Brown83c09682010-12-23 17:50:18 -08001068 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001069
Jeff Brownbe1aa822011-07-27 16:04:54 -07001070 int32_t mButtonState;
1071 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001072
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001073 void configureParameters();
1074 void dumpParameters(String8& dump);
1075
Jeff Brown6d0fec22010-07-23 21:28:06 -07001076 void sync(nsecs_t when);
1077};
1078
1079
1080class TouchInputMapper : public InputMapper {
1081public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001082 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001083 virtual ~TouchInputMapper();
1084
1085 virtual uint32_t getSources();
1086 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001087 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001088 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1089 virtual void reset(nsecs_t when);
1090 virtual void process(const RawEvent* rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001091
1092 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1093 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1094 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1095 const int32_t* keyCodes, uint8_t* outFlags);
1096
Jeff Brownace13b12011-03-09 17:39:48 -08001097 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -07001098 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -08001099
Jeff Brown6d0fec22010-07-23 21:28:06 -07001100protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001101 CursorButtonAccumulator mCursorButtonAccumulator;
1102 CursorScrollAccumulator mCursorScrollAccumulator;
1103 TouchButtonAccumulator mTouchButtonAccumulator;
1104
Jeff Brown6d0fec22010-07-23 21:28:06 -07001105 struct VirtualKey {
1106 int32_t keyCode;
1107 int32_t scanCode;
1108 uint32_t flags;
1109
1110 // computed hit box, specified in touch screen coords based on known display size
1111 int32_t hitLeft;
1112 int32_t hitTop;
1113 int32_t hitRight;
1114 int32_t hitBottom;
1115
1116 inline bool isHit(int32_t x, int32_t y) const {
1117 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1118 }
1119 };
1120
Jeff Brown65fd2512011-08-18 11:20:58 -07001121 // Input sources and device mode.
1122 uint32_t mSource;
1123
1124 enum DeviceMode {
1125 DEVICE_MODE_DISABLED, // input is disabled
1126 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1127 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1128 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1129 };
1130 DeviceMode mDeviceMode;
Jeff Brown83c09682010-12-23 17:50:18 -08001131
Jeff Brown214eaf42011-05-26 19:17:02 -07001132 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001133 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -07001134
Jeff Brown6d0fec22010-07-23 21:28:06 -07001135 // Immutable configuration parameters.
1136 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001137 enum DeviceType {
1138 DEVICE_TYPE_TOUCH_SCREEN,
1139 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -08001140 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001141 };
1142
1143 DeviceType deviceType;
1144 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -07001145 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001146 bool orientationAware;
1147
Jeff Brown2352b972011-04-12 22:39:53 -07001148 enum GestureMode {
1149 GESTURE_MODE_POINTER,
1150 GESTURE_MODE_SPOTS,
1151 };
1152 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001153 } mParameters;
1154
Jeff Brown8d608662010-08-30 03:02:23 -07001155 // Immutable calibration parameters in parsed form.
1156 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -07001157 // Size
1158 enum SizeCalibration {
1159 SIZE_CALIBRATION_DEFAULT,
1160 SIZE_CALIBRATION_NONE,
1161 SIZE_CALIBRATION_GEOMETRIC,
1162 SIZE_CALIBRATION_DIAMETER,
1163 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -07001164 };
1165
Jeff Browna1f89ce2011-08-11 00:05:01 -07001166 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001167
Jeff Browna1f89ce2011-08-11 00:05:01 -07001168 bool haveSizeScale;
1169 float sizeScale;
1170 bool haveSizeBias;
1171 float sizeBias;
1172 bool haveSizeIsSummed;
1173 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001174
1175 // Pressure
1176 enum PressureCalibration {
1177 PRESSURE_CALIBRATION_DEFAULT,
1178 PRESSURE_CALIBRATION_NONE,
1179 PRESSURE_CALIBRATION_PHYSICAL,
1180 PRESSURE_CALIBRATION_AMPLITUDE,
1181 };
Jeff Brown8d608662010-08-30 03:02:23 -07001182
1183 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001184 bool havePressureScale;
1185 float pressureScale;
1186
Jeff Brown8d608662010-08-30 03:02:23 -07001187 // Orientation
1188 enum OrientationCalibration {
1189 ORIENTATION_CALIBRATION_DEFAULT,
1190 ORIENTATION_CALIBRATION_NONE,
1191 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001192 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001193 };
1194
1195 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001196
1197 // Distance
1198 enum DistanceCalibration {
1199 DISTANCE_CALIBRATION_DEFAULT,
1200 DISTANCE_CALIBRATION_NONE,
1201 DISTANCE_CALIBRATION_SCALED,
1202 };
1203
1204 DistanceCalibration distanceCalibration;
1205 bool haveDistanceScale;
1206 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001207
1208 inline void applySizeScaleAndBias(float* outSize) const {
1209 if (haveSizeScale) {
1210 *outSize *= sizeScale;
1211 }
1212 if (haveSizeBias) {
1213 *outSize += sizeBias;
1214 }
1215 }
Jeff Brown8d608662010-08-30 03:02:23 -07001216 } mCalibration;
1217
Jeff Brownbe1aa822011-07-27 16:04:54 -07001218 // Raw pointer axis information from the driver.
1219 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001220
Jeff Brownbe1aa822011-07-27 16:04:54 -07001221 // Raw pointer sample data.
1222 RawPointerData mCurrentRawPointerData;
1223 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001224
Jeff Brownbe1aa822011-07-27 16:04:54 -07001225 // Cooked pointer sample data.
1226 CookedPointerData mCurrentCookedPointerData;
1227 CookedPointerData mLastCookedPointerData;
1228
1229 // Button state.
1230 int32_t mCurrentButtonState;
1231 int32_t mLastButtonState;
1232
Jeff Brown65fd2512011-08-18 11:20:58 -07001233 // Scroll state.
1234 int32_t mCurrentRawVScroll;
1235 int32_t mCurrentRawHScroll;
1236
1237 // Id bits used to differentiate fingers, stylus and mouse tools.
1238 BitSet32 mCurrentFingerIdBits; // finger or unknown
1239 BitSet32 mLastFingerIdBits;
1240 BitSet32 mCurrentStylusIdBits; // stylus or eraser
1241 BitSet32 mLastStylusIdBits;
1242 BitSet32 mCurrentMouseIdBits; // mouse or lens
1243 BitSet32 mLastMouseIdBits;
1244
Jeff Brownbe1aa822011-07-27 16:04:54 -07001245 // True if we sent a HOVER_ENTER event.
1246 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001247
1248 // The time the primary pointer last went down.
1249 nsecs_t mDownTime;
1250
Jeff Brownace13b12011-03-09 17:39:48 -08001251 // The pointer controller, or null if the device is not a pointer.
1252 sp<PointerControllerInterface> mPointerController;
1253
Jeff Brownbe1aa822011-07-27 16:04:54 -07001254 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001255
Jeff Brown8d608662010-08-30 03:02:23 -07001256 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001257 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001258 virtual void configureRawPointerAxes();
1259 virtual void dumpRawPointerAxes(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001260 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001261 virtual void dumpSurface(String8& dump);
1262 virtual void configureVirtualKeys();
1263 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001264 virtual void parseCalibration();
1265 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001266 virtual void dumpCalibration(String8& dump);
Jeff Brown00710e92012-04-19 15:18:26 -07001267 virtual bool hasStylus() const = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001268
Jeff Brown65fd2512011-08-18 11:20:58 -07001269 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001270
1271private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001272 // The surface orientation and width and height set by configureSurface().
1273 int32_t mSurfaceOrientation;
1274 int32_t mSurfaceWidth;
1275 int32_t mSurfaceHeight;
1276
1277 // The associated display orientation and width and height set by configureSurface().
1278 int32_t mAssociatedDisplayOrientation;
1279 int32_t mAssociatedDisplayWidth;
1280 int32_t mAssociatedDisplayHeight;
1281
1282 // Translation and scaling factors, orientation-independent.
1283 float mXScale;
1284 float mXPrecision;
1285
1286 float mYScale;
1287 float mYPrecision;
1288
1289 float mGeometricScale;
1290
Jeff Brownbe1aa822011-07-27 16:04:54 -07001291 float mPressureScale;
1292
1293 float mSizeScale;
1294
Jeff Brown65fd2512011-08-18 11:20:58 -07001295 float mOrientationCenter;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001296 float mOrientationScale;
1297
1298 float mDistanceScale;
1299
Jeff Brown65fd2512011-08-18 11:20:58 -07001300 bool mHaveTilt;
1301 float mTiltXCenter;
1302 float mTiltXScale;
1303 float mTiltYCenter;
1304 float mTiltYScale;
1305
Jeff Brownbe1aa822011-07-27 16:04:54 -07001306 // Oriented motion ranges for input device info.
1307 struct OrientedRanges {
1308 InputDeviceInfo::MotionRange x;
1309 InputDeviceInfo::MotionRange y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001310 InputDeviceInfo::MotionRange pressure;
1311
1312 bool haveSize;
1313 InputDeviceInfo::MotionRange size;
1314
1315 bool haveTouchSize;
1316 InputDeviceInfo::MotionRange touchMajor;
1317 InputDeviceInfo::MotionRange touchMinor;
1318
1319 bool haveToolSize;
1320 InputDeviceInfo::MotionRange toolMajor;
1321 InputDeviceInfo::MotionRange toolMinor;
1322
1323 bool haveOrientation;
1324 InputDeviceInfo::MotionRange orientation;
1325
1326 bool haveDistance;
1327 InputDeviceInfo::MotionRange distance;
Jeff Brown65fd2512011-08-18 11:20:58 -07001328
1329 bool haveTilt;
1330 InputDeviceInfo::MotionRange tilt;
1331
1332 OrientedRanges() {
1333 clear();
1334 }
1335
1336 void clear() {
1337 haveSize = false;
1338 haveTouchSize = false;
1339 haveToolSize = false;
1340 haveOrientation = false;
1341 haveDistance = false;
1342 haveTilt = false;
1343 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001344 } mOrientedRanges;
1345
1346 // Oriented dimensions and precision.
1347 float mOrientedSurfaceWidth;
1348 float mOrientedSurfaceHeight;
1349 float mOrientedXPrecision;
1350 float mOrientedYPrecision;
1351
1352 struct CurrentVirtualKeyState {
1353 bool down;
1354 bool ignored;
1355 nsecs_t downTime;
1356 int32_t keyCode;
1357 int32_t scanCode;
1358 } mCurrentVirtualKey;
1359
Jeff Brown65fd2512011-08-18 11:20:58 -07001360 // Scale factor for gesture or mouse based pointer movements.
1361 float mPointerXMovementScale;
1362 float mPointerYMovementScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001363
1364 // Scale factor for gesture based zooming and other freeform motions.
Jeff Brown65fd2512011-08-18 11:20:58 -07001365 float mPointerXZoomScale;
1366 float mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001367
1368 // The maximum swipe width.
1369 float mPointerGestureMaxSwipeWidth;
1370
Jeff Brown6d0fec22010-07-23 21:28:06 -07001371 struct PointerDistanceHeapElement {
1372 uint32_t currentPointerIndex : 8;
1373 uint32_t lastPointerIndex : 8;
1374 uint64_t distance : 48; // squared distance
1375 };
1376
Jeff Brown65fd2512011-08-18 11:20:58 -07001377 enum PointerUsage {
1378 POINTER_USAGE_NONE,
1379 POINTER_USAGE_GESTURES,
1380 POINTER_USAGE_STYLUS,
1381 POINTER_USAGE_MOUSE,
1382 };
1383 PointerUsage mPointerUsage;
1384
Jeff Brownace13b12011-03-09 17:39:48 -08001385 struct PointerGesture {
1386 enum Mode {
1387 // No fingers, button is not pressed.
1388 // Nothing happening.
1389 NEUTRAL,
1390
1391 // No fingers, button is not pressed.
1392 // Tap detected.
1393 // Emits DOWN and UP events at the pointer location.
1394 TAP,
1395
Jeff Brown79ac9692011-04-19 21:20:10 -07001396 // Exactly one finger dragging following a tap.
1397 // Pointer follows the active finger.
1398 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001399 //
1400 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001401 TAP_DRAG,
1402
Jeff Brownace13b12011-03-09 17:39:48 -08001403 // Button is pressed.
1404 // Pointer follows the active finger if there is one. Other fingers are ignored.
1405 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001406 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001407
1408 // Exactly one finger, button is not pressed.
1409 // Pointer follows the active finger.
1410 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001411 //
1412 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001413 HOVER,
1414
Jeff Brown2352b972011-04-12 22:39:53 -07001415 // Exactly two fingers but neither have moved enough to clearly indicate
1416 // whether a swipe or freeform gesture was intended. We consider the
1417 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1418 // Pointer does not move.
1419 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1420 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001421
1422 // Exactly two fingers moving in the same direction, button is not pressed.
1423 // Pointer does not move.
1424 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1425 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001426 SWIPE,
1427
1428 // Two or more fingers moving in arbitrary directions, button is not pressed.
1429 // Pointer does not move.
1430 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1431 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001432 FREEFORM,
1433
1434 // Waiting for quiet time to end before starting the next gesture.
1435 QUIET,
1436 };
1437
Jeff Brown2352b972011-04-12 22:39:53 -07001438 // Time the first finger went down.
1439 nsecs_t firstTouchTime;
1440
Jeff Brownace13b12011-03-09 17:39:48 -08001441 // The active pointer id from the raw touch data.
1442 int32_t activeTouchId; // -1 if none
1443
1444 // The active pointer id from the gesture last delivered to the application.
1445 int32_t activeGestureId; // -1 if none
1446
1447 // Pointer coords and ids for the current and previous pointer gesture.
1448 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001449 BitSet32 currentGestureIdBits;
1450 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001451 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001452 PointerCoords currentGestureCoords[MAX_POINTERS];
1453
1454 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001455 BitSet32 lastGestureIdBits;
1456 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001457 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001458 PointerCoords lastGestureCoords[MAX_POINTERS];
1459
Jeff Brownace13b12011-03-09 17:39:48 -08001460 // Time the pointer gesture last went down.
1461 nsecs_t downTime;
1462
Jeff Brown79ac9692011-04-19 21:20:10 -07001463 // Time when the pointer went down for a TAP.
1464 nsecs_t tapDownTime;
1465
1466 // Time when the pointer went up for a TAP.
1467 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001468
Jeff Brown2352b972011-04-12 22:39:53 -07001469 // Location of initial tap.
1470 float tapX, tapY;
1471
Jeff Brownace13b12011-03-09 17:39:48 -08001472 // Time we started waiting for quiescence.
1473 nsecs_t quietTime;
1474
Jeff Brown2352b972011-04-12 22:39:53 -07001475 // Reference points for multitouch gestures.
1476 float referenceTouchX; // reference touch X/Y coordinates in surface units
1477 float referenceTouchY;
1478 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1479 float referenceGestureY;
1480
Jeff Brown538881e2011-05-25 18:23:38 -07001481 // Distance that each pointer has traveled which has not yet been
1482 // subsumed into the reference gesture position.
1483 BitSet32 referenceIdBits;
1484 struct Delta {
1485 float dx, dy;
1486 };
1487 Delta referenceDeltas[MAX_POINTER_ID + 1];
1488
Jeff Brown2352b972011-04-12 22:39:53 -07001489 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1490 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1491
Jeff Brownace13b12011-03-09 17:39:48 -08001492 // A velocity tracker for determining whether to switch active pointers during drags.
1493 VelocityTracker velocityTracker;
1494
1495 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001496 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001497 activeTouchId = -1;
1498 activeGestureId = -1;
1499 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001500 currentGestureIdBits.clear();
1501 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001502 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001503 downTime = 0;
1504 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001505 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001506 resetQuietTime();
1507 }
1508
Jeff Brown79ac9692011-04-19 21:20:10 -07001509 void resetTap() {
1510 tapDownTime = LLONG_MIN;
1511 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001512 }
1513
1514 void resetQuietTime() {
1515 quietTime = LLONG_MIN;
1516 }
1517 } mPointerGesture;
1518
Jeff Brown65fd2512011-08-18 11:20:58 -07001519 struct PointerSimple {
1520 PointerCoords currentCoords;
1521 PointerProperties currentProperties;
1522 PointerCoords lastCoords;
1523 PointerProperties lastProperties;
1524
1525 // True if the pointer is down.
1526 bool down;
1527
1528 // True if the pointer is hovering.
1529 bool hovering;
1530
1531 // Time the pointer last went down.
1532 nsecs_t downTime;
1533
1534 void reset() {
1535 currentCoords.clear();
1536 currentProperties.clear();
1537 lastCoords.clear();
1538 lastProperties.clear();
1539 down = false;
1540 hovering = false;
1541 downTime = 0;
1542 }
1543 } mPointerSimple;
1544
1545 // The pointer and scroll velocity controls.
1546 VelocityControl mPointerVelocityControl;
1547 VelocityControl mWheelXVelocityControl;
1548 VelocityControl mWheelYVelocityControl;
1549
1550 void sync(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001551
Jeff Brownbe1aa822011-07-27 16:04:54 -07001552 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1553 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1554 int32_t keyEventAction, int32_t keyEventFlags);
1555
Jeff Brown6d0fec22010-07-23 21:28:06 -07001556 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001557 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1558 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1559 void cookPointerData();
1560
Jeff Brown65fd2512011-08-18 11:20:58 -07001561 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1562 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1563
Jeff Brown79ac9692011-04-19 21:20:10 -07001564 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
Jeff Brown65fd2512011-08-18 11:20:58 -07001565 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Jeff Brown79ac9692011-04-19 21:20:10 -07001566 bool preparePointerGestures(nsecs_t when,
Jeff Brown65fd2512011-08-18 11:20:58 -07001567 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1568 bool isTimeout);
1569
1570 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1571 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1572
1573 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1574 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1575
1576 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1577 bool down, bool hovering);
1578 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001579
1580 // Dispatches a motion event.
1581 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1582 // method will take care of setting the index and transmuting the action to DOWN or UP
1583 // it is the first / last pointer to go down / up.
1584 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001585 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1586 int32_t edgeFlags,
1587 const PointerProperties* properties, const PointerCoords* coords,
1588 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001589 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1590
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001591 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001592 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001593 bool updateMovedPointers(const PointerProperties* inProperties,
1594 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1595 PointerProperties* outProperties, PointerCoords* outCoords,
1596 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001597
Jeff Brownbe1aa822011-07-27 16:04:54 -07001598 bool isPointInsideSurface(int32_t x, int32_t y);
1599 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001600
Jeff Brownbe1aa822011-07-27 16:04:54 -07001601 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001602};
1603
1604
1605class SingleTouchInputMapper : public TouchInputMapper {
1606public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001607 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001608 virtual ~SingleTouchInputMapper();
1609
Jeff Brown65fd2512011-08-18 11:20:58 -07001610 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001611 virtual void process(const RawEvent* rawEvent);
1612
1613protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001614 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001615 virtual void configureRawPointerAxes();
Jeff Brown00710e92012-04-19 15:18:26 -07001616 virtual bool hasStylus() const;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001617
1618private:
Jeff Brown49754db2011-07-01 17:37:58 -07001619 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001620};
1621
1622
1623class MultiTouchInputMapper : public TouchInputMapper {
1624public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001625 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001626 virtual ~MultiTouchInputMapper();
1627
Jeff Brown65fd2512011-08-18 11:20:58 -07001628 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001629 virtual void process(const RawEvent* rawEvent);
1630
1631protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001632 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001633 virtual void configureRawPointerAxes();
Jeff Brown00710e92012-04-19 15:18:26 -07001634 virtual bool hasStylus() const;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001635
1636private:
Jeff Brown49754db2011-07-01 17:37:58 -07001637 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001638
Jeff Brown6894a292011-07-01 17:59:27 -07001639 // Specifies the pointer id bits that are in use, and their associated tracking id.
1640 BitSet32 mPointerIdBits;
1641 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001642};
1643
Jeff Browncb1404e2011-01-15 18:14:15 -08001644
1645class JoystickInputMapper : public InputMapper {
1646public:
1647 JoystickInputMapper(InputDevice* device);
1648 virtual ~JoystickInputMapper();
1649
1650 virtual uint32_t getSources();
1651 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1652 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001653 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1654 virtual void reset(nsecs_t when);
Jeff Browncb1404e2011-01-15 18:14:15 -08001655 virtual void process(const RawEvent* rawEvent);
1656
1657private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001658 struct Axis {
1659 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001660 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001661
Jeff Brown6f2fba42011-02-19 01:08:02 -08001662 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001663
Jeff Brown6f2fba42011-02-19 01:08:02 -08001664 float scale; // scale factor from raw to normalized values
1665 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001666 float highScale; // scale factor from raw to normalized values of high split
1667 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001668
Jeff Brown6f2fba42011-02-19 01:08:02 -08001669 float min; // normalized inclusive minimum
1670 float max; // normalized inclusive maximum
1671 float flat; // normalized flat region size
1672 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001673
Jeff Brown6f2fba42011-02-19 01:08:02 -08001674 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001675 float currentValue; // current value
1676 float newValue; // most recent value
1677 float highCurrentValue; // current value of high split
1678 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001679
Jeff Brown85297452011-03-04 13:07:49 -08001680 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1681 bool explicitlyMapped, float scale, float offset,
1682 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001683 float min, float max, float flat, float fuzz) {
1684 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001685 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001686 this->explicitlyMapped = explicitlyMapped;
1687 this->scale = scale;
1688 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001689 this->highScale = highScale;
1690 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001691 this->min = min;
1692 this->max = max;
1693 this->flat = flat;
1694 this->fuzz = fuzz;
1695 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001696 resetValue();
1697 }
1698
1699 void resetValue() {
1700 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001701 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001702 this->highCurrentValue = 0;
1703 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001704 }
1705 };
1706
Jeff Brown6f2fba42011-02-19 01:08:02 -08001707 // Axes indexed by raw ABS_* axis index.
1708 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001709
Jeff Brown6f2fba42011-02-19 01:08:02 -08001710 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001711
Jeff Brown85297452011-03-04 13:07:49 -08001712 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001713 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001714 bool filterAxes(bool force);
1715
1716 static bool hasValueChangedSignificantly(float filter,
1717 float newValue, float currentValue, float min, float max);
1718 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1719 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001720
Jeff Brown6f2fba42011-02-19 01:08:02 -08001721 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001722};
1723
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001724} // namespace android
1725
1726#endif // _UI_INPUT_READER_H