blob: d7ec8ea64dfb36dfa5940db023ac07243ff3aa4b [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
38
Jeff Brown54bc2812010-06-15 01:31:58 -070039/*
40 * Input reader policy interface.
41 *
42 * The input reader policy is used by the input reader to interact with the Window Manager
43 * and other system components.
44 *
45 * The actual implementation is partially supported by callbacks into the DVM
46 * via JNI. This interface is also mocked in the unit tests.
47 */
48class InputReaderPolicyInterface : public virtual RefBase {
49protected:
50 InputReaderPolicyInterface() { }
51 virtual ~InputReaderPolicyInterface() { }
52
53public:
54 /* Display orientations. */
55 enum {
56 ROTATION_0 = 0,
57 ROTATION_90 = 1,
58 ROTATION_180 = 2,
59 ROTATION_270 = 3
60 };
61
62 /* Actions returned by interceptXXX methods. */
63 enum {
64 // The input dispatcher should do nothing and discard the input unless other
65 // flags are set.
66 ACTION_NONE = 0,
67
68 // The input dispatcher should dispatch the input to the application.
69 ACTION_DISPATCH = 0x00000001,
70
71 // The input dispatcher should perform special filtering in preparation for
72 // a pending app switch.
73 ACTION_APP_SWITCH_COMING = 0x00000002,
Jeff Brown54bc2812010-06-15 01:31:58 -070074 };
75
76 /* Describes a virtual key. */
77 struct VirtualKeyDefinition {
78 int32_t scanCode;
79
80 // configured position data, specified in display coords
81 int32_t centerX;
82 int32_t centerY;
83 int32_t width;
84 int32_t height;
85 };
86
87 /* Gets information about the display with the specified id.
88 * Returns true if the display info is available, false otherwise.
89 */
90 virtual bool getDisplayInfo(int32_t displayId,
91 int32_t* width, int32_t* height, int32_t* orientation) = 0;
92
Jeff Brownf16c26d2010-07-02 15:37:36 -070093 /* Provides feedback for a virtual key down.
Jeff Brown54bc2812010-06-15 01:31:58 -070094 */
Jeff Brownf16c26d2010-07-02 15:37:36 -070095 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -070096
97 /* Intercepts a key event.
98 * The policy can use this method as an opportunity to perform power management functions
Jeff Browne57e8952010-07-23 21:28:06 -070099 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-06-15 01:31:58 -0700100 *
101 * Returns a policy action constant such as ACTION_DISPATCH.
102 */
103 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
Jeff Browne57e8952010-07-23 21:28:06 -0700104 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700105
106 /* Intercepts a switch 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 * Switches are not dispatched to applications so this method should
111 * usually return ACTION_NONE.
112 */
Jeff Browne57e8952010-07-23 21:28:06 -0700113 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
114 uint32_t& policyFlags) = 0;
115
116 /* Intercepts a generic touch, trackball or other event.
117 * The policy can use this method as an opportunity to perform power management functions
118 * and early event preprocessing such as updating policy flags.
119 *
120 * Returns a policy action constant such as ACTION_DISPATCH.
121 */
122 virtual int32_t interceptGeneric(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700123
124 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
125 * certain device whose screen currently is not all that good.
126 */
127 virtual bool filterTouchEvents() = 0;
128
129 /* Determines whether to turn on some hacks to improve touch interaction with another device
130 * where touch coordinate data can get corrupted.
131 */
132 virtual bool filterJumpyTouchEvents() = 0;
133
134 /* Gets the configured virtual key definitions for an input device. */
135 virtual void getVirtualKeyDefinitions(const String8& deviceName,
136 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
137
138 /* Gets the excluded device names for the platform. */
139 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
140};
141
142
143/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700144class InputReaderInterface : public virtual RefBase {
145protected:
146 InputReaderInterface() { }
147 virtual ~InputReaderInterface() { }
148
149public:
150 /* Runs a single iteration of the processing loop.
151 * Nominally reads and processes one incoming message from the EventHub.
152 *
153 * This method should be called on the input reader thread.
154 */
155 virtual void loopOnce() = 0;
156
Jeff Brown54bc2812010-06-15 01:31:58 -0700157 /* Gets the current input device configuration.
158 *
159 * This method may be called on any thread (usually by the input manager).
160 */
Jeff Browne57e8952010-07-23 21:28:06 -0700161 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700162
Jeff Browne57e8952010-07-23 21:28:06 -0700163 /* Gets information about the specified input device.
164 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
165 * was no such device.
166 *
167 * This method may be called on any thread (usually by the input manager).
Jeff Brown54bc2812010-06-15 01:31:58 -0700168 */
Jeff Browne57e8952010-07-23 21:28:06 -0700169 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
170
171 /* Gets the list of all registered device ids. */
172 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
173
174 /* Query current input state. */
175 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
176 int32_t scanCode) = 0;
177 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
178 int32_t keyCode) = 0;
179 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
180 int32_t sw) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700181
182 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700183 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
184 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
185};
186
187
188/* Internal interface used by individual input devices to access global input device state
189 * and parameters maintained by the input reader.
190 */
191class InputReaderContext {
192protected:
193 InputReaderContext() { }
194 virtual ~InputReaderContext() { }
195
196public:
197 virtual void updateGlobalMetaState() = 0;
198 virtual int32_t getGlobalMetaState() = 0;
199
200 virtual InputReaderPolicyInterface* getPolicy() = 0;
201 virtual InputDispatcherInterface* getDispatcher() = 0;
202 virtual EventHubInterface* getEventHub() = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700203};
204
Jeff Brown54bc2812010-06-15 01:31:58 -0700205
Jeff Browne839a582010-04-22 18:58:52 -0700206/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700207 * that it sends to the input dispatcher. Some functions of the input reader, such as early
208 * event filtering in low power states, are controlled by a separate policy object.
209 *
210 * IMPORTANT INVARIANT:
Jeff Browne57e8952010-07-23 21:28:06 -0700211 * Because the policy and dispatcher can potentially block or cause re-entrance into
212 * the input reader, the input reader never calls into other components while holding
213 * an exclusive internal lock.
Jeff Browne839a582010-04-22 18:58:52 -0700214 */
Jeff Browne57e8952010-07-23 21:28:06 -0700215class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700216public:
217 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700218 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700219 const sp<InputDispatcherInterface>& dispatcher);
220 virtual ~InputReader();
221
222 virtual void loopOnce();
223
Jeff Browne57e8952010-07-23 21:28:06 -0700224 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700225
Jeff Browne57e8952010-07-23 21:28:06 -0700226 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
227 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700228
Jeff Browne57e8952010-07-23 21:28:06 -0700229 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
230 int32_t scanCode);
231 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
232 int32_t keyCode);
233 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
234 int32_t sw);
Jeff Brown54bc2812010-06-15 01:31:58 -0700235
Jeff Browne57e8952010-07-23 21:28:06 -0700236 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
237 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700238
Jeff Browne839a582010-04-22 18:58:52 -0700239private:
Jeff Browne839a582010-04-22 18:58:52 -0700240 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700241 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700242 sp<InputDispatcherInterface> mDispatcher;
243
Jeff Browne57e8952010-07-23 21:28:06 -0700244 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
245 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
246 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
247
248 // This reader/writer lock guards the list of input devices.
249 // The writer lock must be held whenever the list of input devices is modified
250 // and then promptly released.
251 // The reader lock must be held whenever the list of input devices is traversed or an
252 // input device in the list is accessed.
253 // This lock only protects the registry and prevents inadvertent deletion of device objects
254 // that are in use. Individual devices are responsible for guarding their own internal state
255 // as needed for concurrent operation.
256 RWLock mDeviceRegistryLock;
Jeff Browne839a582010-04-22 18:58:52 -0700257 KeyedVector<int32_t, InputDevice*> mDevices;
258
Jeff Browne57e8952010-07-23 21:28:06 -0700259 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700260 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700261
Jeff Browne839a582010-04-22 18:58:52 -0700262 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700263 void removeDevice(nsecs_t when, int32_t deviceId);
264 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown54bc2812010-06-15 01:31:58 -0700265 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700266
Jeff Browne57e8952010-07-23 21:28:06 -0700267 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700268
Jeff Browne57e8952010-07-23 21:28:06 -0700269 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700270
Jeff Browne57e8952010-07-23 21:28:06 -0700271 // state management for all devices
272 Mutex mStateLock;
273
274 int32_t mGlobalMetaState;
275 virtual void updateGlobalMetaState();
276 virtual int32_t getGlobalMetaState();
277
278 InputConfiguration mInputConfiguration;
279 void updateInputConfiguration();
280
281 // state queries
282 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
283 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
284 GetStateFunc getStateFunc);
285 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
286 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700287};
288
289
290/* Reads raw events from the event hub and processes them, endlessly. */
291class InputReaderThread : public Thread {
292public:
293 InputReaderThread(const sp<InputReaderInterface>& reader);
294 virtual ~InputReaderThread();
295
296private:
297 sp<InputReaderInterface> mReader;
298
299 virtual bool threadLoop();
300};
301
Jeff Browne57e8952010-07-23 21:28:06 -0700302
303/* Represents the state of a single input device. */
304class InputDevice {
305public:
306 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
307 ~InputDevice();
308
309 inline InputReaderContext* getContext() { return mContext; }
310 inline int32_t getId() { return mId; }
311 inline const String8& getName() { return mName; }
312 inline uint32_t getSources() { return mSources; }
313
314 inline bool isIgnored() { return mMappers.isEmpty(); }
315
316 void addMapper(InputMapper* mapper);
317 void configure();
318 void reset();
319 void process(const RawEvent* rawEvent);
320
321 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
322 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
323 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
324 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
325 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
326 const int32_t* keyCodes, uint8_t* outFlags);
327
328 int32_t getMetaState();
329
330private:
331 InputReaderContext* mContext;
332 int32_t mId;
333
334 Vector<InputMapper*> mMappers;
335
336 String8 mName;
337 uint32_t mSources;
338
339 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
340 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
341};
342
343
344/* An input mapper transforms raw input events into cooked event data.
345 * A single input device can have multiple associated input mappers in order to interpret
346 * different classes of events.
347 */
348class InputMapper {
349public:
350 InputMapper(InputDevice* device);
351 virtual ~InputMapper();
352
353 inline InputDevice* getDevice() { return mDevice; }
354 inline int32_t getDeviceId() { return mDevice->getId(); }
355 inline const String8 getDeviceName() { return mDevice->getName(); }
356 inline InputReaderContext* getContext() { return mContext; }
357 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
358 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
359 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
360
361 virtual uint32_t getSources() = 0;
362 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
363 virtual void configure();
364 virtual void reset();
365 virtual void process(const RawEvent* rawEvent) = 0;
366
367 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
368 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
369 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
370 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
371 const int32_t* keyCodes, uint8_t* outFlags);
372
373 virtual int32_t getMetaState();
374
375protected:
376 InputDevice* mDevice;
377 InputReaderContext* mContext;
378
379 bool applyStandardPolicyActions(nsecs_t when, int32_t policyActions);
380};
381
382
383class SwitchInputMapper : public InputMapper {
384public:
385 SwitchInputMapper(InputDevice* device);
386 virtual ~SwitchInputMapper();
387
388 virtual uint32_t getSources();
389 virtual void process(const RawEvent* rawEvent);
390
391 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
392
393private:
394 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
395};
396
397
398class KeyboardInputMapper : public InputMapper {
399public:
400 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
401 int32_t keyboardType);
402 virtual ~KeyboardInputMapper();
403
404 virtual uint32_t getSources();
405 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
406 virtual void reset();
407 virtual void process(const RawEvent* rawEvent);
408
409 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
410 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
411 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
412 const int32_t* keyCodes, uint8_t* outFlags);
413
414 virtual int32_t getMetaState();
415
416private:
417 struct KeyDown {
418 int32_t keyCode;
419 int32_t scanCode;
420 };
421
422 int32_t mAssociatedDisplayId;
423 uint32_t mSources;
424 int32_t mKeyboardType;
425
426 Vector<KeyDown> mKeyDowns; // keys that are down
427 int32_t mMetaState;
428 nsecs_t mDownTime; // time of most recent key down
429
430 void initialize();
431
432 bool isKeyboardOrGamepadKey(int32_t scanCode);
433 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
434 uint32_t policyFlags);
435
436 ssize_t findKeyDown(int32_t scanCode);
437};
438
439
440class TrackballInputMapper : public InputMapper {
441public:
442 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
443 virtual ~TrackballInputMapper();
444
445 virtual uint32_t getSources();
446 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
447 virtual void reset();
448 virtual void process(const RawEvent* rawEvent);
449
450private:
451 // Amount that trackball needs to move in order to generate a key event.
452 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
453
454 int32_t mAssociatedDisplayId;
455
456 struct Accumulator {
457 enum {
458 FIELD_BTN_MOUSE = 1,
459 FIELD_REL_X = 2,
460 FIELD_REL_Y = 4
461 };
462
463 uint32_t fields;
464
465 bool btnMouse;
466 int32_t relX;
467 int32_t relY;
468
469 inline void clear() {
470 fields = 0;
471 }
472
473 inline bool isDirty() {
474 return fields != 0;
475 }
476 } mAccumulator;
477
478 bool mDown;
479 nsecs_t mDownTime;
480
481 float mXScale;
482 float mYScale;
483 float mXPrecision;
484 float mYPrecision;
485
486 void initialize();
487
488 void sync(nsecs_t when);
489};
490
491
492class TouchInputMapper : public InputMapper {
493public:
494 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
495 virtual ~TouchInputMapper();
496
497 virtual uint32_t getSources();
498 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
499 virtual void configure();
500 virtual void reset();
501
502 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
503 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
504 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
505 const int32_t* keyCodes, uint8_t* outFlags);
506
507protected:
508 /* Maximum pointer id value supported.
509 * (This is limited by our use of BitSet32 to track pointer assignments.) */
510 static const uint32_t MAX_POINTER_ID = 31;
511
512 struct VirtualKey {
513 int32_t keyCode;
514 int32_t scanCode;
515 uint32_t flags;
516
517 // computed hit box, specified in touch screen coords based on known display size
518 int32_t hitLeft;
519 int32_t hitTop;
520 int32_t hitRight;
521 int32_t hitBottom;
522
523 inline bool isHit(int32_t x, int32_t y) const {
524 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
525 }
526 };
527
528 struct PointerData {
529 uint32_t id;
530 int32_t x;
531 int32_t y;
532 int32_t pressure;
533 int32_t size;
534 int32_t touchMajor;
535 int32_t touchMinor;
536 int32_t toolMajor;
537 int32_t toolMinor;
538 int32_t orientation;
539 };
540
541 struct TouchData {
542 uint32_t pointerCount;
543 PointerData pointers[MAX_POINTERS];
544 BitSet32 idBits;
545 uint32_t idToIndex[MAX_POINTER_ID + 1];
546
547 void copyFrom(const TouchData& other) {
548 pointerCount = other.pointerCount;
549 idBits = other.idBits;
550
551 for (uint32_t i = 0; i < pointerCount; i++) {
552 pointers[i] = other.pointers[i];
553 idToIndex[i] = other.idToIndex[i];
554 }
555 }
556
557 inline void clear() {
558 pointerCount = 0;
559 idBits.clear();
560 }
561 };
562
563 int32_t mAssociatedDisplayId;
564 Vector<VirtualKey> mVirtualKeys;
565
566 // Immutable configuration parameters.
567 struct Parameters {
568 bool useBadTouchFilter;
569 bool useJumpyTouchFilter;
570 bool useAveragingTouchFilter;
571 } mParameters;
572
573 // Raw axis information.
574 struct Axes {
575 RawAbsoluteAxisInfo x;
576 RawAbsoluteAxisInfo y;
577 RawAbsoluteAxisInfo pressure;
578 RawAbsoluteAxisInfo size;
579 RawAbsoluteAxisInfo touchMajor;
580 RawAbsoluteAxisInfo touchMinor;
581 RawAbsoluteAxisInfo toolMajor;
582 RawAbsoluteAxisInfo toolMinor;
583 RawAbsoluteAxisInfo orientation;
584 } mAxes;
585
586 // The surface orientation and width and height set by configureSurface().
587 int32_t mSurfaceOrientation;
588 int32_t mSurfaceWidth, mSurfaceHeight;
589
590 // Translation and scaling factors, orientation-independent.
591 int32_t mXOrigin;
592 float mXScale;
593 float mXPrecision;
594
595 int32_t mYOrigin;
596 float mYScale;
597 float mYPrecision;
598
599 int32_t mPressureOrigin;
600 float mPressureScale;
601
602 int32_t mSizeOrigin;
603 float mSizeScale;
604
605 float mOrientationScale;
606
607 // Oriented motion ranges for input device info.
608 struct OrientedRanges {
609 InputDeviceInfo::MotionRange x;
610 InputDeviceInfo::MotionRange y;
611 InputDeviceInfo::MotionRange pressure;
612 InputDeviceInfo::MotionRange size;
613 InputDeviceInfo::MotionRange touchMajor;
614 InputDeviceInfo::MotionRange touchMinor;
615 InputDeviceInfo::MotionRange toolMajor;
616 InputDeviceInfo::MotionRange toolMinor;
617 InputDeviceInfo::MotionRange orientation;
618 } mOrientedRanges;
619
620 // Oriented dimensions and precision.
621 float mOrientedSurfaceWidth, mOrientedSurfaceHeight;
622 float mOrientedXPrecision, mOrientedYPrecision;
623
624 // The touch data of the current sample being processed.
625 TouchData mCurrentTouch;
626
627 // The touch data of the previous sample that was processed. This is updated
628 // incrementally while the current sample is being processed.
629 TouchData mLastTouch;
630
631 // The time the primary pointer last went down.
632 nsecs_t mDownTime;
633
634 struct CurrentVirtualKeyState {
635 bool down;
636 nsecs_t downTime;
637 int32_t keyCode;
638 int32_t scanCode;
639 } mCurrentVirtualKey;
640
641 // Lock for virtual key state.
642 Mutex mVirtualKeyLock; // methods use "Lvk" suffix
643
644 virtual void configureAxes();
645 virtual bool configureSurface();
646 virtual void configureVirtualKeys();
647
648 enum TouchResult {
649 // Dispatch the touch normally.
650 DISPATCH_TOUCH,
651 // Do not dispatch the touch, but keep tracking the current stroke.
652 SKIP_TOUCH,
653 // Do not dispatch the touch, and drop all information associated with the current stoke
654 // so the next movement will appear as a new down.
655 DROP_STROKE
656 };
657
658 void syncTouch(nsecs_t when, bool havePointerIds);
659
660private:
661 /* Maximum number of historical samples to average. */
662 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
663
664 /* Slop distance for jumpy pointer detection.
665 * The vertical range of the screen divided by this is our epsilon value. */
666 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
667
668 /* Number of jumpy points to drop for touchscreens that need it. */
669 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
670 static const uint32_t JUMPY_DROP_LIMIT = 3;
671
672 /* Maximum squared distance for averaging.
673 * If moving farther than this, turn of averaging to avoid lag in response. */
674 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
675
676 struct AveragingTouchFilterState {
677 // Individual history tracks are stored by pointer id
678 uint32_t historyStart[MAX_POINTERS];
679 uint32_t historyEnd[MAX_POINTERS];
680 struct {
681 struct {
682 int32_t x;
683 int32_t y;
684 int32_t pressure;
685 } pointers[MAX_POINTERS];
686 } historyData[AVERAGING_HISTORY_SIZE];
687 } mAveragingTouchFilter;
688
689 struct JumpTouchFilterState {
690 uint32_t jumpyPointsDropped;
691 } mJumpyTouchFilter;
692
693 struct PointerDistanceHeapElement {
694 uint32_t currentPointerIndex : 8;
695 uint32_t lastPointerIndex : 8;
696 uint64_t distance : 48; // squared distance
697 };
698
699 void initialize();
700
701 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
702 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
703 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
704 BitSet32 idBits, uint32_t changedId, int32_t motionEventAction);
705
706 bool isPointInsideSurface(int32_t x, int32_t y);
707 const VirtualKey* findVirtualKeyHitLvk(int32_t x, int32_t y);
708
709 bool applyBadTouchFilter();
710 bool applyJumpyTouchFilter();
711 void applyAveragingTouchFilter();
712 void calculatePointerIds();
713};
714
715
716class SingleTouchInputMapper : public TouchInputMapper {
717public:
718 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
719 virtual ~SingleTouchInputMapper();
720
721 virtual void reset();
722 virtual void process(const RawEvent* rawEvent);
723
724protected:
725 virtual void configureAxes();
726
727private:
728 struct Accumulator {
729 enum {
730 FIELD_BTN_TOUCH = 1,
731 FIELD_ABS_X = 2,
732 FIELD_ABS_Y = 4,
733 FIELD_ABS_PRESSURE = 8,
734 FIELD_ABS_TOOL_WIDTH = 16
735 };
736
737 uint32_t fields;
738
739 bool btnTouch;
740 int32_t absX;
741 int32_t absY;
742 int32_t absPressure;
743 int32_t absToolWidth;
744
745 inline void clear() {
746 fields = 0;
747 }
748
749 inline bool isDirty() {
750 return fields != 0;
751 }
752 } mAccumulator;
753
754 bool mDown;
755 int32_t mX;
756 int32_t mY;
757 int32_t mPressure;
758 int32_t mSize;
759
760 void initialize();
761
762 void sync(nsecs_t when);
763};
764
765
766class MultiTouchInputMapper : public TouchInputMapper {
767public:
768 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
769 virtual ~MultiTouchInputMapper();
770
771 virtual void reset();
772 virtual void process(const RawEvent* rawEvent);
773
774protected:
775 virtual void configureAxes();
776
777private:
778 struct Accumulator {
779 enum {
780 FIELD_ABS_MT_POSITION_X = 1,
781 FIELD_ABS_MT_POSITION_Y = 2,
782 FIELD_ABS_MT_TOUCH_MAJOR = 4,
783 FIELD_ABS_MT_TOUCH_MINOR = 8,
784 FIELD_ABS_MT_WIDTH_MAJOR = 16,
785 FIELD_ABS_MT_WIDTH_MINOR = 32,
786 FIELD_ABS_MT_ORIENTATION = 64,
787 FIELD_ABS_MT_TRACKING_ID = 128
788 };
789
790 uint32_t pointerCount;
791 struct Pointer {
792 uint32_t fields;
793
794 int32_t absMTPositionX;
795 int32_t absMTPositionY;
796 int32_t absMTTouchMajor;
797 int32_t absMTTouchMinor;
798 int32_t absMTWidthMajor;
799 int32_t absMTWidthMinor;
800 int32_t absMTOrientation;
801 int32_t absMTTrackingId;
802
803 inline void clear() {
804 fields = 0;
805 }
806 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
807
808 inline void clear() {
809 pointerCount = 0;
810 pointers[0].clear();
811 }
812
813 inline bool isDirty() {
814 return pointerCount != 0;
815 }
816 } mAccumulator;
817
818 void initialize();
819
820 void sync(nsecs_t when);
821};
822
Jeff Browne839a582010-04-22 18:58:52 -0700823} // namespace android
824
825#endif // _UI_INPUT_READER_H