blob: aaffce294e66a281adef2642a00a78d09df88da3 [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"
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include "InputReaderBase.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Santos Cordonfa5cf462017-04-05 10:37:00 -070025#include <input/DisplayViewport.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/Input.h>
27#include <input/VelocityControl.h>
28#include <input/VelocityTracker.h>
29#include <ui/DisplayInfo.h>
30#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070031#include <utils/Condition.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070032#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <utils/BitSet.h>
35
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010036#include <optional>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.h>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010039#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
43class InputDevice;
44class InputMapper;
45
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Michael Wright842500e2015-03-13 17:32:02 -070047struct StylusState {
48 /* Time the stylus event was received. */
49 nsecs_t when;
50 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
51 float pressure;
52 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
53 uint32_t buttons;
54 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
55 int32_t toolType;
56
57 void copyFrom(const StylusState& other) {
58 when = other.when;
59 pressure = other.pressure;
60 buttons = other.buttons;
61 toolType = other.toolType;
62 }
63
64 void clear() {
65 when = LLONG_MAX;
66 pressure = 0.f;
67 buttons = 0;
68 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
69 }
70};
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
73/* Internal interface used by individual input devices to access global input device state
74 * and parameters maintained by the input reader.
75 */
76class InputReaderContext {
77public:
78 InputReaderContext() { }
79 virtual ~InputReaderContext() { }
80
81 virtual void updateGlobalMetaState() = 0;
82 virtual int32_t getGlobalMetaState() = 0;
83
84 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
85 virtual bool shouldDropVirtualKey(nsecs_t now,
86 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
87
88 virtual void fadePointer() = 0;
89
90 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
91 virtual int32_t bumpGeneration() = 0;
92
Michael Wrightb85401d2015-04-17 18:35:15 +010093 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
94 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -070095
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 virtual InputReaderPolicyInterface* getPolicy() = 0;
97 virtual InputListenerInterface* getListener() = 0;
98 virtual EventHubInterface* getEventHub() = 0;
Prabir Pradhan42611e02018-11-27 14:04:02 -080099
100 virtual uint32_t getNextSequenceNum() = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101};
102
103
104/* The input reader reads raw event data from the event hub and processes it into input events
105 * that it sends to the input listener. Some functions of the input reader, such as early
106 * event filtering in low power states, are controlled by a separate policy object.
107 *
108 * The InputReader owns a collection of InputMappers. Most of the work it does happens
109 * on the input reader thread but the InputReader can receive queries from other system
110 * components running on arbitrary threads. To keep things manageable, the InputReader
111 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
112 * EventHub or the InputReaderPolicy but it is never held while calling into the
113 * InputListener.
114 */
115class InputReader : public InputReaderInterface {
116public:
117 InputReader(const sp<EventHubInterface>& eventHub,
118 const sp<InputReaderPolicyInterface>& policy,
119 const sp<InputListenerInterface>& listener);
120 virtual ~InputReader();
121
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800122 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 virtual void monitor();
124
125 virtual void loopOnce();
126
127 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
128
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700129 virtual bool isInputDeviceEnabled(int32_t deviceId);
130
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
132 int32_t scanCode);
133 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
134 int32_t keyCode);
135 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
136 int32_t sw);
137
Andrii Kulian763a3a42016-03-08 10:46:16 -0800138 virtual void toggleCapsLockState(int32_t deviceId);
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
142
143 virtual void requestRefreshConfiguration(uint32_t changes);
144
145 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
146 ssize_t repeat, int32_t token);
147 virtual void cancelVibrate(int32_t deviceId, int32_t token);
148
149protected:
150 // These members are protected so they can be instrumented by test cases.
151 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
152 const InputDeviceIdentifier& identifier, uint32_t classes);
153
154 class ContextImpl : public InputReaderContext {
155 InputReader* mReader;
156
157 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700158 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
160 virtual void updateGlobalMetaState();
161 virtual int32_t getGlobalMetaState();
162 virtual void disableVirtualKeysUntil(nsecs_t time);
163 virtual bool shouldDropVirtualKey(nsecs_t now,
164 InputDevice* device, int32_t keyCode, int32_t scanCode);
165 virtual void fadePointer();
166 virtual void requestTimeoutAtTime(nsecs_t when);
167 virtual int32_t bumpGeneration();
Michael Wright842500e2015-03-13 17:32:02 -0700168 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
169 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 virtual InputReaderPolicyInterface* getPolicy();
171 virtual InputListenerInterface* getListener();
172 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800173 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 } mContext;
175
176 friend class ContextImpl;
177
178private:
179 Mutex mLock;
180
181 Condition mReaderIsAliveCondition;
182
183 sp<EventHubInterface> mEventHub;
184 sp<InputReaderPolicyInterface> mPolicy;
185 sp<QueuedInputListener> mQueuedListener;
186
187 InputReaderConfiguration mConfig;
188
Prabir Pradhan42611e02018-11-27 14:04:02 -0800189 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
190 uint32_t mNextSequenceNum;
191
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 // The event queue.
193 static const int EVENT_BUFFER_SIZE = 256;
194 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
195
196 KeyedVector<int32_t, InputDevice*> mDevices;
197
198 // low-level input event decoding and device management
199 void processEventsLocked(const RawEvent* rawEvents, size_t count);
200
201 void addDeviceLocked(nsecs_t when, int32_t deviceId);
202 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
203 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
204 void timeoutExpiredLocked(nsecs_t when);
205
206 void handleConfigurationChangedLocked(nsecs_t when);
207
208 int32_t mGlobalMetaState;
209 void updateGlobalMetaStateLocked();
210 int32_t getGlobalMetaStateLocked();
211
Michael Wright842500e2015-03-13 17:32:02 -0700212 void notifyExternalStylusPresenceChanged();
213 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
214 void dispatchExternalStylusState(const StylusState& state);
215
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 void fadePointerLocked();
217
218 int32_t mGeneration;
219 int32_t bumpGenerationLocked();
220
221 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
222
223 nsecs_t mDisableVirtualKeysTimeout;
224 void disableVirtualKeysUntilLocked(nsecs_t time);
225 bool shouldDropVirtualKeyLocked(nsecs_t now,
226 InputDevice* device, int32_t keyCode, int32_t scanCode);
227
228 nsecs_t mNextTimeout;
229 void requestTimeoutAtTimeLocked(nsecs_t when);
230
231 uint32_t mConfigurationChangesToRefresh;
232 void refreshConfigurationLocked(uint32_t changes);
233
234 // state queries
235 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
236 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
237 GetStateFunc getStateFunc);
238 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
239 const int32_t* keyCodes, uint8_t* outFlags);
240};
241
242
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243/* Represents the state of a single input device. */
244class InputDevice {
245public:
246 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
247 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
248 ~InputDevice();
249
250 inline InputReaderContext* getContext() { return mContext; }
251 inline int32_t getId() const { return mId; }
252 inline int32_t getControllerNumber() const { return mControllerNumber; }
253 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100254 inline const std::string getName() const { return mIdentifier.name; }
255 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 inline uint32_t getClasses() const { return mClasses; }
257 inline uint32_t getSources() const { return mSources; }
258
259 inline bool isExternal() { return mIsExternal; }
260 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700261 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
262 return mAssociatedDisplayPort;
263 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264
Tim Kilbourn063ff532015-04-08 10:26:18 -0700265 inline void setMic(bool hasMic) { mHasMic = hasMic; }
266 inline bool hasMic() const { return mHasMic; }
267
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 inline bool isIgnored() { return mMappers.isEmpty(); }
269
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700270 bool isEnabled();
271 void setEnabled(bool enabled, nsecs_t when);
272
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800273 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 void addMapper(InputMapper* mapper);
275 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
276 void reset(nsecs_t when);
277 void process(const RawEvent* rawEvents, size_t count);
278 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700279 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280
281 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
282 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
283 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
284 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
285 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
286 const int32_t* keyCodes, uint8_t* outFlags);
287 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
288 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800289 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290
291 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800292 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293
294 void fadePointer();
295
296 void bumpGeneration();
297
298 void notifyReset(nsecs_t when);
299
300 inline const PropertyMap& getConfiguration() { return mConfiguration; }
301 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
302
303 bool hasKey(int32_t code) {
304 return getEventHub()->hasScanCode(mId, code);
305 }
306
307 bool hasAbsoluteAxis(int32_t code) {
308 RawAbsoluteAxisInfo info;
309 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
310 return info.valid;
311 }
312
313 bool isKeyPressed(int32_t code) {
314 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
315 }
316
317 int32_t getAbsoluteAxisValue(int32_t code) {
318 int32_t value;
319 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
320 return value;
321 }
322
323private:
324 InputReaderContext* mContext;
325 int32_t mId;
326 int32_t mGeneration;
327 int32_t mControllerNumber;
328 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100329 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 uint32_t mClasses;
331
332 Vector<InputMapper*> mMappers;
333
334 uint32_t mSources;
335 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700336 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700337 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 bool mDropUntilNextSync;
339
340 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
341 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
342
343 PropertyMap mConfiguration;
344};
345
346
347/* Keeps track of the state of mouse or touch pad buttons. */
348class CursorButtonAccumulator {
349public:
350 CursorButtonAccumulator();
351 void reset(InputDevice* device);
352
353 void process(const RawEvent* rawEvent);
354
355 uint32_t getButtonState() const;
356
357private:
358 bool mBtnLeft;
359 bool mBtnRight;
360 bool mBtnMiddle;
361 bool mBtnBack;
362 bool mBtnSide;
363 bool mBtnForward;
364 bool mBtnExtra;
365 bool mBtnTask;
366
367 void clearButtons();
368};
369
370
371/* Keeps track of cursor movements. */
372
373class CursorMotionAccumulator {
374public:
375 CursorMotionAccumulator();
376 void reset(InputDevice* device);
377
378 void process(const RawEvent* rawEvent);
379 void finishSync();
380
381 inline int32_t getRelativeX() const { return mRelX; }
382 inline int32_t getRelativeY() const { return mRelY; }
383
384private:
385 int32_t mRelX;
386 int32_t mRelY;
387
388 void clearRelativeAxes();
389};
390
391
392/* Keeps track of cursor scrolling motions. */
393
394class CursorScrollAccumulator {
395public:
396 CursorScrollAccumulator();
397 void configure(InputDevice* device);
398 void reset(InputDevice* device);
399
400 void process(const RawEvent* rawEvent);
401 void finishSync();
402
403 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
404 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
405
406 inline int32_t getRelativeX() const { return mRelX; }
407 inline int32_t getRelativeY() const { return mRelY; }
408 inline int32_t getRelativeVWheel() const { return mRelWheel; }
409 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
410
411private:
412 bool mHaveRelWheel;
413 bool mHaveRelHWheel;
414
415 int32_t mRelX;
416 int32_t mRelY;
417 int32_t mRelWheel;
418 int32_t mRelHWheel;
419
420 void clearRelativeAxes();
421};
422
423
424/* Keeps track of the state of touch, stylus and tool buttons. */
425class TouchButtonAccumulator {
426public:
427 TouchButtonAccumulator();
428 void configure(InputDevice* device);
429 void reset(InputDevice* device);
430
431 void process(const RawEvent* rawEvent);
432
433 uint32_t getButtonState() const;
434 int32_t getToolType() const;
435 bool isToolActive() const;
436 bool isHovering() const;
437 bool hasStylus() const;
438
439private:
440 bool mHaveBtnTouch;
441 bool mHaveStylus;
442
443 bool mBtnTouch;
444 bool mBtnStylus;
445 bool mBtnStylus2;
446 bool mBtnToolFinger;
447 bool mBtnToolPen;
448 bool mBtnToolRubber;
449 bool mBtnToolBrush;
450 bool mBtnToolPencil;
451 bool mBtnToolAirbrush;
452 bool mBtnToolMouse;
453 bool mBtnToolLens;
454 bool mBtnToolDoubleTap;
455 bool mBtnToolTripleTap;
456 bool mBtnToolQuadTap;
457
458 void clearButtons();
459};
460
461
462/* Raw axis information from the driver. */
463struct RawPointerAxes {
464 RawAbsoluteAxisInfo x;
465 RawAbsoluteAxisInfo y;
466 RawAbsoluteAxisInfo pressure;
467 RawAbsoluteAxisInfo touchMajor;
468 RawAbsoluteAxisInfo touchMinor;
469 RawAbsoluteAxisInfo toolMajor;
470 RawAbsoluteAxisInfo toolMinor;
471 RawAbsoluteAxisInfo orientation;
472 RawAbsoluteAxisInfo distance;
473 RawAbsoluteAxisInfo tiltX;
474 RawAbsoluteAxisInfo tiltY;
475 RawAbsoluteAxisInfo trackingId;
476 RawAbsoluteAxisInfo slot;
477
478 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800479 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
480 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 void clear();
482};
483
484
485/* Raw data for a collection of pointers including a pointer id mapping table. */
486struct RawPointerData {
487 struct Pointer {
488 uint32_t id;
489 int32_t x;
490 int32_t y;
491 int32_t pressure;
492 int32_t touchMajor;
493 int32_t touchMinor;
494 int32_t toolMajor;
495 int32_t toolMinor;
496 int32_t orientation;
497 int32_t distance;
498 int32_t tiltX;
499 int32_t tiltY;
500 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
501 bool isHovering;
502 };
503
504 uint32_t pointerCount;
505 Pointer pointers[MAX_POINTERS];
506 BitSet32 hoveringIdBits, touchingIdBits;
507 uint32_t idToIndex[MAX_POINTER_ID + 1];
508
509 RawPointerData();
510 void clear();
511 void copyFrom(const RawPointerData& other);
512 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
513
514 inline void markIdBit(uint32_t id, bool isHovering) {
515 if (isHovering) {
516 hoveringIdBits.markBit(id);
517 } else {
518 touchingIdBits.markBit(id);
519 }
520 }
521
522 inline void clearIdBits() {
523 hoveringIdBits.clear();
524 touchingIdBits.clear();
525 }
526
527 inline const Pointer& pointerForId(uint32_t id) const {
528 return pointers[idToIndex[id]];
529 }
530
531 inline bool isHovering(uint32_t pointerIndex) {
532 return pointers[pointerIndex].isHovering;
533 }
534};
535
536
537/* Cooked data for a collection of pointers including a pointer id mapping table. */
538struct CookedPointerData {
539 uint32_t pointerCount;
540 PointerProperties pointerProperties[MAX_POINTERS];
541 PointerCoords pointerCoords[MAX_POINTERS];
542 BitSet32 hoveringIdBits, touchingIdBits;
543 uint32_t idToIndex[MAX_POINTER_ID + 1];
544
545 CookedPointerData();
546 void clear();
547 void copyFrom(const CookedPointerData& other);
548
549 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
550 return pointerCoords[idToIndex[id]];
551 }
552
Michael Wright842500e2015-03-13 17:32:02 -0700553 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
554 return pointerCoords[idToIndex[id]];
555 }
556
557 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
558 return pointerProperties[idToIndex[id]];
559 }
560
Michael Wright53dca3a2015-04-23 17:39:53 +0100561 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
563 }
Michael Wright842500e2015-03-13 17:32:02 -0700564
Michael Wright53dca3a2015-04-23 17:39:53 +0100565 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700566 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
567 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568};
569
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -0800570/**
571 * Basic statistics information.
572 * Keep track of min, max, average, and standard deviation of the received samples.
573 * Used to report latency information about input events.
574 */
575struct LatencyStatistics {
576 float min;
577 float max;
578 // Sum of all samples
579 float sum;
580 // Sum of squares of all samples
581 float sum2;
582 // The number of samples
583 size_t count;
584 // The last time statistics were reported.
585 nsecs_t lastReportTime;
586
587 LatencyStatistics() {
588 reset(systemTime(SYSTEM_TIME_MONOTONIC));
589 }
590
591 inline void addValue(float x) {
592 if (x < min) {
593 min = x;
594 }
595 if (x > max) {
596 max = x;
597 }
598 sum += x;
599 sum2 += x * x;
600 count++;
601 }
602
603 // Get the average value. Should not be called if no samples have been added.
604 inline float mean() {
605 if (count == 0) {
606 return 0;
607 }
608 return sum / count;
609 }
610
611 // Get the standard deviation. Should not be called if no samples have been added.
612 inline float stdev() {
613 if (count == 0) {
614 return 0;
615 }
616 float average = mean();
617 return sqrt(sum2 / count - average * average);
618 }
619
620 /**
621 * Reset internal state. The variable 'when' is the time when the data collection started.
622 * Call this to start a new data collection window.
623 */
624 inline void reset(nsecs_t when) {
625 max = 0;
626 min = std::numeric_limits<float>::max();
627 sum = 0;
628 sum2 = 0;
629 count = 0;
630 lastReportTime = when;
631 }
632};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633
634/* Keeps track of the state of single-touch protocol. */
635class SingleTouchMotionAccumulator {
636public:
637 SingleTouchMotionAccumulator();
638
639 void process(const RawEvent* rawEvent);
640 void reset(InputDevice* device);
641
642 inline int32_t getAbsoluteX() const { return mAbsX; }
643 inline int32_t getAbsoluteY() const { return mAbsY; }
644 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
645 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
646 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
647 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
648 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
649
650private:
651 int32_t mAbsX;
652 int32_t mAbsY;
653 int32_t mAbsPressure;
654 int32_t mAbsToolWidth;
655 int32_t mAbsDistance;
656 int32_t mAbsTiltX;
657 int32_t mAbsTiltY;
658
659 void clearAbsoluteAxes();
660};
661
662
663/* Keeps track of the state of multi-touch protocol. */
664class MultiTouchMotionAccumulator {
665public:
666 class Slot {
667 public:
668 inline bool isInUse() const { return mInUse; }
669 inline int32_t getX() const { return mAbsMTPositionX; }
670 inline int32_t getY() const { return mAbsMTPositionY; }
671 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
672 inline int32_t getTouchMinor() const {
673 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
674 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
675 inline int32_t getToolMinor() const {
676 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
677 inline int32_t getOrientation() const { return mAbsMTOrientation; }
678 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
679 inline int32_t getPressure() const { return mAbsMTPressure; }
680 inline int32_t getDistance() const { return mAbsMTDistance; }
681 inline int32_t getToolType() const;
682
683 private:
684 friend class MultiTouchMotionAccumulator;
685
686 bool mInUse;
687 bool mHaveAbsMTTouchMinor;
688 bool mHaveAbsMTWidthMinor;
689 bool mHaveAbsMTToolType;
690
691 int32_t mAbsMTPositionX;
692 int32_t mAbsMTPositionY;
693 int32_t mAbsMTTouchMajor;
694 int32_t mAbsMTTouchMinor;
695 int32_t mAbsMTWidthMajor;
696 int32_t mAbsMTWidthMinor;
697 int32_t mAbsMTOrientation;
698 int32_t mAbsMTTrackingId;
699 int32_t mAbsMTPressure;
700 int32_t mAbsMTDistance;
701 int32_t mAbsMTToolType;
702
703 Slot();
704 void clear();
705 };
706
707 MultiTouchMotionAccumulator();
708 ~MultiTouchMotionAccumulator();
709
710 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
711 void reset(InputDevice* device);
712 void process(const RawEvent* rawEvent);
713 void finishSync();
714 bool hasStylus() const;
715
716 inline size_t getSlotCount() const { return mSlotCount; }
717 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800718 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719
720private:
721 int32_t mCurrentSlot;
722 Slot* mSlots;
723 size_t mSlotCount;
724 bool mUsingSlotsProtocol;
725 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800726 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727
728 void clearSlots(int32_t initialSlot);
729};
730
731
732/* An input mapper transforms raw input events into cooked event data.
733 * A single input device can have multiple associated input mappers in order to interpret
734 * different classes of events.
735 *
736 * InputMapper lifecycle:
737 * - create
738 * - configure with 0 changes
739 * - reset
740 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
741 * - reset
742 * - destroy
743 */
744class InputMapper {
745public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700746 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 virtual ~InputMapper();
748
749 inline InputDevice* getDevice() { return mDevice; }
750 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100751 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 inline InputReaderContext* getContext() { return mContext; }
753 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
754 inline InputListenerInterface* getListener() { return mContext->getListener(); }
755 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
756
757 virtual uint32_t getSources() = 0;
758 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800759 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
761 virtual void reset(nsecs_t when);
762 virtual void process(const RawEvent* rawEvent) = 0;
763 virtual void timeoutExpired(nsecs_t when);
764
765 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
766 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
767 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
768 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
769 const int32_t* keyCodes, uint8_t* outFlags);
770 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
771 int32_t token);
772 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800773 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774
775 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800776 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777
Michael Wright842500e2015-03-13 17:32:02 -0700778 virtual void updateExternalStylusState(const StylusState& state);
779
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780 virtual void fadePointer();
781
782protected:
783 InputDevice* mDevice;
784 InputReaderContext* mContext;
785
786 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
787 void bumpGeneration();
788
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800789 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800791 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792};
793
794
795class SwitchInputMapper : public InputMapper {
796public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700797 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 virtual ~SwitchInputMapper();
799
800 virtual uint32_t getSources();
801 virtual void process(const RawEvent* rawEvent);
802
803 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800804 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700807 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 uint32_t mUpdatedSwitchMask;
809
810 void processSwitch(int32_t switchCode, int32_t switchValue);
811 void sync(nsecs_t when);
812};
813
814
815class VibratorInputMapper : public InputMapper {
816public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700817 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 virtual ~VibratorInputMapper();
819
820 virtual uint32_t getSources();
821 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
822 virtual void process(const RawEvent* rawEvent);
823
824 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
825 int32_t token);
826 virtual void cancelVibrate(int32_t token);
827 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800828 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829
830private:
831 bool mVibrating;
832 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
833 size_t mPatternSize;
834 ssize_t mRepeat;
835 int32_t mToken;
836 ssize_t mIndex;
837 nsecs_t mNextStepTime;
838
839 void nextStep();
840 void stopVibrating();
841};
842
843
844class KeyboardInputMapper : public InputMapper {
845public:
846 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
847 virtual ~KeyboardInputMapper();
848
849 virtual uint32_t getSources();
850 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800851 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
853 virtual void reset(nsecs_t when);
854 virtual void process(const RawEvent* rawEvent);
855
856 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
857 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
858 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
859 const int32_t* keyCodes, uint8_t* outFlags);
860
861 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800862 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
864private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100865 // The current viewport.
866 std::optional<DisplayViewport> mViewport;
867
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 struct KeyDown {
869 int32_t keyCode;
870 int32_t scanCode;
871 };
872
873 uint32_t mSource;
874 int32_t mKeyboardType;
875
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 Vector<KeyDown> mKeyDowns; // keys that are down
877 int32_t mMetaState;
878 nsecs_t mDownTime; // time of most recent key down
879
880 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
881
882 struct LedState {
883 bool avail; // led is available
884 bool on; // we think the led is currently on
885 };
886 LedState mCapsLockLedState;
887 LedState mNumLockLedState;
888 LedState mScrollLockLedState;
889
890 // Immutable configuration parameters.
891 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700893 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 } mParameters;
895
896 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800897 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100899 int32_t getOrientation();
900 int32_t getDisplayId();
901
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100903 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700905 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906
Andrii Kulian763a3a42016-03-08 10:46:16 -0800907 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
908
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 ssize_t findKeyDown(int32_t scanCode);
910
911 void resetLedState();
912 void initializeLedState(LedState& ledState, int32_t led);
913 void updateLedState(bool reset);
914 void updateLedStateForModifier(LedState& ledState, int32_t led,
915 int32_t modifier, bool reset);
916};
917
918
919class CursorInputMapper : public InputMapper {
920public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700921 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 virtual ~CursorInputMapper();
923
924 virtual uint32_t getSources();
925 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800926 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
928 virtual void reset(nsecs_t when);
929 virtual void process(const RawEvent* rawEvent);
930
931 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
932
933 virtual void fadePointer();
934
935private:
936 // Amount that trackball needs to move in order to generate a key event.
937 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
938
939 // Immutable configuration parameters.
940 struct Parameters {
941 enum Mode {
942 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800943 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 MODE_NAVIGATION,
945 };
946
947 Mode mode;
948 bool hasAssociatedDisplay;
949 bool orientationAware;
950 } mParameters;
951
952 CursorButtonAccumulator mCursorButtonAccumulator;
953 CursorMotionAccumulator mCursorMotionAccumulator;
954 CursorScrollAccumulator mCursorScrollAccumulator;
955
956 int32_t mSource;
957 float mXScale;
958 float mYScale;
959 float mXPrecision;
960 float mYPrecision;
961
962 float mVWheelScale;
963 float mHWheelScale;
964
965 // Velocity controls for mouse pointer and wheel movements.
966 // The controls for X and Y wheel movements are separate to keep them decoupled.
967 VelocityControl mPointerVelocityControl;
968 VelocityControl mWheelXVelocityControl;
969 VelocityControl mWheelYVelocityControl;
970
971 int32_t mOrientation;
972
973 sp<PointerControllerInterface> mPointerController;
974
975 int32_t mButtonState;
976 nsecs_t mDownTime;
977
978 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800979 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980
981 void sync(nsecs_t when);
982};
983
984
Prashant Malani1941ff52015-08-11 18:29:28 -0700985class RotaryEncoderInputMapper : public InputMapper {
986public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700987 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700988 virtual ~RotaryEncoderInputMapper();
989
990 virtual uint32_t getSources();
991 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800992 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700993 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
994 virtual void reset(nsecs_t when);
995 virtual void process(const RawEvent* rawEvent);
996
997private:
998 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
999
1000 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001001 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +01001002 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -07001003
1004 void sync(nsecs_t when);
1005};
1006
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007class TouchInputMapper : public InputMapper {
1008public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001009 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 virtual ~TouchInputMapper();
1011
1012 virtual uint32_t getSources();
1013 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001014 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1016 virtual void reset(nsecs_t when);
1017 virtual void process(const RawEvent* rawEvent);
1018
1019 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1020 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1021 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1022 const int32_t* keyCodes, uint8_t* outFlags);
1023
1024 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001025 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001027 virtual void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028
1029protected:
1030 CursorButtonAccumulator mCursorButtonAccumulator;
1031 CursorScrollAccumulator mCursorScrollAccumulator;
1032 TouchButtonAccumulator mTouchButtonAccumulator;
1033
1034 struct VirtualKey {
1035 int32_t keyCode;
1036 int32_t scanCode;
1037 uint32_t flags;
1038
1039 // computed hit box, specified in touch screen coords based on known display size
1040 int32_t hitLeft;
1041 int32_t hitTop;
1042 int32_t hitRight;
1043 int32_t hitBottom;
1044
1045 inline bool isHit(int32_t x, int32_t y) const {
1046 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1047 }
1048 };
1049
1050 // Input sources and device mode.
1051 uint32_t mSource;
1052
1053 enum DeviceMode {
1054 DEVICE_MODE_DISABLED, // input is disabled
1055 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1056 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1057 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1058 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1059 };
1060 DeviceMode mDeviceMode;
1061
1062 // The reader's configuration.
1063 InputReaderConfiguration mConfig;
1064
1065 // Immutable configuration parameters.
1066 struct Parameters {
1067 enum DeviceType {
1068 DEVICE_TYPE_TOUCH_SCREEN,
1069 DEVICE_TYPE_TOUCH_PAD,
1070 DEVICE_TYPE_TOUCH_NAVIGATION,
1071 DEVICE_TYPE_POINTER,
1072 };
1073
1074 DeviceType deviceType;
1075 bool hasAssociatedDisplay;
1076 bool associatedDisplayIsExternal;
1077 bool orientationAware;
1078 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001079 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080
1081 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001082 GESTURE_MODE_SINGLE_TOUCH,
1083 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 };
1085 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001086
1087 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088 } mParameters;
1089
1090 // Immutable calibration parameters in parsed form.
1091 struct Calibration {
1092 // Size
1093 enum SizeCalibration {
1094 SIZE_CALIBRATION_DEFAULT,
1095 SIZE_CALIBRATION_NONE,
1096 SIZE_CALIBRATION_GEOMETRIC,
1097 SIZE_CALIBRATION_DIAMETER,
1098 SIZE_CALIBRATION_BOX,
1099 SIZE_CALIBRATION_AREA,
1100 };
1101
1102 SizeCalibration sizeCalibration;
1103
1104 bool haveSizeScale;
1105 float sizeScale;
1106 bool haveSizeBias;
1107 float sizeBias;
1108 bool haveSizeIsSummed;
1109 bool sizeIsSummed;
1110
1111 // Pressure
1112 enum PressureCalibration {
1113 PRESSURE_CALIBRATION_DEFAULT,
1114 PRESSURE_CALIBRATION_NONE,
1115 PRESSURE_CALIBRATION_PHYSICAL,
1116 PRESSURE_CALIBRATION_AMPLITUDE,
1117 };
1118
1119 PressureCalibration pressureCalibration;
1120 bool havePressureScale;
1121 float pressureScale;
1122
1123 // Orientation
1124 enum OrientationCalibration {
1125 ORIENTATION_CALIBRATION_DEFAULT,
1126 ORIENTATION_CALIBRATION_NONE,
1127 ORIENTATION_CALIBRATION_INTERPOLATED,
1128 ORIENTATION_CALIBRATION_VECTOR,
1129 };
1130
1131 OrientationCalibration orientationCalibration;
1132
1133 // Distance
1134 enum DistanceCalibration {
1135 DISTANCE_CALIBRATION_DEFAULT,
1136 DISTANCE_CALIBRATION_NONE,
1137 DISTANCE_CALIBRATION_SCALED,
1138 };
1139
1140 DistanceCalibration distanceCalibration;
1141 bool haveDistanceScale;
1142 float distanceScale;
1143
1144 enum CoverageCalibration {
1145 COVERAGE_CALIBRATION_DEFAULT,
1146 COVERAGE_CALIBRATION_NONE,
1147 COVERAGE_CALIBRATION_BOX,
1148 };
1149
1150 CoverageCalibration coverageCalibration;
1151
1152 inline void applySizeScaleAndBias(float* outSize) const {
1153 if (haveSizeScale) {
1154 *outSize *= sizeScale;
1155 }
1156 if (haveSizeBias) {
1157 *outSize += sizeBias;
1158 }
1159 if (*outSize < 0) {
1160 *outSize = 0;
1161 }
1162 }
1163 } mCalibration;
1164
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001165 // Affine location transformation/calibration
1166 struct TouchAffineTransformation mAffineTransform;
1167
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 RawPointerAxes mRawPointerAxes;
1169
Michael Wright842500e2015-03-13 17:32:02 -07001170 struct RawState {
1171 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001172 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173
Michael Wright842500e2015-03-13 17:32:02 -07001174 // Raw pointer sample data.
1175 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Michael Wright842500e2015-03-13 17:32:02 -07001177 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Michael Wright842500e2015-03-13 17:32:02 -07001179 // Scroll state.
1180 int32_t rawVScroll;
1181 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182
Michael Wright842500e2015-03-13 17:32:02 -07001183 void copyFrom(const RawState& other) {
1184 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001185 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001186 rawPointerData.copyFrom(other.rawPointerData);
1187 buttonState = other.buttonState;
1188 rawVScroll = other.rawVScroll;
1189 rawHScroll = other.rawHScroll;
1190 }
1191
1192 void clear() {
1193 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001194 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001195 rawPointerData.clear();
1196 buttonState = 0;
1197 rawVScroll = 0;
1198 rawHScroll = 0;
1199 }
1200 };
1201
1202 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001203 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001204 // Cooked pointer sample data.
1205 CookedPointerData cookedPointerData;
1206
1207 // Id bits used to differentiate fingers, stylus and mouse tools.
1208 BitSet32 fingerIdBits;
1209 BitSet32 stylusIdBits;
1210 BitSet32 mouseIdBits;
1211
Michael Wright7b159c92015-05-14 14:48:03 +01001212 int32_t buttonState;
1213
Michael Wright842500e2015-03-13 17:32:02 -07001214 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001215 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001216 cookedPointerData.copyFrom(other.cookedPointerData);
1217 fingerIdBits = other.fingerIdBits;
1218 stylusIdBits = other.stylusIdBits;
1219 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001220 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001221 }
1222
1223 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001224 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001225 cookedPointerData.clear();
1226 fingerIdBits.clear();
1227 stylusIdBits.clear();
1228 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001229 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001230 }
1231 };
1232
1233 Vector<RawState> mRawStatesPending;
1234 RawState mCurrentRawState;
1235 CookedState mCurrentCookedState;
1236 RawState mLastRawState;
1237 CookedState mLastCookedState;
1238
1239 // State provided by an external stylus
1240 StylusState mExternalStylusState;
1241 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001242 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001243 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244
1245 // True if we sent a HOVER_ENTER event.
1246 bool mSentHoverEnter;
1247
Michael Wright842500e2015-03-13 17:32:02 -07001248 // Have we assigned pointer IDs for this stream
1249 bool mHavePointerIds;
1250
Michael Wright8e812822015-06-22 16:18:21 +01001251 // Is the current stream of direct touch events aborted
1252 bool mCurrentMotionAborted;
1253
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 // The time the primary pointer last went down.
1255 nsecs_t mDownTime;
1256
1257 // The pointer controller, or null if the device is not a pointer.
1258 sp<PointerControllerInterface> mPointerController;
1259
1260 Vector<VirtualKey> mVirtualKeys;
1261
1262 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001263 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001265 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001267 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001269 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 virtual void parseCalibration();
1271 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001272 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001273 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001274 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001275 virtual void resolveExternalStylusPresence();
1276 virtual bool hasStylus() const = 0;
1277 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278
Michael Wright842500e2015-03-13 17:32:02 -07001279 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
1281private:
1282 // The current viewport.
1283 // The components of the viewport are specified in the display's rotated orientation.
1284 DisplayViewport mViewport;
1285
1286 // The surface orientation, width and height set by configureSurface().
1287 // The width and height are derived from the viewport but are specified
1288 // in the natural orientation.
1289 // The surface origin specifies how the surface coordinates should be translated
1290 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001291 int32_t mSurfaceWidth;
1292 int32_t mSurfaceHeight;
1293 int32_t mSurfaceLeft;
1294 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001295
1296 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1297 // the logical coordinate space.
1298 int32_t mPhysicalWidth;
1299 int32_t mPhysicalHeight;
1300 int32_t mPhysicalLeft;
1301 int32_t mPhysicalTop;
1302
1303 // The orientation may be different from the viewport orientation as it specifies
1304 // the rotation of the surface coordinates required to produce the viewport's
1305 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 int32_t mSurfaceOrientation;
1307
1308 // Translation and scaling factors, orientation-independent.
1309 float mXTranslate;
1310 float mXScale;
1311 float mXPrecision;
1312
1313 float mYTranslate;
1314 float mYScale;
1315 float mYPrecision;
1316
1317 float mGeometricScale;
1318
1319 float mPressureScale;
1320
1321 float mSizeScale;
1322
1323 float mOrientationScale;
1324
1325 float mDistanceScale;
1326
1327 bool mHaveTilt;
1328 float mTiltXCenter;
1329 float mTiltXScale;
1330 float mTiltYCenter;
1331 float mTiltYScale;
1332
Michael Wright842500e2015-03-13 17:32:02 -07001333 bool mExternalStylusConnected;
1334
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 // Oriented motion ranges for input device info.
1336 struct OrientedRanges {
1337 InputDeviceInfo::MotionRange x;
1338 InputDeviceInfo::MotionRange y;
1339 InputDeviceInfo::MotionRange pressure;
1340
1341 bool haveSize;
1342 InputDeviceInfo::MotionRange size;
1343
1344 bool haveTouchSize;
1345 InputDeviceInfo::MotionRange touchMajor;
1346 InputDeviceInfo::MotionRange touchMinor;
1347
1348 bool haveToolSize;
1349 InputDeviceInfo::MotionRange toolMajor;
1350 InputDeviceInfo::MotionRange toolMinor;
1351
1352 bool haveOrientation;
1353 InputDeviceInfo::MotionRange orientation;
1354
1355 bool haveDistance;
1356 InputDeviceInfo::MotionRange distance;
1357
1358 bool haveTilt;
1359 InputDeviceInfo::MotionRange tilt;
1360
1361 OrientedRanges() {
1362 clear();
1363 }
1364
1365 void clear() {
1366 haveSize = false;
1367 haveTouchSize = false;
1368 haveToolSize = false;
1369 haveOrientation = false;
1370 haveDistance = false;
1371 haveTilt = false;
1372 }
1373 } mOrientedRanges;
1374
1375 // Oriented dimensions and precision.
1376 float mOrientedXPrecision;
1377 float mOrientedYPrecision;
1378
1379 struct CurrentVirtualKeyState {
1380 bool down;
1381 bool ignored;
1382 nsecs_t downTime;
1383 int32_t keyCode;
1384 int32_t scanCode;
1385 } mCurrentVirtualKey;
1386
1387 // Scale factor for gesture or mouse based pointer movements.
1388 float mPointerXMovementScale;
1389 float mPointerYMovementScale;
1390
1391 // Scale factor for gesture based zooming and other freeform motions.
1392 float mPointerXZoomScale;
1393 float mPointerYZoomScale;
1394
1395 // The maximum swipe width.
1396 float mPointerGestureMaxSwipeWidth;
1397
1398 struct PointerDistanceHeapElement {
1399 uint32_t currentPointerIndex : 8;
1400 uint32_t lastPointerIndex : 8;
1401 uint64_t distance : 48; // squared distance
1402 };
1403
1404 enum PointerUsage {
1405 POINTER_USAGE_NONE,
1406 POINTER_USAGE_GESTURES,
1407 POINTER_USAGE_STYLUS,
1408 POINTER_USAGE_MOUSE,
1409 };
1410 PointerUsage mPointerUsage;
1411
1412 struct PointerGesture {
1413 enum Mode {
1414 // No fingers, button is not pressed.
1415 // Nothing happening.
1416 NEUTRAL,
1417
1418 // No fingers, button is not pressed.
1419 // Tap detected.
1420 // Emits DOWN and UP events at the pointer location.
1421 TAP,
1422
1423 // Exactly one finger dragging following a tap.
1424 // Pointer follows the active finger.
1425 // Emits DOWN, MOVE and UP events at the pointer location.
1426 //
1427 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1428 TAP_DRAG,
1429
1430 // Button is pressed.
1431 // Pointer follows the active finger if there is one. Other fingers are ignored.
1432 // Emits DOWN, MOVE and UP events at the pointer location.
1433 BUTTON_CLICK_OR_DRAG,
1434
1435 // Exactly one finger, button is not pressed.
1436 // Pointer follows the active finger.
1437 // Emits HOVER_MOVE events at the pointer location.
1438 //
1439 // Detect taps when the finger goes up while in HOVER mode.
1440 HOVER,
1441
1442 // Exactly two fingers but neither have moved enough to clearly indicate
1443 // whether a swipe or freeform gesture was intended. We consider the
1444 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1445 // Pointer does not move.
1446 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1447 PRESS,
1448
1449 // Exactly two fingers moving in the same direction, button is not pressed.
1450 // Pointer does not move.
1451 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1452 // follows the midpoint between both fingers.
1453 SWIPE,
1454
1455 // Two or more fingers moving in arbitrary directions, button is not pressed.
1456 // Pointer does not move.
1457 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1458 // each finger individually relative to the initial centroid of the finger.
1459 FREEFORM,
1460
1461 // Waiting for quiet time to end before starting the next gesture.
1462 QUIET,
1463 };
1464
1465 // Time the first finger went down.
1466 nsecs_t firstTouchTime;
1467
1468 // The active pointer id from the raw touch data.
1469 int32_t activeTouchId; // -1 if none
1470
1471 // The active pointer id from the gesture last delivered to the application.
1472 int32_t activeGestureId; // -1 if none
1473
1474 // Pointer coords and ids for the current and previous pointer gesture.
1475 Mode currentGestureMode;
1476 BitSet32 currentGestureIdBits;
1477 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1478 PointerProperties currentGestureProperties[MAX_POINTERS];
1479 PointerCoords currentGestureCoords[MAX_POINTERS];
1480
1481 Mode lastGestureMode;
1482 BitSet32 lastGestureIdBits;
1483 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1484 PointerProperties lastGestureProperties[MAX_POINTERS];
1485 PointerCoords lastGestureCoords[MAX_POINTERS];
1486
1487 // Time the pointer gesture last went down.
1488 nsecs_t downTime;
1489
1490 // Time when the pointer went down for a TAP.
1491 nsecs_t tapDownTime;
1492
1493 // Time when the pointer went up for a TAP.
1494 nsecs_t tapUpTime;
1495
1496 // Location of initial tap.
1497 float tapX, tapY;
1498
1499 // Time we started waiting for quiescence.
1500 nsecs_t quietTime;
1501
1502 // Reference points for multitouch gestures.
1503 float referenceTouchX; // reference touch X/Y coordinates in surface units
1504 float referenceTouchY;
1505 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1506 float referenceGestureY;
1507
1508 // Distance that each pointer has traveled which has not yet been
1509 // subsumed into the reference gesture position.
1510 BitSet32 referenceIdBits;
1511 struct Delta {
1512 float dx, dy;
1513 };
1514 Delta referenceDeltas[MAX_POINTER_ID + 1];
1515
1516 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1517 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1518
1519 // A velocity tracker for determining whether to switch active pointers during drags.
1520 VelocityTracker velocityTracker;
1521
1522 void reset() {
1523 firstTouchTime = LLONG_MIN;
1524 activeTouchId = -1;
1525 activeGestureId = -1;
1526 currentGestureMode = NEUTRAL;
1527 currentGestureIdBits.clear();
1528 lastGestureMode = NEUTRAL;
1529 lastGestureIdBits.clear();
1530 downTime = 0;
1531 velocityTracker.clear();
1532 resetTap();
1533 resetQuietTime();
1534 }
1535
1536 void resetTap() {
1537 tapDownTime = LLONG_MIN;
1538 tapUpTime = LLONG_MIN;
1539 }
1540
1541 void resetQuietTime() {
1542 quietTime = LLONG_MIN;
1543 }
1544 } mPointerGesture;
1545
1546 struct PointerSimple {
1547 PointerCoords currentCoords;
1548 PointerProperties currentProperties;
1549 PointerCoords lastCoords;
1550 PointerProperties lastProperties;
1551
1552 // True if the pointer is down.
1553 bool down;
1554
1555 // True if the pointer is hovering.
1556 bool hovering;
1557
1558 // Time the pointer last went down.
1559 nsecs_t downTime;
1560
1561 void reset() {
1562 currentCoords.clear();
1563 currentProperties.clear();
1564 lastCoords.clear();
1565 lastProperties.clear();
1566 down = false;
1567 hovering = false;
1568 downTime = 0;
1569 }
1570 } mPointerSimple;
1571
1572 // The pointer and scroll velocity controls.
1573 VelocityControl mPointerVelocityControl;
1574 VelocityControl mWheelXVelocityControl;
1575 VelocityControl mWheelYVelocityControl;
1576
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001577 // Latency statistics for touch events
1578 struct LatencyStatistics mStatistics;
1579
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001580 std::optional<DisplayViewport> findViewport();
1581
Michael Wright842500e2015-03-13 17:32:02 -07001582 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001583 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001584
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 void sync(nsecs_t when);
1586
1587 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001588 void processRawTouches(bool timeout);
1589 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1591 int32_t keyEventAction, int32_t keyEventFlags);
1592
1593 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1594 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1595 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001596 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1597 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1598 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001600 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601
1602 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1603 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1604
1605 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1606 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1607 bool preparePointerGestures(nsecs_t when,
1608 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1609 bool isTimeout);
1610
1611 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1612 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1613
1614 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1615 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1616
1617 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1618 bool down, bool hovering);
1619 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1620
Michael Wright842500e2015-03-13 17:32:02 -07001621 bool assignExternalStylusId(const RawState& state, bool timeout);
1622 void applyExternalStylusButtonState(nsecs_t when);
1623 void applyExternalStylusTouchState(nsecs_t when);
1624
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 // Dispatches a motion event.
1626 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1627 // method will take care of setting the index and transmuting the action to DOWN or UP
1628 // it is the first / last pointer to go down / up.
1629 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001630 int32_t action, int32_t actionButton,
1631 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001632 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633 const PointerProperties* properties, const PointerCoords* coords,
1634 const uint32_t* idToIndex, BitSet32 idBits,
1635 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1636
1637 // Updates pointer coords and properties for pointers with specified ids that have moved.
1638 // Returns true if any of them changed.
1639 bool updateMovedPointers(const PointerProperties* inProperties,
1640 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1641 PointerProperties* outProperties, PointerCoords* outCoords,
1642 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1643
1644 bool isPointInsideSurface(int32_t x, int32_t y);
1645 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1646
Michael Wright842500e2015-03-13 17:32:02 -07001647 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001648
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001649 void reportEventForStatistics(nsecs_t evdevTime);
1650
Santos Cordonfa5cf462017-04-05 10:37:00 -07001651 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652};
1653
1654
1655class SingleTouchInputMapper : public TouchInputMapper {
1656public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001657 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658 virtual ~SingleTouchInputMapper();
1659
1660 virtual void reset(nsecs_t when);
1661 virtual void process(const RawEvent* rawEvent);
1662
1663protected:
Michael Wright842500e2015-03-13 17:32:02 -07001664 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 virtual void configureRawPointerAxes();
1666 virtual bool hasStylus() const;
1667
1668private:
1669 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1670};
1671
1672
1673class MultiTouchInputMapper : public TouchInputMapper {
1674public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001675 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676 virtual ~MultiTouchInputMapper();
1677
1678 virtual void reset(nsecs_t when);
1679 virtual void process(const RawEvent* rawEvent);
1680
1681protected:
Michael Wright842500e2015-03-13 17:32:02 -07001682 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683 virtual void configureRawPointerAxes();
1684 virtual bool hasStylus() const;
1685
1686private:
1687 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1688
1689 // Specifies the pointer id bits that are in use, and their associated tracking id.
1690 BitSet32 mPointerIdBits;
1691 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1692};
1693
Michael Wright842500e2015-03-13 17:32:02 -07001694class ExternalStylusInputMapper : public InputMapper {
1695public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001696 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001697 virtual ~ExternalStylusInputMapper() = default;
1698
1699 virtual uint32_t getSources();
1700 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001701 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001702 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1703 virtual void reset(nsecs_t when);
1704 virtual void process(const RawEvent* rawEvent);
1705 virtual void sync(nsecs_t when);
1706
1707private:
1708 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1709 RawAbsoluteAxisInfo mRawPressureAxis;
1710 TouchButtonAccumulator mTouchButtonAccumulator;
1711
1712 StylusState mStylusState;
1713};
1714
Michael Wrightd02c5b62014-02-10 15:10:22 -08001715
1716class JoystickInputMapper : public InputMapper {
1717public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001718 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 virtual ~JoystickInputMapper();
1720
1721 virtual uint32_t getSources();
1722 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001723 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001724 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1725 virtual void reset(nsecs_t when);
1726 virtual void process(const RawEvent* rawEvent);
1727
1728private:
1729 struct Axis {
1730 RawAbsoluteAxisInfo rawAxisInfo;
1731 AxisInfo axisInfo;
1732
1733 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1734
1735 float scale; // scale factor from raw to normalized values
1736 float offset; // offset to add after scaling for normalization
1737 float highScale; // scale factor from raw to normalized values of high split
1738 float highOffset; // offset to add after scaling for normalization of high split
1739
1740 float min; // normalized inclusive minimum
1741 float max; // normalized inclusive maximum
1742 float flat; // normalized flat region size
1743 float fuzz; // normalized error tolerance
1744 float resolution; // normalized resolution in units/mm
1745
1746 float filter; // filter out small variations of this size
1747 float currentValue; // current value
1748 float newValue; // most recent value
1749 float highCurrentValue; // current value of high split
1750 float highNewValue; // most recent value of high split
1751
1752 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1753 bool explicitlyMapped, float scale, float offset,
1754 float highScale, float highOffset,
1755 float min, float max, float flat, float fuzz, float resolution) {
1756 this->rawAxisInfo = rawAxisInfo;
1757 this->axisInfo = axisInfo;
1758 this->explicitlyMapped = explicitlyMapped;
1759 this->scale = scale;
1760 this->offset = offset;
1761 this->highScale = highScale;
1762 this->highOffset = highOffset;
1763 this->min = min;
1764 this->max = max;
1765 this->flat = flat;
1766 this->fuzz = fuzz;
1767 this->resolution = resolution;
1768 this->filter = 0;
1769 resetValue();
1770 }
1771
1772 void resetValue() {
1773 this->currentValue = 0;
1774 this->newValue = 0;
1775 this->highCurrentValue = 0;
1776 this->highNewValue = 0;
1777 }
1778 };
1779
1780 // Axes indexed by raw ABS_* axis index.
1781 KeyedVector<int32_t, Axis> mAxes;
1782
1783 void sync(nsecs_t when, bool force);
1784
1785 bool haveAxis(int32_t axisId);
1786 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1787 bool filterAxes(bool force);
1788
1789 static bool hasValueChangedSignificantly(float filter,
1790 float newValue, float currentValue, float min, float max);
1791 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1792 float newValue, float currentValue, float thresholdValue);
1793
1794 static bool isCenteredAxis(int32_t axis);
1795 static int32_t getCompatAxis(int32_t axis);
1796
1797 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1798 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1799 float value);
1800};
1801
1802} // namespace android
1803
1804#endif // _UI_INPUT_READER_H