blob: f5d095d481f1243cb6765afdcb8fb1148744e3b4 [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
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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700213 /* Runs a single iteration of the processing loop.
214 * Nominally reads and processes one incoming message from the EventHub.
215 *
216 * This method should be called on the input reader thread.
217 */
218 virtual void loopOnce() = 0;
219
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220 /* Gets the current input device configuration.
221 *
222 * This method may be called on any thread (usually by the input manager).
223 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700224 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700225
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 /* Gets information about the specified input device.
227 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
228 * was no such device.
229 *
230 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700231 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700232 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
233
234 /* Gets the list of all registered device ids. */
235 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
236
237 /* Query current input state. */
238 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
239 int32_t scanCode) = 0;
240 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
241 int32_t keyCode) = 0;
242 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
243 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700244
245 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700246 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
247 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700248
Jeff Brown474dcb52011-06-14 20:22:50 -0700249 /* Requests that a reconfiguration of all input devices.
250 * The changes flag is a bitfield that indicates what has changed and whether
251 * the input devices must all be reopened. */
252 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700253};
254
255
256/* Internal interface used by individual input devices to access global input device state
257 * and parameters maintained by the input reader.
258 */
259class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700260public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700261 InputReaderContext() { }
262 virtual ~InputReaderContext() { }
263
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 virtual void updateGlobalMetaState() = 0;
265 virtual int32_t getGlobalMetaState() = 0;
266
Jeff Brownfe508922011-01-18 15:10:10 -0800267 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
268 virtual bool shouldDropVirtualKey(nsecs_t now,
269 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
270
Jeff Brown05dc66a2011-03-02 14:41:58 -0800271 virtual void fadePointer() = 0;
272
Jeff Brownaa3855d2011-03-17 01:34:19 -0700273 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
274
Jeff Brown6d0fec22010-07-23 21:28:06 -0700275 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700276 virtual InputListenerInterface* getListener() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700277 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700278};
279
Jeff Brown9c3cda02010-06-15 01:31:58 -0700280
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brownbe1aa822011-07-27 16:04:54 -0700282 * that it sends to the input listener. Some functions of the input reader, such as early
Jeff Brown9c3cda02010-06-15 01:31:58 -0700283 * event filtering in low power states, are controlled by a separate policy object.
284 *
Jeff Brownbe1aa822011-07-27 16:04:54 -0700285 * The InputReader owns a collection of InputMappers. Most of the work it does happens
286 * on the input reader thread but the InputReader can receive queries from other system
287 * components running on arbitrary threads. To keep things manageable, the InputReader
288 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
289 * EventHub or the InputReaderPolicy but it is never held while calling into the
290 * InputListener.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700291 */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700292class InputReader : public InputReaderInterface {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700293public:
294 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700295 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700296 const sp<InputListenerInterface>& listener);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700297 virtual ~InputReader();
298
Jeff Brownb88102f2010-09-08 11:49:43 -0700299 virtual void dump(String8& dump);
300
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301 virtual void loopOnce();
302
Jeff Brown6d0fec22010-07-23 21:28:06 -0700303 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700304
Jeff Brown6d0fec22010-07-23 21:28:06 -0700305 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
306 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700307
Jeff Brown6d0fec22010-07-23 21:28:06 -0700308 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
309 int32_t scanCode);
310 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
311 int32_t keyCode);
312 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
313 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314
Jeff Brown6d0fec22010-07-23 21:28:06 -0700315 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
316 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317
Jeff Brown474dcb52011-06-14 20:22:50 -0700318 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700319
Jeff Brownc3db8582010-10-20 15:33:38 -0700320protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700321 // These members are protected so they can be instrumented by test cases.
322 virtual InputDevice* createDeviceLocked(int32_t deviceId,
323 const String8& name, uint32_t classes);
324
325 class ContextImpl : public InputReaderContext {
326 InputReader* mReader;
327
328 public:
329 ContextImpl(InputReader* reader);
330
331 virtual void updateGlobalMetaState();
332 virtual int32_t getGlobalMetaState();
333 virtual void disableVirtualKeysUntil(nsecs_t time);
334 virtual bool shouldDropVirtualKey(nsecs_t now,
335 InputDevice* device, int32_t keyCode, int32_t scanCode);
336 virtual void fadePointer();
337 virtual void requestTimeoutAtTime(nsecs_t when);
338 virtual InputReaderPolicyInterface* getPolicy();
339 virtual InputListenerInterface* getListener();
340 virtual EventHubInterface* getEventHub();
341 } mContext;
342
343 friend class ContextImpl;
Jeff Brownc3db8582010-10-20 15:33:38 -0700344
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345private:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700346 Mutex mLock;
347
Jeff Brown46b9ac02010-04-22 18:58:52 -0700348 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700349 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700350 sp<QueuedInputListener> mQueuedListener;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700351
Jeff Brown214eaf42011-05-26 19:17:02 -0700352 InputReaderConfiguration mConfig;
353
Jeff Brownb7198742011-03-18 18:14:26 -0700354 // The event queue.
355 static const int EVENT_BUFFER_SIZE = 256;
356 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
357
Jeff Brown46b9ac02010-04-22 18:58:52 -0700358 KeyedVector<int32_t, InputDevice*> mDevices;
359
Jeff Brown6d0fec22010-07-23 21:28:06 -0700360 // low-level input event decoding and device management
Jeff Brownbe1aa822011-07-27 16:04:54 -0700361 void processEventsLocked(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362
Jeff Brownbe1aa822011-07-27 16:04:54 -0700363 void addDeviceLocked(int32_t deviceId);
364 void removeDeviceLocked(int32_t deviceId);
365 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
366 void timeoutExpiredLocked(nsecs_t when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700367
Jeff Brownbe1aa822011-07-27 16:04:54 -0700368 void handleConfigurationChangedLocked(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 int32_t mGlobalMetaState;
371 void updateGlobalMetaStateLocked();
372 int32_t getGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373
Jeff Brownbe1aa822011-07-27 16:04:54 -0700374 void fadePointerLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375
Jeff Brownbe1aa822011-07-27 16:04:54 -0700376 InputConfiguration mInputConfiguration;
377 void updateInputConfigurationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800378
Jeff Brownbe1aa822011-07-27 16:04:54 -0700379 nsecs_t mDisableVirtualKeysTimeout;
380 void disableVirtualKeysUntilLocked(nsecs_t time);
381 bool shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800382 InputDevice* device, int32_t keyCode, int32_t scanCode);
383
Jeff Brownbe1aa822011-07-27 16:04:54 -0700384 nsecs_t mNextTimeout;
385 void requestTimeoutAtTimeLocked(nsecs_t when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700386
Jeff Brownbe1aa822011-07-27 16:04:54 -0700387 uint32_t mConfigurationChangesToRefresh;
388 void refreshConfigurationLocked(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700389
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 // state queries
391 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700392 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700393 GetStateFunc getStateFunc);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700394 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396};
397
398
399/* Reads raw events from the event hub and processes them, endlessly. */
400class InputReaderThread : public Thread {
401public:
402 InputReaderThread(const sp<InputReaderInterface>& reader);
403 virtual ~InputReaderThread();
404
405private:
406 sp<InputReaderInterface> mReader;
407
408 virtual bool threadLoop();
409};
410
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411
412/* Represents the state of a single input device. */
413class InputDevice {
414public:
415 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
416 ~InputDevice();
417
418 inline InputReaderContext* getContext() { return mContext; }
419 inline int32_t getId() { return mId; }
420 inline const String8& getName() { return mName; }
421 inline uint32_t getSources() { return mSources; }
422
Jeff Brown56194eb2011-03-02 19:23:13 -0800423 inline bool isExternal() { return mIsExternal; }
424 inline void setExternal(bool external) { mIsExternal = external; }
425
Jeff Brown6d0fec22010-07-23 21:28:06 -0700426 inline bool isIgnored() { return mMappers.isEmpty(); }
427
Jeff Brownef3d7e82010-09-30 14:33:04 -0700428 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700429 void addMapper(InputMapper* mapper);
Jeff Brown474dcb52011-06-14 20:22:50 -0700430 void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431 void reset();
Jeff Brownb7198742011-03-18 18:14:26 -0700432 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700433 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434
435 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
436 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
437 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
438 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
439 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
440 const int32_t* keyCodes, uint8_t* outFlags);
441
442 int32_t getMetaState();
443
Jeff Brown05dc66a2011-03-02 14:41:58 -0800444 void fadePointer();
445
Jeff Brown49754db2011-07-01 17:37:58 -0700446 inline const PropertyMap& getConfiguration() { return mConfiguration; }
447 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700448
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449private:
450 InputReaderContext* mContext;
451 int32_t mId;
452
453 Vector<InputMapper*> mMappers;
454
455 String8 mName;
456 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800457 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700458 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459
460 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
461 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700462
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800463 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700464};
465
466
Jeff Brown49754db2011-07-01 17:37:58 -0700467/* Keeps track of the state of mouse or touch pad buttons. */
468class CursorButtonAccumulator {
469public:
470 CursorButtonAccumulator();
471
472 void clearButtons();
473 void process(const RawEvent* rawEvent);
474
475 uint32_t getButtonState() const;
476
477private:
478 bool mBtnLeft;
479 bool mBtnRight;
480 bool mBtnMiddle;
481 bool mBtnBack;
482 bool mBtnSide;
483 bool mBtnForward;
484 bool mBtnExtra;
485 bool mBtnTask;
486};
487
488
489/* Keeps track of cursor movements. */
490
491class CursorMotionAccumulator {
492public:
493 CursorMotionAccumulator();
494 void configure(InputDevice* device);
495
496 void clearRelativeAxes();
497 void process(const RawEvent* rawEvent);
498
499 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
500 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
501
502 inline int32_t getRelativeX() const { return mRelX; }
503 inline int32_t getRelativeY() const { return mRelY; }
504 inline int32_t getRelativeVWheel() const { return mRelWheel; }
505 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
506
507private:
508 bool mHaveRelWheel;
509 bool mHaveRelHWheel;
510
511 int32_t mRelX;
512 int32_t mRelY;
513 int32_t mRelWheel;
514 int32_t mRelHWheel;
515};
516
517
518/* Keeps track of the state of touch, stylus and tool buttons. */
519class TouchButtonAccumulator {
520public:
521 TouchButtonAccumulator();
522 void configure(InputDevice* device);
523
524 void clearButtons();
525 void process(const RawEvent* rawEvent);
526
527 uint32_t getButtonState() const;
528 int32_t getToolType() const;
529 bool isActive() const;
530 bool isHovering() const;
531
532private:
533 bool mHaveBtnTouch;
534
535 bool mBtnTouch;
536 bool mBtnStylus;
537 bool mBtnStylus2;
538 bool mBtnToolFinger;
539 bool mBtnToolPen;
540 bool mBtnToolRubber;
541};
542
543
Jeff Brownbe1aa822011-07-27 16:04:54 -0700544/* Raw axis information from the driver. */
545struct RawPointerAxes {
546 RawAbsoluteAxisInfo x;
547 RawAbsoluteAxisInfo y;
548 RawAbsoluteAxisInfo pressure;
549 RawAbsoluteAxisInfo touchMajor;
550 RawAbsoluteAxisInfo touchMinor;
551 RawAbsoluteAxisInfo toolMajor;
552 RawAbsoluteAxisInfo toolMinor;
553 RawAbsoluteAxisInfo orientation;
554 RawAbsoluteAxisInfo distance;
555 RawAbsoluteAxisInfo trackingId;
556 RawAbsoluteAxisInfo slot;
557
558 RawPointerAxes();
559 void clear();
560};
561
562
563/* Raw data for a collection of pointers including a pointer id mapping table. */
564struct RawPointerData {
565 struct Pointer {
566 uint32_t id;
567 int32_t x;
568 int32_t y;
569 int32_t pressure;
570 int32_t touchMajor;
571 int32_t touchMinor;
572 int32_t toolMajor;
573 int32_t toolMinor;
574 int32_t orientation;
575 int32_t distance;
576 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
577 bool isHovering;
578 };
579
580 uint32_t pointerCount;
581 Pointer pointers[MAX_POINTERS];
582 BitSet32 hoveringIdBits, touchingIdBits;
583 uint32_t idToIndex[MAX_POINTER_ID + 1];
584
585 RawPointerData();
586 void clear();
587 void copyFrom(const RawPointerData& other);
588 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
589
590 inline void markIdBit(uint32_t id, bool isHovering) {
591 if (isHovering) {
592 hoveringIdBits.markBit(id);
593 } else {
594 touchingIdBits.markBit(id);
595 }
596 }
597
598 inline void clearIdBits() {
599 hoveringIdBits.clear();
600 touchingIdBits.clear();
601 }
602
603 inline const Pointer& pointerForId(uint32_t id) const {
604 return pointers[idToIndex[id]];
605 }
606
607 inline bool isHovering(uint32_t pointerIndex) {
608 return pointers[pointerIndex].isHovering;
609 }
610};
611
612
613/* Cooked data for a collection of pointers including a pointer id mapping table. */
614struct CookedPointerData {
615 uint32_t pointerCount;
616 PointerProperties pointerProperties[MAX_POINTERS];
617 PointerCoords pointerCoords[MAX_POINTERS];
618 BitSet32 hoveringIdBits, touchingIdBits;
619 uint32_t idToIndex[MAX_POINTER_ID + 1];
620
621 CookedPointerData();
622 void clear();
623 void copyFrom(const CookedPointerData& other);
624
625 inline bool isHovering(uint32_t pointerIndex) {
626 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
627 }
628};
629
630
Jeff Brown49754db2011-07-01 17:37:58 -0700631/* Keeps track of the state of single-touch protocol. */
632class SingleTouchMotionAccumulator {
633public:
634 SingleTouchMotionAccumulator();
635
636 void clearAbsoluteAxes();
637 void process(const RawEvent* rawEvent);
638
639 inline int32_t getAbsoluteX() const { return mAbsX; }
640 inline int32_t getAbsoluteY() const { return mAbsY; }
641 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
642 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
643 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
644
645private:
646 int32_t mAbsX;
647 int32_t mAbsY;
648 int32_t mAbsPressure;
649 int32_t mAbsToolWidth;
650 int32_t mAbsDistance;
651};
652
653
654/* Keeps track of the state of multi-touch protocol. */
655class MultiTouchMotionAccumulator {
656public:
657 class Slot {
658 public:
659 inline bool isInUse() const { return mInUse; }
660 inline int32_t getX() const { return mAbsMTPositionX; }
661 inline int32_t getY() const { return mAbsMTPositionY; }
662 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
663 inline int32_t getTouchMinor() const {
664 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
665 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
666 inline int32_t getToolMinor() const {
667 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
668 inline int32_t getOrientation() const { return mAbsMTOrientation; }
669 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
670 inline int32_t getPressure() const { return mAbsMTPressure; }
671 inline int32_t getDistance() const { return mAbsMTDistance; }
672 inline int32_t getToolType() const;
673
674 private:
675 friend class MultiTouchMotionAccumulator;
676
677 bool mInUse;
678 bool mHaveAbsMTTouchMinor;
679 bool mHaveAbsMTWidthMinor;
680 bool mHaveAbsMTToolType;
681
682 int32_t mAbsMTPositionX;
683 int32_t mAbsMTPositionY;
684 int32_t mAbsMTTouchMajor;
685 int32_t mAbsMTTouchMinor;
686 int32_t mAbsMTWidthMajor;
687 int32_t mAbsMTWidthMinor;
688 int32_t mAbsMTOrientation;
689 int32_t mAbsMTTrackingId;
690 int32_t mAbsMTPressure;
Jeff Brown49754db2011-07-01 17:37:58 -0700691 int32_t mAbsMTDistance;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700692 int32_t mAbsMTToolType;
Jeff Brown49754db2011-07-01 17:37:58 -0700693
694 Slot();
695 void clearIfInUse();
696 void clear();
697 };
698
699 MultiTouchMotionAccumulator();
700 ~MultiTouchMotionAccumulator();
701
702 void configure(size_t slotCount, bool usingSlotsProtocol);
703 void process(const RawEvent* rawEvent);
704
705 void clearSlots(int32_t initialSlot);
706
707 inline bool isUsingSlotsProtocol() const { return mUsingSlotsProtocol; }
708 inline size_t getSlotCount() const { return mSlotCount; }
709 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
710
711private:
712 int32_t mCurrentSlot;
713 Slot* mSlots;
714 size_t mSlotCount;
715 bool mUsingSlotsProtocol;
716};
717
718
Jeff Brown6d0fec22010-07-23 21:28:06 -0700719/* An input mapper transforms raw input events into cooked event data.
720 * A single input device can have multiple associated input mappers in order to interpret
721 * different classes of events.
722 */
723class InputMapper {
724public:
725 InputMapper(InputDevice* device);
726 virtual ~InputMapper();
727
728 inline InputDevice* getDevice() { return mDevice; }
729 inline int32_t getDeviceId() { return mDevice->getId(); }
730 inline const String8 getDeviceName() { return mDevice->getName(); }
731 inline InputReaderContext* getContext() { return mContext; }
732 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700733 inline InputListenerInterface* getListener() { return mContext->getListener(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700734 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
735
736 virtual uint32_t getSources() = 0;
737 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700738 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700739 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700740 virtual void reset();
741 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700742 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700743
744 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
745 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
746 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
747 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
748 const int32_t* keyCodes, uint8_t* outFlags);
749
750 virtual int32_t getMetaState();
751
Jeff Brown05dc66a2011-03-02 14:41:58 -0800752 virtual void fadePointer();
753
Jeff Brown6d0fec22010-07-23 21:28:06 -0700754protected:
755 InputDevice* mDevice;
756 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800757
Jeff Brownbe1aa822011-07-27 16:04:54 -0700758 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
759
Jeff Browncb1404e2011-01-15 18:14:15 -0800760 static void dumpRawAbsoluteAxisInfo(String8& dump,
761 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700762};
763
764
765class SwitchInputMapper : public InputMapper {
766public:
767 SwitchInputMapper(InputDevice* device);
768 virtual ~SwitchInputMapper();
769
770 virtual uint32_t getSources();
771 virtual void process(const RawEvent* rawEvent);
772
773 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
774
775private:
776 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
777};
778
779
780class KeyboardInputMapper : public InputMapper {
781public:
Jeff Brownefd32662011-03-08 15:13:06 -0800782 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700783 virtual ~KeyboardInputMapper();
784
785 virtual uint32_t getSources();
786 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700787 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700788 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700789 virtual void reset();
790 virtual void process(const RawEvent* rawEvent);
791
792 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
793 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
794 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
795 const int32_t* keyCodes, uint8_t* outFlags);
796
797 virtual int32_t getMetaState();
798
799private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700800 Mutex mLock;
801
Jeff Brown6d0fec22010-07-23 21:28:06 -0700802 struct KeyDown {
803 int32_t keyCode;
804 int32_t scanCode;
805 };
806
Jeff Brownefd32662011-03-08 15:13:06 -0800807 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700808 int32_t mKeyboardType;
809
Jeff Brownbe1aa822011-07-27 16:04:54 -0700810 Vector<KeyDown> mKeyDowns; // keys that are down
811 int32_t mMetaState;
812 nsecs_t mDownTime; // time of most recent key down
813
814 struct LedState {
815 bool avail; // led is available
816 bool on; // we think the led is currently on
817 };
818 LedState mCapsLockLedState;
819 LedState mNumLockLedState;
820 LedState mScrollLockLedState;
821
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800822 // Immutable configuration parameters.
823 struct Parameters {
824 int32_t associatedDisplayId;
825 bool orientationAware;
826 } mParameters;
827
Jeff Brownbe1aa822011-07-27 16:04:54 -0700828 void initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700829
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800830 void configureParameters();
831 void dumpParameters(String8& dump);
832
Jeff Brown6d0fec22010-07-23 21:28:06 -0700833 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700834
Jeff Brown6d0fec22010-07-23 21:28:06 -0700835 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
836 uint32_t policyFlags);
837
Jeff Brownbe1aa822011-07-27 16:04:54 -0700838 ssize_t findKeyDown(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700839
Jeff Brownbe1aa822011-07-27 16:04:54 -0700840 void resetLedState();
841 void initializeLedState(LedState& ledState, int32_t led);
842 void updateLedState(bool reset);
843 void updateLedStateForModifier(LedState& ledState, int32_t led,
Jeff Brown497a92c2010-09-12 17:55:08 -0700844 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700845};
846
847
Jeff Brown83c09682010-12-23 17:50:18 -0800848class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700849public:
Jeff Brown83c09682010-12-23 17:50:18 -0800850 CursorInputMapper(InputDevice* device);
851 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700852
853 virtual uint32_t getSources();
854 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700855 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700856 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700857 virtual void reset();
858 virtual void process(const RawEvent* rawEvent);
859
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700860 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
861
Jeff Brown05dc66a2011-03-02 14:41:58 -0800862 virtual void fadePointer();
863
Jeff Brown6d0fec22010-07-23 21:28:06 -0700864private:
865 // Amount that trackball needs to move in order to generate a key event.
866 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
867
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800868 // Immutable configuration parameters.
869 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800870 enum Mode {
871 MODE_POINTER,
872 MODE_NAVIGATION,
873 };
874
875 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800876 int32_t associatedDisplayId;
877 bool orientationAware;
878 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700879
Jeff Brown49754db2011-07-01 17:37:58 -0700880 CursorButtonAccumulator mCursorButtonAccumulator;
881 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882
Jeff Brownefd32662011-03-08 15:13:06 -0800883 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700884 float mXScale;
885 float mYScale;
886 float mXPrecision;
887 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800888
Jeff Brown6f2fba42011-02-19 01:08:02 -0800889 float mVWheelScale;
890 float mHWheelScale;
891
Jeff Brown19c97d462011-06-01 12:33:19 -0700892 // Velocity controls for mouse pointer and wheel movements.
893 // The controls for X and Y wheel movements are separate to keep them decoupled.
894 VelocityControl mPointerVelocityControl;
895 VelocityControl mWheelXVelocityControl;
896 VelocityControl mWheelYVelocityControl;
897
Jeff Brown83c09682010-12-23 17:50:18 -0800898 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899
Jeff Brownbe1aa822011-07-27 16:04:54 -0700900 int32_t mButtonState;
901 nsecs_t mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700902
Jeff Brownbe1aa822011-07-27 16:04:54 -0700903 void initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700904
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800905 void configureParameters();
906 void dumpParameters(String8& dump);
907
Jeff Brown6d0fec22010-07-23 21:28:06 -0700908 void sync(nsecs_t when);
909};
910
911
912class TouchInputMapper : public InputMapper {
913public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800914 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700915 virtual ~TouchInputMapper();
916
917 virtual uint32_t getSources();
918 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700919 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700920 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 virtual void reset();
922
923 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
924 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
925 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
926 const int32_t* keyCodes, uint8_t* outFlags);
927
Jeff Brownace13b12011-03-09 17:39:48 -0800928 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -0700929 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -0800930
Jeff Brown6d0fec22010-07-23 21:28:06 -0700931protected:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700932 struct VirtualKey {
933 int32_t keyCode;
934 int32_t scanCode;
935 uint32_t flags;
936
937 // computed hit box, specified in touch screen coords based on known display size
938 int32_t hitLeft;
939 int32_t hitTop;
940 int32_t hitRight;
941 int32_t hitBottom;
942
943 inline bool isHit(int32_t x, int32_t y) const {
944 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
945 }
946 };
947
Jeff Brown83c09682010-12-23 17:50:18 -0800948 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800949 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brownace13b12011-03-09 17:39:48 -0800950 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800951
Jeff Brown214eaf42011-05-26 19:17:02 -0700952 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -0700953 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -0700954
Jeff Brown6d0fec22010-07-23 21:28:06 -0700955 // Immutable configuration parameters.
956 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800957 enum DeviceType {
958 DEVICE_TYPE_TOUCH_SCREEN,
959 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -0800960 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800961 };
962
963 DeviceType deviceType;
964 int32_t associatedDisplayId;
Jeff Brownbc68a592011-07-25 12:58:12 -0700965 bool associatedDisplayIsExternal;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800966 bool orientationAware;
967
Jeff Brown2352b972011-04-12 22:39:53 -0700968 enum GestureMode {
969 GESTURE_MODE_POINTER,
970 GESTURE_MODE_SPOTS,
971 };
972 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700973 } mParameters;
974
Jeff Brown8d608662010-08-30 03:02:23 -0700975 // Immutable calibration parameters in parsed form.
976 struct Calibration {
Jeff Brownc6d282b2010-10-14 21:42:15 -0700977 // Touch Size
978 enum TouchSizeCalibration {
979 TOUCH_SIZE_CALIBRATION_DEFAULT,
980 TOUCH_SIZE_CALIBRATION_NONE,
981 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
982 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700983 };
984
Jeff Brownc6d282b2010-10-14 21:42:15 -0700985 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700986
Jeff Brownc6d282b2010-10-14 21:42:15 -0700987 // Tool Size
988 enum ToolSizeCalibration {
989 TOOL_SIZE_CALIBRATION_DEFAULT,
990 TOOL_SIZE_CALIBRATION_NONE,
991 TOOL_SIZE_CALIBRATION_GEOMETRIC,
992 TOOL_SIZE_CALIBRATION_LINEAR,
993 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700994 };
995
Jeff Brownc6d282b2010-10-14 21:42:15 -0700996 ToolSizeCalibration toolSizeCalibration;
997 bool haveToolSizeLinearScale;
998 float toolSizeLinearScale;
999 bool haveToolSizeLinearBias;
1000 float toolSizeLinearBias;
1001 bool haveToolSizeAreaScale;
1002 float toolSizeAreaScale;
1003 bool haveToolSizeAreaBias;
1004 float toolSizeAreaBias;
1005 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001006 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -07001007
1008 // Pressure
1009 enum PressureCalibration {
1010 PRESSURE_CALIBRATION_DEFAULT,
1011 PRESSURE_CALIBRATION_NONE,
1012 PRESSURE_CALIBRATION_PHYSICAL,
1013 PRESSURE_CALIBRATION_AMPLITUDE,
1014 };
1015 enum PressureSource {
1016 PRESSURE_SOURCE_DEFAULT,
1017 PRESSURE_SOURCE_PRESSURE,
1018 PRESSURE_SOURCE_TOUCH,
1019 };
1020
1021 PressureCalibration pressureCalibration;
1022 PressureSource pressureSource;
1023 bool havePressureScale;
1024 float pressureScale;
1025
1026 // Size
1027 enum SizeCalibration {
1028 SIZE_CALIBRATION_DEFAULT,
1029 SIZE_CALIBRATION_NONE,
1030 SIZE_CALIBRATION_NORMALIZED,
1031 };
1032
1033 SizeCalibration sizeCalibration;
1034
1035 // Orientation
1036 enum OrientationCalibration {
1037 ORIENTATION_CALIBRATION_DEFAULT,
1038 ORIENTATION_CALIBRATION_NONE,
1039 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001040 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001041 };
1042
1043 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001044
1045 // Distance
1046 enum DistanceCalibration {
1047 DISTANCE_CALIBRATION_DEFAULT,
1048 DISTANCE_CALIBRATION_NONE,
1049 DISTANCE_CALIBRATION_SCALED,
1050 };
1051
1052 DistanceCalibration distanceCalibration;
1053 bool haveDistanceScale;
1054 float distanceScale;
Jeff Brown8d608662010-08-30 03:02:23 -07001055 } mCalibration;
1056
Jeff Brownbe1aa822011-07-27 16:04:54 -07001057 // Raw pointer axis information from the driver.
1058 RawPointerAxes mRawPointerAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001059
Jeff Brownbe1aa822011-07-27 16:04:54 -07001060 // Raw pointer sample data.
1061 RawPointerData mCurrentRawPointerData;
1062 RawPointerData mLastRawPointerData;
Jeff Brownace13b12011-03-09 17:39:48 -08001063
Jeff Brownbe1aa822011-07-27 16:04:54 -07001064 // Cooked pointer sample data.
1065 CookedPointerData mCurrentCookedPointerData;
1066 CookedPointerData mLastCookedPointerData;
1067
1068 // Button state.
1069 int32_t mCurrentButtonState;
1070 int32_t mLastButtonState;
1071
1072 // True if we sent a HOVER_ENTER event.
1073 bool mSentHoverEnter;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001074
1075 // The time the primary pointer last went down.
1076 nsecs_t mDownTime;
1077
Jeff Brownace13b12011-03-09 17:39:48 -08001078 // The pointer controller, or null if the device is not a pointer.
1079 sp<PointerControllerInterface> mPointerController;
1080
Jeff Brownbe1aa822011-07-27 16:04:54 -07001081 Vector<VirtualKey> mVirtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001082
Jeff Brown8d608662010-08-30 03:02:23 -07001083 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001084 virtual void dumpParameters(String8& dump);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001085 virtual void configureRawPointerAxes();
1086 virtual void dumpRawPointerAxes(String8& dump);
1087 virtual bool configureSurface();
1088 virtual void dumpSurface(String8& dump);
1089 virtual void configureVirtualKeys();
1090 virtual void dumpVirtualKeys(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001091 virtual void parseCalibration();
1092 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001093 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094
Jeff Brown6d0fec22010-07-23 21:28:06 -07001095 void syncTouch(nsecs_t when, bool havePointerIds);
1096
1097private:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001098 // The surface orientation and width and height set by configureSurface().
1099 int32_t mSurfaceOrientation;
1100 int32_t mSurfaceWidth;
1101 int32_t mSurfaceHeight;
1102
1103 // The associated display orientation and width and height set by configureSurface().
1104 int32_t mAssociatedDisplayOrientation;
1105 int32_t mAssociatedDisplayWidth;
1106 int32_t mAssociatedDisplayHeight;
1107
1108 // Translation and scaling factors, orientation-independent.
1109 float mXScale;
1110 float mXPrecision;
1111
1112 float mYScale;
1113 float mYPrecision;
1114
1115 float mGeometricScale;
1116
1117 float mToolSizeLinearScale;
1118 float mToolSizeLinearBias;
1119 float mToolSizeAreaScale;
1120 float mToolSizeAreaBias;
1121
1122 float mPressureScale;
1123
1124 float mSizeScale;
1125
1126 float mOrientationScale;
1127
1128 float mDistanceScale;
1129
1130 // Oriented motion ranges for input device info.
1131 struct OrientedRanges {
1132 InputDeviceInfo::MotionRange x;
1133 InputDeviceInfo::MotionRange y;
1134
1135 bool havePressure;
1136 InputDeviceInfo::MotionRange pressure;
1137
1138 bool haveSize;
1139 InputDeviceInfo::MotionRange size;
1140
1141 bool haveTouchSize;
1142 InputDeviceInfo::MotionRange touchMajor;
1143 InputDeviceInfo::MotionRange touchMinor;
1144
1145 bool haveToolSize;
1146 InputDeviceInfo::MotionRange toolMajor;
1147 InputDeviceInfo::MotionRange toolMinor;
1148
1149 bool haveOrientation;
1150 InputDeviceInfo::MotionRange orientation;
1151
1152 bool haveDistance;
1153 InputDeviceInfo::MotionRange distance;
1154 } mOrientedRanges;
1155
1156 // Oriented dimensions and precision.
1157 float mOrientedSurfaceWidth;
1158 float mOrientedSurfaceHeight;
1159 float mOrientedXPrecision;
1160 float mOrientedYPrecision;
1161
1162 struct CurrentVirtualKeyState {
1163 bool down;
1164 bool ignored;
1165 nsecs_t downTime;
1166 int32_t keyCode;
1167 int32_t scanCode;
1168 } mCurrentVirtualKey;
1169
1170 // Scale factor for gesture based pointer movements.
1171 float mPointerGestureXMovementScale;
1172 float mPointerGestureYMovementScale;
1173
1174 // Scale factor for gesture based zooming and other freeform motions.
1175 float mPointerGestureXZoomScale;
1176 float mPointerGestureYZoomScale;
1177
1178 // The maximum swipe width.
1179 float mPointerGestureMaxSwipeWidth;
1180
Jeff Brown6d0fec22010-07-23 21:28:06 -07001181 struct PointerDistanceHeapElement {
1182 uint32_t currentPointerIndex : 8;
1183 uint32_t lastPointerIndex : 8;
1184 uint64_t distance : 48; // squared distance
1185 };
1186
Jeff Brownace13b12011-03-09 17:39:48 -08001187 struct PointerGesture {
1188 enum Mode {
1189 // No fingers, button is not pressed.
1190 // Nothing happening.
1191 NEUTRAL,
1192
1193 // No fingers, button is not pressed.
1194 // Tap detected.
1195 // Emits DOWN and UP events at the pointer location.
1196 TAP,
1197
Jeff Brown79ac9692011-04-19 21:20:10 -07001198 // Exactly one finger dragging following a tap.
1199 // Pointer follows the active finger.
1200 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001201 //
1202 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001203 TAP_DRAG,
1204
Jeff Brownace13b12011-03-09 17:39:48 -08001205 // Button is pressed.
1206 // Pointer follows the active finger if there is one. Other fingers are ignored.
1207 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001208 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001209
1210 // Exactly one finger, button is not pressed.
1211 // Pointer follows the active finger.
1212 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001213 //
1214 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001215 HOVER,
1216
Jeff Brown2352b972011-04-12 22:39:53 -07001217 // Exactly two fingers but neither have moved enough to clearly indicate
1218 // whether a swipe or freeform gesture was intended. We consider the
1219 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1220 // Pointer does not move.
1221 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1222 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001223
1224 // Exactly two fingers moving in the same direction, button is not pressed.
1225 // Pointer does not move.
1226 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1227 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001228 SWIPE,
1229
1230 // Two or more fingers moving in arbitrary directions, button is not pressed.
1231 // Pointer does not move.
1232 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1233 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001234 FREEFORM,
1235
1236 // Waiting for quiet time to end before starting the next gesture.
1237 QUIET,
1238 };
1239
Jeff Brown2352b972011-04-12 22:39:53 -07001240 // Time the first finger went down.
1241 nsecs_t firstTouchTime;
1242
Jeff Brownace13b12011-03-09 17:39:48 -08001243 // The active pointer id from the raw touch data.
1244 int32_t activeTouchId; // -1 if none
1245
1246 // The active pointer id from the gesture last delivered to the application.
1247 int32_t activeGestureId; // -1 if none
1248
1249 // Pointer coords and ids for the current and previous pointer gesture.
1250 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001251 BitSet32 currentGestureIdBits;
1252 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001253 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001254 PointerCoords currentGestureCoords[MAX_POINTERS];
1255
1256 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001257 BitSet32 lastGestureIdBits;
1258 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001259 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001260 PointerCoords lastGestureCoords[MAX_POINTERS];
1261
Jeff Brownace13b12011-03-09 17:39:48 -08001262 // Time the pointer gesture last went down.
1263 nsecs_t downTime;
1264
Jeff Brown79ac9692011-04-19 21:20:10 -07001265 // Time when the pointer went down for a TAP.
1266 nsecs_t tapDownTime;
1267
1268 // Time when the pointer went up for a TAP.
1269 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001270
Jeff Brown2352b972011-04-12 22:39:53 -07001271 // Location of initial tap.
1272 float tapX, tapY;
1273
Jeff Brownace13b12011-03-09 17:39:48 -08001274 // Time we started waiting for quiescence.
1275 nsecs_t quietTime;
1276
Jeff Brown2352b972011-04-12 22:39:53 -07001277 // Reference points for multitouch gestures.
1278 float referenceTouchX; // reference touch X/Y coordinates in surface units
1279 float referenceTouchY;
1280 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1281 float referenceGestureY;
1282
Jeff Brown538881e2011-05-25 18:23:38 -07001283 // Distance that each pointer has traveled which has not yet been
1284 // subsumed into the reference gesture position.
1285 BitSet32 referenceIdBits;
1286 struct Delta {
1287 float dx, dy;
1288 };
1289 Delta referenceDeltas[MAX_POINTER_ID + 1];
1290
Jeff Brown2352b972011-04-12 22:39:53 -07001291 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1292 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1293
Jeff Brownace13b12011-03-09 17:39:48 -08001294 // A velocity tracker for determining whether to switch active pointers during drags.
1295 VelocityTracker velocityTracker;
1296
Jeff Brown19c97d462011-06-01 12:33:19 -07001297 // Velocity control for pointer movements.
1298 VelocityControl pointerVelocityControl;
1299
Jeff Brownace13b12011-03-09 17:39:48 -08001300 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001301 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001302 activeTouchId = -1;
1303 activeGestureId = -1;
1304 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001305 currentGestureIdBits.clear();
1306 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001307 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001308 downTime = 0;
1309 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001310 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001311 resetQuietTime();
Jeff Brown19c97d462011-06-01 12:33:19 -07001312 pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08001313 }
1314
Jeff Brown79ac9692011-04-19 21:20:10 -07001315 void resetTap() {
1316 tapDownTime = LLONG_MIN;
1317 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001318 }
1319
1320 void resetQuietTime() {
1321 quietTime = LLONG_MIN;
1322 }
1323 } mPointerGesture;
1324
Jeff Brownbe1aa822011-07-27 16:04:54 -07001325 void initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001326
Jeff Brownbe1aa822011-07-27 16:04:54 -07001327 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1328 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1329 int32_t keyEventAction, int32_t keyEventFlags);
1330
Jeff Brown6d0fec22010-07-23 21:28:06 -07001331 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001332 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1333 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1334 void cookPointerData();
1335
Jeff Brown79ac9692011-04-19 21:20:10 -07001336 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1337 bool preparePointerGestures(nsecs_t when,
1338 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
Jeff Brownace13b12011-03-09 17:39:48 -08001339
1340 // Dispatches a motion event.
1341 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1342 // method will take care of setting the index and transmuting the action to DOWN or UP
1343 // it is the first / last pointer to go down / up.
1344 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001345 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1346 int32_t edgeFlags,
1347 const PointerProperties* properties, const PointerCoords* coords,
1348 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001349 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1350
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001351 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001352 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001353 bool updateMovedPointers(const PointerProperties* inProperties,
1354 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1355 PointerProperties* outProperties, PointerCoords* outCoords,
1356 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001357
Jeff Brownbe1aa822011-07-27 16:04:54 -07001358 bool isPointInsideSurface(int32_t x, int32_t y);
1359 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001360
Jeff Brownbe1aa822011-07-27 16:04:54 -07001361 void assignPointerIds();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001362};
1363
1364
1365class SingleTouchInputMapper : public TouchInputMapper {
1366public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001367 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001368 virtual ~SingleTouchInputMapper();
1369
1370 virtual void reset();
1371 virtual void process(const RawEvent* rawEvent);
1372
1373protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001374 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001375
1376private:
Jeff Brown49754db2011-07-01 17:37:58 -07001377 CursorButtonAccumulator mCursorButtonAccumulator;
1378 TouchButtonAccumulator mTouchButtonAccumulator;
1379 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001380
Jeff Brown80fd47c2011-05-24 01:07:44 -07001381 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001382
1383 void sync(nsecs_t when);
1384};
1385
1386
1387class MultiTouchInputMapper : public TouchInputMapper {
1388public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001389 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001390 virtual ~MultiTouchInputMapper();
1391
1392 virtual void reset();
1393 virtual void process(const RawEvent* rawEvent);
1394
1395protected:
Jeff Brownbe1aa822011-07-27 16:04:54 -07001396 virtual void configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001397
1398private:
Jeff Brown49754db2011-07-01 17:37:58 -07001399 CursorButtonAccumulator mCursorButtonAccumulator;
1400 TouchButtonAccumulator mTouchButtonAccumulator;
1401 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001402
Jeff Brown6894a292011-07-01 17:59:27 -07001403 // Specifies the pointer id bits that are in use, and their associated tracking id.
1404 BitSet32 mPointerIdBits;
1405 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1406
Jeff Brown80fd47c2011-05-24 01:07:44 -07001407 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001408
1409 void sync(nsecs_t when);
1410};
1411
Jeff Browncb1404e2011-01-15 18:14:15 -08001412
1413class JoystickInputMapper : public InputMapper {
1414public:
1415 JoystickInputMapper(InputDevice* device);
1416 virtual ~JoystickInputMapper();
1417
1418 virtual uint32_t getSources();
1419 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1420 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -07001421 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08001422 virtual void reset();
1423 virtual void process(const RawEvent* rawEvent);
1424
1425private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001426 struct Axis {
1427 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001428 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001429
Jeff Brown6f2fba42011-02-19 01:08:02 -08001430 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001431
Jeff Brown6f2fba42011-02-19 01:08:02 -08001432 float scale; // scale factor from raw to normalized values
1433 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001434 float highScale; // scale factor from raw to normalized values of high split
1435 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001436
Jeff Brown6f2fba42011-02-19 01:08:02 -08001437 float min; // normalized inclusive minimum
1438 float max; // normalized inclusive maximum
1439 float flat; // normalized flat region size
1440 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001441
Jeff Brown6f2fba42011-02-19 01:08:02 -08001442 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001443 float currentValue; // current value
1444 float newValue; // most recent value
1445 float highCurrentValue; // current value of high split
1446 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001447
Jeff Brown85297452011-03-04 13:07:49 -08001448 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1449 bool explicitlyMapped, float scale, float offset,
1450 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001451 float min, float max, float flat, float fuzz) {
1452 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001453 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001454 this->explicitlyMapped = explicitlyMapped;
1455 this->scale = scale;
1456 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001457 this->highScale = highScale;
1458 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001459 this->min = min;
1460 this->max = max;
1461 this->flat = flat;
1462 this->fuzz = fuzz;
1463 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001464 resetValue();
1465 }
1466
1467 void resetValue() {
1468 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001469 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001470 this->highCurrentValue = 0;
1471 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001472 }
1473 };
1474
Jeff Brown6f2fba42011-02-19 01:08:02 -08001475 // Axes indexed by raw ABS_* axis index.
1476 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001477
Jeff Brown6f2fba42011-02-19 01:08:02 -08001478 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001479
Jeff Brown85297452011-03-04 13:07:49 -08001480 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001481 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001482 bool filterAxes(bool force);
1483
1484 static bool hasValueChangedSignificantly(float filter,
1485 float newValue, float currentValue, float min, float max);
1486 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1487 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001488
Jeff Brown6f2fba42011-02-19 01:08:02 -08001489 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001490};
1491
Jeff Brown46b9ac02010-04-22 18:58:52 -07001492} // namespace android
1493
1494#endif // _UI_INPUT_READER_H