blob: 1eec0e911d9e9173ecc0516c0c63b6d31ec85ef2 [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 Pradhan2b281812019-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>
Prabir Pradhan2b281812019-08-29 14:12:42 -070030#include <utils/BitSet.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070031#include <utils/Condition.h>
Prabir Pradhan2b281812019-08-29 14:12:42 -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 Pradhan2b281812019-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 Pradhan2b281812019-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 Pradhan2b281812019-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:
Prabir Pradhan2b281812019-08-29 14:12:42 -0700114 InputReader(const sp<EventHubInterface>& eventHub, const sp<InputReaderPolicyInterface>& policy,
115 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116 virtual ~InputReader();
117
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800118 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 virtual void monitor();
120
121 virtual void loopOnce();
122
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800123 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700125 virtual bool isInputDeviceEnabled(int32_t deviceId);
126
Prabir Pradhan2b281812019-08-29 14:12:42 -0700127 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode);
128 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode);
129 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130
Andrii Kulian763a3a42016-03-08 10:46:16 -0800131 virtual void toggleCapsLockState(int32_t deviceId);
132
Prabir Pradhan2b281812019-08-29 14:12:42 -0700133 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
134 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135
136 virtual void requestRefreshConfiguration(uint32_t changes);
137
138 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700139 ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 virtual void cancelVibrate(int32_t deviceId, int32_t token);
141
Arthur Hungc23540e2018-11-29 20:42:11 +0800142 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
Prabir Pradhan2b281812019-08-29 14:12:42 -0700143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144protected:
145 // These members are protected so they can be instrumented by test cases.
146 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700147 const InputDeviceIdentifier& identifier,
148 uint32_t classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
150 class ContextImpl : public InputReaderContext {
151 InputReader* mReader;
152
153 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700154 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
156 virtual void updateGlobalMetaState();
157 virtual int32_t getGlobalMetaState();
158 virtual void disableVirtualKeysUntil(nsecs_t time);
Prabir Pradhan2b281812019-08-29 14:12:42 -0700159 virtual bool shouldDropVirtualKey(nsecs_t now, InputDevice* device, int32_t keyCode,
160 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 virtual void fadePointer();
162 virtual void requestTimeoutAtTime(nsecs_t when);
163 virtual int32_t bumpGeneration();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800164 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700165 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 virtual InputReaderPolicyInterface* getPolicy();
167 virtual InputListenerInterface* getListener();
168 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800169 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 } mContext;
171
172 friend class ContextImpl;
173
174private:
175 Mutex mLock;
176
177 Condition mReaderIsAliveCondition;
178
179 sp<EventHubInterface> mEventHub;
180 sp<InputReaderPolicyInterface> mPolicy;
181 sp<QueuedInputListener> mQueuedListener;
182
183 InputReaderConfiguration mConfig;
184
Prabir Pradhan42611e02018-11-27 14:04:02 -0800185 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
186 uint32_t mNextSequenceNum;
187
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 // The event queue.
189 static const int EVENT_BUFFER_SIZE = 256;
190 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
191
192 KeyedVector<int32_t, InputDevice*> mDevices;
193
194 // low-level input event decoding and device management
195 void processEventsLocked(const RawEvent* rawEvents, size_t count);
196
197 void addDeviceLocked(nsecs_t when, int32_t deviceId);
198 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
199 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
200 void timeoutExpiredLocked(nsecs_t when);
201
202 void handleConfigurationChangedLocked(nsecs_t when);
203
204 int32_t mGlobalMetaState;
205 void updateGlobalMetaStateLocked();
206 int32_t getGlobalMetaStateLocked();
207
Michael Wright842500e2015-03-13 17:32:02 -0700208 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800209 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700210 void dispatchExternalStylusState(const StylusState& state);
211
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212 void fadePointerLocked();
213
214 int32_t mGeneration;
215 int32_t bumpGenerationLocked();
216
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800217 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
219 nsecs_t mDisableVirtualKeysTimeout;
220 void disableVirtualKeysUntilLocked(nsecs_t time);
Prabir Pradhan2b281812019-08-29 14:12:42 -0700221 bool shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
222 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
224 nsecs_t mNextTimeout;
225 void requestTimeoutAtTimeLocked(nsecs_t when);
226
227 uint32_t mConfigurationChangesToRefresh;
228 void refreshConfigurationLocked(uint32_t changes);
229
230 // state queries
231 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
232 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700233 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700235 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236};
237
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238/* Represents the state of a single input device. */
239class InputDevice {
240public:
Prabir Pradhan2b281812019-08-29 14:12:42 -0700241 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
242 int32_t controllerNumber, const InputDeviceIdentifier& identifier,
243 uint32_t classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244 ~InputDevice();
245
246 inline InputReaderContext* getContext() { return mContext; }
247 inline int32_t getId() const { return mId; }
248 inline int32_t getControllerNumber() const { return mControllerNumber; }
249 inline int32_t getGeneration() const { return mGeneration; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100250 inline const std::string getName() const { return mIdentifier.name; }
251 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 inline uint32_t getClasses() const { return mClasses; }
253 inline uint32_t getSources() const { return mSources; }
254
255 inline bool isExternal() { return mIsExternal; }
256 inline void setExternal(bool external) { mIsExternal = external; }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700257 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
258 return mAssociatedDisplayPort;
259 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260
Tim Kilbourn063ff532015-04-08 10:26:18 -0700261 inline void setMic(bool hasMic) { mHasMic = hasMic; }
262 inline bool hasMic() const { return mHasMic; }
263
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800264 inline bool isIgnored() { return mMappers.empty(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700266 bool isEnabled();
267 void setEnabled(bool enabled, nsecs_t when);
268
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800269 void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 void addMapper(InputMapper* mapper);
271 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
272 void reset(nsecs_t when);
273 void process(const RawEvent* rawEvents, size_t count);
274 void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -0700275 void updateExternalStylusState(const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276
277 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
278 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
279 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
280 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Prabir Pradhan2b281812019-08-29 14:12:42 -0700281 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
282 uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
284 void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800285 void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286
287 int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800288 void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289
290 void fadePointer();
291
292 void bumpGeneration();
293
294 void notifyReset(nsecs_t when);
295
296 inline const PropertyMap& getConfiguration() { return mConfiguration; }
297 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
298
Prabir Pradhan2b281812019-08-29 14:12:42 -0700299 bool hasKey(int32_t code) { return getEventHub()->hasScanCode(mId, code); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300
301 bool hasAbsoluteAxis(int32_t code) {
302 RawAbsoluteAxisInfo info;
303 getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
304 return info.valid;
305 }
306
307 bool isKeyPressed(int32_t code) {
308 return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
309 }
310
311 int32_t getAbsoluteAxisValue(int32_t code) {
312 int32_t value;
313 getEventHub()->getAbsoluteAxisValue(mId, code, &value);
314 return value;
315 }
316
Arthur Hungc23540e2018-11-29 20:42:11 +0800317 std::optional<int32_t> getAssociatedDisplay();
Prabir Pradhan2b281812019-08-29 14:12:42 -0700318
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319private:
320 InputReaderContext* mContext;
321 int32_t mId;
322 int32_t mGeneration;
323 int32_t mControllerNumber;
324 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 std::string mAlias;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 uint32_t mClasses;
327
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800328 std::vector<InputMapper*> mMappers;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329
330 uint32_t mSources;
331 bool mIsExternal;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700332 std::optional<uint8_t> mAssociatedDisplayPort;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700333 bool mHasMic;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 bool mDropUntilNextSync;
335
336 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
337 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
338
339 PropertyMap mConfiguration;
340};
341
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342/* Keeps track of the state of mouse or touch pad buttons. */
343class CursorButtonAccumulator {
344public:
345 CursorButtonAccumulator();
346 void reset(InputDevice* device);
347
348 void process(const RawEvent* rawEvent);
349
350 uint32_t getButtonState() const;
351
352private:
353 bool mBtnLeft;
354 bool mBtnRight;
355 bool mBtnMiddle;
356 bool mBtnBack;
357 bool mBtnSide;
358 bool mBtnForward;
359 bool mBtnExtra;
360 bool mBtnTask;
361
362 void clearButtons();
363};
364
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365/* Keeps track of cursor movements. */
366
367class CursorMotionAccumulator {
368public:
369 CursorMotionAccumulator();
370 void reset(InputDevice* device);
371
372 void process(const RawEvent* rawEvent);
373 void finishSync();
374
375 inline int32_t getRelativeX() const { return mRelX; }
376 inline int32_t getRelativeY() const { return mRelY; }
377
378private:
379 int32_t mRelX;
380 int32_t mRelY;
381
382 void clearRelativeAxes();
383};
384
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385/* Keeps track of cursor scrolling motions. */
386
387class CursorScrollAccumulator {
388public:
389 CursorScrollAccumulator();
390 void configure(InputDevice* device);
391 void reset(InputDevice* device);
392
393 void process(const RawEvent* rawEvent);
394 void finishSync();
395
396 inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
397 inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
398
399 inline int32_t getRelativeX() const { return mRelX; }
400 inline int32_t getRelativeY() const { return mRelY; }
401 inline int32_t getRelativeVWheel() const { return mRelWheel; }
402 inline int32_t getRelativeHWheel() const { return mRelHWheel; }
403
404private:
405 bool mHaveRelWheel;
406 bool mHaveRelHWheel;
407
408 int32_t mRelX;
409 int32_t mRelY;
410 int32_t mRelWheel;
411 int32_t mRelHWheel;
412
413 void clearRelativeAxes();
414};
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416/* Keeps track of the state of touch, stylus and tool buttons. */
417class TouchButtonAccumulator {
418public:
419 TouchButtonAccumulator();
420 void configure(InputDevice* device);
421 void reset(InputDevice* device);
422
423 void process(const RawEvent* rawEvent);
424
425 uint32_t getButtonState() const;
426 int32_t getToolType() const;
427 bool isToolActive() const;
428 bool isHovering() const;
429 bool hasStylus() const;
430
431private:
432 bool mHaveBtnTouch;
433 bool mHaveStylus;
434
435 bool mBtnTouch;
436 bool mBtnStylus;
437 bool mBtnStylus2;
438 bool mBtnToolFinger;
439 bool mBtnToolPen;
440 bool mBtnToolRubber;
441 bool mBtnToolBrush;
442 bool mBtnToolPencil;
443 bool mBtnToolAirbrush;
444 bool mBtnToolMouse;
445 bool mBtnToolLens;
446 bool mBtnToolDoubleTap;
447 bool mBtnToolTripleTap;
448 bool mBtnToolQuadTap;
449
450 void clearButtons();
451};
452
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453/* Raw axis information from the driver. */
454struct RawPointerAxes {
455 RawAbsoluteAxisInfo x;
456 RawAbsoluteAxisInfo y;
457 RawAbsoluteAxisInfo pressure;
458 RawAbsoluteAxisInfo touchMajor;
459 RawAbsoluteAxisInfo touchMinor;
460 RawAbsoluteAxisInfo toolMajor;
461 RawAbsoluteAxisInfo toolMinor;
462 RawAbsoluteAxisInfo orientation;
463 RawAbsoluteAxisInfo distance;
464 RawAbsoluteAxisInfo tiltX;
465 RawAbsoluteAxisInfo tiltY;
466 RawAbsoluteAxisInfo trackingId;
467 RawAbsoluteAxisInfo slot;
468
469 RawPointerAxes();
Siarhei Vishniakou26e34d92018-11-12 13:51:26 -0800470 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
471 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472 void clear();
473};
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475/* Raw data for a collection of pointers including a pointer id mapping table. */
476struct RawPointerData {
477 struct Pointer {
478 uint32_t id;
479 int32_t x;
480 int32_t y;
481 int32_t pressure;
482 int32_t touchMajor;
483 int32_t touchMinor;
484 int32_t toolMajor;
485 int32_t toolMinor;
486 int32_t orientation;
487 int32_t distance;
488 int32_t tiltX;
489 int32_t tiltY;
490 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
491 bool isHovering;
492 };
493
494 uint32_t pointerCount;
495 Pointer pointers[MAX_POINTERS];
496 BitSet32 hoveringIdBits, touchingIdBits;
497 uint32_t idToIndex[MAX_POINTER_ID + 1];
498
499 RawPointerData();
500 void clear();
501 void copyFrom(const RawPointerData& other);
502 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
503
504 inline void markIdBit(uint32_t id, bool isHovering) {
505 if (isHovering) {
506 hoveringIdBits.markBit(id);
507 } else {
508 touchingIdBits.markBit(id);
509 }
510 }
511
512 inline void clearIdBits() {
513 hoveringIdBits.clear();
514 touchingIdBits.clear();
515 }
516
Prabir Pradhan2b281812019-08-29 14:12:42 -0700517 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Prabir Pradhan2b281812019-08-29 14:12:42 -0700519 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520};
521
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522/* Cooked data for a collection of pointers including a pointer id mapping table. */
523struct CookedPointerData {
524 uint32_t pointerCount;
525 PointerProperties pointerProperties[MAX_POINTERS];
526 PointerCoords pointerCoords[MAX_POINTERS];
527 BitSet32 hoveringIdBits, touchingIdBits;
528 uint32_t idToIndex[MAX_POINTER_ID + 1];
529
530 CookedPointerData();
531 void clear();
532 void copyFrom(const CookedPointerData& other);
533
534 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
535 return pointerCoords[idToIndex[id]];
536 }
537
Michael Wright842500e2015-03-13 17:32:02 -0700538 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
539 return pointerCoords[idToIndex[id]];
540 }
541
542 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
543 return pointerProperties[idToIndex[id]];
544 }
545
Michael Wright53dca3a2015-04-23 17:39:53 +0100546 inline bool isHovering(uint32_t pointerIndex) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
548 }
Michael Wright842500e2015-03-13 17:32:02 -0700549
Michael Wright53dca3a2015-04-23 17:39:53 +0100550 inline bool isTouching(uint32_t pointerIndex) const {
Michael Wright842500e2015-03-13 17:32:02 -0700551 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
552 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553};
554
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -0800555/**
556 * Basic statistics information.
557 * Keep track of min, max, average, and standard deviation of the received samples.
558 * Used to report latency information about input events.
559 */
560struct LatencyStatistics {
561 float min;
562 float max;
563 // Sum of all samples
564 float sum;
565 // Sum of squares of all samples
566 float sum2;
567 // The number of samples
568 size_t count;
569 // The last time statistics were reported.
570 nsecs_t lastReportTime;
571
Prabir Pradhan2b281812019-08-29 14:12:42 -0700572 LatencyStatistics() { reset(systemTime(SYSTEM_TIME_MONOTONIC)); }
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -0800573
574 inline void addValue(float x) {
575 if (x < min) {
576 min = x;
577 }
578 if (x > max) {
579 max = x;
580 }
581 sum += x;
582 sum2 += x * x;
583 count++;
584 }
585
586 // Get the average value. Should not be called if no samples have been added.
587 inline float mean() {
588 if (count == 0) {
589 return 0;
590 }
591 return sum / count;
592 }
593
594 // Get the standard deviation. Should not be called if no samples have been added.
595 inline float stdev() {
596 if (count == 0) {
597 return 0;
598 }
599 float average = mean();
600 return sqrt(sum2 / count - average * average);
601 }
602
603 /**
604 * Reset internal state. The variable 'when' is the time when the data collection started.
605 * Call this to start a new data collection window.
606 */
607 inline void reset(nsecs_t when) {
608 max = 0;
609 min = std::numeric_limits<float>::max();
610 sum = 0;
611 sum2 = 0;
612 count = 0;
613 lastReportTime = when;
614 }
615};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616
617/* Keeps track of the state of single-touch protocol. */
618class SingleTouchMotionAccumulator {
619public:
620 SingleTouchMotionAccumulator();
621
622 void process(const RawEvent* rawEvent);
623 void reset(InputDevice* device);
624
625 inline int32_t getAbsoluteX() const { return mAbsX; }
626 inline int32_t getAbsoluteY() const { return mAbsY; }
627 inline int32_t getAbsolutePressure() const { return mAbsPressure; }
628 inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
629 inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
630 inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
631 inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
632
633private:
634 int32_t mAbsX;
635 int32_t mAbsY;
636 int32_t mAbsPressure;
637 int32_t mAbsToolWidth;
638 int32_t mAbsDistance;
639 int32_t mAbsTiltX;
640 int32_t mAbsTiltY;
641
642 void clearAbsoluteAxes();
643};
644
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645/* Keeps track of the state of multi-touch protocol. */
646class MultiTouchMotionAccumulator {
647public:
648 class Slot {
649 public:
650 inline bool isInUse() const { return mInUse; }
651 inline int32_t getX() const { return mAbsMTPositionX; }
652 inline int32_t getY() const { return mAbsMTPositionY; }
653 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
654 inline int32_t getTouchMinor() const {
Prabir Pradhan2b281812019-08-29 14:12:42 -0700655 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor;
656 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
658 inline int32_t getToolMinor() const {
Prabir Pradhan2b281812019-08-29 14:12:42 -0700659 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor;
660 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 inline int32_t getOrientation() const { return mAbsMTOrientation; }
662 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
663 inline int32_t getPressure() const { return mAbsMTPressure; }
664 inline int32_t getDistance() const { return mAbsMTDistance; }
665 inline int32_t getToolType() const;
666
667 private:
668 friend class MultiTouchMotionAccumulator;
669
670 bool mInUse;
671 bool mHaveAbsMTTouchMinor;
672 bool mHaveAbsMTWidthMinor;
673 bool mHaveAbsMTToolType;
674
675 int32_t mAbsMTPositionX;
676 int32_t mAbsMTPositionY;
677 int32_t mAbsMTTouchMajor;
678 int32_t mAbsMTTouchMinor;
679 int32_t mAbsMTWidthMajor;
680 int32_t mAbsMTWidthMinor;
681 int32_t mAbsMTOrientation;
682 int32_t mAbsMTTrackingId;
683 int32_t mAbsMTPressure;
684 int32_t mAbsMTDistance;
685 int32_t mAbsMTToolType;
686
687 Slot();
688 void clear();
689 };
690
691 MultiTouchMotionAccumulator();
692 ~MultiTouchMotionAccumulator();
693
694 void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
695 void reset(InputDevice* device);
696 void process(const RawEvent* rawEvent);
697 void finishSync();
698 bool hasStylus() const;
699
700 inline size_t getSlotCount() const { return mSlotCount; }
701 inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800702 inline uint32_t getDeviceTimestamp() const { return mDeviceTimestamp; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703
704private:
705 int32_t mCurrentSlot;
706 Slot* mSlots;
707 size_t mSlotCount;
708 bool mUsingSlotsProtocol;
709 bool mHaveStylus;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -0800710 uint32_t mDeviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711
712 void clearSlots(int32_t initialSlot);
713};
714
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715/* An input mapper transforms raw input events into cooked event data.
716 * A single input device can have multiple associated input mappers in order to interpret
717 * different classes of events.
718 *
719 * InputMapper lifecycle:
720 * - create
721 * - configure with 0 changes
722 * - reset
723 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
724 * - reset
725 * - destroy
726 */
727class InputMapper {
728public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700729 explicit InputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 virtual ~InputMapper();
731
732 inline InputDevice* getDevice() { return mDevice; }
733 inline int32_t getDeviceId() { return mDevice->getId(); }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100734 inline const std::string getDeviceName() { return mDevice->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 inline InputReaderContext* getContext() { return mContext; }
736 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
737 inline InputListenerInterface* getListener() { return mContext->getListener(); }
738 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
739
740 virtual uint32_t getSources() = 0;
741 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800742 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
744 virtual void reset(nsecs_t when);
745 virtual void process(const RawEvent* rawEvent) = 0;
746 virtual void timeoutExpired(nsecs_t when);
747
748 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
749 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
750 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
751 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700752 const int32_t* keyCodes, uint8_t* outFlags);
753 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 virtual void cancelVibrate(int32_t token);
Jeff Brownc9aa6282015-02-11 19:03:28 -0800755 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756
757 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800758 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759
Michael Wright842500e2015-03-13 17:32:02 -0700760 virtual void updateExternalStylusState(const StylusState& state);
761
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 virtual void fadePointer();
Prabir Pradhan2b281812019-08-29 14:12:42 -0700763 virtual std::optional<int32_t> getAssociatedDisplay() { return std::nullopt; }
764
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765protected:
766 InputDevice* mDevice;
767 InputReaderContext* mContext;
768
769 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
770 void bumpGeneration();
771
Prabir Pradhan2b281812019-08-29 14:12:42 -0700772 static void dumpRawAbsoluteAxisInfo(std::string& dump, const RawAbsoluteAxisInfo& axis,
773 const char* name);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800774 static void dumpStylusState(std::string& dump, const StylusState& state);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775};
776
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777class SwitchInputMapper : public InputMapper {
778public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700779 explicit SwitchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780 virtual ~SwitchInputMapper();
781
782 virtual uint32_t getSources();
783 virtual void process(const RawEvent* rawEvent);
784
785 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800786 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788private:
Michael Wrightbcbf97e2014-08-29 14:31:32 -0700789 uint32_t mSwitchValues;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 uint32_t mUpdatedSwitchMask;
791
792 void processSwitch(int32_t switchCode, int32_t switchValue);
793 void sync(nsecs_t when);
794};
795
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796class VibratorInputMapper : public InputMapper {
797public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700798 explicit VibratorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799 virtual ~VibratorInputMapper();
800
801 virtual uint32_t getSources();
802 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
803 virtual void process(const RawEvent* rawEvent);
804
Prabir Pradhan2b281812019-08-29 14:12:42 -0700805 virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 virtual void cancelVibrate(int32_t token);
807 virtual void timeoutExpired(nsecs_t when);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800808 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809
810private:
811 bool mVibrating;
812 nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
813 size_t mPatternSize;
814 ssize_t mRepeat;
815 int32_t mToken;
816 ssize_t mIndex;
817 nsecs_t mNextStepTime;
818
819 void nextStep();
820 void stopVibrating();
821};
822
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823class KeyboardInputMapper : public InputMapper {
824public:
825 KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
826 virtual ~KeyboardInputMapper();
827
828 virtual uint32_t getSources();
829 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800830 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
832 virtual void reset(nsecs_t when);
833 virtual void process(const RawEvent* rawEvent);
834
835 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
836 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
837 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700838 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
840 virtual int32_t getMetaState();
Andrii Kulian763a3a42016-03-08 10:46:16 -0800841 virtual void updateMetaState(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842
843private:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100844 // The current viewport.
845 std::optional<DisplayViewport> mViewport;
846
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 struct KeyDown {
848 int32_t keyCode;
849 int32_t scanCode;
850 };
851
852 uint32_t mSource;
853 int32_t mKeyboardType;
854
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800855 std::vector<KeyDown> mKeyDowns; // keys that are down
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 int32_t mMetaState;
857 nsecs_t mDownTime; // time of most recent key down
858
859 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
860
861 struct LedState {
862 bool avail; // led is available
863 bool on; // we think the led is currently on
864 };
865 LedState mCapsLockLedState;
866 LedState mNumLockLedState;
867 LedState mScrollLockLedState;
868
869 // Immutable configuration parameters.
870 struct Parameters {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871 bool orientationAware;
Michael Wrightdcfcf5d2014-03-17 12:58:21 -0700872 bool handlesKeyRepeat;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 } mParameters;
874
875 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800876 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100878 int32_t getOrientation();
879 int32_t getDisplayId();
880
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 bool isKeyboardOrGamepadKey(int32_t scanCode);
Michael Wright58ba9882017-07-26 16:19:11 +0100882 bool isMediaKey(int32_t keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700884 void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885
Andrii Kulian763a3a42016-03-08 10:46:16 -0800886 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
887
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 ssize_t findKeyDown(int32_t scanCode);
889
890 void resetLedState();
891 void initializeLedState(LedState& ledState, int32_t led);
892 void updateLedState(bool reset);
Prabir Pradhan2b281812019-08-29 14:12:42 -0700893 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894};
895
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896class CursorInputMapper : public InputMapper {
897public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700898 explicit CursorInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 virtual ~CursorInputMapper();
900
901 virtual uint32_t getSources();
902 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800903 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
905 virtual void reset(nsecs_t when);
906 virtual void process(const RawEvent* rawEvent);
907
908 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
909
910 virtual void fadePointer();
911
Arthur Hungc23540e2018-11-29 20:42:11 +0800912 virtual std::optional<int32_t> getAssociatedDisplay();
Prabir Pradhan2b281812019-08-29 14:12:42 -0700913
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914private:
915 // Amount that trackball needs to move in order to generate a key event.
916 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
917
918 // Immutable configuration parameters.
919 struct Parameters {
920 enum Mode {
921 MODE_POINTER,
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800922 MODE_POINTER_RELATIVE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 MODE_NAVIGATION,
924 };
925
926 Mode mode;
927 bool hasAssociatedDisplay;
928 bool orientationAware;
929 } mParameters;
930
931 CursorButtonAccumulator mCursorButtonAccumulator;
932 CursorMotionAccumulator mCursorMotionAccumulator;
933 CursorScrollAccumulator mCursorScrollAccumulator;
934
935 int32_t mSource;
936 float mXScale;
937 float mYScale;
938 float mXPrecision;
939 float mYPrecision;
940
941 float mVWheelScale;
942 float mHWheelScale;
943
944 // Velocity controls for mouse pointer and wheel movements.
945 // The controls for X and Y wheel movements are separate to keep them decoupled.
946 VelocityControl mPointerVelocityControl;
947 VelocityControl mWheelXVelocityControl;
948 VelocityControl mWheelYVelocityControl;
949
950 int32_t mOrientation;
951
952 sp<PointerControllerInterface> mPointerController;
953
954 int32_t mButtonState;
955 nsecs_t mDownTime;
956
957 void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800958 void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959
960 void sync(nsecs_t when);
961};
962
Prashant Malani1941ff52015-08-11 18:29:28 -0700963class RotaryEncoderInputMapper : public InputMapper {
964public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700965 explicit RotaryEncoderInputMapper(InputDevice* device);
Prashant Malani1941ff52015-08-11 18:29:28 -0700966 virtual ~RotaryEncoderInputMapper();
967
968 virtual uint32_t getSources();
969 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800970 virtual void dump(std::string& dump);
Prashant Malani1941ff52015-08-11 18:29:28 -0700971 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
972 virtual void reset(nsecs_t when);
973 virtual void process(const RawEvent* rawEvent);
974
975private:
976 CursorScrollAccumulator mRotaryEncoderScrollAccumulator;
977
978 int32_t mSource;
Prashant Malanidae627a2016-01-11 17:08:18 -0800979 float mScalingFactor;
Ivan Podogovad437252016-09-29 16:29:55 +0100980 int32_t mOrientation;
Prashant Malani1941ff52015-08-11 18:29:28 -0700981
982 void sync(nsecs_t when);
983};
984
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985class TouchInputMapper : public InputMapper {
986public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700987 explicit TouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988 virtual ~TouchInputMapper();
989
990 virtual uint32_t getSources();
991 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800992 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
994 virtual void reset(nsecs_t when);
995 virtual void process(const RawEvent* rawEvent);
996
997 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
998 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
999 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
Prabir Pradhan2b281812019-08-29 14:12:42 -07001000 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
1002 virtual void fadePointer();
Jeff Brownc9aa6282015-02-11 19:03:28 -08001003 virtual void cancelTouch(nsecs_t when);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 virtual void timeoutExpired(nsecs_t when);
Michael Wright842500e2015-03-13 17:32:02 -07001005 virtual void updateExternalStylusState(const StylusState& state);
Arthur Hungc23540e2018-11-29 20:42:11 +08001006 virtual std::optional<int32_t> getAssociatedDisplay();
Prabir Pradhan2b281812019-08-29 14:12:42 -07001007
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008protected:
1009 CursorButtonAccumulator mCursorButtonAccumulator;
1010 CursorScrollAccumulator mCursorScrollAccumulator;
1011 TouchButtonAccumulator mTouchButtonAccumulator;
1012
1013 struct VirtualKey {
1014 int32_t keyCode;
1015 int32_t scanCode;
1016 uint32_t flags;
1017
1018 // computed hit box, specified in touch screen coords based on known display size
1019 int32_t hitLeft;
1020 int32_t hitTop;
1021 int32_t hitRight;
1022 int32_t hitBottom;
1023
1024 inline bool isHit(int32_t x, int32_t y) const {
1025 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1026 }
1027 };
1028
1029 // Input sources and device mode.
1030 uint32_t mSource;
1031
1032 enum DeviceMode {
Prabir Pradhan2b281812019-08-29 14:12:42 -07001033 DEVICE_MODE_DISABLED, // input is disabled
1034 DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1035 DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036 DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
Prabir Pradhan2b281812019-08-29 14:12:42 -07001037 DEVICE_MODE_POINTER, // pointer mapping (pointer)
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038 };
1039 DeviceMode mDeviceMode;
1040
1041 // The reader's configuration.
1042 InputReaderConfiguration mConfig;
1043
1044 // Immutable configuration parameters.
1045 struct Parameters {
1046 enum DeviceType {
1047 DEVICE_TYPE_TOUCH_SCREEN,
1048 DEVICE_TYPE_TOUCH_PAD,
1049 DEVICE_TYPE_TOUCH_NAVIGATION,
1050 DEVICE_TYPE_POINTER,
1051 };
1052
1053 DeviceType deviceType;
1054 bool hasAssociatedDisplay;
1055 bool associatedDisplayIsExternal;
1056 bool orientationAware;
1057 bool hasButtonUnderPad;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001058 std::string uniqueDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001059
1060 enum GestureMode {
Amirhossein Simjour3dd617b2015-10-09 10:39:48 -04001061 GESTURE_MODE_SINGLE_TOUCH,
1062 GESTURE_MODE_MULTI_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 };
1064 GestureMode gestureMode;
Jeff Brownc5e24422014-02-26 18:48:51 -08001065
1066 bool wake;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 } mParameters;
1068
1069 // Immutable calibration parameters in parsed form.
1070 struct Calibration {
1071 // Size
1072 enum SizeCalibration {
1073 SIZE_CALIBRATION_DEFAULT,
1074 SIZE_CALIBRATION_NONE,
1075 SIZE_CALIBRATION_GEOMETRIC,
1076 SIZE_CALIBRATION_DIAMETER,
1077 SIZE_CALIBRATION_BOX,
1078 SIZE_CALIBRATION_AREA,
1079 };
1080
1081 SizeCalibration sizeCalibration;
1082
1083 bool haveSizeScale;
1084 float sizeScale;
1085 bool haveSizeBias;
1086 float sizeBias;
1087 bool haveSizeIsSummed;
1088 bool sizeIsSummed;
1089
1090 // Pressure
1091 enum PressureCalibration {
1092 PRESSURE_CALIBRATION_DEFAULT,
1093 PRESSURE_CALIBRATION_NONE,
1094 PRESSURE_CALIBRATION_PHYSICAL,
1095 PRESSURE_CALIBRATION_AMPLITUDE,
1096 };
1097
1098 PressureCalibration pressureCalibration;
1099 bool havePressureScale;
1100 float pressureScale;
1101
1102 // Orientation
1103 enum OrientationCalibration {
1104 ORIENTATION_CALIBRATION_DEFAULT,
1105 ORIENTATION_CALIBRATION_NONE,
1106 ORIENTATION_CALIBRATION_INTERPOLATED,
1107 ORIENTATION_CALIBRATION_VECTOR,
1108 };
1109
1110 OrientationCalibration orientationCalibration;
1111
1112 // Distance
1113 enum DistanceCalibration {
1114 DISTANCE_CALIBRATION_DEFAULT,
1115 DISTANCE_CALIBRATION_NONE,
1116 DISTANCE_CALIBRATION_SCALED,
1117 };
1118
1119 DistanceCalibration distanceCalibration;
1120 bool haveDistanceScale;
1121 float distanceScale;
1122
1123 enum CoverageCalibration {
1124 COVERAGE_CALIBRATION_DEFAULT,
1125 COVERAGE_CALIBRATION_NONE,
1126 COVERAGE_CALIBRATION_BOX,
1127 };
1128
1129 CoverageCalibration coverageCalibration;
1130
1131 inline void applySizeScaleAndBias(float* outSize) const {
1132 if (haveSizeScale) {
1133 *outSize *= sizeScale;
1134 }
1135 if (haveSizeBias) {
1136 *outSize += sizeBias;
1137 }
1138 if (*outSize < 0) {
1139 *outSize = 0;
1140 }
1141 }
1142 } mCalibration;
1143
Jason Gereckeaf126fb2012-05-10 14:22:47 -07001144 // Affine location transformation/calibration
1145 struct TouchAffineTransformation mAffineTransform;
1146
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 RawPointerAxes mRawPointerAxes;
1148
Michael Wright842500e2015-03-13 17:32:02 -07001149 struct RawState {
1150 nsecs_t when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001151 uint32_t deviceTimestamp;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152
Michael Wright842500e2015-03-13 17:32:02 -07001153 // Raw pointer sample data.
1154 RawPointerData rawPointerData;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155
Michael Wright842500e2015-03-13 17:32:02 -07001156 int32_t buttonState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157
Michael Wright842500e2015-03-13 17:32:02 -07001158 // Scroll state.
1159 int32_t rawVScroll;
1160 int32_t rawHScroll;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161
Michael Wright842500e2015-03-13 17:32:02 -07001162 void copyFrom(const RawState& other) {
1163 when = other.when;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001164 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001165 rawPointerData.copyFrom(other.rawPointerData);
1166 buttonState = other.buttonState;
1167 rawVScroll = other.rawVScroll;
1168 rawHScroll = other.rawHScroll;
1169 }
1170
1171 void clear() {
1172 when = 0;
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001173 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001174 rawPointerData.clear();
1175 buttonState = 0;
1176 rawVScroll = 0;
1177 rawHScroll = 0;
1178 }
1179 };
1180
1181 struct CookedState {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001182 uint32_t deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001183 // Cooked pointer sample data.
1184 CookedPointerData cookedPointerData;
1185
1186 // Id bits used to differentiate fingers, stylus and mouse tools.
1187 BitSet32 fingerIdBits;
1188 BitSet32 stylusIdBits;
1189 BitSet32 mouseIdBits;
1190
Michael Wright7b159c92015-05-14 14:48:03 +01001191 int32_t buttonState;
1192
Michael Wright842500e2015-03-13 17:32:02 -07001193 void copyFrom(const CookedState& other) {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001194 deviceTimestamp = other.deviceTimestamp;
Michael Wright842500e2015-03-13 17:32:02 -07001195 cookedPointerData.copyFrom(other.cookedPointerData);
1196 fingerIdBits = other.fingerIdBits;
1197 stylusIdBits = other.stylusIdBits;
1198 mouseIdBits = other.mouseIdBits;
Michael Wright7b159c92015-05-14 14:48:03 +01001199 buttonState = other.buttonState;
Michael Wright842500e2015-03-13 17:32:02 -07001200 }
1201
1202 void clear() {
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08001203 deviceTimestamp = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001204 cookedPointerData.clear();
1205 fingerIdBits.clear();
1206 stylusIdBits.clear();
1207 mouseIdBits.clear();
Michael Wright7b159c92015-05-14 14:48:03 +01001208 buttonState = 0;
Michael Wright842500e2015-03-13 17:32:02 -07001209 }
1210 };
1211
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001212 std::vector<RawState> mRawStatesPending;
Michael Wright842500e2015-03-13 17:32:02 -07001213 RawState mCurrentRawState;
1214 CookedState mCurrentCookedState;
1215 RawState mLastRawState;
1216 CookedState mLastCookedState;
1217
1218 // State provided by an external stylus
1219 StylusState mExternalStylusState;
1220 int64_t mExternalStylusId;
Michael Wright43fd19f2015-04-21 19:02:58 +01001221 nsecs_t mExternalStylusFusionTimeout;
Michael Wright842500e2015-03-13 17:32:02 -07001222 bool mExternalStylusDataPending;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223
1224 // True if we sent a HOVER_ENTER event.
1225 bool mSentHoverEnter;
1226
Michael Wright842500e2015-03-13 17:32:02 -07001227 // Have we assigned pointer IDs for this stream
1228 bool mHavePointerIds;
1229
Michael Wright8e812822015-06-22 16:18:21 +01001230 // Is the current stream of direct touch events aborted
1231 bool mCurrentMotionAborted;
1232
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 // The time the primary pointer last went down.
1234 nsecs_t mDownTime;
1235
1236 // The pointer controller, or null if the device is not a pointer.
1237 sp<PointerControllerInterface> mPointerController;
1238
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001239 std::vector<VirtualKey> mVirtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240
1241 virtual void configureParameters();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001242 virtual void dumpParameters(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 virtual void configureRawPointerAxes();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001244 virtual void dumpRawPointerAxes(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001246 virtual void dumpSurface(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 virtual void configureVirtualKeys();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001248 virtual void dumpVirtualKeys(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249 virtual void parseCalibration();
1250 virtual void resolveCalibration();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001251 virtual void dumpCalibration(std::string& dump);
Jason Gerecke12d6baa2014-01-27 18:34:20 -08001252 virtual void updateAffineTransformation();
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001253 virtual void dumpAffineTransformation(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001254 virtual void resolveExternalStylusPresence();
1255 virtual bool hasStylus() const = 0;
1256 virtual bool hasExternalStylus() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Michael Wright842500e2015-03-13 17:32:02 -07001258 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
1260private:
1261 // The current viewport.
1262 // The components of the viewport are specified in the display's rotated orientation.
1263 DisplayViewport mViewport;
1264
1265 // The surface orientation, width and height set by configureSurface().
1266 // The width and height are derived from the viewport but are specified
1267 // in the natural orientation.
1268 // The surface origin specifies how the surface coordinates should be translated
1269 // to align with the logical display coordinate space.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 int32_t mSurfaceWidth;
1271 int32_t mSurfaceHeight;
1272 int32_t mSurfaceLeft;
1273 int32_t mSurfaceTop;
Michael Wright358bcc72018-08-21 04:01:07 +01001274
1275 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
1276 // the logical coordinate space.
1277 int32_t mPhysicalWidth;
1278 int32_t mPhysicalHeight;
1279 int32_t mPhysicalLeft;
1280 int32_t mPhysicalTop;
1281
1282 // The orientation may be different from the viewport orientation as it specifies
1283 // the rotation of the surface coordinates required to produce the viewport's
1284 // requested orientation, so it will depend on whether the device is orientation aware.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 int32_t mSurfaceOrientation;
1286
1287 // Translation and scaling factors, orientation-independent.
1288 float mXTranslate;
1289 float mXScale;
1290 float mXPrecision;
1291
1292 float mYTranslate;
1293 float mYScale;
1294 float mYPrecision;
1295
1296 float mGeometricScale;
1297
1298 float mPressureScale;
1299
1300 float mSizeScale;
1301
1302 float mOrientationScale;
1303
1304 float mDistanceScale;
1305
1306 bool mHaveTilt;
1307 float mTiltXCenter;
1308 float mTiltXScale;
1309 float mTiltYCenter;
1310 float mTiltYScale;
1311
Michael Wright842500e2015-03-13 17:32:02 -07001312 bool mExternalStylusConnected;
1313
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314 // Oriented motion ranges for input device info.
1315 struct OrientedRanges {
1316 InputDeviceInfo::MotionRange x;
1317 InputDeviceInfo::MotionRange y;
1318 InputDeviceInfo::MotionRange pressure;
1319
1320 bool haveSize;
1321 InputDeviceInfo::MotionRange size;
1322
1323 bool haveTouchSize;
1324 InputDeviceInfo::MotionRange touchMajor;
1325 InputDeviceInfo::MotionRange touchMinor;
1326
1327 bool haveToolSize;
1328 InputDeviceInfo::MotionRange toolMajor;
1329 InputDeviceInfo::MotionRange toolMinor;
1330
1331 bool haveOrientation;
1332 InputDeviceInfo::MotionRange orientation;
1333
1334 bool haveDistance;
1335 InputDeviceInfo::MotionRange distance;
1336
1337 bool haveTilt;
1338 InputDeviceInfo::MotionRange tilt;
1339
Prabir Pradhan2b281812019-08-29 14:12:42 -07001340 OrientedRanges() { clear(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001341
1342 void clear() {
1343 haveSize = false;
1344 haveTouchSize = false;
1345 haveToolSize = false;
1346 haveOrientation = false;
1347 haveDistance = false;
1348 haveTilt = false;
1349 }
1350 } mOrientedRanges;
1351
1352 // Oriented dimensions and precision.
1353 float mOrientedXPrecision;
1354 float mOrientedYPrecision;
1355
1356 struct CurrentVirtualKeyState {
1357 bool down;
1358 bool ignored;
1359 nsecs_t downTime;
1360 int32_t keyCode;
1361 int32_t scanCode;
1362 } mCurrentVirtualKey;
1363
1364 // Scale factor for gesture or mouse based pointer movements.
1365 float mPointerXMovementScale;
1366 float mPointerYMovementScale;
1367
1368 // Scale factor for gesture based zooming and other freeform motions.
1369 float mPointerXZoomScale;
1370 float mPointerYZoomScale;
1371
1372 // The maximum swipe width.
1373 float mPointerGestureMaxSwipeWidth;
1374
1375 struct PointerDistanceHeapElement {
1376 uint32_t currentPointerIndex : 8;
1377 uint32_t lastPointerIndex : 8;
1378 uint64_t distance : 48; // squared distance
1379 };
1380
1381 enum PointerUsage {
1382 POINTER_USAGE_NONE,
1383 POINTER_USAGE_GESTURES,
1384 POINTER_USAGE_STYLUS,
1385 POINTER_USAGE_MOUSE,
1386 };
1387 PointerUsage mPointerUsage;
1388
1389 struct PointerGesture {
1390 enum Mode {
1391 // No fingers, button is not pressed.
1392 // Nothing happening.
1393 NEUTRAL,
1394
1395 // No fingers, button is not pressed.
1396 // Tap detected.
1397 // Emits DOWN and UP events at the pointer location.
1398 TAP,
1399
1400 // Exactly one finger dragging following a tap.
1401 // Pointer follows the active finger.
1402 // Emits DOWN, MOVE and UP events at the pointer location.
1403 //
1404 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1405 TAP_DRAG,
1406
1407 // Button is pressed.
1408 // Pointer follows the active finger if there is one. Other fingers are ignored.
1409 // Emits DOWN, MOVE and UP events at the pointer location.
1410 BUTTON_CLICK_OR_DRAG,
1411
1412 // Exactly one finger, button is not pressed.
1413 // Pointer follows the active finger.
1414 // Emits HOVER_MOVE events at the pointer location.
1415 //
1416 // Detect taps when the finger goes up while in HOVER mode.
1417 HOVER,
1418
1419 // Exactly two fingers but neither have moved enough to clearly indicate
1420 // whether a swipe or freeform gesture was intended. We consider the
1421 // pointer to be pressed so this enables clicking or long-pressing on buttons.
1422 // Pointer does not move.
1423 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1424 PRESS,
1425
1426 // Exactly two fingers moving in the same direction, button is not pressed.
1427 // Pointer does not move.
1428 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1429 // follows the midpoint between both fingers.
1430 SWIPE,
1431
1432 // Two or more fingers moving in arbitrary directions, button is not pressed.
1433 // Pointer does not move.
1434 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1435 // each finger individually relative to the initial centroid of the finger.
1436 FREEFORM,
1437
1438 // Waiting for quiet time to end before starting the next gesture.
1439 QUIET,
1440 };
1441
1442 // Time the first finger went down.
1443 nsecs_t firstTouchTime;
1444
1445 // The active pointer id from the raw touch data.
1446 int32_t activeTouchId; // -1 if none
1447
1448 // The active pointer id from the gesture last delivered to the application.
1449 int32_t activeGestureId; // -1 if none
1450
1451 // Pointer coords and ids for the current and previous pointer gesture.
1452 Mode currentGestureMode;
1453 BitSet32 currentGestureIdBits;
1454 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1455 PointerProperties currentGestureProperties[MAX_POINTERS];
1456 PointerCoords currentGestureCoords[MAX_POINTERS];
1457
1458 Mode lastGestureMode;
1459 BitSet32 lastGestureIdBits;
1460 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1461 PointerProperties lastGestureProperties[MAX_POINTERS];
1462 PointerCoords lastGestureCoords[MAX_POINTERS];
1463
1464 // Time the pointer gesture last went down.
1465 nsecs_t downTime;
1466
1467 // Time when the pointer went down for a TAP.
1468 nsecs_t tapDownTime;
1469
1470 // Time when the pointer went up for a TAP.
1471 nsecs_t tapUpTime;
1472
1473 // Location of initial tap.
1474 float tapX, tapY;
1475
1476 // Time we started waiting for quiescence.
1477 nsecs_t quietTime;
1478
1479 // Reference points for multitouch gestures.
Prabir Pradhan2b281812019-08-29 14:12:42 -07001480 float referenceTouchX; // reference touch X/Y coordinates in surface units
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481 float referenceTouchY;
Prabir Pradhan2b281812019-08-29 14:12:42 -07001482 float referenceGestureX; // reference gesture X/Y coordinates in pixels
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483 float referenceGestureY;
1484
1485 // Distance that each pointer has traveled which has not yet been
1486 // subsumed into the reference gesture position.
1487 BitSet32 referenceIdBits;
1488 struct Delta {
1489 float dx, dy;
1490 };
1491 Delta referenceDeltas[MAX_POINTER_ID + 1];
1492
1493 // Describes how touch ids are mapped to gesture ids for freeform gestures.
1494 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1495
1496 // A velocity tracker for determining whether to switch active pointers during drags.
1497 VelocityTracker velocityTracker;
1498
1499 void reset() {
1500 firstTouchTime = LLONG_MIN;
1501 activeTouchId = -1;
1502 activeGestureId = -1;
1503 currentGestureMode = NEUTRAL;
1504 currentGestureIdBits.clear();
1505 lastGestureMode = NEUTRAL;
1506 lastGestureIdBits.clear();
1507 downTime = 0;
1508 velocityTracker.clear();
1509 resetTap();
1510 resetQuietTime();
1511 }
1512
1513 void resetTap() {
1514 tapDownTime = LLONG_MIN;
1515 tapUpTime = LLONG_MIN;
1516 }
1517
Prabir Pradhan2b281812019-08-29 14:12:42 -07001518 void resetQuietTime() { quietTime = LLONG_MIN; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 } mPointerGesture;
1520
1521 struct PointerSimple {
1522 PointerCoords currentCoords;
1523 PointerProperties currentProperties;
1524 PointerCoords lastCoords;
1525 PointerProperties lastProperties;
1526
1527 // True if the pointer is down.
1528 bool down;
1529
1530 // True if the pointer is hovering.
1531 bool hovering;
1532
1533 // Time the pointer last went down.
1534 nsecs_t downTime;
1535
1536 void reset() {
1537 currentCoords.clear();
1538 currentProperties.clear();
1539 lastCoords.clear();
1540 lastProperties.clear();
1541 down = false;
1542 hovering = false;
1543 downTime = 0;
1544 }
1545 } mPointerSimple;
1546
1547 // The pointer and scroll velocity controls.
1548 VelocityControl mPointerVelocityControl;
1549 VelocityControl mWheelXVelocityControl;
1550 VelocityControl mWheelYVelocityControl;
1551
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001552 // Latency statistics for touch events
1553 struct LatencyStatistics mStatistics;
1554
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001555 std::optional<DisplayViewport> findViewport();
1556
Michael Wright842500e2015-03-13 17:32:02 -07001557 void resetExternalStylus();
Michael Wright43fd19f2015-04-21 19:02:58 +01001558 void clearStylusDataPendingFlags();
Michael Wright842500e2015-03-13 17:32:02 -07001559
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560 void sync(nsecs_t when);
1561
1562 bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
Michael Wright842500e2015-03-13 17:32:02 -07001563 void processRawTouches(bool timeout);
1564 void cookAndDispatch(nsecs_t when);
Prabir Pradhan2b281812019-08-29 14:12:42 -07001565 void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, int32_t keyEventAction,
1566 int32_t keyEventFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
1568 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1569 void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1570 void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
Michael Wright7b159c92015-05-14 14:48:03 +01001571 void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
1572 void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
1573 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 void cookPointerData();
Michael Wright8e812822015-06-22 16:18:21 +01001575 void abortTouches(nsecs_t when, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576
1577 void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1578 void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1579
1580 void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1581 void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
Prabir Pradhan2b281812019-08-29 14:12:42 -07001582 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
1583 bool* outFinishPreviousGesture, bool isTimeout);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1586 void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1587
1588 void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1589 void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1590
Prabir Pradhan2b281812019-08-29 14:12:42 -07001591 void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, bool down, bool hovering);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1593
Michael Wright842500e2015-03-13 17:32:02 -07001594 bool assignExternalStylusId(const RawState& state, bool timeout);
1595 void applyExternalStylusButtonState(nsecs_t when);
1596 void applyExternalStylusTouchState(nsecs_t when);
1597
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 // Dispatches a motion event.
1599 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1600 // method will take care of setting the index and transmuting the action to DOWN or UP
1601 // it is the first / last pointer to go down / up.
Prabir Pradhan2b281812019-08-29 14:12:42 -07001602 void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, int32_t action,
1603 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
1604 int32_t edgeFlags, uint32_t deviceTimestamp,
1605 const PointerProperties* properties, const PointerCoords* coords,
1606 const uint32_t* idToIndex, BitSet32 idBits, int32_t changedId,
1607 float xPrecision, float yPrecision, nsecs_t downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608
1609 // Updates pointer coords and properties for pointers with specified ids that have moved.
1610 // Returns true if any of them changed.
Prabir Pradhan2b281812019-08-29 14:12:42 -07001611 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
1612 const uint32_t* inIdToIndex, PointerProperties* outProperties,
1613 PointerCoords* outCoords, const uint32_t* outIdToIndex,
1614 BitSet32 idBits) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615
1616 bool isPointInsideSurface(int32_t x, int32_t y);
1617 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1618
Michael Wright842500e2015-03-13 17:32:02 -07001619 static void assignPointerIds(const RawState* last, RawState* current);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001620
Siarhei Vishniakou9ffab0c2018-11-08 19:54:22 -08001621 void reportEventForStatistics(nsecs_t evdevTime);
1622
Santos Cordonfa5cf462017-04-05 10:37:00 -07001623 const char* modeToString(DeviceMode deviceMode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624};
1625
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626class SingleTouchInputMapper : public TouchInputMapper {
1627public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001628 explicit SingleTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 virtual ~SingleTouchInputMapper();
1630
1631 virtual void reset(nsecs_t when);
1632 virtual void process(const RawEvent* rawEvent);
1633
1634protected:
Michael Wright842500e2015-03-13 17:32:02 -07001635 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636 virtual void configureRawPointerAxes();
1637 virtual bool hasStylus() const;
1638
1639private:
1640 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1641};
1642
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643class MultiTouchInputMapper : public TouchInputMapper {
1644public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001645 explicit MultiTouchInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 virtual ~MultiTouchInputMapper();
1647
1648 virtual void reset(nsecs_t when);
1649 virtual void process(const RawEvent* rawEvent);
1650
1651protected:
Michael Wright842500e2015-03-13 17:32:02 -07001652 virtual void syncTouch(nsecs_t when, RawState* outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653 virtual void configureRawPointerAxes();
1654 virtual bool hasStylus() const;
1655
1656private:
1657 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1658
1659 // Specifies the pointer id bits that are in use, and their associated tracking id.
1660 BitSet32 mPointerIdBits;
1661 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1662};
1663
Michael Wright842500e2015-03-13 17:32:02 -07001664class ExternalStylusInputMapper : public InputMapper {
1665public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001666 explicit ExternalStylusInputMapper(InputDevice* device);
Michael Wright842500e2015-03-13 17:32:02 -07001667 virtual ~ExternalStylusInputMapper() = default;
1668
1669 virtual uint32_t getSources();
1670 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001671 virtual void dump(std::string& dump);
Michael Wright842500e2015-03-13 17:32:02 -07001672 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1673 virtual void reset(nsecs_t when);
1674 virtual void process(const RawEvent* rawEvent);
1675 virtual void sync(nsecs_t when);
1676
1677private:
1678 SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1679 RawAbsoluteAxisInfo mRawPressureAxis;
1680 TouchButtonAccumulator mTouchButtonAccumulator;
1681
1682 StylusState mStylusState;
1683};
1684
Michael Wrightd02c5b62014-02-10 15:10:22 -08001685class JoystickInputMapper : public InputMapper {
1686public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -07001687 explicit JoystickInputMapper(InputDevice* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001688 virtual ~JoystickInputMapper();
1689
1690 virtual uint32_t getSources();
1691 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001692 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001693 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1694 virtual void reset(nsecs_t when);
1695 virtual void process(const RawEvent* rawEvent);
1696
1697private:
1698 struct Axis {
1699 RawAbsoluteAxisInfo rawAxisInfo;
1700 AxisInfo axisInfo;
1701
1702 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1703
Prabir Pradhan2b281812019-08-29 14:12:42 -07001704 float scale; // scale factor from raw to normalized values
1705 float offset; // offset to add after scaling for normalization
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706 float highScale; // scale factor from raw to normalized values of high split
1707 float highOffset; // offset to add after scaling for normalization of high split
1708
1709 float min; // normalized inclusive minimum
1710 float max; // normalized inclusive maximum
1711 float flat; // normalized flat region size
1712 float fuzz; // normalized error tolerance
1713 float resolution; // normalized resolution in units/mm
1714
Prabir Pradhan2b281812019-08-29 14:12:42 -07001715 float filter; // filter out small variations of this size
1716 float currentValue; // current value
1717 float newValue; // most recent value
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718 float highCurrentValue; // current value of high split
Prabir Pradhan2b281812019-08-29 14:12:42 -07001719 float highNewValue; // most recent value of high split
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720
1721 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
Prabir Pradhan2b281812019-08-29 14:12:42 -07001722 bool explicitlyMapped, float scale, float offset, float highScale,
1723 float highOffset, float min, float max, float flat, float fuzz,
1724 float resolution) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725 this->rawAxisInfo = rawAxisInfo;
1726 this->axisInfo = axisInfo;
1727 this->explicitlyMapped = explicitlyMapped;
1728 this->scale = scale;
1729 this->offset = offset;
1730 this->highScale = highScale;
1731 this->highOffset = highOffset;
1732 this->min = min;
1733 this->max = max;
1734 this->flat = flat;
1735 this->fuzz = fuzz;
1736 this->resolution = resolution;
1737 this->filter = 0;
1738 resetValue();
1739 }
1740
1741 void resetValue() {
1742 this->currentValue = 0;
1743 this->newValue = 0;
1744 this->highCurrentValue = 0;
1745 this->highNewValue = 0;
1746 }
1747 };
1748
1749 // Axes indexed by raw ABS_* axis index.
1750 KeyedVector<int32_t, Axis> mAxes;
1751
1752 void sync(nsecs_t when, bool force);
1753
1754 bool haveAxis(int32_t axisId);
1755 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1756 bool filterAxes(bool force);
1757
Prabir Pradhan2b281812019-08-29 14:12:42 -07001758 static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue,
1759 float min, float max);
1760 static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
1761 float currentValue, float thresholdValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001762
1763 static bool isCenteredAxis(int32_t axis);
1764 static int32_t getCompatAxis(int32_t axis);
1765
1766 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
Prabir Pradhan2b281812019-08-29 14:12:42 -07001767 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768};
1769
1770} // namespace android
1771
1772#endif // _UI_INPUT_READER_H