blob: ad89a222293efb55aeea7d99c848309b32cefd9f [file] [log] [blame]
Jeff Brown46b9ac02010-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
Jeff Brown46b9ac02010-04-22 18:58:52 -070024#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown214eaf42011-05-26 19:17:02 -070043 * Input reader configuration.
44 *
45 * Specifies various options that modify the behavior of the input reader.
46 */
47struct InputReaderConfiguration {
Jeff Brown474dcb52011-06-14 20:22:50 -070048 // Describes changes that have occurred.
49 enum {
50 // The pointer speed changed.
51 CHANGE_POINTER_SPEED = 1 << 0,
52
53 // The pointer gesture control changed.
54 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
55
Jeff Brown65fd2512011-08-18 11:20:58 -070056 // The display size or orientation changed.
57 CHANGE_DISPLAY_INFO = 1 << 2,
58
Jeff Browndaf4a122011-08-26 17:14:14 -070059 // The visible touches option changed.
60 CHANGE_SHOW_TOUCHES = 1 << 3,
61
Jeff Brown474dcb52011-06-14 20:22:50 -070062 // All devices must be reopened.
63 CHANGE_MUST_REOPEN = 1 << 31,
64 };
65
Jeff Brown214eaf42011-05-26 19:17:02 -070066 // Gets the amount of time to disable virtual keys after the screen is touched
67 // in order to filter out accidental virtual key presses due to swiping gestures
68 // or taps near the edge of the display. May be 0 to disable the feature.
69 nsecs_t virtualKeyQuietTime;
70
71 // The excluded device names for the platform.
72 // Devices with these names will be ignored.
73 Vector<String8> excludedDeviceNames;
74
Jeff Brown19c97d462011-06-01 12:33:19 -070075 // Velocity control parameters for mouse pointer movements.
76 VelocityControlParameters pointerVelocityControlParameters;
77
78 // Velocity control parameters for mouse wheel movements.
79 VelocityControlParameters wheelVelocityControlParameters;
80
Jeff Brown474dcb52011-06-14 20:22:50 -070081 // True if pointer gestures are enabled.
82 bool pointerGesturesEnabled;
83
Jeff Brown214eaf42011-05-26 19:17:02 -070084 // Quiet time between certain pointer gesture transitions.
85 // Time to allow for all fingers or buttons to settle into a stable state before
86 // starting a new gesture.
87 nsecs_t pointerGestureQuietInterval;
88
89 // The minimum speed that a pointer must travel for us to consider switching the active
90 // touch pointer to it during a drag. This threshold is set to avoid switching due
91 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
92 float pointerGestureDragMinSwitchSpeed; // in pixels per second
93
94 // Tap gesture delay time.
95 // The time between down and up must be less than this to be considered a tap.
96 nsecs_t pointerGestureTapInterval;
97
98 // Tap drag gesture delay time.
99 // The time between the previous tap's up and the next down must be less than
100 // this to be considered a drag. Otherwise, the previous tap is finished and a
101 // new tap begins.
102 //
103 // Note that the previous tap will be held down for this entire duration so this
104 // interval must be shorter than the long press timeout.
105 nsecs_t pointerGestureTapDragInterval;
106
107 // The distance in pixels that the pointer is allowed to move from initial down
108 // to up and still be called a tap.
109 float pointerGestureTapSlop; // in pixels
110
111 // Time after the first touch points go down to settle on an initial centroid.
112 // This is intended to be enough time to handle cases where the user puts down two
113 // fingers at almost but not quite exactly the same time.
114 nsecs_t pointerGestureMultitouchSettleInterval;
115
116 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700117 // at least two pointers have moved at least this far from their starting place.
118 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700119
120 // The transition from PRESS to SWIPE gesture mode can only occur when the
121 // cosine of the angle between the two vectors is greater than or equal to than this value
122 // which indicates that the vectors are oriented in the same direction.
123 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
124 // (In exactly opposite directions, the cosine is -1.0.)
125 float pointerGestureSwipeTransitionAngleCosine;
126
127 // The transition from PRESS to SWIPE gesture mode can only occur when the
128 // fingers are no more than this far apart relative to the diagonal size of
129 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
130 // no more than half the diagonal size of the touch pad apart.
131 float pointerGestureSwipeMaxWidthRatio;
132
133 // The gesture movement speed factor relative to the size of the display.
134 // Movement speed applies when the fingers are moving in the same direction.
135 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
136 // will cover this portion of the display diagonal.
137 float pointerGestureMovementSpeedRatio;
138
139 // The gesture zoom speed factor relative to the size of the display.
140 // Zoom speed applies when the fingers are mostly moving relative to each other
141 // to execute a scale gesture or similar.
142 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
143 // will cover this portion of the display diagonal.
144 float pointerGestureZoomSpeedRatio;
145
Jeff Browndaf4a122011-08-26 17:14:14 -0700146 // True to show the location of touches on the touch screen as spots.
147 bool showTouches;
148
Jeff Brown214eaf42011-05-26 19:17:02 -0700149 InputReaderConfiguration() :
Jeff Brown214eaf42011-05-26 19:17:02 -0700150 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700151 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700152 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700153 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700154 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
155 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
156 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
157 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
158 pointerGestureTapSlop(10.0f), // 10 pixels
159 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700160 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700161 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700162 pointerGestureSwipeMaxWidthRatio(0.25f),
163 pointerGestureMovementSpeedRatio(0.8f),
Jeff Browndaf4a122011-08-26 17:14:14 -0700164 pointerGestureZoomSpeedRatio(0.3f),
165 showTouches(false) { }
Jeff Brown65fd2512011-08-18 11:20:58 -0700166
167 bool getDisplayInfo(int32_t displayId, bool external,
168 int32_t* width, int32_t* height, int32_t* orientation) const;
169
170 void setDisplayInfo(int32_t displayId, bool external,
171 int32_t width, int32_t height, int32_t orientation);
172
173private:
174 struct DisplayInfo {
175 int32_t width;
176 int32_t height;
177 int32_t orientation;
178
179 DisplayInfo() :
180 width(-1), height(-1), orientation(DISPLAY_ORIENTATION_0) {
181 }
182 };
183
184 DisplayInfo mInternalDisplay;
185 DisplayInfo mExternalDisplay;
Jeff Brown214eaf42011-05-26 19:17:02 -0700186};
187
188
189/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190 * Input reader policy interface.
191 *
192 * The input reader policy is used by the input reader to interact with the Window Manager
193 * and other system components.
194 *
195 * The actual implementation is partially supported by callbacks into the DVM
196 * via JNI. This interface is also mocked in the unit tests.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700197 *
198 * These methods must NOT re-enter the input reader since they may be called while
199 * holding the input reader lock.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200 */
201class InputReaderPolicyInterface : public virtual RefBase {
202protected:
203 InputReaderPolicyInterface() { }
204 virtual ~InputReaderPolicyInterface() { }
205
206public:
Jeff Brown214eaf42011-05-26 19:17:02 -0700207 /* Gets the input reader configuration. */
208 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800209
210 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
211 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212};
213
214
Jeff Brownbe1aa822011-07-27 16:04:54 -0700215/* Processes raw input events and sends cooked event data to an input listener. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700216class InputReaderInterface : public virtual RefBase {
217protected:
218 InputReaderInterface() { }
219 virtual ~InputReaderInterface() { }
220
221public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700222 /* Dumps the state of the input reader.
223 *
224 * This method may be called on any thread (usually by the input manager). */
225 virtual void dump(String8& dump) = 0;
226
Jeff Brown89ef0722011-08-10 16:25:21 -0700227 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
228 virtual void monitor() = 0;
229
Jeff Brown46b9ac02010-04-22 18:58:52 -0700230 /* Runs a single iteration of the processing loop.
231 * Nominally reads and processes one incoming message from the EventHub.
232 *
233 * This method should be called on the input reader thread.
234 */
235 virtual void loopOnce() = 0;
236
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 /* Gets the current input device configuration.
238 *
239 * This method may be called on any thread (usually by the input manager).
240 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700241 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700242
Jeff Brown6d0fec22010-07-23 21:28:06 -0700243 /* Gets information about the specified input device.
244 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
245 * was no such device.
246 *
247 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700248 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
250
251 /* Gets the list of all registered device ids. */
252 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
253
254 /* Query current input state. */
255 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
256 int32_t scanCode) = 0;
257 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
258 int32_t keyCode) = 0;
259 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
260 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700261
262 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700263 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
264 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700265
Jeff Brown474dcb52011-06-14 20:22:50 -0700266 /* Requests that a reconfiguration of all input devices.
267 * The changes flag is a bitfield that indicates what has changed and whether
268 * the input devices must all be reopened. */
269 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700270};
271
272
273/* Internal interface used by individual input devices to access global input device state
274 * and parameters maintained by the input reader.
275 */
276class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700277public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700278 InputReaderContext() { }
279 virtual ~InputReaderContext() { }
280
Jeff Brown6d0fec22010-07-23 21:28:06 -0700281 virtual void updateGlobalMetaState() = 0;
282 virtual int32_t getGlobalMetaState() = 0;
283
Jeff Brownfe508922011-01-18 15:10:10 -0800284 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
285 virtual bool shouldDropVirtualKey(nsecs_t now,
286 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
287
Jeff Brown05dc66a2011-03-02 14:41:58 -0800288 virtual void fadePointer() = 0;
289
Jeff Brownaa3855d2011-03-17 01:34:19 -0700290 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
291
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700293 virtual InputListenerInterface* getListener() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700294 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700295};
296
Jeff Brown9c3cda02010-06-15 01:31:58 -0700297
Jeff Brown46b9ac02010-04-22 18:58:52 -0700298/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brownbe1aa822011-07-27 16:04:54 -0700299 * that it sends to the input listener. Some functions of the input reader, such as early
Jeff Brown9c3cda02010-06-15 01:31:58 -0700300 * event filtering in low power states, are controlled by a separate policy object.
301 *
Jeff Brownbe1aa822011-07-27 16:04:54 -0700302 * The InputReader owns a collection of InputMappers. Most of the work it does happens
303 * on the input reader thread but the InputReader can receive queries from other system
304 * components running on arbitrary threads. To keep things manageable, the InputReader
305 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
306 * EventHub or the InputReaderPolicy but it is never held while calling into the
307 * InputListener.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700308 */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700309class InputReader : public InputReaderInterface {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700310public:
311 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700312 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700313 const sp<InputListenerInterface>& listener);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700314 virtual ~InputReader();
315
Jeff Brownb88102f2010-09-08 11:49:43 -0700316 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700317 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700318
Jeff Brown46b9ac02010-04-22 18:58:52 -0700319 virtual void loopOnce();
320
Jeff Brown6d0fec22010-07-23 21:28:06 -0700321 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700322
Jeff Brown6d0fec22010-07-23 21:28:06 -0700323 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
324 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325
Jeff Brown6d0fec22010-07-23 21:28:06 -0700326 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
327 int32_t scanCode);
328 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
329 int32_t keyCode);
330 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
331 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700332
Jeff Brown6d0fec22010-07-23 21:28:06 -0700333 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
334 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700335
Jeff Brown474dcb52011-06-14 20:22:50 -0700336 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700337
Jeff Brownc3db8582010-10-20 15:33:38 -0700338protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700339 // These members are protected so they can be instrumented by test cases.
340 virtual InputDevice* createDeviceLocked(int32_t deviceId,
341 const String8& name, uint32_t classes);
342
343 class ContextImpl : public InputReaderContext {
344 InputReader* mReader;
345
346 public:
347 ContextImpl(InputReader* reader);
348
349 virtual void updateGlobalMetaState();
350 virtual int32_t getGlobalMetaState();
351 virtual void disableVirtualKeysUntil(nsecs_t time);
352 virtual bool shouldDropVirtualKey(nsecs_t now,
353 InputDevice* device, int32_t keyCode, int32_t scanCode);
354 virtual void fadePointer();
355 virtual void requestTimeoutAtTime(nsecs_t when);
356 virtual InputReaderPolicyInterface* getPolicy();
357 virtual InputListenerInterface* getListener();
358 virtual EventHubInterface* getEventHub();
359 } mContext;
360
361 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700362
Jeff Brown46b9ac02010-04-22 18:58:52 -0700363private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700364 Mutex mLock;
365
Jeff Brown112b5f52012-01-27 17:32:06 -0800366 Condition mReaderIsAliveCondition;
367
Jeff Brown46b9ac02010-04-22 18:58:52 -0700368 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700371
Jeff Brown214eaf42011-05-26 19:17:02 -0700372 InputReaderConfiguration mConfig;
373
Jeff Brownb7198742011-03-18 18:14:26 -0700374 // The event queue.
375 static const int EVENT_BUFFER_SIZE = 256;
376 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
377
Jeff Brown46b9ac02010-04-22 18:58:52 -0700378 KeyedVector<int32_t, InputDevice*> mDevices;
379
Jeff Brown6d0fec22010-07-23 21:28:06 -0700380 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700381 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700382
Jeff Brown65fd2512011-08-18 11:20:58 -0700383 void addDeviceLocked(nsecs_t when, int32_t deviceId);
384 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700385 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
386 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700387
Jeff Brownbe1aa822011-07-27 16:04:54 -0700388 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700389
Jeff Brownbe1aa822011-07-27 16:04:54 -0700390 int32_t mGlobalMetaState;
391 void updateGlobalMetaStateLocked();
392 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700393
Jeff Brownbe1aa822011-07-27 16:04:54 -0700394 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395
Jeff Brownbe1aa822011-07-27 16:04:54 -0700396 InputConfiguration mInputConfiguration;
397 void updateInputConfigurationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800398
Jeff Brownbe1aa822011-07-27 16:04:54 -0700399 nsecs_t mDisableVirtualKeysTimeout;
400 void disableVirtualKeysUntilLocked(nsecs_t time);
401 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800402 InputDevice* device, int32_t keyCode, int32_t scanCode);
403
Jeff Brownbe1aa822011-07-27 16:04:54 -0700404 nsecs_t mNextTimeout;
405 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700406
Jeff Brownbe1aa822011-07-27 16:04:54 -0700407 uint32_t mConfigurationChangesToRefresh;
408 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700409
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 // state queries
411 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700412 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700414 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700416};
417
418
419/* Reads raw events from the event hub and processes them, endlessly. */
420class InputReaderThread : public Thread {
421public:
422 InputReaderThread(const sp<InputReaderInterface>& reader);
423 virtual ~InputReaderThread();
424
425private:
426 sp<InputReaderInterface> mReader;
427
428 virtual bool threadLoop();
429};
430
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431
432/* Represents the state of a single input device. */
433class InputDevice {
434public:
Jeff Brown9ee285a2011-08-31 12:56:34 -0700435 InputDevice(InputReaderContext* context, int32_t id, const String8& name, uint32_t classes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700436 ~InputDevice();
437
438 inline InputReaderContext* getContext() { return mContext; }
439 inline int32_t getId() { return mId; }
440 inline const String8& getName() { return mName; }
Jeff Brown9ee285a2011-08-31 12:56:34 -0700441 inline uint32_t getClasses() { return mClasses; }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 inline uint32_t getSources() { return mSources; }
443
Jeff Brown56194eb2011-03-02 19:23:13 -0800444 inline bool isExternal() { return mIsExternal; }
445 inline void setExternal(bool external) { mIsExternal = external; }
446
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 inline bool isIgnored() { return mMappers.isEmpty(); }
448
Jeff Brownef3d7e82010-09-30 14:33:04 -0700449 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700450 void addMapper(InputMapper* mapper);
Jeff Brown65fd2512011-08-18 11:20:58 -0700451 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
452 void reset(nsecs_t when);
Jeff Brownb7198742011-03-18 18:14:26 -0700453 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700454 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455
456 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
457 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
458 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
459 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
460 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
461 const int32_t* keyCodes, uint8_t* outFlags);
462
463 int32_t getMetaState();
464
Jeff Brown05dc66a2011-03-02 14:41:58 -0800465 void fadePointer();
466
Jeff Brown65fd2512011-08-18 11:20:58 -0700467 void notifyReset(nsecs_t when);
468
Jeff Brown49754db2011-07-01 17:37:58 -0700469 inline const PropertyMap& getConfiguration() { return mConfiguration; }
470 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700471
Jeff Brown65fd2512011-08-18 11:20:58 -0700472 bool hasKey(int32_t code) {
473 return getEventHub()->hasScanCode(mId, code);
474 }
475
476 bool isKeyPressed(int32_t code) {
477 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
478 }
479
480 int32_t getAbsoluteAxisValue(int32_t code) {
481 int32_t value;
482 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
483 return value;
484 }
485
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486private:
487 InputReaderContext* mContext;
488 int32_t mId;
Jeff Brown9ee285a2011-08-31 12:56:34 -0700489 String8 mName;
490 uint32_t mClasses;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700491
492 Vector<InputMapper*> mMappers;
493
Jeff Brown6d0fec22010-07-23 21:28:06 -0700494 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800495 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700496 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700497
498 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
499 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700500
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800501 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700502};
503
504
Jeff Brown49754db2011-07-01 17:37:58 -0700505/* Keeps track of the state of mouse or touch pad buttons. */
506class CursorButtonAccumulator {
507public:
508 CursorButtonAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700509 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700510
Jeff Brown49754db2011-07-01 17:37:58 -0700511 void process(const RawEvent* rawEvent);
512
513 uint32_t getButtonState() const;
514
515private:
516 bool mBtnLeft;
517 bool mBtnRight;
518 bool mBtnMiddle;
519 bool mBtnBack;
520 bool mBtnSide;
521 bool mBtnForward;
522 bool mBtnExtra;
523 bool mBtnTask;
Jeff Brown65fd2512011-08-18 11:20:58 -0700524
525 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700526};
527
528
529/* Keeps track of cursor movements. */
530
531class CursorMotionAccumulator {
532public:
533 CursorMotionAccumulator();
Jeff Brown65fd2512011-08-18 11:20:58 -0700534 void reset(InputDevice* device);
535
536 void process(const RawEvent* rawEvent);
537 void finishSync();
538
539 inline int32_t getRelativeX() const { return mRelX; }
540 inline int32_t getRelativeY() const { return mRelY; }
541
542private:
543 int32_t mRelX;
544 int32_t mRelY;
Jeff Brown49754db2011-07-01 17:37:58 -0700545
546 void clearRelativeAxes();
Jeff Brown65fd2512011-08-18 11:20:58 -0700547};
548
549
550/* Keeps track of cursor scrolling motions. */
551
552class CursorScrollAccumulator {
553public:
554 CursorScrollAccumulator();
555 void configure(InputDevice* device);
556 void reset(InputDevice* device);
557
Jeff Brown49754db2011-07-01 17:37:58 -0700558 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700559 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700560
561 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
562 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
563
564 inline int32_t getRelativeX() const { return mRelX; }
565 inline int32_t getRelativeY() const { return mRelY; }
566 inline int32_t getRelativeVWheel() const { return mRelWheel; }
567 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
568
569private:
570 bool mHaveRelWheel;
571 bool mHaveRelHWheel;
572
573 int32_t mRelX;
574 int32_t mRelY;
575 int32_t mRelWheel;
576 int32_t mRelHWheel;
Jeff Brown65fd2512011-08-18 11:20:58 -0700577
578 void clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700579};
580
581
582/* Keeps track of the state of touch, stylus and tool buttons. */
583class TouchButtonAccumulator {
584public:
585 TouchButtonAccumulator();
586 void configure(InputDevice* device);
Jeff Brown65fd2512011-08-18 11:20:58 -0700587 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700588
Jeff Brown49754db2011-07-01 17:37:58 -0700589 void process(const RawEvent* rawEvent);
590
591 uint32_t getButtonState() const;
592 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700593 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700594 bool isHovering() const;
595
596private:
597 bool mHaveBtnTouch;
598
599 bool mBtnTouch;
600 bool mBtnStylus;
601 bool mBtnStylus2;
602 bool mBtnToolFinger;
603 bool mBtnToolPen;
604 bool mBtnToolRubber;
Jeff Brown65fd2512011-08-18 11:20:58 -0700605 bool mBtnToolBrush;
606 bool mBtnToolPencil;
607 bool mBtnToolAirbrush;
608 bool mBtnToolMouse;
609 bool mBtnToolLens;
Jeff Brownea6892e2011-08-23 17:31:25 -0700610 bool mBtnToolDoubleTap;
611 bool mBtnToolTripleTap;
612 bool mBtnToolQuadTap;
Jeff Brown65fd2512011-08-18 11:20:58 -0700613
614 void clearButtons();
Jeff Brown49754db2011-07-01 17:37:58 -0700615};
616
617
Jeff Brownbe1aa822011-07-27 16:04:54 -0700618/* Raw axis information from the driver. */
619struct RawPointerAxes {
620 RawAbsoluteAxisInfo x;
621 RawAbsoluteAxisInfo y;
622 RawAbsoluteAxisInfo pressure;
623 RawAbsoluteAxisInfo touchMajor;
624 RawAbsoluteAxisInfo touchMinor;
625 RawAbsoluteAxisInfo toolMajor;
626 RawAbsoluteAxisInfo toolMinor;
627 RawAbsoluteAxisInfo orientation;
628 RawAbsoluteAxisInfo distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700629 RawAbsoluteAxisInfo tiltX;
630 RawAbsoluteAxisInfo tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631 RawAbsoluteAxisInfo trackingId;
632 RawAbsoluteAxisInfo slot;
633
634 RawPointerAxes();
635 void clear();
636};
637
638
639/* Raw data for a collection of pointers including a pointer id mapping table. */
640struct RawPointerData {
641 struct Pointer {
642 uint32_t id;
643 int32_t x;
644 int32_t y;
645 int32_t pressure;
646 int32_t touchMajor;
647 int32_t touchMinor;
648 int32_t toolMajor;
649 int32_t toolMinor;
650 int32_t orientation;
651 int32_t distance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700652 int32_t tiltX;
653 int32_t tiltY;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700654 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
655 bool isHovering;
656 };
657
658 uint32_t pointerCount;
659 Pointer pointers[MAX_POINTERS];
660 BitSet32 hoveringIdBits, touchingIdBits;
661 uint32_t idToIndex[MAX_POINTER_ID + 1];
662
663 RawPointerData();
664 void clear();
665 void copyFrom(const RawPointerData& other);
666 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
667
668 inline void markIdBit(uint32_t id, bool isHovering) {
669 if (isHovering) {
670 hoveringIdBits.markBit(id);
671 } else {
672 touchingIdBits.markBit(id);
673 }
674 }
675
676 inline void clearIdBits() {
677 hoveringIdBits.clear();
678 touchingIdBits.clear();
679 }
680
681 inline const Pointer& pointerForId(uint32_t id) const {
682 return pointers[idToIndex[id]];
683 }
684
685 inline bool isHovering(uint32_t pointerIndex) {
686 return pointers[pointerIndex].isHovering;
687 }
688};
689
690
691/* Cooked data for a collection of pointers including a pointer id mapping table. */
692struct CookedPointerData {
693 uint32_t pointerCount;
694 PointerProperties pointerProperties[MAX_POINTERS];
695 PointerCoords pointerCoords[MAX_POINTERS];
696 BitSet32 hoveringIdBits, touchingIdBits;
697 uint32_t idToIndex[MAX_POINTER_ID + 1];
698
699 CookedPointerData();
700 void clear();
701 void copyFrom(const CookedPointerData& other);
702
703 inline bool isHovering(uint32_t pointerIndex) {
704 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
705 }
706};
707
708
Jeff Brown49754db2011-07-01 17:37:58 -0700709/* Keeps track of the state of single-touch protocol. */
710class SingleTouchMotionAccumulator {
711public:
712 SingleTouchMotionAccumulator();
713
Jeff Brown49754db2011-07-01 17:37:58 -0700714 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700715 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700716
717 inline int32_t getAbsoluteX() const { return mAbsX; }
718 inline int32_t getAbsoluteY() const { return mAbsY; }
719 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
720 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
721 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
Jeff Brown65fd2512011-08-18 11:20:58 -0700722 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
723 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
Jeff Brown49754db2011-07-01 17:37:58 -0700724
725private:
726 int32_t mAbsX;
727 int32_t mAbsY;
728 int32_t mAbsPressure;
729 int32_t mAbsToolWidth;
730 int32_t mAbsDistance;
Jeff Brown65fd2512011-08-18 11:20:58 -0700731 int32_t mAbsTiltX;
732 int32_t mAbsTiltY;
733
734 void clearAbsoluteAxes();
Jeff Brown49754db2011-07-01 17:37:58 -0700735};
736
737
738/* Keeps track of the state of multi-touch protocol. */
739class MultiTouchMotionAccumulator {
740public:
741 class Slot {
742 public:
743 inline bool isInUse() const { return mInUse; }
744 inline int32_t getX() const { return mAbsMTPositionX; }
745 inline int32_t getY() const { return mAbsMTPositionY; }
746 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
747 inline int32_t getTouchMinor() const {
748 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
749 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
750 inline int32_t getToolMinor() const {
751 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
752 inline int32_t getOrientation() const { return mAbsMTOrientation; }
753 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
754 inline int32_t getPressure() const { return mAbsMTPressure; }
755 inline int32_t getDistance() const { return mAbsMTDistance; }
756 inline int32_t getToolType() const;
757
758 private:
759 friend class MultiTouchMotionAccumulator;
760
761 bool mInUse;
762 bool mHaveAbsMTTouchMinor;
763 bool mHaveAbsMTWidthMinor;
764 bool mHaveAbsMTToolType;
765
766 int32_t mAbsMTPositionX;
767 int32_t mAbsMTPositionY;
768 int32_t mAbsMTTouchMajor;
769 int32_t mAbsMTTouchMinor;
770 int32_t mAbsMTWidthMajor;
771 int32_t mAbsMTWidthMinor;
772 int32_t mAbsMTOrientation;
773 int32_t mAbsMTTrackingId;
774 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700775 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700776 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700777
778 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700779 void clear();
780 };
781
782 MultiTouchMotionAccumulator();
783 ~MultiTouchMotionAccumulator();
784
785 void configure(size_t slotCount, bool usingSlotsProtocol);
Jeff Brown65fd2512011-08-18 11:20:58 -0700786 void reset(InputDevice* device);
Jeff Brown49754db2011-07-01 17:37:58 -0700787 void process(const RawEvent* rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -0700788 void finishSync();
Jeff Brown49754db2011-07-01 17:37:58 -0700789
Jeff Brown49754db2011-07-01 17:37:58 -0700790 inline size_t getSlotCount() const { return mSlotCount; }
791 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
792
793private:
794 int32_t mCurrentSlot;
795 Slot* mSlots;
796 size_t mSlotCount;
797 bool mUsingSlotsProtocol;
Jeff Brown65fd2512011-08-18 11:20:58 -0700798
799 void clearSlots(int32_t initialSlot);
Jeff Brown49754db2011-07-01 17:37:58 -0700800};
801
802
Jeff Brown6d0fec22010-07-23 21:28:06 -0700803/* An input mapper transforms raw input events into cooked event data.
804 * A single input device can have multiple associated input mappers in order to interpret
805 * different classes of events.
Jeff Brown65fd2512011-08-18 11:20:58 -0700806 *
807 * InputMapper lifecycle:
808 * - create
809 * - configure with 0 changes
810 * - reset
811 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
812 * - reset
813 * - destroy
Jeff Brown6d0fec22010-07-23 21:28:06 -0700814 */
815class InputMapper {
816public:
817 InputMapper(InputDevice* device);
818 virtual ~InputMapper();
819
820 inline InputDevice* getDevice() { return mDevice; }
821 inline int32_t getDeviceId() { return mDevice->getId(); }
822 inline const String8 getDeviceName() { return mDevice->getName(); }
823 inline InputReaderContext* getContext() { return mContext; }
824 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700825 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700826 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
827
828 virtual uint32_t getSources() = 0;
829 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700830 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700831 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
832 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700833 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700834 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700835
836 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
837 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
838 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
839 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
840 const int32_t* keyCodes, uint8_t* outFlags);
841
842 virtual int32_t getMetaState();
843
Jeff Brown05dc66a2011-03-02 14:41:58 -0800844 virtual void fadePointer();
845
Jeff Brown6d0fec22010-07-23 21:28:06 -0700846protected:
847 InputDevice* mDevice;
848 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800849
Jeff Brownbe1aa822011-07-27 16:04:54 -0700850 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
851
Jeff Browncb1404e2011-01-15 18:14:15 -0800852 static void dumpRawAbsoluteAxisInfo(String8& dump,
853 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700854};
855
856
857class SwitchInputMapper : public InputMapper {
858public:
859 SwitchInputMapper(InputDevice* device);
860 virtual ~SwitchInputMapper();
861
862 virtual uint32_t getSources();
863 virtual void process(const RawEvent* rawEvent);
864
865 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
866
867private:
868 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
869};
870
871
872class KeyboardInputMapper : public InputMapper {
873public:
Jeff Brownefd32662011-03-08 15:13:06 -0800874 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700875 virtual ~KeyboardInputMapper();
876
877 virtual uint32_t getSources();
878 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700879 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700880 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
881 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882 virtual void process(const RawEvent* rawEvent);
883
884 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
885 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
886 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
887 const int32_t* keyCodes, uint8_t* outFlags);
888
889 virtual int32_t getMetaState();
890
891private:
892 struct KeyDown {
893 int32_t keyCode;
894 int32_t scanCode;
895 };
896
Jeff Brownefd32662011-03-08 15:13:06 -0800897 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700898 int32_t mKeyboardType;
899
Jeff Brown65fd2512011-08-18 11:20:58 -0700900 int32_t mOrientation; // orientation for dpad keys
901
Jeff Brownbe1aa822011-07-27 16:04:54 -0700902 Vector<KeyDown> mKeyDowns; // keys that are down
903 int32_t mMetaState;
904 nsecs_t mDownTime; // time of most recent key down
905
906 struct LedState {
907 bool avail; // led is available
908 bool on; // we think the led is currently on
909 };
910 LedState mCapsLockLedState;
911 LedState mNumLockLedState;
912 LedState mScrollLockLedState;
913
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800914 // Immutable configuration parameters.
915 struct Parameters {
916 int32_t associatedDisplayId;
917 bool orientationAware;
918 } mParameters;
919
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800920 void configureParameters();
921 void dumpParameters(String8& dump);
922
Jeff Brown6d0fec22010-07-23 21:28:06 -0700923 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700924
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
926 uint32_t policyFlags);
927
Jeff Brownbe1aa822011-07-27 16:04:54 -0700928 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700929
Jeff Brownbe1aa822011-07-27 16:04:54 -0700930 void resetLedState();
931 void initializeLedState(LedState& ledState, int32_t led);
932 void updateLedState(bool reset);
933 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -0700934 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700935};
936
937
Jeff Brown83c09682010-12-23 17:50:18 -0800938class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700939public:
Jeff Brown83c09682010-12-23 17:50:18 -0800940 CursorInputMapper(InputDevice* device);
941 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700942
943 virtual uint32_t getSources();
944 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700945 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -0700946 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
947 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700948 virtual void process(const RawEvent* rawEvent);
949
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700950 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
951
Jeff Brown05dc66a2011-03-02 14:41:58 -0800952 virtual void fadePointer();
953
Jeff Brown6d0fec22010-07-23 21:28:06 -0700954private:
955 // Amount that trackball needs to move in order to generate a key event.
956 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
957
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800958 // Immutable configuration parameters.
959 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800960 enum Mode {
961 MODE_POINTER,
962 MODE_NAVIGATION,
963 };
964
965 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800966 int32_t associatedDisplayId;
967 bool orientationAware;
968 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700969
Jeff Brown49754db2011-07-01 17:37:58 -0700970 CursorButtonAccumulator mCursorButtonAccumulator;
971 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown65fd2512011-08-18 11:20:58 -0700972 CursorScrollAccumulator mCursorScrollAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700973
Jeff Brownefd32662011-03-08 15:13:06 -0800974 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700975 float mXScale;
976 float mYScale;
977 float mXPrecision;
978 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800979
Jeff Brown6f2fba42011-02-19 01:08:02 -0800980 float mVWheelScale;
981 float mHWheelScale;
982
Jeff Brown19c97d462011-06-01 12:33:19 -0700983 // Velocity controls for mouse pointer and wheel movements.
984 // The controls for X and Y wheel movements are separate to keep them decoupled.
985 VelocityControl mPointerVelocityControl;
986 VelocityControl mWheelXVelocityControl;
987 VelocityControl mWheelYVelocityControl;
988
Jeff Brown65fd2512011-08-18 11:20:58 -0700989 int32_t mOrientation;
990
Jeff Brown83c09682010-12-23 17:50:18 -0800991 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700992
Jeff Brownbe1aa822011-07-27 16:04:54 -0700993 int32_t mButtonState;
994 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700995
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800996 void configureParameters();
997 void dumpParameters(String8& dump);
998
Jeff Brown6d0fec22010-07-23 21:28:06 -0700999 void sync(nsecs_t when);
1000};
1001
1002
1003class TouchInputMapper : public InputMapper {
1004public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001005 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001006 virtual ~TouchInputMapper();
1007
1008 virtual uint32_t getSources();
1009 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001010 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001011 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1012 virtual void reset(nsecs_t when);
1013 virtual void process(const RawEvent* rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001014
1015 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1016 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1017 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1018 const int32_t* keyCodes, uint8_t* outFlags);
1019
Jeff Brownace13b12011-03-09 17:39:48 -08001020 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -07001021 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -08001022
Jeff Brown6d0fec22010-07-23 21:28:06 -07001023protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001024 CursorButtonAccumulator mCursorButtonAccumulator;
1025 CursorScrollAccumulator mCursorScrollAccumulator;
1026 TouchButtonAccumulator mTouchButtonAccumulator;
1027
Jeff Brown6d0fec22010-07-23 21:28:06 -07001028 struct VirtualKey {
1029 int32_t keyCode;
1030 int32_t scanCode;
1031 uint32_t flags;
1032
1033 // computed hit box, specified in touch screen coords based on known display size
1034 int32_t hitLeft;
1035 int32_t hitTop;
1036 int32_t hitRight;
1037 int32_t hitBottom;
1038
1039 inline bool isHit(int32_t x, int32_t y) const {
1040 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1041 }
1042 };
1043
Jeff Brown65fd2512011-08-18 11:20:58 -07001044 // Input sources and device mode.
1045 uint32_t mSource;
1046
1047 enum DeviceMode {
1048 DEVICE_MODE_DISABLED, // input is disabled
1049 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1050 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1051 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1052 };
1053 DeviceMode mDeviceMode;
Jeff Brown83c09682010-12-23 17:50:18 -08001054
Jeff Brown214eaf42011-05-26 19:17:02 -07001055 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001056 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -07001057
Jeff Brown6d0fec22010-07-23 21:28:06 -07001058 // Immutable configuration parameters.
1059 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001060 enum DeviceType {
1061 DEVICE_TYPE_TOUCH_SCREEN,
1062 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -08001063 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001064 };
1065
1066 DeviceType deviceType;
1067 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -07001068 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001069 bool orientationAware;
1070
Jeff Brown2352b972011-04-12 22:39:53 -07001071 enum GestureMode {
1072 GESTURE_MODE_POINTER,
1073 GESTURE_MODE_SPOTS,
1074 };
1075 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001076 } mParameters;
1077
Jeff Brown8d608662010-08-30 03:02:23 -07001078 // Immutable calibration parameters in parsed form.
1079 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -07001080 // Size
1081 enum SizeCalibration {
1082 SIZE_CALIBRATION_DEFAULT,
1083 SIZE_CALIBRATION_NONE,
1084 SIZE_CALIBRATION_GEOMETRIC,
1085 SIZE_CALIBRATION_DIAMETER,
1086 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -07001087 };
1088
Jeff Browna1f89ce2011-08-11 00:05:01 -07001089 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001090
Jeff Browna1f89ce2011-08-11 00:05:01 -07001091 bool haveSizeScale;
1092 float sizeScale;
1093 bool haveSizeBias;
1094 float sizeBias;
1095 bool haveSizeIsSummed;
1096 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001097
1098 // Pressure
1099 enum PressureCalibration {
1100 PRESSURE_CALIBRATION_DEFAULT,
1101 PRESSURE_CALIBRATION_NONE,
1102 PRESSURE_CALIBRATION_PHYSICAL,
1103 PRESSURE_CALIBRATION_AMPLITUDE,
1104 };
Jeff Brown8d608662010-08-30 03:02:23 -07001105
1106 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001107 bool havePressureScale;
1108 float pressureScale;
1109
Jeff Brown8d608662010-08-30 03:02:23 -07001110 // Orientation
1111 enum OrientationCalibration {
1112 ORIENTATION_CALIBRATION_DEFAULT,
1113 ORIENTATION_CALIBRATION_NONE,
1114 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001115 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001116 };
1117
1118 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001119
1120 // Distance
1121 enum DistanceCalibration {
1122 DISTANCE_CALIBRATION_DEFAULT,
1123 DISTANCE_CALIBRATION_NONE,
1124 DISTANCE_CALIBRATION_SCALED,
1125 };
1126
1127 DistanceCalibration distanceCalibration;
1128 bool haveDistanceScale;
1129 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001130
1131 inline void applySizeScaleAndBias(float* outSize) const {
1132 if (haveSizeScale) {
1133 *outSize *= sizeScale;
1134 }
1135 if (haveSizeBias) {
1136 *outSize += sizeBias;
1137 }
1138 }
Jeff Brown8d608662010-08-30 03:02:23 -07001139 } mCalibration;
1140
Jeff Brownbe1aa822011-07-27 16:04:54 -07001141 // Raw pointer axis information from the driver.
1142 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001143
Jeff Brownbe1aa822011-07-27 16:04:54 -07001144 // Raw pointer sample data.
1145 RawPointerData mCurrentRawPointerData;
1146 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001147
Jeff Brownbe1aa822011-07-27 16:04:54 -07001148 // Cooked pointer sample data.
1149 CookedPointerData mCurrentCookedPointerData;
1150 CookedPointerData mLastCookedPointerData;
1151
1152 // Button state.
1153 int32_t mCurrentButtonState;
1154 int32_t mLastButtonState;
1155
Jeff Brown65fd2512011-08-18 11:20:58 -07001156 // Scroll state.
1157 int32_t mCurrentRawVScroll;
1158 int32_t mCurrentRawHScroll;
1159
1160 // Id bits used to differentiate fingers, stylus and mouse tools.
1161 BitSet32 mCurrentFingerIdBits; // finger or unknown
1162 BitSet32 mLastFingerIdBits;
1163 BitSet32 mCurrentStylusIdBits; // stylus or eraser
1164 BitSet32 mLastStylusIdBits;
1165 BitSet32 mCurrentMouseIdBits; // mouse or lens
1166 BitSet32 mLastMouseIdBits;
1167
Jeff Brownbe1aa822011-07-27 16:04:54 -07001168 // True if we sent a HOVER_ENTER event.
1169 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001170
1171 // The time the primary pointer last went down.
1172 nsecs_t mDownTime;
1173
Jeff Brownace13b12011-03-09 17:39:48 -08001174 // The pointer controller, or null if the device is not a pointer.
1175 sp<PointerControllerInterface> mPointerController;
1176
Jeff Brownbe1aa822011-07-27 16:04:54 -07001177 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001178
Jeff Brown8d608662010-08-30 03:02:23 -07001179 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001180 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001181 virtual void configureRawPointerAxes();
1182 virtual void dumpRawPointerAxes(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001183 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001184 virtual void dumpSurface(String8& dump);
1185 virtual void configureVirtualKeys();
1186 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001187 virtual void parseCalibration();
1188 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001189 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001190
Jeff Brown65fd2512011-08-18 11:20:58 -07001191 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001192
1193private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001194 // The surface orientation and width and height set by configureSurface().
1195 int32_t mSurfaceOrientation;
1196 int32_t mSurfaceWidth;
1197 int32_t mSurfaceHeight;
1198
1199 // The associated display orientation and width and height set by configureSurface().
1200 int32_t mAssociatedDisplayOrientation;
1201 int32_t mAssociatedDisplayWidth;
1202 int32_t mAssociatedDisplayHeight;
1203
1204 // Translation and scaling factors, orientation-independent.
1205 float mXScale;
1206 float mXPrecision;
1207
1208 float mYScale;
1209 float mYPrecision;
1210
1211 float mGeometricScale;
1212
Jeff Brownbe1aa822011-07-27 16:04:54 -07001213 float mPressureScale;
1214
1215 float mSizeScale;
1216
Jeff Brown65fd2512011-08-18 11:20:58 -07001217 float mOrientationCenter;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001218 float mOrientationScale;
1219
1220 float mDistanceScale;
1221
Jeff Brown65fd2512011-08-18 11:20:58 -07001222 bool mHaveTilt;
1223 float mTiltXCenter;
1224 float mTiltXScale;
1225 float mTiltYCenter;
1226 float mTiltYScale;
1227
Jeff Brownbe1aa822011-07-27 16:04:54 -07001228 // Oriented motion ranges for input device info.
1229 struct OrientedRanges {
1230 InputDeviceInfo::MotionRange x;
1231 InputDeviceInfo::MotionRange y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001232 InputDeviceInfo::MotionRange pressure;
1233
1234 bool haveSize;
1235 InputDeviceInfo::MotionRange size;
1236
1237 bool haveTouchSize;
1238 InputDeviceInfo::MotionRange touchMajor;
1239 InputDeviceInfo::MotionRange touchMinor;
1240
1241 bool haveToolSize;
1242 InputDeviceInfo::MotionRange toolMajor;
1243 InputDeviceInfo::MotionRange toolMinor;
1244
1245 bool haveOrientation;
1246 InputDeviceInfo::MotionRange orientation;
1247
1248 bool haveDistance;
1249 InputDeviceInfo::MotionRange distance;
Jeff Brown65fd2512011-08-18 11:20:58 -07001250
1251 bool haveTilt;
1252 InputDeviceInfo::MotionRange tilt;
1253
1254 OrientedRanges() {
1255 clear();
1256 }
1257
1258 void clear() {
1259 haveSize = false;
1260 haveTouchSize = false;
1261 haveToolSize = false;
1262 haveOrientation = false;
1263 haveDistance = false;
1264 haveTilt = false;
1265 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001266 } mOrientedRanges;
1267
1268 // Oriented dimensions and precision.
1269 float mOrientedSurfaceWidth;
1270 float mOrientedSurfaceHeight;
1271 float mOrientedXPrecision;
1272 float mOrientedYPrecision;
1273
1274 struct CurrentVirtualKeyState {
1275 bool down;
1276 bool ignored;
1277 nsecs_t downTime;
1278 int32_t keyCode;
1279 int32_t scanCode;
1280 } mCurrentVirtualKey;
1281
Jeff Brown65fd2512011-08-18 11:20:58 -07001282 // Scale factor for gesture or mouse based pointer movements.
1283 float mPointerXMovementScale;
1284 float mPointerYMovementScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001285
1286 // Scale factor for gesture based zooming and other freeform motions.
Jeff Brown65fd2512011-08-18 11:20:58 -07001287 float mPointerXZoomScale;
1288 float mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001289
1290 // The maximum swipe width.
1291 float mPointerGestureMaxSwipeWidth;
1292
Jeff Brown6d0fec22010-07-23 21:28:06 -07001293 struct PointerDistanceHeapElement {
1294 uint32_t currentPointerIndex : 8;
1295 uint32_t lastPointerIndex : 8;
1296 uint64_t distance : 48; // squared distance
1297 };
1298
Jeff Brown65fd2512011-08-18 11:20:58 -07001299 enum PointerUsage {
1300 POINTER_USAGE_NONE,
1301 POINTER_USAGE_GESTURES,
1302 POINTER_USAGE_STYLUS,
1303 POINTER_USAGE_MOUSE,
1304 };
1305 PointerUsage mPointerUsage;
1306
Jeff Brownace13b12011-03-09 17:39:48 -08001307 struct PointerGesture {
1308 enum Mode {
1309 // No fingers, button is not pressed.
1310 // Nothing happening.
1311 NEUTRAL,
1312
1313 // No fingers, button is not pressed.
1314 // Tap detected.
1315 // Emits DOWN and UP events at the pointer location.
1316 TAP,
1317
Jeff Brown79ac9692011-04-19 21:20:10 -07001318 // Exactly one finger dragging following a tap.
1319 // Pointer follows the active finger.
1320 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001321 //
1322 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001323 TAP_DRAG,
1324
Jeff Brownace13b12011-03-09 17:39:48 -08001325 // Button is pressed.
1326 // Pointer follows the active finger if there is one. Other fingers are ignored.
1327 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001328 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001329
1330 // Exactly one finger, button is not pressed.
1331 // Pointer follows the active finger.
1332 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001333 //
1334 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001335 HOVER,
1336
Jeff Brown2352b972011-04-12 22:39:53 -07001337 // Exactly two fingers but neither have moved enough to clearly indicate
1338 // whether a swipe or freeform gesture was intended. We consider the
1339 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1340 // Pointer does not move.
1341 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1342 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001343
1344 // Exactly two fingers moving in the same direction, button is not pressed.
1345 // Pointer does not move.
1346 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1347 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001348 SWIPE,
1349
1350 // Two or more fingers moving in arbitrary directions, button is not pressed.
1351 // Pointer does not move.
1352 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1353 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001354 FREEFORM,
1355
1356 // Waiting for quiet time to end before starting the next gesture.
1357 QUIET,
1358 };
1359
Jeff Brown2352b972011-04-12 22:39:53 -07001360 // Time the first finger went down.
1361 nsecs_t firstTouchTime;
1362
Jeff Brownace13b12011-03-09 17:39:48 -08001363 // The active pointer id from the raw touch data.
1364 int32_t activeTouchId; // -1 if none
1365
1366 // The active pointer id from the gesture last delivered to the application.
1367 int32_t activeGestureId; // -1 if none
1368
1369 // Pointer coords and ids for the current and previous pointer gesture.
1370 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001371 BitSet32 currentGestureIdBits;
1372 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001373 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001374 PointerCoords currentGestureCoords[MAX_POINTERS];
1375
1376 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001377 BitSet32 lastGestureIdBits;
1378 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001379 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001380 PointerCoords lastGestureCoords[MAX_POINTERS];
1381
Jeff Brownace13b12011-03-09 17:39:48 -08001382 // Time the pointer gesture last went down.
1383 nsecs_t downTime;
1384
Jeff Brown79ac9692011-04-19 21:20:10 -07001385 // Time when the pointer went down for a TAP.
1386 nsecs_t tapDownTime;
1387
1388 // Time when the pointer went up for a TAP.
1389 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001390
Jeff Brown2352b972011-04-12 22:39:53 -07001391 // Location of initial tap.
1392 float tapX, tapY;
1393
Jeff Brownace13b12011-03-09 17:39:48 -08001394 // Time we started waiting for quiescence.
1395 nsecs_t quietTime;
1396
Jeff Brown2352b972011-04-12 22:39:53 -07001397 // Reference points for multitouch gestures.
1398 float referenceTouchX; // reference touch X/Y coordinates in surface units
1399 float referenceTouchY;
1400 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1401 float referenceGestureY;
1402
Jeff Brown538881e2011-05-25 18:23:38 -07001403 // Distance that each pointer has traveled which has not yet been
1404 // subsumed into the reference gesture position.
1405 BitSet32 referenceIdBits;
1406 struct Delta {
1407 float dx, dy;
1408 };
1409 Delta referenceDeltas[MAX_POINTER_ID + 1];
1410
Jeff Brown2352b972011-04-12 22:39:53 -07001411 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1412 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1413
Jeff Brownace13b12011-03-09 17:39:48 -08001414 // A velocity tracker for determining whether to switch active pointers during drags.
1415 VelocityTracker velocityTracker;
1416
1417 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001418 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001419 activeTouchId = -1;
1420 activeGestureId = -1;
1421 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001422 currentGestureIdBits.clear();
1423 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001424 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001425 downTime = 0;
1426 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001427 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001428 resetQuietTime();
1429 }
1430
Jeff Brown79ac9692011-04-19 21:20:10 -07001431 void resetTap() {
1432 tapDownTime = LLONG_MIN;
1433 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001434 }
1435
1436 void resetQuietTime() {
1437 quietTime = LLONG_MIN;
1438 }
1439 } mPointerGesture;
1440
Jeff Brown65fd2512011-08-18 11:20:58 -07001441 struct PointerSimple {
1442 PointerCoords currentCoords;
1443 PointerProperties currentProperties;
1444 PointerCoords lastCoords;
1445 PointerProperties lastProperties;
1446
1447 // True if the pointer is down.
1448 bool down;
1449
1450 // True if the pointer is hovering.
1451 bool hovering;
1452
1453 // Time the pointer last went down.
1454 nsecs_t downTime;
1455
1456 void reset() {
1457 currentCoords.clear();
1458 currentProperties.clear();
1459 lastCoords.clear();
1460 lastProperties.clear();
1461 down = false;
1462 hovering = false;
1463 downTime = 0;
1464 }
1465 } mPointerSimple;
1466
1467 // The pointer and scroll velocity controls.
1468 VelocityControl mPointerVelocityControl;
1469 VelocityControl mWheelXVelocityControl;
1470 VelocityControl mWheelYVelocityControl;
1471
1472 void sync(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001473
Jeff Brownbe1aa822011-07-27 16:04:54 -07001474 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1475 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1476 int32_t keyEventAction, int32_t keyEventFlags);
1477
Jeff Brown6d0fec22010-07-23 21:28:06 -07001478 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001479 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1480 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1481 void cookPointerData();
1482
Jeff Brown65fd2512011-08-18 11:20:58 -07001483 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1484 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1485
Jeff Brown79ac9692011-04-19 21:20:10 -07001486 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
Jeff Brown65fd2512011-08-18 11:20:58 -07001487 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Jeff Brown79ac9692011-04-19 21:20:10 -07001488 bool preparePointerGestures(nsecs_t when,
Jeff Brown65fd2512011-08-18 11:20:58 -07001489 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1490 bool isTimeout);
1491
1492 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1493 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1494
1495 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1496 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1497
1498 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1499 bool down, bool hovering);
1500 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001501
1502 // Dispatches a motion event.
1503 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1504 // method will take care of setting the index and transmuting the action to DOWN or UP
1505 // it is the first / last pointer to go down / up.
1506 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001507 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1508 int32_t edgeFlags,
1509 const PointerProperties* properties, const PointerCoords* coords,
1510 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001511 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1512
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001513 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001514 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001515 bool updateMovedPointers(const PointerProperties* inProperties,
1516 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1517 PointerProperties* outProperties, PointerCoords* outCoords,
1518 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001519
Jeff Brownbe1aa822011-07-27 16:04:54 -07001520 bool isPointInsideSurface(int32_t x, int32_t y);
1521 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001522
Jeff Brownbe1aa822011-07-27 16:04:54 -07001523 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001524};
1525
1526
1527class SingleTouchInputMapper : public TouchInputMapper {
1528public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001529 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001530 virtual ~SingleTouchInputMapper();
1531
Jeff Brown65fd2512011-08-18 11:20:58 -07001532 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001533 virtual void process(const RawEvent* rawEvent);
1534
1535protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001536 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001537 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001538
1539private:
Jeff Brown49754db2011-07-01 17:37:58 -07001540 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001541};
1542
1543
1544class MultiTouchInputMapper : public TouchInputMapper {
1545public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001546 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001547 virtual ~MultiTouchInputMapper();
1548
Jeff Brown65fd2512011-08-18 11:20:58 -07001549 virtual void reset(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001550 virtual void process(const RawEvent* rawEvent);
1551
1552protected:
Jeff Brown65fd2512011-08-18 11:20:58 -07001553 virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001554 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001555
1556private:
Jeff Brown49754db2011-07-01 17:37:58 -07001557 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001558
Jeff Brown6894a292011-07-01 17:59:27 -07001559 // Specifies the pointer id bits that are in use, and their associated tracking id.
1560 BitSet32 mPointerIdBits;
1561 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001562};
1563
Jeff Browncb1404e2011-01-15 18:14:15 -08001564
1565class JoystickInputMapper : public InputMapper {
1566public:
1567 JoystickInputMapper(InputDevice* device);
1568 virtual ~JoystickInputMapper();
1569
1570 virtual uint32_t getSources();
1571 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1572 virtual void dump(String8& dump);
Jeff Brown65fd2512011-08-18 11:20:58 -07001573 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1574 virtual void reset(nsecs_t when);
Jeff Browncb1404e2011-01-15 18:14:15 -08001575 virtual void process(const RawEvent* rawEvent);
1576
1577private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001578 struct Axis {
1579 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001580 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001581
Jeff Brown6f2fba42011-02-19 01:08:02 -08001582 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001583
Jeff Brown6f2fba42011-02-19 01:08:02 -08001584 float scale; // scale factor from raw to normalized values
1585 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001586 float highScale; // scale factor from raw to normalized values of high split
1587 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001588
Jeff Brown6f2fba42011-02-19 01:08:02 -08001589 float min; // normalized inclusive minimum
1590 float max; // normalized inclusive maximum
1591 float flat; // normalized flat region size
1592 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001593
Jeff Brown6f2fba42011-02-19 01:08:02 -08001594 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001595 float currentValue; // current value
1596 float newValue; // most recent value
1597 float highCurrentValue; // current value of high split
1598 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001599
Jeff Brown85297452011-03-04 13:07:49 -08001600 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1601 bool explicitlyMapped, float scale, float offset,
1602 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001603 float min, float max, float flat, float fuzz) {
1604 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001605 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001606 this->explicitlyMapped = explicitlyMapped;
1607 this->scale = scale;
1608 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001609 this->highScale = highScale;
1610 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001611 this->min = min;
1612 this->max = max;
1613 this->flat = flat;
1614 this->fuzz = fuzz;
1615 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001616 resetValue();
1617 }
1618
1619 void resetValue() {
1620 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001621 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001622 this->highCurrentValue = 0;
1623 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001624 }
1625 };
1626
Jeff Brown6f2fba42011-02-19 01:08:02 -08001627 // Axes indexed by raw ABS_* axis index.
1628 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001629
Jeff Brown6f2fba42011-02-19 01:08:02 -08001630 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001631
Jeff Brown85297452011-03-04 13:07:49 -08001632 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001633 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001634 bool filterAxes(bool force);
1635
1636 static bool hasValueChangedSignificantly(float filter,
1637 float newValue, float currentValue, float min, float max);
1638 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1639 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001640
Jeff Brown6f2fba42011-02-19 01:08:02 -08001641 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001642};
1643
Jeff Brown46b9ac02010-04-22 18:58:52 -07001644} // namespace android
1645
1646#endif // _UI_INPUT_READER_H