blob: 89290a82e3cb7fef49e71836fdc48b2b3f8f4e9f [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
Santos Cordonfa5cf462017-04-05 10:37:00 -070024#include <input/DisplayViewport.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025#include <input/Input.h>
26#include <input/VelocityControl.h>
27#include <input/VelocityTracker.h>
28#include <ui/DisplayInfo.h>
29#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070030#include <utils/Condition.h>
31#include <utils/Thread.h>
32#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <utils/Timers.h>
34#include <utils/RefBase.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <utils/BitSet.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070036#include <utils/SortedVector.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010038#include <optional>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <stddef.h>
40#include <unistd.h>
41
42// Maximum supported size of a vibration pattern.
43// Must be at least 2.
44#define MAX_VIBRATE_PATTERN_SIZE 100
45
46// Maximum allowable delay value in a vibration pattern before
47// which the delay will be truncated.
48#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
49
50namespace android {
51
52class InputDevice;
53class InputMapper;
54
55/*
Michael Wrightd02c5b62014-02-10 15:10:22 -080056 * Input reader configuration.
57 *
58 * Specifies various options that modify the behavior of the input reader.
59 */
60struct InputReaderConfiguration {
61 // Describes changes that have occurred.
62 enum {
63 // The pointer speed changed.
64 CHANGE_POINTER_SPEED = 1 << 0,
65
66 // The pointer gesture control changed.
67 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
68
69 // The display size or orientation changed.
70 CHANGE_DISPLAY_INFO = 1 << 2,
71
72 // The visible touches option changed.
73 CHANGE_SHOW_TOUCHES = 1 << 3,
74
75 // The keyboard layouts must be reloaded.
76 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
77
78 // The device name alias supplied by the may have changed for some devices.
79 CHANGE_DEVICE_ALIAS = 1 << 5,
80
Jason Gerecke12d6baa2014-01-27 18:34:20 -080081 // The location calibration matrix changed.
Michael Wright842500e2015-03-13 17:32:02 -070082 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
83
84 // The presence of an external stylus has changed.
85 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
Jason Gerecke12d6baa2014-01-27 18:34:20 -080086
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -080087 // The pointer capture mode has changed.
88 CHANGE_POINTER_CAPTURE = 1 << 8,
89
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070090 // The set of disabled input devices (disabledDevices) has changed.
91 CHANGE_ENABLED_STATE = 1 << 9,
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093 // All devices must be reopened.
94 CHANGE_MUST_REOPEN = 1 << 31,
95 };
96
97 // Gets the amount of time to disable virtual keys after the screen is touched
98 // in order to filter out accidental virtual key presses due to swiping gestures
99 // or taps near the edge of the display. May be 0 to disable the feature.
100 nsecs_t virtualKeyQuietTime;
101
102 // The excluded device names for the platform.
103 // Devices with these names will be ignored.
104 Vector<String8> excludedDeviceNames;
105
106 // Velocity control parameters for mouse pointer movements.
107 VelocityControlParameters pointerVelocityControlParameters;
108
109 // Velocity control parameters for mouse wheel movements.
110 VelocityControlParameters wheelVelocityControlParameters;
111
112 // True if pointer gestures are enabled.
113 bool pointerGesturesEnabled;
114
115 // Quiet time between certain pointer gesture transitions.
116 // Time to allow for all fingers or buttons to settle into a stable state before
117 // starting a new gesture.
118 nsecs_t pointerGestureQuietInterval;
119
120 // The minimum speed that a pointer must travel for us to consider switching the active
121 // touch pointer to it during a drag. This threshold is set to avoid switching due
122 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
123 float pointerGestureDragMinSwitchSpeed; // in pixels per second
124
125 // Tap gesture delay time.
126 // The time between down and up must be less than this to be considered a tap.
127 nsecs_t pointerGestureTapInterval;
128
129 // Tap drag gesture delay time.
130 // The time between the previous tap's up and the next down must be less than
131 // this to be considered a drag. Otherwise, the previous tap is finished and a
132 // new tap begins.
133 //
134 // Note that the previous tap will be held down for this entire duration so this
135 // interval must be shorter than the long press timeout.
136 nsecs_t pointerGestureTapDragInterval;
137
138 // The distance in pixels that the pointer is allowed to move from initial down
139 // to up and still be called a tap.
140 float pointerGestureTapSlop; // in pixels
141
142 // Time after the first touch points go down to settle on an initial centroid.
143 // This is intended to be enough time to handle cases where the user puts down two
144 // fingers at almost but not quite exactly the same time.
145 nsecs_t pointerGestureMultitouchSettleInterval;
146
147 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
148 // at least two pointers have moved at least this far from their starting place.
149 float pointerGestureMultitouchMinDistance; // in pixels
150
151 // The transition from PRESS to SWIPE gesture mode can only occur when the
152 // cosine of the angle between the two vectors is greater than or equal to than this value
153 // which indicates that the vectors are oriented in the same direction.
154 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
155 // (In exactly opposite directions, the cosine is -1.0.)
156 float pointerGestureSwipeTransitionAngleCosine;
157
158 // The transition from PRESS to SWIPE gesture mode can only occur when the
159 // fingers are no more than this far apart relative to the diagonal size of
160 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
161 // no more than half the diagonal size of the touch pad apart.
162 float pointerGestureSwipeMaxWidthRatio;
163
164 // The gesture movement speed factor relative to the size of the display.
165 // Movement speed applies when the fingers are moving in the same direction.
166 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
167 // will cover this portion of the display diagonal.
168 float pointerGestureMovementSpeedRatio;
169
170 // The gesture zoom speed factor relative to the size of the display.
171 // Zoom speed applies when the fingers are mostly moving relative to each other
172 // to execute a scale gesture or similar.
173 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
174 // will cover this portion of the display diagonal.
175 float pointerGestureZoomSpeedRatio;
176
177 // True to show the location of touches on the touch screen as spots.
178 bool showTouches;
179
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800180 // True if pointer capture is enabled.
181 bool pointerCapture;
182
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700183 // The set of currently disabled input devices.
184 SortedVector<int32_t> disabledDevices;
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 InputReaderConfiguration() :
187 virtualKeyQuietTime(0),
188 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
189 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
190 pointerGesturesEnabled(true),
191 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
192 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
193 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
194 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
195 pointerGestureTapSlop(10.0f), // 10 pixels
196 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
197 pointerGestureMultitouchMinDistance(15), // 15 pixels
198 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
199 pointerGestureSwipeMaxWidthRatio(0.25f),
200 pointerGestureMovementSpeedRatio(0.8f),
201 pointerGestureZoomSpeedRatio(0.3f),
202 showTouches(false) { }
203
Santos Cordonfa5cf462017-04-05 10:37:00 -0700204 bool getDisplayViewport(ViewportType viewportType, const String8* displayId,
205 DisplayViewport* outViewport) const;
206 void setPhysicalDisplayViewport(ViewportType viewportType, const DisplayViewport& viewport);
207 void setVirtualDisplayViewports(const Vector<DisplayViewport>& viewports);
208
209
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800210 void dump(std::string& dump) const;
211 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
213private:
214 DisplayViewport mInternalDisplay;
215 DisplayViewport mExternalDisplay;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700216 Vector<DisplayViewport> mVirtualDisplays;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217};
218
219
Jason Gereckeaf126fb2012-05-10 14:22:47 -0700220struct TouchAffineTransformation {
221 float x_scale;
222 float x_ymix;
223 float x_offset;
224 float y_xmix;
225 float y_scale;
226 float y_offset;
227
228 TouchAffineTransformation() :
229 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
230 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
231 }
232
Jason Gerecke489fda82012-09-07 17:19:40 -0700233 TouchAffineTransformation(float xscale, float xymix, float xoffset,
234 float yxmix, float yscale, float yoffset) :
235 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
236 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
237 }
238
Jason Gereckeaf126fb2012-05-10 14:22:47 -0700239 void applyTo(float& x, float& y) const;
240};
241
242
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243/*
244 * Input reader policy interface.
245 *
246 * The input reader policy is used by the input reader to interact with the Window Manager
247 * and other system components.
248 *
249 * The actual implementation is partially supported by callbacks into the DVM
250 * via JNI. This interface is also mocked in the unit tests.
251 *
252 * These methods must NOT re-enter the input reader since they may be called while
253 * holding the input reader lock.
254 */
255class InputReaderPolicyInterface : public virtual RefBase {
256protected:
257 InputReaderPolicyInterface() { }
258 virtual ~InputReaderPolicyInterface() { }
259
260public:
261 /* Gets the input reader configuration. */
262 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
263
264 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
265 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
266
267 /* Notifies the input reader policy that some input devices have changed
268 * and provides information about all current input devices.
269 */
270 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
271
272 /* Gets the keyboard layout for a particular input device. */
273 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
274 const InputDeviceIdentifier& identifier) = 0;
275
276 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
277 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800278
279 /* Gets the affine calibration associated with the specified device. */
280 virtual TouchAffineTransformation getTouchAffineTransformation(
Jason Gerecke71b16e82014-03-10 09:47:59 -0700281 const String8& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282};
283
284
285/* Processes raw input events and sends cooked event data to an input listener. */
286class InputReaderInterface : public virtual RefBase {
287protected:
288 InputReaderInterface() { }
289 virtual ~InputReaderInterface() { }
290
291public:
292 /* Dumps the state of the input reader.
293 *
294 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800295 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296
297 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
298 virtual void monitor() = 0;
299
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700300 /* Returns true if the input device is enabled. */
301 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
302
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 /* Runs a single iteration of the processing loop.
304 * Nominally reads and processes one incoming message from the EventHub.
305 *
306 * This method should be called on the input reader thread.
307 */
308 virtual void loopOnce() = 0;
309
310 /* Gets information about all input devices.
311 *
312 * This method may be called on any thread (usually by the input manager).
313 */
314 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
315
316 /* Query current input state. */
317 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
318 int32_t scanCode) = 0;
319 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
320 int32_t keyCode) = 0;
321 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
322 int32_t sw) = 0;
323
Andrii Kulian763a3a42016-03-08 10:46:16 -0800324 /* Toggle Caps Lock */
325 virtual void toggleCapsLockState(int32_t deviceId) = 0;
326
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 /* Determine whether physical keys exist for the given framework-domain key codes. */
328 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
329 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
330
331 /* Requests that a reconfiguration of all input devices.
332 * The changes flag is a bitfield that indicates what has changed and whether
333 * the input devices must all be reopened. */
334 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
335
336 /* Controls the vibrator of a particular input device. */
337 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
338 ssize_t repeat, int32_t token) = 0;
339 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
340};
341
Michael Wright842500e2015-03-13 17:32:02 -0700342struct StylusState {
343 /* Time the stylus event was received. */
344 nsecs_t when;
345 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
346 float pressure;
347 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
348 uint32_t buttons;
349 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
350 int32_t toolType;
351
352 void copyFrom(const StylusState& other) {
353 when = other.when;
354 pressure = other.pressure;
355 buttons = other.buttons;
356 toolType = other.toolType;
357 }
358
359 void clear() {
360 when = LLONG_MAX;
361 pressure = 0.f;
362 buttons = 0;
363 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
364 }
365};
366
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367
368/* Internal interface used by individual input devices to access global input device state
369 * and parameters maintained by the input reader.
370 */
371class InputReaderContext {
372public:
373 InputReaderContext() { }
374 virtual ~InputReaderContext() { }
375
376 virtual void updateGlobalMetaState() = 0;
377 virtual int32_t getGlobalMetaState() = 0;
378
379 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
380 virtual bool shouldDropVirtualKey(nsecs_t now,
381 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
382
383 virtual void fadePointer() = 0;
384
385 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
386 virtual int32_t bumpGeneration() = 0;
387
Michael Wrightb85401d2015-04-17 18:35:15 +0100388 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
389 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -0700390
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 virtual InputReaderPolicyInterface* getPolicy() = 0;
392 virtual InputListenerInterface* getListener() = 0;
393 virtual EventHubInterface* getEventHub() = 0;
394};
395
396
397/* The input reader reads raw event data from the event hub and processes it into input events
398 * that it sends to the input listener. Some functions of the input reader, such as early
399 * event filtering in low power states, are controlled by a separate policy object.
400 *
401 * The InputReader owns a collection of InputMappers. Most of the work it does happens
402 * on the input reader thread but the InputReader can receive queries from other system
403 * components running on arbitrary threads. To keep things manageable, the InputReader
404 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
405 * EventHub or the InputReaderPolicy but it is never held while calling into the
406 * InputListener.
407 */
408class InputReader : public InputReaderInterface {
409public:
410 InputReader(const sp<EventHubInterface>& eventHub,
411 const sp<InputReaderPolicyInterface>& policy,
412 const sp<InputListenerInterface>& listener);
413 virtual ~InputReader();
414
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800415 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 virtual void monitor();
417
418 virtual void loopOnce();
419
420 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
421
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700422 virtual bool isInputDeviceEnabled(int32_t deviceId);
423
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
425 int32_t scanCode);
426 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
427 int32_t keyCode);
428 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
429 int32_t sw);
430
Andrii Kulian763a3a42016-03-08 10:46:16 -0800431 virtual void toggleCapsLockState(int32_t deviceId);
432
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
434 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
435
436 virtual void requestRefreshConfiguration(uint32_t changes);
437
438 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
439 ssize_t repeat, int32_t token);
440 virtual void cancelVibrate(int32_t deviceId, int32_t token);
441
442protected:
443 // These members are protected so they can be instrumented by test cases.
444 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
445 const InputDeviceIdentifier& identifier, uint32_t classes);
446
447 class ContextImpl : public InputReaderContext {
448 InputReader* mReader;
449
450 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700451 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
453 virtual void updateGlobalMetaState();
454 virtual int32_t getGlobalMetaState();
455 virtual void disableVirtualKeysUntil(nsecs_t time);
456 virtual bool shouldDropVirtualKey(nsecs_t now,
457 InputDevice* device, int32_t keyCode, int32_t scanCode);
458 virtual void fadePointer();
459 virtual void requestTimeoutAtTime(nsecs_t when);
460 virtual int32_t bumpGeneration();
Michael Wright842500e2015-03-13 17:32:02 -0700461 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
462 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 virtual InputReaderPolicyInterface* getPolicy();
464 virtual InputListenerInterface* getListener();
465 virtual EventHubInterface* getEventHub();
466 } mContext;
467
468 friend class ContextImpl;
469
470private:
471 Mutex mLock;
472
473 Condition mReaderIsAliveCondition;
474
475 sp<EventHubInterface> mEventHub;
476 sp<InputReaderPolicyInterface> mPolicy;
477 sp<QueuedInputListener> mQueuedListener;
478
479 InputReaderConfiguration mConfig;
480
481 // The event queue.
482 static const int EVENT_BUFFER_SIZE = 256;
483 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
484
485 KeyedVector<int32_t, InputDevice*> mDevices;
486
487 // low-level input event decoding and device management
488 void processEventsLocked(const RawEvent* rawEvents, size_t count);
489
490 void addDeviceLocked(nsecs_t when, int32_t deviceId);
491 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
492 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
493 void timeoutExpiredLocked(nsecs_t when);
494
495 void handleConfigurationChangedLocked(nsecs_t when);
496
497 int32_t mGlobalMetaState;
498 void updateGlobalMetaStateLocked();
499 int32_t getGlobalMetaStateLocked();
500
Michael Wright842500e2015-03-13 17:32:02 -0700501 void notifyExternalStylusPresenceChanged();
502 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
503 void dispatchExternalStylusState(const StylusState& state);
504
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 void fadePointerLocked();
506
507 int32_t mGeneration;
508 int32_t bumpGenerationLocked();
509
510 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
511
512 nsecs_t mDisableVirtualKeysTimeout;
513 void disableVirtualKeysUntilLocked(nsecs_t time);
514 bool shouldDropVirtualKeyLocked(nsecs_t now,
515 InputDevice* device, int32_t keyCode, int32_t scanCode);
516
517 nsecs_t mNextTimeout;
518 void requestTimeoutAtTimeLocked(nsecs_t when);
519
520 uint32_t mConfigurationChangesToRefresh;
521 void refreshConfigurationLocked(uint32_t changes);
522
523 // state queries
524 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
525 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
526 GetStateFunc getStateFunc);
527 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
528 const int32_t* keyCodes, uint8_t* outFlags);
529};
530
531
532/* Reads raw events from the event hub and processes them, endlessly. */
533class InputReaderThread : public Thread {
534public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700535 explicit InputReaderThread(const sp<InputReaderInterface>& reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 virtual ~InputReaderThread();
537
538private:
539 sp<InputReaderInterface> mReader;
540
541 virtual bool threadLoop();
542};
543
544
545/* Represents the state of a single input device. */
546class InputDevice {
547public:
548 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
549 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
550 ~InputDevice();
551
552 inline InputReaderContext* getContext() { return mContext; }
553 inline int32_t getId() const { return mId; }
554 inline int32_t getControllerNumber() const { return mControllerNumber; }
555 inline int32_t getGeneration() const { return mGeneration; }
556 inline const String8& getName() const { return mIdentifier.name; }
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800557 inline const String8& getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 inline uint32_t getClasses() const { return mClasses; }
559 inline uint32_t getSources() const { return mSources; }
560
561 inline bool isExternal() { return mIsExternal; }
562 inline void setExternal(bool external) { mIsExternal = external; }
563
Tim Kilbourn063ff532015-04-08 10:26:18 -0700564 inline void setMic(bool hasMic) { mHasMic = hasMic; }
565 inline bool hasMic() const { return mHasMic; }
566
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 inline bool isIgnored() { return mMappers.isEmpty(); }
568
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700569 bool isEnabled();
570 void setEnabled(bool enabled, nsecs_t when);
571
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800572 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573 void addMapper(InputMapper* mapper);
574 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
575 void reset(nsecs_t when);
576 void process(const RawEvent* rawEvents, size_t count);
577 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700578 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579
580 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
581 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
582 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
583 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
584 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
585 const int32_t* keyCodes, uint8_t* outFlags);
586 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
587 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800588 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589
590 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800591 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
593 void fadePointer();
594
595 void bumpGeneration();
596
597 void notifyReset(nsecs_t when);
598
599 inline const PropertyMap& getConfiguration() { return mConfiguration; }
600 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
601
602 bool hasKey(int32_t code) {
603 return getEventHub()->hasScanCode(mId, code);
604 }
605
606 bool hasAbsoluteAxis(int32_t code) {
607 RawAbsoluteAxisInfo info;
608 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
609 return info.valid;
610 }
611
612 bool isKeyPressed(int32_t code) {
613 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
614 }
615
616 int32_t getAbsoluteAxisValue(int32_t code) {
617 int32_t value;
618 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
619 return value;
620 }
621
622private:
623 InputReaderContext* mContext;
624 int32_t mId;
625 int32_t mGeneration;
626 int32_t mControllerNumber;
627 InputDeviceIdentifier mIdentifier;
628 String8 mAlias;
629 uint32_t mClasses;
630
631 Vector<InputMapper*> mMappers;
632
633 uint32_t mSources;
634 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700635 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 bool mDropUntilNextSync;
637
638 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
639 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
640
641 PropertyMap mConfiguration;
642};
643
644
645/* Keeps track of the state of mouse or touch pad buttons. */
646class CursorButtonAccumulator {
647public:
648 CursorButtonAccumulator();
649 void reset(InputDevice* device);
650
651 void process(const RawEvent* rawEvent);
652
653 uint32_t getButtonState() const;
654
655private:
656 bool mBtnLeft;
657 bool mBtnRight;
658 bool mBtnMiddle;
659 bool mBtnBack;
660 bool mBtnSide;
661 bool mBtnForward;
662 bool mBtnExtra;
663 bool mBtnTask;
664
665 void clearButtons();
666};
667
668
669/* Keeps track of cursor movements. */
670
671class CursorMotionAccumulator {
672public:
673 CursorMotionAccumulator();
674 void reset(InputDevice* device);
675
676 void process(const RawEvent* rawEvent);
677 void finishSync();
678
679 inline int32_t getRelativeX() const { return mRelX; }
680 inline int32_t getRelativeY() const { return mRelY; }
681
682private:
683 int32_t mRelX;
684 int32_t mRelY;
685
686 void clearRelativeAxes();
687};
688
689
690/* Keeps track of cursor scrolling motions. */
691
692class CursorScrollAccumulator {
693public:
694 CursorScrollAccumulator();
695 void configure(InputDevice* device);
696 void reset(InputDevice* device);
697
698 void process(const RawEvent* rawEvent);
699 void finishSync();
700
701 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
702 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
703
704 inline int32_t getRelativeX() const { return mRelX; }
705 inline int32_t getRelativeY() const { return mRelY; }
706 inline int32_t getRelativeVWheel() const { return mRelWheel; }
707 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
708
709private:
710 bool mHaveRelWheel;
711 bool mHaveRelHWheel;
712
713 int32_t mRelX;
714 int32_t mRelY;
715 int32_t mRelWheel;
716 int32_t mRelHWheel;
717
718 void clearRelativeAxes();
719};
720
721
722/* Keeps track of the state of touch, stylus and tool buttons. */
723class TouchButtonAccumulator {
724public:
725 TouchButtonAccumulator();
726 void configure(InputDevice* device);
727 void reset(InputDevice* device);
728
729 void process(const RawEvent* rawEvent);
730
731 uint32_t getButtonState() const;
732 int32_t getToolType() const;
733 bool isToolActive() const;
734 bool isHovering() const;
735 bool hasStylus() const;
736
737private:
738 bool mHaveBtnTouch;
739 bool mHaveStylus;
740
741 bool mBtnTouch;
742 bool mBtnStylus;
743 bool mBtnStylus2;
744 bool mBtnToolFinger;
745 bool mBtnToolPen;
746 bool mBtnToolRubber;
747 bool mBtnToolBrush;
748 bool mBtnToolPencil;
749 bool mBtnToolAirbrush;
750 bool mBtnToolMouse;
751 bool mBtnToolLens;
752 bool mBtnToolDoubleTap;
753 bool mBtnToolTripleTap;
754 bool mBtnToolQuadTap;
755
756 void clearButtons();
757};
758
759
760/* Raw axis information from the driver. */
761struct RawPointerAxes {
762 RawAbsoluteAxisInfo x;
763 RawAbsoluteAxisInfo y;
764 RawAbsoluteAxisInfo pressure;
765 RawAbsoluteAxisInfo touchMajor;
766 RawAbsoluteAxisInfo touchMinor;
767 RawAbsoluteAxisInfo toolMajor;
768 RawAbsoluteAxisInfo toolMinor;
769 RawAbsoluteAxisInfo orientation;
770 RawAbsoluteAxisInfo distance;
771 RawAbsoluteAxisInfo tiltX;
772 RawAbsoluteAxisInfo tiltY;
773 RawAbsoluteAxisInfo trackingId;
774 RawAbsoluteAxisInfo slot;
775
776 RawPointerAxes();
777 void clear();
778};
779
780
781/* Raw data for a collection of pointers including a pointer id mapping table. */
782struct RawPointerData {
783 struct Pointer {
784 uint32_t id;
785 int32_t x;
786 int32_t y;
787 int32_t pressure;
788 int32_t touchMajor;
789 int32_t touchMinor;
790 int32_t toolMajor;
791 int32_t toolMinor;
792 int32_t orientation;
793 int32_t distance;
794 int32_t tiltX;
795 int32_t tiltY;
796 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
797 bool isHovering;
798 };
799
800 uint32_t pointerCount;
801 Pointer pointers[MAX_POINTERS];
802 BitSet32 hoveringIdBits, touchingIdBits;
803 uint32_t idToIndex[MAX_POINTER_ID + 1];
804
805 RawPointerData();
806 void clear();
807 void copyFrom(const RawPointerData& other);
808 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
809
810 inline void markIdBit(uint32_t id, bool isHovering) {
811 if (isHovering) {
812 hoveringIdBits.markBit(id);
813 } else {
814 touchingIdBits.markBit(id);
815 }
816 }
817
818 inline void clearIdBits() {
819 hoveringIdBits.clear();
820 touchingIdBits.clear();
821 }
822
823 inline const Pointer& pointerForId(uint32_t id) const {
824 return pointers[idToIndex[id]];
825 }
826
827 inline bool isHovering(uint32_t pointerIndex) {
828 return pointers[pointerIndex].isHovering;
829 }
830};
831
832
833/* Cooked data for a collection of pointers including a pointer id mapping table. */
834struct CookedPointerData {
835 uint32_t pointerCount;
836 PointerProperties pointerProperties[MAX_POINTERS];
837 PointerCoords pointerCoords[MAX_POINTERS];
838 BitSet32 hoveringIdBits, touchingIdBits;
839 uint32_t idToIndex[MAX_POINTER_ID + 1];
840
841 CookedPointerData();
842 void clear();
843 void copyFrom(const CookedPointerData& other);
844
845 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
846 return pointerCoords[idToIndex[id]];
847 }
848
Michael Wright842500e2015-03-13 17:32:02 -0700849 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
850 return pointerCoords[idToIndex[id]];
851 }
852
853 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
854 return pointerProperties[idToIndex[id]];
855 }
856
Michael Wright53dca3a2015-04-23 17:39:53 +0100857 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
859 }
Michael Wright842500e2015-03-13 17:32:02 -0700860
Michael Wright53dca3a2015-04-23 17:39:53 +0100861 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700862 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
863 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864};
865
866
867/* Keeps track of the state of single-touch protocol. */
868class SingleTouchMotionAccumulator {
869public:
870 SingleTouchMotionAccumulator();
871
872 void process(const RawEvent* rawEvent);
873 void reset(InputDevice* device);
874
875 inline int32_t getAbsoluteX() const { return mAbsX; }
876 inline int32_t getAbsoluteY() const { return mAbsY; }
877 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
878 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
879 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
880 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
881 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
882
883private:
884 int32_t mAbsX;
885 int32_t mAbsY;
886 int32_t mAbsPressure;
887 int32_t mAbsToolWidth;
888 int32_t mAbsDistance;
889 int32_t mAbsTiltX;
890 int32_t mAbsTiltY;
891
892 void clearAbsoluteAxes();
893};
894
895
896/* Keeps track of the state of multi-touch protocol. */
897class MultiTouchMotionAccumulator {
898public:
899 class Slot {
900 public:
901 inline bool isInUse() const { return mInUse; }
902 inline int32_t getX() const { return mAbsMTPositionX; }
903 inline int32_t getY() const { return mAbsMTPositionY; }
904 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
905 inline int32_t getTouchMinor() const {
906 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
907 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
908 inline int32_t getToolMinor() const {
909 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
910 inline int32_t getOrientation() const { return mAbsMTOrientation; }
911 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
912 inline int32_t getPressure() const { return mAbsMTPressure; }
913 inline int32_t getDistance() const { return mAbsMTDistance; }
914 inline int32_t getToolType() const;
915
916 private:
917 friend class MultiTouchMotionAccumulator;
918
919 bool mInUse;
920 bool mHaveAbsMTTouchMinor;
921 bool mHaveAbsMTWidthMinor;
922 bool mHaveAbsMTToolType;
923
924 int32_t mAbsMTPositionX;
925 int32_t mAbsMTPositionY;
926 int32_t mAbsMTTouchMajor;
927 int32_t mAbsMTTouchMinor;
928 int32_t mAbsMTWidthMajor;
929 int32_t mAbsMTWidthMinor;
930 int32_t mAbsMTOrientation;
931 int32_t mAbsMTTrackingId;
932 int32_t mAbsMTPressure;
933 int32_t mAbsMTDistance;
934 int32_t mAbsMTToolType;
935
936 Slot();
937 void clear();
938 };
939
940 MultiTouchMotionAccumulator();
941 ~MultiTouchMotionAccumulator();
942
943 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
944 void reset(InputDevice* device);
945 void process(const RawEvent* rawEvent);
946 void finishSync();
947 bool hasStylus() const;
948
949 inline size_t getSlotCount() const { return mSlotCount; }
950 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800951 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952
953private:
954 int32_t mCurrentSlot;
955 Slot* mSlots;
956 size_t mSlotCount;
957 bool mUsingSlotsProtocol;
958 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800959 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
961 void clearSlots(int32_t initialSlot);
962};
963
964
965/* An input mapper transforms raw input events into cooked event data.
966 * A single input device can have multiple associated input mappers in order to interpret
967 * different classes of events.
968 *
969 * InputMapper lifecycle:
970 * - create
971 * - configure with 0 changes
972 * - reset
973 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
974 * - reset
975 * - destroy
976 */
977class InputMapper {
978public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700979 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 virtual ~InputMapper();
981
982 inline InputDevice* getDevice() { return mDevice; }
983 inline int32_t getDeviceId() { return mDevice->getId(); }
984 inline const String8 getDeviceName() { return mDevice->getName(); }
985 inline InputReaderContext* getContext() { return mContext; }
986 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
987 inline InputListenerInterface* getListener() { return mContext->getListener(); }
988 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
989
990 virtual uint32_t getSources() = 0;
991 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800992 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
994 virtual void reset(nsecs_t when);
995 virtual void process(const RawEvent* rawEvent) = 0;
996 virtual void timeoutExpired(nsecs_t when);
997
998 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
999 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1000 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
1001 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1002 const int32_t* keyCodes, uint8_t* outFlags);
1003 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1004 int32_t token);
1005 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -08001006 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007
1008 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -08001009 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010
Michael Wright842500e2015-03-13 17:32:02 -07001011 virtual void updateExternalStylusState(const StylusState& state);
1012
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 virtual void fadePointer();
1014
1015protected:
1016 InputDevice* mDevice;
1017 InputReaderContext* mContext;
1018
1019 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
1020 void bumpGeneration();
1021
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001022 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001024 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025};
1026
1027
1028class SwitchInputMapper : public InputMapper {
1029public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001030 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 virtual ~SwitchInputMapper();
1032
1033 virtual uint32_t getSources();
1034 virtual void process(const RawEvent* rawEvent);
1035
1036 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001037 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
1039private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -07001040 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 uint32_t mUpdatedSwitchMask;
1042
1043 void processSwitch(int32_t switchCode, int32_t switchValue);
1044 void sync(nsecs_t when);
1045};
1046
1047
1048class VibratorInputMapper : public InputMapper {
1049public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001050 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 virtual ~VibratorInputMapper();
1052
1053 virtual uint32_t getSources();
1054 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1055 virtual void process(const RawEvent* rawEvent);
1056
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);
1060 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001061 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062
1063private:
1064 bool mVibrating;
1065 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
1066 size_t mPatternSize;
1067 ssize_t mRepeat;
1068 int32_t mToken;
1069 ssize_t mIndex;
1070 nsecs_t mNextStepTime;
1071
1072 void nextStep();
1073 void stopVibrating();
1074};
1075
1076
1077class KeyboardInputMapper : public InputMapper {
1078public:
1079 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
1080 virtual ~KeyboardInputMapper();
1081
1082 virtual uint32_t getSources();
1083 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001084 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1086 virtual void reset(nsecs_t when);
1087 virtual void process(const RawEvent* rawEvent);
1088
1089 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1090 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1091 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1092 const int32_t* keyCodes, uint8_t* outFlags);
1093
1094 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -08001095 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096
1097private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01001098 // The current viewport.
1099 std::optional<DisplayViewport> mViewport;
1100
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 struct KeyDown {
1102 int32_t keyCode;
1103 int32_t scanCode;
1104 };
1105
1106 uint32_t mSource;
1107 int32_t mKeyboardType;
1108
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 Vector<KeyDown> mKeyDowns; // keys that are down
1110 int32_t mMetaState;
1111 nsecs_t mDownTime; // time of most recent key down
1112
1113 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
1114
1115 struct LedState {
1116 bool avail; // led is available
1117 bool on; // we think the led is currently on
1118 };
1119 LedState mCapsLockLedState;
1120 LedState mNumLockLedState;
1121 LedState mScrollLockLedState;
1122
1123 // Immutable configuration parameters.
1124 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -07001126 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 } mParameters;
1128
1129 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001130 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01001132 int32_t getOrientation();
1133 int32_t getDisplayId();
1134
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +01001136 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -07001138 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139
Andrii Kulian763a3a42016-03-08 10:46:16 -08001140 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
1141
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 ssize_t findKeyDown(int32_t scanCode);
1143
1144 void resetLedState();
1145 void initializeLedState(LedState& ledState, int32_t led);
1146 void updateLedState(bool reset);
1147 void updateLedStateForModifier(LedState& ledState, int32_t led,
1148 int32_t modifier, bool reset);
1149};
1150
1151
1152class CursorInputMapper : public InputMapper {
1153public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001154 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155 virtual ~CursorInputMapper();
1156
1157 virtual uint32_t getSources();
1158 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001159 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1161 virtual void reset(nsecs_t when);
1162 virtual void process(const RawEvent* rawEvent);
1163
1164 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1165
1166 virtual void fadePointer();
1167
1168private:
1169 // Amount that trackball needs to move in order to generate a key event.
1170 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1171
1172 // Immutable configuration parameters.
1173 struct Parameters {
1174 enum Mode {
1175 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001176 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 MODE_NAVIGATION,
1178 };
1179
1180 Mode mode;
1181 bool hasAssociatedDisplay;
1182 bool orientationAware;
1183 } mParameters;
1184
1185 CursorButtonAccumulator mCursorButtonAccumulator;
1186 CursorMotionAccumulator mCursorMotionAccumulator;
1187 CursorScrollAccumulator mCursorScrollAccumulator;
1188
1189 int32_t mSource;
1190 float mXScale;
1191 float mYScale;
1192 float mXPrecision;
1193 float mYPrecision;
1194
1195 float mVWheelScale;
1196 float mHWheelScale;
1197
1198 // Velocity controls for mouse pointer and wheel movements.
1199 // The controls for X and Y wheel movements are separate to keep them decoupled.
1200 VelocityControl mPointerVelocityControl;
1201 VelocityControl mWheelXVelocityControl;
1202 VelocityControl mWheelYVelocityControl;
1203
1204 int32_t mOrientation;
1205
1206 sp<PointerControllerInterface> mPointerController;
1207
1208 int32_t mButtonState;
1209 nsecs_t mDownTime;
1210
1211 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001212 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
1214 void sync(nsecs_t when);
1215};
1216
1217
Prashant Malani1941ff52015-08-11 18:29:28 -07001218class RotaryEncoderInputMapper : public InputMapper {
1219public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001220 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -07001221 virtual ~RotaryEncoderInputMapper();
1222
1223 virtual uint32_t getSources();
1224 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001225 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -07001226 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1227 virtual void reset(nsecs_t when);
1228 virtual void process(const RawEvent* rawEvent);
1229
1230private:
1231 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
1232
1233 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001234 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +01001235 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -07001236
1237 void sync(nsecs_t when);
1238};
1239
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240class TouchInputMapper : public InputMapper {
1241public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001242 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 virtual ~TouchInputMapper();
1244
1245 virtual uint32_t getSources();
1246 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001247 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1249 virtual void reset(nsecs_t when);
1250 virtual void process(const RawEvent* rawEvent);
1251
1252 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1253 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1254 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1255 const int32_t* keyCodes, uint8_t* outFlags);
1256
1257 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001258 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001260 virtual void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261
1262protected:
1263 CursorButtonAccumulator mCursorButtonAccumulator;
1264 CursorScrollAccumulator mCursorScrollAccumulator;
1265 TouchButtonAccumulator mTouchButtonAccumulator;
1266
1267 struct VirtualKey {
1268 int32_t keyCode;
1269 int32_t scanCode;
1270 uint32_t flags;
1271
1272 // computed hit box, specified in touch screen coords based on known display size
1273 int32_t hitLeft;
1274 int32_t hitTop;
1275 int32_t hitRight;
1276 int32_t hitBottom;
1277
1278 inline bool isHit(int32_t x, int32_t y) const {
1279 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1280 }
1281 };
1282
1283 // Input sources and device mode.
1284 uint32_t mSource;
1285
1286 enum DeviceMode {
1287 DEVICE_MODE_DISABLED, // input is disabled
1288 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1289 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1290 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1291 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1292 };
1293 DeviceMode mDeviceMode;
1294
1295 // The reader's configuration.
1296 InputReaderConfiguration mConfig;
1297
1298 // Immutable configuration parameters.
1299 struct Parameters {
1300 enum DeviceType {
1301 DEVICE_TYPE_TOUCH_SCREEN,
1302 DEVICE_TYPE_TOUCH_PAD,
1303 DEVICE_TYPE_TOUCH_NAVIGATION,
1304 DEVICE_TYPE_POINTER,
1305 };
1306
1307 DeviceType deviceType;
1308 bool hasAssociatedDisplay;
1309 bool associatedDisplayIsExternal;
1310 bool orientationAware;
1311 bool hasButtonUnderPad;
Santos Cordonfa5cf462017-04-05 10:37:00 -07001312 String8 uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313
1314 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001315 GESTURE_MODE_SINGLE_TOUCH,
1316 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317 };
1318 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001319
1320 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 } mParameters;
1322
1323 // Immutable calibration parameters in parsed form.
1324 struct Calibration {
1325 // Size
1326 enum SizeCalibration {
1327 SIZE_CALIBRATION_DEFAULT,
1328 SIZE_CALIBRATION_NONE,
1329 SIZE_CALIBRATION_GEOMETRIC,
1330 SIZE_CALIBRATION_DIAMETER,
1331 SIZE_CALIBRATION_BOX,
1332 SIZE_CALIBRATION_AREA,
1333 };
1334
1335 SizeCalibration sizeCalibration;
1336
1337 bool haveSizeScale;
1338 float sizeScale;
1339 bool haveSizeBias;
1340 float sizeBias;
1341 bool haveSizeIsSummed;
1342 bool sizeIsSummed;
1343
1344 // Pressure
1345 enum PressureCalibration {
1346 PRESSURE_CALIBRATION_DEFAULT,
1347 PRESSURE_CALIBRATION_NONE,
1348 PRESSURE_CALIBRATION_PHYSICAL,
1349 PRESSURE_CALIBRATION_AMPLITUDE,
1350 };
1351
1352 PressureCalibration pressureCalibration;
1353 bool havePressureScale;
1354 float pressureScale;
1355
1356 // Orientation
1357 enum OrientationCalibration {
1358 ORIENTATION_CALIBRATION_DEFAULT,
1359 ORIENTATION_CALIBRATION_NONE,
1360 ORIENTATION_CALIBRATION_INTERPOLATED,
1361 ORIENTATION_CALIBRATION_VECTOR,
1362 };
1363
1364 OrientationCalibration orientationCalibration;
1365
1366 // Distance
1367 enum DistanceCalibration {
1368 DISTANCE_CALIBRATION_DEFAULT,
1369 DISTANCE_CALIBRATION_NONE,
1370 DISTANCE_CALIBRATION_SCALED,
1371 };
1372
1373 DistanceCalibration distanceCalibration;
1374 bool haveDistanceScale;
1375 float distanceScale;
1376
1377 enum CoverageCalibration {
1378 COVERAGE_CALIBRATION_DEFAULT,
1379 COVERAGE_CALIBRATION_NONE,
1380 COVERAGE_CALIBRATION_BOX,
1381 };
1382
1383 CoverageCalibration coverageCalibration;
1384
1385 inline void applySizeScaleAndBias(float* outSize) const {
1386 if (haveSizeScale) {
1387 *outSize *= sizeScale;
1388 }
1389 if (haveSizeBias) {
1390 *outSize += sizeBias;
1391 }
1392 if (*outSize < 0) {
1393 *outSize = 0;
1394 }
1395 }
1396 } mCalibration;
1397
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001398 // Affine location transformation/calibration
1399 struct TouchAffineTransformation mAffineTransform;
1400
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 RawPointerAxes mRawPointerAxes;
1402
Michael Wright842500e2015-03-13 17:32:02 -07001403 struct RawState {
1404 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001405 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406
Michael Wright842500e2015-03-13 17:32:02 -07001407 // Raw pointer sample data.
1408 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409
Michael Wright842500e2015-03-13 17:32:02 -07001410 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001411
Michael Wright842500e2015-03-13 17:32:02 -07001412 // Scroll state.
1413 int32_t rawVScroll;
1414 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001415
Michael Wright842500e2015-03-13 17:32:02 -07001416 void copyFrom(const RawState& other) {
1417 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001418 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001419 rawPointerData.copyFrom(other.rawPointerData);
1420 buttonState = other.buttonState;
1421 rawVScroll = other.rawVScroll;
1422 rawHScroll = other.rawHScroll;
1423 }
1424
1425 void clear() {
1426 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001427 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001428 rawPointerData.clear();
1429 buttonState = 0;
1430 rawVScroll = 0;
1431 rawHScroll = 0;
1432 }
1433 };
1434
1435 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001436 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001437 // Cooked pointer sample data.
1438 CookedPointerData cookedPointerData;
1439
1440 // Id bits used to differentiate fingers, stylus and mouse tools.
1441 BitSet32 fingerIdBits;
1442 BitSet32 stylusIdBits;
1443 BitSet32 mouseIdBits;
1444
Michael Wright7b159c92015-05-14 14:48:03 +01001445 int32_t buttonState;
1446
Michael Wright842500e2015-03-13 17:32:02 -07001447 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001448 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001449 cookedPointerData.copyFrom(other.cookedPointerData);
1450 fingerIdBits = other.fingerIdBits;
1451 stylusIdBits = other.stylusIdBits;
1452 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001453 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001454 }
1455
1456 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001457 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001458 cookedPointerData.clear();
1459 fingerIdBits.clear();
1460 stylusIdBits.clear();
1461 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001462 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001463 }
1464 };
1465
1466 Vector<RawState> mRawStatesPending;
1467 RawState mCurrentRawState;
1468 CookedState mCurrentCookedState;
1469 RawState mLastRawState;
1470 CookedState mLastCookedState;
1471
1472 // State provided by an external stylus
1473 StylusState mExternalStylusState;
1474 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001475 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001476 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
1478 // True if we sent a HOVER_ENTER event.
1479 bool mSentHoverEnter;
1480
Michael Wright842500e2015-03-13 17:32:02 -07001481 // Have we assigned pointer IDs for this stream
1482 bool mHavePointerIds;
1483
Michael Wright8e812822015-06-22 16:18:21 +01001484 // Is the current stream of direct touch events aborted
1485 bool mCurrentMotionAborted;
1486
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487 // The time the primary pointer last went down.
1488 nsecs_t mDownTime;
1489
1490 // The pointer controller, or null if the device is not a pointer.
1491 sp<PointerControllerInterface> mPointerController;
1492
1493 Vector<VirtualKey> mVirtualKeys;
1494
1495 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001496 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001498 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001500 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001502 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503 virtual void parseCalibration();
1504 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001505 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001506 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001507 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001508 virtual void resolveExternalStylusPresence();
1509 virtual bool hasStylus() const = 0;
1510 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511
Michael Wright842500e2015-03-13 17:32:02 -07001512 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513
1514private:
1515 // The current viewport.
1516 // The components of the viewport are specified in the display's rotated orientation.
1517 DisplayViewport mViewport;
1518
1519 // The surface orientation, width and height set by configureSurface().
1520 // The width and height are derived from the viewport but are specified
1521 // in the natural orientation.
1522 // The surface origin specifies how the surface coordinates should be translated
1523 // to align with the logical display coordinate space.
1524 // The orientation may be different from the viewport orientation as it specifies
1525 // the rotation of the surface coordinates required to produce the viewport's
1526 // requested orientation, so it will depend on whether the device is orientation aware.
1527 int32_t mSurfaceWidth;
1528 int32_t mSurfaceHeight;
1529 int32_t mSurfaceLeft;
1530 int32_t mSurfaceTop;
1531 int32_t mSurfaceOrientation;
1532
1533 // Translation and scaling factors, orientation-independent.
1534 float mXTranslate;
1535 float mXScale;
1536 float mXPrecision;
1537
1538 float mYTranslate;
1539 float mYScale;
1540 float mYPrecision;
1541
1542 float mGeometricScale;
1543
1544 float mPressureScale;
1545
1546 float mSizeScale;
1547
1548 float mOrientationScale;
1549
1550 float mDistanceScale;
1551
1552 bool mHaveTilt;
1553 float mTiltXCenter;
1554 float mTiltXScale;
1555 float mTiltYCenter;
1556 float mTiltYScale;
1557
Michael Wright842500e2015-03-13 17:32:02 -07001558 bool mExternalStylusConnected;
1559
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560 // Oriented motion ranges for input device info.
1561 struct OrientedRanges {
1562 InputDeviceInfo::MotionRange x;
1563 InputDeviceInfo::MotionRange y;
1564 InputDeviceInfo::MotionRange pressure;
1565
1566 bool haveSize;
1567 InputDeviceInfo::MotionRange size;
1568
1569 bool haveTouchSize;
1570 InputDeviceInfo::MotionRange touchMajor;
1571 InputDeviceInfo::MotionRange touchMinor;
1572
1573 bool haveToolSize;
1574 InputDeviceInfo::MotionRange toolMajor;
1575 InputDeviceInfo::MotionRange toolMinor;
1576
1577 bool haveOrientation;
1578 InputDeviceInfo::MotionRange orientation;
1579
1580 bool haveDistance;
1581 InputDeviceInfo::MotionRange distance;
1582
1583 bool haveTilt;
1584 InputDeviceInfo::MotionRange tilt;
1585
1586 OrientedRanges() {
1587 clear();
1588 }
1589
1590 void clear() {
1591 haveSize = false;
1592 haveTouchSize = false;
1593 haveToolSize = false;
1594 haveOrientation = false;
1595 haveDistance = false;
1596 haveTilt = false;
1597 }
1598 } mOrientedRanges;
1599
1600 // Oriented dimensions and precision.
1601 float mOrientedXPrecision;
1602 float mOrientedYPrecision;
1603
1604 struct CurrentVirtualKeyState {
1605 bool down;
1606 bool ignored;
1607 nsecs_t downTime;
1608 int32_t keyCode;
1609 int32_t scanCode;
1610 } mCurrentVirtualKey;
1611
1612 // Scale factor for gesture or mouse based pointer movements.
1613 float mPointerXMovementScale;
1614 float mPointerYMovementScale;
1615
1616 // Scale factor for gesture based zooming and other freeform motions.
1617 float mPointerXZoomScale;
1618 float mPointerYZoomScale;
1619
1620 // The maximum swipe width.
1621 float mPointerGestureMaxSwipeWidth;
1622
1623 struct PointerDistanceHeapElement {
1624 uint32_t currentPointerIndex : 8;
1625 uint32_t lastPointerIndex : 8;
1626 uint64_t distance : 48; // squared distance
1627 };
1628
1629 enum PointerUsage {
1630 POINTER_USAGE_NONE,
1631 POINTER_USAGE_GESTURES,
1632 POINTER_USAGE_STYLUS,
1633 POINTER_USAGE_MOUSE,
1634 };
1635 PointerUsage mPointerUsage;
1636
1637 struct PointerGesture {
1638 enum Mode {
1639 // No fingers, button is not pressed.
1640 // Nothing happening.
1641 NEUTRAL,
1642
1643 // No fingers, button is not pressed.
1644 // Tap detected.
1645 // Emits DOWN and UP events at the pointer location.
1646 TAP,
1647
1648 // Exactly one finger dragging following a tap.
1649 // Pointer follows the active finger.
1650 // Emits DOWN, MOVE and UP events at the pointer location.
1651 //
1652 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1653 TAP_DRAG,
1654
1655 // Button is pressed.
1656 // Pointer follows the active finger if there is one. Other fingers are ignored.
1657 // Emits DOWN, MOVE and UP events at the pointer location.
1658 BUTTON_CLICK_OR_DRAG,
1659
1660 // Exactly one finger, button is not pressed.
1661 // Pointer follows the active finger.
1662 // Emits HOVER_MOVE events at the pointer location.
1663 //
1664 // Detect taps when the finger goes up while in HOVER mode.
1665 HOVER,
1666
1667 // Exactly two fingers but neither have moved enough to clearly indicate
1668 // whether a swipe or freeform gesture was intended. We consider the
1669 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1670 // Pointer does not move.
1671 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1672 PRESS,
1673
1674 // Exactly two fingers moving in the same direction, button is not pressed.
1675 // Pointer does not move.
1676 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1677 // follows the midpoint between both fingers.
1678 SWIPE,
1679
1680 // Two or more fingers moving in arbitrary directions, button is not pressed.
1681 // Pointer does not move.
1682 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1683 // each finger individually relative to the initial centroid of the finger.
1684 FREEFORM,
1685
1686 // Waiting for quiet time to end before starting the next gesture.
1687 QUIET,
1688 };
1689
1690 // Time the first finger went down.
1691 nsecs_t firstTouchTime;
1692
1693 // The active pointer id from the raw touch data.
1694 int32_t activeTouchId; // -1 if none
1695
1696 // The active pointer id from the gesture last delivered to the application.
1697 int32_t activeGestureId; // -1 if none
1698
1699 // Pointer coords and ids for the current and previous pointer gesture.
1700 Mode currentGestureMode;
1701 BitSet32 currentGestureIdBits;
1702 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1703 PointerProperties currentGestureProperties[MAX_POINTERS];
1704 PointerCoords currentGestureCoords[MAX_POINTERS];
1705
1706 Mode lastGestureMode;
1707 BitSet32 lastGestureIdBits;
1708 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1709 PointerProperties lastGestureProperties[MAX_POINTERS];
1710 PointerCoords lastGestureCoords[MAX_POINTERS];
1711
1712 // Time the pointer gesture last went down.
1713 nsecs_t downTime;
1714
1715 // Time when the pointer went down for a TAP.
1716 nsecs_t tapDownTime;
1717
1718 // Time when the pointer went up for a TAP.
1719 nsecs_t tapUpTime;
1720
1721 // Location of initial tap.
1722 float tapX, tapY;
1723
1724 // Time we started waiting for quiescence.
1725 nsecs_t quietTime;
1726
1727 // Reference points for multitouch gestures.
1728 float referenceTouchX; // reference touch X/Y coordinates in surface units
1729 float referenceTouchY;
1730 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1731 float referenceGestureY;
1732
1733 // Distance that each pointer has traveled which has not yet been
1734 // subsumed into the reference gesture position.
1735 BitSet32 referenceIdBits;
1736 struct Delta {
1737 float dx, dy;
1738 };
1739 Delta referenceDeltas[MAX_POINTER_ID + 1];
1740
1741 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1742 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1743
1744 // A velocity tracker for determining whether to switch active pointers during drags.
1745 VelocityTracker velocityTracker;
1746
1747 void reset() {
1748 firstTouchTime = LLONG_MIN;
1749 activeTouchId = -1;
1750 activeGestureId = -1;
1751 currentGestureMode = NEUTRAL;
1752 currentGestureIdBits.clear();
1753 lastGestureMode = NEUTRAL;
1754 lastGestureIdBits.clear();
1755 downTime = 0;
1756 velocityTracker.clear();
1757 resetTap();
1758 resetQuietTime();
1759 }
1760
1761 void resetTap() {
1762 tapDownTime = LLONG_MIN;
1763 tapUpTime = LLONG_MIN;
1764 }
1765
1766 void resetQuietTime() {
1767 quietTime = LLONG_MIN;
1768 }
1769 } mPointerGesture;
1770
1771 struct PointerSimple {
1772 PointerCoords currentCoords;
1773 PointerProperties currentProperties;
1774 PointerCoords lastCoords;
1775 PointerProperties lastProperties;
1776
1777 // True if the pointer is down.
1778 bool down;
1779
1780 // True if the pointer is hovering.
1781 bool hovering;
1782
1783 // Time the pointer last went down.
1784 nsecs_t downTime;
1785
1786 void reset() {
1787 currentCoords.clear();
1788 currentProperties.clear();
1789 lastCoords.clear();
1790 lastProperties.clear();
1791 down = false;
1792 hovering = false;
1793 downTime = 0;
1794 }
1795 } mPointerSimple;
1796
1797 // The pointer and scroll velocity controls.
1798 VelocityControl mPointerVelocityControl;
1799 VelocityControl mWheelXVelocityControl;
1800 VelocityControl mWheelYVelocityControl;
1801
Michael Wright842500e2015-03-13 17:32:02 -07001802 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001803 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001804
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805 void sync(nsecs_t when);
1806
1807 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001808 void processRawTouches(bool timeout);
1809 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1811 int32_t keyEventAction, int32_t keyEventFlags);
1812
1813 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1814 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1815 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001816 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1817 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1818 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001819 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001820 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821
1822 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1823 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1824
1825 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1826 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1827 bool preparePointerGestures(nsecs_t when,
1828 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1829 bool isTimeout);
1830
1831 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1832 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1833
1834 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1835 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1836
1837 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1838 bool down, bool hovering);
1839 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1840
Michael Wright842500e2015-03-13 17:32:02 -07001841 bool assignExternalStylusId(const RawState& state, bool timeout);
1842 void applyExternalStylusButtonState(nsecs_t when);
1843 void applyExternalStylusTouchState(nsecs_t when);
1844
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 // Dispatches a motion event.
1846 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1847 // method will take care of setting the index and transmuting the action to DOWN or UP
1848 // it is the first / last pointer to go down / up.
1849 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001850 int32_t action, int32_t actionButton,
1851 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001852 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001853 const PointerProperties* properties, const PointerCoords* coords,
1854 const uint32_t* idToIndex, BitSet32 idBits,
1855 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1856
1857 // Updates pointer coords and properties for pointers with specified ids that have moved.
1858 // Returns true if any of them changed.
1859 bool updateMovedPointers(const PointerProperties* inProperties,
1860 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1861 PointerProperties* outProperties, PointerCoords* outCoords,
1862 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1863
1864 bool isPointInsideSurface(int32_t x, int32_t y);
1865 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1866
Michael Wright842500e2015-03-13 17:32:02 -07001867 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001868
1869 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001870};
1871
1872
1873class SingleTouchInputMapper : public TouchInputMapper {
1874public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001875 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876 virtual ~SingleTouchInputMapper();
1877
1878 virtual void reset(nsecs_t when);
1879 virtual void process(const RawEvent* rawEvent);
1880
1881protected:
Michael Wright842500e2015-03-13 17:32:02 -07001882 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883 virtual void configureRawPointerAxes();
1884 virtual bool hasStylus() const;
1885
1886private:
1887 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1888};
1889
1890
1891class MultiTouchInputMapper : public TouchInputMapper {
1892public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001893 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 virtual ~MultiTouchInputMapper();
1895
1896 virtual void reset(nsecs_t when);
1897 virtual void process(const RawEvent* rawEvent);
1898
1899protected:
Michael Wright842500e2015-03-13 17:32:02 -07001900 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901 virtual void configureRawPointerAxes();
1902 virtual bool hasStylus() const;
1903
1904private:
1905 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1906
1907 // Specifies the pointer id bits that are in use, and their associated tracking id.
1908 BitSet32 mPointerIdBits;
1909 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1910};
1911
Michael Wright842500e2015-03-13 17:32:02 -07001912class ExternalStylusInputMapper : public InputMapper {
1913public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001914 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001915 virtual ~ExternalStylusInputMapper() = default;
1916
1917 virtual uint32_t getSources();
1918 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001919 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001920 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1921 virtual void reset(nsecs_t when);
1922 virtual void process(const RawEvent* rawEvent);
1923 virtual void sync(nsecs_t when);
1924
1925private:
1926 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1927 RawAbsoluteAxisInfo mRawPressureAxis;
1928 TouchButtonAccumulator mTouchButtonAccumulator;
1929
1930 StylusState mStylusState;
1931};
1932
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933
1934class JoystickInputMapper : public InputMapper {
1935public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001936 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 virtual ~JoystickInputMapper();
1938
1939 virtual uint32_t getSources();
1940 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001941 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001942 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
1946private:
1947 struct Axis {
1948 RawAbsoluteAxisInfo rawAxisInfo;
1949 AxisInfo axisInfo;
1950
1951 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1952
1953 float scale; // scale factor from raw to normalized values
1954 float offset; // offset to add after scaling for normalization
1955 float highScale; // scale factor from raw to normalized values of high split
1956 float highOffset; // offset to add after scaling for normalization of high split
1957
1958 float min; // normalized inclusive minimum
1959 float max; // normalized inclusive maximum
1960 float flat; // normalized flat region size
1961 float fuzz; // normalized error tolerance
1962 float resolution; // normalized resolution in units/mm
1963
1964 float filter; // filter out small variations of this size
1965 float currentValue; // current value
1966 float newValue; // most recent value
1967 float highCurrentValue; // current value of high split
1968 float highNewValue; // most recent value of high split
1969
1970 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1971 bool explicitlyMapped, float scale, float offset,
1972 float highScale, float highOffset,
1973 float min, float max, float flat, float fuzz, float resolution) {
1974 this->rawAxisInfo = rawAxisInfo;
1975 this->axisInfo = axisInfo;
1976 this->explicitlyMapped = explicitlyMapped;
1977 this->scale = scale;
1978 this->offset = offset;
1979 this->highScale = highScale;
1980 this->highOffset = highOffset;
1981 this->min = min;
1982 this->max = max;
1983 this->flat = flat;
1984 this->fuzz = fuzz;
1985 this->resolution = resolution;
1986 this->filter = 0;
1987 resetValue();
1988 }
1989
1990 void resetValue() {
1991 this->currentValue = 0;
1992 this->newValue = 0;
1993 this->highCurrentValue = 0;
1994 this->highNewValue = 0;
1995 }
1996 };
1997
1998 // Axes indexed by raw ABS_* axis index.
1999 KeyedVector<int32_t, Axis> mAxes;
2000
2001 void sync(nsecs_t when, bool force);
2002
2003 bool haveAxis(int32_t axisId);
2004 void pruneAxes(bool ignoreExplicitlyMappedAxes);
2005 bool filterAxes(bool force);
2006
2007 static bool hasValueChangedSignificantly(float filter,
2008 float newValue, float currentValue, float min, float max);
2009 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
2010 float newValue, float currentValue, float thresholdValue);
2011
2012 static bool isCenteredAxis(int32_t axis);
2013 static int32_t getCompatAxis(int32_t axis);
2014
2015 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
2016 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
2017 float value);
2018};
2019
2020} // namespace android
2021
2022#endif // _UI_INPUT_READER_H