blob: cfceaab1f425380a6bf321b0942926764ada0b24 [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
Jeff Brown54bc2812010-06-15 01:31:58 -070050/*
51 * Input reader policy interface.
52 *
53 * The input reader policy is used by the input reader to interact with the Window Manager
54 * and other system components.
55 *
56 * The actual implementation is partially supported by callbacks into the DVM
57 * via JNI. This interface is also mocked in the unit tests.
58 */
59class InputReaderPolicyInterface : public virtual RefBase {
60protected:
61 InputReaderPolicyInterface() { }
62 virtual ~InputReaderPolicyInterface() { }
63
64public:
65 /* Display orientations. */
66 enum {
67 ROTATION_0 = 0,
68 ROTATION_90 = 1,
69 ROTATION_180 = 2,
70 ROTATION_270 = 3
71 };
72
Jeff Brown54bc2812010-06-15 01:31:58 -070073 /* Gets information about the display with the specified id.
74 * Returns true if the display info is available, false otherwise.
75 */
76 virtual bool getDisplayInfo(int32_t displayId,
77 int32_t* width, int32_t* height, int32_t* orientation) = 0;
78
Jeff Brown54bc2812010-06-15 01:31:58 -070079 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
80 * certain device whose screen currently is not all that good.
81 */
82 virtual bool filterTouchEvents() = 0;
83
84 /* Determines whether to turn on some hacks to improve touch interaction with another device
85 * where touch coordinate data can get corrupted.
86 */
87 virtual bool filterJumpyTouchEvents() = 0;
88
89 /* Gets the configured virtual key definitions for an input device. */
90 virtual void getVirtualKeyDefinitions(const String8& deviceName,
91 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
92
93 /* Gets the excluded device names for the platform. */
94 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
95};
96
97
98/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -070099class InputReaderInterface : public virtual RefBase {
100protected:
101 InputReaderInterface() { }
102 virtual ~InputReaderInterface() { }
103
104public:
Jeff Browna665ca82010-09-08 11:49:43 -0700105 /* Dumps the state of the input reader.
106 *
107 * This method may be called on any thread (usually by the input manager). */
108 virtual void dump(String8& dump) = 0;
109
Jeff Browne839a582010-04-22 18:58:52 -0700110 /* Runs a single iteration of the processing loop.
111 * Nominally reads and processes one incoming message from the EventHub.
112 *
113 * This method should be called on the input reader thread.
114 */
115 virtual void loopOnce() = 0;
116
Jeff Brown54bc2812010-06-15 01:31:58 -0700117 /* Gets the current input device configuration.
118 *
119 * This method may be called on any thread (usually by the input manager).
120 */
Jeff Browne57e8952010-07-23 21:28:06 -0700121 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700122
Jeff Browne57e8952010-07-23 21:28:06 -0700123 /* Gets information about the specified input device.
124 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
125 * was no such device.
126 *
127 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700128 */
Jeff Browne57e8952010-07-23 21:28:06 -0700129 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
130
131 /* Gets the list of all registered device ids. */
132 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
133
134 /* Query current input state. */
135 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
136 int32_t scanCode) = 0;
137 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
138 int32_t keyCode) = 0;
139 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
140 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700141
142 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700143 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
144 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
145};
146
147
148/* Internal interface used by individual input devices to access global input device state
149 * and parameters maintained by the input reader.
150 */
151class InputReaderContext {
Jeff Brown3c3cc622010-10-20 15:33:38 -0700152public:
Jeff Browne57e8952010-07-23 21:28:06 -0700153 InputReaderContext() { }
154 virtual ~InputReaderContext() { }
155
Jeff Browne57e8952010-07-23 21:28:06 -0700156 virtual void updateGlobalMetaState() = 0;
157 virtual int32_t getGlobalMetaState() = 0;
158
159 virtual InputReaderPolicyInterface* getPolicy() = 0;
160 virtual InputDispatcherInterface* getDispatcher() = 0;
161 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700162};
163
Jeff Brown54bc2812010-06-15 01:31:58 -0700164
Jeff Browne839a582010-04-22 18:58:52 -0700165/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700166 * that it sends to the input dispatcher. Some functions of the input reader, such as early
167 * event filtering in low power states, are controlled by a separate policy object.
168 *
169 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700170 * Because the policy and dispatcher can potentially block or cause re-entrance into
171 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700172 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700173 */
Jeff Brown3c3cc622010-10-20 15:33:38 -0700174class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700175public:
176 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700177 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700178 const sp<InputDispatcherInterface>& dispatcher);
179 virtual ~InputReader();
180
Jeff Browna665ca82010-09-08 11:49:43 -0700181 virtual void dump(String8& dump);
182
Jeff Browne839a582010-04-22 18:58:52 -0700183 virtual void loopOnce();
184
Jeff Browne57e8952010-07-23 21:28:06 -0700185 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700186
Jeff Browne57e8952010-07-23 21:28:06 -0700187 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
188 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700189
Jeff Browne57e8952010-07-23 21:28:06 -0700190 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
191 int32_t scanCode);
192 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
193 int32_t keyCode);
194 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
195 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700196
Jeff Browne57e8952010-07-23 21:28:06 -0700197 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
198 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700199
Jeff Brown3c3cc622010-10-20 15:33:38 -0700200protected:
201 // These methods are protected virtual so they can be overridden and instrumented
202 // by test cases.
203 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
204
Jeff Browne839a582010-04-22 18:58:52 -0700205private:
Jeff Browne839a582010-04-22 18:58:52 -0700206 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700207 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700208 sp<InputDispatcherInterface> mDispatcher;
209
Jeff Browne57e8952010-07-23 21:28:06 -0700210 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
211 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
212 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
213
214 // This reader/writer lock guards the list of input devices.
215 // The writer lock must be held whenever the list of input devices is modified
216 // and then promptly released.
217 // The reader lock must be held whenever the list of input devices is traversed or an
218 // input device in the list is accessed.
219 // This lock only protects the registry and prevents inadvertent deletion of device objects
220 // that are in use. Individual devices are responsible for guarding their own internal state
221 // as needed for concurrent operation.
222 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700223 KeyedVector<int32_t, InputDevice*> mDevices;
224
Jeff Browne57e8952010-07-23 21:28:06 -0700225 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700226 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700227
Jeff Brown1ad00e92010-10-01 18:55:43 -0700228 void addDevice(int32_t deviceId);
229 void removeDevice(int32_t deviceId);
Jeff Brown54bc2812010-06-15 01:31:58 -0700230 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700231
Jeff Browne57e8952010-07-23 21:28:06 -0700232 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700233
Jeff Brown3c3cc622010-10-20 15:33:38 -0700234 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700235
Jeff Browne57e8952010-07-23 21:28:06 -0700236 // state management for all devices
237 Mutex mStateLock;
238
239 int32_t mGlobalMetaState;
240 virtual void updateGlobalMetaState();
241 virtual int32_t getGlobalMetaState();
242
243 InputConfiguration mInputConfiguration;
244 void updateInputConfiguration();
245
246 // state queries
247 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
248 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
249 GetStateFunc getStateFunc);
250 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
251 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700252};
253
254
255/* Reads raw events from the event hub and processes them, endlessly. */
256class InputReaderThread : public Thread {
257public:
258 InputReaderThread(const sp<InputReaderInterface>& reader);
259 virtual ~InputReaderThread();
260
261private:
262 sp<InputReaderInterface> mReader;
263
264 virtual bool threadLoop();
265};
266
Jeff Browne57e8952010-07-23 21:28:06 -0700267
268/* Represents the state of a single input device. */
269class InputDevice {
270public:
271 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
272 ~InputDevice();
273
274 inline InputReaderContext* getContext() { return mContext; }
275 inline int32_t getId() { return mId; }
276 inline const String8& getName() { return mName; }
277 inline uint32_t getSources() { return mSources; }
278
279 inline bool isIgnored() { return mMappers.isEmpty(); }
280
Jeff Brown26c94ff2010-09-30 14:33:04 -0700281 void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700282 void addMapper(InputMapper* mapper);
283 void configure();
284 void reset();
285 void process(const RawEvent* rawEvent);
286
287 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
288 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
289 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
290 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
291 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
292 const int32_t* keyCodes, uint8_t* outFlags);
293
294 int32_t getMetaState();
295
Jeff Brown66888372010-11-29 17:37:49 -0800296 inline const PropertyMap& getConfiguration() {
297 return mConfiguration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700298 }
299
Jeff Browne57e8952010-07-23 21:28:06 -0700300private:
301 InputReaderContext* mContext;
302 int32_t mId;
303
304 Vector<InputMapper*> mMappers;
305
306 String8 mName;
307 uint32_t mSources;
308
309 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
310 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700311
Jeff Brown66888372010-11-29 17:37:49 -0800312 PropertyMap mConfiguration;
Jeff Browne57e8952010-07-23 21:28:06 -0700313};
314
315
316/* An input mapper transforms raw input events into cooked event data.
317 * A single input device can have multiple associated input mappers in order to interpret
318 * different classes of events.
319 */
320class InputMapper {
321public:
322 InputMapper(InputDevice* device);
323 virtual ~InputMapper();
324
325 inline InputDevice* getDevice() { return mDevice; }
326 inline int32_t getDeviceId() { return mDevice->getId(); }
327 inline const String8 getDeviceName() { return mDevice->getName(); }
328 inline InputReaderContext* getContext() { return mContext; }
329 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
330 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
331 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
332
333 virtual uint32_t getSources() = 0;
334 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700335 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700336 virtual void configure();
337 virtual void reset();
338 virtual void process(const RawEvent* rawEvent) = 0;
339
340 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
341 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
342 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
343 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
344 const int32_t* keyCodes, uint8_t* outFlags);
345
346 virtual int32_t getMetaState();
347
348protected:
349 InputDevice* mDevice;
350 InputReaderContext* mContext;
Jeff Browne57e8952010-07-23 21:28:06 -0700351};
352
353
354class SwitchInputMapper : public InputMapper {
355public:
356 SwitchInputMapper(InputDevice* device);
357 virtual ~SwitchInputMapper();
358
359 virtual uint32_t getSources();
360 virtual void process(const RawEvent* rawEvent);
361
362 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
363
364private:
365 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
366};
367
368
369class KeyboardInputMapper : public InputMapper {
370public:
Jeff Brown66888372010-11-29 17:37:49 -0800371 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Browne57e8952010-07-23 21:28:06 -0700372 virtual ~KeyboardInputMapper();
373
374 virtual uint32_t getSources();
375 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700376 virtual void dump(String8& dump);
Jeff Brown66888372010-11-29 17:37:49 -0800377 virtual void configure();
Jeff Browne57e8952010-07-23 21:28:06 -0700378 virtual void reset();
379 virtual void process(const RawEvent* rawEvent);
380
381 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
382 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
383 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
384 const int32_t* keyCodes, uint8_t* outFlags);
385
386 virtual int32_t getMetaState();
387
388private:
Jeff Brownb51719b2010-07-29 18:18:33 -0700389 Mutex mLock;
390
Jeff Browne57e8952010-07-23 21:28:06 -0700391 struct KeyDown {
392 int32_t keyCode;
393 int32_t scanCode;
394 };
395
Jeff Browne57e8952010-07-23 21:28:06 -0700396 uint32_t mSources;
397 int32_t mKeyboardType;
398
Jeff Brown66888372010-11-29 17:37:49 -0800399 // Immutable configuration parameters.
400 struct Parameters {
401 int32_t associatedDisplayId;
402 bool orientationAware;
403 } mParameters;
404
Jeff Brownb51719b2010-07-29 18:18:33 -0700405 struct LockedState {
406 Vector<KeyDown> keyDowns; // keys that are down
407 int32_t metaState;
408 nsecs_t downTime; // time of most recent key down
Jeff Brown6a817e22010-09-12 17:55:08 -0700409
410 struct LedState {
411 bool avail; // led is available
412 bool on; // we think the led is currently on
413 };
414 LedState capsLockLedState;
415 LedState numLockLedState;
416 LedState scrollLockLedState;
Jeff Brownb51719b2010-07-29 18:18:33 -0700417 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700418
Jeff Brownb51719b2010-07-29 18:18:33 -0700419 void initializeLocked();
Jeff Brown6a817e22010-09-12 17:55:08 -0700420 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Browne57e8952010-07-23 21:28:06 -0700421
Jeff Brown66888372010-11-29 17:37:49 -0800422 void configureParameters();
423 void dumpParameters(String8& dump);
424
Jeff Browne57e8952010-07-23 21:28:06 -0700425 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700426
Jeff Browne57e8952010-07-23 21:28:06 -0700427 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
428 uint32_t policyFlags);
429
Jeff Brownb51719b2010-07-29 18:18:33 -0700430 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6a817e22010-09-12 17:55:08 -0700431
432 void updateLedStateLocked(bool reset);
433 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
434 int32_t modifier, bool reset);
Jeff Browne57e8952010-07-23 21:28:06 -0700435};
436
437
438class TrackballInputMapper : public InputMapper {
439public:
Jeff Brown66888372010-11-29 17:37:49 -0800440 TrackballInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700441 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 Brown66888372010-11-29 17:37:49 -0800446 virtual void configure();
Jeff Browne57e8952010-07-23 21:28:06 -0700447 virtual void reset();
448 virtual void process(const RawEvent* rawEvent);
449
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700450 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
451
Jeff Browne57e8952010-07-23 21:28:06 -0700452private:
453 // Amount that trackball needs to move in order to generate a key event.
454 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
455
Jeff Brownb51719b2010-07-29 18:18:33 -0700456 Mutex mLock;
457
Jeff Brown66888372010-11-29 17:37:49 -0800458 // Immutable configuration parameters.
459 struct Parameters {
460 int32_t associatedDisplayId;
461 bool orientationAware;
462 } mParameters;
Jeff Browne57e8952010-07-23 21:28:06 -0700463
464 struct Accumulator {
465 enum {
466 FIELD_BTN_MOUSE = 1,
467 FIELD_REL_X = 2,
468 FIELD_REL_Y = 4
469 };
470
471 uint32_t fields;
472
473 bool btnMouse;
474 int32_t relX;
475 int32_t relY;
476
477 inline void clear() {
478 fields = 0;
479 }
Jeff Browne57e8952010-07-23 21:28:06 -0700480 } mAccumulator;
481
Jeff Browne57e8952010-07-23 21:28:06 -0700482 float mXScale;
483 float mYScale;
484 float mXPrecision;
485 float mYPrecision;
486
Jeff Brownb51719b2010-07-29 18:18:33 -0700487 struct LockedState {
488 bool down;
489 nsecs_t downTime;
490 } mLocked;
491
492 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700493
Jeff Brown66888372010-11-29 17:37:49 -0800494 void configureParameters();
495 void dumpParameters(String8& dump);
496
Jeff Browne57e8952010-07-23 21:28:06 -0700497 void sync(nsecs_t when);
498};
499
500
501class TouchInputMapper : public InputMapper {
502public:
Jeff Brown66888372010-11-29 17:37:49 -0800503 TouchInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700504 virtual ~TouchInputMapper();
505
506 virtual uint32_t getSources();
507 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700508 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700509 virtual void configure();
510 virtual void reset();
511
512 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
513 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
514 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
515 const int32_t* keyCodes, uint8_t* outFlags);
516
517protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700518 Mutex mLock;
519
Jeff Browne57e8952010-07-23 21:28:06 -0700520 struct VirtualKey {
521 int32_t keyCode;
522 int32_t scanCode;
523 uint32_t flags;
524
525 // computed hit box, specified in touch screen coords based on known display size
526 int32_t hitLeft;
527 int32_t hitTop;
528 int32_t hitRight;
529 int32_t hitBottom;
530
531 inline bool isHit(int32_t x, int32_t y) const {
532 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
533 }
534 };
535
Jeff Brown38a7fab2010-08-30 03:02:23 -0700536 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700537 struct PointerData {
538 uint32_t id;
539 int32_t x;
540 int32_t y;
541 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700542 int32_t touchMajor;
543 int32_t touchMinor;
544 int32_t toolMajor;
545 int32_t toolMinor;
546 int32_t orientation;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700547
548 inline bool operator== (const PointerData& other) const {
549 return id == other.id
550 && x == other.x
551 && y == other.y
552 && pressure == other.pressure
553 && touchMajor == other.touchMajor
554 && touchMinor == other.touchMinor
555 && toolMajor == other.toolMajor
556 && toolMinor == other.toolMinor
557 && orientation == other.orientation;
558 }
559 inline bool operator!= (const PointerData& other) const {
560 return !(*this == other);
561 }
Jeff Browne57e8952010-07-23 21:28:06 -0700562 };
563
Jeff Brown38a7fab2010-08-30 03:02:23 -0700564 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700565 struct TouchData {
566 uint32_t pointerCount;
567 PointerData pointers[MAX_POINTERS];
568 BitSet32 idBits;
569 uint32_t idToIndex[MAX_POINTER_ID + 1];
570
571 void copyFrom(const TouchData& other) {
572 pointerCount = other.pointerCount;
573 idBits = other.idBits;
574
575 for (uint32_t i = 0; i < pointerCount; i++) {
576 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700577
578 int id = pointers[i].id;
579 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700580 }
581 }
582
583 inline void clear() {
584 pointerCount = 0;
585 idBits.clear();
586 }
587 };
588
Jeff Browne57e8952010-07-23 21:28:06 -0700589 // Immutable configuration parameters.
590 struct Parameters {
Jeff Brown66888372010-11-29 17:37:49 -0800591 enum DeviceType {
592 DEVICE_TYPE_TOUCH_SCREEN,
593 DEVICE_TYPE_TOUCH_PAD,
594 };
595
596 DeviceType deviceType;
597 int32_t associatedDisplayId;
598 bool orientationAware;
599
Jeff Browne57e8952010-07-23 21:28:06 -0700600 bool useBadTouchFilter;
601 bool useJumpyTouchFilter;
602 bool useAveragingTouchFilter;
603 } mParameters;
604
Jeff Brown38a7fab2010-08-30 03:02:23 -0700605 // Immutable calibration parameters in parsed form.
606 struct Calibration {
Jeff Brown60b57762010-10-18 13:32:20 -0700607 // Position
608 bool haveXOrigin;
609 int32_t xOrigin;
610 bool haveYOrigin;
611 int32_t yOrigin;
612 bool haveXScale;
613 float xScale;
614 bool haveYScale;
615 float yScale;
616
Jeff Brown6b337e72010-10-14 21:42:15 -0700617 // Touch Size
618 enum TouchSizeCalibration {
619 TOUCH_SIZE_CALIBRATION_DEFAULT,
620 TOUCH_SIZE_CALIBRATION_NONE,
621 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
622 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700623 };
624
Jeff Brown6b337e72010-10-14 21:42:15 -0700625 TouchSizeCalibration touchSizeCalibration;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700626
Jeff Brown6b337e72010-10-14 21:42:15 -0700627 // Tool Size
628 enum ToolSizeCalibration {
629 TOOL_SIZE_CALIBRATION_DEFAULT,
630 TOOL_SIZE_CALIBRATION_NONE,
631 TOOL_SIZE_CALIBRATION_GEOMETRIC,
632 TOOL_SIZE_CALIBRATION_LINEAR,
633 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700634 };
635
Jeff Brown6b337e72010-10-14 21:42:15 -0700636 ToolSizeCalibration toolSizeCalibration;
637 bool haveToolSizeLinearScale;
638 float toolSizeLinearScale;
639 bool haveToolSizeLinearBias;
640 float toolSizeLinearBias;
641 bool haveToolSizeAreaScale;
642 float toolSizeAreaScale;
643 bool haveToolSizeAreaBias;
644 float toolSizeAreaBias;
645 bool haveToolSizeIsSummed;
Jeff Brown66888372010-11-29 17:37:49 -0800646 bool toolSizeIsSummed;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700647
648 // Pressure
649 enum PressureCalibration {
650 PRESSURE_CALIBRATION_DEFAULT,
651 PRESSURE_CALIBRATION_NONE,
652 PRESSURE_CALIBRATION_PHYSICAL,
653 PRESSURE_CALIBRATION_AMPLITUDE,
654 };
655 enum PressureSource {
656 PRESSURE_SOURCE_DEFAULT,
657 PRESSURE_SOURCE_PRESSURE,
658 PRESSURE_SOURCE_TOUCH,
659 };
660
661 PressureCalibration pressureCalibration;
662 PressureSource pressureSource;
663 bool havePressureScale;
664 float pressureScale;
665
666 // Size
667 enum SizeCalibration {
668 SIZE_CALIBRATION_DEFAULT,
669 SIZE_CALIBRATION_NONE,
670 SIZE_CALIBRATION_NORMALIZED,
671 };
672
673 SizeCalibration sizeCalibration;
674
675 // Orientation
676 enum OrientationCalibration {
677 ORIENTATION_CALIBRATION_DEFAULT,
678 ORIENTATION_CALIBRATION_NONE,
679 ORIENTATION_CALIBRATION_INTERPOLATED,
680 };
681
682 OrientationCalibration orientationCalibration;
683 } mCalibration;
684
685 // Raw axis information from the driver.
686 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700687 RawAbsoluteAxisInfo x;
688 RawAbsoluteAxisInfo y;
689 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700690 RawAbsoluteAxisInfo touchMajor;
691 RawAbsoluteAxisInfo touchMinor;
692 RawAbsoluteAxisInfo toolMajor;
693 RawAbsoluteAxisInfo toolMinor;
694 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700695 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700696
Jeff Brownb51719b2010-07-29 18:18:33 -0700697 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700698 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700699 TouchData mLastTouch;
700
701 // The time the primary pointer last went down.
702 nsecs_t mDownTime;
703
Jeff Brownb51719b2010-07-29 18:18:33 -0700704 struct LockedState {
705 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700706
Jeff Brownb51719b2010-07-29 18:18:33 -0700707 // The surface orientation and width and height set by configureSurfaceLocked().
708 int32_t surfaceOrientation;
709 int32_t surfaceWidth, surfaceHeight;
710
711 // Translation and scaling factors, orientation-independent.
712 int32_t xOrigin;
713 float xScale;
714 float xPrecision;
715
716 int32_t yOrigin;
717 float yScale;
718 float yPrecision;
719
Jeff Brown38a7fab2010-08-30 03:02:23 -0700720 float geometricScale;
721
Jeff Brown6b337e72010-10-14 21:42:15 -0700722 float toolSizeLinearScale;
723 float toolSizeLinearBias;
724 float toolSizeAreaScale;
725 float toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700726
Jeff Brownb51719b2010-07-29 18:18:33 -0700727 float pressureScale;
728
Jeff Brownb51719b2010-07-29 18:18:33 -0700729 float sizeScale;
730
731 float orientationScale;
732
733 // Oriented motion ranges for input device info.
734 struct OrientedRanges {
735 InputDeviceInfo::MotionRange x;
736 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700737
738 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700739 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700740
741 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700742 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700743
Jeff Brown6b337e72010-10-14 21:42:15 -0700744 bool haveTouchSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700745 InputDeviceInfo::MotionRange touchMajor;
746 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700747
Jeff Brown6b337e72010-10-14 21:42:15 -0700748 bool haveToolSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700749 InputDeviceInfo::MotionRange toolMajor;
750 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700751
752 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700753 InputDeviceInfo::MotionRange orientation;
754 } orientedRanges;
755
756 // Oriented dimensions and precision.
757 float orientedSurfaceWidth, orientedSurfaceHeight;
758 float orientedXPrecision, orientedYPrecision;
759
760 struct CurrentVirtualKeyState {
761 bool down;
762 nsecs_t downTime;
763 int32_t keyCode;
764 int32_t scanCode;
765 } currentVirtualKey;
766 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700767
Jeff Brown38a7fab2010-08-30 03:02:23 -0700768 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700769 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700770 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700771 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700772 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700773 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700774 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700775 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700776 virtual void parseCalibration();
777 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700778 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700779
780 enum TouchResult {
781 // Dispatch the touch normally.
782 DISPATCH_TOUCH,
783 // Do not dispatch the touch, but keep tracking the current stroke.
784 SKIP_TOUCH,
785 // Do not dispatch the touch, and drop all information associated with the current stoke
786 // so the next movement will appear as a new down.
787 DROP_STROKE
788 };
789
790 void syncTouch(nsecs_t when, bool havePointerIds);
791
792private:
793 /* Maximum number of historical samples to average. */
794 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
795
796 /* Slop distance for jumpy pointer detection.
797 * The vertical range of the screen divided by this is our epsilon value. */
798 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
799
800 /* Number of jumpy points to drop for touchscreens that need it. */
801 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
802 static const uint32_t JUMPY_DROP_LIMIT = 3;
803
804 /* Maximum squared distance for averaging.
805 * If moving farther than this, turn of averaging to avoid lag in response. */
806 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
807
808 struct AveragingTouchFilterState {
809 // Individual history tracks are stored by pointer id
810 uint32_t historyStart[MAX_POINTERS];
811 uint32_t historyEnd[MAX_POINTERS];
812 struct {
813 struct {
814 int32_t x;
815 int32_t y;
816 int32_t pressure;
817 } pointers[MAX_POINTERS];
818 } historyData[AVERAGING_HISTORY_SIZE];
819 } mAveragingTouchFilter;
820
Jeff Brownd64c8552010-08-17 20:38:35 -0700821 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700822 uint32_t jumpyPointsDropped;
823 } mJumpyTouchFilter;
824
825 struct PointerDistanceHeapElement {
826 uint32_t currentPointerIndex : 8;
827 uint32_t lastPointerIndex : 8;
828 uint64_t distance : 48; // squared distance
829 };
830
Jeff Brownb51719b2010-07-29 18:18:33 -0700831 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700832
833 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
834 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
835 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700836 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
837 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700838
Jeff Brownb51719b2010-07-29 18:18:33 -0700839 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
840 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700841
842 bool applyBadTouchFilter();
843 bool applyJumpyTouchFilter();
844 void applyAveragingTouchFilter();
845 void calculatePointerIds();
846};
847
848
849class SingleTouchInputMapper : public TouchInputMapper {
850public:
Jeff Brown66888372010-11-29 17:37:49 -0800851 SingleTouchInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700852 virtual ~SingleTouchInputMapper();
853
854 virtual void reset();
855 virtual void process(const RawEvent* rawEvent);
856
857protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700858 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700859
860private:
861 struct Accumulator {
862 enum {
863 FIELD_BTN_TOUCH = 1,
864 FIELD_ABS_X = 2,
865 FIELD_ABS_Y = 4,
866 FIELD_ABS_PRESSURE = 8,
867 FIELD_ABS_TOOL_WIDTH = 16
868 };
869
870 uint32_t fields;
871
872 bool btnTouch;
873 int32_t absX;
874 int32_t absY;
875 int32_t absPressure;
876 int32_t absToolWidth;
877
878 inline void clear() {
879 fields = 0;
880 }
Jeff Browne57e8952010-07-23 21:28:06 -0700881 } mAccumulator;
882
883 bool mDown;
884 int32_t mX;
885 int32_t mY;
886 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700887 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700888
889 void initialize();
890
891 void sync(nsecs_t when);
892};
893
894
895class MultiTouchInputMapper : public TouchInputMapper {
896public:
Jeff Brown66888372010-11-29 17:37:49 -0800897 MultiTouchInputMapper(InputDevice* device);
Jeff Browne57e8952010-07-23 21:28:06 -0700898 virtual ~MultiTouchInputMapper();
899
900 virtual void reset();
901 virtual void process(const RawEvent* rawEvent);
902
903protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700904 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700905
906private:
907 struct Accumulator {
908 enum {
909 FIELD_ABS_MT_POSITION_X = 1,
910 FIELD_ABS_MT_POSITION_Y = 2,
911 FIELD_ABS_MT_TOUCH_MAJOR = 4,
912 FIELD_ABS_MT_TOUCH_MINOR = 8,
913 FIELD_ABS_MT_WIDTH_MAJOR = 16,
914 FIELD_ABS_MT_WIDTH_MINOR = 32,
915 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700916 FIELD_ABS_MT_TRACKING_ID = 128,
917 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700918 };
919
920 uint32_t pointerCount;
921 struct Pointer {
922 uint32_t fields;
923
924 int32_t absMTPositionX;
925 int32_t absMTPositionY;
926 int32_t absMTTouchMajor;
927 int32_t absMTTouchMinor;
928 int32_t absMTWidthMajor;
929 int32_t absMTWidthMinor;
930 int32_t absMTOrientation;
931 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700932 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700933
934 inline void clear() {
935 fields = 0;
936 }
937 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
938
939 inline void clear() {
940 pointerCount = 0;
941 pointers[0].clear();
942 }
Jeff Browne57e8952010-07-23 21:28:06 -0700943 } mAccumulator;
944
945 void initialize();
946
947 void sync(nsecs_t when);
948};
949
Jeff Browne839a582010-04-22 18:58:52 -0700950} // namespace android
951
952#endif // _UI_INPUT_READER_H