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