blob: 8ec54219405914c5129e1dca81ecb5024aacfaf8 [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
Jeff Brown54bc2812010-06-15 01:31:58 -070039/*
40 * Input reader policy interface.
41 *
42 * The input reader policy is used by the input reader to interact with the Window Manager
43 * and other system components.
44 *
45 * The actual implementation is partially supported by callbacks into the DVM
46 * via JNI. This interface is also mocked in the unit tests.
47 */
48class InputReaderPolicyInterface : public virtual RefBase {
49protected:
50 InputReaderPolicyInterface() { }
51 virtual ~InputReaderPolicyInterface() { }
52
53public:
54 /* Display orientations. */
55 enum {
56 ROTATION_0 = 0,
57 ROTATION_90 = 1,
58 ROTATION_180 = 2,
59 ROTATION_270 = 3
60 };
61
Jeff Brown54bc2812010-06-15 01:31:58 -070062 /* Gets information about the display with the specified id.
63 * Returns true if the display info is available, false otherwise.
64 */
65 virtual bool getDisplayInfo(int32_t displayId,
66 int32_t* width, int32_t* height, int32_t* orientation) = 0;
67
Jeff Brown54bc2812010-06-15 01:31:58 -070068 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
69 * certain device whose screen currently is not all that good.
70 */
71 virtual bool filterTouchEvents() = 0;
72
73 /* Determines whether to turn on some hacks to improve touch interaction with another device
74 * where touch coordinate data can get corrupted.
75 */
76 virtual bool filterJumpyTouchEvents() = 0;
77
Jeff Brown54bc2812010-06-15 01:31:58 -070078 /* Gets the excluded device names for the platform. */
79 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
80};
81
82
83/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -070084class InputReaderInterface : public virtual RefBase {
85protected:
86 InputReaderInterface() { }
87 virtual ~InputReaderInterface() { }
88
89public:
Jeff Browna665ca82010-09-08 11:49:43 -070090 /* Dumps the state of the input reader.
91 *
92 * This method may be called on any thread (usually by the input manager). */
93 virtual void dump(String8& dump) = 0;
94
Jeff Browne839a582010-04-22 18:58:52 -070095 /* Runs a single iteration of the processing loop.
96 * Nominally reads and processes one incoming message from the EventHub.
97 *
98 * This method should be called on the input reader thread.
99 */
100 virtual void loopOnce() = 0;
101
Jeff Brown54bc2812010-06-15 01:31:58 -0700102 /* Gets the current input device configuration.
103 *
104 * This method may be called on any thread (usually by the input manager).
105 */
Jeff Browne57e8952010-07-23 21:28:06 -0700106 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700107
Jeff Browne57e8952010-07-23 21:28:06 -0700108 /* Gets information about the specified input device.
109 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
110 * was no such device.
111 *
112 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700113 */
Jeff Browne57e8952010-07-23 21:28:06 -0700114 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
115
116 /* Gets the list of all registered device ids. */
117 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
118
119 /* Query current input state. */
120 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
121 int32_t scanCode) = 0;
122 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
123 int32_t keyCode) = 0;
124 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
125 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700126
127 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700128 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
129 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
130};
131
132
133/* Internal interface used by individual input devices to access global input device state
134 * and parameters maintained by the input reader.
135 */
136class InputReaderContext {
Jeff Brown3c3cc622010-10-20 15:33:38 -0700137public:
Jeff Browne57e8952010-07-23 21:28:06 -0700138 InputReaderContext() { }
139 virtual ~InputReaderContext() { }
140
Jeff Browne57e8952010-07-23 21:28:06 -0700141 virtual void updateGlobalMetaState() = 0;
142 virtual int32_t getGlobalMetaState() = 0;
143
144 virtual InputReaderPolicyInterface* getPolicy() = 0;
145 virtual InputDispatcherInterface* getDispatcher() = 0;
146 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700147};
148
Jeff Brown54bc2812010-06-15 01:31:58 -0700149
Jeff Browne839a582010-04-22 18:58:52 -0700150/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700151 * that it sends to the input dispatcher. Some functions of the input reader, such as early
152 * event filtering in low power states, are controlled by a separate policy object.
153 *
154 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700155 * Because the policy and dispatcher can potentially block or cause re-entrance into
156 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700157 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700158 */
Jeff Brown3c3cc622010-10-20 15:33:38 -0700159class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700160public:
161 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700162 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700163 const sp<InputDispatcherInterface>& dispatcher);
164 virtual ~InputReader();
165
Jeff Browna665ca82010-09-08 11:49:43 -0700166 virtual void dump(String8& dump);
167
Jeff Browne839a582010-04-22 18:58:52 -0700168 virtual void loopOnce();
169
Jeff Browne57e8952010-07-23 21:28:06 -0700170 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700171
Jeff Browne57e8952010-07-23 21:28:06 -0700172 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
173 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700174
Jeff Browne57e8952010-07-23 21:28:06 -0700175 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
176 int32_t scanCode);
177 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
178 int32_t keyCode);
179 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
180 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700181
Jeff Browne57e8952010-07-23 21:28:06 -0700182 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
183 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700184
Jeff Brown3c3cc622010-10-20 15:33:38 -0700185protected:
186 // These methods are protected virtual so they can be overridden and instrumented
187 // by test cases.
188 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
189
Jeff Browne839a582010-04-22 18:58:52 -0700190private:
Jeff Browne839a582010-04-22 18:58:52 -0700191 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700192 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700193 sp<InputDispatcherInterface> mDispatcher;
194
Jeff Browne57e8952010-07-23 21:28:06 -0700195 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
196 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
197 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
198
199 // This reader/writer lock guards the list of input devices.
200 // The writer lock must be held whenever the list of input devices is modified
201 // and then promptly released.
202 // The reader lock must be held whenever the list of input devices is traversed or an
203 // input device in the list is accessed.
204 // This lock only protects the registry and prevents inadvertent deletion of device objects
205 // that are in use. Individual devices are responsible for guarding their own internal state
206 // as needed for concurrent operation.
207 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700208 KeyedVector<int32_t, InputDevice*> mDevices;
209
Jeff Browne57e8952010-07-23 21:28:06 -0700210 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700211 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700212
Jeff Brown1ad00e92010-10-01 18:55:43 -0700213 void addDevice(int32_t deviceId);
214 void removeDevice(int32_t deviceId);
Jeff Brown54bc2812010-06-15 01:31:58 -0700215 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700216
Jeff Browne57e8952010-07-23 21:28:06 -0700217 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700218
Jeff Brown3c3cc622010-10-20 15:33:38 -0700219 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700220
Jeff Browne57e8952010-07-23 21:28:06 -0700221 // state management for all devices
222 Mutex mStateLock;
223
224 int32_t mGlobalMetaState;
225 virtual void updateGlobalMetaState();
226 virtual int32_t getGlobalMetaState();
227
228 InputConfiguration mInputConfiguration;
229 void updateInputConfiguration();
230
231 // state queries
232 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
233 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
234 GetStateFunc getStateFunc);
235 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
236 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700237};
238
239
240/* Reads raw events from the event hub and processes them, endlessly. */
241class InputReaderThread : public Thread {
242public:
243 InputReaderThread(const sp<InputReaderInterface>& reader);
244 virtual ~InputReaderThread();
245
246private:
247 sp<InputReaderInterface> mReader;
248
249 virtual bool threadLoop();
250};
251
Jeff Browne57e8952010-07-23 21:28:06 -0700252
253/* Represents the state of a single input device. */
254class InputDevice {
255public:
256 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
257 ~InputDevice();
258
259 inline InputReaderContext* getContext() { return mContext; }
260 inline int32_t getId() { return mId; }
261 inline const String8& getName() { return mName; }
262 inline uint32_t getSources() { return mSources; }
263
264 inline bool isIgnored() { return mMappers.isEmpty(); }
265
Jeff Brown26c94ff2010-09-30 14:33:04 -0700266 void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700267 void addMapper(InputMapper* mapper);
268 void configure();
269 void reset();
270 void process(const RawEvent* rawEvent);
271
272 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
273 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
274 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
275 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
276 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
277 const int32_t* keyCodes, uint8_t* outFlags);
278
279 int32_t getMetaState();
280
Jeff Brown66888372010-11-29 17:37:49 -0800281 inline const PropertyMap& getConfiguration() {
282 return mConfiguration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700283 }
284
Jeff Browne57e8952010-07-23 21:28:06 -0700285private:
286 InputReaderContext* mContext;
287 int32_t mId;
288
289 Vector<InputMapper*> mMappers;
290
291 String8 mName;
292 uint32_t mSources;
293
294 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
295 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700296
Jeff Brown66888372010-11-29 17:37:49 -0800297 PropertyMap mConfiguration;
Jeff Browne57e8952010-07-23 21:28:06 -0700298};
299
300
301/* An input mapper transforms raw input events into cooked event data.
302 * A single input device can have multiple associated input mappers in order to interpret
303 * different classes of events.
304 */
305class InputMapper {
306public:
307 InputMapper(InputDevice* device);
308 virtual ~InputMapper();
309
310 inline InputDevice* getDevice() { return mDevice; }
311 inline int32_t getDeviceId() { return mDevice->getId(); }
312 inline const String8 getDeviceName() { return mDevice->getName(); }
313 inline InputReaderContext* getContext() { return mContext; }
314 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
315 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
316 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
317
318 virtual uint32_t getSources() = 0;
319 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700320 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700321 virtual void configure();
322 virtual void reset();
323 virtual void process(const RawEvent* rawEvent) = 0;
324
325 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
326 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
327 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
328 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
329 const int32_t* keyCodes, uint8_t* outFlags);
330
331 virtual int32_t getMetaState();
332
333protected:
334 InputDevice* mDevice;
335 InputReaderContext* mContext;
Jeff Browne57e8952010-07-23 21:28:06 -0700336};
337
338
339class SwitchInputMapper : public InputMapper {
340public:
341 SwitchInputMapper(InputDevice* device);
342 virtual ~SwitchInputMapper();
343
344 virtual uint32_t getSources();
345 virtual void process(const RawEvent* rawEvent);
346
347 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
348
349private:
350 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
351};
352
353
354class KeyboardInputMapper : public InputMapper {
355public:
Jeff Brown66888372010-11-29 17:37:49 -0800356 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Browne57e8952010-07-23 21:28:06 -0700357 virtual ~KeyboardInputMapper();
358
359 virtual uint32_t getSources();
360 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700361 virtual void dump(String8& dump);
Jeff Brown66888372010-11-29 17:37:49 -0800362 virtual void configure();
Jeff Browne57e8952010-07-23 21:28:06 -0700363 virtual void reset();
364 virtual void process(const RawEvent* rawEvent);
365
366 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
367 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
368 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
369 const int32_t* keyCodes, uint8_t* outFlags);
370
371 virtual int32_t getMetaState();
372
373private:
Jeff Brownb51719b2010-07-29 18:18:33 -0700374 Mutex mLock;
375
Jeff Browne57e8952010-07-23 21:28:06 -0700376 struct KeyDown {
377 int32_t keyCode;
378 int32_t scanCode;
379 };
380
Jeff Browne57e8952010-07-23 21:28:06 -0700381 uint32_t mSources;
382 int32_t mKeyboardType;
383
Jeff Brown66888372010-11-29 17:37:49 -0800384 // Immutable configuration parameters.
385 struct Parameters {
386 int32_t associatedDisplayId;
387 bool orientationAware;
388 } mParameters;
389
Jeff Brownb51719b2010-07-29 18:18:33 -0700390 struct LockedState {
391 Vector<KeyDown> keyDowns; // keys that are down
392 int32_t metaState;
393 nsecs_t downTime; // time of most recent key down
Jeff Brown6a817e22010-09-12 17:55:08 -0700394
395 struct LedState {
396 bool avail; // led is available
397 bool on; // we think the led is currently on
398 };
399 LedState capsLockLedState;
400 LedState numLockLedState;
401 LedState scrollLockLedState;
Jeff Brownb51719b2010-07-29 18:18:33 -0700402 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700403
Jeff Brownb51719b2010-07-29 18:18:33 -0700404 void initializeLocked();
Jeff Brown6a817e22010-09-12 17:55:08 -0700405 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Browne57e8952010-07-23 21:28:06 -0700406
Jeff Brown66888372010-11-29 17:37:49 -0800407 void configureParameters();
408 void dumpParameters(String8& dump);
409
Jeff Browne57e8952010-07-23 21:28:06 -0700410 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700411
Jeff Browne57e8952010-07-23 21:28:06 -0700412 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
413 uint32_t policyFlags);
414
Jeff Brownb51719b2010-07-29 18:18:33 -0700415 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6a817e22010-09-12 17:55:08 -0700416
417 void updateLedStateLocked(bool reset);
418 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
419 int32_t modifier, bool reset);
Jeff Browne57e8952010-07-23 21:28:06 -0700420};
421
422
423class TrackballInputMapper : public InputMapper {
424public:
Jeff Brown66888372010-11-29 17:37:49 -0800425 TrackballInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700426 virtual ~TrackballInputMapper();
427
428 virtual uint32_t getSources();
429 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700430 virtual void dump(String8& dump);
Jeff Brown66888372010-11-29 17:37:49 -0800431 virtual void configure();
Jeff Browne57e8952010-07-23 21:28:06 -0700432 virtual void reset();
433 virtual void process(const RawEvent* rawEvent);
434
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700435 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
436
Jeff Browne57e8952010-07-23 21:28:06 -0700437private:
438 // Amount that trackball needs to move in order to generate a key event.
439 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
440
Jeff Brownb51719b2010-07-29 18:18:33 -0700441 Mutex mLock;
442
Jeff Brown66888372010-11-29 17:37:49 -0800443 // Immutable configuration parameters.
444 struct Parameters {
445 int32_t associatedDisplayId;
446 bool orientationAware;
447 } mParameters;
Jeff Browne57e8952010-07-23 21:28:06 -0700448
449 struct Accumulator {
450 enum {
451 FIELD_BTN_MOUSE = 1,
452 FIELD_REL_X = 2,
453 FIELD_REL_Y = 4
454 };
455
456 uint32_t fields;
457
458 bool btnMouse;
459 int32_t relX;
460 int32_t relY;
461
462 inline void clear() {
463 fields = 0;
464 }
Jeff Browne57e8952010-07-23 21:28:06 -0700465 } mAccumulator;
466
Jeff Browne57e8952010-07-23 21:28:06 -0700467 float mXScale;
468 float mYScale;
469 float mXPrecision;
470 float mYPrecision;
471
Jeff Brownb51719b2010-07-29 18:18:33 -0700472 struct LockedState {
473 bool down;
474 nsecs_t downTime;
475 } mLocked;
476
477 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700478
Jeff Brown66888372010-11-29 17:37:49 -0800479 void configureParameters();
480 void dumpParameters(String8& dump);
481
Jeff Browne57e8952010-07-23 21:28:06 -0700482 void sync(nsecs_t when);
483};
484
485
486class TouchInputMapper : public InputMapper {
487public:
Jeff Brown66888372010-11-29 17:37:49 -0800488 TouchInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700489 virtual ~TouchInputMapper();
490
491 virtual uint32_t getSources();
492 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700493 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700494 virtual void configure();
495 virtual void reset();
496
497 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
498 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
499 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
500 const int32_t* keyCodes, uint8_t* outFlags);
501
502protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700503 Mutex mLock;
504
Jeff Browne57e8952010-07-23 21:28:06 -0700505 struct VirtualKey {
506 int32_t keyCode;
507 int32_t scanCode;
508 uint32_t flags;
509
510 // computed hit box, specified in touch screen coords based on known display size
511 int32_t hitLeft;
512 int32_t hitTop;
513 int32_t hitRight;
514 int32_t hitBottom;
515
516 inline bool isHit(int32_t x, int32_t y) const {
517 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
518 }
519 };
520
Jeff Brown38a7fab2010-08-30 03:02:23 -0700521 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700522 struct PointerData {
523 uint32_t id;
524 int32_t x;
525 int32_t y;
526 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700527 int32_t touchMajor;
528 int32_t touchMinor;
529 int32_t toolMajor;
530 int32_t toolMinor;
531 int32_t orientation;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700532
533 inline bool operator== (const PointerData& other) const {
534 return id == other.id
535 && x == other.x
536 && y == other.y
537 && pressure == other.pressure
538 && touchMajor == other.touchMajor
539 && touchMinor == other.touchMinor
540 && toolMajor == other.toolMajor
541 && toolMinor == other.toolMinor
542 && orientation == other.orientation;
543 }
544 inline bool operator!= (const PointerData& other) const {
545 return !(*this == other);
546 }
Jeff Browne57e8952010-07-23 21:28:06 -0700547 };
548
Jeff Brown38a7fab2010-08-30 03:02:23 -0700549 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700550 struct TouchData {
551 uint32_t pointerCount;
552 PointerData pointers[MAX_POINTERS];
553 BitSet32 idBits;
554 uint32_t idToIndex[MAX_POINTER_ID + 1];
555
556 void copyFrom(const TouchData& other) {
557 pointerCount = other.pointerCount;
558 idBits = other.idBits;
559
560 for (uint32_t i = 0; i < pointerCount; i++) {
561 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700562
563 int id = pointers[i].id;
564 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700565 }
566 }
567
568 inline void clear() {
569 pointerCount = 0;
570 idBits.clear();
571 }
572 };
573
Jeff Browne57e8952010-07-23 21:28:06 -0700574 // Immutable configuration parameters.
575 struct Parameters {
Jeff Brown66888372010-11-29 17:37:49 -0800576 enum DeviceType {
577 DEVICE_TYPE_TOUCH_SCREEN,
578 DEVICE_TYPE_TOUCH_PAD,
579 };
580
581 DeviceType deviceType;
582 int32_t associatedDisplayId;
583 bool orientationAware;
584
Jeff Browne57e8952010-07-23 21:28:06 -0700585 bool useBadTouchFilter;
586 bool useJumpyTouchFilter;
587 bool useAveragingTouchFilter;
588 } mParameters;
589
Jeff Brown38a7fab2010-08-30 03:02:23 -0700590 // Immutable calibration parameters in parsed form.
591 struct Calibration {
Jeff Brown60b57762010-10-18 13:32:20 -0700592 // Position
593 bool haveXOrigin;
594 int32_t xOrigin;
595 bool haveYOrigin;
596 int32_t yOrigin;
597 bool haveXScale;
598 float xScale;
599 bool haveYScale;
600 float yScale;
601
Jeff Brown6b337e72010-10-14 21:42:15 -0700602 // Touch Size
603 enum TouchSizeCalibration {
604 TOUCH_SIZE_CALIBRATION_DEFAULT,
605 TOUCH_SIZE_CALIBRATION_NONE,
606 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
607 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700608 };
609
Jeff Brown6b337e72010-10-14 21:42:15 -0700610 TouchSizeCalibration touchSizeCalibration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700611
Jeff Brown6b337e72010-10-14 21:42:15 -0700612 // Tool Size
613 enum ToolSizeCalibration {
614 TOOL_SIZE_CALIBRATION_DEFAULT,
615 TOOL_SIZE_CALIBRATION_NONE,
616 TOOL_SIZE_CALIBRATION_GEOMETRIC,
617 TOOL_SIZE_CALIBRATION_LINEAR,
618 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700619 };
620
Jeff Brown6b337e72010-10-14 21:42:15 -0700621 ToolSizeCalibration toolSizeCalibration;
622 bool haveToolSizeLinearScale;
623 float toolSizeLinearScale;
624 bool haveToolSizeLinearBias;
625 float toolSizeLinearBias;
626 bool haveToolSizeAreaScale;
627 float toolSizeAreaScale;
628 bool haveToolSizeAreaBias;
629 float toolSizeAreaBias;
630 bool haveToolSizeIsSummed;
Jeff Brown66888372010-11-29 17:37:49 -0800631 bool toolSizeIsSummed;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700632
633 // Pressure
634 enum PressureCalibration {
635 PRESSURE_CALIBRATION_DEFAULT,
636 PRESSURE_CALIBRATION_NONE,
637 PRESSURE_CALIBRATION_PHYSICAL,
638 PRESSURE_CALIBRATION_AMPLITUDE,
639 };
640 enum PressureSource {
641 PRESSURE_SOURCE_DEFAULT,
642 PRESSURE_SOURCE_PRESSURE,
643 PRESSURE_SOURCE_TOUCH,
644 };
645
646 PressureCalibration pressureCalibration;
647 PressureSource pressureSource;
648 bool havePressureScale;
649 float pressureScale;
650
651 // Size
652 enum SizeCalibration {
653 SIZE_CALIBRATION_DEFAULT,
654 SIZE_CALIBRATION_NONE,
655 SIZE_CALIBRATION_NORMALIZED,
656 };
657
658 SizeCalibration sizeCalibration;
659
660 // Orientation
661 enum OrientationCalibration {
662 ORIENTATION_CALIBRATION_DEFAULT,
663 ORIENTATION_CALIBRATION_NONE,
664 ORIENTATION_CALIBRATION_INTERPOLATED,
665 };
666
667 OrientationCalibration orientationCalibration;
668 } mCalibration;
669
670 // Raw axis information from the driver.
671 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700672 RawAbsoluteAxisInfo x;
673 RawAbsoluteAxisInfo y;
674 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700675 RawAbsoluteAxisInfo touchMajor;
676 RawAbsoluteAxisInfo touchMinor;
677 RawAbsoluteAxisInfo toolMajor;
678 RawAbsoluteAxisInfo toolMinor;
679 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700680 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700681
Jeff Brownb51719b2010-07-29 18:18:33 -0700682 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700683 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700684 TouchData mLastTouch;
685
686 // The time the primary pointer last went down.
687 nsecs_t mDownTime;
688
Jeff Brownb51719b2010-07-29 18:18:33 -0700689 struct LockedState {
690 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700691
Jeff Brownb51719b2010-07-29 18:18:33 -0700692 // The surface orientation and width and height set by configureSurfaceLocked().
693 int32_t surfaceOrientation;
694 int32_t surfaceWidth, surfaceHeight;
695
696 // Translation and scaling factors, orientation-independent.
697 int32_t xOrigin;
698 float xScale;
699 float xPrecision;
700
701 int32_t yOrigin;
702 float yScale;
703 float yPrecision;
704
Jeff Brown38a7fab2010-08-30 03:02:23 -0700705 float geometricScale;
706
Jeff Brown6b337e72010-10-14 21:42:15 -0700707 float toolSizeLinearScale;
708 float toolSizeLinearBias;
709 float toolSizeAreaScale;
710 float toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700711
Jeff Brownb51719b2010-07-29 18:18:33 -0700712 float pressureScale;
713
Jeff Brownb51719b2010-07-29 18:18:33 -0700714 float sizeScale;
715
716 float orientationScale;
717
718 // Oriented motion ranges for input device info.
719 struct OrientedRanges {
720 InputDeviceInfo::MotionRange x;
721 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700722
723 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700724 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700725
726 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700727 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700728
Jeff Brown6b337e72010-10-14 21:42:15 -0700729 bool haveTouchSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700730 InputDeviceInfo::MotionRange touchMajor;
731 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700732
Jeff Brown6b337e72010-10-14 21:42:15 -0700733 bool haveToolSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700734 InputDeviceInfo::MotionRange toolMajor;
735 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700736
737 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700738 InputDeviceInfo::MotionRange orientation;
739 } orientedRanges;
740
741 // Oriented dimensions and precision.
742 float orientedSurfaceWidth, orientedSurfaceHeight;
743 float orientedXPrecision, orientedYPrecision;
744
745 struct CurrentVirtualKeyState {
746 bool down;
747 nsecs_t downTime;
748 int32_t keyCode;
749 int32_t scanCode;
750 } currentVirtualKey;
751 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700752
Jeff Brown38a7fab2010-08-30 03:02:23 -0700753 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700754 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700755 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700756 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700757 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700758 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700759 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700760 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700761 virtual void parseCalibration();
762 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700763 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700764
765 enum TouchResult {
766 // Dispatch the touch normally.
767 DISPATCH_TOUCH,
768 // Do not dispatch the touch, but keep tracking the current stroke.
769 SKIP_TOUCH,
770 // Do not dispatch the touch, and drop all information associated with the current stoke
771 // so the next movement will appear as a new down.
772 DROP_STROKE
773 };
774
775 void syncTouch(nsecs_t when, bool havePointerIds);
776
777private:
778 /* Maximum number of historical samples to average. */
779 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
780
781 /* Slop distance for jumpy pointer detection.
782 * The vertical range of the screen divided by this is our epsilon value. */
783 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
784
785 /* Number of jumpy points to drop for touchscreens that need it. */
786 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
787 static const uint32_t JUMPY_DROP_LIMIT = 3;
788
789 /* Maximum squared distance for averaging.
790 * If moving farther than this, turn of averaging to avoid lag in response. */
791 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
792
793 struct AveragingTouchFilterState {
794 // Individual history tracks are stored by pointer id
795 uint32_t historyStart[MAX_POINTERS];
796 uint32_t historyEnd[MAX_POINTERS];
797 struct {
798 struct {
799 int32_t x;
800 int32_t y;
801 int32_t pressure;
802 } pointers[MAX_POINTERS];
803 } historyData[AVERAGING_HISTORY_SIZE];
804 } mAveragingTouchFilter;
805
Jeff Brownd64c8552010-08-17 20:38:35 -0700806 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700807 uint32_t jumpyPointsDropped;
808 } mJumpyTouchFilter;
809
810 struct PointerDistanceHeapElement {
811 uint32_t currentPointerIndex : 8;
812 uint32_t lastPointerIndex : 8;
813 uint64_t distance : 48; // squared distance
814 };
815
Jeff Brownb51719b2010-07-29 18:18:33 -0700816 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700817
818 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
819 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
820 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700821 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
822 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700823
Jeff Brownb51719b2010-07-29 18:18:33 -0700824 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
825 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700826
827 bool applyBadTouchFilter();
828 bool applyJumpyTouchFilter();
829 void applyAveragingTouchFilter();
830 void calculatePointerIds();
831};
832
833
834class SingleTouchInputMapper : public TouchInputMapper {
835public:
Jeff Brown66888372010-11-29 17:37:49 -0800836 SingleTouchInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700837 virtual ~SingleTouchInputMapper();
838
839 virtual void reset();
840 virtual void process(const RawEvent* rawEvent);
841
842protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700843 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700844
845private:
846 struct Accumulator {
847 enum {
848 FIELD_BTN_TOUCH = 1,
849 FIELD_ABS_X = 2,
850 FIELD_ABS_Y = 4,
851 FIELD_ABS_PRESSURE = 8,
852 FIELD_ABS_TOOL_WIDTH = 16
853 };
854
855 uint32_t fields;
856
857 bool btnTouch;
858 int32_t absX;
859 int32_t absY;
860 int32_t absPressure;
861 int32_t absToolWidth;
862
863 inline void clear() {
864 fields = 0;
865 }
Jeff Browne57e8952010-07-23 21:28:06 -0700866 } mAccumulator;
867
868 bool mDown;
869 int32_t mX;
870 int32_t mY;
871 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700872 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700873
874 void initialize();
875
876 void sync(nsecs_t when);
877};
878
879
880class MultiTouchInputMapper : public TouchInputMapper {
881public:
Jeff Brown66888372010-11-29 17:37:49 -0800882 MultiTouchInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700883 virtual ~MultiTouchInputMapper();
884
885 virtual void reset();
886 virtual void process(const RawEvent* rawEvent);
887
888protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700889 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700890
891private:
892 struct Accumulator {
893 enum {
894 FIELD_ABS_MT_POSITION_X = 1,
895 FIELD_ABS_MT_POSITION_Y = 2,
896 FIELD_ABS_MT_TOUCH_MAJOR = 4,
897 FIELD_ABS_MT_TOUCH_MINOR = 8,
898 FIELD_ABS_MT_WIDTH_MAJOR = 16,
899 FIELD_ABS_MT_WIDTH_MINOR = 32,
900 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700901 FIELD_ABS_MT_TRACKING_ID = 128,
902 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700903 };
904
905 uint32_t pointerCount;
906 struct Pointer {
907 uint32_t fields;
908
909 int32_t absMTPositionX;
910 int32_t absMTPositionY;
911 int32_t absMTTouchMajor;
912 int32_t absMTTouchMinor;
913 int32_t absMTWidthMajor;
914 int32_t absMTWidthMinor;
915 int32_t absMTOrientation;
916 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700917 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700918
919 inline void clear() {
920 fields = 0;
921 }
922 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
923
924 inline void clear() {
925 pointerCount = 0;
926 pointers[0].clear();
927 }
Jeff Browne57e8952010-07-23 21:28:06 -0700928 } mAccumulator;
929
930 void initialize();
931
932 void sync(nsecs_t when);
933};
934
Jeff Browne839a582010-04-22 18:58:52 -0700935} // namespace android
936
937#endif // _UI_INPUT_READER_H