blob: 288ff4e4d3861fa84ad308e0d5c860647493015a [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"
21#include "InputDispatcher.h"
22#include "PointerController.h"
23
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 // Determines whether to turn on some hacks we have to improve the touch interaction with a
61 // certain device whose screen currently is not all that good.
62 bool filterTouchEvents;
63
64 // Determines whether to turn on some hacks to improve touch interaction with another device
65 // where touch coordinate data can get corrupted.
66 bool filterJumpyTouchEvents;
67
68 // Gets the amount of time to disable virtual keys after the screen is touched
69 // in order to filter out accidental virtual key presses due to swiping gestures
70 // or taps near the edge of the display. May be 0 to disable the feature.
71 nsecs_t virtualKeyQuietTime;
72
73 // The excluded device names for the platform.
74 // Devices with these names will be ignored.
75 Vector<String8> excludedDeviceNames;
76
Jeff Brown19c97d462011-06-01 12:33:19 -070077 // Velocity control parameters for mouse pointer movements.
78 VelocityControlParameters pointerVelocityControlParameters;
79
80 // Velocity control parameters for mouse wheel movements.
81 VelocityControlParameters wheelVelocityControlParameters;
82
Jeff Brown474dcb52011-06-14 20:22:50 -070083 // True if pointer gestures are enabled.
84 bool pointerGesturesEnabled;
85
Jeff Brown214eaf42011-05-26 19:17:02 -070086 // Quiet time between certain pointer gesture transitions.
87 // Time to allow for all fingers or buttons to settle into a stable state before
88 // starting a new gesture.
89 nsecs_t pointerGestureQuietInterval;
90
91 // The minimum speed that a pointer must travel for us to consider switching the active
92 // touch pointer to it during a drag. This threshold is set to avoid switching due
93 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
94 float pointerGestureDragMinSwitchSpeed; // in pixels per second
95
96 // Tap gesture delay time.
97 // The time between down and up must be less than this to be considered a tap.
98 nsecs_t pointerGestureTapInterval;
99
100 // Tap drag gesture delay time.
101 // The time between the previous tap's up and the next down must be less than
102 // this to be considered a drag. Otherwise, the previous tap is finished and a
103 // new tap begins.
104 //
105 // Note that the previous tap will be held down for this entire duration so this
106 // interval must be shorter than the long press timeout.
107 nsecs_t pointerGestureTapDragInterval;
108
109 // The distance in pixels that the pointer is allowed to move from initial down
110 // to up and still be called a tap.
111 float pointerGestureTapSlop; // in pixels
112
113 // Time after the first touch points go down to settle on an initial centroid.
114 // This is intended to be enough time to handle cases where the user puts down two
115 // fingers at almost but not quite exactly the same time.
116 nsecs_t pointerGestureMultitouchSettleInterval;
117
118 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700119 // at least two pointers have moved at least this far from their starting place.
120 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700121
122 // The transition from PRESS to SWIPE gesture mode can only occur when the
123 // cosine of the angle between the two vectors is greater than or equal to than this value
124 // which indicates that the vectors are oriented in the same direction.
125 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
126 // (In exactly opposite directions, the cosine is -1.0.)
127 float pointerGestureSwipeTransitionAngleCosine;
128
129 // The transition from PRESS to SWIPE gesture mode can only occur when the
130 // fingers are no more than this far apart relative to the diagonal size of
131 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
132 // no more than half the diagonal size of the touch pad apart.
133 float pointerGestureSwipeMaxWidthRatio;
134
135 // The gesture movement speed factor relative to the size of the display.
136 // Movement speed applies when the fingers are moving in the same direction.
137 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
138 // will cover this portion of the display diagonal.
139 float pointerGestureMovementSpeedRatio;
140
141 // The gesture zoom speed factor relative to the size of the display.
142 // Zoom speed applies when the fingers are mostly moving relative to each other
143 // to execute a scale gesture or similar.
144 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
145 // will cover this portion of the display diagonal.
146 float pointerGestureZoomSpeedRatio;
147
148 InputReaderConfiguration() :
149 filterTouchEvents(false),
150 filterJumpyTouchEvents(false),
151 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700152 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700153 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700154 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700155 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
156 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
157 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
158 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
159 pointerGestureTapSlop(10.0f), // 10 pixels
160 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700161 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700162 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700163 pointerGestureSwipeMaxWidthRatio(0.25f),
164 pointerGestureMovementSpeedRatio(0.8f),
Jeff Brown214eaf42011-05-26 19:17:02 -0700165 pointerGestureZoomSpeedRatio(0.3f) { }
166};
167
168
169/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700170 * Input reader policy interface.
171 *
172 * The input reader policy is used by the input reader to interact with the Window Manager
173 * and other system components.
174 *
175 * The actual implementation is partially supported by callbacks into the DVM
176 * via JNI. This interface is also mocked in the unit tests.
177 */
178class InputReaderPolicyInterface : public virtual RefBase {
179protected:
180 InputReaderPolicyInterface() { }
181 virtual ~InputReaderPolicyInterface() { }
182
183public:
184 /* Display orientations. */
185 enum {
186 ROTATION_0 = 0,
187 ROTATION_90 = 1,
188 ROTATION_180 = 2,
189 ROTATION_270 = 3
190 };
191
Jeff Brown9c3cda02010-06-15 01:31:58 -0700192 /* Gets information about the display with the specified id.
193 * Returns true if the display info is available, false otherwise.
194 */
195 virtual bool getDisplayInfo(int32_t displayId,
196 int32_t* width, int32_t* height, int32_t* orientation) = 0;
197
Jeff Brown214eaf42011-05-26 19:17:02 -0700198 /* Gets the input reader configuration. */
199 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800200
201 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
202 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203};
204
205
206/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700207class InputReaderInterface : public virtual RefBase {
208protected:
209 InputReaderInterface() { }
210 virtual ~InputReaderInterface() { }
211
212public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700213 /* Dumps the state of the input reader.
214 *
215 * This method may be called on any thread (usually by the input manager). */
216 virtual void dump(String8& dump) = 0;
217
Jeff Brown46b9ac02010-04-22 18:58:52 -0700218 /* Runs a single iteration of the processing loop.
219 * Nominally reads and processes one incoming message from the EventHub.
220 *
221 * This method should be called on the input reader thread.
222 */
223 virtual void loopOnce() = 0;
224
Jeff Brown9c3cda02010-06-15 01:31:58 -0700225 /* Gets the current input device configuration.
226 *
227 * This method may be called on any thread (usually by the input manager).
228 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700230
Jeff Brown6d0fec22010-07-23 21:28:06 -0700231 /* Gets information about the specified input device.
232 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
233 * was no such device.
234 *
235 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700236 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700237 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
238
239 /* Gets the list of all registered device ids. */
240 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
241
242 /* Query current input state. */
243 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
244 int32_t scanCode) = 0;
245 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
246 int32_t keyCode) = 0;
247 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
248 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700249
250 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700251 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
252 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700253
Jeff Brown474dcb52011-06-14 20:22:50 -0700254 /* Requests that a reconfiguration of all input devices.
255 * The changes flag is a bitfield that indicates what has changed and whether
256 * the input devices must all be reopened. */
257 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700258};
259
260
261/* Internal interface used by individual input devices to access global input device state
262 * and parameters maintained by the input reader.
263 */
264class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700265public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700266 InputReaderContext() { }
267 virtual ~InputReaderContext() { }
268
Jeff Brown6d0fec22010-07-23 21:28:06 -0700269 virtual void updateGlobalMetaState() = 0;
270 virtual int32_t getGlobalMetaState() = 0;
271
Jeff Brownfe508922011-01-18 15:10:10 -0800272 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
273 virtual bool shouldDropVirtualKey(nsecs_t now,
274 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
275
Jeff Brown05dc66a2011-03-02 14:41:58 -0800276 virtual void fadePointer() = 0;
277
Jeff Brownaa3855d2011-03-17 01:34:19 -0700278 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
279
Jeff Brown6d0fec22010-07-23 21:28:06 -0700280 virtual InputReaderPolicyInterface* getPolicy() = 0;
281 virtual InputDispatcherInterface* getDispatcher() = 0;
282 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700283};
284
Jeff Brown9c3cda02010-06-15 01:31:58 -0700285
Jeff Brown46b9ac02010-04-22 18:58:52 -0700286/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700287 * that it sends to the input dispatcher. Some functions of the input reader, such as early
288 * event filtering in low power states, are controlled by a separate policy object.
289 *
290 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700291 * Because the policy and dispatcher can potentially block or cause re-entrance into
292 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700293 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700294 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700295class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700299 const sp<InputDispatcherInterface>& dispatcher);
300 virtual ~InputReader();
301
Jeff Brownb88102f2010-09-08 11:49:43 -0700302 virtual void dump(String8& dump);
303
Jeff Brown46b9ac02010-04-22 18:58:52 -0700304 virtual void loopOnce();
305
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700307
Jeff Brown6d0fec22010-07-23 21:28:06 -0700308 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
309 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700310
Jeff Brown6d0fec22010-07-23 21:28:06 -0700311 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
312 int32_t scanCode);
313 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
314 int32_t keyCode);
315 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
316 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317
Jeff Brown6d0fec22010-07-23 21:28:06 -0700318 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
319 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320
Jeff Brown474dcb52011-06-14 20:22:50 -0700321 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700322
Jeff Brownc3db8582010-10-20 15:33:38 -0700323protected:
324 // These methods are protected virtual so they can be overridden and instrumented
325 // by test cases.
326 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
327
Jeff Brown46b9ac02010-04-22 18:58:52 -0700328private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700329 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700330 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700331 sp<InputDispatcherInterface> mDispatcher;
332
Jeff Brown214eaf42011-05-26 19:17:02 -0700333 InputReaderConfiguration mConfig;
334
Jeff Brown6d0fec22010-07-23 21:28:06 -0700335 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
336 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
337 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
338
Jeff Brownb7198742011-03-18 18:14:26 -0700339 // The event queue.
340 static const int EVENT_BUFFER_SIZE = 256;
341 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
342
Jeff Brown6d0fec22010-07-23 21:28:06 -0700343 // This reader/writer lock guards the list of input devices.
344 // The writer lock must be held whenever the list of input devices is modified
345 // and then promptly released.
346 // The reader lock must be held whenever the list of input devices is traversed or an
347 // input device in the list is accessed.
348 // This lock only protects the registry and prevents inadvertent deletion of device objects
349 // that are in use. Individual devices are responsible for guarding their own internal state
350 // as needed for concurrent operation.
351 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700352 KeyedVector<int32_t, InputDevice*> mDevices;
353
Jeff Brown6d0fec22010-07-23 21:28:06 -0700354 // low-level input event decoding and device management
Jeff Brownb7198742011-03-18 18:14:26 -0700355 void processEvents(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700356
Jeff Brown7342bb92010-10-01 18:55:43 -0700357 void addDevice(int32_t deviceId);
358 void removeDevice(int32_t deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700359 void processEventsForDevice(int32_t deviceId, const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700360 void timeoutExpired(nsecs_t when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700361
Jeff Brownc3db8582010-10-20 15:33:38 -0700362 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700363
Jeff Brown6d0fec22010-07-23 21:28:06 -0700364 // state management for all devices
365 Mutex mStateLock;
366
Jeff Brown1a84fd12011-06-02 01:26:32 -0700367 int32_t mGlobalMetaState; // guarded by mStateLock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700368 virtual void updateGlobalMetaState();
369 virtual int32_t getGlobalMetaState();
370
Jeff Brown05dc66a2011-03-02 14:41:58 -0800371 virtual void fadePointer();
372
Jeff Brown1a84fd12011-06-02 01:26:32 -0700373 InputConfiguration mInputConfiguration; // guarded by mStateLock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 void updateInputConfiguration();
375
Jeff Brownb7198742011-03-18 18:14:26 -0700376 nsecs_t mDisableVirtualKeysTimeout; // only accessed by reader thread
Jeff Brownfe508922011-01-18 15:10:10 -0800377 virtual void disableVirtualKeysUntil(nsecs_t time);
378 virtual bool shouldDropVirtualKey(nsecs_t now,
379 InputDevice* device, int32_t keyCode, int32_t scanCode);
380
Jeff Brown1a84fd12011-06-02 01:26:32 -0700381 nsecs_t mNextTimeout; // only accessed by reader thread, not guarded
Jeff Brownaa3855d2011-03-17 01:34:19 -0700382 virtual void requestTimeoutAtTime(nsecs_t when);
383
Jeff Brown474dcb52011-06-14 20:22:50 -0700384 uint32_t mConfigurationChangesToRefresh; // guarded by mStateLock
385 void refreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700386
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387 // state queries
388 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
389 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
390 GetStateFunc getStateFunc);
391 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
392 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700393};
394
395
396/* Reads raw events from the event hub and processes them, endlessly. */
397class InputReaderThread : public Thread {
398public:
399 InputReaderThread(const sp<InputReaderInterface>& reader);
400 virtual ~InputReaderThread();
401
402private:
403 sp<InputReaderInterface> mReader;
404
405 virtual bool threadLoop();
406};
407
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408
409/* Represents the state of a single input device. */
410class InputDevice {
411public:
412 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
413 ~InputDevice();
414
415 inline InputReaderContext* getContext() { return mContext; }
416 inline int32_t getId() { return mId; }
417 inline const String8& getName() { return mName; }
418 inline uint32_t getSources() { return mSources; }
419
Jeff Brown56194eb2011-03-02 19:23:13 -0800420 inline bool isExternal() { return mIsExternal; }
421 inline void setExternal(bool external) { mIsExternal = external; }
422
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 inline bool isIgnored() { return mMappers.isEmpty(); }
424
Jeff Brownef3d7e82010-09-30 14:33:04 -0700425 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700426 void addMapper(InputMapper* mapper);
Jeff Brown474dcb52011-06-14 20:22:50 -0700427 void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700428 void reset();
Jeff Brownb7198742011-03-18 18:14:26 -0700429 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700430 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431
432 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
433 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
434 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
435 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
436 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
437 const int32_t* keyCodes, uint8_t* outFlags);
438
439 int32_t getMetaState();
440
Jeff Brown05dc66a2011-03-02 14:41:58 -0800441 void fadePointer();
442
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800443 inline const PropertyMap& getConfiguration() {
444 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700445 }
446
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447private:
448 InputReaderContext* mContext;
449 int32_t mId;
450
451 Vector<InputMapper*> mMappers;
452
453 String8 mName;
454 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800455 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700456 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457
458 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
459 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700460
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800461 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462};
463
464
465/* An input mapper transforms raw input events into cooked event data.
466 * A single input device can have multiple associated input mappers in order to interpret
467 * different classes of events.
468 */
469class InputMapper {
470public:
471 InputMapper(InputDevice* device);
472 virtual ~InputMapper();
473
474 inline InputDevice* getDevice() { return mDevice; }
475 inline int32_t getDeviceId() { return mDevice->getId(); }
476 inline const String8 getDeviceName() { return mDevice->getName(); }
477 inline InputReaderContext* getContext() { return mContext; }
478 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
479 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
480 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
481
482 virtual uint32_t getSources() = 0;
483 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700484 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700485 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486 virtual void reset();
487 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700488 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700489
490 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
491 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
492 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
493 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
494 const int32_t* keyCodes, uint8_t* outFlags);
495
496 virtual int32_t getMetaState();
497
Jeff Brown05dc66a2011-03-02 14:41:58 -0800498 virtual void fadePointer();
499
Jeff Brown6d0fec22010-07-23 21:28:06 -0700500protected:
501 InputDevice* mDevice;
502 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800503
504 static void dumpRawAbsoluteAxisInfo(String8& dump,
505 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700506};
507
508
509class SwitchInputMapper : public InputMapper {
510public:
511 SwitchInputMapper(InputDevice* device);
512 virtual ~SwitchInputMapper();
513
514 virtual uint32_t getSources();
515 virtual void process(const RawEvent* rawEvent);
516
517 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
518
519private:
520 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
521};
522
523
524class KeyboardInputMapper : public InputMapper {
525public:
Jeff Brownefd32662011-03-08 15:13:06 -0800526 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527 virtual ~KeyboardInputMapper();
528
529 virtual uint32_t getSources();
530 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700531 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700532 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700533 virtual void reset();
534 virtual void process(const RawEvent* rawEvent);
535
536 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
537 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
538 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
539 const int32_t* keyCodes, uint8_t* outFlags);
540
541 virtual int32_t getMetaState();
542
543private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700544 Mutex mLock;
545
Jeff Brown6d0fec22010-07-23 21:28:06 -0700546 struct KeyDown {
547 int32_t keyCode;
548 int32_t scanCode;
549 };
550
Jeff Brownefd32662011-03-08 15:13:06 -0800551 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700552 int32_t mKeyboardType;
553
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800554 // Immutable configuration parameters.
555 struct Parameters {
556 int32_t associatedDisplayId;
557 bool orientationAware;
558 } mParameters;
559
Jeff Brown6328cdc2010-07-29 18:18:33 -0700560 struct LockedState {
561 Vector<KeyDown> keyDowns; // keys that are down
562 int32_t metaState;
563 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700564
565 struct LedState {
566 bool avail; // led is available
567 bool on; // we think the led is currently on
568 };
569 LedState capsLockLedState;
570 LedState numLockLedState;
571 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700572 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700573
Jeff Brown6328cdc2010-07-29 18:18:33 -0700574 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800576 void configureParameters();
577 void dumpParameters(String8& dump);
578
Jeff Brown6d0fec22010-07-23 21:28:06 -0700579 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700580
Jeff Brown6d0fec22010-07-23 21:28:06 -0700581 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
582 uint32_t policyFlags);
583
Jeff Brown6328cdc2010-07-29 18:18:33 -0700584 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700585
Jeff Brown49ed71d2010-12-06 17:13:33 -0800586 void resetLedStateLocked();
587 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700588 void updateLedStateLocked(bool reset);
589 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
590 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700591};
592
593
Jeff Brown83c09682010-12-23 17:50:18 -0800594class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595public:
Jeff Brown83c09682010-12-23 17:50:18 -0800596 CursorInputMapper(InputDevice* device);
597 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598
599 virtual uint32_t getSources();
600 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700601 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700602 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700603 virtual void reset();
604 virtual void process(const RawEvent* rawEvent);
605
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700606 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
607
Jeff Brown05dc66a2011-03-02 14:41:58 -0800608 virtual void fadePointer();
609
Jeff Brown6d0fec22010-07-23 21:28:06 -0700610private:
611 // Amount that trackball needs to move in order to generate a key event.
612 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
613
Jeff Brown6328cdc2010-07-29 18:18:33 -0700614 Mutex mLock;
615
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800616 // Immutable configuration parameters.
617 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800618 enum Mode {
619 MODE_POINTER,
620 MODE_NAVIGATION,
621 };
622
623 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800624 int32_t associatedDisplayId;
625 bool orientationAware;
626 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700627
628 struct Accumulator {
629 enum {
Jeff Brownefd32662011-03-08 15:13:06 -0800630 FIELD_BUTTONS = 1,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700631 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800632 FIELD_REL_Y = 4,
633 FIELD_REL_WHEEL = 8,
634 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635 };
636
637 uint32_t fields;
638
Jeff Brownefd32662011-03-08 15:13:06 -0800639 uint32_t buttonDown;
640 uint32_t buttonUp;
641
Jeff Brown6d0fec22010-07-23 21:28:06 -0700642 int32_t relX;
643 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800644 int32_t relWheel;
645 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700646
647 inline void clear() {
648 fields = 0;
649 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700650 } mAccumulator;
651
Jeff Brownefd32662011-03-08 15:13:06 -0800652 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700653 float mXScale;
654 float mYScale;
655 float mXPrecision;
656 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800657
658 bool mHaveVWheel;
659 bool mHaveHWheel;
660 float mVWheelScale;
661 float mHWheelScale;
662
Jeff Brown19c97d462011-06-01 12:33:19 -0700663 // Velocity controls for mouse pointer and wheel movements.
664 // The controls for X and Y wheel movements are separate to keep them decoupled.
665 VelocityControl mPointerVelocityControl;
666 VelocityControl mWheelXVelocityControl;
667 VelocityControl mWheelYVelocityControl;
668
Jeff Brown83c09682010-12-23 17:50:18 -0800669 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700670
Jeff Brown6328cdc2010-07-29 18:18:33 -0700671 struct LockedState {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700672 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700673 nsecs_t downTime;
674 } mLocked;
675
676 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700677
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800678 void configureParameters();
679 void dumpParameters(String8& dump);
680
Jeff Brown6d0fec22010-07-23 21:28:06 -0700681 void sync(nsecs_t when);
682};
683
684
685class TouchInputMapper : public InputMapper {
686public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800687 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700688 virtual ~TouchInputMapper();
689
690 virtual uint32_t getSources();
691 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700692 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700693 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700694 virtual void reset();
695
696 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
697 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
698 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
699 const int32_t* keyCodes, uint8_t* outFlags);
700
Jeff Brownace13b12011-03-09 17:39:48 -0800701 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -0700702 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -0800703
Jeff Brown6d0fec22010-07-23 21:28:06 -0700704protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700705 Mutex mLock;
706
Jeff Brown6d0fec22010-07-23 21:28:06 -0700707 struct VirtualKey {
708 int32_t keyCode;
709 int32_t scanCode;
710 uint32_t flags;
711
712 // computed hit box, specified in touch screen coords based on known display size
713 int32_t hitLeft;
714 int32_t hitTop;
715 int32_t hitRight;
716 int32_t hitBottom;
717
718 inline bool isHit(int32_t x, int32_t y) const {
719 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
720 }
721 };
722
Jeff Brown8d608662010-08-30 03:02:23 -0700723 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700724 struct PointerData {
725 uint32_t id;
726 int32_t x;
727 int32_t y;
728 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700729 int32_t touchMajor;
730 int32_t touchMinor;
731 int32_t toolMajor;
732 int32_t toolMinor;
733 int32_t orientation;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700734 int32_t distance;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700735 bool isStylus;
Jeff Brownc3db8582010-10-20 15:33:38 -0700736
737 inline bool operator== (const PointerData& other) const {
738 return id == other.id
739 && x == other.x
740 && y == other.y
741 && pressure == other.pressure
742 && touchMajor == other.touchMajor
743 && touchMinor == other.touchMinor
744 && toolMajor == other.toolMajor
745 && toolMinor == other.toolMinor
Jeff Brown80fd47c2011-05-24 01:07:44 -0700746 && orientation == other.orientation
747 && distance == other.distance;
Jeff Brownc3db8582010-10-20 15:33:38 -0700748 }
749 inline bool operator!= (const PointerData& other) const {
750 return !(*this == other);
751 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700752 };
753
Jeff Brown8d608662010-08-30 03:02:23 -0700754 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700755 struct TouchData {
756 uint32_t pointerCount;
757 PointerData pointers[MAX_POINTERS];
758 BitSet32 idBits;
759 uint32_t idToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700760 int32_t buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700761
762 void copyFrom(const TouchData& other) {
763 pointerCount = other.pointerCount;
764 idBits = other.idBits;
Jeff Brownace13b12011-03-09 17:39:48 -0800765 buttonState = other.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700766
767 for (uint32_t i = 0; i < pointerCount; i++) {
768 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700769
770 int id = pointers[i].id;
771 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700772 }
773 }
774
775 inline void clear() {
776 pointerCount = 0;
777 idBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -0800778 buttonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700779 }
Jeff Brown2352b972011-04-12 22:39:53 -0700780
781 void getCentroid(float* outX, float* outY) {
782 float x = 0, y = 0;
783 if (pointerCount != 0) {
784 for (uint32_t i = 0; i < pointerCount; i++) {
785 x += pointers[i].x;
786 y += pointers[i].y;
787 }
788 x /= pointerCount;
789 y /= pointerCount;
790 }
791 *outX = x;
792 *outY = y;
793 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700794 };
795
Jeff Brown83c09682010-12-23 17:50:18 -0800796 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800797 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brownace13b12011-03-09 17:39:48 -0800798 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800799
Jeff Brown214eaf42011-05-26 19:17:02 -0700800 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -0700801 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -0700802
Jeff Brown6d0fec22010-07-23 21:28:06 -0700803 // Immutable configuration parameters.
804 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800805 enum DeviceType {
806 DEVICE_TYPE_TOUCH_SCREEN,
807 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -0800808 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800809 };
810
811 DeviceType deviceType;
812 int32_t associatedDisplayId;
813 bool orientationAware;
814
Jeff Brown6d0fec22010-07-23 21:28:06 -0700815 bool useBadTouchFilter;
816 bool useJumpyTouchFilter;
817 bool useAveragingTouchFilter;
Jeff Brown2352b972011-04-12 22:39:53 -0700818
819 enum GestureMode {
820 GESTURE_MODE_POINTER,
821 GESTURE_MODE_SPOTS,
822 };
823 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700824 } mParameters;
825
Jeff Brown8d608662010-08-30 03:02:23 -0700826 // Immutable calibration parameters in parsed form.
827 struct Calibration {
Jeff Brownc6d282b2010-10-14 21:42:15 -0700828 // Touch Size
829 enum TouchSizeCalibration {
830 TOUCH_SIZE_CALIBRATION_DEFAULT,
831 TOUCH_SIZE_CALIBRATION_NONE,
832 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
833 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700834 };
835
Jeff Brownc6d282b2010-10-14 21:42:15 -0700836 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700837
Jeff Brownc6d282b2010-10-14 21:42:15 -0700838 // Tool Size
839 enum ToolSizeCalibration {
840 TOOL_SIZE_CALIBRATION_DEFAULT,
841 TOOL_SIZE_CALIBRATION_NONE,
842 TOOL_SIZE_CALIBRATION_GEOMETRIC,
843 TOOL_SIZE_CALIBRATION_LINEAR,
844 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700845 };
846
Jeff Brownc6d282b2010-10-14 21:42:15 -0700847 ToolSizeCalibration toolSizeCalibration;
848 bool haveToolSizeLinearScale;
849 float toolSizeLinearScale;
850 bool haveToolSizeLinearBias;
851 float toolSizeLinearBias;
852 bool haveToolSizeAreaScale;
853 float toolSizeAreaScale;
854 bool haveToolSizeAreaBias;
855 float toolSizeAreaBias;
856 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800857 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700858
859 // Pressure
860 enum PressureCalibration {
861 PRESSURE_CALIBRATION_DEFAULT,
862 PRESSURE_CALIBRATION_NONE,
863 PRESSURE_CALIBRATION_PHYSICAL,
864 PRESSURE_CALIBRATION_AMPLITUDE,
865 };
866 enum PressureSource {
867 PRESSURE_SOURCE_DEFAULT,
868 PRESSURE_SOURCE_PRESSURE,
869 PRESSURE_SOURCE_TOUCH,
870 };
871
872 PressureCalibration pressureCalibration;
873 PressureSource pressureSource;
874 bool havePressureScale;
875 float pressureScale;
876
877 // Size
878 enum SizeCalibration {
879 SIZE_CALIBRATION_DEFAULT,
880 SIZE_CALIBRATION_NONE,
881 SIZE_CALIBRATION_NORMALIZED,
882 };
883
884 SizeCalibration sizeCalibration;
885
886 // Orientation
887 enum OrientationCalibration {
888 ORIENTATION_CALIBRATION_DEFAULT,
889 ORIENTATION_CALIBRATION_NONE,
890 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800891 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700892 };
893
894 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700895
896 // Distance
897 enum DistanceCalibration {
898 DISTANCE_CALIBRATION_DEFAULT,
899 DISTANCE_CALIBRATION_NONE,
900 DISTANCE_CALIBRATION_SCALED,
901 };
902
903 DistanceCalibration distanceCalibration;
904 bool haveDistanceScale;
905 float distanceScale;
Jeff Brown8d608662010-08-30 03:02:23 -0700906 } mCalibration;
907
908 // Raw axis information from the driver.
909 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700910 RawAbsoluteAxisInfo x;
911 RawAbsoluteAxisInfo y;
912 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700913 RawAbsoluteAxisInfo touchMajor;
914 RawAbsoluteAxisInfo touchMinor;
915 RawAbsoluteAxisInfo toolMajor;
916 RawAbsoluteAxisInfo toolMinor;
917 RawAbsoluteAxisInfo orientation;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700918 RawAbsoluteAxisInfo distance;
919 RawAbsoluteAxisInfo trackingId;
920 RawAbsoluteAxisInfo slot;
Jeff Brown8d608662010-08-30 03:02:23 -0700921 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700922
Jeff Brown6328cdc2010-07-29 18:18:33 -0700923 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700924 TouchData mCurrentTouch;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700925 PointerProperties mCurrentTouchProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -0800926 PointerCoords mCurrentTouchCoords[MAX_POINTERS];
927
Jeff Brown6d0fec22010-07-23 21:28:06 -0700928 TouchData mLastTouch;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700929 PointerProperties mLastTouchProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -0800930 PointerCoords mLastTouchCoords[MAX_POINTERS];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700931
932 // The time the primary pointer last went down.
933 nsecs_t mDownTime;
934
Jeff Brownace13b12011-03-09 17:39:48 -0800935 // The pointer controller, or null if the device is not a pointer.
936 sp<PointerControllerInterface> mPointerController;
937
Jeff Brown6328cdc2010-07-29 18:18:33 -0700938 struct LockedState {
939 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700940
Jeff Brown6328cdc2010-07-29 18:18:33 -0700941 // The surface orientation and width and height set by configureSurfaceLocked().
942 int32_t surfaceOrientation;
943 int32_t surfaceWidth, surfaceHeight;
944
Jeff Brownefd32662011-03-08 15:13:06 -0800945 // The associated display orientation and width and height set by configureSurfaceLocked().
946 int32_t associatedDisplayOrientation;
947 int32_t associatedDisplayWidth, associatedDisplayHeight;
948
Jeff Brown6328cdc2010-07-29 18:18:33 -0700949 // Translation and scaling factors, orientation-independent.
Jeff Brown6328cdc2010-07-29 18:18:33 -0700950 float xScale;
951 float xPrecision;
952
Jeff Brown6328cdc2010-07-29 18:18:33 -0700953 float yScale;
954 float yPrecision;
955
Jeff Brown8d608662010-08-30 03:02:23 -0700956 float geometricScale;
957
Jeff Brownc6d282b2010-10-14 21:42:15 -0700958 float toolSizeLinearScale;
959 float toolSizeLinearBias;
960 float toolSizeAreaScale;
961 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700962
Jeff Brown6328cdc2010-07-29 18:18:33 -0700963 float pressureScale;
964
Jeff Brown6328cdc2010-07-29 18:18:33 -0700965 float sizeScale;
966
967 float orientationScale;
968
Jeff Brown80fd47c2011-05-24 01:07:44 -0700969 float distanceScale;
970
Jeff Brown6328cdc2010-07-29 18:18:33 -0700971 // Oriented motion ranges for input device info.
972 struct OrientedRanges {
973 InputDeviceInfo::MotionRange x;
974 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700975
976 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700977 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700978
979 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700980 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700981
Jeff Brownc6d282b2010-10-14 21:42:15 -0700982 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700983 InputDeviceInfo::MotionRange touchMajor;
984 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700985
Jeff Brownc6d282b2010-10-14 21:42:15 -0700986 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700987 InputDeviceInfo::MotionRange toolMajor;
988 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700989
990 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700991 InputDeviceInfo::MotionRange orientation;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700992
993 bool haveDistance;
994 InputDeviceInfo::MotionRange distance;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700995 } orientedRanges;
996
997 // Oriented dimensions and precision.
998 float orientedSurfaceWidth, orientedSurfaceHeight;
999 float orientedXPrecision, orientedYPrecision;
1000
1001 struct CurrentVirtualKeyState {
1002 bool down;
1003 nsecs_t downTime;
1004 int32_t keyCode;
1005 int32_t scanCode;
1006 } currentVirtualKey;
Jeff Brownace13b12011-03-09 17:39:48 -08001007
1008 // Scale factor for gesture based pointer movements.
1009 float pointerGestureXMovementScale;
1010 float pointerGestureYMovementScale;
1011
1012 // Scale factor for gesture based zooming and other freeform motions.
1013 float pointerGestureXZoomScale;
1014 float pointerGestureYZoomScale;
1015
Jeff Brown2352b972011-04-12 22:39:53 -07001016 // The maximum swipe width.
1017 float pointerGestureMaxSwipeWidth;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001018 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019
Jeff Brown8d608662010-08-30 03:02:23 -07001020 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001021 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001022 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001023 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001024 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001025 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001026 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001027 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001028 virtual void parseCalibration();
1029 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001030 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031
1032 enum TouchResult {
1033 // Dispatch the touch normally.
1034 DISPATCH_TOUCH,
1035 // Do not dispatch the touch, but keep tracking the current stroke.
1036 SKIP_TOUCH,
1037 // Do not dispatch the touch, and drop all information associated with the current stoke
1038 // so the next movement will appear as a new down.
1039 DROP_STROKE
1040 };
1041
1042 void syncTouch(nsecs_t when, bool havePointerIds);
1043
1044private:
1045 /* Maximum number of historical samples to average. */
1046 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
1047
1048 /* Slop distance for jumpy pointer detection.
1049 * The vertical range of the screen divided by this is our epsilon value. */
1050 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
1051
1052 /* Number of jumpy points to drop for touchscreens that need it. */
1053 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
1054 static const uint32_t JUMPY_DROP_LIMIT = 3;
1055
1056 /* Maximum squared distance for averaging.
1057 * If moving farther than this, turn of averaging to avoid lag in response. */
1058 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
1059
1060 struct AveragingTouchFilterState {
1061 // Individual history tracks are stored by pointer id
1062 uint32_t historyStart[MAX_POINTERS];
1063 uint32_t historyEnd[MAX_POINTERS];
1064 struct {
1065 struct {
1066 int32_t x;
1067 int32_t y;
1068 int32_t pressure;
1069 } pointers[MAX_POINTERS];
1070 } historyData[AVERAGING_HISTORY_SIZE];
1071 } mAveragingTouchFilter;
1072
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001073 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001074 uint32_t jumpyPointsDropped;
1075 } mJumpyTouchFilter;
1076
1077 struct PointerDistanceHeapElement {
1078 uint32_t currentPointerIndex : 8;
1079 uint32_t lastPointerIndex : 8;
1080 uint64_t distance : 48; // squared distance
1081 };
1082
Jeff Brownace13b12011-03-09 17:39:48 -08001083 struct PointerGesture {
1084 enum Mode {
1085 // No fingers, button is not pressed.
1086 // Nothing happening.
1087 NEUTRAL,
1088
1089 // No fingers, button is not pressed.
1090 // Tap detected.
1091 // Emits DOWN and UP events at the pointer location.
1092 TAP,
1093
Jeff Brown79ac9692011-04-19 21:20:10 -07001094 // Exactly one finger dragging following a tap.
1095 // Pointer follows the active finger.
1096 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001097 //
1098 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001099 TAP_DRAG,
1100
Jeff Brownace13b12011-03-09 17:39:48 -08001101 // Button is pressed.
1102 // Pointer follows the active finger if there is one. Other fingers are ignored.
1103 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001104 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001105
1106 // Exactly one finger, button is not pressed.
1107 // Pointer follows the active finger.
1108 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001109 //
1110 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001111 HOVER,
1112
Jeff Brown2352b972011-04-12 22:39:53 -07001113 // Exactly two fingers but neither have moved enough to clearly indicate
1114 // whether a swipe or freeform gesture was intended. We consider the
1115 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1116 // Pointer does not move.
1117 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1118 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001119
1120 // Exactly two fingers moving in the same direction, button is not pressed.
1121 // Pointer does not move.
1122 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1123 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001124 SWIPE,
1125
1126 // Two or more fingers moving in arbitrary directions, button is not pressed.
1127 // Pointer does not move.
1128 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1129 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001130 FREEFORM,
1131
1132 // Waiting for quiet time to end before starting the next gesture.
1133 QUIET,
1134 };
1135
Jeff Brown2352b972011-04-12 22:39:53 -07001136 // Time the first finger went down.
1137 nsecs_t firstTouchTime;
1138
Jeff Brownace13b12011-03-09 17:39:48 -08001139 // The active pointer id from the raw touch data.
1140 int32_t activeTouchId; // -1 if none
1141
1142 // The active pointer id from the gesture last delivered to the application.
1143 int32_t activeGestureId; // -1 if none
1144
1145 // Pointer coords and ids for the current and previous pointer gesture.
1146 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001147 BitSet32 currentGestureIdBits;
1148 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001149 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001150 PointerCoords currentGestureCoords[MAX_POINTERS];
1151
1152 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001153 BitSet32 lastGestureIdBits;
1154 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001155 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001156 PointerCoords lastGestureCoords[MAX_POINTERS];
1157
Jeff Brownace13b12011-03-09 17:39:48 -08001158 // Time the pointer gesture last went down.
1159 nsecs_t downTime;
1160
Jeff Brown79ac9692011-04-19 21:20:10 -07001161 // Time when the pointer went down for a TAP.
1162 nsecs_t tapDownTime;
1163
1164 // Time when the pointer went up for a TAP.
1165 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001166
Jeff Brown2352b972011-04-12 22:39:53 -07001167 // Location of initial tap.
1168 float tapX, tapY;
1169
Jeff Brownace13b12011-03-09 17:39:48 -08001170 // Time we started waiting for quiescence.
1171 nsecs_t quietTime;
1172
Jeff Brown2352b972011-04-12 22:39:53 -07001173 // Reference points for multitouch gestures.
1174 float referenceTouchX; // reference touch X/Y coordinates in surface units
1175 float referenceTouchY;
1176 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1177 float referenceGestureY;
1178
Jeff Brown538881e2011-05-25 18:23:38 -07001179 // Distance that each pointer has traveled which has not yet been
1180 // subsumed into the reference gesture position.
1181 BitSet32 referenceIdBits;
1182 struct Delta {
1183 float dx, dy;
1184 };
1185 Delta referenceDeltas[MAX_POINTER_ID + 1];
1186
Jeff Brown2352b972011-04-12 22:39:53 -07001187 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1188 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1189
Jeff Brownace13b12011-03-09 17:39:48 -08001190 // A velocity tracker for determining whether to switch active pointers during drags.
1191 VelocityTracker velocityTracker;
1192
Jeff Brown19c97d462011-06-01 12:33:19 -07001193 // Velocity control for pointer movements.
1194 VelocityControl pointerVelocityControl;
1195
Jeff Brownace13b12011-03-09 17:39:48 -08001196 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001197 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001198 activeTouchId = -1;
1199 activeGestureId = -1;
1200 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001201 currentGestureIdBits.clear();
1202 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001203 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001204 downTime = 0;
1205 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001206 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001207 resetQuietTime();
Jeff Brown19c97d462011-06-01 12:33:19 -07001208 pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08001209 }
1210
Jeff Brown79ac9692011-04-19 21:20:10 -07001211 void resetTap() {
1212 tapDownTime = LLONG_MIN;
1213 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001214 }
1215
1216 void resetQuietTime() {
1217 quietTime = LLONG_MIN;
1218 }
1219 } mPointerGesture;
1220
Jeff Brown6328cdc2010-07-29 18:18:33 -07001221 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001222
1223 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
1224 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001225 void prepareTouches(int32_t* outEdgeFlags, float* outXPrecision, float* outYPrecision);
Jeff Brown79ac9692011-04-19 21:20:10 -07001226 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1227 bool preparePointerGestures(nsecs_t when,
1228 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
Jeff Brownace13b12011-03-09 17:39:48 -08001229
1230 // Dispatches a motion event.
1231 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1232 // method will take care of setting the index and transmuting the action to DOWN or UP
1233 // it is the first / last pointer to go down / up.
1234 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001235 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1236 int32_t edgeFlags,
1237 const PointerProperties* properties, const PointerCoords* coords,
1238 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001239 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1240
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001241 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001242 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001243 bool updateMovedPointers(const PointerProperties* inProperties,
1244 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1245 PointerProperties* outProperties, PointerCoords* outCoords,
1246 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001247
Jeff Brownefd32662011-03-08 15:13:06 -08001248 void suppressSwipeOntoVirtualKeys(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001249
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001250 int32_t getTouchToolType(bool isStylus) const;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001251 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
1252 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001253
1254 bool applyBadTouchFilter();
1255 bool applyJumpyTouchFilter();
1256 void applyAveragingTouchFilter();
1257 void calculatePointerIds();
1258};
1259
1260
1261class SingleTouchInputMapper : public TouchInputMapper {
1262public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001263 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001264 virtual ~SingleTouchInputMapper();
1265
1266 virtual void reset();
1267 virtual void process(const RawEvent* rawEvent);
1268
1269protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001270 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001271
1272private:
1273 struct Accumulator {
1274 enum {
1275 FIELD_BTN_TOUCH = 1,
1276 FIELD_ABS_X = 2,
1277 FIELD_ABS_Y = 4,
1278 FIELD_ABS_PRESSURE = 8,
Jeff Brownefd32662011-03-08 15:13:06 -08001279 FIELD_ABS_TOOL_WIDTH = 16,
Jeff Brownace13b12011-03-09 17:39:48 -08001280 FIELD_BUTTONS = 32,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001281 };
1282
1283 uint32_t fields;
1284
1285 bool btnTouch;
1286 int32_t absX;
1287 int32_t absY;
1288 int32_t absPressure;
1289 int32_t absToolWidth;
1290
Jeff Brownace13b12011-03-09 17:39:48 -08001291 uint32_t buttonDown;
1292 uint32_t buttonUp;
1293
Jeff Brown6d0fec22010-07-23 21:28:06 -07001294 inline void clear() {
1295 fields = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08001296 buttonDown = 0;
1297 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001298 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001299 } mAccumulator;
1300
1301 bool mDown;
1302 int32_t mX;
1303 int32_t mY;
1304 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07001305 int32_t mToolWidth;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001306 int32_t mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001307
Jeff Brown80fd47c2011-05-24 01:07:44 -07001308 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001309
1310 void sync(nsecs_t when);
1311};
1312
1313
1314class MultiTouchInputMapper : public TouchInputMapper {
1315public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001316 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001317 virtual ~MultiTouchInputMapper();
1318
1319 virtual void reset();
1320 virtual void process(const RawEvent* rawEvent);
1321
1322protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001323 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001324
1325private:
1326 struct Accumulator {
1327 enum {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001328 FIELD_ABS_MT_POSITION_X = 1 << 0,
1329 FIELD_ABS_MT_POSITION_Y = 1 << 1,
1330 FIELD_ABS_MT_TOUCH_MAJOR = 1 << 2,
1331 FIELD_ABS_MT_TOUCH_MINOR = 1 << 3,
1332 FIELD_ABS_MT_WIDTH_MAJOR = 1 << 4,
1333 FIELD_ABS_MT_WIDTH_MINOR = 1 << 5,
1334 FIELD_ABS_MT_ORIENTATION = 1 << 6,
1335 FIELD_ABS_MT_TRACKING_ID = 1 << 7,
1336 FIELD_ABS_MT_PRESSURE = 1 << 8,
1337 FIELD_ABS_MT_TOOL_TYPE = 1 << 9,
1338 FIELD_ABS_MT_DISTANCE = 1 << 10,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001339 };
1340
Jeff Brown80fd47c2011-05-24 01:07:44 -07001341 struct Slot {
1342 uint32_t fields; // 0 if slot is unused
Jeff Brown6d0fec22010-07-23 21:28:06 -07001343
1344 int32_t absMTPositionX;
1345 int32_t absMTPositionY;
1346 int32_t absMTTouchMajor;
1347 int32_t absMTTouchMinor;
1348 int32_t absMTWidthMajor;
1349 int32_t absMTWidthMinor;
1350 int32_t absMTOrientation;
1351 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001352 int32_t absMTPressure;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001353 int32_t absMTToolType;
1354 int32_t absMTDistance;
1355
1356 inline Slot() {
1357 clear();
1358 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001359
1360 inline void clear() {
1361 fields = 0;
1362 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07001363 };
1364
1365 // Current slot index.
1366 int32_t currentSlot;
1367
1368 // Array of slots.
1369 Slot* slots;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001370
Jeff Brownace13b12011-03-09 17:39:48 -08001371 // Bitfield of buttons that went down or up.
1372 uint32_t buttonDown;
1373 uint32_t buttonUp;
1374
Jeff Brown441a9c22011-06-02 18:22:25 -07001375 Accumulator() : currentSlot(0), slots(NULL), buttonDown(0), buttonUp(0) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001376 }
1377
1378 ~Accumulator() {
1379 delete[] slots;
1380 }
1381
1382 void allocateSlots(size_t slotCount) {
1383 slots = new Slot[slotCount];
1384 }
1385
Jeff Brown441a9c22011-06-02 18:22:25 -07001386 void clearSlots(size_t slotCount) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001387 for (size_t i = 0; i < slotCount; i++) {
1388 slots[i].clear();
1389 }
1390 currentSlot = 0;
Jeff Brown441a9c22011-06-02 18:22:25 -07001391 }
1392
1393 void clearButtons() {
Jeff Brownace13b12011-03-09 17:39:48 -08001394 buttonDown = 0;
1395 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001396 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001397 } mAccumulator;
1398
Jeff Brown80fd47c2011-05-24 01:07:44 -07001399 size_t mSlotCount;
1400 bool mUsingSlotsProtocol;
1401
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001402 int32_t mButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08001403
Jeff Brown80fd47c2011-05-24 01:07:44 -07001404 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001405
1406 void sync(nsecs_t when);
1407};
1408
Jeff Browncb1404e2011-01-15 18:14:15 -08001409
1410class JoystickInputMapper : public InputMapper {
1411public:
1412 JoystickInputMapper(InputDevice* device);
1413 virtual ~JoystickInputMapper();
1414
1415 virtual uint32_t getSources();
1416 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1417 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -07001418 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08001419 virtual void reset();
1420 virtual void process(const RawEvent* rawEvent);
1421
1422private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001423 struct Axis {
1424 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001425 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001426
Jeff Brown6f2fba42011-02-19 01:08:02 -08001427 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001428
Jeff Brown6f2fba42011-02-19 01:08:02 -08001429 float scale; // scale factor from raw to normalized values
1430 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001431 float highScale; // scale factor from raw to normalized values of high split
1432 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001433
Jeff Brown6f2fba42011-02-19 01:08:02 -08001434 float min; // normalized inclusive minimum
1435 float max; // normalized inclusive maximum
1436 float flat; // normalized flat region size
1437 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001438
Jeff Brown6f2fba42011-02-19 01:08:02 -08001439 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001440 float currentValue; // current value
1441 float newValue; // most recent value
1442 float highCurrentValue; // current value of high split
1443 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001444
Jeff Brown85297452011-03-04 13:07:49 -08001445 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1446 bool explicitlyMapped, float scale, float offset,
1447 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001448 float min, float max, float flat, float fuzz) {
1449 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001450 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001451 this->explicitlyMapped = explicitlyMapped;
1452 this->scale = scale;
1453 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001454 this->highScale = highScale;
1455 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001456 this->min = min;
1457 this->max = max;
1458 this->flat = flat;
1459 this->fuzz = fuzz;
1460 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001461 resetValue();
1462 }
1463
1464 void resetValue() {
1465 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001466 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001467 this->highCurrentValue = 0;
1468 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001469 }
1470 };
1471
Jeff Brown6f2fba42011-02-19 01:08:02 -08001472 // Axes indexed by raw ABS_* axis index.
1473 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001474
Jeff Brown6f2fba42011-02-19 01:08:02 -08001475 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001476
Jeff Brown85297452011-03-04 13:07:49 -08001477 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001478 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001479 bool filterAxes(bool force);
1480
1481 static bool hasValueChangedSignificantly(float filter,
1482 float newValue, float currentValue, float min, float max);
1483 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1484 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001485
Jeff Brown6f2fba42011-02-19 01:08:02 -08001486 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001487};
1488
Jeff Brown46b9ac02010-04-22 18:58:52 -07001489} // namespace android
1490
1491#endif // _UI_INPUT_READER_H