blob: 8c849c4beeb843fa8957c6ed157171325f9584b5 [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,
683 };
684
685 OrientationCalibration orientationCalibration;
686 } mCalibration;
687
688 // Raw axis information from the driver.
689 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700690 RawAbsoluteAxisInfo x;
691 RawAbsoluteAxisInfo y;
692 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700693 RawAbsoluteAxisInfo touchMajor;
694 RawAbsoluteAxisInfo touchMinor;
695 RawAbsoluteAxisInfo toolMajor;
696 RawAbsoluteAxisInfo toolMinor;
697 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700698 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700699
Jeff Brown6328cdc2010-07-29 18:18:33 -0700700 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700701 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700702 TouchData mLastTouch;
703
704 // The time the primary pointer last went down.
705 nsecs_t mDownTime;
706
Jeff Brown6328cdc2010-07-29 18:18:33 -0700707 struct LockedState {
708 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700709
Jeff Brown6328cdc2010-07-29 18:18:33 -0700710 // The surface orientation and width and height set by configureSurfaceLocked().
711 int32_t surfaceOrientation;
712 int32_t surfaceWidth, surfaceHeight;
713
714 // Translation and scaling factors, orientation-independent.
715 int32_t xOrigin;
716 float xScale;
717 float xPrecision;
718
719 int32_t yOrigin;
720 float yScale;
721 float yPrecision;
722
Jeff Brown8d608662010-08-30 03:02:23 -0700723 float geometricScale;
724
Jeff Brownc6d282b2010-10-14 21:42:15 -0700725 float toolSizeLinearScale;
726 float toolSizeLinearBias;
727 float toolSizeAreaScale;
728 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700729
Jeff Brown6328cdc2010-07-29 18:18:33 -0700730 float pressureScale;
731
Jeff Brown6328cdc2010-07-29 18:18:33 -0700732 float sizeScale;
733
734 float orientationScale;
735
736 // Oriented motion ranges for input device info.
737 struct OrientedRanges {
738 InputDeviceInfo::MotionRange x;
739 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700740
741 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700742 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700743
744 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700745 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700746
Jeff Brownc6d282b2010-10-14 21:42:15 -0700747 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700748 InputDeviceInfo::MotionRange touchMajor;
749 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700750
Jeff Brownc6d282b2010-10-14 21:42:15 -0700751 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700752 InputDeviceInfo::MotionRange toolMajor;
753 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700754
755 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700756 InputDeviceInfo::MotionRange orientation;
757 } orientedRanges;
758
759 // Oriented dimensions and precision.
760 float orientedSurfaceWidth, orientedSurfaceHeight;
761 float orientedXPrecision, orientedYPrecision;
762
763 struct CurrentVirtualKeyState {
764 bool down;
765 nsecs_t downTime;
766 int32_t keyCode;
767 int32_t scanCode;
768 } currentVirtualKey;
769 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700770
Jeff Brown8d608662010-08-30 03:02:23 -0700771 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700772 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700773 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700774 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700775 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700776 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700777 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700778 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700779 virtual void parseCalibration();
780 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700781 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700782
783 enum TouchResult {
784 // Dispatch the touch normally.
785 DISPATCH_TOUCH,
786 // Do not dispatch the touch, but keep tracking the current stroke.
787 SKIP_TOUCH,
788 // Do not dispatch the touch, and drop all information associated with the current stoke
789 // so the next movement will appear as a new down.
790 DROP_STROKE
791 };
792
793 void syncTouch(nsecs_t when, bool havePointerIds);
794
795private:
796 /* Maximum number of historical samples to average. */
797 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
798
799 /* Slop distance for jumpy pointer detection.
800 * The vertical range of the screen divided by this is our epsilon value. */
801 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
802
803 /* Number of jumpy points to drop for touchscreens that need it. */
804 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
805 static const uint32_t JUMPY_DROP_LIMIT = 3;
806
807 /* Maximum squared distance for averaging.
808 * If moving farther than this, turn of averaging to avoid lag in response. */
809 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
810
811 struct AveragingTouchFilterState {
812 // Individual history tracks are stored by pointer id
813 uint32_t historyStart[MAX_POINTERS];
814 uint32_t historyEnd[MAX_POINTERS];
815 struct {
816 struct {
817 int32_t x;
818 int32_t y;
819 int32_t pressure;
820 } pointers[MAX_POINTERS];
821 } historyData[AVERAGING_HISTORY_SIZE];
822 } mAveragingTouchFilter;
823
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700824 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700825 uint32_t jumpyPointsDropped;
826 } mJumpyTouchFilter;
827
828 struct PointerDistanceHeapElement {
829 uint32_t currentPointerIndex : 8;
830 uint32_t lastPointerIndex : 8;
831 uint64_t distance : 48; // squared distance
832 };
833
Jeff Brown6328cdc2010-07-29 18:18:33 -0700834 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700835
836 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
837 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
838 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700839 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
840 int32_t motionEventAction);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700841
Jeff Brown6328cdc2010-07-29 18:18:33 -0700842 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
843 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700844
845 bool applyBadTouchFilter();
846 bool applyJumpyTouchFilter();
847 void applyAveragingTouchFilter();
848 void calculatePointerIds();
849};
850
851
852class SingleTouchInputMapper : public TouchInputMapper {
853public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800854 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700855 virtual ~SingleTouchInputMapper();
856
857 virtual void reset();
858 virtual void process(const RawEvent* rawEvent);
859
860protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700861 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700862
863private:
864 struct Accumulator {
865 enum {
866 FIELD_BTN_TOUCH = 1,
867 FIELD_ABS_X = 2,
868 FIELD_ABS_Y = 4,
869 FIELD_ABS_PRESSURE = 8,
870 FIELD_ABS_TOOL_WIDTH = 16
871 };
872
873 uint32_t fields;
874
875 bool btnTouch;
876 int32_t absX;
877 int32_t absY;
878 int32_t absPressure;
879 int32_t absToolWidth;
880
881 inline void clear() {
882 fields = 0;
883 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700884 } mAccumulator;
885
886 bool mDown;
887 int32_t mX;
888 int32_t mY;
889 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700890 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700891
892 void initialize();
893
894 void sync(nsecs_t when);
895};
896
897
898class MultiTouchInputMapper : public TouchInputMapper {
899public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800900 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700901 virtual ~MultiTouchInputMapper();
902
903 virtual void reset();
904 virtual void process(const RawEvent* rawEvent);
905
906protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700907 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700908
909private:
910 struct Accumulator {
911 enum {
912 FIELD_ABS_MT_POSITION_X = 1,
913 FIELD_ABS_MT_POSITION_Y = 2,
914 FIELD_ABS_MT_TOUCH_MAJOR = 4,
915 FIELD_ABS_MT_TOUCH_MINOR = 8,
916 FIELD_ABS_MT_WIDTH_MAJOR = 16,
917 FIELD_ABS_MT_WIDTH_MINOR = 32,
918 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700919 FIELD_ABS_MT_TRACKING_ID = 128,
920 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 };
922
923 uint32_t pointerCount;
924 struct Pointer {
925 uint32_t fields;
926
927 int32_t absMTPositionX;
928 int32_t absMTPositionY;
929 int32_t absMTTouchMajor;
930 int32_t absMTTouchMinor;
931 int32_t absMTWidthMajor;
932 int32_t absMTWidthMinor;
933 int32_t absMTOrientation;
934 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700935 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936
937 inline void clear() {
938 fields = 0;
939 }
940 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
941
942 inline void clear() {
943 pointerCount = 0;
944 pointers[0].clear();
945 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700946 } mAccumulator;
947
948 void initialize();
949
950 void sync(nsecs_t when);
951};
952
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700953} // namespace android
954
955#endif // _UI_INPUT_READER_H