blob: 7619682a01163f4a6276cc2b173c4150c5b9841b [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 Brownfe508922011-01-18 15:10:10 -080081 /* Gets the amount of time to disable virtual keys after the screen is touched
82 * in order to filter out accidental virtual key presses due to swiping gestures
83 * or taps near the edge of the display. May be 0 to disable the feature.
84 */
85 virtual nsecs_t getVirtualKeyQuietTime() = 0;
86
Jeff Brown9c3cda02010-06-15 01:31:58 -070087 /* Gets the excluded device names for the platform. */
88 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -080089
90 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
91 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070092};
93
94
95/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070096class InputReaderInterface : public virtual RefBase {
97protected:
98 InputReaderInterface() { }
99 virtual ~InputReaderInterface() { }
100
101public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700102 /* Dumps the state of the input reader.
103 *
104 * This method may be called on any thread (usually by the input manager). */
105 virtual void dump(String8& dump) = 0;
106
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700107 /* Runs a single iteration of the processing loop.
108 * Nominally reads and processes one incoming message from the EventHub.
109 *
110 * This method should be called on the input reader thread.
111 */
112 virtual void loopOnce() = 0;
113
Jeff Brown9c3cda02010-06-15 01:31:58 -0700114 /* Gets the current input device configuration.
115 *
116 * This method may be called on any thread (usually by the input manager).
117 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700118 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700119
Jeff Brown6d0fec22010-07-23 21:28:06 -0700120 /* Gets information about the specified input device.
121 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
122 * was no such device.
123 *
124 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700125 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700126 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
127
128 /* Gets the list of all registered device ids. */
129 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
130
131 /* Query current input state. */
132 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
133 int32_t scanCode) = 0;
134 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
135 int32_t keyCode) = 0;
136 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
137 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700138
139 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
142};
143
144
145/* Internal interface used by individual input devices to access global input device state
146 * and parameters maintained by the input reader.
147 */
148class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700149public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150 InputReaderContext() { }
151 virtual ~InputReaderContext() { }
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153 virtual void updateGlobalMetaState() = 0;
154 virtual int32_t getGlobalMetaState() = 0;
155
Jeff Brownfe508922011-01-18 15:10:10 -0800156 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
157 virtual bool shouldDropVirtualKey(nsecs_t now,
158 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
159
Jeff Brown6d0fec22010-07-23 21:28:06 -0700160 virtual InputReaderPolicyInterface* getPolicy() = 0;
161 virtual InputDispatcherInterface* getDispatcher() = 0;
162 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700163};
164
Jeff Brown9c3cda02010-06-15 01:31:58 -0700165
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700166/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700167 * that it sends to the input dispatcher. Some functions of the input reader, such as early
168 * event filtering in low power states, are controlled by a separate policy object.
169 *
170 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700171 * Because the policy and dispatcher can potentially block or cause re-entrance into
172 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700173 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700174 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700175class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700176public:
177 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700179 const sp<InputDispatcherInterface>& dispatcher);
180 virtual ~InputReader();
181
Jeff Brownb88102f2010-09-08 11:49:43 -0700182 virtual void dump(String8& dump);
183
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700184 virtual void loopOnce();
185
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700187
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
189 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190
Jeff Brown6d0fec22010-07-23 21:28:06 -0700191 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
192 int32_t scanCode);
193 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
194 int32_t keyCode);
195 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
196 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700197
Jeff Brown6d0fec22010-07-23 21:28:06 -0700198 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
199 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200
Jeff Brownc3db8582010-10-20 15:33:38 -0700201protected:
202 // These methods are protected virtual so they can be overridden and instrumented
203 // by test cases.
204 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
205
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700206private:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700207 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700208 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700209 sp<InputDispatcherInterface> mDispatcher;
210
Jeff Brown6d0fec22010-07-23 21:28:06 -0700211 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
212 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
213 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
214
215 // This reader/writer lock guards the list of input devices.
216 // The writer lock must be held whenever the list of input devices is modified
217 // and then promptly released.
218 // The reader lock must be held whenever the list of input devices is traversed or an
219 // input device in the list is accessed.
220 // This lock only protects the registry and prevents inadvertent deletion of device objects
221 // that are in use. Individual devices are responsible for guarding their own internal state
222 // as needed for concurrent operation.
223 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224 KeyedVector<int32_t, InputDevice*> mDevices;
225
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 // low-level input event decoding and device management
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700227 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700228
Jeff Brown7342bb92010-10-01 18:55:43 -0700229 void addDevice(int32_t deviceId);
230 void removeDevice(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700231 void configureExcludedDevices();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700232
Jeff Brown6d0fec22010-07-23 21:28:06 -0700233 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234
Jeff Brownc3db8582010-10-20 15:33:38 -0700235 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700236
Jeff Brown6d0fec22010-07-23 21:28:06 -0700237 // state management for all devices
238 Mutex mStateLock;
239
240 int32_t mGlobalMetaState;
241 virtual void updateGlobalMetaState();
242 virtual int32_t getGlobalMetaState();
243
244 InputConfiguration mInputConfiguration;
245 void updateInputConfiguration();
246
Jeff Brownfe508922011-01-18 15:10:10 -0800247 nsecs_t mDisableVirtualKeysTimeout;
248 virtual void disableVirtualKeysUntil(nsecs_t time);
249 virtual bool shouldDropVirtualKey(nsecs_t now,
250 InputDevice* device, int32_t keyCode, int32_t scanCode);
251
Jeff Brown6d0fec22010-07-23 21:28:06 -0700252 // state queries
253 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
254 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
255 GetStateFunc getStateFunc);
256 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
257 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700258};
259
260
261/* Reads raw events from the event hub and processes them, endlessly. */
262class InputReaderThread : public Thread {
263public:
264 InputReaderThread(const sp<InputReaderInterface>& reader);
265 virtual ~InputReaderThread();
266
267private:
268 sp<InputReaderInterface> mReader;
269
270 virtual bool threadLoop();
271};
272
Jeff Brown6d0fec22010-07-23 21:28:06 -0700273
274/* Represents the state of a single input device. */
275class InputDevice {
276public:
277 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
278 ~InputDevice();
279
280 inline InputReaderContext* getContext() { return mContext; }
281 inline int32_t getId() { return mId; }
282 inline const String8& getName() { return mName; }
283 inline uint32_t getSources() { return mSources; }
284
285 inline bool isIgnored() { return mMappers.isEmpty(); }
286
Jeff Brownef3d7e82010-09-30 14:33:04 -0700287 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700288 void addMapper(InputMapper* mapper);
289 void configure();
290 void reset();
291 void process(const RawEvent* rawEvent);
292
293 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
294 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
295 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
296 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
297 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
298 const int32_t* keyCodes, uint8_t* outFlags);
299
300 int32_t getMetaState();
301
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800302 inline const PropertyMap& getConfiguration() {
303 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700304 }
305
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306private:
307 InputReaderContext* mContext;
308 int32_t mId;
309
310 Vector<InputMapper*> mMappers;
311
312 String8 mName;
313 uint32_t mSources;
314
315 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
316 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700317
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800318 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700319};
320
321
322/* An input mapper transforms raw input events into cooked event data.
323 * A single input device can have multiple associated input mappers in order to interpret
324 * different classes of events.
325 */
326class InputMapper {
327public:
328 InputMapper(InputDevice* device);
329 virtual ~InputMapper();
330
331 inline InputDevice* getDevice() { return mDevice; }
332 inline int32_t getDeviceId() { return mDevice->getId(); }
333 inline const String8 getDeviceName() { return mDevice->getName(); }
334 inline InputReaderContext* getContext() { return mContext; }
335 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
336 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
337 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
338
339 virtual uint32_t getSources() = 0;
340 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700341 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700342 virtual void configure();
343 virtual void reset();
344 virtual void process(const RawEvent* rawEvent) = 0;
345
346 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
347 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
348 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
349 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
350 const int32_t* keyCodes, uint8_t* outFlags);
351
352 virtual int32_t getMetaState();
353
354protected:
355 InputDevice* mDevice;
356 InputReaderContext* mContext;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700357};
358
359
360class SwitchInputMapper : public InputMapper {
361public:
362 SwitchInputMapper(InputDevice* device);
363 virtual ~SwitchInputMapper();
364
365 virtual uint32_t getSources();
366 virtual void process(const RawEvent* rawEvent);
367
368 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
369
370private:
371 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
372};
373
374
375class KeyboardInputMapper : public InputMapper {
376public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800377 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700378 virtual ~KeyboardInputMapper();
379
380 virtual uint32_t getSources();
381 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700382 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800383 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 virtual void reset();
385 virtual void process(const RawEvent* rawEvent);
386
387 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
388 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
389 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
390 const int32_t* keyCodes, uint8_t* outFlags);
391
392 virtual int32_t getMetaState();
393
394private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700395 Mutex mLock;
396
Jeff Brown6d0fec22010-07-23 21:28:06 -0700397 struct KeyDown {
398 int32_t keyCode;
399 int32_t scanCode;
400 };
401
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 uint32_t mSources;
403 int32_t mKeyboardType;
404
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800405 // Immutable configuration parameters.
406 struct Parameters {
407 int32_t associatedDisplayId;
408 bool orientationAware;
409 } mParameters;
410
Jeff Brown6328cdc2010-07-29 18:18:33 -0700411 struct LockedState {
412 Vector<KeyDown> keyDowns; // keys that are down
413 int32_t metaState;
414 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700415
416 struct LedState {
417 bool avail; // led is available
418 bool on; // we think the led is currently on
419 };
420 LedState capsLockLedState;
421 LedState numLockLedState;
422 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700423 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700424
Jeff Brown6328cdc2010-07-29 18:18:33 -0700425 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700426
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800427 void configureParameters();
428 void dumpParameters(String8& dump);
429
Jeff Brown6d0fec22010-07-23 21:28:06 -0700430 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700431
Jeff Brown6d0fec22010-07-23 21:28:06 -0700432 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
433 uint32_t policyFlags);
434
Jeff Brown6328cdc2010-07-29 18:18:33 -0700435 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700436
Jeff Brown49ed71d2010-12-06 17:13:33 -0800437 void resetLedStateLocked();
438 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700439 void updateLedStateLocked(bool reset);
440 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
441 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442};
443
444
Jeff Brown83c09682010-12-23 17:50:18 -0800445class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446public:
Jeff Brown83c09682010-12-23 17:50:18 -0800447 CursorInputMapper(InputDevice* device);
448 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449
450 virtual uint32_t getSources();
451 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700452 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800453 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700454 virtual void reset();
455 virtual void process(const RawEvent* rawEvent);
456
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700457 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
458
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459private:
460 // Amount that trackball needs to move in order to generate a key event.
461 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
462
Jeff Brown6328cdc2010-07-29 18:18:33 -0700463 Mutex mLock;
464
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800465 // Immutable configuration parameters.
466 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800467 enum Mode {
468 MODE_POINTER,
469 MODE_NAVIGATION,
470 };
471
472 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800473 int32_t associatedDisplayId;
474 bool orientationAware;
475 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476
477 struct Accumulator {
478 enum {
479 FIELD_BTN_MOUSE = 1,
480 FIELD_REL_X = 2,
481 FIELD_REL_Y = 4
482 };
483
484 uint32_t fields;
485
486 bool btnMouse;
487 int32_t relX;
488 int32_t relY;
489
490 inline void clear() {
491 fields = 0;
492 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493 } mAccumulator;
494
Jeff Brown83c09682010-12-23 17:50:18 -0800495 int32_t mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700496 float mXScale;
497 float mYScale;
498 float mXPrecision;
499 float mYPrecision;
Jeff Brown83c09682010-12-23 17:50:18 -0800500 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700501
Jeff Brown6328cdc2010-07-29 18:18:33 -0700502 struct LockedState {
503 bool down;
504 nsecs_t downTime;
505 } mLocked;
506
507 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700508
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800509 void configureParameters();
510 void dumpParameters(String8& dump);
511
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512 void sync(nsecs_t when);
513};
514
515
516class TouchInputMapper : public InputMapper {
517public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800518 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700519 virtual ~TouchInputMapper();
520
521 virtual uint32_t getSources();
522 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700523 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524 virtual void configure();
525 virtual void reset();
526
527 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
528 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
529 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
530 const int32_t* keyCodes, uint8_t* outFlags);
531
532protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700533 Mutex mLock;
534
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535 struct VirtualKey {
536 int32_t keyCode;
537 int32_t scanCode;
538 uint32_t flags;
539
540 // computed hit box, specified in touch screen coords based on known display size
541 int32_t hitLeft;
542 int32_t hitTop;
543 int32_t hitRight;
544 int32_t hitBottom;
545
546 inline bool isHit(int32_t x, int32_t y) const {
547 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
548 }
549 };
550
Jeff Brown8d608662010-08-30 03:02:23 -0700551 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700552 struct PointerData {
553 uint32_t id;
554 int32_t x;
555 int32_t y;
556 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700557 int32_t touchMajor;
558 int32_t touchMinor;
559 int32_t toolMajor;
560 int32_t toolMinor;
561 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700562
563 inline bool operator== (const PointerData& other) const {
564 return id == other.id
565 && x == other.x
566 && y == other.y
567 && pressure == other.pressure
568 && touchMajor == other.touchMajor
569 && touchMinor == other.touchMinor
570 && toolMajor == other.toolMajor
571 && toolMinor == other.toolMinor
572 && orientation == other.orientation;
573 }
574 inline bool operator!= (const PointerData& other) const {
575 return !(*this == other);
576 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700577 };
578
Jeff Brown8d608662010-08-30 03:02:23 -0700579 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580 struct TouchData {
581 uint32_t pointerCount;
582 PointerData pointers[MAX_POINTERS];
583 BitSet32 idBits;
584 uint32_t idToIndex[MAX_POINTER_ID + 1];
585
586 void copyFrom(const TouchData& other) {
587 pointerCount = other.pointerCount;
588 idBits = other.idBits;
589
590 for (uint32_t i = 0; i < pointerCount; i++) {
591 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700592
593 int id = pointers[i].id;
594 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595 }
596 }
597
598 inline void clear() {
599 pointerCount = 0;
600 idBits.clear();
601 }
602 };
603
Jeff Brown83c09682010-12-23 17:50:18 -0800604 // Input sources supported by the device.
605 int32_t mSources;
606
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607 // Immutable configuration parameters.
608 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800609 enum DeviceType {
610 DEVICE_TYPE_TOUCH_SCREEN,
611 DEVICE_TYPE_TOUCH_PAD,
612 };
613
614 DeviceType deviceType;
615 int32_t associatedDisplayId;
616 bool orientationAware;
617
Jeff Brown6d0fec22010-07-23 21:28:06 -0700618 bool useBadTouchFilter;
619 bool useJumpyTouchFilter;
620 bool useAveragingTouchFilter;
Jeff Brownfe508922011-01-18 15:10:10 -0800621 nsecs_t virtualKeyQuietTime;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700622 } mParameters;
623
Jeff Brown8d608662010-08-30 03:02:23 -0700624 // Immutable calibration parameters in parsed form.
625 struct Calibration {
Jeff Brown511ee5f2010-10-18 13:32:20 -0700626 // Position
627 bool haveXOrigin;
628 int32_t xOrigin;
629 bool haveYOrigin;
630 int32_t yOrigin;
631 bool haveXScale;
632 float xScale;
633 bool haveYScale;
634 float yScale;
635
Jeff Brownc6d282b2010-10-14 21:42:15 -0700636 // Touch Size
637 enum TouchSizeCalibration {
638 TOUCH_SIZE_CALIBRATION_DEFAULT,
639 TOUCH_SIZE_CALIBRATION_NONE,
640 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
641 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700642 };
643
Jeff Brownc6d282b2010-10-14 21:42:15 -0700644 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700645
Jeff Brownc6d282b2010-10-14 21:42:15 -0700646 // Tool Size
647 enum ToolSizeCalibration {
648 TOOL_SIZE_CALIBRATION_DEFAULT,
649 TOOL_SIZE_CALIBRATION_NONE,
650 TOOL_SIZE_CALIBRATION_GEOMETRIC,
651 TOOL_SIZE_CALIBRATION_LINEAR,
652 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700653 };
654
Jeff Brownc6d282b2010-10-14 21:42:15 -0700655 ToolSizeCalibration toolSizeCalibration;
656 bool haveToolSizeLinearScale;
657 float toolSizeLinearScale;
658 bool haveToolSizeLinearBias;
659 float toolSizeLinearBias;
660 bool haveToolSizeAreaScale;
661 float toolSizeAreaScale;
662 bool haveToolSizeAreaBias;
663 float toolSizeAreaBias;
664 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800665 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700666
667 // Pressure
668 enum PressureCalibration {
669 PRESSURE_CALIBRATION_DEFAULT,
670 PRESSURE_CALIBRATION_NONE,
671 PRESSURE_CALIBRATION_PHYSICAL,
672 PRESSURE_CALIBRATION_AMPLITUDE,
673 };
674 enum PressureSource {
675 PRESSURE_SOURCE_DEFAULT,
676 PRESSURE_SOURCE_PRESSURE,
677 PRESSURE_SOURCE_TOUCH,
678 };
679
680 PressureCalibration pressureCalibration;
681 PressureSource pressureSource;
682 bool havePressureScale;
683 float pressureScale;
684
685 // Size
686 enum SizeCalibration {
687 SIZE_CALIBRATION_DEFAULT,
688 SIZE_CALIBRATION_NONE,
689 SIZE_CALIBRATION_NORMALIZED,
690 };
691
692 SizeCalibration sizeCalibration;
693
694 // Orientation
695 enum OrientationCalibration {
696 ORIENTATION_CALIBRATION_DEFAULT,
697 ORIENTATION_CALIBRATION_NONE,
698 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800699 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700700 };
701
702 OrientationCalibration orientationCalibration;
703 } mCalibration;
704
705 // Raw axis information from the driver.
706 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700707 RawAbsoluteAxisInfo x;
708 RawAbsoluteAxisInfo y;
709 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700710 RawAbsoluteAxisInfo touchMajor;
711 RawAbsoluteAxisInfo touchMinor;
712 RawAbsoluteAxisInfo toolMajor;
713 RawAbsoluteAxisInfo toolMinor;
714 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700715 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700716
Jeff Brown6328cdc2010-07-29 18:18:33 -0700717 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700718 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700719 TouchData mLastTouch;
720
721 // The time the primary pointer last went down.
722 nsecs_t mDownTime;
723
Jeff Brown6328cdc2010-07-29 18:18:33 -0700724 struct LockedState {
725 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700726
Jeff Brown6328cdc2010-07-29 18:18:33 -0700727 // The surface orientation and width and height set by configureSurfaceLocked().
728 int32_t surfaceOrientation;
729 int32_t surfaceWidth, surfaceHeight;
730
731 // Translation and scaling factors, orientation-independent.
732 int32_t xOrigin;
733 float xScale;
734 float xPrecision;
735
736 int32_t yOrigin;
737 float yScale;
738 float yPrecision;
739
Jeff Brown8d608662010-08-30 03:02:23 -0700740 float geometricScale;
741
Jeff Brownc6d282b2010-10-14 21:42:15 -0700742 float toolSizeLinearScale;
743 float toolSizeLinearBias;
744 float toolSizeAreaScale;
745 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700746
Jeff Brown6328cdc2010-07-29 18:18:33 -0700747 float pressureScale;
748
Jeff Brown6328cdc2010-07-29 18:18:33 -0700749 float sizeScale;
750
751 float orientationScale;
752
753 // Oriented motion ranges for input device info.
754 struct OrientedRanges {
755 InputDeviceInfo::MotionRange x;
756 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700757
758 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700759 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700760
761 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700762 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700763
Jeff Brownc6d282b2010-10-14 21:42:15 -0700764 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700765 InputDeviceInfo::MotionRange touchMajor;
766 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700767
Jeff Brownc6d282b2010-10-14 21:42:15 -0700768 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700769 InputDeviceInfo::MotionRange toolMajor;
770 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700771
772 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700773 InputDeviceInfo::MotionRange orientation;
774 } orientedRanges;
775
776 // Oriented dimensions and precision.
777 float orientedSurfaceWidth, orientedSurfaceHeight;
778 float orientedXPrecision, orientedYPrecision;
779
780 struct CurrentVirtualKeyState {
781 bool down;
782 nsecs_t downTime;
783 int32_t keyCode;
784 int32_t scanCode;
785 } currentVirtualKey;
786 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700787
Jeff Brown8d608662010-08-30 03:02:23 -0700788 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700789 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700790 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700791 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700792 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700793 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700794 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700795 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700796 virtual void parseCalibration();
797 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700798 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700799
800 enum TouchResult {
801 // Dispatch the touch normally.
802 DISPATCH_TOUCH,
803 // Do not dispatch the touch, but keep tracking the current stroke.
804 SKIP_TOUCH,
805 // Do not dispatch the touch, and drop all information associated with the current stoke
806 // so the next movement will appear as a new down.
807 DROP_STROKE
808 };
809
810 void syncTouch(nsecs_t when, bool havePointerIds);
811
812private:
813 /* Maximum number of historical samples to average. */
814 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
815
816 /* Slop distance for jumpy pointer detection.
817 * The vertical range of the screen divided by this is our epsilon value. */
818 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
819
820 /* Number of jumpy points to drop for touchscreens that need it. */
821 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
822 static const uint32_t JUMPY_DROP_LIMIT = 3;
823
824 /* Maximum squared distance for averaging.
825 * If moving farther than this, turn of averaging to avoid lag in response. */
826 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
827
828 struct AveragingTouchFilterState {
829 // Individual history tracks are stored by pointer id
830 uint32_t historyStart[MAX_POINTERS];
831 uint32_t historyEnd[MAX_POINTERS];
832 struct {
833 struct {
834 int32_t x;
835 int32_t y;
836 int32_t pressure;
837 } pointers[MAX_POINTERS];
838 } historyData[AVERAGING_HISTORY_SIZE];
839 } mAveragingTouchFilter;
840
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700841 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700842 uint32_t jumpyPointsDropped;
843 } mJumpyTouchFilter;
844
845 struct PointerDistanceHeapElement {
846 uint32_t currentPointerIndex : 8;
847 uint32_t lastPointerIndex : 8;
848 uint64_t distance : 48; // squared distance
849 };
850
Jeff Brown6328cdc2010-07-29 18:18:33 -0700851 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700852
853 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
854 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
855 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700856 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
857 int32_t motionEventAction);
Jeff Brownfe508922011-01-18 15:10:10 -0800858 void detectGestures(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700859
Jeff Brown6328cdc2010-07-29 18:18:33 -0700860 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
861 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700862
863 bool applyBadTouchFilter();
864 bool applyJumpyTouchFilter();
865 void applyAveragingTouchFilter();
866 void calculatePointerIds();
867};
868
869
870class SingleTouchInputMapper : public TouchInputMapper {
871public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800872 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700873 virtual ~SingleTouchInputMapper();
874
875 virtual void reset();
876 virtual void process(const RawEvent* rawEvent);
877
878protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700879 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700880
881private:
882 struct Accumulator {
883 enum {
884 FIELD_BTN_TOUCH = 1,
885 FIELD_ABS_X = 2,
886 FIELD_ABS_Y = 4,
887 FIELD_ABS_PRESSURE = 8,
888 FIELD_ABS_TOOL_WIDTH = 16
889 };
890
891 uint32_t fields;
892
893 bool btnTouch;
894 int32_t absX;
895 int32_t absY;
896 int32_t absPressure;
897 int32_t absToolWidth;
898
899 inline void clear() {
900 fields = 0;
901 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700902 } mAccumulator;
903
904 bool mDown;
905 int32_t mX;
906 int32_t mY;
907 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700908 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700909
910 void initialize();
911
912 void sync(nsecs_t when);
913};
914
915
916class MultiTouchInputMapper : public TouchInputMapper {
917public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800918 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700919 virtual ~MultiTouchInputMapper();
920
921 virtual void reset();
922 virtual void process(const RawEvent* rawEvent);
923
924protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700925 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700926
927private:
928 struct Accumulator {
929 enum {
930 FIELD_ABS_MT_POSITION_X = 1,
931 FIELD_ABS_MT_POSITION_Y = 2,
932 FIELD_ABS_MT_TOUCH_MAJOR = 4,
933 FIELD_ABS_MT_TOUCH_MINOR = 8,
934 FIELD_ABS_MT_WIDTH_MAJOR = 16,
935 FIELD_ABS_MT_WIDTH_MINOR = 32,
936 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700937 FIELD_ABS_MT_TRACKING_ID = 128,
938 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700939 };
940
941 uint32_t pointerCount;
942 struct Pointer {
943 uint32_t fields;
944
945 int32_t absMTPositionX;
946 int32_t absMTPositionY;
947 int32_t absMTTouchMajor;
948 int32_t absMTTouchMinor;
949 int32_t absMTWidthMajor;
950 int32_t absMTWidthMinor;
951 int32_t absMTOrientation;
952 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700953 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700954
955 inline void clear() {
956 fields = 0;
957 }
958 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
959
960 inline void clear() {
961 pointerCount = 0;
962 pointers[0].clear();
963 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700964 } mAccumulator;
965
966 void initialize();
967
968 void sync(nsecs_t when);
969};
970
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700971} // namespace android
972
973#endif // _UI_INPUT_READER_H