blob: f3a2dd269ef12b12935bb78ce030691f3b2cf4b6 [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
Jeff Brown6a817e22010-09-12 17:55:08 -0700425
426 struct LedState {
427 bool avail; // led is available
428 bool on; // we think the led is currently on
429 };
430 LedState capsLockLedState;
431 LedState numLockLedState;
432 LedState scrollLockLedState;
Jeff Brownb51719b2010-07-29 18:18:33 -0700433 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700434
Jeff Brownb51719b2010-07-29 18:18:33 -0700435 void initializeLocked();
Jeff Brown6a817e22010-09-12 17:55:08 -0700436 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Browne57e8952010-07-23 21:28:06 -0700437
438 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700439
Jeff Browne57e8952010-07-23 21:28:06 -0700440 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
441 uint32_t policyFlags);
442
Jeff Brownb51719b2010-07-29 18:18:33 -0700443 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6a817e22010-09-12 17:55:08 -0700444
445 void updateLedStateLocked(bool reset);
446 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
447 int32_t modifier, bool reset);
Jeff Browne57e8952010-07-23 21:28:06 -0700448};
449
450
451class TrackballInputMapper : public InputMapper {
452public:
453 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
454 virtual ~TrackballInputMapper();
455
456 virtual uint32_t getSources();
457 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700458 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700459 virtual void reset();
460 virtual void process(const RawEvent* rawEvent);
461
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700462 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
463
Jeff Browne57e8952010-07-23 21:28:06 -0700464private:
465 // Amount that trackball needs to move in order to generate a key event.
466 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
467
Jeff Brownb51719b2010-07-29 18:18:33 -0700468 Mutex mLock;
469
Jeff Browne57e8952010-07-23 21:28:06 -0700470 int32_t mAssociatedDisplayId;
471
472 struct Accumulator {
473 enum {
474 FIELD_BTN_MOUSE = 1,
475 FIELD_REL_X = 2,
476 FIELD_REL_Y = 4
477 };
478
479 uint32_t fields;
480
481 bool btnMouse;
482 int32_t relX;
483 int32_t relY;
484
485 inline void clear() {
486 fields = 0;
487 }
Jeff Browne57e8952010-07-23 21:28:06 -0700488 } mAccumulator;
489
Jeff Browne57e8952010-07-23 21:28:06 -0700490 float mXScale;
491 float mYScale;
492 float mXPrecision;
493 float mYPrecision;
494
Jeff Brownb51719b2010-07-29 18:18:33 -0700495 struct LockedState {
496 bool down;
497 nsecs_t downTime;
498 } mLocked;
499
500 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700501
502 void sync(nsecs_t when);
503};
504
505
506class TouchInputMapper : public InputMapper {
507public:
508 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
509 virtual ~TouchInputMapper();
510
511 virtual uint32_t getSources();
512 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700513 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700514 virtual void configure();
515 virtual void reset();
516
517 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
518 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
519 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
520 const int32_t* keyCodes, uint8_t* outFlags);
521
522protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700523 Mutex mLock;
524
Jeff Browne57e8952010-07-23 21:28:06 -0700525 struct VirtualKey {
526 int32_t keyCode;
527 int32_t scanCode;
528 uint32_t flags;
529
530 // computed hit box, specified in touch screen coords based on known display size
531 int32_t hitLeft;
532 int32_t hitTop;
533 int32_t hitRight;
534 int32_t hitBottom;
535
536 inline bool isHit(int32_t x, int32_t y) const {
537 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
538 }
539 };
540
Jeff Brown38a7fab2010-08-30 03:02:23 -0700541 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700542 struct PointerData {
543 uint32_t id;
544 int32_t x;
545 int32_t y;
546 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700547 int32_t touchMajor;
548 int32_t touchMinor;
549 int32_t toolMajor;
550 int32_t toolMinor;
551 int32_t orientation;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700552
553 inline bool operator== (const PointerData& other) const {
554 return id == other.id
555 && x == other.x
556 && y == other.y
557 && pressure == other.pressure
558 && touchMajor == other.touchMajor
559 && touchMinor == other.touchMinor
560 && toolMajor == other.toolMajor
561 && toolMinor == other.toolMinor
562 && orientation == other.orientation;
563 }
564 inline bool operator!= (const PointerData& other) const {
565 return !(*this == other);
566 }
Jeff Browne57e8952010-07-23 21:28:06 -0700567 };
568
Jeff Brown38a7fab2010-08-30 03:02:23 -0700569 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700570 struct TouchData {
571 uint32_t pointerCount;
572 PointerData pointers[MAX_POINTERS];
573 BitSet32 idBits;
574 uint32_t idToIndex[MAX_POINTER_ID + 1];
575
576 void copyFrom(const TouchData& other) {
577 pointerCount = other.pointerCount;
578 idBits = other.idBits;
579
580 for (uint32_t i = 0; i < pointerCount; i++) {
581 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700582
583 int id = pointers[i].id;
584 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700585 }
586 }
587
588 inline void clear() {
589 pointerCount = 0;
590 idBits.clear();
591 }
592 };
593
594 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700595
596 // Immutable configuration parameters.
597 struct Parameters {
598 bool useBadTouchFilter;
599 bool useJumpyTouchFilter;
600 bool useAveragingTouchFilter;
601 } mParameters;
602
Jeff Brown38a7fab2010-08-30 03:02:23 -0700603 // Immutable calibration parameters in parsed form.
604 struct Calibration {
Jeff Brown60b57762010-10-18 13:32:20 -0700605 // Position
606 bool haveXOrigin;
607 int32_t xOrigin;
608 bool haveYOrigin;
609 int32_t yOrigin;
610 bool haveXScale;
611 float xScale;
612 bool haveYScale;
613 float yScale;
614
Jeff Brown6b337e72010-10-14 21:42:15 -0700615 // Touch Size
616 enum TouchSizeCalibration {
617 TOUCH_SIZE_CALIBRATION_DEFAULT,
618 TOUCH_SIZE_CALIBRATION_NONE,
619 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
620 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700621 };
622
Jeff Brown6b337e72010-10-14 21:42:15 -0700623 TouchSizeCalibration touchSizeCalibration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700624
Jeff Brown6b337e72010-10-14 21:42:15 -0700625 // Tool Size
626 enum ToolSizeCalibration {
627 TOOL_SIZE_CALIBRATION_DEFAULT,
628 TOOL_SIZE_CALIBRATION_NONE,
629 TOOL_SIZE_CALIBRATION_GEOMETRIC,
630 TOOL_SIZE_CALIBRATION_LINEAR,
631 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700632 };
633
Jeff Brown6b337e72010-10-14 21:42:15 -0700634 ToolSizeCalibration toolSizeCalibration;
635 bool haveToolSizeLinearScale;
636 float toolSizeLinearScale;
637 bool haveToolSizeLinearBias;
638 float toolSizeLinearBias;
639 bool haveToolSizeAreaScale;
640 float toolSizeAreaScale;
641 bool haveToolSizeAreaBias;
642 float toolSizeAreaBias;
643 bool haveToolSizeIsSummed;
644 int32_t toolSizeIsSummed;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700645
646 // Pressure
647 enum PressureCalibration {
648 PRESSURE_CALIBRATION_DEFAULT,
649 PRESSURE_CALIBRATION_NONE,
650 PRESSURE_CALIBRATION_PHYSICAL,
651 PRESSURE_CALIBRATION_AMPLITUDE,
652 };
653 enum PressureSource {
654 PRESSURE_SOURCE_DEFAULT,
655 PRESSURE_SOURCE_PRESSURE,
656 PRESSURE_SOURCE_TOUCH,
657 };
658
659 PressureCalibration pressureCalibration;
660 PressureSource pressureSource;
661 bool havePressureScale;
662 float pressureScale;
663
664 // Size
665 enum SizeCalibration {
666 SIZE_CALIBRATION_DEFAULT,
667 SIZE_CALIBRATION_NONE,
668 SIZE_CALIBRATION_NORMALIZED,
669 };
670
671 SizeCalibration sizeCalibration;
672
673 // Orientation
674 enum OrientationCalibration {
675 ORIENTATION_CALIBRATION_DEFAULT,
676 ORIENTATION_CALIBRATION_NONE,
677 ORIENTATION_CALIBRATION_INTERPOLATED,
678 };
679
680 OrientationCalibration orientationCalibration;
681 } mCalibration;
682
683 // Raw axis information from the driver.
684 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700685 RawAbsoluteAxisInfo x;
686 RawAbsoluteAxisInfo y;
687 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700688 RawAbsoluteAxisInfo touchMajor;
689 RawAbsoluteAxisInfo touchMinor;
690 RawAbsoluteAxisInfo toolMajor;
691 RawAbsoluteAxisInfo toolMinor;
692 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700693 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700694
Jeff Brownb51719b2010-07-29 18:18:33 -0700695 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700696 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700697 TouchData mLastTouch;
698
699 // The time the primary pointer last went down.
700 nsecs_t mDownTime;
701
Jeff Brownb51719b2010-07-29 18:18:33 -0700702 struct LockedState {
703 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700704
Jeff Brownb51719b2010-07-29 18:18:33 -0700705 // The surface orientation and width and height set by configureSurfaceLocked().
706 int32_t surfaceOrientation;
707 int32_t surfaceWidth, surfaceHeight;
708
709 // Translation and scaling factors, orientation-independent.
710 int32_t xOrigin;
711 float xScale;
712 float xPrecision;
713
714 int32_t yOrigin;
715 float yScale;
716 float yPrecision;
717
Jeff Brown38a7fab2010-08-30 03:02:23 -0700718 float geometricScale;
719
Jeff Brown6b337e72010-10-14 21:42:15 -0700720 float toolSizeLinearScale;
721 float toolSizeLinearBias;
722 float toolSizeAreaScale;
723 float toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700724
Jeff Brownb51719b2010-07-29 18:18:33 -0700725 float pressureScale;
726
Jeff Brownb51719b2010-07-29 18:18:33 -0700727 float sizeScale;
728
729 float orientationScale;
730
731 // Oriented motion ranges for input device info.
732 struct OrientedRanges {
733 InputDeviceInfo::MotionRange x;
734 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700735
736 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700737 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700738
739 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700740 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700741
Jeff Brown6b337e72010-10-14 21:42:15 -0700742 bool haveTouchSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700743 InputDeviceInfo::MotionRange touchMajor;
744 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700745
Jeff Brown6b337e72010-10-14 21:42:15 -0700746 bool haveToolSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700747 InputDeviceInfo::MotionRange toolMajor;
748 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700749
750 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700751 InputDeviceInfo::MotionRange orientation;
752 } orientedRanges;
753
754 // Oriented dimensions and precision.
755 float orientedSurfaceWidth, orientedSurfaceHeight;
756 float orientedXPrecision, orientedYPrecision;
757
758 struct CurrentVirtualKeyState {
759 bool down;
760 nsecs_t downTime;
761 int32_t keyCode;
762 int32_t scanCode;
763 } currentVirtualKey;
764 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700765
Jeff Brown38a7fab2010-08-30 03:02:23 -0700766 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700767 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700768 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700769 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700770 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700771 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700772 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700773 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700774 virtual void parseCalibration();
775 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700776 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700777
778 enum TouchResult {
779 // Dispatch the touch normally.
780 DISPATCH_TOUCH,
781 // Do not dispatch the touch, but keep tracking the current stroke.
782 SKIP_TOUCH,
783 // Do not dispatch the touch, and drop all information associated with the current stoke
784 // so the next movement will appear as a new down.
785 DROP_STROKE
786 };
787
788 void syncTouch(nsecs_t when, bool havePointerIds);
789
790private:
791 /* Maximum number of historical samples to average. */
792 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
793
794 /* Slop distance for jumpy pointer detection.
795 * The vertical range of the screen divided by this is our epsilon value. */
796 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
797
798 /* Number of jumpy points to drop for touchscreens that need it. */
799 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
800 static const uint32_t JUMPY_DROP_LIMIT = 3;
801
802 /* Maximum squared distance for averaging.
803 * If moving farther than this, turn of averaging to avoid lag in response. */
804 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
805
806 struct AveragingTouchFilterState {
807 // Individual history tracks are stored by pointer id
808 uint32_t historyStart[MAX_POINTERS];
809 uint32_t historyEnd[MAX_POINTERS];
810 struct {
811 struct {
812 int32_t x;
813 int32_t y;
814 int32_t pressure;
815 } pointers[MAX_POINTERS];
816 } historyData[AVERAGING_HISTORY_SIZE];
817 } mAveragingTouchFilter;
818
Jeff Brownd64c8552010-08-17 20:38:35 -0700819 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700820 uint32_t jumpyPointsDropped;
821 } mJumpyTouchFilter;
822
823 struct PointerDistanceHeapElement {
824 uint32_t currentPointerIndex : 8;
825 uint32_t lastPointerIndex : 8;
826 uint64_t distance : 48; // squared distance
827 };
828
Jeff Brownb51719b2010-07-29 18:18:33 -0700829 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700830
831 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
832 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
833 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700834 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
835 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700836
Jeff Brownb51719b2010-07-29 18:18:33 -0700837 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
838 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700839
840 bool applyBadTouchFilter();
841 bool applyJumpyTouchFilter();
842 void applyAveragingTouchFilter();
843 void calculatePointerIds();
844};
845
846
847class SingleTouchInputMapper : public TouchInputMapper {
848public:
849 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
850 virtual ~SingleTouchInputMapper();
851
852 virtual void reset();
853 virtual void process(const RawEvent* rawEvent);
854
855protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700856 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700857
858private:
859 struct Accumulator {
860 enum {
861 FIELD_BTN_TOUCH = 1,
862 FIELD_ABS_X = 2,
863 FIELD_ABS_Y = 4,
864 FIELD_ABS_PRESSURE = 8,
865 FIELD_ABS_TOOL_WIDTH = 16
866 };
867
868 uint32_t fields;
869
870 bool btnTouch;
871 int32_t absX;
872 int32_t absY;
873 int32_t absPressure;
874 int32_t absToolWidth;
875
876 inline void clear() {
877 fields = 0;
878 }
Jeff Browne57e8952010-07-23 21:28:06 -0700879 } mAccumulator;
880
881 bool mDown;
882 int32_t mX;
883 int32_t mY;
884 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700885 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700886
887 void initialize();
888
889 void sync(nsecs_t when);
890};
891
892
893class MultiTouchInputMapper : public TouchInputMapper {
894public:
895 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
896 virtual ~MultiTouchInputMapper();
897
898 virtual void reset();
899 virtual void process(const RawEvent* rawEvent);
900
901protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700902 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700903
904private:
905 struct Accumulator {
906 enum {
907 FIELD_ABS_MT_POSITION_X = 1,
908 FIELD_ABS_MT_POSITION_Y = 2,
909 FIELD_ABS_MT_TOUCH_MAJOR = 4,
910 FIELD_ABS_MT_TOUCH_MINOR = 8,
911 FIELD_ABS_MT_WIDTH_MAJOR = 16,
912 FIELD_ABS_MT_WIDTH_MINOR = 32,
913 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700914 FIELD_ABS_MT_TRACKING_ID = 128,
915 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700916 };
917
918 uint32_t pointerCount;
919 struct Pointer {
920 uint32_t fields;
921
922 int32_t absMTPositionX;
923 int32_t absMTPositionY;
924 int32_t absMTTouchMajor;
925 int32_t absMTTouchMinor;
926 int32_t absMTWidthMajor;
927 int32_t absMTWidthMinor;
928 int32_t absMTOrientation;
929 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700930 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700931
932 inline void clear() {
933 fields = 0;
934 }
935 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
936
937 inline void clear() {
938 pointerCount = 0;
939 pointers[0].clear();
940 }
Jeff Browne57e8952010-07-23 21:28:06 -0700941 } mAccumulator;
942
943 void initialize();
944
945 void sync(nsecs_t when);
946};
947
Jeff Browne839a582010-04-22 18:58:52 -0700948} // namespace android
949
950#endif // _UI_INPUT_READER_H