blob: efbe885a8c742733dcb6f1a04e9ae9b05f2cd201 [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;
365 int64_t mExternalStylusId;
366 nsecs_t mExternalStylusFusionTimeout;
367 bool mExternalStylusDataPending;
Prabir Pradhan124ea442022-10-28 20:27:44 +0000368 // A subset of the buttons in mCurrentRawState that came from an external stylus.
369 int32_t mExternalStylusButtonsApplied;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700370
371 // True if we sent a HOVER_ENTER event.
372 bool mSentHoverEnter;
373
374 // Have we assigned pointer IDs for this stream
375 bool mHavePointerIds;
376
377 // Is the current stream of direct touch events aborted
378 bool mCurrentMotionAborted;
379
380 // The time the primary pointer last went down.
381 nsecs_t mDownTime;
382
383 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100384 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700385
386 std::vector<VirtualKey> mVirtualKeys;
387
388 virtual void configureParameters();
389 virtual void dumpParameters(std::string& dump);
390 virtual void configureRawPointerAxes();
391 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700392 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
393 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394 virtual void configureVirtualKeys();
395 virtual void dumpVirtualKeys(std::string& dump);
396 virtual void parseCalibration();
397 virtual void resolveCalibration();
398 virtual void dumpCalibration(std::string& dump);
399 virtual void updateAffineTransformation();
400 virtual void dumpAffineTransformation(std::string& dump);
401 virtual void resolveExternalStylusPresence();
402 virtual bool hasStylus() const = 0;
403 virtual bool hasExternalStylus() const;
404
405 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
406
407private:
408 // The current viewport.
409 // The components of the viewport are specified in the display's rotated orientation.
410 DisplayViewport mViewport;
411
Prabir Pradhan1728b212021-10-19 16:00:03 -0700412 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700413 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700414 int32_t mDisplayWidth;
415 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800416
Prabir Pradhan1728b212021-10-19 16:00:03 -0700417 // The physical frame is the rectangle in the display's coordinate space that maps to the
418 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700419 int32_t mPhysicalWidth;
420 int32_t mPhysicalHeight;
421 int32_t mPhysicalLeft;
422 int32_t mPhysicalTop;
423
Prabir Pradhan1728b212021-10-19 16:00:03 -0700424 // The orientation of the input device relative to that of the display panel. It specifies
425 // the rotation of the input device coordinates required to produce the display panel
426 // orientation, so it will depend on whether the device is orientation aware.
427 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700428
429 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430 float mXScale;
431 float mXPrecision;
432
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 float mYScale;
434 float mYPrecision;
435
436 float mGeometricScale;
437
438 float mPressureScale;
439
440 float mSizeScale;
441
442 float mOrientationScale;
443
444 float mDistanceScale;
445
446 bool mHaveTilt;
447 float mTiltXCenter;
448 float mTiltXScale;
449 float mTiltYCenter;
450 float mTiltYScale;
451
452 bool mExternalStylusConnected;
453
454 // Oriented motion ranges for input device info.
455 struct OrientedRanges {
456 InputDeviceInfo::MotionRange x;
457 InputDeviceInfo::MotionRange y;
458 InputDeviceInfo::MotionRange pressure;
459
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700460 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700462 std::optional<InputDeviceInfo::MotionRange> touchMajor;
463 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700464
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700465 std::optional<InputDeviceInfo::MotionRange> toolMajor;
466 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700468 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700469
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700470 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700471
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700472 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473
474 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700475 size = std::nullopt;
476 touchMajor = std::nullopt;
477 touchMinor = std::nullopt;
478 toolMajor = std::nullopt;
479 toolMinor = std::nullopt;
480 orientation = std::nullopt;
481 distance = std::nullopt;
482 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700483 }
484 } mOrientedRanges;
485
486 // Oriented dimensions and precision.
487 float mOrientedXPrecision;
488 float mOrientedYPrecision;
489
490 struct CurrentVirtualKeyState {
491 bool down;
492 bool ignored;
493 nsecs_t downTime;
494 int32_t keyCode;
495 int32_t scanCode;
496 } mCurrentVirtualKey;
497
498 // Scale factor for gesture or mouse based pointer movements.
499 float mPointerXMovementScale;
500 float mPointerYMovementScale;
501
502 // Scale factor for gesture based zooming and other freeform motions.
503 float mPointerXZoomScale;
504 float mPointerYZoomScale;
505
HQ Liue6983c72022-04-19 22:14:56 +0000506 // The maximum swipe width between pointers to detect a swipe gesture
507 // in the number of pixels.Touches that are wider than this are translated
508 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700509 float mPointerGestureMaxSwipeWidth;
510
511 struct PointerDistanceHeapElement {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000512 uint32_t currentPointerIndex : 8 {};
513 uint32_t lastPointerIndex : 8 {};
514 uint64_t distance : 48 {}; // squared distance
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700515 };
516
Michael Wright227c5542020-07-02 18:30:52 +0100517 enum class PointerUsage {
518 NONE,
519 GESTURES,
520 STYLUS,
521 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700522 };
523 PointerUsage mPointerUsage;
524
525 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100526 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700527 // No fingers, button is not pressed.
528 // Nothing happening.
529 NEUTRAL,
530
531 // No fingers, button is not pressed.
532 // Tap detected.
533 // Emits DOWN and UP events at the pointer location.
534 TAP,
535
536 // Exactly one finger dragging following a tap.
537 // Pointer follows the active finger.
538 // Emits DOWN, MOVE and UP events at the pointer location.
539 //
540 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
541 TAP_DRAG,
542
543 // Button is pressed.
544 // Pointer follows the active finger if there is one. Other fingers are ignored.
545 // Emits DOWN, MOVE and UP events at the pointer location.
546 BUTTON_CLICK_OR_DRAG,
547
548 // Exactly one finger, button is not pressed.
549 // Pointer follows the active finger.
550 // Emits HOVER_MOVE events at the pointer location.
551 //
552 // Detect taps when the finger goes up while in HOVER mode.
553 HOVER,
554
555 // Exactly two fingers but neither have moved enough to clearly indicate
556 // whether a swipe or freeform gesture was intended. We consider the
557 // pointer to be pressed so this enables clicking or long-pressing on buttons.
558 // Pointer does not move.
559 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
560 PRESS,
561
562 // Exactly two fingers moving in the same direction, button is not pressed.
563 // Pointer does not move.
564 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
565 // follows the midpoint between both fingers.
566 SWIPE,
567
568 // Two or more fingers moving in arbitrary directions, button is not pressed.
569 // Pointer does not move.
570 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
571 // each finger individually relative to the initial centroid of the finger.
572 FREEFORM,
573
574 // Waiting for quiet time to end before starting the next gesture.
575 QUIET,
576 };
577
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800578 // When a gesture is sent to an unfocused window, return true if it can bring that window
579 // into focus, false otherwise.
580 static bool canGestureAffectWindowFocus(Mode mode) {
581 switch (mode) {
582 case Mode::TAP:
583 case Mode::TAP_DRAG:
584 case Mode::BUTTON_CLICK_OR_DRAG:
585 // Taps can affect window focus.
586 return true;
587 case Mode::FREEFORM:
588 case Mode::HOVER:
589 case Mode::NEUTRAL:
590 case Mode::PRESS:
591 case Mode::QUIET:
592 case Mode::SWIPE:
593 // Most gestures can be performed on an unfocused window, so they should not
594 // not affect window focus.
595 return false;
596 }
597 }
598
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700599 // Time the first finger went down.
600 nsecs_t firstTouchTime;
601
602 // The active pointer id from the raw touch data.
603 int32_t activeTouchId; // -1 if none
604
605 // The active pointer id from the gesture last delivered to the application.
606 int32_t activeGestureId; // -1 if none
607
608 // Pointer coords and ids for the current and previous pointer gesture.
609 Mode currentGestureMode;
610 BitSet32 currentGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000611 IdToIndexArray currentGestureIdToIndex{};
612 PropertiesArray currentGestureProperties{};
613 CoordsArray currentGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700614
615 Mode lastGestureMode;
616 BitSet32 lastGestureIdBits;
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000617 IdToIndexArray lastGestureIdToIndex{};
618 PropertiesArray lastGestureProperties{};
619 CoordsArray lastGestureCoords{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700620
621 // Time the pointer gesture last went down.
622 nsecs_t downTime;
623
624 // Time when the pointer went down for a TAP.
625 nsecs_t tapDownTime;
626
627 // Time when the pointer went up for a TAP.
628 nsecs_t tapUpTime;
629
630 // Location of initial tap.
631 float tapX, tapY;
632
633 // Time we started waiting for quiescence.
634 nsecs_t quietTime;
635
636 // Reference points for multitouch gestures.
637 float referenceTouchX; // reference touch X/Y coordinates in surface units
638 float referenceTouchY;
639 float referenceGestureX; // reference gesture X/Y coordinates in pixels
640 float referenceGestureY;
641
642 // Distance that each pointer has traveled which has not yet been
643 // subsumed into the reference gesture position.
644 BitSet32 referenceIdBits;
645 struct Delta {
646 float dx, dy;
647 };
648 Delta referenceDeltas[MAX_POINTER_ID + 1];
649
650 // Describes how touch ids are mapped to gesture ids for freeform gestures.
651 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
652
653 // A velocity tracker for determining whether to switch active pointers during drags.
654 VelocityTracker velocityTracker;
655
656 void reset() {
657 firstTouchTime = LLONG_MIN;
658 activeTouchId = -1;
659 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100660 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700661 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100662 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700663 lastGestureIdBits.clear();
664 downTime = 0;
665 velocityTracker.clear();
666 resetTap();
667 resetQuietTime();
668 }
669
670 void resetTap() {
671 tapDownTime = LLONG_MIN;
672 tapUpTime = LLONG_MIN;
673 }
674
675 void resetQuietTime() { quietTime = LLONG_MIN; }
676 } mPointerGesture;
677
678 struct PointerSimple {
679 PointerCoords currentCoords;
680 PointerProperties currentProperties;
681 PointerCoords lastCoords;
682 PointerProperties lastProperties;
683
684 // True if the pointer is down.
685 bool down;
686
687 // True if the pointer is hovering.
688 bool hovering;
689
690 // Time the pointer last went down.
691 nsecs_t downTime;
692
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000693 // Values reported for the last pointer event.
694 uint32_t source;
695 int32_t displayId;
696 float lastCursorX;
697 float lastCursorY;
698
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700699 void reset() {
700 currentCoords.clear();
701 currentProperties.clear();
702 lastCoords.clear();
703 lastProperties.clear();
704 down = false;
705 hovering = false;
706 downTime = 0;
Prabir Pradhanb80b6c02022-11-02 20:05:13 +0000707 source = 0;
708 displayId = ADISPLAY_ID_NONE;
709 lastCursorX = 0.f;
710 lastCursorY = 0.f;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700711 }
712 } mPointerSimple;
713
714 // The pointer and scroll velocity controls.
715 VelocityControl mPointerVelocityControl;
716 VelocityControl mWheelXVelocityControl;
717 VelocityControl mWheelYVelocityControl;
718
719 std::optional<DisplayViewport> findViewport();
720
721 void resetExternalStylus();
722 void clearStylusDataPendingFlags();
723
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800724 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800725 void initializeOrientedRanges();
726 void initializeSizeRanges();
727
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700728 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700729
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700730 [[nodiscard]] std::list<NotifyArgs> consumeRawTouches(nsecs_t when, nsecs_t readTime,
731 uint32_t policyFlags, bool& outConsumed);
732 [[nodiscard]] std::list<NotifyArgs> processRawTouches(bool timeout);
733 [[nodiscard]] std::list<NotifyArgs> cookAndDispatch(nsecs_t when, nsecs_t readTime);
734 [[nodiscard]] NotifyKeyArgs dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
735 uint32_t policyFlags, int32_t keyEventAction,
736 int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700737
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700738 [[nodiscard]] std::list<NotifyArgs> dispatchTouches(nsecs_t when, nsecs_t readTime,
739 uint32_t policyFlags);
740 [[nodiscard]] std::list<NotifyArgs> dispatchHoverExit(nsecs_t when, nsecs_t readTime,
741 uint32_t policyFlags);
742 [[nodiscard]] std::list<NotifyArgs> dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
743 uint32_t policyFlags);
744 [[nodiscard]] std::list<NotifyArgs> dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
745 uint32_t policyFlags);
746 [[nodiscard]] std::list<NotifyArgs> dispatchButtonPress(nsecs_t when, nsecs_t readTime,
747 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700748 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
749 void cookPointerData();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700750 [[nodiscard]] std::list<NotifyArgs> abortTouches(nsecs_t when, nsecs_t readTime,
751 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700752
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700753 [[nodiscard]] std::list<NotifyArgs> dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
754 uint32_t policyFlags,
755 PointerUsage pointerUsage);
756 [[nodiscard]] std::list<NotifyArgs> abortPointerUsage(nsecs_t when, nsecs_t readTime,
757 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700758
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700759 [[nodiscard]] std::list<NotifyArgs> dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
760 uint32_t policyFlags,
761 bool isTimeout);
762 [[nodiscard]] std::list<NotifyArgs> abortPointerGestures(nsecs_t when, nsecs_t readTime,
763 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700764 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
765 bool* outFinishPreviousGesture, bool isTimeout);
766
Harry Cuttsbea6ce52022-10-14 15:17:30 +0000767 // Returns true if we're in a period of "quiet time" when touchpad gestures should be ignored.
768 bool checkForTouchpadQuietTime(nsecs_t when);
769
770 std::pair<int32_t, float> getFastestFinger();
771
772 void prepareMultiFingerPointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
773 bool* outFinishPreviousGesture);
774
Harry Cutts714d1ad2022-08-24 16:36:43 +0000775 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
776 // between the last and current events. Uses a relative motion.
777 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
778
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700779 [[nodiscard]] std::list<NotifyArgs> dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
780 uint32_t policyFlags);
781 [[nodiscard]] std::list<NotifyArgs> abortPointerStylus(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> dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
785 uint32_t policyFlags);
786 [[nodiscard]] std::list<NotifyArgs> abortPointerMouse(nsecs_t when, nsecs_t readTime,
787 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700788
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700789 [[nodiscard]] std::list<NotifyArgs> dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
790 uint32_t policyFlags, bool down,
791 bool hovering);
792 [[nodiscard]] std::list<NotifyArgs> abortPointerSimple(nsecs_t when, nsecs_t readTime,
793 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700794
795 bool assignExternalStylusId(const RawState& state, bool timeout);
796 void applyExternalStylusButtonState(nsecs_t when);
797 void applyExternalStylusTouchState(nsecs_t when);
798
799 // Dispatches a motion event.
800 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
801 // method will take care of setting the index and transmuting the action to DOWN or UP
802 // it is the first / last pointer to go down / up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700803 [[nodiscard]] NotifyMotionArgs dispatchMotion(
804 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
805 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
Prabir Pradhand6ccedb2022-09-27 21:04:06 +0000806 int32_t edgeFlags, const PropertiesArray& properties, const CoordsArray& coords,
807 const IdToIndexArray& idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700808 float yPrecision, nsecs_t downTime, MotionClassification classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700809
Garfield Tanc734e4f2021-01-15 20:01:39 -0800810 // Returns if this touch device is a touch screen with an associated display.
811 bool isTouchScreen();
812 // Updates touch spots if they are enabled. Should only be used when this device is a
813 // touchscreen.
814 void updateTouchSpots();
815
Prabir Pradhan1728b212021-10-19 16:00:03 -0700816 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700817 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
818
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000819 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700820
Prabir Pradhan1728b212021-10-19 16:00:03 -0700821 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700822};
823
824} // namespace android