blob: 33763b6cdfae662a219b550501e65e45381e6180 [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>
Atif Niyaz83846822019-07-18 15:17:40 -070027#include <input/LatencyStatistics.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <input/VelocityControl.h>
29#include <input/VelocityTracker.h>
30#include <ui/DisplayInfo.h>
Atif Niyaz83846822019-07-18 15:17:40 -070031#include <utils/BitSet.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070032#include <utils/Condition.h>
Atif Niyaz83846822019-07-18 15:17:40 -070033#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070034#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010037#include <optional>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <stddef.h>
39#include <unistd.h>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010040#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Michael Wrightd02c5b62014-02-10 15:10:22 -080042namespace android {
43
44class InputDevice;
45class InputMapper;
46
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Michael Wright842500e2015-03-13 17:32:02 -070048struct StylusState {
49 /* Time the stylus event was received. */
50 nsecs_t when;
51 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
52 float pressure;
53 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
54 uint32_t buttons;
55 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
56 int32_t toolType;
57
58 void copyFrom(const StylusState& other) {
59 when = other.when;
60 pressure = other.pressure;
61 buttons = other.buttons;
62 toolType = other.toolType;
63 }
64
65 void clear() {
66 when = LLONG_MAX;
67 pressure = 0.f;
68 buttons = 0;
69 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
70 }
71};
72
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
74/* Internal interface used by individual input devices to access global input device state
75 * and parameters maintained by the input reader.
76 */
77class InputReaderContext {
78public:
79 InputReaderContext() { }
80 virtual ~InputReaderContext() { }
81
82 virtual void updateGlobalMetaState() = 0;
83 virtual int32_t getGlobalMetaState() = 0;
84
85 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
86 virtual bool shouldDropVirtualKey(nsecs_t now,
87 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
88
89 virtual void fadePointer() = 0;
90
91 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
92 virtual int32_t bumpGeneration() = 0;
93
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080094 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) = 0;
Michael Wrightb85401d2015-04-17 18:35:15 +010095 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -070096
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 virtual InputReaderPolicyInterface* getPolicy() = 0;
98 virtual InputListenerInterface* getListener() = 0;
99 virtual EventHubInterface* getEventHub() = 0;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800100
101 virtual uint32_t getNextSequenceNum() = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102};
103
104
105/* The input reader reads raw event data from the event hub and processes it into input events
106 * that it sends to the input listener. Some functions of the input reader, such as early
107 * event filtering in low power states, are controlled by a separate policy object.
108 *
109 * The InputReader owns a collection of InputMappers. Most of the work it does happens
110 * on the input reader thread but the InputReader can receive queries from other system
111 * components running on arbitrary threads. To keep things manageable, the InputReader
112 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
113 * EventHub or the InputReaderPolicy but it is never held while calling into the
114 * InputListener.
115 */
116class InputReader : public InputReaderInterface {
117public:
118 InputReader(const sp<EventHubInterface>& eventHub,
119 const sp<InputReaderPolicyInterface>& policy,
120 const sp<InputListenerInterface>& listener);
121 virtual ~InputReader();
122
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800123 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124 virtual void monitor();
125
126 virtual void loopOnce();
127
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800128 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700130 virtual bool isInputDeviceEnabled(int32_t deviceId);
131
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
133 int32_t scanCode);
134 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
135 int32_t keyCode);
136 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
137 int32_t sw);
138
Andrii Kulian763a3a42016-03-08 10:46:16 -0800139 virtual void toggleCapsLockState(int32_t deviceId);
140
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
142 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
143
144 virtual void requestRefreshConfiguration(uint32_t changes);
145
146 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
147 ssize_t repeat, int32_t token);
148 virtual void cancelVibrate(int32_t deviceId, int32_t token);
149
Arthur Hungc23540e2018-11-29 20:42:11 +0800150 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151protected:
152 // These members are protected so they can be instrumented by test cases.
153 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
154 const InputDeviceIdentifier& identifier, uint32_t classes);
155
156 class ContextImpl : public InputReaderContext {
157 InputReader* mReader;
158
159 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700160 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161
162 virtual void updateGlobalMetaState();
163 virtual int32_t getGlobalMetaState();
164 virtual void disableVirtualKeysUntil(nsecs_t time);
165 virtual bool shouldDropVirtualKey(nsecs_t now,
166 InputDevice* device, int32_t keyCode, int32_t scanCode);
167 virtual void fadePointer();
168 virtual void requestTimeoutAtTime(nsecs_t when);
169 virtual int32_t bumpGeneration();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800170 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700171 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 virtual InputReaderPolicyInterface* getPolicy();
173 virtual InputListenerInterface* getListener();
174 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800175 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 } mContext;
177
178 friend class ContextImpl;
179
180private:
181 Mutex mLock;
182
183 Condition mReaderIsAliveCondition;
184
185 sp<EventHubInterface> mEventHub;
186 sp<InputReaderPolicyInterface> mPolicy;
187 sp<QueuedInputListener> mQueuedListener;
188
189 InputReaderConfiguration mConfig;
190
Prabir Pradhan42611e02018-11-27 14:04:02 -0800191 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
192 uint32_t mNextSequenceNum;
193
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194 // The event queue.
195 static const int EVENT_BUFFER_SIZE = 256;
196 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
197
198 KeyedVector<int32_t, InputDevice*> mDevices;
199
200 // low-level input event decoding and device management
201 void processEventsLocked(const RawEvent* rawEvents, size_t count);
202
203 void addDeviceLocked(nsecs_t when, int32_t deviceId);
204 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
205 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
206 void timeoutExpiredLocked(nsecs_t when);
207
208 void handleConfigurationChangedLocked(nsecs_t when);
209
210 int32_t mGlobalMetaState;
211 void updateGlobalMetaStateLocked();
212 int32_t getGlobalMetaStateLocked();
213
Michael Wright842500e2015-03-13 17:32:02 -0700214 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800215 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700216 void dispatchExternalStylusState(const StylusState& state);
217
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 void fadePointerLocked();
219
220 int32_t mGeneration;
221 int32_t bumpGenerationLocked();
222
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800223 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224
225 nsecs_t mDisableVirtualKeysTimeout;
226 void disableVirtualKeysUntilLocked(nsecs_t time);
227 bool shouldDropVirtualKeyLocked(nsecs_t now,
228 InputDevice* device, int32_t keyCode, int32_t scanCode);
229
230 nsecs_t mNextTimeout;
231 void requestTimeoutAtTimeLocked(nsecs_t when);
232
233 uint32_t mConfigurationChangesToRefresh;
234 void refreshConfigurationLocked(uint32_t changes);
235
236 // state queries
237 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
238 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
239 GetStateFunc getStateFunc);
240 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
241 const int32_t* keyCodes, uint8_t* outFlags);
242};
243
244
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245/* Represents the state of a single input device. */
246class InputDevice {
247public:
248 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
249 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
250 ~InputDevice();
251
252 inline InputReaderContext* getContext() { return mContext; }
253 inline int32_t getId() const { return mId; }
254 inline int32_t getControllerNumber() const { return mControllerNumber; }
255 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100256 inline const std::string getName() const { return mIdentifier.name; }
257 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 inline uint32_t getClasses() const { return mClasses; }
259 inline uint32_t getSources() const { return mSources; }
260
261 inline bool isExternal() { return mIsExternal; }
262 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700263 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
264 return mAssociatedDisplayPort;
265 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800266
Tim Kilbourn063ff532015-04-08 10:26:18 -0700267 inline void setMic(bool hasMic) { mHasMic = hasMic; }
268 inline bool hasMic() const { return mHasMic; }
269
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800270 inline bool isIgnored() { return mMappers.empty(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700272 bool isEnabled();
273 void setEnabled(bool enabled, nsecs_t when);
274
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800275 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 void addMapper(InputMapper* mapper);
277 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
278 void reset(nsecs_t when);
279 void process(const RawEvent* rawEvents, size_t count);
280 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700281 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282
283 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
284 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
285 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
286 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
287 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
288 const int32_t* keyCodes, uint8_t* outFlags);
289 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
290 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800291 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292
293 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800294 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295
296 void fadePointer();
297
298 void bumpGeneration();
299
300 void notifyReset(nsecs_t when);
301
302 inline const PropertyMap& getConfiguration() { return mConfiguration; }
303 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
304
305 bool hasKey(int32_t code) {
306 return getEventHub()->hasScanCode(mId, code);
307 }
308
309 bool hasAbsoluteAxis(int32_t code) {
310 RawAbsoluteAxisInfo info;
311 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
312 return info.valid;
313 }
314
315 bool isKeyPressed(int32_t code) {
316 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
317 }
318
319 int32_t getAbsoluteAxisValue(int32_t code) {
320 int32_t value;
321 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
322 return value;
323 }
324
Arthur Hungc23540e2018-11-29 20:42:11 +0800325 std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326private:
327 InputReaderContext* mContext;
328 int32_t mId;
329 int32_t mGeneration;
330 int32_t mControllerNumber;
331 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100332 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 uint32_t mClasses;
334
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800335 std::vector<InputMapper*> mMappers;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336
337 uint32_t mSources;
338 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700339 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700340 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 bool mDropUntilNextSync;
342
343 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
344 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
345
346 PropertyMap mConfiguration;
347};
348
349
350/* Keeps track of the state of mouse or touch pad buttons. */
351class CursorButtonAccumulator {
352public:
353 CursorButtonAccumulator();
354 void reset(InputDevice* device);
355
356 void process(const RawEvent* rawEvent);
357
358 uint32_t getButtonState() const;
359
360private:
361 bool mBtnLeft;
362 bool mBtnRight;
363 bool mBtnMiddle;
364 bool mBtnBack;
365 bool mBtnSide;
366 bool mBtnForward;
367 bool mBtnExtra;
368 bool mBtnTask;
369
370 void clearButtons();
371};
372
373
374/* Keeps track of cursor movements. */
375
376class CursorMotionAccumulator {
377public:
378 CursorMotionAccumulator();
379 void reset(InputDevice* device);
380
381 void process(const RawEvent* rawEvent);
382 void finishSync();
383
384 inline int32_t getRelativeX() const { return mRelX; }
385 inline int32_t getRelativeY() const { return mRelY; }
386
387private:
388 int32_t mRelX;
389 int32_t mRelY;
390
391 void clearRelativeAxes();
392};
393
394
395/* Keeps track of cursor scrolling motions. */
396
397class CursorScrollAccumulator {
398public:
399 CursorScrollAccumulator();
400 void configure(InputDevice* device);
401 void reset(InputDevice* device);
402
403 void process(const RawEvent* rawEvent);
404 void finishSync();
405
406 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
407 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
408
409 inline int32_t getRelativeX() const { return mRelX; }
410 inline int32_t getRelativeY() const { return mRelY; }
411 inline int32_t getRelativeVWheel() const { return mRelWheel; }
412 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
413
414private:
415 bool mHaveRelWheel;
416 bool mHaveRelHWheel;
417
418 int32_t mRelX;
419 int32_t mRelY;
420 int32_t mRelWheel;
421 int32_t mRelHWheel;
422
423 void clearRelativeAxes();
424};
425
426
427/* Keeps track of the state of touch, stylus and tool buttons. */
428class TouchButtonAccumulator {
429public:
430 TouchButtonAccumulator();
431 void configure(InputDevice* device);
432 void reset(InputDevice* device);
433
434 void process(const RawEvent* rawEvent);
435
436 uint32_t getButtonState() const;
437 int32_t getToolType() const;
438 bool isToolActive() const;
439 bool isHovering() const;
440 bool hasStylus() const;
441
442private:
443 bool mHaveBtnTouch;
444 bool mHaveStylus;
445
446 bool mBtnTouch;
447 bool mBtnStylus;
448 bool mBtnStylus2;
449 bool mBtnToolFinger;
450 bool mBtnToolPen;
451 bool mBtnToolRubber;
452 bool mBtnToolBrush;
453 bool mBtnToolPencil;
454 bool mBtnToolAirbrush;
455 bool mBtnToolMouse;
456 bool mBtnToolLens;
457 bool mBtnToolDoubleTap;
458 bool mBtnToolTripleTap;
459 bool mBtnToolQuadTap;
460
461 void clearButtons();
462};
463
464
465/* Raw axis information from the driver. */
466struct RawPointerAxes {
467 RawAbsoluteAxisInfo x;
468 RawAbsoluteAxisInfo y;
469 RawAbsoluteAxisInfo pressure;
470 RawAbsoluteAxisInfo touchMajor;
471 RawAbsoluteAxisInfo touchMinor;
472 RawAbsoluteAxisInfo toolMajor;
473 RawAbsoluteAxisInfo toolMinor;
474 RawAbsoluteAxisInfo orientation;
475 RawAbsoluteAxisInfo distance;
476 RawAbsoluteAxisInfo tiltX;
477 RawAbsoluteAxisInfo tiltY;
478 RawAbsoluteAxisInfo trackingId;
479 RawAbsoluteAxisInfo slot;
480
481 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800482 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
483 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 void clear();
485};
486
487
488/* Raw data for a collection of pointers including a pointer id mapping table. */
489struct RawPointerData {
490 struct Pointer {
491 uint32_t id;
492 int32_t x;
493 int32_t y;
494 int32_t pressure;
495 int32_t touchMajor;
496 int32_t touchMinor;
497 int32_t toolMajor;
498 int32_t toolMinor;
499 int32_t orientation;
500 int32_t distance;
501 int32_t tiltX;
502 int32_t tiltY;
503 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
504 bool isHovering;
505 };
506
507 uint32_t pointerCount;
508 Pointer pointers[MAX_POINTERS];
509 BitSet32 hoveringIdBits, touchingIdBits;
510 uint32_t idToIndex[MAX_POINTER_ID + 1];
511
512 RawPointerData();
513 void clear();
514 void copyFrom(const RawPointerData& other);
515 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
516
517 inline void markIdBit(uint32_t id, bool isHovering) {
518 if (isHovering) {
519 hoveringIdBits.markBit(id);
520 } else {
521 touchingIdBits.markBit(id);
522 }
523 }
524
525 inline void clearIdBits() {
526 hoveringIdBits.clear();
527 touchingIdBits.clear();
528 }
529
530 inline const Pointer& pointerForId(uint32_t id) const {
531 return pointers[idToIndex[id]];
532 }
533
534 inline bool isHovering(uint32_t pointerIndex) {
535 return pointers[pointerIndex].isHovering;
536 }
537};
538
539
540/* Cooked data for a collection of pointers including a pointer id mapping table. */
541struct CookedPointerData {
542 uint32_t pointerCount;
543 PointerProperties pointerProperties[MAX_POINTERS];
544 PointerCoords pointerCoords[MAX_POINTERS];
545 BitSet32 hoveringIdBits, touchingIdBits;
546 uint32_t idToIndex[MAX_POINTER_ID + 1];
547
548 CookedPointerData();
549 void clear();
550 void copyFrom(const CookedPointerData& other);
551
552 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
553 return pointerCoords[idToIndex[id]];
554 }
555
Michael Wright842500e2015-03-13 17:32:02 -0700556 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
557 return pointerCoords[idToIndex[id]];
558 }
559
560 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
561 return pointerProperties[idToIndex[id]];
562 }
563
Michael Wright53dca3a2015-04-23 17:39:53 +0100564 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
566 }
Michael Wright842500e2015-03-13 17:32:02 -0700567
Michael Wright53dca3a2015-04-23 17:39:53 +0100568 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700569 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
570 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571};
572
573
574/* Keeps track of the state of single-touch protocol. */
575class SingleTouchMotionAccumulator {
576public:
577 SingleTouchMotionAccumulator();
578
579 void process(const RawEvent* rawEvent);
580 void reset(InputDevice* device);
581
582 inline int32_t getAbsoluteX() const { return mAbsX; }
583 inline int32_t getAbsoluteY() const { return mAbsY; }
584 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
585 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
586 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
587 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
588 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
589
590private:
591 int32_t mAbsX;
592 int32_t mAbsY;
593 int32_t mAbsPressure;
594 int32_t mAbsToolWidth;
595 int32_t mAbsDistance;
596 int32_t mAbsTiltX;
597 int32_t mAbsTiltY;
598
599 void clearAbsoluteAxes();
600};
601
602
603/* Keeps track of the state of multi-touch protocol. */
604class MultiTouchMotionAccumulator {
605public:
606 class Slot {
607 public:
608 inline bool isInUse() const { return mInUse; }
609 inline int32_t getX() const { return mAbsMTPositionX; }
610 inline int32_t getY() const { return mAbsMTPositionY; }
611 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
612 inline int32_t getTouchMinor() const {
613 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
614 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
615 inline int32_t getToolMinor() const {
616 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
617 inline int32_t getOrientation() const { return mAbsMTOrientation; }
618 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
619 inline int32_t getPressure() const { return mAbsMTPressure; }
620 inline int32_t getDistance() const { return mAbsMTDistance; }
621 inline int32_t getToolType() const;
622
623 private:
624 friend class MultiTouchMotionAccumulator;
625
626 bool mInUse;
627 bool mHaveAbsMTTouchMinor;
628 bool mHaveAbsMTWidthMinor;
629 bool mHaveAbsMTToolType;
630
631 int32_t mAbsMTPositionX;
632 int32_t mAbsMTPositionY;
633 int32_t mAbsMTTouchMajor;
634 int32_t mAbsMTTouchMinor;
635 int32_t mAbsMTWidthMajor;
636 int32_t mAbsMTWidthMinor;
637 int32_t mAbsMTOrientation;
638 int32_t mAbsMTTrackingId;
639 int32_t mAbsMTPressure;
640 int32_t mAbsMTDistance;
641 int32_t mAbsMTToolType;
642
643 Slot();
644 void clear();
645 };
646
647 MultiTouchMotionAccumulator();
648 ~MultiTouchMotionAccumulator();
649
650 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
651 void reset(InputDevice* device);
652 void process(const RawEvent* rawEvent);
653 void finishSync();
654 bool hasStylus() const;
655
656 inline size_t getSlotCount() const { return mSlotCount; }
657 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
658
659private:
660 int32_t mCurrentSlot;
661 Slot* mSlots;
662 size_t mSlotCount;
663 bool mUsingSlotsProtocol;
664 bool mHaveStylus;
665
666 void clearSlots(int32_t initialSlot);
667};
668
669
670/* An input mapper transforms raw input events into cooked event data.
671 * A single input device can have multiple associated input mappers in order to interpret
672 * different classes of events.
673 *
674 * InputMapper lifecycle:
675 * - create
676 * - configure with 0 changes
677 * - reset
678 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
679 * - reset
680 * - destroy
681 */
682class InputMapper {
683public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700684 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 virtual ~InputMapper();
686
687 inline InputDevice* getDevice() { return mDevice; }
688 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100689 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800690 inline InputReaderContext* getContext() { return mContext; }
691 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
692 inline InputListenerInterface* getListener() { return mContext->getListener(); }
693 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
694
695 virtual uint32_t getSources() = 0;
696 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800697 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
699 virtual void reset(nsecs_t when);
700 virtual void process(const RawEvent* rawEvent) = 0;
701 virtual void timeoutExpired(nsecs_t when);
702
703 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
704 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
705 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
706 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
707 const int32_t* keyCodes, uint8_t* outFlags);
708 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
709 int32_t token);
710 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800711 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800712
713 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800714 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
Michael Wright842500e2015-03-13 17:32:02 -0700716 virtual void updateExternalStylusState(const StylusState& state);
717
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 virtual void fadePointer();
Arthur Hungc23540e2018-11-29 20:42:11 +0800719 virtual std::optional<int32_t> getAssociatedDisplay() {
720 return std::nullopt;
721 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722protected:
723 InputDevice* mDevice;
724 InputReaderContext* mContext;
725
726 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
727 void bumpGeneration();
728
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800729 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800731 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732};
733
734
735class SwitchInputMapper : public InputMapper {
736public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700737 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 virtual ~SwitchInputMapper();
739
740 virtual uint32_t getSources();
741 virtual void process(const RawEvent* rawEvent);
742
743 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800744 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745
746private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700747 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 uint32_t mUpdatedSwitchMask;
749
750 void processSwitch(int32_t switchCode, int32_t switchValue);
751 void sync(nsecs_t when);
752};
753
754
755class VibratorInputMapper : public InputMapper {
756public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700757 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 virtual ~VibratorInputMapper();
759
760 virtual uint32_t getSources();
761 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
762 virtual void process(const RawEvent* rawEvent);
763
764 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
765 int32_t token);
766 virtual void cancelVibrate(int32_t token);
767 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800768 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769
770private:
771 bool mVibrating;
772 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
773 size_t mPatternSize;
774 ssize_t mRepeat;
775 int32_t mToken;
776 ssize_t mIndex;
777 nsecs_t mNextStepTime;
778
779 void nextStep();
780 void stopVibrating();
781};
782
783
784class KeyboardInputMapper : public InputMapper {
785public:
786 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
787 virtual ~KeyboardInputMapper();
788
789 virtual uint32_t getSources();
790 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800791 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
793 virtual void reset(nsecs_t when);
794 virtual void process(const RawEvent* rawEvent);
795
796 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
797 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
798 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
799 const int32_t* keyCodes, uint8_t* outFlags);
800
801 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800802 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
804private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100805 // The current viewport.
806 std::optional<DisplayViewport> mViewport;
807
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 struct KeyDown {
809 int32_t keyCode;
810 int32_t scanCode;
811 };
812
813 uint32_t mSource;
814 int32_t mKeyboardType;
815
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800816 std::vector<KeyDown> mKeyDowns; // keys that are down
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 int32_t mMetaState;
818 nsecs_t mDownTime; // time of most recent key down
819
820 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
821
822 struct LedState {
823 bool avail; // led is available
824 bool on; // we think the led is currently on
825 };
826 LedState mCapsLockLedState;
827 LedState mNumLockLedState;
828 LedState mScrollLockLedState;
829
830 // Immutable configuration parameters.
831 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700833 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 } mParameters;
835
836 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800837 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100839 int32_t getOrientation();
840 int32_t getDisplayId();
841
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100843 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700845 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846
Andrii Kulian763a3a42016-03-08 10:46:16 -0800847 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
848
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 ssize_t findKeyDown(int32_t scanCode);
850
851 void resetLedState();
852 void initializeLedState(LedState& ledState, int32_t led);
853 void updateLedState(bool reset);
854 void updateLedStateForModifier(LedState& ledState, int32_t led,
855 int32_t modifier, bool reset);
856};
857
858
859class CursorInputMapper : public InputMapper {
860public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700861 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862 virtual ~CursorInputMapper();
863
864 virtual uint32_t getSources();
865 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800866 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
868 virtual void reset(nsecs_t when);
869 virtual void process(const RawEvent* rawEvent);
870
871 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
872
873 virtual void fadePointer();
874
Arthur Hungc23540e2018-11-29 20:42:11 +0800875 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876private:
877 // Amount that trackball needs to move in order to generate a key event.
878 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
879
880 // Immutable configuration parameters.
881 struct Parameters {
882 enum Mode {
883 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800884 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 MODE_NAVIGATION,
886 };
887
888 Mode mode;
889 bool hasAssociatedDisplay;
890 bool orientationAware;
891 } mParameters;
892
893 CursorButtonAccumulator mCursorButtonAccumulator;
894 CursorMotionAccumulator mCursorMotionAccumulator;
895 CursorScrollAccumulator mCursorScrollAccumulator;
896
897 int32_t mSource;
898 float mXScale;
899 float mYScale;
900 float mXPrecision;
901 float mYPrecision;
902
903 float mVWheelScale;
904 float mHWheelScale;
905
906 // Velocity controls for mouse pointer and wheel movements.
907 // The controls for X and Y wheel movements are separate to keep them decoupled.
908 VelocityControl mPointerVelocityControl;
909 VelocityControl mWheelXVelocityControl;
910 VelocityControl mWheelYVelocityControl;
911
912 int32_t mOrientation;
913
914 sp<PointerControllerInterface> mPointerController;
915
916 int32_t mButtonState;
917 nsecs_t mDownTime;
918
919 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800920 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921
922 void sync(nsecs_t when);
923};
924
925
Prashant Malani1941ff52015-08-11 18:29:28 -0700926class RotaryEncoderInputMapper : public InputMapper {
927public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700928 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700929 virtual ~RotaryEncoderInputMapper();
930
931 virtual uint32_t getSources();
932 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800933 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700934 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
935 virtual void reset(nsecs_t when);
936 virtual void process(const RawEvent* rawEvent);
937
938private:
939 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
940
941 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -0800942 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +0100943 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -0700944
945 void sync(nsecs_t when);
946};
947
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948class TouchInputMapper : public InputMapper {
949public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700950 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 virtual ~TouchInputMapper();
952
953 virtual uint32_t getSources();
954 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800955 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
957 virtual void reset(nsecs_t when);
958 virtual void process(const RawEvent* rawEvent);
959
960 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
961 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
962 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
963 const int32_t* keyCodes, uint8_t* outFlags);
964
965 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -0800966 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700968 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hungc23540e2018-11-29 20:42:11 +0800969 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970protected:
971 CursorButtonAccumulator mCursorButtonAccumulator;
972 CursorScrollAccumulator mCursorScrollAccumulator;
973 TouchButtonAccumulator mTouchButtonAccumulator;
974
975 struct VirtualKey {
976 int32_t keyCode;
977 int32_t scanCode;
978 uint32_t flags;
979
980 // computed hit box, specified in touch screen coords based on known display size
981 int32_t hitLeft;
982 int32_t hitTop;
983 int32_t hitRight;
984 int32_t hitBottom;
985
986 inline bool isHit(int32_t x, int32_t y) const {
987 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
988 }
989 };
990
991 // Input sources and device mode.
992 uint32_t mSource;
993
994 enum DeviceMode {
995 DEVICE_MODE_DISABLED, // input is disabled
996 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
997 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
998 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
999 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1000 };
1001 DeviceMode mDeviceMode;
1002
1003 // The reader's configuration.
1004 InputReaderConfiguration mConfig;
1005
1006 // Immutable configuration parameters.
1007 struct Parameters {
1008 enum DeviceType {
1009 DEVICE_TYPE_TOUCH_SCREEN,
1010 DEVICE_TYPE_TOUCH_PAD,
1011 DEVICE_TYPE_TOUCH_NAVIGATION,
1012 DEVICE_TYPE_POINTER,
1013 };
1014
1015 DeviceType deviceType;
1016 bool hasAssociatedDisplay;
1017 bool associatedDisplayIsExternal;
1018 bool orientationAware;
1019 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001020 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021
1022 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001023 GESTURE_MODE_SINGLE_TOUCH,
1024 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 };
1026 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001027
1028 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 } mParameters;
1030
1031 // Immutable calibration parameters in parsed form.
1032 struct Calibration {
1033 // Size
1034 enum SizeCalibration {
1035 SIZE_CALIBRATION_DEFAULT,
1036 SIZE_CALIBRATION_NONE,
1037 SIZE_CALIBRATION_GEOMETRIC,
1038 SIZE_CALIBRATION_DIAMETER,
1039 SIZE_CALIBRATION_BOX,
1040 SIZE_CALIBRATION_AREA,
1041 };
1042
1043 SizeCalibration sizeCalibration;
1044
1045 bool haveSizeScale;
1046 float sizeScale;
1047 bool haveSizeBias;
1048 float sizeBias;
1049 bool haveSizeIsSummed;
1050 bool sizeIsSummed;
1051
1052 // Pressure
1053 enum PressureCalibration {
1054 PRESSURE_CALIBRATION_DEFAULT,
1055 PRESSURE_CALIBRATION_NONE,
1056 PRESSURE_CALIBRATION_PHYSICAL,
1057 PRESSURE_CALIBRATION_AMPLITUDE,
1058 };
1059
1060 PressureCalibration pressureCalibration;
1061 bool havePressureScale;
1062 float pressureScale;
1063
1064 // Orientation
1065 enum OrientationCalibration {
1066 ORIENTATION_CALIBRATION_DEFAULT,
1067 ORIENTATION_CALIBRATION_NONE,
1068 ORIENTATION_CALIBRATION_INTERPOLATED,
1069 ORIENTATION_CALIBRATION_VECTOR,
1070 };
1071
1072 OrientationCalibration orientationCalibration;
1073
1074 // Distance
1075 enum DistanceCalibration {
1076 DISTANCE_CALIBRATION_DEFAULT,
1077 DISTANCE_CALIBRATION_NONE,
1078 DISTANCE_CALIBRATION_SCALED,
1079 };
1080
1081 DistanceCalibration distanceCalibration;
1082 bool haveDistanceScale;
1083 float distanceScale;
1084
1085 enum CoverageCalibration {
1086 COVERAGE_CALIBRATION_DEFAULT,
1087 COVERAGE_CALIBRATION_NONE,
1088 COVERAGE_CALIBRATION_BOX,
1089 };
1090
1091 CoverageCalibration coverageCalibration;
1092
1093 inline void applySizeScaleAndBias(float* outSize) const {
1094 if (haveSizeScale) {
1095 *outSize *= sizeScale;
1096 }
1097 if (haveSizeBias) {
1098 *outSize += sizeBias;
1099 }
1100 if (*outSize < 0) {
1101 *outSize = 0;
1102 }
1103 }
1104 } mCalibration;
1105
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001106 // Affine location transformation/calibration
1107 struct TouchAffineTransformation mAffineTransform;
1108
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 RawPointerAxes mRawPointerAxes;
1110
Michael Wright842500e2015-03-13 17:32:02 -07001111 struct RawState {
1112 nsecs_t when;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113
Michael Wright842500e2015-03-13 17:32:02 -07001114 // Raw pointer sample data.
1115 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116
Michael Wright842500e2015-03-13 17:32:02 -07001117 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
Michael Wright842500e2015-03-13 17:32:02 -07001119 // Scroll state.
1120 int32_t rawVScroll;
1121 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122
Michael Wright842500e2015-03-13 17:32:02 -07001123 void copyFrom(const RawState& other) {
1124 when = other.when;
1125 rawPointerData.copyFrom(other.rawPointerData);
1126 buttonState = other.buttonState;
1127 rawVScroll = other.rawVScroll;
1128 rawHScroll = other.rawHScroll;
1129 }
1130
1131 void clear() {
1132 when = 0;
1133 rawPointerData.clear();
1134 buttonState = 0;
1135 rawVScroll = 0;
1136 rawHScroll = 0;
1137 }
1138 };
1139
1140 struct CookedState {
1141 // Cooked pointer sample data.
1142 CookedPointerData cookedPointerData;
1143
1144 // Id bits used to differentiate fingers, stylus and mouse tools.
1145 BitSet32 fingerIdBits;
1146 BitSet32 stylusIdBits;
1147 BitSet32 mouseIdBits;
1148
Michael Wright7b159c92015-05-14 14:48:03 +01001149 int32_t buttonState;
1150
Michael Wright842500e2015-03-13 17:32:02 -07001151 void copyFrom(const CookedState& other) {
1152 cookedPointerData.copyFrom(other.cookedPointerData);
1153 fingerIdBits = other.fingerIdBits;
1154 stylusIdBits = other.stylusIdBits;
1155 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001156 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001157 }
1158
1159 void clear() {
1160 cookedPointerData.clear();
1161 fingerIdBits.clear();
1162 stylusIdBits.clear();
1163 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001164 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001165 }
1166 };
1167
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001168 std::vector<RawState> mRawStatesPending;
Michael Wright842500e2015-03-13 17:32:02 -07001169 RawState mCurrentRawState;
1170 CookedState mCurrentCookedState;
1171 RawState mLastRawState;
1172 CookedState mLastCookedState;
1173
1174 // State provided by an external stylus
1175 StylusState mExternalStylusState;
1176 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001177 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001178 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179
1180 // True if we sent a HOVER_ENTER event.
1181 bool mSentHoverEnter;
1182
Michael Wright842500e2015-03-13 17:32:02 -07001183 // Have we assigned pointer IDs for this stream
1184 bool mHavePointerIds;
1185
Michael Wright8e812822015-06-22 16:18:21 +01001186 // Is the current stream of direct touch events aborted
1187 bool mCurrentMotionAborted;
1188
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 // The time the primary pointer last went down.
1190 nsecs_t mDownTime;
1191
1192 // The pointer controller, or null if the device is not a pointer.
1193 sp<PointerControllerInterface> mPointerController;
1194
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001195 std::vector<VirtualKey> mVirtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196
1197 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001198 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001200 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001202 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001204 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 virtual void parseCalibration();
1206 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001207 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001208 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001209 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001210 virtual void resolveExternalStylusPresence();
1211 virtual bool hasStylus() const = 0;
1212 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
Michael Wright842500e2015-03-13 17:32:02 -07001214 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215
1216private:
1217 // The current viewport.
1218 // The components of the viewport are specified in the display's rotated orientation.
1219 DisplayViewport mViewport;
1220
1221 // The surface orientation, width and height set by configureSurface().
1222 // The width and height are derived from the viewport but are specified
1223 // in the natural orientation.
1224 // The surface origin specifies how the surface coordinates should be translated
1225 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 int32_t mSurfaceWidth;
1227 int32_t mSurfaceHeight;
1228 int32_t mSurfaceLeft;
1229 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001230
1231 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1232 // the logical coordinate space.
1233 int32_t mPhysicalWidth;
1234 int32_t mPhysicalHeight;
1235 int32_t mPhysicalLeft;
1236 int32_t mPhysicalTop;
1237
1238 // The orientation may be different from the viewport orientation as it specifies
1239 // the rotation of the surface coordinates required to produce the viewport's
1240 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 int32_t mSurfaceOrientation;
1242
1243 // Translation and scaling factors, orientation-independent.
1244 float mXTranslate;
1245 float mXScale;
1246 float mXPrecision;
1247
1248 float mYTranslate;
1249 float mYScale;
1250 float mYPrecision;
1251
1252 float mGeometricScale;
1253
1254 float mPressureScale;
1255
1256 float mSizeScale;
1257
1258 float mOrientationScale;
1259
1260 float mDistanceScale;
1261
1262 bool mHaveTilt;
1263 float mTiltXCenter;
1264 float mTiltXScale;
1265 float mTiltYCenter;
1266 float mTiltYScale;
1267
Michael Wright842500e2015-03-13 17:32:02 -07001268 bool mExternalStylusConnected;
1269
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 // Oriented motion ranges for input device info.
1271 struct OrientedRanges {
1272 InputDeviceInfo::MotionRange x;
1273 InputDeviceInfo::MotionRange y;
1274 InputDeviceInfo::MotionRange pressure;
1275
1276 bool haveSize;
1277 InputDeviceInfo::MotionRange size;
1278
1279 bool haveTouchSize;
1280 InputDeviceInfo::MotionRange touchMajor;
1281 InputDeviceInfo::MotionRange touchMinor;
1282
1283 bool haveToolSize;
1284 InputDeviceInfo::MotionRange toolMajor;
1285 InputDeviceInfo::MotionRange toolMinor;
1286
1287 bool haveOrientation;
1288 InputDeviceInfo::MotionRange orientation;
1289
1290 bool haveDistance;
1291 InputDeviceInfo::MotionRange distance;
1292
1293 bool haveTilt;
1294 InputDeviceInfo::MotionRange tilt;
1295
1296 OrientedRanges() {
1297 clear();
1298 }
1299
1300 void clear() {
1301 haveSize = false;
1302 haveTouchSize = false;
1303 haveToolSize = false;
1304 haveOrientation = false;
1305 haveDistance = false;
1306 haveTilt = false;
1307 }
1308 } mOrientedRanges;
1309
1310 // Oriented dimensions and precision.
1311 float mOrientedXPrecision;
1312 float mOrientedYPrecision;
1313
1314 struct CurrentVirtualKeyState {
1315 bool down;
1316 bool ignored;
1317 nsecs_t downTime;
1318 int32_t keyCode;
1319 int32_t scanCode;
1320 } mCurrentVirtualKey;
1321
1322 // Scale factor for gesture or mouse based pointer movements.
1323 float mPointerXMovementScale;
1324 float mPointerYMovementScale;
1325
1326 // Scale factor for gesture based zooming and other freeform motions.
1327 float mPointerXZoomScale;
1328 float mPointerYZoomScale;
1329
1330 // The maximum swipe width.
1331 float mPointerGestureMaxSwipeWidth;
1332
1333 struct PointerDistanceHeapElement {
1334 uint32_t currentPointerIndex : 8;
1335 uint32_t lastPointerIndex : 8;
1336 uint64_t distance : 48; // squared distance
1337 };
1338
1339 enum PointerUsage {
1340 POINTER_USAGE_NONE,
1341 POINTER_USAGE_GESTURES,
1342 POINTER_USAGE_STYLUS,
1343 POINTER_USAGE_MOUSE,
1344 };
1345 PointerUsage mPointerUsage;
1346
1347 struct PointerGesture {
1348 enum Mode {
1349 // No fingers, button is not pressed.
1350 // Nothing happening.
1351 NEUTRAL,
1352
1353 // No fingers, button is not pressed.
1354 // Tap detected.
1355 // Emits DOWN and UP events at the pointer location.
1356 TAP,
1357
1358 // Exactly one finger dragging following a tap.
1359 // Pointer follows the active finger.
1360 // Emits DOWN, MOVE and UP events at the pointer location.
1361 //
1362 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1363 TAP_DRAG,
1364
1365 // Button is pressed.
1366 // Pointer follows the active finger if there is one. Other fingers are ignored.
1367 // Emits DOWN, MOVE and UP events at the pointer location.
1368 BUTTON_CLICK_OR_DRAG,
1369
1370 // Exactly one finger, button is not pressed.
1371 // Pointer follows the active finger.
1372 // Emits HOVER_MOVE events at the pointer location.
1373 //
1374 // Detect taps when the finger goes up while in HOVER mode.
1375 HOVER,
1376
1377 // Exactly two fingers but neither have moved enough to clearly indicate
1378 // whether a swipe or freeform gesture was intended. We consider the
1379 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1380 // Pointer does not move.
1381 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1382 PRESS,
1383
1384 // Exactly two fingers moving in the same direction, button is not pressed.
1385 // Pointer does not move.
1386 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1387 // follows the midpoint between both fingers.
1388 SWIPE,
1389
1390 // Two or more fingers moving in arbitrary directions, button is not pressed.
1391 // Pointer does not move.
1392 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1393 // each finger individually relative to the initial centroid of the finger.
1394 FREEFORM,
1395
1396 // Waiting for quiet time to end before starting the next gesture.
1397 QUIET,
1398 };
1399
1400 // Time the first finger went down.
1401 nsecs_t firstTouchTime;
1402
1403 // The active pointer id from the raw touch data.
1404 int32_t activeTouchId; // -1 if none
1405
1406 // The active pointer id from the gesture last delivered to the application.
1407 int32_t activeGestureId; // -1 if none
1408
1409 // Pointer coords and ids for the current and previous pointer gesture.
1410 Mode currentGestureMode;
1411 BitSet32 currentGestureIdBits;
1412 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1413 PointerProperties currentGestureProperties[MAX_POINTERS];
1414 PointerCoords currentGestureCoords[MAX_POINTERS];
1415
1416 Mode lastGestureMode;
1417 BitSet32 lastGestureIdBits;
1418 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1419 PointerProperties lastGestureProperties[MAX_POINTERS];
1420 PointerCoords lastGestureCoords[MAX_POINTERS];
1421
1422 // Time the pointer gesture last went down.
1423 nsecs_t downTime;
1424
1425 // Time when the pointer went down for a TAP.
1426 nsecs_t tapDownTime;
1427
1428 // Time when the pointer went up for a TAP.
1429 nsecs_t tapUpTime;
1430
1431 // Location of initial tap.
1432 float tapX, tapY;
1433
1434 // Time we started waiting for quiescence.
1435 nsecs_t quietTime;
1436
1437 // Reference points for multitouch gestures.
1438 float referenceTouchX; // reference touch X/Y coordinates in surface units
1439 float referenceTouchY;
1440 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1441 float referenceGestureY;
1442
1443 // Distance that each pointer has traveled which has not yet been
1444 // subsumed into the reference gesture position.
1445 BitSet32 referenceIdBits;
1446 struct Delta {
1447 float dx, dy;
1448 };
1449 Delta referenceDeltas[MAX_POINTER_ID + 1];
1450
1451 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1452 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1453
1454 // A velocity tracker for determining whether to switch active pointers during drags.
1455 VelocityTracker velocityTracker;
1456
1457 void reset() {
1458 firstTouchTime = LLONG_MIN;
1459 activeTouchId = -1;
1460 activeGestureId = -1;
1461 currentGestureMode = NEUTRAL;
1462 currentGestureIdBits.clear();
1463 lastGestureMode = NEUTRAL;
1464 lastGestureIdBits.clear();
1465 downTime = 0;
1466 velocityTracker.clear();
1467 resetTap();
1468 resetQuietTime();
1469 }
1470
1471 void resetTap() {
1472 tapDownTime = LLONG_MIN;
1473 tapUpTime = LLONG_MIN;
1474 }
1475
1476 void resetQuietTime() {
1477 quietTime = LLONG_MIN;
1478 }
1479 } mPointerGesture;
1480
1481 struct PointerSimple {
1482 PointerCoords currentCoords;
1483 PointerProperties currentProperties;
1484 PointerCoords lastCoords;
1485 PointerProperties lastProperties;
1486
1487 // True if the pointer is down.
1488 bool down;
1489
1490 // True if the pointer is hovering.
1491 bool hovering;
1492
1493 // Time the pointer last went down.
1494 nsecs_t downTime;
1495
1496 void reset() {
1497 currentCoords.clear();
1498 currentProperties.clear();
1499 lastCoords.clear();
1500 lastProperties.clear();
1501 down = false;
1502 hovering = false;
1503 downTime = 0;
1504 }
1505 } mPointerSimple;
1506
1507 // The pointer and scroll velocity controls.
1508 VelocityControl mPointerVelocityControl;
1509 VelocityControl mWheelXVelocityControl;
1510 VelocityControl mWheelYVelocityControl;
1511
Atif Niyaz83846822019-07-18 15:17:40 -07001512 static constexpr std::chrono::duration STATS_REPORT_PERIOD = 5min;
1513
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001514 // Latency statistics for touch events
Atif Niyaz83846822019-07-18 15:17:40 -07001515 LatencyStatistics mStatistics{STATS_REPORT_PERIOD};
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001517 std::optional<DisplayViewport> findViewport();
1518
Michael Wright842500e2015-03-13 17:32:02 -07001519 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001520 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001521
Michael Wrightd02c5b62014-02-10 15:10:22 -08001522 void sync(nsecs_t when);
1523
1524 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001525 void processRawTouches(bool timeout);
1526 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001527 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1528 int32_t keyEventAction, int32_t keyEventFlags);
1529
1530 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1531 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1532 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001533 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1534 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1535 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001537 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538
1539 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1540 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1541
1542 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1543 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1544 bool preparePointerGestures(nsecs_t when,
1545 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1546 bool isTimeout);
1547
1548 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1549 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1550
1551 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1552 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1553
1554 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1555 bool down, bool hovering);
1556 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1557
Michael Wright842500e2015-03-13 17:32:02 -07001558 bool assignExternalStylusId(const RawState& state, bool timeout);
1559 void applyExternalStylusButtonState(nsecs_t when);
1560 void applyExternalStylusTouchState(nsecs_t when);
1561
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562 // Dispatches a motion event.
1563 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1564 // method will take care of setting the index and transmuting the action to DOWN or UP
1565 // it is the first / last pointer to go down / up.
1566 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001567 int32_t action, int32_t actionButton,
1568 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 const PointerProperties* properties, const PointerCoords* coords,
1570 const uint32_t* idToIndex, BitSet32 idBits,
1571 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1572
1573 // Updates pointer coords and properties for pointers with specified ids that have moved.
1574 // Returns true if any of them changed.
1575 bool updateMovedPointers(const PointerProperties* inProperties,
1576 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1577 PointerProperties* outProperties, PointerCoords* outCoords,
1578 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1579
1580 bool isPointInsideSurface(int32_t x, int32_t y);
1581 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1582
Michael Wright842500e2015-03-13 17:32:02 -07001583 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001584
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001585 void reportEventForStatistics(nsecs_t evdevTime);
1586
Santos Cordonfa5cf462017-04-05 10:37:00 -07001587 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588};
1589
1590
1591class SingleTouchInputMapper : public TouchInputMapper {
1592public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001593 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 virtual ~SingleTouchInputMapper();
1595
1596 virtual void reset(nsecs_t when);
1597 virtual void process(const RawEvent* rawEvent);
1598
1599protected:
Michael Wright842500e2015-03-13 17:32:02 -07001600 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 virtual void configureRawPointerAxes();
1602 virtual bool hasStylus() const;
1603
1604private:
1605 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1606};
1607
1608
1609class MultiTouchInputMapper : public TouchInputMapper {
1610public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001611 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612 virtual ~MultiTouchInputMapper();
1613
1614 virtual void reset(nsecs_t when);
1615 virtual void process(const RawEvent* rawEvent);
1616
1617protected:
Michael Wright842500e2015-03-13 17:32:02 -07001618 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619 virtual void configureRawPointerAxes();
1620 virtual bool hasStylus() const;
1621
1622private:
1623 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1624
1625 // Specifies the pointer id bits that are in use, and their associated tracking id.
1626 BitSet32 mPointerIdBits;
1627 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1628};
1629
Michael Wright842500e2015-03-13 17:32:02 -07001630class ExternalStylusInputMapper : public InputMapper {
1631public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001632 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001633 virtual ~ExternalStylusInputMapper() = default;
1634
1635 virtual uint32_t getSources();
1636 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001637 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001638 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1639 virtual void reset(nsecs_t when);
1640 virtual void process(const RawEvent* rawEvent);
1641 virtual void sync(nsecs_t when);
1642
1643private:
1644 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1645 RawAbsoluteAxisInfo mRawPressureAxis;
1646 TouchButtonAccumulator mTouchButtonAccumulator;
1647
1648 StylusState mStylusState;
1649};
1650
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651
1652class JoystickInputMapper : public InputMapper {
1653public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001654 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655 virtual ~JoystickInputMapper();
1656
1657 virtual uint32_t getSources();
1658 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001659 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1661 virtual void reset(nsecs_t when);
1662 virtual void process(const RawEvent* rawEvent);
1663
1664private:
1665 struct Axis {
1666 RawAbsoluteAxisInfo rawAxisInfo;
1667 AxisInfo axisInfo;
1668
1669 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1670
1671 float scale; // scale factor from raw to normalized values
1672 float offset; // offset to add after scaling for normalization
1673 float highScale; // scale factor from raw to normalized values of high split
1674 float highOffset; // offset to add after scaling for normalization of high split
1675
1676 float min; // normalized inclusive minimum
1677 float max; // normalized inclusive maximum
1678 float flat; // normalized flat region size
1679 float fuzz; // normalized error tolerance
1680 float resolution; // normalized resolution in units/mm
1681
1682 float filter; // filter out small variations of this size
1683 float currentValue; // current value
1684 float newValue; // most recent value
1685 float highCurrentValue; // current value of high split
1686 float highNewValue; // most recent value of high split
1687
1688 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1689 bool explicitlyMapped, float scale, float offset,
1690 float highScale, float highOffset,
1691 float min, float max, float flat, float fuzz, float resolution) {
1692 this->rawAxisInfo = rawAxisInfo;
1693 this->axisInfo = axisInfo;
1694 this->explicitlyMapped = explicitlyMapped;
1695 this->scale = scale;
1696 this->offset = offset;
1697 this->highScale = highScale;
1698 this->highOffset = highOffset;
1699 this->min = min;
1700 this->max = max;
1701 this->flat = flat;
1702 this->fuzz = fuzz;
1703 this->resolution = resolution;
1704 this->filter = 0;
1705 resetValue();
1706 }
1707
1708 void resetValue() {
1709 this->currentValue = 0;
1710 this->newValue = 0;
1711 this->highCurrentValue = 0;
1712 this->highNewValue = 0;
1713 }
1714 };
1715
1716 // Axes indexed by raw ABS_* axis index.
1717 KeyedVector<int32_t, Axis> mAxes;
1718
1719 void sync(nsecs_t when, bool force);
1720
1721 bool haveAxis(int32_t axisId);
1722 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1723 bool filterAxes(bool force);
1724
1725 static bool hasValueChangedSignificantly(float filter,
1726 float newValue, float currentValue, float min, float max);
1727 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1728 float newValue, float currentValue, float thresholdValue);
1729
1730 static bool isCenteredAxis(int32_t axis);
1731 static int32_t getCompatAxis(int32_t axis);
1732
1733 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1734 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1735 float value);
1736};
1737
1738} // namespace android
1739
1740#endif // _UI_INPUT_READER_H