blob: f9750d0b8863d26217ad9e639680eec38ff6c02b [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
Jeff Brownb4ff35d2011-01-02 16:37:43 -080020#include "EventHub.h"
21#include "InputDispatcher.h"
22#include "PointerController.h"
23
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31#include <utils/BitSet.h>
32
33#include <stddef.h>
34#include <unistd.h>
35
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown214eaf42011-05-26 19:17:02 -070043 * Input reader configuration.
44 *
45 * Specifies various options that modify the behavior of the input reader.
46 */
47struct InputReaderConfiguration {
Jeff Brown474dcb52011-06-14 20:22:50 -070048 // Describes changes that have occurred.
49 enum {
50 // The pointer speed changed.
51 CHANGE_POINTER_SPEED = 1 << 0,
52
53 // The pointer gesture control changed.
54 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
55
56 // All devices must be reopened.
57 CHANGE_MUST_REOPEN = 1 << 31,
58 };
59
Jeff Brown214eaf42011-05-26 19:17:02 -070060 // Gets the amount of time to disable virtual keys after the screen is touched
61 // in order to filter out accidental virtual key presses due to swiping gestures
62 // or taps near the edge of the display. May be 0 to disable the feature.
63 nsecs_t virtualKeyQuietTime;
64
65 // The excluded device names for the platform.
66 // Devices with these names will be ignored.
67 Vector<String8> excludedDeviceNames;
68
Jeff Brown19c97d462011-06-01 12:33:19 -070069 // Velocity control parameters for mouse pointer movements.
70 VelocityControlParameters pointerVelocityControlParameters;
71
72 // Velocity control parameters for mouse wheel movements.
73 VelocityControlParameters wheelVelocityControlParameters;
74
Jeff Brown474dcb52011-06-14 20:22:50 -070075 // True if pointer gestures are enabled.
76 bool pointerGesturesEnabled;
77
Jeff Brown214eaf42011-05-26 19:17:02 -070078 // Quiet time between certain pointer gesture transitions.
79 // Time to allow for all fingers or buttons to settle into a stable state before
80 // starting a new gesture.
81 nsecs_t pointerGestureQuietInterval;
82
83 // The minimum speed that a pointer must travel for us to consider switching the active
84 // touch pointer to it during a drag. This threshold is set to avoid switching due
85 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
86 float pointerGestureDragMinSwitchSpeed; // in pixels per second
87
88 // Tap gesture delay time.
89 // The time between down and up must be less than this to be considered a tap.
90 nsecs_t pointerGestureTapInterval;
91
92 // Tap drag gesture delay time.
93 // The time between the previous tap's up and the next down must be less than
94 // this to be considered a drag. Otherwise, the previous tap is finished and a
95 // new tap begins.
96 //
97 // Note that the previous tap will be held down for this entire duration so this
98 // interval must be shorter than the long press timeout.
99 nsecs_t pointerGestureTapDragInterval;
100
101 // The distance in pixels that the pointer is allowed to move from initial down
102 // to up and still be called a tap.
103 float pointerGestureTapSlop; // in pixels
104
105 // Time after the first touch points go down to settle on an initial centroid.
106 // This is intended to be enough time to handle cases where the user puts down two
107 // fingers at almost but not quite exactly the same time.
108 nsecs_t pointerGestureMultitouchSettleInterval;
109
110 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700111 // at least two pointers have moved at least this far from their starting place.
112 float pointerGestureMultitouchMinDistance; // in pixels
Jeff Brown214eaf42011-05-26 19:17:02 -0700113
114 // The transition from PRESS to SWIPE gesture mode can only occur when the
115 // cosine of the angle between the two vectors is greater than or equal to than this value
116 // which indicates that the vectors are oriented in the same direction.
117 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
118 // (In exactly opposite directions, the cosine is -1.0.)
119 float pointerGestureSwipeTransitionAngleCosine;
120
121 // The transition from PRESS to SWIPE gesture mode can only occur when the
122 // fingers are no more than this far apart relative to the diagonal size of
123 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
124 // no more than half the diagonal size of the touch pad apart.
125 float pointerGestureSwipeMaxWidthRatio;
126
127 // The gesture movement speed factor relative to the size of the display.
128 // Movement speed applies when the fingers are moving in the same direction.
129 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
130 // will cover this portion of the display diagonal.
131 float pointerGestureMovementSpeedRatio;
132
133 // The gesture zoom speed factor relative to the size of the display.
134 // Zoom speed applies when the fingers are mostly moving relative to each other
135 // to execute a scale gesture or similar.
136 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
137 // will cover this portion of the display diagonal.
138 float pointerGestureZoomSpeedRatio;
139
140 InputReaderConfiguration() :
Jeff Brown214eaf42011-05-26 19:17:02 -0700141 virtualKeyQuietTime(0),
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700142 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
Jeff Brown19c97d462011-06-01 12:33:19 -0700143 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown474dcb52011-06-14 20:22:50 -0700144 pointerGesturesEnabled(true),
Jeff Brown214eaf42011-05-26 19:17:02 -0700145 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
146 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
147 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
148 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
149 pointerGestureTapSlop(10.0f), // 10 pixels
150 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700151 pointerGestureMultitouchMinDistance(15), // 15 pixels
Jeff Brown6674d9b2011-06-07 16:50:14 -0700152 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700153 pointerGestureSwipeMaxWidthRatio(0.25f),
154 pointerGestureMovementSpeedRatio(0.8f),
Jeff Brown214eaf42011-05-26 19:17:02 -0700155 pointerGestureZoomSpeedRatio(0.3f) { }
156};
157
158
159/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700160 * Input reader policy interface.
161 *
162 * The input reader policy is used by the input reader to interact with the Window Manager
163 * and other system components.
164 *
165 * The actual implementation is partially supported by callbacks into the DVM
166 * via JNI. This interface is also mocked in the unit tests.
167 */
168class InputReaderPolicyInterface : public virtual RefBase {
169protected:
170 InputReaderPolicyInterface() { }
171 virtual ~InputReaderPolicyInterface() { }
172
173public:
174 /* Display orientations. */
175 enum {
176 ROTATION_0 = 0,
177 ROTATION_90 = 1,
178 ROTATION_180 = 2,
179 ROTATION_270 = 3
180 };
181
Jeff Brown9c3cda02010-06-15 01:31:58 -0700182 /* Gets information about the display with the specified id.
183 * Returns true if the display info is available, false otherwise.
184 */
185 virtual bool getDisplayInfo(int32_t displayId,
186 int32_t* width, int32_t* height, int32_t* orientation) = 0;
187
Jeff Brown214eaf42011-05-26 19:17:02 -0700188 /* Gets the input reader configuration. */
189 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800190
191 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
192 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700193};
194
195
196/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700197class InputReaderInterface : public virtual RefBase {
198protected:
199 InputReaderInterface() { }
200 virtual ~InputReaderInterface() { }
201
202public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700203 /* Dumps the state of the input reader.
204 *
205 * This method may be called on any thread (usually by the input manager). */
206 virtual void dump(String8& dump) = 0;
207
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700208 /* Runs a single iteration of the processing loop.
209 * Nominally reads and processes one incoming message from the EventHub.
210 *
211 * This method should be called on the input reader thread.
212 */
213 virtual void loopOnce() = 0;
214
Jeff Brown9c3cda02010-06-15 01:31:58 -0700215 /* Gets the current input device configuration.
216 *
217 * This method may be called on any thread (usually by the input manager).
218 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700219 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 /* Gets information about the specified input device.
222 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
223 * was no such device.
224 *
225 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700226 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700227 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
228
229 /* Gets the list of all registered device ids. */
230 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
231
232 /* Query current input state. */
233 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
234 int32_t scanCode) = 0;
235 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
236 int32_t keyCode) = 0;
237 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
238 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700239
240 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700241 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
242 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700243
Jeff Brown474dcb52011-06-14 20:22:50 -0700244 /* Requests that a reconfiguration of all input devices.
245 * The changes flag is a bitfield that indicates what has changed and whether
246 * the input devices must all be reopened. */
247 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700248};
249
250
251/* Internal interface used by individual input devices to access global input device state
252 * and parameters maintained by the input reader.
253 */
254class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700255public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700256 InputReaderContext() { }
257 virtual ~InputReaderContext() { }
258
Jeff Brown6d0fec22010-07-23 21:28:06 -0700259 virtual void updateGlobalMetaState() = 0;
260 virtual int32_t getGlobalMetaState() = 0;
261
Jeff Brownfe508922011-01-18 15:10:10 -0800262 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
263 virtual bool shouldDropVirtualKey(nsecs_t now,
264 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
265
Jeff Brown05dc66a2011-03-02 14:41:58 -0800266 virtual void fadePointer() = 0;
267
Jeff Brownaa3855d2011-03-17 01:34:19 -0700268 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
269
Jeff Brown6d0fec22010-07-23 21:28:06 -0700270 virtual InputReaderPolicyInterface* getPolicy() = 0;
271 virtual InputDispatcherInterface* getDispatcher() = 0;
272 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700273};
274
Jeff Brown9c3cda02010-06-15 01:31:58 -0700275
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700276/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700277 * that it sends to the input dispatcher. Some functions of the input reader, such as early
278 * event filtering in low power states, are controlled by a separate policy object.
279 *
280 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700281 * Because the policy and dispatcher can potentially block or cause re-entrance into
282 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700283 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700284 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700285class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700286public:
287 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700288 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700289 const sp<InputDispatcherInterface>& dispatcher);
290 virtual ~InputReader();
291
Jeff Brownb88102f2010-09-08 11:49:43 -0700292 virtual void dump(String8& dump);
293
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700294 virtual void loopOnce();
295
Jeff Brown6d0fec22010-07-23 21:28:06 -0700296 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700297
Jeff Brown6d0fec22010-07-23 21:28:06 -0700298 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
299 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700300
Jeff Brown6d0fec22010-07-23 21:28:06 -0700301 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
302 int32_t scanCode);
303 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
304 int32_t keyCode);
305 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
306 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700307
Jeff Brown6d0fec22010-07-23 21:28:06 -0700308 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
309 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700310
Jeff Brown474dcb52011-06-14 20:22:50 -0700311 virtual void requestRefreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700312
Jeff Brownc3db8582010-10-20 15:33:38 -0700313protected:
314 // These methods are protected virtual so they can be overridden and instrumented
315 // by test cases.
316 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
317
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700318private:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700319 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700321 sp<InputDispatcherInterface> mDispatcher;
322
Jeff Brown214eaf42011-05-26 19:17:02 -0700323 InputReaderConfiguration mConfig;
324
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
326 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
327 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
328
Jeff Brownb7198742011-03-18 18:14:26 -0700329 // The event queue.
330 static const int EVENT_BUFFER_SIZE = 256;
331 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
332
Jeff Brown6d0fec22010-07-23 21:28:06 -0700333 // This reader/writer lock guards the list of input devices.
334 // The writer lock must be held whenever the list of input devices is modified
335 // and then promptly released.
336 // The reader lock must be held whenever the list of input devices is traversed or an
337 // input device in the list is accessed.
338 // This lock only protects the registry and prevents inadvertent deletion of device objects
339 // that are in use. Individual devices are responsible for guarding their own internal state
340 // as needed for concurrent operation.
341 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 KeyedVector<int32_t, InputDevice*> mDevices;
343
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 // low-level input event decoding and device management
Jeff Brownb7198742011-03-18 18:14:26 -0700345 void processEvents(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700346
Jeff Brown7342bb92010-10-01 18:55:43 -0700347 void addDevice(int32_t deviceId);
348 void removeDevice(int32_t deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700349 void processEventsForDevice(int32_t deviceId, const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700350 void timeoutExpired(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700351
Jeff Brownc3db8582010-10-20 15:33:38 -0700352 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353
Jeff Brown6d0fec22010-07-23 21:28:06 -0700354 // state management for all devices
355 Mutex mStateLock;
356
Jeff Brown1a84fd12011-06-02 01:26:32 -0700357 int32_t mGlobalMetaState; // guarded by mStateLock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700358 virtual void updateGlobalMetaState();
359 virtual int32_t getGlobalMetaState();
360
Jeff Brown05dc66a2011-03-02 14:41:58 -0800361 virtual void fadePointer();
362
Jeff Brown1a84fd12011-06-02 01:26:32 -0700363 InputConfiguration mInputConfiguration; // guarded by mStateLock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700364 void updateInputConfiguration();
365
Jeff Brownb7198742011-03-18 18:14:26 -0700366 nsecs_t mDisableVirtualKeysTimeout; // only accessed by reader thread
Jeff Brownfe508922011-01-18 15:10:10 -0800367 virtual void disableVirtualKeysUntil(nsecs_t time);
368 virtual bool shouldDropVirtualKey(nsecs_t now,
369 InputDevice* device, int32_t keyCode, int32_t scanCode);
370
Jeff Brown1a84fd12011-06-02 01:26:32 -0700371 nsecs_t mNextTimeout; // only accessed by reader thread, not guarded
Jeff Brownaa3855d2011-03-17 01:34:19 -0700372 virtual void requestTimeoutAtTime(nsecs_t when);
373
Jeff Brown474dcb52011-06-14 20:22:50 -0700374 uint32_t mConfigurationChangesToRefresh; // guarded by mStateLock
375 void refreshConfiguration(uint32_t changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700376
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 // state queries
378 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
379 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
380 GetStateFunc getStateFunc);
381 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
382 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700383};
384
385
386/* Reads raw events from the event hub and processes them, endlessly. */
387class InputReaderThread : public Thread {
388public:
389 InputReaderThread(const sp<InputReaderInterface>& reader);
390 virtual ~InputReaderThread();
391
392private:
393 sp<InputReaderInterface> mReader;
394
395 virtual bool threadLoop();
396};
397
Jeff Brown6d0fec22010-07-23 21:28:06 -0700398
399/* Represents the state of a single input device. */
400class InputDevice {
401public:
402 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
403 ~InputDevice();
404
405 inline InputReaderContext* getContext() { return mContext; }
406 inline int32_t getId() { return mId; }
407 inline const String8& getName() { return mName; }
408 inline uint32_t getSources() { return mSources; }
409
Jeff Brown56194eb2011-03-02 19:23:13 -0800410 inline bool isExternal() { return mIsExternal; }
411 inline void setExternal(bool external) { mIsExternal = external; }
412
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413 inline bool isIgnored() { return mMappers.isEmpty(); }
414
Jeff Brownef3d7e82010-09-30 14:33:04 -0700415 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416 void addMapper(InputMapper* mapper);
Jeff Brown474dcb52011-06-14 20:22:50 -0700417 void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700418 void reset();
Jeff Brownb7198742011-03-18 18:14:26 -0700419 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700420 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700421
422 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
423 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
424 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
425 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
426 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
427 const int32_t* keyCodes, uint8_t* outFlags);
428
429 int32_t getMetaState();
430
Jeff Brown05dc66a2011-03-02 14:41:58 -0800431 void fadePointer();
432
Jeff Brown49754db2011-07-01 17:37:58 -0700433 inline const PropertyMap& getConfiguration() { return mConfiguration; }
434 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
Jeff Brown8d608662010-08-30 03:02:23 -0700435
Jeff Brown6d0fec22010-07-23 21:28:06 -0700436private:
437 InputReaderContext* mContext;
438 int32_t mId;
439
440 Vector<InputMapper*> mMappers;
441
442 String8 mName;
443 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800444 bool mIsExternal;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700445 bool mDropUntilNextSync;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446
447 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
448 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700449
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800450 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700451};
452
453
Jeff Brown49754db2011-07-01 17:37:58 -0700454/* Keeps track of the state of mouse or touch pad buttons. */
455class CursorButtonAccumulator {
456public:
457 CursorButtonAccumulator();
458
459 void clearButtons();
460 void process(const RawEvent* rawEvent);
461
462 uint32_t getButtonState() const;
463
464private:
465 bool mBtnLeft;
466 bool mBtnRight;
467 bool mBtnMiddle;
468 bool mBtnBack;
469 bool mBtnSide;
470 bool mBtnForward;
471 bool mBtnExtra;
472 bool mBtnTask;
473};
474
475
476/* Keeps track of cursor movements. */
477
478class CursorMotionAccumulator {
479public:
480 CursorMotionAccumulator();
481 void configure(InputDevice* device);
482
483 void clearRelativeAxes();
484 void process(const RawEvent* rawEvent);
485
486 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
487 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
488
489 inline int32_t getRelativeX() const { return mRelX; }
490 inline int32_t getRelativeY() const { return mRelY; }
491 inline int32_t getRelativeVWheel() const { return mRelWheel; }
492 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
493
494private:
495 bool mHaveRelWheel;
496 bool mHaveRelHWheel;
497
498 int32_t mRelX;
499 int32_t mRelY;
500 int32_t mRelWheel;
501 int32_t mRelHWheel;
502};
503
504
505/* Keeps track of the state of touch, stylus and tool buttons. */
506class TouchButtonAccumulator {
507public:
508 TouchButtonAccumulator();
509 void configure(InputDevice* device);
510
511 void clearButtons();
512 void process(const RawEvent* rawEvent);
513
514 uint32_t getButtonState() const;
515 int32_t getToolType() const;
516 bool isActive() const;
517 bool isHovering() const;
518
519private:
520 bool mHaveBtnTouch;
521
522 bool mBtnTouch;
523 bool mBtnStylus;
524 bool mBtnStylus2;
525 bool mBtnToolFinger;
526 bool mBtnToolPen;
527 bool mBtnToolRubber;
528};
529
530
531/* Keeps track of the state of single-touch protocol. */
532class SingleTouchMotionAccumulator {
533public:
534 SingleTouchMotionAccumulator();
535
536 void clearAbsoluteAxes();
537 void process(const RawEvent* rawEvent);
538
539 inline int32_t getAbsoluteX() const { return mAbsX; }
540 inline int32_t getAbsoluteY() const { return mAbsY; }
541 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
542 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
543 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
544
545private:
546 int32_t mAbsX;
547 int32_t mAbsY;
548 int32_t mAbsPressure;
549 int32_t mAbsToolWidth;
550 int32_t mAbsDistance;
551};
552
553
554/* Keeps track of the state of multi-touch protocol. */
555class MultiTouchMotionAccumulator {
556public:
557 class Slot {
558 public:
559 inline bool isInUse() const { return mInUse; }
560 inline int32_t getX() const { return mAbsMTPositionX; }
561 inline int32_t getY() const { return mAbsMTPositionY; }
562 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
563 inline int32_t getTouchMinor() const {
564 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
565 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
566 inline int32_t getToolMinor() const {
567 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
568 inline int32_t getOrientation() const { return mAbsMTOrientation; }
569 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
570 inline int32_t getPressure() const { return mAbsMTPressure; }
571 inline int32_t getDistance() const { return mAbsMTDistance; }
572 inline int32_t getToolType() const;
573
574 private:
575 friend class MultiTouchMotionAccumulator;
576
577 bool mInUse;
578 bool mHaveAbsMTTouchMinor;
579 bool mHaveAbsMTWidthMinor;
580 bool mHaveAbsMTToolType;
581
582 int32_t mAbsMTPositionX;
583 int32_t mAbsMTPositionY;
584 int32_t mAbsMTTouchMajor;
585 int32_t mAbsMTTouchMinor;
586 int32_t mAbsMTWidthMajor;
587 int32_t mAbsMTWidthMinor;
588 int32_t mAbsMTOrientation;
589 int32_t mAbsMTTrackingId;
590 int32_t mAbsMTPressure;
591 int32_t mAbsMTToolType;
592 int32_t mAbsMTDistance;
593
594 Slot();
595 void clearIfInUse();
596 void clear();
597 };
598
599 MultiTouchMotionAccumulator();
600 ~MultiTouchMotionAccumulator();
601
602 void configure(size_t slotCount, bool usingSlotsProtocol);
603 void process(const RawEvent* rawEvent);
604
605 void clearSlots(int32_t initialSlot);
606
607 inline bool isUsingSlotsProtocol() const { return mUsingSlotsProtocol; }
608 inline size_t getSlotCount() const { return mSlotCount; }
609 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
610
611private:
612 int32_t mCurrentSlot;
613 Slot* mSlots;
614 size_t mSlotCount;
615 bool mUsingSlotsProtocol;
616};
617
618
Jeff Brown6d0fec22010-07-23 21:28:06 -0700619/* An input mapper transforms raw input events into cooked event data.
620 * A single input device can have multiple associated input mappers in order to interpret
621 * different classes of events.
622 */
623class InputMapper {
624public:
625 InputMapper(InputDevice* device);
626 virtual ~InputMapper();
627
628 inline InputDevice* getDevice() { return mDevice; }
629 inline int32_t getDeviceId() { return mDevice->getId(); }
630 inline const String8 getDeviceName() { return mDevice->getName(); }
631 inline InputReaderContext* getContext() { return mContext; }
632 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
633 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
634 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
635
636 virtual uint32_t getSources() = 0;
637 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700638 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700639 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640 virtual void reset();
641 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700642 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700643
644 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
645 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
646 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
647 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
648 const int32_t* keyCodes, uint8_t* outFlags);
649
650 virtual int32_t getMetaState();
651
Jeff Brown05dc66a2011-03-02 14:41:58 -0800652 virtual void fadePointer();
653
Jeff Brown6d0fec22010-07-23 21:28:06 -0700654protected:
655 InputDevice* mDevice;
656 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800657
658 static void dumpRawAbsoluteAxisInfo(String8& dump,
659 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700660};
661
662
663class SwitchInputMapper : public InputMapper {
664public:
665 SwitchInputMapper(InputDevice* device);
666 virtual ~SwitchInputMapper();
667
668 virtual uint32_t getSources();
669 virtual void process(const RawEvent* rawEvent);
670
671 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
672
673private:
674 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
675};
676
677
678class KeyboardInputMapper : public InputMapper {
679public:
Jeff Brownefd32662011-03-08 15:13:06 -0800680 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700681 virtual ~KeyboardInputMapper();
682
683 virtual uint32_t getSources();
684 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700685 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700686 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700687 virtual void reset();
688 virtual void process(const RawEvent* rawEvent);
689
690 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
691 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
692 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
693 const int32_t* keyCodes, uint8_t* outFlags);
694
695 virtual int32_t getMetaState();
696
697private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700698 Mutex mLock;
699
Jeff Brown6d0fec22010-07-23 21:28:06 -0700700 struct KeyDown {
701 int32_t keyCode;
702 int32_t scanCode;
703 };
704
Jeff Brownefd32662011-03-08 15:13:06 -0800705 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700706 int32_t mKeyboardType;
707
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800708 // Immutable configuration parameters.
709 struct Parameters {
710 int32_t associatedDisplayId;
711 bool orientationAware;
712 } mParameters;
713
Jeff Brown6328cdc2010-07-29 18:18:33 -0700714 struct LockedState {
715 Vector<KeyDown> keyDowns; // keys that are down
716 int32_t metaState;
717 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700718
719 struct LedState {
720 bool avail; // led is available
721 bool on; // we think the led is currently on
722 };
723 LedState capsLockLedState;
724 LedState numLockLedState;
725 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700726 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700727
Jeff Brown6328cdc2010-07-29 18:18:33 -0700728 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700729
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800730 void configureParameters();
731 void dumpParameters(String8& dump);
732
Jeff Brown6d0fec22010-07-23 21:28:06 -0700733 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700734
Jeff Brown6d0fec22010-07-23 21:28:06 -0700735 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
736 uint32_t policyFlags);
737
Jeff Brown6328cdc2010-07-29 18:18:33 -0700738 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700739
Jeff Brown49ed71d2010-12-06 17:13:33 -0800740 void resetLedStateLocked();
741 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700742 void updateLedStateLocked(bool reset);
743 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
744 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700745};
746
747
Jeff Brown83c09682010-12-23 17:50:18 -0800748class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700749public:
Jeff Brown83c09682010-12-23 17:50:18 -0800750 CursorInputMapper(InputDevice* device);
751 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700752
753 virtual uint32_t getSources();
754 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700755 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700756 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700757 virtual void reset();
758 virtual void process(const RawEvent* rawEvent);
759
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700760 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
761
Jeff Brown05dc66a2011-03-02 14:41:58 -0800762 virtual void fadePointer();
763
Jeff Brown6d0fec22010-07-23 21:28:06 -0700764private:
765 // Amount that trackball needs to move in order to generate a key event.
766 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
767
Jeff Brown6328cdc2010-07-29 18:18:33 -0700768 Mutex mLock;
769
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800770 // Immutable configuration parameters.
771 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800772 enum Mode {
773 MODE_POINTER,
774 MODE_NAVIGATION,
775 };
776
777 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800778 int32_t associatedDisplayId;
779 bool orientationAware;
780 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700781
Jeff Brown49754db2011-07-01 17:37:58 -0700782 CursorButtonAccumulator mCursorButtonAccumulator;
783 CursorMotionAccumulator mCursorMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700784
Jeff Brownefd32662011-03-08 15:13:06 -0800785 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700786 float mXScale;
787 float mYScale;
788 float mXPrecision;
789 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800790
Jeff Brown6f2fba42011-02-19 01:08:02 -0800791 float mVWheelScale;
792 float mHWheelScale;
793
Jeff Brown19c97d462011-06-01 12:33:19 -0700794 // Velocity controls for mouse pointer and wheel movements.
795 // The controls for X and Y wheel movements are separate to keep them decoupled.
796 VelocityControl mPointerVelocityControl;
797 VelocityControl mWheelXVelocityControl;
798 VelocityControl mWheelYVelocityControl;
799
Jeff Brown83c09682010-12-23 17:50:18 -0800800 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700801
Jeff Brown6328cdc2010-07-29 18:18:33 -0700802 struct LockedState {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700803 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700804 nsecs_t downTime;
805 } mLocked;
806
807 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700808
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800809 void configureParameters();
810 void dumpParameters(String8& dump);
811
Jeff Brown6d0fec22010-07-23 21:28:06 -0700812 void sync(nsecs_t when);
813};
814
815
816class TouchInputMapper : public InputMapper {
817public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800818 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700819 virtual ~TouchInputMapper();
820
821 virtual uint32_t getSources();
822 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700823 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -0700824 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700825 virtual void reset();
826
827 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
828 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
829 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
830 const int32_t* keyCodes, uint8_t* outFlags);
831
Jeff Brownace13b12011-03-09 17:39:48 -0800832 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -0700833 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -0800834
Jeff Brown6d0fec22010-07-23 21:28:06 -0700835protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700836 Mutex mLock;
837
Jeff Brown6d0fec22010-07-23 21:28:06 -0700838 struct VirtualKey {
839 int32_t keyCode;
840 int32_t scanCode;
841 uint32_t flags;
842
843 // computed hit box, specified in touch screen coords based on known display size
844 int32_t hitLeft;
845 int32_t hitTop;
846 int32_t hitRight;
847 int32_t hitBottom;
848
849 inline bool isHit(int32_t x, int32_t y) const {
850 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
851 }
852 };
853
Jeff Brown8d608662010-08-30 03:02:23 -0700854 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700855 struct PointerData {
856 uint32_t id;
857 int32_t x;
858 int32_t y;
859 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700860 int32_t touchMajor;
861 int32_t touchMinor;
862 int32_t toolMajor;
863 int32_t toolMinor;
864 int32_t orientation;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700865 int32_t distance;
Jeff Brown49754db2011-07-01 17:37:58 -0700866 int32_t toolType; // AMOTION_EVENT_TOOL_TYPE constant
867 bool isHovering;
Jeff Brownc3db8582010-10-20 15:33:38 -0700868
869 inline bool operator== (const PointerData& other) const {
870 return id == other.id
871 && x == other.x
872 && y == other.y
873 && pressure == other.pressure
874 && touchMajor == other.touchMajor
875 && touchMinor == other.touchMinor
876 && toolMajor == other.toolMajor
877 && toolMinor == other.toolMinor
Jeff Brown80fd47c2011-05-24 01:07:44 -0700878 && orientation == other.orientation
Jeff Brown49754db2011-07-01 17:37:58 -0700879 && distance == other.distance
880 && toolType == other.toolType
881 && isHovering == other.isHovering;
Jeff Brownc3db8582010-10-20 15:33:38 -0700882 }
883 inline bool operator!= (const PointerData& other) const {
884 return !(*this == other);
885 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886 };
887
Jeff Brown8d608662010-08-30 03:02:23 -0700888 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700889 struct TouchData {
890 uint32_t pointerCount;
891 PointerData pointers[MAX_POINTERS];
892 BitSet32 idBits;
893 uint32_t idToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700894 int32_t buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700895
896 void copyFrom(const TouchData& other) {
897 pointerCount = other.pointerCount;
898 idBits = other.idBits;
Jeff Brownace13b12011-03-09 17:39:48 -0800899 buttonState = other.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700900
901 for (uint32_t i = 0; i < pointerCount; i++) {
902 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700903
904 int id = pointers[i].id;
905 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906 }
907 }
908
909 inline void clear() {
910 pointerCount = 0;
911 idBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -0800912 buttonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700913 }
Jeff Brown2352b972011-04-12 22:39:53 -0700914
915 void getCentroid(float* outX, float* outY) {
916 float x = 0, y = 0;
917 if (pointerCount != 0) {
918 for (uint32_t i = 0; i < pointerCount; i++) {
919 x += pointers[i].x;
920 y += pointers[i].y;
921 }
922 x /= pointerCount;
923 y /= pointerCount;
924 }
925 *outX = x;
926 *outY = y;
927 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700928 };
929
Jeff Brown83c09682010-12-23 17:50:18 -0800930 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800931 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brownace13b12011-03-09 17:39:48 -0800932 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800933
Jeff Brown214eaf42011-05-26 19:17:02 -0700934 // The reader's configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -0700935 InputReaderConfiguration mConfig;
Jeff Brown214eaf42011-05-26 19:17:02 -0700936
Jeff Brown6d0fec22010-07-23 21:28:06 -0700937 // Immutable configuration parameters.
938 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800939 enum DeviceType {
940 DEVICE_TYPE_TOUCH_SCREEN,
941 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -0800942 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800943 };
944
945 DeviceType deviceType;
946 int32_t associatedDisplayId;
947 bool orientationAware;
948
Jeff Brown2352b972011-04-12 22:39:53 -0700949 enum GestureMode {
950 GESTURE_MODE_POINTER,
951 GESTURE_MODE_SPOTS,
952 };
953 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700954 } mParameters;
955
Jeff Brown8d608662010-08-30 03:02:23 -0700956 // Immutable calibration parameters in parsed form.
957 struct Calibration {
Jeff Brownc6d282b2010-10-14 21:42:15 -0700958 // Touch Size
959 enum TouchSizeCalibration {
960 TOUCH_SIZE_CALIBRATION_DEFAULT,
961 TOUCH_SIZE_CALIBRATION_NONE,
962 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
963 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700964 };
965
Jeff Brownc6d282b2010-10-14 21:42:15 -0700966 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700967
Jeff Brownc6d282b2010-10-14 21:42:15 -0700968 // Tool Size
969 enum ToolSizeCalibration {
970 TOOL_SIZE_CALIBRATION_DEFAULT,
971 TOOL_SIZE_CALIBRATION_NONE,
972 TOOL_SIZE_CALIBRATION_GEOMETRIC,
973 TOOL_SIZE_CALIBRATION_LINEAR,
974 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700975 };
976
Jeff Brownc6d282b2010-10-14 21:42:15 -0700977 ToolSizeCalibration toolSizeCalibration;
978 bool haveToolSizeLinearScale;
979 float toolSizeLinearScale;
980 bool haveToolSizeLinearBias;
981 float toolSizeLinearBias;
982 bool haveToolSizeAreaScale;
983 float toolSizeAreaScale;
984 bool haveToolSizeAreaBias;
985 float toolSizeAreaBias;
986 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800987 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700988
989 // Pressure
990 enum PressureCalibration {
991 PRESSURE_CALIBRATION_DEFAULT,
992 PRESSURE_CALIBRATION_NONE,
993 PRESSURE_CALIBRATION_PHYSICAL,
994 PRESSURE_CALIBRATION_AMPLITUDE,
995 };
996 enum PressureSource {
997 PRESSURE_SOURCE_DEFAULT,
998 PRESSURE_SOURCE_PRESSURE,
999 PRESSURE_SOURCE_TOUCH,
1000 };
1001
1002 PressureCalibration pressureCalibration;
1003 PressureSource pressureSource;
1004 bool havePressureScale;
1005 float pressureScale;
1006
1007 // Size
1008 enum SizeCalibration {
1009 SIZE_CALIBRATION_DEFAULT,
1010 SIZE_CALIBRATION_NONE,
1011 SIZE_CALIBRATION_NORMALIZED,
1012 };
1013
1014 SizeCalibration sizeCalibration;
1015
1016 // Orientation
1017 enum OrientationCalibration {
1018 ORIENTATION_CALIBRATION_DEFAULT,
1019 ORIENTATION_CALIBRATION_NONE,
1020 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -08001021 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001022 };
1023
1024 OrientationCalibration orientationCalibration;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001025
1026 // Distance
1027 enum DistanceCalibration {
1028 DISTANCE_CALIBRATION_DEFAULT,
1029 DISTANCE_CALIBRATION_NONE,
1030 DISTANCE_CALIBRATION_SCALED,
1031 };
1032
1033 DistanceCalibration distanceCalibration;
1034 bool haveDistanceScale;
1035 float distanceScale;
Jeff Brown8d608662010-08-30 03:02:23 -07001036 } mCalibration;
1037
1038 // Raw axis information from the driver.
1039 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001040 RawAbsoluteAxisInfo x;
1041 RawAbsoluteAxisInfo y;
1042 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001043 RawAbsoluteAxisInfo touchMajor;
1044 RawAbsoluteAxisInfo touchMinor;
1045 RawAbsoluteAxisInfo toolMajor;
1046 RawAbsoluteAxisInfo toolMinor;
1047 RawAbsoluteAxisInfo orientation;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001048 RawAbsoluteAxisInfo distance;
1049 RawAbsoluteAxisInfo trackingId;
1050 RawAbsoluteAxisInfo slot;
Jeff Brown8d608662010-08-30 03:02:23 -07001051 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001052
Jeff Brown6328cdc2010-07-29 18:18:33 -07001053 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001054 TouchData mCurrentTouch;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001055 PointerProperties mCurrentTouchProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001056 PointerCoords mCurrentTouchCoords[MAX_POINTERS];
1057
Jeff Brown6d0fec22010-07-23 21:28:06 -07001058 TouchData mLastTouch;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001059 PointerProperties mLastTouchProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001060 PointerCoords mLastTouchCoords[MAX_POINTERS];
Jeff Brown6d0fec22010-07-23 21:28:06 -07001061
1062 // The time the primary pointer last went down.
1063 nsecs_t mDownTime;
1064
Jeff Brownace13b12011-03-09 17:39:48 -08001065 // The pointer controller, or null if the device is not a pointer.
1066 sp<PointerControllerInterface> mPointerController;
1067
Jeff Brown6328cdc2010-07-29 18:18:33 -07001068 struct LockedState {
1069 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001070
Jeff Brown6328cdc2010-07-29 18:18:33 -07001071 // The surface orientation and width and height set by configureSurfaceLocked().
1072 int32_t surfaceOrientation;
1073 int32_t surfaceWidth, surfaceHeight;
1074
Jeff Brownefd32662011-03-08 15:13:06 -08001075 // The associated display orientation and width and height set by configureSurfaceLocked().
1076 int32_t associatedDisplayOrientation;
1077 int32_t associatedDisplayWidth, associatedDisplayHeight;
1078
Jeff Brown6328cdc2010-07-29 18:18:33 -07001079 // Translation and scaling factors, orientation-independent.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001080 float xScale;
1081 float xPrecision;
1082
Jeff Brown6328cdc2010-07-29 18:18:33 -07001083 float yScale;
1084 float yPrecision;
1085
Jeff Brown8d608662010-08-30 03:02:23 -07001086 float geometricScale;
1087
Jeff Brownc6d282b2010-10-14 21:42:15 -07001088 float toolSizeLinearScale;
1089 float toolSizeLinearBias;
1090 float toolSizeAreaScale;
1091 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07001092
Jeff Brown6328cdc2010-07-29 18:18:33 -07001093 float pressureScale;
1094
Jeff Brown6328cdc2010-07-29 18:18:33 -07001095 float sizeScale;
1096
1097 float orientationScale;
1098
Jeff Brown80fd47c2011-05-24 01:07:44 -07001099 float distanceScale;
1100
Jeff Brown6328cdc2010-07-29 18:18:33 -07001101 // Oriented motion ranges for input device info.
1102 struct OrientedRanges {
1103 InputDeviceInfo::MotionRange x;
1104 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -07001105
1106 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001107 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -07001108
1109 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001110 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -07001111
Jeff Brownc6d282b2010-10-14 21:42:15 -07001112 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001113 InputDeviceInfo::MotionRange touchMajor;
1114 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -07001115
Jeff Brownc6d282b2010-10-14 21:42:15 -07001116 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001117 InputDeviceInfo::MotionRange toolMajor;
1118 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -07001119
1120 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001121 InputDeviceInfo::MotionRange orientation;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001122
1123 bool haveDistance;
1124 InputDeviceInfo::MotionRange distance;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001125 } orientedRanges;
1126
1127 // Oriented dimensions and precision.
1128 float orientedSurfaceWidth, orientedSurfaceHeight;
1129 float orientedXPrecision, orientedYPrecision;
1130
1131 struct CurrentVirtualKeyState {
1132 bool down;
1133 nsecs_t downTime;
1134 int32_t keyCode;
1135 int32_t scanCode;
1136 } currentVirtualKey;
Jeff Brownace13b12011-03-09 17:39:48 -08001137
1138 // Scale factor for gesture based pointer movements.
1139 float pointerGestureXMovementScale;
1140 float pointerGestureYMovementScale;
1141
1142 // Scale factor for gesture based zooming and other freeform motions.
1143 float pointerGestureXZoomScale;
1144 float pointerGestureYZoomScale;
1145
Jeff Brown2352b972011-04-12 22:39:53 -07001146 // The maximum swipe width.
1147 float pointerGestureMaxSwipeWidth;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001148 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001149
Jeff Brown8d608662010-08-30 03:02:23 -07001150 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001151 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001152 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001153 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001154 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001155 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001156 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001157 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -07001158 virtual void parseCalibration();
1159 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -07001160 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001161
1162 enum TouchResult {
1163 // Dispatch the touch normally.
1164 DISPATCH_TOUCH,
1165 // Do not dispatch the touch, but keep tracking the current stroke.
1166 SKIP_TOUCH,
1167 // Do not dispatch the touch, and drop all information associated with the current stoke
1168 // so the next movement will appear as a new down.
1169 DROP_STROKE
1170 };
1171
1172 void syncTouch(nsecs_t when, bool havePointerIds);
1173
1174private:
Jeff Brown6d0fec22010-07-23 21:28:06 -07001175 struct PointerDistanceHeapElement {
1176 uint32_t currentPointerIndex : 8;
1177 uint32_t lastPointerIndex : 8;
1178 uint64_t distance : 48; // squared distance
1179 };
1180
Jeff Brownace13b12011-03-09 17:39:48 -08001181 struct PointerGesture {
1182 enum Mode {
1183 // No fingers, button is not pressed.
1184 // Nothing happening.
1185 NEUTRAL,
1186
1187 // No fingers, button is not pressed.
1188 // Tap detected.
1189 // Emits DOWN and UP events at the pointer location.
1190 TAP,
1191
Jeff Brown79ac9692011-04-19 21:20:10 -07001192 // Exactly one finger dragging following a tap.
1193 // Pointer follows the active finger.
1194 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001195 //
1196 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown79ac9692011-04-19 21:20:10 -07001197 TAP_DRAG,
1198
Jeff Brownace13b12011-03-09 17:39:48 -08001199 // Button is pressed.
1200 // Pointer follows the active finger if there is one. Other fingers are ignored.
1201 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -07001202 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -08001203
1204 // Exactly one finger, button is not pressed.
1205 // Pointer follows the active finger.
1206 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001207 //
1208 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brownace13b12011-03-09 17:39:48 -08001209 HOVER,
1210
Jeff Brown2352b972011-04-12 22:39:53 -07001211 // Exactly two fingers but neither have moved enough to clearly indicate
1212 // whether a swipe or freeform gesture was intended. We consider the
1213 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1214 // Pointer does not move.
1215 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1216 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -08001217
1218 // Exactly two fingers moving in the same direction, button is not pressed.
1219 // Pointer does not move.
1220 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1221 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -08001222 SWIPE,
1223
1224 // Two or more fingers moving in arbitrary directions, button is not pressed.
1225 // Pointer does not move.
1226 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1227 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -08001228 FREEFORM,
1229
1230 // Waiting for quiet time to end before starting the next gesture.
1231 QUIET,
1232 };
1233
Jeff Brown2352b972011-04-12 22:39:53 -07001234 // Time the first finger went down.
1235 nsecs_t firstTouchTime;
1236
Jeff Brownace13b12011-03-09 17:39:48 -08001237 // The active pointer id from the raw touch data.
1238 int32_t activeTouchId; // -1 if none
1239
1240 // The active pointer id from the gesture last delivered to the application.
1241 int32_t activeGestureId; // -1 if none
1242
1243 // Pointer coords and ids for the current and previous pointer gesture.
1244 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001245 BitSet32 currentGestureIdBits;
1246 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001247 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001248 PointerCoords currentGestureCoords[MAX_POINTERS];
1249
1250 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -08001251 BitSet32 lastGestureIdBits;
1252 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001253 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001254 PointerCoords lastGestureCoords[MAX_POINTERS];
1255
Jeff Brownace13b12011-03-09 17:39:48 -08001256 // Time the pointer gesture last went down.
1257 nsecs_t downTime;
1258
Jeff Brown79ac9692011-04-19 21:20:10 -07001259 // Time when the pointer went down for a TAP.
1260 nsecs_t tapDownTime;
1261
1262 // Time when the pointer went up for a TAP.
1263 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001264
Jeff Brown2352b972011-04-12 22:39:53 -07001265 // Location of initial tap.
1266 float tapX, tapY;
1267
Jeff Brownace13b12011-03-09 17:39:48 -08001268 // Time we started waiting for quiescence.
1269 nsecs_t quietTime;
1270
Jeff Brown2352b972011-04-12 22:39:53 -07001271 // Reference points for multitouch gestures.
1272 float referenceTouchX; // reference touch X/Y coordinates in surface units
1273 float referenceTouchY;
1274 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1275 float referenceGestureY;
1276
Jeff Brown538881e2011-05-25 18:23:38 -07001277 // Distance that each pointer has traveled which has not yet been
1278 // subsumed into the reference gesture position.
1279 BitSet32 referenceIdBits;
1280 struct Delta {
1281 float dx, dy;
1282 };
1283 Delta referenceDeltas[MAX_POINTER_ID + 1];
1284
Jeff Brown2352b972011-04-12 22:39:53 -07001285 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1286 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1287
Jeff Brownace13b12011-03-09 17:39:48 -08001288 // A velocity tracker for determining whether to switch active pointers during drags.
1289 VelocityTracker velocityTracker;
1290
Jeff Brown19c97d462011-06-01 12:33:19 -07001291 // Velocity control for pointer movements.
1292 VelocityControl pointerVelocityControl;
1293
Jeff Brownace13b12011-03-09 17:39:48 -08001294 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001295 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001296 activeTouchId = -1;
1297 activeGestureId = -1;
1298 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001299 currentGestureIdBits.clear();
1300 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001301 lastGestureIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001302 downTime = 0;
1303 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001304 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001305 resetQuietTime();
Jeff Brown19c97d462011-06-01 12:33:19 -07001306 pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08001307 }
1308
Jeff Brown79ac9692011-04-19 21:20:10 -07001309 void resetTap() {
1310 tapDownTime = LLONG_MIN;
1311 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001312 }
1313
1314 void resetQuietTime() {
1315 quietTime = LLONG_MIN;
1316 }
1317 } mPointerGesture;
1318
Jeff Brown6328cdc2010-07-29 18:18:33 -07001319 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001320
1321 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
1322 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Browna6111372011-07-14 21:48:23 -07001323 void prepareTouches(float* outXPrecision, float* outYPrecision);
Jeff Brown79ac9692011-04-19 21:20:10 -07001324 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1325 bool preparePointerGestures(nsecs_t when,
1326 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
Jeff Brownace13b12011-03-09 17:39:48 -08001327
1328 // Dispatches a motion event.
1329 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1330 // method will take care of setting the index and transmuting the action to DOWN or UP
1331 // it is the first / last pointer to go down / up.
1332 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001333 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1334 int32_t edgeFlags,
1335 const PointerProperties* properties, const PointerCoords* coords,
1336 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001337 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1338
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001339 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001340 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001341 bool updateMovedPointers(const PointerProperties* inProperties,
1342 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1343 PointerProperties* outProperties, PointerCoords* outCoords,
1344 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001345
Jeff Brownefd32662011-03-08 15:13:06 -08001346 void suppressSwipeOntoVirtualKeys(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001347
Jeff Brown6328cdc2010-07-29 18:18:33 -07001348 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
1349 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001350
Jeff Brown6d0fec22010-07-23 21:28:06 -07001351 void calculatePointerIds();
1352};
1353
1354
1355class SingleTouchInputMapper : public TouchInputMapper {
1356public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001357 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001358 virtual ~SingleTouchInputMapper();
1359
1360 virtual void reset();
1361 virtual void process(const RawEvent* rawEvent);
1362
1363protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001364 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001365
1366private:
Jeff Brown49754db2011-07-01 17:37:58 -07001367 CursorButtonAccumulator mCursorButtonAccumulator;
1368 TouchButtonAccumulator mTouchButtonAccumulator;
1369 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001370
Jeff Brown80fd47c2011-05-24 01:07:44 -07001371 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001372
1373 void sync(nsecs_t when);
1374};
1375
1376
1377class MultiTouchInputMapper : public TouchInputMapper {
1378public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001379 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001380 virtual ~MultiTouchInputMapper();
1381
1382 virtual void reset();
1383 virtual void process(const RawEvent* rawEvent);
1384
1385protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001386 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001387
1388private:
Jeff Brown49754db2011-07-01 17:37:58 -07001389 CursorButtonAccumulator mCursorButtonAccumulator;
1390 TouchButtonAccumulator mTouchButtonAccumulator;
1391 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
Jeff Brownace13b12011-03-09 17:39:48 -08001392
Jeff Brown6894a292011-07-01 17:59:27 -07001393 // Specifies the pointer id bits that are in use, and their associated tracking id.
1394 BitSet32 mPointerIdBits;
1395 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1396
Jeff Brown80fd47c2011-05-24 01:07:44 -07001397 void clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001398
1399 void sync(nsecs_t when);
1400};
1401
Jeff Browncb1404e2011-01-15 18:14:15 -08001402
1403class JoystickInputMapper : public InputMapper {
1404public:
1405 JoystickInputMapper(InputDevice* device);
1406 virtual ~JoystickInputMapper();
1407
1408 virtual uint32_t getSources();
1409 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1410 virtual void dump(String8& dump);
Jeff Brown474dcb52011-06-14 20:22:50 -07001411 virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08001412 virtual void reset();
1413 virtual void process(const RawEvent* rawEvent);
1414
1415private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001416 struct Axis {
1417 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001418 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001419
Jeff Brown6f2fba42011-02-19 01:08:02 -08001420 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001421
Jeff Brown6f2fba42011-02-19 01:08:02 -08001422 float scale; // scale factor from raw to normalized values
1423 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001424 float highScale; // scale factor from raw to normalized values of high split
1425 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001426
Jeff Brown6f2fba42011-02-19 01:08:02 -08001427 float min; // normalized inclusive minimum
1428 float max; // normalized inclusive maximum
1429 float flat; // normalized flat region size
1430 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001431
Jeff Brown6f2fba42011-02-19 01:08:02 -08001432 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001433 float currentValue; // current value
1434 float newValue; // most recent value
1435 float highCurrentValue; // current value of high split
1436 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001437
Jeff Brown85297452011-03-04 13:07:49 -08001438 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1439 bool explicitlyMapped, float scale, float offset,
1440 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001441 float min, float max, float flat, float fuzz) {
1442 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001443 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001444 this->explicitlyMapped = explicitlyMapped;
1445 this->scale = scale;
1446 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001447 this->highScale = highScale;
1448 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001449 this->min = min;
1450 this->max = max;
1451 this->flat = flat;
1452 this->fuzz = fuzz;
1453 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001454 resetValue();
1455 }
1456
1457 void resetValue() {
1458 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001459 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001460 this->highCurrentValue = 0;
1461 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001462 }
1463 };
1464
Jeff Brown6f2fba42011-02-19 01:08:02 -08001465 // Axes indexed by raw ABS_* axis index.
1466 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001467
Jeff Brown6f2fba42011-02-19 01:08:02 -08001468 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001469
Jeff Brown85297452011-03-04 13:07:49 -08001470 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001471 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001472 bool filterAxes(bool force);
1473
1474 static bool hasValueChangedSignificantly(float filter,
1475 float newValue, float currentValue, float min, float max);
1476 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1477 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001478
Jeff Brown6f2fba42011-02-19 01:08:02 -08001479 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001480};
1481
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001482} // namespace android
1483
1484#endif // _UI_INPUT_READER_H