blob: 7a089a45a1aff1c2dd8a22196f384f0c8c3ae062 [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070033namespace android {
34
Jeff Brown6d0fec22010-07-23 21:28:06 -070035class InputDevice;
36class InputMapper;
37
Jeff Brown8d608662010-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 Brown6d0fec22010-07-23 21:28:06 -070066
Jeff Brown9c3cda02010-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,
98
99 // The input dispatcher should perform special filtering in preparation for
100 // a pending app switch.
101 ACTION_APP_SWITCH_COMING = 0x00000002,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700102 };
103
Jeff Brown9c3cda02010-06-15 01:31:58 -0700104 /* Gets information about the display with the specified id.
105 * Returns true if the display info is available, false otherwise.
106 */
107 virtual bool getDisplayInfo(int32_t displayId,
108 int32_t* width, int32_t* height, int32_t* orientation) = 0;
109
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700110 /* Provides feedback for a virtual key down.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700111 */
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700112 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700113
114 /* Intercepts a key event.
115 * The policy can use this method as an opportunity to perform power management functions
Jeff Brown6d0fec22010-07-23 21:28:06 -0700116 * and early event preprocessing such as updating policy flags.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700117 *
118 * Returns a policy action constant such as ACTION_DISPATCH.
119 */
120 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700121 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700122
123 /* Intercepts a switch event.
124 * The policy can use this method as an opportunity to perform power management functions
Jeff Brown6d0fec22010-07-23 21:28:06 -0700125 * and early event preprocessing such as updating policy flags.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700126 *
127 * Switches are not dispatched to applications so this method should
128 * usually return ACTION_NONE.
129 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700130 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
131 uint32_t& policyFlags) = 0;
132
133 /* Intercepts a generic touch, trackball or other event.
134 * The policy can use this method as an opportunity to perform power management functions
135 * and early event preprocessing such as updating policy flags.
136 *
137 * Returns a policy action constant such as ACTION_DISPATCH.
138 */
139 virtual int32_t interceptGeneric(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700140
141 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
142 * certain device whose screen currently is not all that good.
143 */
144 virtual bool filterTouchEvents() = 0;
145
146 /* Determines whether to turn on some hacks to improve touch interaction with another device
147 * where touch coordinate data can get corrupted.
148 */
149 virtual bool filterJumpyTouchEvents() = 0;
150
151 /* Gets the configured virtual key definitions for an input device. */
152 virtual void getVirtualKeyDefinitions(const String8& deviceName,
153 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
154
Jeff Brown8d608662010-08-30 03:02:23 -0700155 /* Gets the calibration for an input device. */
156 virtual void getInputDeviceCalibration(const String8& deviceName,
157 InputDeviceCalibration& outCalibration) = 0;
158
Jeff Brown9c3cda02010-06-15 01:31:58 -0700159 /* Gets the excluded device names for the platform. */
160 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
161};
162
163
164/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700165class InputReaderInterface : public virtual RefBase {
166protected:
167 InputReaderInterface() { }
168 virtual ~InputReaderInterface() { }
169
170public:
171 /* Runs a single iteration of the processing loop.
172 * Nominally reads and processes one incoming message from the EventHub.
173 *
174 * This method should be called on the input reader thread.
175 */
176 virtual void loopOnce() = 0;
177
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178 /* Gets the current input device configuration.
179 *
180 * This method may be called on any thread (usually by the input manager).
181 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700182 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700183
Jeff Brown6d0fec22010-07-23 21:28:06 -0700184 /* Gets information about the specified input device.
185 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
186 * was no such device.
187 *
188 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700190 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
191
192 /* Gets the list of all registered device ids. */
193 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
194
195 /* Query current input state. */
196 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
197 int32_t scanCode) = 0;
198 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
199 int32_t keyCode) = 0;
200 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
201 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700202
203 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700204 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
205 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
206};
207
208
209/* Internal interface used by individual input devices to access global input device state
210 * and parameters maintained by the input reader.
211 */
212class InputReaderContext {
213protected:
214 InputReaderContext() { }
215 virtual ~InputReaderContext() { }
216
217public:
218 virtual void updateGlobalMetaState() = 0;
219 virtual int32_t getGlobalMetaState() = 0;
220
221 virtual InputReaderPolicyInterface* getPolicy() = 0;
222 virtual InputDispatcherInterface* getDispatcher() = 0;
223 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700224};
225
Jeff Brown9c3cda02010-06-15 01:31:58 -0700226
Jeff Brown46b9ac02010-04-22 18:58:52 -0700227/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700228 * that it sends to the input dispatcher. Some functions of the input reader, such as early
229 * event filtering in low power states, are controlled by a separate policy object.
230 *
231 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700232 * Because the policy and dispatcher can potentially block or cause re-entrance into
233 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700234 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700235 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700236class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700237public:
238 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700239 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 const sp<InputDispatcherInterface>& dispatcher);
241 virtual ~InputReader();
242
243 virtual void loopOnce();
244
Jeff Brown6d0fec22010-07-23 21:28:06 -0700245 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246
Jeff Brown6d0fec22010-07-23 21:28:06 -0700247 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
248 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700249
Jeff Brown6d0fec22010-07-23 21:28:06 -0700250 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
251 int32_t scanCode);
252 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
253 int32_t keyCode);
254 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
255 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700256
Jeff Brown6d0fec22010-07-23 21:28:06 -0700257 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
258 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700259
Jeff Brown46b9ac02010-04-22 18:58:52 -0700260private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700261 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700262 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700263 sp<InputDispatcherInterface> mDispatcher;
264
Jeff Brown6d0fec22010-07-23 21:28:06 -0700265 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
266 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
267 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
268
269 // This reader/writer lock guards the list of input devices.
270 // The writer lock must be held whenever the list of input devices is modified
271 // and then promptly released.
272 // The reader lock must be held whenever the list of input devices is traversed or an
273 // input device in the list is accessed.
274 // This lock only protects the registry and prevents inadvertent deletion of device objects
275 // that are in use. Individual devices are responsible for guarding their own internal state
276 // as needed for concurrent operation.
277 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700278 KeyedVector<int32_t, InputDevice*> mDevices;
279
Jeff Brown6d0fec22010-07-23 21:28:06 -0700280 // low-level input event decoding and device management
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700282
Jeff Brown46b9ac02010-04-22 18:58:52 -0700283 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700284 void removeDevice(nsecs_t when, int32_t deviceId);
285 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700286 void configureExcludedDevices();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287
Jeff Brown6d0fec22010-07-23 21:28:06 -0700288 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700289
Jeff Brown6d0fec22010-07-23 21:28:06 -0700290 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700291
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292 // state management for all devices
293 Mutex mStateLock;
294
295 int32_t mGlobalMetaState;
296 virtual void updateGlobalMetaState();
297 virtual int32_t getGlobalMetaState();
298
299 InputConfiguration mInputConfiguration;
300 void updateInputConfiguration();
301
302 // state queries
303 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
304 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
305 GetStateFunc getStateFunc);
306 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
307 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700308};
309
310
311/* Reads raw events from the event hub and processes them, endlessly. */
312class InputReaderThread : public Thread {
313public:
314 InputReaderThread(const sp<InputReaderInterface>& reader);
315 virtual ~InputReaderThread();
316
317private:
318 sp<InputReaderInterface> mReader;
319
320 virtual bool threadLoop();
321};
322
Jeff Brown6d0fec22010-07-23 21:28:06 -0700323
324/* Represents the state of a single input device. */
325class InputDevice {
326public:
327 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
328 ~InputDevice();
329
330 inline InputReaderContext* getContext() { return mContext; }
331 inline int32_t getId() { return mId; }
332 inline const String8& getName() { return mName; }
333 inline uint32_t getSources() { return mSources; }
334
335 inline bool isIgnored() { return mMappers.isEmpty(); }
336
337 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 Brown8d608662010-08-30 03:02:23 -0700351 inline const InputDeviceCalibration& getCalibration() {
352 return mCalibration;
353 }
354
Jeff Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700366
367 InputDeviceCalibration mCalibration;
Jeff Brown6d0fec22010-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);
390 virtual void configure();
391 virtual void reset();
392 virtual void process(const RawEvent* rawEvent) = 0;
393
394 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
395 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
396 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
397 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
398 const int32_t* keyCodes, uint8_t* outFlags);
399
400 virtual int32_t getMetaState();
401
402protected:
403 InputDevice* mDevice;
404 InputReaderContext* mContext;
405
406 bool applyStandardPolicyActions(nsecs_t when, int32_t policyActions);
407};
408
409
410class SwitchInputMapper : public InputMapper {
411public:
412 SwitchInputMapper(InputDevice* device);
413 virtual ~SwitchInputMapper();
414
415 virtual uint32_t getSources();
416 virtual void process(const RawEvent* rawEvent);
417
418 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
419
420private:
421 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
422};
423
424
425class KeyboardInputMapper : public InputMapper {
426public:
427 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
428 int32_t keyboardType);
429 virtual ~KeyboardInputMapper();
430
431 virtual uint32_t getSources();
432 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
433 virtual void reset();
434 virtual void process(const RawEvent* rawEvent);
435
436 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
437 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
438 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
439 const int32_t* keyCodes, uint8_t* outFlags);
440
441 virtual int32_t getMetaState();
442
443private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700444 Mutex mLock;
445
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446 struct KeyDown {
447 int32_t keyCode;
448 int32_t scanCode;
449 };
450
451 int32_t mAssociatedDisplayId;
452 uint32_t mSources;
453 int32_t mKeyboardType;
454
Jeff Brown6328cdc2010-07-29 18:18:33 -0700455 struct LockedState {
456 Vector<KeyDown> keyDowns; // keys that are down
457 int32_t metaState;
458 nsecs_t downTime; // time of most recent key down
459 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460
Jeff Brown6328cdc2010-07-29 18:18:33 -0700461 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462
463 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700464
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
466 uint32_t policyFlags);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700467 void applyPolicyAndDispatch(nsecs_t when, uint32_t policyFlags,
468 bool down, int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700469
Jeff Brown6328cdc2010-07-29 18:18:33 -0700470 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471};
472
473
474class TrackballInputMapper : public InputMapper {
475public:
476 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
477 virtual ~TrackballInputMapper();
478
479 virtual uint32_t getSources();
480 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
481 virtual void reset();
482 virtual void process(const RawEvent* rawEvent);
483
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700484 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
485
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486private:
487 // Amount that trackball needs to move in order to generate a key event.
488 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
489
Jeff Brown6328cdc2010-07-29 18:18:33 -0700490 Mutex mLock;
491
Jeff Brown6d0fec22010-07-23 21:28:06 -0700492 int32_t mAssociatedDisplayId;
493
494 struct Accumulator {
495 enum {
496 FIELD_BTN_MOUSE = 1,
497 FIELD_REL_X = 2,
498 FIELD_REL_Y = 4
499 };
500
501 uint32_t fields;
502
503 bool btnMouse;
504 int32_t relX;
505 int32_t relY;
506
507 inline void clear() {
508 fields = 0;
509 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700510 } mAccumulator;
511
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512 float mXScale;
513 float mYScale;
514 float mXPrecision;
515 float mYPrecision;
516
Jeff Brown6328cdc2010-07-29 18:18:33 -0700517 struct LockedState {
518 bool down;
519 nsecs_t downTime;
520 } mLocked;
521
522 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700523
524 void sync(nsecs_t when);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700525 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
526 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527};
528
529
530class TouchInputMapper : public InputMapper {
531public:
532 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
533 virtual ~TouchInputMapper();
534
535 virtual uint32_t getSources();
536 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
537 virtual void configure();
538 virtual void reset();
539
540 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
541 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
542 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
543 const int32_t* keyCodes, uint8_t* outFlags);
544
545protected:
546 /* Maximum pointer id value supported.
547 * (This is limited by our use of BitSet32 to track pointer assignments.) */
548 static const uint32_t MAX_POINTER_ID = 31;
549
Jeff Brown6328cdc2010-07-29 18:18:33 -0700550 Mutex mLock;
551
Jeff Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700568 // Raw data for a single pointer.
Jeff Brown6d0fec22010-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 Brown6d0fec22010-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 Brown8d608662010-08-30 03:02:23 -0700581 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-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 Brown9e2ad362010-07-30 19:20:11 -0700594
595 int id = pointers[i].id;
596 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-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 Brown6d0fec22010-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 Brown8d608662010-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 Brown6d0fec22010-07-23 21:28:06 -0700682 RawAbsoluteAxisInfo x;
683 RawAbsoluteAxisInfo y;
684 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700685 RawAbsoluteAxisInfo touchMajor;
686 RawAbsoluteAxisInfo touchMinor;
687 RawAbsoluteAxisInfo toolMajor;
688 RawAbsoluteAxisInfo toolMinor;
689 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700690 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700691
Jeff Brown6328cdc2010-07-29 18:18:33 -0700692 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700693 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700694 TouchData mLastTouch;
695
696 // The time the primary pointer last went down.
697 nsecs_t mDownTime;
698
Jeff Brown6328cdc2010-07-29 18:18:33 -0700699 struct LockedState {
700 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700701
Jeff Brown6328cdc2010-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 Brown8d608662010-08-30 03:02:23 -0700715 float geometricScale;
716
717 float toolAreaLinearScale;
718 float toolAreaLinearBias;
719
Jeff Brown6328cdc2010-07-29 18:18:33 -0700720 float pressureScale;
721
Jeff Brown6328cdc2010-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 Brown8d608662010-08-30 03:02:23 -0700730
731 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700732 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700733
734 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700735 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700736
737 bool haveTouchArea;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700738 InputDeviceInfo::MotionRange touchMajor;
739 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700740
741 bool haveToolArea;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700742 InputDeviceInfo::MotionRange toolMajor;
743 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700744
745 bool haveOrientation;
Jeff Brown6328cdc2010-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 Brown6d0fec22010-07-23 21:28:06 -0700760
Jeff Brown8d608662010-08-30 03:02:23 -0700761 virtual void configureParameters();
762 virtual void configureRawAxes();
763 virtual void logRawAxes();
Jeff Brown6328cdc2010-07-29 18:18:33 -0700764 virtual bool configureSurfaceLocked();
765 virtual void configureVirtualKeysLocked();
Jeff Brown8d608662010-08-30 03:02:23 -0700766 virtual void parseCalibration();
767 virtual void resolveCalibration();
768 virtual void logCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700769
770 enum TouchResult {
771 // Dispatch the touch normally.
772 DISPATCH_TOUCH,
773 // Do not dispatch the touch, but keep tracking the current stroke.
774 SKIP_TOUCH,
775 // Do not dispatch the touch, and drop all information associated with the current stoke
776 // so the next movement will appear as a new down.
777 DROP_STROKE
778 };
779
780 void syncTouch(nsecs_t when, bool havePointerIds);
781
782private:
783 /* Maximum number of historical samples to average. */
784 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
785
786 /* Slop distance for jumpy pointer detection.
787 * The vertical range of the screen divided by this is our epsilon value. */
788 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
789
790 /* Number of jumpy points to drop for touchscreens that need it. */
791 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
792 static const uint32_t JUMPY_DROP_LIMIT = 3;
793
794 /* Maximum squared distance for averaging.
795 * If moving farther than this, turn of averaging to avoid lag in response. */
796 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
797
798 struct AveragingTouchFilterState {
799 // Individual history tracks are stored by pointer id
800 uint32_t historyStart[MAX_POINTERS];
801 uint32_t historyEnd[MAX_POINTERS];
802 struct {
803 struct {
804 int32_t x;
805 int32_t y;
806 int32_t pressure;
807 } pointers[MAX_POINTERS];
808 } historyData[AVERAGING_HISTORY_SIZE];
809 } mAveragingTouchFilter;
810
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700811 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700812 uint32_t jumpyPointsDropped;
813 } mJumpyTouchFilter;
814
815 struct PointerDistanceHeapElement {
816 uint32_t currentPointerIndex : 8;
817 uint32_t lastPointerIndex : 8;
818 uint64_t distance : 48; // squared distance
819 };
820
Jeff Brown6328cdc2010-07-29 18:18:33 -0700821 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700822
823 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
824 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
825 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700826 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
827 int32_t motionEventAction);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828
Jeff Brown6328cdc2010-07-29 18:18:33 -0700829 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
830 int32_t keyEventAction, int32_t keyEventFlags,
831 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
832
833 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
834 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700835
836 bool applyBadTouchFilter();
837 bool applyJumpyTouchFilter();
838 void applyAveragingTouchFilter();
839 void calculatePointerIds();
840};
841
842
843class SingleTouchInputMapper : public TouchInputMapper {
844public:
845 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
846 virtual ~SingleTouchInputMapper();
847
848 virtual void reset();
849 virtual void process(const RawEvent* rawEvent);
850
851protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700852 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700853
854private:
855 struct Accumulator {
856 enum {
857 FIELD_BTN_TOUCH = 1,
858 FIELD_ABS_X = 2,
859 FIELD_ABS_Y = 4,
860 FIELD_ABS_PRESSURE = 8,
861 FIELD_ABS_TOOL_WIDTH = 16
862 };
863
864 uint32_t fields;
865
866 bool btnTouch;
867 int32_t absX;
868 int32_t absY;
869 int32_t absPressure;
870 int32_t absToolWidth;
871
872 inline void clear() {
873 fields = 0;
874 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700875 } mAccumulator;
876
877 bool mDown;
878 int32_t mX;
879 int32_t mY;
880 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700881 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882
883 void initialize();
884
885 void sync(nsecs_t when);
886};
887
888
889class MultiTouchInputMapper : public TouchInputMapper {
890public:
891 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
892 virtual ~MultiTouchInputMapper();
893
894 virtual void reset();
895 virtual void process(const RawEvent* rawEvent);
896
897protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700898 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899
900private:
901 struct Accumulator {
902 enum {
903 FIELD_ABS_MT_POSITION_X = 1,
904 FIELD_ABS_MT_POSITION_Y = 2,
905 FIELD_ABS_MT_TOUCH_MAJOR = 4,
906 FIELD_ABS_MT_TOUCH_MINOR = 8,
907 FIELD_ABS_MT_WIDTH_MAJOR = 16,
908 FIELD_ABS_MT_WIDTH_MINOR = 32,
909 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700910 FIELD_ABS_MT_TRACKING_ID = 128,
911 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700912 };
913
914 uint32_t pointerCount;
915 struct Pointer {
916 uint32_t fields;
917
918 int32_t absMTPositionX;
919 int32_t absMTPositionY;
920 int32_t absMTTouchMajor;
921 int32_t absMTTouchMinor;
922 int32_t absMTWidthMajor;
923 int32_t absMTWidthMinor;
924 int32_t absMTOrientation;
925 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700926 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700927
928 inline void clear() {
929 fields = 0;
930 }
931 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
932
933 inline void clear() {
934 pointerCount = 0;
935 pointers[0].clear();
936 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700937 } mAccumulator;
938
939 void initialize();
940
941 void sync(nsecs_t when);
942};
943
Jeff Brown46b9ac02010-04-22 18:58:52 -0700944} // namespace android
945
946#endif // _UI_INPUT_READER_H