blob: e5897e7051eeca4b6d9505c62926e46ee546918a [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 Brown474dcb52011-06-14 20:22:50 -070073 // All devices must be reopened.
74 CHANGE_MUST_REOPEN = 1 << 31,
75 };
76
Jeff Brown214eaf42011-05-26 19:17:02 -070077 // Gets the amount of time to disable virtual keys after the screen is touched
78 // in order to filter out accidental virtual key presses due to swiping gestures
79 // or taps near the edge of the display. May be 0 to disable the feature.
80 nsecs_t virtualKeyQuietTime;
81
82 // The excluded device names for the platform.
83 // Devices with these names will be ignored.
84 Vector<String8> excludedDeviceNames;
85
Jeff Brown19c97d462011-06-01 12:33:19 -070086 // Velocity control parameters for mouse pointer movements.
87 VelocityControlParameters pointerVelocityControlParameters;
88
89 // Velocity control parameters for mouse wheel movements.
90 VelocityControlParameters wheelVelocityControlParameters;
91
Jeff Brown474dcb52011-06-14 20:22:50 -070092 // True if pointer gestures are enabled.
93 bool pointerGesturesEnabled;
94
Jeff Brown214eaf42011-05-26 19:17:02 -070095 // Quiet time between certain pointer gesture transitions.
96 // Time to allow for all fingers or buttons to settle into a stable state before
97 // starting a new gesture.
98 nsecs_t pointerGestureQuietInterval;
99
100 // The minimum speed that a pointer must travel for us to consider switching the active
101 // touch pointer to it during a drag. This threshold is set to avoid switching due
102 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
103 float pointerGestureDragMinSwitchSpeed; // in pixels per second
104
105 // Tap gesture delay time.
106 // The time between down and up must be less than this to be considered a tap.
107 nsecs_t pointerGestureTapInterval;
108
109 // Tap drag gesture delay time.
110 // The time between the previous tap's up and the next down must be less than
111 // this to be considered a drag. Otherwise, the previous tap is finished and a
112 // new tap begins.
113 //
114 // Note that the previous tap will be held down for this entire duration so this
115 // interval must be shorter than the long press timeout.
116 nsecs_t pointerGestureTapDragInterval;
117
118 // The distance in pixels that the pointer is allowed to move from initial down
119 // to up and still be called a tap.
120 float pointerGestureTapSlop; // in pixels
121
122 // Time after the first touch points go down to settle on an initial centroid.
123 // This is intended to be enough time to handle cases where the user puts down two
124 // fingers at almost but not quite exactly the same time.
125 nsecs_t pointerGestureMultitouchSettleInterval;
126
127 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700128 // at least two pointers have moved at least this far from their starting place.
129 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700130
131 // The transition from PRESS to SWIPE gesture mode can only occur when the
132 // cosine of the angle between the two vectors is greater than or equal to than this value
133 // which indicates that the vectors are oriented in the same direction.
134 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
135 // (In exactly opposite directions, the cosine is -1.0.)
136 float pointerGestureSwipeTransitionAngleCosine;
137
138 // The transition from PRESS to SWIPE gesture mode can only occur when the
139 // fingers are no more than this far apart relative to the diagonal size of
140 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
141 // no more than half the diagonal size of the touch pad apart.
142 float pointerGestureSwipeMaxWidthRatio;
143
144 // The gesture movement speed factor relative to the size of the display.
145 // Movement speed applies when the fingers are moving in the same direction.
146 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
147 // will cover this portion of the display diagonal.
148 float pointerGestureMovementSpeedRatio;
149
150 // The gesture zoom speed factor relative to the size of the display.
151 // Zoom speed applies when the fingers are mostly moving relative to each other
152 // to execute a scale gesture or similar.
153 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
154 // will cover this portion of the display diagonal.
155 float pointerGestureZoomSpeedRatio;
156
Jeff Browndaf4a122011-08-26 17:14:14 -0700157 // True to show the location of touches on the touch screen as spots.
158 bool showTouches;
159
Jeff Brown214eaf42011-05-26 19:17:02 -0700160 InputReaderConfiguration() :
Jeff Brown214eaf42011-05-26 19:17:02 -0700161 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700162 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700163 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700164 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700165 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
166 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
167 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
168 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
169 pointerGestureTapSlop(10.0f), // 10 pixels
170 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700171 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700172 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700173 pointerGestureSwipeMaxWidthRatio(0.25f),
174 pointerGestureMovementSpeedRatio(0.8f),
Jeff Browndaf4a122011-08-26 17:14:14 -0700175 pointerGestureZoomSpeedRatio(0.3f),
176 showTouches(false) { }
Jeff Brown65fd2512011-08-18 11:20:58 -0700177
178 bool getDisplayInfo(int32_t displayId, bool external,
179 int32_t* width, int32_t* height, int32_t* orientation) const;
180
181 void setDisplayInfo(int32_t displayId, bool external,
182 int32_t width, int32_t height, int32_t orientation);
183
184private:
185 struct DisplayInfo {
186 int32_t width;
187 int32_t height;
188 int32_t orientation;
189
190 DisplayInfo() :
191 width(-1), height(-1), orientation(DISPLAY_ORIENTATION_0) {
192 }
193 };
194
195 DisplayInfo mInternalDisplay;
196 DisplayInfo mExternalDisplay;
Jeff Brown214eaf42011-05-26 19:17:02 -0700197};
198
199
200/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700201 * Input reader policy interface.
202 *
203 * The input reader policy is used by the input reader to interact with the Window Manager
204 * and other system components.
205 *
206 * The actual implementation is partially supported by callbacks into the DVM
207 * via JNI. This interface is also mocked in the unit tests.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700208 *
209 * These methods must NOT re-enter the input reader since they may be called while
210 * holding the input reader lock.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700211 */
212class InputReaderPolicyInterface : public virtual RefBase {
213protected:
214 InputReaderPolicyInterface() { }
215 virtual ~InputReaderPolicyInterface() { }
216
217public:
Jeff Brown214eaf42011-05-26 19:17:02 -0700218 /* Gets the input reader configuration. */
219 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800220
221 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
222 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700223
224 /* Notifies the input reader policy that some input devices have changed
225 * and provides information about all current input devices.
226 */
227 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700228
229 /* Gets the keyboard layout for a particular input device. */
230 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700231};
232
233
Jeff Brownbe1aa822011-07-27 16:04:54 -0700234/* Processes raw input events and sends cooked event data to an input listener. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235class InputReaderInterface : public virtual RefBase {
236protected:
237 InputReaderInterface() { }
238 virtual ~InputReaderInterface() { }
239
240public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700241 /* Dumps the state of the input reader.
242 *
243 * This method may be called on any thread (usually by the input manager). */
244 virtual void dump(String8& dump) = 0;
245
Jeff Brown89ef0722011-08-10 16:25:21 -0700246 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
247 virtual void monitor() = 0;
248
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700249 /* Runs a single iteration of the processing loop.
250 * Nominally reads and processes one incoming message from the EventHub.
251 *
252 * This method should be called on the input reader thread.
253 */
254 virtual void loopOnce() = 0;
255
Jeff Brown9c3cda02010-06-15 01:31:58 -0700256 /* Gets the current input device configuration.
257 *
258 * This method may be called on any thread (usually by the input manager).
259 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700260 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700261
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 Brown6d0fec22010-07-23 21:28:06 -0700341 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700342 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700343
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
345 int32_t scanCode);
346 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
347 int32_t keyCode);
348 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
349 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700350
Jeff Brown6d0fec22010-07-23 21:28:06 -0700351 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
352 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353
Jeff Brown474dcb52011-06-14 20:22:50 -0700354 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700355
Jeff Browna47425a2012-04-13 04:09:27 -0700356 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
357 ssize_t repeat, int32_t token);
358 virtual void cancelVibrate(int32_t deviceId, int32_t token);
359
Jeff Brownc3db8582010-10-20 15:33:38 -0700360protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700361 // These members are protected so they can be instrumented by test cases.
362 virtual InputDevice* createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700363 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700364
365 class ContextImpl : public InputReaderContext {
366 InputReader* mReader;
367
368 public:
369 ContextImpl(InputReader* reader);
370
371 virtual void updateGlobalMetaState();
372 virtual int32_t getGlobalMetaState();
373 virtual void disableVirtualKeysUntil(nsecs_t time);
374 virtual bool shouldDropVirtualKey(nsecs_t now,
375 InputDevice* device, int32_t keyCode, int32_t scanCode);
376 virtual void fadePointer();
377 virtual void requestTimeoutAtTime(nsecs_t when);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700378 virtual int32_t bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700379 virtual InputReaderPolicyInterface* getPolicy();
380 virtual InputListenerInterface* getListener();
381 virtual EventHubInterface* getEventHub();
382 } mContext;
383
384 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700385
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700386private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700387 Mutex mLock;
388
Jeff Brown112b5f52012-01-27 17:32:06 -0800389 Condition mReaderIsAliveCondition;
390
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700391 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700392 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700393 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700394
Jeff Brown214eaf42011-05-26 19:17:02 -0700395 InputReaderConfiguration mConfig;
396
Jeff Brownb7198742011-03-18 18:14:26 -0700397 // The event queue.
398 static const int EVENT_BUFFER_SIZE = 256;
399 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
400
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700401 KeyedVector<int32_t, InputDevice*> mDevices;
402
Jeff Brown6d0fec22010-07-23 21:28:06 -0700403 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700404 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700405
Jeff Brown65fd2512011-08-18 11:20:58 -0700406 void addDeviceLocked(nsecs_t when, int32_t deviceId);
407 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700408 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
409 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700410
Jeff Brownbe1aa822011-07-27 16:04:54 -0700411 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700412
Jeff Brownbe1aa822011-07-27 16:04:54 -0700413 int32_t mGlobalMetaState;
414 void updateGlobalMetaStateLocked();
415 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416
Jeff Brownbe1aa822011-07-27 16:04:54 -0700417 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700418
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700419 int32_t mGeneration;
420 int32_t bumpGenerationLocked();
421
Jeff Brownbe1aa822011-07-27 16:04:54 -0700422 InputConfiguration mInputConfiguration;
423 void updateInputConfigurationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800424
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700425 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
426
Jeff Brownbe1aa822011-07-27 16:04:54 -0700427 nsecs_t mDisableVirtualKeysTimeout;
428 void disableVirtualKeysUntilLocked(nsecs_t time);
429 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800430 InputDevice* device, int32_t keyCode, int32_t scanCode);
431
Jeff Brownbe1aa822011-07-27 16:04:54 -0700432 nsecs_t mNextTimeout;
433 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700434
Jeff Brownbe1aa822011-07-27 16:04:54 -0700435 uint32_t mConfigurationChangesToRefresh;
436 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700437
Jeff Brown6d0fec22010-07-23 21:28:06 -0700438 // state queries
439 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700440 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700442 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700443 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700444};
445
446
447/* Reads raw events from the event hub and processes them, endlessly. */
448class InputReaderThread : public Thread {
449public:
450 InputReaderThread(const sp<InputReaderInterface>& reader);
451 virtual ~InputReaderThread();
452
453private:
454 sp<InputReaderInterface> mReader;
455
456 virtual bool threadLoop();
457};
458
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459
460/* Represents the state of a single input device. */
461class InputDevice {
462public:
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700463 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700464 const InputDeviceIdentifier& identifier, uint32_t classes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 ~InputDevice();
466
467 inline InputReaderContext* getContext() { return mContext; }
468 inline int32_t getId() { return mId; }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700469 inline int32_t getGeneration() { return mGeneration; }
Jeff Browne38fdfa2012-04-06 14:51:01 -0700470 inline const String8& getName() { return mIdentifier.name; }
Jeff Brown9ee285af2011-08-31 12:56:34 -0700471 inline uint32_t getClasses() { return mClasses; }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 inline uint32_t getSources() { return mSources; }
473
Jeff Brown56194eb2011-03-02 19:23:13 -0800474 inline bool isExternal() { return mIsExternal; }
475 inline void setExternal(bool external) { mIsExternal = external; }
476
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477 inline bool isIgnored() { return mMappers.isEmpty(); }
478
Jeff Brownef3d7e82010-09-30 14:33:04 -0700479 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700480 void addMapper(InputMapper* mapper);
Jeff Brown65fd2512011-08-18 11:20:58 -0700481 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
482 void reset(nsecs_t when);
Jeff Brownb7198742011-03-18 18:14:26 -0700483 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700484 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700485
486 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
487 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
488 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
489 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
490 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
491 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700492 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
493 void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700494
495 int32_t getMetaState();
496
Jeff Brown05dc66a2011-03-02 14:41:58 -0800497 void fadePointer();
498
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700499 void bumpGeneration();
500
Jeff Brown65fd2512011-08-18 11:20:58 -0700501 void notifyReset(nsecs_t when);
502
Jeff Brown49754db2011-07-01 17:37:58 -0700503 inline const PropertyMap& getConfiguration() { return mConfiguration; }
504 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700505
Jeff Brown65fd2512011-08-18 11:20:58 -0700506 bool hasKey(int32_t code) {
507 return getEventHub()->hasScanCode(mId, code);
508 }
509
510 bool isKeyPressed(int32_t code) {
511 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
512 }
513
514 int32_t getAbsoluteAxisValue(int32_t code) {
515 int32_t value;
516 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
517 return value;
518 }
519
Jeff Brown6d0fec22010-07-23 21:28:06 -0700520private:
521 InputReaderContext* mContext;
522 int32_t mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700523 int32_t mGeneration;
Jeff Browne38fdfa2012-04-06 14:51:01 -0700524 InputDeviceIdentifier mIdentifier;
Jeff Brown9ee285af2011-08-31 12:56:34 -0700525 uint32_t mClasses;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700526
527 Vector<InputMapper*> mMappers;
528
Jeff Brown6d0fec22010-07-23 21:28:06 -0700529 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800530 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700531 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700532
533 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
534 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700535
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800536 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537};
538
539
Jeff Brown49754db2011-07-01 17:37:58 -0700540/* Keeps track of the state of mouse or touch pad buttons. */
541class CursorButtonAccumulator {
542public:
543 CursorButtonAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700544 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700545
Jeff Brown49754db2011-07-01 17:37:58 -0700546 void process(const RawEvent* rawEvent);
547
548 uint32_t getButtonState() const;
549
550private:
551 bool mBtnLeft;
552 bool mBtnRight;
553 bool mBtnMiddle;
554 bool mBtnBack;
555 bool mBtnSide;
556 bool mBtnForward;
557 bool mBtnExtra;
558 bool mBtnTask;
Jeff Brown65fd2512011-08-18 11:20:58 -0700559
560 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700561};
562
563
564/* Keeps track of cursor movements. */
565
566class CursorMotionAccumulator {
567public:
568 CursorMotionAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700569 void reset(InputDevice* device);
570
571 void process(const RawEvent* rawEvent);
572 void finishSync();
573
574 inline int32_t getRelativeX() const { return mRelX; }
575 inline int32_t getRelativeY() const { return mRelY; }
576
577private:
578 int32_t mRelX;
579 int32_t mRelY;
Jeff Brown49754db2011-07-01 17:37:58 -0700580
581 void clearRelativeAxes();
Jeff Brown65fd2512011-08-18 11:20:58 -0700582};
583
584
585/* Keeps track of cursor scrolling motions. */
586
587class CursorScrollAccumulator {
588public:
589 CursorScrollAccumulator();
590 void configure(InputDevice* device);
591 void reset(InputDevice* device);
592
Jeff Brown49754db2011-07-01 17:37:58 -0700593 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700594 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700595
596 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
597 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
598
599 inline int32_t getRelativeX() const { return mRelX; }
600 inline int32_t getRelativeY() const { return mRelY; }
601 inline int32_t getRelativeVWheel() const { return mRelWheel; }
602 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
603
604private:
605 bool mHaveRelWheel;
606 bool mHaveRelHWheel;
607
608 int32_t mRelX;
609 int32_t mRelY;
610 int32_t mRelWheel;
611 int32_t mRelHWheel;
Jeff Brown65fd2512011-08-18 11:20:58 -0700612
613 void clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700614};
615
616
617/* Keeps track of the state of touch, stylus and tool buttons. */
618class TouchButtonAccumulator {
619public:
620 TouchButtonAccumulator();
621 void configure(InputDevice* device);
Jeff Brown65fd2512011-08-18 11:20:58 -0700622 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700623
Jeff Brown49754db2011-07-01 17:37:58 -0700624 void process(const RawEvent* rawEvent);
625
626 uint32_t getButtonState() const;
627 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700628 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700629 bool isHovering() const;
630
631private:
632 bool mHaveBtnTouch;
633
634 bool mBtnTouch;
635 bool mBtnStylus;
636 bool mBtnStylus2;
637 bool mBtnToolFinger;
638 bool mBtnToolPen;
639 bool mBtnToolRubber;
Jeff Brown65fd2512011-08-18 11:20:58 -0700640 bool mBtnToolBrush;
641 bool mBtnToolPencil;
642 bool mBtnToolAirbrush;
643 bool mBtnToolMouse;
644 bool mBtnToolLens;
Jeff Brownea6892e2011-08-23 17:31:25 -0700645 bool mBtnToolDoubleTap;
646 bool mBtnToolTripleTap;
647 bool mBtnToolQuadTap;
Jeff Brown65fd2512011-08-18 11:20:58 -0700648
649 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700650};
651
652
Jeff Brownbe1aa822011-07-27 16:04:54 -0700653/* Raw axis information from the driver. */
654struct RawPointerAxes {
655 RawAbsoluteAxisInfo x;
656 RawAbsoluteAxisInfo y;
657 RawAbsoluteAxisInfo pressure;
658 RawAbsoluteAxisInfo touchMajor;
659 RawAbsoluteAxisInfo touchMinor;
660 RawAbsoluteAxisInfo toolMajor;
661 RawAbsoluteAxisInfo toolMinor;
662 RawAbsoluteAxisInfo orientation;
663 RawAbsoluteAxisInfo distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700664 RawAbsoluteAxisInfo tiltX;
665 RawAbsoluteAxisInfo tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700666 RawAbsoluteAxisInfo trackingId;
667 RawAbsoluteAxisInfo slot;
668
669 RawPointerAxes();
670 void clear();
671};
672
673
674/* Raw data for a collection of pointers including a pointer id mapping table. */
675struct RawPointerData {
676 struct Pointer {
677 uint32_t id;
678 int32_t x;
679 int32_t y;
680 int32_t pressure;
681 int32_t touchMajor;
682 int32_t touchMinor;
683 int32_t toolMajor;
684 int32_t toolMinor;
685 int32_t orientation;
686 int32_t distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700687 int32_t tiltX;
688 int32_t tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700689 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
690 bool isHovering;
691 };
692
693 uint32_t pointerCount;
694 Pointer pointers[MAX_POINTERS];
695 BitSet32 hoveringIdBits, touchingIdBits;
696 uint32_t idToIndex[MAX_POINTER_ID + 1];
697
698 RawPointerData();
699 void clear();
700 void copyFrom(const RawPointerData& other);
701 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
702
703 inline void markIdBit(uint32_t id, bool isHovering) {
704 if (isHovering) {
705 hoveringIdBits.markBit(id);
706 } else {
707 touchingIdBits.markBit(id);
708 }
709 }
710
711 inline void clearIdBits() {
712 hoveringIdBits.clear();
713 touchingIdBits.clear();
714 }
715
716 inline const Pointer& pointerForId(uint32_t id) const {
717 return pointers[idToIndex[id]];
718 }
719
720 inline bool isHovering(uint32_t pointerIndex) {
721 return pointers[pointerIndex].isHovering;
722 }
723};
724
725
726/* Cooked data for a collection of pointers including a pointer id mapping table. */
727struct CookedPointerData {
728 uint32_t pointerCount;
729 PointerProperties pointerProperties[MAX_POINTERS];
730 PointerCoords pointerCoords[MAX_POINTERS];
731 BitSet32 hoveringIdBits, touchingIdBits;
732 uint32_t idToIndex[MAX_POINTER_ID + 1];
733
734 CookedPointerData();
735 void clear();
736 void copyFrom(const CookedPointerData& other);
737
738 inline bool isHovering(uint32_t pointerIndex) {
739 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
740 }
741};
742
743
Jeff Brown49754db2011-07-01 17:37:58 -0700744/* Keeps track of the state of single-touch protocol. */
745class SingleTouchMotionAccumulator {
746public:
747 SingleTouchMotionAccumulator();
748
Jeff Brown49754db2011-07-01 17:37:58 -0700749 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700750 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700751
752 inline int32_t getAbsoluteX() const { return mAbsX; }
753 inline int32_t getAbsoluteY() const { return mAbsY; }
754 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
755 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
756 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
Jeff Brown65fd2512011-08-18 11:20:58 -0700757 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
758 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
Jeff Brown49754db2011-07-01 17:37:58 -0700759
760private:
761 int32_t mAbsX;
762 int32_t mAbsY;
763 int32_t mAbsPressure;
764 int32_t mAbsToolWidth;
765 int32_t mAbsDistance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700766 int32_t mAbsTiltX;
767 int32_t mAbsTiltY;
768
769 void clearAbsoluteAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700770};
771
772
773/* Keeps track of the state of multi-touch protocol. */
774class MultiTouchMotionAccumulator {
775public:
776 class Slot {
777 public:
778 inline bool isInUse() const { return mInUse; }
779 inline int32_t getX() const { return mAbsMTPositionX; }
780 inline int32_t getY() const { return mAbsMTPositionY; }
781 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
782 inline int32_t getTouchMinor() const {
783 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
784 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
785 inline int32_t getToolMinor() const {
786 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
787 inline int32_t getOrientation() const { return mAbsMTOrientation; }
788 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
789 inline int32_t getPressure() const { return mAbsMTPressure; }
790 inline int32_t getDistance() const { return mAbsMTDistance; }
791 inline int32_t getToolType() const;
792
793 private:
794 friend class MultiTouchMotionAccumulator;
795
796 bool mInUse;
797 bool mHaveAbsMTTouchMinor;
798 bool mHaveAbsMTWidthMinor;
799 bool mHaveAbsMTToolType;
800
801 int32_t mAbsMTPositionX;
802 int32_t mAbsMTPositionY;
803 int32_t mAbsMTTouchMajor;
804 int32_t mAbsMTTouchMinor;
805 int32_t mAbsMTWidthMajor;
806 int32_t mAbsMTWidthMinor;
807 int32_t mAbsMTOrientation;
808 int32_t mAbsMTTrackingId;
809 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700810 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700811 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700812
813 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700814 void clear();
815 };
816
817 MultiTouchMotionAccumulator();
818 ~MultiTouchMotionAccumulator();
819
820 void configure(size_t slotCount, bool usingSlotsProtocol);
Jeff Brown65fd2512011-08-18 11:20:58 -0700821 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700822 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700823 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700824
Jeff Brown49754db2011-07-01 17:37:58 -0700825 inline size_t getSlotCount() const { return mSlotCount; }
826 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
827
828private:
829 int32_t mCurrentSlot;
830 Slot* mSlots;
831 size_t mSlotCount;
832 bool mUsingSlotsProtocol;
Jeff Brown65fd2512011-08-18 11:20:58 -0700833
834 void clearSlots(int32_t initialSlot);
Jeff Brown49754db2011-07-01 17:37:58 -0700835};
836
837
Jeff Brown6d0fec22010-07-23 21:28:06 -0700838/* An input mapper transforms raw input events into cooked event data.
839 * A single input device can have multiple associated input mappers in order to interpret
840 * different classes of events.
Jeff Brown65fd2512011-08-18 11:20:58 -0700841 *
842 * InputMapper lifecycle:
843 * - create
844 * - configure with 0 changes
845 * - reset
846 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
847 * - reset
848 * - destroy
Jeff Brown6d0fec22010-07-23 21:28:06 -0700849 */
850class InputMapper {
851public:
852 InputMapper(InputDevice* device);
853 virtual ~InputMapper();
854
855 inline InputDevice* getDevice() { return mDevice; }
856 inline int32_t getDeviceId() { return mDevice->getId(); }
857 inline const String8 getDeviceName() { return mDevice->getName(); }
858 inline InputReaderContext* getContext() { return mContext; }
859 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700860 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700861 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
862
863 virtual uint32_t getSources() = 0;
864 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700865 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700866 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
867 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700868 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700869 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700870
871 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
872 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
873 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
874 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
875 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700876 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
877 int32_t token);
878 virtual void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700879
880 virtual int32_t getMetaState();
881
Jeff Brown05dc66a2011-03-02 14:41:58 -0800882 virtual void fadePointer();
883
Jeff Brown6d0fec22010-07-23 21:28:06 -0700884protected:
885 InputDevice* mDevice;
886 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800887
Jeff Brownbe1aa822011-07-27 16:04:54 -0700888 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700889 void bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700890
Jeff Browncb1404e2011-01-15 18:14:15 -0800891 static void dumpRawAbsoluteAxisInfo(String8& dump,
892 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700893};
894
895
896class SwitchInputMapper : public InputMapper {
897public:
898 SwitchInputMapper(InputDevice* device);
899 virtual ~SwitchInputMapper();
900
901 virtual uint32_t getSources();
902 virtual void process(const RawEvent* rawEvent);
903
904 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
905
906private:
907 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
908};
909
910
Jeff Browna47425a2012-04-13 04:09:27 -0700911class VibratorInputMapper : public InputMapper {
912public:
913 VibratorInputMapper(InputDevice* device);
914 virtual ~VibratorInputMapper();
915
916 virtual uint32_t getSources();
917 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
918 virtual void process(const RawEvent* rawEvent);
919
920 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
921 int32_t token);
922 virtual void cancelVibrate(int32_t token);
923 virtual void timeoutExpired(nsecs_t when);
924 virtual void dump(String8& dump);
925
926private:
927 bool mVibrating;
928 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
929 size_t mPatternSize;
930 ssize_t mRepeat;
931 int32_t mToken;
932 ssize_t mIndex;
933 nsecs_t mNextStepTime;
934
935 void nextStep();
936 void stopVibrating();
937};
938
939
Jeff Brown6d0fec22010-07-23 21:28:06 -0700940class KeyboardInputMapper : public InputMapper {
941public:
Jeff Brownefd32662011-03-08 15:13:06 -0800942 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700943 virtual ~KeyboardInputMapper();
944
945 virtual uint32_t getSources();
946 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700947 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700948 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
949 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700950 virtual void process(const RawEvent* rawEvent);
951
952 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
953 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
954 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
955 const int32_t* keyCodes, uint8_t* outFlags);
956
957 virtual int32_t getMetaState();
958
959private:
960 struct KeyDown {
961 int32_t keyCode;
962 int32_t scanCode;
963 };
964
Jeff Brownefd32662011-03-08 15:13:06 -0800965 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700966 int32_t mKeyboardType;
967
Jeff Brown65fd2512011-08-18 11:20:58 -0700968 int32_t mOrientation; // orientation for dpad keys
969
Jeff Brownbe1aa822011-07-27 16:04:54 -0700970 Vector<KeyDown> mKeyDowns; // keys that are down
971 int32_t mMetaState;
972 nsecs_t mDownTime; // time of most recent key down
973
Jeff Brown49ccac52012-04-11 18:27:33 -0700974 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
975
Jeff Brownbe1aa822011-07-27 16:04:54 -0700976 struct LedState {
977 bool avail; // led is available
978 bool on; // we think the led is currently on
979 };
980 LedState mCapsLockLedState;
981 LedState mNumLockLedState;
982 LedState mScrollLockLedState;
983
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800984 // Immutable configuration parameters.
985 struct Parameters {
986 int32_t associatedDisplayId;
987 bool orientationAware;
988 } mParameters;
989
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800990 void configureParameters();
991 void dumpParameters(String8& dump);
992
Jeff Brown6d0fec22010-07-23 21:28:06 -0700993 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700994
Jeff Brown6d0fec22010-07-23 21:28:06 -0700995 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
996 uint32_t policyFlags);
997
Jeff Brownbe1aa822011-07-27 16:04:54 -0700998 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700999
Jeff Brownbe1aa822011-07-27 16:04:54 -07001000 void resetLedState();
1001 void initializeLedState(LedState& ledState, int32_t led);
1002 void updateLedState(bool reset);
1003 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -07001004 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005};
1006
1007
Jeff Brown83c09682010-12-23 17:50:18 -08001008class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001009public:
Jeff Brown83c09682010-12-23 17:50:18 -08001010 CursorInputMapper(InputDevice* device);
1011 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001012
1013 virtual uint32_t getSources();
1014 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001015 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001016 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1017 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001018 virtual void process(const RawEvent* rawEvent);
1019
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001020 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1021
Jeff Brown05dc66a2011-03-02 14:41:58 -08001022 virtual void fadePointer();
1023
Jeff Brown6d0fec22010-07-23 21:28:06 -07001024private:
1025 // Amount that trackball needs to move in order to generate a key event.
1026 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1027
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001028 // Immutable configuration parameters.
1029 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -08001030 enum Mode {
1031 MODE_POINTER,
1032 MODE_NAVIGATION,
1033 };
1034
1035 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001036 int32_t associatedDisplayId;
1037 bool orientationAware;
1038 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001039
Jeff Brown49754db2011-07-01 17:37:58 -07001040 CursorButtonAccumulator mCursorButtonAccumulator;
1041 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown65fd2512011-08-18 11:20:58 -07001042 CursorScrollAccumulator mCursorScrollAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001043
Jeff Brownefd32662011-03-08 15:13:06 -08001044 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001045 float mXScale;
1046 float mYScale;
1047 float mXPrecision;
1048 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001049
Jeff Brown6f2fba42011-02-19 01:08:02 -08001050 float mVWheelScale;
1051 float mHWheelScale;
1052
Jeff Brown19c97d462011-06-01 12:33:19 -07001053 // Velocity controls for mouse pointer and wheel movements.
1054 // The controls for X and Y wheel movements are separate to keep them decoupled.
1055 VelocityControl mPointerVelocityControl;
1056 VelocityControl mWheelXVelocityControl;
1057 VelocityControl mWheelYVelocityControl;
1058
Jeff Brown65fd2512011-08-18 11:20:58 -07001059 int32_t mOrientation;
1060
Jeff Brown83c09682010-12-23 17:50:18 -08001061 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001062
Jeff Brownbe1aa822011-07-27 16:04:54 -07001063 int32_t mButtonState;
1064 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001065
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001066 void configureParameters();
1067 void dumpParameters(String8& dump);
1068
Jeff Brown6d0fec22010-07-23 21:28:06 -07001069 void sync(nsecs_t when);
1070};
1071
1072
1073class TouchInputMapper : public InputMapper {
1074public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001075 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001076 virtual ~TouchInputMapper();
1077
1078 virtual uint32_t getSources();
1079 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001080 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001081 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1082 virtual void reset(nsecs_t when);
1083 virtual void process(const RawEvent* rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001084
1085 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1086 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1087 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1088 const int32_t* keyCodes, uint8_t* outFlags);
1089
Jeff Brownace13b12011-03-09 17:39:48 -08001090 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -07001091 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -08001092
Jeff Brown6d0fec22010-07-23 21:28:06 -07001093protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001094 CursorButtonAccumulator mCursorButtonAccumulator;
1095 CursorScrollAccumulator mCursorScrollAccumulator;
1096 TouchButtonAccumulator mTouchButtonAccumulator;
1097
Jeff Brown6d0fec22010-07-23 21:28:06 -07001098 struct VirtualKey {
1099 int32_t keyCode;
1100 int32_t scanCode;
1101 uint32_t flags;
1102
1103 // computed hit box, specified in touch screen coords based on known display size
1104 int32_t hitLeft;
1105 int32_t hitTop;
1106 int32_t hitRight;
1107 int32_t hitBottom;
1108
1109 inline bool isHit(int32_t x, int32_t y) const {
1110 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1111 }
1112 };
1113
Jeff Brown65fd2512011-08-18 11:20:58 -07001114 // Input sources and device mode.
1115 uint32_t mSource;
1116
1117 enum DeviceMode {
1118 DEVICE_MODE_DISABLED, // input is disabled
1119 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1120 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1121 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1122 };
1123 DeviceMode mDeviceMode;
Jeff Brown83c09682010-12-23 17:50:18 -08001124
Jeff Brown214eaf42011-05-26 19:17:02 -07001125 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001126 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -07001127
Jeff Brown6d0fec22010-07-23 21:28:06 -07001128 // Immutable configuration parameters.
1129 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001130 enum DeviceType {
1131 DEVICE_TYPE_TOUCH_SCREEN,
1132 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -08001133 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001134 };
1135
1136 DeviceType deviceType;
1137 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -07001138 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001139 bool orientationAware;
1140
Jeff Brown2352b972011-04-12 22:39:53 -07001141 enum GestureMode {
1142 GESTURE_MODE_POINTER,
1143 GESTURE_MODE_SPOTS,
1144 };
1145 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001146 } mParameters;
1147
Jeff Brown8d608662010-08-30 03:02:23 -07001148 // Immutable calibration parameters in parsed form.
1149 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -07001150 // Size
1151 enum SizeCalibration {
1152 SIZE_CALIBRATION_DEFAULT,
1153 SIZE_CALIBRATION_NONE,
1154 SIZE_CALIBRATION_GEOMETRIC,
1155 SIZE_CALIBRATION_DIAMETER,
1156 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -07001157 };
1158
Jeff Browna1f89ce2011-08-11 00:05:01 -07001159 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001160
Jeff Browna1f89ce2011-08-11 00:05:01 -07001161 bool haveSizeScale;
1162 float sizeScale;
1163 bool haveSizeBias;
1164 float sizeBias;
1165 bool haveSizeIsSummed;
1166 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001167
1168 // Pressure
1169 enum PressureCalibration {
1170 PRESSURE_CALIBRATION_DEFAULT,
1171 PRESSURE_CALIBRATION_NONE,
1172 PRESSURE_CALIBRATION_PHYSICAL,
1173 PRESSURE_CALIBRATION_AMPLITUDE,
1174 };
Jeff Brown8d608662010-08-30 03:02:23 -07001175
1176 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001177 bool havePressureScale;
1178 float pressureScale;
1179
Jeff Brown8d608662010-08-30 03:02:23 -07001180 // Orientation
1181 enum OrientationCalibration {
1182 ORIENTATION_CALIBRATION_DEFAULT,
1183 ORIENTATION_CALIBRATION_NONE,
1184 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001185 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001186 };
1187
1188 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001189
1190 // Distance
1191 enum DistanceCalibration {
1192 DISTANCE_CALIBRATION_DEFAULT,
1193 DISTANCE_CALIBRATION_NONE,
1194 DISTANCE_CALIBRATION_SCALED,
1195 };
1196
1197 DistanceCalibration distanceCalibration;
1198 bool haveDistanceScale;
1199 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001200
1201 inline void applySizeScaleAndBias(float* outSize) const {
1202 if (haveSizeScale) {
1203 *outSize *= sizeScale;
1204 }
1205 if (haveSizeBias) {
1206 *outSize += sizeBias;
1207 }
1208 }
Jeff Brown8d608662010-08-30 03:02:23 -07001209 } mCalibration;
1210
Jeff Brownbe1aa822011-07-27 16:04:54 -07001211 // Raw pointer axis information from the driver.
1212 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001213
Jeff Brownbe1aa822011-07-27 16:04:54 -07001214 // Raw pointer sample data.
1215 RawPointerData mCurrentRawPointerData;
1216 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001217
Jeff Brownbe1aa822011-07-27 16:04:54 -07001218 // Cooked pointer sample data.
1219 CookedPointerData mCurrentCookedPointerData;
1220 CookedPointerData mLastCookedPointerData;
1221
1222 // Button state.
1223 int32_t mCurrentButtonState;
1224 int32_t mLastButtonState;
1225
Jeff Brown65fd2512011-08-18 11:20:58 -07001226 // Scroll state.
1227 int32_t mCurrentRawVScroll;
1228 int32_t mCurrentRawHScroll;
1229
1230 // Id bits used to differentiate fingers, stylus and mouse tools.
1231 BitSet32 mCurrentFingerIdBits; // finger or unknown
1232 BitSet32 mLastFingerIdBits;
1233 BitSet32 mCurrentStylusIdBits; // stylus or eraser
1234 BitSet32 mLastStylusIdBits;
1235 BitSet32 mCurrentMouseIdBits; // mouse or lens
1236 BitSet32 mLastMouseIdBits;
1237
Jeff Brownbe1aa822011-07-27 16:04:54 -07001238 // True if we sent a HOVER_ENTER event.
1239 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001240
1241 // The time the primary pointer last went down.
1242 nsecs_t mDownTime;
1243
Jeff Brownace13b12011-03-09 17:39:48 -08001244 // The pointer controller, or null if the device is not a pointer.
1245 sp<PointerControllerInterface> mPointerController;
1246
Jeff Brownbe1aa822011-07-27 16:04:54 -07001247 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001248
Jeff Brown8d608662010-08-30 03:02:23 -07001249 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001250 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001251 virtual void configureRawPointerAxes();
1252 virtual void dumpRawPointerAxes(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001253 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001254 virtual void dumpSurface(String8& dump);
1255 virtual void configureVirtualKeys();
1256 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001257 virtual void parseCalibration();
1258 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001259 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001260
Jeff Brown65fd2512011-08-18 11:20:58 -07001261 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001262
1263private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001264 // The surface orientation and width and height set by configureSurface().
1265 int32_t mSurfaceOrientation;
1266 int32_t mSurfaceWidth;
1267 int32_t mSurfaceHeight;
1268
1269 // The associated display orientation and width and height set by configureSurface().
1270 int32_t mAssociatedDisplayOrientation;
1271 int32_t mAssociatedDisplayWidth;
1272 int32_t mAssociatedDisplayHeight;
1273
1274 // Translation and scaling factors, orientation-independent.
1275 float mXScale;
1276 float mXPrecision;
1277
1278 float mYScale;
1279 float mYPrecision;
1280
1281 float mGeometricScale;
1282
Jeff Brownbe1aa822011-07-27 16:04:54 -07001283 float mPressureScale;
1284
1285 float mSizeScale;
1286
Jeff Brown65fd2512011-08-18 11:20:58 -07001287 float mOrientationCenter;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001288 float mOrientationScale;
1289
1290 float mDistanceScale;
1291
Jeff Brown65fd2512011-08-18 11:20:58 -07001292 bool mHaveTilt;
1293 float mTiltXCenter;
1294 float mTiltXScale;
1295 float mTiltYCenter;
1296 float mTiltYScale;
1297
Jeff Brownbe1aa822011-07-27 16:04:54 -07001298 // Oriented motion ranges for input device info.
1299 struct OrientedRanges {
1300 InputDeviceInfo::MotionRange x;
1301 InputDeviceInfo::MotionRange y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001302 InputDeviceInfo::MotionRange pressure;
1303
1304 bool haveSize;
1305 InputDeviceInfo::MotionRange size;
1306
1307 bool haveTouchSize;
1308 InputDeviceInfo::MotionRange touchMajor;
1309 InputDeviceInfo::MotionRange touchMinor;
1310
1311 bool haveToolSize;
1312 InputDeviceInfo::MotionRange toolMajor;
1313 InputDeviceInfo::MotionRange toolMinor;
1314
1315 bool haveOrientation;
1316 InputDeviceInfo::MotionRange orientation;
1317
1318 bool haveDistance;
1319 InputDeviceInfo::MotionRange distance;
Jeff Brown65fd2512011-08-18 11:20:58 -07001320
1321 bool haveTilt;
1322 InputDeviceInfo::MotionRange tilt;
1323
1324 OrientedRanges() {
1325 clear();
1326 }
1327
1328 void clear() {
1329 haveSize = false;
1330 haveTouchSize = false;
1331 haveToolSize = false;
1332 haveOrientation = false;
1333 haveDistance = false;
1334 haveTilt = false;
1335 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001336 } mOrientedRanges;
1337
1338 // Oriented dimensions and precision.
1339 float mOrientedSurfaceWidth;
1340 float mOrientedSurfaceHeight;
1341 float mOrientedXPrecision;
1342 float mOrientedYPrecision;
1343
1344 struct CurrentVirtualKeyState {
1345 bool down;
1346 bool ignored;
1347 nsecs_t downTime;
1348 int32_t keyCode;
1349 int32_t scanCode;
1350 } mCurrentVirtualKey;
1351
Jeff Brown65fd2512011-08-18 11:20:58 -07001352 // Scale factor for gesture or mouse based pointer movements.
1353 float mPointerXMovementScale;
1354 float mPointerYMovementScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001355
1356 // Scale factor for gesture based zooming and other freeform motions.
Jeff Brown65fd2512011-08-18 11:20:58 -07001357 float mPointerXZoomScale;
1358 float mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001359
1360 // The maximum swipe width.
1361 float mPointerGestureMaxSwipeWidth;
1362
Jeff Brown6d0fec22010-07-23 21:28:06 -07001363 struct PointerDistanceHeapElement {
1364 uint32_t currentPointerIndex : 8;
1365 uint32_t lastPointerIndex : 8;
1366 uint64_t distance : 48; // squared distance
1367 };
1368
Jeff Brown65fd2512011-08-18 11:20:58 -07001369 enum PointerUsage {
1370 POINTER_USAGE_NONE,
1371 POINTER_USAGE_GESTURES,
1372 POINTER_USAGE_STYLUS,
1373 POINTER_USAGE_MOUSE,
1374 };
1375 PointerUsage mPointerUsage;
1376
Jeff Brownace13b12011-03-09 17:39:48 -08001377 struct PointerGesture {
1378 enum Mode {
1379 // No fingers, button is not pressed.
1380 // Nothing happening.
1381 NEUTRAL,
1382
1383 // No fingers, button is not pressed.
1384 // Tap detected.
1385 // Emits DOWN and UP events at the pointer location.
1386 TAP,
1387
Jeff Brown79ac9692011-04-19 21:20:10 -07001388 // Exactly one finger dragging following a tap.
1389 // Pointer follows the active finger.
1390 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001391 //
1392 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001393 TAP_DRAG,
1394
Jeff Brownace13b12011-03-09 17:39:48 -08001395 // Button is pressed.
1396 // Pointer follows the active finger if there is one. Other fingers are ignored.
1397 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001398 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001399
1400 // Exactly one finger, button is not pressed.
1401 // Pointer follows the active finger.
1402 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001403 //
1404 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001405 HOVER,
1406
Jeff Brown2352b972011-04-12 22:39:53 -07001407 // Exactly two fingers but neither have moved enough to clearly indicate
1408 // whether a swipe or freeform gesture was intended. We consider the
1409 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1410 // Pointer does not move.
1411 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1412 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001413
1414 // Exactly two fingers moving in the same direction, button is not pressed.
1415 // Pointer does not move.
1416 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1417 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001418 SWIPE,
1419
1420 // Two or more fingers moving in arbitrary directions, button is not pressed.
1421 // Pointer does not move.
1422 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1423 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001424 FREEFORM,
1425
1426 // Waiting for quiet time to end before starting the next gesture.
1427 QUIET,
1428 };
1429
Jeff Brown2352b972011-04-12 22:39:53 -07001430 // Time the first finger went down.
1431 nsecs_t firstTouchTime;
1432
Jeff Brownace13b12011-03-09 17:39:48 -08001433 // The active pointer id from the raw touch data.
1434 int32_t activeTouchId; // -1 if none
1435
1436 // The active pointer id from the gesture last delivered to the application.
1437 int32_t activeGestureId; // -1 if none
1438
1439 // Pointer coords and ids for the current and previous pointer gesture.
1440 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001441 BitSet32 currentGestureIdBits;
1442 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001443 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001444 PointerCoords currentGestureCoords[MAX_POINTERS];
1445
1446 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001447 BitSet32 lastGestureIdBits;
1448 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001449 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001450 PointerCoords lastGestureCoords[MAX_POINTERS];
1451
Jeff Brownace13b12011-03-09 17:39:48 -08001452 // Time the pointer gesture last went down.
1453 nsecs_t downTime;
1454
Jeff Brown79ac9692011-04-19 21:20:10 -07001455 // Time when the pointer went down for a TAP.
1456 nsecs_t tapDownTime;
1457
1458 // Time when the pointer went up for a TAP.
1459 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001460
Jeff Brown2352b972011-04-12 22:39:53 -07001461 // Location of initial tap.
1462 float tapX, tapY;
1463
Jeff Brownace13b12011-03-09 17:39:48 -08001464 // Time we started waiting for quiescence.
1465 nsecs_t quietTime;
1466
Jeff Brown2352b972011-04-12 22:39:53 -07001467 // Reference points for multitouch gestures.
1468 float referenceTouchX; // reference touch X/Y coordinates in surface units
1469 float referenceTouchY;
1470 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1471 float referenceGestureY;
1472
Jeff Brown538881e2011-05-25 18:23:38 -07001473 // Distance that each pointer has traveled which has not yet been
1474 // subsumed into the reference gesture position.
1475 BitSet32 referenceIdBits;
1476 struct Delta {
1477 float dx, dy;
1478 };
1479 Delta referenceDeltas[MAX_POINTER_ID + 1];
1480
Jeff Brown2352b972011-04-12 22:39:53 -07001481 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1482 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1483
Jeff Brownace13b12011-03-09 17:39:48 -08001484 // A velocity tracker for determining whether to switch active pointers during drags.
1485 VelocityTracker velocityTracker;
1486
1487 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001488 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001489 activeTouchId = -1;
1490 activeGestureId = -1;
1491 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001492 currentGestureIdBits.clear();
1493 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001494 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001495 downTime = 0;
1496 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001497 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001498 resetQuietTime();
1499 }
1500
Jeff Brown79ac9692011-04-19 21:20:10 -07001501 void resetTap() {
1502 tapDownTime = LLONG_MIN;
1503 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001504 }
1505
1506 void resetQuietTime() {
1507 quietTime = LLONG_MIN;
1508 }
1509 } mPointerGesture;
1510
Jeff Brown65fd2512011-08-18 11:20:58 -07001511 struct PointerSimple {
1512 PointerCoords currentCoords;
1513 PointerProperties currentProperties;
1514 PointerCoords lastCoords;
1515 PointerProperties lastProperties;
1516
1517 // True if the pointer is down.
1518 bool down;
1519
1520 // True if the pointer is hovering.
1521 bool hovering;
1522
1523 // Time the pointer last went down.
1524 nsecs_t downTime;
1525
1526 void reset() {
1527 currentCoords.clear();
1528 currentProperties.clear();
1529 lastCoords.clear();
1530 lastProperties.clear();
1531 down = false;
1532 hovering = false;
1533 downTime = 0;
1534 }
1535 } mPointerSimple;
1536
1537 // The pointer and scroll velocity controls.
1538 VelocityControl mPointerVelocityControl;
1539 VelocityControl mWheelXVelocityControl;
1540 VelocityControl mWheelYVelocityControl;
1541
1542 void sync(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001543
Jeff Brownbe1aa822011-07-27 16:04:54 -07001544 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1545 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1546 int32_t keyEventAction, int32_t keyEventFlags);
1547
Jeff Brown6d0fec22010-07-23 21:28:06 -07001548 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001549 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1550 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1551 void cookPointerData();
1552
Jeff Brown65fd2512011-08-18 11:20:58 -07001553 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1554 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1555
Jeff Brown79ac9692011-04-19 21:20:10 -07001556 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
Jeff Brown65fd2512011-08-18 11:20:58 -07001557 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Jeff Brown79ac9692011-04-19 21:20:10 -07001558 bool preparePointerGestures(nsecs_t when,
Jeff Brown65fd2512011-08-18 11:20:58 -07001559 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1560 bool isTimeout);
1561
1562 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1563 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1564
1565 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1566 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1567
1568 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1569 bool down, bool hovering);
1570 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001571
1572 // Dispatches a motion event.
1573 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1574 // method will take care of setting the index and transmuting the action to DOWN or UP
1575 // it is the first / last pointer to go down / up.
1576 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001577 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1578 int32_t edgeFlags,
1579 const PointerProperties* properties, const PointerCoords* coords,
1580 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001581 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1582
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001583 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001584 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001585 bool updateMovedPointers(const PointerProperties* inProperties,
1586 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1587 PointerProperties* outProperties, PointerCoords* outCoords,
1588 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001589
Jeff Brownbe1aa822011-07-27 16:04:54 -07001590 bool isPointInsideSurface(int32_t x, int32_t y);
1591 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001592
Jeff Brownbe1aa822011-07-27 16:04:54 -07001593 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001594};
1595
1596
1597class SingleTouchInputMapper : public TouchInputMapper {
1598public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001599 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001600 virtual ~SingleTouchInputMapper();
1601
Jeff Brown65fd2512011-08-18 11:20:58 -07001602 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001603 virtual void process(const RawEvent* rawEvent);
1604
1605protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001606 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001607 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001608
1609private:
Jeff Brown49754db2011-07-01 17:37:58 -07001610 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001611};
1612
1613
1614class MultiTouchInputMapper : public TouchInputMapper {
1615public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001616 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001617 virtual ~MultiTouchInputMapper();
1618
Jeff Brown65fd2512011-08-18 11:20:58 -07001619 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001620 virtual void process(const RawEvent* rawEvent);
1621
1622protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001623 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001624 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001625
1626private:
Jeff Brown49754db2011-07-01 17:37:58 -07001627 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001628
Jeff Brown6894a292011-07-01 17:59:27 -07001629 // Specifies the pointer id bits that are in use, and their associated tracking id.
1630 BitSet32 mPointerIdBits;
1631 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001632};
1633
Jeff Browncb1404e2011-01-15 18:14:15 -08001634
1635class JoystickInputMapper : public InputMapper {
1636public:
1637 JoystickInputMapper(InputDevice* device);
1638 virtual ~JoystickInputMapper();
1639
1640 virtual uint32_t getSources();
1641 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1642 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001643 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1644 virtual void reset(nsecs_t when);
Jeff Browncb1404e2011-01-15 18:14:15 -08001645 virtual void process(const RawEvent* rawEvent);
1646
1647private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001648 struct Axis {
1649 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001650 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001651
Jeff Brown6f2fba42011-02-19 01:08:02 -08001652 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001653
Jeff Brown6f2fba42011-02-19 01:08:02 -08001654 float scale; // scale factor from raw to normalized values
1655 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001656 float highScale; // scale factor from raw to normalized values of high split
1657 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001658
Jeff Brown6f2fba42011-02-19 01:08:02 -08001659 float min; // normalized inclusive minimum
1660 float max; // normalized inclusive maximum
1661 float flat; // normalized flat region size
1662 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001663
Jeff Brown6f2fba42011-02-19 01:08:02 -08001664 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001665 float currentValue; // current value
1666 float newValue; // most recent value
1667 float highCurrentValue; // current value of high split
1668 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001669
Jeff Brown85297452011-03-04 13:07:49 -08001670 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1671 bool explicitlyMapped, float scale, float offset,
1672 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001673 float min, float max, float flat, float fuzz) {
1674 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001675 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001676 this->explicitlyMapped = explicitlyMapped;
1677 this->scale = scale;
1678 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001679 this->highScale = highScale;
1680 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001681 this->min = min;
1682 this->max = max;
1683 this->flat = flat;
1684 this->fuzz = fuzz;
1685 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001686 resetValue();
1687 }
1688
1689 void resetValue() {
1690 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001691 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001692 this->highCurrentValue = 0;
1693 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001694 }
1695 };
1696
Jeff Brown6f2fba42011-02-19 01:08:02 -08001697 // Axes indexed by raw ABS_* axis index.
1698 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001699
Jeff Brown6f2fba42011-02-19 01:08:02 -08001700 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001701
Jeff Brown85297452011-03-04 13:07:49 -08001702 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001703 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001704 bool filterAxes(bool force);
1705
1706 static bool hasValueChangedSignificantly(float filter,
1707 float newValue, float currentValue, float min, float max);
1708 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1709 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001710
Jeff Brown6f2fba42011-02-19 01:08:02 -08001711 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001712};
1713
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001714} // namespace android
1715
1716#endif // _UI_INPUT_READER_H