blob: fed55ac0e6310ce28d4df0984d0abbb64d4ec120 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
20#include "EventHub.h"
21#include "PointerControllerInterface.h"
22#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include "InputReaderBase.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Santos Cordonfa5cf462017-04-05 10:37:00 -070025#include <input/DisplayViewport.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/Input.h>
27#include <input/VelocityControl.h>
28#include <input/VelocityTracker.h>
29#include <ui/DisplayInfo.h>
30#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070031#include <utils/Condition.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070032#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <utils/BitSet.h>
35
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010036#include <optional>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.h>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010039#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
43class InputDevice;
44class InputMapper;
45
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Michael Wright842500e2015-03-13 17:32:02 -070047struct StylusState {
48 /* Time the stylus event was received. */
49 nsecs_t when;
50 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
51 float pressure;
52 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
53 uint32_t buttons;
54 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
55 int32_t toolType;
56
57 void copyFrom(const StylusState& other) {
58 when = other.when;
59 pressure = other.pressure;
60 buttons = other.buttons;
61 toolType = other.toolType;
62 }
63
64 void clear() {
65 when = LLONG_MAX;
66 pressure = 0.f;
67 buttons = 0;
68 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
69 }
70};
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
73/* Internal interface used by individual input devices to access global input device state
74 * and parameters maintained by the input reader.
75 */
76class InputReaderContext {
77public:
78 InputReaderContext() { }
79 virtual ~InputReaderContext() { }
80
81 virtual void updateGlobalMetaState() = 0;
82 virtual int32_t getGlobalMetaState() = 0;
83
84 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
85 virtual bool shouldDropVirtualKey(nsecs_t now,
86 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
87
88 virtual void fadePointer() = 0;
89
90 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
91 virtual int32_t bumpGeneration() = 0;
92
Michael Wrightb85401d2015-04-17 18:35:15 +010093 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
94 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -070095
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 virtual InputReaderPolicyInterface* getPolicy() = 0;
97 virtual InputListenerInterface* getListener() = 0;
98 virtual EventHubInterface* getEventHub() = 0;
Prabir Pradhan42611e02018-11-27 14:04:02 -080099
100 virtual uint32_t getNextSequenceNum() = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101};
102
103
104/* The input reader reads raw event data from the event hub and processes it into input events
105 * that it sends to the input listener. Some functions of the input reader, such as early
106 * event filtering in low power states, are controlled by a separate policy object.
107 *
108 * The InputReader owns a collection of InputMappers. Most of the work it does happens
109 * on the input reader thread but the InputReader can receive queries from other system
110 * components running on arbitrary threads. To keep things manageable, the InputReader
111 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
112 * EventHub or the InputReaderPolicy but it is never held while calling into the
113 * InputListener.
114 */
115class InputReader : public InputReaderInterface {
116public:
117 InputReader(const sp<EventHubInterface>& eventHub,
118 const sp<InputReaderPolicyInterface>& policy,
119 const sp<InputListenerInterface>& listener);
120 virtual ~InputReader();
121
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800122 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 virtual void monitor();
124
125 virtual void loopOnce();
126
127 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
128
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700129 virtual bool isInputDeviceEnabled(int32_t deviceId);
130
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
132 int32_t scanCode);
133 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
134 int32_t keyCode);
135 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
136 int32_t sw);
137
Andrii Kulian763a3a42016-03-08 10:46:16 -0800138 virtual void toggleCapsLockState(int32_t deviceId);
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
142
143 virtual void requestRefreshConfiguration(uint32_t changes);
144
145 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
146 ssize_t repeat, int32_t token);
147 virtual void cancelVibrate(int32_t deviceId, int32_t token);
148
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();
Michael Wright842500e2015-03-13 17:32:02 -0700169 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
170 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();
214 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
215 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
222 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
223
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
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 inline bool isIgnored() { return mMappers.isEmpty(); }
270
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
334 Vector<InputMapper*> mMappers;
335
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]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800720 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721
722private:
723 int32_t mCurrentSlot;
724 Slot* mSlots;
725 size_t mSlotCount;
726 bool mUsingSlotsProtocol;
727 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800728 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729
730 void clearSlots(int32_t initialSlot);
731};
732
733
734/* An input mapper transforms raw input events into cooked event data.
735 * A single input device can have multiple associated input mappers in order to interpret
736 * different classes of events.
737 *
738 * InputMapper lifecycle:
739 * - create
740 * - configure with 0 changes
741 * - reset
742 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
743 * - reset
744 * - destroy
745 */
746class InputMapper {
747public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700748 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 virtual ~InputMapper();
750
751 inline InputDevice* getDevice() { return mDevice; }
752 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100753 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 inline InputReaderContext* getContext() { return mContext; }
755 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
756 inline InputListenerInterface* getListener() { return mContext->getListener(); }
757 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
758
759 virtual uint32_t getSources() = 0;
760 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800761 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
763 virtual void reset(nsecs_t when);
764 virtual void process(const RawEvent* rawEvent) = 0;
765 virtual void timeoutExpired(nsecs_t when);
766
767 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
768 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
769 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
770 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
771 const int32_t* keyCodes, uint8_t* outFlags);
772 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
773 int32_t token);
774 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800775 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776
777 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800778 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
Michael Wright842500e2015-03-13 17:32:02 -0700780 virtual void updateExternalStylusState(const StylusState& state);
781
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782 virtual void fadePointer();
Arthur Hungc23540e2018-11-29 20:42:11 +0800783 virtual std::optional<int32_t> getAssociatedDisplay() {
784 return std::nullopt;
785 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786protected:
787 InputDevice* mDevice;
788 InputReaderContext* mContext;
789
790 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
791 void bumpGeneration();
792
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800793 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800795 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796};
797
798
799class SwitchInputMapper : public InputMapper {
800public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700801 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 virtual ~SwitchInputMapper();
803
804 virtual uint32_t getSources();
805 virtual void process(const RawEvent* rawEvent);
806
807 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800808 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809
810private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700811 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812 uint32_t mUpdatedSwitchMask;
813
814 void processSwitch(int32_t switchCode, int32_t switchValue);
815 void sync(nsecs_t when);
816};
817
818
819class VibratorInputMapper : public InputMapper {
820public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700821 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 virtual ~VibratorInputMapper();
823
824 virtual uint32_t getSources();
825 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
826 virtual void process(const RawEvent* rawEvent);
827
828 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
829 int32_t token);
830 virtual void cancelVibrate(int32_t token);
831 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800832 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833
834private:
835 bool mVibrating;
836 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
837 size_t mPatternSize;
838 ssize_t mRepeat;
839 int32_t mToken;
840 ssize_t mIndex;
841 nsecs_t mNextStepTime;
842
843 void nextStep();
844 void stopVibrating();
845};
846
847
848class KeyboardInputMapper : public InputMapper {
849public:
850 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
851 virtual ~KeyboardInputMapper();
852
853 virtual uint32_t getSources();
854 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800855 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
857 virtual void reset(nsecs_t when);
858 virtual void process(const RawEvent* rawEvent);
859
860 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
861 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
862 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
863 const int32_t* keyCodes, uint8_t* outFlags);
864
865 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800866 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867
868private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100869 // The current viewport.
870 std::optional<DisplayViewport> mViewport;
871
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 struct KeyDown {
873 int32_t keyCode;
874 int32_t scanCode;
875 };
876
877 uint32_t mSource;
878 int32_t mKeyboardType;
879
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 Vector<KeyDown> mKeyDowns; // keys that are down
881 int32_t mMetaState;
882 nsecs_t mDownTime; // time of most recent key down
883
884 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
885
886 struct LedState {
887 bool avail; // led is available
888 bool on; // we think the led is currently on
889 };
890 LedState mCapsLockLedState;
891 LedState mNumLockLedState;
892 LedState mScrollLockLedState;
893
894 // Immutable configuration parameters.
895 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700897 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 } mParameters;
899
900 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800901 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100903 int32_t getOrientation();
904 int32_t getDisplayId();
905
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100907 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700909 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910
Andrii Kulian763a3a42016-03-08 10:46:16 -0800911 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
912
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 ssize_t findKeyDown(int32_t scanCode);
914
915 void resetLedState();
916 void initializeLedState(LedState& ledState, int32_t led);
917 void updateLedState(bool reset);
918 void updateLedStateForModifier(LedState& ledState, int32_t led,
919 int32_t modifier, bool reset);
920};
921
922
923class CursorInputMapper : public InputMapper {
924public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700925 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 virtual ~CursorInputMapper();
927
928 virtual uint32_t getSources();
929 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800930 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
932 virtual void reset(nsecs_t when);
933 virtual void process(const RawEvent* rawEvent);
934
935 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
936
937 virtual void fadePointer();
938
Arthur Hungc23540e2018-11-29 20:42:11 +0800939 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940private:
941 // Amount that trackball needs to move in order to generate a key event.
942 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
943
944 // Immutable configuration parameters.
945 struct Parameters {
946 enum Mode {
947 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800948 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 MODE_NAVIGATION,
950 };
951
952 Mode mode;
953 bool hasAssociatedDisplay;
954 bool orientationAware;
955 } mParameters;
956
957 CursorButtonAccumulator mCursorButtonAccumulator;
958 CursorMotionAccumulator mCursorMotionAccumulator;
959 CursorScrollAccumulator mCursorScrollAccumulator;
960
961 int32_t mSource;
962 float mXScale;
963 float mYScale;
964 float mXPrecision;
965 float mYPrecision;
966
967 float mVWheelScale;
968 float mHWheelScale;
969
970 // Velocity controls for mouse pointer and wheel movements.
971 // The controls for X and Y wheel movements are separate to keep them decoupled.
972 VelocityControl mPointerVelocityControl;
973 VelocityControl mWheelXVelocityControl;
974 VelocityControl mWheelYVelocityControl;
975
976 int32_t mOrientation;
977
978 sp<PointerControllerInterface> mPointerController;
979
980 int32_t mButtonState;
981 nsecs_t mDownTime;
982
983 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800984 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
986 void sync(nsecs_t when);
987};
988
989
Prashant Malani1941ff52015-08-11 18:29:28 -0700990class RotaryEncoderInputMapper : public InputMapper {
991public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700992 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700993 virtual ~RotaryEncoderInputMapper();
994
995 virtual uint32_t getSources();
996 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800997 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700998 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
999 virtual void reset(nsecs_t when);
1000 virtual void process(const RawEvent* rawEvent);
1001
1002private:
1003 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
1004
1005 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001006 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +01001007 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -07001008
1009 void sync(nsecs_t when);
1010};
1011
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012class TouchInputMapper : public InputMapper {
1013public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001014 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 virtual ~TouchInputMapper();
1016
1017 virtual uint32_t getSources();
1018 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001019 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1021 virtual void reset(nsecs_t when);
1022 virtual void process(const RawEvent* rawEvent);
1023
1024 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1025 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1026 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1027 const int32_t* keyCodes, uint8_t* outFlags);
1028
1029 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001030 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001032 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034protected:
1035 CursorButtonAccumulator mCursorButtonAccumulator;
1036 CursorScrollAccumulator mCursorScrollAccumulator;
1037 TouchButtonAccumulator mTouchButtonAccumulator;
1038
1039 struct VirtualKey {
1040 int32_t keyCode;
1041 int32_t scanCode;
1042 uint32_t flags;
1043
1044 // computed hit box, specified in touch screen coords based on known display size
1045 int32_t hitLeft;
1046 int32_t hitTop;
1047 int32_t hitRight;
1048 int32_t hitBottom;
1049
1050 inline bool isHit(int32_t x, int32_t y) const {
1051 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1052 }
1053 };
1054
1055 // Input sources and device mode.
1056 uint32_t mSource;
1057
1058 enum DeviceMode {
1059 DEVICE_MODE_DISABLED, // input is disabled
1060 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1061 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1062 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1063 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1064 };
1065 DeviceMode mDeviceMode;
1066
1067 // The reader's configuration.
1068 InputReaderConfiguration mConfig;
1069
1070 // Immutable configuration parameters.
1071 struct Parameters {
1072 enum DeviceType {
1073 DEVICE_TYPE_TOUCH_SCREEN,
1074 DEVICE_TYPE_TOUCH_PAD,
1075 DEVICE_TYPE_TOUCH_NAVIGATION,
1076 DEVICE_TYPE_POINTER,
1077 };
1078
1079 DeviceType deviceType;
1080 bool hasAssociatedDisplay;
1081 bool associatedDisplayIsExternal;
1082 bool orientationAware;
1083 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001084 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
1086 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001087 GESTURE_MODE_SINGLE_TOUCH,
1088 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 };
1090 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001091
1092 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093 } mParameters;
1094
1095 // Immutable calibration parameters in parsed form.
1096 struct Calibration {
1097 // Size
1098 enum SizeCalibration {
1099 SIZE_CALIBRATION_DEFAULT,
1100 SIZE_CALIBRATION_NONE,
1101 SIZE_CALIBRATION_GEOMETRIC,
1102 SIZE_CALIBRATION_DIAMETER,
1103 SIZE_CALIBRATION_BOX,
1104 SIZE_CALIBRATION_AREA,
1105 };
1106
1107 SizeCalibration sizeCalibration;
1108
1109 bool haveSizeScale;
1110 float sizeScale;
1111 bool haveSizeBias;
1112 float sizeBias;
1113 bool haveSizeIsSummed;
1114 bool sizeIsSummed;
1115
1116 // Pressure
1117 enum PressureCalibration {
1118 PRESSURE_CALIBRATION_DEFAULT,
1119 PRESSURE_CALIBRATION_NONE,
1120 PRESSURE_CALIBRATION_PHYSICAL,
1121 PRESSURE_CALIBRATION_AMPLITUDE,
1122 };
1123
1124 PressureCalibration pressureCalibration;
1125 bool havePressureScale;
1126 float pressureScale;
1127
1128 // Orientation
1129 enum OrientationCalibration {
1130 ORIENTATION_CALIBRATION_DEFAULT,
1131 ORIENTATION_CALIBRATION_NONE,
1132 ORIENTATION_CALIBRATION_INTERPOLATED,
1133 ORIENTATION_CALIBRATION_VECTOR,
1134 };
1135
1136 OrientationCalibration orientationCalibration;
1137
1138 // Distance
1139 enum DistanceCalibration {
1140 DISTANCE_CALIBRATION_DEFAULT,
1141 DISTANCE_CALIBRATION_NONE,
1142 DISTANCE_CALIBRATION_SCALED,
1143 };
1144
1145 DistanceCalibration distanceCalibration;
1146 bool haveDistanceScale;
1147 float distanceScale;
1148
1149 enum CoverageCalibration {
1150 COVERAGE_CALIBRATION_DEFAULT,
1151 COVERAGE_CALIBRATION_NONE,
1152 COVERAGE_CALIBRATION_BOX,
1153 };
1154
1155 CoverageCalibration coverageCalibration;
1156
1157 inline void applySizeScaleAndBias(float* outSize) const {
1158 if (haveSizeScale) {
1159 *outSize *= sizeScale;
1160 }
1161 if (haveSizeBias) {
1162 *outSize += sizeBias;
1163 }
1164 if (*outSize < 0) {
1165 *outSize = 0;
1166 }
1167 }
1168 } mCalibration;
1169
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001170 // Affine location transformation/calibration
1171 struct TouchAffineTransformation mAffineTransform;
1172
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 RawPointerAxes mRawPointerAxes;
1174
Michael Wright842500e2015-03-13 17:32:02 -07001175 struct RawState {
1176 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001177 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Michael Wright842500e2015-03-13 17:32:02 -07001179 // Raw pointer sample data.
1180 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181
Michael Wright842500e2015-03-13 17:32:02 -07001182 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183
Michael Wright842500e2015-03-13 17:32:02 -07001184 // Scroll state.
1185 int32_t rawVScroll;
1186 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187
Michael Wright842500e2015-03-13 17:32:02 -07001188 void copyFrom(const RawState& other) {
1189 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001190 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001191 rawPointerData.copyFrom(other.rawPointerData);
1192 buttonState = other.buttonState;
1193 rawVScroll = other.rawVScroll;
1194 rawHScroll = other.rawHScroll;
1195 }
1196
1197 void clear() {
1198 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001199 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001200 rawPointerData.clear();
1201 buttonState = 0;
1202 rawVScroll = 0;
1203 rawHScroll = 0;
1204 }
1205 };
1206
1207 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001208 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001209 // Cooked pointer sample data.
1210 CookedPointerData cookedPointerData;
1211
1212 // Id bits used to differentiate fingers, stylus and mouse tools.
1213 BitSet32 fingerIdBits;
1214 BitSet32 stylusIdBits;
1215 BitSet32 mouseIdBits;
1216
Michael Wright7b159c92015-05-14 14:48:03 +01001217 int32_t buttonState;
1218
Michael Wright842500e2015-03-13 17:32:02 -07001219 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001220 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001221 cookedPointerData.copyFrom(other.cookedPointerData);
1222 fingerIdBits = other.fingerIdBits;
1223 stylusIdBits = other.stylusIdBits;
1224 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001225 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001226 }
1227
1228 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001229 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001230 cookedPointerData.clear();
1231 fingerIdBits.clear();
1232 stylusIdBits.clear();
1233 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001234 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001235 }
1236 };
1237
1238 Vector<RawState> mRawStatesPending;
1239 RawState mCurrentRawState;
1240 CookedState mCurrentCookedState;
1241 RawState mLastRawState;
1242 CookedState mLastCookedState;
1243
1244 // State provided by an external stylus
1245 StylusState mExternalStylusState;
1246 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001247 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001248 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250 // True if we sent a HOVER_ENTER event.
1251 bool mSentHoverEnter;
1252
Michael Wright842500e2015-03-13 17:32:02 -07001253 // Have we assigned pointer IDs for this stream
1254 bool mHavePointerIds;
1255
Michael Wright8e812822015-06-22 16:18:21 +01001256 // Is the current stream of direct touch events aborted
1257 bool mCurrentMotionAborted;
1258
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 // The time the primary pointer last went down.
1260 nsecs_t mDownTime;
1261
1262 // The pointer controller, or null if the device is not a pointer.
1263 sp<PointerControllerInterface> mPointerController;
1264
1265 Vector<VirtualKey> mVirtualKeys;
1266
1267 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001268 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001270 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001272 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001274 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 virtual void parseCalibration();
1276 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001277 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001278 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001279 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001280 virtual void resolveExternalStylusPresence();
1281 virtual bool hasStylus() const = 0;
1282 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283
Michael Wright842500e2015-03-13 17:32:02 -07001284 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285
1286private:
1287 // The current viewport.
1288 // The components of the viewport are specified in the display's rotated orientation.
1289 DisplayViewport mViewport;
1290
1291 // The surface orientation, width and height set by configureSurface().
1292 // The width and height are derived from the viewport but are specified
1293 // in the natural orientation.
1294 // The surface origin specifies how the surface coordinates should be translated
1295 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296 int32_t mSurfaceWidth;
1297 int32_t mSurfaceHeight;
1298 int32_t mSurfaceLeft;
1299 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001300
1301 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1302 // the logical coordinate space.
1303 int32_t mPhysicalWidth;
1304 int32_t mPhysicalHeight;
1305 int32_t mPhysicalLeft;
1306 int32_t mPhysicalTop;
1307
1308 // The orientation may be different from the viewport orientation as it specifies
1309 // the rotation of the surface coordinates required to produce the viewport's
1310 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001311 int32_t mSurfaceOrientation;
1312
1313 // Translation and scaling factors, orientation-independent.
1314 float mXTranslate;
1315 float mXScale;
1316 float mXPrecision;
1317
1318 float mYTranslate;
1319 float mYScale;
1320 float mYPrecision;
1321
1322 float mGeometricScale;
1323
1324 float mPressureScale;
1325
1326 float mSizeScale;
1327
1328 float mOrientationScale;
1329
1330 float mDistanceScale;
1331
1332 bool mHaveTilt;
1333 float mTiltXCenter;
1334 float mTiltXScale;
1335 float mTiltYCenter;
1336 float mTiltYScale;
1337
Michael Wright842500e2015-03-13 17:32:02 -07001338 bool mExternalStylusConnected;
1339
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 // Oriented motion ranges for input device info.
1341 struct OrientedRanges {
1342 InputDeviceInfo::MotionRange x;
1343 InputDeviceInfo::MotionRange y;
1344 InputDeviceInfo::MotionRange pressure;
1345
1346 bool haveSize;
1347 InputDeviceInfo::MotionRange size;
1348
1349 bool haveTouchSize;
1350 InputDeviceInfo::MotionRange touchMajor;
1351 InputDeviceInfo::MotionRange touchMinor;
1352
1353 bool haveToolSize;
1354 InputDeviceInfo::MotionRange toolMajor;
1355 InputDeviceInfo::MotionRange toolMinor;
1356
1357 bool haveOrientation;
1358 InputDeviceInfo::MotionRange orientation;
1359
1360 bool haveDistance;
1361 InputDeviceInfo::MotionRange distance;
1362
1363 bool haveTilt;
1364 InputDeviceInfo::MotionRange tilt;
1365
1366 OrientedRanges() {
1367 clear();
1368 }
1369
1370 void clear() {
1371 haveSize = false;
1372 haveTouchSize = false;
1373 haveToolSize = false;
1374 haveOrientation = false;
1375 haveDistance = false;
1376 haveTilt = false;
1377 }
1378 } mOrientedRanges;
1379
1380 // Oriented dimensions and precision.
1381 float mOrientedXPrecision;
1382 float mOrientedYPrecision;
1383
1384 struct CurrentVirtualKeyState {
1385 bool down;
1386 bool ignored;
1387 nsecs_t downTime;
1388 int32_t keyCode;
1389 int32_t scanCode;
1390 } mCurrentVirtualKey;
1391
1392 // Scale factor for gesture or mouse based pointer movements.
1393 float mPointerXMovementScale;
1394 float mPointerYMovementScale;
1395
1396 // Scale factor for gesture based zooming and other freeform motions.
1397 float mPointerXZoomScale;
1398 float mPointerYZoomScale;
1399
1400 // The maximum swipe width.
1401 float mPointerGestureMaxSwipeWidth;
1402
1403 struct PointerDistanceHeapElement {
1404 uint32_t currentPointerIndex : 8;
1405 uint32_t lastPointerIndex : 8;
1406 uint64_t distance : 48; // squared distance
1407 };
1408
1409 enum PointerUsage {
1410 POINTER_USAGE_NONE,
1411 POINTER_USAGE_GESTURES,
1412 POINTER_USAGE_STYLUS,
1413 POINTER_USAGE_MOUSE,
1414 };
1415 PointerUsage mPointerUsage;
1416
1417 struct PointerGesture {
1418 enum Mode {
1419 // No fingers, button is not pressed.
1420 // Nothing happening.
1421 NEUTRAL,
1422
1423 // No fingers, button is not pressed.
1424 // Tap detected.
1425 // Emits DOWN and UP events at the pointer location.
1426 TAP,
1427
1428 // Exactly one finger dragging following a tap.
1429 // Pointer follows the active finger.
1430 // Emits DOWN, MOVE and UP events at the pointer location.
1431 //
1432 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1433 TAP_DRAG,
1434
1435 // Button is pressed.
1436 // Pointer follows the active finger if there is one. Other fingers are ignored.
1437 // Emits DOWN, MOVE and UP events at the pointer location.
1438 BUTTON_CLICK_OR_DRAG,
1439
1440 // Exactly one finger, button is not pressed.
1441 // Pointer follows the active finger.
1442 // Emits HOVER_MOVE events at the pointer location.
1443 //
1444 // Detect taps when the finger goes up while in HOVER mode.
1445 HOVER,
1446
1447 // Exactly two fingers but neither have moved enough to clearly indicate
1448 // whether a swipe or freeform gesture was intended. We consider the
1449 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1450 // Pointer does not move.
1451 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1452 PRESS,
1453
1454 // Exactly two fingers moving in the same direction, button is not pressed.
1455 // Pointer does not move.
1456 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1457 // follows the midpoint between both fingers.
1458 SWIPE,
1459
1460 // Two or more fingers moving in arbitrary directions, button is not pressed.
1461 // Pointer does not move.
1462 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1463 // each finger individually relative to the initial centroid of the finger.
1464 FREEFORM,
1465
1466 // Waiting for quiet time to end before starting the next gesture.
1467 QUIET,
1468 };
1469
1470 // Time the first finger went down.
1471 nsecs_t firstTouchTime;
1472
1473 // The active pointer id from the raw touch data.
1474 int32_t activeTouchId; // -1 if none
1475
1476 // The active pointer id from the gesture last delivered to the application.
1477 int32_t activeGestureId; // -1 if none
1478
1479 // Pointer coords and ids for the current and previous pointer gesture.
1480 Mode currentGestureMode;
1481 BitSet32 currentGestureIdBits;
1482 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1483 PointerProperties currentGestureProperties[MAX_POINTERS];
1484 PointerCoords currentGestureCoords[MAX_POINTERS];
1485
1486 Mode lastGestureMode;
1487 BitSet32 lastGestureIdBits;
1488 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1489 PointerProperties lastGestureProperties[MAX_POINTERS];
1490 PointerCoords lastGestureCoords[MAX_POINTERS];
1491
1492 // Time the pointer gesture last went down.
1493 nsecs_t downTime;
1494
1495 // Time when the pointer went down for a TAP.
1496 nsecs_t tapDownTime;
1497
1498 // Time when the pointer went up for a TAP.
1499 nsecs_t tapUpTime;
1500
1501 // Location of initial tap.
1502 float tapX, tapY;
1503
1504 // Time we started waiting for quiescence.
1505 nsecs_t quietTime;
1506
1507 // Reference points for multitouch gestures.
1508 float referenceTouchX; // reference touch X/Y coordinates in surface units
1509 float referenceTouchY;
1510 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1511 float referenceGestureY;
1512
1513 // Distance that each pointer has traveled which has not yet been
1514 // subsumed into the reference gesture position.
1515 BitSet32 referenceIdBits;
1516 struct Delta {
1517 float dx, dy;
1518 };
1519 Delta referenceDeltas[MAX_POINTER_ID + 1];
1520
1521 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1522 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1523
1524 // A velocity tracker for determining whether to switch active pointers during drags.
1525 VelocityTracker velocityTracker;
1526
1527 void reset() {
1528 firstTouchTime = LLONG_MIN;
1529 activeTouchId = -1;
1530 activeGestureId = -1;
1531 currentGestureMode = NEUTRAL;
1532 currentGestureIdBits.clear();
1533 lastGestureMode = NEUTRAL;
1534 lastGestureIdBits.clear();
1535 downTime = 0;
1536 velocityTracker.clear();
1537 resetTap();
1538 resetQuietTime();
1539 }
1540
1541 void resetTap() {
1542 tapDownTime = LLONG_MIN;
1543 tapUpTime = LLONG_MIN;
1544 }
1545
1546 void resetQuietTime() {
1547 quietTime = LLONG_MIN;
1548 }
1549 } mPointerGesture;
1550
1551 struct PointerSimple {
1552 PointerCoords currentCoords;
1553 PointerProperties currentProperties;
1554 PointerCoords lastCoords;
1555 PointerProperties lastProperties;
1556
1557 // True if the pointer is down.
1558 bool down;
1559
1560 // True if the pointer is hovering.
1561 bool hovering;
1562
1563 // Time the pointer last went down.
1564 nsecs_t downTime;
1565
1566 void reset() {
1567 currentCoords.clear();
1568 currentProperties.clear();
1569 lastCoords.clear();
1570 lastProperties.clear();
1571 down = false;
1572 hovering = false;
1573 downTime = 0;
1574 }
1575 } mPointerSimple;
1576
1577 // The pointer and scroll velocity controls.
1578 VelocityControl mPointerVelocityControl;
1579 VelocityControl mWheelXVelocityControl;
1580 VelocityControl mWheelYVelocityControl;
1581
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001582 // Latency statistics for touch events
1583 struct LatencyStatistics mStatistics;
1584
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001585 std::optional<DisplayViewport> findViewport();
1586
Michael Wright842500e2015-03-13 17:32:02 -07001587 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001588 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001589
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 void sync(nsecs_t when);
1591
1592 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001593 void processRawTouches(bool timeout);
1594 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1596 int32_t keyEventAction, int32_t keyEventFlags);
1597
1598 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1599 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1600 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001601 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1602 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1603 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001605 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606
1607 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1608 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1609
1610 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1611 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1612 bool preparePointerGestures(nsecs_t when,
1613 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1614 bool isTimeout);
1615
1616 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1617 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1618
1619 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1620 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1621
1622 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1623 bool down, bool hovering);
1624 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1625
Michael Wright842500e2015-03-13 17:32:02 -07001626 bool assignExternalStylusId(const RawState& state, bool timeout);
1627 void applyExternalStylusButtonState(nsecs_t when);
1628 void applyExternalStylusTouchState(nsecs_t when);
1629
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 // Dispatches a motion event.
1631 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1632 // method will take care of setting the index and transmuting the action to DOWN or UP
1633 // it is the first / last pointer to go down / up.
1634 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001635 int32_t action, int32_t actionButton,
1636 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001637 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638 const PointerProperties* properties, const PointerCoords* coords,
1639 const uint32_t* idToIndex, BitSet32 idBits,
1640 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1641
1642 // Updates pointer coords and properties for pointers with specified ids that have moved.
1643 // Returns true if any of them changed.
1644 bool updateMovedPointers(const PointerProperties* inProperties,
1645 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1646 PointerProperties* outProperties, PointerCoords* outCoords,
1647 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1648
1649 bool isPointInsideSurface(int32_t x, int32_t y);
1650 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1651
Michael Wright842500e2015-03-13 17:32:02 -07001652 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001653
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001654 void reportEventForStatistics(nsecs_t evdevTime);
1655
Santos Cordonfa5cf462017-04-05 10:37:00 -07001656 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657};
1658
1659
1660class SingleTouchInputMapper : public TouchInputMapper {
1661public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001662 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663 virtual ~SingleTouchInputMapper();
1664
1665 virtual void reset(nsecs_t when);
1666 virtual void process(const RawEvent* rawEvent);
1667
1668protected:
Michael Wright842500e2015-03-13 17:32:02 -07001669 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670 virtual void configureRawPointerAxes();
1671 virtual bool hasStylus() const;
1672
1673private:
1674 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1675};
1676
1677
1678class MultiTouchInputMapper : public TouchInputMapper {
1679public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001680 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681 virtual ~MultiTouchInputMapper();
1682
1683 virtual void reset(nsecs_t when);
1684 virtual void process(const RawEvent* rawEvent);
1685
1686protected:
Michael Wright842500e2015-03-13 17:32:02 -07001687 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001688 virtual void configureRawPointerAxes();
1689 virtual bool hasStylus() const;
1690
1691private:
1692 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1693
1694 // Specifies the pointer id bits that are in use, and their associated tracking id.
1695 BitSet32 mPointerIdBits;
1696 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1697};
1698
Michael Wright842500e2015-03-13 17:32:02 -07001699class ExternalStylusInputMapper : public InputMapper {
1700public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001701 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001702 virtual ~ExternalStylusInputMapper() = default;
1703
1704 virtual uint32_t getSources();
1705 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001706 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001707 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1708 virtual void reset(nsecs_t when);
1709 virtual void process(const RawEvent* rawEvent);
1710 virtual void sync(nsecs_t when);
1711
1712private:
1713 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1714 RawAbsoluteAxisInfo mRawPressureAxis;
1715 TouchButtonAccumulator mTouchButtonAccumulator;
1716
1717 StylusState mStylusState;
1718};
1719
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720
1721class JoystickInputMapper : public InputMapper {
1722public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001723 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001724 virtual ~JoystickInputMapper();
1725
1726 virtual uint32_t getSources();
1727 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001728 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1730 virtual void reset(nsecs_t when);
1731 virtual void process(const RawEvent* rawEvent);
1732
1733private:
1734 struct Axis {
1735 RawAbsoluteAxisInfo rawAxisInfo;
1736 AxisInfo axisInfo;
1737
1738 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1739
1740 float scale; // scale factor from raw to normalized values
1741 float offset; // offset to add after scaling for normalization
1742 float highScale; // scale factor from raw to normalized values of high split
1743 float highOffset; // offset to add after scaling for normalization of high split
1744
1745 float min; // normalized inclusive minimum
1746 float max; // normalized inclusive maximum
1747 float flat; // normalized flat region size
1748 float fuzz; // normalized error tolerance
1749 float resolution; // normalized resolution in units/mm
1750
1751 float filter; // filter out small variations of this size
1752 float currentValue; // current value
1753 float newValue; // most recent value
1754 float highCurrentValue; // current value of high split
1755 float highNewValue; // most recent value of high split
1756
1757 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1758 bool explicitlyMapped, float scale, float offset,
1759 float highScale, float highOffset,
1760 float min, float max, float flat, float fuzz, float resolution) {
1761 this->rawAxisInfo = rawAxisInfo;
1762 this->axisInfo = axisInfo;
1763 this->explicitlyMapped = explicitlyMapped;
1764 this->scale = scale;
1765 this->offset = offset;
1766 this->highScale = highScale;
1767 this->highOffset = highOffset;
1768 this->min = min;
1769 this->max = max;
1770 this->flat = flat;
1771 this->fuzz = fuzz;
1772 this->resolution = resolution;
1773 this->filter = 0;
1774 resetValue();
1775 }
1776
1777 void resetValue() {
1778 this->currentValue = 0;
1779 this->newValue = 0;
1780 this->highCurrentValue = 0;
1781 this->highNewValue = 0;
1782 }
1783 };
1784
1785 // Axes indexed by raw ABS_* axis index.
1786 KeyedVector<int32_t, Axis> mAxes;
1787
1788 void sync(nsecs_t when, bool force);
1789
1790 bool haveAxis(int32_t axisId);
1791 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1792 bool filterAxes(bool force);
1793
1794 static bool hasValueChangedSignificantly(float filter,
1795 float newValue, float currentValue, float min, float max);
1796 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1797 float newValue, float currentValue, float thresholdValue);
1798
1799 static bool isCenteredAxis(int32_t axis);
1800 static int32_t getCompatAxis(int32_t axis);
1801
1802 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1803 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1804 float value);
1805};
1806
1807} // namespace android
1808
1809#endif // _UI_INPUT_READER_H