blob: b7f94c18f3ed3f624083984144569c20f34f8dbb [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>
Atif Niyaz83846822019-07-18 15:17:40 -070030#include <utils/BitSet.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070031#include <utils/Condition.h>
Atif Niyaz83846822019-07-18 15:17:40 -070032#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070033#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
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
575
576/* Keeps track of the state of single-touch protocol. */
577class SingleTouchMotionAccumulator {
578public:
579 SingleTouchMotionAccumulator();
580
581 void process(const RawEvent* rawEvent);
582 void reset(InputDevice* device);
583
584 inline int32_t getAbsoluteX() const { return mAbsX; }
585 inline int32_t getAbsoluteY() const { return mAbsY; }
586 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
587 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
588 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
589 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
590 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
591
592private:
593 int32_t mAbsX;
594 int32_t mAbsY;
595 int32_t mAbsPressure;
596 int32_t mAbsToolWidth;
597 int32_t mAbsDistance;
598 int32_t mAbsTiltX;
599 int32_t mAbsTiltY;
600
601 void clearAbsoluteAxes();
602};
603
604
605/* Keeps track of the state of multi-touch protocol. */
606class MultiTouchMotionAccumulator {
607public:
608 class Slot {
609 public:
610 inline bool isInUse() const { return mInUse; }
611 inline int32_t getX() const { return mAbsMTPositionX; }
612 inline int32_t getY() const { return mAbsMTPositionY; }
613 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
614 inline int32_t getTouchMinor() const {
615 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
616 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
617 inline int32_t getToolMinor() const {
618 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
619 inline int32_t getOrientation() const { return mAbsMTOrientation; }
620 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
621 inline int32_t getPressure() const { return mAbsMTPressure; }
622 inline int32_t getDistance() const { return mAbsMTDistance; }
623 inline int32_t getToolType() const;
624
625 private:
626 friend class MultiTouchMotionAccumulator;
627
628 bool mInUse;
629 bool mHaveAbsMTTouchMinor;
630 bool mHaveAbsMTWidthMinor;
631 bool mHaveAbsMTToolType;
632
633 int32_t mAbsMTPositionX;
634 int32_t mAbsMTPositionY;
635 int32_t mAbsMTTouchMajor;
636 int32_t mAbsMTTouchMinor;
637 int32_t mAbsMTWidthMajor;
638 int32_t mAbsMTWidthMinor;
639 int32_t mAbsMTOrientation;
640 int32_t mAbsMTTrackingId;
641 int32_t mAbsMTPressure;
642 int32_t mAbsMTDistance;
643 int32_t mAbsMTToolType;
644
645 Slot();
646 void clear();
647 };
648
649 MultiTouchMotionAccumulator();
650 ~MultiTouchMotionAccumulator();
651
652 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
653 void reset(InputDevice* device);
654 void process(const RawEvent* rawEvent);
655 void finishSync();
656 bool hasStylus() const;
657
658 inline size_t getSlotCount() const { return mSlotCount; }
659 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
660
661private:
662 int32_t mCurrentSlot;
663 Slot* mSlots;
664 size_t mSlotCount;
665 bool mUsingSlotsProtocol;
666 bool mHaveStylus;
667
668 void clearSlots(int32_t initialSlot);
669};
670
671
672/* An input mapper transforms raw input events into cooked event data.
673 * A single input device can have multiple associated input mappers in order to interpret
674 * different classes of events.
675 *
676 * InputMapper lifecycle:
677 * - create
678 * - configure with 0 changes
679 * - reset
680 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
681 * - reset
682 * - destroy
683 */
684class InputMapper {
685public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700686 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 virtual ~InputMapper();
688
689 inline InputDevice* getDevice() { return mDevice; }
690 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100691 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 inline InputReaderContext* getContext() { return mContext; }
693 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
694 inline InputListenerInterface* getListener() { return mContext->getListener(); }
695 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
696
697 virtual uint32_t getSources() = 0;
698 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800699 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
701 virtual void reset(nsecs_t when);
702 virtual void process(const RawEvent* rawEvent) = 0;
703 virtual void timeoutExpired(nsecs_t when);
704
705 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
706 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
707 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
708 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
709 const int32_t* keyCodes, uint8_t* outFlags);
710 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
711 int32_t token);
712 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800713 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714
715 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800716 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717
Michael Wright842500e2015-03-13 17:32:02 -0700718 virtual void updateExternalStylusState(const StylusState& state);
719
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 virtual void fadePointer();
Arthur Hungc23540e2018-11-29 20:42:11 +0800721 virtual std::optional<int32_t> getAssociatedDisplay() {
722 return std::nullopt;
723 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724protected:
725 InputDevice* mDevice;
726 InputReaderContext* mContext;
727
728 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
729 void bumpGeneration();
730
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800731 static void dumpRawAbsoluteAxisInfo(std::string& dump,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 const RawAbsoluteAxisInfo& axis, const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800733 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734};
735
736
737class SwitchInputMapper : public InputMapper {
738public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700739 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 virtual ~SwitchInputMapper();
741
742 virtual uint32_t getSources();
743 virtual void process(const RawEvent* rawEvent);
744
745 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800746 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747
748private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700749 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 uint32_t mUpdatedSwitchMask;
751
752 void processSwitch(int32_t switchCode, int32_t switchValue);
753 void sync(nsecs_t when);
754};
755
756
757class VibratorInputMapper : public InputMapper {
758public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700759 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 virtual ~VibratorInputMapper();
761
762 virtual uint32_t getSources();
763 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
764 virtual void process(const RawEvent* rawEvent);
765
766 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
767 int32_t token);
768 virtual void cancelVibrate(int32_t token);
769 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800770 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771
772private:
773 bool mVibrating;
774 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
775 size_t mPatternSize;
776 ssize_t mRepeat;
777 int32_t mToken;
778 ssize_t mIndex;
779 nsecs_t mNextStepTime;
780
781 void nextStep();
782 void stopVibrating();
783};
784
785
786class KeyboardInputMapper : public InputMapper {
787public:
788 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
789 virtual ~KeyboardInputMapper();
790
791 virtual uint32_t getSources();
792 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800793 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
795 virtual void reset(nsecs_t when);
796 virtual void process(const RawEvent* rawEvent);
797
798 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
799 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
800 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
801 const int32_t* keyCodes, uint8_t* outFlags);
802
803 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800804 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100807 // The current viewport.
808 std::optional<DisplayViewport> mViewport;
809
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 struct KeyDown {
811 int32_t keyCode;
812 int32_t scanCode;
813 };
814
815 uint32_t mSource;
816 int32_t mKeyboardType;
817
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800818 std::vector<KeyDown> mKeyDowns; // keys that are down
Michael Wrightd02c5b62014-02-10 15:10:22 -0800819 int32_t mMetaState;
820 nsecs_t mDownTime; // time of most recent key down
821
822 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
823
824 struct LedState {
825 bool avail; // led is available
826 bool on; // we think the led is currently on
827 };
828 LedState mCapsLockLedState;
829 LedState mNumLockLedState;
830 LedState mScrollLockLedState;
831
832 // Immutable configuration parameters.
833 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700835 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 } mParameters;
837
838 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800839 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100841 int32_t getOrientation();
842 int32_t getDisplayId();
843
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100845 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700847 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848
Andrii Kulian763a3a42016-03-08 10:46:16 -0800849 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 ssize_t findKeyDown(int32_t scanCode);
852
853 void resetLedState();
854 void initializeLedState(LedState& ledState, int32_t led);
855 void updateLedState(bool reset);
856 void updateLedStateForModifier(LedState& ledState, int32_t led,
857 int32_t modifier, bool reset);
858};
859
860
861class CursorInputMapper : public InputMapper {
862public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700863 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864 virtual ~CursorInputMapper();
865
866 virtual uint32_t getSources();
867 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800868 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
870 virtual void reset(nsecs_t when);
871 virtual void process(const RawEvent* rawEvent);
872
873 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
874
875 virtual void fadePointer();
876
Arthur Hungc23540e2018-11-29 20:42:11 +0800877 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878private:
879 // Amount that trackball needs to move in order to generate a key event.
880 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
881
882 // Immutable configuration parameters.
883 struct Parameters {
884 enum Mode {
885 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800886 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 MODE_NAVIGATION,
888 };
889
890 Mode mode;
891 bool hasAssociatedDisplay;
892 bool orientationAware;
893 } mParameters;
894
895 CursorButtonAccumulator mCursorButtonAccumulator;
896 CursorMotionAccumulator mCursorMotionAccumulator;
897 CursorScrollAccumulator mCursorScrollAccumulator;
898
899 int32_t mSource;
900 float mXScale;
901 float mYScale;
902 float mXPrecision;
903 float mYPrecision;
904
905 float mVWheelScale;
906 float mHWheelScale;
907
908 // Velocity controls for mouse pointer and wheel movements.
909 // The controls for X and Y wheel movements are separate to keep them decoupled.
910 VelocityControl mPointerVelocityControl;
911 VelocityControl mWheelXVelocityControl;
912 VelocityControl mWheelYVelocityControl;
913
914 int32_t mOrientation;
915
916 sp<PointerControllerInterface> mPointerController;
917
918 int32_t mButtonState;
919 nsecs_t mDownTime;
920
921 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800922 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923
924 void sync(nsecs_t when);
925};
926
927
Prashant Malani1941ff52015-08-11 18:29:28 -0700928class RotaryEncoderInputMapper : public InputMapper {
929public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700930 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700931 virtual ~RotaryEncoderInputMapper();
932
933 virtual uint32_t getSources();
934 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800935 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700936 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
937 virtual void reset(nsecs_t when);
938 virtual void process(const RawEvent* rawEvent);
939
940private:
941 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
942
943 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -0800944 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +0100945 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -0700946
947 void sync(nsecs_t when);
948};
949
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950class TouchInputMapper : public InputMapper {
951public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700952 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 virtual ~TouchInputMapper();
954
955 virtual uint32_t getSources();
956 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800957 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
959 virtual void reset(nsecs_t when);
960 virtual void process(const RawEvent* rawEvent);
961
962 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
963 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
964 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
965 const int32_t* keyCodes, uint8_t* outFlags);
966
967 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -0800968 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700970 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hungc23540e2018-11-29 20:42:11 +0800971 virtual std::optional<int32_t> getAssociatedDisplay();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972protected:
973 CursorButtonAccumulator mCursorButtonAccumulator;
974 CursorScrollAccumulator mCursorScrollAccumulator;
975 TouchButtonAccumulator mTouchButtonAccumulator;
976
977 struct VirtualKey {
978 int32_t keyCode;
979 int32_t scanCode;
980 uint32_t flags;
981
982 // computed hit box, specified in touch screen coords based on known display size
983 int32_t hitLeft;
984 int32_t hitTop;
985 int32_t hitRight;
986 int32_t hitBottom;
987
988 inline bool isHit(int32_t x, int32_t y) const {
989 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
990 }
991 };
992
993 // Input sources and device mode.
994 uint32_t mSource;
995
996 enum DeviceMode {
997 DEVICE_MODE_DISABLED, // input is disabled
998 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
999 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1000 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1001 DEVICE_MODE_POINTER, // pointer mapping (pointer)
1002 };
1003 DeviceMode mDeviceMode;
1004
1005 // The reader's configuration.
1006 InputReaderConfiguration mConfig;
1007
1008 // Immutable configuration parameters.
1009 struct Parameters {
1010 enum DeviceType {
1011 DEVICE_TYPE_TOUCH_SCREEN,
1012 DEVICE_TYPE_TOUCH_PAD,
1013 DEVICE_TYPE_TOUCH_NAVIGATION,
1014 DEVICE_TYPE_POINTER,
1015 };
1016
1017 DeviceType deviceType;
1018 bool hasAssociatedDisplay;
1019 bool associatedDisplayIsExternal;
1020 bool orientationAware;
1021 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001022 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023
1024 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001025 GESTURE_MODE_SINGLE_TOUCH,
1026 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027 };
1028 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001029
1030 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 } mParameters;
1032
1033 // Immutable calibration parameters in parsed form.
1034 struct Calibration {
1035 // Size
1036 enum SizeCalibration {
1037 SIZE_CALIBRATION_DEFAULT,
1038 SIZE_CALIBRATION_NONE,
1039 SIZE_CALIBRATION_GEOMETRIC,
1040 SIZE_CALIBRATION_DIAMETER,
1041 SIZE_CALIBRATION_BOX,
1042 SIZE_CALIBRATION_AREA,
1043 };
1044
1045 SizeCalibration sizeCalibration;
1046
1047 bool haveSizeScale;
1048 float sizeScale;
1049 bool haveSizeBias;
1050 float sizeBias;
1051 bool haveSizeIsSummed;
1052 bool sizeIsSummed;
1053
1054 // Pressure
1055 enum PressureCalibration {
1056 PRESSURE_CALIBRATION_DEFAULT,
1057 PRESSURE_CALIBRATION_NONE,
1058 PRESSURE_CALIBRATION_PHYSICAL,
1059 PRESSURE_CALIBRATION_AMPLITUDE,
1060 };
1061
1062 PressureCalibration pressureCalibration;
1063 bool havePressureScale;
1064 float pressureScale;
1065
1066 // Orientation
1067 enum OrientationCalibration {
1068 ORIENTATION_CALIBRATION_DEFAULT,
1069 ORIENTATION_CALIBRATION_NONE,
1070 ORIENTATION_CALIBRATION_INTERPOLATED,
1071 ORIENTATION_CALIBRATION_VECTOR,
1072 };
1073
1074 OrientationCalibration orientationCalibration;
1075
1076 // Distance
1077 enum DistanceCalibration {
1078 DISTANCE_CALIBRATION_DEFAULT,
1079 DISTANCE_CALIBRATION_NONE,
1080 DISTANCE_CALIBRATION_SCALED,
1081 };
1082
1083 DistanceCalibration distanceCalibration;
1084 bool haveDistanceScale;
1085 float distanceScale;
1086
1087 enum CoverageCalibration {
1088 COVERAGE_CALIBRATION_DEFAULT,
1089 COVERAGE_CALIBRATION_NONE,
1090 COVERAGE_CALIBRATION_BOX,
1091 };
1092
1093 CoverageCalibration coverageCalibration;
1094
1095 inline void applySizeScaleAndBias(float* outSize) const {
1096 if (haveSizeScale) {
1097 *outSize *= sizeScale;
1098 }
1099 if (haveSizeBias) {
1100 *outSize += sizeBias;
1101 }
1102 if (*outSize < 0) {
1103 *outSize = 0;
1104 }
1105 }
1106 } mCalibration;
1107
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001108 // Affine location transformation/calibration
1109 struct TouchAffineTransformation mAffineTransform;
1110
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 RawPointerAxes mRawPointerAxes;
1112
Michael Wright842500e2015-03-13 17:32:02 -07001113 struct RawState {
1114 nsecs_t when;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115
Michael Wright842500e2015-03-13 17:32:02 -07001116 // Raw pointer sample data.
1117 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
Michael Wright842500e2015-03-13 17:32:02 -07001119 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Michael Wright842500e2015-03-13 17:32:02 -07001121 // Scroll state.
1122 int32_t rawVScroll;
1123 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124
Michael Wright842500e2015-03-13 17:32:02 -07001125 void copyFrom(const RawState& other) {
1126 when = other.when;
1127 rawPointerData.copyFrom(other.rawPointerData);
1128 buttonState = other.buttonState;
1129 rawVScroll = other.rawVScroll;
1130 rawHScroll = other.rawHScroll;
1131 }
1132
1133 void clear() {
1134 when = 0;
1135 rawPointerData.clear();
1136 buttonState = 0;
1137 rawVScroll = 0;
1138 rawHScroll = 0;
1139 }
1140 };
1141
1142 struct CookedState {
1143 // Cooked pointer sample data.
1144 CookedPointerData cookedPointerData;
1145
1146 // Id bits used to differentiate fingers, stylus and mouse tools.
1147 BitSet32 fingerIdBits;
1148 BitSet32 stylusIdBits;
1149 BitSet32 mouseIdBits;
1150
Michael Wright7b159c92015-05-14 14:48:03 +01001151 int32_t buttonState;
1152
Michael Wright842500e2015-03-13 17:32:02 -07001153 void copyFrom(const CookedState& other) {
1154 cookedPointerData.copyFrom(other.cookedPointerData);
1155 fingerIdBits = other.fingerIdBits;
1156 stylusIdBits = other.stylusIdBits;
1157 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001158 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001159 }
1160
1161 void clear() {
1162 cookedPointerData.clear();
1163 fingerIdBits.clear();
1164 stylusIdBits.clear();
1165 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001166 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001167 }
1168 };
1169
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001170 std::vector<RawState> mRawStatesPending;
Michael Wright842500e2015-03-13 17:32:02 -07001171 RawState mCurrentRawState;
1172 CookedState mCurrentCookedState;
1173 RawState mLastRawState;
1174 CookedState mLastCookedState;
1175
1176 // State provided by an external stylus
1177 StylusState mExternalStylusState;
1178 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001179 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001180 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181
1182 // True if we sent a HOVER_ENTER event.
1183 bool mSentHoverEnter;
1184
Michael Wright842500e2015-03-13 17:32:02 -07001185 // Have we assigned pointer IDs for this stream
1186 bool mHavePointerIds;
1187
Michael Wright8e812822015-06-22 16:18:21 +01001188 // Is the current stream of direct touch events aborted
1189 bool mCurrentMotionAborted;
1190
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 // The time the primary pointer last went down.
1192 nsecs_t mDownTime;
1193
1194 // The pointer controller, or null if the device is not a pointer.
1195 sp<PointerControllerInterface> mPointerController;
1196
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001197 std::vector<VirtualKey> mVirtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198
1199 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001200 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001202 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001204 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001206 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 virtual void parseCalibration();
1208 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001209 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001210 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001211 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001212 virtual void resolveExternalStylusPresence();
1213 virtual bool hasStylus() const = 0;
1214 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215
Michael Wright842500e2015-03-13 17:32:02 -07001216 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217
1218private:
1219 // The current viewport.
1220 // The components of the viewport are specified in the display's rotated orientation.
1221 DisplayViewport mViewport;
1222
1223 // The surface orientation, width and height set by configureSurface().
1224 // The width and height are derived from the viewport but are specified
1225 // in the natural orientation.
1226 // The surface origin specifies how the surface coordinates should be translated
1227 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 int32_t mSurfaceWidth;
1229 int32_t mSurfaceHeight;
1230 int32_t mSurfaceLeft;
1231 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001232
1233 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1234 // the logical coordinate space.
1235 int32_t mPhysicalWidth;
1236 int32_t mPhysicalHeight;
1237 int32_t mPhysicalLeft;
1238 int32_t mPhysicalTop;
1239
1240 // The orientation may be different from the viewport orientation as it specifies
1241 // the rotation of the surface coordinates required to produce the viewport's
1242 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 int32_t mSurfaceOrientation;
1244
1245 // Translation and scaling factors, orientation-independent.
1246 float mXTranslate;
1247 float mXScale;
1248 float mXPrecision;
1249
1250 float mYTranslate;
1251 float mYScale;
1252 float mYPrecision;
1253
1254 float mGeometricScale;
1255
1256 float mPressureScale;
1257
1258 float mSizeScale;
1259
1260 float mOrientationScale;
1261
1262 float mDistanceScale;
1263
1264 bool mHaveTilt;
1265 float mTiltXCenter;
1266 float mTiltXScale;
1267 float mTiltYCenter;
1268 float mTiltYScale;
1269
Michael Wright842500e2015-03-13 17:32:02 -07001270 bool mExternalStylusConnected;
1271
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 // Oriented motion ranges for input device info.
1273 struct OrientedRanges {
1274 InputDeviceInfo::MotionRange x;
1275 InputDeviceInfo::MotionRange y;
1276 InputDeviceInfo::MotionRange pressure;
1277
1278 bool haveSize;
1279 InputDeviceInfo::MotionRange size;
1280
1281 bool haveTouchSize;
1282 InputDeviceInfo::MotionRange touchMajor;
1283 InputDeviceInfo::MotionRange touchMinor;
1284
1285 bool haveToolSize;
1286 InputDeviceInfo::MotionRange toolMajor;
1287 InputDeviceInfo::MotionRange toolMinor;
1288
1289 bool haveOrientation;
1290 InputDeviceInfo::MotionRange orientation;
1291
1292 bool haveDistance;
1293 InputDeviceInfo::MotionRange distance;
1294
1295 bool haveTilt;
1296 InputDeviceInfo::MotionRange tilt;
1297
1298 OrientedRanges() {
1299 clear();
1300 }
1301
1302 void clear() {
1303 haveSize = false;
1304 haveTouchSize = false;
1305 haveToolSize = false;
1306 haveOrientation = false;
1307 haveDistance = false;
1308 haveTilt = false;
1309 }
1310 } mOrientedRanges;
1311
1312 // Oriented dimensions and precision.
1313 float mOrientedXPrecision;
1314 float mOrientedYPrecision;
1315
1316 struct CurrentVirtualKeyState {
1317 bool down;
1318 bool ignored;
1319 nsecs_t downTime;
1320 int32_t keyCode;
1321 int32_t scanCode;
1322 } mCurrentVirtualKey;
1323
1324 // Scale factor for gesture or mouse based pointer movements.
1325 float mPointerXMovementScale;
1326 float mPointerYMovementScale;
1327
1328 // Scale factor for gesture based zooming and other freeform motions.
1329 float mPointerXZoomScale;
1330 float mPointerYZoomScale;
1331
1332 // The maximum swipe width.
1333 float mPointerGestureMaxSwipeWidth;
1334
1335 struct PointerDistanceHeapElement {
1336 uint32_t currentPointerIndex : 8;
1337 uint32_t lastPointerIndex : 8;
1338 uint64_t distance : 48; // squared distance
1339 };
1340
1341 enum PointerUsage {
1342 POINTER_USAGE_NONE,
1343 POINTER_USAGE_GESTURES,
1344 POINTER_USAGE_STYLUS,
1345 POINTER_USAGE_MOUSE,
1346 };
1347 PointerUsage mPointerUsage;
1348
1349 struct PointerGesture {
1350 enum Mode {
1351 // No fingers, button is not pressed.
1352 // Nothing happening.
1353 NEUTRAL,
1354
1355 // No fingers, button is not pressed.
1356 // Tap detected.
1357 // Emits DOWN and UP events at the pointer location.
1358 TAP,
1359
1360 // Exactly one finger dragging following a tap.
1361 // Pointer follows the active finger.
1362 // Emits DOWN, MOVE and UP events at the pointer location.
1363 //
1364 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1365 TAP_DRAG,
1366
1367 // Button is pressed.
1368 // Pointer follows the active finger if there is one. Other fingers are ignored.
1369 // Emits DOWN, MOVE and UP events at the pointer location.
1370 BUTTON_CLICK_OR_DRAG,
1371
1372 // Exactly one finger, button is not pressed.
1373 // Pointer follows the active finger.
1374 // Emits HOVER_MOVE events at the pointer location.
1375 //
1376 // Detect taps when the finger goes up while in HOVER mode.
1377 HOVER,
1378
1379 // Exactly two fingers but neither have moved enough to clearly indicate
1380 // whether a swipe or freeform gesture was intended. We consider the
1381 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1382 // Pointer does not move.
1383 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1384 PRESS,
1385
1386 // Exactly two fingers moving in the same direction, button is not pressed.
1387 // Pointer does not move.
1388 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1389 // follows the midpoint between both fingers.
1390 SWIPE,
1391
1392 // Two or more fingers moving in arbitrary directions, button is not pressed.
1393 // Pointer does not move.
1394 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1395 // each finger individually relative to the initial centroid of the finger.
1396 FREEFORM,
1397
1398 // Waiting for quiet time to end before starting the next gesture.
1399 QUIET,
1400 };
1401
1402 // Time the first finger went down.
1403 nsecs_t firstTouchTime;
1404
1405 // The active pointer id from the raw touch data.
1406 int32_t activeTouchId; // -1 if none
1407
1408 // The active pointer id from the gesture last delivered to the application.
1409 int32_t activeGestureId; // -1 if none
1410
1411 // Pointer coords and ids for the current and previous pointer gesture.
1412 Mode currentGestureMode;
1413 BitSet32 currentGestureIdBits;
1414 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1415 PointerProperties currentGestureProperties[MAX_POINTERS];
1416 PointerCoords currentGestureCoords[MAX_POINTERS];
1417
1418 Mode lastGestureMode;
1419 BitSet32 lastGestureIdBits;
1420 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1421 PointerProperties lastGestureProperties[MAX_POINTERS];
1422 PointerCoords lastGestureCoords[MAX_POINTERS];
1423
1424 // Time the pointer gesture last went down.
1425 nsecs_t downTime;
1426
1427 // Time when the pointer went down for a TAP.
1428 nsecs_t tapDownTime;
1429
1430 // Time when the pointer went up for a TAP.
1431 nsecs_t tapUpTime;
1432
1433 // Location of initial tap.
1434 float tapX, tapY;
1435
1436 // Time we started waiting for quiescence.
1437 nsecs_t quietTime;
1438
1439 // Reference points for multitouch gestures.
1440 float referenceTouchX; // reference touch X/Y coordinates in surface units
1441 float referenceTouchY;
1442 float referenceGestureX; // reference gesture X/Y coordinates in pixels
1443 float referenceGestureY;
1444
1445 // Distance that each pointer has traveled which has not yet been
1446 // subsumed into the reference gesture position.
1447 BitSet32 referenceIdBits;
1448 struct Delta {
1449 float dx, dy;
1450 };
1451 Delta referenceDeltas[MAX_POINTER_ID + 1];
1452
1453 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1454 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1455
1456 // A velocity tracker for determining whether to switch active pointers during drags.
1457 VelocityTracker velocityTracker;
1458
1459 void reset() {
1460 firstTouchTime = LLONG_MIN;
1461 activeTouchId = -1;
1462 activeGestureId = -1;
1463 currentGestureMode = NEUTRAL;
1464 currentGestureIdBits.clear();
1465 lastGestureMode = NEUTRAL;
1466 lastGestureIdBits.clear();
1467 downTime = 0;
1468 velocityTracker.clear();
1469 resetTap();
1470 resetQuietTime();
1471 }
1472
1473 void resetTap() {
1474 tapDownTime = LLONG_MIN;
1475 tapUpTime = LLONG_MIN;
1476 }
1477
1478 void resetQuietTime() {
1479 quietTime = LLONG_MIN;
1480 }
1481 } mPointerGesture;
1482
1483 struct PointerSimple {
1484 PointerCoords currentCoords;
1485 PointerProperties currentProperties;
1486 PointerCoords lastCoords;
1487 PointerProperties lastProperties;
1488
1489 // True if the pointer is down.
1490 bool down;
1491
1492 // True if the pointer is hovering.
1493 bool hovering;
1494
1495 // Time the pointer last went down.
1496 nsecs_t downTime;
1497
1498 void reset() {
1499 currentCoords.clear();
1500 currentProperties.clear();
1501 lastCoords.clear();
1502 lastProperties.clear();
1503 down = false;
1504 hovering = false;
1505 downTime = 0;
1506 }
1507 } mPointerSimple;
1508
1509 // The pointer and scroll velocity controls.
1510 VelocityControl mPointerVelocityControl;
1511 VelocityControl mWheelXVelocityControl;
1512 VelocityControl mWheelYVelocityControl;
1513
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001514 std::optional<DisplayViewport> findViewport();
1515
Michael Wright842500e2015-03-13 17:32:02 -07001516 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001517 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001518
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 void sync(nsecs_t when);
1520
1521 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001522 void processRawTouches(bool timeout);
1523 void cookAndDispatch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1525 int32_t keyEventAction, int32_t keyEventFlags);
1526
1527 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1528 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1529 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001530 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1531 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1532 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001534 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535
1536 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1537 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1538
1539 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1540 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1541 bool preparePointerGestures(nsecs_t when,
1542 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1543 bool isTimeout);
1544
1545 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1546 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1547
1548 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1549 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1550
1551 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1552 bool down, bool hovering);
1553 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1554
Michael Wright842500e2015-03-13 17:32:02 -07001555 bool assignExternalStylusId(const RawState& state, bool timeout);
1556 void applyExternalStylusButtonState(nsecs_t when);
1557 void applyExternalStylusTouchState(nsecs_t when);
1558
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 // Dispatches a motion event.
1560 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1561 // method will take care of setting the index and transmuting the action to DOWN or UP
1562 // it is the first / last pointer to go down / up.
1563 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Michael Wright7b159c92015-05-14 14:48:03 +01001564 int32_t action, int32_t actionButton,
1565 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566 const PointerProperties* properties, const PointerCoords* coords,
1567 const uint32_t* idToIndex, BitSet32 idBits,
1568 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1569
1570 // Updates pointer coords and properties for pointers with specified ids that have moved.
1571 // Returns true if any of them changed.
1572 bool updateMovedPointers(const PointerProperties* inProperties,
1573 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1574 PointerProperties* outProperties, PointerCoords* outCoords,
1575 const uint32_t* outIdToIndex, BitSet32 idBits) const;
1576
1577 bool isPointInsideSurface(int32_t x, int32_t y);
1578 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1579
Michael Wright842500e2015-03-13 17:32:02 -07001580 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001581
1582 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583};
1584
1585
1586class SingleTouchInputMapper : public TouchInputMapper {
1587public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001588 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 virtual ~SingleTouchInputMapper();
1590
1591 virtual void reset(nsecs_t when);
1592 virtual void process(const RawEvent* rawEvent);
1593
1594protected:
Michael Wright842500e2015-03-13 17:32:02 -07001595 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 virtual void configureRawPointerAxes();
1597 virtual bool hasStylus() const;
1598
1599private:
1600 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1601};
1602
1603
1604class MultiTouchInputMapper : public TouchInputMapper {
1605public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001606 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 virtual ~MultiTouchInputMapper();
1608
1609 virtual void reset(nsecs_t when);
1610 virtual void process(const RawEvent* rawEvent);
1611
1612protected:
Michael Wright842500e2015-03-13 17:32:02 -07001613 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 virtual void configureRawPointerAxes();
1615 virtual bool hasStylus() const;
1616
1617private:
1618 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1619
1620 // Specifies the pointer id bits that are in use, and their associated tracking id.
1621 BitSet32 mPointerIdBits;
1622 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1623};
1624
Michael Wright842500e2015-03-13 17:32:02 -07001625class ExternalStylusInputMapper : public InputMapper {
1626public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001627 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001628 virtual ~ExternalStylusInputMapper() = default;
1629
1630 virtual uint32_t getSources();
1631 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001632 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001633 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1634 virtual void reset(nsecs_t when);
1635 virtual void process(const RawEvent* rawEvent);
1636 virtual void sync(nsecs_t when);
1637
1638private:
1639 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1640 RawAbsoluteAxisInfo mRawPressureAxis;
1641 TouchButtonAccumulator mTouchButtonAccumulator;
1642
1643 StylusState mStylusState;
1644};
1645
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646
1647class JoystickInputMapper : public InputMapper {
1648public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001649 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 virtual ~JoystickInputMapper();
1651
1652 virtual uint32_t getSources();
1653 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001654 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1656 virtual void reset(nsecs_t when);
1657 virtual void process(const RawEvent* rawEvent);
1658
1659private:
1660 struct Axis {
1661 RawAbsoluteAxisInfo rawAxisInfo;
1662 AxisInfo axisInfo;
1663
1664 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1665
1666 float scale; // scale factor from raw to normalized values
1667 float offset; // offset to add after scaling for normalization
1668 float highScale; // scale factor from raw to normalized values of high split
1669 float highOffset; // offset to add after scaling for normalization of high split
1670
1671 float min; // normalized inclusive minimum
1672 float max; // normalized inclusive maximum
1673 float flat; // normalized flat region size
1674 float fuzz; // normalized error tolerance
1675 float resolution; // normalized resolution in units/mm
1676
1677 float filter; // filter out small variations of this size
1678 float currentValue; // current value
1679 float newValue; // most recent value
1680 float highCurrentValue; // current value of high split
1681 float highNewValue; // most recent value of high split
1682
1683 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1684 bool explicitlyMapped, float scale, float offset,
1685 float highScale, float highOffset,
1686 float min, float max, float flat, float fuzz, float resolution) {
1687 this->rawAxisInfo = rawAxisInfo;
1688 this->axisInfo = axisInfo;
1689 this->explicitlyMapped = explicitlyMapped;
1690 this->scale = scale;
1691 this->offset = offset;
1692 this->highScale = highScale;
1693 this->highOffset = highOffset;
1694 this->min = min;
1695 this->max = max;
1696 this->flat = flat;
1697 this->fuzz = fuzz;
1698 this->resolution = resolution;
1699 this->filter = 0;
1700 resetValue();
1701 }
1702
1703 void resetValue() {
1704 this->currentValue = 0;
1705 this->newValue = 0;
1706 this->highCurrentValue = 0;
1707 this->highNewValue = 0;
1708 }
1709 };
1710
1711 // Axes indexed by raw ABS_* axis index.
1712 KeyedVector<int32_t, Axis> mAxes;
1713
1714 void sync(nsecs_t when, bool force);
1715
1716 bool haveAxis(int32_t axisId);
1717 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1718 bool filterAxes(bool force);
1719
1720 static bool hasValueChangedSignificantly(float filter,
1721 float newValue, float currentValue, float min, float max);
1722 static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1723 float newValue, float currentValue, float thresholdValue);
1724
1725 static bool isCenteredAxis(int32_t axis);
1726 static int32_t getCompatAxis(int32_t axis);
1727
1728 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1729 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1730 float value);
1731};
1732
1733} // namespace android
1734
1735#endif // _UI_INPUT_READER_H