blob: 903c3c4fadada8e998f38076cb70c08faa5ad97b [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
20#include <ui/EventHub.h>
21#include <ui/Input.h>
Jeff Browne839a582010-04-22 18:58:52 -070022#include <ui/InputDispatcher.h>
23#include <utils/KeyedVector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/BitSet.h>
29
30#include <stddef.h>
31#include <unistd.h>
32
Jeff Browne839a582010-04-22 18:58:52 -070033namespace android {
34
Jeff Browne57e8952010-07-23 21:28:06 -070035class InputDevice;
36class InputMapper;
37
Jeff Brown38a7fab2010-08-30 03:02:23 -070038/* Describes a virtual key. */
39struct VirtualKeyDefinition {
40 int32_t scanCode;
41
42 // configured position data, specified in display coords
43 int32_t centerX;
44 int32_t centerY;
45 int32_t width;
46 int32_t height;
47};
48
49
50/* Specifies input device calibration settings. */
51class InputDeviceCalibration {
52public:
53 InputDeviceCalibration();
54
55 void clear();
56 void addProperty(const String8& key, const String8& value);
57
58 bool tryGetProperty(const String8& key, String8& outValue) const;
59 bool tryGetProperty(const String8& key, int32_t& outValue) const;
60 bool tryGetProperty(const String8& key, float& outValue) const;
61
62private:
63 KeyedVector<String8, String8> mProperties;
64};
65
Jeff Browne57e8952010-07-23 21:28:06 -070066
Jeff Brown54bc2812010-06-15 01:31:58 -070067/*
68 * Input reader policy interface.
69 *
70 * The input reader policy is used by the input reader to interact with the Window Manager
71 * and other system components.
72 *
73 * The actual implementation is partially supported by callbacks into the DVM
74 * via JNI. This interface is also mocked in the unit tests.
75 */
76class InputReaderPolicyInterface : public virtual RefBase {
77protected:
78 InputReaderPolicyInterface() { }
79 virtual ~InputReaderPolicyInterface() { }
80
81public:
82 /* Display orientations. */
83 enum {
84 ROTATION_0 = 0,
85 ROTATION_90 = 1,
86 ROTATION_180 = 2,
87 ROTATION_270 = 3
88 };
89
90 /* Actions returned by interceptXXX methods. */
91 enum {
92 // The input dispatcher should do nothing and discard the input unless other
93 // flags are set.
94 ACTION_NONE = 0,
95
96 // The input dispatcher should dispatch the input to the application.
97 ACTION_DISPATCH = 0x00000001,
Jeff Brown54bc2812010-06-15 01:31:58 -070098 };
99
Jeff Brown54bc2812010-06-15 01:31:58 -0700100 /* Gets information about the display with the specified id.
101 * Returns true if the display info is available, false otherwise.
102 */
103 virtual bool getDisplayInfo(int32_t displayId,
104 int32_t* width, int32_t* height, int32_t* orientation) = 0;
105
Jeff Brownf16c26d2010-07-02 15:37:36 -0700106 /* Provides feedback for a virtual key down.
Jeff Brown54bc2812010-06-15 01:31:58 -0700107 */
Jeff Brownf16c26d2010-07-02 15:37:36 -0700108 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700109
110 /* Intercepts a key event.
111 * The policy can use this method as an opportunity to perform power management functions
Jeff Browne57e8952010-07-23 21:28:06 -0700112 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-06-15 01:31:58 -0700113 *
114 * Returns a policy action constant such as ACTION_DISPATCH.
115 */
116 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
Jeff Browne57e8952010-07-23 21:28:06 -0700117 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700118
119 /* Intercepts a switch event.
120 * The policy can use this method as an opportunity to perform power management functions
Jeff Browne57e8952010-07-23 21:28:06 -0700121 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-06-15 01:31:58 -0700122 *
123 * Switches are not dispatched to applications so this method should
124 * usually return ACTION_NONE.
125 */
Jeff Browne57e8952010-07-23 21:28:06 -0700126 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
127 uint32_t& policyFlags) = 0;
128
129 /* Intercepts a generic touch, trackball or other event.
130 * The policy can use this method as an opportunity to perform power management functions
131 * and early event preprocessing such as updating policy flags.
132 *
133 * Returns a policy action constant such as ACTION_DISPATCH.
134 */
135 virtual int32_t interceptGeneric(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700136
137 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
138 * certain device whose screen currently is not all that good.
139 */
140 virtual bool filterTouchEvents() = 0;
141
142 /* Determines whether to turn on some hacks to improve touch interaction with another device
143 * where touch coordinate data can get corrupted.
144 */
145 virtual bool filterJumpyTouchEvents() = 0;
146
147 /* Gets the configured virtual key definitions for an input device. */
148 virtual void getVirtualKeyDefinitions(const String8& deviceName,
149 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
150
Jeff Brown38a7fab2010-08-30 03:02:23 -0700151 /* Gets the calibration for an input device. */
152 virtual void getInputDeviceCalibration(const String8& deviceName,
153 InputDeviceCalibration& outCalibration) = 0;
154
Jeff Brown54bc2812010-06-15 01:31:58 -0700155 /* Gets the excluded device names for the platform. */
156 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
157};
158
159
160/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700161class InputReaderInterface : public virtual RefBase {
162protected:
163 InputReaderInterface() { }
164 virtual ~InputReaderInterface() { }
165
166public:
Jeff Browna665ca82010-09-08 11:49:43 -0700167 /* Dumps the state of the input reader.
168 *
169 * This method may be called on any thread (usually by the input manager). */
170 virtual void dump(String8& dump) = 0;
171
Jeff Browne839a582010-04-22 18:58:52 -0700172 /* Runs a single iteration of the processing loop.
173 * Nominally reads and processes one incoming message from the EventHub.
174 *
175 * This method should be called on the input reader thread.
176 */
177 virtual void loopOnce() = 0;
178
Jeff Brown54bc2812010-06-15 01:31:58 -0700179 /* Gets the current input device configuration.
180 *
181 * This method may be called on any thread (usually by the input manager).
182 */
Jeff Browne57e8952010-07-23 21:28:06 -0700183 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700184
Jeff Browne57e8952010-07-23 21:28:06 -0700185 /* Gets information about the specified input device.
186 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
187 * was no such device.
188 *
189 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700190 */
Jeff Browne57e8952010-07-23 21:28:06 -0700191 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
192
193 /* Gets the list of all registered device ids. */
194 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
195
196 /* Query current input state. */
197 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
198 int32_t scanCode) = 0;
199 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
200 int32_t keyCode) = 0;
201 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
202 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700203
204 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700205 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
206 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
207};
208
209
210/* Internal interface used by individual input devices to access global input device state
211 * and parameters maintained by the input reader.
212 */
213class InputReaderContext {
214protected:
215 InputReaderContext() { }
216 virtual ~InputReaderContext() { }
217
218public:
219 virtual void updateGlobalMetaState() = 0;
220 virtual int32_t getGlobalMetaState() = 0;
221
222 virtual InputReaderPolicyInterface* getPolicy() = 0;
223 virtual InputDispatcherInterface* getDispatcher() = 0;
224 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700225};
226
Jeff Brown54bc2812010-06-15 01:31:58 -0700227
Jeff Browne839a582010-04-22 18:58:52 -0700228/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700229 * that it sends to the input dispatcher. Some functions of the input reader, such as early
230 * event filtering in low power states, are controlled by a separate policy object.
231 *
232 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700233 * Because the policy and dispatcher can potentially block or cause re-entrance into
234 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700235 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700236 */
Jeff Browne57e8952010-07-23 21:28:06 -0700237class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700238public:
239 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700240 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700241 const sp<InputDispatcherInterface>& dispatcher);
242 virtual ~InputReader();
243
Jeff Browna665ca82010-09-08 11:49:43 -0700244 virtual void dump(String8& dump);
245
Jeff Browne839a582010-04-22 18:58:52 -0700246 virtual void loopOnce();
247
Jeff Browne57e8952010-07-23 21:28:06 -0700248 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700249
Jeff Browne57e8952010-07-23 21:28:06 -0700250 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
251 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700252
Jeff Browne57e8952010-07-23 21:28:06 -0700253 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
254 int32_t scanCode);
255 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
256 int32_t keyCode);
257 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
258 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700259
Jeff Browne57e8952010-07-23 21:28:06 -0700260 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
261 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700262
Jeff Browne839a582010-04-22 18:58:52 -0700263private:
Jeff Browne839a582010-04-22 18:58:52 -0700264 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700265 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700266 sp<InputDispatcherInterface> mDispatcher;
267
Jeff Browne57e8952010-07-23 21:28:06 -0700268 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
269 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
270 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
271
272 // This reader/writer lock guards the list of input devices.
273 // The writer lock must be held whenever the list of input devices is modified
274 // and then promptly released.
275 // The reader lock must be held whenever the list of input devices is traversed or an
276 // input device in the list is accessed.
277 // This lock only protects the registry and prevents inadvertent deletion of device objects
278 // that are in use. Individual devices are responsible for guarding their own internal state
279 // as needed for concurrent operation.
280 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700281 KeyedVector<int32_t, InputDevice*> mDevices;
282
Jeff Browne57e8952010-07-23 21:28:06 -0700283 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700284 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700285
Jeff Browne839a582010-04-22 18:58:52 -0700286 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700287 void removeDevice(nsecs_t when, int32_t deviceId);
288 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown54bc2812010-06-15 01:31:58 -0700289 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700290
Jeff Browne57e8952010-07-23 21:28:06 -0700291 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700292
Jeff Browne57e8952010-07-23 21:28:06 -0700293 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700294
Jeff Browne57e8952010-07-23 21:28:06 -0700295 // state management for all devices
296 Mutex mStateLock;
297
298 int32_t mGlobalMetaState;
299 virtual void updateGlobalMetaState();
300 virtual int32_t getGlobalMetaState();
301
302 InputConfiguration mInputConfiguration;
303 void updateInputConfiguration();
304
305 // state queries
306 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
307 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
308 GetStateFunc getStateFunc);
309 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
310 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browna665ca82010-09-08 11:49:43 -0700311
312 // dump state
313 void dumpDeviceInfo(String8& dump);
Jeff Browne839a582010-04-22 18:58:52 -0700314};
315
316
317/* Reads raw events from the event hub and processes them, endlessly. */
318class InputReaderThread : public Thread {
319public:
320 InputReaderThread(const sp<InputReaderInterface>& reader);
321 virtual ~InputReaderThread();
322
323private:
324 sp<InputReaderInterface> mReader;
325
326 virtual bool threadLoop();
327};
328
Jeff Browne57e8952010-07-23 21:28:06 -0700329
330/* Represents the state of a single input device. */
331class InputDevice {
332public:
333 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
334 ~InputDevice();
335
336 inline InputReaderContext* getContext() { return mContext; }
337 inline int32_t getId() { return mId; }
338 inline const String8& getName() { return mName; }
339 inline uint32_t getSources() { return mSources; }
340
341 inline bool isIgnored() { return mMappers.isEmpty(); }
342
343 void addMapper(InputMapper* mapper);
344 void configure();
345 void reset();
346 void process(const RawEvent* rawEvent);
347
348 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
349 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
350 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
351 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
352 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
353 const int32_t* keyCodes, uint8_t* outFlags);
354
355 int32_t getMetaState();
356
Jeff Brown38a7fab2010-08-30 03:02:23 -0700357 inline const InputDeviceCalibration& getCalibration() {
358 return mCalibration;
359 }
360
Jeff Browne57e8952010-07-23 21:28:06 -0700361private:
362 InputReaderContext* mContext;
363 int32_t mId;
364
365 Vector<InputMapper*> mMappers;
366
367 String8 mName;
368 uint32_t mSources;
369
370 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
371 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700372
373 InputDeviceCalibration mCalibration;
Jeff Browne57e8952010-07-23 21:28:06 -0700374};
375
376
377/* An input mapper transforms raw input events into cooked event data.
378 * A single input device can have multiple associated input mappers in order to interpret
379 * different classes of events.
380 */
381class InputMapper {
382public:
383 InputMapper(InputDevice* device);
384 virtual ~InputMapper();
385
386 inline InputDevice* getDevice() { return mDevice; }
387 inline int32_t getDeviceId() { return mDevice->getId(); }
388 inline const String8 getDeviceName() { return mDevice->getName(); }
389 inline InputReaderContext* getContext() { return mContext; }
390 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
391 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
392 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
393
394 virtual uint32_t getSources() = 0;
395 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
396 virtual void configure();
397 virtual void reset();
398 virtual void process(const RawEvent* rawEvent) = 0;
399
400 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
401 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
402 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
403 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
404 const int32_t* keyCodes, uint8_t* outFlags);
405
406 virtual int32_t getMetaState();
407
408protected:
409 InputDevice* mDevice;
410 InputReaderContext* mContext;
411
412 bool applyStandardPolicyActions(nsecs_t when, int32_t policyActions);
413};
414
415
416class SwitchInputMapper : public InputMapper {
417public:
418 SwitchInputMapper(InputDevice* device);
419 virtual ~SwitchInputMapper();
420
421 virtual uint32_t getSources();
422 virtual void process(const RawEvent* rawEvent);
423
424 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
425
426private:
427 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
428};
429
430
431class KeyboardInputMapper : public InputMapper {
432public:
433 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
434 int32_t keyboardType);
435 virtual ~KeyboardInputMapper();
436
437 virtual uint32_t getSources();
438 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
439 virtual void reset();
440 virtual void process(const RawEvent* rawEvent);
441
442 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
443 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
444 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
445 const int32_t* keyCodes, uint8_t* outFlags);
446
447 virtual int32_t getMetaState();
448
449private:
Jeff Brownb51719b2010-07-29 18:18:33 -0700450 Mutex mLock;
451
Jeff Browne57e8952010-07-23 21:28:06 -0700452 struct KeyDown {
453 int32_t keyCode;
454 int32_t scanCode;
455 };
456
457 int32_t mAssociatedDisplayId;
458 uint32_t mSources;
459 int32_t mKeyboardType;
460
Jeff Brownb51719b2010-07-29 18:18:33 -0700461 struct LockedState {
462 Vector<KeyDown> keyDowns; // keys that are down
463 int32_t metaState;
464 nsecs_t downTime; // time of most recent key down
465 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700466
Jeff Brownb51719b2010-07-29 18:18:33 -0700467 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700468
469 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700470
Jeff Browne57e8952010-07-23 21:28:06 -0700471 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
472 uint32_t policyFlags);
Jeff Brownb51719b2010-07-29 18:18:33 -0700473 void applyPolicyAndDispatch(nsecs_t when, uint32_t policyFlags,
474 bool down, int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Browne57e8952010-07-23 21:28:06 -0700475
Jeff Brownb51719b2010-07-29 18:18:33 -0700476 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -0700477};
478
479
480class TrackballInputMapper : public InputMapper {
481public:
482 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
483 virtual ~TrackballInputMapper();
484
485 virtual uint32_t getSources();
486 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
487 virtual void reset();
488 virtual void process(const RawEvent* rawEvent);
489
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700490 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
491
Jeff Browne57e8952010-07-23 21:28:06 -0700492private:
493 // Amount that trackball needs to move in order to generate a key event.
494 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
495
Jeff Brownb51719b2010-07-29 18:18:33 -0700496 Mutex mLock;
497
Jeff Browne57e8952010-07-23 21:28:06 -0700498 int32_t mAssociatedDisplayId;
499
500 struct Accumulator {
501 enum {
502 FIELD_BTN_MOUSE = 1,
503 FIELD_REL_X = 2,
504 FIELD_REL_Y = 4
505 };
506
507 uint32_t fields;
508
509 bool btnMouse;
510 int32_t relX;
511 int32_t relY;
512
513 inline void clear() {
514 fields = 0;
515 }
Jeff Browne57e8952010-07-23 21:28:06 -0700516 } mAccumulator;
517
Jeff Browne57e8952010-07-23 21:28:06 -0700518 float mXScale;
519 float mYScale;
520 float mXPrecision;
521 float mYPrecision;
522
Jeff Brownb51719b2010-07-29 18:18:33 -0700523 struct LockedState {
524 bool down;
525 nsecs_t downTime;
526 } mLocked;
527
528 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700529
530 void sync(nsecs_t when);
Jeff Brownb51719b2010-07-29 18:18:33 -0700531 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
532 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Browne57e8952010-07-23 21:28:06 -0700533};
534
535
536class TouchInputMapper : public InputMapper {
537public:
538 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
539 virtual ~TouchInputMapper();
540
541 virtual uint32_t getSources();
542 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
543 virtual void configure();
544 virtual void reset();
545
546 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
547 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
548 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
549 const int32_t* keyCodes, uint8_t* outFlags);
550
551protected:
552 /* Maximum pointer id value supported.
553 * (This is limited by our use of BitSet32 to track pointer assignments.) */
554 static const uint32_t MAX_POINTER_ID = 31;
555
Jeff Brownb51719b2010-07-29 18:18:33 -0700556 Mutex mLock;
557
Jeff Browne57e8952010-07-23 21:28:06 -0700558 struct VirtualKey {
559 int32_t keyCode;
560 int32_t scanCode;
561 uint32_t flags;
562
563 // computed hit box, specified in touch screen coords based on known display size
564 int32_t hitLeft;
565 int32_t hitTop;
566 int32_t hitRight;
567 int32_t hitBottom;
568
569 inline bool isHit(int32_t x, int32_t y) const {
570 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
571 }
572 };
573
Jeff Brown38a7fab2010-08-30 03:02:23 -0700574 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700575 struct PointerData {
576 uint32_t id;
577 int32_t x;
578 int32_t y;
579 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700580 int32_t touchMajor;
581 int32_t touchMinor;
582 int32_t toolMajor;
583 int32_t toolMinor;
584 int32_t orientation;
585 };
586
Jeff Brown38a7fab2010-08-30 03:02:23 -0700587 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700588 struct TouchData {
589 uint32_t pointerCount;
590 PointerData pointers[MAX_POINTERS];
591 BitSet32 idBits;
592 uint32_t idToIndex[MAX_POINTER_ID + 1];
593
594 void copyFrom(const TouchData& other) {
595 pointerCount = other.pointerCount;
596 idBits = other.idBits;
597
598 for (uint32_t i = 0; i < pointerCount; i++) {
599 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700600
601 int id = pointers[i].id;
602 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700603 }
604 }
605
606 inline void clear() {
607 pointerCount = 0;
608 idBits.clear();
609 }
610 };
611
612 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700613
614 // Immutable configuration parameters.
615 struct Parameters {
616 bool useBadTouchFilter;
617 bool useJumpyTouchFilter;
618 bool useAveragingTouchFilter;
619 } mParameters;
620
Jeff Brown38a7fab2010-08-30 03:02:23 -0700621 // Immutable calibration parameters in parsed form.
622 struct Calibration {
623 // Touch Area
624 enum TouchAreaCalibration {
625 TOUCH_AREA_CALIBRATION_DEFAULT,
626 TOUCH_AREA_CALIBRATION_NONE,
627 TOUCH_AREA_CALIBRATION_GEOMETRIC,
628 TOUCH_AREA_CALIBRATION_PRESSURE,
629 };
630
631 TouchAreaCalibration touchAreaCalibration;
632
633 // Tool Area
634 enum ToolAreaCalibration {
635 TOOL_AREA_CALIBRATION_DEFAULT,
636 TOOL_AREA_CALIBRATION_NONE,
637 TOOL_AREA_CALIBRATION_GEOMETRIC,
638 TOOL_AREA_CALIBRATION_LINEAR,
639 };
640
641 ToolAreaCalibration toolAreaCalibration;
642 bool haveToolAreaLinearScale;
643 float toolAreaLinearScale;
644 bool haveToolAreaLinearBias;
645 float toolAreaLinearBias;
646 bool haveToolAreaIsSummed;
647 int32_t toolAreaIsSummed;
648
649 // Pressure
650 enum PressureCalibration {
651 PRESSURE_CALIBRATION_DEFAULT,
652 PRESSURE_CALIBRATION_NONE,
653 PRESSURE_CALIBRATION_PHYSICAL,
654 PRESSURE_CALIBRATION_AMPLITUDE,
655 };
656 enum PressureSource {
657 PRESSURE_SOURCE_DEFAULT,
658 PRESSURE_SOURCE_PRESSURE,
659 PRESSURE_SOURCE_TOUCH,
660 };
661
662 PressureCalibration pressureCalibration;
663 PressureSource pressureSource;
664 bool havePressureScale;
665 float pressureScale;
666
667 // Size
668 enum SizeCalibration {
669 SIZE_CALIBRATION_DEFAULT,
670 SIZE_CALIBRATION_NONE,
671 SIZE_CALIBRATION_NORMALIZED,
672 };
673
674 SizeCalibration sizeCalibration;
675
676 // Orientation
677 enum OrientationCalibration {
678 ORIENTATION_CALIBRATION_DEFAULT,
679 ORIENTATION_CALIBRATION_NONE,
680 ORIENTATION_CALIBRATION_INTERPOLATED,
681 };
682
683 OrientationCalibration orientationCalibration;
684 } mCalibration;
685
686 // Raw axis information from the driver.
687 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700688 RawAbsoluteAxisInfo x;
689 RawAbsoluteAxisInfo y;
690 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700691 RawAbsoluteAxisInfo touchMajor;
692 RawAbsoluteAxisInfo touchMinor;
693 RawAbsoluteAxisInfo toolMajor;
694 RawAbsoluteAxisInfo toolMinor;
695 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700696 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700697
Jeff Brownb51719b2010-07-29 18:18:33 -0700698 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700699 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700700 TouchData mLastTouch;
701
702 // The time the primary pointer last went down.
703 nsecs_t mDownTime;
704
Jeff Brownb51719b2010-07-29 18:18:33 -0700705 struct LockedState {
706 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700707
Jeff Brownb51719b2010-07-29 18:18:33 -0700708 // The surface orientation and width and height set by configureSurfaceLocked().
709 int32_t surfaceOrientation;
710 int32_t surfaceWidth, surfaceHeight;
711
712 // Translation and scaling factors, orientation-independent.
713 int32_t xOrigin;
714 float xScale;
715 float xPrecision;
716
717 int32_t yOrigin;
718 float yScale;
719 float yPrecision;
720
Jeff Brown38a7fab2010-08-30 03:02:23 -0700721 float geometricScale;
722
723 float toolAreaLinearScale;
724 float toolAreaLinearBias;
725
Jeff Brownb51719b2010-07-29 18:18:33 -0700726 float pressureScale;
727
Jeff Brownb51719b2010-07-29 18:18:33 -0700728 float sizeScale;
729
730 float orientationScale;
731
732 // Oriented motion ranges for input device info.
733 struct OrientedRanges {
734 InputDeviceInfo::MotionRange x;
735 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700736
737 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700738 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700739
740 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700741 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700742
743 bool haveTouchArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700744 InputDeviceInfo::MotionRange touchMajor;
745 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700746
747 bool haveToolArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700748 InputDeviceInfo::MotionRange toolMajor;
749 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700750
751 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700752 InputDeviceInfo::MotionRange orientation;
753 } orientedRanges;
754
755 // Oriented dimensions and precision.
756 float orientedSurfaceWidth, orientedSurfaceHeight;
757 float orientedXPrecision, orientedYPrecision;
758
759 struct CurrentVirtualKeyState {
760 bool down;
761 nsecs_t downTime;
762 int32_t keyCode;
763 int32_t scanCode;
764 } currentVirtualKey;
765 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700766
Jeff Brown38a7fab2010-08-30 03:02:23 -0700767 virtual void configureParameters();
Jeff Browna665ca82010-09-08 11:49:43 -0700768 virtual void logParameters();
Jeff Brown38a7fab2010-08-30 03:02:23 -0700769 virtual void configureRawAxes();
770 virtual void logRawAxes();
Jeff Brownb51719b2010-07-29 18:18:33 -0700771 virtual bool configureSurfaceLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700772 virtual void logMotionRangesLocked();
Jeff Brownb51719b2010-07-29 18:18:33 -0700773 virtual void configureVirtualKeysLocked();
Jeff Brown38a7fab2010-08-30 03:02:23 -0700774 virtual void parseCalibration();
775 virtual void resolveCalibration();
776 virtual void logCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -0700777
778 enum TouchResult {
779 // Dispatch the touch normally.
780 DISPATCH_TOUCH,
781 // Do not dispatch the touch, but keep tracking the current stroke.
782 SKIP_TOUCH,
783 // Do not dispatch the touch, and drop all information associated with the current stoke
784 // so the next movement will appear as a new down.
785 DROP_STROKE
786 };
787
788 void syncTouch(nsecs_t when, bool havePointerIds);
789
790private:
791 /* Maximum number of historical samples to average. */
792 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
793
794 /* Slop distance for jumpy pointer detection.
795 * The vertical range of the screen divided by this is our epsilon value. */
796 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
797
798 /* Number of jumpy points to drop for touchscreens that need it. */
799 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
800 static const uint32_t JUMPY_DROP_LIMIT = 3;
801
802 /* Maximum squared distance for averaging.
803 * If moving farther than this, turn of averaging to avoid lag in response. */
804 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
805
806 struct AveragingTouchFilterState {
807 // Individual history tracks are stored by pointer id
808 uint32_t historyStart[MAX_POINTERS];
809 uint32_t historyEnd[MAX_POINTERS];
810 struct {
811 struct {
812 int32_t x;
813 int32_t y;
814 int32_t pressure;
815 } pointers[MAX_POINTERS];
816 } historyData[AVERAGING_HISTORY_SIZE];
817 } mAveragingTouchFilter;
818
Jeff Brownd64c8552010-08-17 20:38:35 -0700819 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700820 uint32_t jumpyPointsDropped;
821 } mJumpyTouchFilter;
822
823 struct PointerDistanceHeapElement {
824 uint32_t currentPointerIndex : 8;
825 uint32_t lastPointerIndex : 8;
826 uint64_t distance : 48; // squared distance
827 };
828
Jeff Brownb51719b2010-07-29 18:18:33 -0700829 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700830
831 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
832 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
833 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700834 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
835 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700836
Jeff Brownb51719b2010-07-29 18:18:33 -0700837 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
838 int32_t keyEventAction, int32_t keyEventFlags,
839 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
840
841 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
842 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700843
844 bool applyBadTouchFilter();
845 bool applyJumpyTouchFilter();
846 void applyAveragingTouchFilter();
847 void calculatePointerIds();
848};
849
850
851class SingleTouchInputMapper : public TouchInputMapper {
852public:
853 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
854 virtual ~SingleTouchInputMapper();
855
856 virtual void reset();
857 virtual void process(const RawEvent* rawEvent);
858
859protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700860 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700861
862private:
863 struct Accumulator {
864 enum {
865 FIELD_BTN_TOUCH = 1,
866 FIELD_ABS_X = 2,
867 FIELD_ABS_Y = 4,
868 FIELD_ABS_PRESSURE = 8,
869 FIELD_ABS_TOOL_WIDTH = 16
870 };
871
872 uint32_t fields;
873
874 bool btnTouch;
875 int32_t absX;
876 int32_t absY;
877 int32_t absPressure;
878 int32_t absToolWidth;
879
880 inline void clear() {
881 fields = 0;
882 }
Jeff Browne57e8952010-07-23 21:28:06 -0700883 } mAccumulator;
884
885 bool mDown;
886 int32_t mX;
887 int32_t mY;
888 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700889 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700890
891 void initialize();
892
893 void sync(nsecs_t when);
894};
895
896
897class MultiTouchInputMapper : public TouchInputMapper {
898public:
899 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
900 virtual ~MultiTouchInputMapper();
901
902 virtual void reset();
903 virtual void process(const RawEvent* rawEvent);
904
905protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700906 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700907
908private:
909 struct Accumulator {
910 enum {
911 FIELD_ABS_MT_POSITION_X = 1,
912 FIELD_ABS_MT_POSITION_Y = 2,
913 FIELD_ABS_MT_TOUCH_MAJOR = 4,
914 FIELD_ABS_MT_TOUCH_MINOR = 8,
915 FIELD_ABS_MT_WIDTH_MAJOR = 16,
916 FIELD_ABS_MT_WIDTH_MINOR = 32,
917 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700918 FIELD_ABS_MT_TRACKING_ID = 128,
919 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700920 };
921
922 uint32_t pointerCount;
923 struct Pointer {
924 uint32_t fields;
925
926 int32_t absMTPositionX;
927 int32_t absMTPositionY;
928 int32_t absMTTouchMajor;
929 int32_t absMTTouchMinor;
930 int32_t absMTWidthMajor;
931 int32_t absMTWidthMinor;
932 int32_t absMTOrientation;
933 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700934 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700935
936 inline void clear() {
937 fields = 0;
938 }
939 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
940
941 inline void clear() {
942 pointerCount = 0;
943 pointers[0].clear();
944 }
Jeff Browne57e8952010-07-23 21:28:06 -0700945 } mAccumulator;
946
947 void initialize();
948
949 void sync(nsecs_t when);
950};
951
Jeff Browne839a582010-04-22 18:58:52 -0700952} // namespace android
953
954#endif // _UI_INPUT_READER_H