blob: 35f3c232c7eac5995325983e784b4eae36cce305 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
20#include "EventHub.h"
21#include "PointerControllerInterface.h"
22#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include "InputReaderBase.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Santos Cordonfa5cf462017-04-05 10:37:00 -070025#include <input/DisplayViewport.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/Input.h>
27#include <input/VelocityControl.h>
28#include <input/VelocityTracker.h>
29#include <ui/DisplayInfo.h>
30#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070031#include <utils/Condition.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070032#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <utils/BitSet.h>
35
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010036#include <optional>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.h>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010039#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
43class InputDevice;
44class InputMapper;
45
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Michael Wright842500e2015-03-13 17:32:02 -070047struct StylusState {
48 /* Time the stylus event was received. */
49 nsecs_t when;
50 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
51 float pressure;
52 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
53 uint32_t buttons;
54 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
55 int32_t toolType;
56
57 void copyFrom(const StylusState& other) {
58 when = other.when;
59 pressure = other.pressure;
60 buttons = other.buttons;
61 toolType = other.toolType;
62 }
63
64 void clear() {
65 when = LLONG_MAX;
66 pressure = 0.f;
67 buttons = 0;
68 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
69 }
70};
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
73/* Internal interface used by individual input devices to access global input device state
74 * and parameters maintained by the input reader.
75 */
76class InputReaderContext {
77public:
78 InputReaderContext() { }
79 virtual ~InputReaderContext() { }
80
81 virtual void updateGlobalMetaState() = 0;
82 virtual int32_t getGlobalMetaState() = 0;
83
84 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
85 virtual bool shouldDropVirtualKey(nsecs_t now,
86 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
87
88 virtual void fadePointer() = 0;
89
90 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
91 virtual int32_t bumpGeneration() = 0;
92
Michael Wrightb85401d2015-04-17 18:35:15 +010093 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
94 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -070095
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 virtual InputReaderPolicyInterface* getPolicy() = 0;
97 virtual InputListenerInterface* getListener() = 0;
98 virtual EventHubInterface* getEventHub() = 0;
Prabir Pradhan42611e02018-11-27 14:04:02 -080099
100 virtual uint32_t getNextSequenceNum() = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101};
102
103
104/* The input reader reads raw event data from the event hub and processes it into input events
105 * that it sends to the input listener. Some functions of the input reader, such as early
106 * event filtering in low power states, are controlled by a separate policy object.
107 *
108 * The InputReader owns a collection of InputMappers. Most of the work it does happens
109 * on the input reader thread but the InputReader can receive queries from other system
110 * components running on arbitrary threads. To keep things manageable, the InputReader
111 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
112 * EventHub or the InputReaderPolicy but it is never held while calling into the
113 * InputListener.
114 */
115class InputReader : public InputReaderInterface {
116public:
117 InputReader(const sp<EventHubInterface>& eventHub,
118 const sp<InputReaderPolicyInterface>& policy,
119 const sp<InputListenerInterface>& listener);
120 virtual ~InputReader();
121
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800122 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 virtual void monitor();
124
125 virtual void loopOnce();
126
127 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
128
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700129 virtual bool isInputDeviceEnabled(int32_t deviceId);
130
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
132 int32_t scanCode);
133 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
134 int32_t keyCode);
135 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
136 int32_t sw);
137
Andrii Kulian763a3a42016-03-08 10:46:16 -0800138 virtual void toggleCapsLockState(int32_t deviceId);
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
142
143 virtual void requestRefreshConfiguration(uint32_t changes);
144
145 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
146 ssize_t repeat, int32_t token);
147 virtual void cancelVibrate(int32_t deviceId, int32_t token);
148
149protected:
150 // These members are protected so they can be instrumented by test cases.
151 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
152 const InputDeviceIdentifier& identifier, uint32_t classes);
153
154 class ContextImpl : public InputReaderContext {
155 InputReader* mReader;
156
157 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700158 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
160 virtual void updateGlobalMetaState();
161 virtual int32_t getGlobalMetaState();
162 virtual void disableVirtualKeysUntil(nsecs_t time);
163 virtual bool shouldDropVirtualKey(nsecs_t now,
164 InputDevice* device, int32_t keyCode, int32_t scanCode);
165 virtual void fadePointer();
166 virtual void requestTimeoutAtTime(nsecs_t when);
167 virtual int32_t bumpGeneration();
Michael Wright842500e2015-03-13 17:32:02 -0700168 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
169 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 virtual InputReaderPolicyInterface* getPolicy();
171 virtual InputListenerInterface* getListener();
172 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800173 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 } mContext;
175
176 friend class ContextImpl;
177
178private:
179 Mutex mLock;
180
181 Condition mReaderIsAliveCondition;
182
183 sp<EventHubInterface> mEventHub;
184 sp<InputReaderPolicyInterface> mPolicy;
185 sp<QueuedInputListener> mQueuedListener;
186
187 InputReaderConfiguration mConfig;
188
Prabir Pradhan42611e02018-11-27 14:04:02 -0800189 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
190 uint32_t mNextSequenceNum;
191
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 // The event queue.
193 static const int EVENT_BUFFER_SIZE = 256;
194 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
195
196 KeyedVector<int32_t, InputDevice*> mDevices;
197
198 // low-level input event decoding and device management
199 void processEventsLocked(const RawEvent* rawEvents, size_t count);
200
201 void addDeviceLocked(nsecs_t when, int32_t deviceId);
202 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
203 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
204 void timeoutExpiredLocked(nsecs_t when);
205
206 void handleConfigurationChangedLocked(nsecs_t when);
207
208 int32_t mGlobalMetaState;
209 void updateGlobalMetaStateLocked();
210 int32_t getGlobalMetaStateLocked();
211
Michael Wright842500e2015-03-13 17:32:02 -0700212 void notifyExternalStylusPresenceChanged();
213 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
214 void dispatchExternalStylusState(const StylusState& state);
215
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 void fadePointerLocked();
217
218 int32_t mGeneration;
219 int32_t bumpGenerationLocked();
220
221 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
222
223 nsecs_t mDisableVirtualKeysTimeout;
224 void disableVirtualKeysUntilLocked(nsecs_t time);
225 bool shouldDropVirtualKeyLocked(nsecs_t now,
226 InputDevice* device, int32_t keyCode, int32_t scanCode);
227
228 nsecs_t mNextTimeout;
229 void requestTimeoutAtTimeLocked(nsecs_t when);
230
231 uint32_t mConfigurationChangesToRefresh;
232 void refreshConfigurationLocked(uint32_t changes);
233
234 // state queries
235 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
236 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
237 GetStateFunc getStateFunc);
238 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
239 const int32_t* keyCodes, uint8_t* outFlags);
240};
241
242
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243/* Represents the state of a single input device. */
244class InputDevice {
245public:
246 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
247 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
248 ~InputDevice();
249
250 inline InputReaderContext* getContext() { return mContext; }
251 inline int32_t getId() const { return mId; }
252 inline int32_t getControllerNumber() const { return mControllerNumber; }
253 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100254 inline const std::string getName() const { return mIdentifier.name; }
255 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 inline uint32_t getClasses() const { return mClasses; }
257 inline uint32_t getSources() const { return mSources; }
258
259 inline bool isExternal() { return mIsExternal; }
260 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700261 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
262 return mAssociatedDisplayPort;
263 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264
Tim Kilbourn063ff532015-04-08 10:26:18 -0700265 inline void setMic(bool hasMic) { mHasMic = hasMic; }
266 inline bool hasMic() const { return mHasMic; }
267
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 inline bool isIgnored() { return mMappers.isEmpty(); }
269
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700270 bool isEnabled();
271 void setEnabled(bool enabled, nsecs_t when);
272
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800273 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 void addMapper(InputMapper* mapper);
275 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
276 void reset(nsecs_t when);
277 void process(const RawEvent* rawEvents, size_t count);
278 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700279 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280
281 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
282 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
283 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
284 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
285 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
286 const int32_t* keyCodes, uint8_t* outFlags);
287 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
288 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800289 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290
291 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800292 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293
294 void fadePointer();
295
296 void bumpGeneration();
297
298 void notifyReset(nsecs_t when);
299
300 inline const PropertyMap& getConfiguration() { return mConfiguration; }
301 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
302
303 bool hasKey(int32_t code) {
304 return getEventHub()->hasScanCode(mId, code);
305 }
306
307 bool hasAbsoluteAxis(int32_t code) {
308 RawAbsoluteAxisInfo info;
309 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
310 return info.valid;
311 }
312
313 bool isKeyPressed(int32_t code) {
314 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
315 }
316
317 int32_t getAbsoluteAxisValue(int32_t code) {
318 int32_t value;
319 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
320 return value;
321 }
322
323private:
324 InputReaderContext* mContext;
325 int32_t mId;
326 int32_t mGeneration;
327 int32_t mControllerNumber;
328 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100329 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 uint32_t mClasses;
331
332 Vector<InputMapper*> mMappers;
333
334 uint32_t mSources;
335 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700336 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700337 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 bool mDropUntilNextSync;
339
340 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
341 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
342
343 PropertyMap mConfiguration;
344};
345
346
347/* Keeps track of the state of mouse or touch pad buttons. */
348class CursorButtonAccumulator {
349public:
350 CursorButtonAccumulator();
351 void reset(InputDevice* device);
352
353 void process(const RawEvent* rawEvent);
354
355 uint32_t getButtonState() const;
356
357private:
358 bool mBtnLeft;
359 bool mBtnRight;
360 bool mBtnMiddle;
361 bool mBtnBack;
362 bool mBtnSide;
363 bool mBtnForward;
364 bool mBtnExtra;
365 bool mBtnTask;
366
367 void clearButtons();
368};
369
370
371/* Keeps track of cursor movements. */
372
373class CursorMotionAccumulator {
374public:
375 CursorMotionAccumulator();
376 void reset(InputDevice* device);
377
378 void process(const RawEvent* rawEvent);
379 void finishSync();
380
381 inline int32_t getRelativeX() const { return mRelX; }
382 inline int32_t getRelativeY() const { return mRelY; }
383
384private:
385 int32_t mRelX;
386 int32_t mRelY;
387
388 void clearRelativeAxes();
389};
390
391
392/* Keeps track of cursor scrolling motions. */
393
394class CursorScrollAccumulator {
395public:
396 CursorScrollAccumulator();
397 void configure(InputDevice* device);
398 void reset(InputDevice* device);
399
400 void process(const RawEvent* rawEvent);
401 void finishSync();
402
403 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
404 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
405
406 inline int32_t getRelativeX() const { return mRelX; }
407 inline int32_t getRelativeY() const { return mRelY; }
408 inline int32_t getRelativeVWheel() const { return mRelWheel; }
409 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
410
411private:
412 bool mHaveRelWheel;
413 bool mHaveRelHWheel;
414
415 int32_t mRelX;
416 int32_t mRelY;
417 int32_t mRelWheel;
418 int32_t mRelHWheel;
419
420 void clearRelativeAxes();
421};
422
423
424/* Keeps track of the state of touch, stylus and tool buttons. */
425class TouchButtonAccumulator {
426public:
427 TouchButtonAccumulator();
428 void configure(InputDevice* device);
429 void reset(InputDevice* device);
430
431 void process(const RawEvent* rawEvent);
432
433 uint32_t getButtonState() const;
434 int32_t getToolType() const;
435 bool isToolActive() const;
436 bool isHovering() const;
437 bool hasStylus() const;
438
439private:
440 bool mHaveBtnTouch;
441 bool mHaveStylus;
442
443 bool mBtnTouch;
444 bool mBtnStylus;
445 bool mBtnStylus2;
446 bool mBtnToolFinger;
447 bool mBtnToolPen;
448 bool mBtnToolRubber;
449 bool mBtnToolBrush;
450 bool mBtnToolPencil;
451 bool mBtnToolAirbrush;
452 bool mBtnToolMouse;
453 bool mBtnToolLens;
454 bool mBtnToolDoubleTap;
455 bool mBtnToolTripleTap;
456 bool mBtnToolQuadTap;
457
458 void clearButtons();
459};
460
461
462/* Raw axis information from the driver. */
463struct RawPointerAxes {
464 RawAbsoluteAxisInfo x;
465 RawAbsoluteAxisInfo y;
466 RawAbsoluteAxisInfo pressure;
467 RawAbsoluteAxisInfo touchMajor;
468 RawAbsoluteAxisInfo touchMinor;
469 RawAbsoluteAxisInfo toolMajor;
470 RawAbsoluteAxisInfo toolMinor;
471 RawAbsoluteAxisInfo orientation;
472 RawAbsoluteAxisInfo distance;
473 RawAbsoluteAxisInfo tiltX;
474 RawAbsoluteAxisInfo tiltY;
475 RawAbsoluteAxisInfo trackingId;
476 RawAbsoluteAxisInfo slot;
477
478 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800479 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
480 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 void clear();
482};
483
484
485/* Raw data for a collection of pointers including a pointer id mapping table. */
486struct RawPointerData {
487 struct Pointer {
488 uint32_t id;
489 int32_t x;
490 int32_t y;
491 int32_t pressure;
492 int32_t touchMajor;
493 int32_t touchMinor;
494 int32_t toolMajor;
495 int32_t toolMinor;
496 int32_t orientation;
497 int32_t distance;
498 int32_t tiltX;
499 int32_t tiltY;
500 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
501 bool isHovering;
502 };
503
504 uint32_t pointerCount;
505 Pointer pointers[MAX_POINTERS];
506 BitSet32 hoveringIdBits, touchingIdBits;
507 uint32_t idToIndex[MAX_POINTER_ID + 1];
508
509 RawPointerData();
510 void clear();
511 void copyFrom(const RawPointerData& other);
512 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
513
514 inline void markIdBit(uint32_t id, bool isHovering) {
515 if (isHovering) {
516 hoveringIdBits.markBit(id);
517 } else {
518 touchingIdBits.markBit(id);
519 }
520 }
521
522 inline void clearIdBits() {
523 hoveringIdBits.clear();
524 touchingIdBits.clear();
525 }
526
527 inline const Pointer& pointerForId(uint32_t id) const {
528 return pointers[idToIndex[id]];
529 }
530
531 inline bool isHovering(uint32_t pointerIndex) {
532 return pointers[pointerIndex].isHovering;
533 }
534};
535
536
537/* Cooked data for a collection of pointers including a pointer id mapping table. */
538struct CookedPointerData {
539 uint32_t pointerCount;
540 PointerProperties pointerProperties[MAX_POINTERS];
541 PointerCoords pointerCoords[MAX_POINTERS];
542 BitSet32 hoveringIdBits, touchingIdBits;
543 uint32_t idToIndex[MAX_POINTER_ID + 1];
544
545 CookedPointerData();
546 void clear();
547 void copyFrom(const CookedPointerData& other);
548
549 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
550 return pointerCoords[idToIndex[id]];
551 }
552
Michael Wright842500e2015-03-13 17:32:02 -0700553 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
554 return pointerCoords[idToIndex[id]];
555 }
556
557 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
558 return pointerProperties[idToIndex[id]];
559 }
560
Michael Wright53dca3a2015-04-23 17:39:53 +0100561 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
563 }
Michael Wright842500e2015-03-13 17:32:02 -0700564
Michael Wright53dca3a2015-04-23 17:39:53 +0100565 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700566 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
567 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568};
569
570
571/* Keeps track of the state of single-touch protocol. */
572class SingleTouchMotionAccumulator {
573public:
574 SingleTouchMotionAccumulator();
575
576 void process(const RawEvent* rawEvent);
577 void reset(InputDevice* device);
578
579 inline int32_t getAbsoluteX() const { return mAbsX; }
580 inline int32_t getAbsoluteY() const { return mAbsY; }
581 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
582 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
583 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
584 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
585 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
586
587private:
588 int32_t mAbsX;
589 int32_t mAbsY;
590 int32_t mAbsPressure;
591 int32_t mAbsToolWidth;
592 int32_t mAbsDistance;
593 int32_t mAbsTiltX;
594 int32_t mAbsTiltY;
595
596 void clearAbsoluteAxes();
597};
598
599
600/* Keeps track of the state of multi-touch protocol. */
601class MultiTouchMotionAccumulator {
602public:
603 class Slot {
604 public:
605 inline bool isInUse() const { return mInUse; }
606 inline int32_t getX() const { return mAbsMTPositionX; }
607 inline int32_t getY() const { return mAbsMTPositionY; }
608 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
609 inline int32_t getTouchMinor() const {
610 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
611 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
612 inline int32_t getToolMinor() const {
613 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
614 inline int32_t getOrientation() const { return mAbsMTOrientation; }
615 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
616 inline int32_t getPressure() const { return mAbsMTPressure; }
617 inline int32_t getDistance() const { return mAbsMTDistance; }
618 inline int32_t getToolType() const;
619
620 private:
621 friend class MultiTouchMotionAccumulator;
622
623 bool mInUse;
624 bool mHaveAbsMTTouchMinor;
625 bool mHaveAbsMTWidthMinor;
626 bool mHaveAbsMTToolType;
627
628 int32_t mAbsMTPositionX;
629 int32_t mAbsMTPositionY;
630 int32_t mAbsMTTouchMajor;
631 int32_t mAbsMTTouchMinor;
632 int32_t mAbsMTWidthMajor;
633 int32_t mAbsMTWidthMinor;
634 int32_t mAbsMTOrientation;
635 int32_t mAbsMTTrackingId;
636 int32_t mAbsMTPressure;
637 int32_t mAbsMTDistance;
638 int32_t mAbsMTToolType;
639
640 Slot();
641 void clear();
642 };
643
644 MultiTouchMotionAccumulator();
645 ~MultiTouchMotionAccumulator();
646
647 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
648 void reset(InputDevice* device);
649 void process(const RawEvent* rawEvent);
650 void finishSync();
651 bool hasStylus() const;
652
653 inline size_t getSlotCount() const { return mSlotCount; }
654 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800655 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
657private:
658 int32_t mCurrentSlot;
659 Slot* mSlots;
660 size_t mSlotCount;
661 bool mUsingSlotsProtocol;
662 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800663 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664
665 void clearSlots(int32_t initialSlot);
666};
667
668
669/* An input mapper transforms raw input events into cooked event data.
670 * A single input device can have multiple associated input mappers in order to interpret
671 * different classes of events.
672 *
673 * InputMapper lifecycle:
674 * - create
675 * - configure with 0 changes
676 * - reset
677 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
678 * - reset
679 * - destroy
680 */
681class InputMapper {
682public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700683 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 virtual ~InputMapper();
685
686 inline InputDevice* getDevice() { return mDevice; }
687 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100688 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 inline InputReaderContext* getContext() { return mContext; }
690 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
691 inline InputListenerInterface* getListener() { return mContext->getListener(); }
692 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
693
694 virtual uint32_t getSources() = 0;
695 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800696 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
698 virtual void reset(nsecs_t when);
699 virtual void process(const RawEvent* rawEvent) = 0;
700 virtual void timeoutExpired(nsecs_t when);
701
702 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
703 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
704 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
705 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
706 const int32_t* keyCodes, uint8_t* outFlags);
707 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
708 int32_t token);
709 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800710 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711
712 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800713 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714
Michael Wright842500e2015-03-13 17:32:02 -0700715 virtual void updateExternalStylusState(const StylusState& state);
716
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 virtual void fadePointer();
718
719protected:
720 InputDevice* mDevice;
721 InputReaderContext* mContext;
722
723 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
724 void bumpGeneration();
725
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800726 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800728 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729};
730
731
732class SwitchInputMapper : public InputMapper {
733public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700734 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 virtual ~SwitchInputMapper();
736
737 virtual uint32_t getSources();
738 virtual void process(const RawEvent* rawEvent);
739
740 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800741 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742
743private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700744 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 uint32_t mUpdatedSwitchMask;
746
747 void processSwitch(int32_t switchCode, int32_t switchValue);
748 void sync(nsecs_t when);
749};
750
751
752class VibratorInputMapper : public InputMapper {
753public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700754 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 virtual ~VibratorInputMapper();
756
757 virtual uint32_t getSources();
758 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
759 virtual void process(const RawEvent* rawEvent);
760
761 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
762 int32_t token);
763 virtual void cancelVibrate(int32_t token);
764 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800765 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766
767private:
768 bool mVibrating;
769 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
770 size_t mPatternSize;
771 ssize_t mRepeat;
772 int32_t mToken;
773 ssize_t mIndex;
774 nsecs_t mNextStepTime;
775
776 void nextStep();
777 void stopVibrating();
778};
779
780
781class KeyboardInputMapper : public InputMapper {
782public:
783 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
784 virtual ~KeyboardInputMapper();
785
786 virtual uint32_t getSources();
787 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800788 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
790 virtual void reset(nsecs_t when);
791 virtual void process(const RawEvent* rawEvent);
792
793 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
794 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
795 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
796 const int32_t* keyCodes, uint8_t* outFlags);
797
798 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800799 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800
801private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100802 // The current viewport.
803 std::optional<DisplayViewport> mViewport;
804
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 struct KeyDown {
806 int32_t keyCode;
807 int32_t scanCode;
808 };
809
810 uint32_t mSource;
811 int32_t mKeyboardType;
812
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 Vector<KeyDown> mKeyDowns; // keys that are down
814 int32_t mMetaState;
815 nsecs_t mDownTime; // time of most recent key down
816
817 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
818
819 struct LedState {
820 bool avail; // led is available
821 bool on; // we think the led is currently on
822 };
823 LedState mCapsLockLedState;
824 LedState mNumLockLedState;
825 LedState mScrollLockLedState;
826
827 // Immutable configuration parameters.
828 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700830 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 } mParameters;
832
833 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800834 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100836 int32_t getOrientation();
837 int32_t getDisplayId();
838
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100840 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700842 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
Andrii Kulian763a3a42016-03-08 10:46:16 -0800844 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
845
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846 ssize_t findKeyDown(int32_t scanCode);
847
848 void resetLedState();
849 void initializeLedState(LedState& ledState, int32_t led);
850 void updateLedState(bool reset);
851 void updateLedStateForModifier(LedState& ledState, int32_t led,
852 int32_t modifier, bool reset);
853};
854
855
856class CursorInputMapper : public InputMapper {
857public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700858 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859 virtual ~CursorInputMapper();
860
861 virtual uint32_t getSources();
862 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800863 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
865 virtual void reset(nsecs_t when);
866 virtual void process(const RawEvent* rawEvent);
867
868 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
869
870 virtual void fadePointer();
871
872private:
873 // Amount that trackball needs to move in order to generate a key event.
874 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
875
876 // Immutable configuration parameters.
877 struct Parameters {
878 enum Mode {
879 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800880 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 MODE_NAVIGATION,
882 };
883
884 Mode mode;
885 bool hasAssociatedDisplay;
886 bool orientationAware;
887 } mParameters;
888
889 CursorButtonAccumulator mCursorButtonAccumulator;
890 CursorMotionAccumulator mCursorMotionAccumulator;
891 CursorScrollAccumulator mCursorScrollAccumulator;
892
893 int32_t mSource;
894 float mXScale;
895 float mYScale;
896 float mXPrecision;
897 float mYPrecision;
898
899 float mVWheelScale;
900 float mHWheelScale;
901
902 // Velocity controls for mouse pointer and wheel movements.
903 // The controls for X and Y wheel movements are separate to keep them decoupled.
904 VelocityControl mPointerVelocityControl;
905 VelocityControl mWheelXVelocityControl;
906 VelocityControl mWheelYVelocityControl;
907
908 int32_t mOrientation;
909
910 sp<PointerControllerInterface> mPointerController;
911
912 int32_t mButtonState;
913 nsecs_t mDownTime;
914
915 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800916 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917
918 void sync(nsecs_t when);
919};
920
921
Prashant Malani1941ff52015-08-11 18:29:28 -0700922class RotaryEncoderInputMapper : public InputMapper {
923public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700924 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700925 virtual ~RotaryEncoderInputMapper();
926
927 virtual uint32_t getSources();
928 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800929 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700930 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
931 virtual void reset(nsecs_t when);
932 virtual void process(const RawEvent* rawEvent);
933
934private:
935 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
936
937 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -0800938 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +0100939 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -0700940
941 void sync(nsecs_t when);
942};
943
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944class TouchInputMapper : public InputMapper {
945public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700946 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 virtual ~TouchInputMapper();
948
949 virtual uint32_t getSources();
950 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800951 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
953 virtual void reset(nsecs_t when);
954 virtual void process(const RawEvent* rawEvent);
955
956 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
957 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
958 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
959 const int32_t* keyCodes, uint8_t* outFlags);
960
961 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -0800962 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700964 virtual void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966protected:
967 CursorButtonAccumulator mCursorButtonAccumulator;
968 CursorScrollAccumulator mCursorScrollAccumulator;
969 TouchButtonAccumulator mTouchButtonAccumulator;
970
971 struct VirtualKey {
972 int32_t keyCode;
973 int32_t scanCode;
974 uint32_t flags;
975
976 // computed hit box, specified in touch screen coords based on known display size
977 int32_t hitLeft;
978 int32_t hitTop;
979 int32_t hitRight;
980 int32_t hitBottom;
981
982 inline bool isHit(int32_t x, int32_t y) const {
983 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
984 }
985 };
986
987 // Input sources and device mode.
988 uint32_t mSource;
989
990 enum DeviceMode {
991 DEVICE_MODE_DISABLED, // input is disabled
992 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
993 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
994 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
995 DEVICE_MODE_POINTER, // pointer mapping (pointer)
996 };
997 DeviceMode mDeviceMode;
998
999 // The reader's configuration.
1000 InputReaderConfiguration mConfig;
1001
1002 // Immutable configuration parameters.
1003 struct Parameters {
1004 enum DeviceType {
1005 DEVICE_TYPE_TOUCH_SCREEN,
1006 DEVICE_TYPE_TOUCH_PAD,
1007 DEVICE_TYPE_TOUCH_NAVIGATION,
1008 DEVICE_TYPE_POINTER,
1009 };
1010
1011 DeviceType deviceType;
1012 bool hasAssociatedDisplay;
1013 bool associatedDisplayIsExternal;
1014 bool orientationAware;
1015 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001016 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017
1018 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001019 GESTURE_MODE_SINGLE_TOUCH,
1020 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 };
1022 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001023
1024 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 } mParameters;
1026
1027 // Immutable calibration parameters in parsed form.
1028 struct Calibration {
1029 // Size
1030 enum SizeCalibration {
1031 SIZE_CALIBRATION_DEFAULT,
1032 SIZE_CALIBRATION_NONE,
1033 SIZE_CALIBRATION_GEOMETRIC,
1034 SIZE_CALIBRATION_DIAMETER,
1035 SIZE_CALIBRATION_BOX,
1036 SIZE_CALIBRATION_AREA,
1037 };
1038
1039 SizeCalibration sizeCalibration;
1040
1041 bool haveSizeScale;
1042 float sizeScale;
1043 bool haveSizeBias;
1044 float sizeBias;
1045 bool haveSizeIsSummed;
1046 bool sizeIsSummed;
1047
1048 // Pressure
1049 enum PressureCalibration {
1050 PRESSURE_CALIBRATION_DEFAULT,
1051 PRESSURE_CALIBRATION_NONE,
1052 PRESSURE_CALIBRATION_PHYSICAL,
1053 PRESSURE_CALIBRATION_AMPLITUDE,
1054 };
1055
1056 PressureCalibration pressureCalibration;
1057 bool havePressureScale;
1058 float pressureScale;
1059
1060 // Orientation
1061 enum OrientationCalibration {
1062 ORIENTATION_CALIBRATION_DEFAULT,
1063 ORIENTATION_CALIBRATION_NONE,
1064 ORIENTATION_CALIBRATION_INTERPOLATED,
1065 ORIENTATION_CALIBRATION_VECTOR,
1066 };
1067
1068 OrientationCalibration orientationCalibration;
1069
1070 // Distance
1071 enum DistanceCalibration {
1072 DISTANCE_CALIBRATION_DEFAULT,
1073 DISTANCE_CALIBRATION_NONE,
1074 DISTANCE_CALIBRATION_SCALED,
1075 };
1076
1077 DistanceCalibration distanceCalibration;
1078 bool haveDistanceScale;
1079 float distanceScale;
1080
1081 enum CoverageCalibration {
1082 COVERAGE_CALIBRATION_DEFAULT,
1083 COVERAGE_CALIBRATION_NONE,
1084 COVERAGE_CALIBRATION_BOX,
1085 };
1086
1087 CoverageCalibration coverageCalibration;
1088
1089 inline void applySizeScaleAndBias(float* outSize) const {
1090 if (haveSizeScale) {
1091 *outSize *= sizeScale;
1092 }
1093 if (haveSizeBias) {
1094 *outSize += sizeBias;
1095 }
1096 if (*outSize < 0) {
1097 *outSize = 0;
1098 }
1099 }
1100 } mCalibration;
1101
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001102 // Affine location transformation/calibration
1103 struct TouchAffineTransformation mAffineTransform;
1104
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 RawPointerAxes mRawPointerAxes;
1106
Michael Wright842500e2015-03-13 17:32:02 -07001107 struct RawState {
1108 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001109 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
Michael Wright842500e2015-03-13 17:32:02 -07001111 // Raw pointer sample data.
1112 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113
Michael Wright842500e2015-03-13 17:32:02 -07001114 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115
Michael Wright842500e2015-03-13 17:32:02 -07001116 // Scroll state.
1117 int32_t rawVScroll;
1118 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119
Michael Wright842500e2015-03-13 17:32:02 -07001120 void copyFrom(const RawState& other) {
1121 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001122 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001123 rawPointerData.copyFrom(other.rawPointerData);
1124 buttonState = other.buttonState;
1125 rawVScroll = other.rawVScroll;
1126 rawHScroll = other.rawHScroll;
1127 }
1128
1129 void clear() {
1130 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001131 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001132 rawPointerData.clear();
1133 buttonState = 0;
1134 rawVScroll = 0;
1135 rawHScroll = 0;
1136 }
1137 };
1138
1139 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001140 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001141 // Cooked pointer sample data.
1142 CookedPointerData cookedPointerData;
1143
1144 // Id bits used to differentiate fingers, stylus and mouse tools.
1145 BitSet32 fingerIdBits;
1146 BitSet32 stylusIdBits;
1147 BitSet32 mouseIdBits;
1148
Michael Wright7b159c92015-05-14 14:48:03 +01001149 int32_t buttonState;
1150
Michael Wright842500e2015-03-13 17:32:02 -07001151 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001152 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001153 cookedPointerData.copyFrom(other.cookedPointerData);
1154 fingerIdBits = other.fingerIdBits;
1155 stylusIdBits = other.stylusIdBits;
1156 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001157 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001158 }
1159
1160 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001161 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001162 cookedPointerData.clear();
1163 fingerIdBits.clear();
1164 stylusIdBits.clear();
1165 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001166 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001167 }
1168 };
1169
1170 Vector<RawState> mRawStatesPending;
1171 RawState mCurrentRawState;
1172 CookedState mCurrentCookedState;
1173 RawState mLastRawState;
1174 CookedState mLastCookedState;
1175
1176 // State provided by an external stylus
1177 StylusState mExternalStylusState;
1178 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001179 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001180 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181
1182 // True if we sent a HOVER_ENTER event.
1183 bool mSentHoverEnter;
1184
Michael Wright842500e2015-03-13 17:32:02 -07001185 // Have we assigned pointer IDs for this stream
1186 bool mHavePointerIds;
1187
Michael Wright8e812822015-06-22 16:18:21 +01001188 // Is the current stream of direct touch events aborted
1189 bool mCurrentMotionAborted;
1190
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 // The time the primary pointer last went down.
1192 nsecs_t mDownTime;
1193
1194 // The pointer controller, or null if the device is not a pointer.
1195 sp<PointerControllerInterface> mPointerController;
1196
1197 Vector<VirtualKey> mVirtualKeys;
1198
1199 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001200 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001202 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001204 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001206 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 virtual void parseCalibration();
1208 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001209 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001210 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001211 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001212 virtual void resolveExternalStylusPresence();
1213 virtual bool hasStylus() const = 0;
1214 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215
Michael Wright842500e2015-03-13 17:32:02 -07001216 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217
1218private:
1219 // The current viewport.
1220 // The components of the viewport are specified in the display's rotated orientation.
1221 DisplayViewport mViewport;
1222
1223 // The surface orientation, width and height set by configureSurface().
1224 // The width and height are derived from the viewport but are specified
1225 // in the natural orientation.
1226 // The surface origin specifies how the surface coordinates should be translated
1227 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 int32_t mSurfaceWidth;
1229 int32_t mSurfaceHeight;
1230 int32_t mSurfaceLeft;
1231 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001232
1233 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1234 // the logical coordinate space.
1235 int32_t mPhysicalWidth;
1236 int32_t mPhysicalHeight;
1237 int32_t mPhysicalLeft;
1238 int32_t mPhysicalTop;
1239
1240 // The orientation may be different from the viewport orientation as it specifies
1241 // the rotation of the surface coordinates required to produce the viewport's
1242 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 int32_t mSurfaceOrientation;
1244
1245 // Translation and scaling factors, orientation-independent.
1246 float mXTranslate;
1247 float mXScale;
1248 float mXPrecision;
1249
1250 float mYTranslate;
1251 float mYScale;
1252 float mYPrecision;
1253
1254 float mGeometricScale;
1255
1256 float mPressureScale;
1257
1258 float mSizeScale;
1259
1260 float mOrientationScale;
1261
1262 float mDistanceScale;
1263
1264 bool mHaveTilt;
1265 float mTiltXCenter;
1266 float mTiltXScale;
1267 float mTiltYCenter;
1268 float mTiltYScale;
1269
Michael Wright842500e2015-03-13 17:32:02 -07001270 bool mExternalStylusConnected;
1271
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 // Oriented motion ranges for input device info.
1273 struct OrientedRanges {
1274 InputDeviceInfo::MotionRange x;
1275 InputDeviceInfo::MotionRange y;
1276 InputDeviceInfo::MotionRange pressure;
1277
1278 bool haveSize;
1279 InputDeviceInfo::MotionRange size;
1280
1281 bool haveTouchSize;
1282 InputDeviceInfo::MotionRange touchMajor;
1283 InputDeviceInfo::MotionRange touchMinor;
1284
1285 bool haveToolSize;
1286 InputDeviceInfo::MotionRange toolMajor;
1287 InputDeviceInfo::MotionRange toolMinor;
1288
1289 bool haveOrientation;
1290 InputDeviceInfo::MotionRange orientation;
1291
1292 bool haveDistance;
1293 InputDeviceInfo::MotionRange distance;
1294
1295 bool haveTilt;
1296 InputDeviceInfo::MotionRange tilt;
1297
1298 OrientedRanges() {
1299 clear();
1300 }
1301
1302 void clear() {
1303 haveSize = false;
1304 haveTouchSize = false;
1305 haveToolSize = false;
1306 haveOrientation = false;
1307 haveDistance = false;
1308 haveTilt = false;
1309 }
1310 } mOrientedRanges;
1311
1312 // Oriented dimensions and precision.
1313 float mOrientedXPrecision;
1314 float mOrientedYPrecision;
1315
1316 struct CurrentVirtualKeyState {
1317 bool down;
1318 bool ignored;
1319 nsecs_t downTime;
1320 int32_t keyCode;
1321 int32_t scanCode;
1322 } mCurrentVirtualKey;
1323
1324 // Scale factor for gesture or mouse based pointer movements.
1325 float mPointerXMovementScale;
1326 float mPointerYMovementScale;
1327
1328 // Scale factor for gesture based zooming and other freeform motions.
1329 float mPointerXZoomScale;
1330 float mPointerYZoomScale;
1331
1332 // The maximum swipe width.
1333 float mPointerGestureMaxSwipeWidth;
1334
1335 struct PointerDistanceHeapElement {
1336 uint32_t currentPointerIndex : 8;
1337 uint32_t lastPointerIndex : 8;
1338 uint64_t distance : 48; // squared distance
1339 };
1340
1341 enum PointerUsage {
1342 POINTER_USAGE_NONE,
1343 POINTER_USAGE_GESTURES,
1344 POINTER_USAGE_STYLUS,
1345 POINTER_USAGE_MOUSE,
1346 };
1347 PointerUsage mPointerUsage;
1348
1349 struct PointerGesture {
1350 enum Mode {
1351 // No fingers, button is not pressed.
1352 // Nothing happening.
1353 NEUTRAL,
1354
1355 // No fingers, button is not pressed.
1356 // Tap detected.
1357 // Emits DOWN and UP events at the pointer location.
1358 TAP,
1359
1360 // Exactly one finger dragging following a tap.
1361 // Pointer follows the active finger.
1362 // Emits DOWN, MOVE and UP events at the pointer location.
1363 //
1364 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1365 TAP_DRAG,
1366
1367 // Button is pressed.
1368 // Pointer follows the active finger if there is one. Other fingers are ignored.
1369 // Emits DOWN, MOVE and UP events at the pointer location.
1370 BUTTON_CLICK_OR_DRAG,
1371
1372 // Exactly one finger, button is not pressed.
1373 // Pointer follows the active finger.
1374 // Emits HOVER_MOVE events at the pointer location.
1375 //
1376 // Detect taps when the finger goes up while in HOVER mode.
1377 HOVER,
1378
1379 // Exactly two fingers but neither have moved enough to clearly indicate
1380 // whether a swipe or freeform gesture was intended. We consider the
1381 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1382 // Pointer does not move.
1383 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1384 PRESS,
1385
1386 // Exactly two fingers moving in the same direction, button is not pressed.
1387 // Pointer does not move.
1388 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1389 // follows the midpoint between both fingers.
1390 SWIPE,
1391
1392 // Two or more fingers moving in arbitrary directions, button is not pressed.
1393 // Pointer does not move.
1394 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1395 // each finger individually relative to the initial centroid of the finger.
1396 FREEFORM,
1397
1398 // Waiting for quiet time to end before starting the next gesture.
1399 QUIET,
1400 };
1401
1402 // Time the first finger went down.
1403 nsecs_t firstTouchTime;
1404
1405 // The active pointer id from the raw touch data.
1406 int32_t activeTouchId; // -1 if none
1407
1408 // The active pointer id from the gesture last delivered to the application.
1409 int32_t activeGestureId; // -1 if none
1410
1411 // Pointer coords and ids for the current and previous pointer gesture.
1412 Mode currentGestureMode;
1413 BitSet32 currentGestureIdBits;
1414 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1415 PointerProperties currentGestureProperties[MAX_POINTERS];
1416 PointerCoords currentGestureCoords[MAX_POINTERS];
1417
1418 Mode lastGestureMode;
1419 BitSet32 lastGestureIdBits;
1420 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1421 PointerProperties lastGestureProperties[MAX_POINTERS];
1422 PointerCoords lastGestureCoords[MAX_POINTERS];
1423
1424 // Time the pointer gesture last went down.
1425 nsecs_t downTime;
1426
1427 // Time when the pointer went down for a TAP.
1428 nsecs_t tapDownTime;
1429
1430 // Time when the pointer went up for a TAP.
1431 nsecs_t tapUpTime;
1432
1433 // Location of initial tap.
1434 float tapX, tapY;
1435
1436 // Time we started waiting for quiescence.
1437 nsecs_t quietTime;
1438
1439 // Reference points for multitouch gestures.
1440 float referenceTouchX; // reference touch X/Y coordinates in surface units
1441 float referenceTouchY;
1442 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1443 float referenceGestureY;
1444
1445 // Distance that each pointer has traveled which has not yet been
1446 // subsumed into the reference gesture position.
1447 BitSet32 referenceIdBits;
1448 struct Delta {
1449 float dx, dy;
1450 };
1451 Delta referenceDeltas[MAX_POINTER_ID + 1];
1452
1453 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1454 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1455
1456 // A velocity tracker for determining whether to switch active pointers during drags.
1457 VelocityTracker velocityTracker;
1458
1459 void reset() {
1460 firstTouchTime = LLONG_MIN;
1461 activeTouchId = -1;
1462 activeGestureId = -1;
1463 currentGestureMode = NEUTRAL;
1464 currentGestureIdBits.clear();
1465 lastGestureMode = NEUTRAL;
1466 lastGestureIdBits.clear();
1467 downTime = 0;
1468 velocityTracker.clear();
1469 resetTap();
1470 resetQuietTime();
1471 }
1472
1473 void resetTap() {
1474 tapDownTime = LLONG_MIN;
1475 tapUpTime = LLONG_MIN;
1476 }
1477
1478 void resetQuietTime() {
1479 quietTime = LLONG_MIN;
1480 }
1481 } mPointerGesture;
1482
1483 struct PointerSimple {
1484 PointerCoords currentCoords;
1485 PointerProperties currentProperties;
1486 PointerCoords lastCoords;
1487 PointerProperties lastProperties;
1488
1489 // True if the pointer is down.
1490 bool down;
1491
1492 // True if the pointer is hovering.
1493 bool hovering;
1494
1495 // Time the pointer last went down.
1496 nsecs_t downTime;
1497
1498 void reset() {
1499 currentCoords.clear();
1500 currentProperties.clear();
1501 lastCoords.clear();
1502 lastProperties.clear();
1503 down = false;
1504 hovering = false;
1505 downTime = 0;
1506 }
1507 } mPointerSimple;
1508
1509 // The pointer and scroll velocity controls.
1510 VelocityControl mPointerVelocityControl;
1511 VelocityControl mWheelXVelocityControl;
1512 VelocityControl mWheelYVelocityControl;
1513
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001514 std::optional<DisplayViewport> findViewport();
1515
Michael Wright842500e2015-03-13 17:32:02 -07001516 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001517 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001518
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 void sync(nsecs_t when);
1520
1521 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001522 void processRawTouches(bool timeout);
1523 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1525 int32_t keyEventAction, int32_t keyEventFlags);
1526
1527 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1528 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1529 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001530 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1531 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1532 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001534 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535
1536 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1537 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1538
1539 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1540 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1541 bool preparePointerGestures(nsecs_t when,
1542 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1543 bool isTimeout);
1544
1545 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1546 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1547
1548 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1549 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1550
1551 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1552 bool down, bool hovering);
1553 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1554
Michael Wright842500e2015-03-13 17:32:02 -07001555 bool assignExternalStylusId(const RawState& state, bool timeout);
1556 void applyExternalStylusButtonState(nsecs_t when);
1557 void applyExternalStylusTouchState(nsecs_t when);
1558
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 // Dispatches a motion event.
1560 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1561 // method will take care of setting the index and transmuting the action to DOWN or UP
1562 // it is the first / last pointer to go down / up.
1563 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001564 int32_t action, int32_t actionButton,
1565 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001566 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 const PointerProperties* properties, const PointerCoords* coords,
1568 const uint32_t* idToIndex, BitSet32 idBits,
1569 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1570
1571 // Updates pointer coords and properties for pointers with specified ids that have moved.
1572 // Returns true if any of them changed.
1573 bool updateMovedPointers(const PointerProperties* inProperties,
1574 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1575 PointerProperties* outProperties, PointerCoords* outCoords,
1576 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1577
1578 bool isPointInsideSurface(int32_t x, int32_t y);
1579 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1580
Michael Wright842500e2015-03-13 17:32:02 -07001581 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001582
1583 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584};
1585
1586
1587class SingleTouchInputMapper : public TouchInputMapper {
1588public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001589 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 virtual ~SingleTouchInputMapper();
1591
1592 virtual void reset(nsecs_t when);
1593 virtual void process(const RawEvent* rawEvent);
1594
1595protected:
Michael Wright842500e2015-03-13 17:32:02 -07001596 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 virtual void configureRawPointerAxes();
1598 virtual bool hasStylus() const;
1599
1600private:
1601 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1602};
1603
1604
1605class MultiTouchInputMapper : public TouchInputMapper {
1606public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001607 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608 virtual ~MultiTouchInputMapper();
1609
1610 virtual void reset(nsecs_t when);
1611 virtual void process(const RawEvent* rawEvent);
1612
1613protected:
Michael Wright842500e2015-03-13 17:32:02 -07001614 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 virtual void configureRawPointerAxes();
1616 virtual bool hasStylus() const;
1617
1618private:
1619 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1620
1621 // Specifies the pointer id bits that are in use, and their associated tracking id.
1622 BitSet32 mPointerIdBits;
1623 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1624};
1625
Michael Wright842500e2015-03-13 17:32:02 -07001626class ExternalStylusInputMapper : public InputMapper {
1627public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001628 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001629 virtual ~ExternalStylusInputMapper() = default;
1630
1631 virtual uint32_t getSources();
1632 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001633 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001634 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1635 virtual void reset(nsecs_t when);
1636 virtual void process(const RawEvent* rawEvent);
1637 virtual void sync(nsecs_t when);
1638
1639private:
1640 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1641 RawAbsoluteAxisInfo mRawPressureAxis;
1642 TouchButtonAccumulator mTouchButtonAccumulator;
1643
1644 StylusState mStylusState;
1645};
1646
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647
1648class JoystickInputMapper : public InputMapper {
1649public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001650 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651 virtual ~JoystickInputMapper();
1652
1653 virtual uint32_t getSources();
1654 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001655 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1657 virtual void reset(nsecs_t when);
1658 virtual void process(const RawEvent* rawEvent);
1659
1660private:
1661 struct Axis {
1662 RawAbsoluteAxisInfo rawAxisInfo;
1663 AxisInfo axisInfo;
1664
1665 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1666
1667 float scale; // scale factor from raw to normalized values
1668 float offset; // offset to add after scaling for normalization
1669 float highScale; // scale factor from raw to normalized values of high split
1670 float highOffset; // offset to add after scaling for normalization of high split
1671
1672 float min; // normalized inclusive minimum
1673 float max; // normalized inclusive maximum
1674 float flat; // normalized flat region size
1675 float fuzz; // normalized error tolerance
1676 float resolution; // normalized resolution in units/mm
1677
1678 float filter; // filter out small variations of this size
1679 float currentValue; // current value
1680 float newValue; // most recent value
1681 float highCurrentValue; // current value of high split
1682 float highNewValue; // most recent value of high split
1683
1684 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1685 bool explicitlyMapped, float scale, float offset,
1686 float highScale, float highOffset,
1687 float min, float max, float flat, float fuzz, float resolution) {
1688 this->rawAxisInfo = rawAxisInfo;
1689 this->axisInfo = axisInfo;
1690 this->explicitlyMapped = explicitlyMapped;
1691 this->scale = scale;
1692 this->offset = offset;
1693 this->highScale = highScale;
1694 this->highOffset = highOffset;
1695 this->min = min;
1696 this->max = max;
1697 this->flat = flat;
1698 this->fuzz = fuzz;
1699 this->resolution = resolution;
1700 this->filter = 0;
1701 resetValue();
1702 }
1703
1704 void resetValue() {
1705 this->currentValue = 0;
1706 this->newValue = 0;
1707 this->highCurrentValue = 0;
1708 this->highNewValue = 0;
1709 }
1710 };
1711
1712 // Axes indexed by raw ABS_* axis index.
1713 KeyedVector<int32_t, Axis> mAxes;
1714
1715 void sync(nsecs_t when, bool force);
1716
1717 bool haveAxis(int32_t axisId);
1718 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1719 bool filterAxes(bool force);
1720
1721 static bool hasValueChangedSignificantly(float filter,
1722 float newValue, float currentValue, float min, float max);
1723 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1724 float newValue, float currentValue, float thresholdValue);
1725
1726 static bool isCenteredAxis(int32_t axis);
1727 static int32_t getCompatAxis(int32_t axis);
1728
1729 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1730 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1731 float value);
1732};
1733
1734} // namespace android
1735
1736#endif // _UI_INPUT_READER_H