blob: 8b2d40a096d23a68334718f2a61a6acef5d712f0 [file] [log] [blame]
Jeff Brown46b9ac0a2010-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
Jeff Brownb4ff35d2011-01-02 16:37:43 -080020#include "EventHub.h"
21#include "InputDispatcher.h"
22#include "PointerController.h"
23
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31#include <utils/BitSet.h>
32
33#include <stddef.h>
34#include <unistd.h>
35
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
43 * Input reader policy interface.
44 *
45 * The input reader policy is used by the input reader to interact with the Window Manager
46 * and other system components.
47 *
48 * The actual implementation is partially supported by callbacks into the DVM
49 * via JNI. This interface is also mocked in the unit tests.
50 */
51class InputReaderPolicyInterface : public virtual RefBase {
52protected:
53 InputReaderPolicyInterface() { }
54 virtual ~InputReaderPolicyInterface() { }
55
56public:
57 /* Display orientations. */
58 enum {
59 ROTATION_0 = 0,
60 ROTATION_90 = 1,
61 ROTATION_180 = 2,
62 ROTATION_270 = 3
63 };
64
Jeff Brown9c3cda02010-06-15 01:31:58 -070065 /* Gets information about the display with the specified id.
66 * Returns true if the display info is available, false otherwise.
67 */
68 virtual bool getDisplayInfo(int32_t displayId,
69 int32_t* width, int32_t* height, int32_t* orientation) = 0;
70
Jeff Brown9c3cda02010-06-15 01:31:58 -070071 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
72 * certain device whose screen currently is not all that good.
73 */
74 virtual bool filterTouchEvents() = 0;
75
76 /* Determines whether to turn on some hacks to improve touch interaction with another device
77 * where touch coordinate data can get corrupted.
78 */
79 virtual bool filterJumpyTouchEvents() = 0;
80
Jeff Brown9c3cda02010-06-15 01:31:58 -070081 /* Gets the excluded device names for the platform. */
82 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -080083
84 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
85 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070086};
87
88
89/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070090class InputReaderInterface : public virtual RefBase {
91protected:
92 InputReaderInterface() { }
93 virtual ~InputReaderInterface() { }
94
95public:
Jeff Brownb88102f2010-09-08 11:49:43 -070096 /* Dumps the state of the input reader.
97 *
98 * This method may be called on any thread (usually by the input manager). */
99 virtual void dump(String8& dump) = 0;
100
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700101 /* Runs a single iteration of the processing loop.
102 * Nominally reads and processes one incoming message from the EventHub.
103 *
104 * This method should be called on the input reader thread.
105 */
106 virtual void loopOnce() = 0;
107
Jeff Brown9c3cda02010-06-15 01:31:58 -0700108 /* Gets the current input device configuration.
109 *
110 * This method may be called on any thread (usually by the input manager).
111 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700112 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700113
Jeff Brown6d0fec22010-07-23 21:28:06 -0700114 /* Gets information about the specified input device.
115 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
116 * was no such device.
117 *
118 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700119 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700120 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
121
122 /* Gets the list of all registered device ids. */
123 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
124
125 /* Query current input state. */
126 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
127 int32_t scanCode) = 0;
128 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
129 int32_t keyCode) = 0;
130 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
131 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700132
133 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700134 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
135 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
136};
137
138
139/* Internal interface used by individual input devices to access global input device state
140 * and parameters maintained by the input reader.
141 */
142class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700143public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700144 InputReaderContext() { }
145 virtual ~InputReaderContext() { }
146
Jeff Brown6d0fec22010-07-23 21:28:06 -0700147 virtual void updateGlobalMetaState() = 0;
148 virtual int32_t getGlobalMetaState() = 0;
149
150 virtual InputReaderPolicyInterface* getPolicy() = 0;
151 virtual InputDispatcherInterface* getDispatcher() = 0;
152 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700153};
154
Jeff Brown9c3cda02010-06-15 01:31:58 -0700155
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700156/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700157 * that it sends to the input dispatcher. Some functions of the input reader, such as early
158 * event filtering in low power states, are controlled by a separate policy object.
159 *
160 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700161 * Because the policy and dispatcher can potentially block or cause re-entrance into
162 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700163 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700164 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700165class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700166public:
167 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700168 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700169 const sp<InputDispatcherInterface>& dispatcher);
170 virtual ~InputReader();
171
Jeff Brownb88102f2010-09-08 11:49:43 -0700172 virtual void dump(String8& dump);
173
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700174 virtual void loopOnce();
175
Jeff Brown6d0fec22010-07-23 21:28:06 -0700176 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700177
Jeff Brown6d0fec22010-07-23 21:28:06 -0700178 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
179 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700180
Jeff Brown6d0fec22010-07-23 21:28:06 -0700181 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
182 int32_t scanCode);
183 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
184 int32_t keyCode);
185 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
186 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700187
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
189 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190
Jeff Brownc3db8582010-10-20 15:33:38 -0700191protected:
192 // These methods are protected virtual so they can be overridden and instrumented
193 // by test cases.
194 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
195
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700196private:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700197 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700199 sp<InputDispatcherInterface> mDispatcher;
200
Jeff Brown6d0fec22010-07-23 21:28:06 -0700201 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
202 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
203 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
204
205 // This reader/writer lock guards the list of input devices.
206 // The writer lock must be held whenever the list of input devices is modified
207 // and then promptly released.
208 // The reader lock must be held whenever the list of input devices is traversed or an
209 // input device in the list is accessed.
210 // This lock only protects the registry and prevents inadvertent deletion of device objects
211 // that are in use. Individual devices are responsible for guarding their own internal state
212 // as needed for concurrent operation.
213 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700214 KeyedVector<int32_t, InputDevice*> mDevices;
215
Jeff Brown6d0fec22010-07-23 21:28:06 -0700216 // low-level input event decoding and device management
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700217 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700218
Jeff Brown7342bb92010-10-01 18:55:43 -0700219 void addDevice(int32_t deviceId);
220 void removeDevice(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700221 void configureExcludedDevices();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222
Jeff Brown6d0fec22010-07-23 21:28:06 -0700223 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224
Jeff Brownc3db8582010-10-20 15:33:38 -0700225 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700226
Jeff Brown6d0fec22010-07-23 21:28:06 -0700227 // state management for all devices
228 Mutex mStateLock;
229
230 int32_t mGlobalMetaState;
231 virtual void updateGlobalMetaState();
232 virtual int32_t getGlobalMetaState();
233
234 InputConfiguration mInputConfiguration;
235 void updateInputConfiguration();
236
237 // state queries
238 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
239 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
240 GetStateFunc getStateFunc);
241 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
242 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700243};
244
245
246/* Reads raw events from the event hub and processes them, endlessly. */
247class InputReaderThread : public Thread {
248public:
249 InputReaderThread(const sp<InputReaderInterface>& reader);
250 virtual ~InputReaderThread();
251
252private:
253 sp<InputReaderInterface> mReader;
254
255 virtual bool threadLoop();
256};
257
Jeff Brown6d0fec22010-07-23 21:28:06 -0700258
259/* Represents the state of a single input device. */
260class InputDevice {
261public:
262 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
263 ~InputDevice();
264
265 inline InputReaderContext* getContext() { return mContext; }
266 inline int32_t getId() { return mId; }
267 inline const String8& getName() { return mName; }
268 inline uint32_t getSources() { return mSources; }
269
270 inline bool isIgnored() { return mMappers.isEmpty(); }
271
Jeff Brownef3d7e82010-09-30 14:33:04 -0700272 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700273 void addMapper(InputMapper* mapper);
274 void configure();
275 void reset();
276 void process(const RawEvent* rawEvent);
277
278 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
279 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
280 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
281 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
282 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
283 const int32_t* keyCodes, uint8_t* outFlags);
284
285 int32_t getMetaState();
286
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800287 inline const PropertyMap& getConfiguration() {
288 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700289 }
290
Jeff Brown6d0fec22010-07-23 21:28:06 -0700291private:
292 InputReaderContext* mContext;
293 int32_t mId;
294
295 Vector<InputMapper*> mMappers;
296
297 String8 mName;
298 uint32_t mSources;
299
300 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
301 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700302
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800303 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700304};
305
306
307/* An input mapper transforms raw input events into cooked event data.
308 * A single input device can have multiple associated input mappers in order to interpret
309 * different classes of events.
310 */
311class InputMapper {
312public:
313 InputMapper(InputDevice* device);
314 virtual ~InputMapper();
315
316 inline InputDevice* getDevice() { return mDevice; }
317 inline int32_t getDeviceId() { return mDevice->getId(); }
318 inline const String8 getDeviceName() { return mDevice->getName(); }
319 inline InputReaderContext* getContext() { return mContext; }
320 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
321 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
322 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
323
324 virtual uint32_t getSources() = 0;
325 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700326 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700327 virtual void configure();
328 virtual void reset();
329 virtual void process(const RawEvent* rawEvent) = 0;
330
331 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
332 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
333 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
334 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
335 const int32_t* keyCodes, uint8_t* outFlags);
336
337 virtual int32_t getMetaState();
338
339protected:
340 InputDevice* mDevice;
341 InputReaderContext* mContext;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700342};
343
344
345class SwitchInputMapper : public InputMapper {
346public:
347 SwitchInputMapper(InputDevice* device);
348 virtual ~SwitchInputMapper();
349
350 virtual uint32_t getSources();
351 virtual void process(const RawEvent* rawEvent);
352
353 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
354
355private:
356 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
357};
358
359
360class KeyboardInputMapper : public InputMapper {
361public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800362 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700363 virtual ~KeyboardInputMapper();
364
365 virtual uint32_t getSources();
366 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700367 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800368 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 virtual void reset();
370 virtual void process(const RawEvent* rawEvent);
371
372 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
373 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
374 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
375 const int32_t* keyCodes, uint8_t* outFlags);
376
377 virtual int32_t getMetaState();
378
379private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700380 Mutex mLock;
381
Jeff Brown6d0fec22010-07-23 21:28:06 -0700382 struct KeyDown {
383 int32_t keyCode;
384 int32_t scanCode;
385 };
386
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387 uint32_t mSources;
388 int32_t mKeyboardType;
389
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800390 // Immutable configuration parameters.
391 struct Parameters {
392 int32_t associatedDisplayId;
393 bool orientationAware;
394 } mParameters;
395
Jeff Brown6328cdc2010-07-29 18:18:33 -0700396 struct LockedState {
397 Vector<KeyDown> keyDowns; // keys that are down
398 int32_t metaState;
399 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700400
401 struct LedState {
402 bool avail; // led is available
403 bool on; // we think the led is currently on
404 };
405 LedState capsLockLedState;
406 LedState numLockLedState;
407 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700408 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409
Jeff Brown6328cdc2010-07-29 18:18:33 -0700410 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800412 void configureParameters();
413 void dumpParameters(String8& dump);
414
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700416
Jeff Brown6d0fec22010-07-23 21:28:06 -0700417 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
418 uint32_t policyFlags);
419
Jeff Brown6328cdc2010-07-29 18:18:33 -0700420 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700421
Jeff Brown49ed71d2010-12-06 17:13:33 -0800422 void resetLedStateLocked();
423 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700424 void updateLedStateLocked(bool reset);
425 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
426 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427};
428
429
Jeff Brown83c09682010-12-23 17:50:18 -0800430class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431public:
Jeff Brown83c09682010-12-23 17:50:18 -0800432 CursorInputMapper(InputDevice* device);
433 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434
435 virtual uint32_t getSources();
436 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700437 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800438 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 virtual void reset();
440 virtual void process(const RawEvent* rawEvent);
441
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700442 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
443
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444private:
445 // Amount that trackball needs to move in order to generate a key event.
446 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
447
Jeff Brown6328cdc2010-07-29 18:18:33 -0700448 Mutex mLock;
449
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800450 // Immutable configuration parameters.
451 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800452 enum Mode {
453 MODE_POINTER,
454 MODE_NAVIGATION,
455 };
456
457 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800458 int32_t associatedDisplayId;
459 bool orientationAware;
460 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461
462 struct Accumulator {
463 enum {
464 FIELD_BTN_MOUSE = 1,
465 FIELD_REL_X = 2,
466 FIELD_REL_Y = 4
467 };
468
469 uint32_t fields;
470
471 bool btnMouse;
472 int32_t relX;
473 int32_t relY;
474
475 inline void clear() {
476 fields = 0;
477 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700478 } mAccumulator;
479
Jeff Brown83c09682010-12-23 17:50:18 -0800480 int32_t mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481 float mXScale;
482 float mYScale;
483 float mXPrecision;
484 float mYPrecision;
Jeff Brown83c09682010-12-23 17:50:18 -0800485 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486
Jeff Brown6328cdc2010-07-29 18:18:33 -0700487 struct LockedState {
488 bool down;
489 nsecs_t downTime;
490 } mLocked;
491
492 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800494 void configureParameters();
495 void dumpParameters(String8& dump);
496
Jeff Brown6d0fec22010-07-23 21:28:06 -0700497 void sync(nsecs_t when);
498};
499
500
501class TouchInputMapper : public InputMapper {
502public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800503 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504 virtual ~TouchInputMapper();
505
506 virtual uint32_t getSources();
507 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700508 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-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 Brown6328cdc2010-07-29 18:18:33 -0700518 Mutex mLock;
519
Jeff Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700536 // Raw data for a single pointer.
Jeff Brown6d0fec22010-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 Brown6d0fec22010-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 Brownc3db8582010-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 Brown6d0fec22010-07-23 21:28:06 -0700562 };
563
Jeff Brown8d608662010-08-30 03:02:23 -0700564 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-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 Brown9e2ad362010-07-30 19:20:11 -0700577
578 int id = pointers[i].id;
579 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580 }
581 }
582
583 inline void clear() {
584 pointerCount = 0;
585 idBits.clear();
586 }
587 };
588
Jeff Brown83c09682010-12-23 17:50:18 -0800589 // Input sources supported by the device.
590 int32_t mSources;
591
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592 // Immutable configuration parameters.
593 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800594 enum DeviceType {
595 DEVICE_TYPE_TOUCH_SCREEN,
596 DEVICE_TYPE_TOUCH_PAD,
597 };
598
599 DeviceType deviceType;
600 int32_t associatedDisplayId;
601 bool orientationAware;
602
Jeff Brown6d0fec22010-07-23 21:28:06 -0700603 bool useBadTouchFilter;
604 bool useJumpyTouchFilter;
605 bool useAveragingTouchFilter;
606 } mParameters;
607
Jeff Brown8d608662010-08-30 03:02:23 -0700608 // Immutable calibration parameters in parsed form.
609 struct Calibration {
Jeff Brown511ee5f2010-10-18 13:32:20 -0700610 // Position
611 bool haveXOrigin;
612 int32_t xOrigin;
613 bool haveYOrigin;
614 int32_t yOrigin;
615 bool haveXScale;
616 float xScale;
617 bool haveYScale;
618 float yScale;
619
Jeff Brownc6d282b2010-10-14 21:42:15 -0700620 // Touch Size
621 enum TouchSizeCalibration {
622 TOUCH_SIZE_CALIBRATION_DEFAULT,
623 TOUCH_SIZE_CALIBRATION_NONE,
624 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
625 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700626 };
627
Jeff Brownc6d282b2010-10-14 21:42:15 -0700628 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700629
Jeff Brownc6d282b2010-10-14 21:42:15 -0700630 // Tool Size
631 enum ToolSizeCalibration {
632 TOOL_SIZE_CALIBRATION_DEFAULT,
633 TOOL_SIZE_CALIBRATION_NONE,
634 TOOL_SIZE_CALIBRATION_GEOMETRIC,
635 TOOL_SIZE_CALIBRATION_LINEAR,
636 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700637 };
638
Jeff Brownc6d282b2010-10-14 21:42:15 -0700639 ToolSizeCalibration toolSizeCalibration;
640 bool haveToolSizeLinearScale;
641 float toolSizeLinearScale;
642 bool haveToolSizeLinearBias;
643 float toolSizeLinearBias;
644 bool haveToolSizeAreaScale;
645 float toolSizeAreaScale;
646 bool haveToolSizeAreaBias;
647 float toolSizeAreaBias;
648 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800649 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700650
651 // Pressure
652 enum PressureCalibration {
653 PRESSURE_CALIBRATION_DEFAULT,
654 PRESSURE_CALIBRATION_NONE,
655 PRESSURE_CALIBRATION_PHYSICAL,
656 PRESSURE_CALIBRATION_AMPLITUDE,
657 };
658 enum PressureSource {
659 PRESSURE_SOURCE_DEFAULT,
660 PRESSURE_SOURCE_PRESSURE,
661 PRESSURE_SOURCE_TOUCH,
662 };
663
664 PressureCalibration pressureCalibration;
665 PressureSource pressureSource;
666 bool havePressureScale;
667 float pressureScale;
668
669 // Size
670 enum SizeCalibration {
671 SIZE_CALIBRATION_DEFAULT,
672 SIZE_CALIBRATION_NONE,
673 SIZE_CALIBRATION_NORMALIZED,
674 };
675
676 SizeCalibration sizeCalibration;
677
678 // Orientation
679 enum OrientationCalibration {
680 ORIENTATION_CALIBRATION_DEFAULT,
681 ORIENTATION_CALIBRATION_NONE,
682 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800683 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700684 };
685
686 OrientationCalibration orientationCalibration;
687 } mCalibration;
688
689 // Raw axis information from the driver.
690 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700691 RawAbsoluteAxisInfo x;
692 RawAbsoluteAxisInfo y;
693 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700694 RawAbsoluteAxisInfo touchMajor;
695 RawAbsoluteAxisInfo touchMinor;
696 RawAbsoluteAxisInfo toolMajor;
697 RawAbsoluteAxisInfo toolMinor;
698 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700699 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700700
Jeff Brown6328cdc2010-07-29 18:18:33 -0700701 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700702 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700703 TouchData mLastTouch;
704
705 // The time the primary pointer last went down.
706 nsecs_t mDownTime;
707
Jeff Brown6328cdc2010-07-29 18:18:33 -0700708 struct LockedState {
709 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700710
Jeff Brown6328cdc2010-07-29 18:18:33 -0700711 // The surface orientation and width and height set by configureSurfaceLocked().
712 int32_t surfaceOrientation;
713 int32_t surfaceWidth, surfaceHeight;
714
715 // Translation and scaling factors, orientation-independent.
716 int32_t xOrigin;
717 float xScale;
718 float xPrecision;
719
720 int32_t yOrigin;
721 float yScale;
722 float yPrecision;
723
Jeff Brown8d608662010-08-30 03:02:23 -0700724 float geometricScale;
725
Jeff Brownc6d282b2010-10-14 21:42:15 -0700726 float toolSizeLinearScale;
727 float toolSizeLinearBias;
728 float toolSizeAreaScale;
729 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700730
Jeff Brown6328cdc2010-07-29 18:18:33 -0700731 float pressureScale;
732
Jeff Brown6328cdc2010-07-29 18:18:33 -0700733 float sizeScale;
734
735 float orientationScale;
736
737 // Oriented motion ranges for input device info.
738 struct OrientedRanges {
739 InputDeviceInfo::MotionRange x;
740 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700741
742 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700743 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700744
745 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700746 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700747
Jeff Brownc6d282b2010-10-14 21:42:15 -0700748 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700749 InputDeviceInfo::MotionRange touchMajor;
750 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700751
Jeff Brownc6d282b2010-10-14 21:42:15 -0700752 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700753 InputDeviceInfo::MotionRange toolMajor;
754 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700755
756 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700757 InputDeviceInfo::MotionRange orientation;
758 } orientedRanges;
759
760 // Oriented dimensions and precision.
761 float orientedSurfaceWidth, orientedSurfaceHeight;
762 float orientedXPrecision, orientedYPrecision;
763
764 struct CurrentVirtualKeyState {
765 bool down;
766 nsecs_t downTime;
767 int32_t keyCode;
768 int32_t scanCode;
769 } currentVirtualKey;
770 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700771
Jeff Brown8d608662010-08-30 03:02:23 -0700772 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700773 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700774 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700775 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700776 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700777 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700778 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700779 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700780 virtual void parseCalibration();
781 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700782 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700783
784 enum TouchResult {
785 // Dispatch the touch normally.
786 DISPATCH_TOUCH,
787 // Do not dispatch the touch, but keep tracking the current stroke.
788 SKIP_TOUCH,
789 // Do not dispatch the touch, and drop all information associated with the current stoke
790 // so the next movement will appear as a new down.
791 DROP_STROKE
792 };
793
794 void syncTouch(nsecs_t when, bool havePointerIds);
795
796private:
797 /* Maximum number of historical samples to average. */
798 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
799
800 /* Slop distance for jumpy pointer detection.
801 * The vertical range of the screen divided by this is our epsilon value. */
802 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
803
804 /* Number of jumpy points to drop for touchscreens that need it. */
805 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
806 static const uint32_t JUMPY_DROP_LIMIT = 3;
807
808 /* Maximum squared distance for averaging.
809 * If moving farther than this, turn of averaging to avoid lag in response. */
810 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
811
812 struct AveragingTouchFilterState {
813 // Individual history tracks are stored by pointer id
814 uint32_t historyStart[MAX_POINTERS];
815 uint32_t historyEnd[MAX_POINTERS];
816 struct {
817 struct {
818 int32_t x;
819 int32_t y;
820 int32_t pressure;
821 } pointers[MAX_POINTERS];
822 } historyData[AVERAGING_HISTORY_SIZE];
823 } mAveragingTouchFilter;
824
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700825 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700826 uint32_t jumpyPointsDropped;
827 } mJumpyTouchFilter;
828
829 struct PointerDistanceHeapElement {
830 uint32_t currentPointerIndex : 8;
831 uint32_t lastPointerIndex : 8;
832 uint64_t distance : 48; // squared distance
833 };
834
Jeff Brown6328cdc2010-07-29 18:18:33 -0700835 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700836
837 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
838 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
839 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700840 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
841 int32_t motionEventAction);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700842
Jeff Brown6328cdc2010-07-29 18:18:33 -0700843 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
844 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700845
846 bool applyBadTouchFilter();
847 bool applyJumpyTouchFilter();
848 void applyAveragingTouchFilter();
849 void calculatePointerIds();
850};
851
852
853class SingleTouchInputMapper : public TouchInputMapper {
854public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800855 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700856 virtual ~SingleTouchInputMapper();
857
858 virtual void reset();
859 virtual void process(const RawEvent* rawEvent);
860
861protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700862 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700863
864private:
865 struct Accumulator {
866 enum {
867 FIELD_BTN_TOUCH = 1,
868 FIELD_ABS_X = 2,
869 FIELD_ABS_Y = 4,
870 FIELD_ABS_PRESSURE = 8,
871 FIELD_ABS_TOOL_WIDTH = 16
872 };
873
874 uint32_t fields;
875
876 bool btnTouch;
877 int32_t absX;
878 int32_t absY;
879 int32_t absPressure;
880 int32_t absToolWidth;
881
882 inline void clear() {
883 fields = 0;
884 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700885 } mAccumulator;
886
887 bool mDown;
888 int32_t mX;
889 int32_t mY;
890 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700891 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700892
893 void initialize();
894
895 void sync(nsecs_t when);
896};
897
898
899class MultiTouchInputMapper : public TouchInputMapper {
900public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800901 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700902 virtual ~MultiTouchInputMapper();
903
904 virtual void reset();
905 virtual void process(const RawEvent* rawEvent);
906
907protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700908 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700909
910private:
911 struct Accumulator {
912 enum {
913 FIELD_ABS_MT_POSITION_X = 1,
914 FIELD_ABS_MT_POSITION_Y = 2,
915 FIELD_ABS_MT_TOUCH_MAJOR = 4,
916 FIELD_ABS_MT_TOUCH_MINOR = 8,
917 FIELD_ABS_MT_WIDTH_MAJOR = 16,
918 FIELD_ABS_MT_WIDTH_MINOR = 32,
919 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700920 FIELD_ABS_MT_TRACKING_ID = 128,
921 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700922 };
923
924 uint32_t pointerCount;
925 struct Pointer {
926 uint32_t fields;
927
928 int32_t absMTPositionX;
929 int32_t absMTPositionY;
930 int32_t absMTTouchMajor;
931 int32_t absMTTouchMinor;
932 int32_t absMTWidthMajor;
933 int32_t absMTWidthMinor;
934 int32_t absMTOrientation;
935 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700936 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700937
938 inline void clear() {
939 fields = 0;
940 }
941 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
942
943 inline void clear() {
944 pointerCount = 0;
945 pointers[0].clear();
946 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700947 } mAccumulator;
948
949 void initialize();
950
951 void sync(nsecs_t when);
952};
953
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700954} // namespace android
955
956#endif // _UI_INPUT_READER_H