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