blob: 00a06be161b6c4603f8418be3b715c3fd4258557 [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>
Jeff Brown83c09682010-12-23 17:50:18 -080023#include <ui/PointerController.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070024#include <utils/KeyedVector.h>
25#include <utils/threads.h>
26#include <utils/Timers.h>
27#include <utils/RefBase.h>
28#include <utils/String8.h>
29#include <utils/BitSet.h>
30
31#include <stddef.h>
32#include <unistd.h>
33
Jeff Brown46b9ac02010-04-22 18:58:52 -070034namespace android {
35
Jeff Brown6d0fec22010-07-23 21:28:06 -070036class InputDevice;
37class InputMapper;
38
Jeff Brown8d608662010-08-30 03:02:23 -070039
Jeff Brown9c3cda02010-06-15 01:31:58 -070040/*
41 * Input reader policy interface.
42 *
43 * The input reader policy is used by the input reader to interact with the Window Manager
44 * and other system components.
45 *
46 * The actual implementation is partially supported by callbacks into the DVM
47 * via JNI. This interface is also mocked in the unit tests.
48 */
49class InputReaderPolicyInterface : public virtual RefBase {
50protected:
51 InputReaderPolicyInterface() { }
52 virtual ~InputReaderPolicyInterface() { }
53
54public:
55 /* Display orientations. */
56 enum {
57 ROTATION_0 = 0,
58 ROTATION_90 = 1,
59 ROTATION_180 = 2,
60 ROTATION_270 = 3
61 };
62
Jeff Brown9c3cda02010-06-15 01:31:58 -070063 /* Gets information about the display with the specified id.
64 * Returns true if the display info is available, false otherwise.
65 */
66 virtual bool getDisplayInfo(int32_t displayId,
67 int32_t* width, int32_t* height, int32_t* orientation) = 0;
68
Jeff Brown9c3cda02010-06-15 01:31:58 -070069 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
70 * certain device whose screen currently is not all that good.
71 */
72 virtual bool filterTouchEvents() = 0;
73
74 /* Determines whether to turn on some hacks to improve touch interaction with another device
75 * where touch coordinate data can get corrupted.
76 */
77 virtual bool filterJumpyTouchEvents() = 0;
78
Jeff Brown9c3cda02010-06-15 01:31:58 -070079 /* Gets the excluded device names for the platform. */
80 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -080081
82 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
83 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070084};
85
86
87/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -070088class InputReaderInterface : public virtual RefBase {
89protected:
90 InputReaderInterface() { }
91 virtual ~InputReaderInterface() { }
92
93public:
Jeff Brownb88102f2010-09-08 11:49:43 -070094 /* Dumps the state of the input reader.
95 *
96 * This method may be called on any thread (usually by the input manager). */
97 virtual void dump(String8& dump) = 0;
98
Jeff Brown46b9ac02010-04-22 18:58:52 -070099 /* Runs a single iteration of the processing loop.
100 * Nominally reads and processes one incoming message from the EventHub.
101 *
102 * This method should be called on the input reader thread.
103 */
104 virtual void loopOnce() = 0;
105
Jeff Brown9c3cda02010-06-15 01:31:58 -0700106 /* Gets the current input device configuration.
107 *
108 * This method may be called on any thread (usually by the input manager).
109 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700110 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700111
Jeff Brown6d0fec22010-07-23 21:28:06 -0700112 /* Gets information about the specified input device.
113 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
114 * was no such device.
115 *
116 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700117 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700118 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
119
120 /* Gets the list of all registered device ids. */
121 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
122
123 /* Query current input state. */
124 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
125 int32_t scanCode) = 0;
126 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
127 int32_t keyCode) = 0;
128 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
129 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700130
131 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700132 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
133 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
134};
135
136
137/* Internal interface used by individual input devices to access global input device state
138 * and parameters maintained by the input reader.
139 */
140class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700141public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700142 InputReaderContext() { }
143 virtual ~InputReaderContext() { }
144
Jeff Brown6d0fec22010-07-23 21:28:06 -0700145 virtual void updateGlobalMetaState() = 0;
146 virtual int32_t getGlobalMetaState() = 0;
147
148 virtual InputReaderPolicyInterface* getPolicy() = 0;
149 virtual InputDispatcherInterface* getDispatcher() = 0;
150 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700151};
152
Jeff Brown9c3cda02010-06-15 01:31:58 -0700153
Jeff Brown46b9ac02010-04-22 18:58:52 -0700154/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700155 * that it sends to the input dispatcher. Some functions of the input reader, such as early
156 * event filtering in low power states, are controlled by a separate policy object.
157 *
158 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700159 * Because the policy and dispatcher can potentially block or cause re-entrance into
160 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700161 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700162 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700163class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700164public:
165 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700166 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700167 const sp<InputDispatcherInterface>& dispatcher);
168 virtual ~InputReader();
169
Jeff Brownb88102f2010-09-08 11:49:43 -0700170 virtual void dump(String8& dump);
171
Jeff Brown46b9ac02010-04-22 18:58:52 -0700172 virtual void loopOnce();
173
Jeff Brown6d0fec22010-07-23 21:28:06 -0700174 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700175
Jeff Brown6d0fec22010-07-23 21:28:06 -0700176 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
177 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178
Jeff Brown6d0fec22010-07-23 21:28:06 -0700179 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
180 int32_t scanCode);
181 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
182 int32_t keyCode);
183 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
184 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700185
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
187 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188
Jeff Brownc3db8582010-10-20 15:33:38 -0700189protected:
190 // These methods are protected virtual so they can be overridden and instrumented
191 // by test cases.
192 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
193
Jeff Brown46b9ac02010-04-22 18:58:52 -0700194private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700196 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700197 sp<InputDispatcherInterface> mDispatcher;
198
Jeff Brown6d0fec22010-07-23 21:28:06 -0700199 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
200 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
201 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
202
203 // This reader/writer lock guards the list of input devices.
204 // The writer lock must be held whenever the list of input devices is modified
205 // and then promptly released.
206 // The reader lock must be held whenever the list of input devices is traversed or an
207 // input device in the list is accessed.
208 // This lock only protects the registry and prevents inadvertent deletion of device objects
209 // that are in use. Individual devices are responsible for guarding their own internal state
210 // as needed for concurrent operation.
211 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700212 KeyedVector<int32_t, InputDevice*> mDevices;
213
Jeff Brown6d0fec22010-07-23 21:28:06 -0700214 // low-level input event decoding and device management
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700216
Jeff Brown7342bb92010-10-01 18:55:43 -0700217 void addDevice(int32_t deviceId);
218 void removeDevice(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700219 void configureExcludedDevices();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700220
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700222
Jeff Brownc3db8582010-10-20 15:33:38 -0700223 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224
Jeff Brown6d0fec22010-07-23 21:28:06 -0700225 // state management for all devices
226 Mutex mStateLock;
227
228 int32_t mGlobalMetaState;
229 virtual void updateGlobalMetaState();
230 virtual int32_t getGlobalMetaState();
231
232 InputConfiguration mInputConfiguration;
233 void updateInputConfiguration();
234
235 // state queries
236 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
237 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
238 GetStateFunc getStateFunc);
239 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
240 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241};
242
243
244/* Reads raw events from the event hub and processes them, endlessly. */
245class InputReaderThread : public Thread {
246public:
247 InputReaderThread(const sp<InputReaderInterface>& reader);
248 virtual ~InputReaderThread();
249
250private:
251 sp<InputReaderInterface> mReader;
252
253 virtual bool threadLoop();
254};
255
Jeff Brown6d0fec22010-07-23 21:28:06 -0700256
257/* Represents the state of a single input device. */
258class InputDevice {
259public:
260 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
261 ~InputDevice();
262
263 inline InputReaderContext* getContext() { return mContext; }
264 inline int32_t getId() { return mId; }
265 inline const String8& getName() { return mName; }
266 inline uint32_t getSources() { return mSources; }
267
268 inline bool isIgnored() { return mMappers.isEmpty(); }
269
Jeff Brownef3d7e82010-09-30 14:33:04 -0700270 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700271 void addMapper(InputMapper* mapper);
272 void configure();
273 void reset();
274 void process(const RawEvent* rawEvent);
275
276 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
277 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
278 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
279 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
280 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
281 const int32_t* keyCodes, uint8_t* outFlags);
282
283 int32_t getMetaState();
284
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800285 inline const PropertyMap& getConfiguration() {
286 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700287 }
288
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289private:
290 InputReaderContext* mContext;
291 int32_t mId;
292
293 Vector<InputMapper*> mMappers;
294
295 String8 mName;
296 uint32_t mSources;
297
298 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
299 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700300
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800301 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700302};
303
304
305/* An input mapper transforms raw input events into cooked event data.
306 * A single input device can have multiple associated input mappers in order to interpret
307 * different classes of events.
308 */
309class InputMapper {
310public:
311 InputMapper(InputDevice* device);
312 virtual ~InputMapper();
313
314 inline InputDevice* getDevice() { return mDevice; }
315 inline int32_t getDeviceId() { return mDevice->getId(); }
316 inline const String8 getDeviceName() { return mDevice->getName(); }
317 inline InputReaderContext* getContext() { return mContext; }
318 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
319 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
320 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
321
322 virtual uint32_t getSources() = 0;
323 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700324 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325 virtual void configure();
326 virtual void reset();
327 virtual void process(const RawEvent* rawEvent) = 0;
328
329 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
330 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
331 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
332 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
333 const int32_t* keyCodes, uint8_t* outFlags);
334
335 virtual int32_t getMetaState();
336
337protected:
338 InputDevice* mDevice;
339 InputReaderContext* mContext;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700340};
341
342
343class SwitchInputMapper : public InputMapper {
344public:
345 SwitchInputMapper(InputDevice* device);
346 virtual ~SwitchInputMapper();
347
348 virtual uint32_t getSources();
349 virtual void process(const RawEvent* rawEvent);
350
351 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
352
353private:
354 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
355};
356
357
358class KeyboardInputMapper : public InputMapper {
359public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800360 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700361 virtual ~KeyboardInputMapper();
362
363 virtual uint32_t getSources();
364 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700365 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800366 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 virtual void reset();
368 virtual void process(const RawEvent* rawEvent);
369
370 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
371 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
372 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
373 const int32_t* keyCodes, uint8_t* outFlags);
374
375 virtual int32_t getMetaState();
376
377private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700378 Mutex mLock;
379
Jeff Brown6d0fec22010-07-23 21:28:06 -0700380 struct KeyDown {
381 int32_t keyCode;
382 int32_t scanCode;
383 };
384
Jeff Brown6d0fec22010-07-23 21:28:06 -0700385 uint32_t mSources;
386 int32_t mKeyboardType;
387
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800388 // Immutable configuration parameters.
389 struct Parameters {
390 int32_t associatedDisplayId;
391 bool orientationAware;
392 } mParameters;
393
Jeff Brown6328cdc2010-07-29 18:18:33 -0700394 struct LockedState {
395 Vector<KeyDown> keyDowns; // keys that are down
396 int32_t metaState;
397 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700398
399 struct LedState {
400 bool avail; // led is available
401 bool on; // we think the led is currently on
402 };
403 LedState capsLockLedState;
404 LedState numLockLedState;
405 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700406 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407
Jeff Brown6328cdc2010-07-29 18:18:33 -0700408 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800410 void configureParameters();
411 void dumpParameters(String8& dump);
412
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700414
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
416 uint32_t policyFlags);
417
Jeff Brown6328cdc2010-07-29 18:18:33 -0700418 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700419
Jeff Brown49ed71d2010-12-06 17:13:33 -0800420 void resetLedStateLocked();
421 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700422 void updateLedStateLocked(bool reset);
423 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
424 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425};
426
427
Jeff Brown83c09682010-12-23 17:50:18 -0800428class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700429public:
Jeff Brown83c09682010-12-23 17:50:18 -0800430 CursorInputMapper(InputDevice* device);
431 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700432
433 virtual uint32_t getSources();
434 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700435 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800436 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437 virtual void reset();
438 virtual void process(const RawEvent* rawEvent);
439
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700440 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
441
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442private:
443 // Amount that trackball needs to move in order to generate a key event.
444 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
445
Jeff Brown6328cdc2010-07-29 18:18:33 -0700446 Mutex mLock;
447
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800448 // Immutable configuration parameters.
449 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800450 enum Mode {
451 MODE_POINTER,
452 MODE_NAVIGATION,
453 };
454
455 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800456 int32_t associatedDisplayId;
457 bool orientationAware;
458 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459
460 struct Accumulator {
461 enum {
462 FIELD_BTN_MOUSE = 1,
463 FIELD_REL_X = 2,
464 FIELD_REL_Y = 4
465 };
466
467 uint32_t fields;
468
469 bool btnMouse;
470 int32_t relX;
471 int32_t relY;
472
473 inline void clear() {
474 fields = 0;
475 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476 } mAccumulator;
477
Jeff Brown83c09682010-12-23 17:50:18 -0800478 int32_t mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700479 float mXScale;
480 float mYScale;
481 float mXPrecision;
482 float mYPrecision;
Jeff Brown83c09682010-12-23 17:50:18 -0800483 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700484
Jeff Brown6328cdc2010-07-29 18:18:33 -0700485 struct LockedState {
486 bool down;
487 nsecs_t downTime;
488 } mLocked;
489
490 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700491
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800492 void configureParameters();
493 void dumpParameters(String8& dump);
494
Jeff Brown6d0fec22010-07-23 21:28:06 -0700495 void sync(nsecs_t when);
496};
497
498
499class TouchInputMapper : public InputMapper {
500public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800501 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700502 virtual ~TouchInputMapper();
503
504 virtual uint32_t getSources();
505 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700506 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507 virtual void configure();
508 virtual void reset();
509
510 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
511 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
512 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
513 const int32_t* keyCodes, uint8_t* outFlags);
514
515protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700516 Mutex mLock;
517
Jeff Brown6d0fec22010-07-23 21:28:06 -0700518 struct VirtualKey {
519 int32_t keyCode;
520 int32_t scanCode;
521 uint32_t flags;
522
523 // computed hit box, specified in touch screen coords based on known display size
524 int32_t hitLeft;
525 int32_t hitTop;
526 int32_t hitRight;
527 int32_t hitBottom;
528
529 inline bool isHit(int32_t x, int32_t y) const {
530 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
531 }
532 };
533
Jeff Brown8d608662010-08-30 03:02:23 -0700534 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535 struct PointerData {
536 uint32_t id;
537 int32_t x;
538 int32_t y;
539 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700540 int32_t touchMajor;
541 int32_t touchMinor;
542 int32_t toolMajor;
543 int32_t toolMinor;
544 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700545
546 inline bool operator== (const PointerData& other) const {
547 return id == other.id
548 && x == other.x
549 && y == other.y
550 && pressure == other.pressure
551 && touchMajor == other.touchMajor
552 && touchMinor == other.touchMinor
553 && toolMajor == other.toolMajor
554 && toolMinor == other.toolMinor
555 && orientation == other.orientation;
556 }
557 inline bool operator!= (const PointerData& other) const {
558 return !(*this == other);
559 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700560 };
561
Jeff Brown8d608662010-08-30 03:02:23 -0700562 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700563 struct TouchData {
564 uint32_t pointerCount;
565 PointerData pointers[MAX_POINTERS];
566 BitSet32 idBits;
567 uint32_t idToIndex[MAX_POINTER_ID + 1];
568
569 void copyFrom(const TouchData& other) {
570 pointerCount = other.pointerCount;
571 idBits = other.idBits;
572
573 for (uint32_t i = 0; i < pointerCount; i++) {
574 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700575
576 int id = pointers[i].id;
577 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578 }
579 }
580
581 inline void clear() {
582 pointerCount = 0;
583 idBits.clear();
584 }
585 };
586
Jeff Brown83c09682010-12-23 17:50:18 -0800587 // Input sources supported by the device.
588 int32_t mSources;
589
Jeff Brown6d0fec22010-07-23 21:28:06 -0700590 // Immutable configuration parameters.
591 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800592 enum DeviceType {
593 DEVICE_TYPE_TOUCH_SCREEN,
594 DEVICE_TYPE_TOUCH_PAD,
595 };
596
597 DeviceType deviceType;
598 int32_t associatedDisplayId;
599 bool orientationAware;
600
Jeff Brown6d0fec22010-07-23 21:28:06 -0700601 bool useBadTouchFilter;
602 bool useJumpyTouchFilter;
603 bool useAveragingTouchFilter;
604 } mParameters;
605
Jeff Brown8d608662010-08-30 03:02:23 -0700606 // Immutable calibration parameters in parsed form.
607 struct Calibration {
Jeff Brown511ee5f2010-10-18 13:32:20 -0700608 // Position
609 bool haveXOrigin;
610 int32_t xOrigin;
611 bool haveYOrigin;
612 int32_t yOrigin;
613 bool haveXScale;
614 float xScale;
615 bool haveYScale;
616 float yScale;
617
Jeff Brownc6d282b2010-10-14 21:42:15 -0700618 // Touch Size
619 enum TouchSizeCalibration {
620 TOUCH_SIZE_CALIBRATION_DEFAULT,
621 TOUCH_SIZE_CALIBRATION_NONE,
622 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
623 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700624 };
625
Jeff Brownc6d282b2010-10-14 21:42:15 -0700626 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700627
Jeff Brownc6d282b2010-10-14 21:42:15 -0700628 // Tool Size
629 enum ToolSizeCalibration {
630 TOOL_SIZE_CALIBRATION_DEFAULT,
631 TOOL_SIZE_CALIBRATION_NONE,
632 TOOL_SIZE_CALIBRATION_GEOMETRIC,
633 TOOL_SIZE_CALIBRATION_LINEAR,
634 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700635 };
636
Jeff Brownc6d282b2010-10-14 21:42:15 -0700637 ToolSizeCalibration toolSizeCalibration;
638 bool haveToolSizeLinearScale;
639 float toolSizeLinearScale;
640 bool haveToolSizeLinearBias;
641 float toolSizeLinearBias;
642 bool haveToolSizeAreaScale;
643 float toolSizeAreaScale;
644 bool haveToolSizeAreaBias;
645 float toolSizeAreaBias;
646 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800647 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700648
649 // Pressure
650 enum PressureCalibration {
651 PRESSURE_CALIBRATION_DEFAULT,
652 PRESSURE_CALIBRATION_NONE,
653 PRESSURE_CALIBRATION_PHYSICAL,
654 PRESSURE_CALIBRATION_AMPLITUDE,
655 };
656 enum PressureSource {
657 PRESSURE_SOURCE_DEFAULT,
658 PRESSURE_SOURCE_PRESSURE,
659 PRESSURE_SOURCE_TOUCH,
660 };
661
662 PressureCalibration pressureCalibration;
663 PressureSource pressureSource;
664 bool havePressureScale;
665 float pressureScale;
666
667 // Size
668 enum SizeCalibration {
669 SIZE_CALIBRATION_DEFAULT,
670 SIZE_CALIBRATION_NONE,
671 SIZE_CALIBRATION_NORMALIZED,
672 };
673
674 SizeCalibration sizeCalibration;
675
676 // Orientation
677 enum OrientationCalibration {
678 ORIENTATION_CALIBRATION_DEFAULT,
679 ORIENTATION_CALIBRATION_NONE,
680 ORIENTATION_CALIBRATION_INTERPOLATED,
681 };
682
683 OrientationCalibration orientationCalibration;
684 } mCalibration;
685
686 // Raw axis information from the driver.
687 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700688 RawAbsoluteAxisInfo x;
689 RawAbsoluteAxisInfo y;
690 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700691 RawAbsoluteAxisInfo touchMajor;
692 RawAbsoluteAxisInfo touchMinor;
693 RawAbsoluteAxisInfo toolMajor;
694 RawAbsoluteAxisInfo toolMinor;
695 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700696 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700697
Jeff Brown6328cdc2010-07-29 18:18:33 -0700698 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700699 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700700 TouchData mLastTouch;
701
702 // The time the primary pointer last went down.
703 nsecs_t mDownTime;
704
Jeff Brown6328cdc2010-07-29 18:18:33 -0700705 struct LockedState {
706 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700707
Jeff Brown6328cdc2010-07-29 18:18:33 -0700708 // The surface orientation and width and height set by configureSurfaceLocked().
709 int32_t surfaceOrientation;
710 int32_t surfaceWidth, surfaceHeight;
711
712 // Translation and scaling factors, orientation-independent.
713 int32_t xOrigin;
714 float xScale;
715 float xPrecision;
716
717 int32_t yOrigin;
718 float yScale;
719 float yPrecision;
720
Jeff Brown8d608662010-08-30 03:02:23 -0700721 float geometricScale;
722
Jeff Brownc6d282b2010-10-14 21:42:15 -0700723 float toolSizeLinearScale;
724 float toolSizeLinearBias;
725 float toolSizeAreaScale;
726 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700727
Jeff Brown6328cdc2010-07-29 18:18:33 -0700728 float pressureScale;
729
Jeff Brown6328cdc2010-07-29 18:18:33 -0700730 float sizeScale;
731
732 float orientationScale;
733
734 // Oriented motion ranges for input device info.
735 struct OrientedRanges {
736 InputDeviceInfo::MotionRange x;
737 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700738
739 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700740 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700741
742 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700743 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700744
Jeff Brownc6d282b2010-10-14 21:42:15 -0700745 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700746 InputDeviceInfo::MotionRange touchMajor;
747 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700748
Jeff Brownc6d282b2010-10-14 21:42:15 -0700749 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700750 InputDeviceInfo::MotionRange toolMajor;
751 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700752
753 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700754 InputDeviceInfo::MotionRange orientation;
755 } orientedRanges;
756
757 // Oriented dimensions and precision.
758 float orientedSurfaceWidth, orientedSurfaceHeight;
759 float orientedXPrecision, orientedYPrecision;
760
761 struct CurrentVirtualKeyState {
762 bool down;
763 nsecs_t downTime;
764 int32_t keyCode;
765 int32_t scanCode;
766 } currentVirtualKey;
767 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700768
Jeff Brown8d608662010-08-30 03:02:23 -0700769 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700770 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700771 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700772 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700773 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700774 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700775 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700776 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700777 virtual void parseCalibration();
778 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700779 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700780
781 enum TouchResult {
782 // Dispatch the touch normally.
783 DISPATCH_TOUCH,
784 // Do not dispatch the touch, but keep tracking the current stroke.
785 SKIP_TOUCH,
786 // Do not dispatch the touch, and drop all information associated with the current stoke
787 // so the next movement will appear as a new down.
788 DROP_STROKE
789 };
790
791 void syncTouch(nsecs_t when, bool havePointerIds);
792
793private:
794 /* Maximum number of historical samples to average. */
795 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
796
797 /* Slop distance for jumpy pointer detection.
798 * The vertical range of the screen divided by this is our epsilon value. */
799 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
800
801 /* Number of jumpy points to drop for touchscreens that need it. */
802 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
803 static const uint32_t JUMPY_DROP_LIMIT = 3;
804
805 /* Maximum squared distance for averaging.
806 * If moving farther than this, turn of averaging to avoid lag in response. */
807 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
808
809 struct AveragingTouchFilterState {
810 // Individual history tracks are stored by pointer id
811 uint32_t historyStart[MAX_POINTERS];
812 uint32_t historyEnd[MAX_POINTERS];
813 struct {
814 struct {
815 int32_t x;
816 int32_t y;
817 int32_t pressure;
818 } pointers[MAX_POINTERS];
819 } historyData[AVERAGING_HISTORY_SIZE];
820 } mAveragingTouchFilter;
821
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700822 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700823 uint32_t jumpyPointsDropped;
824 } mJumpyTouchFilter;
825
826 struct PointerDistanceHeapElement {
827 uint32_t currentPointerIndex : 8;
828 uint32_t lastPointerIndex : 8;
829 uint64_t distance : 48; // squared distance
830 };
831
Jeff Brown6328cdc2010-07-29 18:18:33 -0700832 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700833
834 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
835 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
836 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700837 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
838 int32_t motionEventAction);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700839
Jeff Brown6328cdc2010-07-29 18:18:33 -0700840 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
841 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700842
843 bool applyBadTouchFilter();
844 bool applyJumpyTouchFilter();
845 void applyAveragingTouchFilter();
846 void calculatePointerIds();
847};
848
849
850class SingleTouchInputMapper : public TouchInputMapper {
851public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800852 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700853 virtual ~SingleTouchInputMapper();
854
855 virtual void reset();
856 virtual void process(const RawEvent* rawEvent);
857
858protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700859 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700860
861private:
862 struct Accumulator {
863 enum {
864 FIELD_BTN_TOUCH = 1,
865 FIELD_ABS_X = 2,
866 FIELD_ABS_Y = 4,
867 FIELD_ABS_PRESSURE = 8,
868 FIELD_ABS_TOOL_WIDTH = 16
869 };
870
871 uint32_t fields;
872
873 bool btnTouch;
874 int32_t absX;
875 int32_t absY;
876 int32_t absPressure;
877 int32_t absToolWidth;
878
879 inline void clear() {
880 fields = 0;
881 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882 } mAccumulator;
883
884 bool mDown;
885 int32_t mX;
886 int32_t mY;
887 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700888 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700889
890 void initialize();
891
892 void sync(nsecs_t when);
893};
894
895
896class MultiTouchInputMapper : public TouchInputMapper {
897public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800898 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899 virtual ~MultiTouchInputMapper();
900
901 virtual void reset();
902 virtual void process(const RawEvent* rawEvent);
903
904protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700905 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906
907private:
908 struct Accumulator {
909 enum {
910 FIELD_ABS_MT_POSITION_X = 1,
911 FIELD_ABS_MT_POSITION_Y = 2,
912 FIELD_ABS_MT_TOUCH_MAJOR = 4,
913 FIELD_ABS_MT_TOUCH_MINOR = 8,
914 FIELD_ABS_MT_WIDTH_MAJOR = 16,
915 FIELD_ABS_MT_WIDTH_MINOR = 32,
916 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700917 FIELD_ABS_MT_TRACKING_ID = 128,
918 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700919 };
920
921 uint32_t pointerCount;
922 struct Pointer {
923 uint32_t fields;
924
925 int32_t absMTPositionX;
926 int32_t absMTPositionY;
927 int32_t absMTTouchMajor;
928 int32_t absMTTouchMinor;
929 int32_t absMTWidthMajor;
930 int32_t absMTWidthMinor;
931 int32_t absMTOrientation;
932 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700933 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700934
935 inline void clear() {
936 fields = 0;
937 }
938 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
939
940 inline void clear() {
941 pointerCount = 0;
942 pointers[0].clear();
943 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944 } mAccumulator;
945
946 void initialize();
947
948 void sync(nsecs_t when);
949};
950
Jeff Brown46b9ac02010-04-22 18:58:52 -0700951} // namespace android
952
953#endif // _UI_INPUT_READER_H