blob: c15e3825ed1d1c55526511e1a0f805e669d93623 [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
422 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700423
Jeff Brownb51719b2010-07-29 18:18:33 -0700424 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700425
426 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700427
Jeff Browne57e8952010-07-23 21:28:06 -0700428 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
429 uint32_t policyFlags);
430
Jeff Brownb51719b2010-07-29 18:18:33 -0700431 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -0700432};
433
434
435class TrackballInputMapper : public InputMapper {
436public:
437 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
438 virtual ~TrackballInputMapper();
439
440 virtual uint32_t getSources();
441 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700442 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700443 virtual void reset();
444 virtual void process(const RawEvent* rawEvent);
445
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700446 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
447
Jeff Browne57e8952010-07-23 21:28:06 -0700448private:
449 // Amount that trackball needs to move in order to generate a key event.
450 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
451
Jeff Brownb51719b2010-07-29 18:18:33 -0700452 Mutex mLock;
453
Jeff Browne57e8952010-07-23 21:28:06 -0700454 int32_t mAssociatedDisplayId;
455
456 struct Accumulator {
457 enum {
458 FIELD_BTN_MOUSE = 1,
459 FIELD_REL_X = 2,
460 FIELD_REL_Y = 4
461 };
462
463 uint32_t fields;
464
465 bool btnMouse;
466 int32_t relX;
467 int32_t relY;
468
469 inline void clear() {
470 fields = 0;
471 }
Jeff Browne57e8952010-07-23 21:28:06 -0700472 } mAccumulator;
473
Jeff Browne57e8952010-07-23 21:28:06 -0700474 float mXScale;
475 float mYScale;
476 float mXPrecision;
477 float mYPrecision;
478
Jeff Brownb51719b2010-07-29 18:18:33 -0700479 struct LockedState {
480 bool down;
481 nsecs_t downTime;
482 } mLocked;
483
484 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700485
486 void sync(nsecs_t when);
487};
488
489
490class TouchInputMapper : public InputMapper {
491public:
492 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
493 virtual ~TouchInputMapper();
494
495 virtual uint32_t getSources();
496 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700497 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700498 virtual void configure();
499 virtual void reset();
500
501 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
502 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
503 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
504 const int32_t* keyCodes, uint8_t* outFlags);
505
506protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700507 Mutex mLock;
508
Jeff Browne57e8952010-07-23 21:28:06 -0700509 struct VirtualKey {
510 int32_t keyCode;
511 int32_t scanCode;
512 uint32_t flags;
513
514 // computed hit box, specified in touch screen coords based on known display size
515 int32_t hitLeft;
516 int32_t hitTop;
517 int32_t hitRight;
518 int32_t hitBottom;
519
520 inline bool isHit(int32_t x, int32_t y) const {
521 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
522 }
523 };
524
Jeff Brown38a7fab2010-08-30 03:02:23 -0700525 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700526 struct PointerData {
527 uint32_t id;
528 int32_t x;
529 int32_t y;
530 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700531 int32_t touchMajor;
532 int32_t touchMinor;
533 int32_t toolMajor;
534 int32_t toolMinor;
535 int32_t orientation;
536 };
537
Jeff Brown38a7fab2010-08-30 03:02:23 -0700538 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700539 struct TouchData {
540 uint32_t pointerCount;
541 PointerData pointers[MAX_POINTERS];
542 BitSet32 idBits;
543 uint32_t idToIndex[MAX_POINTER_ID + 1];
544
545 void copyFrom(const TouchData& other) {
546 pointerCount = other.pointerCount;
547 idBits = other.idBits;
548
549 for (uint32_t i = 0; i < pointerCount; i++) {
550 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700551
552 int id = pointers[i].id;
553 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700554 }
555 }
556
557 inline void clear() {
558 pointerCount = 0;
559 idBits.clear();
560 }
561 };
562
563 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700564
565 // Immutable configuration parameters.
566 struct Parameters {
567 bool useBadTouchFilter;
568 bool useJumpyTouchFilter;
569 bool useAveragingTouchFilter;
570 } mParameters;
571
Jeff Brown38a7fab2010-08-30 03:02:23 -0700572 // Immutable calibration parameters in parsed form.
573 struct Calibration {
574 // Touch Area
575 enum TouchAreaCalibration {
576 TOUCH_AREA_CALIBRATION_DEFAULT,
577 TOUCH_AREA_CALIBRATION_NONE,
578 TOUCH_AREA_CALIBRATION_GEOMETRIC,
579 TOUCH_AREA_CALIBRATION_PRESSURE,
580 };
581
582 TouchAreaCalibration touchAreaCalibration;
583
584 // Tool Area
585 enum ToolAreaCalibration {
586 TOOL_AREA_CALIBRATION_DEFAULT,
587 TOOL_AREA_CALIBRATION_NONE,
588 TOOL_AREA_CALIBRATION_GEOMETRIC,
589 TOOL_AREA_CALIBRATION_LINEAR,
590 };
591
592 ToolAreaCalibration toolAreaCalibration;
593 bool haveToolAreaLinearScale;
594 float toolAreaLinearScale;
595 bool haveToolAreaLinearBias;
596 float toolAreaLinearBias;
597 bool haveToolAreaIsSummed;
598 int32_t toolAreaIsSummed;
599
600 // Pressure
601 enum PressureCalibration {
602 PRESSURE_CALIBRATION_DEFAULT,
603 PRESSURE_CALIBRATION_NONE,
604 PRESSURE_CALIBRATION_PHYSICAL,
605 PRESSURE_CALIBRATION_AMPLITUDE,
606 };
607 enum PressureSource {
608 PRESSURE_SOURCE_DEFAULT,
609 PRESSURE_SOURCE_PRESSURE,
610 PRESSURE_SOURCE_TOUCH,
611 };
612
613 PressureCalibration pressureCalibration;
614 PressureSource pressureSource;
615 bool havePressureScale;
616 float pressureScale;
617
618 // Size
619 enum SizeCalibration {
620 SIZE_CALIBRATION_DEFAULT,
621 SIZE_CALIBRATION_NONE,
622 SIZE_CALIBRATION_NORMALIZED,
623 };
624
625 SizeCalibration sizeCalibration;
626
627 // Orientation
628 enum OrientationCalibration {
629 ORIENTATION_CALIBRATION_DEFAULT,
630 ORIENTATION_CALIBRATION_NONE,
631 ORIENTATION_CALIBRATION_INTERPOLATED,
632 };
633
634 OrientationCalibration orientationCalibration;
635 } mCalibration;
636
637 // Raw axis information from the driver.
638 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700639 RawAbsoluteAxisInfo x;
640 RawAbsoluteAxisInfo y;
641 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700642 RawAbsoluteAxisInfo touchMajor;
643 RawAbsoluteAxisInfo touchMinor;
644 RawAbsoluteAxisInfo toolMajor;
645 RawAbsoluteAxisInfo toolMinor;
646 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700647 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700648
Jeff Brownb51719b2010-07-29 18:18:33 -0700649 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700650 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700651 TouchData mLastTouch;
652
653 // The time the primary pointer last went down.
654 nsecs_t mDownTime;
655
Jeff Brownb51719b2010-07-29 18:18:33 -0700656 struct LockedState {
657 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700658
Jeff Brownb51719b2010-07-29 18:18:33 -0700659 // The surface orientation and width and height set by configureSurfaceLocked().
660 int32_t surfaceOrientation;
661 int32_t surfaceWidth, surfaceHeight;
662
663 // Translation and scaling factors, orientation-independent.
664 int32_t xOrigin;
665 float xScale;
666 float xPrecision;
667
668 int32_t yOrigin;
669 float yScale;
670 float yPrecision;
671
Jeff Brown38a7fab2010-08-30 03:02:23 -0700672 float geometricScale;
673
674 float toolAreaLinearScale;
675 float toolAreaLinearBias;
676
Jeff Brownb51719b2010-07-29 18:18:33 -0700677 float pressureScale;
678
Jeff Brownb51719b2010-07-29 18:18:33 -0700679 float sizeScale;
680
681 float orientationScale;
682
683 // Oriented motion ranges for input device info.
684 struct OrientedRanges {
685 InputDeviceInfo::MotionRange x;
686 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700687
688 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700689 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700690
691 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700692 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700693
694 bool haveTouchArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700695 InputDeviceInfo::MotionRange touchMajor;
696 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700697
698 bool haveToolArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700699 InputDeviceInfo::MotionRange toolMajor;
700 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700701
702 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700703 InputDeviceInfo::MotionRange orientation;
704 } orientedRanges;
705
706 // Oriented dimensions and precision.
707 float orientedSurfaceWidth, orientedSurfaceHeight;
708 float orientedXPrecision, orientedYPrecision;
709
710 struct CurrentVirtualKeyState {
711 bool down;
712 nsecs_t downTime;
713 int32_t keyCode;
714 int32_t scanCode;
715 } currentVirtualKey;
716 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700717
Jeff Brown38a7fab2010-08-30 03:02:23 -0700718 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700719 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700720 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700721 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700722 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700723 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700724 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700725 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700726 virtual void parseCalibration();
727 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700728 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700729
730 enum TouchResult {
731 // Dispatch the touch normally.
732 DISPATCH_TOUCH,
733 // Do not dispatch the touch, but keep tracking the current stroke.
734 SKIP_TOUCH,
735 // Do not dispatch the touch, and drop all information associated with the current stoke
736 // so the next movement will appear as a new down.
737 DROP_STROKE
738 };
739
740 void syncTouch(nsecs_t when, bool havePointerIds);
741
742private:
743 /* Maximum number of historical samples to average. */
744 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
745
746 /* Slop distance for jumpy pointer detection.
747 * The vertical range of the screen divided by this is our epsilon value. */
748 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
749
750 /* Number of jumpy points to drop for touchscreens that need it. */
751 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
752 static const uint32_t JUMPY_DROP_LIMIT = 3;
753
754 /* Maximum squared distance for averaging.
755 * If moving farther than this, turn of averaging to avoid lag in response. */
756 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
757
758 struct AveragingTouchFilterState {
759 // Individual history tracks are stored by pointer id
760 uint32_t historyStart[MAX_POINTERS];
761 uint32_t historyEnd[MAX_POINTERS];
762 struct {
763 struct {
764 int32_t x;
765 int32_t y;
766 int32_t pressure;
767 } pointers[MAX_POINTERS];
768 } historyData[AVERAGING_HISTORY_SIZE];
769 } mAveragingTouchFilter;
770
Jeff Brownd64c8552010-08-17 20:38:35 -0700771 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700772 uint32_t jumpyPointsDropped;
773 } mJumpyTouchFilter;
774
775 struct PointerDistanceHeapElement {
776 uint32_t currentPointerIndex : 8;
777 uint32_t lastPointerIndex : 8;
778 uint64_t distance : 48; // squared distance
779 };
780
Jeff Brownb51719b2010-07-29 18:18:33 -0700781 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700782
783 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
784 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
785 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700786 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
787 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700788
Jeff Brownb51719b2010-07-29 18:18:33 -0700789 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
790 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700791
792 bool applyBadTouchFilter();
793 bool applyJumpyTouchFilter();
794 void applyAveragingTouchFilter();
795 void calculatePointerIds();
796};
797
798
799class SingleTouchInputMapper : public TouchInputMapper {
800public:
801 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
802 virtual ~SingleTouchInputMapper();
803
804 virtual void reset();
805 virtual void process(const RawEvent* rawEvent);
806
807protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700808 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700809
810private:
811 struct Accumulator {
812 enum {
813 FIELD_BTN_TOUCH = 1,
814 FIELD_ABS_X = 2,
815 FIELD_ABS_Y = 4,
816 FIELD_ABS_PRESSURE = 8,
817 FIELD_ABS_TOOL_WIDTH = 16
818 };
819
820 uint32_t fields;
821
822 bool btnTouch;
823 int32_t absX;
824 int32_t absY;
825 int32_t absPressure;
826 int32_t absToolWidth;
827
828 inline void clear() {
829 fields = 0;
830 }
Jeff Browne57e8952010-07-23 21:28:06 -0700831 } mAccumulator;
832
833 bool mDown;
834 int32_t mX;
835 int32_t mY;
836 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700837 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700838
839 void initialize();
840
841 void sync(nsecs_t when);
842};
843
844
845class MultiTouchInputMapper : public TouchInputMapper {
846public:
847 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
848 virtual ~MultiTouchInputMapper();
849
850 virtual void reset();
851 virtual void process(const RawEvent* rawEvent);
852
853protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700854 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700855
856private:
857 struct Accumulator {
858 enum {
859 FIELD_ABS_MT_POSITION_X = 1,
860 FIELD_ABS_MT_POSITION_Y = 2,
861 FIELD_ABS_MT_TOUCH_MAJOR = 4,
862 FIELD_ABS_MT_TOUCH_MINOR = 8,
863 FIELD_ABS_MT_WIDTH_MAJOR = 16,
864 FIELD_ABS_MT_WIDTH_MINOR = 32,
865 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700866 FIELD_ABS_MT_TRACKING_ID = 128,
867 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700868 };
869
870 uint32_t pointerCount;
871 struct Pointer {
872 uint32_t fields;
873
874 int32_t absMTPositionX;
875 int32_t absMTPositionY;
876 int32_t absMTTouchMajor;
877 int32_t absMTTouchMinor;
878 int32_t absMTWidthMajor;
879 int32_t absMTWidthMinor;
880 int32_t absMTOrientation;
881 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700882 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700883
884 inline void clear() {
885 fields = 0;
886 }
887 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
888
889 inline void clear() {
890 pointerCount = 0;
891 pointers[0].clear();
892 }
Jeff Browne57e8952010-07-23 21:28:06 -0700893 } mAccumulator;
894
895 void initialize();
896
897 void sync(nsecs_t when);
898};
899
Jeff Browne839a582010-04-22 18:58:52 -0700900} // namespace android
901
902#endif // _UI_INPUT_READER_H