blob: 0c08e7da38de9639c744bb2d74ab33ac53d16bb6 [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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080093 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) = 0;
Michael Wrightb85401d2015-04-17 18:35:15 +010094 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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800127 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128
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
Arthur Hungc23540e2018-11-29 20:42:11 +0800149 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150protected:
151 // These members are protected so they can be instrumented by test cases.
152 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
153 const InputDeviceIdentifier& identifier, uint32_t classes);
154
155 class ContextImpl : public InputReaderContext {
156 InputReader* mReader;
157
158 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700159 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160
161 virtual void updateGlobalMetaState();
162 virtual int32_t getGlobalMetaState();
163 virtual void disableVirtualKeysUntil(nsecs_t time);
164 virtual bool shouldDropVirtualKey(nsecs_t now,
165 InputDevice* device, int32_t keyCode, int32_t scanCode);
166 virtual void fadePointer();
167 virtual void requestTimeoutAtTime(nsecs_t when);
168 virtual int32_t bumpGeneration();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700170 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 virtual InputReaderPolicyInterface* getPolicy();
172 virtual InputListenerInterface* getListener();
173 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800174 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 } mContext;
176
177 friend class ContextImpl;
178
179private:
180 Mutex mLock;
181
182 Condition mReaderIsAliveCondition;
183
184 sp<EventHubInterface> mEventHub;
185 sp<InputReaderPolicyInterface> mPolicy;
186 sp<QueuedInputListener> mQueuedListener;
187
188 InputReaderConfiguration mConfig;
189
Prabir Pradhan42611e02018-11-27 14:04:02 -0800190 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
191 uint32_t mNextSequenceNum;
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 // The event queue.
194 static const int EVENT_BUFFER_SIZE = 256;
195 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
196
197 KeyedVector<int32_t, InputDevice*> mDevices;
198
199 // low-level input event decoding and device management
200 void processEventsLocked(const RawEvent* rawEvents, size_t count);
201
202 void addDeviceLocked(nsecs_t when, int32_t deviceId);
203 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
204 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
205 void timeoutExpiredLocked(nsecs_t when);
206
207 void handleConfigurationChangedLocked(nsecs_t when);
208
209 int32_t mGlobalMetaState;
210 void updateGlobalMetaStateLocked();
211 int32_t getGlobalMetaStateLocked();
212
Michael Wright842500e2015-03-13 17:32:02 -0700213 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800214 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700215 void dispatchExternalStylusState(const StylusState& state);
216
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 void fadePointerLocked();
218
219 int32_t mGeneration;
220 int32_t bumpGenerationLocked();
221
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800222 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
224 nsecs_t mDisableVirtualKeysTimeout;
225 void disableVirtualKeysUntilLocked(nsecs_t time);
226 bool shouldDropVirtualKeyLocked(nsecs_t now,
227 InputDevice* device, int32_t keyCode, int32_t scanCode);
228
229 nsecs_t mNextTimeout;
230 void requestTimeoutAtTimeLocked(nsecs_t when);
231
232 uint32_t mConfigurationChangesToRefresh;
233 void refreshConfigurationLocked(uint32_t changes);
234
235 // state queries
236 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
237 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
238 GetStateFunc getStateFunc);
239 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
240 const int32_t* keyCodes, uint8_t* outFlags);
241};
242
243
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244/* Represents the state of a single input device. */
245class InputDevice {
246public:
247 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
248 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
249 ~InputDevice();
250
251 inline InputReaderContext* getContext() { return mContext; }
252 inline int32_t getId() const { return mId; }
253 inline int32_t getControllerNumber() const { return mControllerNumber; }
254 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100255 inline const std::string getName() const { return mIdentifier.name; }
256 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 inline uint32_t getClasses() const { return mClasses; }
258 inline uint32_t getSources() const { return mSources; }
259
260 inline bool isExternal() { return mIsExternal; }
261 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700262 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
263 return mAssociatedDisplayPort;
264 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265
Tim Kilbourn063ff532015-04-08 10:26:18 -0700266 inline void setMic(bool hasMic) { mHasMic = hasMic; }
267 inline bool hasMic() const { return mHasMic; }
268
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800269 inline bool isIgnored() { return mMappers.empty(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700271 bool isEnabled();
272 void setEnabled(bool enabled, nsecs_t when);
273
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800274 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 void addMapper(InputMapper* mapper);
276 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
277 void reset(nsecs_t when);
278 void process(const RawEvent* rawEvents, size_t count);
279 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700280 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
282 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
283 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
284 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
285 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
286 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
287 const int32_t* keyCodes, uint8_t* outFlags);
288 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
289 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800290 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291
292 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800293 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294
295 void fadePointer();
296
297 void bumpGeneration();
298
299 void notifyReset(nsecs_t when);
300
301 inline const PropertyMap& getConfiguration() { return mConfiguration; }
302 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
303
304 bool hasKey(int32_t code) {
305 return getEventHub()->hasScanCode(mId, code);
306 }
307
308 bool hasAbsoluteAxis(int32_t code) {
309 RawAbsoluteAxisInfo info;
310 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
311 return info.valid;
312 }
313
314 bool isKeyPressed(int32_t code) {
315 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
316 }
317
318 int32_t getAbsoluteAxisValue(int32_t code) {
319 int32_t value;
320 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
321 return value;
322 }
323
Arthur Hungc23540e2018-11-29 20:42:11 +0800324 std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325private:
326 InputReaderContext* mContext;
327 int32_t mId;
328 int32_t mGeneration;
329 int32_t mControllerNumber;
330 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100331 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 uint32_t mClasses;
333
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800334 std::vector<InputMapper*> mMappers;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
336 uint32_t mSources;
337 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700338 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700339 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 bool mDropUntilNextSync;
341
342 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
343 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
344
345 PropertyMap mConfiguration;
346};
347
348
349/* Keeps track of the state of mouse or touch pad buttons. */
350class CursorButtonAccumulator {
351public:
352 CursorButtonAccumulator();
353 void reset(InputDevice* device);
354
355 void process(const RawEvent* rawEvent);
356
357 uint32_t getButtonState() const;
358
359private:
360 bool mBtnLeft;
361 bool mBtnRight;
362 bool mBtnMiddle;
363 bool mBtnBack;
364 bool mBtnSide;
365 bool mBtnForward;
366 bool mBtnExtra;
367 bool mBtnTask;
368
369 void clearButtons();
370};
371
372
373/* Keeps track of cursor movements. */
374
375class CursorMotionAccumulator {
376public:
377 CursorMotionAccumulator();
378 void reset(InputDevice* device);
379
380 void process(const RawEvent* rawEvent);
381 void finishSync();
382
383 inline int32_t getRelativeX() const { return mRelX; }
384 inline int32_t getRelativeY() const { return mRelY; }
385
386private:
387 int32_t mRelX;
388 int32_t mRelY;
389
390 void clearRelativeAxes();
391};
392
393
394/* Keeps track of cursor scrolling motions. */
395
396class CursorScrollAccumulator {
397public:
398 CursorScrollAccumulator();
399 void configure(InputDevice* device);
400 void reset(InputDevice* device);
401
402 void process(const RawEvent* rawEvent);
403 void finishSync();
404
405 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
406 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
407
408 inline int32_t getRelativeX() const { return mRelX; }
409 inline int32_t getRelativeY() const { return mRelY; }
410 inline int32_t getRelativeVWheel() const { return mRelWheel; }
411 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
412
413private:
414 bool mHaveRelWheel;
415 bool mHaveRelHWheel;
416
417 int32_t mRelX;
418 int32_t mRelY;
419 int32_t mRelWheel;
420 int32_t mRelHWheel;
421
422 void clearRelativeAxes();
423};
424
425
426/* Keeps track of the state of touch, stylus and tool buttons. */
427class TouchButtonAccumulator {
428public:
429 TouchButtonAccumulator();
430 void configure(InputDevice* device);
431 void reset(InputDevice* device);
432
433 void process(const RawEvent* rawEvent);
434
435 uint32_t getButtonState() const;
436 int32_t getToolType() const;
437 bool isToolActive() const;
438 bool isHovering() const;
439 bool hasStylus() const;
440
441private:
442 bool mHaveBtnTouch;
443 bool mHaveStylus;
444
445 bool mBtnTouch;
446 bool mBtnStylus;
447 bool mBtnStylus2;
448 bool mBtnToolFinger;
449 bool mBtnToolPen;
450 bool mBtnToolRubber;
451 bool mBtnToolBrush;
452 bool mBtnToolPencil;
453 bool mBtnToolAirbrush;
454 bool mBtnToolMouse;
455 bool mBtnToolLens;
456 bool mBtnToolDoubleTap;
457 bool mBtnToolTripleTap;
458 bool mBtnToolQuadTap;
459
460 void clearButtons();
461};
462
463
464/* Raw axis information from the driver. */
465struct RawPointerAxes {
466 RawAbsoluteAxisInfo x;
467 RawAbsoluteAxisInfo y;
468 RawAbsoluteAxisInfo pressure;
469 RawAbsoluteAxisInfo touchMajor;
470 RawAbsoluteAxisInfo touchMinor;
471 RawAbsoluteAxisInfo toolMajor;
472 RawAbsoluteAxisInfo toolMinor;
473 RawAbsoluteAxisInfo orientation;
474 RawAbsoluteAxisInfo distance;
475 RawAbsoluteAxisInfo tiltX;
476 RawAbsoluteAxisInfo tiltY;
477 RawAbsoluteAxisInfo trackingId;
478 RawAbsoluteAxisInfo slot;
479
480 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800481 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
482 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483 void clear();
484};
485
486
487/* Raw data for a collection of pointers including a pointer id mapping table. */
488struct RawPointerData {
489 struct Pointer {
490 uint32_t id;
491 int32_t x;
492 int32_t y;
493 int32_t pressure;
494 int32_t touchMajor;
495 int32_t touchMinor;
496 int32_t toolMajor;
497 int32_t toolMinor;
498 int32_t orientation;
499 int32_t distance;
500 int32_t tiltX;
501 int32_t tiltY;
502 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
503 bool isHovering;
504 };
505
506 uint32_t pointerCount;
507 Pointer pointers[MAX_POINTERS];
508 BitSet32 hoveringIdBits, touchingIdBits;
509 uint32_t idToIndex[MAX_POINTER_ID + 1];
510
511 RawPointerData();
512 void clear();
513 void copyFrom(const RawPointerData& other);
514 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
515
516 inline void markIdBit(uint32_t id, bool isHovering) {
517 if (isHovering) {
518 hoveringIdBits.markBit(id);
519 } else {
520 touchingIdBits.markBit(id);
521 }
522 }
523
524 inline void clearIdBits() {
525 hoveringIdBits.clear();
526 touchingIdBits.clear();
527 }
528
529 inline const Pointer& pointerForId(uint32_t id) const {
530 return pointers[idToIndex[id]];
531 }
532
533 inline bool isHovering(uint32_t pointerIndex) {
534 return pointers[pointerIndex].isHovering;
535 }
536};
537
538
539/* Cooked data for a collection of pointers including a pointer id mapping table. */
540struct CookedPointerData {
541 uint32_t pointerCount;
542 PointerProperties pointerProperties[MAX_POINTERS];
543 PointerCoords pointerCoords[MAX_POINTERS];
544 BitSet32 hoveringIdBits, touchingIdBits;
545 uint32_t idToIndex[MAX_POINTER_ID + 1];
546
547 CookedPointerData();
548 void clear();
549 void copyFrom(const CookedPointerData& other);
550
551 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
552 return pointerCoords[idToIndex[id]];
553 }
554
Michael Wright842500e2015-03-13 17:32:02 -0700555 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
556 return pointerCoords[idToIndex[id]];
557 }
558
559 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
560 return pointerProperties[idToIndex[id]];
561 }
562
Michael Wright53dca3a2015-04-23 17:39:53 +0100563 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
565 }
Michael Wright842500e2015-03-13 17:32:02 -0700566
Michael Wright53dca3a2015-04-23 17:39:53 +0100567 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700568 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
569 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570};
571
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -0800572/**
573 * Basic statistics information.
574 * Keep track of min, max, average, and standard deviation of the received samples.
575 * Used to report latency information about input events.
576 */
577struct LatencyStatistics {
578 float min;
579 float max;
580 // Sum of all samples
581 float sum;
582 // Sum of squares of all samples
583 float sum2;
584 // The number of samples
585 size_t count;
586 // The last time statistics were reported.
587 nsecs_t lastReportTime;
588
589 LatencyStatistics() {
590 reset(systemTime(SYSTEM_TIME_MONOTONIC));
591 }
592
593 inline void addValue(float x) {
594 if (x < min) {
595 min = x;
596 }
597 if (x > max) {
598 max = x;
599 }
600 sum += x;
601 sum2 += x * x;
602 count++;
603 }
604
605 // Get the average value. Should not be called if no samples have been added.
606 inline float mean() {
607 if (count == 0) {
608 return 0;
609 }
610 return sum / count;
611 }
612
613 // Get the standard deviation. Should not be called if no samples have been added.
614 inline float stdev() {
615 if (count == 0) {
616 return 0;
617 }
618 float average = mean();
619 return sqrt(sum2 / count - average * average);
620 }
621
622 /**
623 * Reset internal state. The variable 'when' is the time when the data collection started.
624 * Call this to start a new data collection window.
625 */
626 inline void reset(nsecs_t when) {
627 max = 0;
628 min = std::numeric_limits<float>::max();
629 sum = 0;
630 sum2 = 0;
631 count = 0;
632 lastReportTime = when;
633 }
634};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635
636/* Keeps track of the state of single-touch protocol. */
637class SingleTouchMotionAccumulator {
638public:
639 SingleTouchMotionAccumulator();
640
641 void process(const RawEvent* rawEvent);
642 void reset(InputDevice* device);
643
644 inline int32_t getAbsoluteX() const { return mAbsX; }
645 inline int32_t getAbsoluteY() const { return mAbsY; }
646 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
647 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
648 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
649 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
650 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
651
652private:
653 int32_t mAbsX;
654 int32_t mAbsY;
655 int32_t mAbsPressure;
656 int32_t mAbsToolWidth;
657 int32_t mAbsDistance;
658 int32_t mAbsTiltX;
659 int32_t mAbsTiltY;
660
661 void clearAbsoluteAxes();
662};
663
664
665/* Keeps track of the state of multi-touch protocol. */
666class MultiTouchMotionAccumulator {
667public:
668 class Slot {
669 public:
670 inline bool isInUse() const { return mInUse; }
671 inline int32_t getX() const { return mAbsMTPositionX; }
672 inline int32_t getY() const { return mAbsMTPositionY; }
673 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
674 inline int32_t getTouchMinor() const {
675 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
676 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
677 inline int32_t getToolMinor() const {
678 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
679 inline int32_t getOrientation() const { return mAbsMTOrientation; }
680 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
681 inline int32_t getPressure() const { return mAbsMTPressure; }
682 inline int32_t getDistance() const { return mAbsMTDistance; }
683 inline int32_t getToolType() const;
684
685 private:
686 friend class MultiTouchMotionAccumulator;
687
688 bool mInUse;
689 bool mHaveAbsMTTouchMinor;
690 bool mHaveAbsMTWidthMinor;
691 bool mHaveAbsMTToolType;
692
693 int32_t mAbsMTPositionX;
694 int32_t mAbsMTPositionY;
695 int32_t mAbsMTTouchMajor;
696 int32_t mAbsMTTouchMinor;
697 int32_t mAbsMTWidthMajor;
698 int32_t mAbsMTWidthMinor;
699 int32_t mAbsMTOrientation;
700 int32_t mAbsMTTrackingId;
701 int32_t mAbsMTPressure;
702 int32_t mAbsMTDistance;
703 int32_t mAbsMTToolType;
704
705 Slot();
706 void clear();
707 };
708
709 MultiTouchMotionAccumulator();
710 ~MultiTouchMotionAccumulator();
711
712 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
713 void reset(InputDevice* device);
714 void process(const RawEvent* rawEvent);
715 void finishSync();
716 bool hasStylus() const;
717
718 inline size_t getSlotCount() const { return mSlotCount; }
719 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
720
721private:
722 int32_t mCurrentSlot;
723 Slot* mSlots;
724 size_t mSlotCount;
725 bool mUsingSlotsProtocol;
726 bool mHaveStylus;
727
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();
Arthur Hungc23540e2018-11-29 20:42:11 +0800781 virtual std::optional<int32_t> getAssociatedDisplay() {
782 return std::nullopt;
783 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784protected:
785 InputDevice* mDevice;
786 InputReaderContext* mContext;
787
788 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
789 void bumpGeneration();
790
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800791 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800793 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794};
795
796
797class SwitchInputMapper : public InputMapper {
798public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700799 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 virtual ~SwitchInputMapper();
801
802 virtual uint32_t getSources();
803 virtual void process(const RawEvent* rawEvent);
804
805 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800806 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807
808private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700809 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 uint32_t mUpdatedSwitchMask;
811
812 void processSwitch(int32_t switchCode, int32_t switchValue);
813 void sync(nsecs_t when);
814};
815
816
817class VibratorInputMapper : public InputMapper {
818public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700819 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 virtual ~VibratorInputMapper();
821
822 virtual uint32_t getSources();
823 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
824 virtual void process(const RawEvent* rawEvent);
825
826 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
827 int32_t token);
828 virtual void cancelVibrate(int32_t token);
829 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800830 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
832private:
833 bool mVibrating;
834 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
835 size_t mPatternSize;
836 ssize_t mRepeat;
837 int32_t mToken;
838 ssize_t mIndex;
839 nsecs_t mNextStepTime;
840
841 void nextStep();
842 void stopVibrating();
843};
844
845
846class KeyboardInputMapper : public InputMapper {
847public:
848 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
849 virtual ~KeyboardInputMapper();
850
851 virtual uint32_t getSources();
852 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800853 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
855 virtual void reset(nsecs_t when);
856 virtual void process(const RawEvent* rawEvent);
857
858 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
859 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
860 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
861 const int32_t* keyCodes, uint8_t* outFlags);
862
863 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800864 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
866private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100867 // The current viewport.
868 std::optional<DisplayViewport> mViewport;
869
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870 struct KeyDown {
871 int32_t keyCode;
872 int32_t scanCode;
873 };
874
875 uint32_t mSource;
876 int32_t mKeyboardType;
877
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800878 std::vector<KeyDown> mKeyDowns; // keys that are down
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 int32_t mMetaState;
880 nsecs_t mDownTime; // time of most recent key down
881
882 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
883
884 struct LedState {
885 bool avail; // led is available
886 bool on; // we think the led is currently on
887 };
888 LedState mCapsLockLedState;
889 LedState mNumLockLedState;
890 LedState mScrollLockLedState;
891
892 // Immutable configuration parameters.
893 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700895 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 } mParameters;
897
898 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800899 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100901 int32_t getOrientation();
902 int32_t getDisplayId();
903
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100905 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700907 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
Andrii Kulian763a3a42016-03-08 10:46:16 -0800909 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
910
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911 ssize_t findKeyDown(int32_t scanCode);
912
913 void resetLedState();
914 void initializeLedState(LedState& ledState, int32_t led);
915 void updateLedState(bool reset);
916 void updateLedStateForModifier(LedState& ledState, int32_t led,
917 int32_t modifier, bool reset);
918};
919
920
921class CursorInputMapper : public InputMapper {
922public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700923 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 virtual ~CursorInputMapper();
925
926 virtual uint32_t getSources();
927 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800928 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
930 virtual void reset(nsecs_t when);
931 virtual void process(const RawEvent* rawEvent);
932
933 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
934
935 virtual void fadePointer();
936
Arthur Hungc23540e2018-11-29 20:42:11 +0800937 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938private:
939 // Amount that trackball needs to move in order to generate a key event.
940 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
941
942 // Immutable configuration parameters.
943 struct Parameters {
944 enum Mode {
945 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800946 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 MODE_NAVIGATION,
948 };
949
950 Mode mode;
951 bool hasAssociatedDisplay;
952 bool orientationAware;
953 } mParameters;
954
955 CursorButtonAccumulator mCursorButtonAccumulator;
956 CursorMotionAccumulator mCursorMotionAccumulator;
957 CursorScrollAccumulator mCursorScrollAccumulator;
958
959 int32_t mSource;
960 float mXScale;
961 float mYScale;
962 float mXPrecision;
963 float mYPrecision;
964
965 float mVWheelScale;
966 float mHWheelScale;
967
968 // Velocity controls for mouse pointer and wheel movements.
969 // The controls for X and Y wheel movements are separate to keep them decoupled.
970 VelocityControl mPointerVelocityControl;
971 VelocityControl mWheelXVelocityControl;
972 VelocityControl mWheelYVelocityControl;
973
974 int32_t mOrientation;
975
976 sp<PointerControllerInterface> mPointerController;
977
978 int32_t mButtonState;
979 nsecs_t mDownTime;
980
981 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800982 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
984 void sync(nsecs_t when);
985};
986
987
Prashant Malani1941ff52015-08-11 18:29:28 -0700988class RotaryEncoderInputMapper : public InputMapper {
989public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700990 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700991 virtual ~RotaryEncoderInputMapper();
992
993 virtual uint32_t getSources();
994 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800995 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700996 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
997 virtual void reset(nsecs_t when);
998 virtual void process(const RawEvent* rawEvent);
999
1000private:
1001 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
1002
1003 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001004 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +01001005 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -07001006
1007 void sync(nsecs_t when);
1008};
1009
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010class TouchInputMapper : public InputMapper {
1011public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001012 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 virtual ~TouchInputMapper();
1014
1015 virtual uint32_t getSources();
1016 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001017 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1019 virtual void reset(nsecs_t when);
1020 virtual void process(const RawEvent* rawEvent);
1021
1022 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1023 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1024 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1025 const int32_t* keyCodes, uint8_t* outFlags);
1026
1027 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001028 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001030 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hungc23540e2018-11-29 20:42:11 +08001031 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032protected:
1033 CursorButtonAccumulator mCursorButtonAccumulator;
1034 CursorScrollAccumulator mCursorScrollAccumulator;
1035 TouchButtonAccumulator mTouchButtonAccumulator;
1036
1037 struct VirtualKey {
1038 int32_t keyCode;
1039 int32_t scanCode;
1040 uint32_t flags;
1041
1042 // computed hit box, specified in touch screen coords based on known display size
1043 int32_t hitLeft;
1044 int32_t hitTop;
1045 int32_t hitRight;
1046 int32_t hitBottom;
1047
1048 inline bool isHit(int32_t x, int32_t y) const {
1049 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1050 }
1051 };
1052
1053 // Input sources and device mode.
1054 uint32_t mSource;
1055
1056 enum DeviceMode {
1057 DEVICE_MODE_DISABLED, // input is disabled
1058 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1059 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1060 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1061 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1062 };
1063 DeviceMode mDeviceMode;
1064
1065 // The reader's configuration.
1066 InputReaderConfiguration mConfig;
1067
1068 // Immutable configuration parameters.
1069 struct Parameters {
1070 enum DeviceType {
1071 DEVICE_TYPE_TOUCH_SCREEN,
1072 DEVICE_TYPE_TOUCH_PAD,
1073 DEVICE_TYPE_TOUCH_NAVIGATION,
1074 DEVICE_TYPE_POINTER,
1075 };
1076
1077 DeviceType deviceType;
1078 bool hasAssociatedDisplay;
1079 bool associatedDisplayIsExternal;
1080 bool orientationAware;
1081 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001082 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
1084 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001085 GESTURE_MODE_SINGLE_TOUCH,
1086 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 };
1088 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001089
1090 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091 } mParameters;
1092
1093 // Immutable calibration parameters in parsed form.
1094 struct Calibration {
1095 // Size
1096 enum SizeCalibration {
1097 SIZE_CALIBRATION_DEFAULT,
1098 SIZE_CALIBRATION_NONE,
1099 SIZE_CALIBRATION_GEOMETRIC,
1100 SIZE_CALIBRATION_DIAMETER,
1101 SIZE_CALIBRATION_BOX,
1102 SIZE_CALIBRATION_AREA,
1103 };
1104
1105 SizeCalibration sizeCalibration;
1106
1107 bool haveSizeScale;
1108 float sizeScale;
1109 bool haveSizeBias;
1110 float sizeBias;
1111 bool haveSizeIsSummed;
1112 bool sizeIsSummed;
1113
1114 // Pressure
1115 enum PressureCalibration {
1116 PRESSURE_CALIBRATION_DEFAULT,
1117 PRESSURE_CALIBRATION_NONE,
1118 PRESSURE_CALIBRATION_PHYSICAL,
1119 PRESSURE_CALIBRATION_AMPLITUDE,
1120 };
1121
1122 PressureCalibration pressureCalibration;
1123 bool havePressureScale;
1124 float pressureScale;
1125
1126 // Orientation
1127 enum OrientationCalibration {
1128 ORIENTATION_CALIBRATION_DEFAULT,
1129 ORIENTATION_CALIBRATION_NONE,
1130 ORIENTATION_CALIBRATION_INTERPOLATED,
1131 ORIENTATION_CALIBRATION_VECTOR,
1132 };
1133
1134 OrientationCalibration orientationCalibration;
1135
1136 // Distance
1137 enum DistanceCalibration {
1138 DISTANCE_CALIBRATION_DEFAULT,
1139 DISTANCE_CALIBRATION_NONE,
1140 DISTANCE_CALIBRATION_SCALED,
1141 };
1142
1143 DistanceCalibration distanceCalibration;
1144 bool haveDistanceScale;
1145 float distanceScale;
1146
1147 enum CoverageCalibration {
1148 COVERAGE_CALIBRATION_DEFAULT,
1149 COVERAGE_CALIBRATION_NONE,
1150 COVERAGE_CALIBRATION_BOX,
1151 };
1152
1153 CoverageCalibration coverageCalibration;
1154
1155 inline void applySizeScaleAndBias(float* outSize) const {
1156 if (haveSizeScale) {
1157 *outSize *= sizeScale;
1158 }
1159 if (haveSizeBias) {
1160 *outSize += sizeBias;
1161 }
1162 if (*outSize < 0) {
1163 *outSize = 0;
1164 }
1165 }
1166 } mCalibration;
1167
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001168 // Affine location transformation/calibration
1169 struct TouchAffineTransformation mAffineTransform;
1170
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 RawPointerAxes mRawPointerAxes;
1172
Michael Wright842500e2015-03-13 17:32:02 -07001173 struct RawState {
1174 nsecs_t when;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175
Michael Wright842500e2015-03-13 17:32:02 -07001176 // Raw pointer sample data.
1177 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Michael Wright842500e2015-03-13 17:32:02 -07001179 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180
Michael Wright842500e2015-03-13 17:32:02 -07001181 // Scroll state.
1182 int32_t rawVScroll;
1183 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184
Michael Wright842500e2015-03-13 17:32:02 -07001185 void copyFrom(const RawState& other) {
1186 when = other.when;
1187 rawPointerData.copyFrom(other.rawPointerData);
1188 buttonState = other.buttonState;
1189 rawVScroll = other.rawVScroll;
1190 rawHScroll = other.rawHScroll;
1191 }
1192
1193 void clear() {
1194 when = 0;
1195 rawPointerData.clear();
1196 buttonState = 0;
1197 rawVScroll = 0;
1198 rawHScroll = 0;
1199 }
1200 };
1201
1202 struct CookedState {
1203 // Cooked pointer sample data.
1204 CookedPointerData cookedPointerData;
1205
1206 // Id bits used to differentiate fingers, stylus and mouse tools.
1207 BitSet32 fingerIdBits;
1208 BitSet32 stylusIdBits;
1209 BitSet32 mouseIdBits;
1210
Michael Wright7b159c92015-05-14 14:48:03 +01001211 int32_t buttonState;
1212
Michael Wright842500e2015-03-13 17:32:02 -07001213 void copyFrom(const CookedState& other) {
1214 cookedPointerData.copyFrom(other.cookedPointerData);
1215 fingerIdBits = other.fingerIdBits;
1216 stylusIdBits = other.stylusIdBits;
1217 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001218 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001219 }
1220
1221 void clear() {
1222 cookedPointerData.clear();
1223 fingerIdBits.clear();
1224 stylusIdBits.clear();
1225 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001226 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001227 }
1228 };
1229
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001230 std::vector<RawState> mRawStatesPending;
Michael Wright842500e2015-03-13 17:32:02 -07001231 RawState mCurrentRawState;
1232 CookedState mCurrentCookedState;
1233 RawState mLastRawState;
1234 CookedState mLastCookedState;
1235
1236 // State provided by an external stylus
1237 StylusState mExternalStylusState;
1238 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001239 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001240 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241
1242 // True if we sent a HOVER_ENTER event.
1243 bool mSentHoverEnter;
1244
Michael Wright842500e2015-03-13 17:32:02 -07001245 // Have we assigned pointer IDs for this stream
1246 bool mHavePointerIds;
1247
Michael Wright8e812822015-06-22 16:18:21 +01001248 // Is the current stream of direct touch events aborted
1249 bool mCurrentMotionAborted;
1250
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 // The time the primary pointer last went down.
1252 nsecs_t mDownTime;
1253
1254 // The pointer controller, or null if the device is not a pointer.
1255 sp<PointerControllerInterface> mPointerController;
1256
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001257 std::vector<VirtualKey> mVirtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258
1259 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001260 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001262 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001264 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001266 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 virtual void parseCalibration();
1268 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001269 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001270 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001271 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001272 virtual void resolveExternalStylusPresence();
1273 virtual bool hasStylus() const = 0;
1274 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Michael Wright842500e2015-03-13 17:32:02 -07001276 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
1278private:
1279 // The current viewport.
1280 // The components of the viewport are specified in the display's rotated orientation.
1281 DisplayViewport mViewport;
1282
1283 // The surface orientation, width and height set by configureSurface().
1284 // The width and height are derived from the viewport but are specified
1285 // in the natural orientation.
1286 // The surface origin specifies how the surface coordinates should be translated
1287 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288 int32_t mSurfaceWidth;
1289 int32_t mSurfaceHeight;
1290 int32_t mSurfaceLeft;
1291 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001292
1293 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1294 // the logical coordinate space.
1295 int32_t mPhysicalWidth;
1296 int32_t mPhysicalHeight;
1297 int32_t mPhysicalLeft;
1298 int32_t mPhysicalTop;
1299
1300 // The orientation may be different from the viewport orientation as it specifies
1301 // the rotation of the surface coordinates required to produce the viewport's
1302 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303 int32_t mSurfaceOrientation;
1304
1305 // Translation and scaling factors, orientation-independent.
1306 float mXTranslate;
1307 float mXScale;
1308 float mXPrecision;
1309
1310 float mYTranslate;
1311 float mYScale;
1312 float mYPrecision;
1313
1314 float mGeometricScale;
1315
1316 float mPressureScale;
1317
1318 float mSizeScale;
1319
1320 float mOrientationScale;
1321
1322 float mDistanceScale;
1323
1324 bool mHaveTilt;
1325 float mTiltXCenter;
1326 float mTiltXScale;
1327 float mTiltYCenter;
1328 float mTiltYScale;
1329
Michael Wright842500e2015-03-13 17:32:02 -07001330 bool mExternalStylusConnected;
1331
Michael Wrightd02c5b62014-02-10 15:10:22 -08001332 // Oriented motion ranges for input device info.
1333 struct OrientedRanges {
1334 InputDeviceInfo::MotionRange x;
1335 InputDeviceInfo::MotionRange y;
1336 InputDeviceInfo::MotionRange pressure;
1337
1338 bool haveSize;
1339 InputDeviceInfo::MotionRange size;
1340
1341 bool haveTouchSize;
1342 InputDeviceInfo::MotionRange touchMajor;
1343 InputDeviceInfo::MotionRange touchMinor;
1344
1345 bool haveToolSize;
1346 InputDeviceInfo::MotionRange toolMajor;
1347 InputDeviceInfo::MotionRange toolMinor;
1348
1349 bool haveOrientation;
1350 InputDeviceInfo::MotionRange orientation;
1351
1352 bool haveDistance;
1353 InputDeviceInfo::MotionRange distance;
1354
1355 bool haveTilt;
1356 InputDeviceInfo::MotionRange tilt;
1357
1358 OrientedRanges() {
1359 clear();
1360 }
1361
1362 void clear() {
1363 haveSize = false;
1364 haveTouchSize = false;
1365 haveToolSize = false;
1366 haveOrientation = false;
1367 haveDistance = false;
1368 haveTilt = false;
1369 }
1370 } mOrientedRanges;
1371
1372 // Oriented dimensions and precision.
1373 float mOrientedXPrecision;
1374 float mOrientedYPrecision;
1375
1376 struct CurrentVirtualKeyState {
1377 bool down;
1378 bool ignored;
1379 nsecs_t downTime;
1380 int32_t keyCode;
1381 int32_t scanCode;
1382 } mCurrentVirtualKey;
1383
1384 // Scale factor for gesture or mouse based pointer movements.
1385 float mPointerXMovementScale;
1386 float mPointerYMovementScale;
1387
1388 // Scale factor for gesture based zooming and other freeform motions.
1389 float mPointerXZoomScale;
1390 float mPointerYZoomScale;
1391
1392 // The maximum swipe width.
1393 float mPointerGestureMaxSwipeWidth;
1394
1395 struct PointerDistanceHeapElement {
1396 uint32_t currentPointerIndex : 8;
1397 uint32_t lastPointerIndex : 8;
1398 uint64_t distance : 48; // squared distance
1399 };
1400
1401 enum PointerUsage {
1402 POINTER_USAGE_NONE,
1403 POINTER_USAGE_GESTURES,
1404 POINTER_USAGE_STYLUS,
1405 POINTER_USAGE_MOUSE,
1406 };
1407 PointerUsage mPointerUsage;
1408
1409 struct PointerGesture {
1410 enum Mode {
1411 // No fingers, button is not pressed.
1412 // Nothing happening.
1413 NEUTRAL,
1414
1415 // No fingers, button is not pressed.
1416 // Tap detected.
1417 // Emits DOWN and UP events at the pointer location.
1418 TAP,
1419
1420 // Exactly one finger dragging following a tap.
1421 // Pointer follows the active finger.
1422 // Emits DOWN, MOVE and UP events at the pointer location.
1423 //
1424 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1425 TAP_DRAG,
1426
1427 // Button is pressed.
1428 // Pointer follows the active finger if there is one. Other fingers are ignored.
1429 // Emits DOWN, MOVE and UP events at the pointer location.
1430 BUTTON_CLICK_OR_DRAG,
1431
1432 // Exactly one finger, button is not pressed.
1433 // Pointer follows the active finger.
1434 // Emits HOVER_MOVE events at the pointer location.
1435 //
1436 // Detect taps when the finger goes up while in HOVER mode.
1437 HOVER,
1438
1439 // Exactly two fingers but neither have moved enough to clearly indicate
1440 // whether a swipe or freeform gesture was intended. We consider the
1441 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1442 // Pointer does not move.
1443 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1444 PRESS,
1445
1446 // Exactly two fingers moving in the same direction, button is not pressed.
1447 // Pointer does not move.
1448 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1449 // follows the midpoint between both fingers.
1450 SWIPE,
1451
1452 // Two or more fingers moving in arbitrary directions, button is not pressed.
1453 // Pointer does not move.
1454 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1455 // each finger individually relative to the initial centroid of the finger.
1456 FREEFORM,
1457
1458 // Waiting for quiet time to end before starting the next gesture.
1459 QUIET,
1460 };
1461
1462 // Time the first finger went down.
1463 nsecs_t firstTouchTime;
1464
1465 // The active pointer id from the raw touch data.
1466 int32_t activeTouchId; // -1 if none
1467
1468 // The active pointer id from the gesture last delivered to the application.
1469 int32_t activeGestureId; // -1 if none
1470
1471 // Pointer coords and ids for the current and previous pointer gesture.
1472 Mode currentGestureMode;
1473 BitSet32 currentGestureIdBits;
1474 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1475 PointerProperties currentGestureProperties[MAX_POINTERS];
1476 PointerCoords currentGestureCoords[MAX_POINTERS];
1477
1478 Mode lastGestureMode;
1479 BitSet32 lastGestureIdBits;
1480 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1481 PointerProperties lastGestureProperties[MAX_POINTERS];
1482 PointerCoords lastGestureCoords[MAX_POINTERS];
1483
1484 // Time the pointer gesture last went down.
1485 nsecs_t downTime;
1486
1487 // Time when the pointer went down for a TAP.
1488 nsecs_t tapDownTime;
1489
1490 // Time when the pointer went up for a TAP.
1491 nsecs_t tapUpTime;
1492
1493 // Location of initial tap.
1494 float tapX, tapY;
1495
1496 // Time we started waiting for quiescence.
1497 nsecs_t quietTime;
1498
1499 // Reference points for multitouch gestures.
1500 float referenceTouchX; // reference touch X/Y coordinates in surface units
1501 float referenceTouchY;
1502 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1503 float referenceGestureY;
1504
1505 // Distance that each pointer has traveled which has not yet been
1506 // subsumed into the reference gesture position.
1507 BitSet32 referenceIdBits;
1508 struct Delta {
1509 float dx, dy;
1510 };
1511 Delta referenceDeltas[MAX_POINTER_ID + 1];
1512
1513 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1514 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1515
1516 // A velocity tracker for determining whether to switch active pointers during drags.
1517 VelocityTracker velocityTracker;
1518
1519 void reset() {
1520 firstTouchTime = LLONG_MIN;
1521 activeTouchId = -1;
1522 activeGestureId = -1;
1523 currentGestureMode = NEUTRAL;
1524 currentGestureIdBits.clear();
1525 lastGestureMode = NEUTRAL;
1526 lastGestureIdBits.clear();
1527 downTime = 0;
1528 velocityTracker.clear();
1529 resetTap();
1530 resetQuietTime();
1531 }
1532
1533 void resetTap() {
1534 tapDownTime = LLONG_MIN;
1535 tapUpTime = LLONG_MIN;
1536 }
1537
1538 void resetQuietTime() {
1539 quietTime = LLONG_MIN;
1540 }
1541 } mPointerGesture;
1542
1543 struct PointerSimple {
1544 PointerCoords currentCoords;
1545 PointerProperties currentProperties;
1546 PointerCoords lastCoords;
1547 PointerProperties lastProperties;
1548
1549 // True if the pointer is down.
1550 bool down;
1551
1552 // True if the pointer is hovering.
1553 bool hovering;
1554
1555 // Time the pointer last went down.
1556 nsecs_t downTime;
1557
1558 void reset() {
1559 currentCoords.clear();
1560 currentProperties.clear();
1561 lastCoords.clear();
1562 lastProperties.clear();
1563 down = false;
1564 hovering = false;
1565 downTime = 0;
1566 }
1567 } mPointerSimple;
1568
1569 // The pointer and scroll velocity controls.
1570 VelocityControl mPointerVelocityControl;
1571 VelocityControl mWheelXVelocityControl;
1572 VelocityControl mWheelYVelocityControl;
1573
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001574 // Latency statistics for touch events
1575 struct LatencyStatistics mStatistics;
1576
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001577 std::optional<DisplayViewport> findViewport();
1578
Michael Wright842500e2015-03-13 17:32:02 -07001579 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001580 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001581
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 void sync(nsecs_t when);
1583
1584 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001585 void processRawTouches(bool timeout);
1586 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1588 int32_t keyEventAction, int32_t keyEventFlags);
1589
1590 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1591 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1592 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001593 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1594 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1595 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001597 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598
1599 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1600 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1601
1602 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1603 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1604 bool preparePointerGestures(nsecs_t when,
1605 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1606 bool isTimeout);
1607
1608 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1609 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1610
1611 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1612 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1613
1614 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1615 bool down, bool hovering);
1616 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1617
Michael Wright842500e2015-03-13 17:32:02 -07001618 bool assignExternalStylusId(const RawState& state, bool timeout);
1619 void applyExternalStylusButtonState(nsecs_t when);
1620 void applyExternalStylusTouchState(nsecs_t when);
1621
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 // Dispatches a motion event.
1623 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1624 // method will take care of setting the index and transmuting the action to DOWN or UP
1625 // it is the first / last pointer to go down / up.
1626 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001627 int32_t action, int32_t actionButton,
1628 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 const PointerProperties* properties, const PointerCoords* coords,
1630 const uint32_t* idToIndex, BitSet32 idBits,
1631 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1632
1633 // Updates pointer coords and properties for pointers with specified ids that have moved.
1634 // Returns true if any of them changed.
1635 bool updateMovedPointers(const PointerProperties* inProperties,
1636 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1637 PointerProperties* outProperties, PointerCoords* outCoords,
1638 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1639
1640 bool isPointInsideSurface(int32_t x, int32_t y);
1641 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1642
Michael Wright842500e2015-03-13 17:32:02 -07001643 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001644
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001645 void reportEventForStatistics(nsecs_t evdevTime);
1646
Santos Cordonfa5cf462017-04-05 10:37:00 -07001647 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648};
1649
1650
1651class SingleTouchInputMapper : public TouchInputMapper {
1652public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001653 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 virtual ~SingleTouchInputMapper();
1655
1656 virtual void reset(nsecs_t when);
1657 virtual void process(const RawEvent* rawEvent);
1658
1659protected:
Michael Wright842500e2015-03-13 17:32:02 -07001660 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661 virtual void configureRawPointerAxes();
1662 virtual bool hasStylus() const;
1663
1664private:
1665 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1666};
1667
1668
1669class MultiTouchInputMapper : public TouchInputMapper {
1670public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001671 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672 virtual ~MultiTouchInputMapper();
1673
1674 virtual void reset(nsecs_t when);
1675 virtual void process(const RawEvent* rawEvent);
1676
1677protected:
Michael Wright842500e2015-03-13 17:32:02 -07001678 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 virtual void configureRawPointerAxes();
1680 virtual bool hasStylus() const;
1681
1682private:
1683 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1684
1685 // Specifies the pointer id bits that are in use, and their associated tracking id.
1686 BitSet32 mPointerIdBits;
1687 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1688};
1689
Michael Wright842500e2015-03-13 17:32:02 -07001690class ExternalStylusInputMapper : public InputMapper {
1691public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001692 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001693 virtual ~ExternalStylusInputMapper() = default;
1694
1695 virtual uint32_t getSources();
1696 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001697 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001698 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1699 virtual void reset(nsecs_t when);
1700 virtual void process(const RawEvent* rawEvent);
1701 virtual void sync(nsecs_t when);
1702
1703private:
1704 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1705 RawAbsoluteAxisInfo mRawPressureAxis;
1706 TouchButtonAccumulator mTouchButtonAccumulator;
1707
1708 StylusState mStylusState;
1709};
1710
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711
1712class JoystickInputMapper : public InputMapper {
1713public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001714 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001715 virtual ~JoystickInputMapper();
1716
1717 virtual uint32_t getSources();
1718 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001719 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1721 virtual void reset(nsecs_t when);
1722 virtual void process(const RawEvent* rawEvent);
1723
1724private:
1725 struct Axis {
1726 RawAbsoluteAxisInfo rawAxisInfo;
1727 AxisInfo axisInfo;
1728
1729 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1730
1731 float scale; // scale factor from raw to normalized values
1732 float offset; // offset to add after scaling for normalization
1733 float highScale; // scale factor from raw to normalized values of high split
1734 float highOffset; // offset to add after scaling for normalization of high split
1735
1736 float min; // normalized inclusive minimum
1737 float max; // normalized inclusive maximum
1738 float flat; // normalized flat region size
1739 float fuzz; // normalized error tolerance
1740 float resolution; // normalized resolution in units/mm
1741
1742 float filter; // filter out small variations of this size
1743 float currentValue; // current value
1744 float newValue; // most recent value
1745 float highCurrentValue; // current value of high split
1746 float highNewValue; // most recent value of high split
1747
1748 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1749 bool explicitlyMapped, float scale, float offset,
1750 float highScale, float highOffset,
1751 float min, float max, float flat, float fuzz, float resolution) {
1752 this->rawAxisInfo = rawAxisInfo;
1753 this->axisInfo = axisInfo;
1754 this->explicitlyMapped = explicitlyMapped;
1755 this->scale = scale;
1756 this->offset = offset;
1757 this->highScale = highScale;
1758 this->highOffset = highOffset;
1759 this->min = min;
1760 this->max = max;
1761 this->flat = flat;
1762 this->fuzz = fuzz;
1763 this->resolution = resolution;
1764 this->filter = 0;
1765 resetValue();
1766 }
1767
1768 void resetValue() {
1769 this->currentValue = 0;
1770 this->newValue = 0;
1771 this->highCurrentValue = 0;
1772 this->highNewValue = 0;
1773 }
1774 };
1775
1776 // Axes indexed by raw ABS_* axis index.
1777 KeyedVector<int32_t, Axis> mAxes;
1778
1779 void sync(nsecs_t when, bool force);
1780
1781 bool haveAxis(int32_t axisId);
1782 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1783 bool filterAxes(bool force);
1784
1785 static bool hasValueChangedSignificantly(float filter,
1786 float newValue, float currentValue, float min, float max);
1787 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1788 float newValue, float currentValue, float thresholdValue);
1789
1790 static bool isCenteredAxis(int32_t axis);
1791 static int32_t getCompatAxis(int32_t axis);
1792
1793 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1794 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1795 float value);
1796};
1797
1798} // namespace android
1799
1800#endif // _UI_INPUT_READER_H