blob: 13f1bedb650061f80760365339670050f9e17340 [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;
99};
100
101
102/* The input reader reads raw event data from the event hub and processes it into input events
103 * that it sends to the input listener. Some functions of the input reader, such as early
104 * event filtering in low power states, are controlled by a separate policy object.
105 *
106 * The InputReader owns a collection of InputMappers. Most of the work it does happens
107 * on the input reader thread but the InputReader can receive queries from other system
108 * components running on arbitrary threads. To keep things manageable, the InputReader
109 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
110 * EventHub or the InputReaderPolicy but it is never held while calling into the
111 * InputListener.
112 */
113class InputReader : public InputReaderInterface {
114public:
115 InputReader(const sp<EventHubInterface>& eventHub,
116 const sp<InputReaderPolicyInterface>& policy,
117 const sp<InputListenerInterface>& listener);
118 virtual ~InputReader();
119
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800120 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121 virtual void monitor();
122
123 virtual void loopOnce();
124
125 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
126
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700127 virtual bool isInputDeviceEnabled(int32_t deviceId);
128
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
130 int32_t scanCode);
131 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
132 int32_t keyCode);
133 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
134 int32_t sw);
135
Andrii Kulian763a3a42016-03-08 10:46:16 -0800136 virtual void toggleCapsLockState(int32_t deviceId);
137
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
139 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
140
141 virtual void requestRefreshConfiguration(uint32_t changes);
142
143 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
144 ssize_t repeat, int32_t token);
145 virtual void cancelVibrate(int32_t deviceId, int32_t token);
146
147protected:
148 // These members are protected so they can be instrumented by test cases.
149 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
150 const InputDeviceIdentifier& identifier, uint32_t classes);
151
152 class ContextImpl : public InputReaderContext {
153 InputReader* mReader;
154
155 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700156 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157
158 virtual void updateGlobalMetaState();
159 virtual int32_t getGlobalMetaState();
160 virtual void disableVirtualKeysUntil(nsecs_t time);
161 virtual bool shouldDropVirtualKey(nsecs_t now,
162 InputDevice* device, int32_t keyCode, int32_t scanCode);
163 virtual void fadePointer();
164 virtual void requestTimeoutAtTime(nsecs_t when);
165 virtual int32_t bumpGeneration();
Michael Wright842500e2015-03-13 17:32:02 -0700166 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
167 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 virtual InputReaderPolicyInterface* getPolicy();
169 virtual InputListenerInterface* getListener();
170 virtual EventHubInterface* getEventHub();
171 } mContext;
172
173 friend class ContextImpl;
174
175private:
176 Mutex mLock;
177
178 Condition mReaderIsAliveCondition;
179
180 sp<EventHubInterface> mEventHub;
181 sp<InputReaderPolicyInterface> mPolicy;
182 sp<QueuedInputListener> mQueuedListener;
183
184 InputReaderConfiguration mConfig;
185
186 // The event queue.
187 static const int EVENT_BUFFER_SIZE = 256;
188 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
189
190 KeyedVector<int32_t, InputDevice*> mDevices;
191
192 // low-level input event decoding and device management
193 void processEventsLocked(const RawEvent* rawEvents, size_t count);
194
195 void addDeviceLocked(nsecs_t when, int32_t deviceId);
196 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
197 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
198 void timeoutExpiredLocked(nsecs_t when);
199
200 void handleConfigurationChangedLocked(nsecs_t when);
201
202 int32_t mGlobalMetaState;
203 void updateGlobalMetaStateLocked();
204 int32_t getGlobalMetaStateLocked();
205
Michael Wright842500e2015-03-13 17:32:02 -0700206 void notifyExternalStylusPresenceChanged();
207 void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
208 void dispatchExternalStylusState(const StylusState& state);
209
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 void fadePointerLocked();
211
212 int32_t mGeneration;
213 int32_t bumpGenerationLocked();
214
215 void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
216
217 nsecs_t mDisableVirtualKeysTimeout;
218 void disableVirtualKeysUntilLocked(nsecs_t time);
219 bool shouldDropVirtualKeyLocked(nsecs_t now,
220 InputDevice* device, int32_t keyCode, int32_t scanCode);
221
222 nsecs_t mNextTimeout;
223 void requestTimeoutAtTimeLocked(nsecs_t when);
224
225 uint32_t mConfigurationChangesToRefresh;
226 void refreshConfigurationLocked(uint32_t changes);
227
228 // state queries
229 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
230 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
231 GetStateFunc getStateFunc);
232 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
233 const int32_t* keyCodes, uint8_t* outFlags);
234};
235
236
Michael Wrightd02c5b62014-02-10 15:10:22 -0800237/* Represents the state of a single input device. */
238class InputDevice {
239public:
240 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
241 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
242 ~InputDevice();
243
244 inline InputReaderContext* getContext() { return mContext; }
245 inline int32_t getId() const { return mId; }
246 inline int32_t getControllerNumber() const { return mControllerNumber; }
247 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100248 inline const std::string getName() const { return mIdentifier.name; }
249 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 inline uint32_t getClasses() const { return mClasses; }
251 inline uint32_t getSources() const { return mSources; }
252
253 inline bool isExternal() { return mIsExternal; }
254 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700255 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
256 return mAssociatedDisplayPort;
257 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Tim Kilbourn063ff532015-04-08 10:26:18 -0700259 inline void setMic(bool hasMic) { mHasMic = hasMic; }
260 inline bool hasMic() const { return mHasMic; }
261
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 inline bool isIgnored() { return mMappers.isEmpty(); }
263
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700264 bool isEnabled();
265 void setEnabled(bool enabled, nsecs_t when);
266
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800267 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 void addMapper(InputMapper* mapper);
269 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
270 void reset(nsecs_t when);
271 void process(const RawEvent* rawEvents, size_t count);
272 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700273 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274
275 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
276 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
277 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
278 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
279 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
280 const int32_t* keyCodes, uint8_t* outFlags);
281 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
282 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800283 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800286 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287
288 void fadePointer();
289
290 void bumpGeneration();
291
292 void notifyReset(nsecs_t when);
293
294 inline const PropertyMap& getConfiguration() { return mConfiguration; }
295 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
296
297 bool hasKey(int32_t code) {
298 return getEventHub()->hasScanCode(mId, code);
299 }
300
301 bool hasAbsoluteAxis(int32_t code) {
302 RawAbsoluteAxisInfo info;
303 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
304 return info.valid;
305 }
306
307 bool isKeyPressed(int32_t code) {
308 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
309 }
310
311 int32_t getAbsoluteAxisValue(int32_t code) {
312 int32_t value;
313 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
314 return value;
315 }
316
317private:
318 InputReaderContext* mContext;
319 int32_t mId;
320 int32_t mGeneration;
321 int32_t mControllerNumber;
322 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100323 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 uint32_t mClasses;
325
326 Vector<InputMapper*> mMappers;
327
328 uint32_t mSources;
329 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700330 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700331 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 bool mDropUntilNextSync;
333
334 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
335 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
336
337 PropertyMap mConfiguration;
338};
339
340
341/* Keeps track of the state of mouse or touch pad buttons. */
342class CursorButtonAccumulator {
343public:
344 CursorButtonAccumulator();
345 void reset(InputDevice* device);
346
347 void process(const RawEvent* rawEvent);
348
349 uint32_t getButtonState() const;
350
351private:
352 bool mBtnLeft;
353 bool mBtnRight;
354 bool mBtnMiddle;
355 bool mBtnBack;
356 bool mBtnSide;
357 bool mBtnForward;
358 bool mBtnExtra;
359 bool mBtnTask;
360
361 void clearButtons();
362};
363
364
365/* Keeps track of cursor movements. */
366
367class CursorMotionAccumulator {
368public:
369 CursorMotionAccumulator();
370 void reset(InputDevice* device);
371
372 void process(const RawEvent* rawEvent);
373 void finishSync();
374
375 inline int32_t getRelativeX() const { return mRelX; }
376 inline int32_t getRelativeY() const { return mRelY; }
377
378private:
379 int32_t mRelX;
380 int32_t mRelY;
381
382 void clearRelativeAxes();
383};
384
385
386/* Keeps track of cursor scrolling motions. */
387
388class CursorScrollAccumulator {
389public:
390 CursorScrollAccumulator();
391 void configure(InputDevice* device);
392 void reset(InputDevice* device);
393
394 void process(const RawEvent* rawEvent);
395 void finishSync();
396
397 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
398 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
399
400 inline int32_t getRelativeX() const { return mRelX; }
401 inline int32_t getRelativeY() const { return mRelY; }
402 inline int32_t getRelativeVWheel() const { return mRelWheel; }
403 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
404
405private:
406 bool mHaveRelWheel;
407 bool mHaveRelHWheel;
408
409 int32_t mRelX;
410 int32_t mRelY;
411 int32_t mRelWheel;
412 int32_t mRelHWheel;
413
414 void clearRelativeAxes();
415};
416
417
418/* Keeps track of the state of touch, stylus and tool buttons. */
419class TouchButtonAccumulator {
420public:
421 TouchButtonAccumulator();
422 void configure(InputDevice* device);
423 void reset(InputDevice* device);
424
425 void process(const RawEvent* rawEvent);
426
427 uint32_t getButtonState() const;
428 int32_t getToolType() const;
429 bool isToolActive() const;
430 bool isHovering() const;
431 bool hasStylus() const;
432
433private:
434 bool mHaveBtnTouch;
435 bool mHaveStylus;
436
437 bool mBtnTouch;
438 bool mBtnStylus;
439 bool mBtnStylus2;
440 bool mBtnToolFinger;
441 bool mBtnToolPen;
442 bool mBtnToolRubber;
443 bool mBtnToolBrush;
444 bool mBtnToolPencil;
445 bool mBtnToolAirbrush;
446 bool mBtnToolMouse;
447 bool mBtnToolLens;
448 bool mBtnToolDoubleTap;
449 bool mBtnToolTripleTap;
450 bool mBtnToolQuadTap;
451
452 void clearButtons();
453};
454
455
456/* Raw axis information from the driver. */
457struct RawPointerAxes {
458 RawAbsoluteAxisInfo x;
459 RawAbsoluteAxisInfo y;
460 RawAbsoluteAxisInfo pressure;
461 RawAbsoluteAxisInfo touchMajor;
462 RawAbsoluteAxisInfo touchMinor;
463 RawAbsoluteAxisInfo toolMajor;
464 RawAbsoluteAxisInfo toolMinor;
465 RawAbsoluteAxisInfo orientation;
466 RawAbsoluteAxisInfo distance;
467 RawAbsoluteAxisInfo tiltX;
468 RawAbsoluteAxisInfo tiltY;
469 RawAbsoluteAxisInfo trackingId;
470 RawAbsoluteAxisInfo slot;
471
472 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800473 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
474 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 void clear();
476};
477
478
479/* Raw data for a collection of pointers including a pointer id mapping table. */
480struct RawPointerData {
481 struct Pointer {
482 uint32_t id;
483 int32_t x;
484 int32_t y;
485 int32_t pressure;
486 int32_t touchMajor;
487 int32_t touchMinor;
488 int32_t toolMajor;
489 int32_t toolMinor;
490 int32_t orientation;
491 int32_t distance;
492 int32_t tiltX;
493 int32_t tiltY;
494 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
495 bool isHovering;
496 };
497
498 uint32_t pointerCount;
499 Pointer pointers[MAX_POINTERS];
500 BitSet32 hoveringIdBits, touchingIdBits;
501 uint32_t idToIndex[MAX_POINTER_ID + 1];
502
503 RawPointerData();
504 void clear();
505 void copyFrom(const RawPointerData& other);
506 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
507
508 inline void markIdBit(uint32_t id, bool isHovering) {
509 if (isHovering) {
510 hoveringIdBits.markBit(id);
511 } else {
512 touchingIdBits.markBit(id);
513 }
514 }
515
516 inline void clearIdBits() {
517 hoveringIdBits.clear();
518 touchingIdBits.clear();
519 }
520
521 inline const Pointer& pointerForId(uint32_t id) const {
522 return pointers[idToIndex[id]];
523 }
524
525 inline bool isHovering(uint32_t pointerIndex) {
526 return pointers[pointerIndex].isHovering;
527 }
528};
529
530
531/* Cooked data for a collection of pointers including a pointer id mapping table. */
532struct CookedPointerData {
533 uint32_t pointerCount;
534 PointerProperties pointerProperties[MAX_POINTERS];
535 PointerCoords pointerCoords[MAX_POINTERS];
536 BitSet32 hoveringIdBits, touchingIdBits;
537 uint32_t idToIndex[MAX_POINTER_ID + 1];
538
539 CookedPointerData();
540 void clear();
541 void copyFrom(const CookedPointerData& other);
542
543 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
544 return pointerCoords[idToIndex[id]];
545 }
546
Michael Wright842500e2015-03-13 17:32:02 -0700547 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
548 return pointerCoords[idToIndex[id]];
549 }
550
551 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
552 return pointerProperties[idToIndex[id]];
553 }
554
Michael Wright53dca3a2015-04-23 17:39:53 +0100555 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
557 }
Michael Wright842500e2015-03-13 17:32:02 -0700558
Michael Wright53dca3a2015-04-23 17:39:53 +0100559 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700560 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
561 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562};
563
564
565/* Keeps track of the state of single-touch protocol. */
566class SingleTouchMotionAccumulator {
567public:
568 SingleTouchMotionAccumulator();
569
570 void process(const RawEvent* rawEvent);
571 void reset(InputDevice* device);
572
573 inline int32_t getAbsoluteX() const { return mAbsX; }
574 inline int32_t getAbsoluteY() const { return mAbsY; }
575 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
576 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
577 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
578 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
579 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
580
581private:
582 int32_t mAbsX;
583 int32_t mAbsY;
584 int32_t mAbsPressure;
585 int32_t mAbsToolWidth;
586 int32_t mAbsDistance;
587 int32_t mAbsTiltX;
588 int32_t mAbsTiltY;
589
590 void clearAbsoluteAxes();
591};
592
593
594/* Keeps track of the state of multi-touch protocol. */
595class MultiTouchMotionAccumulator {
596public:
597 class Slot {
598 public:
599 inline bool isInUse() const { return mInUse; }
600 inline int32_t getX() const { return mAbsMTPositionX; }
601 inline int32_t getY() const { return mAbsMTPositionY; }
602 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
603 inline int32_t getTouchMinor() const {
604 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
605 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
606 inline int32_t getToolMinor() const {
607 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
608 inline int32_t getOrientation() const { return mAbsMTOrientation; }
609 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
610 inline int32_t getPressure() const { return mAbsMTPressure; }
611 inline int32_t getDistance() const { return mAbsMTDistance; }
612 inline int32_t getToolType() const;
613
614 private:
615 friend class MultiTouchMotionAccumulator;
616
617 bool mInUse;
618 bool mHaveAbsMTTouchMinor;
619 bool mHaveAbsMTWidthMinor;
620 bool mHaveAbsMTToolType;
621
622 int32_t mAbsMTPositionX;
623 int32_t mAbsMTPositionY;
624 int32_t mAbsMTTouchMajor;
625 int32_t mAbsMTTouchMinor;
626 int32_t mAbsMTWidthMajor;
627 int32_t mAbsMTWidthMinor;
628 int32_t mAbsMTOrientation;
629 int32_t mAbsMTTrackingId;
630 int32_t mAbsMTPressure;
631 int32_t mAbsMTDistance;
632 int32_t mAbsMTToolType;
633
634 Slot();
635 void clear();
636 };
637
638 MultiTouchMotionAccumulator();
639 ~MultiTouchMotionAccumulator();
640
641 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
642 void reset(InputDevice* device);
643 void process(const RawEvent* rawEvent);
644 void finishSync();
645 bool hasStylus() const;
646
647 inline size_t getSlotCount() const { return mSlotCount; }
648 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800649 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650
651private:
652 int32_t mCurrentSlot;
653 Slot* mSlots;
654 size_t mSlotCount;
655 bool mUsingSlotsProtocol;
656 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800657 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658
659 void clearSlots(int32_t initialSlot);
660};
661
662
663/* An input mapper transforms raw input events into cooked event data.
664 * A single input device can have multiple associated input mappers in order to interpret
665 * different classes of events.
666 *
667 * InputMapper lifecycle:
668 * - create
669 * - configure with 0 changes
670 * - reset
671 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
672 * - reset
673 * - destroy
674 */
675class InputMapper {
676public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700677 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 virtual ~InputMapper();
679
680 inline InputDevice* getDevice() { return mDevice; }
681 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100682 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 inline InputReaderContext* getContext() { return mContext; }
684 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
685 inline InputListenerInterface* getListener() { return mContext->getListener(); }
686 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
687
688 virtual uint32_t getSources() = 0;
689 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800690 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
692 virtual void reset(nsecs_t when);
693 virtual void process(const RawEvent* rawEvent) = 0;
694 virtual void timeoutExpired(nsecs_t when);
695
696 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
697 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
698 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
699 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
700 const int32_t* keyCodes, uint8_t* outFlags);
701 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
702 int32_t token);
703 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800704 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705
706 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800707 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708
Michael Wright842500e2015-03-13 17:32:02 -0700709 virtual void updateExternalStylusState(const StylusState& state);
710
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 virtual void fadePointer();
712
713protected:
714 InputDevice* mDevice;
715 InputReaderContext* mContext;
716
717 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
718 void bumpGeneration();
719
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800720 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800722 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723};
724
725
726class SwitchInputMapper : public InputMapper {
727public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700728 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 virtual ~SwitchInputMapper();
730
731 virtual uint32_t getSources();
732 virtual void process(const RawEvent* rawEvent);
733
734 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800735 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736
737private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700738 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 uint32_t mUpdatedSwitchMask;
740
741 void processSwitch(int32_t switchCode, int32_t switchValue);
742 void sync(nsecs_t when);
743};
744
745
746class VibratorInputMapper : public InputMapper {
747public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700748 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 virtual ~VibratorInputMapper();
750
751 virtual uint32_t getSources();
752 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
753 virtual void process(const RawEvent* rawEvent);
754
755 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
756 int32_t token);
757 virtual void cancelVibrate(int32_t token);
758 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800759 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760
761private:
762 bool mVibrating;
763 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
764 size_t mPatternSize;
765 ssize_t mRepeat;
766 int32_t mToken;
767 ssize_t mIndex;
768 nsecs_t mNextStepTime;
769
770 void nextStep();
771 void stopVibrating();
772};
773
774
775class KeyboardInputMapper : public InputMapper {
776public:
777 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
778 virtual ~KeyboardInputMapper();
779
780 virtual uint32_t getSources();
781 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800782 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
784 virtual void reset(nsecs_t when);
785 virtual void process(const RawEvent* rawEvent);
786
787 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
788 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
789 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
790 const int32_t* keyCodes, uint8_t* outFlags);
791
792 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800793 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794
795private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100796 // The current viewport.
797 std::optional<DisplayViewport> mViewport;
798
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799 struct KeyDown {
800 int32_t keyCode;
801 int32_t scanCode;
802 };
803
804 uint32_t mSource;
805 int32_t mKeyboardType;
806
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 Vector<KeyDown> mKeyDowns; // keys that are down
808 int32_t mMetaState;
809 nsecs_t mDownTime; // time of most recent key down
810
811 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
812
813 struct LedState {
814 bool avail; // led is available
815 bool on; // we think the led is currently on
816 };
817 LedState mCapsLockLedState;
818 LedState mNumLockLedState;
819 LedState mScrollLockLedState;
820
821 // Immutable configuration parameters.
822 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700824 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 } mParameters;
826
827 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800828 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100830 int32_t getOrientation();
831 int32_t getDisplayId();
832
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100834 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700836 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837
Andrii Kulian763a3a42016-03-08 10:46:16 -0800838 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
839
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 ssize_t findKeyDown(int32_t scanCode);
841
842 void resetLedState();
843 void initializeLedState(LedState& ledState, int32_t led);
844 void updateLedState(bool reset);
845 void updateLedStateForModifier(LedState& ledState, int32_t led,
846 int32_t modifier, bool reset);
847};
848
849
850class CursorInputMapper : public InputMapper {
851public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700852 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 virtual ~CursorInputMapper();
854
855 virtual uint32_t getSources();
856 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800857 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
859 virtual void reset(nsecs_t when);
860 virtual void process(const RawEvent* rawEvent);
861
862 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
863
864 virtual void fadePointer();
865
866private:
867 // Amount that trackball needs to move in order to generate a key event.
868 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
869
870 // Immutable configuration parameters.
871 struct Parameters {
872 enum Mode {
873 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800874 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 MODE_NAVIGATION,
876 };
877
878 Mode mode;
879 bool hasAssociatedDisplay;
880 bool orientationAware;
881 } mParameters;
882
883 CursorButtonAccumulator mCursorButtonAccumulator;
884 CursorMotionAccumulator mCursorMotionAccumulator;
885 CursorScrollAccumulator mCursorScrollAccumulator;
886
887 int32_t mSource;
888 float mXScale;
889 float mYScale;
890 float mXPrecision;
891 float mYPrecision;
892
893 float mVWheelScale;
894 float mHWheelScale;
895
896 // Velocity controls for mouse pointer and wheel movements.
897 // The controls for X and Y wheel movements are separate to keep them decoupled.
898 VelocityControl mPointerVelocityControl;
899 VelocityControl mWheelXVelocityControl;
900 VelocityControl mWheelYVelocityControl;
901
902 int32_t mOrientation;
903
904 sp<PointerControllerInterface> mPointerController;
905
906 int32_t mButtonState;
907 nsecs_t mDownTime;
908
909 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800910 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
912 void sync(nsecs_t when);
913};
914
915
Prashant Malani1941ff52015-08-11 18:29:28 -0700916class RotaryEncoderInputMapper : public InputMapper {
917public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700918 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700919 virtual ~RotaryEncoderInputMapper();
920
921 virtual uint32_t getSources();
922 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800923 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700924 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
925 virtual void reset(nsecs_t when);
926 virtual void process(const RawEvent* rawEvent);
927
928private:
929 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
930
931 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -0800932 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +0100933 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -0700934
935 void sync(nsecs_t when);
936};
937
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938class TouchInputMapper : public InputMapper {
939public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700940 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 virtual ~TouchInputMapper();
942
943 virtual uint32_t getSources();
944 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800945 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
947 virtual void reset(nsecs_t when);
948 virtual void process(const RawEvent* rawEvent);
949
950 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
951 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
952 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
953 const int32_t* keyCodes, uint8_t* outFlags);
954
955 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -0800956 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700958 virtual void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959
960protected:
961 CursorButtonAccumulator mCursorButtonAccumulator;
962 CursorScrollAccumulator mCursorScrollAccumulator;
963 TouchButtonAccumulator mTouchButtonAccumulator;
964
965 struct VirtualKey {
966 int32_t keyCode;
967 int32_t scanCode;
968 uint32_t flags;
969
970 // computed hit box, specified in touch screen coords based on known display size
971 int32_t hitLeft;
972 int32_t hitTop;
973 int32_t hitRight;
974 int32_t hitBottom;
975
976 inline bool isHit(int32_t x, int32_t y) const {
977 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
978 }
979 };
980
981 // Input sources and device mode.
982 uint32_t mSource;
983
984 enum DeviceMode {
985 DEVICE_MODE_DISABLED, // input is disabled
986 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
987 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
988 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
989 DEVICE_MODE_POINTER, // pointer mapping (pointer)
990 };
991 DeviceMode mDeviceMode;
992
993 // The reader's configuration.
994 InputReaderConfiguration mConfig;
995
996 // Immutable configuration parameters.
997 struct Parameters {
998 enum DeviceType {
999 DEVICE_TYPE_TOUCH_SCREEN,
1000 DEVICE_TYPE_TOUCH_PAD,
1001 DEVICE_TYPE_TOUCH_NAVIGATION,
1002 DEVICE_TYPE_POINTER,
1003 };
1004
1005 DeviceType deviceType;
1006 bool hasAssociatedDisplay;
1007 bool associatedDisplayIsExternal;
1008 bool orientationAware;
1009 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001010 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011
1012 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001013 GESTURE_MODE_SINGLE_TOUCH,
1014 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 };
1016 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001017
1018 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 } mParameters;
1020
1021 // Immutable calibration parameters in parsed form.
1022 struct Calibration {
1023 // Size
1024 enum SizeCalibration {
1025 SIZE_CALIBRATION_DEFAULT,
1026 SIZE_CALIBRATION_NONE,
1027 SIZE_CALIBRATION_GEOMETRIC,
1028 SIZE_CALIBRATION_DIAMETER,
1029 SIZE_CALIBRATION_BOX,
1030 SIZE_CALIBRATION_AREA,
1031 };
1032
1033 SizeCalibration sizeCalibration;
1034
1035 bool haveSizeScale;
1036 float sizeScale;
1037 bool haveSizeBias;
1038 float sizeBias;
1039 bool haveSizeIsSummed;
1040 bool sizeIsSummed;
1041
1042 // Pressure
1043 enum PressureCalibration {
1044 PRESSURE_CALIBRATION_DEFAULT,
1045 PRESSURE_CALIBRATION_NONE,
1046 PRESSURE_CALIBRATION_PHYSICAL,
1047 PRESSURE_CALIBRATION_AMPLITUDE,
1048 };
1049
1050 PressureCalibration pressureCalibration;
1051 bool havePressureScale;
1052 float pressureScale;
1053
1054 // Orientation
1055 enum OrientationCalibration {
1056 ORIENTATION_CALIBRATION_DEFAULT,
1057 ORIENTATION_CALIBRATION_NONE,
1058 ORIENTATION_CALIBRATION_INTERPOLATED,
1059 ORIENTATION_CALIBRATION_VECTOR,
1060 };
1061
1062 OrientationCalibration orientationCalibration;
1063
1064 // Distance
1065 enum DistanceCalibration {
1066 DISTANCE_CALIBRATION_DEFAULT,
1067 DISTANCE_CALIBRATION_NONE,
1068 DISTANCE_CALIBRATION_SCALED,
1069 };
1070
1071 DistanceCalibration distanceCalibration;
1072 bool haveDistanceScale;
1073 float distanceScale;
1074
1075 enum CoverageCalibration {
1076 COVERAGE_CALIBRATION_DEFAULT,
1077 COVERAGE_CALIBRATION_NONE,
1078 COVERAGE_CALIBRATION_BOX,
1079 };
1080
1081 CoverageCalibration coverageCalibration;
1082
1083 inline void applySizeScaleAndBias(float* outSize) const {
1084 if (haveSizeScale) {
1085 *outSize *= sizeScale;
1086 }
1087 if (haveSizeBias) {
1088 *outSize += sizeBias;
1089 }
1090 if (*outSize < 0) {
1091 *outSize = 0;
1092 }
1093 }
1094 } mCalibration;
1095
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001096 // Affine location transformation/calibration
1097 struct TouchAffineTransformation mAffineTransform;
1098
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 RawPointerAxes mRawPointerAxes;
1100
Michael Wright842500e2015-03-13 17:32:02 -07001101 struct RawState {
1102 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001103 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104
Michael Wright842500e2015-03-13 17:32:02 -07001105 // Raw pointer sample data.
1106 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107
Michael Wright842500e2015-03-13 17:32:02 -07001108 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Michael Wright842500e2015-03-13 17:32:02 -07001110 // Scroll state.
1111 int32_t rawVScroll;
1112 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113
Michael Wright842500e2015-03-13 17:32:02 -07001114 void copyFrom(const RawState& other) {
1115 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001116 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001117 rawPointerData.copyFrom(other.rawPointerData);
1118 buttonState = other.buttonState;
1119 rawVScroll = other.rawVScroll;
1120 rawHScroll = other.rawHScroll;
1121 }
1122
1123 void clear() {
1124 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001125 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001126 rawPointerData.clear();
1127 buttonState = 0;
1128 rawVScroll = 0;
1129 rawHScroll = 0;
1130 }
1131 };
1132
1133 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001134 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001135 // Cooked pointer sample data.
1136 CookedPointerData cookedPointerData;
1137
1138 // Id bits used to differentiate fingers, stylus and mouse tools.
1139 BitSet32 fingerIdBits;
1140 BitSet32 stylusIdBits;
1141 BitSet32 mouseIdBits;
1142
Michael Wright7b159c92015-05-14 14:48:03 +01001143 int32_t buttonState;
1144
Michael Wright842500e2015-03-13 17:32:02 -07001145 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001146 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001147 cookedPointerData.copyFrom(other.cookedPointerData);
1148 fingerIdBits = other.fingerIdBits;
1149 stylusIdBits = other.stylusIdBits;
1150 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001151 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001152 }
1153
1154 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001155 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001156 cookedPointerData.clear();
1157 fingerIdBits.clear();
1158 stylusIdBits.clear();
1159 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001160 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001161 }
1162 };
1163
1164 Vector<RawState> mRawStatesPending;
1165 RawState mCurrentRawState;
1166 CookedState mCurrentCookedState;
1167 RawState mLastRawState;
1168 CookedState mLastCookedState;
1169
1170 // State provided by an external stylus
1171 StylusState mExternalStylusState;
1172 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001173 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001174 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175
1176 // True if we sent a HOVER_ENTER event.
1177 bool mSentHoverEnter;
1178
Michael Wright842500e2015-03-13 17:32:02 -07001179 // Have we assigned pointer IDs for this stream
1180 bool mHavePointerIds;
1181
Michael Wright8e812822015-06-22 16:18:21 +01001182 // Is the current stream of direct touch events aborted
1183 bool mCurrentMotionAborted;
1184
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 // The time the primary pointer last went down.
1186 nsecs_t mDownTime;
1187
1188 // The pointer controller, or null if the device is not a pointer.
1189 sp<PointerControllerInterface> mPointerController;
1190
1191 Vector<VirtualKey> mVirtualKeys;
1192
1193 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001194 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001196 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001198 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001200 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 virtual void parseCalibration();
1202 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001203 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001204 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001205 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001206 virtual void resolveExternalStylusPresence();
1207 virtual bool hasStylus() const = 0;
1208 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209
Michael Wright842500e2015-03-13 17:32:02 -07001210 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211
1212private:
1213 // The current viewport.
1214 // The components of the viewport are specified in the display's rotated orientation.
1215 DisplayViewport mViewport;
1216
1217 // The surface orientation, width and height set by configureSurface().
1218 // The width and height are derived from the viewport but are specified
1219 // in the natural orientation.
1220 // The surface origin specifies how the surface coordinates should be translated
1221 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 int32_t mSurfaceWidth;
1223 int32_t mSurfaceHeight;
1224 int32_t mSurfaceLeft;
1225 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001226
1227 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1228 // the logical coordinate space.
1229 int32_t mPhysicalWidth;
1230 int32_t mPhysicalHeight;
1231 int32_t mPhysicalLeft;
1232 int32_t mPhysicalTop;
1233
1234 // The orientation may be different from the viewport orientation as it specifies
1235 // the rotation of the surface coordinates required to produce the viewport's
1236 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001237 int32_t mSurfaceOrientation;
1238
1239 // Translation and scaling factors, orientation-independent.
1240 float mXTranslate;
1241 float mXScale;
1242 float mXPrecision;
1243
1244 float mYTranslate;
1245 float mYScale;
1246 float mYPrecision;
1247
1248 float mGeometricScale;
1249
1250 float mPressureScale;
1251
1252 float mSizeScale;
1253
1254 float mOrientationScale;
1255
1256 float mDistanceScale;
1257
1258 bool mHaveTilt;
1259 float mTiltXCenter;
1260 float mTiltXScale;
1261 float mTiltYCenter;
1262 float mTiltYScale;
1263
Michael Wright842500e2015-03-13 17:32:02 -07001264 bool mExternalStylusConnected;
1265
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 // Oriented motion ranges for input device info.
1267 struct OrientedRanges {
1268 InputDeviceInfo::MotionRange x;
1269 InputDeviceInfo::MotionRange y;
1270 InputDeviceInfo::MotionRange pressure;
1271
1272 bool haveSize;
1273 InputDeviceInfo::MotionRange size;
1274
1275 bool haveTouchSize;
1276 InputDeviceInfo::MotionRange touchMajor;
1277 InputDeviceInfo::MotionRange touchMinor;
1278
1279 bool haveToolSize;
1280 InputDeviceInfo::MotionRange toolMajor;
1281 InputDeviceInfo::MotionRange toolMinor;
1282
1283 bool haveOrientation;
1284 InputDeviceInfo::MotionRange orientation;
1285
1286 bool haveDistance;
1287 InputDeviceInfo::MotionRange distance;
1288
1289 bool haveTilt;
1290 InputDeviceInfo::MotionRange tilt;
1291
1292 OrientedRanges() {
1293 clear();
1294 }
1295
1296 void clear() {
1297 haveSize = false;
1298 haveTouchSize = false;
1299 haveToolSize = false;
1300 haveOrientation = false;
1301 haveDistance = false;
1302 haveTilt = false;
1303 }
1304 } mOrientedRanges;
1305
1306 // Oriented dimensions and precision.
1307 float mOrientedXPrecision;
1308 float mOrientedYPrecision;
1309
1310 struct CurrentVirtualKeyState {
1311 bool down;
1312 bool ignored;
1313 nsecs_t downTime;
1314 int32_t keyCode;
1315 int32_t scanCode;
1316 } mCurrentVirtualKey;
1317
1318 // Scale factor for gesture or mouse based pointer movements.
1319 float mPointerXMovementScale;
1320 float mPointerYMovementScale;
1321
1322 // Scale factor for gesture based zooming and other freeform motions.
1323 float mPointerXZoomScale;
1324 float mPointerYZoomScale;
1325
1326 // The maximum swipe width.
1327 float mPointerGestureMaxSwipeWidth;
1328
1329 struct PointerDistanceHeapElement {
1330 uint32_t currentPointerIndex : 8;
1331 uint32_t lastPointerIndex : 8;
1332 uint64_t distance : 48; // squared distance
1333 };
1334
1335 enum PointerUsage {
1336 POINTER_USAGE_NONE,
1337 POINTER_USAGE_GESTURES,
1338 POINTER_USAGE_STYLUS,
1339 POINTER_USAGE_MOUSE,
1340 };
1341 PointerUsage mPointerUsage;
1342
1343 struct PointerGesture {
1344 enum Mode {
1345 // No fingers, button is not pressed.
1346 // Nothing happening.
1347 NEUTRAL,
1348
1349 // No fingers, button is not pressed.
1350 // Tap detected.
1351 // Emits DOWN and UP events at the pointer location.
1352 TAP,
1353
1354 // Exactly one finger dragging following a tap.
1355 // Pointer follows the active finger.
1356 // Emits DOWN, MOVE and UP events at the pointer location.
1357 //
1358 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1359 TAP_DRAG,
1360
1361 // Button is pressed.
1362 // Pointer follows the active finger if there is one. Other fingers are ignored.
1363 // Emits DOWN, MOVE and UP events at the pointer location.
1364 BUTTON_CLICK_OR_DRAG,
1365
1366 // Exactly one finger, button is not pressed.
1367 // Pointer follows the active finger.
1368 // Emits HOVER_MOVE events at the pointer location.
1369 //
1370 // Detect taps when the finger goes up while in HOVER mode.
1371 HOVER,
1372
1373 // Exactly two fingers but neither have moved enough to clearly indicate
1374 // whether a swipe or freeform gesture was intended. We consider the
1375 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1376 // Pointer does not move.
1377 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1378 PRESS,
1379
1380 // Exactly two fingers moving in the same direction, button is not pressed.
1381 // Pointer does not move.
1382 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1383 // follows the midpoint between both fingers.
1384 SWIPE,
1385
1386 // Two or more fingers moving in arbitrary directions, button is not pressed.
1387 // Pointer does not move.
1388 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1389 // each finger individually relative to the initial centroid of the finger.
1390 FREEFORM,
1391
1392 // Waiting for quiet time to end before starting the next gesture.
1393 QUIET,
1394 };
1395
1396 // Time the first finger went down.
1397 nsecs_t firstTouchTime;
1398
1399 // The active pointer id from the raw touch data.
1400 int32_t activeTouchId; // -1 if none
1401
1402 // The active pointer id from the gesture last delivered to the application.
1403 int32_t activeGestureId; // -1 if none
1404
1405 // Pointer coords and ids for the current and previous pointer gesture.
1406 Mode currentGestureMode;
1407 BitSet32 currentGestureIdBits;
1408 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1409 PointerProperties currentGestureProperties[MAX_POINTERS];
1410 PointerCoords currentGestureCoords[MAX_POINTERS];
1411
1412 Mode lastGestureMode;
1413 BitSet32 lastGestureIdBits;
1414 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1415 PointerProperties lastGestureProperties[MAX_POINTERS];
1416 PointerCoords lastGestureCoords[MAX_POINTERS];
1417
1418 // Time the pointer gesture last went down.
1419 nsecs_t downTime;
1420
1421 // Time when the pointer went down for a TAP.
1422 nsecs_t tapDownTime;
1423
1424 // Time when the pointer went up for a TAP.
1425 nsecs_t tapUpTime;
1426
1427 // Location of initial tap.
1428 float tapX, tapY;
1429
1430 // Time we started waiting for quiescence.
1431 nsecs_t quietTime;
1432
1433 // Reference points for multitouch gestures.
1434 float referenceTouchX; // reference touch X/Y coordinates in surface units
1435 float referenceTouchY;
1436 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1437 float referenceGestureY;
1438
1439 // Distance that each pointer has traveled which has not yet been
1440 // subsumed into the reference gesture position.
1441 BitSet32 referenceIdBits;
1442 struct Delta {
1443 float dx, dy;
1444 };
1445 Delta referenceDeltas[MAX_POINTER_ID + 1];
1446
1447 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1448 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1449
1450 // A velocity tracker for determining whether to switch active pointers during drags.
1451 VelocityTracker velocityTracker;
1452
1453 void reset() {
1454 firstTouchTime = LLONG_MIN;
1455 activeTouchId = -1;
1456 activeGestureId = -1;
1457 currentGestureMode = NEUTRAL;
1458 currentGestureIdBits.clear();
1459 lastGestureMode = NEUTRAL;
1460 lastGestureIdBits.clear();
1461 downTime = 0;
1462 velocityTracker.clear();
1463 resetTap();
1464 resetQuietTime();
1465 }
1466
1467 void resetTap() {
1468 tapDownTime = LLONG_MIN;
1469 tapUpTime = LLONG_MIN;
1470 }
1471
1472 void resetQuietTime() {
1473 quietTime = LLONG_MIN;
1474 }
1475 } mPointerGesture;
1476
1477 struct PointerSimple {
1478 PointerCoords currentCoords;
1479 PointerProperties currentProperties;
1480 PointerCoords lastCoords;
1481 PointerProperties lastProperties;
1482
1483 // True if the pointer is down.
1484 bool down;
1485
1486 // True if the pointer is hovering.
1487 bool hovering;
1488
1489 // Time the pointer last went down.
1490 nsecs_t downTime;
1491
1492 void reset() {
1493 currentCoords.clear();
1494 currentProperties.clear();
1495 lastCoords.clear();
1496 lastProperties.clear();
1497 down = false;
1498 hovering = false;
1499 downTime = 0;
1500 }
1501 } mPointerSimple;
1502
1503 // The pointer and scroll velocity controls.
1504 VelocityControl mPointerVelocityControl;
1505 VelocityControl mWheelXVelocityControl;
1506 VelocityControl mWheelYVelocityControl;
1507
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001508 std::optional<DisplayViewport> findViewport();
1509
Michael Wright842500e2015-03-13 17:32:02 -07001510 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001511 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001512
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513 void sync(nsecs_t when);
1514
1515 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001516 void processRawTouches(bool timeout);
1517 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1519 int32_t keyEventAction, int32_t keyEventFlags);
1520
1521 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1522 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1523 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001524 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1525 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1526 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001527 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001528 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1531 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1532
1533 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1534 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1535 bool preparePointerGestures(nsecs_t when,
1536 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1537 bool isTimeout);
1538
1539 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1540 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1541
1542 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1543 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1544
1545 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1546 bool down, bool hovering);
1547 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1548
Michael Wright842500e2015-03-13 17:32:02 -07001549 bool assignExternalStylusId(const RawState& state, bool timeout);
1550 void applyExternalStylusButtonState(nsecs_t when);
1551 void applyExternalStylusTouchState(nsecs_t when);
1552
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553 // Dispatches a motion event.
1554 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1555 // method will take care of setting the index and transmuting the action to DOWN or UP
1556 // it is the first / last pointer to go down / up.
1557 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001558 int32_t action, int32_t actionButton,
1559 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001560 uint32_t deviceTimestamp,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561 const PointerProperties* properties, const PointerCoords* coords,
1562 const uint32_t* idToIndex, BitSet32 idBits,
1563 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1564
1565 // Updates pointer coords and properties for pointers with specified ids that have moved.
1566 // Returns true if any of them changed.
1567 bool updateMovedPointers(const PointerProperties* inProperties,
1568 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1569 PointerProperties* outProperties, PointerCoords* outCoords,
1570 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1571
1572 bool isPointInsideSurface(int32_t x, int32_t y);
1573 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1574
Michael Wright842500e2015-03-13 17:32:02 -07001575 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001576
1577 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578};
1579
1580
1581class SingleTouchInputMapper : public TouchInputMapper {
1582public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001583 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 virtual ~SingleTouchInputMapper();
1585
1586 virtual void reset(nsecs_t when);
1587 virtual void process(const RawEvent* rawEvent);
1588
1589protected:
Michael Wright842500e2015-03-13 17:32:02 -07001590 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 virtual void configureRawPointerAxes();
1592 virtual bool hasStylus() const;
1593
1594private:
1595 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1596};
1597
1598
1599class MultiTouchInputMapper : public TouchInputMapper {
1600public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001601 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 virtual ~MultiTouchInputMapper();
1603
1604 virtual void reset(nsecs_t when);
1605 virtual void process(const RawEvent* rawEvent);
1606
1607protected:
Michael Wright842500e2015-03-13 17:32:02 -07001608 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 virtual void configureRawPointerAxes();
1610 virtual bool hasStylus() const;
1611
1612private:
1613 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1614
1615 // Specifies the pointer id bits that are in use, and their associated tracking id.
1616 BitSet32 mPointerIdBits;
1617 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1618};
1619
Michael Wright842500e2015-03-13 17:32:02 -07001620class ExternalStylusInputMapper : public InputMapper {
1621public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001622 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001623 virtual ~ExternalStylusInputMapper() = default;
1624
1625 virtual uint32_t getSources();
1626 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001627 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001628 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1629 virtual void reset(nsecs_t when);
1630 virtual void process(const RawEvent* rawEvent);
1631 virtual void sync(nsecs_t when);
1632
1633private:
1634 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1635 RawAbsoluteAxisInfo mRawPressureAxis;
1636 TouchButtonAccumulator mTouchButtonAccumulator;
1637
1638 StylusState mStylusState;
1639};
1640
Michael Wrightd02c5b62014-02-10 15:10:22 -08001641
1642class JoystickInputMapper : public InputMapper {
1643public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001644 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 virtual ~JoystickInputMapper();
1646
1647 virtual uint32_t getSources();
1648 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001649 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1651 virtual void reset(nsecs_t when);
1652 virtual void process(const RawEvent* rawEvent);
1653
1654private:
1655 struct Axis {
1656 RawAbsoluteAxisInfo rawAxisInfo;
1657 AxisInfo axisInfo;
1658
1659 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1660
1661 float scale; // scale factor from raw to normalized values
1662 float offset; // offset to add after scaling for normalization
1663 float highScale; // scale factor from raw to normalized values of high split
1664 float highOffset; // offset to add after scaling for normalization of high split
1665
1666 float min; // normalized inclusive minimum
1667 float max; // normalized inclusive maximum
1668 float flat; // normalized flat region size
1669 float fuzz; // normalized error tolerance
1670 float resolution; // normalized resolution in units/mm
1671
1672 float filter; // filter out small variations of this size
1673 float currentValue; // current value
1674 float newValue; // most recent value
1675 float highCurrentValue; // current value of high split
1676 float highNewValue; // most recent value of high split
1677
1678 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1679 bool explicitlyMapped, float scale, float offset,
1680 float highScale, float highOffset,
1681 float min, float max, float flat, float fuzz, float resolution) {
1682 this->rawAxisInfo = rawAxisInfo;
1683 this->axisInfo = axisInfo;
1684 this->explicitlyMapped = explicitlyMapped;
1685 this->scale = scale;
1686 this->offset = offset;
1687 this->highScale = highScale;
1688 this->highOffset = highOffset;
1689 this->min = min;
1690 this->max = max;
1691 this->flat = flat;
1692 this->fuzz = fuzz;
1693 this->resolution = resolution;
1694 this->filter = 0;
1695 resetValue();
1696 }
1697
1698 void resetValue() {
1699 this->currentValue = 0;
1700 this->newValue = 0;
1701 this->highCurrentValue = 0;
1702 this->highNewValue = 0;
1703 }
1704 };
1705
1706 // Axes indexed by raw ABS_* axis index.
1707 KeyedVector<int32_t, Axis> mAxes;
1708
1709 void sync(nsecs_t when, bool force);
1710
1711 bool haveAxis(int32_t axisId);
1712 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1713 bool filterAxes(bool force);
1714
1715 static bool hasValueChangedSignificantly(float filter,
1716 float newValue, float currentValue, float min, float max);
1717 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1718 float newValue, float currentValue, float thresholdValue);
1719
1720 static bool isCenteredAxis(int32_t axis);
1721 static int32_t getCompatAxis(int32_t axis);
1722
1723 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1724 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1725 float value);
1726};
1727
1728} // namespace android
1729
1730#endif // _UI_INPUT_READER_H