blob: 2d4bf8bb1587a9496c9785721875c4fc3e4a9818 [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
106 /* Gets the configured virtual key definitions for an input device. */
107 virtual void getVirtualKeyDefinitions(const String8& deviceName,
108 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
109
Jeff Brown38a7fab2010-08-30 03:02:23 -0700110 /* Gets the calibration for an input device. */
111 virtual void getInputDeviceCalibration(const String8& deviceName,
112 InputDeviceCalibration& outCalibration) = 0;
113
Jeff Brown54bc2812010-06-15 01:31:58 -0700114 /* Gets the excluded device names for the platform. */
115 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
116};
117
118
119/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700120class InputReaderInterface : public virtual RefBase {
121protected:
122 InputReaderInterface() { }
123 virtual ~InputReaderInterface() { }
124
125public:
Jeff Browna665ca82010-09-08 11:49:43 -0700126 /* Dumps the state of the input reader.
127 *
128 * This method may be called on any thread (usually by the input manager). */
129 virtual void dump(String8& dump) = 0;
130
Jeff Browne839a582010-04-22 18:58:52 -0700131 /* Runs a single iteration of the processing loop.
132 * Nominally reads and processes one incoming message from the EventHub.
133 *
134 * This method should be called on the input reader thread.
135 */
136 virtual void loopOnce() = 0;
137
Jeff Brown54bc2812010-06-15 01:31:58 -0700138 /* Gets the current input device configuration.
139 *
140 * This method may be called on any thread (usually by the input manager).
141 */
Jeff Browne57e8952010-07-23 21:28:06 -0700142 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700143
Jeff Browne57e8952010-07-23 21:28:06 -0700144 /* Gets information about the specified input device.
145 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
146 * was no such device.
147 *
148 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700149 */
Jeff Browne57e8952010-07-23 21:28:06 -0700150 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
151
152 /* Gets the list of all registered device ids. */
153 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
154
155 /* Query current input state. */
156 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
157 int32_t scanCode) = 0;
158 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
159 int32_t keyCode) = 0;
160 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
161 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700162
163 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700164 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
165 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
166};
167
168
169/* Internal interface used by individual input devices to access global input device state
170 * and parameters maintained by the input reader.
171 */
172class InputReaderContext {
173protected:
174 InputReaderContext() { }
175 virtual ~InputReaderContext() { }
176
177public:
178 virtual void updateGlobalMetaState() = 0;
179 virtual int32_t getGlobalMetaState() = 0;
180
181 virtual InputReaderPolicyInterface* getPolicy() = 0;
182 virtual InputDispatcherInterface* getDispatcher() = 0;
183 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700184};
185
Jeff Brown54bc2812010-06-15 01:31:58 -0700186
Jeff Browne839a582010-04-22 18:58:52 -0700187/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700188 * that it sends to the input dispatcher. Some functions of the input reader, such as early
189 * event filtering in low power states, are controlled by a separate policy object.
190 *
191 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700192 * Because the policy and dispatcher can potentially block or cause re-entrance into
193 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700194 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700195 */
Jeff Browne57e8952010-07-23 21:28:06 -0700196class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700197public:
198 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700199 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700200 const sp<InputDispatcherInterface>& dispatcher);
201 virtual ~InputReader();
202
Jeff Browna665ca82010-09-08 11:49:43 -0700203 virtual void dump(String8& dump);
204
Jeff Browne839a582010-04-22 18:58:52 -0700205 virtual void loopOnce();
206
Jeff Browne57e8952010-07-23 21:28:06 -0700207 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700208
Jeff Browne57e8952010-07-23 21:28:06 -0700209 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
210 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700211
Jeff Browne57e8952010-07-23 21:28:06 -0700212 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
213 int32_t scanCode);
214 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
215 int32_t keyCode);
216 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
217 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700218
Jeff Browne57e8952010-07-23 21:28:06 -0700219 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
220 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700221
Jeff Browne839a582010-04-22 18:58:52 -0700222private:
Jeff Browne839a582010-04-22 18:58:52 -0700223 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700224 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700225 sp<InputDispatcherInterface> mDispatcher;
226
Jeff Browne57e8952010-07-23 21:28:06 -0700227 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
228 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
229 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
230
231 // This reader/writer lock guards the list of input devices.
232 // The writer lock must be held whenever the list of input devices is modified
233 // and then promptly released.
234 // The reader lock must be held whenever the list of input devices is traversed or an
235 // input device in the list is accessed.
236 // This lock only protects the registry and prevents inadvertent deletion of device objects
237 // that are in use. Individual devices are responsible for guarding their own internal state
238 // as needed for concurrent operation.
239 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700240 KeyedVector<int32_t, InputDevice*> mDevices;
241
Jeff Browne57e8952010-07-23 21:28:06 -0700242 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700243 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700244
Jeff Brown1ad00e92010-10-01 18:55:43 -0700245 void addDevice(int32_t deviceId);
246 void removeDevice(int32_t deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700247 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown54bc2812010-06-15 01:31:58 -0700248 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700249
Jeff Browne57e8952010-07-23 21:28:06 -0700250 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700251
Jeff Brown1ad00e92010-10-01 18:55:43 -0700252 void handleConfigurationChanged();
Jeff Brown54bc2812010-06-15 01:31:58 -0700253
Jeff Browne57e8952010-07-23 21:28:06 -0700254 // state management for all devices
255 Mutex mStateLock;
256
257 int32_t mGlobalMetaState;
258 virtual void updateGlobalMetaState();
259 virtual int32_t getGlobalMetaState();
260
261 InputConfiguration mInputConfiguration;
262 void updateInputConfiguration();
263
264 // state queries
265 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
266 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
267 GetStateFunc getStateFunc);
268 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
269 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700270};
271
272
273/* Reads raw events from the event hub and processes them, endlessly. */
274class InputReaderThread : public Thread {
275public:
276 InputReaderThread(const sp<InputReaderInterface>& reader);
277 virtual ~InputReaderThread();
278
279private:
280 sp<InputReaderInterface> mReader;
281
282 virtual bool threadLoop();
283};
284
Jeff Browne57e8952010-07-23 21:28:06 -0700285
286/* Represents the state of a single input device. */
287class InputDevice {
288public:
289 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
290 ~InputDevice();
291
292 inline InputReaderContext* getContext() { return mContext; }
293 inline int32_t getId() { return mId; }
294 inline const String8& getName() { return mName; }
295 inline uint32_t getSources() { return mSources; }
296
297 inline bool isIgnored() { return mMappers.isEmpty(); }
298
Jeff Brown26c94ff2010-09-30 14:33:04 -0700299 void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700300 void addMapper(InputMapper* mapper);
301 void configure();
302 void reset();
303 void process(const RawEvent* rawEvent);
304
305 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
306 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
307 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
308 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
309 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
310 const int32_t* keyCodes, uint8_t* outFlags);
311
312 int32_t getMetaState();
313
Jeff Brown38a7fab2010-08-30 03:02:23 -0700314 inline const InputDeviceCalibration& getCalibration() {
315 return mCalibration;
316 }
317
Jeff Browne57e8952010-07-23 21:28:06 -0700318private:
319 InputReaderContext* mContext;
320 int32_t mId;
321
322 Vector<InputMapper*> mMappers;
323
324 String8 mName;
325 uint32_t mSources;
326
327 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
328 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700329
330 InputDeviceCalibration mCalibration;
Jeff Browne57e8952010-07-23 21:28:06 -0700331};
332
333
334/* An input mapper transforms raw input events into cooked event data.
335 * A single input device can have multiple associated input mappers in order to interpret
336 * different classes of events.
337 */
338class InputMapper {
339public:
340 InputMapper(InputDevice* device);
341 virtual ~InputMapper();
342
343 inline InputDevice* getDevice() { return mDevice; }
344 inline int32_t getDeviceId() { return mDevice->getId(); }
345 inline const String8 getDeviceName() { return mDevice->getName(); }
346 inline InputReaderContext* getContext() { return mContext; }
347 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
348 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
349 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
350
351 virtual uint32_t getSources() = 0;
352 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700353 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700354 virtual void configure();
355 virtual void reset();
356 virtual void process(const RawEvent* rawEvent) = 0;
357
358 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
359 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
360 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
361 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
362 const int32_t* keyCodes, uint8_t* outFlags);
363
364 virtual int32_t getMetaState();
365
366protected:
367 InputDevice* mDevice;
368 InputReaderContext* mContext;
Jeff Browne57e8952010-07-23 21:28:06 -0700369};
370
371
372class SwitchInputMapper : public InputMapper {
373public:
374 SwitchInputMapper(InputDevice* device);
375 virtual ~SwitchInputMapper();
376
377 virtual uint32_t getSources();
378 virtual void process(const RawEvent* rawEvent);
379
380 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
381
382private:
383 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
384};
385
386
387class KeyboardInputMapper : public InputMapper {
388public:
389 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
390 int32_t keyboardType);
391 virtual ~KeyboardInputMapper();
392
393 virtual uint32_t getSources();
394 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700395 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700396 virtual void reset();
397 virtual void process(const RawEvent* rawEvent);
398
399 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
400 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
401 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
402 const int32_t* keyCodes, uint8_t* outFlags);
403
404 virtual int32_t getMetaState();
405
406private:
Jeff Brownb51719b2010-07-29 18:18:33 -0700407 Mutex mLock;
408
Jeff Browne57e8952010-07-23 21:28:06 -0700409 struct KeyDown {
410 int32_t keyCode;
411 int32_t scanCode;
412 };
413
414 int32_t mAssociatedDisplayId;
415 uint32_t mSources;
416 int32_t mKeyboardType;
417
Jeff Brownb51719b2010-07-29 18:18:33 -0700418 struct LockedState {
419 Vector<KeyDown> keyDowns; // keys that are down
420 int32_t metaState;
421 nsecs_t downTime; // time of most recent key down
Jeff Brown6a817e22010-09-12 17:55:08 -0700422
423 struct LedState {
424 bool avail; // led is available
425 bool on; // we think the led is currently on
426 };
427 LedState capsLockLedState;
428 LedState numLockLedState;
429 LedState scrollLockLedState;
Jeff Brownb51719b2010-07-29 18:18:33 -0700430 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700431
Jeff Brownb51719b2010-07-29 18:18:33 -0700432 void initializeLocked();
Jeff Brown6a817e22010-09-12 17:55:08 -0700433 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Browne57e8952010-07-23 21:28:06 -0700434
435 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700436
Jeff Browne57e8952010-07-23 21:28:06 -0700437 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
438 uint32_t policyFlags);
439
Jeff Brownb51719b2010-07-29 18:18:33 -0700440 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6a817e22010-09-12 17:55:08 -0700441
442 void updateLedStateLocked(bool reset);
443 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
444 int32_t modifier, bool reset);
Jeff Browne57e8952010-07-23 21:28:06 -0700445};
446
447
448class TrackballInputMapper : public InputMapper {
449public:
450 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
451 virtual ~TrackballInputMapper();
452
453 virtual uint32_t getSources();
454 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700455 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700456 virtual void reset();
457 virtual void process(const RawEvent* rawEvent);
458
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700459 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
460
Jeff Browne57e8952010-07-23 21:28:06 -0700461private:
462 // Amount that trackball needs to move in order to generate a key event.
463 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
464
Jeff Brownb51719b2010-07-29 18:18:33 -0700465 Mutex mLock;
466
Jeff Browne57e8952010-07-23 21:28:06 -0700467 int32_t mAssociatedDisplayId;
468
469 struct Accumulator {
470 enum {
471 FIELD_BTN_MOUSE = 1,
472 FIELD_REL_X = 2,
473 FIELD_REL_Y = 4
474 };
475
476 uint32_t fields;
477
478 bool btnMouse;
479 int32_t relX;
480 int32_t relY;
481
482 inline void clear() {
483 fields = 0;
484 }
Jeff Browne57e8952010-07-23 21:28:06 -0700485 } mAccumulator;
486
Jeff Browne57e8952010-07-23 21:28:06 -0700487 float mXScale;
488 float mYScale;
489 float mXPrecision;
490 float mYPrecision;
491
Jeff Brownb51719b2010-07-29 18:18:33 -0700492 struct LockedState {
493 bool down;
494 nsecs_t downTime;
495 } mLocked;
496
497 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700498
499 void sync(nsecs_t when);
500};
501
502
503class TouchInputMapper : public InputMapper {
504public:
505 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
506 virtual ~TouchInputMapper();
507
508 virtual uint32_t getSources();
509 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700510 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700511 virtual void configure();
512 virtual void reset();
513
514 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
515 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
516 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
517 const int32_t* keyCodes, uint8_t* outFlags);
518
519protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700520 Mutex mLock;
521
Jeff Browne57e8952010-07-23 21:28:06 -0700522 struct VirtualKey {
523 int32_t keyCode;
524 int32_t scanCode;
525 uint32_t flags;
526
527 // computed hit box, specified in touch screen coords based on known display size
528 int32_t hitLeft;
529 int32_t hitTop;
530 int32_t hitRight;
531 int32_t hitBottom;
532
533 inline bool isHit(int32_t x, int32_t y) const {
534 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
535 }
536 };
537
Jeff Brown38a7fab2010-08-30 03:02:23 -0700538 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700539 struct PointerData {
540 uint32_t id;
541 int32_t x;
542 int32_t y;
543 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700544 int32_t touchMajor;
545 int32_t touchMinor;
546 int32_t toolMajor;
547 int32_t toolMinor;
548 int32_t orientation;
549 };
550
Jeff Brown38a7fab2010-08-30 03:02:23 -0700551 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700552 struct TouchData {
553 uint32_t pointerCount;
554 PointerData pointers[MAX_POINTERS];
555 BitSet32 idBits;
556 uint32_t idToIndex[MAX_POINTER_ID + 1];
557
558 void copyFrom(const TouchData& other) {
559 pointerCount = other.pointerCount;
560 idBits = other.idBits;
561
562 for (uint32_t i = 0; i < pointerCount; i++) {
563 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700564
565 int id = pointers[i].id;
566 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700567 }
568 }
569
570 inline void clear() {
571 pointerCount = 0;
572 idBits.clear();
573 }
574 };
575
576 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700577
578 // Immutable configuration parameters.
579 struct Parameters {
580 bool useBadTouchFilter;
581 bool useJumpyTouchFilter;
582 bool useAveragingTouchFilter;
583 } mParameters;
584
Jeff Brown38a7fab2010-08-30 03:02:23 -0700585 // Immutable calibration parameters in parsed form.
586 struct Calibration {
Jeff Brown60b57762010-10-18 13:32:20 -0700587 // Position
588 bool haveXOrigin;
589 int32_t xOrigin;
590 bool haveYOrigin;
591 int32_t yOrigin;
592 bool haveXScale;
593 float xScale;
594 bool haveYScale;
595 float yScale;
596
Jeff Brown6b337e72010-10-14 21:42:15 -0700597 // Touch Size
598 enum TouchSizeCalibration {
599 TOUCH_SIZE_CALIBRATION_DEFAULT,
600 TOUCH_SIZE_CALIBRATION_NONE,
601 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
602 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700603 };
604
Jeff Brown6b337e72010-10-14 21:42:15 -0700605 TouchSizeCalibration touchSizeCalibration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700606
Jeff Brown6b337e72010-10-14 21:42:15 -0700607 // Tool Size
608 enum ToolSizeCalibration {
609 TOOL_SIZE_CALIBRATION_DEFAULT,
610 TOOL_SIZE_CALIBRATION_NONE,
611 TOOL_SIZE_CALIBRATION_GEOMETRIC,
612 TOOL_SIZE_CALIBRATION_LINEAR,
613 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700614 };
615
Jeff Brown6b337e72010-10-14 21:42:15 -0700616 ToolSizeCalibration toolSizeCalibration;
617 bool haveToolSizeLinearScale;
618 float toolSizeLinearScale;
619 bool haveToolSizeLinearBias;
620 float toolSizeLinearBias;
621 bool haveToolSizeAreaScale;
622 float toolSizeAreaScale;
623 bool haveToolSizeAreaBias;
624 float toolSizeAreaBias;
625 bool haveToolSizeIsSummed;
626 int32_t toolSizeIsSummed;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700627
628 // Pressure
629 enum PressureCalibration {
630 PRESSURE_CALIBRATION_DEFAULT,
631 PRESSURE_CALIBRATION_NONE,
632 PRESSURE_CALIBRATION_PHYSICAL,
633 PRESSURE_CALIBRATION_AMPLITUDE,
634 };
635 enum PressureSource {
636 PRESSURE_SOURCE_DEFAULT,
637 PRESSURE_SOURCE_PRESSURE,
638 PRESSURE_SOURCE_TOUCH,
639 };
640
641 PressureCalibration pressureCalibration;
642 PressureSource pressureSource;
643 bool havePressureScale;
644 float pressureScale;
645
646 // Size
647 enum SizeCalibration {
648 SIZE_CALIBRATION_DEFAULT,
649 SIZE_CALIBRATION_NONE,
650 SIZE_CALIBRATION_NORMALIZED,
651 };
652
653 SizeCalibration sizeCalibration;
654
655 // Orientation
656 enum OrientationCalibration {
657 ORIENTATION_CALIBRATION_DEFAULT,
658 ORIENTATION_CALIBRATION_NONE,
659 ORIENTATION_CALIBRATION_INTERPOLATED,
660 };
661
662 OrientationCalibration orientationCalibration;
663 } mCalibration;
664
665 // Raw axis information from the driver.
666 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700667 RawAbsoluteAxisInfo x;
668 RawAbsoluteAxisInfo y;
669 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700670 RawAbsoluteAxisInfo touchMajor;
671 RawAbsoluteAxisInfo touchMinor;
672 RawAbsoluteAxisInfo toolMajor;
673 RawAbsoluteAxisInfo toolMinor;
674 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700675 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700676
Jeff Brownb51719b2010-07-29 18:18:33 -0700677 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700678 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700679 TouchData mLastTouch;
680
681 // The time the primary pointer last went down.
682 nsecs_t mDownTime;
683
Jeff Brownb51719b2010-07-29 18:18:33 -0700684 struct LockedState {
685 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700686
Jeff Brownb51719b2010-07-29 18:18:33 -0700687 // The surface orientation and width and height set by configureSurfaceLocked().
688 int32_t surfaceOrientation;
689 int32_t surfaceWidth, surfaceHeight;
690
691 // Translation and scaling factors, orientation-independent.
692 int32_t xOrigin;
693 float xScale;
694 float xPrecision;
695
696 int32_t yOrigin;
697 float yScale;
698 float yPrecision;
699
Jeff Brown38a7fab2010-08-30 03:02:23 -0700700 float geometricScale;
701
Jeff Brown6b337e72010-10-14 21:42:15 -0700702 float toolSizeLinearScale;
703 float toolSizeLinearBias;
704 float toolSizeAreaScale;
705 float toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700706
Jeff Brownb51719b2010-07-29 18:18:33 -0700707 float pressureScale;
708
Jeff Brownb51719b2010-07-29 18:18:33 -0700709 float sizeScale;
710
711 float orientationScale;
712
713 // Oriented motion ranges for input device info.
714 struct OrientedRanges {
715 InputDeviceInfo::MotionRange x;
716 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700717
718 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700719 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700720
721 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700722 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700723
Jeff Brown6b337e72010-10-14 21:42:15 -0700724 bool haveTouchSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700725 InputDeviceInfo::MotionRange touchMajor;
726 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700727
Jeff Brown6b337e72010-10-14 21:42:15 -0700728 bool haveToolSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700729 InputDeviceInfo::MotionRange toolMajor;
730 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700731
732 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700733 InputDeviceInfo::MotionRange orientation;
734 } orientedRanges;
735
736 // Oriented dimensions and precision.
737 float orientedSurfaceWidth, orientedSurfaceHeight;
738 float orientedXPrecision, orientedYPrecision;
739
740 struct CurrentVirtualKeyState {
741 bool down;
742 nsecs_t downTime;
743 int32_t keyCode;
744 int32_t scanCode;
745 } currentVirtualKey;
746 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700747
Jeff Brown38a7fab2010-08-30 03:02:23 -0700748 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700749 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700750 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700751 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700752 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700753 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700754 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700755 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700756 virtual void parseCalibration();
757 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700758 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700759
760 enum TouchResult {
761 // Dispatch the touch normally.
762 DISPATCH_TOUCH,
763 // Do not dispatch the touch, but keep tracking the current stroke.
764 SKIP_TOUCH,
765 // Do not dispatch the touch, and drop all information associated with the current stoke
766 // so the next movement will appear as a new down.
767 DROP_STROKE
768 };
769
770 void syncTouch(nsecs_t when, bool havePointerIds);
771
772private:
773 /* Maximum number of historical samples to average. */
774 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
775
776 /* Slop distance for jumpy pointer detection.
777 * The vertical range of the screen divided by this is our epsilon value. */
778 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
779
780 /* Number of jumpy points to drop for touchscreens that need it. */
781 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
782 static const uint32_t JUMPY_DROP_LIMIT = 3;
783
784 /* Maximum squared distance for averaging.
785 * If moving farther than this, turn of averaging to avoid lag in response. */
786 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
787
788 struct AveragingTouchFilterState {
789 // Individual history tracks are stored by pointer id
790 uint32_t historyStart[MAX_POINTERS];
791 uint32_t historyEnd[MAX_POINTERS];
792 struct {
793 struct {
794 int32_t x;
795 int32_t y;
796 int32_t pressure;
797 } pointers[MAX_POINTERS];
798 } historyData[AVERAGING_HISTORY_SIZE];
799 } mAveragingTouchFilter;
800
Jeff Brownd64c8552010-08-17 20:38:35 -0700801 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700802 uint32_t jumpyPointsDropped;
803 } mJumpyTouchFilter;
804
805 struct PointerDistanceHeapElement {
806 uint32_t currentPointerIndex : 8;
807 uint32_t lastPointerIndex : 8;
808 uint64_t distance : 48; // squared distance
809 };
810
Jeff Brownb51719b2010-07-29 18:18:33 -0700811 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700812
813 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
814 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
815 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700816 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
817 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700818
Jeff Brownb51719b2010-07-29 18:18:33 -0700819 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
820 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700821
822 bool applyBadTouchFilter();
823 bool applyJumpyTouchFilter();
824 void applyAveragingTouchFilter();
825 void calculatePointerIds();
826};
827
828
829class SingleTouchInputMapper : public TouchInputMapper {
830public:
831 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
832 virtual ~SingleTouchInputMapper();
833
834 virtual void reset();
835 virtual void process(const RawEvent* rawEvent);
836
837protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700838 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700839
840private:
841 struct Accumulator {
842 enum {
843 FIELD_BTN_TOUCH = 1,
844 FIELD_ABS_X = 2,
845 FIELD_ABS_Y = 4,
846 FIELD_ABS_PRESSURE = 8,
847 FIELD_ABS_TOOL_WIDTH = 16
848 };
849
850 uint32_t fields;
851
852 bool btnTouch;
853 int32_t absX;
854 int32_t absY;
855 int32_t absPressure;
856 int32_t absToolWidth;
857
858 inline void clear() {
859 fields = 0;
860 }
Jeff Browne57e8952010-07-23 21:28:06 -0700861 } mAccumulator;
862
863 bool mDown;
864 int32_t mX;
865 int32_t mY;
866 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700867 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700868
869 void initialize();
870
871 void sync(nsecs_t when);
872};
873
874
875class MultiTouchInputMapper : public TouchInputMapper {
876public:
877 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
878 virtual ~MultiTouchInputMapper();
879
880 virtual void reset();
881 virtual void process(const RawEvent* rawEvent);
882
883protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700884 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700885
886private:
887 struct Accumulator {
888 enum {
889 FIELD_ABS_MT_POSITION_X = 1,
890 FIELD_ABS_MT_POSITION_Y = 2,
891 FIELD_ABS_MT_TOUCH_MAJOR = 4,
892 FIELD_ABS_MT_TOUCH_MINOR = 8,
893 FIELD_ABS_MT_WIDTH_MAJOR = 16,
894 FIELD_ABS_MT_WIDTH_MINOR = 32,
895 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700896 FIELD_ABS_MT_TRACKING_ID = 128,
897 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700898 };
899
900 uint32_t pointerCount;
901 struct Pointer {
902 uint32_t fields;
903
904 int32_t absMTPositionX;
905 int32_t absMTPositionY;
906 int32_t absMTTouchMajor;
907 int32_t absMTTouchMinor;
908 int32_t absMTWidthMajor;
909 int32_t absMTWidthMinor;
910 int32_t absMTOrientation;
911 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700912 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700913
914 inline void clear() {
915 fields = 0;
916 }
917 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
918
919 inline void clear() {
920 pointerCount = 0;
921 pointers[0].clear();
922 }
Jeff Browne57e8952010-07-23 21:28:06 -0700923 } mAccumulator;
924
925 void initialize();
926
927 void sync(nsecs_t when);
928};
929
Jeff Browne839a582010-04-22 18:58:52 -0700930} // namespace android
931
932#endif // _UI_INPUT_READER_H