blob: 9b0007a2b8ac367085dd90ffaf33b54a08ca8001 [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>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010041#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042
43// Maximum supported size of a vibration pattern.
44// Must be at least 2.
45#define MAX_VIBRATE_PATTERN_SIZE 100
46
47// Maximum allowable delay value in a vibration pattern before
48// which the delay will be truncated.
49#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
50
51namespace android {
52
53class InputDevice;
54class InputMapper;
55
56/*
Michael Wrightd02c5b62014-02-10 15:10:22 -080057 * Input reader configuration.
58 *
59 * Specifies various options that modify the behavior of the input reader.
60 */
61struct InputReaderConfiguration {
62 // Describes changes that have occurred.
63 enum {
64 // The pointer speed changed.
65 CHANGE_POINTER_SPEED = 1 << 0,
66
67 // The pointer gesture control changed.
68 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
69
70 // The display size or orientation changed.
71 CHANGE_DISPLAY_INFO = 1 << 2,
72
73 // The visible touches option changed.
74 CHANGE_SHOW_TOUCHES = 1 << 3,
75
76 // The keyboard layouts must be reloaded.
77 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
78
79 // The device name alias supplied by the may have changed for some devices.
80 CHANGE_DEVICE_ALIAS = 1 << 5,
81
Jason Gerecke12d6baa2014-01-27 18:34:20 -080082 // The location calibration matrix changed.
Michael Wright842500e2015-03-13 17:32:02 -070083 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
84
85 // The presence of an external stylus has changed.
86 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
Jason Gerecke12d6baa2014-01-27 18:34:20 -080087
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -080088 // The pointer capture mode has changed.
89 CHANGE_POINTER_CAPTURE = 1 << 8,
90
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070091 // The set of disabled input devices (disabledDevices) has changed.
92 CHANGE_ENABLED_STATE = 1 << 9,
93
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 // All devices must be reopened.
95 CHANGE_MUST_REOPEN = 1 << 31,
96 };
97
98 // Gets the amount of time to disable virtual keys after the screen is touched
99 // in order to filter out accidental virtual key presses due to swiping gestures
100 // or taps near the edge of the display. May be 0 to disable the feature.
101 nsecs_t virtualKeyQuietTime;
102
103 // The excluded device names for the platform.
104 // Devices with these names will be ignored.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100105 std::vector<std::string> excludedDeviceNames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
107 // Velocity control parameters for mouse pointer movements.
108 VelocityControlParameters pointerVelocityControlParameters;
109
110 // Velocity control parameters for mouse wheel movements.
111 VelocityControlParameters wheelVelocityControlParameters;
112
113 // True if pointer gestures are enabled.
114 bool pointerGesturesEnabled;
115
116 // Quiet time between certain pointer gesture transitions.
117 // Time to allow for all fingers or buttons to settle into a stable state before
118 // starting a new gesture.
119 nsecs_t pointerGestureQuietInterval;
120
121 // The minimum speed that a pointer must travel for us to consider switching the active
122 // touch pointer to it during a drag. This threshold is set to avoid switching due
123 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
124 float pointerGestureDragMinSwitchSpeed; // in pixels per second
125
126 // Tap gesture delay time.
127 // The time between down and up must be less than this to be considered a tap.
128 nsecs_t pointerGestureTapInterval;
129
130 // Tap drag gesture delay time.
131 // The time between the previous tap's up and the next down must be less than
132 // this to be considered a drag. Otherwise, the previous tap is finished and a
133 // new tap begins.
134 //
135 // Note that the previous tap will be held down for this entire duration so this
136 // interval must be shorter than the long press timeout.
137 nsecs_t pointerGestureTapDragInterval;
138
139 // The distance in pixels that the pointer is allowed to move from initial down
140 // to up and still be called a tap.
141 float pointerGestureTapSlop; // in pixels
142
143 // Time after the first touch points go down to settle on an initial centroid.
144 // This is intended to be enough time to handle cases where the user puts down two
145 // fingers at almost but not quite exactly the same time.
146 nsecs_t pointerGestureMultitouchSettleInterval;
147
148 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
149 // at least two pointers have moved at least this far from their starting place.
150 float pointerGestureMultitouchMinDistance; // in pixels
151
152 // The transition from PRESS to SWIPE gesture mode can only occur when the
153 // cosine of the angle between the two vectors is greater than or equal to than this value
154 // which indicates that the vectors are oriented in the same direction.
155 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
156 // (In exactly opposite directions, the cosine is -1.0.)
157 float pointerGestureSwipeTransitionAngleCosine;
158
159 // The transition from PRESS to SWIPE gesture mode can only occur when the
160 // fingers are no more than this far apart relative to the diagonal size of
161 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
162 // no more than half the diagonal size of the touch pad apart.
163 float pointerGestureSwipeMaxWidthRatio;
164
165 // The gesture movement speed factor relative to the size of the display.
166 // Movement speed applies when the fingers are moving in the same direction.
167 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
168 // will cover this portion of the display diagonal.
169 float pointerGestureMovementSpeedRatio;
170
171 // The gesture zoom speed factor relative to the size of the display.
172 // Zoom speed applies when the fingers are mostly moving relative to each other
173 // to execute a scale gesture or similar.
174 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
175 // will cover this portion of the display diagonal.
176 float pointerGestureZoomSpeedRatio;
177
178 // True to show the location of touches on the touch screen as spots.
179 bool showTouches;
180
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800181 // True if pointer capture is enabled.
182 bool pointerCapture;
183
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700184 // The set of currently disabled input devices.
185 SortedVector<int32_t> disabledDevices;
186
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 InputReaderConfiguration() :
188 virtualKeyQuietTime(0),
189 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
190 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
191 pointerGesturesEnabled(true),
192 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
193 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
194 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
195 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
196 pointerGestureTapSlop(10.0f), // 10 pixels
197 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
198 pointerGestureMultitouchMinDistance(15), // 15 pixels
199 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
200 pointerGestureSwipeMaxWidthRatio(0.25f),
201 pointerGestureMovementSpeedRatio(0.8f),
202 pointerGestureZoomSpeedRatio(0.3f),
203 showTouches(false) { }
204
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700205 std::optional<DisplayViewport> getDisplayViewport(ViewportType viewportType,
206 const std::string& uniqueDisplayId) const;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100207 void setDisplayViewports(const std::vector<DisplayViewport>& viewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700208
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:
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100214 std::vector<DisplayViewport> mDisplays;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215};
216
217
Jason Gereckeaf126fb2012-05-10 14:22:47 -0700218struct TouchAffineTransformation {
219 float x_scale;
220 float x_ymix;
221 float x_offset;
222 float y_xmix;
223 float y_scale;
224 float y_offset;
225
226 TouchAffineTransformation() :
227 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
228 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
229 }
230
Jason Gerecke489fda82012-09-07 17:19:40 -0700231 TouchAffineTransformation(float xscale, float xymix, float xoffset,
232 float yxmix, float yscale, float yoffset) :
233 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
234 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
235 }
236
Jason Gereckeaf126fb2012-05-10 14:22:47 -0700237 void applyTo(float& x, float& y) const;
238};
239
240
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241/*
242 * Input reader policy interface.
243 *
244 * The input reader policy is used by the input reader to interact with the Window Manager
245 * and other system components.
246 *
247 * The actual implementation is partially supported by callbacks into the DVM
248 * via JNI. This interface is also mocked in the unit tests.
249 *
250 * These methods must NOT re-enter the input reader since they may be called while
251 * holding the input reader lock.
252 */
253class InputReaderPolicyInterface : public virtual RefBase {
254protected:
255 InputReaderPolicyInterface() { }
256 virtual ~InputReaderPolicyInterface() { }
257
258public:
259 /* Gets the input reader configuration. */
260 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
261
262 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
263 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
264
265 /* Notifies the input reader policy that some input devices have changed
266 * and provides information about all current input devices.
267 */
268 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
269
270 /* Gets the keyboard layout for a particular input device. */
271 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
272 const InputDeviceIdentifier& identifier) = 0;
273
274 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100275 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800276
277 /* Gets the affine calibration associated with the specified device. */
278 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280};
281
282
283/* Processes raw input events and sends cooked event data to an input listener. */
284class InputReaderInterface : public virtual RefBase {
285protected:
286 InputReaderInterface() { }
287 virtual ~InputReaderInterface() { }
288
289public:
290 /* Dumps the state of the input reader.
291 *
292 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800293 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294
295 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
296 virtual void monitor() = 0;
297
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700298 /* Returns true if the input device is enabled. */
299 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
300
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 /* Runs a single iteration of the processing loop.
302 * Nominally reads and processes one incoming message from the EventHub.
303 *
304 * This method should be called on the input reader thread.
305 */
306 virtual void loopOnce() = 0;
307
308 /* Gets information about all input devices.
309 *
310 * This method may be called on any thread (usually by the input manager).
311 */
312 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
313
314 /* Query current input state. */
315 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
316 int32_t scanCode) = 0;
317 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
318 int32_t keyCode) = 0;
319 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
320 int32_t sw) = 0;
321
Andrii Kulian763a3a42016-03-08 10:46:16 -0800322 /* Toggle Caps Lock */
323 virtual void toggleCapsLockState(int32_t deviceId) = 0;
324
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 /* Determine whether physical keys exist for the given framework-domain key codes. */
326 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
327 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
328
329 /* Requests that a reconfiguration of all input devices.
330 * The changes flag is a bitfield that indicates what has changed and whether
331 * the input devices must all be reopened. */
332 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
333
334 /* Controls the vibrator of a particular input device. */
335 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
336 ssize_t repeat, int32_t token) = 0;
337 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
338};
339
Michael Wright842500e2015-03-13 17:32:02 -0700340struct StylusState {
341 /* Time the stylus event was received. */
342 nsecs_t when;
343 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
344 float pressure;
345 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
346 uint32_t buttons;
347 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
348 int32_t toolType;
349
350 void copyFrom(const StylusState& other) {
351 when = other.when;
352 pressure = other.pressure;
353 buttons = other.buttons;
354 toolType = other.toolType;
355 }
356
357 void clear() {
358 when = LLONG_MAX;
359 pressure = 0.f;
360 buttons = 0;
361 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
362 }
363};
364
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365
366/* Internal interface used by individual input devices to access global input device state
367 * and parameters maintained by the input reader.
368 */
369class InputReaderContext {
370public:
371 InputReaderContext() { }
372 virtual ~InputReaderContext() { }
373
374 virtual void updateGlobalMetaState() = 0;
375 virtual int32_t getGlobalMetaState() = 0;
376
377 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
378 virtual bool shouldDropVirtualKey(nsecs_t now,
379 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
380
381 virtual void fadePointer() = 0;
382
383 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
384 virtual int32_t bumpGeneration() = 0;
385
Michael Wrightb85401d2015-04-17 18:35:15 +0100386 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
387 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -0700388
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 virtual InputReaderPolicyInterface* getPolicy() = 0;
390 virtual InputListenerInterface* getListener() = 0;
391 virtual EventHubInterface* getEventHub() = 0;
392};
393
394
395/* The input reader reads raw event data from the event hub and processes it into input events
396 * that it sends to the input listener. Some functions of the input reader, such as early
397 * event filtering in low power states, are controlled by a separate policy object.
398 *
399 * The InputReader owns a collection of InputMappers. Most of the work it does happens
400 * on the input reader thread but the InputReader can receive queries from other system
401 * components running on arbitrary threads. To keep things manageable, the InputReader
402 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
403 * EventHub or the InputReaderPolicy but it is never held while calling into the
404 * InputListener.
405 */
406class InputReader : public InputReaderInterface {
407public:
408 InputReader(const sp<EventHubInterface>& eventHub,
409 const sp<InputReaderPolicyInterface>& policy,
410 const sp<InputListenerInterface>& listener);
411 virtual ~InputReader();
412
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800413 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414 virtual void monitor();
415
416 virtual void loopOnce();
417
418 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
419
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700420 virtual bool isInputDeviceEnabled(int32_t deviceId);
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
423 int32_t scanCode);
424 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
425 int32_t keyCode);
426 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
427 int32_t sw);
428
Andrii Kulian763a3a42016-03-08 10:46:16 -0800429 virtual void toggleCapsLockState(int32_t deviceId);
430
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
432 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
433
434 virtual void requestRefreshConfiguration(uint32_t changes);
435
436 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
437 ssize_t repeat, int32_t token);
438 virtual void cancelVibrate(int32_t deviceId, int32_t token);
439
440protected:
441 // These members are protected so they can be instrumented by test cases.
442 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
443 const InputDeviceIdentifier& identifier, uint32_t classes);
444
445 class ContextImpl : public InputReaderContext {
446 InputReader* mReader;
447
448 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700449 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450
451 virtual void updateGlobalMetaState();
452 virtual int32_t getGlobalMetaState();
453 virtual void disableVirtualKeysUntil(nsecs_t time);
454 virtual bool shouldDropVirtualKey(nsecs_t now,
455 InputDevice* device, int32_t keyCode, int32_t scanCode);
456 virtual void fadePointer();
457 virtual void requestTimeoutAtTime(nsecs_t when);
458 virtual int32_t bumpGeneration();
Michael Wright842500e2015-03-13 17:32:02 -0700459 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
460 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 virtual InputReaderPolicyInterface* getPolicy();
462 virtual InputListenerInterface* getListener();
463 virtual EventHubInterface* getEventHub();
464 } mContext;
465
466 friend class ContextImpl;
467
468private:
469 Mutex mLock;
470
471 Condition mReaderIsAliveCondition;
472
473 sp<EventHubInterface> mEventHub;
474 sp<InputReaderPolicyInterface> mPolicy;
475 sp<QueuedInputListener> mQueuedListener;
476
477 InputReaderConfiguration mConfig;
478
479 // The event queue.
480 static const int EVENT_BUFFER_SIZE = 256;
481 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
482
483 KeyedVector<int32_t, InputDevice*> mDevices;
484
485 // low-level input event decoding and device management
486 void processEventsLocked(const RawEvent* rawEvents, size_t count);
487
488 void addDeviceLocked(nsecs_t when, int32_t deviceId);
489 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
490 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
491 void timeoutExpiredLocked(nsecs_t when);
492
493 void handleConfigurationChangedLocked(nsecs_t when);
494
495 int32_t mGlobalMetaState;
496 void updateGlobalMetaStateLocked();
497 int32_t getGlobalMetaStateLocked();
498
Michael Wright842500e2015-03-13 17:32:02 -0700499 void notifyExternalStylusPresenceChanged();
500 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
501 void dispatchExternalStylusState(const StylusState& state);
502
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 void fadePointerLocked();
504
505 int32_t mGeneration;
506 int32_t bumpGenerationLocked();
507
508 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
509
510 nsecs_t mDisableVirtualKeysTimeout;
511 void disableVirtualKeysUntilLocked(nsecs_t time);
512 bool shouldDropVirtualKeyLocked(nsecs_t now,
513 InputDevice* device, int32_t keyCode, int32_t scanCode);
514
515 nsecs_t mNextTimeout;
516 void requestTimeoutAtTimeLocked(nsecs_t when);
517
518 uint32_t mConfigurationChangesToRefresh;
519 void refreshConfigurationLocked(uint32_t changes);
520
521 // state queries
522 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
523 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
524 GetStateFunc getStateFunc);
525 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
526 const int32_t* keyCodes, uint8_t* outFlags);
527};
528
529
530/* Reads raw events from the event hub and processes them, endlessly. */
531class InputReaderThread : public Thread {
532public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700533 explicit InputReaderThread(const sp<InputReaderInterface>& reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 virtual ~InputReaderThread();
535
536private:
537 sp<InputReaderInterface> mReader;
538
539 virtual bool threadLoop();
540};
541
542
543/* Represents the state of a single input device. */
544class InputDevice {
545public:
546 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
547 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
548 ~InputDevice();
549
550 inline InputReaderContext* getContext() { return mContext; }
551 inline int32_t getId() const { return mId; }
552 inline int32_t getControllerNumber() const { return mControllerNumber; }
553 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100554 inline const std::string getName() const { return mIdentifier.name; }
555 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 inline uint32_t getClasses() const { return mClasses; }
557 inline uint32_t getSources() const { return mSources; }
558
559 inline bool isExternal() { return mIsExternal; }
560 inline void setExternal(bool external) { mIsExternal = external; }
561
Tim Kilbourn063ff532015-04-08 10:26:18 -0700562 inline void setMic(bool hasMic) { mHasMic = hasMic; }
563 inline bool hasMic() const { return mHasMic; }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 inline bool isIgnored() { return mMappers.isEmpty(); }
566
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700567 bool isEnabled();
568 void setEnabled(bool enabled, nsecs_t when);
569
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800570 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 void addMapper(InputMapper* mapper);
572 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
573 void reset(nsecs_t when);
574 void process(const RawEvent* rawEvents, size_t count);
575 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700576 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577
578 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
579 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
580 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
581 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
582 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
583 const int32_t* keyCodes, uint8_t* outFlags);
584 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
585 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800586 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
588 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800589 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
591 void fadePointer();
592
593 void bumpGeneration();
594
595 void notifyReset(nsecs_t when);
596
597 inline const PropertyMap& getConfiguration() { return mConfiguration; }
598 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
599
600 bool hasKey(int32_t code) {
601 return getEventHub()->hasScanCode(mId, code);
602 }
603
604 bool hasAbsoluteAxis(int32_t code) {
605 RawAbsoluteAxisInfo info;
606 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
607 return info.valid;
608 }
609
610 bool isKeyPressed(int32_t code) {
611 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
612 }
613
614 int32_t getAbsoluteAxisValue(int32_t code) {
615 int32_t value;
616 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
617 return value;
618 }
619
620private:
621 InputReaderContext* mContext;
622 int32_t mId;
623 int32_t mGeneration;
624 int32_t mControllerNumber;
625 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100626 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 uint32_t mClasses;
628
629 Vector<InputMapper*> mMappers;
630
631 uint32_t mSources;
632 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700633 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 bool mDropUntilNextSync;
635
636 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
637 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
638
639 PropertyMap mConfiguration;
640};
641
642
643/* Keeps track of the state of mouse or touch pad buttons. */
644class CursorButtonAccumulator {
645public:
646 CursorButtonAccumulator();
647 void reset(InputDevice* device);
648
649 void process(const RawEvent* rawEvent);
650
651 uint32_t getButtonState() const;
652
653private:
654 bool mBtnLeft;
655 bool mBtnRight;
656 bool mBtnMiddle;
657 bool mBtnBack;
658 bool mBtnSide;
659 bool mBtnForward;
660 bool mBtnExtra;
661 bool mBtnTask;
662
663 void clearButtons();
664};
665
666
667/* Keeps track of cursor movements. */
668
669class CursorMotionAccumulator {
670public:
671 CursorMotionAccumulator();
672 void reset(InputDevice* device);
673
674 void process(const RawEvent* rawEvent);
675 void finishSync();
676
677 inline int32_t getRelativeX() const { return mRelX; }
678 inline int32_t getRelativeY() const { return mRelY; }
679
680private:
681 int32_t mRelX;
682 int32_t mRelY;
683
684 void clearRelativeAxes();
685};
686
687
688/* Keeps track of cursor scrolling motions. */
689
690class CursorScrollAccumulator {
691public:
692 CursorScrollAccumulator();
693 void configure(InputDevice* device);
694 void reset(InputDevice* device);
695
696 void process(const RawEvent* rawEvent);
697 void finishSync();
698
699 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
700 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
701
702 inline int32_t getRelativeX() const { return mRelX; }
703 inline int32_t getRelativeY() const { return mRelY; }
704 inline int32_t getRelativeVWheel() const { return mRelWheel; }
705 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
706
707private:
708 bool mHaveRelWheel;
709 bool mHaveRelHWheel;
710
711 int32_t mRelX;
712 int32_t mRelY;
713 int32_t mRelWheel;
714 int32_t mRelHWheel;
715
716 void clearRelativeAxes();
717};
718
719
720/* Keeps track of the state of touch, stylus and tool buttons. */
721class TouchButtonAccumulator {
722public:
723 TouchButtonAccumulator();
724 void configure(InputDevice* device);
725 void reset(InputDevice* device);
726
727 void process(const RawEvent* rawEvent);
728
729 uint32_t getButtonState() const;
730 int32_t getToolType() const;
731 bool isToolActive() const;
732 bool isHovering() const;
733 bool hasStylus() const;
734
735private:
736 bool mHaveBtnTouch;
737 bool mHaveStylus;
738
739 bool mBtnTouch;
740 bool mBtnStylus;
741 bool mBtnStylus2;
742 bool mBtnToolFinger;
743 bool mBtnToolPen;
744 bool mBtnToolRubber;
745 bool mBtnToolBrush;
746 bool mBtnToolPencil;
747 bool mBtnToolAirbrush;
748 bool mBtnToolMouse;
749 bool mBtnToolLens;
750 bool mBtnToolDoubleTap;
751 bool mBtnToolTripleTap;
752 bool mBtnToolQuadTap;
753
754 void clearButtons();
755};
756
757
758/* Raw axis information from the driver. */
759struct RawPointerAxes {
760 RawAbsoluteAxisInfo x;
761 RawAbsoluteAxisInfo y;
762 RawAbsoluteAxisInfo pressure;
763 RawAbsoluteAxisInfo touchMajor;
764 RawAbsoluteAxisInfo touchMinor;
765 RawAbsoluteAxisInfo toolMajor;
766 RawAbsoluteAxisInfo toolMinor;
767 RawAbsoluteAxisInfo orientation;
768 RawAbsoluteAxisInfo distance;
769 RawAbsoluteAxisInfo tiltX;
770 RawAbsoluteAxisInfo tiltY;
771 RawAbsoluteAxisInfo trackingId;
772 RawAbsoluteAxisInfo slot;
773
774 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800775 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
776 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 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(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100984 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 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;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001312 std::string 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.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524 int32_t mSurfaceWidth;
1525 int32_t mSurfaceHeight;
1526 int32_t mSurfaceLeft;
1527 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001528
1529 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1530 // the logical coordinate space.
1531 int32_t mPhysicalWidth;
1532 int32_t mPhysicalHeight;
1533 int32_t mPhysicalLeft;
1534 int32_t mPhysicalTop;
1535
1536 // The orientation may be different from the viewport orientation as it specifies
1537 // the rotation of the surface coordinates required to produce the viewport's
1538 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001539 int32_t mSurfaceOrientation;
1540
1541 // Translation and scaling factors, orientation-independent.
1542 float mXTranslate;
1543 float mXScale;
1544 float mXPrecision;
1545
1546 float mYTranslate;
1547 float mYScale;
1548 float mYPrecision;
1549
1550 float mGeometricScale;
1551
1552 float mPressureScale;
1553
1554 float mSizeScale;
1555
1556 float mOrientationScale;
1557
1558 float mDistanceScale;
1559
1560 bool mHaveTilt;
1561 float mTiltXCenter;
1562 float mTiltXScale;
1563 float mTiltYCenter;
1564 float mTiltYScale;
1565
Michael Wright842500e2015-03-13 17:32:02 -07001566 bool mExternalStylusConnected;
1567
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568 // Oriented motion ranges for input device info.
1569 struct OrientedRanges {
1570 InputDeviceInfo::MotionRange x;
1571 InputDeviceInfo::MotionRange y;
1572 InputDeviceInfo::MotionRange pressure;
1573
1574 bool haveSize;
1575 InputDeviceInfo::MotionRange size;
1576
1577 bool haveTouchSize;
1578 InputDeviceInfo::MotionRange touchMajor;
1579 InputDeviceInfo::MotionRange touchMinor;
1580
1581 bool haveToolSize;
1582 InputDeviceInfo::MotionRange toolMajor;
1583 InputDeviceInfo::MotionRange toolMinor;
1584
1585 bool haveOrientation;
1586 InputDeviceInfo::MotionRange orientation;
1587
1588 bool haveDistance;
1589 InputDeviceInfo::MotionRange distance;
1590
1591 bool haveTilt;
1592 InputDeviceInfo::MotionRange tilt;
1593
1594 OrientedRanges() {
1595 clear();
1596 }
1597
1598 void clear() {
1599 haveSize = false;
1600 haveTouchSize = false;
1601 haveToolSize = false;
1602 haveOrientation = false;
1603 haveDistance = false;
1604 haveTilt = false;
1605 }
1606 } mOrientedRanges;
1607
1608 // Oriented dimensions and precision.
1609 float mOrientedXPrecision;
1610 float mOrientedYPrecision;
1611
1612 struct CurrentVirtualKeyState {
1613 bool down;
1614 bool ignored;
1615 nsecs_t downTime;
1616 int32_t keyCode;
1617 int32_t scanCode;
1618 } mCurrentVirtualKey;
1619
1620 // Scale factor for gesture or mouse based pointer movements.
1621 float mPointerXMovementScale;
1622 float mPointerYMovementScale;
1623
1624 // Scale factor for gesture based zooming and other freeform motions.
1625 float mPointerXZoomScale;
1626 float mPointerYZoomScale;
1627
1628 // The maximum swipe width.
1629 float mPointerGestureMaxSwipeWidth;
1630
1631 struct PointerDistanceHeapElement {
1632 uint32_t currentPointerIndex : 8;
1633 uint32_t lastPointerIndex : 8;
1634 uint64_t distance : 48; // squared distance
1635 };
1636
1637 enum PointerUsage {
1638 POINTER_USAGE_NONE,
1639 POINTER_USAGE_GESTURES,
1640 POINTER_USAGE_STYLUS,
1641 POINTER_USAGE_MOUSE,
1642 };
1643 PointerUsage mPointerUsage;
1644
1645 struct PointerGesture {
1646 enum Mode {
1647 // No fingers, button is not pressed.
1648 // Nothing happening.
1649 NEUTRAL,
1650
1651 // No fingers, button is not pressed.
1652 // Tap detected.
1653 // Emits DOWN and UP events at the pointer location.
1654 TAP,
1655
1656 // Exactly one finger dragging following a tap.
1657 // Pointer follows the active finger.
1658 // Emits DOWN, MOVE and UP events at the pointer location.
1659 //
1660 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1661 TAP_DRAG,
1662
1663 // Button is pressed.
1664 // Pointer follows the active finger if there is one. Other fingers are ignored.
1665 // Emits DOWN, MOVE and UP events at the pointer location.
1666 BUTTON_CLICK_OR_DRAG,
1667
1668 // Exactly one finger, button is not pressed.
1669 // Pointer follows the active finger.
1670 // Emits HOVER_MOVE events at the pointer location.
1671 //
1672 // Detect taps when the finger goes up while in HOVER mode.
1673 HOVER,
1674
1675 // Exactly two fingers but neither have moved enough to clearly indicate
1676 // whether a swipe or freeform gesture was intended. We consider the
1677 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1678 // Pointer does not move.
1679 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1680 PRESS,
1681
1682 // Exactly two fingers moving in the same direction, button is not pressed.
1683 // Pointer does not move.
1684 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1685 // follows the midpoint between both fingers.
1686 SWIPE,
1687
1688 // Two or more fingers moving in arbitrary directions, button is not pressed.
1689 // Pointer does not move.
1690 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1691 // each finger individually relative to the initial centroid of the finger.
1692 FREEFORM,
1693
1694 // Waiting for quiet time to end before starting the next gesture.
1695 QUIET,
1696 };
1697
1698 // Time the first finger went down.
1699 nsecs_t firstTouchTime;
1700
1701 // The active pointer id from the raw touch data.
1702 int32_t activeTouchId; // -1 if none
1703
1704 // The active pointer id from the gesture last delivered to the application.
1705 int32_t activeGestureId; // -1 if none
1706
1707 // Pointer coords and ids for the current and previous pointer gesture.
1708 Mode currentGestureMode;
1709 BitSet32 currentGestureIdBits;
1710 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1711 PointerProperties currentGestureProperties[MAX_POINTERS];
1712 PointerCoords currentGestureCoords[MAX_POINTERS];
1713
1714 Mode lastGestureMode;
1715 BitSet32 lastGestureIdBits;
1716 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1717 PointerProperties lastGestureProperties[MAX_POINTERS];
1718 PointerCoords lastGestureCoords[MAX_POINTERS];
1719
1720 // Time the pointer gesture last went down.
1721 nsecs_t downTime;
1722
1723 // Time when the pointer went down for a TAP.
1724 nsecs_t tapDownTime;
1725
1726 // Time when the pointer went up for a TAP.
1727 nsecs_t tapUpTime;
1728
1729 // Location of initial tap.
1730 float tapX, tapY;
1731
1732 // Time we started waiting for quiescence.
1733 nsecs_t quietTime;
1734
1735 // Reference points for multitouch gestures.
1736 float referenceTouchX; // reference touch X/Y coordinates in surface units
1737 float referenceTouchY;
1738 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1739 float referenceGestureY;
1740
1741 // Distance that each pointer has traveled which has not yet been
1742 // subsumed into the reference gesture position.
1743 BitSet32 referenceIdBits;
1744 struct Delta {
1745 float dx, dy;
1746 };
1747 Delta referenceDeltas[MAX_POINTER_ID + 1];
1748
1749 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1750 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1751
1752 // A velocity tracker for determining whether to switch active pointers during drags.
1753 VelocityTracker velocityTracker;
1754
1755 void reset() {
1756 firstTouchTime = LLONG_MIN;
1757 activeTouchId = -1;
1758 activeGestureId = -1;
1759 currentGestureMode = NEUTRAL;
1760 currentGestureIdBits.clear();
1761 lastGestureMode = NEUTRAL;
1762 lastGestureIdBits.clear();
1763 downTime = 0;
1764 velocityTracker.clear();
1765 resetTap();
1766 resetQuietTime();
1767 }
1768
1769 void resetTap() {
1770 tapDownTime = LLONG_MIN;
1771 tapUpTime = LLONG_MIN;
1772 }
1773
1774 void resetQuietTime() {
1775 quietTime = LLONG_MIN;
1776 }
1777 } mPointerGesture;
1778
1779 struct PointerSimple {
1780 PointerCoords currentCoords;
1781 PointerProperties currentProperties;
1782 PointerCoords lastCoords;
1783 PointerProperties lastProperties;
1784
1785 // True if the pointer is down.
1786 bool down;
1787
1788 // True if the pointer is hovering.
1789 bool hovering;
1790
1791 // Time the pointer last went down.
1792 nsecs_t downTime;
1793
1794 void reset() {
1795 currentCoords.clear();
1796 currentProperties.clear();
1797 lastCoords.clear();
1798 lastProperties.clear();
1799 down = false;
1800 hovering = false;
1801 downTime = 0;
1802 }
1803 } mPointerSimple;
1804
1805 // The pointer and scroll velocity controls.
1806 VelocityControl mPointerVelocityControl;
1807 VelocityControl mWheelXVelocityControl;
1808 VelocityControl mWheelYVelocityControl;
1809
Michael Wright842500e2015-03-13 17:32:02 -07001810 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001811 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001812
Michael Wrightd02c5b62014-02-10 15:10:22 -08001813 void sync(nsecs_t when);
1814
1815 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001816 void processRawTouches(bool timeout);
1817 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1819 int32_t keyEventAction, int32_t keyEventFlags);
1820
1821 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1822 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1823 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001824 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1825 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1826 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001828 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001829
1830 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1831 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1832
1833 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1834 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1835 bool preparePointerGestures(nsecs_t when,
1836 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1837 bool isTimeout);
1838
1839 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1840 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1841
1842 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1843 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1844
1845 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1846 bool down, bool hovering);
1847 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1848
Michael Wright842500e2015-03-13 17:32:02 -07001849 bool assignExternalStylusId(const RawState& state, bool timeout);
1850 void applyExternalStylusButtonState(nsecs_t when);
1851 void applyExternalStylusTouchState(nsecs_t when);
1852
Michael Wrightd02c5b62014-02-10 15:10:22 -08001853 // Dispatches a motion event.
1854 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1855 // method will take care of setting the index and transmuting the action to DOWN or UP
1856 // it is the first / last pointer to go down / up.
1857 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001858 int32_t action, int32_t actionButton,
1859 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001860 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861 const PointerProperties* properties, const PointerCoords* coords,
1862 const uint32_t* idToIndex, BitSet32 idBits,
1863 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1864
1865 // Updates pointer coords and properties for pointers with specified ids that have moved.
1866 // Returns true if any of them changed.
1867 bool updateMovedPointers(const PointerProperties* inProperties,
1868 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1869 PointerProperties* outProperties, PointerCoords* outCoords,
1870 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1871
1872 bool isPointInsideSurface(int32_t x, int32_t y);
1873 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1874
Michael Wright842500e2015-03-13 17:32:02 -07001875 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001876
1877 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878};
1879
1880
1881class SingleTouchInputMapper : public TouchInputMapper {
1882public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001883 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884 virtual ~SingleTouchInputMapper();
1885
1886 virtual void reset(nsecs_t when);
1887 virtual void process(const RawEvent* rawEvent);
1888
1889protected:
Michael Wright842500e2015-03-13 17:32:02 -07001890 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 virtual void configureRawPointerAxes();
1892 virtual bool hasStylus() const;
1893
1894private:
1895 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1896};
1897
1898
1899class MultiTouchInputMapper : public TouchInputMapper {
1900public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001901 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 virtual ~MultiTouchInputMapper();
1903
1904 virtual void reset(nsecs_t when);
1905 virtual void process(const RawEvent* rawEvent);
1906
1907protected:
Michael Wright842500e2015-03-13 17:32:02 -07001908 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 virtual void configureRawPointerAxes();
1910 virtual bool hasStylus() const;
1911
1912private:
1913 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1914
1915 // Specifies the pointer id bits that are in use, and their associated tracking id.
1916 BitSet32 mPointerIdBits;
1917 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1918};
1919
Michael Wright842500e2015-03-13 17:32:02 -07001920class ExternalStylusInputMapper : public InputMapper {
1921public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001922 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001923 virtual ~ExternalStylusInputMapper() = default;
1924
1925 virtual uint32_t getSources();
1926 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001927 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001928 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1929 virtual void reset(nsecs_t when);
1930 virtual void process(const RawEvent* rawEvent);
1931 virtual void sync(nsecs_t when);
1932
1933private:
1934 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1935 RawAbsoluteAxisInfo mRawPressureAxis;
1936 TouchButtonAccumulator mTouchButtonAccumulator;
1937
1938 StylusState mStylusState;
1939};
1940
Michael Wrightd02c5b62014-02-10 15:10:22 -08001941
1942class JoystickInputMapper : public InputMapper {
1943public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001944 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 virtual ~JoystickInputMapper();
1946
1947 virtual uint32_t getSources();
1948 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001949 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1951 virtual void reset(nsecs_t when);
1952 virtual void process(const RawEvent* rawEvent);
1953
1954private:
1955 struct Axis {
1956 RawAbsoluteAxisInfo rawAxisInfo;
1957 AxisInfo axisInfo;
1958
1959 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1960
1961 float scale; // scale factor from raw to normalized values
1962 float offset; // offset to add after scaling for normalization
1963 float highScale; // scale factor from raw to normalized values of high split
1964 float highOffset; // offset to add after scaling for normalization of high split
1965
1966 float min; // normalized inclusive minimum
1967 float max; // normalized inclusive maximum
1968 float flat; // normalized flat region size
1969 float fuzz; // normalized error tolerance
1970 float resolution; // normalized resolution in units/mm
1971
1972 float filter; // filter out small variations of this size
1973 float currentValue; // current value
1974 float newValue; // most recent value
1975 float highCurrentValue; // current value of high split
1976 float highNewValue; // most recent value of high split
1977
1978 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1979 bool explicitlyMapped, float scale, float offset,
1980 float highScale, float highOffset,
1981 float min, float max, float flat, float fuzz, float resolution) {
1982 this->rawAxisInfo = rawAxisInfo;
1983 this->axisInfo = axisInfo;
1984 this->explicitlyMapped = explicitlyMapped;
1985 this->scale = scale;
1986 this->offset = offset;
1987 this->highScale = highScale;
1988 this->highOffset = highOffset;
1989 this->min = min;
1990 this->max = max;
1991 this->flat = flat;
1992 this->fuzz = fuzz;
1993 this->resolution = resolution;
1994 this->filter = 0;
1995 resetValue();
1996 }
1997
1998 void resetValue() {
1999 this->currentValue = 0;
2000 this->newValue = 0;
2001 this->highCurrentValue = 0;
2002 this->highNewValue = 0;
2003 }
2004 };
2005
2006 // Axes indexed by raw ABS_* axis index.
2007 KeyedVector<int32_t, Axis> mAxes;
2008
2009 void sync(nsecs_t when, bool force);
2010
2011 bool haveAxis(int32_t axisId);
2012 void pruneAxes(bool ignoreExplicitlyMappedAxes);
2013 bool filterAxes(bool force);
2014
2015 static bool hasValueChangedSignificantly(float filter,
2016 float newValue, float currentValue, float min, float max);
2017 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
2018 float newValue, float currentValue, float thresholdValue);
2019
2020 static bool isCenteredAxis(int32_t axis);
2021 static int32_t getCompatAxis(int32_t axis);
2022
2023 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
2024 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
2025 float value);
2026};
2027
2028} // namespace android
2029
2030#endif // _UI_INPUT_READER_H