blob: 11ef9347dce28255aa1d23791c28c530a5d8c215 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
20#include "EventHub.h"
21#include "PointerControllerInterface.h"
22#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include "InputReaderBase.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Santos Cordonfa5cf462017-04-05 10:37:00 -070025#include <input/DisplayViewport.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/Input.h>
27#include <input/VelocityControl.h>
28#include <input/VelocityTracker.h>
29#include <ui/DisplayInfo.h>
30#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070031#include <utils/Condition.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070032#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <utils/BitSet.h>
35
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010036#include <optional>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.h>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010039#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
43class InputDevice;
44class InputMapper;
45
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Michael Wright842500e2015-03-13 17:32:02 -070047struct StylusState {
48 /* Time the stylus event was received. */
49 nsecs_t when;
50 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
51 float pressure;
52 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
53 uint32_t buttons;
54 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
55 int32_t toolType;
56
57 void copyFrom(const StylusState& other) {
58 when = other.when;
59 pressure = other.pressure;
60 buttons = other.buttons;
61 toolType = other.toolType;
62 }
63
64 void clear() {
65 when = LLONG_MAX;
66 pressure = 0.f;
67 buttons = 0;
68 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
69 }
70};
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
73/* Internal interface used by individual input devices to access global input device state
74 * and parameters maintained by the input reader.
75 */
76class InputReaderContext {
77public:
78 InputReaderContext() { }
79 virtual ~InputReaderContext() { }
80
81 virtual void updateGlobalMetaState() = 0;
82 virtual int32_t getGlobalMetaState() = 0;
83
84 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
85 virtual bool shouldDropVirtualKey(nsecs_t now,
86 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
87
88 virtual void fadePointer() = 0;
89
90 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
91 virtual int32_t bumpGeneration() = 0;
92
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080093 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) = 0;
Michael Wrightb85401d2015-04-17 18:35:15 +010094 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -070095
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 virtual InputReaderPolicyInterface* getPolicy() = 0;
97 virtual InputListenerInterface* getListener() = 0;
98 virtual EventHubInterface* getEventHub() = 0;
Prabir Pradhan42611e02018-11-27 14:04:02 -080099
100 virtual uint32_t getNextSequenceNum() = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101};
102
103
104/* The input reader reads raw event data from the event hub and processes it into input events
105 * that it sends to the input listener. Some functions of the input reader, such as early
106 * event filtering in low power states, are controlled by a separate policy object.
107 *
108 * The InputReader owns a collection of InputMappers. Most of the work it does happens
109 * on the input reader thread but the InputReader can receive queries from other system
110 * components running on arbitrary threads. To keep things manageable, the InputReader
111 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
112 * EventHub or the InputReaderPolicy but it is never held while calling into the
113 * InputListener.
114 */
115class InputReader : public InputReaderInterface {
116public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700117 InputReader(std::shared_ptr<EventHubInterface> eventHub,
118 const sp<InputReaderPolicyInterface>& policy,
119 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120 virtual ~InputReader();
121
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800122 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 virtual void monitor();
124
125 virtual void loopOnce();
126
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800127 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700129 virtual bool isInputDeviceEnabled(int32_t deviceId);
130
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
132 int32_t scanCode);
133 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
134 int32_t keyCode);
135 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
136 int32_t sw);
137
Andrii Kulian763a3a42016-03-08 10:46:16 -0800138 virtual void toggleCapsLockState(int32_t deviceId);
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
142
143 virtual void requestRefreshConfiguration(uint32_t changes);
144
145 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
146 ssize_t repeat, int32_t token);
147 virtual void cancelVibrate(int32_t deviceId, int32_t token);
148
Arthur Hungc23540e2018-11-29 20:42:11 +0800149 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150protected:
151 // These members are protected so they can be instrumented by test cases.
152 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
153 const InputDeviceIdentifier& identifier, uint32_t classes);
154
155 class ContextImpl : public InputReaderContext {
156 InputReader* mReader;
157
158 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700159 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160
161 virtual void updateGlobalMetaState();
162 virtual int32_t getGlobalMetaState();
163 virtual void disableVirtualKeysUntil(nsecs_t time);
164 virtual bool shouldDropVirtualKey(nsecs_t now,
165 InputDevice* device, int32_t keyCode, int32_t scanCode);
166 virtual void fadePointer();
167 virtual void requestTimeoutAtTime(nsecs_t when);
168 virtual int32_t bumpGeneration();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700170 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 virtual InputReaderPolicyInterface* getPolicy();
172 virtual InputListenerInterface* getListener();
173 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800174 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 } mContext;
176
177 friend class ContextImpl;
178
179private:
180 Mutex mLock;
181
182 Condition mReaderIsAliveCondition;
183
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700184 // This could be unique_ptr, but due to the way InputReader tests are written,
185 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
186 // in parallel to passing it to the InputReader.
187 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 sp<InputReaderPolicyInterface> mPolicy;
189 sp<QueuedInputListener> mQueuedListener;
190
191 InputReaderConfiguration mConfig;
192
Prabir Pradhan42611e02018-11-27 14:04:02 -0800193 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
194 uint32_t mNextSequenceNum;
195
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 // The event queue.
197 static const int EVENT_BUFFER_SIZE = 256;
198 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
199
200 KeyedVector<int32_t, InputDevice*> mDevices;
201
202 // low-level input event decoding and device management
203 void processEventsLocked(const RawEvent* rawEvents, size_t count);
204
205 void addDeviceLocked(nsecs_t when, int32_t deviceId);
206 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
207 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
208 void timeoutExpiredLocked(nsecs_t when);
209
210 void handleConfigurationChangedLocked(nsecs_t when);
211
212 int32_t mGlobalMetaState;
213 void updateGlobalMetaStateLocked();
214 int32_t getGlobalMetaStateLocked();
215
Michael Wright842500e2015-03-13 17:32:02 -0700216 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800217 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700218 void dispatchExternalStylusState(const StylusState& state);
219
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 void fadePointerLocked();
221
222 int32_t mGeneration;
223 int32_t bumpGenerationLocked();
224
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800225 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226
227 nsecs_t mDisableVirtualKeysTimeout;
228 void disableVirtualKeysUntilLocked(nsecs_t time);
229 bool shouldDropVirtualKeyLocked(nsecs_t now,
230 InputDevice* device, int32_t keyCode, int32_t scanCode);
231
232 nsecs_t mNextTimeout;
233 void requestTimeoutAtTimeLocked(nsecs_t when);
234
235 uint32_t mConfigurationChangesToRefresh;
236 void refreshConfigurationLocked(uint32_t changes);
237
238 // state queries
239 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
240 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
241 GetStateFunc getStateFunc);
242 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
243 const int32_t* keyCodes, uint8_t* outFlags);
244};
245
246
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247/* Represents the state of a single input device. */
248class InputDevice {
249public:
250 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
251 controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
252 ~InputDevice();
253
254 inline InputReaderContext* getContext() { return mContext; }
255 inline int32_t getId() const { return mId; }
256 inline int32_t getControllerNumber() const { return mControllerNumber; }
257 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100258 inline const std::string getName() const { return mIdentifier.name; }
259 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 inline uint32_t getClasses() const { return mClasses; }
261 inline uint32_t getSources() const { return mSources; }
262
263 inline bool isExternal() { return mIsExternal; }
264 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700265 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
266 return mAssociatedDisplayPort;
267 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268
Tim Kilbourn063ff532015-04-08 10:26:18 -0700269 inline void setMic(bool hasMic) { mHasMic = hasMic; }
270 inline bool hasMic() const { return mHasMic; }
271
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 inline bool isIgnored() { return mMappers.empty(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700274 bool isEnabled();
275 void setEnabled(bool enabled, nsecs_t when);
276
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800277 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 void addMapper(InputMapper* mapper);
279 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
280 void reset(nsecs_t when);
281 void process(const RawEvent* rawEvents, size_t count);
282 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700283 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
286 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
287 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
288 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
289 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
290 const int32_t* keyCodes, uint8_t* outFlags);
291 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
292 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800293 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294
295 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800296 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297
298 void fadePointer();
299
300 void bumpGeneration();
301
302 void notifyReset(nsecs_t when);
303
304 inline const PropertyMap& getConfiguration() { return mConfiguration; }
305 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
306
307 bool hasKey(int32_t code) {
308 return getEventHub()->hasScanCode(mId, code);
309 }
310
311 bool hasAbsoluteAxis(int32_t code) {
312 RawAbsoluteAxisInfo info;
313 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
314 return info.valid;
315 }
316
317 bool isKeyPressed(int32_t code) {
318 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
319 }
320
321 int32_t getAbsoluteAxisValue(int32_t code) {
322 int32_t value;
323 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
324 return value;
325 }
326
Arthur Hungc23540e2018-11-29 20:42:11 +0800327 std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328private:
329 InputReaderContext* mContext;
330 int32_t mId;
331 int32_t mGeneration;
332 int32_t mControllerNumber;
333 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100334 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 uint32_t mClasses;
336
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800337 std::vector<InputMapper*> mMappers;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
339 uint32_t mSources;
340 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700341 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700342 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 bool mDropUntilNextSync;
344
345 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
346 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
347
348 PropertyMap mConfiguration;
349};
350
351
352/* Keeps track of the state of mouse or touch pad buttons. */
353class CursorButtonAccumulator {
354public:
355 CursorButtonAccumulator();
356 void reset(InputDevice* device);
357
358 void process(const RawEvent* rawEvent);
359
360 uint32_t getButtonState() const;
361
362private:
363 bool mBtnLeft;
364 bool mBtnRight;
365 bool mBtnMiddle;
366 bool mBtnBack;
367 bool mBtnSide;
368 bool mBtnForward;
369 bool mBtnExtra;
370 bool mBtnTask;
371
372 void clearButtons();
373};
374
375
376/* Keeps track of cursor movements. */
377
378class CursorMotionAccumulator {
379public:
380 CursorMotionAccumulator();
381 void reset(InputDevice* device);
382
383 void process(const RawEvent* rawEvent);
384 void finishSync();
385
386 inline int32_t getRelativeX() const { return mRelX; }
387 inline int32_t getRelativeY() const { return mRelY; }
388
389private:
390 int32_t mRelX;
391 int32_t mRelY;
392
393 void clearRelativeAxes();
394};
395
396
397/* Keeps track of cursor scrolling motions. */
398
399class CursorScrollAccumulator {
400public:
401 CursorScrollAccumulator();
402 void configure(InputDevice* device);
403 void reset(InputDevice* device);
404
405 void process(const RawEvent* rawEvent);
406 void finishSync();
407
408 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
409 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
410
411 inline int32_t getRelativeX() const { return mRelX; }
412 inline int32_t getRelativeY() const { return mRelY; }
413 inline int32_t getRelativeVWheel() const { return mRelWheel; }
414 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
415
416private:
417 bool mHaveRelWheel;
418 bool mHaveRelHWheel;
419
420 int32_t mRelX;
421 int32_t mRelY;
422 int32_t mRelWheel;
423 int32_t mRelHWheel;
424
425 void clearRelativeAxes();
426};
427
428
429/* Keeps track of the state of touch, stylus and tool buttons. */
430class TouchButtonAccumulator {
431public:
432 TouchButtonAccumulator();
433 void configure(InputDevice* device);
434 void reset(InputDevice* device);
435
436 void process(const RawEvent* rawEvent);
437
438 uint32_t getButtonState() const;
439 int32_t getToolType() const;
440 bool isToolActive() const;
441 bool isHovering() const;
442 bool hasStylus() const;
443
444private:
445 bool mHaveBtnTouch;
446 bool mHaveStylus;
447
448 bool mBtnTouch;
449 bool mBtnStylus;
450 bool mBtnStylus2;
451 bool mBtnToolFinger;
452 bool mBtnToolPen;
453 bool mBtnToolRubber;
454 bool mBtnToolBrush;
455 bool mBtnToolPencil;
456 bool mBtnToolAirbrush;
457 bool mBtnToolMouse;
458 bool mBtnToolLens;
459 bool mBtnToolDoubleTap;
460 bool mBtnToolTripleTap;
461 bool mBtnToolQuadTap;
462
463 void clearButtons();
464};
465
466
467/* Raw axis information from the driver. */
468struct RawPointerAxes {
469 RawAbsoluteAxisInfo x;
470 RawAbsoluteAxisInfo y;
471 RawAbsoluteAxisInfo pressure;
472 RawAbsoluteAxisInfo touchMajor;
473 RawAbsoluteAxisInfo touchMinor;
474 RawAbsoluteAxisInfo toolMajor;
475 RawAbsoluteAxisInfo toolMinor;
476 RawAbsoluteAxisInfo orientation;
477 RawAbsoluteAxisInfo distance;
478 RawAbsoluteAxisInfo tiltX;
479 RawAbsoluteAxisInfo tiltY;
480 RawAbsoluteAxisInfo trackingId;
481 RawAbsoluteAxisInfo slot;
482
483 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800484 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
485 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 void clear();
487};
488
489
490/* Raw data for a collection of pointers including a pointer id mapping table. */
491struct RawPointerData {
492 struct Pointer {
493 uint32_t id;
494 int32_t x;
495 int32_t y;
496 int32_t pressure;
497 int32_t touchMajor;
498 int32_t touchMinor;
499 int32_t toolMajor;
500 int32_t toolMinor;
501 int32_t orientation;
502 int32_t distance;
503 int32_t tiltX;
504 int32_t tiltY;
505 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
506 bool isHovering;
507 };
508
509 uint32_t pointerCount;
510 Pointer pointers[MAX_POINTERS];
511 BitSet32 hoveringIdBits, touchingIdBits;
512 uint32_t idToIndex[MAX_POINTER_ID + 1];
513
514 RawPointerData();
515 void clear();
516 void copyFrom(const RawPointerData& other);
517 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
518
519 inline void markIdBit(uint32_t id, bool isHovering) {
520 if (isHovering) {
521 hoveringIdBits.markBit(id);
522 } else {
523 touchingIdBits.markBit(id);
524 }
525 }
526
527 inline void clearIdBits() {
528 hoveringIdBits.clear();
529 touchingIdBits.clear();
530 }
531
532 inline const Pointer& pointerForId(uint32_t id) const {
533 return pointers[idToIndex[id]];
534 }
535
536 inline bool isHovering(uint32_t pointerIndex) {
537 return pointers[pointerIndex].isHovering;
538 }
539};
540
541
542/* Cooked data for a collection of pointers including a pointer id mapping table. */
543struct CookedPointerData {
544 uint32_t pointerCount;
545 PointerProperties pointerProperties[MAX_POINTERS];
546 PointerCoords pointerCoords[MAX_POINTERS];
547 BitSet32 hoveringIdBits, touchingIdBits;
548 uint32_t idToIndex[MAX_POINTER_ID + 1];
549
550 CookedPointerData();
551 void clear();
552 void copyFrom(const CookedPointerData& other);
553
554 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
555 return pointerCoords[idToIndex[id]];
556 }
557
Michael Wright842500e2015-03-13 17:32:02 -0700558 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
559 return pointerCoords[idToIndex[id]];
560 }
561
562 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
563 return pointerProperties[idToIndex[id]];
564 }
565
Michael Wright53dca3a2015-04-23 17:39:53 +0100566 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
568 }
Michael Wright842500e2015-03-13 17:32:02 -0700569
Michael Wright53dca3a2015-04-23 17:39:53 +0100570 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700571 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
572 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573};
574
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -0800575/**
576 * Basic statistics information.
577 * Keep track of min, max, average, and standard deviation of the received samples.
578 * Used to report latency information about input events.
579 */
580struct LatencyStatistics {
581 float min;
582 float max;
583 // Sum of all samples
584 float sum;
585 // Sum of squares of all samples
586 float sum2;
587 // The number of samples
588 size_t count;
589 // The last time statistics were reported.
590 nsecs_t lastReportTime;
591
592 LatencyStatistics() {
593 reset(systemTime(SYSTEM_TIME_MONOTONIC));
594 }
595
596 inline void addValue(float x) {
597 if (x < min) {
598 min = x;
599 }
600 if (x > max) {
601 max = x;
602 }
603 sum += x;
604 sum2 += x * x;
605 count++;
606 }
607
608 // Get the average value. Should not be called if no samples have been added.
609 inline float mean() {
610 if (count == 0) {
611 return 0;
612 }
613 return sum / count;
614 }
615
616 // Get the standard deviation. Should not be called if no samples have been added.
617 inline float stdev() {
618 if (count == 0) {
619 return 0;
620 }
621 float average = mean();
622 return sqrt(sum2 / count - average * average);
623 }
624
625 /**
626 * Reset internal state. The variable 'when' is the time when the data collection started.
627 * Call this to start a new data collection window.
628 */
629 inline void reset(nsecs_t when) {
630 max = 0;
631 min = std::numeric_limits<float>::max();
632 sum = 0;
633 sum2 = 0;
634 count = 0;
635 lastReportTime = when;
636 }
637};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638
639/* Keeps track of the state of single-touch protocol. */
640class SingleTouchMotionAccumulator {
641public:
642 SingleTouchMotionAccumulator();
643
644 void process(const RawEvent* rawEvent);
645 void reset(InputDevice* device);
646
647 inline int32_t getAbsoluteX() const { return mAbsX; }
648 inline int32_t getAbsoluteY() const { return mAbsY; }
649 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
650 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
651 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
652 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
653 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
654
655private:
656 int32_t mAbsX;
657 int32_t mAbsY;
658 int32_t mAbsPressure;
659 int32_t mAbsToolWidth;
660 int32_t mAbsDistance;
661 int32_t mAbsTiltX;
662 int32_t mAbsTiltY;
663
664 void clearAbsoluteAxes();
665};
666
667
668/* Keeps track of the state of multi-touch protocol. */
669class MultiTouchMotionAccumulator {
670public:
671 class Slot {
672 public:
673 inline bool isInUse() const { return mInUse; }
674 inline int32_t getX() const { return mAbsMTPositionX; }
675 inline int32_t getY() const { return mAbsMTPositionY; }
676 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
677 inline int32_t getTouchMinor() const {
678 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
679 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
680 inline int32_t getToolMinor() const {
681 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
682 inline int32_t getOrientation() const { return mAbsMTOrientation; }
683 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
684 inline int32_t getPressure() const { return mAbsMTPressure; }
685 inline int32_t getDistance() const { return mAbsMTDistance; }
686 inline int32_t getToolType() const;
687
688 private:
689 friend class MultiTouchMotionAccumulator;
690
691 bool mInUse;
692 bool mHaveAbsMTTouchMinor;
693 bool mHaveAbsMTWidthMinor;
694 bool mHaveAbsMTToolType;
695
696 int32_t mAbsMTPositionX;
697 int32_t mAbsMTPositionY;
698 int32_t mAbsMTTouchMajor;
699 int32_t mAbsMTTouchMinor;
700 int32_t mAbsMTWidthMajor;
701 int32_t mAbsMTWidthMinor;
702 int32_t mAbsMTOrientation;
703 int32_t mAbsMTTrackingId;
704 int32_t mAbsMTPressure;
705 int32_t mAbsMTDistance;
706 int32_t mAbsMTToolType;
707
708 Slot();
709 void clear();
710 };
711
712 MultiTouchMotionAccumulator();
713 ~MultiTouchMotionAccumulator();
714
715 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
716 void reset(InputDevice* device);
717 void process(const RawEvent* rawEvent);
718 void finishSync();
719 bool hasStylus() const;
720
721 inline size_t getSlotCount() const { return mSlotCount; }
722 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
723
724private:
725 int32_t mCurrentSlot;
726 Slot* mSlots;
727 size_t mSlotCount;
728 bool mUsingSlotsProtocol;
729 bool mHaveStylus;
730
731 void clearSlots(int32_t initialSlot);
732};
733
734
735/* An input mapper transforms raw input events into cooked event data.
736 * A single input device can have multiple associated input mappers in order to interpret
737 * different classes of events.
738 *
739 * InputMapper lifecycle:
740 * - create
741 * - configure with 0 changes
742 * - reset
743 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
744 * - reset
745 * - destroy
746 */
747class InputMapper {
748public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700749 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 virtual ~InputMapper();
751
752 inline InputDevice* getDevice() { return mDevice; }
753 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100754 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 inline InputReaderContext* getContext() { return mContext; }
756 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
757 inline InputListenerInterface* getListener() { return mContext->getListener(); }
758 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
759
760 virtual uint32_t getSources() = 0;
761 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800762 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
764 virtual void reset(nsecs_t when);
765 virtual void process(const RawEvent* rawEvent) = 0;
766 virtual void timeoutExpired(nsecs_t when);
767
768 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
769 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
770 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
771 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
772 const int32_t* keyCodes, uint8_t* outFlags);
773 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
774 int32_t token);
775 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800776 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777
778 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800779 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780
Michael Wright842500e2015-03-13 17:32:02 -0700781 virtual void updateExternalStylusState(const StylusState& state);
782
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 virtual void fadePointer();
Arthur Hungc23540e2018-11-29 20:42:11 +0800784 virtual std::optional<int32_t> getAssociatedDisplay() {
785 return std::nullopt;
786 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787protected:
788 InputDevice* mDevice;
789 InputReaderContext* mContext;
790
791 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
792 void bumpGeneration();
793
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800794 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800796 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797};
798
799
800class SwitchInputMapper : public InputMapper {
801public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700802 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 virtual ~SwitchInputMapper();
804
805 virtual uint32_t getSources();
806 virtual void process(const RawEvent* rawEvent);
807
808 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800809 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700812 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 uint32_t mUpdatedSwitchMask;
814
815 void processSwitch(int32_t switchCode, int32_t switchValue);
816 void sync(nsecs_t when);
817};
818
819
820class VibratorInputMapper : public InputMapper {
821public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700822 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 virtual ~VibratorInputMapper();
824
825 virtual uint32_t getSources();
826 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
827 virtual void process(const RawEvent* rawEvent);
828
829 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
830 int32_t token);
831 virtual void cancelVibrate(int32_t token);
832 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800833 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
835private:
836 bool mVibrating;
837 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
838 size_t mPatternSize;
839 ssize_t mRepeat;
840 int32_t mToken;
841 ssize_t mIndex;
842 nsecs_t mNextStepTime;
843
844 void nextStep();
845 void stopVibrating();
846};
847
848
849class KeyboardInputMapper : public InputMapper {
850public:
851 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
852 virtual ~KeyboardInputMapper();
853
854 virtual uint32_t getSources();
855 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800856 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
858 virtual void reset(nsecs_t when);
859 virtual void process(const RawEvent* rawEvent);
860
861 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
862 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
863 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
864 const int32_t* keyCodes, uint8_t* outFlags);
865
866 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800867 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
869private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100870 // The current viewport.
871 std::optional<DisplayViewport> mViewport;
872
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 struct KeyDown {
874 int32_t keyCode;
875 int32_t scanCode;
876 };
877
878 uint32_t mSource;
879 int32_t mKeyboardType;
880
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800881 std::vector<KeyDown> mKeyDowns; // keys that are down
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 int32_t mMetaState;
883 nsecs_t mDownTime; // time of most recent key down
884
885 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
886
887 struct LedState {
888 bool avail; // led is available
889 bool on; // we think the led is currently on
890 };
891 LedState mCapsLockLedState;
892 LedState mNumLockLedState;
893 LedState mScrollLockLedState;
894
895 // Immutable configuration parameters.
896 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700898 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 } mParameters;
900
901 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800902 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100904 int32_t getOrientation();
905 int32_t getDisplayId();
906
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100908 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700910 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
Andrii Kulian763a3a42016-03-08 10:46:16 -0800912 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
913
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 ssize_t findKeyDown(int32_t scanCode);
915
916 void resetLedState();
917 void initializeLedState(LedState& ledState, int32_t led);
918 void updateLedState(bool reset);
919 void updateLedStateForModifier(LedState& ledState, int32_t led,
920 int32_t modifier, bool reset);
921};
922
923
924class CursorInputMapper : public InputMapper {
925public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700926 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 virtual ~CursorInputMapper();
928
929 virtual uint32_t getSources();
930 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800931 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
933 virtual void reset(nsecs_t when);
934 virtual void process(const RawEvent* rawEvent);
935
936 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
937
938 virtual void fadePointer();
939
Arthur Hungc23540e2018-11-29 20:42:11 +0800940 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941private:
942 // Amount that trackball needs to move in order to generate a key event.
943 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
944
945 // Immutable configuration parameters.
946 struct Parameters {
947 enum Mode {
948 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800949 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 MODE_NAVIGATION,
951 };
952
953 Mode mode;
954 bool hasAssociatedDisplay;
955 bool orientationAware;
956 } mParameters;
957
958 CursorButtonAccumulator mCursorButtonAccumulator;
959 CursorMotionAccumulator mCursorMotionAccumulator;
960 CursorScrollAccumulator mCursorScrollAccumulator;
961
962 int32_t mSource;
963 float mXScale;
964 float mYScale;
965 float mXPrecision;
966 float mYPrecision;
967
968 float mVWheelScale;
969 float mHWheelScale;
970
971 // Velocity controls for mouse pointer and wheel movements.
972 // The controls for X and Y wheel movements are separate to keep them decoupled.
973 VelocityControl mPointerVelocityControl;
974 VelocityControl mWheelXVelocityControl;
975 VelocityControl mWheelYVelocityControl;
976
977 int32_t mOrientation;
978
979 sp<PointerControllerInterface> mPointerController;
980
981 int32_t mButtonState;
982 nsecs_t mDownTime;
983
984 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800985 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986
987 void sync(nsecs_t when);
988};
989
990
Prashant Malani1941ff52015-08-11 18:29:28 -0700991class RotaryEncoderInputMapper : public InputMapper {
992public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700993 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700994 virtual ~RotaryEncoderInputMapper();
995
996 virtual uint32_t getSources();
997 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800998 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700999 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1000 virtual void reset(nsecs_t when);
1001 virtual void process(const RawEvent* rawEvent);
1002
1003private:
1004 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
1005
1006 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -08001007 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +01001008 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -07001009
1010 void sync(nsecs_t when);
1011};
1012
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013class TouchInputMapper : public InputMapper {
1014public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001015 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 virtual ~TouchInputMapper();
1017
1018 virtual uint32_t getSources();
1019 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001020 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1022 virtual void reset(nsecs_t when);
1023 virtual void process(const RawEvent* rawEvent);
1024
1025 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1026 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1027 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1028 const int32_t* keyCodes, uint8_t* outFlags);
1029
1030 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001031 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001033 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hungc23540e2018-11-29 20:42:11 +08001034 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035protected:
1036 CursorButtonAccumulator mCursorButtonAccumulator;
1037 CursorScrollAccumulator mCursorScrollAccumulator;
1038 TouchButtonAccumulator mTouchButtonAccumulator;
1039
1040 struct VirtualKey {
1041 int32_t keyCode;
1042 int32_t scanCode;
1043 uint32_t flags;
1044
1045 // computed hit box, specified in touch screen coords based on known display size
1046 int32_t hitLeft;
1047 int32_t hitTop;
1048 int32_t hitRight;
1049 int32_t hitBottom;
1050
1051 inline bool isHit(int32_t x, int32_t y) const {
1052 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1053 }
1054 };
1055
1056 // Input sources and device mode.
1057 uint32_t mSource;
1058
1059 enum DeviceMode {
1060 DEVICE_MODE_DISABLED, // input is disabled
1061 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1062 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1063 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1064 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1065 };
1066 DeviceMode mDeviceMode;
1067
1068 // The reader's configuration.
1069 InputReaderConfiguration mConfig;
1070
1071 // Immutable configuration parameters.
1072 struct Parameters {
1073 enum DeviceType {
1074 DEVICE_TYPE_TOUCH_SCREEN,
1075 DEVICE_TYPE_TOUCH_PAD,
1076 DEVICE_TYPE_TOUCH_NAVIGATION,
1077 DEVICE_TYPE_POINTER,
1078 };
1079
1080 DeviceType deviceType;
1081 bool hasAssociatedDisplay;
1082 bool associatedDisplayIsExternal;
1083 bool orientationAware;
1084 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001085 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086
1087 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001088 GESTURE_MODE_SINGLE_TOUCH,
1089 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 };
1091 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001092
1093 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 } mParameters;
1095
1096 // Immutable calibration parameters in parsed form.
1097 struct Calibration {
1098 // Size
1099 enum SizeCalibration {
1100 SIZE_CALIBRATION_DEFAULT,
1101 SIZE_CALIBRATION_NONE,
1102 SIZE_CALIBRATION_GEOMETRIC,
1103 SIZE_CALIBRATION_DIAMETER,
1104 SIZE_CALIBRATION_BOX,
1105 SIZE_CALIBRATION_AREA,
1106 };
1107
1108 SizeCalibration sizeCalibration;
1109
1110 bool haveSizeScale;
1111 float sizeScale;
1112 bool haveSizeBias;
1113 float sizeBias;
1114 bool haveSizeIsSummed;
1115 bool sizeIsSummed;
1116
1117 // Pressure
1118 enum PressureCalibration {
1119 PRESSURE_CALIBRATION_DEFAULT,
1120 PRESSURE_CALIBRATION_NONE,
1121 PRESSURE_CALIBRATION_PHYSICAL,
1122 PRESSURE_CALIBRATION_AMPLITUDE,
1123 };
1124
1125 PressureCalibration pressureCalibration;
1126 bool havePressureScale;
1127 float pressureScale;
1128
1129 // Orientation
1130 enum OrientationCalibration {
1131 ORIENTATION_CALIBRATION_DEFAULT,
1132 ORIENTATION_CALIBRATION_NONE,
1133 ORIENTATION_CALIBRATION_INTERPOLATED,
1134 ORIENTATION_CALIBRATION_VECTOR,
1135 };
1136
1137 OrientationCalibration orientationCalibration;
1138
1139 // Distance
1140 enum DistanceCalibration {
1141 DISTANCE_CALIBRATION_DEFAULT,
1142 DISTANCE_CALIBRATION_NONE,
1143 DISTANCE_CALIBRATION_SCALED,
1144 };
1145
1146 DistanceCalibration distanceCalibration;
1147 bool haveDistanceScale;
1148 float distanceScale;
1149
1150 enum CoverageCalibration {
1151 COVERAGE_CALIBRATION_DEFAULT,
1152 COVERAGE_CALIBRATION_NONE,
1153 COVERAGE_CALIBRATION_BOX,
1154 };
1155
1156 CoverageCalibration coverageCalibration;
1157
1158 inline void applySizeScaleAndBias(float* outSize) const {
1159 if (haveSizeScale) {
1160 *outSize *= sizeScale;
1161 }
1162 if (haveSizeBias) {
1163 *outSize += sizeBias;
1164 }
1165 if (*outSize < 0) {
1166 *outSize = 0;
1167 }
1168 }
1169 } mCalibration;
1170
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001171 // Affine location transformation/calibration
1172 struct TouchAffineTransformation mAffineTransform;
1173
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 RawPointerAxes mRawPointerAxes;
1175
Michael Wright842500e2015-03-13 17:32:02 -07001176 struct RawState {
1177 nsecs_t when;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Michael Wright842500e2015-03-13 17:32:02 -07001179 // Raw pointer sample data.
1180 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181
Michael Wright842500e2015-03-13 17:32:02 -07001182 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183
Michael Wright842500e2015-03-13 17:32:02 -07001184 // Scroll state.
1185 int32_t rawVScroll;
1186 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187
Michael Wright842500e2015-03-13 17:32:02 -07001188 void copyFrom(const RawState& other) {
1189 when = other.when;
1190 rawPointerData.copyFrom(other.rawPointerData);
1191 buttonState = other.buttonState;
1192 rawVScroll = other.rawVScroll;
1193 rawHScroll = other.rawHScroll;
1194 }
1195
1196 void clear() {
1197 when = 0;
1198 rawPointerData.clear();
1199 buttonState = 0;
1200 rawVScroll = 0;
1201 rawHScroll = 0;
1202 }
1203 };
1204
1205 struct CookedState {
1206 // Cooked pointer sample data.
1207 CookedPointerData cookedPointerData;
1208
1209 // Id bits used to differentiate fingers, stylus and mouse tools.
1210 BitSet32 fingerIdBits;
1211 BitSet32 stylusIdBits;
1212 BitSet32 mouseIdBits;
1213
Michael Wright7b159c92015-05-14 14:48:03 +01001214 int32_t buttonState;
1215
Michael Wright842500e2015-03-13 17:32:02 -07001216 void copyFrom(const CookedState& other) {
1217 cookedPointerData.copyFrom(other.cookedPointerData);
1218 fingerIdBits = other.fingerIdBits;
1219 stylusIdBits = other.stylusIdBits;
1220 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001221 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001222 }
1223
1224 void clear() {
1225 cookedPointerData.clear();
1226 fingerIdBits.clear();
1227 stylusIdBits.clear();
1228 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001229 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001230 }
1231 };
1232
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001233 std::vector<RawState> mRawStatesPending;
Michael Wright842500e2015-03-13 17:32:02 -07001234 RawState mCurrentRawState;
1235 CookedState mCurrentCookedState;
1236 RawState mLastRawState;
1237 CookedState mLastCookedState;
1238
1239 // State provided by an external stylus
1240 StylusState mExternalStylusState;
1241 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001242 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001243 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244
1245 // True if we sent a HOVER_ENTER event.
1246 bool mSentHoverEnter;
1247
Michael Wright842500e2015-03-13 17:32:02 -07001248 // Have we assigned pointer IDs for this stream
1249 bool mHavePointerIds;
1250
Michael Wright8e812822015-06-22 16:18:21 +01001251 // Is the current stream of direct touch events aborted
1252 bool mCurrentMotionAborted;
1253
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 // The time the primary pointer last went down.
1255 nsecs_t mDownTime;
1256
1257 // The pointer controller, or null if the device is not a pointer.
1258 sp<PointerControllerInterface> mPointerController;
1259
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001260 std::vector<VirtualKey> mVirtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261
1262 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001263 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001265 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001267 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001269 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 virtual void parseCalibration();
1271 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001272 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001273 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001274 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001275 virtual void resolveExternalStylusPresence();
1276 virtual bool hasStylus() const = 0;
1277 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278
Michael Wright842500e2015-03-13 17:32:02 -07001279 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
1281private:
1282 // The current viewport.
1283 // The components of the viewport are specified in the display's rotated orientation.
1284 DisplayViewport mViewport;
1285
1286 // The surface orientation, width and height set by configureSurface().
1287 // The width and height are derived from the viewport but are specified
1288 // in the natural orientation.
1289 // The surface origin specifies how the surface coordinates should be translated
1290 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001291 int32_t mSurfaceWidth;
1292 int32_t mSurfaceHeight;
1293 int32_t mSurfaceLeft;
1294 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001295
1296 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1297 // the logical coordinate space.
1298 int32_t mPhysicalWidth;
1299 int32_t mPhysicalHeight;
1300 int32_t mPhysicalLeft;
1301 int32_t mPhysicalTop;
1302
1303 // The orientation may be different from the viewport orientation as it specifies
1304 // the rotation of the surface coordinates required to produce the viewport's
1305 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 int32_t mSurfaceOrientation;
1307
1308 // Translation and scaling factors, orientation-independent.
1309 float mXTranslate;
1310 float mXScale;
1311 float mXPrecision;
1312
1313 float mYTranslate;
1314 float mYScale;
1315 float mYPrecision;
1316
1317 float mGeometricScale;
1318
1319 float mPressureScale;
1320
1321 float mSizeScale;
1322
1323 float mOrientationScale;
1324
1325 float mDistanceScale;
1326
1327 bool mHaveTilt;
1328 float mTiltXCenter;
1329 float mTiltXScale;
1330 float mTiltYCenter;
1331 float mTiltYScale;
1332
Michael Wright842500e2015-03-13 17:32:02 -07001333 bool mExternalStylusConnected;
1334
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 // Oriented motion ranges for input device info.
1336 struct OrientedRanges {
1337 InputDeviceInfo::MotionRange x;
1338 InputDeviceInfo::MotionRange y;
1339 InputDeviceInfo::MotionRange pressure;
1340
1341 bool haveSize;
1342 InputDeviceInfo::MotionRange size;
1343
1344 bool haveTouchSize;
1345 InputDeviceInfo::MotionRange touchMajor;
1346 InputDeviceInfo::MotionRange touchMinor;
1347
1348 bool haveToolSize;
1349 InputDeviceInfo::MotionRange toolMajor;
1350 InputDeviceInfo::MotionRange toolMinor;
1351
1352 bool haveOrientation;
1353 InputDeviceInfo::MotionRange orientation;
1354
1355 bool haveDistance;
1356 InputDeviceInfo::MotionRange distance;
1357
1358 bool haveTilt;
1359 InputDeviceInfo::MotionRange tilt;
1360
1361 OrientedRanges() {
1362 clear();
1363 }
1364
1365 void clear() {
1366 haveSize = false;
1367 haveTouchSize = false;
1368 haveToolSize = false;
1369 haveOrientation = false;
1370 haveDistance = false;
1371 haveTilt = false;
1372 }
1373 } mOrientedRanges;
1374
1375 // Oriented dimensions and precision.
1376 float mOrientedXPrecision;
1377 float mOrientedYPrecision;
1378
1379 struct CurrentVirtualKeyState {
1380 bool down;
1381 bool ignored;
1382 nsecs_t downTime;
1383 int32_t keyCode;
1384 int32_t scanCode;
1385 } mCurrentVirtualKey;
1386
1387 // Scale factor for gesture or mouse based pointer movements.
1388 float mPointerXMovementScale;
1389 float mPointerYMovementScale;
1390
1391 // Scale factor for gesture based zooming and other freeform motions.
1392 float mPointerXZoomScale;
1393 float mPointerYZoomScale;
1394
1395 // The maximum swipe width.
1396 float mPointerGestureMaxSwipeWidth;
1397
1398 struct PointerDistanceHeapElement {
1399 uint32_t currentPointerIndex : 8;
1400 uint32_t lastPointerIndex : 8;
1401 uint64_t distance : 48; // squared distance
1402 };
1403
1404 enum PointerUsage {
1405 POINTER_USAGE_NONE,
1406 POINTER_USAGE_GESTURES,
1407 POINTER_USAGE_STYLUS,
1408 POINTER_USAGE_MOUSE,
1409 };
1410 PointerUsage mPointerUsage;
1411
1412 struct PointerGesture {
1413 enum Mode {
1414 // No fingers, button is not pressed.
1415 // Nothing happening.
1416 NEUTRAL,
1417
1418 // No fingers, button is not pressed.
1419 // Tap detected.
1420 // Emits DOWN and UP events at the pointer location.
1421 TAP,
1422
1423 // Exactly one finger dragging following a tap.
1424 // Pointer follows the active finger.
1425 // Emits DOWN, MOVE and UP events at the pointer location.
1426 //
1427 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1428 TAP_DRAG,
1429
1430 // Button is pressed.
1431 // Pointer follows the active finger if there is one. Other fingers are ignored.
1432 // Emits DOWN, MOVE and UP events at the pointer location.
1433 BUTTON_CLICK_OR_DRAG,
1434
1435 // Exactly one finger, button is not pressed.
1436 // Pointer follows the active finger.
1437 // Emits HOVER_MOVE events at the pointer location.
1438 //
1439 // Detect taps when the finger goes up while in HOVER mode.
1440 HOVER,
1441
1442 // Exactly two fingers but neither have moved enough to clearly indicate
1443 // whether a swipe or freeform gesture was intended. We consider the
1444 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1445 // Pointer does not move.
1446 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1447 PRESS,
1448
1449 // Exactly two fingers moving in the same direction, button is not pressed.
1450 // Pointer does not move.
1451 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1452 // follows the midpoint between both fingers.
1453 SWIPE,
1454
1455 // Two or more fingers moving in arbitrary directions, button is not pressed.
1456 // Pointer does not move.
1457 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1458 // each finger individually relative to the initial centroid of the finger.
1459 FREEFORM,
1460
1461 // Waiting for quiet time to end before starting the next gesture.
1462 QUIET,
1463 };
1464
1465 // Time the first finger went down.
1466 nsecs_t firstTouchTime;
1467
1468 // The active pointer id from the raw touch data.
1469 int32_t activeTouchId; // -1 if none
1470
1471 // The active pointer id from the gesture last delivered to the application.
1472 int32_t activeGestureId; // -1 if none
1473
1474 // Pointer coords and ids for the current and previous pointer gesture.
1475 Mode currentGestureMode;
1476 BitSet32 currentGestureIdBits;
1477 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1478 PointerProperties currentGestureProperties[MAX_POINTERS];
1479 PointerCoords currentGestureCoords[MAX_POINTERS];
1480
1481 Mode lastGestureMode;
1482 BitSet32 lastGestureIdBits;
1483 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1484 PointerProperties lastGestureProperties[MAX_POINTERS];
1485 PointerCoords lastGestureCoords[MAX_POINTERS];
1486
1487 // Time the pointer gesture last went down.
1488 nsecs_t downTime;
1489
1490 // Time when the pointer went down for a TAP.
1491 nsecs_t tapDownTime;
1492
1493 // Time when the pointer went up for a TAP.
1494 nsecs_t tapUpTime;
1495
1496 // Location of initial tap.
1497 float tapX, tapY;
1498
1499 // Time we started waiting for quiescence.
1500 nsecs_t quietTime;
1501
1502 // Reference points for multitouch gestures.
1503 float referenceTouchX; // reference touch X/Y coordinates in surface units
1504 float referenceTouchY;
1505 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1506 float referenceGestureY;
1507
1508 // Distance that each pointer has traveled which has not yet been
1509 // subsumed into the reference gesture position.
1510 BitSet32 referenceIdBits;
1511 struct Delta {
1512 float dx, dy;
1513 };
1514 Delta referenceDeltas[MAX_POINTER_ID + 1];
1515
1516 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1517 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1518
1519 // A velocity tracker for determining whether to switch active pointers during drags.
1520 VelocityTracker velocityTracker;
1521
1522 void reset() {
1523 firstTouchTime = LLONG_MIN;
1524 activeTouchId = -1;
1525 activeGestureId = -1;
1526 currentGestureMode = NEUTRAL;
1527 currentGestureIdBits.clear();
1528 lastGestureMode = NEUTRAL;
1529 lastGestureIdBits.clear();
1530 downTime = 0;
1531 velocityTracker.clear();
1532 resetTap();
1533 resetQuietTime();
1534 }
1535
1536 void resetTap() {
1537 tapDownTime = LLONG_MIN;
1538 tapUpTime = LLONG_MIN;
1539 }
1540
1541 void resetQuietTime() {
1542 quietTime = LLONG_MIN;
1543 }
1544 } mPointerGesture;
1545
1546 struct PointerSimple {
1547 PointerCoords currentCoords;
1548 PointerProperties currentProperties;
1549 PointerCoords lastCoords;
1550 PointerProperties lastProperties;
1551
1552 // True if the pointer is down.
1553 bool down;
1554
1555 // True if the pointer is hovering.
1556 bool hovering;
1557
1558 // Time the pointer last went down.
1559 nsecs_t downTime;
1560
1561 void reset() {
1562 currentCoords.clear();
1563 currentProperties.clear();
1564 lastCoords.clear();
1565 lastProperties.clear();
1566 down = false;
1567 hovering = false;
1568 downTime = 0;
1569 }
1570 } mPointerSimple;
1571
1572 // The pointer and scroll velocity controls.
1573 VelocityControl mPointerVelocityControl;
1574 VelocityControl mWheelXVelocityControl;
1575 VelocityControl mWheelYVelocityControl;
1576
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001577 // Latency statistics for touch events
1578 struct LatencyStatistics mStatistics;
1579
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001580 std::optional<DisplayViewport> findViewport();
1581
Michael Wright842500e2015-03-13 17:32:02 -07001582 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001583 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001584
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 void sync(nsecs_t when);
1586
1587 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001588 void processRawTouches(bool timeout);
1589 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1591 int32_t keyEventAction, int32_t keyEventFlags);
1592
1593 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1594 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1595 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001596 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1597 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1598 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001600 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601
1602 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1603 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1604
1605 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1606 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1607 bool preparePointerGestures(nsecs_t when,
1608 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1609 bool isTimeout);
1610
1611 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1612 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1613
1614 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1615 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1616
1617 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1618 bool down, bool hovering);
1619 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1620
Michael Wright842500e2015-03-13 17:32:02 -07001621 bool assignExternalStylusId(const RawState& state, bool timeout);
1622 void applyExternalStylusButtonState(nsecs_t when);
1623 void applyExternalStylusTouchState(nsecs_t when);
1624
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 // Dispatches a motion event.
1626 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1627 // method will take care of setting the index and transmuting the action to DOWN or UP
1628 // it is the first / last pointer to go down / up.
1629 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001630 int32_t action, int32_t actionButton,
1631 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632 const PointerProperties* properties, const PointerCoords* coords,
1633 const uint32_t* idToIndex, BitSet32 idBits,
1634 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1635
1636 // Updates pointer coords and properties for pointers with specified ids that have moved.
1637 // Returns true if any of them changed.
1638 bool updateMovedPointers(const PointerProperties* inProperties,
1639 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1640 PointerProperties* outProperties, PointerCoords* outCoords,
1641 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1642
1643 bool isPointInsideSurface(int32_t x, int32_t y);
1644 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1645
Michael Wright842500e2015-03-13 17:32:02 -07001646 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001647
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001648 void reportEventForStatistics(nsecs_t evdevTime);
1649
Santos Cordonfa5cf462017-04-05 10:37:00 -07001650 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651};
1652
1653
1654class SingleTouchInputMapper : public TouchInputMapper {
1655public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001656 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 virtual ~SingleTouchInputMapper();
1658
1659 virtual void reset(nsecs_t when);
1660 virtual void process(const RawEvent* rawEvent);
1661
1662protected:
Michael Wright842500e2015-03-13 17:32:02 -07001663 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 virtual void configureRawPointerAxes();
1665 virtual bool hasStylus() const;
1666
1667private:
1668 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1669};
1670
1671
1672class MultiTouchInputMapper : public TouchInputMapper {
1673public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001674 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 virtual ~MultiTouchInputMapper();
1676
1677 virtual void reset(nsecs_t when);
1678 virtual void process(const RawEvent* rawEvent);
1679
1680protected:
Michael Wright842500e2015-03-13 17:32:02 -07001681 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 virtual void configureRawPointerAxes();
1683 virtual bool hasStylus() const;
1684
1685private:
1686 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1687
1688 // Specifies the pointer id bits that are in use, and their associated tracking id.
1689 BitSet32 mPointerIdBits;
1690 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1691};
1692
Michael Wright842500e2015-03-13 17:32:02 -07001693class ExternalStylusInputMapper : public InputMapper {
1694public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001695 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001696 virtual ~ExternalStylusInputMapper() = default;
1697
1698 virtual uint32_t getSources();
1699 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001700 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001701 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1702 virtual void reset(nsecs_t when);
1703 virtual void process(const RawEvent* rawEvent);
1704 virtual void sync(nsecs_t when);
1705
1706private:
1707 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1708 RawAbsoluteAxisInfo mRawPressureAxis;
1709 TouchButtonAccumulator mTouchButtonAccumulator;
1710
1711 StylusState mStylusState;
1712};
1713
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715class JoystickInputMapper : public InputMapper {
1716public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001717 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718 virtual ~JoystickInputMapper();
1719
1720 virtual uint32_t getSources();
1721 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001722 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1724 virtual void reset(nsecs_t when);
1725 virtual void process(const RawEvent* rawEvent);
1726
1727private:
1728 struct Axis {
1729 RawAbsoluteAxisInfo rawAxisInfo;
1730 AxisInfo axisInfo;
1731
1732 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1733
1734 float scale; // scale factor from raw to normalized values
1735 float offset; // offset to add after scaling for normalization
1736 float highScale; // scale factor from raw to normalized values of high split
1737 float highOffset; // offset to add after scaling for normalization of high split
1738
1739 float min; // normalized inclusive minimum
1740 float max; // normalized inclusive maximum
1741 float flat; // normalized flat region size
1742 float fuzz; // normalized error tolerance
1743 float resolution; // normalized resolution in units/mm
1744
1745 float filter; // filter out small variations of this size
1746 float currentValue; // current value
1747 float newValue; // most recent value
1748 float highCurrentValue; // current value of high split
1749 float highNewValue; // most recent value of high split
1750
1751 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1752 bool explicitlyMapped, float scale, float offset,
1753 float highScale, float highOffset,
1754 float min, float max, float flat, float fuzz, float resolution) {
1755 this->rawAxisInfo = rawAxisInfo;
1756 this->axisInfo = axisInfo;
1757 this->explicitlyMapped = explicitlyMapped;
1758 this->scale = scale;
1759 this->offset = offset;
1760 this->highScale = highScale;
1761 this->highOffset = highOffset;
1762 this->min = min;
1763 this->max = max;
1764 this->flat = flat;
1765 this->fuzz = fuzz;
1766 this->resolution = resolution;
1767 this->filter = 0;
1768 resetValue();
1769 }
1770
1771 void resetValue() {
1772 this->currentValue = 0;
1773 this->newValue = 0;
1774 this->highCurrentValue = 0;
1775 this->highNewValue = 0;
1776 }
1777 };
1778
1779 // Axes indexed by raw ABS_* axis index.
1780 KeyedVector<int32_t, Axis> mAxes;
1781
1782 void sync(nsecs_t when, bool force);
1783
1784 bool haveAxis(int32_t axisId);
1785 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1786 bool filterAxes(bool force);
1787
1788 static bool hasValueChangedSignificantly(float filter,
1789 float newValue, float currentValue, float min, float max);
1790 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1791 float newValue, float currentValue, float thresholdValue);
1792
1793 static bool isCenteredAxis(int32_t axis);
1794 static int32_t getCompatAxis(int32_t axis);
1795
1796 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1797 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1798 float value);
1799};
1800
1801} // namespace android
1802
1803#endif // _UI_INPUT_READER_H