blob: d2faf3fe1dd1267f6f054d0dac79a6da3fd52797 [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
Michael Wright227c5542020-07-02 18:30:52 +010019#include <stdint.h>
20
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070021#include "CursorButtonAccumulator.h"
22#include "CursorScrollAccumulator.h"
23#include "EventHub.h"
24#include "InputMapper.h"
25#include "InputReaderBase.h"
26#include "TouchButtonAccumulator.h"
27
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028namespace android {
29
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +000030// Maximum amount of latency to add to touch events while waiting for data from an
31// external stylus.
32static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
33
34// Maximum amount of time to wait on touch data before pushing out new pressure data.
35static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
36
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037/* Raw axis information from the driver. */
38struct RawPointerAxes {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000039 RawAbsoluteAxisInfo x{};
40 RawAbsoluteAxisInfo y{};
41 RawAbsoluteAxisInfo pressure{};
42 RawAbsoluteAxisInfo touchMajor{};
43 RawAbsoluteAxisInfo touchMinor{};
44 RawAbsoluteAxisInfo toolMajor{};
45 RawAbsoluteAxisInfo toolMinor{};
46 RawAbsoluteAxisInfo orientation{};
47 RawAbsoluteAxisInfo distance{};
48 RawAbsoluteAxisInfo tiltX{};
49 RawAbsoluteAxisInfo tiltY{};
50 RawAbsoluteAxisInfo trackingId{};
51 RawAbsoluteAxisInfo slot{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
54 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000055 inline void clear() { *this = RawPointerAxes(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056};
57
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000058using PropertiesArray = std::array<PointerProperties, MAX_POINTERS>;
59using CoordsArray = std::array<PointerCoords, MAX_POINTERS>;
60using IdToIndexArray = std::array<uint32_t, MAX_POINTER_ID + 1>;
61
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062/* Raw data for a collection of pointers including a pointer id mapping table. */
63struct RawPointerData {
64 struct Pointer {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000065 uint32_t id{0xFFFFFFFF};
66 int32_t x{};
67 int32_t y{};
68 int32_t pressure{};
69 int32_t touchMajor{};
70 int32_t touchMinor{};
71 int32_t toolMajor{};
72 int32_t toolMinor{};
73 int32_t orientation{};
74 int32_t distance{};
75 int32_t tiltX{};
76 int32_t tiltY{};
77 // A fully decoded AMOTION_EVENT_TOOL_TYPE constant.
78 int32_t toolType{AMOTION_EVENT_TOOL_TYPE_UNKNOWN};
79 bool isHovering{false};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 };
81
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000082 uint32_t pointerCount{};
83 std::array<Pointer, MAX_POINTERS> pointers{};
84 BitSet32 hoveringIdBits{}, touchingIdBits{}, canceledIdBits{};
85 IdToIndexArray idToIndex{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070086
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000087 inline void clear() { *this = RawPointerData(); }
88
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
90
91 inline void markIdBit(uint32_t id, bool isHovering) {
92 if (isHovering) {
93 hoveringIdBits.markBit(id);
94 } else {
95 touchingIdBits.markBit(id);
96 }
97 }
98
99 inline void clearIdBits() {
100 hoveringIdBits.clear();
101 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800102 canceledIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103 }
104
105 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
106
107 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
108};
109
110/* Cooked data for a collection of pointers including a pointer id mapping table. */
111struct CookedPointerData {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000112 uint32_t pointerCount{};
113 PropertiesArray pointerProperties{};
114 CoordsArray pointerCoords{};
115 BitSet32 hoveringIdBits{}, touchingIdBits{}, canceledIdBits{}, validIdBits{};
116 IdToIndexArray idToIndex{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000118 inline void clear() { *this = CookedPointerData(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119
120 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
121 return pointerCoords[idToIndex[id]];
122 }
123
124 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
125 return pointerCoords[idToIndex[id]];
126 }
127
128 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
129 return pointerProperties[idToIndex[id]];
130 }
131
132 inline bool isHovering(uint32_t pointerIndex) const {
133 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
134 }
135
136 inline bool isTouching(uint32_t pointerIndex) const {
137 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
138 }
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000139
140 inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141};
142
143class TouchInputMapper : public InputMapper {
144public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800145 explicit TouchInputMapper(InputDeviceContext& deviceContext);
Michael Wright227c5542020-07-02 18:30:52 +0100146 ~TouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147
Philip Junker4af3b3d2021-12-14 10:36:55 +0100148 uint32_t getSources() const override;
Michael Wright227c5542020-07-02 18:30:52 +0100149 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
150 void dump(std::string& dump) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700151 [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
152 const InputReaderConfiguration* config,
153 uint32_t changes) override;
154 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
155 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700156
Michael Wright227c5542020-07-02 18:30:52 +0100157 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
158 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700159 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
Michael Wright227c5542020-07-02 18:30:52 +0100160 uint8_t* outFlags) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700161
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700162 [[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) override;
163 [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when) override;
164 [[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(
165 const StylusState& state) override;
Michael Wright227c5542020-07-02 18:30:52 +0100166 std::optional<int32_t> getAssociatedDisplayId() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700167
168protected:
169 CursorButtonAccumulator mCursorButtonAccumulator;
170 CursorScrollAccumulator mCursorScrollAccumulator;
171 TouchButtonAccumulator mTouchButtonAccumulator;
172
173 struct VirtualKey {
174 int32_t keyCode;
175 int32_t scanCode;
176 uint32_t flags;
177
178 // computed hit box, specified in touch screen coords based on known display size
179 int32_t hitLeft;
180 int32_t hitTop;
181 int32_t hitRight;
182 int32_t hitBottom;
183
184 inline bool isHit(int32_t x, int32_t y) const {
185 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
186 }
187 };
188
189 // Input sources and device mode.
190 uint32_t mSource;
191
Michael Wright227c5542020-07-02 18:30:52 +0100192 enum class DeviceMode {
193 DISABLED, // input is disabled
194 DIRECT, // direct mapping (touchscreen)
195 UNSCALED, // unscaled mapping (touchpad)
196 NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
197 POINTER, // pointer mapping (pointer)
Dominik Laskowski75788452021-02-09 18:51:25 -0800198
199 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700200 };
201 DeviceMode mDeviceMode;
202
203 // The reader's configuration.
204 InputReaderConfiguration mConfig;
205
206 // Immutable configuration parameters.
207 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100208 enum class DeviceType {
209 TOUCH_SCREEN,
Michael Wright227c5542020-07-02 18:30:52 +0100210 TOUCH_NAVIGATION,
211 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800212
213 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700214 };
215
216 DeviceType deviceType;
217 bool hasAssociatedDisplay;
218 bool associatedDisplayIsExternal;
219 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700220
221 enum class Orientation : int32_t {
222 ORIENTATION_0 = DISPLAY_ORIENTATION_0,
223 ORIENTATION_90 = DISPLAY_ORIENTATION_90,
224 ORIENTATION_180 = DISPLAY_ORIENTATION_180,
225 ORIENTATION_270 = DISPLAY_ORIENTATION_270,
Dominik Laskowski75788452021-02-09 18:51:25 -0800226
227 ftl_last = ORIENTATION_270
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700228 };
229 Orientation orientation;
230
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700231 bool hasButtonUnderPad;
232 std::string uniqueDisplayId;
233
Michael Wright227c5542020-07-02 18:30:52 +0100234 enum class GestureMode {
235 SINGLE_TOUCH,
236 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800237
238 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700239 };
240 GestureMode gestureMode;
241
242 bool wake;
Prabir Pradhan167c2702022-09-14 00:37:24 +0000243
244 // Whether the device supports the Universal Stylus Initiative (USI) protocol for styluses.
245 bool supportsUsi;
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700246
247 // Allows touches while the display is off.
248 bool enableForInactiveViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 } mParameters;
250
251 // Immutable calibration parameters in parsed form.
252 struct Calibration {
253 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100254 enum class SizeCalibration {
255 DEFAULT,
256 NONE,
257 GEOMETRIC,
258 DIAMETER,
259 BOX,
260 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800261 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700262 };
263
264 SizeCalibration sizeCalibration;
265
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700266 std::optional<float> sizeScale;
267 std::optional<float> sizeBias;
268 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700269
270 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100271 enum class PressureCalibration {
272 DEFAULT,
273 NONE,
274 PHYSICAL,
275 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 };
277
278 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700279 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280
281 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100282 enum class OrientationCalibration {
283 DEFAULT,
284 NONE,
285 INTERPOLATED,
286 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700287 };
288
289 OrientationCalibration orientationCalibration;
290
291 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100292 enum class DistanceCalibration {
293 DEFAULT,
294 NONE,
295 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700296 };
297
298 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700299 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300
Michael Wright227c5542020-07-02 18:30:52 +0100301 enum class CoverageCalibration {
302 DEFAULT,
303 NONE,
304 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700305 };
306
307 CoverageCalibration coverageCalibration;
308
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700309 inline void applySizeScaleAndBias(float& outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700310 if (sizeScale) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700311 outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700312 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700313 if (sizeBias) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700314 outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700315 }
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700316 if (outSize < 0) {
317 outSize = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318 }
319 }
320 } mCalibration;
321
322 // Affine location transformation/calibration
323 struct TouchAffineTransformation mAffineTransform;
324
325 RawPointerAxes mRawPointerAxes;
326
327 struct RawState {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000328 nsecs_t when{};
329 nsecs_t readTime{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330
331 // Raw pointer sample data.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000332 RawPointerData rawPointerData{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000334 int32_t buttonState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700335
336 // Scroll state.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000337 int32_t rawVScroll{};
338 int32_t rawHScroll{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700339
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000340 inline void clear() { *this = RawState(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341 };
342
343 struct CookedState {
344 // Cooked pointer sample data.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000345 CookedPointerData cookedPointerData{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346
347 // Id bits used to differentiate fingers, stylus and mouse tools.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000348 BitSet32 fingerIdBits{};
349 BitSet32 stylusIdBits{};
350 BitSet32 mouseIdBits{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700351
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000352 int32_t buttonState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700353
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000354 inline void clear() { *this = CookedState(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700355 };
356
357 std::vector<RawState> mRawStatesPending;
358 RawState mCurrentRawState;
359 CookedState mCurrentCookedState;
360 RawState mLastRawState;
361 CookedState mLastCookedState;
362
363 // State provided by an external stylus
364 StylusState mExternalStylusState;
Prabir Pradhan8d9ba912022-11-11 22:26:33 +0000365 // If an external stylus is capable of reporting pointer-specific data like pressure, we will
366 // attempt to fuse the pointer data reported by the stylus to the first touch pointer. This is
367 // the id of the pointer to which the external stylus data is fused.
368 std::optional<uint32_t> mFusedStylusPointerId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700369 nsecs_t mExternalStylusFusionTimeout;
370 bool mExternalStylusDataPending;
Prabir Pradhan124ea442022-10-28 20:27:44 +0000371 // A subset of the buttons in mCurrentRawState that came from an external stylus.
372 int32_t mExternalStylusButtonsApplied;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373
374 // True if we sent a HOVER_ENTER event.
375 bool mSentHoverEnter;
376
377 // Have we assigned pointer IDs for this stream
378 bool mHavePointerIds;
379
380 // Is the current stream of direct touch events aborted
381 bool mCurrentMotionAborted;
382
383 // The time the primary pointer last went down.
384 nsecs_t mDownTime;
385
386 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100387 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388
389 std::vector<VirtualKey> mVirtualKeys;
390
391 virtual void configureParameters();
392 virtual void dumpParameters(std::string& dump);
393 virtual void configureRawPointerAxes();
394 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700395 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
396 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700397 virtual void configureVirtualKeys();
398 virtual void dumpVirtualKeys(std::string& dump);
399 virtual void parseCalibration();
400 virtual void resolveCalibration();
401 virtual void dumpCalibration(std::string& dump);
402 virtual void updateAffineTransformation();
403 virtual void dumpAffineTransformation(std::string& dump);
404 virtual void resolveExternalStylusPresence();
405 virtual bool hasStylus() const = 0;
406 virtual bool hasExternalStylus() const;
407
408 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
409
410private:
411 // The current viewport.
412 // The components of the viewport are specified in the display's rotated orientation.
413 DisplayViewport mViewport;
414
Prabir Pradhan1728b212021-10-19 16:00:03 -0700415 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700416 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700417 int32_t mDisplayWidth;
418 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800419
Prabir Pradhan1728b212021-10-19 16:00:03 -0700420 // The physical frame is the rectangle in the display's coordinate space that maps to the
421 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700422 int32_t mPhysicalWidth;
423 int32_t mPhysicalHeight;
424 int32_t mPhysicalLeft;
425 int32_t mPhysicalTop;
426
Prabir Pradhan1728b212021-10-19 16:00:03 -0700427 // The orientation of the input device relative to that of the display panel. It specifies
428 // the rotation of the input device coordinates required to produce the display panel
429 // orientation, so it will depend on whether the device is orientation aware.
430 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431
432 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 float mXScale;
434 float mXPrecision;
435
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700436 float mYScale;
437 float mYPrecision;
438
439 float mGeometricScale;
440
441 float mPressureScale;
442
443 float mSizeScale;
444
445 float mOrientationScale;
446
447 float mDistanceScale;
448
449 bool mHaveTilt;
450 float mTiltXCenter;
451 float mTiltXScale;
452 float mTiltYCenter;
453 float mTiltYScale;
454
455 bool mExternalStylusConnected;
456
457 // Oriented motion ranges for input device info.
458 struct OrientedRanges {
459 InputDeviceInfo::MotionRange x;
460 InputDeviceInfo::MotionRange y;
461 InputDeviceInfo::MotionRange pressure;
462
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700463 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700464
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700465 std::optional<InputDeviceInfo::MotionRange> touchMajor;
466 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700468 std::optional<InputDeviceInfo::MotionRange> toolMajor;
469 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700470
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700471 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700472
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700473 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700474
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700475 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700476
477 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700478 size = std::nullopt;
479 touchMajor = std::nullopt;
480 touchMinor = std::nullopt;
481 toolMajor = std::nullopt;
482 toolMinor = std::nullopt;
483 orientation = std::nullopt;
484 distance = std::nullopt;
485 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700486 }
487 } mOrientedRanges;
488
489 // Oriented dimensions and precision.
490 float mOrientedXPrecision;
491 float mOrientedYPrecision;
492
493 struct CurrentVirtualKeyState {
494 bool down;
495 bool ignored;
496 nsecs_t downTime;
497 int32_t keyCode;
498 int32_t scanCode;
499 } mCurrentVirtualKey;
500
501 // Scale factor for gesture or mouse based pointer movements.
502 float mPointerXMovementScale;
503 float mPointerYMovementScale;
504
505 // Scale factor for gesture based zooming and other freeform motions.
506 float mPointerXZoomScale;
507 float mPointerYZoomScale;
508
HQ Liue6983c72022-04-19 22:14:56 +0000509 // The maximum swipe width between pointers to detect a swipe gesture
510 // in the number of pixels.Touches that are wider than this are translated
511 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700512 float mPointerGestureMaxSwipeWidth;
513
514 struct PointerDistanceHeapElement {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000515 uint32_t currentPointerIndex : 8 {};
516 uint32_t lastPointerIndex : 8 {};
517 uint64_t distance : 48 {}; // squared distance
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700518 };
519
Michael Wright227c5542020-07-02 18:30:52 +0100520 enum class PointerUsage {
521 NONE,
522 GESTURES,
523 STYLUS,
524 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700525 };
526 PointerUsage mPointerUsage;
527
528 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100529 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700530 // No fingers, button is not pressed.
531 // Nothing happening.
532 NEUTRAL,
533
534 // No fingers, button is not pressed.
535 // Tap detected.
536 // Emits DOWN and UP events at the pointer location.
537 TAP,
538
539 // Exactly one finger dragging following a tap.
540 // Pointer follows the active finger.
541 // Emits DOWN, MOVE and UP events at the pointer location.
542 //
543 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
544 TAP_DRAG,
545
546 // Button is pressed.
547 // Pointer follows the active finger if there is one. Other fingers are ignored.
548 // Emits DOWN, MOVE and UP events at the pointer location.
549 BUTTON_CLICK_OR_DRAG,
550
551 // Exactly one finger, button is not pressed.
552 // Pointer follows the active finger.
553 // Emits HOVER_MOVE events at the pointer location.
554 //
555 // Detect taps when the finger goes up while in HOVER mode.
556 HOVER,
557
558 // Exactly two fingers but neither have moved enough to clearly indicate
559 // whether a swipe or freeform gesture was intended. We consider the
560 // pointer to be pressed so this enables clicking or long-pressing on buttons.
561 // Pointer does not move.
562 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
563 PRESS,
564
565 // Exactly two fingers moving in the same direction, button is not pressed.
566 // Pointer does not move.
567 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
568 // follows the midpoint between both fingers.
569 SWIPE,
570
571 // Two or more fingers moving in arbitrary directions, button is not pressed.
572 // Pointer does not move.
573 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
574 // each finger individually relative to the initial centroid of the finger.
575 FREEFORM,
576
577 // Waiting for quiet time to end before starting the next gesture.
578 QUIET,
579 };
580
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800581 // When a gesture is sent to an unfocused window, return true if it can bring that window
582 // into focus, false otherwise.
583 static bool canGestureAffectWindowFocus(Mode mode) {
584 switch (mode) {
585 case Mode::TAP:
586 case Mode::TAP_DRAG:
587 case Mode::BUTTON_CLICK_OR_DRAG:
588 // Taps can affect window focus.
589 return true;
590 case Mode::FREEFORM:
591 case Mode::HOVER:
592 case Mode::NEUTRAL:
593 case Mode::PRESS:
594 case Mode::QUIET:
595 case Mode::SWIPE:
596 // Most gestures can be performed on an unfocused window, so they should not
597 // not affect window focus.
598 return false;
599 }
600 }
601
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700602 // Time the first finger went down.
603 nsecs_t firstTouchTime;
604
605 // The active pointer id from the raw touch data.
606 int32_t activeTouchId; // -1 if none
607
608 // The active pointer id from the gesture last delivered to the application.
609 int32_t activeGestureId; // -1 if none
610
611 // Pointer coords and ids for the current and previous pointer gesture.
612 Mode currentGestureMode;
613 BitSet32 currentGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000614 IdToIndexArray currentGestureIdToIndex{};
615 PropertiesArray currentGestureProperties{};
616 CoordsArray currentGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700617
618 Mode lastGestureMode;
619 BitSet32 lastGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000620 IdToIndexArray lastGestureIdToIndex{};
621 PropertiesArray lastGestureProperties{};
622 CoordsArray lastGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700623
624 // Time the pointer gesture last went down.
625 nsecs_t downTime;
626
627 // Time when the pointer went down for a TAP.
628 nsecs_t tapDownTime;
629
630 // Time when the pointer went up for a TAP.
631 nsecs_t tapUpTime;
632
633 // Location of initial tap.
634 float tapX, tapY;
635
636 // Time we started waiting for quiescence.
637 nsecs_t quietTime;
638
639 // Reference points for multitouch gestures.
640 float referenceTouchX; // reference touch X/Y coordinates in surface units
641 float referenceTouchY;
642 float referenceGestureX; // reference gesture X/Y coordinates in pixels
643 float referenceGestureY;
644
645 // Distance that each pointer has traveled which has not yet been
646 // subsumed into the reference gesture position.
647 BitSet32 referenceIdBits;
648 struct Delta {
649 float dx, dy;
650 };
651 Delta referenceDeltas[MAX_POINTER_ID + 1];
652
653 // Describes how touch ids are mapped to gesture ids for freeform gestures.
654 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
655
656 // A velocity tracker for determining whether to switch active pointers during drags.
657 VelocityTracker velocityTracker;
658
659 void reset() {
660 firstTouchTime = LLONG_MIN;
661 activeTouchId = -1;
662 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100663 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700664 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100665 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700666 lastGestureIdBits.clear();
667 downTime = 0;
668 velocityTracker.clear();
669 resetTap();
670 resetQuietTime();
671 }
672
673 void resetTap() {
674 tapDownTime = LLONG_MIN;
675 tapUpTime = LLONG_MIN;
676 }
677
678 void resetQuietTime() { quietTime = LLONG_MIN; }
679 } mPointerGesture;
680
681 struct PointerSimple {
682 PointerCoords currentCoords;
683 PointerProperties currentProperties;
684 PointerCoords lastCoords;
685 PointerProperties lastProperties;
686
687 // True if the pointer is down.
688 bool down;
689
690 // True if the pointer is hovering.
691 bool hovering;
692
693 // Time the pointer last went down.
694 nsecs_t downTime;
695
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000696 // Values reported for the last pointer event.
697 uint32_t source;
698 int32_t displayId;
699 float lastCursorX;
700 float lastCursorY;
701
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700702 void reset() {
703 currentCoords.clear();
704 currentProperties.clear();
705 lastCoords.clear();
706 lastProperties.clear();
707 down = false;
708 hovering = false;
709 downTime = 0;
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000710 source = 0;
711 displayId = ADISPLAY_ID_NONE;
712 lastCursorX = 0.f;
713 lastCursorY = 0.f;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700714 }
715 } mPointerSimple;
716
717 // The pointer and scroll velocity controls.
718 VelocityControl mPointerVelocityControl;
719 VelocityControl mWheelXVelocityControl;
720 VelocityControl mWheelYVelocityControl;
721
722 std::optional<DisplayViewport> findViewport();
723
724 void resetExternalStylus();
725 void clearStylusDataPendingFlags();
726
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800727 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800728 void initializeOrientedRanges();
729 void initializeSizeRanges();
730
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700731 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700732
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700733 [[nodiscard]] std::list<NotifyArgs> consumeRawTouches(nsecs_t when, nsecs_t readTime,
734 uint32_t policyFlags, bool& outConsumed);
735 [[nodiscard]] std::list<NotifyArgs> processRawTouches(bool timeout);
736 [[nodiscard]] std::list<NotifyArgs> cookAndDispatch(nsecs_t when, nsecs_t readTime);
737 [[nodiscard]] NotifyKeyArgs dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
738 uint32_t policyFlags, int32_t keyEventAction,
739 int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700740
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700741 [[nodiscard]] std::list<NotifyArgs> dispatchTouches(nsecs_t when, nsecs_t readTime,
742 uint32_t policyFlags);
743 [[nodiscard]] std::list<NotifyArgs> dispatchHoverExit(nsecs_t when, nsecs_t readTime,
744 uint32_t policyFlags);
745 [[nodiscard]] std::list<NotifyArgs> dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
746 uint32_t policyFlags);
747 [[nodiscard]] std::list<NotifyArgs> dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
748 uint32_t policyFlags);
749 [[nodiscard]] std::list<NotifyArgs> dispatchButtonPress(nsecs_t when, nsecs_t readTime,
750 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700751 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
752 void cookPointerData();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700753 [[nodiscard]] std::list<NotifyArgs> abortTouches(nsecs_t when, nsecs_t readTime,
754 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700755
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700756 [[nodiscard]] std::list<NotifyArgs> dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
757 uint32_t policyFlags,
758 PointerUsage pointerUsage);
759 [[nodiscard]] std::list<NotifyArgs> abortPointerUsage(nsecs_t when, nsecs_t readTime,
760 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700761
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700762 [[nodiscard]] std::list<NotifyArgs> dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
763 uint32_t policyFlags,
764 bool isTimeout);
765 [[nodiscard]] std::list<NotifyArgs> abortPointerGestures(nsecs_t when, nsecs_t readTime,
766 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700767 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
768 bool* outFinishPreviousGesture, bool isTimeout);
769
Harry Cuttsbea6ce52022-10-14 15:17:30 +0000770 // Returns true if we're in a period of "quiet time" when touchpad gestures should be ignored.
771 bool checkForTouchpadQuietTime(nsecs_t when);
772
773 std::pair<int32_t, float> getFastestFinger();
774
775 void prepareMultiFingerPointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
776 bool* outFinishPreviousGesture);
777
Harry Cutts714d1ad2022-08-24 16:36:43 +0000778 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
779 // between the last and current events. Uses a relative motion.
780 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
781
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700782 [[nodiscard]] std::list<NotifyArgs> dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
783 uint32_t policyFlags);
784 [[nodiscard]] std::list<NotifyArgs> abortPointerStylus(nsecs_t when, nsecs_t readTime,
785 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700786
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700787 [[nodiscard]] std::list<NotifyArgs> dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
788 uint32_t policyFlags);
789 [[nodiscard]] std::list<NotifyArgs> abortPointerMouse(nsecs_t when, nsecs_t readTime,
790 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700791
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700792 [[nodiscard]] std::list<NotifyArgs> dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
793 uint32_t policyFlags, bool down,
794 bool hovering);
795 [[nodiscard]] std::list<NotifyArgs> abortPointerSimple(nsecs_t when, nsecs_t readTime,
796 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700797
798 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 Pradhan1728b212021-10-19 16:00:03 -0700824 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700825};
826
827} // namespace android