blob: 49351b0ca5dc14488ceee5f741e7e367792e4628 [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 {
Jeff Brown3c3cc622010-10-20 15:33:38 -0700173public:
Jeff Browne57e8952010-07-23 21:28:06 -0700174 InputReaderContext() { }
175 virtual ~InputReaderContext() { }
176
Jeff Browne57e8952010-07-23 21:28:06 -0700177 virtual void updateGlobalMetaState() = 0;
178 virtual int32_t getGlobalMetaState() = 0;
179
180 virtual InputReaderPolicyInterface* getPolicy() = 0;
181 virtual InputDispatcherInterface* getDispatcher() = 0;
182 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700183};
184
Jeff Brown54bc2812010-06-15 01:31:58 -0700185
Jeff Browne839a582010-04-22 18:58:52 -0700186/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700187 * that it sends to the input dispatcher. Some functions of the input reader, such as early
188 * event filtering in low power states, are controlled by a separate policy object.
189 *
190 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700191 * Because the policy and dispatcher can potentially block or cause re-entrance into
192 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700193 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700194 */
Jeff Brown3c3cc622010-10-20 15:33:38 -0700195class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700196public:
197 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700198 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700199 const sp<InputDispatcherInterface>& dispatcher);
200 virtual ~InputReader();
201
Jeff Browna665ca82010-09-08 11:49:43 -0700202 virtual void dump(String8& dump);
203
Jeff Browne839a582010-04-22 18:58:52 -0700204 virtual void loopOnce();
205
Jeff Browne57e8952010-07-23 21:28:06 -0700206 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700207
Jeff Browne57e8952010-07-23 21:28:06 -0700208 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
209 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700210
Jeff Browne57e8952010-07-23 21:28:06 -0700211 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
212 int32_t scanCode);
213 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
214 int32_t keyCode);
215 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
216 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700217
Jeff Browne57e8952010-07-23 21:28:06 -0700218 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
219 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700220
Jeff Brown3c3cc622010-10-20 15:33:38 -0700221protected:
222 // These methods are protected virtual so they can be overridden and instrumented
223 // by test cases.
224 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
225
Jeff Browne839a582010-04-22 18:58:52 -0700226private:
Jeff Browne839a582010-04-22 18:58:52 -0700227 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700228 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700229 sp<InputDispatcherInterface> mDispatcher;
230
Jeff Browne57e8952010-07-23 21:28:06 -0700231 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
232 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
233 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
234
235 // This reader/writer lock guards the list of input devices.
236 // The writer lock must be held whenever the list of input devices is modified
237 // and then promptly released.
238 // The reader lock must be held whenever the list of input devices is traversed or an
239 // input device in the list is accessed.
240 // This lock only protects the registry and prevents inadvertent deletion of device objects
241 // that are in use. Individual devices are responsible for guarding their own internal state
242 // as needed for concurrent operation.
243 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700244 KeyedVector<int32_t, InputDevice*> mDevices;
245
Jeff Browne57e8952010-07-23 21:28:06 -0700246 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700247 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700248
Jeff Brown1ad00e92010-10-01 18:55:43 -0700249 void addDevice(int32_t deviceId);
250 void removeDevice(int32_t deviceId);
Jeff Brown54bc2812010-06-15 01:31:58 -0700251 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700252
Jeff Browne57e8952010-07-23 21:28:06 -0700253 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700254
Jeff Brown3c3cc622010-10-20 15:33:38 -0700255 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700256
Jeff Browne57e8952010-07-23 21:28:06 -0700257 // state management for all devices
258 Mutex mStateLock;
259
260 int32_t mGlobalMetaState;
261 virtual void updateGlobalMetaState();
262 virtual int32_t getGlobalMetaState();
263
264 InputConfiguration mInputConfiguration;
265 void updateInputConfiguration();
266
267 // state queries
268 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
269 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
270 GetStateFunc getStateFunc);
271 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
272 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700273};
274
275
276/* Reads raw events from the event hub and processes them, endlessly. */
277class InputReaderThread : public Thread {
278public:
279 InputReaderThread(const sp<InputReaderInterface>& reader);
280 virtual ~InputReaderThread();
281
282private:
283 sp<InputReaderInterface> mReader;
284
285 virtual bool threadLoop();
286};
287
Jeff Browne57e8952010-07-23 21:28:06 -0700288
289/* Represents the state of a single input device. */
290class InputDevice {
291public:
292 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
293 ~InputDevice();
294
295 inline InputReaderContext* getContext() { return mContext; }
296 inline int32_t getId() { return mId; }
297 inline const String8& getName() { return mName; }
298 inline uint32_t getSources() { return mSources; }
299
300 inline bool isIgnored() { return mMappers.isEmpty(); }
301
Jeff Brown26c94ff2010-09-30 14:33:04 -0700302 void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700303 void addMapper(InputMapper* mapper);
304 void configure();
305 void reset();
306 void process(const RawEvent* rawEvent);
307
308 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
309 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
310 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
311 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
312 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
313 const int32_t* keyCodes, uint8_t* outFlags);
314
315 int32_t getMetaState();
316
Jeff Brown38a7fab2010-08-30 03:02:23 -0700317 inline const InputDeviceCalibration& getCalibration() {
318 return mCalibration;
319 }
320
Jeff Browne57e8952010-07-23 21:28:06 -0700321private:
322 InputReaderContext* mContext;
323 int32_t mId;
324
325 Vector<InputMapper*> mMappers;
326
327 String8 mName;
328 uint32_t mSources;
329
330 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
331 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700332
333 InputDeviceCalibration mCalibration;
Jeff Browne57e8952010-07-23 21:28:06 -0700334};
335
336
337/* An input mapper transforms raw input events into cooked event data.
338 * A single input device can have multiple associated input mappers in order to interpret
339 * different classes of events.
340 */
341class InputMapper {
342public:
343 InputMapper(InputDevice* device);
344 virtual ~InputMapper();
345
346 inline InputDevice* getDevice() { return mDevice; }
347 inline int32_t getDeviceId() { return mDevice->getId(); }
348 inline const String8 getDeviceName() { return mDevice->getName(); }
349 inline InputReaderContext* getContext() { return mContext; }
350 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
351 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
352 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
353
354 virtual uint32_t getSources() = 0;
355 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700356 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700357 virtual void configure();
358 virtual void reset();
359 virtual void process(const RawEvent* rawEvent) = 0;
360
361 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
362 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
363 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
364 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
365 const int32_t* keyCodes, uint8_t* outFlags);
366
367 virtual int32_t getMetaState();
368
369protected:
370 InputDevice* mDevice;
371 InputReaderContext* mContext;
Jeff Browne57e8952010-07-23 21:28:06 -0700372};
373
374
375class SwitchInputMapper : public InputMapper {
376public:
377 SwitchInputMapper(InputDevice* device);
378 virtual ~SwitchInputMapper();
379
380 virtual uint32_t getSources();
381 virtual void process(const RawEvent* rawEvent);
382
383 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
384
385private:
386 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
387};
388
389
390class KeyboardInputMapper : public InputMapper {
391public:
392 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
393 int32_t keyboardType);
394 virtual ~KeyboardInputMapper();
395
396 virtual uint32_t getSources();
397 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700398 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700399 virtual void reset();
400 virtual void process(const RawEvent* rawEvent);
401
402 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
403 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
404 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
405 const int32_t* keyCodes, uint8_t* outFlags);
406
407 virtual int32_t getMetaState();
408
409private:
Jeff Brownb51719b2010-07-29 18:18:33 -0700410 Mutex mLock;
411
Jeff Browne57e8952010-07-23 21:28:06 -0700412 struct KeyDown {
413 int32_t keyCode;
414 int32_t scanCode;
415 };
416
417 int32_t mAssociatedDisplayId;
418 uint32_t mSources;
419 int32_t mKeyboardType;
420
Jeff Brownb51719b2010-07-29 18:18:33 -0700421 struct LockedState {
422 Vector<KeyDown> keyDowns; // keys that are down
423 int32_t metaState;
424 nsecs_t downTime; // time of most recent key down
425 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700426
Jeff Brownb51719b2010-07-29 18:18:33 -0700427 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700428
429 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700430
Jeff Browne57e8952010-07-23 21:28:06 -0700431 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
432 uint32_t policyFlags);
433
Jeff Brownb51719b2010-07-29 18:18:33 -0700434 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -0700435};
436
437
438class TrackballInputMapper : public InputMapper {
439public:
440 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
441 virtual ~TrackballInputMapper();
442
443 virtual uint32_t getSources();
444 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700445 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700446 virtual void reset();
447 virtual void process(const RawEvent* rawEvent);
448
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700449 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
450
Jeff Browne57e8952010-07-23 21:28:06 -0700451private:
452 // Amount that trackball needs to move in order to generate a key event.
453 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
454
Jeff Brownb51719b2010-07-29 18:18:33 -0700455 Mutex mLock;
456
Jeff Browne57e8952010-07-23 21:28:06 -0700457 int32_t mAssociatedDisplayId;
458
459 struct Accumulator {
460 enum {
461 FIELD_BTN_MOUSE = 1,
462 FIELD_REL_X = 2,
463 FIELD_REL_Y = 4
464 };
465
466 uint32_t fields;
467
468 bool btnMouse;
469 int32_t relX;
470 int32_t relY;
471
472 inline void clear() {
473 fields = 0;
474 }
Jeff Browne57e8952010-07-23 21:28:06 -0700475 } mAccumulator;
476
Jeff Browne57e8952010-07-23 21:28:06 -0700477 float mXScale;
478 float mYScale;
479 float mXPrecision;
480 float mYPrecision;
481
Jeff Brownb51719b2010-07-29 18:18:33 -0700482 struct LockedState {
483 bool down;
484 nsecs_t downTime;
485 } mLocked;
486
487 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700488
489 void sync(nsecs_t when);
490};
491
492
493class TouchInputMapper : public InputMapper {
494public:
495 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
496 virtual ~TouchInputMapper();
497
498 virtual uint32_t getSources();
499 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700500 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700501 virtual void configure();
502 virtual void reset();
503
504 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
505 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
506 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
507 const int32_t* keyCodes, uint8_t* outFlags);
508
509protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700510 Mutex mLock;
511
Jeff Browne57e8952010-07-23 21:28:06 -0700512 struct VirtualKey {
513 int32_t keyCode;
514 int32_t scanCode;
515 uint32_t flags;
516
517 // computed hit box, specified in touch screen coords based on known display size
518 int32_t hitLeft;
519 int32_t hitTop;
520 int32_t hitRight;
521 int32_t hitBottom;
522
523 inline bool isHit(int32_t x, int32_t y) const {
524 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
525 }
526 };
527
Jeff Brown38a7fab2010-08-30 03:02:23 -0700528 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700529 struct PointerData {
530 uint32_t id;
531 int32_t x;
532 int32_t y;
533 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700534 int32_t touchMajor;
535 int32_t touchMinor;
536 int32_t toolMajor;
537 int32_t toolMinor;
538 int32_t orientation;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700539
540 inline bool operator== (const PointerData& other) const {
541 return id == other.id
542 && x == other.x
543 && y == other.y
544 && pressure == other.pressure
545 && touchMajor == other.touchMajor
546 && touchMinor == other.touchMinor
547 && toolMajor == other.toolMajor
548 && toolMinor == other.toolMinor
549 && orientation == other.orientation;
550 }
551 inline bool operator!= (const PointerData& other) const {
552 return !(*this == other);
553 }
Jeff Browne57e8952010-07-23 21:28:06 -0700554 };
555
Jeff Brown38a7fab2010-08-30 03:02:23 -0700556 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700557 struct TouchData {
558 uint32_t pointerCount;
559 PointerData pointers[MAX_POINTERS];
560 BitSet32 idBits;
561 uint32_t idToIndex[MAX_POINTER_ID + 1];
562
563 void copyFrom(const TouchData& other) {
564 pointerCount = other.pointerCount;
565 idBits = other.idBits;
566
567 for (uint32_t i = 0; i < pointerCount; i++) {
568 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700569
570 int id = pointers[i].id;
571 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700572 }
573 }
574
575 inline void clear() {
576 pointerCount = 0;
577 idBits.clear();
578 }
579 };
580
581 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700582
583 // Immutable configuration parameters.
584 struct Parameters {
585 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 Brown6b337e72010-10-14 21:42:15 -0700592 // Touch Size
593 enum TouchSizeCalibration {
594 TOUCH_SIZE_CALIBRATION_DEFAULT,
595 TOUCH_SIZE_CALIBRATION_NONE,
596 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
597 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700598 };
599
Jeff Brown6b337e72010-10-14 21:42:15 -0700600 TouchSizeCalibration touchSizeCalibration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700601
Jeff Brown6b337e72010-10-14 21:42:15 -0700602 // Tool Size
603 enum ToolSizeCalibration {
604 TOOL_SIZE_CALIBRATION_DEFAULT,
605 TOOL_SIZE_CALIBRATION_NONE,
606 TOOL_SIZE_CALIBRATION_GEOMETRIC,
607 TOOL_SIZE_CALIBRATION_LINEAR,
608 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700609 };
610
Jeff Brown6b337e72010-10-14 21:42:15 -0700611 ToolSizeCalibration toolSizeCalibration;
612 bool haveToolSizeLinearScale;
613 float toolSizeLinearScale;
614 bool haveToolSizeLinearBias;
615 float toolSizeLinearBias;
616 bool haveToolSizeAreaScale;
617 float toolSizeAreaScale;
618 bool haveToolSizeAreaBias;
619 float toolSizeAreaBias;
620 bool haveToolSizeIsSummed;
621 int32_t toolSizeIsSummed;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700622
623 // Pressure
624 enum PressureCalibration {
625 PRESSURE_CALIBRATION_DEFAULT,
626 PRESSURE_CALIBRATION_NONE,
627 PRESSURE_CALIBRATION_PHYSICAL,
628 PRESSURE_CALIBRATION_AMPLITUDE,
629 };
630 enum PressureSource {
631 PRESSURE_SOURCE_DEFAULT,
632 PRESSURE_SOURCE_PRESSURE,
633 PRESSURE_SOURCE_TOUCH,
634 };
635
636 PressureCalibration pressureCalibration;
637 PressureSource pressureSource;
638 bool havePressureScale;
639 float pressureScale;
640
641 // Size
642 enum SizeCalibration {
643 SIZE_CALIBRATION_DEFAULT,
644 SIZE_CALIBRATION_NONE,
645 SIZE_CALIBRATION_NORMALIZED,
646 };
647
648 SizeCalibration sizeCalibration;
649
650 // Orientation
651 enum OrientationCalibration {
652 ORIENTATION_CALIBRATION_DEFAULT,
653 ORIENTATION_CALIBRATION_NONE,
654 ORIENTATION_CALIBRATION_INTERPOLATED,
655 };
656
657 OrientationCalibration orientationCalibration;
658 } mCalibration;
659
660 // Raw axis information from the driver.
661 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700662 RawAbsoluteAxisInfo x;
663 RawAbsoluteAxisInfo y;
664 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700665 RawAbsoluteAxisInfo touchMajor;
666 RawAbsoluteAxisInfo touchMinor;
667 RawAbsoluteAxisInfo toolMajor;
668 RawAbsoluteAxisInfo toolMinor;
669 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700670 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700671
Jeff Brownb51719b2010-07-29 18:18:33 -0700672 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700673 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700674 TouchData mLastTouch;
675
676 // The time the primary pointer last went down.
677 nsecs_t mDownTime;
678
Jeff Brownb51719b2010-07-29 18:18:33 -0700679 struct LockedState {
680 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700681
Jeff Brownb51719b2010-07-29 18:18:33 -0700682 // The surface orientation and width and height set by configureSurfaceLocked().
683 int32_t surfaceOrientation;
684 int32_t surfaceWidth, surfaceHeight;
685
686 // Translation and scaling factors, orientation-independent.
687 int32_t xOrigin;
688 float xScale;
689 float xPrecision;
690
691 int32_t yOrigin;
692 float yScale;
693 float yPrecision;
694
Jeff Brown38a7fab2010-08-30 03:02:23 -0700695 float geometricScale;
696
Jeff Brown6b337e72010-10-14 21:42:15 -0700697 float toolSizeLinearScale;
698 float toolSizeLinearBias;
699 float toolSizeAreaScale;
700 float toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700701
Jeff Brownb51719b2010-07-29 18:18:33 -0700702 float pressureScale;
703
Jeff Brownb51719b2010-07-29 18:18:33 -0700704 float sizeScale;
705
706 float orientationScale;
707
708 // Oriented motion ranges for input device info.
709 struct OrientedRanges {
710 InputDeviceInfo::MotionRange x;
711 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700712
713 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700714 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700715
716 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700717 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700718
Jeff Brown6b337e72010-10-14 21:42:15 -0700719 bool haveTouchSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700720 InputDeviceInfo::MotionRange touchMajor;
721 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700722
Jeff Brown6b337e72010-10-14 21:42:15 -0700723 bool haveToolSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700724 InputDeviceInfo::MotionRange toolMajor;
725 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700726
727 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700728 InputDeviceInfo::MotionRange orientation;
729 } orientedRanges;
730
731 // Oriented dimensions and precision.
732 float orientedSurfaceWidth, orientedSurfaceHeight;
733 float orientedXPrecision, orientedYPrecision;
734
735 struct CurrentVirtualKeyState {
736 bool down;
737 nsecs_t downTime;
738 int32_t keyCode;
739 int32_t scanCode;
740 } currentVirtualKey;
741 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700742
Jeff Brown38a7fab2010-08-30 03:02:23 -0700743 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700744 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700745 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700746 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700747 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700748 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700749 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700750 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700751 virtual void parseCalibration();
752 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700753 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700754
755 enum TouchResult {
756 // Dispatch the touch normally.
757 DISPATCH_TOUCH,
758 // Do not dispatch the touch, but keep tracking the current stroke.
759 SKIP_TOUCH,
760 // Do not dispatch the touch, and drop all information associated with the current stoke
761 // so the next movement will appear as a new down.
762 DROP_STROKE
763 };
764
765 void syncTouch(nsecs_t when, bool havePointerIds);
766
767private:
768 /* Maximum number of historical samples to average. */
769 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
770
771 /* Slop distance for jumpy pointer detection.
772 * The vertical range of the screen divided by this is our epsilon value. */
773 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
774
775 /* Number of jumpy points to drop for touchscreens that need it. */
776 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
777 static const uint32_t JUMPY_DROP_LIMIT = 3;
778
779 /* Maximum squared distance for averaging.
780 * If moving farther than this, turn of averaging to avoid lag in response. */
781 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
782
783 struct AveragingTouchFilterState {
784 // Individual history tracks are stored by pointer id
785 uint32_t historyStart[MAX_POINTERS];
786 uint32_t historyEnd[MAX_POINTERS];
787 struct {
788 struct {
789 int32_t x;
790 int32_t y;
791 int32_t pressure;
792 } pointers[MAX_POINTERS];
793 } historyData[AVERAGING_HISTORY_SIZE];
794 } mAveragingTouchFilter;
795
Jeff Brownd64c8552010-08-17 20:38:35 -0700796 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700797 uint32_t jumpyPointsDropped;
798 } mJumpyTouchFilter;
799
800 struct PointerDistanceHeapElement {
801 uint32_t currentPointerIndex : 8;
802 uint32_t lastPointerIndex : 8;
803 uint64_t distance : 48; // squared distance
804 };
805
Jeff Brownb51719b2010-07-29 18:18:33 -0700806 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700807
808 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
809 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
810 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700811 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
812 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700813
Jeff Brownb51719b2010-07-29 18:18:33 -0700814 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
815 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700816
817 bool applyBadTouchFilter();
818 bool applyJumpyTouchFilter();
819 void applyAveragingTouchFilter();
820 void calculatePointerIds();
821};
822
823
824class SingleTouchInputMapper : public TouchInputMapper {
825public:
826 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
827 virtual ~SingleTouchInputMapper();
828
829 virtual void reset();
830 virtual void process(const RawEvent* rawEvent);
831
832protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700833 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700834
835private:
836 struct Accumulator {
837 enum {
838 FIELD_BTN_TOUCH = 1,
839 FIELD_ABS_X = 2,
840 FIELD_ABS_Y = 4,
841 FIELD_ABS_PRESSURE = 8,
842 FIELD_ABS_TOOL_WIDTH = 16
843 };
844
845 uint32_t fields;
846
847 bool btnTouch;
848 int32_t absX;
849 int32_t absY;
850 int32_t absPressure;
851 int32_t absToolWidth;
852
853 inline void clear() {
854 fields = 0;
855 }
Jeff Browne57e8952010-07-23 21:28:06 -0700856 } mAccumulator;
857
858 bool mDown;
859 int32_t mX;
860 int32_t mY;
861 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700862 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700863
864 void initialize();
865
866 void sync(nsecs_t when);
867};
868
869
870class MultiTouchInputMapper : public TouchInputMapper {
871public:
872 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
873 virtual ~MultiTouchInputMapper();
874
875 virtual void reset();
876 virtual void process(const RawEvent* rawEvent);
877
878protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700879 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700880
881private:
882 struct Accumulator {
883 enum {
884 FIELD_ABS_MT_POSITION_X = 1,
885 FIELD_ABS_MT_POSITION_Y = 2,
886 FIELD_ABS_MT_TOUCH_MAJOR = 4,
887 FIELD_ABS_MT_TOUCH_MINOR = 8,
888 FIELD_ABS_MT_WIDTH_MAJOR = 16,
889 FIELD_ABS_MT_WIDTH_MINOR = 32,
890 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700891 FIELD_ABS_MT_TRACKING_ID = 128,
892 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700893 };
894
895 uint32_t pointerCount;
896 struct Pointer {
897 uint32_t fields;
898
899 int32_t absMTPositionX;
900 int32_t absMTPositionY;
901 int32_t absMTTouchMajor;
902 int32_t absMTTouchMinor;
903 int32_t absMTWidthMajor;
904 int32_t absMTWidthMinor;
905 int32_t absMTOrientation;
906 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700907 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700908
909 inline void clear() {
910 fields = 0;
911 }
912 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
913
914 inline void clear() {
915 pointerCount = 0;
916 pointers[0].clear();
917 }
Jeff Browne57e8952010-07-23 21:28:06 -0700918 } mAccumulator;
919
920 void initialize();
921
922 void sync(nsecs_t when);
923};
924
Jeff Browne839a582010-04-22 18:58:52 -0700925} // namespace android
926
927#endif // _UI_INPUT_READER_H