blob: 7568ba72329a59b25f168c18542d748e8442883f [file] [log] [blame]
Jeff Browne839a582010-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
20#include <ui/EventHub.h>
21#include <ui/Input.h>
Jeff Browne839a582010-04-22 18:58:52 -070022#include <ui/InputDispatcher.h>
23#include <utils/KeyedVector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/BitSet.h>
29
30#include <stddef.h>
31#include <unistd.h>
32
Jeff Browne839a582010-04-22 18:58:52 -070033namespace android {
34
Jeff Browne57e8952010-07-23 21:28:06 -070035class InputDevice;
36class InputMapper;
37
Jeff Brown38a7fab2010-08-30 03:02:23 -070038/* Describes a virtual key. */
39struct VirtualKeyDefinition {
40 int32_t scanCode;
41
42 // configured position data, specified in display coords
43 int32_t centerX;
44 int32_t centerY;
45 int32_t width;
46 int32_t height;
47};
48
49
50/* Specifies input device calibration settings. */
51class InputDeviceCalibration {
52public:
53 InputDeviceCalibration();
54
55 void clear();
56 void addProperty(const String8& key, const String8& value);
57
58 bool tryGetProperty(const String8& key, String8& outValue) const;
59 bool tryGetProperty(const String8& key, int32_t& outValue) const;
60 bool tryGetProperty(const String8& key, float& outValue) const;
61
62private:
63 KeyedVector<String8, String8> mProperties;
64};
65
Jeff Browne57e8952010-07-23 21:28:06 -070066
Jeff Brown54bc2812010-06-15 01:31:58 -070067/*
68 * Input reader policy interface.
69 *
70 * The input reader policy is used by the input reader to interact with the Window Manager
71 * and other system components.
72 *
73 * The actual implementation is partially supported by callbacks into the DVM
74 * via JNI. This interface is also mocked in the unit tests.
75 */
76class InputReaderPolicyInterface : public virtual RefBase {
77protected:
78 InputReaderPolicyInterface() { }
79 virtual ~InputReaderPolicyInterface() { }
80
81public:
82 /* Display orientations. */
83 enum {
84 ROTATION_0 = 0,
85 ROTATION_90 = 1,
86 ROTATION_180 = 2,
87 ROTATION_270 = 3
88 };
89
Jeff Brown54bc2812010-06-15 01:31:58 -070090 /* Gets information about the display with the specified id.
91 * Returns true if the display info is available, false otherwise.
92 */
93 virtual bool getDisplayInfo(int32_t displayId,
94 int32_t* width, int32_t* height, int32_t* orientation) = 0;
95
Jeff Brown54bc2812010-06-15 01:31:58 -070096 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
97 * certain device whose screen currently is not all that good.
98 */
99 virtual bool filterTouchEvents() = 0;
100
101 /* Determines whether to turn on some hacks to improve touch interaction with another device
102 * where touch coordinate data can get corrupted.
103 */
104 virtual bool filterJumpyTouchEvents() = 0;
105
Jeff Brown7bfdb292010-10-24 14:39:33 -0700106 /* Gets the amount of time to disable virtual keys after the screen is touched
107 * in order to filter out accidental virtual key presses due to swiping gestures
108 * or taps near the edge of the display. May be 0 to disable the feature.
109 */
110 virtual nsecs_t getVirtualKeyQuietTime() = 0;
111
Jeff Brown54bc2812010-06-15 01:31:58 -0700112 /* Gets the configured virtual key definitions for an input device. */
113 virtual void getVirtualKeyDefinitions(const String8& deviceName,
114 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
115
Jeff Brown38a7fab2010-08-30 03:02:23 -0700116 /* Gets the calibration for an input device. */
117 virtual void getInputDeviceCalibration(const String8& deviceName,
118 InputDeviceCalibration& outCalibration) = 0;
119
Jeff Brown54bc2812010-06-15 01:31:58 -0700120 /* Gets the excluded device names for the platform. */
121 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
122};
123
124
125/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700126class InputReaderInterface : public virtual RefBase {
127protected:
128 InputReaderInterface() { }
129 virtual ~InputReaderInterface() { }
130
131public:
Jeff Browna665ca82010-09-08 11:49:43 -0700132 /* Dumps the state of the input reader.
133 *
134 * This method may be called on any thread (usually by the input manager). */
135 virtual void dump(String8& dump) = 0;
136
Jeff Browne839a582010-04-22 18:58:52 -0700137 /* Runs a single iteration of the processing loop.
138 * Nominally reads and processes one incoming message from the EventHub.
139 *
140 * This method should be called on the input reader thread.
141 */
142 virtual void loopOnce() = 0;
143
Jeff Brown54bc2812010-06-15 01:31:58 -0700144 /* Gets the current input device configuration.
145 *
146 * This method may be called on any thread (usually by the input manager).
147 */
Jeff Browne57e8952010-07-23 21:28:06 -0700148 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700149
Jeff Browne57e8952010-07-23 21:28:06 -0700150 /* Gets information about the specified input device.
151 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
152 * was no such device.
153 *
154 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700155 */
Jeff Browne57e8952010-07-23 21:28:06 -0700156 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
157
158 /* Gets the list of all registered device ids. */
159 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
160
161 /* Query current input state. */
162 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
163 int32_t scanCode) = 0;
164 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
165 int32_t keyCode) = 0;
166 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
167 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700168
169 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700170 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
171 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
172};
173
174
175/* Internal interface used by individual input devices to access global input device state
176 * and parameters maintained by the input reader.
177 */
178class InputReaderContext {
Jeff Brown3c3cc622010-10-20 15:33:38 -0700179public:
Jeff Browne57e8952010-07-23 21:28:06 -0700180 InputReaderContext() { }
181 virtual ~InputReaderContext() { }
182
Jeff Browne57e8952010-07-23 21:28:06 -0700183 virtual void updateGlobalMetaState() = 0;
184 virtual int32_t getGlobalMetaState() = 0;
185
Jeff Brown7bfdb292010-10-24 14:39:33 -0700186 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
187 virtual bool shouldDropVirtualKey(nsecs_t now,
188 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
189
Jeff Browne57e8952010-07-23 21:28:06 -0700190 virtual InputReaderPolicyInterface* getPolicy() = 0;
191 virtual InputDispatcherInterface* getDispatcher() = 0;
192 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700193};
194
Jeff Brown54bc2812010-06-15 01:31:58 -0700195
Jeff Browne839a582010-04-22 18:58:52 -0700196/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700197 * that it sends to the input dispatcher. Some functions of the input reader, such as early
198 * event filtering in low power states, are controlled by a separate policy object.
199 *
200 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700201 * Because the policy and dispatcher can potentially block or cause re-entrance into
202 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700203 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700204 */
Jeff Brown3c3cc622010-10-20 15:33:38 -0700205class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700206public:
207 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700208 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700209 const sp<InputDispatcherInterface>& dispatcher);
210 virtual ~InputReader();
211
Jeff Browna665ca82010-09-08 11:49:43 -0700212 virtual void dump(String8& dump);
213
Jeff Browne839a582010-04-22 18:58:52 -0700214 virtual void loopOnce();
215
Jeff Browne57e8952010-07-23 21:28:06 -0700216 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700217
Jeff Browne57e8952010-07-23 21:28:06 -0700218 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
219 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700220
Jeff Browne57e8952010-07-23 21:28:06 -0700221 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
222 int32_t scanCode);
223 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
224 int32_t keyCode);
225 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
226 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700227
Jeff Browne57e8952010-07-23 21:28:06 -0700228 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
229 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700230
Jeff Brown3c3cc622010-10-20 15:33:38 -0700231protected:
232 // These methods are protected virtual so they can be overridden and instrumented
233 // by test cases.
234 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
235
Jeff Browne839a582010-04-22 18:58:52 -0700236private:
Jeff Browne839a582010-04-22 18:58:52 -0700237 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700238 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700239 sp<InputDispatcherInterface> mDispatcher;
240
Jeff Browne57e8952010-07-23 21:28:06 -0700241 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
242 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
243 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
244
245 // This reader/writer lock guards the list of input devices.
246 // The writer lock must be held whenever the list of input devices is modified
247 // and then promptly released.
248 // The reader lock must be held whenever the list of input devices is traversed or an
249 // input device in the list is accessed.
250 // This lock only protects the registry and prevents inadvertent deletion of device objects
251 // that are in use. Individual devices are responsible for guarding their own internal state
252 // as needed for concurrent operation.
253 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700254 KeyedVector<int32_t, InputDevice*> mDevices;
255
Jeff Browne57e8952010-07-23 21:28:06 -0700256 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700257 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700258
Jeff Brown1ad00e92010-10-01 18:55:43 -0700259 void addDevice(int32_t deviceId);
260 void removeDevice(int32_t deviceId);
Jeff Brown54bc2812010-06-15 01:31:58 -0700261 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700262
Jeff Browne57e8952010-07-23 21:28:06 -0700263 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700264
Jeff Brown3c3cc622010-10-20 15:33:38 -0700265 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700266
Jeff Browne57e8952010-07-23 21:28:06 -0700267 // state management for all devices
268 Mutex mStateLock;
269
270 int32_t mGlobalMetaState;
271 virtual void updateGlobalMetaState();
272 virtual int32_t getGlobalMetaState();
273
274 InputConfiguration mInputConfiguration;
275 void updateInputConfiguration();
276
Jeff Brown7bfdb292010-10-24 14:39:33 -0700277 nsecs_t mDisableVirtualKeysTimeout;
278 virtual void disableVirtualKeysUntil(nsecs_t time);
279 virtual bool shouldDropVirtualKey(nsecs_t now,
280 InputDevice* device, int32_t keyCode, int32_t scanCode);
281
Jeff Browne57e8952010-07-23 21:28:06 -0700282 // state queries
283 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
284 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
285 GetStateFunc getStateFunc);
286 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
287 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700288};
289
290
291/* Reads raw events from the event hub and processes them, endlessly. */
292class InputReaderThread : public Thread {
293public:
294 InputReaderThread(const sp<InputReaderInterface>& reader);
295 virtual ~InputReaderThread();
296
297private:
298 sp<InputReaderInterface> mReader;
299
300 virtual bool threadLoop();
301};
302
Jeff Browne57e8952010-07-23 21:28:06 -0700303
304/* Represents the state of a single input device. */
305class InputDevice {
306public:
307 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
308 ~InputDevice();
309
310 inline InputReaderContext* getContext() { return mContext; }
311 inline int32_t getId() { return mId; }
312 inline const String8& getName() { return mName; }
313 inline uint32_t getSources() { return mSources; }
314
315 inline bool isIgnored() { return mMappers.isEmpty(); }
316
Jeff Brown26c94ff2010-09-30 14:33:04 -0700317 void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700318 void addMapper(InputMapper* mapper);
319 void configure();
320 void reset();
321 void process(const RawEvent* rawEvent);
322
323 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
324 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
325 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
326 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
327 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
328 const int32_t* keyCodes, uint8_t* outFlags);
329
330 int32_t getMetaState();
331
Jeff Brown38a7fab2010-08-30 03:02:23 -0700332 inline const InputDeviceCalibration& getCalibration() {
333 return mCalibration;
334 }
335
Jeff Browne57e8952010-07-23 21:28:06 -0700336private:
337 InputReaderContext* mContext;
338 int32_t mId;
339
340 Vector<InputMapper*> mMappers;
341
342 String8 mName;
343 uint32_t mSources;
344
345 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
346 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700347
348 InputDeviceCalibration mCalibration;
Jeff Browne57e8952010-07-23 21:28:06 -0700349};
350
351
352/* An input mapper transforms raw input events into cooked event data.
353 * A single input device can have multiple associated input mappers in order to interpret
354 * different classes of events.
355 */
356class InputMapper {
357public:
358 InputMapper(InputDevice* device);
359 virtual ~InputMapper();
360
361 inline InputDevice* getDevice() { return mDevice; }
362 inline int32_t getDeviceId() { return mDevice->getId(); }
363 inline const String8 getDeviceName() { return mDevice->getName(); }
364 inline InputReaderContext* getContext() { return mContext; }
365 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
366 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
367 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
368
369 virtual uint32_t getSources() = 0;
370 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700371 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700372 virtual void configure();
373 virtual void reset();
374 virtual void process(const RawEvent* rawEvent) = 0;
375
376 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
377 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
378 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
379 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
380 const int32_t* keyCodes, uint8_t* outFlags);
381
382 virtual int32_t getMetaState();
383
384protected:
385 InputDevice* mDevice;
386 InputReaderContext* mContext;
Jeff Browne57e8952010-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:
407 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
408 int32_t keyboardType);
409 virtual ~KeyboardInputMapper();
410
411 virtual uint32_t getSources();
412 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700413 virtual void dump(String8& dump);
Jeff Browne57e8952010-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 Brownb51719b2010-07-29 18:18:33 -0700425 Mutex mLock;
426
Jeff Browne57e8952010-07-23 21:28:06 -0700427 struct KeyDown {
428 int32_t keyCode;
429 int32_t scanCode;
430 };
431
432 int32_t mAssociatedDisplayId;
433 uint32_t mSources;
434 int32_t mKeyboardType;
435
Jeff Brownb51719b2010-07-29 18:18:33 -0700436 struct LockedState {
437 Vector<KeyDown> keyDowns; // keys that are down
438 int32_t metaState;
439 nsecs_t downTime; // time of most recent key down
440 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700441
Jeff Brownb51719b2010-07-29 18:18:33 -0700442 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700443
444 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700445
Jeff Browne57e8952010-07-23 21:28:06 -0700446 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
447 uint32_t policyFlags);
448
Jeff Brownb51719b2010-07-29 18:18:33 -0700449 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -0700450};
451
452
453class TrackballInputMapper : public InputMapper {
454public:
455 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
456 virtual ~TrackballInputMapper();
457
458 virtual uint32_t getSources();
459 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700460 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700461 virtual void reset();
462 virtual void process(const RawEvent* rawEvent);
463
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700464 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
465
Jeff Browne57e8952010-07-23 21:28:06 -0700466private:
467 // Amount that trackball needs to move in order to generate a key event.
468 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
469
Jeff Brownb51719b2010-07-29 18:18:33 -0700470 Mutex mLock;
471
Jeff Browne57e8952010-07-23 21:28:06 -0700472 int32_t mAssociatedDisplayId;
473
474 struct Accumulator {
475 enum {
476 FIELD_BTN_MOUSE = 1,
477 FIELD_REL_X = 2,
478 FIELD_REL_Y = 4
479 };
480
481 uint32_t fields;
482
483 bool btnMouse;
484 int32_t relX;
485 int32_t relY;
486
487 inline void clear() {
488 fields = 0;
489 }
Jeff Browne57e8952010-07-23 21:28:06 -0700490 } mAccumulator;
491
Jeff Browne57e8952010-07-23 21:28:06 -0700492 float mXScale;
493 float mYScale;
494 float mXPrecision;
495 float mYPrecision;
496
Jeff Brownb51719b2010-07-29 18:18:33 -0700497 struct LockedState {
498 bool down;
499 nsecs_t downTime;
500 } mLocked;
501
502 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700503
504 void sync(nsecs_t when);
505};
506
507
508class TouchInputMapper : public InputMapper {
509public:
510 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
511 virtual ~TouchInputMapper();
512
513 virtual uint32_t getSources();
514 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700515 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700516 virtual void configure();
517 virtual void reset();
518
519 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
520 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
521 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
522 const int32_t* keyCodes, uint8_t* outFlags);
523
524protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700525 Mutex mLock;
526
Jeff Browne57e8952010-07-23 21:28:06 -0700527 struct VirtualKey {
528 int32_t keyCode;
529 int32_t scanCode;
530 uint32_t flags;
531
532 // computed hit box, specified in touch screen coords based on known display size
533 int32_t hitLeft;
534 int32_t hitTop;
535 int32_t hitRight;
536 int32_t hitBottom;
537
538 inline bool isHit(int32_t x, int32_t y) const {
539 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
540 }
541 };
542
Jeff Brown38a7fab2010-08-30 03:02:23 -0700543 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700544 struct PointerData {
545 uint32_t id;
546 int32_t x;
547 int32_t y;
548 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700549 int32_t touchMajor;
550 int32_t touchMinor;
551 int32_t toolMajor;
552 int32_t toolMinor;
553 int32_t orientation;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700554
555 inline bool operator== (const PointerData& other) const {
556 return id == other.id
557 && x == other.x
558 && y == other.y
559 && pressure == other.pressure
560 && touchMajor == other.touchMajor
561 && touchMinor == other.touchMinor
562 && toolMajor == other.toolMajor
563 && toolMinor == other.toolMinor
564 && orientation == other.orientation;
565 }
566 inline bool operator!= (const PointerData& other) const {
567 return !(*this == other);
568 }
Jeff Browne57e8952010-07-23 21:28:06 -0700569 };
570
Jeff Brown38a7fab2010-08-30 03:02:23 -0700571 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700572 struct TouchData {
573 uint32_t pointerCount;
574 PointerData pointers[MAX_POINTERS];
575 BitSet32 idBits;
576 uint32_t idToIndex[MAX_POINTER_ID + 1];
577
578 void copyFrom(const TouchData& other) {
579 pointerCount = other.pointerCount;
580 idBits = other.idBits;
581
582 for (uint32_t i = 0; i < pointerCount; i++) {
583 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700584
585 int id = pointers[i].id;
586 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700587 }
588 }
589
590 inline void clear() {
591 pointerCount = 0;
592 idBits.clear();
593 }
594 };
595
596 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700597
598 // Immutable configuration parameters.
599 struct Parameters {
600 bool useBadTouchFilter;
601 bool useJumpyTouchFilter;
602 bool useAveragingTouchFilter;
Jeff Brown7bfdb292010-10-24 14:39:33 -0700603 nsecs_t virtualKeyQuietTime;
Jeff Browne57e8952010-07-23 21:28:06 -0700604 } mParameters;
605
Jeff Brown38a7fab2010-08-30 03:02:23 -0700606 // Immutable calibration parameters in parsed form.
607 struct Calibration {
Jeff Brown6b337e72010-10-14 21:42:15 -0700608 // Touch Size
609 enum TouchSizeCalibration {
610 TOUCH_SIZE_CALIBRATION_DEFAULT,
611 TOUCH_SIZE_CALIBRATION_NONE,
612 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
613 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700614 };
615
Jeff Brown6b337e72010-10-14 21:42:15 -0700616 TouchSizeCalibration touchSizeCalibration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700617
Jeff Brown6b337e72010-10-14 21:42:15 -0700618 // Tool Size
619 enum ToolSizeCalibration {
620 TOOL_SIZE_CALIBRATION_DEFAULT,
621 TOOL_SIZE_CALIBRATION_NONE,
622 TOOL_SIZE_CALIBRATION_GEOMETRIC,
623 TOOL_SIZE_CALIBRATION_LINEAR,
624 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700625 };
626
Jeff Brown6b337e72010-10-14 21:42:15 -0700627 ToolSizeCalibration toolSizeCalibration;
628 bool haveToolSizeLinearScale;
629 float toolSizeLinearScale;
630 bool haveToolSizeLinearBias;
631 float toolSizeLinearBias;
632 bool haveToolSizeAreaScale;
633 float toolSizeAreaScale;
634 bool haveToolSizeAreaBias;
635 float toolSizeAreaBias;
636 bool haveToolSizeIsSummed;
637 int32_t toolSizeIsSummed;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700638
639 // Pressure
640 enum PressureCalibration {
641 PRESSURE_CALIBRATION_DEFAULT,
642 PRESSURE_CALIBRATION_NONE,
643 PRESSURE_CALIBRATION_PHYSICAL,
644 PRESSURE_CALIBRATION_AMPLITUDE,
645 };
646 enum PressureSource {
647 PRESSURE_SOURCE_DEFAULT,
648 PRESSURE_SOURCE_PRESSURE,
649 PRESSURE_SOURCE_TOUCH,
650 };
651
652 PressureCalibration pressureCalibration;
653 PressureSource pressureSource;
654 bool havePressureScale;
655 float pressureScale;
656
657 // Size
658 enum SizeCalibration {
659 SIZE_CALIBRATION_DEFAULT,
660 SIZE_CALIBRATION_NONE,
661 SIZE_CALIBRATION_NORMALIZED,
662 };
663
664 SizeCalibration sizeCalibration;
665
666 // Orientation
667 enum OrientationCalibration {
668 ORIENTATION_CALIBRATION_DEFAULT,
669 ORIENTATION_CALIBRATION_NONE,
670 ORIENTATION_CALIBRATION_INTERPOLATED,
671 };
672
673 OrientationCalibration orientationCalibration;
674 } mCalibration;
675
676 // Raw axis information from the driver.
677 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700678 RawAbsoluteAxisInfo x;
679 RawAbsoluteAxisInfo y;
680 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700681 RawAbsoluteAxisInfo touchMajor;
682 RawAbsoluteAxisInfo touchMinor;
683 RawAbsoluteAxisInfo toolMajor;
684 RawAbsoluteAxisInfo toolMinor;
685 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700686 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700687
Jeff Brownb51719b2010-07-29 18:18:33 -0700688 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700689 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700690 TouchData mLastTouch;
691
692 // The time the primary pointer last went down.
693 nsecs_t mDownTime;
694
Jeff Brownb51719b2010-07-29 18:18:33 -0700695 struct LockedState {
696 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700697
Jeff Brownb51719b2010-07-29 18:18:33 -0700698 // The surface orientation and width and height set by configureSurfaceLocked().
699 int32_t surfaceOrientation;
700 int32_t surfaceWidth, surfaceHeight;
701
702 // Translation and scaling factors, orientation-independent.
703 int32_t xOrigin;
704 float xScale;
705 float xPrecision;
706
707 int32_t yOrigin;
708 float yScale;
709 float yPrecision;
710
Jeff Brown38a7fab2010-08-30 03:02:23 -0700711 float geometricScale;
712
Jeff Brown6b337e72010-10-14 21:42:15 -0700713 float toolSizeLinearScale;
714 float toolSizeLinearBias;
715 float toolSizeAreaScale;
716 float toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700717
Jeff Brownb51719b2010-07-29 18:18:33 -0700718 float pressureScale;
719
Jeff Brownb51719b2010-07-29 18:18:33 -0700720 float sizeScale;
721
722 float orientationScale;
723
724 // Oriented motion ranges for input device info.
725 struct OrientedRanges {
726 InputDeviceInfo::MotionRange x;
727 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700728
729 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700730 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700731
732 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700733 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700734
Jeff Brown6b337e72010-10-14 21:42:15 -0700735 bool haveTouchSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700736 InputDeviceInfo::MotionRange touchMajor;
737 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700738
Jeff Brown6b337e72010-10-14 21:42:15 -0700739 bool haveToolSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700740 InputDeviceInfo::MotionRange toolMajor;
741 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700742
743 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700744 InputDeviceInfo::MotionRange orientation;
745 } orientedRanges;
746
747 // Oriented dimensions and precision.
748 float orientedSurfaceWidth, orientedSurfaceHeight;
749 float orientedXPrecision, orientedYPrecision;
750
751 struct CurrentVirtualKeyState {
752 bool down;
753 nsecs_t downTime;
754 int32_t keyCode;
755 int32_t scanCode;
756 } currentVirtualKey;
757 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700758
Jeff Brown38a7fab2010-08-30 03:02:23 -0700759 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700760 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700761 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700762 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700763 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700764 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700765 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700766 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700767 virtual void parseCalibration();
768 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700769 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700770
771 enum TouchResult {
772 // Dispatch the touch normally.
773 DISPATCH_TOUCH,
774 // Do not dispatch the touch, but keep tracking the current stroke.
775 SKIP_TOUCH,
776 // Do not dispatch the touch, and drop all information associated with the current stoke
777 // so the next movement will appear as a new down.
778 DROP_STROKE
779 };
780
781 void syncTouch(nsecs_t when, bool havePointerIds);
782
783private:
784 /* Maximum number of historical samples to average. */
785 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
786
787 /* Slop distance for jumpy pointer detection.
788 * The vertical range of the screen divided by this is our epsilon value. */
789 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
790
791 /* Number of jumpy points to drop for touchscreens that need it. */
792 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
793 static const uint32_t JUMPY_DROP_LIMIT = 3;
794
795 /* Maximum squared distance for averaging.
796 * If moving farther than this, turn of averaging to avoid lag in response. */
797 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
798
799 struct AveragingTouchFilterState {
800 // Individual history tracks are stored by pointer id
801 uint32_t historyStart[MAX_POINTERS];
802 uint32_t historyEnd[MAX_POINTERS];
803 struct {
804 struct {
805 int32_t x;
806 int32_t y;
807 int32_t pressure;
808 } pointers[MAX_POINTERS];
809 } historyData[AVERAGING_HISTORY_SIZE];
810 } mAveragingTouchFilter;
811
Jeff Brownd64c8552010-08-17 20:38:35 -0700812 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700813 uint32_t jumpyPointsDropped;
814 } mJumpyTouchFilter;
815
816 struct PointerDistanceHeapElement {
817 uint32_t currentPointerIndex : 8;
818 uint32_t lastPointerIndex : 8;
819 uint64_t distance : 48; // squared distance
820 };
821
Jeff Brownb51719b2010-07-29 18:18:33 -0700822 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700823
824 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
825 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
826 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700827 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
828 int32_t motionEventAction);
Jeff Brown7bfdb292010-10-24 14:39:33 -0700829 void detectGestures(nsecs_t when);
Jeff Browne57e8952010-07-23 21:28:06 -0700830
Jeff Brownb51719b2010-07-29 18:18:33 -0700831 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
832 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700833
834 bool applyBadTouchFilter();
835 bool applyJumpyTouchFilter();
836 void applyAveragingTouchFilter();
837 void calculatePointerIds();
838};
839
840
841class SingleTouchInputMapper : public TouchInputMapper {
842public:
843 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
844 virtual ~SingleTouchInputMapper();
845
846 virtual void reset();
847 virtual void process(const RawEvent* rawEvent);
848
849protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700850 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700851
852private:
853 struct Accumulator {
854 enum {
855 FIELD_BTN_TOUCH = 1,
856 FIELD_ABS_X = 2,
857 FIELD_ABS_Y = 4,
858 FIELD_ABS_PRESSURE = 8,
859 FIELD_ABS_TOOL_WIDTH = 16
860 };
861
862 uint32_t fields;
863
864 bool btnTouch;
865 int32_t absX;
866 int32_t absY;
867 int32_t absPressure;
868 int32_t absToolWidth;
869
870 inline void clear() {
871 fields = 0;
872 }
Jeff Browne57e8952010-07-23 21:28:06 -0700873 } mAccumulator;
874
875 bool mDown;
876 int32_t mX;
877 int32_t mY;
878 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700879 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700880
881 void initialize();
882
883 void sync(nsecs_t when);
884};
885
886
887class MultiTouchInputMapper : public TouchInputMapper {
888public:
889 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
890 virtual ~MultiTouchInputMapper();
891
892 virtual void reset();
893 virtual void process(const RawEvent* rawEvent);
894
895protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700896 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700897
898private:
899 struct Accumulator {
900 enum {
901 FIELD_ABS_MT_POSITION_X = 1,
902 FIELD_ABS_MT_POSITION_Y = 2,
903 FIELD_ABS_MT_TOUCH_MAJOR = 4,
904 FIELD_ABS_MT_TOUCH_MINOR = 8,
905 FIELD_ABS_MT_WIDTH_MAJOR = 16,
906 FIELD_ABS_MT_WIDTH_MINOR = 32,
907 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700908 FIELD_ABS_MT_TRACKING_ID = 128,
909 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700910 };
911
912 uint32_t pointerCount;
913 struct Pointer {
914 uint32_t fields;
915
916 int32_t absMTPositionX;
917 int32_t absMTPositionY;
918 int32_t absMTTouchMajor;
919 int32_t absMTTouchMinor;
920 int32_t absMTWidthMajor;
921 int32_t absMTWidthMinor;
922 int32_t absMTOrientation;
923 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700924 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700925
926 inline void clear() {
927 fields = 0;
928 }
929 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
930
931 inline void clear() {
932 pointerCount = 0;
933 pointers[0].clear();
934 }
Jeff Browne57e8952010-07-23 21:28:06 -0700935 } mAccumulator;
936
937 void initialize();
938
939 void sync(nsecs_t when);
940};
941
Jeff Browne839a582010-04-22 18:58:52 -0700942} // namespace android
943
944#endif // _UI_INPUT_READER_H