blob: 647f2717c2ac534d1afb3df31b252003350c769b [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 "EventHub.h"
21#include "PointerControllerInterface.h"
22#include "InputListener.h"
23
24#include <input/Input.h>
25#include <input/VelocityControl.h>
26#include <input/VelocityTracker.h>
27#include <ui/DisplayInfo.h>
28#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070029#include <utils/Condition.h>
30#include <utils/Thread.h>
31#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <utils/Timers.h>
33#include <utils/RefBase.h>
34#include <utils/String8.h>
35#include <utils/BitSet.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070036#include <utils/SortedVector.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
38#include <stddef.h>
39#include <unistd.h>
40
41// Maximum supported size of a vibration pattern.
42// Must be at least 2.
43#define MAX_VIBRATE_PATTERN_SIZE 100
44
45// Maximum allowable delay value in a vibration pattern before
46// which the delay will be truncated.
47#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
48
49namespace android {
50
51class InputDevice;
52class InputMapper;
53
54/*
55 * Describes how coordinates are mapped on a physical display.
56 * See com.android.server.display.DisplayViewport.
57 */
58struct DisplayViewport {
59 int32_t displayId; // -1 if invalid
60 int32_t orientation;
61 int32_t logicalLeft;
62 int32_t logicalTop;
63 int32_t logicalRight;
64 int32_t logicalBottom;
65 int32_t physicalLeft;
66 int32_t physicalTop;
67 int32_t physicalRight;
68 int32_t physicalBottom;
69 int32_t deviceWidth;
70 int32_t deviceHeight;
71
72 DisplayViewport() :
73 displayId(ADISPLAY_ID_NONE), orientation(DISPLAY_ORIENTATION_0),
74 logicalLeft(0), logicalTop(0), logicalRight(0), logicalBottom(0),
75 physicalLeft(0), physicalTop(0), physicalRight(0), physicalBottom(0),
76 deviceWidth(0), deviceHeight(0) {
77 }
78
79 bool operator==(const DisplayViewport& other) const {
80 return displayId == other.displayId
81 && orientation == other.orientation
82 && logicalLeft == other.logicalLeft
83 && logicalTop == other.logicalTop
84 && logicalRight == other.logicalRight
85 && logicalBottom == other.logicalBottom
86 && physicalLeft == other.physicalLeft
87 && physicalTop == other.physicalTop
88 && physicalRight == other.physicalRight
89 && physicalBottom == other.physicalBottom
90 && deviceWidth == other.deviceWidth
91 && deviceHeight == other.deviceHeight;
92 }
93
94 bool operator!=(const DisplayViewport& other) const {
95 return !(*this == other);
96 }
97
98 inline bool isValid() const {
99 return displayId >= 0;
100 }
101
102 void setNonDisplayViewport(int32_t width, int32_t height) {
103 displayId = ADISPLAY_ID_NONE;
104 orientation = DISPLAY_ORIENTATION_0;
105 logicalLeft = 0;
106 logicalTop = 0;
107 logicalRight = width;
108 logicalBottom = height;
109 physicalLeft = 0;
110 physicalTop = 0;
111 physicalRight = width;
112 physicalBottom = height;
113 deviceWidth = width;
114 deviceHeight = height;
115 }
116};
117
118/*
119 * Input reader configuration.
120 *
121 * Specifies various options that modify the behavior of the input reader.
122 */
123struct InputReaderConfiguration {
124 // Describes changes that have occurred.
125 enum {
126 // The pointer speed changed.
127 CHANGE_POINTER_SPEED = 1 << 0,
128
129 // The pointer gesture control changed.
130 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
131
132 // The display size or orientation changed.
133 CHANGE_DISPLAY_INFO = 1 << 2,
134
135 // The visible touches option changed.
136 CHANGE_SHOW_TOUCHES = 1 << 3,
137
138 // The keyboard layouts must be reloaded.
139 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
140
141 // The device name alias supplied by the may have changed for some devices.
142 CHANGE_DEVICE_ALIAS = 1 << 5,
143
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800144 // The location calibration matrix changed.
Michael Wright842500e2015-03-13 17:32:02 -0700145 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
146
147 // The presence of an external stylus has changed.
148 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800149
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800150 // The pointer capture mode has changed.
151 CHANGE_POINTER_CAPTURE = 1 << 8,
152
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700153 // The set of disabled input devices (disabledDevices) has changed.
154 CHANGE_ENABLED_STATE = 1 << 9,
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 // All devices must be reopened.
157 CHANGE_MUST_REOPEN = 1 << 31,
158 };
159
160 // Gets the amount of time to disable virtual keys after the screen is touched
161 // in order to filter out accidental virtual key presses due to swiping gestures
162 // or taps near the edge of the display. May be 0 to disable the feature.
163 nsecs_t virtualKeyQuietTime;
164
165 // The excluded device names for the platform.
166 // Devices with these names will be ignored.
167 Vector<String8> excludedDeviceNames;
168
169 // Velocity control parameters for mouse pointer movements.
170 VelocityControlParameters pointerVelocityControlParameters;
171
172 // Velocity control parameters for mouse wheel movements.
173 VelocityControlParameters wheelVelocityControlParameters;
174
175 // True if pointer gestures are enabled.
176 bool pointerGesturesEnabled;
177
178 // Quiet time between certain pointer gesture transitions.
179 // Time to allow for all fingers or buttons to settle into a stable state before
180 // starting a new gesture.
181 nsecs_t pointerGestureQuietInterval;
182
183 // The minimum speed that a pointer must travel for us to consider switching the active
184 // touch pointer to it during a drag. This threshold is set to avoid switching due
185 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
186 float pointerGestureDragMinSwitchSpeed; // in pixels per second
187
188 // Tap gesture delay time.
189 // The time between down and up must be less than this to be considered a tap.
190 nsecs_t pointerGestureTapInterval;
191
192 // Tap drag gesture delay time.
193 // The time between the previous tap's up and the next down must be less than
194 // this to be considered a drag. Otherwise, the previous tap is finished and a
195 // new tap begins.
196 //
197 // Note that the previous tap will be held down for this entire duration so this
198 // interval must be shorter than the long press timeout.
199 nsecs_t pointerGestureTapDragInterval;
200
201 // The distance in pixels that the pointer is allowed to move from initial down
202 // to up and still be called a tap.
203 float pointerGestureTapSlop; // in pixels
204
205 // Time after the first touch points go down to settle on an initial centroid.
206 // This is intended to be enough time to handle cases where the user puts down two
207 // fingers at almost but not quite exactly the same time.
208 nsecs_t pointerGestureMultitouchSettleInterval;
209
210 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
211 // at least two pointers have moved at least this far from their starting place.
212 float pointerGestureMultitouchMinDistance; // in pixels
213
214 // The transition from PRESS to SWIPE gesture mode can only occur when the
215 // cosine of the angle between the two vectors is greater than or equal to than this value
216 // which indicates that the vectors are oriented in the same direction.
217 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
218 // (In exactly opposite directions, the cosine is -1.0.)
219 float pointerGestureSwipeTransitionAngleCosine;
220
221 // The transition from PRESS to SWIPE gesture mode can only occur when the
222 // fingers are no more than this far apart relative to the diagonal size of
223 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
224 // no more than half the diagonal size of the touch pad apart.
225 float pointerGestureSwipeMaxWidthRatio;
226
227 // The gesture movement speed factor relative to the size of the display.
228 // Movement speed applies when the fingers are moving in the same direction.
229 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
230 // will cover this portion of the display diagonal.
231 float pointerGestureMovementSpeedRatio;
232
233 // The gesture zoom speed factor relative to the size of the display.
234 // Zoom speed applies when the fingers are mostly moving relative to each other
235 // to execute a scale gesture or similar.
236 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
237 // will cover this portion of the display diagonal.
238 float pointerGestureZoomSpeedRatio;
239
240 // True to show the location of touches on the touch screen as spots.
241 bool showTouches;
242
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800243 // True if pointer capture is enabled.
244 bool pointerCapture;
245
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700246 // The set of currently disabled input devices.
247 SortedVector<int32_t> disabledDevices;
248
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 InputReaderConfiguration() :
250 virtualKeyQuietTime(0),
251 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
252 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
253 pointerGesturesEnabled(true),
254 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
255 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
256 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
257 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
258 pointerGestureTapSlop(10.0f), // 10 pixels
259 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
260 pointerGestureMultitouchMinDistance(15), // 15 pixels
261 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
262 pointerGestureSwipeMaxWidthRatio(0.25f),
263 pointerGestureMovementSpeedRatio(0.8f),
264 pointerGestureZoomSpeedRatio(0.3f),
265 showTouches(false) { }
266
267 bool getDisplayInfo(bool external, DisplayViewport* outViewport) const;
268 void setDisplayInfo(bool external, const DisplayViewport& viewport);
269
270private:
271 DisplayViewport mInternalDisplay;
272 DisplayViewport mExternalDisplay;
273};
274
275
Jason Gereckeaf126fb2012-05-10 14:22:47 -0700276struct TouchAffineTransformation {
277 float x_scale;
278 float x_ymix;
279 float x_offset;
280 float y_xmix;
281 float y_scale;
282 float y_offset;
283
284 TouchAffineTransformation() :
285 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
286 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
287 }
288
Jason Gerecke489fda82012-09-07 17:19:40 -0700289 TouchAffineTransformation(float xscale, float xymix, float xoffset,
290 float yxmix, float yscale, float yoffset) :
291 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
292 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
293 }
294
Jason Gereckeaf126fb2012-05-10 14:22:47 -0700295 void applyTo(float& x, float& y) const;
296};
297
298
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299/*
300 * Input reader policy interface.
301 *
302 * The input reader policy is used by the input reader to interact with the Window Manager
303 * and other system components.
304 *
305 * The actual implementation is partially supported by callbacks into the DVM
306 * via JNI. This interface is also mocked in the unit tests.
307 *
308 * These methods must NOT re-enter the input reader since they may be called while
309 * holding the input reader lock.
310 */
311class InputReaderPolicyInterface : public virtual RefBase {
312protected:
313 InputReaderPolicyInterface() { }
314 virtual ~InputReaderPolicyInterface() { }
315
316public:
317 /* Gets the input reader configuration. */
318 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
319
320 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
321 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
322
323 /* Notifies the input reader policy that some input devices have changed
324 * and provides information about all current input devices.
325 */
326 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
327
328 /* Gets the keyboard layout for a particular input device. */
329 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
330 const InputDeviceIdentifier& identifier) = 0;
331
332 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
333 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800334
335 /* Gets the affine calibration associated with the specified device. */
336 virtual TouchAffineTransformation getTouchAffineTransformation(
Jason Gerecke71b16e82014-03-10 09:47:59 -0700337 const String8& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338};
339
340
341/* Processes raw input events and sends cooked event data to an input listener. */
342class InputReaderInterface : public virtual RefBase {
343protected:
344 InputReaderInterface() { }
345 virtual ~InputReaderInterface() { }
346
347public:
348 /* Dumps the state of the input reader.
349 *
350 * This method may be called on any thread (usually by the input manager). */
351 virtual void dump(String8& dump) = 0;
352
353 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
354 virtual void monitor() = 0;
355
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700356 /* Returns true if the input device is enabled. */
357 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
358
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359 /* Runs a single iteration of the processing loop.
360 * Nominally reads and processes one incoming message from the EventHub.
361 *
362 * This method should be called on the input reader thread.
363 */
364 virtual void loopOnce() = 0;
365
366 /* Gets information about all input devices.
367 *
368 * This method may be called on any thread (usually by the input manager).
369 */
370 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
371
372 /* Query current input state. */
373 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
374 int32_t scanCode) = 0;
375 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
376 int32_t keyCode) = 0;
377 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
378 int32_t sw) = 0;
379
Andrii Kulian763a3a42016-03-08 10:46:16 -0800380 /* Toggle Caps Lock */
381 virtual void toggleCapsLockState(int32_t deviceId) = 0;
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383 /* Determine whether physical keys exist for the given framework-domain key codes. */
384 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
385 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
386
387 /* Requests that a reconfiguration of all input devices.
388 * The changes flag is a bitfield that indicates what has changed and whether
389 * the input devices must all be reopened. */
390 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
391
392 /* Controls the vibrator of a particular input device. */
393 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
394 ssize_t repeat, int32_t token) = 0;
395 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
396};
397
Michael Wright842500e2015-03-13 17:32:02 -0700398struct StylusState {
399 /* Time the stylus event was received. */
400 nsecs_t when;
401 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
402 float pressure;
403 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
404 uint32_t buttons;
405 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
406 int32_t toolType;
407
408 void copyFrom(const StylusState& other) {
409 when = other.when;
410 pressure = other.pressure;
411 buttons = other.buttons;
412 toolType = other.toolType;
413 }
414
415 void clear() {
416 when = LLONG_MAX;
417 pressure = 0.f;
418 buttons = 0;
419 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
420 }
421};
422
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
424/* Internal interface used by individual input devices to access global input device state
425 * and parameters maintained by the input reader.
426 */
427class InputReaderContext {
428public:
429 InputReaderContext() { }
430 virtual ~InputReaderContext() { }
431
432 virtual void updateGlobalMetaState() = 0;
433 virtual int32_t getGlobalMetaState() = 0;
434
435 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
436 virtual bool shouldDropVirtualKey(nsecs_t now,
437 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
438
439 virtual void fadePointer() = 0;
440
441 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
442 virtual int32_t bumpGeneration() = 0;
443
Michael Wrightb85401d2015-04-17 18:35:15 +0100444 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
445 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -0700446
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447 virtual InputReaderPolicyInterface* getPolicy() = 0;
448 virtual InputListenerInterface* getListener() = 0;
449 virtual EventHubInterface* getEventHub() = 0;
450};
451
452
453/* The input reader reads raw event data from the event hub and processes it into input events
454 * that it sends to the input listener. Some functions of the input reader, such as early
455 * event filtering in low power states, are controlled by a separate policy object.
456 *
457 * The InputReader owns a collection of InputMappers. Most of the work it does happens
458 * on the input reader thread but the InputReader can receive queries from other system
459 * components running on arbitrary threads. To keep things manageable, the InputReader
460 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
461 * EventHub or the InputReaderPolicy but it is never held while calling into the
462 * InputListener.
463 */
464class InputReader : public InputReaderInterface {
465public:
466 InputReader(const sp<EventHubInterface>& eventHub,
467 const sp<InputReaderPolicyInterface>& policy,
468 const sp<InputListenerInterface>& listener);
469 virtual ~InputReader();
470
471 virtual void dump(String8& dump);
472 virtual void monitor();
473
474 virtual void loopOnce();
475
476 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
477
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700478 virtual bool isInputDeviceEnabled(int32_t deviceId);
479
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
481 int32_t scanCode);
482 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
483 int32_t keyCode);
484 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
485 int32_t sw);
486
Andrii Kulian763a3a42016-03-08 10:46:16 -0800487 virtual void toggleCapsLockState(int32_t deviceId);
488
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
490 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
491
492 virtual void requestRefreshConfiguration(uint32_t changes);
493
494 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
495 ssize_t repeat, int32_t token);
496 virtual void cancelVibrate(int32_t deviceId, int32_t token);
497
498protected:
499 // These members are protected so they can be instrumented by test cases.
500 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
501 const InputDeviceIdentifier& identifier, uint32_t classes);
502
503 class ContextImpl : public InputReaderContext {
504 InputReader* mReader;
505
506 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700507 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508
509 virtual void updateGlobalMetaState();
510 virtual int32_t getGlobalMetaState();
511 virtual void disableVirtualKeysUntil(nsecs_t time);
512 virtual bool shouldDropVirtualKey(nsecs_t now,
513 InputDevice* device, int32_t keyCode, int32_t scanCode);
514 virtual void fadePointer();
515 virtual void requestTimeoutAtTime(nsecs_t when);
516 virtual int32_t bumpGeneration();
Michael Wright842500e2015-03-13 17:32:02 -0700517 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
518 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 virtual InputReaderPolicyInterface* getPolicy();
520 virtual InputListenerInterface* getListener();
521 virtual EventHubInterface* getEventHub();
522 } mContext;
523
524 friend class ContextImpl;
525
526private:
527 Mutex mLock;
528
529 Condition mReaderIsAliveCondition;
530
531 sp<EventHubInterface> mEventHub;
532 sp<InputReaderPolicyInterface> mPolicy;
533 sp<QueuedInputListener> mQueuedListener;
534
535 InputReaderConfiguration mConfig;
536
537 // The event queue.
538 static const int EVENT_BUFFER_SIZE = 256;
539 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
540
541 KeyedVector<int32_t, InputDevice*> mDevices;
542
543 // low-level input event decoding and device management
544 void processEventsLocked(const RawEvent* rawEvents, size_t count);
545
546 void addDeviceLocked(nsecs_t when, int32_t deviceId);
547 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
548 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
549 void timeoutExpiredLocked(nsecs_t when);
550
551 void handleConfigurationChangedLocked(nsecs_t when);
552
553 int32_t mGlobalMetaState;
554 void updateGlobalMetaStateLocked();
555 int32_t getGlobalMetaStateLocked();
556
Michael Wright842500e2015-03-13 17:32:02 -0700557 void notifyExternalStylusPresenceChanged();
558 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
559 void dispatchExternalStylusState(const StylusState& state);
560
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 void fadePointerLocked();
562
563 int32_t mGeneration;
564 int32_t bumpGenerationLocked();
565
566 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
567
568 nsecs_t mDisableVirtualKeysTimeout;
569 void disableVirtualKeysUntilLocked(nsecs_t time);
570 bool shouldDropVirtualKeyLocked(nsecs_t now,
571 InputDevice* device, int32_t keyCode, int32_t scanCode);
572
573 nsecs_t mNextTimeout;
574 void requestTimeoutAtTimeLocked(nsecs_t when);
575
576 uint32_t mConfigurationChangesToRefresh;
577 void refreshConfigurationLocked(uint32_t changes);
578
579 // state queries
580 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
581 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
582 GetStateFunc getStateFunc);
583 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
584 const int32_t* keyCodes, uint8_t* outFlags);
585};
586
587
588/* Reads raw events from the event hub and processes them, endlessly. */
589class InputReaderThread : public Thread {
590public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700591 explicit InputReaderThread(const sp<InputReaderInterface>& reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 virtual ~InputReaderThread();
593
594private:
595 sp<InputReaderInterface> mReader;
596
597 virtual bool threadLoop();
598};
599
600
601/* Represents the state of a single input device. */
602class InputDevice {
603public:
604 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
605 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
606 ~InputDevice();
607
608 inline InputReaderContext* getContext() { return mContext; }
609 inline int32_t getId() const { return mId; }
610 inline int32_t getControllerNumber() const { return mControllerNumber; }
611 inline int32_t getGeneration() const { return mGeneration; }
612 inline const String8& getName() const { return mIdentifier.name; }
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800613 inline const String8& getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 inline uint32_t getClasses() const { return mClasses; }
615 inline uint32_t getSources() const { return mSources; }
616
617 inline bool isExternal() { return mIsExternal; }
618 inline void setExternal(bool external) { mIsExternal = external; }
619
Tim Kilbourn063ff532015-04-08 10:26:18 -0700620 inline void setMic(bool hasMic) { mHasMic = hasMic; }
621 inline bool hasMic() const { return mHasMic; }
622
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 inline bool isIgnored() { return mMappers.isEmpty(); }
624
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700625 bool isEnabled();
626 void setEnabled(bool enabled, nsecs_t when);
627
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 void dump(String8& dump);
629 void addMapper(InputMapper* mapper);
630 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
631 void reset(nsecs_t when);
632 void process(const RawEvent* rawEvents, size_t count);
633 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700634 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635
636 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
637 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
638 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
639 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
640 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
641 const int32_t* keyCodes, uint8_t* outFlags);
642 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
643 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800644 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645
646 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800647 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648
649 void fadePointer();
650
651 void bumpGeneration();
652
653 void notifyReset(nsecs_t when);
654
655 inline const PropertyMap& getConfiguration() { return mConfiguration; }
656 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
657
658 bool hasKey(int32_t code) {
659 return getEventHub()->hasScanCode(mId, code);
660 }
661
662 bool hasAbsoluteAxis(int32_t code) {
663 RawAbsoluteAxisInfo info;
664 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
665 return info.valid;
666 }
667
668 bool isKeyPressed(int32_t code) {
669 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
670 }
671
672 int32_t getAbsoluteAxisValue(int32_t code) {
673 int32_t value;
674 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
675 return value;
676 }
677
678private:
679 InputReaderContext* mContext;
680 int32_t mId;
681 int32_t mGeneration;
682 int32_t mControllerNumber;
683 InputDeviceIdentifier mIdentifier;
684 String8 mAlias;
685 uint32_t mClasses;
686
687 Vector<InputMapper*> mMappers;
688
689 uint32_t mSources;
690 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700691 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 bool mDropUntilNextSync;
693
694 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
695 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
696
697 PropertyMap mConfiguration;
698};
699
700
701/* Keeps track of the state of mouse or touch pad buttons. */
702class CursorButtonAccumulator {
703public:
704 CursorButtonAccumulator();
705 void reset(InputDevice* device);
706
707 void process(const RawEvent* rawEvent);
708
709 uint32_t getButtonState() const;
710
711private:
712 bool mBtnLeft;
713 bool mBtnRight;
714 bool mBtnMiddle;
715 bool mBtnBack;
716 bool mBtnSide;
717 bool mBtnForward;
718 bool mBtnExtra;
719 bool mBtnTask;
720
721 void clearButtons();
722};
723
724
725/* Keeps track of cursor movements. */
726
727class CursorMotionAccumulator {
728public:
729 CursorMotionAccumulator();
730 void reset(InputDevice* device);
731
732 void process(const RawEvent* rawEvent);
733 void finishSync();
734
735 inline int32_t getRelativeX() const { return mRelX; }
736 inline int32_t getRelativeY() const { return mRelY; }
737
738private:
739 int32_t mRelX;
740 int32_t mRelY;
741
742 void clearRelativeAxes();
743};
744
745
746/* Keeps track of cursor scrolling motions. */
747
748class CursorScrollAccumulator {
749public:
750 CursorScrollAccumulator();
751 void configure(InputDevice* device);
752 void reset(InputDevice* device);
753
754 void process(const RawEvent* rawEvent);
755 void finishSync();
756
757 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
758 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
759
760 inline int32_t getRelativeX() const { return mRelX; }
761 inline int32_t getRelativeY() const { return mRelY; }
762 inline int32_t getRelativeVWheel() const { return mRelWheel; }
763 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
764
765private:
766 bool mHaveRelWheel;
767 bool mHaveRelHWheel;
768
769 int32_t mRelX;
770 int32_t mRelY;
771 int32_t mRelWheel;
772 int32_t mRelHWheel;
773
774 void clearRelativeAxes();
775};
776
777
778/* Keeps track of the state of touch, stylus and tool buttons. */
779class TouchButtonAccumulator {
780public:
781 TouchButtonAccumulator();
782 void configure(InputDevice* device);
783 void reset(InputDevice* device);
784
785 void process(const RawEvent* rawEvent);
786
787 uint32_t getButtonState() const;
788 int32_t getToolType() const;
789 bool isToolActive() const;
790 bool isHovering() const;
791 bool hasStylus() const;
792
793private:
794 bool mHaveBtnTouch;
795 bool mHaveStylus;
796
797 bool mBtnTouch;
798 bool mBtnStylus;
799 bool mBtnStylus2;
800 bool mBtnToolFinger;
801 bool mBtnToolPen;
802 bool mBtnToolRubber;
803 bool mBtnToolBrush;
804 bool mBtnToolPencil;
805 bool mBtnToolAirbrush;
806 bool mBtnToolMouse;
807 bool mBtnToolLens;
808 bool mBtnToolDoubleTap;
809 bool mBtnToolTripleTap;
810 bool mBtnToolQuadTap;
811
812 void clearButtons();
813};
814
815
816/* Raw axis information from the driver. */
817struct RawPointerAxes {
818 RawAbsoluteAxisInfo x;
819 RawAbsoluteAxisInfo y;
820 RawAbsoluteAxisInfo pressure;
821 RawAbsoluteAxisInfo touchMajor;
822 RawAbsoluteAxisInfo touchMinor;
823 RawAbsoluteAxisInfo toolMajor;
824 RawAbsoluteAxisInfo toolMinor;
825 RawAbsoluteAxisInfo orientation;
826 RawAbsoluteAxisInfo distance;
827 RawAbsoluteAxisInfo tiltX;
828 RawAbsoluteAxisInfo tiltY;
829 RawAbsoluteAxisInfo trackingId;
830 RawAbsoluteAxisInfo slot;
831
832 RawPointerAxes();
833 void clear();
834};
835
836
837/* Raw data for a collection of pointers including a pointer id mapping table. */
838struct RawPointerData {
839 struct Pointer {
840 uint32_t id;
841 int32_t x;
842 int32_t y;
843 int32_t pressure;
844 int32_t touchMajor;
845 int32_t touchMinor;
846 int32_t toolMajor;
847 int32_t toolMinor;
848 int32_t orientation;
849 int32_t distance;
850 int32_t tiltX;
851 int32_t tiltY;
852 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
853 bool isHovering;
854 };
855
856 uint32_t pointerCount;
857 Pointer pointers[MAX_POINTERS];
858 BitSet32 hoveringIdBits, touchingIdBits;
859 uint32_t idToIndex[MAX_POINTER_ID + 1];
860
861 RawPointerData();
862 void clear();
863 void copyFrom(const RawPointerData& other);
864 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
865
866 inline void markIdBit(uint32_t id, bool isHovering) {
867 if (isHovering) {
868 hoveringIdBits.markBit(id);
869 } else {
870 touchingIdBits.markBit(id);
871 }
872 }
873
874 inline void clearIdBits() {
875 hoveringIdBits.clear();
876 touchingIdBits.clear();
877 }
878
879 inline const Pointer& pointerForId(uint32_t id) const {
880 return pointers[idToIndex[id]];
881 }
882
883 inline bool isHovering(uint32_t pointerIndex) {
884 return pointers[pointerIndex].isHovering;
885 }
886};
887
888
889/* Cooked data for a collection of pointers including a pointer id mapping table. */
890struct CookedPointerData {
891 uint32_t pointerCount;
892 PointerProperties pointerProperties[MAX_POINTERS];
893 PointerCoords pointerCoords[MAX_POINTERS];
894 BitSet32 hoveringIdBits, touchingIdBits;
895 uint32_t idToIndex[MAX_POINTER_ID + 1];
896
897 CookedPointerData();
898 void clear();
899 void copyFrom(const CookedPointerData& other);
900
901 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
902 return pointerCoords[idToIndex[id]];
903 }
904
Michael Wright842500e2015-03-13 17:32:02 -0700905 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
906 return pointerCoords[idToIndex[id]];
907 }
908
909 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
910 return pointerProperties[idToIndex[id]];
911 }
912
Michael Wright53dca3a2015-04-23 17:39:53 +0100913 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
915 }
Michael Wright842500e2015-03-13 17:32:02 -0700916
Michael Wright53dca3a2015-04-23 17:39:53 +0100917 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700918 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
919 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920};
921
922
923/* Keeps track of the state of single-touch protocol. */
924class SingleTouchMotionAccumulator {
925public:
926 SingleTouchMotionAccumulator();
927
928 void process(const RawEvent* rawEvent);
929 void reset(InputDevice* device);
930
931 inline int32_t getAbsoluteX() const { return mAbsX; }
932 inline int32_t getAbsoluteY() const { return mAbsY; }
933 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
934 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
935 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
936 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
937 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
938
939private:
940 int32_t mAbsX;
941 int32_t mAbsY;
942 int32_t mAbsPressure;
943 int32_t mAbsToolWidth;
944 int32_t mAbsDistance;
945 int32_t mAbsTiltX;
946 int32_t mAbsTiltY;
947
948 void clearAbsoluteAxes();
949};
950
951
952/* Keeps track of the state of multi-touch protocol. */
953class MultiTouchMotionAccumulator {
954public:
955 class Slot {
956 public:
957 inline bool isInUse() const { return mInUse; }
958 inline int32_t getX() const { return mAbsMTPositionX; }
959 inline int32_t getY() const { return mAbsMTPositionY; }
960 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
961 inline int32_t getTouchMinor() const {
962 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
963 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
964 inline int32_t getToolMinor() const {
965 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
966 inline int32_t getOrientation() const { return mAbsMTOrientation; }
967 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
968 inline int32_t getPressure() const { return mAbsMTPressure; }
969 inline int32_t getDistance() const { return mAbsMTDistance; }
970 inline int32_t getToolType() const;
971
972 private:
973 friend class MultiTouchMotionAccumulator;
974
975 bool mInUse;
976 bool mHaveAbsMTTouchMinor;
977 bool mHaveAbsMTWidthMinor;
978 bool mHaveAbsMTToolType;
979
980 int32_t mAbsMTPositionX;
981 int32_t mAbsMTPositionY;
982 int32_t mAbsMTTouchMajor;
983 int32_t mAbsMTTouchMinor;
984 int32_t mAbsMTWidthMajor;
985 int32_t mAbsMTWidthMinor;
986 int32_t mAbsMTOrientation;
987 int32_t mAbsMTTrackingId;
988 int32_t mAbsMTPressure;
989 int32_t mAbsMTDistance;
990 int32_t mAbsMTToolType;
991
992 Slot();
993 void clear();
994 };
995
996 MultiTouchMotionAccumulator();
997 ~MultiTouchMotionAccumulator();
998
999 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
1000 void reset(InputDevice* device);
1001 void process(const RawEvent* rawEvent);
1002 void finishSync();
1003 bool hasStylus() const;
1004
1005 inline size_t getSlotCount() const { return mSlotCount; }
1006 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
1007
1008private:
1009 int32_t mCurrentSlot;
1010 Slot* mSlots;
1011 size_t mSlotCount;
1012 bool mUsingSlotsProtocol;
1013 bool mHaveStylus;
1014
1015 void clearSlots(int32_t initialSlot);
1016};
1017
1018
1019/* An input mapper transforms raw input events into cooked event data.
1020 * A single input device can have multiple associated input mappers in order to interpret
1021 * different classes of events.
1022 *
1023 * InputMapper lifecycle:
1024 * - create
1025 * - configure with 0 changes
1026 * - reset
1027 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
1028 * - reset
1029 * - destroy
1030 */
1031class InputMapper {
1032public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001033 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 virtual ~InputMapper();
1035
1036 inline InputDevice* getDevice() { return mDevice; }
1037 inline int32_t getDeviceId() { return mDevice->getId(); }
1038 inline const String8 getDeviceName() { return mDevice->getName(); }
1039 inline InputReaderContext* getContext() { return mContext; }
1040 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
1041 inline InputListenerInterface* getListener() { return mContext->getListener(); }
1042 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
1043
1044 virtual uint32_t getSources() = 0;
1045 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1046 virtual void dump(String8& dump);
1047 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1048 virtual void reset(nsecs_t when);
1049 virtual void process(const RawEvent* rawEvent) = 0;
1050 virtual void timeoutExpired(nsecs_t when);
1051
1052 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1053 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1054 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
1055 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1056 const int32_t* keyCodes, uint8_t* outFlags);
1057 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1058 int32_t token);
1059 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -08001060 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061
1062 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -08001063 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064
Michael Wright842500e2015-03-13 17:32:02 -07001065 virtual void updateExternalStylusState(const StylusState& state);
1066
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 virtual void fadePointer();
1068
1069protected:
1070 InputDevice* mDevice;
1071 InputReaderContext* mContext;
1072
1073 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
1074 void bumpGeneration();
1075
1076 static void dumpRawAbsoluteAxisInfo(String8& dump,
1077 const RawAbsoluteAxisInfo& axis, const char* name);
Michael Wright842500e2015-03-13 17:32:02 -07001078 static void dumpStylusState(String8& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079};
1080
1081
1082class SwitchInputMapper : public InputMapper {
1083public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001084 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 virtual ~SwitchInputMapper();
1086
1087 virtual uint32_t getSources();
1088 virtual void process(const RawEvent* rawEvent);
1089
1090 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Michael Wrightbcbf97e2014-08-29 14:31:32 -07001091 virtual void dump(String8& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
1093private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -07001094 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 uint32_t mUpdatedSwitchMask;
1096
1097 void processSwitch(int32_t switchCode, int32_t switchValue);
1098 void sync(nsecs_t when);
1099};
1100
1101
1102class VibratorInputMapper : public InputMapper {
1103public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001104 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 virtual ~VibratorInputMapper();
1106
1107 virtual uint32_t getSources();
1108 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1109 virtual void process(const RawEvent* rawEvent);
1110
1111 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1112 int32_t token);
1113 virtual void cancelVibrate(int32_t token);
1114 virtual void timeoutExpired(nsecs_t when);
1115 virtual void dump(String8& dump);
1116
1117private:
1118 bool mVibrating;
1119 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
1120 size_t mPatternSize;
1121 ssize_t mRepeat;
1122 int32_t mToken;
1123 ssize_t mIndex;
1124 nsecs_t mNextStepTime;
1125
1126 void nextStep();
1127 void stopVibrating();
1128};
1129
1130
1131class KeyboardInputMapper : public InputMapper {
1132public:
1133 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
1134 virtual ~KeyboardInputMapper();
1135
1136 virtual uint32_t getSources();
1137 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1138 virtual void dump(String8& dump);
1139 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1140 virtual void reset(nsecs_t when);
1141 virtual void process(const RawEvent* rawEvent);
1142
1143 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1144 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1145 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1146 const int32_t* keyCodes, uint8_t* outFlags);
1147
1148 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -08001149 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
1151private:
1152 struct KeyDown {
1153 int32_t keyCode;
1154 int32_t scanCode;
1155 };
1156
1157 uint32_t mSource;
1158 int32_t mKeyboardType;
1159
1160 int32_t mOrientation; // orientation for dpad keys
1161
1162 Vector<KeyDown> mKeyDowns; // keys that are down
1163 int32_t mMetaState;
1164 nsecs_t mDownTime; // time of most recent key down
1165
1166 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
1167
1168 struct LedState {
1169 bool avail; // led is available
1170 bool on; // we think the led is currently on
1171 };
1172 LedState mCapsLockLedState;
1173 LedState mNumLockLedState;
1174 LedState mScrollLockLedState;
1175
1176 // Immutable configuration parameters.
1177 struct Parameters {
1178 bool hasAssociatedDisplay;
1179 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -07001180 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 } mParameters;
1182
1183 void configureParameters();
1184 void dumpParameters(String8& dump);
1185
1186 bool isKeyboardOrGamepadKey(int32_t scanCode);
1187
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -07001188 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189
Andrii Kulian763a3a42016-03-08 10:46:16 -08001190 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
1191
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192 ssize_t findKeyDown(int32_t scanCode);
1193
1194 void resetLedState();
1195 void initializeLedState(LedState& ledState, int32_t led);
1196 void updateLedState(bool reset);
1197 void updateLedStateForModifier(LedState& ledState, int32_t led,
1198 int32_t modifier, bool reset);
1199};
1200
1201
1202class CursorInputMapper : public InputMapper {
1203public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001204 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 virtual ~CursorInputMapper();
1206
1207 virtual uint32_t getSources();
1208 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1209 virtual void dump(String8& dump);
1210 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1211 virtual void reset(nsecs_t when);
1212 virtual void process(const RawEvent* rawEvent);
1213
1214 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1215
1216 virtual void fadePointer();
1217
1218private:
1219 // Amount that trackball needs to move in order to generate a key event.
1220 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1221
1222 // Immutable configuration parameters.
1223 struct Parameters {
1224 enum Mode {
1225 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001226 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 MODE_NAVIGATION,
1228 };
1229
1230 Mode mode;
1231 bool hasAssociatedDisplay;
1232 bool orientationAware;
1233 } mParameters;
1234
1235 CursorButtonAccumulator mCursorButtonAccumulator;
1236 CursorMotionAccumulator mCursorMotionAccumulator;
1237 CursorScrollAccumulator mCursorScrollAccumulator;
1238
1239 int32_t mSource;
1240 float mXScale;
1241 float mYScale;
1242 float mXPrecision;
1243 float mYPrecision;
1244
1245 float mVWheelScale;
1246 float mHWheelScale;
1247
1248 // Velocity controls for mouse pointer and wheel movements.
1249 // The controls for X and Y wheel movements are separate to keep them decoupled.
1250 VelocityControl mPointerVelocityControl;
1251 VelocityControl mWheelXVelocityControl;
1252 VelocityControl mWheelYVelocityControl;
1253
1254 int32_t mOrientation;
1255
1256 sp<PointerControllerInterface> mPointerController;
1257
1258 int32_t mButtonState;
1259 nsecs_t mDownTime;
1260
1261 void configureParameters();
1262 void dumpParameters(String8& dump);
1263
1264 void sync(nsecs_t when);
1265};
1266
1267
Prashant Malani1941ff52015-08-11 18:29:28 -07001268class RotaryEncoderInputMapper : public InputMapper {
1269public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001270 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -07001271 virtual ~RotaryEncoderInputMapper();
1272
1273 virtual uint32_t getSources();
1274 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1275 virtual void dump(String8& dump);
1276 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1277 virtual void reset(nsecs_t when);
1278 virtual void process(const RawEvent* rawEvent);
1279
1280private:
1281 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
1282
1283 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001284 float mScalingFactor;
Prashant Malani1941ff52015-08-11 18:29:28 -07001285
1286 void sync(nsecs_t when);
1287};
1288
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289class TouchInputMapper : public InputMapper {
1290public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001291 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292 virtual ~TouchInputMapper();
1293
1294 virtual uint32_t getSources();
1295 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1296 virtual void dump(String8& dump);
1297 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1298 virtual void reset(nsecs_t when);
1299 virtual void process(const RawEvent* rawEvent);
1300
1301 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1302 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1303 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1304 const int32_t* keyCodes, uint8_t* outFlags);
1305
1306 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001307 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001308 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001309 virtual void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001310
1311protected:
1312 CursorButtonAccumulator mCursorButtonAccumulator;
1313 CursorScrollAccumulator mCursorScrollAccumulator;
1314 TouchButtonAccumulator mTouchButtonAccumulator;
1315
1316 struct VirtualKey {
1317 int32_t keyCode;
1318 int32_t scanCode;
1319 uint32_t flags;
1320
1321 // computed hit box, specified in touch screen coords based on known display size
1322 int32_t hitLeft;
1323 int32_t hitTop;
1324 int32_t hitRight;
1325 int32_t hitBottom;
1326
1327 inline bool isHit(int32_t x, int32_t y) const {
1328 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1329 }
1330 };
1331
1332 // Input sources and device mode.
1333 uint32_t mSource;
1334
1335 enum DeviceMode {
1336 DEVICE_MODE_DISABLED, // input is disabled
1337 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1338 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1339 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1340 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1341 };
1342 DeviceMode mDeviceMode;
1343
1344 // The reader's configuration.
1345 InputReaderConfiguration mConfig;
1346
1347 // Immutable configuration parameters.
1348 struct Parameters {
1349 enum DeviceType {
1350 DEVICE_TYPE_TOUCH_SCREEN,
1351 DEVICE_TYPE_TOUCH_PAD,
1352 DEVICE_TYPE_TOUCH_NAVIGATION,
1353 DEVICE_TYPE_POINTER,
1354 };
1355
1356 DeviceType deviceType;
1357 bool hasAssociatedDisplay;
1358 bool associatedDisplayIsExternal;
1359 bool orientationAware;
1360 bool hasButtonUnderPad;
1361
1362 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001363 GESTURE_MODE_SINGLE_TOUCH,
1364 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 };
1366 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001367
1368 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 } mParameters;
1370
1371 // Immutable calibration parameters in parsed form.
1372 struct Calibration {
1373 // Size
1374 enum SizeCalibration {
1375 SIZE_CALIBRATION_DEFAULT,
1376 SIZE_CALIBRATION_NONE,
1377 SIZE_CALIBRATION_GEOMETRIC,
1378 SIZE_CALIBRATION_DIAMETER,
1379 SIZE_CALIBRATION_BOX,
1380 SIZE_CALIBRATION_AREA,
1381 };
1382
1383 SizeCalibration sizeCalibration;
1384
1385 bool haveSizeScale;
1386 float sizeScale;
1387 bool haveSizeBias;
1388 float sizeBias;
1389 bool haveSizeIsSummed;
1390 bool sizeIsSummed;
1391
1392 // Pressure
1393 enum PressureCalibration {
1394 PRESSURE_CALIBRATION_DEFAULT,
1395 PRESSURE_CALIBRATION_NONE,
1396 PRESSURE_CALIBRATION_PHYSICAL,
1397 PRESSURE_CALIBRATION_AMPLITUDE,
1398 };
1399
1400 PressureCalibration pressureCalibration;
1401 bool havePressureScale;
1402 float pressureScale;
1403
1404 // Orientation
1405 enum OrientationCalibration {
1406 ORIENTATION_CALIBRATION_DEFAULT,
1407 ORIENTATION_CALIBRATION_NONE,
1408 ORIENTATION_CALIBRATION_INTERPOLATED,
1409 ORIENTATION_CALIBRATION_VECTOR,
1410 };
1411
1412 OrientationCalibration orientationCalibration;
1413
1414 // Distance
1415 enum DistanceCalibration {
1416 DISTANCE_CALIBRATION_DEFAULT,
1417 DISTANCE_CALIBRATION_NONE,
1418 DISTANCE_CALIBRATION_SCALED,
1419 };
1420
1421 DistanceCalibration distanceCalibration;
1422 bool haveDistanceScale;
1423 float distanceScale;
1424
1425 enum CoverageCalibration {
1426 COVERAGE_CALIBRATION_DEFAULT,
1427 COVERAGE_CALIBRATION_NONE,
1428 COVERAGE_CALIBRATION_BOX,
1429 };
1430
1431 CoverageCalibration coverageCalibration;
1432
1433 inline void applySizeScaleAndBias(float* outSize) const {
1434 if (haveSizeScale) {
1435 *outSize *= sizeScale;
1436 }
1437 if (haveSizeBias) {
1438 *outSize += sizeBias;
1439 }
1440 if (*outSize < 0) {
1441 *outSize = 0;
1442 }
1443 }
1444 } mCalibration;
1445
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001446 // Affine location transformation/calibration
1447 struct TouchAffineTransformation mAffineTransform;
1448
Michael Wrightd02c5b62014-02-10 15:10:22 -08001449 RawPointerAxes mRawPointerAxes;
1450
Michael Wright842500e2015-03-13 17:32:02 -07001451 struct RawState {
1452 nsecs_t when;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001453
Michael Wright842500e2015-03-13 17:32:02 -07001454 // Raw pointer sample data.
1455 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001456
Michael Wright842500e2015-03-13 17:32:02 -07001457 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458
Michael Wright842500e2015-03-13 17:32:02 -07001459 // Scroll state.
1460 int32_t rawVScroll;
1461 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462
Michael Wright842500e2015-03-13 17:32:02 -07001463 void copyFrom(const RawState& other) {
1464 when = other.when;
1465 rawPointerData.copyFrom(other.rawPointerData);
1466 buttonState = other.buttonState;
1467 rawVScroll = other.rawVScroll;
1468 rawHScroll = other.rawHScroll;
1469 }
1470
1471 void clear() {
1472 when = 0;
1473 rawPointerData.clear();
1474 buttonState = 0;
1475 rawVScroll = 0;
1476 rawHScroll = 0;
1477 }
1478 };
1479
1480 struct CookedState {
1481 // Cooked pointer sample data.
1482 CookedPointerData cookedPointerData;
1483
1484 // Id bits used to differentiate fingers, stylus and mouse tools.
1485 BitSet32 fingerIdBits;
1486 BitSet32 stylusIdBits;
1487 BitSet32 mouseIdBits;
1488
Michael Wright7b159c92015-05-14 14:48:03 +01001489 int32_t buttonState;
1490
Michael Wright842500e2015-03-13 17:32:02 -07001491 void copyFrom(const CookedState& other) {
1492 cookedPointerData.copyFrom(other.cookedPointerData);
1493 fingerIdBits = other.fingerIdBits;
1494 stylusIdBits = other.stylusIdBits;
1495 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001496 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001497 }
1498
1499 void clear() {
1500 cookedPointerData.clear();
1501 fingerIdBits.clear();
1502 stylusIdBits.clear();
1503 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001504 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001505 }
1506 };
1507
1508 Vector<RawState> mRawStatesPending;
1509 RawState mCurrentRawState;
1510 CookedState mCurrentCookedState;
1511 RawState mLastRawState;
1512 CookedState mLastCookedState;
1513
1514 // State provided by an external stylus
1515 StylusState mExternalStylusState;
1516 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001517 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001518 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519
1520 // True if we sent a HOVER_ENTER event.
1521 bool mSentHoverEnter;
1522
Michael Wright842500e2015-03-13 17:32:02 -07001523 // Have we assigned pointer IDs for this stream
1524 bool mHavePointerIds;
1525
Michael Wright8e812822015-06-22 16:18:21 +01001526 // Is the current stream of direct touch events aborted
1527 bool mCurrentMotionAborted;
1528
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529 // The time the primary pointer last went down.
1530 nsecs_t mDownTime;
1531
1532 // The pointer controller, or null if the device is not a pointer.
1533 sp<PointerControllerInterface> mPointerController;
1534
1535 Vector<VirtualKey> mVirtualKeys;
1536
1537 virtual void configureParameters();
1538 virtual void dumpParameters(String8& dump);
1539 virtual void configureRawPointerAxes();
1540 virtual void dumpRawPointerAxes(String8& dump);
1541 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
1542 virtual void dumpSurface(String8& dump);
1543 virtual void configureVirtualKeys();
1544 virtual void dumpVirtualKeys(String8& dump);
1545 virtual void parseCalibration();
1546 virtual void resolveCalibration();
1547 virtual void dumpCalibration(String8& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001548 virtual void updateAffineTransformation();
Michael Wright842500e2015-03-13 17:32:02 -07001549 virtual void dumpAffineTransformation(String8& dump);
1550 virtual void resolveExternalStylusPresence();
1551 virtual bool hasStylus() const = 0;
1552 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553
Michael Wright842500e2015-03-13 17:32:02 -07001554 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001555
1556private:
1557 // The current viewport.
1558 // The components of the viewport are specified in the display's rotated orientation.
1559 DisplayViewport mViewport;
1560
1561 // The surface orientation, width and height set by configureSurface().
1562 // The width and height are derived from the viewport but are specified
1563 // in the natural orientation.
1564 // The surface origin specifies how the surface coordinates should be translated
1565 // to align with the logical display coordinate space.
1566 // The orientation may be different from the viewport orientation as it specifies
1567 // the rotation of the surface coordinates required to produce the viewport's
1568 // requested orientation, so it will depend on whether the device is orientation aware.
1569 int32_t mSurfaceWidth;
1570 int32_t mSurfaceHeight;
1571 int32_t mSurfaceLeft;
1572 int32_t mSurfaceTop;
1573 int32_t mSurfaceOrientation;
1574
1575 // Translation and scaling factors, orientation-independent.
1576 float mXTranslate;
1577 float mXScale;
1578 float mXPrecision;
1579
1580 float mYTranslate;
1581 float mYScale;
1582 float mYPrecision;
1583
1584 float mGeometricScale;
1585
1586 float mPressureScale;
1587
1588 float mSizeScale;
1589
1590 float mOrientationScale;
1591
1592 float mDistanceScale;
1593
1594 bool mHaveTilt;
1595 float mTiltXCenter;
1596 float mTiltXScale;
1597 float mTiltYCenter;
1598 float mTiltYScale;
1599
Michael Wright842500e2015-03-13 17:32:02 -07001600 bool mExternalStylusConnected;
1601
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 // Oriented motion ranges for input device info.
1603 struct OrientedRanges {
1604 InputDeviceInfo::MotionRange x;
1605 InputDeviceInfo::MotionRange y;
1606 InputDeviceInfo::MotionRange pressure;
1607
1608 bool haveSize;
1609 InputDeviceInfo::MotionRange size;
1610
1611 bool haveTouchSize;
1612 InputDeviceInfo::MotionRange touchMajor;
1613 InputDeviceInfo::MotionRange touchMinor;
1614
1615 bool haveToolSize;
1616 InputDeviceInfo::MotionRange toolMajor;
1617 InputDeviceInfo::MotionRange toolMinor;
1618
1619 bool haveOrientation;
1620 InputDeviceInfo::MotionRange orientation;
1621
1622 bool haveDistance;
1623 InputDeviceInfo::MotionRange distance;
1624
1625 bool haveTilt;
1626 InputDeviceInfo::MotionRange tilt;
1627
1628 OrientedRanges() {
1629 clear();
1630 }
1631
1632 void clear() {
1633 haveSize = false;
1634 haveTouchSize = false;
1635 haveToolSize = false;
1636 haveOrientation = false;
1637 haveDistance = false;
1638 haveTilt = false;
1639 }
1640 } mOrientedRanges;
1641
1642 // Oriented dimensions and precision.
1643 float mOrientedXPrecision;
1644 float mOrientedYPrecision;
1645
1646 struct CurrentVirtualKeyState {
1647 bool down;
1648 bool ignored;
1649 nsecs_t downTime;
1650 int32_t keyCode;
1651 int32_t scanCode;
1652 } mCurrentVirtualKey;
1653
1654 // Scale factor for gesture or mouse based pointer movements.
1655 float mPointerXMovementScale;
1656 float mPointerYMovementScale;
1657
1658 // Scale factor for gesture based zooming and other freeform motions.
1659 float mPointerXZoomScale;
1660 float mPointerYZoomScale;
1661
1662 // The maximum swipe width.
1663 float mPointerGestureMaxSwipeWidth;
1664
1665 struct PointerDistanceHeapElement {
1666 uint32_t currentPointerIndex : 8;
1667 uint32_t lastPointerIndex : 8;
1668 uint64_t distance : 48; // squared distance
1669 };
1670
1671 enum PointerUsage {
1672 POINTER_USAGE_NONE,
1673 POINTER_USAGE_GESTURES,
1674 POINTER_USAGE_STYLUS,
1675 POINTER_USAGE_MOUSE,
1676 };
1677 PointerUsage mPointerUsage;
1678
1679 struct PointerGesture {
1680 enum Mode {
1681 // No fingers, button is not pressed.
1682 // Nothing happening.
1683 NEUTRAL,
1684
1685 // No fingers, button is not pressed.
1686 // Tap detected.
1687 // Emits DOWN and UP events at the pointer location.
1688 TAP,
1689
1690 // Exactly one finger dragging following a tap.
1691 // Pointer follows the active finger.
1692 // Emits DOWN, MOVE and UP events at the pointer location.
1693 //
1694 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1695 TAP_DRAG,
1696
1697 // Button is pressed.
1698 // Pointer follows the active finger if there is one. Other fingers are ignored.
1699 // Emits DOWN, MOVE and UP events at the pointer location.
1700 BUTTON_CLICK_OR_DRAG,
1701
1702 // Exactly one finger, button is not pressed.
1703 // Pointer follows the active finger.
1704 // Emits HOVER_MOVE events at the pointer location.
1705 //
1706 // Detect taps when the finger goes up while in HOVER mode.
1707 HOVER,
1708
1709 // Exactly two fingers but neither have moved enough to clearly indicate
1710 // whether a swipe or freeform gesture was intended. We consider the
1711 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1712 // Pointer does not move.
1713 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1714 PRESS,
1715
1716 // Exactly two fingers moving in the same direction, button is not pressed.
1717 // Pointer does not move.
1718 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1719 // follows the midpoint between both fingers.
1720 SWIPE,
1721
1722 // Two or more fingers moving in arbitrary directions, button is not pressed.
1723 // Pointer does not move.
1724 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1725 // each finger individually relative to the initial centroid of the finger.
1726 FREEFORM,
1727
1728 // Waiting for quiet time to end before starting the next gesture.
1729 QUIET,
1730 };
1731
1732 // Time the first finger went down.
1733 nsecs_t firstTouchTime;
1734
1735 // The active pointer id from the raw touch data.
1736 int32_t activeTouchId; // -1 if none
1737
1738 // The active pointer id from the gesture last delivered to the application.
1739 int32_t activeGestureId; // -1 if none
1740
1741 // Pointer coords and ids for the current and previous pointer gesture.
1742 Mode currentGestureMode;
1743 BitSet32 currentGestureIdBits;
1744 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1745 PointerProperties currentGestureProperties[MAX_POINTERS];
1746 PointerCoords currentGestureCoords[MAX_POINTERS];
1747
1748 Mode lastGestureMode;
1749 BitSet32 lastGestureIdBits;
1750 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1751 PointerProperties lastGestureProperties[MAX_POINTERS];
1752 PointerCoords lastGestureCoords[MAX_POINTERS];
1753
1754 // Time the pointer gesture last went down.
1755 nsecs_t downTime;
1756
1757 // Time when the pointer went down for a TAP.
1758 nsecs_t tapDownTime;
1759
1760 // Time when the pointer went up for a TAP.
1761 nsecs_t tapUpTime;
1762
1763 // Location of initial tap.
1764 float tapX, tapY;
1765
1766 // Time we started waiting for quiescence.
1767 nsecs_t quietTime;
1768
1769 // Reference points for multitouch gestures.
1770 float referenceTouchX; // reference touch X/Y coordinates in surface units
1771 float referenceTouchY;
1772 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1773 float referenceGestureY;
1774
1775 // Distance that each pointer has traveled which has not yet been
1776 // subsumed into the reference gesture position.
1777 BitSet32 referenceIdBits;
1778 struct Delta {
1779 float dx, dy;
1780 };
1781 Delta referenceDeltas[MAX_POINTER_ID + 1];
1782
1783 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1784 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1785
1786 // A velocity tracker for determining whether to switch active pointers during drags.
1787 VelocityTracker velocityTracker;
1788
1789 void reset() {
1790 firstTouchTime = LLONG_MIN;
1791 activeTouchId = -1;
1792 activeGestureId = -1;
1793 currentGestureMode = NEUTRAL;
1794 currentGestureIdBits.clear();
1795 lastGestureMode = NEUTRAL;
1796 lastGestureIdBits.clear();
1797 downTime = 0;
1798 velocityTracker.clear();
1799 resetTap();
1800 resetQuietTime();
1801 }
1802
1803 void resetTap() {
1804 tapDownTime = LLONG_MIN;
1805 tapUpTime = LLONG_MIN;
1806 }
1807
1808 void resetQuietTime() {
1809 quietTime = LLONG_MIN;
1810 }
1811 } mPointerGesture;
1812
1813 struct PointerSimple {
1814 PointerCoords currentCoords;
1815 PointerProperties currentProperties;
1816 PointerCoords lastCoords;
1817 PointerProperties lastProperties;
1818
1819 // True if the pointer is down.
1820 bool down;
1821
1822 // True if the pointer is hovering.
1823 bool hovering;
1824
1825 // Time the pointer last went down.
1826 nsecs_t downTime;
1827
1828 void reset() {
1829 currentCoords.clear();
1830 currentProperties.clear();
1831 lastCoords.clear();
1832 lastProperties.clear();
1833 down = false;
1834 hovering = false;
1835 downTime = 0;
1836 }
1837 } mPointerSimple;
1838
1839 // The pointer and scroll velocity controls.
1840 VelocityControl mPointerVelocityControl;
1841 VelocityControl mWheelXVelocityControl;
1842 VelocityControl mWheelYVelocityControl;
1843
Michael Wright842500e2015-03-13 17:32:02 -07001844 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001845 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001846
Michael Wrightd02c5b62014-02-10 15:10:22 -08001847 void sync(nsecs_t when);
1848
1849 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001850 void processRawTouches(bool timeout);
1851 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1853 int32_t keyEventAction, int32_t keyEventFlags);
1854
1855 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1856 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1857 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001858 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1859 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1860 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001862 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863
1864 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1865 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1866
1867 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1868 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1869 bool preparePointerGestures(nsecs_t when,
1870 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1871 bool isTimeout);
1872
1873 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1874 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1875
1876 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1877 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1878
1879 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1880 bool down, bool hovering);
1881 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1882
Michael Wright842500e2015-03-13 17:32:02 -07001883 bool assignExternalStylusId(const RawState& state, bool timeout);
1884 void applyExternalStylusButtonState(nsecs_t when);
1885 void applyExternalStylusTouchState(nsecs_t when);
1886
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887 // Dispatches a motion event.
1888 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1889 // method will take care of setting the index and transmuting the action to DOWN or UP
1890 // it is the first / last pointer to go down / up.
1891 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001892 int32_t action, int32_t actionButton,
1893 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 const PointerProperties* properties, const PointerCoords* coords,
1895 const uint32_t* idToIndex, BitSet32 idBits,
1896 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1897
1898 // Updates pointer coords and properties for pointers with specified ids that have moved.
1899 // Returns true if any of them changed.
1900 bool updateMovedPointers(const PointerProperties* inProperties,
1901 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1902 PointerProperties* outProperties, PointerCoords* outCoords,
1903 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1904
1905 bool isPointInsideSurface(int32_t x, int32_t y);
1906 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1907
Michael Wright842500e2015-03-13 17:32:02 -07001908 static void assignPointerIds(const RawState* last, RawState* current);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909};
1910
1911
1912class SingleTouchInputMapper : public TouchInputMapper {
1913public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001914 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 virtual ~SingleTouchInputMapper();
1916
1917 virtual void reset(nsecs_t when);
1918 virtual void process(const RawEvent* rawEvent);
1919
1920protected:
Michael Wright842500e2015-03-13 17:32:02 -07001921 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922 virtual void configureRawPointerAxes();
1923 virtual bool hasStylus() const;
1924
1925private:
1926 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1927};
1928
1929
1930class MultiTouchInputMapper : public TouchInputMapper {
1931public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001932 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 virtual ~MultiTouchInputMapper();
1934
1935 virtual void reset(nsecs_t when);
1936 virtual void process(const RawEvent* rawEvent);
1937
1938protected:
Michael Wright842500e2015-03-13 17:32:02 -07001939 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940 virtual void configureRawPointerAxes();
1941 virtual bool hasStylus() const;
1942
1943private:
1944 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1945
1946 // Specifies the pointer id bits that are in use, and their associated tracking id.
1947 BitSet32 mPointerIdBits;
1948 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1949};
1950
Michael Wright842500e2015-03-13 17:32:02 -07001951class ExternalStylusInputMapper : public InputMapper {
1952public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001953 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001954 virtual ~ExternalStylusInputMapper() = default;
1955
1956 virtual uint32_t getSources();
1957 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1958 virtual void dump(String8& dump);
1959 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1960 virtual void reset(nsecs_t when);
1961 virtual void process(const RawEvent* rawEvent);
1962 virtual void sync(nsecs_t when);
1963
1964private:
1965 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1966 RawAbsoluteAxisInfo mRawPressureAxis;
1967 TouchButtonAccumulator mTouchButtonAccumulator;
1968
1969 StylusState mStylusState;
1970};
1971
Michael Wrightd02c5b62014-02-10 15:10:22 -08001972
1973class JoystickInputMapper : public InputMapper {
1974public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001975 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001976 virtual ~JoystickInputMapper();
1977
1978 virtual uint32_t getSources();
1979 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1980 virtual void dump(String8& dump);
1981 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1982 virtual void reset(nsecs_t when);
1983 virtual void process(const RawEvent* rawEvent);
1984
1985private:
1986 struct Axis {
1987 RawAbsoluteAxisInfo rawAxisInfo;
1988 AxisInfo axisInfo;
1989
1990 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1991
1992 float scale; // scale factor from raw to normalized values
1993 float offset; // offset to add after scaling for normalization
1994 float highScale; // scale factor from raw to normalized values of high split
1995 float highOffset; // offset to add after scaling for normalization of high split
1996
1997 float min; // normalized inclusive minimum
1998 float max; // normalized inclusive maximum
1999 float flat; // normalized flat region size
2000 float fuzz; // normalized error tolerance
2001 float resolution; // normalized resolution in units/mm
2002
2003 float filter; // filter out small variations of this size
2004 float currentValue; // current value
2005 float newValue; // most recent value
2006 float highCurrentValue; // current value of high split
2007 float highNewValue; // most recent value of high split
2008
2009 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
2010 bool explicitlyMapped, float scale, float offset,
2011 float highScale, float highOffset,
2012 float min, float max, float flat, float fuzz, float resolution) {
2013 this->rawAxisInfo = rawAxisInfo;
2014 this->axisInfo = axisInfo;
2015 this->explicitlyMapped = explicitlyMapped;
2016 this->scale = scale;
2017 this->offset = offset;
2018 this->highScale = highScale;
2019 this->highOffset = highOffset;
2020 this->min = min;
2021 this->max = max;
2022 this->flat = flat;
2023 this->fuzz = fuzz;
2024 this->resolution = resolution;
2025 this->filter = 0;
2026 resetValue();
2027 }
2028
2029 void resetValue() {
2030 this->currentValue = 0;
2031 this->newValue = 0;
2032 this->highCurrentValue = 0;
2033 this->highNewValue = 0;
2034 }
2035 };
2036
2037 // Axes indexed by raw ABS_* axis index.
2038 KeyedVector<int32_t, Axis> mAxes;
2039
2040 void sync(nsecs_t when, bool force);
2041
2042 bool haveAxis(int32_t axisId);
2043 void pruneAxes(bool ignoreExplicitlyMappedAxes);
2044 bool filterAxes(bool force);
2045
2046 static bool hasValueChangedSignificantly(float filter,
2047 float newValue, float currentValue, float min, float max);
2048 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
2049 float newValue, float currentValue, float thresholdValue);
2050
2051 static bool isCenteredAxis(int32_t axis);
2052 static int32_t getCompatAxis(int32_t axis);
2053
2054 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
2055 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
2056 float value);
2057};
2058
2059} // namespace android
2060
2061#endif // _UI_INPUT_READER_H