blob: 72802fc7fab76233ca0895bb0b4afc2d16c26661 [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
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31#include <utils/BitSet.h>
32
33#include <stddef.h>
34#include <unistd.h>
35
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown214eaf42011-05-26 19:17:02 -070043 * Input reader configuration.
44 *
45 * Specifies various options that modify the behavior of the input reader.
46 */
47struct InputReaderConfiguration {
Jeff Brown474dcb52011-06-14 20:22:50 -070048 // Describes changes that have occurred.
49 enum {
50 // The pointer speed changed.
51 CHANGE_POINTER_SPEED = 1 << 0,
52
53 // The pointer gesture control changed.
54 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
55
56 // All devices must be reopened.
57 CHANGE_MUST_REOPEN = 1 << 31,
58 };
59
Jeff Brown214eaf42011-05-26 19:17:02 -070060 // Gets the amount of time to disable virtual keys after the screen is touched
61 // in order to filter out accidental virtual key presses due to swiping gestures
62 // or taps near the edge of the display. May be 0 to disable the feature.
63 nsecs_t virtualKeyQuietTime;
64
65 // The excluded device names for the platform.
66 // Devices with these names will be ignored.
67 Vector<String8> excludedDeviceNames;
68
Jeff Brown19c97d462011-06-01 12:33:19 -070069 // Velocity control parameters for mouse pointer movements.
70 VelocityControlParameters pointerVelocityControlParameters;
71
72 // Velocity control parameters for mouse wheel movements.
73 VelocityControlParameters wheelVelocityControlParameters;
74
Jeff Brown474dcb52011-06-14 20:22:50 -070075 // True if pointer gestures are enabled.
76 bool pointerGesturesEnabled;
77
Jeff Brown214eaf42011-05-26 19:17:02 -070078 // Quiet time between certain pointer gesture transitions.
79 // Time to allow for all fingers or buttons to settle into a stable state before
80 // starting a new gesture.
81 nsecs_t pointerGestureQuietInterval;
82
83 // The minimum speed that a pointer must travel for us to consider switching the active
84 // touch pointer to it during a drag. This threshold is set to avoid switching due
85 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
86 float pointerGestureDragMinSwitchSpeed; // in pixels per second
87
88 // Tap gesture delay time.
89 // The time between down and up must be less than this to be considered a tap.
90 nsecs_t pointerGestureTapInterval;
91
92 // Tap drag gesture delay time.
93 // The time between the previous tap's up and the next down must be less than
94 // this to be considered a drag. Otherwise, the previous tap is finished and a
95 // new tap begins.
96 //
97 // Note that the previous tap will be held down for this entire duration so this
98 // interval must be shorter than the long press timeout.
99 nsecs_t pointerGestureTapDragInterval;
100
101 // The distance in pixels that the pointer is allowed to move from initial down
102 // to up and still be called a tap.
103 float pointerGestureTapSlop; // in pixels
104
105 // Time after the first touch points go down to settle on an initial centroid.
106 // This is intended to be enough time to handle cases where the user puts down two
107 // fingers at almost but not quite exactly the same time.
108 nsecs_t pointerGestureMultitouchSettleInterval;
109
110 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700111 // at least two pointers have moved at least this far from their starting place.
112 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700113
114 // The transition from PRESS to SWIPE gesture mode can only occur when the
115 // cosine of the angle between the two vectors is greater than or equal to than this value
116 // which indicates that the vectors are oriented in the same direction.
117 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
118 // (In exactly opposite directions, the cosine is -1.0.)
119 float pointerGestureSwipeTransitionAngleCosine;
120
121 // The transition from PRESS to SWIPE gesture mode can only occur when the
122 // fingers are no more than this far apart relative to the diagonal size of
123 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
124 // no more than half the diagonal size of the touch pad apart.
125 float pointerGestureSwipeMaxWidthRatio;
126
127 // The gesture movement speed factor relative to the size of the display.
128 // Movement speed applies when the fingers are moving in the same direction.
129 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
130 // will cover this portion of the display diagonal.
131 float pointerGestureMovementSpeedRatio;
132
133 // The gesture zoom speed factor relative to the size of the display.
134 // Zoom speed applies when the fingers are mostly moving relative to each other
135 // to execute a scale gesture or similar.
136 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
137 // will cover this portion of the display diagonal.
138 float pointerGestureZoomSpeedRatio;
139
140 InputReaderConfiguration() :
Jeff Brown214eaf42011-05-26 19:17:02 -0700141 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700142 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700143 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700144 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700145 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
146 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
147 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
148 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
149 pointerGestureTapSlop(10.0f), // 10 pixels
150 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700151 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700152 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700153 pointerGestureSwipeMaxWidthRatio(0.25f),
154 pointerGestureMovementSpeedRatio(0.8f),
Jeff Brown214eaf42011-05-26 19:17:02 -0700155 pointerGestureZoomSpeedRatio(0.3f) { }
156};
157
158
159/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700160 * Input reader policy interface.
161 *
162 * The input reader policy is used by the input reader to interact with the Window Manager
163 * and other system components.
164 *
165 * The actual implementation is partially supported by callbacks into the DVM
166 * via JNI. This interface is also mocked in the unit tests.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700167 *
168 * These methods must NOT re-enter the input reader since they may be called while
169 * holding the input reader lock.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700170 */
171class InputReaderPolicyInterface : public virtual RefBase {
172protected:
173 InputReaderPolicyInterface() { }
174 virtual ~InputReaderPolicyInterface() { }
175
176public:
177 /* Display orientations. */
178 enum {
179 ROTATION_0 = 0,
180 ROTATION_90 = 1,
181 ROTATION_180 = 2,
182 ROTATION_270 = 3
183 };
184
Jeff Brown9c3cda02010-06-15 01:31:58 -0700185 /* Gets information about the display with the specified id.
Jeff Brownbc68a592011-07-25 12:58:12 -0700186 * If external is true, returns the size of the external mirrored
187 * counterpart of the specified display.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188 * Returns true if the display info is available, false otherwise.
189 */
Jeff Brownbc68a592011-07-25 12:58:12 -0700190 virtual bool getDisplayInfo(int32_t displayId, bool external,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700191 int32_t* width, int32_t* height, int32_t* orientation) = 0;
192
Jeff Brown214eaf42011-05-26 19:17:02 -0700193 /* Gets the input reader configuration. */
194 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800195
196 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
197 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198};
199
200
Jeff Brownbe1aa822011-07-27 16:04:54 -0700201/* Processes raw input events and sends cooked event data to an input listener. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700202class InputReaderInterface : public virtual RefBase {
203protected:
204 InputReaderInterface() { }
205 virtual ~InputReaderInterface() { }
206
207public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700208 /* Dumps the state of the input reader.
209 *
210 * This method may be called on any thread (usually by the input manager). */
211 virtual void dump(String8& dump) = 0;
212
Jeff Brown89ef0722011-08-10 16:25:21 -0700213 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
214 virtual void monitor() = 0;
215
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700216 /* Runs a single iteration of the processing loop.
217 * Nominally reads and processes one incoming message from the EventHub.
218 *
219 * This method should be called on the input reader thread.
220 */
221 virtual void loopOnce() = 0;
222
Jeff Brown9c3cda02010-06-15 01:31:58 -0700223 /* Gets the current input device configuration.
224 *
225 * This method may be called on any thread (usually by the input manager).
226 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700227 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700228
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 /* Gets information about the specified input device.
230 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
231 * was no such device.
232 *
233 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700234 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700235 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
236
237 /* Gets the list of all registered device ids. */
238 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
239
240 /* Query current input state. */
241 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
242 int32_t scanCode) = 0;
243 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
244 int32_t keyCode) = 0;
245 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
246 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700247
248 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
250 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700251
Jeff Brown474dcb52011-06-14 20:22:50 -0700252 /* Requests that a reconfiguration of all input devices.
253 * The changes flag is a bitfield that indicates what has changed and whether
254 * the input devices must all be reopened. */
255 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700256};
257
258
259/* Internal interface used by individual input devices to access global input device state
260 * and parameters maintained by the input reader.
261 */
262class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700263public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 InputReaderContext() { }
265 virtual ~InputReaderContext() { }
266
Jeff Brown6d0fec22010-07-23 21:28:06 -0700267 virtual void updateGlobalMetaState() = 0;
268 virtual int32_t getGlobalMetaState() = 0;
269
Jeff Brownfe508922011-01-18 15:10:10 -0800270 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
271 virtual bool shouldDropVirtualKey(nsecs_t now,
272 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
273
Jeff Brown05dc66a2011-03-02 14:41:58 -0800274 virtual void fadePointer() = 0;
275
Jeff Brownaa3855d2011-03-17 01:34:19 -0700276 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
277
Jeff Brown6d0fec22010-07-23 21:28:06 -0700278 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700279 virtual InputListenerInterface* getListener() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700280 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700281};
282
Jeff Brown9c3cda02010-06-15 01:31:58 -0700283
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700284/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brownbe1aa822011-07-27 16:04:54 -0700285 * that it sends to the input listener. Some functions of the input reader, such as early
Jeff Brown9c3cda02010-06-15 01:31:58 -0700286 * event filtering in low power states, are controlled by a separate policy object.
287 *
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 * The InputReader owns a collection of InputMappers. Most of the work it does happens
289 * on the input reader thread but the InputReader can receive queries from other system
290 * components running on arbitrary threads. To keep things manageable, the InputReader
291 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
292 * EventHub or the InputReaderPolicy but it is never held while calling into the
293 * InputListener.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700294 */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295class InputReader : public InputReaderInterface {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296public:
297 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700298 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700299 const sp<InputListenerInterface>& listener);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700300 virtual ~InputReader();
301
Jeff Brownb88102f2010-09-08 11:49:43 -0700302 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700303 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700304
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700305 virtual void loopOnce();
306
Jeff Brown6d0fec22010-07-23 21:28:06 -0700307 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700308
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
310 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700311
Jeff Brown6d0fec22010-07-23 21:28:06 -0700312 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
313 int32_t scanCode);
314 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
315 int32_t keyCode);
316 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
317 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700318
Jeff Brown6d0fec22010-07-23 21:28:06 -0700319 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
320 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700321
Jeff Brown474dcb52011-06-14 20:22:50 -0700322 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700323
Jeff Brownc3db8582010-10-20 15:33:38 -0700324protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700325 // These members are protected so they can be instrumented by test cases.
326 virtual InputDevice* createDeviceLocked(int32_t deviceId,
327 const String8& name, uint32_t classes);
328
329 class ContextImpl : public InputReaderContext {
330 InputReader* mReader;
331
332 public:
333 ContextImpl(InputReader* reader);
334
335 virtual void updateGlobalMetaState();
336 virtual int32_t getGlobalMetaState();
337 virtual void disableVirtualKeysUntil(nsecs_t time);
338 virtual bool shouldDropVirtualKey(nsecs_t now,
339 InputDevice* device, int32_t keyCode, int32_t scanCode);
340 virtual void fadePointer();
341 virtual void requestTimeoutAtTime(nsecs_t when);
342 virtual InputReaderPolicyInterface* getPolicy();
343 virtual InputListenerInterface* getListener();
344 virtual EventHubInterface* getEventHub();
345 } mContext;
346
347 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700348
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700349private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700350 Mutex mLock;
351
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700352 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700354 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700355
Jeff Brown214eaf42011-05-26 19:17:02 -0700356 InputReaderConfiguration mConfig;
357
Jeff Brownb7198742011-03-18 18:14:26 -0700358 // The event queue.
359 static const int EVENT_BUFFER_SIZE = 256;
360 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
361
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700362 KeyedVector<int32_t, InputDevice*> mDevices;
363
Jeff Brown6d0fec22010-07-23 21:28:06 -0700364 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700365 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700366
Jeff Brownbe1aa822011-07-27 16:04:54 -0700367 void addDeviceLocked(int32_t deviceId);
368 void removeDeviceLocked(int32_t deviceId);
369 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
370 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700371
Jeff Brownbe1aa822011-07-27 16:04:54 -0700372 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700373
Jeff Brownbe1aa822011-07-27 16:04:54 -0700374 int32_t mGlobalMetaState;
375 void updateGlobalMetaStateLocked();
376 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377
Jeff Brownbe1aa822011-07-27 16:04:54 -0700378 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379
Jeff Brownbe1aa822011-07-27 16:04:54 -0700380 InputConfiguration mInputConfiguration;
381 void updateInputConfigurationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800382
Jeff Brownbe1aa822011-07-27 16:04:54 -0700383 nsecs_t mDisableVirtualKeysTimeout;
384 void disableVirtualKeysUntilLocked(nsecs_t time);
385 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800386 InputDevice* device, int32_t keyCode, int32_t scanCode);
387
Jeff Brownbe1aa822011-07-27 16:04:54 -0700388 nsecs_t mNextTimeout;
389 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700390
Jeff Brownbe1aa822011-07-27 16:04:54 -0700391 uint32_t mConfigurationChangesToRefresh;
392 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700393
Jeff Brown6d0fec22010-07-23 21:28:06 -0700394 // state queries
395 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700396 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700397 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700398 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700399 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700400};
401
402
403/* Reads raw events from the event hub and processes them, endlessly. */
404class InputReaderThread : public Thread {
405public:
406 InputReaderThread(const sp<InputReaderInterface>& reader);
407 virtual ~InputReaderThread();
408
409private:
410 sp<InputReaderInterface> mReader;
411
412 virtual bool threadLoop();
413};
414
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415
416/* Represents the state of a single input device. */
417class InputDevice {
418public:
419 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
420 ~InputDevice();
421
422 inline InputReaderContext* getContext() { return mContext; }
423 inline int32_t getId() { return mId; }
424 inline const String8& getName() { return mName; }
425 inline uint32_t getSources() { return mSources; }
426
Jeff Brown56194eb2011-03-02 19:23:13 -0800427 inline bool isExternal() { return mIsExternal; }
428 inline void setExternal(bool external) { mIsExternal = external; }
429
Jeff Brown6d0fec22010-07-23 21:28:06 -0700430 inline bool isIgnored() { return mMappers.isEmpty(); }
431
Jeff Brownef3d7e82010-09-30 14:33:04 -0700432 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433 void addMapper(InputMapper* mapper);
Jeff Brown474dcb52011-06-14 20:22:50 -0700434 void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 void reset();
Jeff Brownb7198742011-03-18 18:14:26 -0700436 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700437 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700438
439 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
440 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
441 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
442 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
443 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
444 const int32_t* keyCodes, uint8_t* outFlags);
445
446 int32_t getMetaState();
447
Jeff Brown05dc66a2011-03-02 14:41:58 -0800448 void fadePointer();
449
Jeff Brown49754db2011-07-01 17:37:58 -0700450 inline const PropertyMap& getConfiguration() { return mConfiguration; }
451 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700452
Jeff Brown6d0fec22010-07-23 21:28:06 -0700453private:
454 InputReaderContext* mContext;
455 int32_t mId;
456
457 Vector<InputMapper*> mMappers;
458
459 String8 mName;
460 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800461 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700462 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700463
464 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
465 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700466
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800467 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700468};
469
470
Jeff Brown49754db2011-07-01 17:37:58 -0700471/* Keeps track of the state of mouse or touch pad buttons. */
472class CursorButtonAccumulator {
473public:
474 CursorButtonAccumulator();
475
476 void clearButtons();
477 void process(const RawEvent* rawEvent);
478
479 uint32_t getButtonState() const;
480
481private:
482 bool mBtnLeft;
483 bool mBtnRight;
484 bool mBtnMiddle;
485 bool mBtnBack;
486 bool mBtnSide;
487 bool mBtnForward;
488 bool mBtnExtra;
489 bool mBtnTask;
490};
491
492
493/* Keeps track of cursor movements. */
494
495class CursorMotionAccumulator {
496public:
497 CursorMotionAccumulator();
498 void configure(InputDevice* device);
499
500 void clearRelativeAxes();
501 void process(const RawEvent* rawEvent);
502
503 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
504 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
505
506 inline int32_t getRelativeX() const { return mRelX; }
507 inline int32_t getRelativeY() const { return mRelY; }
508 inline int32_t getRelativeVWheel() const { return mRelWheel; }
509 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
510
511private:
512 bool mHaveRelWheel;
513 bool mHaveRelHWheel;
514
515 int32_t mRelX;
516 int32_t mRelY;
517 int32_t mRelWheel;
518 int32_t mRelHWheel;
519};
520
521
522/* Keeps track of the state of touch, stylus and tool buttons. */
523class TouchButtonAccumulator {
524public:
525 TouchButtonAccumulator();
526 void configure(InputDevice* device);
527
528 void clearButtons();
529 void process(const RawEvent* rawEvent);
530
531 uint32_t getButtonState() const;
532 int32_t getToolType() const;
Jeff Brownd87c6d52011-08-10 14:55:59 -0700533 bool isToolActive() const;
Jeff Brown49754db2011-07-01 17:37:58 -0700534 bool isHovering() const;
535
536private:
537 bool mHaveBtnTouch;
538
539 bool mBtnTouch;
540 bool mBtnStylus;
541 bool mBtnStylus2;
542 bool mBtnToolFinger;
543 bool mBtnToolPen;
544 bool mBtnToolRubber;
545};
546
547
Jeff Brownbe1aa822011-07-27 16:04:54 -0700548/* Raw axis information from the driver. */
549struct RawPointerAxes {
550 RawAbsoluteAxisInfo x;
551 RawAbsoluteAxisInfo y;
552 RawAbsoluteAxisInfo pressure;
553 RawAbsoluteAxisInfo touchMajor;
554 RawAbsoluteAxisInfo touchMinor;
555 RawAbsoluteAxisInfo toolMajor;
556 RawAbsoluteAxisInfo toolMinor;
557 RawAbsoluteAxisInfo orientation;
558 RawAbsoluteAxisInfo distance;
559 RawAbsoluteAxisInfo trackingId;
560 RawAbsoluteAxisInfo slot;
561
562 RawPointerAxes();
563 void clear();
564};
565
566
567/* Raw data for a collection of pointers including a pointer id mapping table. */
568struct RawPointerData {
569 struct Pointer {
570 uint32_t id;
571 int32_t x;
572 int32_t y;
573 int32_t pressure;
574 int32_t touchMajor;
575 int32_t touchMinor;
576 int32_t toolMajor;
577 int32_t toolMinor;
578 int32_t orientation;
579 int32_t distance;
580 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
581 bool isHovering;
582 };
583
584 uint32_t pointerCount;
585 Pointer pointers[MAX_POINTERS];
586 BitSet32 hoveringIdBits, touchingIdBits;
587 uint32_t idToIndex[MAX_POINTER_ID + 1];
588
589 RawPointerData();
590 void clear();
591 void copyFrom(const RawPointerData& other);
592 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
593
594 inline void markIdBit(uint32_t id, bool isHovering) {
595 if (isHovering) {
596 hoveringIdBits.markBit(id);
597 } else {
598 touchingIdBits.markBit(id);
599 }
600 }
601
602 inline void clearIdBits() {
603 hoveringIdBits.clear();
604 touchingIdBits.clear();
605 }
606
607 inline const Pointer& pointerForId(uint32_t id) const {
608 return pointers[idToIndex[id]];
609 }
610
611 inline bool isHovering(uint32_t pointerIndex) {
612 return pointers[pointerIndex].isHovering;
613 }
614};
615
616
617/* Cooked data for a collection of pointers including a pointer id mapping table. */
618struct CookedPointerData {
619 uint32_t pointerCount;
620 PointerProperties pointerProperties[MAX_POINTERS];
621 PointerCoords pointerCoords[MAX_POINTERS];
622 BitSet32 hoveringIdBits, touchingIdBits;
623 uint32_t idToIndex[MAX_POINTER_ID + 1];
624
625 CookedPointerData();
626 void clear();
627 void copyFrom(const CookedPointerData& other);
628
629 inline bool isHovering(uint32_t pointerIndex) {
630 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
631 }
632};
633
634
Jeff Brown49754db2011-07-01 17:37:58 -0700635/* Keeps track of the state of single-touch protocol. */
636class SingleTouchMotionAccumulator {
637public:
638 SingleTouchMotionAccumulator();
639
640 void clearAbsoluteAxes();
641 void process(const RawEvent* rawEvent);
642
643 inline int32_t getAbsoluteX() const { return mAbsX; }
644 inline int32_t getAbsoluteY() const { return mAbsY; }
645 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
646 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
647 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
648
649private:
650 int32_t mAbsX;
651 int32_t mAbsY;
652 int32_t mAbsPressure;
653 int32_t mAbsToolWidth;
654 int32_t mAbsDistance;
655};
656
657
658/* Keeps track of the state of multi-touch protocol. */
659class MultiTouchMotionAccumulator {
660public:
661 class Slot {
662 public:
663 inline bool isInUse() const { return mInUse; }
664 inline int32_t getX() const { return mAbsMTPositionX; }
665 inline int32_t getY() const { return mAbsMTPositionY; }
666 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
667 inline int32_t getTouchMinor() const {
668 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
669 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
670 inline int32_t getToolMinor() const {
671 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
672 inline int32_t getOrientation() const { return mAbsMTOrientation; }
673 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
674 inline int32_t getPressure() const { return mAbsMTPressure; }
675 inline int32_t getDistance() const { return mAbsMTDistance; }
676 inline int32_t getToolType() const;
677
678 private:
679 friend class MultiTouchMotionAccumulator;
680
681 bool mInUse;
682 bool mHaveAbsMTTouchMinor;
683 bool mHaveAbsMTWidthMinor;
684 bool mHaveAbsMTToolType;
685
686 int32_t mAbsMTPositionX;
687 int32_t mAbsMTPositionY;
688 int32_t mAbsMTTouchMajor;
689 int32_t mAbsMTTouchMinor;
690 int32_t mAbsMTWidthMajor;
691 int32_t mAbsMTWidthMinor;
692 int32_t mAbsMTOrientation;
693 int32_t mAbsMTTrackingId;
694 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700695 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700696 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700697
698 Slot();
Jeff Brown49754db2011-07-01 17:37:58 -0700699 void clear();
700 };
701
702 MultiTouchMotionAccumulator();
703 ~MultiTouchMotionAccumulator();
704
705 void configure(size_t slotCount, bool usingSlotsProtocol);
706 void process(const RawEvent* rawEvent);
707
708 void clearSlots(int32_t initialSlot);
709
710 inline bool isUsingSlotsProtocol() const { return mUsingSlotsProtocol; }
711 inline size_t getSlotCount() const { return mSlotCount; }
712 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
713
714private:
715 int32_t mCurrentSlot;
716 Slot* mSlots;
717 size_t mSlotCount;
718 bool mUsingSlotsProtocol;
719};
720
721
Jeff Brown6d0fec22010-07-23 21:28:06 -0700722/* An input mapper transforms raw input events into cooked event data.
723 * A single input device can have multiple associated input mappers in order to interpret
724 * different classes of events.
725 */
726class InputMapper {
727public:
728 InputMapper(InputDevice* device);
729 virtual ~InputMapper();
730
731 inline InputDevice* getDevice() { return mDevice; }
732 inline int32_t getDeviceId() { return mDevice->getId(); }
733 inline const String8 getDeviceName() { return mDevice->getName(); }
734 inline InputReaderContext* getContext() { return mContext; }
735 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700736 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700737 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
738
739 virtual uint32_t getSources() = 0;
740 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700741 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700742 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700743 virtual void reset();
744 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700745 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700746
747 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
748 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
749 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
750 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
751 const int32_t* keyCodes, uint8_t* outFlags);
752
753 virtual int32_t getMetaState();
754
Jeff Brown05dc66a2011-03-02 14:41:58 -0800755 virtual void fadePointer();
756
Jeff Brown6d0fec22010-07-23 21:28:06 -0700757protected:
758 InputDevice* mDevice;
759 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800760
Jeff Brownbe1aa822011-07-27 16:04:54 -0700761 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
762
Jeff Browncb1404e2011-01-15 18:14:15 -0800763 static void dumpRawAbsoluteAxisInfo(String8& dump,
764 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700765};
766
767
768class SwitchInputMapper : public InputMapper {
769public:
770 SwitchInputMapper(InputDevice* device);
771 virtual ~SwitchInputMapper();
772
773 virtual uint32_t getSources();
774 virtual void process(const RawEvent* rawEvent);
775
776 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
777
778private:
779 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
780};
781
782
783class KeyboardInputMapper : public InputMapper {
784public:
Jeff Brownefd32662011-03-08 15:13:06 -0800785 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700786 virtual ~KeyboardInputMapper();
787
788 virtual uint32_t getSources();
789 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700790 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700791 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700792 virtual void reset();
793 virtual void process(const RawEvent* rawEvent);
794
795 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
796 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
797 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
798 const int32_t* keyCodes, uint8_t* outFlags);
799
800 virtual int32_t getMetaState();
801
802private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700803 Mutex mLock;
804
Jeff Brown6d0fec22010-07-23 21:28:06 -0700805 struct KeyDown {
806 int32_t keyCode;
807 int32_t scanCode;
808 };
809
Jeff Brownefd32662011-03-08 15:13:06 -0800810 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700811 int32_t mKeyboardType;
812
Jeff Brownbe1aa822011-07-27 16:04:54 -0700813 Vector<KeyDown> mKeyDowns; // keys that are down
814 int32_t mMetaState;
815 nsecs_t mDownTime; // time of most recent key down
816
817 struct LedState {
818 bool avail; // led is available
819 bool on; // we think the led is currently on
820 };
821 LedState mCapsLockLedState;
822 LedState mNumLockLedState;
823 LedState mScrollLockLedState;
824
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800825 // Immutable configuration parameters.
826 struct Parameters {
827 int32_t associatedDisplayId;
828 bool orientationAware;
829 } mParameters;
830
Jeff Brownbe1aa822011-07-27 16:04:54 -0700831 void initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700832
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800833 void configureParameters();
834 void dumpParameters(String8& dump);
835
Jeff Brown6d0fec22010-07-23 21:28:06 -0700836 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700837
Jeff Brown6d0fec22010-07-23 21:28:06 -0700838 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
839 uint32_t policyFlags);
840
Jeff Brownbe1aa822011-07-27 16:04:54 -0700841 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700842
Jeff Brownbe1aa822011-07-27 16:04:54 -0700843 void resetLedState();
844 void initializeLedState(LedState& ledState, int32_t led);
845 void updateLedState(bool reset);
846 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -0700847 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700848};
849
850
Jeff Brown83c09682010-12-23 17:50:18 -0800851class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700852public:
Jeff Brown83c09682010-12-23 17:50:18 -0800853 CursorInputMapper(InputDevice* device);
854 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700855
856 virtual uint32_t getSources();
857 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700858 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700859 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700860 virtual void reset();
861 virtual void process(const RawEvent* rawEvent);
862
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700863 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
864
Jeff Brown05dc66a2011-03-02 14:41:58 -0800865 virtual void fadePointer();
866
Jeff Brown6d0fec22010-07-23 21:28:06 -0700867private:
868 // Amount that trackball needs to move in order to generate a key event.
869 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
870
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800871 // Immutable configuration parameters.
872 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800873 enum Mode {
874 MODE_POINTER,
875 MODE_NAVIGATION,
876 };
877
878 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800879 int32_t associatedDisplayId;
880 bool orientationAware;
881 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882
Jeff Brown49754db2011-07-01 17:37:58 -0700883 CursorButtonAccumulator mCursorButtonAccumulator;
884 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700885
Jeff Brownefd32662011-03-08 15:13:06 -0800886 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700887 float mXScale;
888 float mYScale;
889 float mXPrecision;
890 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800891
Jeff Brown6f2fba42011-02-19 01:08:02 -0800892 float mVWheelScale;
893 float mHWheelScale;
894
Jeff Brown19c97d462011-06-01 12:33:19 -0700895 // Velocity controls for mouse pointer and wheel movements.
896 // The controls for X and Y wheel movements are separate to keep them decoupled.
897 VelocityControl mPointerVelocityControl;
898 VelocityControl mWheelXVelocityControl;
899 VelocityControl mWheelYVelocityControl;
900
Jeff Brown83c09682010-12-23 17:50:18 -0800901 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700902
Jeff Brownbe1aa822011-07-27 16:04:54 -0700903 int32_t mButtonState;
904 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700905
Jeff Brownbe1aa822011-07-27 16:04:54 -0700906 void initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700907
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800908 void configureParameters();
909 void dumpParameters(String8& dump);
910
Jeff Brown6d0fec22010-07-23 21:28:06 -0700911 void sync(nsecs_t when);
912};
913
914
915class TouchInputMapper : public InputMapper {
916public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800917 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700918 virtual ~TouchInputMapper();
919
920 virtual uint32_t getSources();
921 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700922 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700923 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700924 virtual void reset();
925
926 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
927 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
928 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
929 const int32_t* keyCodes, uint8_t* outFlags);
930
Jeff Brownace13b12011-03-09 17:39:48 -0800931 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -0700932 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -0800933
Jeff Brown6d0fec22010-07-23 21:28:06 -0700934protected:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700935 struct VirtualKey {
936 int32_t keyCode;
937 int32_t scanCode;
938 uint32_t flags;
939
940 // computed hit box, specified in touch screen coords based on known display size
941 int32_t hitLeft;
942 int32_t hitTop;
943 int32_t hitRight;
944 int32_t hitBottom;
945
946 inline bool isHit(int32_t x, int32_t y) const {
947 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
948 }
949 };
950
Jeff Brown83c09682010-12-23 17:50:18 -0800951 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800952 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brownace13b12011-03-09 17:39:48 -0800953 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800954
Jeff Brown214eaf42011-05-26 19:17:02 -0700955 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -0700956 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -0700957
Jeff Brown6d0fec22010-07-23 21:28:06 -0700958 // Immutable configuration parameters.
959 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800960 enum DeviceType {
961 DEVICE_TYPE_TOUCH_SCREEN,
962 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -0800963 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800964 };
965
966 DeviceType deviceType;
967 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -0700968 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800969 bool orientationAware;
970
Jeff Brown2352b972011-04-12 22:39:53 -0700971 enum GestureMode {
972 GESTURE_MODE_POINTER,
973 GESTURE_MODE_SPOTS,
974 };
975 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700976 } mParameters;
977
Jeff Brown8d608662010-08-30 03:02:23 -0700978 // Immutable calibration parameters in parsed form.
979 struct Calibration {
Jeff Browna1f89ce2011-08-11 00:05:01 -0700980 // Size
981 enum SizeCalibration {
982 SIZE_CALIBRATION_DEFAULT,
983 SIZE_CALIBRATION_NONE,
984 SIZE_CALIBRATION_GEOMETRIC,
985 SIZE_CALIBRATION_DIAMETER,
986 SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700987 };
988
Jeff Browna1f89ce2011-08-11 00:05:01 -0700989 SizeCalibration sizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700990
Jeff Browna1f89ce2011-08-11 00:05:01 -0700991 bool haveSizeScale;
992 float sizeScale;
993 bool haveSizeBias;
994 float sizeBias;
995 bool haveSizeIsSummed;
996 bool sizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700997
998 // Pressure
999 enum PressureCalibration {
1000 PRESSURE_CALIBRATION_DEFAULT,
1001 PRESSURE_CALIBRATION_NONE,
1002 PRESSURE_CALIBRATION_PHYSICAL,
1003 PRESSURE_CALIBRATION_AMPLITUDE,
1004 };
Jeff Brown8d608662010-08-30 03:02:23 -07001005
1006 PressureCalibration pressureCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -07001007 bool havePressureScale;
1008 float pressureScale;
1009
Jeff Brown8d608662010-08-30 03:02:23 -07001010 // Orientation
1011 enum OrientationCalibration {
1012 ORIENTATION_CALIBRATION_DEFAULT,
1013 ORIENTATION_CALIBRATION_NONE,
1014 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001015 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001016 };
1017
1018 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001019
1020 // Distance
1021 enum DistanceCalibration {
1022 DISTANCE_CALIBRATION_DEFAULT,
1023 DISTANCE_CALIBRATION_NONE,
1024 DISTANCE_CALIBRATION_SCALED,
1025 };
1026
1027 DistanceCalibration distanceCalibration;
1028 bool haveDistanceScale;
1029 float distanceScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07001030
1031 inline void applySizeScaleAndBias(float* outSize) const {
1032 if (haveSizeScale) {
1033 *outSize *= sizeScale;
1034 }
1035 if (haveSizeBias) {
1036 *outSize += sizeBias;
1037 }
1038 }
Jeff Brown8d608662010-08-30 03:02:23 -07001039 } mCalibration;
1040
Jeff Brownbe1aa822011-07-27 16:04:54 -07001041 // Raw pointer axis information from the driver.
1042 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001043
Jeff Brownbe1aa822011-07-27 16:04:54 -07001044 // Raw pointer sample data.
1045 RawPointerData mCurrentRawPointerData;
1046 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001047
Jeff Brownbe1aa822011-07-27 16:04:54 -07001048 // Cooked pointer sample data.
1049 CookedPointerData mCurrentCookedPointerData;
1050 CookedPointerData mLastCookedPointerData;
1051
1052 // Button state.
1053 int32_t mCurrentButtonState;
1054 int32_t mLastButtonState;
1055
1056 // True if we sent a HOVER_ENTER event.
1057 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001058
1059 // The time the primary pointer last went down.
1060 nsecs_t mDownTime;
1061
Jeff Brownace13b12011-03-09 17:39:48 -08001062 // The pointer controller, or null if the device is not a pointer.
1063 sp<PointerControllerInterface> mPointerController;
1064
Jeff Brownbe1aa822011-07-27 16:04:54 -07001065 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001066
Jeff Brown8d608662010-08-30 03:02:23 -07001067 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001068 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001069 virtual void configureRawPointerAxes();
1070 virtual void dumpRawPointerAxes(String8& dump);
1071 virtual bool configureSurface();
1072 virtual void dumpSurface(String8& dump);
1073 virtual void configureVirtualKeys();
1074 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001075 virtual void parseCalibration();
1076 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001077 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001078
Jeff Brown6d0fec22010-07-23 21:28:06 -07001079 void syncTouch(nsecs_t when, bool havePointerIds);
1080
1081private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001082 // The surface orientation and width and height set by configureSurface().
1083 int32_t mSurfaceOrientation;
1084 int32_t mSurfaceWidth;
1085 int32_t mSurfaceHeight;
1086
1087 // The associated display orientation and width and height set by configureSurface().
1088 int32_t mAssociatedDisplayOrientation;
1089 int32_t mAssociatedDisplayWidth;
1090 int32_t mAssociatedDisplayHeight;
1091
1092 // Translation and scaling factors, orientation-independent.
1093 float mXScale;
1094 float mXPrecision;
1095
1096 float mYScale;
1097 float mYPrecision;
1098
1099 float mGeometricScale;
1100
Jeff Brownbe1aa822011-07-27 16:04:54 -07001101 float mPressureScale;
1102
1103 float mSizeScale;
1104
1105 float mOrientationScale;
1106
1107 float mDistanceScale;
1108
1109 // Oriented motion ranges for input device info.
1110 struct OrientedRanges {
1111 InputDeviceInfo::MotionRange x;
1112 InputDeviceInfo::MotionRange y;
1113
1114 bool havePressure;
1115 InputDeviceInfo::MotionRange pressure;
1116
1117 bool haveSize;
1118 InputDeviceInfo::MotionRange size;
1119
1120 bool haveTouchSize;
1121 InputDeviceInfo::MotionRange touchMajor;
1122 InputDeviceInfo::MotionRange touchMinor;
1123
1124 bool haveToolSize;
1125 InputDeviceInfo::MotionRange toolMajor;
1126 InputDeviceInfo::MotionRange toolMinor;
1127
1128 bool haveOrientation;
1129 InputDeviceInfo::MotionRange orientation;
1130
1131 bool haveDistance;
1132 InputDeviceInfo::MotionRange distance;
1133 } mOrientedRanges;
1134
1135 // Oriented dimensions and precision.
1136 float mOrientedSurfaceWidth;
1137 float mOrientedSurfaceHeight;
1138 float mOrientedXPrecision;
1139 float mOrientedYPrecision;
1140
1141 struct CurrentVirtualKeyState {
1142 bool down;
1143 bool ignored;
1144 nsecs_t downTime;
1145 int32_t keyCode;
1146 int32_t scanCode;
1147 } mCurrentVirtualKey;
1148
1149 // Scale factor for gesture based pointer movements.
1150 float mPointerGestureXMovementScale;
1151 float mPointerGestureYMovementScale;
1152
1153 // Scale factor for gesture based zooming and other freeform motions.
1154 float mPointerGestureXZoomScale;
1155 float mPointerGestureYZoomScale;
1156
1157 // The maximum swipe width.
1158 float mPointerGestureMaxSwipeWidth;
1159
Jeff Brown6d0fec22010-07-23 21:28:06 -07001160 struct PointerDistanceHeapElement {
1161 uint32_t currentPointerIndex : 8;
1162 uint32_t lastPointerIndex : 8;
1163 uint64_t distance : 48; // squared distance
1164 };
1165
Jeff Brownace13b12011-03-09 17:39:48 -08001166 struct PointerGesture {
1167 enum Mode {
1168 // No fingers, button is not pressed.
1169 // Nothing happening.
1170 NEUTRAL,
1171
1172 // No fingers, button is not pressed.
1173 // Tap detected.
1174 // Emits DOWN and UP events at the pointer location.
1175 TAP,
1176
Jeff Brown79ac9692011-04-19 21:20:10 -07001177 // Exactly one finger dragging following a tap.
1178 // Pointer follows the active finger.
1179 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001180 //
1181 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001182 TAP_DRAG,
1183
Jeff Brownace13b12011-03-09 17:39:48 -08001184 // Button is pressed.
1185 // Pointer follows the active finger if there is one. Other fingers are ignored.
1186 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001187 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001188
1189 // Exactly one finger, button is not pressed.
1190 // Pointer follows the active finger.
1191 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001192 //
1193 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001194 HOVER,
1195
Jeff Brown2352b972011-04-12 22:39:53 -07001196 // Exactly two fingers but neither have moved enough to clearly indicate
1197 // whether a swipe or freeform gesture was intended. We consider the
1198 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1199 // Pointer does not move.
1200 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1201 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001202
1203 // Exactly two fingers moving in the same direction, button is not pressed.
1204 // Pointer does not move.
1205 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1206 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001207 SWIPE,
1208
1209 // Two or more fingers moving in arbitrary directions, button is not pressed.
1210 // Pointer does not move.
1211 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1212 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001213 FREEFORM,
1214
1215 // Waiting for quiet time to end before starting the next gesture.
1216 QUIET,
1217 };
1218
Jeff Brown2352b972011-04-12 22:39:53 -07001219 // Time the first finger went down.
1220 nsecs_t firstTouchTime;
1221
Jeff Brownace13b12011-03-09 17:39:48 -08001222 // The active pointer id from the raw touch data.
1223 int32_t activeTouchId; // -1 if none
1224
1225 // The active pointer id from the gesture last delivered to the application.
1226 int32_t activeGestureId; // -1 if none
1227
1228 // Pointer coords and ids for the current and previous pointer gesture.
1229 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001230 BitSet32 currentGestureIdBits;
1231 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001232 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001233 PointerCoords currentGestureCoords[MAX_POINTERS];
1234
1235 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001236 BitSet32 lastGestureIdBits;
1237 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001238 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001239 PointerCoords lastGestureCoords[MAX_POINTERS];
1240
Jeff Brownace13b12011-03-09 17:39:48 -08001241 // Time the pointer gesture last went down.
1242 nsecs_t downTime;
1243
Jeff Brown79ac9692011-04-19 21:20:10 -07001244 // Time when the pointer went down for a TAP.
1245 nsecs_t tapDownTime;
1246
1247 // Time when the pointer went up for a TAP.
1248 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001249
Jeff Brown2352b972011-04-12 22:39:53 -07001250 // Location of initial tap.
1251 float tapX, tapY;
1252
Jeff Brownace13b12011-03-09 17:39:48 -08001253 // Time we started waiting for quiescence.
1254 nsecs_t quietTime;
1255
Jeff Brown2352b972011-04-12 22:39:53 -07001256 // Reference points for multitouch gestures.
1257 float referenceTouchX; // reference touch X/Y coordinates in surface units
1258 float referenceTouchY;
1259 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1260 float referenceGestureY;
1261
Jeff Brown538881e2011-05-25 18:23:38 -07001262 // Distance that each pointer has traveled which has not yet been
1263 // subsumed into the reference gesture position.
1264 BitSet32 referenceIdBits;
1265 struct Delta {
1266 float dx, dy;
1267 };
1268 Delta referenceDeltas[MAX_POINTER_ID + 1];
1269
Jeff Brown2352b972011-04-12 22:39:53 -07001270 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1271 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1272
Jeff Brownace13b12011-03-09 17:39:48 -08001273 // A velocity tracker for determining whether to switch active pointers during drags.
1274 VelocityTracker velocityTracker;
1275
Jeff Brown19c97d462011-06-01 12:33:19 -07001276 // Velocity control for pointer movements.
1277 VelocityControl pointerVelocityControl;
1278
Jeff Brownace13b12011-03-09 17:39:48 -08001279 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001280 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001281 activeTouchId = -1;
1282 activeGestureId = -1;
1283 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001284 currentGestureIdBits.clear();
1285 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001286 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001287 downTime = 0;
1288 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001289 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001290 resetQuietTime();
Jeff Brown19c97d462011-06-01 12:33:19 -07001291 pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08001292 }
1293
Jeff Brown79ac9692011-04-19 21:20:10 -07001294 void resetTap() {
1295 tapDownTime = LLONG_MIN;
1296 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001297 }
1298
1299 void resetQuietTime() {
1300 quietTime = LLONG_MIN;
1301 }
1302 } mPointerGesture;
1303
Jeff Brownbe1aa822011-07-27 16:04:54 -07001304 void initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001305
Jeff Brownbe1aa822011-07-27 16:04:54 -07001306 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1307 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1308 int32_t keyEventAction, int32_t keyEventFlags);
1309
Jeff Brown6d0fec22010-07-23 21:28:06 -07001310 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001311 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1312 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1313 void cookPointerData();
1314
Jeff Brown79ac9692011-04-19 21:20:10 -07001315 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1316 bool preparePointerGestures(nsecs_t when,
1317 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
Jeff Brownace13b12011-03-09 17:39:48 -08001318
1319 // Dispatches a motion event.
1320 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1321 // method will take care of setting the index and transmuting the action to DOWN or UP
1322 // it is the first / last pointer to go down / up.
1323 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001324 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1325 int32_t edgeFlags,
1326 const PointerProperties* properties, const PointerCoords* coords,
1327 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001328 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1329
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001330 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001331 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001332 bool updateMovedPointers(const PointerProperties* inProperties,
1333 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1334 PointerProperties* outProperties, PointerCoords* outCoords,
1335 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001336
Jeff Brownbe1aa822011-07-27 16:04:54 -07001337 bool isPointInsideSurface(int32_t x, int32_t y);
1338 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001339
Jeff Brownbe1aa822011-07-27 16:04:54 -07001340 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001341};
1342
1343
1344class SingleTouchInputMapper : public TouchInputMapper {
1345public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001346 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001347 virtual ~SingleTouchInputMapper();
1348
1349 virtual void reset();
1350 virtual void process(const RawEvent* rawEvent);
1351
1352protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001353 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001354
1355private:
Jeff Brown49754db2011-07-01 17:37:58 -07001356 CursorButtonAccumulator mCursorButtonAccumulator;
1357 TouchButtonAccumulator mTouchButtonAccumulator;
1358 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001359
Jeff Brown80fd47c2011-05-24 01:07:44 -07001360 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001361
1362 void sync(nsecs_t when);
1363};
1364
1365
1366class MultiTouchInputMapper : public TouchInputMapper {
1367public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001368 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001369 virtual ~MultiTouchInputMapper();
1370
1371 virtual void reset();
1372 virtual void process(const RawEvent* rawEvent);
1373
1374protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001375 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001376
1377private:
Jeff Brown49754db2011-07-01 17:37:58 -07001378 CursorButtonAccumulator mCursorButtonAccumulator;
1379 TouchButtonAccumulator mTouchButtonAccumulator;
1380 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001381
Jeff Brown6894a292011-07-01 17:59:27 -07001382 // Specifies the pointer id bits that are in use, and their associated tracking id.
1383 BitSet32 mPointerIdBits;
1384 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1385
Jeff Brown80fd47c2011-05-24 01:07:44 -07001386 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001387
1388 void sync(nsecs_t when);
1389};
1390
Jeff Browncb1404e2011-01-15 18:14:15 -08001391
1392class JoystickInputMapper : public InputMapper {
1393public:
1394 JoystickInputMapper(InputDevice* device);
1395 virtual ~JoystickInputMapper();
1396
1397 virtual uint32_t getSources();
1398 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1399 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -07001400 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08001401 virtual void reset();
1402 virtual void process(const RawEvent* rawEvent);
1403
1404private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001405 struct Axis {
1406 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001407 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001408
Jeff Brown6f2fba42011-02-19 01:08:02 -08001409 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001410
Jeff Brown6f2fba42011-02-19 01:08:02 -08001411 float scale; // scale factor from raw to normalized values
1412 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001413 float highScale; // scale factor from raw to normalized values of high split
1414 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001415
Jeff Brown6f2fba42011-02-19 01:08:02 -08001416 float min; // normalized inclusive minimum
1417 float max; // normalized inclusive maximum
1418 float flat; // normalized flat region size
1419 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001420
Jeff Brown6f2fba42011-02-19 01:08:02 -08001421 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001422 float currentValue; // current value
1423 float newValue; // most recent value
1424 float highCurrentValue; // current value of high split
1425 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001426
Jeff Brown85297452011-03-04 13:07:49 -08001427 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1428 bool explicitlyMapped, float scale, float offset,
1429 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001430 float min, float max, float flat, float fuzz) {
1431 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001432 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001433 this->explicitlyMapped = explicitlyMapped;
1434 this->scale = scale;
1435 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001436 this->highScale = highScale;
1437 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001438 this->min = min;
1439 this->max = max;
1440 this->flat = flat;
1441 this->fuzz = fuzz;
1442 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001443 resetValue();
1444 }
1445
1446 void resetValue() {
1447 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001448 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001449 this->highCurrentValue = 0;
1450 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001451 }
1452 };
1453
Jeff Brown6f2fba42011-02-19 01:08:02 -08001454 // Axes indexed by raw ABS_* axis index.
1455 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001456
Jeff Brown6f2fba42011-02-19 01:08:02 -08001457 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001458
Jeff Brown85297452011-03-04 13:07:49 -08001459 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001460 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001461 bool filterAxes(bool force);
1462
1463 static bool hasValueChangedSignificantly(float filter,
1464 float newValue, float currentValue, float min, float max);
1465 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1466 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001467
Jeff Brown6f2fba42011-02-19 01:08:02 -08001468 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001469};
1470
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001471} // namespace android
1472
1473#endif // _UI_INPUT_READER_H