blob: 2209cb80dcf2e991ca375879b48f80dec83a17de [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 Brown54bc2812010-06-15 01:31:58 -0700106 /* Intercepts a key event.
107 * The policy can use this method as an opportunity to perform power management functions
Jeff Browne57e8952010-07-23 21:28:06 -0700108 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-06-15 01:31:58 -0700109 *
110 * Returns a policy action constant such as ACTION_DISPATCH.
111 */
112 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
Jeff Browne57e8952010-07-23 21:28:06 -0700113 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700114
115 /* Intercepts a switch event.
116 * The policy can use this method as an opportunity to perform power management functions
Jeff Browne57e8952010-07-23 21:28:06 -0700117 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-06-15 01:31:58 -0700118 *
119 * Switches are not dispatched to applications so this method should
120 * usually return ACTION_NONE.
121 */
Jeff Browne57e8952010-07-23 21:28:06 -0700122 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
123 uint32_t& policyFlags) = 0;
124
125 /* Intercepts a generic touch, trackball or other event.
126 * The policy can use this method as an opportunity to perform power management functions
127 * and early event preprocessing such as updating policy flags.
128 *
129 * Returns a policy action constant such as ACTION_DISPATCH.
130 */
131 virtual int32_t interceptGeneric(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700132
133 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
134 * certain device whose screen currently is not all that good.
135 */
136 virtual bool filterTouchEvents() = 0;
137
138 /* Determines whether to turn on some hacks to improve touch interaction with another device
139 * where touch coordinate data can get corrupted.
140 */
141 virtual bool filterJumpyTouchEvents() = 0;
142
143 /* Gets the configured virtual key definitions for an input device. */
144 virtual void getVirtualKeyDefinitions(const String8& deviceName,
145 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
146
Jeff Brown38a7fab2010-08-30 03:02:23 -0700147 /* Gets the calibration for an input device. */
148 virtual void getInputDeviceCalibration(const String8& deviceName,
149 InputDeviceCalibration& outCalibration) = 0;
150
Jeff Brown54bc2812010-06-15 01:31:58 -0700151 /* Gets the excluded device names for the platform. */
152 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
153};
154
155
156/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700157class InputReaderInterface : public virtual RefBase {
158protected:
159 InputReaderInterface() { }
160 virtual ~InputReaderInterface() { }
161
162public:
Jeff Browna665ca82010-09-08 11:49:43 -0700163 /* Dumps the state of the input reader.
164 *
165 * This method may be called on any thread (usually by the input manager). */
166 virtual void dump(String8& dump) = 0;
167
Jeff Browne839a582010-04-22 18:58:52 -0700168 /* Runs a single iteration of the processing loop.
169 * Nominally reads and processes one incoming message from the EventHub.
170 *
171 * This method should be called on the input reader thread.
172 */
173 virtual void loopOnce() = 0;
174
Jeff Brown54bc2812010-06-15 01:31:58 -0700175 /* Gets the current input device configuration.
176 *
177 * This method may be called on any thread (usually by the input manager).
178 */
Jeff Browne57e8952010-07-23 21:28:06 -0700179 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700180
Jeff Browne57e8952010-07-23 21:28:06 -0700181 /* Gets information about the specified input device.
182 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
183 * was no such device.
184 *
185 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700186 */
Jeff Browne57e8952010-07-23 21:28:06 -0700187 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
188
189 /* Gets the list of all registered device ids. */
190 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
191
192 /* Query current input state. */
193 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
194 int32_t scanCode) = 0;
195 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
196 int32_t keyCode) = 0;
197 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
198 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700199
200 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700201 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
202 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
203};
204
205
206/* Internal interface used by individual input devices to access global input device state
207 * and parameters maintained by the input reader.
208 */
209class InputReaderContext {
210protected:
211 InputReaderContext() { }
212 virtual ~InputReaderContext() { }
213
214public:
215 virtual void updateGlobalMetaState() = 0;
216 virtual int32_t getGlobalMetaState() = 0;
217
218 virtual InputReaderPolicyInterface* getPolicy() = 0;
219 virtual InputDispatcherInterface* getDispatcher() = 0;
220 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700221};
222
Jeff Brown54bc2812010-06-15 01:31:58 -0700223
Jeff Browne839a582010-04-22 18:58:52 -0700224/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700225 * that it sends to the input dispatcher. Some functions of the input reader, such as early
226 * event filtering in low power states, are controlled by a separate policy object.
227 *
228 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700229 * Because the policy and dispatcher can potentially block or cause re-entrance into
230 * the input reader, the input reader never calls into other components while holding
Jeff Brownb51719b2010-07-29 18:18:33 -0700231 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700232 */
Jeff Browne57e8952010-07-23 21:28:06 -0700233class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700234public:
235 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700236 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700237 const sp<InputDispatcherInterface>& dispatcher);
238 virtual ~InputReader();
239
Jeff Browna665ca82010-09-08 11:49:43 -0700240 virtual void dump(String8& dump);
241
Jeff Browne839a582010-04-22 18:58:52 -0700242 virtual void loopOnce();
243
Jeff Browne57e8952010-07-23 21:28:06 -0700244 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700245
Jeff Browne57e8952010-07-23 21:28:06 -0700246 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
247 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700248
Jeff Browne57e8952010-07-23 21:28:06 -0700249 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
250 int32_t scanCode);
251 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
252 int32_t keyCode);
253 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
254 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700255
Jeff Browne57e8952010-07-23 21:28:06 -0700256 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
257 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700258
Jeff Browne839a582010-04-22 18:58:52 -0700259private:
Jeff Browne839a582010-04-22 18:58:52 -0700260 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700261 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700262 sp<InputDispatcherInterface> mDispatcher;
263
Jeff Browne57e8952010-07-23 21:28:06 -0700264 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
265 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
266 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
267
268 // This reader/writer lock guards the list of input devices.
269 // The writer lock must be held whenever the list of input devices is modified
270 // and then promptly released.
271 // The reader lock must be held whenever the list of input devices is traversed or an
272 // input device in the list is accessed.
273 // This lock only protects the registry and prevents inadvertent deletion of device objects
274 // that are in use. Individual devices are responsible for guarding their own internal state
275 // as needed for concurrent operation.
276 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700277 KeyedVector<int32_t, InputDevice*> mDevices;
278
Jeff Browne57e8952010-07-23 21:28:06 -0700279 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700280 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700281
Jeff Browne839a582010-04-22 18:58:52 -0700282 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700283 void removeDevice(nsecs_t when, int32_t deviceId);
284 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown54bc2812010-06-15 01:31:58 -0700285 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700286
Jeff Browne57e8952010-07-23 21:28:06 -0700287 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700288
Jeff Browne57e8952010-07-23 21:28:06 -0700289 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700290
Jeff Browne57e8952010-07-23 21:28:06 -0700291 // state management for all devices
292 Mutex mStateLock;
293
294 int32_t mGlobalMetaState;
295 virtual void updateGlobalMetaState();
296 virtual int32_t getGlobalMetaState();
297
298 InputConfiguration mInputConfiguration;
299 void updateInputConfiguration();
300
301 // state queries
302 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
303 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
304 GetStateFunc getStateFunc);
305 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
306 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700307};
308
309
310/* Reads raw events from the event hub and processes them, endlessly. */
311class InputReaderThread : public Thread {
312public:
313 InputReaderThread(const sp<InputReaderInterface>& reader);
314 virtual ~InputReaderThread();
315
316private:
317 sp<InputReaderInterface> mReader;
318
319 virtual bool threadLoop();
320};
321
Jeff Browne57e8952010-07-23 21:28:06 -0700322
323/* Represents the state of a single input device. */
324class InputDevice {
325public:
326 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
327 ~InputDevice();
328
329 inline InputReaderContext* getContext() { return mContext; }
330 inline int32_t getId() { return mId; }
331 inline const String8& getName() { return mName; }
332 inline uint32_t getSources() { return mSources; }
333
334 inline bool isIgnored() { return mMappers.isEmpty(); }
335
Jeff Brown26c94ff2010-09-30 14:33:04 -0700336 void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700337 void addMapper(InputMapper* mapper);
338 void configure();
339 void reset();
340 void process(const RawEvent* rawEvent);
341
342 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
343 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
344 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
345 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
346 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
347 const int32_t* keyCodes, uint8_t* outFlags);
348
349 int32_t getMetaState();
350
Jeff Brown38a7fab2010-08-30 03:02:23 -0700351 inline const InputDeviceCalibration& getCalibration() {
352 return mCalibration;
353 }
354
Jeff Browne57e8952010-07-23 21:28:06 -0700355private:
356 InputReaderContext* mContext;
357 int32_t mId;
358
359 Vector<InputMapper*> mMappers;
360
361 String8 mName;
362 uint32_t mSources;
363
364 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
365 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700366
367 InputDeviceCalibration mCalibration;
Jeff Browne57e8952010-07-23 21:28:06 -0700368};
369
370
371/* An input mapper transforms raw input events into cooked event data.
372 * A single input device can have multiple associated input mappers in order to interpret
373 * different classes of events.
374 */
375class InputMapper {
376public:
377 InputMapper(InputDevice* device);
378 virtual ~InputMapper();
379
380 inline InputDevice* getDevice() { return mDevice; }
381 inline int32_t getDeviceId() { return mDevice->getId(); }
382 inline const String8 getDeviceName() { return mDevice->getName(); }
383 inline InputReaderContext* getContext() { return mContext; }
384 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
385 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
386 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
387
388 virtual uint32_t getSources() = 0;
389 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700390 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700391 virtual void configure();
392 virtual void reset();
393 virtual void process(const RawEvent* rawEvent) = 0;
394
395 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
396 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
397 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
398 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
399 const int32_t* keyCodes, uint8_t* outFlags);
400
401 virtual int32_t getMetaState();
402
403protected:
404 InputDevice* mDevice;
405 InputReaderContext* mContext;
406
407 bool applyStandardPolicyActions(nsecs_t when, int32_t policyActions);
408};
409
410
411class SwitchInputMapper : public InputMapper {
412public:
413 SwitchInputMapper(InputDevice* device);
414 virtual ~SwitchInputMapper();
415
416 virtual uint32_t getSources();
417 virtual void process(const RawEvent* rawEvent);
418
419 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
420
421private:
422 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
423};
424
425
426class KeyboardInputMapper : public InputMapper {
427public:
428 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
429 int32_t keyboardType);
430 virtual ~KeyboardInputMapper();
431
432 virtual uint32_t getSources();
433 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700434 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700435 virtual void reset();
436 virtual void process(const RawEvent* rawEvent);
437
438 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
439 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
440 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
441 const int32_t* keyCodes, uint8_t* outFlags);
442
443 virtual int32_t getMetaState();
444
445private:
Jeff Brownb51719b2010-07-29 18:18:33 -0700446 Mutex mLock;
447
Jeff Browne57e8952010-07-23 21:28:06 -0700448 struct KeyDown {
449 int32_t keyCode;
450 int32_t scanCode;
451 };
452
453 int32_t mAssociatedDisplayId;
454 uint32_t mSources;
455 int32_t mKeyboardType;
456
Jeff Brownb51719b2010-07-29 18:18:33 -0700457 struct LockedState {
458 Vector<KeyDown> keyDowns; // keys that are down
459 int32_t metaState;
460 nsecs_t downTime; // time of most recent key down
461 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700462
Jeff Brownb51719b2010-07-29 18:18:33 -0700463 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700464
465 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700466
Jeff Browne57e8952010-07-23 21:28:06 -0700467 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
468 uint32_t policyFlags);
Jeff Brownb51719b2010-07-29 18:18:33 -0700469 void applyPolicyAndDispatch(nsecs_t when, uint32_t policyFlags,
470 bool down, int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Browne57e8952010-07-23 21:28:06 -0700471
Jeff Brownb51719b2010-07-29 18:18:33 -0700472 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -0700473};
474
475
476class TrackballInputMapper : public InputMapper {
477public:
478 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
479 virtual ~TrackballInputMapper();
480
481 virtual uint32_t getSources();
482 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700483 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700484 virtual void reset();
485 virtual void process(const RawEvent* rawEvent);
486
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700487 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
488
Jeff Browne57e8952010-07-23 21:28:06 -0700489private:
490 // Amount that trackball needs to move in order to generate a key event.
491 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
492
Jeff Brownb51719b2010-07-29 18:18:33 -0700493 Mutex mLock;
494
Jeff Browne57e8952010-07-23 21:28:06 -0700495 int32_t mAssociatedDisplayId;
496
497 struct Accumulator {
498 enum {
499 FIELD_BTN_MOUSE = 1,
500 FIELD_REL_X = 2,
501 FIELD_REL_Y = 4
502 };
503
504 uint32_t fields;
505
506 bool btnMouse;
507 int32_t relX;
508 int32_t relY;
509
510 inline void clear() {
511 fields = 0;
512 }
Jeff Browne57e8952010-07-23 21:28:06 -0700513 } mAccumulator;
514
Jeff Browne57e8952010-07-23 21:28:06 -0700515 float mXScale;
516 float mYScale;
517 float mXPrecision;
518 float mYPrecision;
519
Jeff Brownb51719b2010-07-29 18:18:33 -0700520 struct LockedState {
521 bool down;
522 nsecs_t downTime;
523 } mLocked;
524
525 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700526
527 void sync(nsecs_t when);
Jeff Brownb51719b2010-07-29 18:18:33 -0700528 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
529 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Browne57e8952010-07-23 21:28:06 -0700530};
531
532
533class TouchInputMapper : public InputMapper {
534public:
535 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
536 virtual ~TouchInputMapper();
537
538 virtual uint32_t getSources();
539 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700540 virtual void dump(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700541 virtual void configure();
542 virtual void reset();
543
544 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
545 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
546 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
547 const int32_t* keyCodes, uint8_t* outFlags);
548
549protected:
Jeff Brownb51719b2010-07-29 18:18:33 -0700550 Mutex mLock;
551
Jeff Browne57e8952010-07-23 21:28:06 -0700552 struct VirtualKey {
553 int32_t keyCode;
554 int32_t scanCode;
555 uint32_t flags;
556
557 // computed hit box, specified in touch screen coords based on known display size
558 int32_t hitLeft;
559 int32_t hitTop;
560 int32_t hitRight;
561 int32_t hitBottom;
562
563 inline bool isHit(int32_t x, int32_t y) const {
564 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
565 }
566 };
567
Jeff Brown38a7fab2010-08-30 03:02:23 -0700568 // Raw data for a single pointer.
Jeff Browne57e8952010-07-23 21:28:06 -0700569 struct PointerData {
570 uint32_t id;
571 int32_t x;
572 int32_t y;
573 int32_t pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700574 int32_t touchMajor;
575 int32_t touchMinor;
576 int32_t toolMajor;
577 int32_t toolMinor;
578 int32_t orientation;
579 };
580
Jeff Brown38a7fab2010-08-30 03:02:23 -0700581 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Browne57e8952010-07-23 21:28:06 -0700582 struct TouchData {
583 uint32_t pointerCount;
584 PointerData pointers[MAX_POINTERS];
585 BitSet32 idBits;
586 uint32_t idToIndex[MAX_POINTER_ID + 1];
587
588 void copyFrom(const TouchData& other) {
589 pointerCount = other.pointerCount;
590 idBits = other.idBits;
591
592 for (uint32_t i = 0; i < pointerCount; i++) {
593 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700594
595 int id = pointers[i].id;
596 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700597 }
598 }
599
600 inline void clear() {
601 pointerCount = 0;
602 idBits.clear();
603 }
604 };
605
606 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700607
608 // Immutable configuration parameters.
609 struct Parameters {
610 bool useBadTouchFilter;
611 bool useJumpyTouchFilter;
612 bool useAveragingTouchFilter;
613 } mParameters;
614
Jeff Brown38a7fab2010-08-30 03:02:23 -0700615 // Immutable calibration parameters in parsed form.
616 struct Calibration {
617 // Touch Area
618 enum TouchAreaCalibration {
619 TOUCH_AREA_CALIBRATION_DEFAULT,
620 TOUCH_AREA_CALIBRATION_NONE,
621 TOUCH_AREA_CALIBRATION_GEOMETRIC,
622 TOUCH_AREA_CALIBRATION_PRESSURE,
623 };
624
625 TouchAreaCalibration touchAreaCalibration;
626
627 // Tool Area
628 enum ToolAreaCalibration {
629 TOOL_AREA_CALIBRATION_DEFAULT,
630 TOOL_AREA_CALIBRATION_NONE,
631 TOOL_AREA_CALIBRATION_GEOMETRIC,
632 TOOL_AREA_CALIBRATION_LINEAR,
633 };
634
635 ToolAreaCalibration toolAreaCalibration;
636 bool haveToolAreaLinearScale;
637 float toolAreaLinearScale;
638 bool haveToolAreaLinearBias;
639 float toolAreaLinearBias;
640 bool haveToolAreaIsSummed;
641 int32_t toolAreaIsSummed;
642
643 // Pressure
644 enum PressureCalibration {
645 PRESSURE_CALIBRATION_DEFAULT,
646 PRESSURE_CALIBRATION_NONE,
647 PRESSURE_CALIBRATION_PHYSICAL,
648 PRESSURE_CALIBRATION_AMPLITUDE,
649 };
650 enum PressureSource {
651 PRESSURE_SOURCE_DEFAULT,
652 PRESSURE_SOURCE_PRESSURE,
653 PRESSURE_SOURCE_TOUCH,
654 };
655
656 PressureCalibration pressureCalibration;
657 PressureSource pressureSource;
658 bool havePressureScale;
659 float pressureScale;
660
661 // Size
662 enum SizeCalibration {
663 SIZE_CALIBRATION_DEFAULT,
664 SIZE_CALIBRATION_NONE,
665 SIZE_CALIBRATION_NORMALIZED,
666 };
667
668 SizeCalibration sizeCalibration;
669
670 // Orientation
671 enum OrientationCalibration {
672 ORIENTATION_CALIBRATION_DEFAULT,
673 ORIENTATION_CALIBRATION_NONE,
674 ORIENTATION_CALIBRATION_INTERPOLATED,
675 };
676
677 OrientationCalibration orientationCalibration;
678 } mCalibration;
679
680 // Raw axis information from the driver.
681 struct RawAxes {
Jeff Browne57e8952010-07-23 21:28:06 -0700682 RawAbsoluteAxisInfo x;
683 RawAbsoluteAxisInfo y;
684 RawAbsoluteAxisInfo pressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700685 RawAbsoluteAxisInfo touchMajor;
686 RawAbsoluteAxisInfo touchMinor;
687 RawAbsoluteAxisInfo toolMajor;
688 RawAbsoluteAxisInfo toolMinor;
689 RawAbsoluteAxisInfo orientation;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700690 } mRawAxes;
Jeff Browne57e8952010-07-23 21:28:06 -0700691
Jeff Brownb51719b2010-07-29 18:18:33 -0700692 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700693 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700694 TouchData mLastTouch;
695
696 // The time the primary pointer last went down.
697 nsecs_t mDownTime;
698
Jeff Brownb51719b2010-07-29 18:18:33 -0700699 struct LockedState {
700 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700701
Jeff Brownb51719b2010-07-29 18:18:33 -0700702 // The surface orientation and width and height set by configureSurfaceLocked().
703 int32_t surfaceOrientation;
704 int32_t surfaceWidth, surfaceHeight;
705
706 // Translation and scaling factors, orientation-independent.
707 int32_t xOrigin;
708 float xScale;
709 float xPrecision;
710
711 int32_t yOrigin;
712 float yScale;
713 float yPrecision;
714
Jeff Brown38a7fab2010-08-30 03:02:23 -0700715 float geometricScale;
716
717 float toolAreaLinearScale;
718 float toolAreaLinearBias;
719
Jeff Brownb51719b2010-07-29 18:18:33 -0700720 float pressureScale;
721
Jeff Brownb51719b2010-07-29 18:18:33 -0700722 float sizeScale;
723
724 float orientationScale;
725
726 // Oriented motion ranges for input device info.
727 struct OrientedRanges {
728 InputDeviceInfo::MotionRange x;
729 InputDeviceInfo::MotionRange y;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700730
731 bool havePressure;
Jeff Brownb51719b2010-07-29 18:18:33 -0700732 InputDeviceInfo::MotionRange pressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700733
734 bool haveSize;
Jeff Brownb51719b2010-07-29 18:18:33 -0700735 InputDeviceInfo::MotionRange size;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700736
737 bool haveTouchArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700738 InputDeviceInfo::MotionRange touchMajor;
739 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700740
741 bool haveToolArea;
Jeff Brownb51719b2010-07-29 18:18:33 -0700742 InputDeviceInfo::MotionRange toolMajor;
743 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700744
745 bool haveOrientation;
Jeff Brownb51719b2010-07-29 18:18:33 -0700746 InputDeviceInfo::MotionRange orientation;
747 } orientedRanges;
748
749 // Oriented dimensions and precision.
750 float orientedSurfaceWidth, orientedSurfaceHeight;
751 float orientedXPrecision, orientedYPrecision;
752
753 struct CurrentVirtualKeyState {
754 bool down;
755 nsecs_t downTime;
756 int32_t keyCode;
757 int32_t scanCode;
758 } currentVirtualKey;
759 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700760
Jeff Brown38a7fab2010-08-30 03:02:23 -0700761 virtual void configureParameters();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700762 virtual void dumpParameters(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700763 virtual void configureRawAxes();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700764 virtual void dumpRawAxes(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700765 virtual bool configureSurfaceLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700766 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brownb51719b2010-07-29 18:18:33 -0700767 virtual void configureVirtualKeysLocked();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700768 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700769 virtual void parseCalibration();
770 virtual void resolveCalibration();
Jeff Brown26c94ff2010-09-30 14:33:04 -0700771 virtual void dumpCalibration(String8& dump);
Jeff Browne57e8952010-07-23 21:28:06 -0700772
773 enum TouchResult {
774 // Dispatch the touch normally.
775 DISPATCH_TOUCH,
776 // Do not dispatch the touch, but keep tracking the current stroke.
777 SKIP_TOUCH,
778 // Do not dispatch the touch, and drop all information associated with the current stoke
779 // so the next movement will appear as a new down.
780 DROP_STROKE
781 };
782
783 void syncTouch(nsecs_t when, bool havePointerIds);
784
785private:
786 /* Maximum number of historical samples to average. */
787 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
788
789 /* Slop distance for jumpy pointer detection.
790 * The vertical range of the screen divided by this is our epsilon value. */
791 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
792
793 /* Number of jumpy points to drop for touchscreens that need it. */
794 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
795 static const uint32_t JUMPY_DROP_LIMIT = 3;
796
797 /* Maximum squared distance for averaging.
798 * If moving farther than this, turn of averaging to avoid lag in response. */
799 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
800
801 struct AveragingTouchFilterState {
802 // Individual history tracks are stored by pointer id
803 uint32_t historyStart[MAX_POINTERS];
804 uint32_t historyEnd[MAX_POINTERS];
805 struct {
806 struct {
807 int32_t x;
808 int32_t y;
809 int32_t pressure;
810 } pointers[MAX_POINTERS];
811 } historyData[AVERAGING_HISTORY_SIZE];
812 } mAveragingTouchFilter;
813
Jeff Brownd64c8552010-08-17 20:38:35 -0700814 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700815 uint32_t jumpyPointsDropped;
816 } mJumpyTouchFilter;
817
818 struct PointerDistanceHeapElement {
819 uint32_t currentPointerIndex : 8;
820 uint32_t lastPointerIndex : 8;
821 uint64_t distance : 48; // squared distance
822 };
823
Jeff Brownb51719b2010-07-29 18:18:33 -0700824 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700825
826 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
827 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
828 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown38a7fab2010-08-30 03:02:23 -0700829 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
830 int32_t motionEventAction);
Jeff Browne57e8952010-07-23 21:28:06 -0700831
Jeff Brownb51719b2010-07-29 18:18:33 -0700832 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
833 int32_t keyEventAction, int32_t keyEventFlags,
834 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
835
836 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
837 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700838
839 bool applyBadTouchFilter();
840 bool applyJumpyTouchFilter();
841 void applyAveragingTouchFilter();
842 void calculatePointerIds();
843};
844
845
846class SingleTouchInputMapper : public TouchInputMapper {
847public:
848 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
849 virtual ~SingleTouchInputMapper();
850
851 virtual void reset();
852 virtual void process(const RawEvent* rawEvent);
853
854protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700855 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700856
857private:
858 struct Accumulator {
859 enum {
860 FIELD_BTN_TOUCH = 1,
861 FIELD_ABS_X = 2,
862 FIELD_ABS_Y = 4,
863 FIELD_ABS_PRESSURE = 8,
864 FIELD_ABS_TOOL_WIDTH = 16
865 };
866
867 uint32_t fields;
868
869 bool btnTouch;
870 int32_t absX;
871 int32_t absY;
872 int32_t absPressure;
873 int32_t absToolWidth;
874
875 inline void clear() {
876 fields = 0;
877 }
Jeff Browne57e8952010-07-23 21:28:06 -0700878 } mAccumulator;
879
880 bool mDown;
881 int32_t mX;
882 int32_t mY;
883 int32_t mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -0700884 int32_t mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -0700885
886 void initialize();
887
888 void sync(nsecs_t when);
889};
890
891
892class MultiTouchInputMapper : public TouchInputMapper {
893public:
894 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
895 virtual ~MultiTouchInputMapper();
896
897 virtual void reset();
898 virtual void process(const RawEvent* rawEvent);
899
900protected:
Jeff Brown38a7fab2010-08-30 03:02:23 -0700901 virtual void configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -0700902
903private:
904 struct Accumulator {
905 enum {
906 FIELD_ABS_MT_POSITION_X = 1,
907 FIELD_ABS_MT_POSITION_Y = 2,
908 FIELD_ABS_MT_TOUCH_MAJOR = 4,
909 FIELD_ABS_MT_TOUCH_MINOR = 8,
910 FIELD_ABS_MT_WIDTH_MAJOR = 16,
911 FIELD_ABS_MT_WIDTH_MINOR = 32,
912 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700913 FIELD_ABS_MT_TRACKING_ID = 128,
914 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700915 };
916
917 uint32_t pointerCount;
918 struct Pointer {
919 uint32_t fields;
920
921 int32_t absMTPositionX;
922 int32_t absMTPositionY;
923 int32_t absMTTouchMajor;
924 int32_t absMTTouchMinor;
925 int32_t absMTWidthMajor;
926 int32_t absMTWidthMinor;
927 int32_t absMTOrientation;
928 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700929 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700930
931 inline void clear() {
932 fields = 0;
933 }
934 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
935
936 inline void clear() {
937 pointerCount = 0;
938 pointers[0].clear();
939 }
Jeff Browne57e8952010-07-23 21:28:06 -0700940 } mAccumulator;
941
942 void initialize();
943
944 void sync(nsecs_t when);
945};
946
Jeff Browne839a582010-04-22 18:58:52 -0700947} // namespace android
948
949#endif // _UI_INPUT_READER_H