blob: a1b59e620fa5f4692e3c3b0f938e486937536bc8 [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070033namespace android {
34
Jeff Brown6d0fec22010-07-23 21:28:06 -070035class InputDevice;
36class InputMapper;
37
Jeff Brown8d608662010-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 Brown6d0fec22010-07-23 21:28:06 -070066
Jeff Brown9c3cda02010-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 Brown9c3cda02010-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 Brown9c3cda02010-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 Brown8d608662010-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 Brown9c3cda02010-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 Brown46b9ac02010-04-22 18:58:52 -0700120class InputReaderInterface : public virtual RefBase {
121protected:
122 InputReaderInterface() { }
123 virtual ~InputReaderInterface() { }
124
125public:
Jeff Brownb88102f2010-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 Brown46b9ac02010-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 Brown9c3cda02010-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 Brown6d0fec22010-07-23 21:28:06 -0700142 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700143
Jeff Brown6d0fec22010-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 Brown9c3cda02010-06-15 01:31:58 -0700149 */
Jeff Brown6d0fec22010-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 Brown9c3cda02010-06-15 01:31:58 -0700162
163 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-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 Brown46b9ac02010-04-22 18:58:52 -0700184};
185
Jeff Brown9c3cda02010-06-15 01:31:58 -0700186
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-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 Brown6d0fec22010-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 Brown6328cdc2010-07-29 18:18:33 -0700194 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700196class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700197public:
198 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700199 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200 const sp<InputDispatcherInterface>& dispatcher);
201 virtual ~InputReader();
202
Jeff Brownb88102f2010-09-08 11:49:43 -0700203 virtual void dump(String8& dump);
204
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205 virtual void loopOnce();
206
Jeff Brown6d0fec22010-07-23 21:28:06 -0700207 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700208
Jeff Brown6d0fec22010-07-23 21:28:06 -0700209 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
210 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700211
Jeff Brown6d0fec22010-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 Brown9c3cda02010-06-15 01:31:58 -0700218
Jeff Brown6d0fec22010-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 Brown9c3cda02010-06-15 01:31:58 -0700221
Jeff Brown46b9ac02010-04-22 18:58:52 -0700222private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700223 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225 sp<InputDispatcherInterface> mDispatcher;
226
Jeff Brown6d0fec22010-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 Brown46b9ac02010-04-22 18:58:52 -0700240 KeyedVector<int32_t, InputDevice*> mDevices;
241
Jeff Brown6d0fec22010-07-23 21:28:06 -0700242 // low-level input event decoding and device management
Jeff Brown46b9ac02010-04-22 18:58:52 -0700243 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700244
Jeff Brown7342bb92010-10-01 18:55:43 -0700245 void addDevice(int32_t deviceId);
246 void removeDevice(int32_t deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700247 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700248 void configureExcludedDevices();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700249
Jeff Brown6d0fec22010-07-23 21:28:06 -0700250 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700251
Jeff Brown7342bb92010-10-01 18:55:43 -0700252 void handleConfigurationChanged();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700253
Jeff Brown6d0fec22010-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 Brown46b9ac02010-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 Brown6d0fec22010-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 Brownef3d7e82010-09-30 14:33:04 -0700299 void dump(String8& dump);
Jeff Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700314 inline const InputDeviceCalibration& getCalibration() {
315 return mCalibration;
316 }
317
Jeff Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700329
330 InputDeviceCalibration mCalibration;
Jeff Brown6d0fec22010-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 Brownef3d7e82010-09-30 14:33:04 -0700353 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-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 Brown6d0fec22010-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 Brownef3d7e82010-09-30 14:33:04 -0700395 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-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 Brown6328cdc2010-07-29 18:18:33 -0700407 Mutex mLock;
408
Jeff Brown6d0fec22010-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 Brown6328cdc2010-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 Brown497a92c2010-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 Brown6328cdc2010-07-29 18:18:33 -0700430 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431
Jeff Brown6328cdc2010-07-29 18:18:33 -0700432 void initializeLocked();
Jeff Brown497a92c2010-09-12 17:55:08 -0700433 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434
435 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700436
Jeff Brown6d0fec22010-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 Brown6328cdc2010-07-29 18:18:33 -0700440 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-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 Brown6d0fec22010-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 Brownef3d7e82010-09-30 14:33:04 -0700455 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700456 virtual void reset();
457 virtual void process(const RawEvent* rawEvent);
458
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700459 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
460
Jeff Brown6d0fec22010-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 Brown6328cdc2010-07-29 18:18:33 -0700465 Mutex mLock;
466
Jeff Brown6d0fec22010-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 Brown6d0fec22010-07-23 21:28:06 -0700485 } mAccumulator;
486
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487 float mXScale;
488 float mYScale;
489 float mXPrecision;
490 float mYPrecision;
491
Jeff Brown6328cdc2010-07-29 18:18:33 -0700492 struct LockedState {
493 bool down;
494 nsecs_t downTime;
495 } mLocked;
496
497 void initializeLocked();
Jeff Brown6d0fec22010-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 Brownef3d7e82010-09-30 14:33:04 -0700510 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-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 Brown6328cdc2010-07-29 18:18:33 -0700520 Mutex mLock;
521
Jeff Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700538 // Raw data for a single pointer.
Jeff Brown6d0fec22010-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 Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700551 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-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 Brown9e2ad362010-07-30 19:20:11 -0700564
565 int id = pointers[i].id;
566 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-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 Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700585 // Immutable calibration parameters in parsed form.
586 struct Calibration {
587 // Touch Area
588 enum TouchAreaCalibration {
589 TOUCH_AREA_CALIBRATION_DEFAULT,
590 TOUCH_AREA_CALIBRATION_NONE,
591 TOUCH_AREA_CALIBRATION_GEOMETRIC,
592 TOUCH_AREA_CALIBRATION_PRESSURE,
593 };
594
595 TouchAreaCalibration touchAreaCalibration;
596
597 // Tool Area
598 enum ToolAreaCalibration {
599 TOOL_AREA_CALIBRATION_DEFAULT,
600 TOOL_AREA_CALIBRATION_NONE,
601 TOOL_AREA_CALIBRATION_GEOMETRIC,
602 TOOL_AREA_CALIBRATION_LINEAR,
603 };
604
605 ToolAreaCalibration toolAreaCalibration;
606 bool haveToolAreaLinearScale;
607 float toolAreaLinearScale;
608 bool haveToolAreaLinearBias;
609 float toolAreaLinearBias;
610 bool haveToolAreaIsSummed;
611 int32_t toolAreaIsSummed;
612
613 // Pressure
614 enum PressureCalibration {
615 PRESSURE_CALIBRATION_DEFAULT,
616 PRESSURE_CALIBRATION_NONE,
617 PRESSURE_CALIBRATION_PHYSICAL,
618 PRESSURE_CALIBRATION_AMPLITUDE,
619 };
620 enum PressureSource {
621 PRESSURE_SOURCE_DEFAULT,
622 PRESSURE_SOURCE_PRESSURE,
623 PRESSURE_SOURCE_TOUCH,
624 };
625
626 PressureCalibration pressureCalibration;
627 PressureSource pressureSource;
628 bool havePressureScale;
629 float pressureScale;
630
631 // Size
632 enum SizeCalibration {
633 SIZE_CALIBRATION_DEFAULT,
634 SIZE_CALIBRATION_NONE,
635 SIZE_CALIBRATION_NORMALIZED,
636 };
637
638 SizeCalibration sizeCalibration;
639
640 // Orientation
641 enum OrientationCalibration {
642 ORIENTATION_CALIBRATION_DEFAULT,
643 ORIENTATION_CALIBRATION_NONE,
644 ORIENTATION_CALIBRATION_INTERPOLATED,
645 };
646
647 OrientationCalibration orientationCalibration;
648 } mCalibration;
649
650 // Raw axis information from the driver.
651 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700652 RawAbsoluteAxisInfo x;
653 RawAbsoluteAxisInfo y;
654 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655 RawAbsoluteAxisInfo touchMajor;
656 RawAbsoluteAxisInfo touchMinor;
657 RawAbsoluteAxisInfo toolMajor;
658 RawAbsoluteAxisInfo toolMinor;
659 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700660 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700661
Jeff Brown6328cdc2010-07-29 18:18:33 -0700662 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700664 TouchData mLastTouch;
665
666 // The time the primary pointer last went down.
667 nsecs_t mDownTime;
668
Jeff Brown6328cdc2010-07-29 18:18:33 -0700669 struct LockedState {
670 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700671
Jeff Brown6328cdc2010-07-29 18:18:33 -0700672 // The surface orientation and width and height set by configureSurfaceLocked().
673 int32_t surfaceOrientation;
674 int32_t surfaceWidth, surfaceHeight;
675
676 // Translation and scaling factors, orientation-independent.
677 int32_t xOrigin;
678 float xScale;
679 float xPrecision;
680
681 int32_t yOrigin;
682 float yScale;
683 float yPrecision;
684
Jeff Brown8d608662010-08-30 03:02:23 -0700685 float geometricScale;
686
687 float toolAreaLinearScale;
688 float toolAreaLinearBias;
689
Jeff Brown6328cdc2010-07-29 18:18:33 -0700690 float pressureScale;
691
Jeff Brown6328cdc2010-07-29 18:18:33 -0700692 float sizeScale;
693
694 float orientationScale;
695
696 // Oriented motion ranges for input device info.
697 struct OrientedRanges {
698 InputDeviceInfo::MotionRange x;
699 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700700
701 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700702 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700703
704 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700705 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700706
707 bool haveTouchArea;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700708 InputDeviceInfo::MotionRange touchMajor;
709 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700710
711 bool haveToolArea;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700712 InputDeviceInfo::MotionRange toolMajor;
713 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700714
715 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700716 InputDeviceInfo::MotionRange orientation;
717 } orientedRanges;
718
719 // Oriented dimensions and precision.
720 float orientedSurfaceWidth, orientedSurfaceHeight;
721 float orientedXPrecision, orientedYPrecision;
722
723 struct CurrentVirtualKeyState {
724 bool down;
725 nsecs_t downTime;
726 int32_t keyCode;
727 int32_t scanCode;
728 } currentVirtualKey;
729 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700730
Jeff Brown8d608662010-08-30 03:02:23 -0700731 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700732 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700733 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700734 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700735 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700736 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700737 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700738 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700739 virtual void parseCalibration();
740 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700741 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700742
743 enum TouchResult {
744 // Dispatch the touch normally.
745 DISPATCH_TOUCH,
746 // Do not dispatch the touch, but keep tracking the current stroke.
747 SKIP_TOUCH,
748 // Do not dispatch the touch, and drop all information associated with the current stoke
749 // so the next movement will appear as a new down.
750 DROP_STROKE
751 };
752
753 void syncTouch(nsecs_t when, bool havePointerIds);
754
755private:
756 /* Maximum number of historical samples to average. */
757 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
758
759 /* Slop distance for jumpy pointer detection.
760 * The vertical range of the screen divided by this is our epsilon value. */
761 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
762
763 /* Number of jumpy points to drop for touchscreens that need it. */
764 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
765 static const uint32_t JUMPY_DROP_LIMIT = 3;
766
767 /* Maximum squared distance for averaging.
768 * If moving farther than this, turn of averaging to avoid lag in response. */
769 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
770
771 struct AveragingTouchFilterState {
772 // Individual history tracks are stored by pointer id
773 uint32_t historyStart[MAX_POINTERS];
774 uint32_t historyEnd[MAX_POINTERS];
775 struct {
776 struct {
777 int32_t x;
778 int32_t y;
779 int32_t pressure;
780 } pointers[MAX_POINTERS];
781 } historyData[AVERAGING_HISTORY_SIZE];
782 } mAveragingTouchFilter;
783
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700784 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700785 uint32_t jumpyPointsDropped;
786 } mJumpyTouchFilter;
787
788 struct PointerDistanceHeapElement {
789 uint32_t currentPointerIndex : 8;
790 uint32_t lastPointerIndex : 8;
791 uint64_t distance : 48; // squared distance
792 };
793
Jeff Brown6328cdc2010-07-29 18:18:33 -0700794 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700795
796 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
797 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
798 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700799 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
800 int32_t motionEventAction);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700801
Jeff Brown6328cdc2010-07-29 18:18:33 -0700802 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
803 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700804
805 bool applyBadTouchFilter();
806 bool applyJumpyTouchFilter();
807 void applyAveragingTouchFilter();
808 void calculatePointerIds();
809};
810
811
812class SingleTouchInputMapper : public TouchInputMapper {
813public:
814 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
815 virtual ~SingleTouchInputMapper();
816
817 virtual void reset();
818 virtual void process(const RawEvent* rawEvent);
819
820protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700821 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700822
823private:
824 struct Accumulator {
825 enum {
826 FIELD_BTN_TOUCH = 1,
827 FIELD_ABS_X = 2,
828 FIELD_ABS_Y = 4,
829 FIELD_ABS_PRESSURE = 8,
830 FIELD_ABS_TOOL_WIDTH = 16
831 };
832
833 uint32_t fields;
834
835 bool btnTouch;
836 int32_t absX;
837 int32_t absY;
838 int32_t absPressure;
839 int32_t absToolWidth;
840
841 inline void clear() {
842 fields = 0;
843 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700844 } mAccumulator;
845
846 bool mDown;
847 int32_t mX;
848 int32_t mY;
849 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700850 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700851
852 void initialize();
853
854 void sync(nsecs_t when);
855};
856
857
858class MultiTouchInputMapper : public TouchInputMapper {
859public:
860 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
861 virtual ~MultiTouchInputMapper();
862
863 virtual void reset();
864 virtual void process(const RawEvent* rawEvent);
865
866protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700867 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700868
869private:
870 struct Accumulator {
871 enum {
872 FIELD_ABS_MT_POSITION_X = 1,
873 FIELD_ABS_MT_POSITION_Y = 2,
874 FIELD_ABS_MT_TOUCH_MAJOR = 4,
875 FIELD_ABS_MT_TOUCH_MINOR = 8,
876 FIELD_ABS_MT_WIDTH_MAJOR = 16,
877 FIELD_ABS_MT_WIDTH_MINOR = 32,
878 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700879 FIELD_ABS_MT_TRACKING_ID = 128,
880 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700881 };
882
883 uint32_t pointerCount;
884 struct Pointer {
885 uint32_t fields;
886
887 int32_t absMTPositionX;
888 int32_t absMTPositionY;
889 int32_t absMTTouchMajor;
890 int32_t absMTTouchMinor;
891 int32_t absMTWidthMajor;
892 int32_t absMTWidthMinor;
893 int32_t absMTOrientation;
894 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700895 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700896
897 inline void clear() {
898 fields = 0;
899 }
900 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
901
902 inline void clear() {
903 pointerCount = 0;
904 pointers[0].clear();
905 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906 } mAccumulator;
907
908 void initialize();
909
910 void sync(nsecs_t when);
911};
912
Jeff Brown46b9ac02010-04-22 18:58:52 -0700913} // namespace android
914
915#endif // _UI_INPUT_READER_H