blob: 1b8177c61857ff41bb8dddf9a0db7c3c5cda9276 [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"
Michael Wrightd02c5b62014-02-10 15:10:22 -080021#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080022#include "InputReaderBase.h"
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070023#include "PointerControllerInterface.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
36#include <stddef.h>
37#include <unistd.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070038#include <optional>
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 Wright842500e2015-03-13 17:32:02 -070046struct StylusState {
47 /* Time the stylus event was received. */
48 nsecs_t when;
49 /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
50 float pressure;
51 /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
52 uint32_t buttons;
53 /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
54 int32_t toolType;
55
56 void copyFrom(const StylusState& other) {
57 when = other.when;
58 pressure = other.pressure;
59 buttons = other.buttons;
60 toolType = other.toolType;
61 }
62
63 void clear() {
64 when = LLONG_MAX;
65 pressure = 0.f;
66 buttons = 0;
67 toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
68 }
69};
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071/* Internal interface used by individual input devices to access global input device state
72 * and parameters maintained by the input reader.
73 */
74class InputReaderContext {
75public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070076 InputReaderContext() {}
77 virtual ~InputReaderContext() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79 virtual void updateGlobalMetaState() = 0;
80 virtual int32_t getGlobalMetaState() = 0;
81
82 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070083 virtual bool shouldDropVirtualKey(nsecs_t now, InputDevice* device, int32_t keyCode,
84 int32_t scanCode) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -080085
86 virtual void fadePointer() = 0;
87
88 virtual void requestTimeoutAtTime(nsecs_t when) = 0;
89 virtual int32_t bumpGeneration() = 0;
90
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080091 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) = 0;
Michael Wrightb85401d2015-04-17 18:35:15 +010092 virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
Michael Wright842500e2015-03-13 17:32:02 -070093
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 virtual InputReaderPolicyInterface* getPolicy() = 0;
95 virtual InputListenerInterface* getListener() = 0;
96 virtual EventHubInterface* getEventHub() = 0;
Prabir Pradhan42611e02018-11-27 14:04:02 -080097
98 virtual uint32_t getNextSequenceNum() = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -080099};
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101/* The input reader reads raw event data from the event hub and processes it into input events
102 * that it sends to the input listener. Some functions of the input reader, such as early
103 * event filtering in low power states, are controlled by a separate policy object.
104 *
105 * The InputReader owns a collection of InputMappers. Most of the work it does happens
106 * on the input reader thread but the InputReader can receive queries from other system
107 * components running on arbitrary threads. To keep things manageable, the InputReader
108 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
109 * EventHub or the InputReaderPolicy but it is never held while calling into the
110 * InputListener.
111 */
112class InputReader : public InputReaderInterface {
113public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700114 InputReader(std::shared_ptr<EventHubInterface> eventHub,
115 const sp<InputReaderPolicyInterface>& policy,
116 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117 virtual ~InputReader();
118
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800119 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120 virtual void monitor();
121
122 virtual void loopOnce();
123
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800124 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700126 virtual bool isInputDeviceEnabled(int32_t deviceId);
127
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700128 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode);
129 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode);
130 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131
Andrii Kulian763a3a42016-03-08 10:46:16 -0800132 virtual void toggleCapsLockState(int32_t deviceId);
133
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700134 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
135 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136
137 virtual void requestRefreshConfiguration(uint32_t changes);
138
139 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700140 ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 virtual void cancelVibrate(int32_t deviceId, int32_t token);
142
Arthur Hungc23540e2018-11-29 20:42:11 +0800143 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700144
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145protected:
146 // These members are protected so they can be instrumented by test cases.
147 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700148 const InputDeviceIdentifier& identifier,
149 uint32_t classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150
151 class ContextImpl : public InputReaderContext {
152 InputReader* mReader;
153
154 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700155 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156
157 virtual void updateGlobalMetaState();
158 virtual int32_t getGlobalMetaState();
159 virtual void disableVirtualKeysUntil(nsecs_t time);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700160 virtual bool shouldDropVirtualKey(nsecs_t now, InputDevice* device, int32_t keyCode,
161 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162 virtual void fadePointer();
163 virtual void requestTimeoutAtTime(nsecs_t when);
164 virtual int32_t bumpGeneration();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800165 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700166 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 virtual InputReaderPolicyInterface* getPolicy();
168 virtual InputListenerInterface* getListener();
169 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800170 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 } mContext;
172
173 friend class ContextImpl;
174
175private:
176 Mutex mLock;
177
178 Condition mReaderIsAliveCondition;
179
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700180 // This could be unique_ptr, but due to the way InputReader tests are written,
181 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
182 // in parallel to passing it to the InputReader.
183 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 sp<InputReaderPolicyInterface> mPolicy;
185 sp<QueuedInputListener> mQueuedListener;
186
187 InputReaderConfiguration mConfig;
188
Prabir Pradhan42611e02018-11-27 14:04:02 -0800189 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
190 uint32_t mNextSequenceNum;
191
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 // The event queue.
193 static const int EVENT_BUFFER_SIZE = 256;
194 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
195
196 KeyedVector<int32_t, InputDevice*> mDevices;
197
198 // low-level input event decoding and device management
199 void processEventsLocked(const RawEvent* rawEvents, size_t count);
200
201 void addDeviceLocked(nsecs_t when, int32_t deviceId);
202 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
203 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
204 void timeoutExpiredLocked(nsecs_t when);
205
206 void handleConfigurationChangedLocked(nsecs_t when);
207
208 int32_t mGlobalMetaState;
209 void updateGlobalMetaStateLocked();
210 int32_t getGlobalMetaStateLocked();
211
Michael Wright842500e2015-03-13 17:32:02 -0700212 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800213 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700214 void dispatchExternalStylusState(const StylusState& state);
215
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 void fadePointerLocked();
217
218 int32_t mGeneration;
219 int32_t bumpGenerationLocked();
220
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800221 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222
223 nsecs_t mDisableVirtualKeysTimeout;
224 void disableVirtualKeysUntilLocked(nsecs_t time);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700225 bool shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
226 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
228 nsecs_t mNextTimeout;
229 void requestTimeoutAtTimeLocked(nsecs_t when);
230
231 uint32_t mConfigurationChangesToRefresh;
232 void refreshConfigurationLocked(uint32_t changes);
233
234 // state queries
235 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
236 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700237 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700239 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240};
241
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242/* Represents the state of a single input device. */
243class InputDevice {
244public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700245 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
246 int32_t controllerNumber, const InputDeviceIdentifier& identifier,
247 uint32_t classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 ~InputDevice();
249
250 inline InputReaderContext* getContext() { return mContext; }
251 inline int32_t getId() const { return mId; }
252 inline int32_t getControllerNumber() const { return mControllerNumber; }
253 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100254 inline const std::string getName() const { return mIdentifier.name; }
255 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 inline uint32_t getClasses() const { return mClasses; }
257 inline uint32_t getSources() const { return mSources; }
258
259 inline bool isExternal() { return mIsExternal; }
260 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700261 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
262 return mAssociatedDisplayPort;
263 }
Arthur Hung2c9a3342019-07-23 14:18:59 +0800264 inline std::optional<DisplayViewport> getAssociatedViewport() const {
265 return mAssociatedViewport;
266 }
Tim Kilbourn063ff532015-04-08 10:26:18 -0700267 inline void setMic(bool hasMic) { mHasMic = hasMic; }
268 inline bool hasMic() const { return mHasMic; }
269
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800270 inline bool isIgnored() { return mMappers.empty(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700272 bool isEnabled();
273 void setEnabled(bool enabled, nsecs_t when);
274
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800275 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 void addMapper(InputMapper* mapper);
277 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
278 void reset(nsecs_t when);
279 void process(const RawEvent* rawEvents, size_t count);
280 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700281 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282
283 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
284 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
285 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
286 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700287 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
288 uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
290 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800291 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292
293 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800294 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295
296 void fadePointer();
297
298 void bumpGeneration();
299
300 void notifyReset(nsecs_t when);
301
302 inline const PropertyMap& getConfiguration() { return mConfiguration; }
303 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
304
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700305 bool hasKey(int32_t code) { return getEventHub()->hasScanCode(mId, code); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306
307 bool hasAbsoluteAxis(int32_t code) {
308 RawAbsoluteAxisInfo info;
309 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
310 return info.valid;
311 }
312
313 bool isKeyPressed(int32_t code) {
314 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
315 }
316
317 int32_t getAbsoluteAxisValue(int32_t code) {
318 int32_t value;
319 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
320 return value;
321 }
322
Arthur Hung2c9a3342019-07-23 14:18:59 +0800323 std::optional<int32_t> getAssociatedDisplayId();
324
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325private:
326 InputReaderContext* mContext;
327 int32_t mId;
328 int32_t mGeneration;
329 int32_t mControllerNumber;
330 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100331 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 uint32_t mClasses;
333
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800334 std::vector<InputMapper*> mMappers;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
336 uint32_t mSources;
337 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700338 std::optional<uint8_t> mAssociatedDisplayPort;
Arthur Hung2c9a3342019-07-23 14:18:59 +0800339 std::optional<DisplayViewport> mAssociatedViewport;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700340 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 bool mDropUntilNextSync;
342
343 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
344 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
345
346 PropertyMap mConfiguration;
347};
348
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349/* Keeps track of the state of mouse or touch pad buttons. */
350class CursorButtonAccumulator {
351public:
352 CursorButtonAccumulator();
353 void reset(InputDevice* device);
354
355 void process(const RawEvent* rawEvent);
356
357 uint32_t getButtonState() const;
358
359private:
360 bool mBtnLeft;
361 bool mBtnRight;
362 bool mBtnMiddle;
363 bool mBtnBack;
364 bool mBtnSide;
365 bool mBtnForward;
366 bool mBtnExtra;
367 bool mBtnTask;
368
369 void clearButtons();
370};
371
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372/* Keeps track of cursor movements. */
373
374class CursorMotionAccumulator {
375public:
376 CursorMotionAccumulator();
377 void reset(InputDevice* device);
378
379 void process(const RawEvent* rawEvent);
380 void finishSync();
381
382 inline int32_t getRelativeX() const { return mRelX; }
383 inline int32_t getRelativeY() const { return mRelY; }
384
385private:
386 int32_t mRelX;
387 int32_t mRelY;
388
389 void clearRelativeAxes();
390};
391
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392/* Keeps track of cursor scrolling motions. */
393
394class CursorScrollAccumulator {
395public:
396 CursorScrollAccumulator();
397 void configure(InputDevice* device);
398 void reset(InputDevice* device);
399
400 void process(const RawEvent* rawEvent);
401 void finishSync();
402
403 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
404 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
405
406 inline int32_t getRelativeX() const { return mRelX; }
407 inline int32_t getRelativeY() const { return mRelY; }
408 inline int32_t getRelativeVWheel() const { return mRelWheel; }
409 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
410
411private:
412 bool mHaveRelWheel;
413 bool mHaveRelHWheel;
414
415 int32_t mRelX;
416 int32_t mRelY;
417 int32_t mRelWheel;
418 int32_t mRelHWheel;
419
420 void clearRelativeAxes();
421};
422
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423/* Keeps track of the state of touch, stylus and tool buttons. */
424class TouchButtonAccumulator {
425public:
426 TouchButtonAccumulator();
427 void configure(InputDevice* device);
428 void reset(InputDevice* device);
429
430 void process(const RawEvent* rawEvent);
431
432 uint32_t getButtonState() const;
433 int32_t getToolType() const;
434 bool isToolActive() const;
435 bool isHovering() const;
436 bool hasStylus() const;
437
438private:
439 bool mHaveBtnTouch;
440 bool mHaveStylus;
441
442 bool mBtnTouch;
443 bool mBtnStylus;
444 bool mBtnStylus2;
445 bool mBtnToolFinger;
446 bool mBtnToolPen;
447 bool mBtnToolRubber;
448 bool mBtnToolBrush;
449 bool mBtnToolPencil;
450 bool mBtnToolAirbrush;
451 bool mBtnToolMouse;
452 bool mBtnToolLens;
453 bool mBtnToolDoubleTap;
454 bool mBtnToolTripleTap;
455 bool mBtnToolQuadTap;
456
457 void clearButtons();
458};
459
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460/* Raw axis information from the driver. */
461struct RawPointerAxes {
462 RawAbsoluteAxisInfo x;
463 RawAbsoluteAxisInfo y;
464 RawAbsoluteAxisInfo pressure;
465 RawAbsoluteAxisInfo touchMajor;
466 RawAbsoluteAxisInfo touchMinor;
467 RawAbsoluteAxisInfo toolMajor;
468 RawAbsoluteAxisInfo toolMinor;
469 RawAbsoluteAxisInfo orientation;
470 RawAbsoluteAxisInfo distance;
471 RawAbsoluteAxisInfo tiltX;
472 RawAbsoluteAxisInfo tiltY;
473 RawAbsoluteAxisInfo trackingId;
474 RawAbsoluteAxisInfo slot;
475
476 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800477 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
478 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 void clear();
480};
481
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482/* Raw data for a collection of pointers including a pointer id mapping table. */
483struct RawPointerData {
484 struct Pointer {
485 uint32_t id;
486 int32_t x;
487 int32_t y;
488 int32_t pressure;
489 int32_t touchMajor;
490 int32_t touchMinor;
491 int32_t toolMajor;
492 int32_t toolMinor;
493 int32_t orientation;
494 int32_t distance;
495 int32_t tiltX;
496 int32_t tiltY;
497 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
498 bool isHovering;
499 };
500
501 uint32_t pointerCount;
502 Pointer pointers[MAX_POINTERS];
503 BitSet32 hoveringIdBits, touchingIdBits;
504 uint32_t idToIndex[MAX_POINTER_ID + 1];
505
506 RawPointerData();
507 void clear();
508 void copyFrom(const RawPointerData& other);
509 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
510
511 inline void markIdBit(uint32_t id, bool isHovering) {
512 if (isHovering) {
513 hoveringIdBits.markBit(id);
514 } else {
515 touchingIdBits.markBit(id);
516 }
517 }
518
519 inline void clearIdBits() {
520 hoveringIdBits.clear();
521 touchingIdBits.clear();
522 }
523
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700524 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700526 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527};
528
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529/* Cooked data for a collection of pointers including a pointer id mapping table. */
530struct CookedPointerData {
531 uint32_t pointerCount;
532 PointerProperties pointerProperties[MAX_POINTERS];
533 PointerCoords pointerCoords[MAX_POINTERS];
534 BitSet32 hoveringIdBits, touchingIdBits;
535 uint32_t idToIndex[MAX_POINTER_ID + 1];
536
537 CookedPointerData();
538 void clear();
539 void copyFrom(const CookedPointerData& other);
540
541 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
542 return pointerCoords[idToIndex[id]];
543 }
544
Michael Wright842500e2015-03-13 17:32:02 -0700545 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
546 return pointerCoords[idToIndex[id]];
547 }
548
549 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
550 return pointerProperties[idToIndex[id]];
551 }
552
Michael Wright53dca3a2015-04-23 17:39:53 +0100553 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
555 }
Michael Wright842500e2015-03-13 17:32:02 -0700556
Michael Wright53dca3a2015-04-23 17:39:53 +0100557 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700558 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
559 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560};
561
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562/* Keeps track of the state of single-touch protocol. */
563class SingleTouchMotionAccumulator {
564public:
565 SingleTouchMotionAccumulator();
566
567 void process(const RawEvent* rawEvent);
568 void reset(InputDevice* device);
569
570 inline int32_t getAbsoluteX() const { return mAbsX; }
571 inline int32_t getAbsoluteY() const { return mAbsY; }
572 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
573 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
574 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
575 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
576 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
577
578private:
579 int32_t mAbsX;
580 int32_t mAbsY;
581 int32_t mAbsPressure;
582 int32_t mAbsToolWidth;
583 int32_t mAbsDistance;
584 int32_t mAbsTiltX;
585 int32_t mAbsTiltY;
586
587 void clearAbsoluteAxes();
588};
589
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590/* Keeps track of the state of multi-touch protocol. */
591class MultiTouchMotionAccumulator {
592public:
593 class Slot {
594 public:
595 inline bool isInUse() const { return mInUse; }
596 inline int32_t getX() const { return mAbsMTPositionX; }
597 inline int32_t getY() const { return mAbsMTPositionY; }
598 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
599 inline int32_t getTouchMinor() const {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700600 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor;
601 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
603 inline int32_t getToolMinor() const {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700604 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor;
605 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 inline int32_t getOrientation() const { return mAbsMTOrientation; }
607 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
608 inline int32_t getPressure() const { return mAbsMTPressure; }
609 inline int32_t getDistance() const { return mAbsMTDistance; }
610 inline int32_t getToolType() const;
611
612 private:
613 friend class MultiTouchMotionAccumulator;
614
615 bool mInUse;
616 bool mHaveAbsMTTouchMinor;
617 bool mHaveAbsMTWidthMinor;
618 bool mHaveAbsMTToolType;
619
620 int32_t mAbsMTPositionX;
621 int32_t mAbsMTPositionY;
622 int32_t mAbsMTTouchMajor;
623 int32_t mAbsMTTouchMinor;
624 int32_t mAbsMTWidthMajor;
625 int32_t mAbsMTWidthMinor;
626 int32_t mAbsMTOrientation;
627 int32_t mAbsMTTrackingId;
628 int32_t mAbsMTPressure;
629 int32_t mAbsMTDistance;
630 int32_t mAbsMTToolType;
631
632 Slot();
633 void clear();
634 };
635
636 MultiTouchMotionAccumulator();
637 ~MultiTouchMotionAccumulator();
638
639 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
640 void reset(InputDevice* device);
641 void process(const RawEvent* rawEvent);
642 void finishSync();
643 bool hasStylus() const;
644
645 inline size_t getSlotCount() const { return mSlotCount; }
646 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
647
648private:
649 int32_t mCurrentSlot;
650 Slot* mSlots;
651 size_t mSlotCount;
652 bool mUsingSlotsProtocol;
653 bool mHaveStylus;
654
655 void clearSlots(int32_t initialSlot);
656};
657
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658/* An input mapper transforms raw input events into cooked event data.
659 * A single input device can have multiple associated input mappers in order to interpret
660 * different classes of events.
661 *
662 * InputMapper lifecycle:
663 * - create
664 * - configure with 0 changes
665 * - reset
666 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
667 * - reset
668 * - destroy
669 */
670class InputMapper {
671public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700672 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 virtual ~InputMapper();
674
675 inline InputDevice* getDevice() { return mDevice; }
676 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100677 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 inline InputReaderContext* getContext() { return mContext; }
679 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
680 inline InputListenerInterface* getListener() { return mContext->getListener(); }
681 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
682
683 virtual uint32_t getSources() = 0;
684 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800685 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
687 virtual void reset(nsecs_t when);
688 virtual void process(const RawEvent* rawEvent) = 0;
689 virtual void timeoutExpired(nsecs_t when);
690
691 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
692 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
693 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
694 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700695 const int32_t* keyCodes, uint8_t* outFlags);
696 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800698 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699
700 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800701 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702
Michael Wright842500e2015-03-13 17:32:02 -0700703 virtual void updateExternalStylusState(const StylusState& state);
704
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 virtual void fadePointer();
Arthur Hung2c9a3342019-07-23 14:18:59 +0800706 virtual std::optional<int32_t> getAssociatedDisplayId() { return std::nullopt; }
707
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708protected:
709 InputDevice* mDevice;
710 InputReaderContext* mContext;
711
712 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
713 void bumpGeneration();
714
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700715 static void dumpRawAbsoluteAxisInfo(std::string& dump, const RawAbsoluteAxisInfo& axis,
716 const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800717 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718};
719
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720class SwitchInputMapper : public InputMapper {
721public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700722 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723 virtual ~SwitchInputMapper();
724
725 virtual uint32_t getSources();
726 virtual void process(const RawEvent* rawEvent);
727
728 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800729 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730
731private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700732 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 uint32_t mUpdatedSwitchMask;
734
735 void processSwitch(int32_t switchCode, int32_t switchValue);
736 void sync(nsecs_t when);
737};
738
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739class VibratorInputMapper : public InputMapper {
740public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700741 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 virtual ~VibratorInputMapper();
743
744 virtual uint32_t getSources();
745 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
746 virtual void process(const RawEvent* rawEvent);
747
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700748 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 virtual void cancelVibrate(int32_t token);
750 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800751 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752
753private:
754 bool mVibrating;
755 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
756 size_t mPatternSize;
757 ssize_t mRepeat;
758 int32_t mToken;
759 ssize_t mIndex;
760 nsecs_t mNextStepTime;
761
762 void nextStep();
763 void stopVibrating();
764};
765
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766class KeyboardInputMapper : public InputMapper {
767public:
768 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
769 virtual ~KeyboardInputMapper();
770
771 virtual uint32_t getSources();
772 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800773 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
775 virtual void reset(nsecs_t when);
776 virtual void process(const RawEvent* rawEvent);
777
778 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
779 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
780 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700781 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782
783 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800784 virtual void updateMetaState(int32_t keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +0800785 virtual std::optional<int32_t> getAssociatedDisplayId();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786
787private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100788 // The current viewport.
789 std::optional<DisplayViewport> mViewport;
790
Michael Wrightd02c5b62014-02-10 15:10:22 -0800791 struct KeyDown {
792 int32_t keyCode;
793 int32_t scanCode;
794 };
795
796 uint32_t mSource;
797 int32_t mKeyboardType;
798
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 std::vector<KeyDown> mKeyDowns; // keys that are down
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 int32_t mMetaState;
801 nsecs_t mDownTime; // time of most recent key down
802
803 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
804
805 struct LedState {
806 bool avail; // led is available
807 bool on; // we think the led is currently on
808 };
809 LedState mCapsLockLedState;
810 LedState mNumLockLedState;
811 LedState mScrollLockLedState;
812
813 // Immutable configuration parameters.
814 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700816 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 } mParameters;
818
819 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800820 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100822 int32_t getOrientation();
823 int32_t getDisplayId();
824
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100826 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700828 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829
Andrii Kulian763a3a42016-03-08 10:46:16 -0800830 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
831
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 ssize_t findKeyDown(int32_t scanCode);
833
834 void resetLedState();
835 void initializeLedState(LedState& ledState, int32_t led);
836 void updateLedState(bool reset);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700837 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
Arthur Hung2c9a3342019-07-23 14:18:59 +0800838 std::optional<DisplayViewport> findViewport(nsecs_t when,
839 const InputReaderConfiguration* config);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840};
841
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842class CursorInputMapper : public InputMapper {
843public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700844 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 virtual ~CursorInputMapper();
846
847 virtual uint32_t getSources();
848 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800849 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
851 virtual void reset(nsecs_t when);
852 virtual void process(const RawEvent* rawEvent);
853
854 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
855
856 virtual void fadePointer();
857
Arthur Hung2c9a3342019-07-23 14:18:59 +0800858 virtual std::optional<int32_t> getAssociatedDisplayId();
859
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860private:
861 // Amount that trackball needs to move in order to generate a key event.
862 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
863
864 // Immutable configuration parameters.
865 struct Parameters {
866 enum Mode {
867 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800868 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 MODE_NAVIGATION,
870 };
871
872 Mode mode;
873 bool hasAssociatedDisplay;
874 bool orientationAware;
875 } mParameters;
876
877 CursorButtonAccumulator mCursorButtonAccumulator;
878 CursorMotionAccumulator mCursorMotionAccumulator;
879 CursorScrollAccumulator mCursorScrollAccumulator;
880
881 int32_t mSource;
882 float mXScale;
883 float mYScale;
884 float mXPrecision;
885 float mYPrecision;
886
887 float mVWheelScale;
888 float mHWheelScale;
889
890 // Velocity controls for mouse pointer and wheel movements.
891 // The controls for X and Y wheel movements are separate to keep them decoupled.
892 VelocityControl mPointerVelocityControl;
893 VelocityControl mWheelXVelocityControl;
894 VelocityControl mWheelYVelocityControl;
895
896 int32_t mOrientation;
897
898 sp<PointerControllerInterface> mPointerController;
899
900 int32_t mButtonState;
901 nsecs_t mDownTime;
902
903 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800904 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
906 void sync(nsecs_t when);
907};
908
Prashant Malani1941ff52015-08-11 18:29:28 -0700909class RotaryEncoderInputMapper : public InputMapper {
910public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700911 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700912 virtual ~RotaryEncoderInputMapper();
913
914 virtual uint32_t getSources();
915 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800916 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700917 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
918 virtual void reset(nsecs_t when);
919 virtual void process(const RawEvent* rawEvent);
920
921private:
922 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
923
924 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -0800925 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +0100926 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -0700927
928 void sync(nsecs_t when);
929};
930
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931class TouchInputMapper : public InputMapper {
932public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700933 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 virtual ~TouchInputMapper();
935
936 virtual uint32_t getSources();
937 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800938 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
940 virtual void reset(nsecs_t when);
941 virtual void process(const RawEvent* rawEvent);
942
943 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
944 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
945 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700946 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947
948 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -0800949 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700951 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hung2c9a3342019-07-23 14:18:59 +0800952 virtual std::optional<int32_t> getAssociatedDisplayId();
953
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954protected:
955 CursorButtonAccumulator mCursorButtonAccumulator;
956 CursorScrollAccumulator mCursorScrollAccumulator;
957 TouchButtonAccumulator mTouchButtonAccumulator;
958
959 struct VirtualKey {
960 int32_t keyCode;
961 int32_t scanCode;
962 uint32_t flags;
963
964 // computed hit box, specified in touch screen coords based on known display size
965 int32_t hitLeft;
966 int32_t hitTop;
967 int32_t hitRight;
968 int32_t hitBottom;
969
970 inline bool isHit(int32_t x, int32_t y) const {
971 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
972 }
973 };
974
975 // Input sources and device mode.
976 uint32_t mSource;
977
978 enum DeviceMode {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700979 DEVICE_MODE_DISABLED, // input is disabled
980 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
981 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700983 DEVICE_MODE_POINTER, // pointer mapping (pointer)
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 };
985 DeviceMode mDeviceMode;
986
987 // The reader's configuration.
988 InputReaderConfiguration mConfig;
989
990 // Immutable configuration parameters.
991 struct Parameters {
992 enum DeviceType {
993 DEVICE_TYPE_TOUCH_SCREEN,
994 DEVICE_TYPE_TOUCH_PAD,
995 DEVICE_TYPE_TOUCH_NAVIGATION,
996 DEVICE_TYPE_POINTER,
997 };
998
999 DeviceType deviceType;
1000 bool hasAssociatedDisplay;
1001 bool associatedDisplayIsExternal;
1002 bool orientationAware;
1003 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001004 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005
1006 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001007 GESTURE_MODE_SINGLE_TOUCH,
1008 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 };
1010 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001011
1012 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 } mParameters;
1014
1015 // Immutable calibration parameters in parsed form.
1016 struct Calibration {
1017 // Size
1018 enum SizeCalibration {
1019 SIZE_CALIBRATION_DEFAULT,
1020 SIZE_CALIBRATION_NONE,
1021 SIZE_CALIBRATION_GEOMETRIC,
1022 SIZE_CALIBRATION_DIAMETER,
1023 SIZE_CALIBRATION_BOX,
1024 SIZE_CALIBRATION_AREA,
1025 };
1026
1027 SizeCalibration sizeCalibration;
1028
1029 bool haveSizeScale;
1030 float sizeScale;
1031 bool haveSizeBias;
1032 float sizeBias;
1033 bool haveSizeIsSummed;
1034 bool sizeIsSummed;
1035
1036 // Pressure
1037 enum PressureCalibration {
1038 PRESSURE_CALIBRATION_DEFAULT,
1039 PRESSURE_CALIBRATION_NONE,
1040 PRESSURE_CALIBRATION_PHYSICAL,
1041 PRESSURE_CALIBRATION_AMPLITUDE,
1042 };
1043
1044 PressureCalibration pressureCalibration;
1045 bool havePressureScale;
1046 float pressureScale;
1047
1048 // Orientation
1049 enum OrientationCalibration {
1050 ORIENTATION_CALIBRATION_DEFAULT,
1051 ORIENTATION_CALIBRATION_NONE,
1052 ORIENTATION_CALIBRATION_INTERPOLATED,
1053 ORIENTATION_CALIBRATION_VECTOR,
1054 };
1055
1056 OrientationCalibration orientationCalibration;
1057
1058 // Distance
1059 enum DistanceCalibration {
1060 DISTANCE_CALIBRATION_DEFAULT,
1061 DISTANCE_CALIBRATION_NONE,
1062 DISTANCE_CALIBRATION_SCALED,
1063 };
1064
1065 DistanceCalibration distanceCalibration;
1066 bool haveDistanceScale;
1067 float distanceScale;
1068
1069 enum CoverageCalibration {
1070 COVERAGE_CALIBRATION_DEFAULT,
1071 COVERAGE_CALIBRATION_NONE,
1072 COVERAGE_CALIBRATION_BOX,
1073 };
1074
1075 CoverageCalibration coverageCalibration;
1076
1077 inline void applySizeScaleAndBias(float* outSize) const {
1078 if (haveSizeScale) {
1079 *outSize *= sizeScale;
1080 }
1081 if (haveSizeBias) {
1082 *outSize += sizeBias;
1083 }
1084 if (*outSize < 0) {
1085 *outSize = 0;
1086 }
1087 }
1088 } mCalibration;
1089
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001090 // Affine location transformation/calibration
1091 struct TouchAffineTransformation mAffineTransform;
1092
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093 RawPointerAxes mRawPointerAxes;
1094
Michael Wright842500e2015-03-13 17:32:02 -07001095 struct RawState {
1096 nsecs_t when;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097
Michael Wright842500e2015-03-13 17:32:02 -07001098 // Raw pointer sample data.
1099 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Michael Wright842500e2015-03-13 17:32:02 -07001101 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Michael Wright842500e2015-03-13 17:32:02 -07001103 // Scroll state.
1104 int32_t rawVScroll;
1105 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106
Michael Wright842500e2015-03-13 17:32:02 -07001107 void copyFrom(const RawState& other) {
1108 when = other.when;
1109 rawPointerData.copyFrom(other.rawPointerData);
1110 buttonState = other.buttonState;
1111 rawVScroll = other.rawVScroll;
1112 rawHScroll = other.rawHScroll;
1113 }
1114
1115 void clear() {
1116 when = 0;
1117 rawPointerData.clear();
1118 buttonState = 0;
1119 rawVScroll = 0;
1120 rawHScroll = 0;
1121 }
1122 };
1123
1124 struct CookedState {
1125 // Cooked pointer sample data.
1126 CookedPointerData cookedPointerData;
1127
1128 // Id bits used to differentiate fingers, stylus and mouse tools.
1129 BitSet32 fingerIdBits;
1130 BitSet32 stylusIdBits;
1131 BitSet32 mouseIdBits;
1132
Michael Wright7b159c92015-05-14 14:48:03 +01001133 int32_t buttonState;
1134
Michael Wright842500e2015-03-13 17:32:02 -07001135 void copyFrom(const CookedState& other) {
1136 cookedPointerData.copyFrom(other.cookedPointerData);
1137 fingerIdBits = other.fingerIdBits;
1138 stylusIdBits = other.stylusIdBits;
1139 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001140 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001141 }
1142
1143 void clear() {
1144 cookedPointerData.clear();
1145 fingerIdBits.clear();
1146 stylusIdBits.clear();
1147 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001148 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001149 }
1150 };
1151
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001152 std::vector<RawState> mRawStatesPending;
Michael Wright842500e2015-03-13 17:32:02 -07001153 RawState mCurrentRawState;
1154 CookedState mCurrentCookedState;
1155 RawState mLastRawState;
1156 CookedState mLastCookedState;
1157
1158 // State provided by an external stylus
1159 StylusState mExternalStylusState;
1160 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001161 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001162 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163
1164 // True if we sent a HOVER_ENTER event.
1165 bool mSentHoverEnter;
1166
Michael Wright842500e2015-03-13 17:32:02 -07001167 // Have we assigned pointer IDs for this stream
1168 bool mHavePointerIds;
1169
Michael Wright8e812822015-06-22 16:18:21 +01001170 // Is the current stream of direct touch events aborted
1171 bool mCurrentMotionAborted;
1172
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 // The time the primary pointer last went down.
1174 nsecs_t mDownTime;
1175
1176 // The pointer controller, or null if the device is not a pointer.
1177 sp<PointerControllerInterface> mPointerController;
1178
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001179 std::vector<VirtualKey> mVirtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180
1181 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001182 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001184 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001186 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001188 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 virtual void parseCalibration();
1190 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001191 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001192 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001193 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001194 virtual void resolveExternalStylusPresence();
1195 virtual bool hasStylus() const = 0;
1196 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197
Michael Wright842500e2015-03-13 17:32:02 -07001198 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199
1200private:
1201 // The current viewport.
1202 // The components of the viewport are specified in the display's rotated orientation.
1203 DisplayViewport mViewport;
1204
1205 // The surface orientation, width and height set by configureSurface().
1206 // The width and height are derived from the viewport but are specified
1207 // in the natural orientation.
1208 // The surface origin specifies how the surface coordinates should be translated
1209 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 int32_t mSurfaceWidth;
1211 int32_t mSurfaceHeight;
1212 int32_t mSurfaceLeft;
1213 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001214
1215 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1216 // the logical coordinate space.
1217 int32_t mPhysicalWidth;
1218 int32_t mPhysicalHeight;
1219 int32_t mPhysicalLeft;
1220 int32_t mPhysicalTop;
1221
1222 // The orientation may be different from the viewport orientation as it specifies
1223 // the rotation of the surface coordinates required to produce the viewport's
1224 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 int32_t mSurfaceOrientation;
1226
1227 // Translation and scaling factors, orientation-independent.
1228 float mXTranslate;
1229 float mXScale;
1230 float mXPrecision;
1231
1232 float mYTranslate;
1233 float mYScale;
1234 float mYPrecision;
1235
1236 float mGeometricScale;
1237
1238 float mPressureScale;
1239
1240 float mSizeScale;
1241
1242 float mOrientationScale;
1243
1244 float mDistanceScale;
1245
1246 bool mHaveTilt;
1247 float mTiltXCenter;
1248 float mTiltXScale;
1249 float mTiltYCenter;
1250 float mTiltYScale;
1251
Michael Wright842500e2015-03-13 17:32:02 -07001252 bool mExternalStylusConnected;
1253
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 // Oriented motion ranges for input device info.
1255 struct OrientedRanges {
1256 InputDeviceInfo::MotionRange x;
1257 InputDeviceInfo::MotionRange y;
1258 InputDeviceInfo::MotionRange pressure;
1259
1260 bool haveSize;
1261 InputDeviceInfo::MotionRange size;
1262
1263 bool haveTouchSize;
1264 InputDeviceInfo::MotionRange touchMajor;
1265 InputDeviceInfo::MotionRange touchMinor;
1266
1267 bool haveToolSize;
1268 InputDeviceInfo::MotionRange toolMajor;
1269 InputDeviceInfo::MotionRange toolMinor;
1270
1271 bool haveOrientation;
1272 InputDeviceInfo::MotionRange orientation;
1273
1274 bool haveDistance;
1275 InputDeviceInfo::MotionRange distance;
1276
1277 bool haveTilt;
1278 InputDeviceInfo::MotionRange tilt;
1279
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001280 OrientedRanges() { clear(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281
1282 void clear() {
1283 haveSize = false;
1284 haveTouchSize = false;
1285 haveToolSize = false;
1286 haveOrientation = false;
1287 haveDistance = false;
1288 haveTilt = false;
1289 }
1290 } mOrientedRanges;
1291
1292 // Oriented dimensions and precision.
1293 float mOrientedXPrecision;
1294 float mOrientedYPrecision;
1295
1296 struct CurrentVirtualKeyState {
1297 bool down;
1298 bool ignored;
1299 nsecs_t downTime;
1300 int32_t keyCode;
1301 int32_t scanCode;
1302 } mCurrentVirtualKey;
1303
1304 // Scale factor for gesture or mouse based pointer movements.
1305 float mPointerXMovementScale;
1306 float mPointerYMovementScale;
1307
1308 // Scale factor for gesture based zooming and other freeform motions.
1309 float mPointerXZoomScale;
1310 float mPointerYZoomScale;
1311
1312 // The maximum swipe width.
1313 float mPointerGestureMaxSwipeWidth;
1314
1315 struct PointerDistanceHeapElement {
1316 uint32_t currentPointerIndex : 8;
1317 uint32_t lastPointerIndex : 8;
1318 uint64_t distance : 48; // squared distance
1319 };
1320
1321 enum PointerUsage {
1322 POINTER_USAGE_NONE,
1323 POINTER_USAGE_GESTURES,
1324 POINTER_USAGE_STYLUS,
1325 POINTER_USAGE_MOUSE,
1326 };
1327 PointerUsage mPointerUsage;
1328
1329 struct PointerGesture {
1330 enum Mode {
1331 // No fingers, button is not pressed.
1332 // Nothing happening.
1333 NEUTRAL,
1334
1335 // No fingers, button is not pressed.
1336 // Tap detected.
1337 // Emits DOWN and UP events at the pointer location.
1338 TAP,
1339
1340 // Exactly one finger dragging following a tap.
1341 // Pointer follows the active finger.
1342 // Emits DOWN, MOVE and UP events at the pointer location.
1343 //
1344 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1345 TAP_DRAG,
1346
1347 // Button is pressed.
1348 // Pointer follows the active finger if there is one. Other fingers are ignored.
1349 // Emits DOWN, MOVE and UP events at the pointer location.
1350 BUTTON_CLICK_OR_DRAG,
1351
1352 // Exactly one finger, button is not pressed.
1353 // Pointer follows the active finger.
1354 // Emits HOVER_MOVE events at the pointer location.
1355 //
1356 // Detect taps when the finger goes up while in HOVER mode.
1357 HOVER,
1358
1359 // Exactly two fingers but neither have moved enough to clearly indicate
1360 // whether a swipe or freeform gesture was intended. We consider the
1361 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1362 // Pointer does not move.
1363 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1364 PRESS,
1365
1366 // Exactly two fingers moving in the same direction, button is not pressed.
1367 // Pointer does not move.
1368 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1369 // follows the midpoint between both fingers.
1370 SWIPE,
1371
1372 // Two or more fingers moving in arbitrary directions, button is not pressed.
1373 // Pointer does not move.
1374 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1375 // each finger individually relative to the initial centroid of the finger.
1376 FREEFORM,
1377
1378 // Waiting for quiet time to end before starting the next gesture.
1379 QUIET,
1380 };
1381
1382 // Time the first finger went down.
1383 nsecs_t firstTouchTime;
1384
1385 // The active pointer id from the raw touch data.
1386 int32_t activeTouchId; // -1 if none
1387
1388 // The active pointer id from the gesture last delivered to the application.
1389 int32_t activeGestureId; // -1 if none
1390
1391 // Pointer coords and ids for the current and previous pointer gesture.
1392 Mode currentGestureMode;
1393 BitSet32 currentGestureIdBits;
1394 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1395 PointerProperties currentGestureProperties[MAX_POINTERS];
1396 PointerCoords currentGestureCoords[MAX_POINTERS];
1397
1398 Mode lastGestureMode;
1399 BitSet32 lastGestureIdBits;
1400 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1401 PointerProperties lastGestureProperties[MAX_POINTERS];
1402 PointerCoords lastGestureCoords[MAX_POINTERS];
1403
1404 // Time the pointer gesture last went down.
1405 nsecs_t downTime;
1406
1407 // Time when the pointer went down for a TAP.
1408 nsecs_t tapDownTime;
1409
1410 // Time when the pointer went up for a TAP.
1411 nsecs_t tapUpTime;
1412
1413 // Location of initial tap.
1414 float tapX, tapY;
1415
1416 // Time we started waiting for quiescence.
1417 nsecs_t quietTime;
1418
1419 // Reference points for multitouch gestures.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001420 float referenceTouchX; // reference touch X/Y coordinates in surface units
Michael Wrightd02c5b62014-02-10 15:10:22 -08001421 float referenceTouchY;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001422 float referenceGestureX; // reference gesture X/Y coordinates in pixels
Michael Wrightd02c5b62014-02-10 15:10:22 -08001423 float referenceGestureY;
1424
1425 // Distance that each pointer has traveled which has not yet been
1426 // subsumed into the reference gesture position.
1427 BitSet32 referenceIdBits;
1428 struct Delta {
1429 float dx, dy;
1430 };
1431 Delta referenceDeltas[MAX_POINTER_ID + 1];
1432
1433 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1434 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1435
1436 // A velocity tracker for determining whether to switch active pointers during drags.
1437 VelocityTracker velocityTracker;
1438
1439 void reset() {
1440 firstTouchTime = LLONG_MIN;
1441 activeTouchId = -1;
1442 activeGestureId = -1;
1443 currentGestureMode = NEUTRAL;
1444 currentGestureIdBits.clear();
1445 lastGestureMode = NEUTRAL;
1446 lastGestureIdBits.clear();
1447 downTime = 0;
1448 velocityTracker.clear();
1449 resetTap();
1450 resetQuietTime();
1451 }
1452
1453 void resetTap() {
1454 tapDownTime = LLONG_MIN;
1455 tapUpTime = LLONG_MIN;
1456 }
1457
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001458 void resetQuietTime() { quietTime = LLONG_MIN; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001459 } mPointerGesture;
1460
1461 struct PointerSimple {
1462 PointerCoords currentCoords;
1463 PointerProperties currentProperties;
1464 PointerCoords lastCoords;
1465 PointerProperties lastProperties;
1466
1467 // True if the pointer is down.
1468 bool down;
1469
1470 // True if the pointer is hovering.
1471 bool hovering;
1472
1473 // Time the pointer last went down.
1474 nsecs_t downTime;
1475
1476 void reset() {
1477 currentCoords.clear();
1478 currentProperties.clear();
1479 lastCoords.clear();
1480 lastProperties.clear();
1481 down = false;
1482 hovering = false;
1483 downTime = 0;
1484 }
1485 } mPointerSimple;
1486
1487 // The pointer and scroll velocity controls.
1488 VelocityControl mPointerVelocityControl;
1489 VelocityControl mWheelXVelocityControl;
1490 VelocityControl mWheelYVelocityControl;
1491
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001492 std::optional<DisplayViewport> findViewport();
1493
Michael Wright842500e2015-03-13 17:32:02 -07001494 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001495 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001496
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 void sync(nsecs_t when);
1498
1499 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001500 void processRawTouches(bool timeout);
1501 void cookAndDispatch(nsecs_t when);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001502 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, int32_t keyEventAction,
1503 int32_t keyEventFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
1505 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1506 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1507 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001508 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1509 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1510 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001512 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513
1514 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1515 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1516
1517 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1518 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001519 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
1520 bool* outFinishPreviousGesture, bool isTimeout);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001521
1522 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1523 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1524
1525 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1526 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1527
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001528 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, bool down, bool hovering);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1530
Michael Wright842500e2015-03-13 17:32:02 -07001531 bool assignExternalStylusId(const RawState& state, bool timeout);
1532 void applyExternalStylusButtonState(nsecs_t when);
1533 void applyExternalStylusTouchState(nsecs_t when);
1534
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 // Dispatches a motion event.
1536 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1537 // method will take care of setting the index and transmuting the action to DOWN or UP
1538 // it is the first / last pointer to go down / up.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001539 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, int32_t action,
1540 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
1541 int32_t edgeFlags, const PointerProperties* properties,
1542 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
1543 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544
1545 // Updates pointer coords and properties for pointers with specified ids that have moved.
1546 // Returns true if any of them changed.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001547 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
1548 const uint32_t* inIdToIndex, PointerProperties* outProperties,
1549 PointerCoords* outCoords, const uint32_t* outIdToIndex,
1550 BitSet32 idBits) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551
1552 bool isPointInsideSurface(int32_t x, int32_t y);
1553 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1554
Michael Wright842500e2015-03-13 17:32:02 -07001555 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001556
1557 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558};
1559
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560class SingleTouchInputMapper : public TouchInputMapper {
1561public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001562 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 virtual ~SingleTouchInputMapper();
1564
1565 virtual void reset(nsecs_t when);
1566 virtual void process(const RawEvent* rawEvent);
1567
1568protected:
Michael Wright842500e2015-03-13 17:32:02 -07001569 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570 virtual void configureRawPointerAxes();
1571 virtual bool hasStylus() const;
1572
1573private:
1574 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1575};
1576
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577class MultiTouchInputMapper : public TouchInputMapper {
1578public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001579 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 virtual ~MultiTouchInputMapper();
1581
1582 virtual void reset(nsecs_t when);
1583 virtual void process(const RawEvent* rawEvent);
1584
1585protected:
Michael Wright842500e2015-03-13 17:32:02 -07001586 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 virtual void configureRawPointerAxes();
1588 virtual bool hasStylus() const;
1589
1590private:
1591 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1592
1593 // Specifies the pointer id bits that are in use, and their associated tracking id.
1594 BitSet32 mPointerIdBits;
1595 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1596};
1597
Michael Wright842500e2015-03-13 17:32:02 -07001598class ExternalStylusInputMapper : public InputMapper {
1599public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001600 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001601 virtual ~ExternalStylusInputMapper() = default;
1602
1603 virtual uint32_t getSources();
1604 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001605 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001606 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1607 virtual void reset(nsecs_t when);
1608 virtual void process(const RawEvent* rawEvent);
1609 virtual void sync(nsecs_t when);
1610
1611private:
1612 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1613 RawAbsoluteAxisInfo mRawPressureAxis;
1614 TouchButtonAccumulator mTouchButtonAccumulator;
1615
1616 StylusState mStylusState;
1617};
1618
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619class JoystickInputMapper : public InputMapper {
1620public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001621 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 virtual ~JoystickInputMapper();
1623
1624 virtual uint32_t getSources();
1625 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001626 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1628 virtual void reset(nsecs_t when);
1629 virtual void process(const RawEvent* rawEvent);
1630
1631private:
1632 struct Axis {
1633 RawAbsoluteAxisInfo rawAxisInfo;
1634 AxisInfo axisInfo;
1635
1636 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1637
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001638 float scale; // scale factor from raw to normalized values
1639 float offset; // offset to add after scaling for normalization
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 float highScale; // scale factor from raw to normalized values of high split
1641 float highOffset; // offset to add after scaling for normalization of high split
1642
1643 float min; // normalized inclusive minimum
1644 float max; // normalized inclusive maximum
1645 float flat; // normalized flat region size
1646 float fuzz; // normalized error tolerance
1647 float resolution; // normalized resolution in units/mm
1648
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001649 float filter; // filter out small variations of this size
1650 float currentValue; // current value
1651 float newValue; // most recent value
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652 float highCurrentValue; // current value of high split
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001653 float highNewValue; // most recent value of high split
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654
1655 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001656 bool explicitlyMapped, float scale, float offset, float highScale,
1657 float highOffset, float min, float max, float flat, float fuzz,
1658 float resolution) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001659 this->rawAxisInfo = rawAxisInfo;
1660 this->axisInfo = axisInfo;
1661 this->explicitlyMapped = explicitlyMapped;
1662 this->scale = scale;
1663 this->offset = offset;
1664 this->highScale = highScale;
1665 this->highOffset = highOffset;
1666 this->min = min;
1667 this->max = max;
1668 this->flat = flat;
1669 this->fuzz = fuzz;
1670 this->resolution = resolution;
1671 this->filter = 0;
1672 resetValue();
1673 }
1674
1675 void resetValue() {
1676 this->currentValue = 0;
1677 this->newValue = 0;
1678 this->highCurrentValue = 0;
1679 this->highNewValue = 0;
1680 }
1681 };
1682
1683 // Axes indexed by raw ABS_* axis index.
1684 KeyedVector<int32_t, Axis> mAxes;
1685
1686 void sync(nsecs_t when, bool force);
1687
1688 bool haveAxis(int32_t axisId);
1689 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1690 bool filterAxes(bool force);
1691
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001692 static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue,
1693 float min, float max);
1694 static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
1695 float currentValue, float thresholdValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696
1697 static bool isCenteredAxis(int32_t axis);
1698 static int32_t getCompatAxis(int32_t axis);
1699
1700 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001701 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001702};
1703
1704} // namespace android
1705
1706#endif // _UI_INPUT_READER_H