blob: 3410bc9c5fb619f635f50003fec1349838e6597d [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();
775 void clear();
776};
777
778
779/* Raw data for a collection of pointers including a pointer id mapping table. */
780struct RawPointerData {
781 struct Pointer {
782 uint32_t id;
783 int32_t x;
784 int32_t y;
785 int32_t pressure;
786 int32_t touchMajor;
787 int32_t touchMinor;
788 int32_t toolMajor;
789 int32_t toolMinor;
790 int32_t orientation;
791 int32_t distance;
792 int32_t tiltX;
793 int32_t tiltY;
794 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
795 bool isHovering;
796 };
797
798 uint32_t pointerCount;
799 Pointer pointers[MAX_POINTERS];
800 BitSet32 hoveringIdBits, touchingIdBits;
801 uint32_t idToIndex[MAX_POINTER_ID + 1];
802
803 RawPointerData();
804 void clear();
805 void copyFrom(const RawPointerData& other);
806 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
807
808 inline void markIdBit(uint32_t id, bool isHovering) {
809 if (isHovering) {
810 hoveringIdBits.markBit(id);
811 } else {
812 touchingIdBits.markBit(id);
813 }
814 }
815
816 inline void clearIdBits() {
817 hoveringIdBits.clear();
818 touchingIdBits.clear();
819 }
820
821 inline const Pointer& pointerForId(uint32_t id) const {
822 return pointers[idToIndex[id]];
823 }
824
825 inline bool isHovering(uint32_t pointerIndex) {
826 return pointers[pointerIndex].isHovering;
827 }
828};
829
830
831/* Cooked data for a collection of pointers including a pointer id mapping table. */
832struct CookedPointerData {
833 uint32_t pointerCount;
834 PointerProperties pointerProperties[MAX_POINTERS];
835 PointerCoords pointerCoords[MAX_POINTERS];
836 BitSet32 hoveringIdBits, touchingIdBits;
837 uint32_t idToIndex[MAX_POINTER_ID + 1];
838
839 CookedPointerData();
840 void clear();
841 void copyFrom(const CookedPointerData& other);
842
843 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
844 return pointerCoords[idToIndex[id]];
845 }
846
Michael Wright842500e2015-03-13 17:32:02 -0700847 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
848 return pointerCoords[idToIndex[id]];
849 }
850
851 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
852 return pointerProperties[idToIndex[id]];
853 }
854
Michael Wright53dca3a2015-04-23 17:39:53 +0100855 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
857 }
Michael Wright842500e2015-03-13 17:32:02 -0700858
Michael Wright53dca3a2015-04-23 17:39:53 +0100859 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700860 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
861 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862};
863
864
865/* Keeps track of the state of single-touch protocol. */
866class SingleTouchMotionAccumulator {
867public:
868 SingleTouchMotionAccumulator();
869
870 void process(const RawEvent* rawEvent);
871 void reset(InputDevice* device);
872
873 inline int32_t getAbsoluteX() const { return mAbsX; }
874 inline int32_t getAbsoluteY() const { return mAbsY; }
875 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
876 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
877 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
878 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
879 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
880
881private:
882 int32_t mAbsX;
883 int32_t mAbsY;
884 int32_t mAbsPressure;
885 int32_t mAbsToolWidth;
886 int32_t mAbsDistance;
887 int32_t mAbsTiltX;
888 int32_t mAbsTiltY;
889
890 void clearAbsoluteAxes();
891};
892
893
894/* Keeps track of the state of multi-touch protocol. */
895class MultiTouchMotionAccumulator {
896public:
897 class Slot {
898 public:
899 inline bool isInUse() const { return mInUse; }
900 inline int32_t getX() const { return mAbsMTPositionX; }
901 inline int32_t getY() const { return mAbsMTPositionY; }
902 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
903 inline int32_t getTouchMinor() const {
904 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
905 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
906 inline int32_t getToolMinor() const {
907 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
908 inline int32_t getOrientation() const { return mAbsMTOrientation; }
909 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
910 inline int32_t getPressure() const { return mAbsMTPressure; }
911 inline int32_t getDistance() const { return mAbsMTDistance; }
912 inline int32_t getToolType() const;
913
914 private:
915 friend class MultiTouchMotionAccumulator;
916
917 bool mInUse;
918 bool mHaveAbsMTTouchMinor;
919 bool mHaveAbsMTWidthMinor;
920 bool mHaveAbsMTToolType;
921
922 int32_t mAbsMTPositionX;
923 int32_t mAbsMTPositionY;
924 int32_t mAbsMTTouchMajor;
925 int32_t mAbsMTTouchMinor;
926 int32_t mAbsMTWidthMajor;
927 int32_t mAbsMTWidthMinor;
928 int32_t mAbsMTOrientation;
929 int32_t mAbsMTTrackingId;
930 int32_t mAbsMTPressure;
931 int32_t mAbsMTDistance;
932 int32_t mAbsMTToolType;
933
934 Slot();
935 void clear();
936 };
937
938 MultiTouchMotionAccumulator();
939 ~MultiTouchMotionAccumulator();
940
941 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
942 void reset(InputDevice* device);
943 void process(const RawEvent* rawEvent);
944 void finishSync();
945 bool hasStylus() const;
946
947 inline size_t getSlotCount() const { return mSlotCount; }
948 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800949 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
951private:
952 int32_t mCurrentSlot;
953 Slot* mSlots;
954 size_t mSlotCount;
955 bool mUsingSlotsProtocol;
956 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800957 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
959 void clearSlots(int32_t initialSlot);
960};
961
962
963/* An input mapper transforms raw input events into cooked event data.
964 * A single input device can have multiple associated input mappers in order to interpret
965 * different classes of events.
966 *
967 * InputMapper lifecycle:
968 * - create
969 * - configure with 0 changes
970 * - reset
971 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
972 * - reset
973 * - destroy
974 */
975class InputMapper {
976public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700977 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978 virtual ~InputMapper();
979
980 inline InputDevice* getDevice() { return mDevice; }
981 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100982 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983 inline InputReaderContext* getContext() { return mContext; }
984 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
985 inline InputListenerInterface* getListener() { return mContext->getListener(); }
986 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
987
988 virtual uint32_t getSources() = 0;
989 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800990 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
992 virtual void reset(nsecs_t when);
993 virtual void process(const RawEvent* rawEvent) = 0;
994 virtual void timeoutExpired(nsecs_t when);
995
996 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
997 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
998 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
999 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1000 const int32_t* keyCodes, uint8_t* outFlags);
1001 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1002 int32_t token);
1003 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -08001004 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005
1006 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -08001007 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008
Michael Wright842500e2015-03-13 17:32:02 -07001009 virtual void updateExternalStylusState(const StylusState& state);
1010
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 virtual void fadePointer();
1012
1013protected:
1014 InputDevice* mDevice;
1015 InputReaderContext* mContext;
1016
1017 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
1018 void bumpGeneration();
1019
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001020 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001022 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023};
1024
1025
1026class SwitchInputMapper : public InputMapper {
1027public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001028 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 virtual ~SwitchInputMapper();
1030
1031 virtual uint32_t getSources();
1032 virtual void process(const RawEvent* rawEvent);
1033
1034 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001035 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036
1037private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -07001038 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 uint32_t mUpdatedSwitchMask;
1040
1041 void processSwitch(int32_t switchCode, int32_t switchValue);
1042 void sync(nsecs_t when);
1043};
1044
1045
1046class VibratorInputMapper : public InputMapper {
1047public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001048 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049 virtual ~VibratorInputMapper();
1050
1051 virtual uint32_t getSources();
1052 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1053 virtual void process(const RawEvent* rawEvent);
1054
1055 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1056 int32_t token);
1057 virtual void cancelVibrate(int32_t token);
1058 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001059 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060
1061private:
1062 bool mVibrating;
1063 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
1064 size_t mPatternSize;
1065 ssize_t mRepeat;
1066 int32_t mToken;
1067 ssize_t mIndex;
1068 nsecs_t mNextStepTime;
1069
1070 void nextStep();
1071 void stopVibrating();
1072};
1073
1074
1075class KeyboardInputMapper : public InputMapper {
1076public:
1077 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
1078 virtual ~KeyboardInputMapper();
1079
1080 virtual uint32_t getSources();
1081 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001082 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1084 virtual void reset(nsecs_t when);
1085 virtual void process(const RawEvent* rawEvent);
1086
1087 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1088 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1089 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1090 const int32_t* keyCodes, uint8_t* outFlags);
1091
1092 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -08001093 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094
1095private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01001096 // The current viewport.
1097 std::optional<DisplayViewport> mViewport;
1098
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 struct KeyDown {
1100 int32_t keyCode;
1101 int32_t scanCode;
1102 };
1103
1104 uint32_t mSource;
1105 int32_t mKeyboardType;
1106
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 Vector<KeyDown> mKeyDowns; // keys that are down
1108 int32_t mMetaState;
1109 nsecs_t mDownTime; // time of most recent key down
1110
1111 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
1112
1113 struct LedState {
1114 bool avail; // led is available
1115 bool on; // we think the led is currently on
1116 };
1117 LedState mCapsLockLedState;
1118 LedState mNumLockLedState;
1119 LedState mScrollLockLedState;
1120
1121 // Immutable configuration parameters.
1122 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -07001124 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 } mParameters;
1126
1127 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001128 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01001130 int32_t getOrientation();
1131 int32_t getDisplayId();
1132
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +01001134 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -07001136 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137
Andrii Kulian763a3a42016-03-08 10:46:16 -08001138 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
1139
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140 ssize_t findKeyDown(int32_t scanCode);
1141
1142 void resetLedState();
1143 void initializeLedState(LedState& ledState, int32_t led);
1144 void updateLedState(bool reset);
1145 void updateLedStateForModifier(LedState& ledState, int32_t led,
1146 int32_t modifier, bool reset);
1147};
1148
1149
1150class CursorInputMapper : public InputMapper {
1151public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001152 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 virtual ~CursorInputMapper();
1154
1155 virtual uint32_t getSources();
1156 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001157 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1159 virtual void reset(nsecs_t when);
1160 virtual void process(const RawEvent* rawEvent);
1161
1162 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1163
1164 virtual void fadePointer();
1165
1166private:
1167 // Amount that trackball needs to move in order to generate a key event.
1168 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1169
1170 // Immutable configuration parameters.
1171 struct Parameters {
1172 enum Mode {
1173 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001174 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 MODE_NAVIGATION,
1176 };
1177
1178 Mode mode;
1179 bool hasAssociatedDisplay;
1180 bool orientationAware;
1181 } mParameters;
1182
1183 CursorButtonAccumulator mCursorButtonAccumulator;
1184 CursorMotionAccumulator mCursorMotionAccumulator;
1185 CursorScrollAccumulator mCursorScrollAccumulator;
1186
1187 int32_t mSource;
1188 float mXScale;
1189 float mYScale;
1190 float mXPrecision;
1191 float mYPrecision;
1192
1193 float mVWheelScale;
1194 float mHWheelScale;
1195
1196 // Velocity controls for mouse pointer and wheel movements.
1197 // The controls for X and Y wheel movements are separate to keep them decoupled.
1198 VelocityControl mPointerVelocityControl;
1199 VelocityControl mWheelXVelocityControl;
1200 VelocityControl mWheelYVelocityControl;
1201
1202 int32_t mOrientation;
1203
1204 sp<PointerControllerInterface> mPointerController;
1205
1206 int32_t mButtonState;
1207 nsecs_t mDownTime;
1208
1209 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001210 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211
1212 void sync(nsecs_t when);
1213};
1214
1215
Prashant Malani1941ff52015-08-11 18:29:28 -07001216class RotaryEncoderInputMapper : public InputMapper {
1217public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001218 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -07001219 virtual ~RotaryEncoderInputMapper();
1220
1221 virtual uint32_t getSources();
1222 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001223 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -07001224 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1225 virtual void reset(nsecs_t when);
1226 virtual void process(const RawEvent* rawEvent);
1227
1228private:
1229 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
1230
1231 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001232 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +01001233 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -07001234
1235 void sync(nsecs_t when);
1236};
1237
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238class TouchInputMapper : public InputMapper {
1239public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001240 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 virtual ~TouchInputMapper();
1242
1243 virtual uint32_t getSources();
1244 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001245 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1247 virtual void reset(nsecs_t when);
1248 virtual void process(const RawEvent* rawEvent);
1249
1250 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1251 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1252 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1253 const int32_t* keyCodes, uint8_t* outFlags);
1254
1255 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001256 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001258 virtual void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
1260protected:
1261 CursorButtonAccumulator mCursorButtonAccumulator;
1262 CursorScrollAccumulator mCursorScrollAccumulator;
1263 TouchButtonAccumulator mTouchButtonAccumulator;
1264
1265 struct VirtualKey {
1266 int32_t keyCode;
1267 int32_t scanCode;
1268 uint32_t flags;
1269
1270 // computed hit box, specified in touch screen coords based on known display size
1271 int32_t hitLeft;
1272 int32_t hitTop;
1273 int32_t hitRight;
1274 int32_t hitBottom;
1275
1276 inline bool isHit(int32_t x, int32_t y) const {
1277 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1278 }
1279 };
1280
1281 // Input sources and device mode.
1282 uint32_t mSource;
1283
1284 enum DeviceMode {
1285 DEVICE_MODE_DISABLED, // input is disabled
1286 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1287 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1288 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1289 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1290 };
1291 DeviceMode mDeviceMode;
1292
1293 // The reader's configuration.
1294 InputReaderConfiguration mConfig;
1295
1296 // Immutable configuration parameters.
1297 struct Parameters {
1298 enum DeviceType {
1299 DEVICE_TYPE_TOUCH_SCREEN,
1300 DEVICE_TYPE_TOUCH_PAD,
1301 DEVICE_TYPE_TOUCH_NAVIGATION,
1302 DEVICE_TYPE_POINTER,
1303 };
1304
1305 DeviceType deviceType;
1306 bool hasAssociatedDisplay;
1307 bool associatedDisplayIsExternal;
1308 bool orientationAware;
1309 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001310 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001311
1312 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001313 GESTURE_MODE_SINGLE_TOUCH,
1314 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315 };
1316 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001317
1318 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 } mParameters;
1320
1321 // Immutable calibration parameters in parsed form.
1322 struct Calibration {
1323 // Size
1324 enum SizeCalibration {
1325 SIZE_CALIBRATION_DEFAULT,
1326 SIZE_CALIBRATION_NONE,
1327 SIZE_CALIBRATION_GEOMETRIC,
1328 SIZE_CALIBRATION_DIAMETER,
1329 SIZE_CALIBRATION_BOX,
1330 SIZE_CALIBRATION_AREA,
1331 };
1332
1333 SizeCalibration sizeCalibration;
1334
1335 bool haveSizeScale;
1336 float sizeScale;
1337 bool haveSizeBias;
1338 float sizeBias;
1339 bool haveSizeIsSummed;
1340 bool sizeIsSummed;
1341
1342 // Pressure
1343 enum PressureCalibration {
1344 PRESSURE_CALIBRATION_DEFAULT,
1345 PRESSURE_CALIBRATION_NONE,
1346 PRESSURE_CALIBRATION_PHYSICAL,
1347 PRESSURE_CALIBRATION_AMPLITUDE,
1348 };
1349
1350 PressureCalibration pressureCalibration;
1351 bool havePressureScale;
1352 float pressureScale;
1353
1354 // Orientation
1355 enum OrientationCalibration {
1356 ORIENTATION_CALIBRATION_DEFAULT,
1357 ORIENTATION_CALIBRATION_NONE,
1358 ORIENTATION_CALIBRATION_INTERPOLATED,
1359 ORIENTATION_CALIBRATION_VECTOR,
1360 };
1361
1362 OrientationCalibration orientationCalibration;
1363
1364 // Distance
1365 enum DistanceCalibration {
1366 DISTANCE_CALIBRATION_DEFAULT,
1367 DISTANCE_CALIBRATION_NONE,
1368 DISTANCE_CALIBRATION_SCALED,
1369 };
1370
1371 DistanceCalibration distanceCalibration;
1372 bool haveDistanceScale;
1373 float distanceScale;
1374
1375 enum CoverageCalibration {
1376 COVERAGE_CALIBRATION_DEFAULT,
1377 COVERAGE_CALIBRATION_NONE,
1378 COVERAGE_CALIBRATION_BOX,
1379 };
1380
1381 CoverageCalibration coverageCalibration;
1382
1383 inline void applySizeScaleAndBias(float* outSize) const {
1384 if (haveSizeScale) {
1385 *outSize *= sizeScale;
1386 }
1387 if (haveSizeBias) {
1388 *outSize += sizeBias;
1389 }
1390 if (*outSize < 0) {
1391 *outSize = 0;
1392 }
1393 }
1394 } mCalibration;
1395
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001396 // Affine location transformation/calibration
1397 struct TouchAffineTransformation mAffineTransform;
1398
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 RawPointerAxes mRawPointerAxes;
1400
Michael Wright842500e2015-03-13 17:32:02 -07001401 struct RawState {
1402 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001403 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404
Michael Wright842500e2015-03-13 17:32:02 -07001405 // Raw pointer sample data.
1406 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407
Michael Wright842500e2015-03-13 17:32:02 -07001408 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409
Michael Wright842500e2015-03-13 17:32:02 -07001410 // Scroll state.
1411 int32_t rawVScroll;
1412 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001413
Michael Wright842500e2015-03-13 17:32:02 -07001414 void copyFrom(const RawState& other) {
1415 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001416 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001417 rawPointerData.copyFrom(other.rawPointerData);
1418 buttonState = other.buttonState;
1419 rawVScroll = other.rawVScroll;
1420 rawHScroll = other.rawHScroll;
1421 }
1422
1423 void clear() {
1424 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001425 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001426 rawPointerData.clear();
1427 buttonState = 0;
1428 rawVScroll = 0;
1429 rawHScroll = 0;
1430 }
1431 };
1432
1433 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001434 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001435 // Cooked pointer sample data.
1436 CookedPointerData cookedPointerData;
1437
1438 // Id bits used to differentiate fingers, stylus and mouse tools.
1439 BitSet32 fingerIdBits;
1440 BitSet32 stylusIdBits;
1441 BitSet32 mouseIdBits;
1442
Michael Wright7b159c92015-05-14 14:48:03 +01001443 int32_t buttonState;
1444
Michael Wright842500e2015-03-13 17:32:02 -07001445 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001446 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001447 cookedPointerData.copyFrom(other.cookedPointerData);
1448 fingerIdBits = other.fingerIdBits;
1449 stylusIdBits = other.stylusIdBits;
1450 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001451 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001452 }
1453
1454 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001455 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001456 cookedPointerData.clear();
1457 fingerIdBits.clear();
1458 stylusIdBits.clear();
1459 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001460 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001461 }
1462 };
1463
1464 Vector<RawState> mRawStatesPending;
1465 RawState mCurrentRawState;
1466 CookedState mCurrentCookedState;
1467 RawState mLastRawState;
1468 CookedState mLastCookedState;
1469
1470 // State provided by an external stylus
1471 StylusState mExternalStylusState;
1472 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001473 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001474 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
1476 // True if we sent a HOVER_ENTER event.
1477 bool mSentHoverEnter;
1478
Michael Wright842500e2015-03-13 17:32:02 -07001479 // Have we assigned pointer IDs for this stream
1480 bool mHavePointerIds;
1481
Michael Wright8e812822015-06-22 16:18:21 +01001482 // Is the current stream of direct touch events aborted
1483 bool mCurrentMotionAborted;
1484
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485 // The time the primary pointer last went down.
1486 nsecs_t mDownTime;
1487
1488 // The pointer controller, or null if the device is not a pointer.
1489 sp<PointerControllerInterface> mPointerController;
1490
1491 Vector<VirtualKey> mVirtualKeys;
1492
1493 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001494 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001496 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001498 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001500 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501 virtual void parseCalibration();
1502 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001503 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001504 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001505 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001506 virtual void resolveExternalStylusPresence();
1507 virtual bool hasStylus() const = 0;
1508 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509
Michael Wright842500e2015-03-13 17:32:02 -07001510 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511
1512private:
1513 // The current viewport.
1514 // The components of the viewport are specified in the display's rotated orientation.
1515 DisplayViewport mViewport;
1516
1517 // The surface orientation, width and height set by configureSurface().
1518 // The width and height are derived from the viewport but are specified
1519 // in the natural orientation.
1520 // The surface origin specifies how the surface coordinates should be translated
1521 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001522 int32_t mSurfaceWidth;
1523 int32_t mSurfaceHeight;
1524 int32_t mSurfaceLeft;
1525 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001526
1527 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1528 // the logical coordinate space.
1529 int32_t mPhysicalWidth;
1530 int32_t mPhysicalHeight;
1531 int32_t mPhysicalLeft;
1532 int32_t mPhysicalTop;
1533
1534 // The orientation may be different from the viewport orientation as it specifies
1535 // the rotation of the surface coordinates required to produce the viewport's
1536 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537 int32_t mSurfaceOrientation;
1538
1539 // Translation and scaling factors, orientation-independent.
1540 float mXTranslate;
1541 float mXScale;
1542 float mXPrecision;
1543
1544 float mYTranslate;
1545 float mYScale;
1546 float mYPrecision;
1547
1548 float mGeometricScale;
1549
1550 float mPressureScale;
1551
1552 float mSizeScale;
1553
1554 float mOrientationScale;
1555
1556 float mDistanceScale;
1557
1558 bool mHaveTilt;
1559 float mTiltXCenter;
1560 float mTiltXScale;
1561 float mTiltYCenter;
1562 float mTiltYScale;
1563
Michael Wright842500e2015-03-13 17:32:02 -07001564 bool mExternalStylusConnected;
1565
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566 // Oriented motion ranges for input device info.
1567 struct OrientedRanges {
1568 InputDeviceInfo::MotionRange x;
1569 InputDeviceInfo::MotionRange y;
1570 InputDeviceInfo::MotionRange pressure;
1571
1572 bool haveSize;
1573 InputDeviceInfo::MotionRange size;
1574
1575 bool haveTouchSize;
1576 InputDeviceInfo::MotionRange touchMajor;
1577 InputDeviceInfo::MotionRange touchMinor;
1578
1579 bool haveToolSize;
1580 InputDeviceInfo::MotionRange toolMajor;
1581 InputDeviceInfo::MotionRange toolMinor;
1582
1583 bool haveOrientation;
1584 InputDeviceInfo::MotionRange orientation;
1585
1586 bool haveDistance;
1587 InputDeviceInfo::MotionRange distance;
1588
1589 bool haveTilt;
1590 InputDeviceInfo::MotionRange tilt;
1591
1592 OrientedRanges() {
1593 clear();
1594 }
1595
1596 void clear() {
1597 haveSize = false;
1598 haveTouchSize = false;
1599 haveToolSize = false;
1600 haveOrientation = false;
1601 haveDistance = false;
1602 haveTilt = false;
1603 }
1604 } mOrientedRanges;
1605
1606 // Oriented dimensions and precision.
1607 float mOrientedXPrecision;
1608 float mOrientedYPrecision;
1609
1610 struct CurrentVirtualKeyState {
1611 bool down;
1612 bool ignored;
1613 nsecs_t downTime;
1614 int32_t keyCode;
1615 int32_t scanCode;
1616 } mCurrentVirtualKey;
1617
1618 // Scale factor for gesture or mouse based pointer movements.
1619 float mPointerXMovementScale;
1620 float mPointerYMovementScale;
1621
1622 // Scale factor for gesture based zooming and other freeform motions.
1623 float mPointerXZoomScale;
1624 float mPointerYZoomScale;
1625
1626 // The maximum swipe width.
1627 float mPointerGestureMaxSwipeWidth;
1628
1629 struct PointerDistanceHeapElement {
1630 uint32_t currentPointerIndex : 8;
1631 uint32_t lastPointerIndex : 8;
1632 uint64_t distance : 48; // squared distance
1633 };
1634
1635 enum PointerUsage {
1636 POINTER_USAGE_NONE,
1637 POINTER_USAGE_GESTURES,
1638 POINTER_USAGE_STYLUS,
1639 POINTER_USAGE_MOUSE,
1640 };
1641 PointerUsage mPointerUsage;
1642
1643 struct PointerGesture {
1644 enum Mode {
1645 // No fingers, button is not pressed.
1646 // Nothing happening.
1647 NEUTRAL,
1648
1649 // No fingers, button is not pressed.
1650 // Tap detected.
1651 // Emits DOWN and UP events at the pointer location.
1652 TAP,
1653
1654 // Exactly one finger dragging following a tap.
1655 // Pointer follows the active finger.
1656 // Emits DOWN, MOVE and UP events at the pointer location.
1657 //
1658 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1659 TAP_DRAG,
1660
1661 // Button is pressed.
1662 // Pointer follows the active finger if there is one. Other fingers are ignored.
1663 // Emits DOWN, MOVE and UP events at the pointer location.
1664 BUTTON_CLICK_OR_DRAG,
1665
1666 // Exactly one finger, button is not pressed.
1667 // Pointer follows the active finger.
1668 // Emits HOVER_MOVE events at the pointer location.
1669 //
1670 // Detect taps when the finger goes up while in HOVER mode.
1671 HOVER,
1672
1673 // Exactly two fingers but neither have moved enough to clearly indicate
1674 // whether a swipe or freeform gesture was intended. We consider the
1675 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1676 // Pointer does not move.
1677 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1678 PRESS,
1679
1680 // Exactly two fingers moving in the same direction, button is not pressed.
1681 // Pointer does not move.
1682 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1683 // follows the midpoint between both fingers.
1684 SWIPE,
1685
1686 // Two or more fingers moving in arbitrary directions, button is not pressed.
1687 // Pointer does not move.
1688 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1689 // each finger individually relative to the initial centroid of the finger.
1690 FREEFORM,
1691
1692 // Waiting for quiet time to end before starting the next gesture.
1693 QUIET,
1694 };
1695
1696 // Time the first finger went down.
1697 nsecs_t firstTouchTime;
1698
1699 // The active pointer id from the raw touch data.
1700 int32_t activeTouchId; // -1 if none
1701
1702 // The active pointer id from the gesture last delivered to the application.
1703 int32_t activeGestureId; // -1 if none
1704
1705 // Pointer coords and ids for the current and previous pointer gesture.
1706 Mode currentGestureMode;
1707 BitSet32 currentGestureIdBits;
1708 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1709 PointerProperties currentGestureProperties[MAX_POINTERS];
1710 PointerCoords currentGestureCoords[MAX_POINTERS];
1711
1712 Mode lastGestureMode;
1713 BitSet32 lastGestureIdBits;
1714 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1715 PointerProperties lastGestureProperties[MAX_POINTERS];
1716 PointerCoords lastGestureCoords[MAX_POINTERS];
1717
1718 // Time the pointer gesture last went down.
1719 nsecs_t downTime;
1720
1721 // Time when the pointer went down for a TAP.
1722 nsecs_t tapDownTime;
1723
1724 // Time when the pointer went up for a TAP.
1725 nsecs_t tapUpTime;
1726
1727 // Location of initial tap.
1728 float tapX, tapY;
1729
1730 // Time we started waiting for quiescence.
1731 nsecs_t quietTime;
1732
1733 // Reference points for multitouch gestures.
1734 float referenceTouchX; // reference touch X/Y coordinates in surface units
1735 float referenceTouchY;
1736 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1737 float referenceGestureY;
1738
1739 // Distance that each pointer has traveled which has not yet been
1740 // subsumed into the reference gesture position.
1741 BitSet32 referenceIdBits;
1742 struct Delta {
1743 float dx, dy;
1744 };
1745 Delta referenceDeltas[MAX_POINTER_ID + 1];
1746
1747 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1748 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1749
1750 // A velocity tracker for determining whether to switch active pointers during drags.
1751 VelocityTracker velocityTracker;
1752
1753 void reset() {
1754 firstTouchTime = LLONG_MIN;
1755 activeTouchId = -1;
1756 activeGestureId = -1;
1757 currentGestureMode = NEUTRAL;
1758 currentGestureIdBits.clear();
1759 lastGestureMode = NEUTRAL;
1760 lastGestureIdBits.clear();
1761 downTime = 0;
1762 velocityTracker.clear();
1763 resetTap();
1764 resetQuietTime();
1765 }
1766
1767 void resetTap() {
1768 tapDownTime = LLONG_MIN;
1769 tapUpTime = LLONG_MIN;
1770 }
1771
1772 void resetQuietTime() {
1773 quietTime = LLONG_MIN;
1774 }
1775 } mPointerGesture;
1776
1777 struct PointerSimple {
1778 PointerCoords currentCoords;
1779 PointerProperties currentProperties;
1780 PointerCoords lastCoords;
1781 PointerProperties lastProperties;
1782
1783 // True if the pointer is down.
1784 bool down;
1785
1786 // True if the pointer is hovering.
1787 bool hovering;
1788
1789 // Time the pointer last went down.
1790 nsecs_t downTime;
1791
1792 void reset() {
1793 currentCoords.clear();
1794 currentProperties.clear();
1795 lastCoords.clear();
1796 lastProperties.clear();
1797 down = false;
1798 hovering = false;
1799 downTime = 0;
1800 }
1801 } mPointerSimple;
1802
1803 // The pointer and scroll velocity controls.
1804 VelocityControl mPointerVelocityControl;
1805 VelocityControl mWheelXVelocityControl;
1806 VelocityControl mWheelYVelocityControl;
1807
Michael Wright842500e2015-03-13 17:32:02 -07001808 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001809 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001810
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811 void sync(nsecs_t when);
1812
1813 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001814 void processRawTouches(bool timeout);
1815 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001816 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1817 int32_t keyEventAction, int32_t keyEventFlags);
1818
1819 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1820 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1821 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001822 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1823 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1824 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001826 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827
1828 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1829 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1830
1831 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1832 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1833 bool preparePointerGestures(nsecs_t when,
1834 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1835 bool isTimeout);
1836
1837 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1838 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1839
1840 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1841 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1842
1843 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1844 bool down, bool hovering);
1845 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1846
Michael Wright842500e2015-03-13 17:32:02 -07001847 bool assignExternalStylusId(const RawState& state, bool timeout);
1848 void applyExternalStylusButtonState(nsecs_t when);
1849 void applyExternalStylusTouchState(nsecs_t when);
1850
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851 // Dispatches a motion event.
1852 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1853 // method will take care of setting the index and transmuting the action to DOWN or UP
1854 // it is the first / last pointer to go down / up.
1855 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001856 int32_t action, int32_t actionButton,
1857 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001858 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859 const PointerProperties* properties, const PointerCoords* coords,
1860 const uint32_t* idToIndex, BitSet32 idBits,
1861 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1862
1863 // Updates pointer coords and properties for pointers with specified ids that have moved.
1864 // Returns true if any of them changed.
1865 bool updateMovedPointers(const PointerProperties* inProperties,
1866 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1867 PointerProperties* outProperties, PointerCoords* outCoords,
1868 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1869
1870 bool isPointInsideSurface(int32_t x, int32_t y);
1871 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1872
Michael Wright842500e2015-03-13 17:32:02 -07001873 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001874
1875 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876};
1877
1878
1879class SingleTouchInputMapper : public TouchInputMapper {
1880public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001881 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001882 virtual ~SingleTouchInputMapper();
1883
1884 virtual void reset(nsecs_t when);
1885 virtual void process(const RawEvent* rawEvent);
1886
1887protected:
Michael Wright842500e2015-03-13 17:32:02 -07001888 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 virtual void configureRawPointerAxes();
1890 virtual bool hasStylus() const;
1891
1892private:
1893 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1894};
1895
1896
1897class MultiTouchInputMapper : public TouchInputMapper {
1898public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001899 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 virtual ~MultiTouchInputMapper();
1901
1902 virtual void reset(nsecs_t when);
1903 virtual void process(const RawEvent* rawEvent);
1904
1905protected:
Michael Wright842500e2015-03-13 17:32:02 -07001906 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907 virtual void configureRawPointerAxes();
1908 virtual bool hasStylus() const;
1909
1910private:
1911 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1912
1913 // Specifies the pointer id bits that are in use, and their associated tracking id.
1914 BitSet32 mPointerIdBits;
1915 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1916};
1917
Michael Wright842500e2015-03-13 17:32:02 -07001918class ExternalStylusInputMapper : public InputMapper {
1919public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001920 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001921 virtual ~ExternalStylusInputMapper() = default;
1922
1923 virtual uint32_t getSources();
1924 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001925 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001926 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1927 virtual void reset(nsecs_t when);
1928 virtual void process(const RawEvent* rawEvent);
1929 virtual void sync(nsecs_t when);
1930
1931private:
1932 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1933 RawAbsoluteAxisInfo mRawPressureAxis;
1934 TouchButtonAccumulator mTouchButtonAccumulator;
1935
1936 StylusState mStylusState;
1937};
1938
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939
1940class JoystickInputMapper : public InputMapper {
1941public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001942 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 virtual ~JoystickInputMapper();
1944
1945 virtual uint32_t getSources();
1946 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001947 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001948 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1949 virtual void reset(nsecs_t when);
1950 virtual void process(const RawEvent* rawEvent);
1951
1952private:
1953 struct Axis {
1954 RawAbsoluteAxisInfo rawAxisInfo;
1955 AxisInfo axisInfo;
1956
1957 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1958
1959 float scale; // scale factor from raw to normalized values
1960 float offset; // offset to add after scaling for normalization
1961 float highScale; // scale factor from raw to normalized values of high split
1962 float highOffset; // offset to add after scaling for normalization of high split
1963
1964 float min; // normalized inclusive minimum
1965 float max; // normalized inclusive maximum
1966 float flat; // normalized flat region size
1967 float fuzz; // normalized error tolerance
1968 float resolution; // normalized resolution in units/mm
1969
1970 float filter; // filter out small variations of this size
1971 float currentValue; // current value
1972 float newValue; // most recent value
1973 float highCurrentValue; // current value of high split
1974 float highNewValue; // most recent value of high split
1975
1976 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1977 bool explicitlyMapped, float scale, float offset,
1978 float highScale, float highOffset,
1979 float min, float max, float flat, float fuzz, float resolution) {
1980 this->rawAxisInfo = rawAxisInfo;
1981 this->axisInfo = axisInfo;
1982 this->explicitlyMapped = explicitlyMapped;
1983 this->scale = scale;
1984 this->offset = offset;
1985 this->highScale = highScale;
1986 this->highOffset = highOffset;
1987 this->min = min;
1988 this->max = max;
1989 this->flat = flat;
1990 this->fuzz = fuzz;
1991 this->resolution = resolution;
1992 this->filter = 0;
1993 resetValue();
1994 }
1995
1996 void resetValue() {
1997 this->currentValue = 0;
1998 this->newValue = 0;
1999 this->highCurrentValue = 0;
2000 this->highNewValue = 0;
2001 }
2002 };
2003
2004 // Axes indexed by raw ABS_* axis index.
2005 KeyedVector<int32_t, Axis> mAxes;
2006
2007 void sync(nsecs_t when, bool force);
2008
2009 bool haveAxis(int32_t axisId);
2010 void pruneAxes(bool ignoreExplicitlyMappedAxes);
2011 bool filterAxes(bool force);
2012
2013 static bool hasValueChangedSignificantly(float filter,
2014 float newValue, float currentValue, float min, float max);
2015 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
2016 float newValue, float currentValue, float thresholdValue);
2017
2018 static bool isCenteredAxis(int32_t axis);
2019 static int32_t getCompatAxis(int32_t axis);
2020
2021 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
2022 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
2023 float value);
2024};
2025
2026} // namespace android
2027
2028#endif // _UI_INPUT_READER_H