blob: 9ed1391301a7fe696d6b7b575b74f53b14ebb761 [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"
Jeff Browna6dbfdd2011-04-11 11:54:25 -070023#include "SpotController.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080024
Jeff Brown46b9ac02010-04-22 18:58:52 -070025#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080026#include <ui/DisplayInfo.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070027#include <utils/KeyedVector.h>
28#include <utils/threads.h>
29#include <utils/Timers.h>
30#include <utils/RefBase.h>
31#include <utils/String8.h>
32#include <utils/BitSet.h>
33
34#include <stddef.h>
35#include <unistd.h>
36
Jeff Brown46b9ac02010-04-22 18:58:52 -070037namespace android {
38
Jeff Brown6d0fec22010-07-23 21:28:06 -070039class InputDevice;
40class InputMapper;
41
Jeff Brown8d608662010-08-30 03:02:23 -070042
Jeff Brown9c3cda02010-06-15 01:31:58 -070043/*
44 * Input reader policy interface.
45 *
46 * The input reader policy is used by the input reader to interact with the Window Manager
47 * and other system components.
48 *
49 * The actual implementation is partially supported by callbacks into the DVM
50 * via JNI. This interface is also mocked in the unit tests.
51 */
52class InputReaderPolicyInterface : public virtual RefBase {
53protected:
54 InputReaderPolicyInterface() { }
55 virtual ~InputReaderPolicyInterface() { }
56
57public:
58 /* Display orientations. */
59 enum {
60 ROTATION_0 = 0,
61 ROTATION_90 = 1,
62 ROTATION_180 = 2,
63 ROTATION_270 = 3
64 };
65
Jeff Brown9c3cda02010-06-15 01:31:58 -070066 /* Gets information about the display with the specified id.
67 * Returns true if the display info is available, false otherwise.
68 */
69 virtual bool getDisplayInfo(int32_t displayId,
70 int32_t* width, int32_t* height, int32_t* orientation) = 0;
71
Jeff Brown9c3cda02010-06-15 01:31:58 -070072 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
73 * certain device whose screen currently is not all that good.
74 */
75 virtual bool filterTouchEvents() = 0;
76
77 /* Determines whether to turn on some hacks to improve touch interaction with another device
78 * where touch coordinate data can get corrupted.
79 */
80 virtual bool filterJumpyTouchEvents() = 0;
81
Jeff Brownfe508922011-01-18 15:10:10 -080082 /* Gets the amount of time to disable virtual keys after the screen is touched
83 * in order to filter out accidental virtual key presses due to swiping gestures
84 * or taps near the edge of the display. May be 0 to disable the feature.
85 */
86 virtual nsecs_t getVirtualKeyQuietTime() = 0;
87
Jeff Brown9c3cda02010-06-15 01:31:58 -070088 /* Gets the excluded device names for the platform. */
89 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -080090
91 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
92 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Browna6dbfdd2011-04-11 11:54:25 -070093
94 /* Gets a spot controller associated with the specified touch pad device. */
95 virtual sp<SpotControllerInterface> obtainSpotController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070096};
97
98
99/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700100class InputReaderInterface : public virtual RefBase {
101protected:
102 InputReaderInterface() { }
103 virtual ~InputReaderInterface() { }
104
105public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700106 /* Dumps the state of the input reader.
107 *
108 * This method may be called on any thread (usually by the input manager). */
109 virtual void dump(String8& dump) = 0;
110
Jeff Brown46b9ac02010-04-22 18:58:52 -0700111 /* Runs a single iteration of the processing loop.
112 * Nominally reads and processes one incoming message from the EventHub.
113 *
114 * This method should be called on the input reader thread.
115 */
116 virtual void loopOnce() = 0;
117
Jeff Brown9c3cda02010-06-15 01:31:58 -0700118 /* Gets the current input device configuration.
119 *
120 * This method may be called on any thread (usually by the input manager).
121 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700122 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700123
Jeff Brown6d0fec22010-07-23 21:28:06 -0700124 /* Gets information about the specified input device.
125 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
126 * was no such device.
127 *
128 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700129 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700130 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
131
132 /* Gets the list of all registered device ids. */
133 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
134
135 /* Query current input state. */
136 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
137 int32_t scanCode) = 0;
138 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
139 int32_t keyCode) = 0;
140 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
141 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700142
143 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700144 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
145 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
146};
147
148
149/* Internal interface used by individual input devices to access global input device state
150 * and parameters maintained by the input reader.
151 */
152class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700153public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700154 InputReaderContext() { }
155 virtual ~InputReaderContext() { }
156
Jeff Brown6d0fec22010-07-23 21:28:06 -0700157 virtual void updateGlobalMetaState() = 0;
158 virtual int32_t getGlobalMetaState() = 0;
159
Jeff Brownfe508922011-01-18 15:10:10 -0800160 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
161 virtual bool shouldDropVirtualKey(nsecs_t now,
162 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
163
Jeff Brown05dc66a2011-03-02 14:41:58 -0800164 virtual void fadePointer() = 0;
165
Jeff Brown68d60752011-03-17 01:34:19 -0700166 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
167
Jeff Brown6d0fec22010-07-23 21:28:06 -0700168 virtual InputReaderPolicyInterface* getPolicy() = 0;
169 virtual InputDispatcherInterface* getDispatcher() = 0;
170 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171};
172
Jeff Brown9c3cda02010-06-15 01:31:58 -0700173
Jeff Brown46b9ac02010-04-22 18:58:52 -0700174/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700175 * that it sends to the input dispatcher. Some functions of the input reader, such as early
176 * event filtering in low power states, are controlled by a separate policy object.
177 *
178 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700179 * Because the policy and dispatcher can potentially block or cause re-entrance into
180 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700181 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700182 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700183class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184public:
185 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700186 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187 const sp<InputDispatcherInterface>& dispatcher);
188 virtual ~InputReader();
189
Jeff Brownb88102f2010-09-08 11:49:43 -0700190 virtual void dump(String8& dump);
191
Jeff Brown46b9ac02010-04-22 18:58:52 -0700192 virtual void loopOnce();
193
Jeff Brown6d0fec22010-07-23 21:28:06 -0700194 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195
Jeff Brown6d0fec22010-07-23 21:28:06 -0700196 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
197 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198
Jeff Brown6d0fec22010-07-23 21:28:06 -0700199 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
200 int32_t scanCode);
201 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
202 int32_t keyCode);
203 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
204 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205
Jeff Brown6d0fec22010-07-23 21:28:06 -0700206 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
207 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700208
Jeff Brownc3db8582010-10-20 15:33:38 -0700209protected:
210 // These methods are protected virtual so they can be overridden and instrumented
211 // by test cases.
212 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
213
Jeff Brown46b9ac02010-04-22 18:58:52 -0700214private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700216 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700217 sp<InputDispatcherInterface> mDispatcher;
218
Jeff Brown6d0fec22010-07-23 21:28:06 -0700219 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
220 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
221 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
222
Jeff Browndbf8d272011-03-18 18:14:26 -0700223 // The event queue.
224 static const int EVENT_BUFFER_SIZE = 256;
225 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
226
Jeff Brown6d0fec22010-07-23 21:28:06 -0700227 // This reader/writer lock guards the list of input devices.
228 // The writer lock must be held whenever the list of input devices is modified
229 // and then promptly released.
230 // The reader lock must be held whenever the list of input devices is traversed or an
231 // input device in the list is accessed.
232 // This lock only protects the registry and prevents inadvertent deletion of device objects
233 // that are in use. Individual devices are responsible for guarding their own internal state
234 // as needed for concurrent operation.
235 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700236 KeyedVector<int32_t, InputDevice*> mDevices;
237
Jeff Brown6d0fec22010-07-23 21:28:06 -0700238 // low-level input event decoding and device management
Jeff Browndbf8d272011-03-18 18:14:26 -0700239 void processEvents(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240
Jeff Brown7342bb92010-10-01 18:55:43 -0700241 void addDevice(int32_t deviceId);
242 void removeDevice(int32_t deviceId);
Jeff Browndbf8d272011-03-18 18:14:26 -0700243 void processEventsForDevice(int32_t deviceId, const RawEvent* rawEvents, size_t count);
Jeff Brown68d60752011-03-17 01:34:19 -0700244 void timeoutExpired(nsecs_t when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700245
Jeff Brownc3db8582010-10-20 15:33:38 -0700246 void handleConfigurationChanged(nsecs_t when);
Jeff Browndbf8d272011-03-18 18:14:26 -0700247 void configureExcludedDevices();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700248
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249 // state management for all devices
250 Mutex mStateLock;
251
252 int32_t mGlobalMetaState;
253 virtual void updateGlobalMetaState();
254 virtual int32_t getGlobalMetaState();
255
Jeff Brown05dc66a2011-03-02 14:41:58 -0800256 virtual void fadePointer();
257
Jeff Brown6d0fec22010-07-23 21:28:06 -0700258 InputConfiguration mInputConfiguration;
259 void updateInputConfiguration();
260
Jeff Browndbf8d272011-03-18 18:14:26 -0700261 nsecs_t mDisableVirtualKeysTimeout; // only accessed by reader thread
Jeff Brownfe508922011-01-18 15:10:10 -0800262 virtual void disableVirtualKeysUntil(nsecs_t time);
263 virtual bool shouldDropVirtualKey(nsecs_t now,
264 InputDevice* device, int32_t keyCode, int32_t scanCode);
265
Jeff Browndbf8d272011-03-18 18:14:26 -0700266 nsecs_t mNextTimeout; // only accessed by reader thread
Jeff Brown68d60752011-03-17 01:34:19 -0700267 virtual void requestTimeoutAtTime(nsecs_t when);
268
Jeff Brown6d0fec22010-07-23 21:28:06 -0700269 // state queries
270 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
271 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
272 GetStateFunc getStateFunc);
273 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
274 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700275};
276
277
278/* Reads raw events from the event hub and processes them, endlessly. */
279class InputReaderThread : public Thread {
280public:
281 InputReaderThread(const sp<InputReaderInterface>& reader);
282 virtual ~InputReaderThread();
283
284private:
285 sp<InputReaderInterface> mReader;
286
287 virtual bool threadLoop();
288};
289
Jeff Brown6d0fec22010-07-23 21:28:06 -0700290
291/* Represents the state of a single input device. */
292class InputDevice {
293public:
294 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
295 ~InputDevice();
296
297 inline InputReaderContext* getContext() { return mContext; }
298 inline int32_t getId() { return mId; }
299 inline const String8& getName() { return mName; }
300 inline uint32_t getSources() { return mSources; }
301
Jeff Brown56194eb2011-03-02 19:23:13 -0800302 inline bool isExternal() { return mIsExternal; }
303 inline void setExternal(bool external) { mIsExternal = external; }
304
Jeff Brown6d0fec22010-07-23 21:28:06 -0700305 inline bool isIgnored() { return mMappers.isEmpty(); }
306
Jeff Brownef3d7e82010-09-30 14:33:04 -0700307 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700308 void addMapper(InputMapper* mapper);
309 void configure();
310 void reset();
Jeff Browndbf8d272011-03-18 18:14:26 -0700311 void process(const RawEvent* rawEvents, size_t count);
Jeff Brown68d60752011-03-17 01:34:19 -0700312 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700313
314 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
315 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
316 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
317 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
318 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
319 const int32_t* keyCodes, uint8_t* outFlags);
320
321 int32_t getMetaState();
322
Jeff Brown05dc66a2011-03-02 14:41:58 -0800323 void fadePointer();
324
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800325 inline const PropertyMap& getConfiguration() {
326 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700327 }
328
Jeff Brown6d0fec22010-07-23 21:28:06 -0700329private:
330 InputReaderContext* mContext;
331 int32_t mId;
332
333 Vector<InputMapper*> mMappers;
334
335 String8 mName;
336 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800337 bool mIsExternal;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700338
339 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
340 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700341
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800342 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700343};
344
345
346/* An input mapper transforms raw input events into cooked event data.
347 * A single input device can have multiple associated input mappers in order to interpret
348 * different classes of events.
349 */
350class InputMapper {
351public:
352 InputMapper(InputDevice* device);
353 virtual ~InputMapper();
354
355 inline InputDevice* getDevice() { return mDevice; }
356 inline int32_t getDeviceId() { return mDevice->getId(); }
357 inline const String8 getDeviceName() { return mDevice->getName(); }
358 inline InputReaderContext* getContext() { return mContext; }
359 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
360 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
361 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
362
363 virtual uint32_t getSources() = 0;
364 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700365 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366 virtual void configure();
367 virtual void reset();
368 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brown68d60752011-03-17 01:34:19 -0700369 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700370
371 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
372 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
373 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
374 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
375 const int32_t* keyCodes, uint8_t* outFlags);
376
377 virtual int32_t getMetaState();
378
Jeff Brown05dc66a2011-03-02 14:41:58 -0800379 virtual void fadePointer();
380
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381protected:
382 InputDevice* mDevice;
383 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800384
385 static void dumpRawAbsoluteAxisInfo(String8& dump,
386 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387};
388
389
390class SwitchInputMapper : public InputMapper {
391public:
392 SwitchInputMapper(InputDevice* device);
393 virtual ~SwitchInputMapper();
394
395 virtual uint32_t getSources();
396 virtual void process(const RawEvent* rawEvent);
397
398 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
399
400private:
401 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
402};
403
404
405class KeyboardInputMapper : public InputMapper {
406public:
Jeff Brownefd32662011-03-08 15:13:06 -0800407 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 virtual ~KeyboardInputMapper();
409
410 virtual uint32_t getSources();
411 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700412 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800413 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700414 virtual void reset();
415 virtual void process(const RawEvent* rawEvent);
416
417 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
418 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
419 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
420 const int32_t* keyCodes, uint8_t* outFlags);
421
422 virtual int32_t getMetaState();
423
424private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700425 Mutex mLock;
426
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427 struct KeyDown {
428 int32_t keyCode;
429 int32_t scanCode;
430 };
431
Jeff Brownefd32662011-03-08 15:13:06 -0800432 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433 int32_t mKeyboardType;
434
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800435 // Immutable configuration parameters.
436 struct Parameters {
437 int32_t associatedDisplayId;
438 bool orientationAware;
439 } mParameters;
440
Jeff Brown6328cdc2010-07-29 18:18:33 -0700441 struct LockedState {
442 Vector<KeyDown> keyDowns; // keys that are down
443 int32_t metaState;
444 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700445
446 struct LedState {
447 bool avail; // led is available
448 bool on; // we think the led is currently on
449 };
450 LedState capsLockLedState;
451 LedState numLockLedState;
452 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700453 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700454
Jeff Brown6328cdc2010-07-29 18:18:33 -0700455 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700456
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800457 void configureParameters();
458 void dumpParameters(String8& dump);
459
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700461
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
463 uint32_t policyFlags);
464
Jeff Brown6328cdc2010-07-29 18:18:33 -0700465 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700466
Jeff Brown49ed71d2010-12-06 17:13:33 -0800467 void resetLedStateLocked();
468 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700469 void updateLedStateLocked(bool reset);
470 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
471 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472};
473
474
Jeff Brown83c09682010-12-23 17:50:18 -0800475class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476public:
Jeff Brown83c09682010-12-23 17:50:18 -0800477 CursorInputMapper(InputDevice* device);
478 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700479
480 virtual uint32_t getSources();
481 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700482 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800483 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700484 virtual void reset();
485 virtual void process(const RawEvent* rawEvent);
486
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700487 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
488
Jeff Brown05dc66a2011-03-02 14:41:58 -0800489 virtual void fadePointer();
490
Jeff Brown6d0fec22010-07-23 21:28:06 -0700491private:
492 // Amount that trackball needs to move in order to generate a key event.
493 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
494
Jeff Brown6328cdc2010-07-29 18:18:33 -0700495 Mutex mLock;
496
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800497 // Immutable configuration parameters.
498 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800499 enum Mode {
500 MODE_POINTER,
501 MODE_NAVIGATION,
502 };
503
504 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800505 int32_t associatedDisplayId;
506 bool orientationAware;
507 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700508
509 struct Accumulator {
510 enum {
Jeff Brownefd32662011-03-08 15:13:06 -0800511 FIELD_BUTTONS = 1,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800513 FIELD_REL_Y = 4,
514 FIELD_REL_WHEEL = 8,
515 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700516 };
517
518 uint32_t fields;
519
Jeff Brownefd32662011-03-08 15:13:06 -0800520 uint32_t buttonDown;
521 uint32_t buttonUp;
522
Jeff Brown6d0fec22010-07-23 21:28:06 -0700523 int32_t relX;
524 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800525 int32_t relWheel;
526 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527
528 inline void clear() {
529 fields = 0;
530 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700531 } mAccumulator;
532
Jeff Brownefd32662011-03-08 15:13:06 -0800533 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700534 float mXScale;
535 float mYScale;
536 float mXPrecision;
537 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800538
539 bool mHaveVWheel;
540 bool mHaveHWheel;
541 float mVWheelScale;
542 float mHWheelScale;
543
Jeff Brown83c09682010-12-23 17:50:18 -0800544 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700545
Jeff Brown6328cdc2010-07-29 18:18:33 -0700546 struct LockedState {
Jeff Brownefd32662011-03-08 15:13:06 -0800547 uint32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700548 nsecs_t downTime;
549 } mLocked;
550
551 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700552
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800553 void configureParameters();
554 void dumpParameters(String8& dump);
555
Jeff Brown6d0fec22010-07-23 21:28:06 -0700556 void sync(nsecs_t when);
557};
558
559
560class TouchInputMapper : public InputMapper {
561public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800562 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700563 virtual ~TouchInputMapper();
564
565 virtual uint32_t getSources();
566 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700567 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700568 virtual void configure();
569 virtual void reset();
570
571 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
572 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
573 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
574 const int32_t* keyCodes, uint8_t* outFlags);
575
Jeff Brown96ad3972011-03-09 17:39:48 -0800576 virtual void fadePointer();
577
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700579 Mutex mLock;
580
Jeff Brown6d0fec22010-07-23 21:28:06 -0700581 struct VirtualKey {
582 int32_t keyCode;
583 int32_t scanCode;
584 uint32_t flags;
585
586 // computed hit box, specified in touch screen coords based on known display size
587 int32_t hitLeft;
588 int32_t hitTop;
589 int32_t hitRight;
590 int32_t hitBottom;
591
592 inline bool isHit(int32_t x, int32_t y) const {
593 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
594 }
595 };
596
Jeff Brown8d608662010-08-30 03:02:23 -0700597 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598 struct PointerData {
599 uint32_t id;
600 int32_t x;
601 int32_t y;
602 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700603 int32_t touchMajor;
604 int32_t touchMinor;
605 int32_t toolMajor;
606 int32_t toolMinor;
607 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700608
609 inline bool operator== (const PointerData& other) const {
610 return id == other.id
611 && x == other.x
612 && y == other.y
613 && pressure == other.pressure
614 && touchMajor == other.touchMajor
615 && touchMinor == other.touchMinor
616 && toolMajor == other.toolMajor
617 && toolMinor == other.toolMinor
618 && orientation == other.orientation;
619 }
620 inline bool operator!= (const PointerData& other) const {
621 return !(*this == other);
622 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700623 };
624
Jeff Brown8d608662010-08-30 03:02:23 -0700625 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700626 struct TouchData {
627 uint32_t pointerCount;
628 PointerData pointers[MAX_POINTERS];
629 BitSet32 idBits;
630 uint32_t idToIndex[MAX_POINTER_ID + 1];
Jeff Brown96ad3972011-03-09 17:39:48 -0800631 uint32_t buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700632
633 void copyFrom(const TouchData& other) {
634 pointerCount = other.pointerCount;
635 idBits = other.idBits;
Jeff Brown96ad3972011-03-09 17:39:48 -0800636 buttonState = other.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700637
638 for (uint32_t i = 0; i < pointerCount; i++) {
639 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700640
641 int id = pointers[i].id;
642 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700643 }
644 }
645
646 inline void clear() {
647 pointerCount = 0;
648 idBits.clear();
Jeff Brown96ad3972011-03-09 17:39:48 -0800649 buttonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700650 }
651 };
652
Jeff Brown83c09682010-12-23 17:50:18 -0800653 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800654 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brown96ad3972011-03-09 17:39:48 -0800655 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800656
Jeff Brown6d0fec22010-07-23 21:28:06 -0700657 // Immutable configuration parameters.
658 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800659 enum DeviceType {
660 DEVICE_TYPE_TOUCH_SCREEN,
661 DEVICE_TYPE_TOUCH_PAD,
Jeff Brown96ad3972011-03-09 17:39:48 -0800662 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800663 };
664
665 DeviceType deviceType;
666 int32_t associatedDisplayId;
667 bool orientationAware;
668
Jeff Brown6d0fec22010-07-23 21:28:06 -0700669 bool useBadTouchFilter;
670 bool useJumpyTouchFilter;
671 bool useAveragingTouchFilter;
Jeff Brownfe508922011-01-18 15:10:10 -0800672 nsecs_t virtualKeyQuietTime;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700673 } mParameters;
674
Jeff Brown8d608662010-08-30 03:02:23 -0700675 // Immutable calibration parameters in parsed form.
676 struct Calibration {
Jeff Brownc6d282b2010-10-14 21:42:15 -0700677 // Touch Size
678 enum TouchSizeCalibration {
679 TOUCH_SIZE_CALIBRATION_DEFAULT,
680 TOUCH_SIZE_CALIBRATION_NONE,
681 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
682 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700683 };
684
Jeff Brownc6d282b2010-10-14 21:42:15 -0700685 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700686
Jeff Brownc6d282b2010-10-14 21:42:15 -0700687 // Tool Size
688 enum ToolSizeCalibration {
689 TOOL_SIZE_CALIBRATION_DEFAULT,
690 TOOL_SIZE_CALIBRATION_NONE,
691 TOOL_SIZE_CALIBRATION_GEOMETRIC,
692 TOOL_SIZE_CALIBRATION_LINEAR,
693 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700694 };
695
Jeff Brownc6d282b2010-10-14 21:42:15 -0700696 ToolSizeCalibration toolSizeCalibration;
697 bool haveToolSizeLinearScale;
698 float toolSizeLinearScale;
699 bool haveToolSizeLinearBias;
700 float toolSizeLinearBias;
701 bool haveToolSizeAreaScale;
702 float toolSizeAreaScale;
703 bool haveToolSizeAreaBias;
704 float toolSizeAreaBias;
705 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800706 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700707
708 // Pressure
709 enum PressureCalibration {
710 PRESSURE_CALIBRATION_DEFAULT,
711 PRESSURE_CALIBRATION_NONE,
712 PRESSURE_CALIBRATION_PHYSICAL,
713 PRESSURE_CALIBRATION_AMPLITUDE,
714 };
715 enum PressureSource {
716 PRESSURE_SOURCE_DEFAULT,
717 PRESSURE_SOURCE_PRESSURE,
718 PRESSURE_SOURCE_TOUCH,
719 };
720
721 PressureCalibration pressureCalibration;
722 PressureSource pressureSource;
723 bool havePressureScale;
724 float pressureScale;
725
726 // Size
727 enum SizeCalibration {
728 SIZE_CALIBRATION_DEFAULT,
729 SIZE_CALIBRATION_NONE,
730 SIZE_CALIBRATION_NORMALIZED,
731 };
732
733 SizeCalibration sizeCalibration;
734
735 // Orientation
736 enum OrientationCalibration {
737 ORIENTATION_CALIBRATION_DEFAULT,
738 ORIENTATION_CALIBRATION_NONE,
739 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800740 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700741 };
742
743 OrientationCalibration orientationCalibration;
744 } mCalibration;
745
746 // Raw axis information from the driver.
747 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700748 RawAbsoluteAxisInfo x;
749 RawAbsoluteAxisInfo y;
750 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700751 RawAbsoluteAxisInfo touchMajor;
752 RawAbsoluteAxisInfo touchMinor;
753 RawAbsoluteAxisInfo toolMajor;
754 RawAbsoluteAxisInfo toolMinor;
755 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700756 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700757
Jeff Brown6328cdc2010-07-29 18:18:33 -0700758 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700759 TouchData mCurrentTouch;
Jeff Brown96ad3972011-03-09 17:39:48 -0800760 PointerCoords mCurrentTouchCoords[MAX_POINTERS];
761
Jeff Brown6d0fec22010-07-23 21:28:06 -0700762 TouchData mLastTouch;
Jeff Brown96ad3972011-03-09 17:39:48 -0800763 PointerCoords mLastTouchCoords[MAX_POINTERS];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700764
765 // The time the primary pointer last went down.
766 nsecs_t mDownTime;
767
Jeff Brown96ad3972011-03-09 17:39:48 -0800768 // The pointer controller, or null if the device is not a pointer.
769 sp<PointerControllerInterface> mPointerController;
770
Jeff Brown6328cdc2010-07-29 18:18:33 -0700771 struct LockedState {
772 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700773
Jeff Brown6328cdc2010-07-29 18:18:33 -0700774 // The surface orientation and width and height set by configureSurfaceLocked().
775 int32_t surfaceOrientation;
776 int32_t surfaceWidth, surfaceHeight;
777
Jeff Brownefd32662011-03-08 15:13:06 -0800778 // The associated display orientation and width and height set by configureSurfaceLocked().
779 int32_t associatedDisplayOrientation;
780 int32_t associatedDisplayWidth, associatedDisplayHeight;
781
Jeff Brown6328cdc2010-07-29 18:18:33 -0700782 // Translation and scaling factors, orientation-independent.
Jeff Brown6328cdc2010-07-29 18:18:33 -0700783 float xScale;
784 float xPrecision;
785
Jeff Brown6328cdc2010-07-29 18:18:33 -0700786 float yScale;
787 float yPrecision;
788
Jeff Brown8d608662010-08-30 03:02:23 -0700789 float geometricScale;
790
Jeff Brownc6d282b2010-10-14 21:42:15 -0700791 float toolSizeLinearScale;
792 float toolSizeLinearBias;
793 float toolSizeAreaScale;
794 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700795
Jeff Brown6328cdc2010-07-29 18:18:33 -0700796 float pressureScale;
797
Jeff Brown6328cdc2010-07-29 18:18:33 -0700798 float sizeScale;
799
800 float orientationScale;
801
802 // Oriented motion ranges for input device info.
803 struct OrientedRanges {
804 InputDeviceInfo::MotionRange x;
805 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700806
807 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700808 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700809
810 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700811 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700812
Jeff Brownc6d282b2010-10-14 21:42:15 -0700813 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700814 InputDeviceInfo::MotionRange touchMajor;
815 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700816
Jeff Brownc6d282b2010-10-14 21:42:15 -0700817 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700818 InputDeviceInfo::MotionRange toolMajor;
819 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700820
821 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700822 InputDeviceInfo::MotionRange orientation;
823 } orientedRanges;
824
825 // Oriented dimensions and precision.
826 float orientedSurfaceWidth, orientedSurfaceHeight;
827 float orientedXPrecision, orientedYPrecision;
828
829 struct CurrentVirtualKeyState {
830 bool down;
831 nsecs_t downTime;
832 int32_t keyCode;
833 int32_t scanCode;
834 } currentVirtualKey;
Jeff Brown96ad3972011-03-09 17:39:48 -0800835
836 // Scale factor for gesture based pointer movements.
837 float pointerGestureXMovementScale;
838 float pointerGestureYMovementScale;
839
840 // Scale factor for gesture based zooming and other freeform motions.
841 float pointerGestureXZoomScale;
842 float pointerGestureYZoomScale;
843
844 // The maximum swipe width squared.
845 int32_t pointerGestureMaxSwipeWidthSquared;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700846 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700847
Jeff Brown8d608662010-08-30 03:02:23 -0700848 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700849 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700850 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700851 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700852 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700853 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700854 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700855 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700856 virtual void parseCalibration();
857 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700858 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700859
860 enum TouchResult {
861 // Dispatch the touch normally.
862 DISPATCH_TOUCH,
863 // Do not dispatch the touch, but keep tracking the current stroke.
864 SKIP_TOUCH,
865 // Do not dispatch the touch, and drop all information associated with the current stoke
866 // so the next movement will appear as a new down.
867 DROP_STROKE
868 };
869
870 void syncTouch(nsecs_t when, bool havePointerIds);
871
872private:
873 /* Maximum number of historical samples to average. */
874 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
875
876 /* Slop distance for jumpy pointer detection.
877 * The vertical range of the screen divided by this is our epsilon value. */
878 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
879
880 /* Number of jumpy points to drop for touchscreens that need it. */
881 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
882 static const uint32_t JUMPY_DROP_LIMIT = 3;
883
884 /* Maximum squared distance for averaging.
885 * If moving farther than this, turn of averaging to avoid lag in response. */
886 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
887
888 struct AveragingTouchFilterState {
889 // Individual history tracks are stored by pointer id
890 uint32_t historyStart[MAX_POINTERS];
891 uint32_t historyEnd[MAX_POINTERS];
892 struct {
893 struct {
894 int32_t x;
895 int32_t y;
896 int32_t pressure;
897 } pointers[MAX_POINTERS];
898 } historyData[AVERAGING_HISTORY_SIZE];
899 } mAveragingTouchFilter;
900
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700901 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700902 uint32_t jumpyPointsDropped;
903 } mJumpyTouchFilter;
904
905 struct PointerDistanceHeapElement {
906 uint32_t currentPointerIndex : 8;
907 uint32_t lastPointerIndex : 8;
908 uint64_t distance : 48; // squared distance
909 };
910
Jeff Brown96ad3972011-03-09 17:39:48 -0800911 struct PointerGesture {
912 enum Mode {
913 // No fingers, button is not pressed.
914 // Nothing happening.
915 NEUTRAL,
916
917 // No fingers, button is not pressed.
918 // Tap detected.
919 // Emits DOWN and UP events at the pointer location.
920 TAP,
921
922 // Button is pressed.
923 // Pointer follows the active finger if there is one. Other fingers are ignored.
924 // Emits DOWN, MOVE and UP events at the pointer location.
925 CLICK_OR_DRAG,
926
927 // Exactly one finger, button is not pressed.
928 // Pointer follows the active finger.
929 // Emits HOVER_MOVE events at the pointer location.
930 HOVER,
931
932 // More than two fingers involved but they haven't moved enough for us
933 // to figure out what is intended.
934 INDETERMINATE_MULTITOUCH,
935
936 // Exactly two fingers moving in the same direction, button is not pressed.
937 // Pointer does not move.
938 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
939 // follows the midpoint between both fingers.
940 // The centroid is fixed when entering this state.
941 SWIPE,
942
943 // Two or more fingers moving in arbitrary directions, button is not pressed.
944 // Pointer does not move.
945 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
946 // each finger individually relative to the initial centroid of the finger.
947 // The centroid is fixed when entering this state.
948 FREEFORM,
949
950 // Waiting for quiet time to end before starting the next gesture.
951 QUIET,
952 };
953
954 // The active pointer id from the raw touch data.
955 int32_t activeTouchId; // -1 if none
956
957 // The active pointer id from the gesture last delivered to the application.
958 int32_t activeGestureId; // -1 if none
959
960 // Pointer coords and ids for the current and previous pointer gesture.
961 Mode currentGestureMode;
962 uint32_t currentGesturePointerCount;
963 BitSet32 currentGestureIdBits;
964 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
965 PointerCoords currentGestureCoords[MAX_POINTERS];
966
967 Mode lastGestureMode;
968 uint32_t lastGesturePointerCount;
969 BitSet32 lastGestureIdBits;
970 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
971 PointerCoords lastGestureCoords[MAX_POINTERS];
972
973 // Tracks for all pointers originally went down.
974 TouchData touchOrigin;
975
976 // Describes how touch ids are mapped to gesture ids for freeform gestures.
977 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
978
979 // Initial centroid of the movement.
980 // Used to calculate how far the touch pointers have moved since the gesture started.
981 int32_t initialCentroidX;
982 int32_t initialCentroidY;
983
984 // Initial pointer location.
985 // Used to track where the pointer was when the gesture started.
986 float initialPointerX;
987 float initialPointerY;
988
989 // Time the pointer gesture last went down.
990 nsecs_t downTime;
991
992 // Time we started waiting for a tap gesture.
993 nsecs_t tapTime;
994
995 // Time we started waiting for quiescence.
996 nsecs_t quietTime;
997
998 // A velocity tracker for determining whether to switch active pointers during drags.
999 VelocityTracker velocityTracker;
1000
1001 void reset() {
1002 activeTouchId = -1;
1003 activeGestureId = -1;
1004 currentGestureMode = NEUTRAL;
1005 currentGesturePointerCount = 0;
1006 currentGestureIdBits.clear();
1007 lastGestureMode = NEUTRAL;
1008 lastGesturePointerCount = 0;
1009 lastGestureIdBits.clear();
1010 touchOrigin.clear();
1011 initialCentroidX = 0;
1012 initialCentroidY = 0;
1013 initialPointerX = 0;
1014 initialPointerY = 0;
1015 downTime = 0;
1016 velocityTracker.clear();
1017 resetTapTime();
1018 resetQuietTime();
1019 }
1020
1021 void resetTapTime() {
1022 tapTime = LLONG_MIN;
1023 }
1024
1025 void resetQuietTime() {
1026 quietTime = LLONG_MIN;
1027 }
1028 } mPointerGesture;
1029
Jeff Brown6328cdc2010-07-29 18:18:33 -07001030 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031
1032 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
1033 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brown96ad3972011-03-09 17:39:48 -08001034 void prepareTouches(int32_t* outEdgeFlags, float* outXPrecision, float* outYPrecision);
1035 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags);
1036 void preparePointerGestures(nsecs_t when,
1037 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture);
1038
1039 // Dispatches a motion event.
1040 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1041 // method will take care of setting the index and transmuting the action to DOWN or UP
1042 // it is the first / last pointer to go down / up.
1043 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
1044 int32_t action, int32_t flags, uint32_t metaState, int32_t edgeFlags,
1045 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
1046 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1047
1048 // Updates pointer coords for pointers with specified ids that have moved.
1049 // Returns true if any of them changed.
1050 bool updateMovedPointerCoords(const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1051 PointerCoords* outCoords, const uint32_t* outIdToIndex, BitSet32 idBits) const;
1052
Jeff Brownefd32662011-03-08 15:13:06 -08001053 void suppressSwipeOntoVirtualKeys(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001054
Jeff Brown6328cdc2010-07-29 18:18:33 -07001055 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
1056 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001057
1058 bool applyBadTouchFilter();
1059 bool applyJumpyTouchFilter();
1060 void applyAveragingTouchFilter();
1061 void calculatePointerIds();
1062};
1063
1064
1065class SingleTouchInputMapper : public TouchInputMapper {
1066public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001067 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001068 virtual ~SingleTouchInputMapper();
1069
1070 virtual void reset();
1071 virtual void process(const RawEvent* rawEvent);
1072
1073protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001074 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001075
1076private:
1077 struct Accumulator {
1078 enum {
1079 FIELD_BTN_TOUCH = 1,
1080 FIELD_ABS_X = 2,
1081 FIELD_ABS_Y = 4,
1082 FIELD_ABS_PRESSURE = 8,
Jeff Brownefd32662011-03-08 15:13:06 -08001083 FIELD_ABS_TOOL_WIDTH = 16,
Jeff Brown96ad3972011-03-09 17:39:48 -08001084 FIELD_BUTTONS = 32,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001085 };
1086
1087 uint32_t fields;
1088
1089 bool btnTouch;
1090 int32_t absX;
1091 int32_t absY;
1092 int32_t absPressure;
1093 int32_t absToolWidth;
1094
Jeff Brown96ad3972011-03-09 17:39:48 -08001095 uint32_t buttonDown;
1096 uint32_t buttonUp;
1097
Jeff Brown6d0fec22010-07-23 21:28:06 -07001098 inline void clear() {
1099 fields = 0;
Jeff Brown96ad3972011-03-09 17:39:48 -08001100 buttonDown = 0;
1101 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001102 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001103 } mAccumulator;
1104
1105 bool mDown;
1106 int32_t mX;
1107 int32_t mY;
1108 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07001109 int32_t mToolWidth;
Jeff Brown96ad3972011-03-09 17:39:48 -08001110 uint32_t mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001111
1112 void initialize();
1113
1114 void sync(nsecs_t when);
1115};
1116
1117
1118class MultiTouchInputMapper : public TouchInputMapper {
1119public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001120 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001121 virtual ~MultiTouchInputMapper();
1122
1123 virtual void reset();
1124 virtual void process(const RawEvent* rawEvent);
1125
1126protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001127 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001128
1129private:
1130 struct Accumulator {
1131 enum {
1132 FIELD_ABS_MT_POSITION_X = 1,
1133 FIELD_ABS_MT_POSITION_Y = 2,
1134 FIELD_ABS_MT_TOUCH_MAJOR = 4,
1135 FIELD_ABS_MT_TOUCH_MINOR = 8,
1136 FIELD_ABS_MT_WIDTH_MAJOR = 16,
1137 FIELD_ABS_MT_WIDTH_MINOR = 32,
1138 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001139 FIELD_ABS_MT_TRACKING_ID = 128,
1140 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001141 };
1142
1143 uint32_t pointerCount;
1144 struct Pointer {
1145 uint32_t fields;
1146
1147 int32_t absMTPositionX;
1148 int32_t absMTPositionY;
1149 int32_t absMTTouchMajor;
1150 int32_t absMTTouchMinor;
1151 int32_t absMTWidthMajor;
1152 int32_t absMTWidthMinor;
1153 int32_t absMTOrientation;
1154 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001155 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001156
1157 inline void clear() {
1158 fields = 0;
1159 }
1160 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
1161
Jeff Brown96ad3972011-03-09 17:39:48 -08001162 // Bitfield of buttons that went down or up.
1163 uint32_t buttonDown;
1164 uint32_t buttonUp;
1165
Jeff Brown6d0fec22010-07-23 21:28:06 -07001166 inline void clear() {
1167 pointerCount = 0;
1168 pointers[0].clear();
Jeff Brown96ad3972011-03-09 17:39:48 -08001169 buttonDown = 0;
1170 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001171 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001172 } mAccumulator;
1173
Jeff Brown96ad3972011-03-09 17:39:48 -08001174 uint32_t mButtonState;
1175
Jeff Brown6d0fec22010-07-23 21:28:06 -07001176 void initialize();
1177
1178 void sync(nsecs_t when);
1179};
1180
Jeff Browncb1404e2011-01-15 18:14:15 -08001181
1182class JoystickInputMapper : public InputMapper {
1183public:
1184 JoystickInputMapper(InputDevice* device);
1185 virtual ~JoystickInputMapper();
1186
1187 virtual uint32_t getSources();
1188 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1189 virtual void dump(String8& dump);
1190 virtual void configure();
1191 virtual void reset();
1192 virtual void process(const RawEvent* rawEvent);
1193
1194private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001195 struct Axis {
1196 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001197 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001198
Jeff Brown6f2fba42011-02-19 01:08:02 -08001199 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001200
Jeff Brown6f2fba42011-02-19 01:08:02 -08001201 float scale; // scale factor from raw to normalized values
1202 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001203 float highScale; // scale factor from raw to normalized values of high split
1204 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001205
Jeff Brown6f2fba42011-02-19 01:08:02 -08001206 float min; // normalized inclusive minimum
1207 float max; // normalized inclusive maximum
1208 float flat; // normalized flat region size
1209 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001210
Jeff Brown6f2fba42011-02-19 01:08:02 -08001211 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001212 float currentValue; // current value
1213 float newValue; // most recent value
1214 float highCurrentValue; // current value of high split
1215 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001216
Jeff Brown85297452011-03-04 13:07:49 -08001217 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1218 bool explicitlyMapped, float scale, float offset,
1219 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001220 float min, float max, float flat, float fuzz) {
1221 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001222 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001223 this->explicitlyMapped = explicitlyMapped;
1224 this->scale = scale;
1225 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001226 this->highScale = highScale;
1227 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001228 this->min = min;
1229 this->max = max;
1230 this->flat = flat;
1231 this->fuzz = fuzz;
1232 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001233 resetValue();
1234 }
1235
1236 void resetValue() {
1237 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001238 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001239 this->highCurrentValue = 0;
1240 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001241 }
1242 };
1243
Jeff Brown6f2fba42011-02-19 01:08:02 -08001244 // Axes indexed by raw ABS_* axis index.
1245 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001246
Jeff Brown6f2fba42011-02-19 01:08:02 -08001247 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001248
Jeff Brown85297452011-03-04 13:07:49 -08001249 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001250 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001251 bool filterAxes(bool force);
1252
1253 static bool hasValueChangedSignificantly(float filter,
1254 float newValue, float currentValue, float min, float max);
1255 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1256 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001257
Jeff Brown6f2fba42011-02-19 01:08:02 -08001258 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001259};
1260
Jeff Brown46b9ac02010-04-22 18:58:52 -07001261} // namespace android
1262
1263#endif // _UI_INPUT_READER_H