blob: 88d331eeb9960becce21397ade03b807a52e1bb4 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
Harry Cutts3f570c72024-04-05 16:44:28 +000019#include <array>
20#include <climits>
21#include <limits>
22#include <list>
23#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000024#include <optional>
25#include <string>
Harry Cutts3f570c72024-04-05 16:44:28 +000026#include <utility>
27#include <vector>
Harry Cuttsf13161a2023-03-08 14:15:49 +000028
Michael Wright227c5542020-07-02 18:30:52 +010029#include <stdint.h>
Harry Cutts3f570c72024-04-05 16:44:28 +000030#include <gui/constants.h>
31#include <input/DisplayViewport.h>
32#include <input/Input.h>
33#include <input/InputDevice.h>
34#include <input/VelocityControl.h>
35#include <input/VelocityTracker.h>
36#include <ui/Rect.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000037#include <ui/Rotation.h>
Harry Cutts3f570c72024-04-05 16:44:28 +000038#include <ui/Size.h>
39#include <ui/Transform.h>
40#include <utils/BitSet.h>
41#include <utils/Timers.h>
Michael Wright227c5542020-07-02 18:30:52 +010042
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070043#include "CursorButtonAccumulator.h"
44#include "CursorScrollAccumulator.h"
45#include "EventHub.h"
46#include "InputMapper.h"
47#include "InputReaderBase.h"
Harry Cutts3f570c72024-04-05 16:44:28 +000048#include "NotifyArgs.h"
49#include "PointerControllerInterface.h"
50#include "StylusState.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051#include "TouchButtonAccumulator.h"
52
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053namespace android {
54
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +000055// Maximum amount of latency to add to touch events while waiting for data from an
56// external stylus.
57static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
58
59// Maximum amount of time to wait on touch data before pushing out new pressure data.
60static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
61
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062/* Raw axis information from the driver. */
63struct RawPointerAxes {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000064 RawAbsoluteAxisInfo x{};
65 RawAbsoluteAxisInfo y{};
66 RawAbsoluteAxisInfo pressure{};
67 RawAbsoluteAxisInfo touchMajor{};
68 RawAbsoluteAxisInfo touchMinor{};
69 RawAbsoluteAxisInfo toolMajor{};
70 RawAbsoluteAxisInfo toolMinor{};
71 RawAbsoluteAxisInfo orientation{};
72 RawAbsoluteAxisInfo distance{};
73 RawAbsoluteAxisInfo tiltX{};
74 RawAbsoluteAxisInfo tiltY{};
75 RawAbsoluteAxisInfo trackingId{};
76 RawAbsoluteAxisInfo slot{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070077
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
79 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000080 inline void clear() { *this = RawPointerAxes(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081};
82
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000083using PropertiesArray = std::array<PointerProperties, MAX_POINTERS>;
84using CoordsArray = std::array<PointerCoords, MAX_POINTERS>;
85using IdToIndexArray = std::array<uint32_t, MAX_POINTER_ID + 1>;
86
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087/* Raw data for a collection of pointers including a pointer id mapping table. */
88struct RawPointerData {
89 struct Pointer {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000090 uint32_t id{0xFFFFFFFF};
91 int32_t x{};
92 int32_t y{};
93 int32_t pressure{};
94 int32_t touchMajor{};
95 int32_t touchMinor{};
96 int32_t toolMajor{};
97 int32_t toolMinor{};
98 int32_t orientation{};
99 int32_t distance{};
100 int32_t tiltX{};
101 int32_t tiltY{};
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700102 // A fully decoded ToolType constant.
103 ToolType toolType{ToolType::UNKNOWN};
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000104 bool isHovering{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105 };
106
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000107 uint32_t pointerCount{};
108 std::array<Pointer, MAX_POINTERS> pointers{};
109 BitSet32 hoveringIdBits{}, touchingIdBits{}, canceledIdBits{};
110 IdToIndexArray idToIndex{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700111
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000112 inline void clear() { *this = RawPointerData(); }
113
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
115
116 inline void markIdBit(uint32_t id, bool isHovering) {
117 if (isHovering) {
118 hoveringIdBits.markBit(id);
119 } else {
120 touchingIdBits.markBit(id);
121 }
122 }
123
124 inline void clearIdBits() {
125 hoveringIdBits.clear();
126 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800127 canceledIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 }
129
130 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
131
132 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
133};
134
135/* Cooked data for a collection of pointers including a pointer id mapping table. */
136struct CookedPointerData {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000137 uint32_t pointerCount{};
138 PropertiesArray pointerProperties{};
139 CoordsArray pointerCoords{};
140 BitSet32 hoveringIdBits{}, touchingIdBits{}, canceledIdBits{}, validIdBits{};
141 IdToIndexArray idToIndex{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000143 inline void clear() { *this = CookedPointerData(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700144
145 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
146 return pointerCoords[idToIndex[id]];
147 }
148
149 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
150 return pointerCoords[idToIndex[id]];
151 }
152
153 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
154 return pointerProperties[idToIndex[id]];
155 }
156
157 inline bool isHovering(uint32_t pointerIndex) const {
158 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
159 }
160
161 inline bool isTouching(uint32_t pointerIndex) const {
162 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
163 }
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000164
165 inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700166};
167
168class TouchInputMapper : public InputMapper {
169public:
Michael Wright227c5542020-07-02 18:30:52 +0100170 ~TouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700171
Philip Junker4af3b3d2021-12-14 10:36:55 +0100172 uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000173 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Michael Wright227c5542020-07-02 18:30:52 +0100174 void dump(std::string& dump) override;
Arpit Singh4be4eef2023-03-28 14:26:01 +0000175 [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000176 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000177 ConfigurationChanges changes) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700178 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
179 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700180
Michael Wright227c5542020-07-02 18:30:52 +0100181 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
182 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700183 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
Michael Wright227c5542020-07-02 18:30:52 +0100184 uint8_t* outFlags) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700185
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700186 [[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) override;
187 [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when) override;
188 [[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(
189 const StylusState& state) override;
Michael Wright227c5542020-07-02 18:30:52 +0100190 std::optional<int32_t> getAssociatedDisplayId() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191
192protected:
193 CursorButtonAccumulator mCursorButtonAccumulator;
194 CursorScrollAccumulator mCursorScrollAccumulator;
195 TouchButtonAccumulator mTouchButtonAccumulator;
196
197 struct VirtualKey {
198 int32_t keyCode;
199 int32_t scanCode;
200 uint32_t flags;
201
202 // computed hit box, specified in touch screen coords based on known display size
203 int32_t hitLeft;
204 int32_t hitTop;
205 int32_t hitRight;
206 int32_t hitBottom;
207
208 inline bool isHit(int32_t x, int32_t y) const {
209 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
210 }
211 };
212
213 // Input sources and device mode.
Arpit Singh403e53c2023-04-18 11:46:56 +0000214 uint32_t mSource{0};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700215
Michael Wright227c5542020-07-02 18:30:52 +0100216 enum class DeviceMode {
217 DISABLED, // input is disabled
218 DIRECT, // direct mapping (touchscreen)
Harry Cuttse3aaf392023-06-19 17:13:43 +0000219 UNSCALED, // unscaled mapping (e.g. captured touchpad)
Michael Wright227c5542020-07-02 18:30:52 +0100220 NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
Harry Cuttse3aaf392023-06-19 17:13:43 +0000221 POINTER, // pointer mapping (e.g. uncaptured touchpad, drawing tablet)
Dominik Laskowski75788452021-02-09 18:51:25 -0800222
223 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 };
Arpit Singh403e53c2023-04-18 11:46:56 +0000225 DeviceMode mDeviceMode{DeviceMode::DISABLED};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700226
227 // The reader's configuration.
228 InputReaderConfiguration mConfig;
229
230 // Immutable configuration parameters.
231 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100232 enum class DeviceType {
233 TOUCH_SCREEN,
Michael Wright227c5542020-07-02 18:30:52 +0100234 TOUCH_NAVIGATION,
235 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800236
237 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700238 };
239
240 DeviceType deviceType;
241 bool hasAssociatedDisplay;
242 bool associatedDisplayIsExternal;
243 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700244
Michael Wrighta9cf4192022-12-01 23:46:39 +0000245 ui::Rotation orientation;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700246
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700247 bool hasButtonUnderPad;
248 std::string uniqueDisplayId;
249
Michael Wright227c5542020-07-02 18:30:52 +0100250 enum class GestureMode {
251 SINGLE_TOUCH,
252 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800253
254 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 };
256 GestureMode gestureMode;
257
258 bool wake;
Prabir Pradhan167c2702022-09-14 00:37:24 +0000259
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000260 // The Universal Stylus Initiative (USI) protocol version supported by this device.
261 std::optional<InputDeviceUsiVersion> usiVersion;
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700262
263 // Allows touches while the display is off.
264 bool enableForInactiveViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700265 } mParameters;
266
267 // Immutable calibration parameters in parsed form.
268 struct Calibration {
269 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100270 enum class SizeCalibration {
271 DEFAULT,
272 NONE,
273 GEOMETRIC,
274 DIAMETER,
275 BOX,
276 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800277 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700278 };
279
280 SizeCalibration sizeCalibration;
281
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700282 std::optional<float> sizeScale;
283 std::optional<float> sizeBias;
284 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285
286 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100287 enum class PressureCalibration {
288 DEFAULT,
289 NONE,
290 PHYSICAL,
291 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 };
293
294 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700295 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700296
297 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100298 enum class OrientationCalibration {
299 DEFAULT,
300 NONE,
301 INTERPOLATED,
302 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700303 };
304
305 OrientationCalibration orientationCalibration;
306
307 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100308 enum class DistanceCalibration {
309 DEFAULT,
310 NONE,
311 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700312 };
313
314 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700315 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700317 inline void applySizeScaleAndBias(float& outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700318 if (sizeScale) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700319 outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700320 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700321 if (sizeBias) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700322 outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323 }
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700324 if (outSize < 0) {
325 outSize = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326 }
327 }
328 } mCalibration;
329
330 // Affine location transformation/calibration
331 struct TouchAffineTransformation mAffineTransform;
332
333 RawPointerAxes mRawPointerAxes;
334
335 struct RawState {
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000336 nsecs_t when{std::numeric_limits<nsecs_t>::min()};
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000337 nsecs_t readTime{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700338
339 // Raw pointer sample data.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000340 RawPointerData rawPointerData{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000342 int32_t buttonState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343
344 // Scroll state.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000345 int32_t rawVScroll{};
346 int32_t rawHScroll{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000348 inline void clear() { *this = RawState(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349 };
350
351 struct CookedState {
352 // Cooked pointer sample data.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000353 CookedPointerData cookedPointerData{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700354
355 // Id bits used to differentiate fingers, stylus and mouse tools.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000356 BitSet32 fingerIdBits{};
357 BitSet32 stylusIdBits{};
358 BitSet32 mouseIdBits{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700359
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000360 int32_t buttonState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700361
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000362 inline void clear() { *this = CookedState(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700363 };
364
365 std::vector<RawState> mRawStatesPending;
366 RawState mCurrentRawState;
367 CookedState mCurrentCookedState;
368 RawState mLastRawState;
369 CookedState mLastCookedState;
370
371 // State provided by an external stylus
372 StylusState mExternalStylusState;
Prabir Pradhan8d9ba912022-11-11 22:26:33 +0000373 // If an external stylus is capable of reporting pointer-specific data like pressure, we will
374 // attempt to fuse the pointer data reported by the stylus to the first touch pointer. This is
375 // the id of the pointer to which the external stylus data is fused.
376 std::optional<uint32_t> mFusedStylusPointerId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700377 nsecs_t mExternalStylusFusionTimeout;
378 bool mExternalStylusDataPending;
Prabir Pradhan124ea442022-10-28 20:27:44 +0000379 // A subset of the buttons in mCurrentRawState that came from an external stylus.
Arpit Singha8c236b2023-04-25 13:56:05 +0000380 int32_t mExternalStylusButtonsApplied{0};
Prabir Pradhanb08a0e82023-09-14 22:28:32 +0000381 // True if the current cooked pointer data was modified due to the state of an external stylus.
382 bool mCurrentStreamModifiedByExternalStylus{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700383
384 // True if we sent a HOVER_ENTER event.
Arpit Singha8c236b2023-04-25 13:56:05 +0000385 bool mSentHoverEnter{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700386
387 // Have we assigned pointer IDs for this stream
Arpit Singha8c236b2023-04-25 13:56:05 +0000388 bool mHavePointerIds{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389
390 // Is the current stream of direct touch events aborted
Arpit Singha8c236b2023-04-25 13:56:05 +0000391 bool mCurrentMotionAborted{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392
393 // The time the primary pointer last went down.
Arpit Singha8c236b2023-04-25 13:56:05 +0000394 nsecs_t mDownTime{0};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395
396 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100397 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398
399 std::vector<VirtualKey> mVirtualKeys;
400
Arpit Singha8c236b2023-04-25 13:56:05 +0000401 explicit TouchInputMapper(InputDeviceContext& deviceContext,
402 const InputReaderConfiguration& readerConfig);
403
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700404 virtual void dumpParameters(std::string& dump);
405 virtual void configureRawPointerAxes();
406 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700407 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
408 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 virtual void configureVirtualKeys();
410 virtual void dumpVirtualKeys(std::string& dump);
411 virtual void parseCalibration();
412 virtual void resolveCalibration();
413 virtual void dumpCalibration(std::string& dump);
414 virtual void updateAffineTransformation();
415 virtual void dumpAffineTransformation(std::string& dump);
416 virtual void resolveExternalStylusPresence();
417 virtual bool hasStylus() const = 0;
418 virtual bool hasExternalStylus() const;
419
420 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
421
422private:
423 // The current viewport.
424 // The components of the viewport are specified in the display's rotated orientation.
425 DisplayViewport mViewport;
426
Prabir Pradhan2d613f42022-11-10 20:22:06 +0000427 // We refer to the display as being in the "natural orientation" when there is no rotation
428 // applied. The display size obtained from the viewport in the natural orientation.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000429 // Always starts at (0, 0).
430 ui::Size mDisplayBounds{ui::kInvalidSize};
Arthur Hung4197f6b2020-03-16 15:39:59 +0800431
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000432 // The physical frame is the rectangle in the rotated display's coordinate space that maps to
Prabir Pradhan1728b212021-10-19 16:00:03 -0700433 // the logical display frame.
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000434 Rect mPhysicalFrameInRotatedDisplay{Rect::INVALID_RECT};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700435
Prabir Pradhan1728b212021-10-19 16:00:03 -0700436 // The orientation of the input device relative to that of the display panel. It specifies
437 // the rotation of the input device coordinates required to produce the display panel
438 // orientation, so it will depend on whether the device is orientation aware.
Arpit Singh403e53c2023-04-18 11:46:56 +0000439 ui::Rotation mInputDeviceOrientation{ui::ROTATION_0};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700440
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000441 // The transform that maps the input device's raw coordinate space to the un-rotated display's
442 // coordinate space. InputReader generates events in the un-rotated display's coordinate space.
443 ui::Transform mRawToDisplay;
444
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000445 // The transform that maps the input device's raw coordinate space to the rotated display's
446 // coordinate space. This used to perform hit-testing of raw events with the physical frame in
447 // the rotated coordinate space. See mPhysicalFrameInRotatedDisplay.
448 ui::Transform mRawToRotatedDisplay;
449
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000450 // The transform used for non-planar raw axes, such as orientation and tilt.
451 ui::Transform mRawRotation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700452
453 float mGeometricScale;
454
455 float mPressureScale;
456
457 float mSizeScale;
458
459 float mOrientationScale;
460
461 float mDistanceScale;
462
463 bool mHaveTilt;
464 float mTiltXCenter;
465 float mTiltXScale;
466 float mTiltYCenter;
467 float mTiltYScale;
468
469 bool mExternalStylusConnected;
470
471 // Oriented motion ranges for input device info.
472 struct OrientedRanges {
473 InputDeviceInfo::MotionRange x;
474 InputDeviceInfo::MotionRange y;
475 InputDeviceInfo::MotionRange pressure;
476
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700477 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700478
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700479 std::optional<InputDeviceInfo::MotionRange> touchMajor;
480 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700481
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700482 std::optional<InputDeviceInfo::MotionRange> toolMajor;
483 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700484
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700485 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700486
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700487 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700488
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700489 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700490
491 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700492 size = std::nullopt;
493 touchMajor = std::nullopt;
494 touchMinor = std::nullopt;
495 toolMajor = std::nullopt;
496 toolMinor = std::nullopt;
497 orientation = std::nullopt;
498 distance = std::nullopt;
499 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700500 }
501 } mOrientedRanges;
502
503 // Oriented dimensions and precision.
504 float mOrientedXPrecision;
505 float mOrientedYPrecision;
506
507 struct CurrentVirtualKeyState {
508 bool down;
509 bool ignored;
510 nsecs_t downTime;
511 int32_t keyCode;
512 int32_t scanCode;
513 } mCurrentVirtualKey;
514
515 // Scale factor for gesture or mouse based pointer movements.
516 float mPointerXMovementScale;
517 float mPointerYMovementScale;
518
519 // Scale factor for gesture based zooming and other freeform motions.
520 float mPointerXZoomScale;
521 float mPointerYZoomScale;
522
HQ Liue6983c72022-04-19 22:14:56 +0000523 // The maximum swipe width between pointers to detect a swipe gesture
524 // in the number of pixels.Touches that are wider than this are translated
525 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700526 float mPointerGestureMaxSwipeWidth;
527
528 struct PointerDistanceHeapElement {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000529 uint32_t currentPointerIndex : 8 {};
530 uint32_t lastPointerIndex : 8 {};
531 uint64_t distance : 48 {}; // squared distance
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700532 };
533
Michael Wright227c5542020-07-02 18:30:52 +0100534 enum class PointerUsage {
535 NONE,
536 GESTURES,
537 STYLUS,
538 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700539 };
Arpit Singha8c236b2023-04-25 13:56:05 +0000540 PointerUsage mPointerUsage{PointerUsage::NONE};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700541
542 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100543 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700544 // No fingers, button is not pressed.
545 // Nothing happening.
546 NEUTRAL,
547
548 // No fingers, button is not pressed.
549 // Tap detected.
550 // Emits DOWN and UP events at the pointer location.
551 TAP,
552
553 // Exactly one finger dragging following a tap.
554 // Pointer follows the active finger.
555 // Emits DOWN, MOVE and UP events at the pointer location.
556 //
557 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
558 TAP_DRAG,
559
560 // Button is pressed.
561 // Pointer follows the active finger if there is one. Other fingers are ignored.
562 // Emits DOWN, MOVE and UP events at the pointer location.
563 BUTTON_CLICK_OR_DRAG,
564
565 // Exactly one finger, button is not pressed.
566 // Pointer follows the active finger.
567 // Emits HOVER_MOVE events at the pointer location.
568 //
569 // Detect taps when the finger goes up while in HOVER mode.
570 HOVER,
571
572 // Exactly two fingers but neither have moved enough to clearly indicate
573 // whether a swipe or freeform gesture was intended. We consider the
574 // pointer to be pressed so this enables clicking or long-pressing on buttons.
575 // Pointer does not move.
576 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
577 PRESS,
578
579 // Exactly two fingers moving in the same direction, button is not pressed.
580 // Pointer does not move.
581 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
582 // follows the midpoint between both fingers.
583 SWIPE,
584
585 // Two or more fingers moving in arbitrary directions, button is not pressed.
586 // Pointer does not move.
587 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
588 // each finger individually relative to the initial centroid of the finger.
589 FREEFORM,
590
591 // Waiting for quiet time to end before starting the next gesture.
592 QUIET,
593 };
594
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800595 // When a gesture is sent to an unfocused window, return true if it can bring that window
596 // into focus, false otherwise.
597 static bool canGestureAffectWindowFocus(Mode mode) {
598 switch (mode) {
599 case Mode::TAP:
600 case Mode::TAP_DRAG:
601 case Mode::BUTTON_CLICK_OR_DRAG:
602 // Taps can affect window focus.
603 return true;
604 case Mode::FREEFORM:
605 case Mode::HOVER:
606 case Mode::NEUTRAL:
607 case Mode::PRESS:
608 case Mode::QUIET:
609 case Mode::SWIPE:
610 // Most gestures can be performed on an unfocused window, so they should not
611 // not affect window focus.
612 return false;
613 }
614 }
615
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700616 // Time the first finger went down.
617 nsecs_t firstTouchTime;
618
619 // The active pointer id from the raw touch data.
620 int32_t activeTouchId; // -1 if none
621
622 // The active pointer id from the gesture last delivered to the application.
623 int32_t activeGestureId; // -1 if none
624
625 // Pointer coords and ids for the current and previous pointer gesture.
626 Mode currentGestureMode;
627 BitSet32 currentGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000628 IdToIndexArray currentGestureIdToIndex{};
629 PropertiesArray currentGestureProperties{};
630 CoordsArray currentGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700631
632 Mode lastGestureMode;
633 BitSet32 lastGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000634 IdToIndexArray lastGestureIdToIndex{};
635 PropertiesArray lastGestureProperties{};
636 CoordsArray lastGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700637
638 // Time the pointer gesture last went down.
639 nsecs_t downTime;
640
641 // Time when the pointer went down for a TAP.
642 nsecs_t tapDownTime;
643
644 // Time when the pointer went up for a TAP.
645 nsecs_t tapUpTime;
646
647 // Location of initial tap.
648 float tapX, tapY;
649
650 // Time we started waiting for quiescence.
651 nsecs_t quietTime;
652
653 // Reference points for multitouch gestures.
654 float referenceTouchX; // reference touch X/Y coordinates in surface units
655 float referenceTouchY;
656 float referenceGestureX; // reference gesture X/Y coordinates in pixels
657 float referenceGestureY;
658
659 // Distance that each pointer has traveled which has not yet been
660 // subsumed into the reference gesture position.
661 BitSet32 referenceIdBits;
662 struct Delta {
663 float dx, dy;
664 };
665 Delta referenceDeltas[MAX_POINTER_ID + 1];
666
667 // Describes how touch ids are mapped to gesture ids for freeform gestures.
668 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
669
670 // A velocity tracker for determining whether to switch active pointers during drags.
671 VelocityTracker velocityTracker;
672
673 void reset() {
674 firstTouchTime = LLONG_MIN;
675 activeTouchId = -1;
676 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100677 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700678 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100679 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700680 lastGestureIdBits.clear();
681 downTime = 0;
682 velocityTracker.clear();
683 resetTap();
684 resetQuietTime();
685 }
686
687 void resetTap() {
688 tapDownTime = LLONG_MIN;
689 tapUpTime = LLONG_MIN;
690 }
691
692 void resetQuietTime() { quietTime = LLONG_MIN; }
693 } mPointerGesture;
694
695 struct PointerSimple {
696 PointerCoords currentCoords;
697 PointerProperties currentProperties;
698 PointerCoords lastCoords;
699 PointerProperties lastProperties;
700
701 // True if the pointer is down.
702 bool down;
703
704 // True if the pointer is hovering.
705 bool hovering;
706
707 // Time the pointer last went down.
708 nsecs_t downTime;
709
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000710 // Values reported for the last pointer event.
711 uint32_t source;
712 int32_t displayId;
713 float lastCursorX;
714 float lastCursorY;
715
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700716 void reset() {
717 currentCoords.clear();
718 currentProperties.clear();
719 lastCoords.clear();
720 lastProperties.clear();
721 down = false;
722 hovering = false;
723 downTime = 0;
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000724 source = 0;
725 displayId = ADISPLAY_ID_NONE;
726 lastCursorX = 0.f;
727 lastCursorY = 0.f;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700728 }
729 } mPointerSimple;
730
731 // The pointer and scroll velocity controls.
Harry Cuttse78184b2024-01-08 15:54:58 +0000732 SimpleVelocityControl mPointerVelocityControl;
733 SimpleVelocityControl mWheelXVelocityControl;
734 SimpleVelocityControl mWheelYVelocityControl;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700735
736 std::optional<DisplayViewport> findViewport();
737
738 void resetExternalStylus();
739 void clearStylusDataPendingFlags();
740
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800741 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800742 void initializeOrientedRanges();
743 void initializeSizeRanges();
744
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700745 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700746
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700747 [[nodiscard]] std::list<NotifyArgs> consumeRawTouches(nsecs_t when, nsecs_t readTime,
748 uint32_t policyFlags, bool& outConsumed);
749 [[nodiscard]] std::list<NotifyArgs> processRawTouches(bool timeout);
750 [[nodiscard]] std::list<NotifyArgs> cookAndDispatch(nsecs_t when, nsecs_t readTime);
751 [[nodiscard]] NotifyKeyArgs dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
752 uint32_t policyFlags, int32_t keyEventAction,
753 int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700754
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700755 [[nodiscard]] std::list<NotifyArgs> dispatchTouches(nsecs_t when, nsecs_t readTime,
756 uint32_t policyFlags);
757 [[nodiscard]] std::list<NotifyArgs> dispatchHoverExit(nsecs_t when, nsecs_t readTime,
758 uint32_t policyFlags);
759 [[nodiscard]] std::list<NotifyArgs> dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
760 uint32_t policyFlags);
761 [[nodiscard]] std::list<NotifyArgs> dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
762 uint32_t policyFlags);
763 [[nodiscard]] std::list<NotifyArgs> dispatchButtonPress(nsecs_t when, nsecs_t readTime,
764 uint32_t policyFlags);
LiZhihong758eb562022-11-03 15:28:29 +0800765 [[nodiscard]] std::list<NotifyArgs> dispatchGestureButtonPress(nsecs_t when,
766 uint32_t policyFlags,
767 BitSet32 idBits,
768 nsecs_t readTime);
769 [[nodiscard]] std::list<NotifyArgs> dispatchGestureButtonRelease(nsecs_t when,
770 uint32_t policyFlags,
771 BitSet32 idBits,
772 nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700773 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
774 void cookPointerData();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700775 [[nodiscard]] std::list<NotifyArgs> abortTouches(nsecs_t when, nsecs_t readTime,
776 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700777
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700778 [[nodiscard]] std::list<NotifyArgs> dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
779 uint32_t policyFlags,
780 PointerUsage pointerUsage);
781 [[nodiscard]] std::list<NotifyArgs> abortPointerUsage(nsecs_t when, nsecs_t readTime,
782 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700783
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700784 [[nodiscard]] std::list<NotifyArgs> dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
785 uint32_t policyFlags,
786 bool isTimeout);
787 [[nodiscard]] std::list<NotifyArgs> abortPointerGestures(nsecs_t when, nsecs_t readTime,
788 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700789 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
790 bool* outFinishPreviousGesture, bool isTimeout);
791
Harry Cuttsbea6ce52022-10-14 15:17:30 +0000792 // Returns true if we're in a period of "quiet time" when touchpad gestures should be ignored.
793 bool checkForTouchpadQuietTime(nsecs_t when);
794
795 std::pair<int32_t, float> getFastestFinger();
796
797 void prepareMultiFingerPointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
798 bool* outFinishPreviousGesture);
799
Harry Cutts714d1ad2022-08-24 16:36:43 +0000800 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
801 // between the last and current events. Uses a relative motion.
802 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
803
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700804 [[nodiscard]] std::list<NotifyArgs> dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
805 uint32_t policyFlags);
806 [[nodiscard]] std::list<NotifyArgs> abortPointerStylus(nsecs_t when, nsecs_t readTime,
807 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700808
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700809 [[nodiscard]] std::list<NotifyArgs> dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
810 uint32_t policyFlags);
811 [[nodiscard]] std::list<NotifyArgs> abortPointerMouse(nsecs_t when, nsecs_t readTime,
812 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700813
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700814 [[nodiscard]] std::list<NotifyArgs> dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
815 uint32_t policyFlags, bool down,
Prabir Pradhane71e5702023-03-29 14:51:38 +0000816 bool hovering, int32_t displayId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700817 [[nodiscard]] std::list<NotifyArgs> abortPointerSimple(nsecs_t when, nsecs_t readTime,
818 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700819
Prabir Pradhan3f7545f2022-10-19 16:56:39 +0000820 // Attempts to assign a pointer id to the external stylus. Returns true if the state should be
821 // withheld from further processing while waiting for data from the stylus.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700822 bool assignExternalStylusId(const RawState& state, bool timeout);
823 void applyExternalStylusButtonState(nsecs_t when);
824 void applyExternalStylusTouchState(nsecs_t when);
825
826 // Dispatches a motion event.
827 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
828 // method will take care of setting the index and transmuting the action to DOWN or UP
829 // it is the first / last pointer to go down / up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700830 [[nodiscard]] NotifyMotionArgs dispatchMotion(
831 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
832 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000833 int32_t edgeFlags, const PropertiesArray& properties, const CoordsArray& coords,
834 const IdToIndexArray& idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700835 float yPrecision, nsecs_t downTime, MotionClassification classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700836
Garfield Tanc734e4f2021-01-15 20:01:39 -0800837 // Returns if this touch device is a touch screen with an associated display.
838 bool isTouchScreen();
839 // Updates touch spots if they are enabled. Should only be used when this device is a
840 // touchscreen.
841 void updateTouchSpots();
842
Prabir Pradhan1728b212021-10-19 16:00:03 -0700843 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700844 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
845
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000846 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700847
Prabir Pradhan7d9cb5a2023-03-14 21:18:07 +0000848 // Compute input transforms for DIRECT and POINTER modes.
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000849 void computeInputTransforms();
Arpit Singh403e53c2023-04-18 11:46:56 +0000850 static Parameters::DeviceType computeDeviceType(const InputDeviceContext& deviceContext);
851 static Parameters computeParameters(const InputDeviceContext& deviceContext);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700852};
853
854} // namespace android