blob: e85735a7171e4c887a671b8874a38e35811737d3 [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:
Jeff Brownb51719b2010-07-29 18:18:33 -0700552 Mutex mLock;
553
Jeff Browne57e8952010-07-23 21:28:06 -0700554 struct VirtualKey {
555 int32_t keyCode;
556 int32_t scanCode;
557 uint32_t flags;
558
559 // computed hit box, specified in touch screen coords based on known display size
560 int32_t hitLeft;
561 int32_t hitTop;
562 int32_t hitRight;
563 int32_t hitBottom;
564
565 inline bool isHit(int32_t x, int32_t y) const {
566 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
567 }
568 };
569
Jeff Brown38a7fab2010-08-30 03:02:23 -0700570 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700571 struct PointerData {
572 uint32_t id;
573 int32_t x;
574 int32_t y;
575 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700576 int32_t touchMajor;
577 int32_t touchMinor;
578 int32_t toolMajor;
579 int32_t toolMinor;
580 int32_t orientation;
581 };
582
Jeff Brown38a7fab2010-08-30 03:02:23 -0700583 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700584 struct TouchData {
585 uint32_t pointerCount;
586 PointerData pointers[MAX_POINTERS];
587 BitSet32 idBits;
588 uint32_t idToIndex[MAX_POINTER_ID + 1];
589
590 void copyFrom(const TouchData& other) {
591 pointerCount = other.pointerCount;
592 idBits = other.idBits;
593
594 for (uint32_t i = 0; i < pointerCount; i++) {
595 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700596
597 int id = pointers[i].id;
598 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700599 }
600 }
601
602 inline void clear() {
603 pointerCount = 0;
604 idBits.clear();
605 }
606 };
607
608 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700609
610 // Immutable configuration parameters.
611 struct Parameters {
612 bool useBadTouchFilter;
613 bool useJumpyTouchFilter;
614 bool useAveragingTouchFilter;
615 } mParameters;
616
Jeff Brown38a7fab2010-08-30 03:02:23 -0700617 // Immutable calibration parameters in parsed form.
618 struct Calibration {
619 // Touch Area
620 enum TouchAreaCalibration {
621 TOUCH_AREA_CALIBRATION_DEFAULT,
622 TOUCH_AREA_CALIBRATION_NONE,
623 TOUCH_AREA_CALIBRATION_GEOMETRIC,
624 TOUCH_AREA_CALIBRATION_PRESSURE,
625 };
626
627 TouchAreaCalibration touchAreaCalibration;
628
629 // Tool Area
630 enum ToolAreaCalibration {
631 TOOL_AREA_CALIBRATION_DEFAULT,
632 TOOL_AREA_CALIBRATION_NONE,
633 TOOL_AREA_CALIBRATION_GEOMETRIC,
634 TOOL_AREA_CALIBRATION_LINEAR,
635 };
636
637 ToolAreaCalibration toolAreaCalibration;
638 bool haveToolAreaLinearScale;
639 float toolAreaLinearScale;
640 bool haveToolAreaLinearBias;
641 float toolAreaLinearBias;
642 bool haveToolAreaIsSummed;
643 int32_t toolAreaIsSummed;
644
645 // Pressure
646 enum PressureCalibration {
647 PRESSURE_CALIBRATION_DEFAULT,
648 PRESSURE_CALIBRATION_NONE,
649 PRESSURE_CALIBRATION_PHYSICAL,
650 PRESSURE_CALIBRATION_AMPLITUDE,
651 };
652 enum PressureSource {
653 PRESSURE_SOURCE_DEFAULT,
654 PRESSURE_SOURCE_PRESSURE,
655 PRESSURE_SOURCE_TOUCH,
656 };
657
658 PressureCalibration pressureCalibration;
659 PressureSource pressureSource;
660 bool havePressureScale;
661 float pressureScale;
662
663 // Size
664 enum SizeCalibration {
665 SIZE_CALIBRATION_DEFAULT,
666 SIZE_CALIBRATION_NONE,
667 SIZE_CALIBRATION_NORMALIZED,
668 };
669
670 SizeCalibration sizeCalibration;
671
672 // Orientation
673 enum OrientationCalibration {
674 ORIENTATION_CALIBRATION_DEFAULT,
675 ORIENTATION_CALIBRATION_NONE,
676 ORIENTATION_CALIBRATION_INTERPOLATED,
677 };
678
679 OrientationCalibration orientationCalibration;
680 } mCalibration;
681
682 // Raw axis information from the driver.
683 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700684 RawAbsoluteAxisInfo x;
685 RawAbsoluteAxisInfo y;
686 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700687 RawAbsoluteAxisInfo touchMajor;
688 RawAbsoluteAxisInfo touchMinor;
689 RawAbsoluteAxisInfo toolMajor;
690 RawAbsoluteAxisInfo toolMinor;
691 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700692 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700693
Jeff Brownb51719b2010-07-29 18:18:33 -0700694 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700695 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700696 TouchData mLastTouch;
697
698 // The time the primary pointer last went down.
699 nsecs_t mDownTime;
700
Jeff Brownb51719b2010-07-29 18:18:33 -0700701 struct LockedState {
702 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700703
Jeff Brownb51719b2010-07-29 18:18:33 -0700704 // The surface orientation and width and height set by configureSurfaceLocked().
705 int32_t surfaceOrientation;
706 int32_t surfaceWidth, surfaceHeight;
707
708 // Translation and scaling factors, orientation-independent.
709 int32_t xOrigin;
710 float xScale;
711 float xPrecision;
712
713 int32_t yOrigin;
714 float yScale;
715 float yPrecision;
716
Jeff Brown38a7fab2010-08-30 03:02:23 -0700717 float geometricScale;
718
719 float toolAreaLinearScale;
720 float toolAreaLinearBias;
721
Jeff Brownb51719b2010-07-29 18:18:33 -0700722 float pressureScale;
723
Jeff Brownb51719b2010-07-29 18:18:33 -0700724 float sizeScale;
725
726 float orientationScale;
727
728 // Oriented motion ranges for input device info.
729 struct OrientedRanges {
730 InputDeviceInfo::MotionRange x;
731 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700732
733 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700734 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700735
736 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700737 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700738
739 bool haveTouchArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700740 InputDeviceInfo::MotionRange touchMajor;
741 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700742
743 bool haveToolArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700744 InputDeviceInfo::MotionRange toolMajor;
745 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700746
747 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700748 InputDeviceInfo::MotionRange orientation;
749 } orientedRanges;
750
751 // Oriented dimensions and precision.
752 float orientedSurfaceWidth, orientedSurfaceHeight;
753 float orientedXPrecision, orientedYPrecision;
754
755 struct CurrentVirtualKeyState {
756 bool down;
757 nsecs_t downTime;
758 int32_t keyCode;
759 int32_t scanCode;
760 } currentVirtualKey;
761 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700762
Jeff Brown38a7fab2010-08-30 03:02:23 -0700763 virtual void configureParameters();
Jeff Browna665ca82010-09-08 11:49:43 -0700764 virtual void logParameters();
Jeff Brown38a7fab2010-08-30 03:02:23 -0700765 virtual void configureRawAxes();
766 virtual void logRawAxes();
Jeff Brownb51719b2010-07-29 18:18:33 -0700767 virtual bool configureSurfaceLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700768 virtual void logMotionRangesLocked();
Jeff Brownb51719b2010-07-29 18:18:33 -0700769 virtual void configureVirtualKeysLocked();
Jeff Brown38a7fab2010-08-30 03:02:23 -0700770 virtual void parseCalibration();
771 virtual void resolveCalibration();
772 virtual void logCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -0700773
774 enum TouchResult {
775 // Dispatch the touch normally.
776 DISPATCH_TOUCH,
777 // Do not dispatch the touch, but keep tracking the current stroke.
778 SKIP_TOUCH,
779 // Do not dispatch the touch, and drop all information associated with the current stoke
780 // so the next movement will appear as a new down.
781 DROP_STROKE
782 };
783
784 void syncTouch(nsecs_t when, bool havePointerIds);
785
786private:
787 /* Maximum number of historical samples to average. */
788 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
789
790 /* Slop distance for jumpy pointer detection.
791 * The vertical range of the screen divided by this is our epsilon value. */
792 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
793
794 /* Number of jumpy points to drop for touchscreens that need it. */
795 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
796 static const uint32_t JUMPY_DROP_LIMIT = 3;
797
798 /* Maximum squared distance for averaging.
799 * If moving farther than this, turn of averaging to avoid lag in response. */
800 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
801
802 struct AveragingTouchFilterState {
803 // Individual history tracks are stored by pointer id
804 uint32_t historyStart[MAX_POINTERS];
805 uint32_t historyEnd[MAX_POINTERS];
806 struct {
807 struct {
808 int32_t x;
809 int32_t y;
810 int32_t pressure;
811 } pointers[MAX_POINTERS];
812 } historyData[AVERAGING_HISTORY_SIZE];
813 } mAveragingTouchFilter;
814
Jeff Brownd64c8552010-08-17 20:38:35 -0700815 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700816 uint32_t jumpyPointsDropped;
817 } mJumpyTouchFilter;
818
819 struct PointerDistanceHeapElement {
820 uint32_t currentPointerIndex : 8;
821 uint32_t lastPointerIndex : 8;
822 uint64_t distance : 48; // squared distance
823 };
824
Jeff Brownb51719b2010-07-29 18:18:33 -0700825 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700826
827 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
828 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
829 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700830 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
831 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700832
Jeff Brownb51719b2010-07-29 18:18:33 -0700833 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
834 int32_t keyEventAction, int32_t keyEventFlags,
835 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
836
837 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
838 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700839
840 bool applyBadTouchFilter();
841 bool applyJumpyTouchFilter();
842 void applyAveragingTouchFilter();
843 void calculatePointerIds();
844};
845
846
847class SingleTouchInputMapper : public TouchInputMapper {
848public:
849 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
850 virtual ~SingleTouchInputMapper();
851
852 virtual void reset();
853 virtual void process(const RawEvent* rawEvent);
854
855protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700856 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700857
858private:
859 struct Accumulator {
860 enum {
861 FIELD_BTN_TOUCH = 1,
862 FIELD_ABS_X = 2,
863 FIELD_ABS_Y = 4,
864 FIELD_ABS_PRESSURE = 8,
865 FIELD_ABS_TOOL_WIDTH = 16
866 };
867
868 uint32_t fields;
869
870 bool btnTouch;
871 int32_t absX;
872 int32_t absY;
873 int32_t absPressure;
874 int32_t absToolWidth;
875
876 inline void clear() {
877 fields = 0;
878 }
Jeff Browne57e8952010-07-23 21:28:06 -0700879 } mAccumulator;
880
881 bool mDown;
882 int32_t mX;
883 int32_t mY;
884 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700885 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700886
887 void initialize();
888
889 void sync(nsecs_t when);
890};
891
892
893class MultiTouchInputMapper : public TouchInputMapper {
894public:
895 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
896 virtual ~MultiTouchInputMapper();
897
898 virtual void reset();
899 virtual void process(const RawEvent* rawEvent);
900
901protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700902 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700903
904private:
905 struct Accumulator {
906 enum {
907 FIELD_ABS_MT_POSITION_X = 1,
908 FIELD_ABS_MT_POSITION_Y = 2,
909 FIELD_ABS_MT_TOUCH_MAJOR = 4,
910 FIELD_ABS_MT_TOUCH_MINOR = 8,
911 FIELD_ABS_MT_WIDTH_MAJOR = 16,
912 FIELD_ABS_MT_WIDTH_MINOR = 32,
913 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700914 FIELD_ABS_MT_TRACKING_ID = 128,
915 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700916 };
917
918 uint32_t pointerCount;
919 struct Pointer {
920 uint32_t fields;
921
922 int32_t absMTPositionX;
923 int32_t absMTPositionY;
924 int32_t absMTTouchMajor;
925 int32_t absMTTouchMinor;
926 int32_t absMTWidthMajor;
927 int32_t absMTWidthMinor;
928 int32_t absMTOrientation;
929 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700930 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700931
932 inline void clear() {
933 fields = 0;
934 }
935 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
936
937 inline void clear() {
938 pointerCount = 0;
939 pointers[0].clear();
940 }
Jeff Browne57e8952010-07-23 21:28:06 -0700941 } mAccumulator;
942
943 void initialize();
944
945 void sync(nsecs_t when);
946};
947
Jeff Browne839a582010-04-22 18:58:52 -0700948} // namespace android
949
950#endif // _UI_INPUT_READER_H