blob: acdec85a6ae5c82af9be9889608f04d69a256306 [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
Jeff Brown00710e92012-04-19 15:18:26 -0700510 bool hasAbsoluteAxis(int32_t code) {
511 RawAbsoluteAxisInfo info;
512 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
513 return info.valid;
514 }
515
Jeff Brown65fd2512011-08-18 11:20:58 -0700516 bool isKeyPressed(int32_t code) {
517 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
518 }
519
520 int32_t getAbsoluteAxisValue(int32_t code) {
521 int32_t value;
522 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
523 return value;
524 }
525
Jeff Brown6d0fec22010-07-23 21:28:06 -0700526private:
527 InputReaderContext* mContext;
528 int32_t mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700529 int32_t mGeneration;
Jeff Browne38fdfa2012-04-06 14:51:01 -0700530 InputDeviceIdentifier mIdentifier;
Jeff Brown9ee285af2011-08-31 12:56:34 -0700531 uint32_t mClasses;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700532
533 Vector<InputMapper*> mMappers;
534
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800536 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700537 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700538
539 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
540 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700541
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800542 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700543};
544
545
Jeff Brown49754db2011-07-01 17:37:58 -0700546/* Keeps track of the state of mouse or touch pad buttons. */
547class CursorButtonAccumulator {
548public:
549 CursorButtonAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700550 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700551
Jeff Brown49754db2011-07-01 17:37:58 -0700552 void process(const RawEvent* rawEvent);
553
554 uint32_t getButtonState() const;
555
556private:
557 bool mBtnLeft;
558 bool mBtnRight;
559 bool mBtnMiddle;
560 bool mBtnBack;
561 bool mBtnSide;
562 bool mBtnForward;
563 bool mBtnExtra;
564 bool mBtnTask;
Jeff Brown65fd2512011-08-18 11:20:58 -0700565
566 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700567};
568
569
570/* Keeps track of cursor movements. */
571
572class CursorMotionAccumulator {
573public:
574 CursorMotionAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700575 void reset(InputDevice* device);
576
577 void process(const RawEvent* rawEvent);
578 void finishSync();
579
580 inline int32_t getRelativeX() const { return mRelX; }
581 inline int32_t getRelativeY() const { return mRelY; }
582
583private:
584 int32_t mRelX;
585 int32_t mRelY;
Jeff Brown49754db2011-07-01 17:37:58 -0700586
587 void clearRelativeAxes();
Jeff Brown65fd2512011-08-18 11:20:58 -0700588};
589
590
591/* Keeps track of cursor scrolling motions. */
592
593class CursorScrollAccumulator {
594public:
595 CursorScrollAccumulator();
596 void configure(InputDevice* device);
597 void reset(InputDevice* device);
598
Jeff Brown49754db2011-07-01 17:37:58 -0700599 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700600 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700601
602 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
603 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
604
605 inline int32_t getRelativeX() const { return mRelX; }
606 inline int32_t getRelativeY() const { return mRelY; }
607 inline int32_t getRelativeVWheel() const { return mRelWheel; }
608 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
609
610private:
611 bool mHaveRelWheel;
612 bool mHaveRelHWheel;
613
614 int32_t mRelX;
615 int32_t mRelY;
616 int32_t mRelWheel;
617 int32_t mRelHWheel;
Jeff Brown65fd2512011-08-18 11:20:58 -0700618
619 void clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700620};
621
622
623/* Keeps track of the state of touch, stylus and tool buttons. */
624class TouchButtonAccumulator {
625public:
626 TouchButtonAccumulator();
627 void configure(InputDevice* device);
Jeff Brown65fd2512011-08-18 11:20:58 -0700628 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700629
Jeff Brown49754db2011-07-01 17:37:58 -0700630 void process(const RawEvent* rawEvent);
631
632 uint32_t getButtonState() const;
633 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700634 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700635 bool isHovering() const;
Jeff Brown00710e92012-04-19 15:18:26 -0700636 bool hasStylus() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700637
638private:
639 bool mHaveBtnTouch;
Jeff Brown00710e92012-04-19 15:18:26 -0700640 bool mHaveStylus;
Jeff Brown49754db2011-07-01 17:37:58 -0700641
642 bool mBtnTouch;
643 bool mBtnStylus;
644 bool mBtnStylus2;
645 bool mBtnToolFinger;
646 bool mBtnToolPen;
647 bool mBtnToolRubber;
Jeff Brown65fd2512011-08-18 11:20:58 -0700648 bool mBtnToolBrush;
649 bool mBtnToolPencil;
650 bool mBtnToolAirbrush;
651 bool mBtnToolMouse;
652 bool mBtnToolLens;
Jeff Brownea6892e2011-08-23 17:31:25 -0700653 bool mBtnToolDoubleTap;
654 bool mBtnToolTripleTap;
655 bool mBtnToolQuadTap;
Jeff Brown65fd2512011-08-18 11:20:58 -0700656
657 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700658};
659
660
Jeff Brownbe1aa822011-07-27 16:04:54 -0700661/* Raw axis information from the driver. */
662struct RawPointerAxes {
663 RawAbsoluteAxisInfo x;
664 RawAbsoluteAxisInfo y;
665 RawAbsoluteAxisInfo pressure;
666 RawAbsoluteAxisInfo touchMajor;
667 RawAbsoluteAxisInfo touchMinor;
668 RawAbsoluteAxisInfo toolMajor;
669 RawAbsoluteAxisInfo toolMinor;
670 RawAbsoluteAxisInfo orientation;
671 RawAbsoluteAxisInfo distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700672 RawAbsoluteAxisInfo tiltX;
673 RawAbsoluteAxisInfo tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700674 RawAbsoluteAxisInfo trackingId;
675 RawAbsoluteAxisInfo slot;
676
677 RawPointerAxes();
678 void clear();
679};
680
681
682/* Raw data for a collection of pointers including a pointer id mapping table. */
683struct RawPointerData {
684 struct Pointer {
685 uint32_t id;
686 int32_t x;
687 int32_t y;
688 int32_t pressure;
689 int32_t touchMajor;
690 int32_t touchMinor;
691 int32_t toolMajor;
692 int32_t toolMinor;
693 int32_t orientation;
694 int32_t distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700695 int32_t tiltX;
696 int32_t tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700697 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
698 bool isHovering;
699 };
700
701 uint32_t pointerCount;
702 Pointer pointers[MAX_POINTERS];
703 BitSet32 hoveringIdBits, touchingIdBits;
704 uint32_t idToIndex[MAX_POINTER_ID + 1];
705
706 RawPointerData();
707 void clear();
708 void copyFrom(const RawPointerData& other);
709 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
710
711 inline void markIdBit(uint32_t id, bool isHovering) {
712 if (isHovering) {
713 hoveringIdBits.markBit(id);
714 } else {
715 touchingIdBits.markBit(id);
716 }
717 }
718
719 inline void clearIdBits() {
720 hoveringIdBits.clear();
721 touchingIdBits.clear();
722 }
723
724 inline const Pointer& pointerForId(uint32_t id) const {
725 return pointers[idToIndex[id]];
726 }
727
728 inline bool isHovering(uint32_t pointerIndex) {
729 return pointers[pointerIndex].isHovering;
730 }
731};
732
733
734/* Cooked data for a collection of pointers including a pointer id mapping table. */
735struct CookedPointerData {
736 uint32_t pointerCount;
737 PointerProperties pointerProperties[MAX_POINTERS];
738 PointerCoords pointerCoords[MAX_POINTERS];
739 BitSet32 hoveringIdBits, touchingIdBits;
740 uint32_t idToIndex[MAX_POINTER_ID + 1];
741
742 CookedPointerData();
743 void clear();
744 void copyFrom(const CookedPointerData& other);
745
746 inline bool isHovering(uint32_t pointerIndex) {
747 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
748 }
749};
750
751
Jeff Brown49754db2011-07-01 17:37:58 -0700752/* Keeps track of the state of single-touch protocol. */
753class SingleTouchMotionAccumulator {
754public:
755 SingleTouchMotionAccumulator();
756
Jeff Brown49754db2011-07-01 17:37:58 -0700757 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700758 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700759
760 inline int32_t getAbsoluteX() const { return mAbsX; }
761 inline int32_t getAbsoluteY() const { return mAbsY; }
762 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
763 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
764 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
Jeff Brown65fd2512011-08-18 11:20:58 -0700765 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
766 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
Jeff Brown49754db2011-07-01 17:37:58 -0700767
768private:
769 int32_t mAbsX;
770 int32_t mAbsY;
771 int32_t mAbsPressure;
772 int32_t mAbsToolWidth;
773 int32_t mAbsDistance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700774 int32_t mAbsTiltX;
775 int32_t mAbsTiltY;
776
777 void clearAbsoluteAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700778};
779
780
781/* Keeps track of the state of multi-touch protocol. */
782class MultiTouchMotionAccumulator {
783public:
784 class Slot {
785 public:
786 inline bool isInUse() const { return mInUse; }
787 inline int32_t getX() const { return mAbsMTPositionX; }
788 inline int32_t getY() const { return mAbsMTPositionY; }
789 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
790 inline int32_t getTouchMinor() const {
791 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
792 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
793 inline int32_t getToolMinor() const {
794 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
795 inline int32_t getOrientation() const { return mAbsMTOrientation; }
796 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
797 inline int32_t getPressure() const { return mAbsMTPressure; }
798 inline int32_t getDistance() const { return mAbsMTDistance; }
799 inline int32_t getToolType() const;
800
801 private:
802 friend class MultiTouchMotionAccumulator;
803
804 bool mInUse;
805 bool mHaveAbsMTTouchMinor;
806 bool mHaveAbsMTWidthMinor;
807 bool mHaveAbsMTToolType;
808
809 int32_t mAbsMTPositionX;
810 int32_t mAbsMTPositionY;
811 int32_t mAbsMTTouchMajor;
812 int32_t mAbsMTTouchMinor;
813 int32_t mAbsMTWidthMajor;
814 int32_t mAbsMTWidthMinor;
815 int32_t mAbsMTOrientation;
816 int32_t mAbsMTTrackingId;
817 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700818 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700819 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700820
821 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700822 void clear();
823 };
824
825 MultiTouchMotionAccumulator();
826 ~MultiTouchMotionAccumulator();
827
Jeff Brown00710e92012-04-19 15:18:26 -0700828 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
Jeff Brown65fd2512011-08-18 11:20:58 -0700829 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700830 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700831 void finishSync();
Jeff Brown00710e92012-04-19 15:18:26 -0700832 bool hasStylus() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700833
Jeff Brown49754db2011-07-01 17:37:58 -0700834 inline size_t getSlotCount() const { return mSlotCount; }
835 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
836
837private:
838 int32_t mCurrentSlot;
839 Slot* mSlots;
840 size_t mSlotCount;
841 bool mUsingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -0700842 bool mHaveStylus;
Jeff Brown65fd2512011-08-18 11:20:58 -0700843
844 void clearSlots(int32_t initialSlot);
Jeff Brown49754db2011-07-01 17:37:58 -0700845};
846
847
Jeff Brown6d0fec22010-07-23 21:28:06 -0700848/* An input mapper transforms raw input events into cooked event data.
849 * A single input device can have multiple associated input mappers in order to interpret
850 * different classes of events.
Jeff Brown65fd2512011-08-18 11:20:58 -0700851 *
852 * InputMapper lifecycle:
853 * - create
854 * - configure with 0 changes
855 * - reset
856 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
857 * - reset
858 * - destroy
Jeff Brown6d0fec22010-07-23 21:28:06 -0700859 */
860class InputMapper {
861public:
862 InputMapper(InputDevice* device);
863 virtual ~InputMapper();
864
865 inline InputDevice* getDevice() { return mDevice; }
866 inline int32_t getDeviceId() { return mDevice->getId(); }
867 inline const String8 getDeviceName() { return mDevice->getName(); }
868 inline InputReaderContext* getContext() { return mContext; }
869 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700870 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700871 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
872
873 virtual uint32_t getSources() = 0;
874 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700875 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700876 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
877 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700878 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700879 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700880
881 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
882 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
883 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
884 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
885 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna47425a2012-04-13 04:09:27 -0700886 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
887 int32_t token);
888 virtual void cancelVibrate(int32_t token);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700889
890 virtual int32_t getMetaState();
891
Jeff Brown05dc66a2011-03-02 14:41:58 -0800892 virtual void fadePointer();
893
Jeff Brown6d0fec22010-07-23 21:28:06 -0700894protected:
895 InputDevice* mDevice;
896 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800897
Jeff Brownbe1aa822011-07-27 16:04:54 -0700898 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700899 void bumpGeneration();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700900
Jeff Browncb1404e2011-01-15 18:14:15 -0800901 static void dumpRawAbsoluteAxisInfo(String8& dump,
902 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700903};
904
905
906class SwitchInputMapper : public InputMapper {
907public:
908 SwitchInputMapper(InputDevice* device);
909 virtual ~SwitchInputMapper();
910
911 virtual uint32_t getSources();
912 virtual void process(const RawEvent* rawEvent);
913
914 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
915
916private:
917 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
918};
919
920
Jeff Browna47425a2012-04-13 04:09:27 -0700921class VibratorInputMapper : public InputMapper {
922public:
923 VibratorInputMapper(InputDevice* device);
924 virtual ~VibratorInputMapper();
925
926 virtual uint32_t getSources();
927 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
928 virtual void process(const RawEvent* rawEvent);
929
930 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
931 int32_t token);
932 virtual void cancelVibrate(int32_t token);
933 virtual void timeoutExpired(nsecs_t when);
934 virtual void dump(String8& dump);
935
936private:
937 bool mVibrating;
938 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
939 size_t mPatternSize;
940 ssize_t mRepeat;
941 int32_t mToken;
942 ssize_t mIndex;
943 nsecs_t mNextStepTime;
944
945 void nextStep();
946 void stopVibrating();
947};
948
949
Jeff Brown6d0fec22010-07-23 21:28:06 -0700950class KeyboardInputMapper : public InputMapper {
951public:
Jeff Brownefd32662011-03-08 15:13:06 -0800952 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700953 virtual ~KeyboardInputMapper();
954
955 virtual uint32_t getSources();
956 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700957 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700958 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
959 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700960 virtual void process(const RawEvent* rawEvent);
961
962 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
963 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
964 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
965 const int32_t* keyCodes, uint8_t* outFlags);
966
967 virtual int32_t getMetaState();
968
969private:
970 struct KeyDown {
971 int32_t keyCode;
972 int32_t scanCode;
973 };
974
Jeff Brownefd32662011-03-08 15:13:06 -0800975 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700976 int32_t mKeyboardType;
977
Jeff Brown65fd2512011-08-18 11:20:58 -0700978 int32_t mOrientation; // orientation for dpad keys
979
Jeff Brownbe1aa822011-07-27 16:04:54 -0700980 Vector<KeyDown> mKeyDowns; // keys that are down
981 int32_t mMetaState;
982 nsecs_t mDownTime; // time of most recent key down
983
Jeff Brown49ccac52012-04-11 18:27:33 -0700984 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
985
Jeff Brownbe1aa822011-07-27 16:04:54 -0700986 struct LedState {
987 bool avail; // led is available
988 bool on; // we think the led is currently on
989 };
990 LedState mCapsLockLedState;
991 LedState mNumLockLedState;
992 LedState mScrollLockLedState;
993
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800994 // Immutable configuration parameters.
995 struct Parameters {
996 int32_t associatedDisplayId;
997 bool orientationAware;
998 } mParameters;
999
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001000 void configureParameters();
1001 void dumpParameters(String8& dump);
1002
Jeff Brown6d0fec22010-07-23 21:28:06 -07001003 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001004
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
1006 uint32_t policyFlags);
1007
Jeff Brownbe1aa822011-07-27 16:04:54 -07001008 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001009
Jeff Brownbe1aa822011-07-27 16:04:54 -07001010 void resetLedState();
1011 void initializeLedState(LedState& ledState, int32_t led);
1012 void updateLedState(bool reset);
1013 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -07001014 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001015};
1016
1017
Jeff Brown83c09682010-12-23 17:50:18 -08001018class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019public:
Jeff Brown83c09682010-12-23 17:50:18 -08001020 CursorInputMapper(InputDevice* device);
1021 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001022
1023 virtual uint32_t getSources();
1024 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001025 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001026 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1027 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001028 virtual void process(const RawEvent* rawEvent);
1029
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001030 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1031
Jeff Brown05dc66a2011-03-02 14:41:58 -08001032 virtual void fadePointer();
1033
Jeff Brown6d0fec22010-07-23 21:28:06 -07001034private:
1035 // Amount that trackball needs to move in order to generate a key event.
1036 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1037
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001038 // Immutable configuration parameters.
1039 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -08001040 enum Mode {
1041 MODE_POINTER,
1042 MODE_NAVIGATION,
1043 };
1044
1045 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001046 int32_t associatedDisplayId;
1047 bool orientationAware;
1048 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001049
Jeff Brown49754db2011-07-01 17:37:58 -07001050 CursorButtonAccumulator mCursorButtonAccumulator;
1051 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown65fd2512011-08-18 11:20:58 -07001052 CursorScrollAccumulator mCursorScrollAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001053
Jeff Brownefd32662011-03-08 15:13:06 -08001054 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001055 float mXScale;
1056 float mYScale;
1057 float mXPrecision;
1058 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001059
Jeff Brown6f2fba42011-02-19 01:08:02 -08001060 float mVWheelScale;
1061 float mHWheelScale;
1062
Jeff Brown19c97d462011-06-01 12:33:19 -07001063 // Velocity controls for mouse pointer and wheel movements.
1064 // The controls for X and Y wheel movements are separate to keep them decoupled.
1065 VelocityControl mPointerVelocityControl;
1066 VelocityControl mWheelXVelocityControl;
1067 VelocityControl mWheelYVelocityControl;
1068
Jeff Brown65fd2512011-08-18 11:20:58 -07001069 int32_t mOrientation;
1070
Jeff Brown83c09682010-12-23 17:50:18 -08001071 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001072
Jeff Brownbe1aa822011-07-27 16:04:54 -07001073 int32_t mButtonState;
1074 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001075
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001076 void configureParameters();
1077 void dumpParameters(String8& dump);
1078
Jeff Brown6d0fec22010-07-23 21:28:06 -07001079 void sync(nsecs_t when);
1080};
1081
1082
1083class TouchInputMapper : public InputMapper {
1084public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001085 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001086 virtual ~TouchInputMapper();
1087
1088 virtual uint32_t getSources();
1089 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001090 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001091 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1092 virtual void reset(nsecs_t when);
1093 virtual void process(const RawEvent* rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094
1095 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1096 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1097 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1098 const int32_t* keyCodes, uint8_t* outFlags);
1099
Jeff Brownace13b12011-03-09 17:39:48 -08001100 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -07001101 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -08001102
Jeff Brown6d0fec22010-07-23 21:28:06 -07001103protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001104 CursorButtonAccumulator mCursorButtonAccumulator;
1105 CursorScrollAccumulator mCursorScrollAccumulator;
1106 TouchButtonAccumulator mTouchButtonAccumulator;
1107
Jeff Brown6d0fec22010-07-23 21:28:06 -07001108 struct VirtualKey {
1109 int32_t keyCode;
1110 int32_t scanCode;
1111 uint32_t flags;
1112
1113 // computed hit box, specified in touch screen coords based on known display size
1114 int32_t hitLeft;
1115 int32_t hitTop;
1116 int32_t hitRight;
1117 int32_t hitBottom;
1118
1119 inline bool isHit(int32_t x, int32_t y) const {
1120 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1121 }
1122 };
1123
Jeff Brown65fd2512011-08-18 11:20:58 -07001124 // Input sources and device mode.
1125 uint32_t mSource;
1126
1127 enum DeviceMode {
1128 DEVICE_MODE_DISABLED, // input is disabled
1129 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1130 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1131 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1132 };
1133 DeviceMode mDeviceMode;
Jeff Brown83c09682010-12-23 17:50:18 -08001134
Jeff Brown214eaf42011-05-26 19:17:02 -07001135 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001136 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -07001137
Jeff Brown6d0fec22010-07-23 21:28:06 -07001138 // Immutable configuration parameters.
1139 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001140 enum DeviceType {
1141 DEVICE_TYPE_TOUCH_SCREEN,
1142 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -08001143 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001144 };
1145
1146 DeviceType deviceType;
1147 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -07001148 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001149 bool orientationAware;
1150
Jeff Brown2352b972011-04-12 22:39:53 -07001151 enum GestureMode {
1152 GESTURE_MODE_POINTER,
1153 GESTURE_MODE_SPOTS,
1154 };
1155 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001156 } mParameters;
1157
Jeff Brown8d608662010-08-30 03:02:23 -07001158 // Immutable calibration parameters in parsed form.
1159 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -07001160 // Size
1161 enum SizeCalibration {
1162 SIZE_CALIBRATION_DEFAULT,
1163 SIZE_CALIBRATION_NONE,
1164 SIZE_CALIBRATION_GEOMETRIC,
1165 SIZE_CALIBRATION_DIAMETER,
1166 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -07001167 };
1168
Jeff Browna1f89ce2011-08-11 00:05:01 -07001169 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001170
Jeff Browna1f89ce2011-08-11 00:05:01 -07001171 bool haveSizeScale;
1172 float sizeScale;
1173 bool haveSizeBias;
1174 float sizeBias;
1175 bool haveSizeIsSummed;
1176 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001177
1178 // Pressure
1179 enum PressureCalibration {
1180 PRESSURE_CALIBRATION_DEFAULT,
1181 PRESSURE_CALIBRATION_NONE,
1182 PRESSURE_CALIBRATION_PHYSICAL,
1183 PRESSURE_CALIBRATION_AMPLITUDE,
1184 };
Jeff Brown8d608662010-08-30 03:02:23 -07001185
1186 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001187 bool havePressureScale;
1188 float pressureScale;
1189
Jeff Brown8d608662010-08-30 03:02:23 -07001190 // Orientation
1191 enum OrientationCalibration {
1192 ORIENTATION_CALIBRATION_DEFAULT,
1193 ORIENTATION_CALIBRATION_NONE,
1194 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001195 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001196 };
1197
1198 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001199
1200 // Distance
1201 enum DistanceCalibration {
1202 DISTANCE_CALIBRATION_DEFAULT,
1203 DISTANCE_CALIBRATION_NONE,
1204 DISTANCE_CALIBRATION_SCALED,
1205 };
1206
1207 DistanceCalibration distanceCalibration;
1208 bool haveDistanceScale;
1209 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001210
1211 inline void applySizeScaleAndBias(float* outSize) const {
1212 if (haveSizeScale) {
1213 *outSize *= sizeScale;
1214 }
1215 if (haveSizeBias) {
1216 *outSize += sizeBias;
1217 }
1218 }
Jeff Brown8d608662010-08-30 03:02:23 -07001219 } mCalibration;
1220
Jeff Brownbe1aa822011-07-27 16:04:54 -07001221 // Raw pointer axis information from the driver.
1222 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001223
Jeff Brownbe1aa822011-07-27 16:04:54 -07001224 // Raw pointer sample data.
1225 RawPointerData mCurrentRawPointerData;
1226 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001227
Jeff Brownbe1aa822011-07-27 16:04:54 -07001228 // Cooked pointer sample data.
1229 CookedPointerData mCurrentCookedPointerData;
1230 CookedPointerData mLastCookedPointerData;
1231
1232 // Button state.
1233 int32_t mCurrentButtonState;
1234 int32_t mLastButtonState;
1235
Jeff Brown65fd2512011-08-18 11:20:58 -07001236 // Scroll state.
1237 int32_t mCurrentRawVScroll;
1238 int32_t mCurrentRawHScroll;
1239
1240 // Id bits used to differentiate fingers, stylus and mouse tools.
1241 BitSet32 mCurrentFingerIdBits; // finger or unknown
1242 BitSet32 mLastFingerIdBits;
1243 BitSet32 mCurrentStylusIdBits; // stylus or eraser
1244 BitSet32 mLastStylusIdBits;
1245 BitSet32 mCurrentMouseIdBits; // mouse or lens
1246 BitSet32 mLastMouseIdBits;
1247
Jeff Brownbe1aa822011-07-27 16:04:54 -07001248 // True if we sent a HOVER_ENTER event.
1249 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001250
1251 // The time the primary pointer last went down.
1252 nsecs_t mDownTime;
1253
Jeff Brownace13b12011-03-09 17:39:48 -08001254 // The pointer controller, or null if the device is not a pointer.
1255 sp<PointerControllerInterface> mPointerController;
1256
Jeff Brownbe1aa822011-07-27 16:04:54 -07001257 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001258
Jeff Brown8d608662010-08-30 03:02:23 -07001259 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001260 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001261 virtual void configureRawPointerAxes();
1262 virtual void dumpRawPointerAxes(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001263 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001264 virtual void dumpSurface(String8& dump);
1265 virtual void configureVirtualKeys();
1266 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001267 virtual void parseCalibration();
1268 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001269 virtual void dumpCalibration(String8& dump);
Jeff Brown00710e92012-04-19 15:18:26 -07001270 virtual bool hasStylus() const = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001271
Jeff Brown65fd2512011-08-18 11:20:58 -07001272 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001273
1274private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001275 // The surface orientation and width and height set by configureSurface().
1276 int32_t mSurfaceOrientation;
1277 int32_t mSurfaceWidth;
1278 int32_t mSurfaceHeight;
1279
1280 // The associated display orientation and width and height set by configureSurface().
1281 int32_t mAssociatedDisplayOrientation;
1282 int32_t mAssociatedDisplayWidth;
1283 int32_t mAssociatedDisplayHeight;
1284
1285 // Translation and scaling factors, orientation-independent.
1286 float mXScale;
1287 float mXPrecision;
1288
1289 float mYScale;
1290 float mYPrecision;
1291
1292 float mGeometricScale;
1293
Jeff Brownbe1aa822011-07-27 16:04:54 -07001294 float mPressureScale;
1295
1296 float mSizeScale;
1297
Jeff Brown65fd2512011-08-18 11:20:58 -07001298 float mOrientationCenter;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001299 float mOrientationScale;
1300
1301 float mDistanceScale;
1302
Jeff Brown65fd2512011-08-18 11:20:58 -07001303 bool mHaveTilt;
1304 float mTiltXCenter;
1305 float mTiltXScale;
1306 float mTiltYCenter;
1307 float mTiltYScale;
1308
Jeff Brownbe1aa822011-07-27 16:04:54 -07001309 // Oriented motion ranges for input device info.
1310 struct OrientedRanges {
1311 InputDeviceInfo::MotionRange x;
1312 InputDeviceInfo::MotionRange y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001313 InputDeviceInfo::MotionRange pressure;
1314
1315 bool haveSize;
1316 InputDeviceInfo::MotionRange size;
1317
1318 bool haveTouchSize;
1319 InputDeviceInfo::MotionRange touchMajor;
1320 InputDeviceInfo::MotionRange touchMinor;
1321
1322 bool haveToolSize;
1323 InputDeviceInfo::MotionRange toolMajor;
1324 InputDeviceInfo::MotionRange toolMinor;
1325
1326 bool haveOrientation;
1327 InputDeviceInfo::MotionRange orientation;
1328
1329 bool haveDistance;
1330 InputDeviceInfo::MotionRange distance;
Jeff Brown65fd2512011-08-18 11:20:58 -07001331
1332 bool haveTilt;
1333 InputDeviceInfo::MotionRange tilt;
1334
1335 OrientedRanges() {
1336 clear();
1337 }
1338
1339 void clear() {
1340 haveSize = false;
1341 haveTouchSize = false;
1342 haveToolSize = false;
1343 haveOrientation = false;
1344 haveDistance = false;
1345 haveTilt = false;
1346 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001347 } mOrientedRanges;
1348
1349 // Oriented dimensions and precision.
1350 float mOrientedSurfaceWidth;
1351 float mOrientedSurfaceHeight;
1352 float mOrientedXPrecision;
1353 float mOrientedYPrecision;
1354
1355 struct CurrentVirtualKeyState {
1356 bool down;
1357 bool ignored;
1358 nsecs_t downTime;
1359 int32_t keyCode;
1360 int32_t scanCode;
1361 } mCurrentVirtualKey;
1362
Jeff Brown65fd2512011-08-18 11:20:58 -07001363 // Scale factor for gesture or mouse based pointer movements.
1364 float mPointerXMovementScale;
1365 float mPointerYMovementScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001366
1367 // Scale factor for gesture based zooming and other freeform motions.
Jeff Brown65fd2512011-08-18 11:20:58 -07001368 float mPointerXZoomScale;
1369 float mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001370
1371 // The maximum swipe width.
1372 float mPointerGestureMaxSwipeWidth;
1373
Jeff Brown6d0fec22010-07-23 21:28:06 -07001374 struct PointerDistanceHeapElement {
1375 uint32_t currentPointerIndex : 8;
1376 uint32_t lastPointerIndex : 8;
1377 uint64_t distance : 48; // squared distance
1378 };
1379
Jeff Brown65fd2512011-08-18 11:20:58 -07001380 enum PointerUsage {
1381 POINTER_USAGE_NONE,
1382 POINTER_USAGE_GESTURES,
1383 POINTER_USAGE_STYLUS,
1384 POINTER_USAGE_MOUSE,
1385 };
1386 PointerUsage mPointerUsage;
1387
Jeff Brownace13b12011-03-09 17:39:48 -08001388 struct PointerGesture {
1389 enum Mode {
1390 // No fingers, button is not pressed.
1391 // Nothing happening.
1392 NEUTRAL,
1393
1394 // No fingers, button is not pressed.
1395 // Tap detected.
1396 // Emits DOWN and UP events at the pointer location.
1397 TAP,
1398
Jeff Brown79ac9692011-04-19 21:20:10 -07001399 // Exactly one finger dragging following a tap.
1400 // Pointer follows the active finger.
1401 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001402 //
1403 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001404 TAP_DRAG,
1405
Jeff Brownace13b12011-03-09 17:39:48 -08001406 // Button is pressed.
1407 // Pointer follows the active finger if there is one. Other fingers are ignored.
1408 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001409 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001410
1411 // Exactly one finger, button is not pressed.
1412 // Pointer follows the active finger.
1413 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001414 //
1415 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001416 HOVER,
1417
Jeff Brown2352b972011-04-12 22:39:53 -07001418 // Exactly two fingers but neither have moved enough to clearly indicate
1419 // whether a swipe or freeform gesture was intended. We consider the
1420 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1421 // Pointer does not move.
1422 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1423 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001424
1425 // Exactly two fingers moving in the same direction, button is not pressed.
1426 // Pointer does not move.
1427 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1428 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001429 SWIPE,
1430
1431 // Two or more fingers moving in arbitrary directions, button is not pressed.
1432 // Pointer does not move.
1433 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1434 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001435 FREEFORM,
1436
1437 // Waiting for quiet time to end before starting the next gesture.
1438 QUIET,
1439 };
1440
Jeff Brown2352b972011-04-12 22:39:53 -07001441 // Time the first finger went down.
1442 nsecs_t firstTouchTime;
1443
Jeff Brownace13b12011-03-09 17:39:48 -08001444 // The active pointer id from the raw touch data.
1445 int32_t activeTouchId; // -1 if none
1446
1447 // The active pointer id from the gesture last delivered to the application.
1448 int32_t activeGestureId; // -1 if none
1449
1450 // Pointer coords and ids for the current and previous pointer gesture.
1451 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001452 BitSet32 currentGestureIdBits;
1453 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001454 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001455 PointerCoords currentGestureCoords[MAX_POINTERS];
1456
1457 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001458 BitSet32 lastGestureIdBits;
1459 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001460 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001461 PointerCoords lastGestureCoords[MAX_POINTERS];
1462
Jeff Brownace13b12011-03-09 17:39:48 -08001463 // Time the pointer gesture last went down.
1464 nsecs_t downTime;
1465
Jeff Brown79ac9692011-04-19 21:20:10 -07001466 // Time when the pointer went down for a TAP.
1467 nsecs_t tapDownTime;
1468
1469 // Time when the pointer went up for a TAP.
1470 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001471
Jeff Brown2352b972011-04-12 22:39:53 -07001472 // Location of initial tap.
1473 float tapX, tapY;
1474
Jeff Brownace13b12011-03-09 17:39:48 -08001475 // Time we started waiting for quiescence.
1476 nsecs_t quietTime;
1477
Jeff Brown2352b972011-04-12 22:39:53 -07001478 // Reference points for multitouch gestures.
1479 float referenceTouchX; // reference touch X/Y coordinates in surface units
1480 float referenceTouchY;
1481 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1482 float referenceGestureY;
1483
Jeff Brown538881e2011-05-25 18:23:38 -07001484 // Distance that each pointer has traveled which has not yet been
1485 // subsumed into the reference gesture position.
1486 BitSet32 referenceIdBits;
1487 struct Delta {
1488 float dx, dy;
1489 };
1490 Delta referenceDeltas[MAX_POINTER_ID + 1];
1491
Jeff Brown2352b972011-04-12 22:39:53 -07001492 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1493 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1494
Jeff Brownace13b12011-03-09 17:39:48 -08001495 // A velocity tracker for determining whether to switch active pointers during drags.
1496 VelocityTracker velocityTracker;
1497
1498 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001499 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001500 activeTouchId = -1;
1501 activeGestureId = -1;
1502 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001503 currentGestureIdBits.clear();
1504 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001505 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001506 downTime = 0;
1507 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001508 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001509 resetQuietTime();
1510 }
1511
Jeff Brown79ac9692011-04-19 21:20:10 -07001512 void resetTap() {
1513 tapDownTime = LLONG_MIN;
1514 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001515 }
1516
1517 void resetQuietTime() {
1518 quietTime = LLONG_MIN;
1519 }
1520 } mPointerGesture;
1521
Jeff Brown65fd2512011-08-18 11:20:58 -07001522 struct PointerSimple {
1523 PointerCoords currentCoords;
1524 PointerProperties currentProperties;
1525 PointerCoords lastCoords;
1526 PointerProperties lastProperties;
1527
1528 // True if the pointer is down.
1529 bool down;
1530
1531 // True if the pointer is hovering.
1532 bool hovering;
1533
1534 // Time the pointer last went down.
1535 nsecs_t downTime;
1536
1537 void reset() {
1538 currentCoords.clear();
1539 currentProperties.clear();
1540 lastCoords.clear();
1541 lastProperties.clear();
1542 down = false;
1543 hovering = false;
1544 downTime = 0;
1545 }
1546 } mPointerSimple;
1547
1548 // The pointer and scroll velocity controls.
1549 VelocityControl mPointerVelocityControl;
1550 VelocityControl mWheelXVelocityControl;
1551 VelocityControl mWheelYVelocityControl;
1552
1553 void sync(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001554
Jeff Brownbe1aa822011-07-27 16:04:54 -07001555 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1556 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1557 int32_t keyEventAction, int32_t keyEventFlags);
1558
Jeff Brown6d0fec22010-07-23 21:28:06 -07001559 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001560 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1561 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1562 void cookPointerData();
1563
Jeff Brown65fd2512011-08-18 11:20:58 -07001564 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1565 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1566
Jeff Brown79ac9692011-04-19 21:20:10 -07001567 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
Jeff Brown65fd2512011-08-18 11:20:58 -07001568 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Jeff Brown79ac9692011-04-19 21:20:10 -07001569 bool preparePointerGestures(nsecs_t when,
Jeff Brown65fd2512011-08-18 11:20:58 -07001570 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1571 bool isTimeout);
1572
1573 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1574 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1575
1576 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1577 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1578
1579 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1580 bool down, bool hovering);
1581 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001582
1583 // Dispatches a motion event.
1584 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1585 // method will take care of setting the index and transmuting the action to DOWN or UP
1586 // it is the first / last pointer to go down / up.
1587 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001588 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1589 int32_t edgeFlags,
1590 const PointerProperties* properties, const PointerCoords* coords,
1591 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001592 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1593
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001594 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001595 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001596 bool updateMovedPointers(const PointerProperties* inProperties,
1597 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1598 PointerProperties* outProperties, PointerCoords* outCoords,
1599 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001600
Jeff Brownbe1aa822011-07-27 16:04:54 -07001601 bool isPointInsideSurface(int32_t x, int32_t y);
1602 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001603
Jeff Brownbe1aa822011-07-27 16:04:54 -07001604 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001605};
1606
1607
1608class SingleTouchInputMapper : public TouchInputMapper {
1609public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001610 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001611 virtual ~SingleTouchInputMapper();
1612
Jeff Brown65fd2512011-08-18 11:20:58 -07001613 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001614 virtual void process(const RawEvent* rawEvent);
1615
1616protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001617 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001618 virtual void configureRawPointerAxes();
Jeff Brown00710e92012-04-19 15:18:26 -07001619 virtual bool hasStylus() const;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001620
1621private:
Jeff Brown49754db2011-07-01 17:37:58 -07001622 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001623};
1624
1625
1626class MultiTouchInputMapper : public TouchInputMapper {
1627public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001628 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001629 virtual ~MultiTouchInputMapper();
1630
Jeff Brown65fd2512011-08-18 11:20:58 -07001631 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001632 virtual void process(const RawEvent* rawEvent);
1633
1634protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001635 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001636 virtual void configureRawPointerAxes();
Jeff Brown00710e92012-04-19 15:18:26 -07001637 virtual bool hasStylus() const;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001638
1639private:
Jeff Brown49754db2011-07-01 17:37:58 -07001640 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001641
Jeff Brown6894a292011-07-01 17:59:27 -07001642 // Specifies the pointer id bits that are in use, and their associated tracking id.
1643 BitSet32 mPointerIdBits;
1644 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001645};
1646
Jeff Browncb1404e2011-01-15 18:14:15 -08001647
1648class JoystickInputMapper : public InputMapper {
1649public:
1650 JoystickInputMapper(InputDevice* device);
1651 virtual ~JoystickInputMapper();
1652
1653 virtual uint32_t getSources();
1654 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1655 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001656 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1657 virtual void reset(nsecs_t when);
Jeff Browncb1404e2011-01-15 18:14:15 -08001658 virtual void process(const RawEvent* rawEvent);
1659
1660private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001661 struct Axis {
1662 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001663 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001664
Jeff Brown6f2fba42011-02-19 01:08:02 -08001665 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001666
Jeff Brown6f2fba42011-02-19 01:08:02 -08001667 float scale; // scale factor from raw to normalized values
1668 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001669 float highScale; // scale factor from raw to normalized values of high split
1670 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001671
Jeff Brown6f2fba42011-02-19 01:08:02 -08001672 float min; // normalized inclusive minimum
1673 float max; // normalized inclusive maximum
1674 float flat; // normalized flat region size
1675 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001676
Jeff Brown6f2fba42011-02-19 01:08:02 -08001677 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001678 float currentValue; // current value
1679 float newValue; // most recent value
1680 float highCurrentValue; // current value of high split
1681 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001682
Jeff Brown85297452011-03-04 13:07:49 -08001683 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1684 bool explicitlyMapped, float scale, float offset,
1685 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001686 float min, float max, float flat, float fuzz) {
1687 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001688 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001689 this->explicitlyMapped = explicitlyMapped;
1690 this->scale = scale;
1691 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001692 this->highScale = highScale;
1693 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001694 this->min = min;
1695 this->max = max;
1696 this->flat = flat;
1697 this->fuzz = fuzz;
1698 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001699 resetValue();
1700 }
1701
1702 void resetValue() {
1703 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001704 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001705 this->highCurrentValue = 0;
1706 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001707 }
1708 };
1709
Jeff Brown6f2fba42011-02-19 01:08:02 -08001710 // Axes indexed by raw ABS_* axis index.
1711 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001712
Jeff Brown6f2fba42011-02-19 01:08:02 -08001713 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001714
Jeff Brown85297452011-03-04 13:07:49 -08001715 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001716 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001717 bool filterAxes(bool force);
1718
1719 static bool hasValueChangedSignificantly(float filter,
1720 float newValue, float currentValue, float min, float max);
1721 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1722 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001723
Jeff Brown6f2fba42011-02-19 01:08:02 -08001724 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001725};
1726
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001727} // namespace android
1728
1729#endif // _UI_INPUT_READER_H