blob: 62ac4b282289630d11b63c21c04945e6d19a1705 [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
Jeff Brownb4ff35d2011-01-02 16:37:43 -080020#include "EventHub.h"
21#include "InputDispatcher.h"
22#include "PointerController.h"
23
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31#include <utils/BitSet.h>
32
33#include <stddef.h>
34#include <unistd.h>
35
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
43 * Input reader policy interface.
44 *
45 * The input reader policy is used by the input reader to interact with the Window Manager
46 * and other system components.
47 *
48 * The actual implementation is partially supported by callbacks into the DVM
49 * via JNI. This interface is also mocked in the unit tests.
50 */
51class InputReaderPolicyInterface : public virtual RefBase {
52protected:
53 InputReaderPolicyInterface() { }
54 virtual ~InputReaderPolicyInterface() { }
55
56public:
57 /* Display orientations. */
58 enum {
59 ROTATION_0 = 0,
60 ROTATION_90 = 1,
61 ROTATION_180 = 2,
62 ROTATION_270 = 3
63 };
64
Jeff Brown9c3cda02010-06-15 01:31:58 -070065 /* Gets information about the display with the specified id.
66 * Returns true if the display info is available, false otherwise.
67 */
68 virtual bool getDisplayInfo(int32_t displayId,
69 int32_t* width, int32_t* height, int32_t* orientation) = 0;
70
Jeff Brown9c3cda02010-06-15 01:31:58 -070071 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
72 * certain device whose screen currently is not all that good.
73 */
74 virtual bool filterTouchEvents() = 0;
75
76 /* Determines whether to turn on some hacks to improve touch interaction with another device
77 * where touch coordinate data can get corrupted.
78 */
79 virtual bool filterJumpyTouchEvents() = 0;
80
Jeff Brownfe508922011-01-18 15:10:10 -080081 /* Gets the amount of time to disable virtual keys after the screen is touched
82 * in order to filter out accidental virtual key presses due to swiping gestures
83 * or taps near the edge of the display. May be 0 to disable the feature.
84 */
85 virtual nsecs_t getVirtualKeyQuietTime() = 0;
86
Jeff Brown9c3cda02010-06-15 01:31:58 -070087 /* Gets the excluded device names for the platform. */
88 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -080089
90 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
91 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070092};
93
94
95/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070096class InputReaderInterface : public virtual RefBase {
97protected:
98 InputReaderInterface() { }
99 virtual ~InputReaderInterface() { }
100
101public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700102 /* Dumps the state of the input reader.
103 *
104 * This method may be called on any thread (usually by the input manager). */
105 virtual void dump(String8& dump) = 0;
106
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700107 /* Runs a single iteration of the processing loop.
108 * Nominally reads and processes one incoming message from the EventHub.
109 *
110 * This method should be called on the input reader thread.
111 */
112 virtual void loopOnce() = 0;
113
Jeff Brown9c3cda02010-06-15 01:31:58 -0700114 /* Gets the current input device configuration.
115 *
116 * This method may be called on any thread (usually by the input manager).
117 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700118 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700119
Jeff Brown6d0fec22010-07-23 21:28:06 -0700120 /* Gets information about the specified input device.
121 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
122 * was no such device.
123 *
124 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700125 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700126 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
127
128 /* Gets the list of all registered device ids. */
129 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
130
131 /* Query current input state. */
132 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
133 int32_t scanCode) = 0;
134 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
135 int32_t keyCode) = 0;
136 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
137 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700138
139 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
142};
143
144
145/* Internal interface used by individual input devices to access global input device state
146 * and parameters maintained by the input reader.
147 */
148class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700149public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150 InputReaderContext() { }
151 virtual ~InputReaderContext() { }
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153 virtual void updateGlobalMetaState() = 0;
154 virtual int32_t getGlobalMetaState() = 0;
155
Jeff Brownfe508922011-01-18 15:10:10 -0800156 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
157 virtual bool shouldDropVirtualKey(nsecs_t now,
158 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
159
Jeff Brown05dc66a2011-03-02 14:41:58 -0800160 virtual void fadePointer() = 0;
161
Jeff Brownaa3855d2011-03-17 01:34:19 -0700162 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
163
Jeff Brown6d0fec22010-07-23 21:28:06 -0700164 virtual InputReaderPolicyInterface* getPolicy() = 0;
165 virtual InputDispatcherInterface* getDispatcher() = 0;
166 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700167};
168
Jeff Brown9c3cda02010-06-15 01:31:58 -0700169
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700170/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700171 * that it sends to the input dispatcher. Some functions of the input reader, such as early
172 * event filtering in low power states, are controlled by a separate policy object.
173 *
174 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700175 * Because the policy and dispatcher can potentially block or cause re-entrance into
176 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700177 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700178 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700179class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700180public:
181 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700182 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700183 const sp<InputDispatcherInterface>& dispatcher);
184 virtual ~InputReader();
185
Jeff Brownb88102f2010-09-08 11:49:43 -0700186 virtual void dump(String8& dump);
187
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700188 virtual void loopOnce();
189
Jeff Brown6d0fec22010-07-23 21:28:06 -0700190 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700191
Jeff Brown6d0fec22010-07-23 21:28:06 -0700192 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
193 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700194
Jeff Brown6d0fec22010-07-23 21:28:06 -0700195 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
196 int32_t scanCode);
197 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
198 int32_t keyCode);
199 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
200 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700201
Jeff Brown6d0fec22010-07-23 21:28:06 -0700202 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
203 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204
Jeff Brownc3db8582010-10-20 15:33:38 -0700205protected:
206 // These methods are protected virtual so they can be overridden and instrumented
207 // by test cases.
208 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
209
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700210private:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700211 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700213 sp<InputDispatcherInterface> mDispatcher;
214
Jeff Brown6d0fec22010-07-23 21:28:06 -0700215 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
216 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
217 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
218
Jeff Brownb7198742011-03-18 18:14:26 -0700219 // The event queue.
220 static const int EVENT_BUFFER_SIZE = 256;
221 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
222
Jeff Brown6d0fec22010-07-23 21:28:06 -0700223 // This reader/writer lock guards the list of input devices.
224 // The writer lock must be held whenever the list of input devices is modified
225 // and then promptly released.
226 // The reader lock must be held whenever the list of input devices is traversed or an
227 // input device in the list is accessed.
228 // This lock only protects the registry and prevents inadvertent deletion of device objects
229 // that are in use. Individual devices are responsible for guarding their own internal state
230 // as needed for concurrent operation.
231 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700232 KeyedVector<int32_t, InputDevice*> mDevices;
233
Jeff Brown6d0fec22010-07-23 21:28:06 -0700234 // low-level input event decoding and device management
Jeff Brownb7198742011-03-18 18:14:26 -0700235 void processEvents(const RawEvent* rawEvents, size_t count);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700236
Jeff Brown7342bb92010-10-01 18:55:43 -0700237 void addDevice(int32_t deviceId);
238 void removeDevice(int32_t deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700239 void processEventsForDevice(int32_t deviceId, const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700240 void timeoutExpired(nsecs_t when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700241
Jeff Brownc3db8582010-10-20 15:33:38 -0700242 void handleConfigurationChanged(nsecs_t when);
Jeff Brownb7198742011-03-18 18:14:26 -0700243 void configureExcludedDevices();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700244
Jeff Brown6d0fec22010-07-23 21:28:06 -0700245 // state management for all devices
246 Mutex mStateLock;
247
248 int32_t mGlobalMetaState;
249 virtual void updateGlobalMetaState();
250 virtual int32_t getGlobalMetaState();
251
Jeff Brown05dc66a2011-03-02 14:41:58 -0800252 virtual void fadePointer();
253
Jeff Brown6d0fec22010-07-23 21:28:06 -0700254 InputConfiguration mInputConfiguration;
255 void updateInputConfiguration();
256
Jeff Brownb7198742011-03-18 18:14:26 -0700257 nsecs_t mDisableVirtualKeysTimeout; // only accessed by reader thread
Jeff Brownfe508922011-01-18 15:10:10 -0800258 virtual void disableVirtualKeysUntil(nsecs_t time);
259 virtual bool shouldDropVirtualKey(nsecs_t now,
260 InputDevice* device, int32_t keyCode, int32_t scanCode);
261
Jeff Brownb7198742011-03-18 18:14:26 -0700262 nsecs_t mNextTimeout; // only accessed by reader thread
Jeff Brownaa3855d2011-03-17 01:34:19 -0700263 virtual void requestTimeoutAtTime(nsecs_t when);
264
Jeff Brown6d0fec22010-07-23 21:28:06 -0700265 // state queries
266 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
267 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
268 GetStateFunc getStateFunc);
269 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
270 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700271};
272
273
274/* Reads raw events from the event hub and processes them, endlessly. */
275class InputReaderThread : public Thread {
276public:
277 InputReaderThread(const sp<InputReaderInterface>& reader);
278 virtual ~InputReaderThread();
279
280private:
281 sp<InputReaderInterface> mReader;
282
283 virtual bool threadLoop();
284};
285
Jeff Brown6d0fec22010-07-23 21:28:06 -0700286
287/* Represents the state of a single input device. */
288class InputDevice {
289public:
290 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
291 ~InputDevice();
292
293 inline InputReaderContext* getContext() { return mContext; }
294 inline int32_t getId() { return mId; }
295 inline const String8& getName() { return mName; }
296 inline uint32_t getSources() { return mSources; }
297
Jeff Brown56194eb2011-03-02 19:23:13 -0800298 inline bool isExternal() { return mIsExternal; }
299 inline void setExternal(bool external) { mIsExternal = external; }
300
Jeff Brown6d0fec22010-07-23 21:28:06 -0700301 inline bool isIgnored() { return mMappers.isEmpty(); }
302
Jeff Brownef3d7e82010-09-30 14:33:04 -0700303 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700304 void addMapper(InputMapper* mapper);
305 void configure();
306 void reset();
Jeff Brownb7198742011-03-18 18:14:26 -0700307 void process(const RawEvent* rawEvents, size_t count);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700308 void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309
310 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
311 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
312 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
313 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
314 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
315 const int32_t* keyCodes, uint8_t* outFlags);
316
317 int32_t getMetaState();
318
Jeff Brown05dc66a2011-03-02 14:41:58 -0800319 void fadePointer();
320
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800321 inline const PropertyMap& getConfiguration() {
322 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700323 }
324
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325private:
326 InputReaderContext* mContext;
327 int32_t mId;
328
329 Vector<InputMapper*> mMappers;
330
331 String8 mName;
332 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800333 bool mIsExternal;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700334
335 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
336 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700337
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800338 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700339};
340
341
342/* An input mapper transforms raw input events into cooked event data.
343 * A single input device can have multiple associated input mappers in order to interpret
344 * different classes of events.
345 */
346class InputMapper {
347public:
348 InputMapper(InputDevice* device);
349 virtual ~InputMapper();
350
351 inline InputDevice* getDevice() { return mDevice; }
352 inline int32_t getDeviceId() { return mDevice->getId(); }
353 inline const String8 getDeviceName() { return mDevice->getName(); }
354 inline InputReaderContext* getContext() { return mContext; }
355 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
356 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
357 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
358
359 virtual uint32_t getSources() = 0;
360 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700361 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 virtual void configure();
363 virtual void reset();
364 virtual void process(const RawEvent* rawEvent) = 0;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700365 virtual void timeoutExpired(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366
367 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
368 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
369 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
370 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
371 const int32_t* keyCodes, uint8_t* outFlags);
372
373 virtual int32_t getMetaState();
374
Jeff Brown05dc66a2011-03-02 14:41:58 -0800375 virtual void fadePointer();
376
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377protected:
378 InputDevice* mDevice;
379 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800380
381 static void dumpRawAbsoluteAxisInfo(String8& dump,
382 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383};
384
385
386class SwitchInputMapper : public InputMapper {
387public:
388 SwitchInputMapper(InputDevice* device);
389 virtual ~SwitchInputMapper();
390
391 virtual uint32_t getSources();
392 virtual void process(const RawEvent* rawEvent);
393
394 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
395
396private:
397 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
398};
399
400
401class KeyboardInputMapper : public InputMapper {
402public:
Jeff Brownefd32662011-03-08 15:13:06 -0800403 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404 virtual ~KeyboardInputMapper();
405
406 virtual uint32_t getSources();
407 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700408 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800409 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 virtual void reset();
411 virtual void process(const RawEvent* rawEvent);
412
413 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
414 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
415 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
416 const int32_t* keyCodes, uint8_t* outFlags);
417
418 virtual int32_t getMetaState();
419
420private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700421 Mutex mLock;
422
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 struct KeyDown {
424 int32_t keyCode;
425 int32_t scanCode;
426 };
427
Jeff Brownefd32662011-03-08 15:13:06 -0800428 uint32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700429 int32_t mKeyboardType;
430
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800431 // Immutable configuration parameters.
432 struct Parameters {
433 int32_t associatedDisplayId;
434 bool orientationAware;
435 } mParameters;
436
Jeff Brown6328cdc2010-07-29 18:18:33 -0700437 struct LockedState {
438 Vector<KeyDown> keyDowns; // keys that are down
439 int32_t metaState;
440 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700441
442 struct LedState {
443 bool avail; // led is available
444 bool on; // we think the led is currently on
445 };
446 LedState capsLockLedState;
447 LedState numLockLedState;
448 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700449 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700450
Jeff Brown6328cdc2010-07-29 18:18:33 -0700451 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800453 void configureParameters();
454 void dumpParameters(String8& dump);
455
Jeff Brown6d0fec22010-07-23 21:28:06 -0700456 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700457
Jeff Brown6d0fec22010-07-23 21:28:06 -0700458 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
459 uint32_t policyFlags);
460
Jeff Brown6328cdc2010-07-29 18:18:33 -0700461 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700462
Jeff Brown49ed71d2010-12-06 17:13:33 -0800463 void resetLedStateLocked();
464 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700465 void updateLedStateLocked(bool reset);
466 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
467 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700468};
469
470
Jeff Brown83c09682010-12-23 17:50:18 -0800471class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472public:
Jeff Brown83c09682010-12-23 17:50:18 -0800473 CursorInputMapper(InputDevice* device);
474 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700475
476 virtual uint32_t getSources();
477 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700478 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800479 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700480 virtual void reset();
481 virtual void process(const RawEvent* rawEvent);
482
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700483 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
484
Jeff Brown05dc66a2011-03-02 14:41:58 -0800485 virtual void fadePointer();
486
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487private:
488 // Amount that trackball needs to move in order to generate a key event.
489 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
490
Jeff Brown6328cdc2010-07-29 18:18:33 -0700491 Mutex mLock;
492
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800493 // Immutable configuration parameters.
494 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800495 enum Mode {
496 MODE_POINTER,
497 MODE_NAVIGATION,
498 };
499
500 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800501 int32_t associatedDisplayId;
502 bool orientationAware;
503 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504
505 struct Accumulator {
506 enum {
Jeff Brownefd32662011-03-08 15:13:06 -0800507 FIELD_BUTTONS = 1,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700508 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800509 FIELD_REL_Y = 4,
510 FIELD_REL_WHEEL = 8,
511 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512 };
513
514 uint32_t fields;
515
Jeff Brownefd32662011-03-08 15:13:06 -0800516 uint32_t buttonDown;
517 uint32_t buttonUp;
518
Jeff Brown6d0fec22010-07-23 21:28:06 -0700519 int32_t relX;
520 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800521 int32_t relWheel;
522 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700523
524 inline void clear() {
525 fields = 0;
526 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527 } mAccumulator;
528
Jeff Brownefd32662011-03-08 15:13:06 -0800529 int32_t mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700530 float mXScale;
531 float mYScale;
532 float mXPrecision;
533 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800534
535 bool mHaveVWheel;
536 bool mHaveHWheel;
537 float mVWheelScale;
538 float mHWheelScale;
539
Jeff Brown83c09682010-12-23 17:50:18 -0800540 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700541
Jeff Brown6328cdc2010-07-29 18:18:33 -0700542 struct LockedState {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700543 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700544 nsecs_t downTime;
545 } mLocked;
546
547 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700548
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800549 void configureParameters();
550 void dumpParameters(String8& dump);
551
Jeff Brown6d0fec22010-07-23 21:28:06 -0700552 void sync(nsecs_t when);
553};
554
555
556class TouchInputMapper : public InputMapper {
557public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800558 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700559 virtual ~TouchInputMapper();
560
561 virtual uint32_t getSources();
562 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700563 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700564 virtual void configure();
565 virtual void reset();
566
567 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
568 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
569 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
570 const int32_t* keyCodes, uint8_t* outFlags);
571
Jeff Brownace13b12011-03-09 17:39:48 -0800572 virtual void fadePointer();
Jeff Brown79ac9692011-04-19 21:20:10 -0700573 virtual void timeoutExpired(nsecs_t when);
Jeff Brownace13b12011-03-09 17:39:48 -0800574
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700576 Mutex mLock;
577
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578 struct VirtualKey {
579 int32_t keyCode;
580 int32_t scanCode;
581 uint32_t flags;
582
583 // computed hit box, specified in touch screen coords based on known display size
584 int32_t hitLeft;
585 int32_t hitTop;
586 int32_t hitRight;
587 int32_t hitBottom;
588
589 inline bool isHit(int32_t x, int32_t y) const {
590 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
591 }
592 };
593
Jeff Brown8d608662010-08-30 03:02:23 -0700594 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595 struct PointerData {
596 uint32_t id;
597 int32_t x;
598 int32_t y;
599 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 int32_t touchMajor;
601 int32_t touchMinor;
602 int32_t toolMajor;
603 int32_t toolMinor;
604 int32_t orientation;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700605 bool isStylus;
Jeff Brownc3db8582010-10-20 15:33:38 -0700606
607 inline bool operator== (const PointerData& other) const {
608 return id == other.id
609 && x == other.x
610 && y == other.y
611 && pressure == other.pressure
612 && touchMajor == other.touchMajor
613 && touchMinor == other.touchMinor
614 && toolMajor == other.toolMajor
615 && toolMinor == other.toolMinor
616 && orientation == other.orientation;
617 }
618 inline bool operator!= (const PointerData& other) const {
619 return !(*this == other);
620 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700621 };
622
Jeff Brown8d608662010-08-30 03:02:23 -0700623 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700624 struct TouchData {
625 uint32_t pointerCount;
626 PointerData pointers[MAX_POINTERS];
627 BitSet32 idBits;
628 uint32_t idToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700629 int32_t buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700630
631 void copyFrom(const TouchData& other) {
632 pointerCount = other.pointerCount;
633 idBits = other.idBits;
Jeff Brownace13b12011-03-09 17:39:48 -0800634 buttonState = other.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635
636 for (uint32_t i = 0; i < pointerCount; i++) {
637 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700638
639 int id = pointers[i].id;
640 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700641 }
642 }
643
644 inline void clear() {
645 pointerCount = 0;
646 idBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -0800647 buttonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700648 }
Jeff Brown2352b972011-04-12 22:39:53 -0700649
650 void getCentroid(float* outX, float* outY) {
651 float x = 0, y = 0;
652 if (pointerCount != 0) {
653 for (uint32_t i = 0; i < pointerCount; i++) {
654 x += pointers[i].x;
655 y += pointers[i].y;
656 }
657 x /= pointerCount;
658 y /= pointerCount;
659 }
660 *outX = x;
661 *outY = y;
662 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663 };
664
Jeff Brown83c09682010-12-23 17:50:18 -0800665 // Input sources supported by the device.
Jeff Brownefd32662011-03-08 15:13:06 -0800666 uint32_t mTouchSource; // sources when reporting touch data
Jeff Brownace13b12011-03-09 17:39:48 -0800667 uint32_t mPointerSource; // sources when reporting pointer gestures
Jeff Brown83c09682010-12-23 17:50:18 -0800668
Jeff Brown6d0fec22010-07-23 21:28:06 -0700669 // Immutable configuration parameters.
670 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800671 enum DeviceType {
672 DEVICE_TYPE_TOUCH_SCREEN,
673 DEVICE_TYPE_TOUCH_PAD,
Jeff Brownace13b12011-03-09 17:39:48 -0800674 DEVICE_TYPE_POINTER,
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800675 };
676
677 DeviceType deviceType;
678 int32_t associatedDisplayId;
679 bool orientationAware;
680
Jeff Brown6d0fec22010-07-23 21:28:06 -0700681 bool useBadTouchFilter;
682 bool useJumpyTouchFilter;
683 bool useAveragingTouchFilter;
Jeff Brownfe508922011-01-18 15:10:10 -0800684 nsecs_t virtualKeyQuietTime;
Jeff Brown2352b972011-04-12 22:39:53 -0700685
686 enum GestureMode {
687 GESTURE_MODE_POINTER,
688 GESTURE_MODE_SPOTS,
689 };
690 GestureMode gestureMode;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700691 } mParameters;
692
Jeff Brown8d608662010-08-30 03:02:23 -0700693 // Immutable calibration parameters in parsed form.
694 struct Calibration {
Jeff Brownc6d282b2010-10-14 21:42:15 -0700695 // Touch Size
696 enum TouchSizeCalibration {
697 TOUCH_SIZE_CALIBRATION_DEFAULT,
698 TOUCH_SIZE_CALIBRATION_NONE,
699 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
700 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700701 };
702
Jeff Brownc6d282b2010-10-14 21:42:15 -0700703 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700704
Jeff Brownc6d282b2010-10-14 21:42:15 -0700705 // Tool Size
706 enum ToolSizeCalibration {
707 TOOL_SIZE_CALIBRATION_DEFAULT,
708 TOOL_SIZE_CALIBRATION_NONE,
709 TOOL_SIZE_CALIBRATION_GEOMETRIC,
710 TOOL_SIZE_CALIBRATION_LINEAR,
711 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700712 };
713
Jeff Brownc6d282b2010-10-14 21:42:15 -0700714 ToolSizeCalibration toolSizeCalibration;
715 bool haveToolSizeLinearScale;
716 float toolSizeLinearScale;
717 bool haveToolSizeLinearBias;
718 float toolSizeLinearBias;
719 bool haveToolSizeAreaScale;
720 float toolSizeAreaScale;
721 bool haveToolSizeAreaBias;
722 float toolSizeAreaBias;
723 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800724 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700725
726 // Pressure
727 enum PressureCalibration {
728 PRESSURE_CALIBRATION_DEFAULT,
729 PRESSURE_CALIBRATION_NONE,
730 PRESSURE_CALIBRATION_PHYSICAL,
731 PRESSURE_CALIBRATION_AMPLITUDE,
732 };
733 enum PressureSource {
734 PRESSURE_SOURCE_DEFAULT,
735 PRESSURE_SOURCE_PRESSURE,
736 PRESSURE_SOURCE_TOUCH,
737 };
738
739 PressureCalibration pressureCalibration;
740 PressureSource pressureSource;
741 bool havePressureScale;
742 float pressureScale;
743
744 // Size
745 enum SizeCalibration {
746 SIZE_CALIBRATION_DEFAULT,
747 SIZE_CALIBRATION_NONE,
748 SIZE_CALIBRATION_NORMALIZED,
749 };
750
751 SizeCalibration sizeCalibration;
752
753 // Orientation
754 enum OrientationCalibration {
755 ORIENTATION_CALIBRATION_DEFAULT,
756 ORIENTATION_CALIBRATION_NONE,
757 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800758 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700759 };
760
761 OrientationCalibration orientationCalibration;
762 } mCalibration;
763
764 // Raw axis information from the driver.
765 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700766 RawAbsoluteAxisInfo x;
767 RawAbsoluteAxisInfo y;
768 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700769 RawAbsoluteAxisInfo touchMajor;
770 RawAbsoluteAxisInfo touchMinor;
771 RawAbsoluteAxisInfo toolMajor;
772 RawAbsoluteAxisInfo toolMinor;
773 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700774 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700775
Jeff Brown6328cdc2010-07-29 18:18:33 -0700776 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700777 TouchData mCurrentTouch;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700778 PointerProperties mCurrentTouchProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -0800779 PointerCoords mCurrentTouchCoords[MAX_POINTERS];
780
Jeff Brown6d0fec22010-07-23 21:28:06 -0700781 TouchData mLastTouch;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700782 PointerProperties mLastTouchProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -0800783 PointerCoords mLastTouchCoords[MAX_POINTERS];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700784
785 // The time the primary pointer last went down.
786 nsecs_t mDownTime;
787
Jeff Brownace13b12011-03-09 17:39:48 -0800788 // The pointer controller, or null if the device is not a pointer.
789 sp<PointerControllerInterface> mPointerController;
790
Jeff Brown6328cdc2010-07-29 18:18:33 -0700791 struct LockedState {
792 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700793
Jeff Brown6328cdc2010-07-29 18:18:33 -0700794 // The surface orientation and width and height set by configureSurfaceLocked().
795 int32_t surfaceOrientation;
796 int32_t surfaceWidth, surfaceHeight;
797
Jeff Brownefd32662011-03-08 15:13:06 -0800798 // The associated display orientation and width and height set by configureSurfaceLocked().
799 int32_t associatedDisplayOrientation;
800 int32_t associatedDisplayWidth, associatedDisplayHeight;
801
Jeff Brown6328cdc2010-07-29 18:18:33 -0700802 // Translation and scaling factors, orientation-independent.
Jeff Brown6328cdc2010-07-29 18:18:33 -0700803 float xScale;
804 float xPrecision;
805
Jeff Brown6328cdc2010-07-29 18:18:33 -0700806 float yScale;
807 float yPrecision;
808
Jeff Brown8d608662010-08-30 03:02:23 -0700809 float geometricScale;
810
Jeff Brownc6d282b2010-10-14 21:42:15 -0700811 float toolSizeLinearScale;
812 float toolSizeLinearBias;
813 float toolSizeAreaScale;
814 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700815
Jeff Brown6328cdc2010-07-29 18:18:33 -0700816 float pressureScale;
817
Jeff Brown6328cdc2010-07-29 18:18:33 -0700818 float sizeScale;
819
820 float orientationScale;
821
822 // Oriented motion ranges for input device info.
823 struct OrientedRanges {
824 InputDeviceInfo::MotionRange x;
825 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700826
827 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700828 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700829
830 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700831 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700832
Jeff Brownc6d282b2010-10-14 21:42:15 -0700833 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700834 InputDeviceInfo::MotionRange touchMajor;
835 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700836
Jeff Brownc6d282b2010-10-14 21:42:15 -0700837 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700838 InputDeviceInfo::MotionRange toolMajor;
839 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700840
841 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700842 InputDeviceInfo::MotionRange orientation;
843 } orientedRanges;
844
845 // Oriented dimensions and precision.
846 float orientedSurfaceWidth, orientedSurfaceHeight;
847 float orientedXPrecision, orientedYPrecision;
848
849 struct CurrentVirtualKeyState {
850 bool down;
851 nsecs_t downTime;
852 int32_t keyCode;
853 int32_t scanCode;
854 } currentVirtualKey;
Jeff Brownace13b12011-03-09 17:39:48 -0800855
856 // Scale factor for gesture based pointer movements.
857 float pointerGestureXMovementScale;
858 float pointerGestureYMovementScale;
859
860 // Scale factor for gesture based zooming and other freeform motions.
861 float pointerGestureXZoomScale;
862 float pointerGestureYZoomScale;
863
Jeff Brown2352b972011-04-12 22:39:53 -0700864 // The maximum swipe width.
865 float pointerGestureMaxSwipeWidth;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700866 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700867
Jeff Brown8d608662010-08-30 03:02:23 -0700868 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700869 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700870 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700871 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700872 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700873 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700874 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700875 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700876 virtual void parseCalibration();
877 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700878 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700879
880 enum TouchResult {
881 // Dispatch the touch normally.
882 DISPATCH_TOUCH,
883 // Do not dispatch the touch, but keep tracking the current stroke.
884 SKIP_TOUCH,
885 // Do not dispatch the touch, and drop all information associated with the current stoke
886 // so the next movement will appear as a new down.
887 DROP_STROKE
888 };
889
890 void syncTouch(nsecs_t when, bool havePointerIds);
891
892private:
893 /* Maximum number of historical samples to average. */
894 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
895
896 /* Slop distance for jumpy pointer detection.
897 * The vertical range of the screen divided by this is our epsilon value. */
898 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
899
900 /* Number of jumpy points to drop for touchscreens that need it. */
901 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
902 static const uint32_t JUMPY_DROP_LIMIT = 3;
903
904 /* Maximum squared distance for averaging.
905 * If moving farther than this, turn of averaging to avoid lag in response. */
906 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
907
908 struct AveragingTouchFilterState {
909 // Individual history tracks are stored by pointer id
910 uint32_t historyStart[MAX_POINTERS];
911 uint32_t historyEnd[MAX_POINTERS];
912 struct {
913 struct {
914 int32_t x;
915 int32_t y;
916 int32_t pressure;
917 } pointers[MAX_POINTERS];
918 } historyData[AVERAGING_HISTORY_SIZE];
919 } mAveragingTouchFilter;
920
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700921 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700922 uint32_t jumpyPointsDropped;
923 } mJumpyTouchFilter;
924
925 struct PointerDistanceHeapElement {
926 uint32_t currentPointerIndex : 8;
927 uint32_t lastPointerIndex : 8;
928 uint64_t distance : 48; // squared distance
929 };
930
Jeff Brownace13b12011-03-09 17:39:48 -0800931 struct PointerGesture {
932 enum Mode {
933 // No fingers, button is not pressed.
934 // Nothing happening.
935 NEUTRAL,
936
937 // No fingers, button is not pressed.
938 // Tap detected.
939 // Emits DOWN and UP events at the pointer location.
940 TAP,
941
Jeff Brown79ac9692011-04-19 21:20:10 -0700942 // Exactly one finger dragging following a tap.
943 // Pointer follows the active finger.
944 // Emits DOWN, MOVE and UP events at the pointer location.
945 TAP_DRAG,
946
Jeff Brownace13b12011-03-09 17:39:48 -0800947 // Button is pressed.
948 // Pointer follows the active finger if there is one. Other fingers are ignored.
949 // Emits DOWN, MOVE and UP events at the pointer location.
Jeff Brown79ac9692011-04-19 21:20:10 -0700950 BUTTON_CLICK_OR_DRAG,
Jeff Brownace13b12011-03-09 17:39:48 -0800951
952 // Exactly one finger, button is not pressed.
953 // Pointer follows the active finger.
954 // Emits HOVER_MOVE events at the pointer location.
955 HOVER,
956
Jeff Brown2352b972011-04-12 22:39:53 -0700957 // Exactly two fingers but neither have moved enough to clearly indicate
958 // whether a swipe or freeform gesture was intended. We consider the
959 // pointer to be pressed so this enables clicking or long-pressing on buttons.
960 // Pointer does not move.
961 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
962 PRESS,
Jeff Brownace13b12011-03-09 17:39:48 -0800963
964 // Exactly two fingers moving in the same direction, button is not pressed.
965 // Pointer does not move.
966 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
967 // follows the midpoint between both fingers.
Jeff Brownace13b12011-03-09 17:39:48 -0800968 SWIPE,
969
970 // Two or more fingers moving in arbitrary directions, button is not pressed.
971 // Pointer does not move.
972 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
973 // each finger individually relative to the initial centroid of the finger.
Jeff Brownace13b12011-03-09 17:39:48 -0800974 FREEFORM,
975
976 // Waiting for quiet time to end before starting the next gesture.
977 QUIET,
978 };
979
Jeff Brown2352b972011-04-12 22:39:53 -0700980 // Time the first finger went down.
981 nsecs_t firstTouchTime;
982
Jeff Brownace13b12011-03-09 17:39:48 -0800983 // The active pointer id from the raw touch data.
984 int32_t activeTouchId; // -1 if none
985
986 // The active pointer id from the gesture last delivered to the application.
987 int32_t activeGestureId; // -1 if none
988
989 // Pointer coords and ids for the current and previous pointer gesture.
990 Mode currentGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -0800991 BitSet32 currentGestureIdBits;
992 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700993 PointerProperties currentGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -0800994 PointerCoords currentGestureCoords[MAX_POINTERS];
995
996 Mode lastGestureMode;
Jeff Brownace13b12011-03-09 17:39:48 -0800997 BitSet32 lastGestureIdBits;
998 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700999 PointerProperties lastGestureProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001000 PointerCoords lastGestureCoords[MAX_POINTERS];
1001
Jeff Brown2352b972011-04-12 22:39:53 -07001002 // Pointer coords and ids for the current spots.
1003 PointerControllerInterface::SpotGesture spotGesture;
1004 BitSet32 spotIdBits; // same set of ids as touch ids
1005 uint32_t spotIdToIndex[MAX_POINTER_ID + 1];
1006 PointerCoords spotCoords[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08001007
1008 // Time the pointer gesture last went down.
1009 nsecs_t downTime;
1010
Jeff Brown79ac9692011-04-19 21:20:10 -07001011 // Time when the pointer went down for a TAP.
1012 nsecs_t tapDownTime;
1013
1014 // Time when the pointer went up for a TAP.
1015 nsecs_t tapUpTime;
Jeff Brownace13b12011-03-09 17:39:48 -08001016
Jeff Brown2352b972011-04-12 22:39:53 -07001017 // Location of initial tap.
1018 float tapX, tapY;
1019
Jeff Brownace13b12011-03-09 17:39:48 -08001020 // Time we started waiting for quiescence.
1021 nsecs_t quietTime;
1022
Jeff Brown2352b972011-04-12 22:39:53 -07001023 // Reference points for multitouch gestures.
1024 float referenceTouchX; // reference touch X/Y coordinates in surface units
1025 float referenceTouchY;
1026 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1027 float referenceGestureY;
1028
1029 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1030 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1031
Jeff Brownace13b12011-03-09 17:39:48 -08001032 // A velocity tracker for determining whether to switch active pointers during drags.
1033 VelocityTracker velocityTracker;
1034
1035 void reset() {
Jeff Brown2352b972011-04-12 22:39:53 -07001036 firstTouchTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001037 activeTouchId = -1;
1038 activeGestureId = -1;
1039 currentGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001040 currentGestureIdBits.clear();
1041 lastGestureMode = NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08001042 lastGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07001043 spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
1044 spotIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001045 downTime = 0;
1046 velocityTracker.clear();
Jeff Brown79ac9692011-04-19 21:20:10 -07001047 resetTap();
Jeff Brownace13b12011-03-09 17:39:48 -08001048 resetQuietTime();
1049 }
1050
Jeff Brown79ac9692011-04-19 21:20:10 -07001051 void resetTap() {
1052 tapDownTime = LLONG_MIN;
1053 tapUpTime = LLONG_MIN;
Jeff Brownace13b12011-03-09 17:39:48 -08001054 }
1055
1056 void resetQuietTime() {
1057 quietTime = LLONG_MIN;
1058 }
1059 } mPointerGesture;
1060
Jeff Brown6328cdc2010-07-29 18:18:33 -07001061 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001062
1063 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
1064 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08001065 void prepareTouches(int32_t* outEdgeFlags, float* outXPrecision, float* outYPrecision);
Jeff Brown79ac9692011-04-19 21:20:10 -07001066 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1067 bool preparePointerGestures(nsecs_t when,
1068 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
Jeff Brown2352b972011-04-12 22:39:53 -07001069 void moveSpotsLocked();
Jeff Brownace13b12011-03-09 17:39:48 -08001070
1071 // Dispatches a motion event.
1072 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1073 // method will take care of setting the index and transmuting the action to DOWN or UP
1074 // it is the first / last pointer to go down / up.
1075 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001076 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1077 int32_t edgeFlags,
1078 const PointerProperties* properties, const PointerCoords* coords,
1079 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08001080 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1081
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001082 // Updates pointer coords and properties for pointers with specified ids that have moved.
Jeff Brownace13b12011-03-09 17:39:48 -08001083 // Returns true if any of them changed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001084 bool updateMovedPointers(const PointerProperties* inProperties,
1085 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1086 PointerProperties* outProperties, PointerCoords* outCoords,
1087 const uint32_t* outIdToIndex, BitSet32 idBits) const;
Jeff Brownace13b12011-03-09 17:39:48 -08001088
Jeff Brownefd32662011-03-08 15:13:06 -08001089 void suppressSwipeOntoVirtualKeys(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001090
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001091 int32_t getTouchToolType(bool isStylus) const;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001092 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
1093 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094
1095 bool applyBadTouchFilter();
1096 bool applyJumpyTouchFilter();
1097 void applyAveragingTouchFilter();
1098 void calculatePointerIds();
1099};
1100
1101
1102class SingleTouchInputMapper : public TouchInputMapper {
1103public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001104 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001105 virtual ~SingleTouchInputMapper();
1106
1107 virtual void reset();
1108 virtual void process(const RawEvent* rawEvent);
1109
1110protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001111 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001112
1113private:
1114 struct Accumulator {
1115 enum {
1116 FIELD_BTN_TOUCH = 1,
1117 FIELD_ABS_X = 2,
1118 FIELD_ABS_Y = 4,
1119 FIELD_ABS_PRESSURE = 8,
Jeff Brownefd32662011-03-08 15:13:06 -08001120 FIELD_ABS_TOOL_WIDTH = 16,
Jeff Brownace13b12011-03-09 17:39:48 -08001121 FIELD_BUTTONS = 32,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001122 };
1123
1124 uint32_t fields;
1125
1126 bool btnTouch;
1127 int32_t absX;
1128 int32_t absY;
1129 int32_t absPressure;
1130 int32_t absToolWidth;
1131
Jeff Brownace13b12011-03-09 17:39:48 -08001132 uint32_t buttonDown;
1133 uint32_t buttonUp;
1134
Jeff Brown6d0fec22010-07-23 21:28:06 -07001135 inline void clear() {
1136 fields = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08001137 buttonDown = 0;
1138 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001139 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001140 } mAccumulator;
1141
1142 bool mDown;
1143 int32_t mX;
1144 int32_t mY;
1145 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07001146 int32_t mToolWidth;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001147 int32_t mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001148
1149 void initialize();
1150
1151 void sync(nsecs_t when);
1152};
1153
1154
1155class MultiTouchInputMapper : public TouchInputMapper {
1156public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001157 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001158 virtual ~MultiTouchInputMapper();
1159
1160 virtual void reset();
1161 virtual void process(const RawEvent* rawEvent);
1162
1163protected:
Jeff Brown8d608662010-08-30 03:02:23 -07001164 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001165
1166private:
1167 struct Accumulator {
1168 enum {
1169 FIELD_ABS_MT_POSITION_X = 1,
1170 FIELD_ABS_MT_POSITION_Y = 2,
1171 FIELD_ABS_MT_TOUCH_MAJOR = 4,
1172 FIELD_ABS_MT_TOUCH_MINOR = 8,
1173 FIELD_ABS_MT_WIDTH_MAJOR = 16,
1174 FIELD_ABS_MT_WIDTH_MINOR = 32,
1175 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001176 FIELD_ABS_MT_TRACKING_ID = 128,
1177 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001178 };
1179
1180 uint32_t pointerCount;
1181 struct Pointer {
1182 uint32_t fields;
1183
1184 int32_t absMTPositionX;
1185 int32_t absMTPositionY;
1186 int32_t absMTTouchMajor;
1187 int32_t absMTTouchMinor;
1188 int32_t absMTWidthMajor;
1189 int32_t absMTWidthMinor;
1190 int32_t absMTOrientation;
1191 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001192 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001193
1194 inline void clear() {
1195 fields = 0;
1196 }
1197 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
1198
Jeff Brownace13b12011-03-09 17:39:48 -08001199 // Bitfield of buttons that went down or up.
1200 uint32_t buttonDown;
1201 uint32_t buttonUp;
1202
Jeff Brown6d0fec22010-07-23 21:28:06 -07001203 inline void clear() {
1204 pointerCount = 0;
1205 pointers[0].clear();
Jeff Brownace13b12011-03-09 17:39:48 -08001206 buttonDown = 0;
1207 buttonUp = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001208 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001209 } mAccumulator;
1210
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001211 int32_t mButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08001212
Jeff Brown6d0fec22010-07-23 21:28:06 -07001213 void initialize();
1214
1215 void sync(nsecs_t when);
1216};
1217
Jeff Browncb1404e2011-01-15 18:14:15 -08001218
1219class JoystickInputMapper : public InputMapper {
1220public:
1221 JoystickInputMapper(InputDevice* device);
1222 virtual ~JoystickInputMapper();
1223
1224 virtual uint32_t getSources();
1225 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1226 virtual void dump(String8& dump);
1227 virtual void configure();
1228 virtual void reset();
1229 virtual void process(const RawEvent* rawEvent);
1230
1231private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001232 struct Axis {
1233 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001234 AxisInfo axisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001235
Jeff Brown6f2fba42011-02-19 01:08:02 -08001236 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001237
Jeff Brown6f2fba42011-02-19 01:08:02 -08001238 float scale; // scale factor from raw to normalized values
1239 float offset; // offset to add after scaling for normalization
Jeff Brown85297452011-03-04 13:07:49 -08001240 float highScale; // scale factor from raw to normalized values of high split
1241 float highOffset; // offset to add after scaling for normalization of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001242
Jeff Brown6f2fba42011-02-19 01:08:02 -08001243 float min; // normalized inclusive minimum
1244 float max; // normalized inclusive maximum
1245 float flat; // normalized flat region size
1246 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001247
Jeff Brown6f2fba42011-02-19 01:08:02 -08001248 float filter; // filter out small variations of this size
Jeff Brown85297452011-03-04 13:07:49 -08001249 float currentValue; // current value
1250 float newValue; // most recent value
1251 float highCurrentValue; // current value of high split
1252 float highNewValue; // most recent value of high split
Jeff Browncb1404e2011-01-15 18:14:15 -08001253
Jeff Brown85297452011-03-04 13:07:49 -08001254 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1255 bool explicitlyMapped, float scale, float offset,
1256 float highScale, float highOffset,
Jeff Brown6f2fba42011-02-19 01:08:02 -08001257 float min, float max, float flat, float fuzz) {
1258 this->rawAxisInfo = rawAxisInfo;
Jeff Brown85297452011-03-04 13:07:49 -08001259 this->axisInfo = axisInfo;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001260 this->explicitlyMapped = explicitlyMapped;
1261 this->scale = scale;
1262 this->offset = offset;
Jeff Brown85297452011-03-04 13:07:49 -08001263 this->highScale = highScale;
1264 this->highOffset = highOffset;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001265 this->min = min;
1266 this->max = max;
1267 this->flat = flat;
1268 this->fuzz = fuzz;
1269 this->filter = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001270 resetValue();
1271 }
1272
1273 void resetValue() {
1274 this->currentValue = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001275 this->newValue = 0;
Jeff Brown85297452011-03-04 13:07:49 -08001276 this->highCurrentValue = 0;
1277 this->highNewValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001278 }
1279 };
1280
Jeff Brown6f2fba42011-02-19 01:08:02 -08001281 // Axes indexed by raw ABS_* axis index.
1282 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001283
Jeff Brown6f2fba42011-02-19 01:08:02 -08001284 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001285
Jeff Brown85297452011-03-04 13:07:49 -08001286 bool haveAxis(int32_t axisId);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001287 void pruneAxes(bool ignoreExplicitlyMappedAxes);
Jeff Brown85297452011-03-04 13:07:49 -08001288 bool filterAxes(bool force);
1289
1290 static bool hasValueChangedSignificantly(float filter,
1291 float newValue, float currentValue, float min, float max);
1292 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1293 float newValue, float currentValue, float thresholdValue);
Jeff Browncb1404e2011-01-15 18:14:15 -08001294
Jeff Brown6f2fba42011-02-19 01:08:02 -08001295 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001296};
1297
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001298} // namespace android
1299
1300#endif // _UI_INPUT_READER_H