blob: bc358b97e6c3c6aa0f83d6dd01e943cedb798b89 [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 Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
20#include <string>
21
Michael Wright227c5542020-07-02 18:30:52 +010022#include <stdint.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000023#include <ui/Rotation.h>
Michael Wright227c5542020-07-02 18:30:52 +010024
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorButtonAccumulator.h"
26#include "CursorScrollAccumulator.h"
27#include "EventHub.h"
28#include "InputMapper.h"
29#include "InputReaderBase.h"
30#include "TouchButtonAccumulator.h"
31
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070032namespace android {
33
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +000034// Maximum amount of latency to add to touch events while waiting for data from an
35// external stylus.
36static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
37
38// Maximum amount of time to wait on touch data before pushing out new pressure data.
39static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
40
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041/* Raw axis information from the driver. */
42struct RawPointerAxes {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000043 RawAbsoluteAxisInfo x{};
44 RawAbsoluteAxisInfo y{};
45 RawAbsoluteAxisInfo pressure{};
46 RawAbsoluteAxisInfo touchMajor{};
47 RawAbsoluteAxisInfo touchMinor{};
48 RawAbsoluteAxisInfo toolMajor{};
49 RawAbsoluteAxisInfo toolMinor{};
50 RawAbsoluteAxisInfo orientation{};
51 RawAbsoluteAxisInfo distance{};
52 RawAbsoluteAxisInfo tiltX{};
53 RawAbsoluteAxisInfo tiltY{};
54 RawAbsoluteAxisInfo trackingId{};
55 RawAbsoluteAxisInfo slot{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
58 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000059 inline void clear() { *this = RawPointerAxes(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060};
61
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000062using PropertiesArray = std::array<PointerProperties, MAX_POINTERS>;
63using CoordsArray = std::array<PointerCoords, MAX_POINTERS>;
64using IdToIndexArray = std::array<uint32_t, MAX_POINTER_ID + 1>;
65
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066/* Raw data for a collection of pointers including a pointer id mapping table. */
67struct RawPointerData {
68 struct Pointer {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000069 uint32_t id{0xFFFFFFFF};
70 int32_t x{};
71 int32_t y{};
72 int32_t pressure{};
73 int32_t touchMajor{};
74 int32_t touchMinor{};
75 int32_t toolMajor{};
76 int32_t toolMinor{};
77 int32_t orientation{};
78 int32_t distance{};
79 int32_t tiltX{};
80 int32_t tiltY{};
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070081 // A fully decoded ToolType constant.
82 ToolType toolType{ToolType::UNKNOWN};
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000083 bool isHovering{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084 };
85
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000086 uint32_t pointerCount{};
87 std::array<Pointer, MAX_POINTERS> pointers{};
88 BitSet32 hoveringIdBits{}, touchingIdBits{}, canceledIdBits{};
89 IdToIndexArray idToIndex{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000091 inline void clear() { *this = RawPointerData(); }
92
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
94
95 inline void markIdBit(uint32_t id, bool isHovering) {
96 if (isHovering) {
97 hoveringIdBits.markBit(id);
98 } else {
99 touchingIdBits.markBit(id);
100 }
101 }
102
103 inline void clearIdBits() {
104 hoveringIdBits.clear();
105 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800106 canceledIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700107 }
108
109 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
110
111 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
112};
113
114/* Cooked data for a collection of pointers including a pointer id mapping table. */
115struct CookedPointerData {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000116 uint32_t pointerCount{};
117 PropertiesArray pointerProperties{};
118 CoordsArray pointerCoords{};
119 BitSet32 hoveringIdBits{}, touchingIdBits{}, canceledIdBits{}, validIdBits{};
120 IdToIndexArray idToIndex{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000122 inline void clear() { *this = CookedPointerData(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700123
124 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
125 return pointerCoords[idToIndex[id]];
126 }
127
128 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
129 return pointerCoords[idToIndex[id]];
130 }
131
132 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
133 return pointerProperties[idToIndex[id]];
134 }
135
136 inline bool isHovering(uint32_t pointerIndex) const {
137 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
138 }
139
140 inline bool isTouching(uint32_t pointerIndex) const {
141 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
142 }
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000143
144 inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700145};
146
147class TouchInputMapper : public InputMapper {
148public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800149 explicit TouchInputMapper(InputDeviceContext& deviceContext);
Michael Wright227c5542020-07-02 18:30:52 +0100150 ~TouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700151
Philip Junker4af3b3d2021-12-14 10:36:55 +0100152 uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000153 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Michael Wright227c5542020-07-02 18:30:52 +0100154 void dump(std::string& dump) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700155 [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
156 const InputReaderConfiguration* config,
157 uint32_t changes) override;
158 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
159 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700160
Michael Wright227c5542020-07-02 18:30:52 +0100161 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
162 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700163 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
Michael Wright227c5542020-07-02 18:30:52 +0100164 uint8_t* outFlags) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700165
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700166 [[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) override;
167 [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when) override;
168 [[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(
169 const StylusState& state) override;
Michael Wright227c5542020-07-02 18:30:52 +0100170 std::optional<int32_t> getAssociatedDisplayId() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700171
172protected:
173 CursorButtonAccumulator mCursorButtonAccumulator;
174 CursorScrollAccumulator mCursorScrollAccumulator;
175 TouchButtonAccumulator mTouchButtonAccumulator;
176
177 struct VirtualKey {
178 int32_t keyCode;
179 int32_t scanCode;
180 uint32_t flags;
181
182 // computed hit box, specified in touch screen coords based on known display size
183 int32_t hitLeft;
184 int32_t hitTop;
185 int32_t hitRight;
186 int32_t hitBottom;
187
188 inline bool isHit(int32_t x, int32_t y) const {
189 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
190 }
191 };
192
193 // Input sources and device mode.
194 uint32_t mSource;
195
Michael Wright227c5542020-07-02 18:30:52 +0100196 enum class DeviceMode {
197 DISABLED, // input is disabled
198 DIRECT, // direct mapping (touchscreen)
199 UNSCALED, // unscaled mapping (touchpad)
200 NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
201 POINTER, // pointer mapping (pointer)
Dominik Laskowski75788452021-02-09 18:51:25 -0800202
203 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700204 };
205 DeviceMode mDeviceMode;
206
207 // The reader's configuration.
208 InputReaderConfiguration mConfig;
209
210 // Immutable configuration parameters.
211 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100212 enum class DeviceType {
213 TOUCH_SCREEN,
Michael Wright227c5542020-07-02 18:30:52 +0100214 TOUCH_NAVIGATION,
215 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800216
217 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218 };
219
220 DeviceType deviceType;
221 bool hasAssociatedDisplay;
222 bool associatedDisplayIsExternal;
223 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700224
Michael Wrighta9cf4192022-12-01 23:46:39 +0000225 ui::Rotation orientation;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700226
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700227 bool hasButtonUnderPad;
228 std::string uniqueDisplayId;
229
Michael Wright227c5542020-07-02 18:30:52 +0100230 enum class GestureMode {
231 SINGLE_TOUCH,
232 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800233
234 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700235 };
236 GestureMode gestureMode;
237
238 bool wake;
Prabir Pradhan167c2702022-09-14 00:37:24 +0000239
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000240 // The Universal Stylus Initiative (USI) protocol version supported by this device.
241 std::optional<InputDeviceUsiVersion> usiVersion;
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700242
243 // Allows touches while the display is off.
244 bool enableForInactiveViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700245 } mParameters;
246
247 // Immutable calibration parameters in parsed form.
248 struct Calibration {
249 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100250 enum class SizeCalibration {
251 DEFAULT,
252 NONE,
253 GEOMETRIC,
254 DIAMETER,
255 BOX,
256 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800257 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700258 };
259
260 SizeCalibration sizeCalibration;
261
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700262 std::optional<float> sizeScale;
263 std::optional<float> sizeBias;
264 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700265
266 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100267 enum class PressureCalibration {
268 DEFAULT,
269 NONE,
270 PHYSICAL,
271 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 };
273
274 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700275 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276
277 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100278 enum class OrientationCalibration {
279 DEFAULT,
280 NONE,
281 INTERPOLATED,
282 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700283 };
284
285 OrientationCalibration orientationCalibration;
286
287 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100288 enum class DistanceCalibration {
289 DEFAULT,
290 NONE,
291 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 };
293
294 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700295 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700296
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700297 inline void applySizeScaleAndBias(float& outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700298 if (sizeScale) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700299 outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700301 if (sizeBias) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700302 outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700303 }
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700304 if (outSize < 0) {
305 outSize = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 }
307 }
308 } mCalibration;
309
310 // Affine location transformation/calibration
311 struct TouchAffineTransformation mAffineTransform;
312
313 RawPointerAxes mRawPointerAxes;
314
315 struct RawState {
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000316 nsecs_t when{std::numeric_limits<nsecs_t>::min()};
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000317 nsecs_t readTime{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318
319 // Raw pointer sample data.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000320 RawPointerData rawPointerData{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000322 int32_t buttonState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323
324 // Scroll state.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000325 int32_t rawVScroll{};
326 int32_t rawHScroll{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700327
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000328 inline void clear() { *this = RawState(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329 };
330
331 struct CookedState {
332 // Cooked pointer sample data.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000333 CookedPointerData cookedPointerData{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700334
335 // Id bits used to differentiate fingers, stylus and mouse tools.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000336 BitSet32 fingerIdBits{};
337 BitSet32 stylusIdBits{};
338 BitSet32 mouseIdBits{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700339
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000340 int32_t buttonState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000342 inline void clear() { *this = CookedState(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343 };
344
345 std::vector<RawState> mRawStatesPending;
346 RawState mCurrentRawState;
347 CookedState mCurrentCookedState;
348 RawState mLastRawState;
349 CookedState mLastCookedState;
350
351 // State provided by an external stylus
352 StylusState mExternalStylusState;
Prabir Pradhan8d9ba912022-11-11 22:26:33 +0000353 // If an external stylus is capable of reporting pointer-specific data like pressure, we will
354 // attempt to fuse the pointer data reported by the stylus to the first touch pointer. This is
355 // the id of the pointer to which the external stylus data is fused.
356 std::optional<uint32_t> mFusedStylusPointerId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700357 nsecs_t mExternalStylusFusionTimeout;
358 bool mExternalStylusDataPending;
Prabir Pradhan124ea442022-10-28 20:27:44 +0000359 // A subset of the buttons in mCurrentRawState that came from an external stylus.
360 int32_t mExternalStylusButtonsApplied;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700361
362 // True if we sent a HOVER_ENTER event.
363 bool mSentHoverEnter;
364
365 // Have we assigned pointer IDs for this stream
366 bool mHavePointerIds;
367
368 // Is the current stream of direct touch events aborted
369 bool mCurrentMotionAborted;
370
371 // The time the primary pointer last went down.
372 nsecs_t mDownTime;
373
374 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100375 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700376
377 std::vector<VirtualKey> mVirtualKeys;
378
379 virtual void configureParameters();
380 virtual void dumpParameters(std::string& dump);
381 virtual void configureRawPointerAxes();
382 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700383 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
384 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700385 virtual void configureVirtualKeys();
386 virtual void dumpVirtualKeys(std::string& dump);
387 virtual void parseCalibration();
388 virtual void resolveCalibration();
389 virtual void dumpCalibration(std::string& dump);
390 virtual void updateAffineTransformation();
391 virtual void dumpAffineTransformation(std::string& dump);
392 virtual void resolveExternalStylusPresence();
393 virtual bool hasStylus() const = 0;
394 virtual bool hasExternalStylus() const;
395
396 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
397
398private:
399 // The current viewport.
400 // The components of the viewport are specified in the display's rotated orientation.
401 DisplayViewport mViewport;
402
Prabir Pradhan2d613f42022-11-10 20:22:06 +0000403 // We refer to the display as being in the "natural orientation" when there is no rotation
404 // applied. The display size obtained from the viewport in the natural orientation.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000405 // Always starts at (0, 0).
406 ui::Size mDisplayBounds{ui::kInvalidSize};
Arthur Hung4197f6b2020-03-16 15:39:59 +0800407
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000408 // The physical frame is the rectangle in the rotated display's coordinate space that maps to
Prabir Pradhan1728b212021-10-19 16:00:03 -0700409 // the logical display frame.
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000410 Rect mPhysicalFrameInRotatedDisplay{Rect::INVALID_RECT};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700411
Prabir Pradhan1728b212021-10-19 16:00:03 -0700412 // The orientation of the input device relative to that of the display panel. It specifies
413 // the rotation of the input device coordinates required to produce the display panel
414 // orientation, so it will depend on whether the device is orientation aware.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ui::Rotation mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700416
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000417 // The transform that maps the input device's raw coordinate space to the un-rotated display's
418 // coordinate space. InputReader generates events in the un-rotated display's coordinate space.
419 ui::Transform mRawToDisplay;
420
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000421 // The transform that maps the input device's raw coordinate space to the rotated display's
422 // coordinate space. This used to perform hit-testing of raw events with the physical frame in
423 // the rotated coordinate space. See mPhysicalFrameInRotatedDisplay.
424 ui::Transform mRawToRotatedDisplay;
425
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000426 // The transform used for non-planar raw axes, such as orientation and tilt.
427 ui::Transform mRawRotation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700428
429 float mGeometricScale;
430
431 float mPressureScale;
432
433 float mSizeScale;
434
435 float mOrientationScale;
436
437 float mDistanceScale;
438
439 bool mHaveTilt;
440 float mTiltXCenter;
441 float mTiltXScale;
442 float mTiltYCenter;
443 float mTiltYScale;
444
445 bool mExternalStylusConnected;
446
447 // Oriented motion ranges for input device info.
448 struct OrientedRanges {
449 InputDeviceInfo::MotionRange x;
450 InputDeviceInfo::MotionRange y;
451 InputDeviceInfo::MotionRange pressure;
452
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700453 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700454
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700455 std::optional<InputDeviceInfo::MotionRange> touchMajor;
456 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700458 std::optional<InputDeviceInfo::MotionRange> toolMajor;
459 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700460
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700461 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700462
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700463 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700464
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700465 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700466
467 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700468 size = std::nullopt;
469 touchMajor = std::nullopt;
470 touchMinor = std::nullopt;
471 toolMajor = std::nullopt;
472 toolMinor = std::nullopt;
473 orientation = std::nullopt;
474 distance = std::nullopt;
475 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700476 }
477 } mOrientedRanges;
478
479 // Oriented dimensions and precision.
480 float mOrientedXPrecision;
481 float mOrientedYPrecision;
482
483 struct CurrentVirtualKeyState {
484 bool down;
485 bool ignored;
486 nsecs_t downTime;
487 int32_t keyCode;
488 int32_t scanCode;
489 } mCurrentVirtualKey;
490
491 // Scale factor for gesture or mouse based pointer movements.
492 float mPointerXMovementScale;
493 float mPointerYMovementScale;
494
495 // Scale factor for gesture based zooming and other freeform motions.
496 float mPointerXZoomScale;
497 float mPointerYZoomScale;
498
HQ Liue6983c72022-04-19 22:14:56 +0000499 // The maximum swipe width between pointers to detect a swipe gesture
500 // in the number of pixels.Touches that are wider than this are translated
501 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700502 float mPointerGestureMaxSwipeWidth;
503
504 struct PointerDistanceHeapElement {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000505 uint32_t currentPointerIndex : 8 {};
506 uint32_t lastPointerIndex : 8 {};
507 uint64_t distance : 48 {}; // squared distance
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700508 };
509
Michael Wright227c5542020-07-02 18:30:52 +0100510 enum class PointerUsage {
511 NONE,
512 GESTURES,
513 STYLUS,
514 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700515 };
516 PointerUsage mPointerUsage;
517
518 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100519 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700520 // No fingers, button is not pressed.
521 // Nothing happening.
522 NEUTRAL,
523
524 // No fingers, button is not pressed.
525 // Tap detected.
526 // Emits DOWN and UP events at the pointer location.
527 TAP,
528
529 // Exactly one finger dragging following a tap.
530 // Pointer follows the active finger.
531 // Emits DOWN, MOVE and UP events at the pointer location.
532 //
533 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
534 TAP_DRAG,
535
536 // Button is pressed.
537 // Pointer follows the active finger if there is one. Other fingers are ignored.
538 // Emits DOWN, MOVE and UP events at the pointer location.
539 BUTTON_CLICK_OR_DRAG,
540
541 // Exactly one finger, button is not pressed.
542 // Pointer follows the active finger.
543 // Emits HOVER_MOVE events at the pointer location.
544 //
545 // Detect taps when the finger goes up while in HOVER mode.
546 HOVER,
547
548 // Exactly two fingers but neither have moved enough to clearly indicate
549 // whether a swipe or freeform gesture was intended. We consider the
550 // pointer to be pressed so this enables clicking or long-pressing on buttons.
551 // Pointer does not move.
552 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
553 PRESS,
554
555 // Exactly two fingers moving in the same direction, button is not pressed.
556 // Pointer does not move.
557 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
558 // follows the midpoint between both fingers.
559 SWIPE,
560
561 // Two or more fingers moving in arbitrary directions, button is not pressed.
562 // Pointer does not move.
563 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
564 // each finger individually relative to the initial centroid of the finger.
565 FREEFORM,
566
567 // Waiting for quiet time to end before starting the next gesture.
568 QUIET,
569 };
570
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800571 // When a gesture is sent to an unfocused window, return true if it can bring that window
572 // into focus, false otherwise.
573 static bool canGestureAffectWindowFocus(Mode mode) {
574 switch (mode) {
575 case Mode::TAP:
576 case Mode::TAP_DRAG:
577 case Mode::BUTTON_CLICK_OR_DRAG:
578 // Taps can affect window focus.
579 return true;
580 case Mode::FREEFORM:
581 case Mode::HOVER:
582 case Mode::NEUTRAL:
583 case Mode::PRESS:
584 case Mode::QUIET:
585 case Mode::SWIPE:
586 // Most gestures can be performed on an unfocused window, so they should not
587 // not affect window focus.
588 return false;
589 }
590 }
591
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700592 // Time the first finger went down.
593 nsecs_t firstTouchTime;
594
595 // The active pointer id from the raw touch data.
596 int32_t activeTouchId; // -1 if none
597
598 // The active pointer id from the gesture last delivered to the application.
599 int32_t activeGestureId; // -1 if none
600
601 // Pointer coords and ids for the current and previous pointer gesture.
602 Mode currentGestureMode;
603 BitSet32 currentGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000604 IdToIndexArray currentGestureIdToIndex{};
605 PropertiesArray currentGestureProperties{};
606 CoordsArray currentGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700607
608 Mode lastGestureMode;
609 BitSet32 lastGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000610 IdToIndexArray lastGestureIdToIndex{};
611 PropertiesArray lastGestureProperties{};
612 CoordsArray lastGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700613
614 // Time the pointer gesture last went down.
615 nsecs_t downTime;
616
617 // Time when the pointer went down for a TAP.
618 nsecs_t tapDownTime;
619
620 // Time when the pointer went up for a TAP.
621 nsecs_t tapUpTime;
622
623 // Location of initial tap.
624 float tapX, tapY;
625
626 // Time we started waiting for quiescence.
627 nsecs_t quietTime;
628
629 // Reference points for multitouch gestures.
630 float referenceTouchX; // reference touch X/Y coordinates in surface units
631 float referenceTouchY;
632 float referenceGestureX; // reference gesture X/Y coordinates in pixels
633 float referenceGestureY;
634
635 // Distance that each pointer has traveled which has not yet been
636 // subsumed into the reference gesture position.
637 BitSet32 referenceIdBits;
638 struct Delta {
639 float dx, dy;
640 };
641 Delta referenceDeltas[MAX_POINTER_ID + 1];
642
643 // Describes how touch ids are mapped to gesture ids for freeform gestures.
644 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
645
646 // A velocity tracker for determining whether to switch active pointers during drags.
647 VelocityTracker velocityTracker;
648
649 void reset() {
650 firstTouchTime = LLONG_MIN;
651 activeTouchId = -1;
652 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100653 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700654 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100655 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700656 lastGestureIdBits.clear();
657 downTime = 0;
658 velocityTracker.clear();
659 resetTap();
660 resetQuietTime();
661 }
662
663 void resetTap() {
664 tapDownTime = LLONG_MIN;
665 tapUpTime = LLONG_MIN;
666 }
667
668 void resetQuietTime() { quietTime = LLONG_MIN; }
669 } mPointerGesture;
670
671 struct PointerSimple {
672 PointerCoords currentCoords;
673 PointerProperties currentProperties;
674 PointerCoords lastCoords;
675 PointerProperties lastProperties;
676
677 // True if the pointer is down.
678 bool down;
679
680 // True if the pointer is hovering.
681 bool hovering;
682
683 // Time the pointer last went down.
684 nsecs_t downTime;
685
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000686 // Values reported for the last pointer event.
687 uint32_t source;
688 int32_t displayId;
689 float lastCursorX;
690 float lastCursorY;
691
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700692 void reset() {
693 currentCoords.clear();
694 currentProperties.clear();
695 lastCoords.clear();
696 lastProperties.clear();
697 down = false;
698 hovering = false;
699 downTime = 0;
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000700 source = 0;
701 displayId = ADISPLAY_ID_NONE;
702 lastCursorX = 0.f;
703 lastCursorY = 0.f;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700704 }
705 } mPointerSimple;
706
707 // The pointer and scroll velocity controls.
708 VelocityControl mPointerVelocityControl;
709 VelocityControl mWheelXVelocityControl;
710 VelocityControl mWheelYVelocityControl;
711
712 std::optional<DisplayViewport> findViewport();
713
714 void resetExternalStylus();
715 void clearStylusDataPendingFlags();
716
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800717 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800718 void initializeOrientedRanges();
719 void initializeSizeRanges();
720
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700721 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700722
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700723 [[nodiscard]] std::list<NotifyArgs> consumeRawTouches(nsecs_t when, nsecs_t readTime,
724 uint32_t policyFlags, bool& outConsumed);
725 [[nodiscard]] std::list<NotifyArgs> processRawTouches(bool timeout);
726 [[nodiscard]] std::list<NotifyArgs> cookAndDispatch(nsecs_t when, nsecs_t readTime);
727 [[nodiscard]] NotifyKeyArgs dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
728 uint32_t policyFlags, int32_t keyEventAction,
729 int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700730
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700731 [[nodiscard]] std::list<NotifyArgs> dispatchTouches(nsecs_t when, nsecs_t readTime,
732 uint32_t policyFlags);
733 [[nodiscard]] std::list<NotifyArgs> dispatchHoverExit(nsecs_t when, nsecs_t readTime,
734 uint32_t policyFlags);
735 [[nodiscard]] std::list<NotifyArgs> dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
736 uint32_t policyFlags);
737 [[nodiscard]] std::list<NotifyArgs> dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
738 uint32_t policyFlags);
739 [[nodiscard]] std::list<NotifyArgs> dispatchButtonPress(nsecs_t when, nsecs_t readTime,
740 uint32_t policyFlags);
LiZhihong758eb562022-11-03 15:28:29 +0800741 [[nodiscard]] std::list<NotifyArgs> dispatchGestureButtonPress(nsecs_t when,
742 uint32_t policyFlags,
743 BitSet32 idBits,
744 nsecs_t readTime);
745 [[nodiscard]] std::list<NotifyArgs> dispatchGestureButtonRelease(nsecs_t when,
746 uint32_t policyFlags,
747 BitSet32 idBits,
748 nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700749 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
750 void cookPointerData();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700751 [[nodiscard]] std::list<NotifyArgs> abortTouches(nsecs_t when, nsecs_t readTime,
752 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700753
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700754 [[nodiscard]] std::list<NotifyArgs> dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
755 uint32_t policyFlags,
756 PointerUsage pointerUsage);
757 [[nodiscard]] std::list<NotifyArgs> abortPointerUsage(nsecs_t when, nsecs_t readTime,
758 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700759
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700760 [[nodiscard]] std::list<NotifyArgs> dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
761 uint32_t policyFlags,
762 bool isTimeout);
763 [[nodiscard]] std::list<NotifyArgs> abortPointerGestures(nsecs_t when, nsecs_t readTime,
764 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700765 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
766 bool* outFinishPreviousGesture, bool isTimeout);
767
Harry Cuttsbea6ce52022-10-14 15:17:30 +0000768 // Returns true if we're in a period of "quiet time" when touchpad gestures should be ignored.
769 bool checkForTouchpadQuietTime(nsecs_t when);
770
771 std::pair<int32_t, float> getFastestFinger();
772
773 void prepareMultiFingerPointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
774 bool* outFinishPreviousGesture);
775
Harry Cutts714d1ad2022-08-24 16:36:43 +0000776 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
777 // between the last and current events. Uses a relative motion.
778 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
779
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700780 [[nodiscard]] std::list<NotifyArgs> dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
781 uint32_t policyFlags);
782 [[nodiscard]] std::list<NotifyArgs> abortPointerStylus(nsecs_t when, nsecs_t readTime,
783 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700784
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700785 [[nodiscard]] std::list<NotifyArgs> dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
786 uint32_t policyFlags);
787 [[nodiscard]] std::list<NotifyArgs> abortPointerMouse(nsecs_t when, nsecs_t readTime,
788 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700789
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700790 [[nodiscard]] std::list<NotifyArgs> dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
791 uint32_t policyFlags, bool down,
792 bool hovering);
793 [[nodiscard]] std::list<NotifyArgs> abortPointerSimple(nsecs_t when, nsecs_t readTime,
794 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700795
Prabir Pradhan3f7545f2022-10-19 16:56:39 +0000796 // Attempts to assign a pointer id to the external stylus. Returns true if the state should be
797 // withheld from further processing while waiting for data from the stylus.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700798 bool assignExternalStylusId(const RawState& state, bool timeout);
799 void applyExternalStylusButtonState(nsecs_t when);
800 void applyExternalStylusTouchState(nsecs_t when);
801
802 // Dispatches a motion event.
803 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
804 // method will take care of setting the index and transmuting the action to DOWN or UP
805 // it is the first / last pointer to go down / up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700806 [[nodiscard]] NotifyMotionArgs dispatchMotion(
807 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
808 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000809 int32_t edgeFlags, const PropertiesArray& properties, const CoordsArray& coords,
810 const IdToIndexArray& idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700811 float yPrecision, nsecs_t downTime, MotionClassification classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700812
Garfield Tanc734e4f2021-01-15 20:01:39 -0800813 // Returns if this touch device is a touch screen with an associated display.
814 bool isTouchScreen();
815 // Updates touch spots if they are enabled. Should only be used when this device is a
816 // touchscreen.
817 void updateTouchSpots();
818
Prabir Pradhan1728b212021-10-19 16:00:03 -0700819 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700820 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
821
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000822 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700823
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000824 void computeInputTransforms();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000825
826 void configureDeviceType();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700827};
828
829} // namespace android