blob: 5028b6017e73442cf4914c714608b75120765aa3 [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 {
48 // Determines whether to turn on some hacks we have to improve the touch interaction with a
49 // certain device whose screen currently is not all that good.
50 bool filterTouchEvents;
51
52 // Determines whether to turn on some hacks to improve touch interaction with another device
53 // where touch coordinate data can get corrupted.
54 bool filterJumpyTouchEvents;
55
56 // Gets the amount of time to disable virtual keys after the screen is touched
57 // in order to filter out accidental virtual key presses due to swiping gestures
58 // or taps near the edge of the display. May be 0 to disable the feature.
59 nsecs_t virtualKeyQuietTime;
60
61 // The excluded device names for the platform.
62 // Devices with these names will be ignored.
63 Vector<String8> excludedDeviceNames;
64
Jeff Brown19c97d462011-06-01 12:33:19 -070065 // Velocity control parameters for mouse pointer movements.
66 VelocityControlParameters pointerVelocityControlParameters;
67
68 // Velocity control parameters for mouse wheel movements.
69 VelocityControlParameters wheelVelocityControlParameters;
70
Jeff Brown214eaf42011-05-26 19:17:02 -070071 // Quiet time between certain pointer gesture transitions.
72 // Time to allow for all fingers or buttons to settle into a stable state before
73 // starting a new gesture.
74 nsecs_t pointerGestureQuietInterval;
75
76 // The minimum speed that a pointer must travel for us to consider switching the active
77 // touch pointer to it during a drag. This threshold is set to avoid switching due
78 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
79 float pointerGestureDragMinSwitchSpeed; // in pixels per second
80
81 // Tap gesture delay time.
82 // The time between down and up must be less than this to be considered a tap.
83 nsecs_t pointerGestureTapInterval;
84
85 // Tap drag gesture delay time.
86 // The time between the previous tap's up and the next down must be less than
87 // this to be considered a drag. Otherwise, the previous tap is finished and a
88 // new tap begins.
89 //
90 // Note that the previous tap will be held down for this entire duration so this
91 // interval must be shorter than the long press timeout.
92 nsecs_t pointerGestureTapDragInterval;
93
94 // The distance in pixels that the pointer is allowed to move from initial down
95 // to up and still be called a tap.
96 float pointerGestureTapSlop; // in pixels
97
98 // Time after the first touch points go down to settle on an initial centroid.
99 // This is intended to be enough time to handle cases where the user puts down two
100 // fingers at almost but not quite exactly the same time.
101 nsecs_t pointerGestureMultitouchSettleInterval;
102
103 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
104 // both of the pointers are moving at least this fast.
105 float pointerGestureMultitouchMinSpeed; // in pixels per second
106
107 // The transition from PRESS to SWIPE gesture mode can only occur when the
108 // cosine of the angle between the two vectors is greater than or equal to than this value
109 // which indicates that the vectors are oriented in the same direction.
110 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
111 // (In exactly opposite directions, the cosine is -1.0.)
112 float pointerGestureSwipeTransitionAngleCosine;
113
114 // The transition from PRESS to SWIPE gesture mode can only occur when the
115 // fingers are no more than this far apart relative to the diagonal size of
116 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
117 // no more than half the diagonal size of the touch pad apart.
118 float pointerGestureSwipeMaxWidthRatio;
119
120 // The gesture movement speed factor relative to the size of the display.
121 // Movement speed applies when the fingers are moving in the same direction.
122 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
123 // will cover this portion of the display diagonal.
124 float pointerGestureMovementSpeedRatio;
125
126 // The gesture zoom speed factor relative to the size of the display.
127 // Zoom speed applies when the fingers are mostly moving relative to each other
128 // to execute a scale gesture or similar.
129 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
130 // will cover this portion of the display diagonal.
131 float pointerGestureZoomSpeedRatio;
132
133 InputReaderConfiguration() :
134 filterTouchEvents(false),
135 filterJumpyTouchEvents(false),
136 virtualKeyQuietTime(0),
Jeff Brown19c97d462011-06-01 12:33:19 -0700137 pointerVelocityControlParameters(1.0f, 80.0f, 400.0f, 4.0f),
138 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
Jeff Brown214eaf42011-05-26 19:17:02 -0700139 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
140 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
141 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
142 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
143 pointerGestureTapSlop(10.0f), // 10 pixels
144 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
145 pointerGestureMultitouchMinSpeed(150.0f), // 150 pixels per second
146 pointerGestureSwipeTransitionAngleCosine(0.5f), // cosine of 45degrees
147 pointerGestureSwipeMaxWidthRatio(0.333f),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700148 pointerGestureMovementSpeedRatio(0.3f),
Jeff Brown214eaf42011-05-26 19:17:02 -0700149 pointerGestureZoomSpeedRatio(0.3f) { }
150};
151
152
153/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700154 * Input reader policy interface.
155 *
156 * The input reader policy is used by the input reader to interact with the Window Manager
157 * and other system components.
158 *
159 * The actual implementation is partially supported by callbacks into the DVM
160 * via JNI. This interface is also mocked in the unit tests.
161 */
162class InputReaderPolicyInterface : public virtual RefBase {
163protected:
164 InputReaderPolicyInterface() { }
165 virtual ~InputReaderPolicyInterface() { }
166
167public:
168 /* Display orientations. */
169 enum {
170 ROTATION_0 = 0,
171 ROTATION_90 = 1,
172 ROTATION_180 = 2,
173 ROTATION_270 = 3
174 };
175
Jeff Brown9c3cda02010-06-15 01:31:58 -0700176 /* Gets information about the display with the specified id.
177 * Returns true if the display info is available, false otherwise.
178 */
179 virtual bool getDisplayInfo(int32_t displayId,
180 int32_t* width, int32_t* height, int32_t* orientation) = 0;
181
Jeff Brown214eaf42011-05-26 19:17:02 -0700182 /* Gets the input reader configuration. */
183 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -0800184
185 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
186 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700187};
188
189
190/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700191class InputReaderInterface : public virtual RefBase {
192protected:
193 InputReaderInterface() { }
194 virtual ~InputReaderInterface() { }
195
196public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700197 /* Dumps the state of the input reader.
198 *
199 * This method may be called on any thread (usually by the input manager). */
200 virtual void dump(String8& dump) = 0;
201
Jeff Brown46b9ac02010-04-22 18:58:52 -0700202 /* Runs a single iteration of the processing loop.
203 * Nominally reads and processes one incoming message from the EventHub.
204 *
205 * This method should be called on the input reader thread.
206 */
207 virtual void loopOnce() = 0;
208
Jeff Brown9c3cda02010-06-15 01:31:58 -0700209 /* Gets the current input device configuration.
210 *
211 * This method may be called on any thread (usually by the input manager).
212 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700213 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700214
Jeff Brown6d0fec22010-07-23 21:28:06 -0700215 /* Gets information about the specified input device.
216 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
217 * was no such device.
218 *
219 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
222
223 /* Gets the list of all registered device ids. */
224 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
225
226 /* Query current input state. */
227 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
228 int32_t scanCode) = 0;
229 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
230 int32_t keyCode) = 0;
231 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
232 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233
234 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700235 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
236 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700237
238 /* Reopens and reconfigures all input devices. */
239 virtual void refreshConfiguration() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700240};
241
242
243/* Internal interface used by individual input devices to access global input device state
244 * and parameters maintained by the input reader.
245 */
246class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700247public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700248 InputReaderContext() { }
249 virtual ~InputReaderContext() { }
250
Jeff Brown6d0fec22010-07-23 21:28:06 -0700251 virtual void updateGlobalMetaState() = 0;
252 virtual int32_t getGlobalMetaState() = 0;
253
Jeff Brownfe508922011-01-18 15:10:10 -0800254 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
255 virtual bool shouldDropVirtualKey(nsecs_t now,
256 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
257
Jeff Brown05dc66a2011-03-02 14:41:58 -0800258 virtual void fadePointer() = 0;
259
Jeff Brown68d60752011-03-17 01:34:19 -0700260 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
261
Jeff Brown6d0fec22010-07-23 21:28:06 -0700262 virtual InputReaderPolicyInterface* getPolicy() = 0;
Jeff Brown214eaf42011-05-26 19:17:02 -0700263 virtual const InputReaderConfiguration* getConfig() = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 virtual InputDispatcherInterface* getDispatcher() = 0;
265 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700266};
267
Jeff Brown9c3cda02010-06-15 01:31:58 -0700268
Jeff Brown46b9ac02010-04-22 18:58:52 -0700269/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270 * that it sends to the input dispatcher. Some functions of the input reader, such as early
271 * event filtering in low power states, are controlled by a separate policy object.
272 *
273 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700274 * Because the policy and dispatcher can potentially block or cause re-entrance into
275 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700276 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700277 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700278class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700279public:
280 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700281 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700282 const sp<InputDispatcherInterface>& dispatcher);
283 virtual ~InputReader();
284
Jeff Brownb88102f2010-09-08 11:49:43 -0700285 virtual void dump(String8& dump);
286
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287 virtual void loopOnce();
288
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700290
Jeff Brown6d0fec22010-07-23 21:28:06 -0700291 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
292 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700293
Jeff Brown6d0fec22010-07-23 21:28:06 -0700294 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
295 int32_t scanCode);
296 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
297 int32_t keyCode);
298 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
299 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700300
Jeff Brown6d0fec22010-07-23 21:28:06 -0700301 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
302 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700303
Jeff Brown1a84fd12011-06-02 01:26:32 -0700304 virtual void refreshConfiguration();
305
Jeff Brownc3db8582010-10-20 15:33:38 -0700306protected:
307 // These methods are protected virtual so they can be overridden and instrumented
308 // by test cases.
309 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
310
Jeff Brown46b9ac02010-04-22 18:58:52 -0700311private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700312 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700313 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700314 sp<InputDispatcherInterface> mDispatcher;
315
Jeff Brown214eaf42011-05-26 19:17:02 -0700316 InputReaderConfiguration mConfig;
317
Jeff Brown6d0fec22010-07-23 21:28:06 -0700318 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
Jeff Brown214eaf42011-05-26 19:17:02 -0700319 virtual const InputReaderConfiguration* getConfig() { return &mConfig; }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700320 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
321 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
322
Jeff Browndbf8d272011-03-18 18:14:26 -0700323 // The event queue.
324 static const int EVENT_BUFFER_SIZE = 256;
325 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
326
Jeff Brown6d0fec22010-07-23 21:28:06 -0700327 // This reader/writer lock guards the list of input devices.
328 // The writer lock must be held whenever the list of input devices is modified
329 // and then promptly released.
330 // The reader lock must be held whenever the list of input devices is traversed or an
331 // input device in the list is accessed.
332 // This lock only protects the registry and prevents inadvertent deletion of device objects
333 // that are in use. Individual devices are responsible for guarding their own internal state
334 // as needed for concurrent operation.
335 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700336 KeyedVector<int32_t, InputDevice*> mDevices;
337
Jeff Brown6d0fec22010-07-23 21:28:06 -0700338 // low-level input event decoding and device management
Jeff Browndbf8d272011-03-18 18:14:26 -0700339 void processEvents(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700340
Jeff Brown7342bb92010-10-01 18:55:43 -0700341 void addDevice(int32_t deviceId);
342 void removeDevice(int32_t deviceId);
Jeff Browndbf8d272011-03-18 18:14:26 -0700343 void processEventsForDevice(int32_t deviceId, const RawEvent* rawEvents, size_t count);
Jeff Brown68d60752011-03-17 01:34:19 -0700344 void timeoutExpired(nsecs_t when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345
Jeff Brownc3db8582010-10-20 15:33:38 -0700346 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700347
Jeff Brown6d0fec22010-07-23 21:28:06 -0700348 // state management for all devices
349 Mutex mStateLock;
350
Jeff Brown1a84fd12011-06-02 01:26:32 -0700351 int32_t mGlobalMetaState; // guarded by mStateLock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700352 virtual void updateGlobalMetaState();
353 virtual int32_t getGlobalMetaState();
354
Jeff Brown05dc66a2011-03-02 14:41:58 -0800355 virtual void fadePointer();
356
Jeff Brown1a84fd12011-06-02 01:26:32 -0700357 InputConfiguration mInputConfiguration; // guarded by mStateLock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700358 void updateInputConfiguration();
359
Jeff Browndbf8d272011-03-18 18:14:26 -0700360 nsecs_t mDisableVirtualKeysTimeout; // only accessed by reader thread
Jeff Brownfe508922011-01-18 15:10:10 -0800361 virtual void disableVirtualKeysUntil(nsecs_t time);
362 virtual bool shouldDropVirtualKey(nsecs_t now,
363 InputDevice* device, int32_t keyCode, int32_t scanCode);
364
Jeff Brown1a84fd12011-06-02 01:26:32 -0700365 nsecs_t mNextTimeout; // only accessed by reader thread, not guarded
Jeff Brown68d60752011-03-17 01:34:19 -0700366 virtual void requestTimeoutAtTime(nsecs_t when);
367
Jeff Brown1a84fd12011-06-02 01:26:32 -0700368 volatile int32_t mRefreshConfiguration; // atomic
369 void configure(bool firstTime);
370
Jeff Brown6d0fec22010-07-23 21:28:06 -0700371 // state queries
372 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
373 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
374 GetStateFunc getStateFunc);
375 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
376 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700377};
378
379
380/* Reads raw events from the event hub and processes them, endlessly. */
381class InputReaderThread : public Thread {
382public:
383 InputReaderThread(const sp<InputReaderInterface>& reader);
384 virtual ~InputReaderThread();
385
386private:
387 sp<InputReaderInterface> mReader;
388
389 virtual bool threadLoop();
390};
391
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392
393/* Represents the state of a single input device. */
394class InputDevice {
395public:
396 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
397 ~InputDevice();
398
399 inline InputReaderContext* getContext() { return mContext; }
400 inline int32_t getId() { return mId; }
401 inline const String8& getName() { return mName; }
402 inline uint32_t getSources() { return mSources; }
403
Jeff Brown56194eb2011-03-02 19:23:13 -0800404 inline bool isExternal() { return mIsExternal; }
405 inline void setExternal(bool external) { mIsExternal = external; }
406
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 inline bool isIgnored() { return mMappers.isEmpty(); }
408
Jeff Brownef3d7e82010-09-30 14:33:04 -0700409 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 void addMapper(InputMapper* mapper);
411 void configure();
412 void reset();
Jeff Browndbf8d272011-03-18 18:14:26 -0700413 void process(const RawEvent* rawEvents, size_t count);
Jeff Brown68d60752011-03-17 01:34:19 -0700414 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415
416 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
417 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
418 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
419 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
420 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
421 const int32_t* keyCodes, uint8_t* outFlags);
422
423 int32_t getMetaState();
424
Jeff Brown05dc66a2011-03-02 14:41:58 -0800425 void fadePointer();
426
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800427 inline const PropertyMap& getConfiguration() {
428 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700429 }
430
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431private:
432 InputReaderContext* mContext;
433 int32_t mId;
434
435 Vector<InputMapper*> mMappers;
436
437 String8 mName;
438 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800439 bool mIsExternal;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700440
441 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
442 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700443
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800444 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700445};
446
447
448/* An input mapper transforms raw input events into cooked event data.
449 * A single input device can have multiple associated input mappers in order to interpret
450 * different classes of events.
451 */
452class InputMapper {
453public:
454 InputMapper(InputDevice* device);
455 virtual ~InputMapper();
456
457 inline InputDevice* getDevice() { return mDevice; }
458 inline int32_t getDeviceId() { return mDevice->getId(); }
459 inline const String8 getDeviceName() { return mDevice->getName(); }
460 inline InputReaderContext* getContext() { return mContext; }
461 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
Jeff Brown214eaf42011-05-26 19:17:02 -0700462 inline const InputReaderConfiguration* getConfig() { return mContext->getConfig(); }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700463 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
464 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
465
466 virtual uint32_t getSources() = 0;
467 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700468 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700469 virtual void configure();
470 virtual void reset();
471 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brown68d60752011-03-17 01:34:19 -0700472 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700473
474 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
475 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
476 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
477 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
478 const int32_t* keyCodes, uint8_t* outFlags);
479
480 virtual int32_t getMetaState();
481
Jeff Brown05dc66a2011-03-02 14:41:58 -0800482 virtual void fadePointer();
483
Jeff Brown6d0fec22010-07-23 21:28:06 -0700484protected:
485 InputDevice* mDevice;
486 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800487
488 static void dumpRawAbsoluteAxisInfo(String8& dump,
489 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490};
491
492
493class SwitchInputMapper : public InputMapper {
494public:
495 SwitchInputMapper(InputDevice* device);
496 virtual ~SwitchInputMapper();
497
498 virtual uint32_t getSources();
499 virtual void process(const RawEvent* rawEvent);
500
501 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
502
503private:
504 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
505};
506
507
508class KeyboardInputMapper : public InputMapper {
509public:
Jeff Brownefd32662011-03-08 15:13:06 -0800510 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700511 virtual ~KeyboardInputMapper();
512
513 virtual uint32_t getSources();
514 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700515 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800516 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700517 virtual void reset();
518 virtual void process(const RawEvent* rawEvent);
519
520 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
521 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
522 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
523 const int32_t* keyCodes, uint8_t* outFlags);
524
525 virtual int32_t getMetaState();
526
527private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700528 Mutex mLock;
529
Jeff Brown6d0fec22010-07-23 21:28:06 -0700530 struct KeyDown {
531 int32_t keyCode;
532 int32_t scanCode;
533 };
534
Jeff Brownefd32662011-03-08 15:13:06 -0800535 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700536 int32_t mKeyboardType;
537
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800538 // Immutable configuration parameters.
539 struct Parameters {
540 int32_t associatedDisplayId;
541 bool orientationAware;
542 } mParameters;
543
Jeff Brown6328cdc2010-07-29 18:18:33 -0700544 struct LockedState {
545 Vector<KeyDown> keyDowns; // keys that are down
546 int32_t metaState;
547 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700548
549 struct LedState {
550 bool avail; // led is available
551 bool on; // we think the led is currently on
552 };
553 LedState capsLockLedState;
554 LedState numLockLedState;
555 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700556 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700557
Jeff Brown6328cdc2010-07-29 18:18:33 -0700558 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700559
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800560 void configureParameters();
561 void dumpParameters(String8& dump);
562
Jeff Brown6d0fec22010-07-23 21:28:06 -0700563 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700564
Jeff Brown6d0fec22010-07-23 21:28:06 -0700565 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
566 uint32_t policyFlags);
567
Jeff Brown6328cdc2010-07-29 18:18:33 -0700568 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700569
Jeff Brown49ed71d2010-12-06 17:13:33 -0800570 void resetLedStateLocked();
571 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700572 void updateLedStateLocked(bool reset);
573 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
574 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575};
576
577
Jeff Brown83c09682010-12-23 17:50:18 -0800578class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700579public:
Jeff Brown83c09682010-12-23 17:50:18 -0800580 CursorInputMapper(InputDevice* device);
581 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582
583 virtual uint32_t getSources();
584 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700585 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800586 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700587 virtual void reset();
588 virtual void process(const RawEvent* rawEvent);
589
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700590 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
591
Jeff Brown05dc66a2011-03-02 14:41:58 -0800592 virtual void fadePointer();
593
Jeff Brown6d0fec22010-07-23 21:28:06 -0700594private:
595 // Amount that trackball needs to move in order to generate a key event.
596 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
597
Jeff Brown6328cdc2010-07-29 18:18:33 -0700598 Mutex mLock;
599
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800600 // Immutable configuration parameters.
601 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800602 enum Mode {
603 MODE_POINTER,
604 MODE_NAVIGATION,
605 };
606
607 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800608 int32_t associatedDisplayId;
609 bool orientationAware;
610 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700611
612 struct Accumulator {
613 enum {
Jeff Brownefd32662011-03-08 15:13:06 -0800614 FIELD_BUTTONS = 1,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700615 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800616 FIELD_REL_Y = 4,
617 FIELD_REL_WHEEL = 8,
618 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700619 };
620
621 uint32_t fields;
622
Jeff Brownefd32662011-03-08 15:13:06 -0800623 uint32_t buttonDown;
624 uint32_t buttonUp;
625
Jeff Brown6d0fec22010-07-23 21:28:06 -0700626 int32_t relX;
627 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800628 int32_t relWheel;
629 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700630
631 inline void clear() {
632 fields = 0;
633 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700634 } mAccumulator;
635
Jeff Brownefd32662011-03-08 15:13:06 -0800636 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700637 float mXScale;
638 float mYScale;
639 float mXPrecision;
640 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800641
642 bool mHaveVWheel;
643 bool mHaveHWheel;
644 float mVWheelScale;
645 float mHWheelScale;
646
Jeff Brown19c97d462011-06-01 12:33:19 -0700647 // Velocity controls for mouse pointer and wheel movements.
648 // The controls for X and Y wheel movements are separate to keep them decoupled.
649 VelocityControl mPointerVelocityControl;
650 VelocityControl mWheelXVelocityControl;
651 VelocityControl mWheelYVelocityControl;
652
Jeff Brown83c09682010-12-23 17:50:18 -0800653 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700654
Jeff Brown6328cdc2010-07-29 18:18:33 -0700655 struct LockedState {
Jeff Brownefd32662011-03-08 15:13:06 -0800656 uint32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700657 nsecs_t downTime;
658 } mLocked;
659
660 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700661
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800662 void configureParameters();
663 void dumpParameters(String8& dump);
664
Jeff Brown6d0fec22010-07-23 21:28:06 -0700665 void sync(nsecs_t when);
666};
667
668
669class TouchInputMapper : public InputMapper {
670public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800671 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700672 virtual ~TouchInputMapper();
673
674 virtual uint32_t getSources();
675 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700676 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700677 virtual void configure();
678 virtual void reset();
679
680 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
681 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
682 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
683 const int32_t* keyCodes, uint8_t* outFlags);
684
Jeff Brown96ad3972011-03-09 17:39:48 -0800685 virtual void fadePointer();
Jeff Brown325bd072011-04-19 21:20:10 -0700686 virtual void timeoutExpired(nsecs_t when);
Jeff Brown96ad3972011-03-09 17:39:48 -0800687
Jeff Brown6d0fec22010-07-23 21:28:06 -0700688protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700689 Mutex mLock;
690
Jeff Brown6d0fec22010-07-23 21:28:06 -0700691 struct VirtualKey {
692 int32_t keyCode;
693 int32_t scanCode;
694 uint32_t flags;
695
696 // computed hit box, specified in touch screen coords based on known display size
697 int32_t hitLeft;
698 int32_t hitTop;
699 int32_t hitRight;
700 int32_t hitBottom;
701
702 inline bool isHit(int32_t x, int32_t y) const {
703 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
704 }
705 };
706
Jeff Brown8d608662010-08-30 03:02:23 -0700707 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700708 struct PointerData {
709 uint32_t id;
710 int32_t x;
711 int32_t y;
712 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700713 int32_t touchMajor;
714 int32_t touchMinor;
715 int32_t toolMajor;
716 int32_t toolMinor;
717 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700718
719 inline bool operator== (const PointerData& other) const {
720 return id == other.id
721 && x == other.x
722 && y == other.y
723 && pressure == other.pressure
724 && touchMajor == other.touchMajor
725 && touchMinor == other.touchMinor
726 && toolMajor == other.toolMajor
727 && toolMinor == other.toolMinor
728 && orientation == other.orientation;
729 }
730 inline bool operator!= (const PointerData& other) const {
731 return !(*this == other);
732 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700733 };
734
Jeff Brown8d608662010-08-30 03:02:23 -0700735 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700736 struct TouchData {
737 uint32_t pointerCount;
738 PointerData pointers[MAX_POINTERS];
739 BitSet32 idBits;
740 uint32_t idToIndex[MAX_POINTER_ID + 1];
Jeff Brown96ad3972011-03-09 17:39:48 -0800741 uint32_t buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700742
743 void copyFrom(const TouchData& other) {
744 pointerCount = other.pointerCount;
745 idBits = other.idBits;
Jeff Brown96ad3972011-03-09 17:39:48 -0800746 buttonState = other.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700747
748 for (uint32_t i = 0; i < pointerCount; i++) {
749 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700750
751 int id = pointers[i].id;
752 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700753 }
754 }
755
756 inline void clear() {
757 pointerCount = 0;
758 idBits.clear();
Jeff Brown96ad3972011-03-09 17:39:48 -0800759 buttonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700760 }
Jeff Brown86ea1f52011-04-12 22:39:53 -0700761
762 void getCentroid(float* outX, float* outY) {
763 float x = 0, y = 0;
764 if (pointerCount != 0) {
765 for (uint32_t i = 0; i < pointerCount; i++) {
766 x += pointers[i].x;
767 y += pointers[i].y;
768 }
769 x /= pointerCount;
770 y /= pointerCount;
771 }
772 *outX = x;
773 *outY = y;
774 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700775 };
776
Jeff Brown83c09682010-12-23 17:50:18 -0800777 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800778 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brown96ad3972011-03-09 17:39:48 -0800779 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800780
Jeff Brown214eaf42011-05-26 19:17:02 -0700781 // The reader's configuration.
782 const InputReaderConfiguration* mConfig;
783
Jeff Brown6d0fec22010-07-23 21:28:06 -0700784 // Immutable configuration parameters.
785 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800786 enum DeviceType {
787 DEVICE_TYPE_TOUCH_SCREEN,
788 DEVICE_TYPE_TOUCH_PAD,
Jeff Brown96ad3972011-03-09 17:39:48 -0800789 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800790 };
791
792 DeviceType deviceType;
793 int32_t associatedDisplayId;
794 bool orientationAware;
795
Jeff Brown6d0fec22010-07-23 21:28:06 -0700796 bool useBadTouchFilter;
797 bool useJumpyTouchFilter;
798 bool useAveragingTouchFilter;
Jeff Brown86ea1f52011-04-12 22:39:53 -0700799
800 enum GestureMode {
801 GESTURE_MODE_POINTER,
802 GESTURE_MODE_SPOTS,
803 };
804 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700805 } mParameters;
806
Jeff Brown8d608662010-08-30 03:02:23 -0700807 // Immutable calibration parameters in parsed form.
808 struct Calibration {
Jeff Brownc6d282b2010-10-14 21:42:15 -0700809 // Touch Size
810 enum TouchSizeCalibration {
811 TOUCH_SIZE_CALIBRATION_DEFAULT,
812 TOUCH_SIZE_CALIBRATION_NONE,
813 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
814 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700815 };
816
Jeff Brownc6d282b2010-10-14 21:42:15 -0700817 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700818
Jeff Brownc6d282b2010-10-14 21:42:15 -0700819 // Tool Size
820 enum ToolSizeCalibration {
821 TOOL_SIZE_CALIBRATION_DEFAULT,
822 TOOL_SIZE_CALIBRATION_NONE,
823 TOOL_SIZE_CALIBRATION_GEOMETRIC,
824 TOOL_SIZE_CALIBRATION_LINEAR,
825 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700826 };
827
Jeff Brownc6d282b2010-10-14 21:42:15 -0700828 ToolSizeCalibration toolSizeCalibration;
829 bool haveToolSizeLinearScale;
830 float toolSizeLinearScale;
831 bool haveToolSizeLinearBias;
832 float toolSizeLinearBias;
833 bool haveToolSizeAreaScale;
834 float toolSizeAreaScale;
835 bool haveToolSizeAreaBias;
836 float toolSizeAreaBias;
837 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800838 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700839
840 // Pressure
841 enum PressureCalibration {
842 PRESSURE_CALIBRATION_DEFAULT,
843 PRESSURE_CALIBRATION_NONE,
844 PRESSURE_CALIBRATION_PHYSICAL,
845 PRESSURE_CALIBRATION_AMPLITUDE,
846 };
847 enum PressureSource {
848 PRESSURE_SOURCE_DEFAULT,
849 PRESSURE_SOURCE_PRESSURE,
850 PRESSURE_SOURCE_TOUCH,
851 };
852
853 PressureCalibration pressureCalibration;
854 PressureSource pressureSource;
855 bool havePressureScale;
856 float pressureScale;
857
858 // Size
859 enum SizeCalibration {
860 SIZE_CALIBRATION_DEFAULT,
861 SIZE_CALIBRATION_NONE,
862 SIZE_CALIBRATION_NORMALIZED,
863 };
864
865 SizeCalibration sizeCalibration;
866
867 // Orientation
868 enum OrientationCalibration {
869 ORIENTATION_CALIBRATION_DEFAULT,
870 ORIENTATION_CALIBRATION_NONE,
871 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800872 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700873 };
874
875 OrientationCalibration orientationCalibration;
876 } mCalibration;
877
878 // Raw axis information from the driver.
879 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700880 RawAbsoluteAxisInfo x;
881 RawAbsoluteAxisInfo y;
882 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700883 RawAbsoluteAxisInfo touchMajor;
884 RawAbsoluteAxisInfo touchMinor;
885 RawAbsoluteAxisInfo toolMajor;
886 RawAbsoluteAxisInfo toolMinor;
887 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700888 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700889
Jeff Brown6328cdc2010-07-29 18:18:33 -0700890 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700891 TouchData mCurrentTouch;
Jeff Brown96ad3972011-03-09 17:39:48 -0800892 PointerCoords mCurrentTouchCoords[MAX_POINTERS];
893
Jeff Brown6d0fec22010-07-23 21:28:06 -0700894 TouchData mLastTouch;
Jeff Brown96ad3972011-03-09 17:39:48 -0800895 PointerCoords mLastTouchCoords[MAX_POINTERS];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700896
897 // The time the primary pointer last went down.
898 nsecs_t mDownTime;
899
Jeff Brown96ad3972011-03-09 17:39:48 -0800900 // The pointer controller, or null if the device is not a pointer.
901 sp<PointerControllerInterface> mPointerController;
902
Jeff Brown6328cdc2010-07-29 18:18:33 -0700903 struct LockedState {
904 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700905
Jeff Brown6328cdc2010-07-29 18:18:33 -0700906 // The surface orientation and width and height set by configureSurfaceLocked().
907 int32_t surfaceOrientation;
908 int32_t surfaceWidth, surfaceHeight;
909
Jeff Brownefd32662011-03-08 15:13:06 -0800910 // The associated display orientation and width and height set by configureSurfaceLocked().
911 int32_t associatedDisplayOrientation;
912 int32_t associatedDisplayWidth, associatedDisplayHeight;
913
Jeff Brown6328cdc2010-07-29 18:18:33 -0700914 // Translation and scaling factors, orientation-independent.
Jeff Brown6328cdc2010-07-29 18:18:33 -0700915 float xScale;
916 float xPrecision;
917
Jeff Brown6328cdc2010-07-29 18:18:33 -0700918 float yScale;
919 float yPrecision;
920
Jeff Brown8d608662010-08-30 03:02:23 -0700921 float geometricScale;
922
Jeff Brownc6d282b2010-10-14 21:42:15 -0700923 float toolSizeLinearScale;
924 float toolSizeLinearBias;
925 float toolSizeAreaScale;
926 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700927
Jeff Brown6328cdc2010-07-29 18:18:33 -0700928 float pressureScale;
929
Jeff Brown6328cdc2010-07-29 18:18:33 -0700930 float sizeScale;
931
932 float orientationScale;
933
934 // Oriented motion ranges for input device info.
935 struct OrientedRanges {
936 InputDeviceInfo::MotionRange x;
937 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700938
939 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700940 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700941
942 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700943 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700944
Jeff Brownc6d282b2010-10-14 21:42:15 -0700945 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700946 InputDeviceInfo::MotionRange touchMajor;
947 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700948
Jeff Brownc6d282b2010-10-14 21:42:15 -0700949 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700950 InputDeviceInfo::MotionRange toolMajor;
951 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700952
953 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700954 InputDeviceInfo::MotionRange orientation;
955 } orientedRanges;
956
957 // Oriented dimensions and precision.
958 float orientedSurfaceWidth, orientedSurfaceHeight;
959 float orientedXPrecision, orientedYPrecision;
960
961 struct CurrentVirtualKeyState {
962 bool down;
963 nsecs_t downTime;
964 int32_t keyCode;
965 int32_t scanCode;
966 } currentVirtualKey;
Jeff Brown96ad3972011-03-09 17:39:48 -0800967
968 // Scale factor for gesture based pointer movements.
969 float pointerGestureXMovementScale;
970 float pointerGestureYMovementScale;
971
972 // Scale factor for gesture based zooming and other freeform motions.
973 float pointerGestureXZoomScale;
974 float pointerGestureYZoomScale;
975
Jeff Brown86ea1f52011-04-12 22:39:53 -0700976 // The maximum swipe width.
977 float pointerGestureMaxSwipeWidth;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700978 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700979
Jeff Brown8d608662010-08-30 03:02:23 -0700980 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700981 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700982 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700983 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700984 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700985 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700986 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700987 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700988 virtual void parseCalibration();
989 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700990 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700991
992 enum TouchResult {
993 // Dispatch the touch normally.
994 DISPATCH_TOUCH,
995 // Do not dispatch the touch, but keep tracking the current stroke.
996 SKIP_TOUCH,
997 // Do not dispatch the touch, and drop all information associated with the current stoke
998 // so the next movement will appear as a new down.
999 DROP_STROKE
1000 };
1001
1002 void syncTouch(nsecs_t when, bool havePointerIds);
1003
1004private:
1005 /* Maximum number of historical samples to average. */
1006 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
1007
1008 /* Slop distance for jumpy pointer detection.
1009 * The vertical range of the screen divided by this is our epsilon value. */
1010 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
1011
1012 /* Number of jumpy points to drop for touchscreens that need it. */
1013 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
1014 static const uint32_t JUMPY_DROP_LIMIT = 3;
1015
1016 /* Maximum squared distance for averaging.
1017 * If moving farther than this, turn of averaging to avoid lag in response. */
1018 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
1019
1020 struct AveragingTouchFilterState {
1021 // Individual history tracks are stored by pointer id
1022 uint32_t historyStart[MAX_POINTERS];
1023 uint32_t historyEnd[MAX_POINTERS];
1024 struct {
1025 struct {
1026 int32_t x;
1027 int32_t y;
1028 int32_t pressure;
1029 } pointers[MAX_POINTERS];
1030 } historyData[AVERAGING_HISTORY_SIZE];
1031 } mAveragingTouchFilter;
1032
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001033 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001034 uint32_t jumpyPointsDropped;
1035 } mJumpyTouchFilter;
1036
1037 struct PointerDistanceHeapElement {
1038 uint32_t currentPointerIndex : 8;
1039 uint32_t lastPointerIndex : 8;
1040 uint64_t distance : 48; // squared distance
1041 };
1042
Jeff Brown96ad3972011-03-09 17:39:48 -08001043 struct PointerGesture {
1044 enum Mode {
1045 // No fingers, button is not pressed.
1046 // Nothing happening.
1047 NEUTRAL,
1048
1049 // No fingers, button is not pressed.
1050 // Tap detected.
1051 // Emits DOWN and UP events at the pointer location.
1052 TAP,
1053
Jeff Brown325bd072011-04-19 21:20:10 -07001054 // Exactly one finger dragging following a tap.
1055 // Pointer follows the active finger.
1056 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001057 //
1058 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
Jeff Brown325bd072011-04-19 21:20:10 -07001059 TAP_DRAG,
1060
Jeff Brown96ad3972011-03-09 17:39:48 -08001061 // Button is pressed.
1062 // Pointer follows the active finger if there is one. Other fingers are ignored.
1063 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown325bd072011-04-19 21:20:10 -07001064 BUTTON_CLICK_OR_DRAG,
Jeff Brown96ad3972011-03-09 17:39:48 -08001065
1066 // Exactly one finger, button is not pressed.
1067 // Pointer follows the active finger.
1068 // Emits HOVER_MOVE events at the pointer location.
Jeff Brown214eaf42011-05-26 19:17:02 -07001069 //
1070 // Detect taps when the finger goes up while in HOVER mode.
Jeff Brown96ad3972011-03-09 17:39:48 -08001071 HOVER,
1072
Jeff Brown86ea1f52011-04-12 22:39:53 -07001073 // Exactly two fingers but neither have moved enough to clearly indicate
1074 // whether a swipe or freeform gesture was intended. We consider the
1075 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1076 // Pointer does not move.
1077 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1078 PRESS,
Jeff Brown96ad3972011-03-09 17:39:48 -08001079
1080 // Exactly two fingers moving in the same direction, button is not pressed.
1081 // Pointer does not move.
1082 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1083 // follows the midpoint between both fingers.
Jeff Brown96ad3972011-03-09 17:39:48 -08001084 SWIPE,
1085
1086 // Two or more fingers moving in arbitrary directions, button is not pressed.
1087 // Pointer does not move.
1088 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1089 // each finger individually relative to the initial centroid of the finger.
Jeff Brown96ad3972011-03-09 17:39:48 -08001090 FREEFORM,
1091
1092 // Waiting for quiet time to end before starting the next gesture.
1093 QUIET,
1094 };
1095
Jeff Brown86ea1f52011-04-12 22:39:53 -07001096 // Time the first finger went down.
1097 nsecs_t firstTouchTime;
1098
Jeff Brown96ad3972011-03-09 17:39:48 -08001099 // The active pointer id from the raw touch data.
1100 int32_t activeTouchId; // -1 if none
1101
1102 // The active pointer id from the gesture last delivered to the application.
1103 int32_t activeGestureId; // -1 if none
1104
1105 // Pointer coords and ids for the current and previous pointer gesture.
1106 Mode currentGestureMode;
Jeff Brown96ad3972011-03-09 17:39:48 -08001107 BitSet32 currentGestureIdBits;
1108 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1109 PointerCoords currentGestureCoords[MAX_POINTERS];
1110
1111 Mode lastGestureMode;
Jeff Brown96ad3972011-03-09 17:39:48 -08001112 BitSet32 lastGestureIdBits;
1113 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1114 PointerCoords lastGestureCoords[MAX_POINTERS];
1115
Jeff Brown86ea1f52011-04-12 22:39:53 -07001116 // Pointer coords and ids for the current spots.
1117 PointerControllerInterface::SpotGesture spotGesture;
1118 BitSet32 spotIdBits; // same set of ids as touch ids
1119 uint32_t spotIdToIndex[MAX_POINTER_ID + 1];
1120 PointerCoords spotCoords[MAX_POINTERS];
Jeff Brown96ad3972011-03-09 17:39:48 -08001121
1122 // Time the pointer gesture last went down.
1123 nsecs_t downTime;
1124
Jeff Brown325bd072011-04-19 21:20:10 -07001125 // Time when the pointer went down for a TAP.
1126 nsecs_t tapDownTime;
1127
1128 // Time when the pointer went up for a TAP.
1129 nsecs_t tapUpTime;
Jeff Brown96ad3972011-03-09 17:39:48 -08001130
Jeff Brown86ea1f52011-04-12 22:39:53 -07001131 // Location of initial tap.
1132 float tapX, tapY;
1133
Jeff Brown96ad3972011-03-09 17:39:48 -08001134 // Time we started waiting for quiescence.
1135 nsecs_t quietTime;
1136
Jeff Brown86ea1f52011-04-12 22:39:53 -07001137 // Reference points for multitouch gestures.
1138 float referenceTouchX; // reference touch X/Y coordinates in surface units
1139 float referenceTouchY;
1140 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1141 float referenceGestureY;
1142
Jeff Brown538881e2011-05-25 18:23:38 -07001143 // Distance that each pointer has traveled which has not yet been
1144 // subsumed into the reference gesture position.
1145 BitSet32 referenceIdBits;
1146 struct Delta {
1147 float dx, dy;
1148 };
1149 Delta referenceDeltas[MAX_POINTER_ID + 1];
1150
Jeff Brown86ea1f52011-04-12 22:39:53 -07001151 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1152 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1153
Jeff Brown96ad3972011-03-09 17:39:48 -08001154 // A velocity tracker for determining whether to switch active pointers during drags.
1155 VelocityTracker velocityTracker;
1156
Jeff Brown19c97d462011-06-01 12:33:19 -07001157 // Velocity control for pointer movements.
1158 VelocityControl pointerVelocityControl;
1159
Jeff Brown96ad3972011-03-09 17:39:48 -08001160 void reset() {
Jeff Brown86ea1f52011-04-12 22:39:53 -07001161 firstTouchTime = LLONG_MIN;
Jeff Brown96ad3972011-03-09 17:39:48 -08001162 activeTouchId = -1;
1163 activeGestureId = -1;
1164 currentGestureMode = NEUTRAL;
Jeff Brown96ad3972011-03-09 17:39:48 -08001165 currentGestureIdBits.clear();
1166 lastGestureMode = NEUTRAL;
Jeff Brown96ad3972011-03-09 17:39:48 -08001167 lastGestureIdBits.clear();
Jeff Brown86ea1f52011-04-12 22:39:53 -07001168 spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
1169 spotIdBits.clear();
Jeff Brown96ad3972011-03-09 17:39:48 -08001170 downTime = 0;
1171 velocityTracker.clear();
Jeff Brown325bd072011-04-19 21:20:10 -07001172 resetTap();
Jeff Brown96ad3972011-03-09 17:39:48 -08001173 resetQuietTime();
Jeff Brown19c97d462011-06-01 12:33:19 -07001174 pointerVelocityControl.reset();
Jeff Brown96ad3972011-03-09 17:39:48 -08001175 }
1176
Jeff Brown325bd072011-04-19 21:20:10 -07001177 void resetTap() {
1178 tapDownTime = LLONG_MIN;
1179 tapUpTime = LLONG_MIN;
Jeff Brown96ad3972011-03-09 17:39:48 -08001180 }
1181
1182 void resetQuietTime() {
1183 quietTime = LLONG_MIN;
1184 }
1185 } mPointerGesture;
1186
Jeff Brown6328cdc2010-07-29 18:18:33 -07001187 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001188
1189 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
1190 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brown96ad3972011-03-09 17:39:48 -08001191 void prepareTouches(int32_t* outEdgeFlags, float* outXPrecision, float* outYPrecision);
Jeff Brown325bd072011-04-19 21:20:10 -07001192 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1193 bool preparePointerGestures(nsecs_t when,
1194 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
Jeff Brown86ea1f52011-04-12 22:39:53 -07001195 void moveSpotsLocked();
Jeff Brown96ad3972011-03-09 17:39:48 -08001196
1197 // Dispatches a motion event.
1198 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1199 // method will take care of setting the index and transmuting the action to DOWN or UP
1200 // it is the first / last pointer to go down / up.
1201 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
1202 int32_t action, int32_t flags, uint32_t metaState, int32_t edgeFlags,
1203 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
1204 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1205
1206 // Updates pointer coords for pointers with specified ids that have moved.
1207 // Returns true if any of them changed.
1208 bool updateMovedPointerCoords(const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1209 PointerCoords* outCoords, const uint32_t* outIdToIndex, BitSet32 idBits) const;
1210
Jeff Brownefd32662011-03-08 15:13:06 -08001211 void suppressSwipeOntoVirtualKeys(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001212
Jeff Brown6328cdc2010-07-29 18:18:33 -07001213 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
1214 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001215
1216 bool applyBadTouchFilter();
1217 bool applyJumpyTouchFilter();
1218 void applyAveragingTouchFilter();
1219 void calculatePointerIds();
1220};
1221
1222
1223class SingleTouchInputMapper : public TouchInputMapper {
1224public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001225 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001226 virtual ~SingleTouchInputMapper();
1227
1228 virtual void reset();
1229 virtual void process(const RawEvent* rawEvent);
1230
1231protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001232 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001233
1234private:
1235 struct Accumulator {
1236 enum {
1237 FIELD_BTN_TOUCH = 1,
1238 FIELD_ABS_X = 2,
1239 FIELD_ABS_Y = 4,
1240 FIELD_ABS_PRESSURE = 8,
Jeff Brownefd32662011-03-08 15:13:06 -08001241 FIELD_ABS_TOOL_WIDTH = 16,
Jeff Brown96ad3972011-03-09 17:39:48 -08001242 FIELD_BUTTONS = 32,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001243 };
1244
1245 uint32_t fields;
1246
1247 bool btnTouch;
1248 int32_t absX;
1249 int32_t absY;
1250 int32_t absPressure;
1251 int32_t absToolWidth;
1252
Jeff Brown96ad3972011-03-09 17:39:48 -08001253 uint32_t buttonDown;
1254 uint32_t buttonUp;
1255
Jeff Brown6d0fec22010-07-23 21:28:06 -07001256 inline void clear() {
1257 fields = 0;
Jeff Brown96ad3972011-03-09 17:39:48 -08001258 buttonDown = 0;
1259 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001260 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001261 } mAccumulator;
1262
1263 bool mDown;
1264 int32_t mX;
1265 int32_t mY;
1266 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07001267 int32_t mToolWidth;
Jeff Brown96ad3972011-03-09 17:39:48 -08001268 uint32_t mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001269
1270 void initialize();
1271
1272 void sync(nsecs_t when);
1273};
1274
1275
1276class MultiTouchInputMapper : public TouchInputMapper {
1277public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001278 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001279 virtual ~MultiTouchInputMapper();
1280
1281 virtual void reset();
1282 virtual void process(const RawEvent* rawEvent);
1283
1284protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001285 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001286
1287private:
1288 struct Accumulator {
1289 enum {
1290 FIELD_ABS_MT_POSITION_X = 1,
1291 FIELD_ABS_MT_POSITION_Y = 2,
1292 FIELD_ABS_MT_TOUCH_MAJOR = 4,
1293 FIELD_ABS_MT_TOUCH_MINOR = 8,
1294 FIELD_ABS_MT_WIDTH_MAJOR = 16,
1295 FIELD_ABS_MT_WIDTH_MINOR = 32,
1296 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001297 FIELD_ABS_MT_TRACKING_ID = 128,
1298 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001299 };
1300
1301 uint32_t pointerCount;
1302 struct Pointer {
1303 uint32_t fields;
1304
1305 int32_t absMTPositionX;
1306 int32_t absMTPositionY;
1307 int32_t absMTTouchMajor;
1308 int32_t absMTTouchMinor;
1309 int32_t absMTWidthMajor;
1310 int32_t absMTWidthMinor;
1311 int32_t absMTOrientation;
1312 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001313 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001314
1315 inline void clear() {
1316 fields = 0;
1317 }
1318 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
1319
Jeff Brown96ad3972011-03-09 17:39:48 -08001320 // Bitfield of buttons that went down or up.
1321 uint32_t buttonDown;
1322 uint32_t buttonUp;
1323
Jeff Brown6d0fec22010-07-23 21:28:06 -07001324 inline void clear() {
1325 pointerCount = 0;
1326 pointers[0].clear();
Jeff Brown96ad3972011-03-09 17:39:48 -08001327 buttonDown = 0;
1328 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001329 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001330 } mAccumulator;
1331
Jeff Brown96ad3972011-03-09 17:39:48 -08001332 uint32_t mButtonState;
1333
Jeff Brown6d0fec22010-07-23 21:28:06 -07001334 void initialize();
1335
1336 void sync(nsecs_t when);
1337};
1338
Jeff Browncb1404e2011-01-15 18:14:15 -08001339
1340class JoystickInputMapper : public InputMapper {
1341public:
1342 JoystickInputMapper(InputDevice* device);
1343 virtual ~JoystickInputMapper();
1344
1345 virtual uint32_t getSources();
1346 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1347 virtual void dump(String8& dump);
1348 virtual void configure();
1349 virtual void reset();
1350 virtual void process(const RawEvent* rawEvent);
1351
1352private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001353 struct Axis {
1354 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001355 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001356
Jeff Brown6f2fba42011-02-19 01:08:02 -08001357 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001358
Jeff Brown6f2fba42011-02-19 01:08:02 -08001359 float scale; // scale factor from raw to normalized values
1360 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001361 float highScale; // scale factor from raw to normalized values of high split
1362 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001363
Jeff Brown6f2fba42011-02-19 01:08:02 -08001364 float min; // normalized inclusive minimum
1365 float max; // normalized inclusive maximum
1366 float flat; // normalized flat region size
1367 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001368
Jeff Brown6f2fba42011-02-19 01:08:02 -08001369 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001370 float currentValue; // current value
1371 float newValue; // most recent value
1372 float highCurrentValue; // current value of high split
1373 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001374
Jeff Brown85297452011-03-04 13:07:49 -08001375 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1376 bool explicitlyMapped, float scale, float offset,
1377 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001378 float min, float max, float flat, float fuzz) {
1379 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001380 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001381 this->explicitlyMapped = explicitlyMapped;
1382 this->scale = scale;
1383 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001384 this->highScale = highScale;
1385 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001386 this->min = min;
1387 this->max = max;
1388 this->flat = flat;
1389 this->fuzz = fuzz;
1390 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001391 resetValue();
1392 }
1393
1394 void resetValue() {
1395 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001396 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001397 this->highCurrentValue = 0;
1398 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001399 }
1400 };
1401
Jeff Brown6f2fba42011-02-19 01:08:02 -08001402 // Axes indexed by raw ABS_* axis index.
1403 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001404
Jeff Brown6f2fba42011-02-19 01:08:02 -08001405 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001406
Jeff Brown85297452011-03-04 13:07:49 -08001407 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001408 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001409 bool filterAxes(bool force);
1410
1411 static bool hasValueChangedSignificantly(float filter,
1412 float newValue, float currentValue, float min, float max);
1413 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1414 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001415
Jeff Brown6f2fba42011-02-19 01:08:02 -08001416 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001417};
1418
Jeff Brown46b9ac02010-04-22 18:58:52 -07001419} // namespace android
1420
1421#endif // _UI_INPUT_READER_H