blob: 50f30c824e6788ab5534a11c3c0693f3582e3519 [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
30/* Raw axis information from the driver. */
31struct RawPointerAxes {
32 RawAbsoluteAxisInfo x;
33 RawAbsoluteAxisInfo y;
34 RawAbsoluteAxisInfo pressure;
35 RawAbsoluteAxisInfo touchMajor;
36 RawAbsoluteAxisInfo touchMinor;
37 RawAbsoluteAxisInfo toolMajor;
38 RawAbsoluteAxisInfo toolMinor;
39 RawAbsoluteAxisInfo orientation;
40 RawAbsoluteAxisInfo distance;
41 RawAbsoluteAxisInfo tiltX;
42 RawAbsoluteAxisInfo tiltY;
43 RawAbsoluteAxisInfo trackingId;
44 RawAbsoluteAxisInfo slot;
45
46 RawPointerAxes();
47 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
48 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
49 void clear();
50};
51
52/* Raw data for a collection of pointers including a pointer id mapping table. */
53struct RawPointerData {
54 struct Pointer {
55 uint32_t id;
56 int32_t x;
57 int32_t y;
58 int32_t pressure;
59 int32_t touchMajor;
60 int32_t touchMinor;
61 int32_t toolMajor;
62 int32_t toolMinor;
63 int32_t orientation;
64 int32_t distance;
65 int32_t tiltX;
66 int32_t tiltY;
67 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
68 bool isHovering;
69 };
70
71 uint32_t pointerCount;
72 Pointer pointers[MAX_POINTERS];
arthurhungcc7f9802020-04-30 17:55:40 +080073 BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074 uint32_t idToIndex[MAX_POINTER_ID + 1];
75
76 RawPointerData();
77 void clear();
78 void copyFrom(const RawPointerData& other);
79 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
80
81 inline void markIdBit(uint32_t id, bool isHovering) {
82 if (isHovering) {
83 hoveringIdBits.markBit(id);
84 } else {
85 touchingIdBits.markBit(id);
86 }
87 }
88
89 inline void clearIdBits() {
90 hoveringIdBits.clear();
91 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +080092 canceledIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 }
94
95 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
96
97 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
98};
99
100/* Cooked data for a collection of pointers including a pointer id mapping table. */
101struct CookedPointerData {
102 uint32_t pointerCount;
103 PointerProperties pointerProperties[MAX_POINTERS];
104 PointerCoords pointerCoords[MAX_POINTERS];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000105 BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits, validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 uint32_t idToIndex[MAX_POINTER_ID + 1];
107
108 CookedPointerData();
109 void clear();
110 void copyFrom(const CookedPointerData& other);
111
112 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
113 return pointerCoords[idToIndex[id]];
114 }
115
116 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
117 return pointerCoords[idToIndex[id]];
118 }
119
120 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
121 return pointerProperties[idToIndex[id]];
122 }
123
124 inline bool isHovering(uint32_t pointerIndex) const {
125 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
126 }
127
128 inline bool isTouching(uint32_t pointerIndex) const {
129 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
130 }
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000131
132 inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133};
134
135class TouchInputMapper : public InputMapper {
136public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800137 explicit TouchInputMapper(InputDeviceContext& deviceContext);
Michael Wright227c5542020-07-02 18:30:52 +0100138 ~TouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139
Philip Junker4af3b3d2021-12-14 10:36:55 +0100140 uint32_t getSources() const override;
Michael Wright227c5542020-07-02 18:30:52 +0100141 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
142 void dump(std::string& dump) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700143 [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
144 const InputReaderConfiguration* config,
145 uint32_t changes) override;
146 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
147 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700148
Michael Wright227c5542020-07-02 18:30:52 +0100149 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
150 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700151 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
Michael Wright227c5542020-07-02 18:30:52 +0100152 uint8_t* outFlags) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700153
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700154 [[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) override;
155 [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when) override;
156 [[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(
157 const StylusState& state) override;
Michael Wright227c5542020-07-02 18:30:52 +0100158 std::optional<int32_t> getAssociatedDisplayId() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159
160protected:
161 CursorButtonAccumulator mCursorButtonAccumulator;
162 CursorScrollAccumulator mCursorScrollAccumulator;
163 TouchButtonAccumulator mTouchButtonAccumulator;
164
165 struct VirtualKey {
166 int32_t keyCode;
167 int32_t scanCode;
168 uint32_t flags;
169
170 // computed hit box, specified in touch screen coords based on known display size
171 int32_t hitLeft;
172 int32_t hitTop;
173 int32_t hitRight;
174 int32_t hitBottom;
175
176 inline bool isHit(int32_t x, int32_t y) const {
177 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
178 }
179 };
180
181 // Input sources and device mode.
182 uint32_t mSource;
183
Michael Wright227c5542020-07-02 18:30:52 +0100184 enum class DeviceMode {
185 DISABLED, // input is disabled
186 DIRECT, // direct mapping (touchscreen)
187 UNSCALED, // unscaled mapping (touchpad)
188 NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
189 POINTER, // pointer mapping (pointer)
Dominik Laskowski75788452021-02-09 18:51:25 -0800190
191 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700192 };
193 DeviceMode mDeviceMode;
194
195 // The reader's configuration.
196 InputReaderConfiguration mConfig;
197
198 // Immutable configuration parameters.
199 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100200 enum class DeviceType {
201 TOUCH_SCREEN,
Michael Wright227c5542020-07-02 18:30:52 +0100202 TOUCH_NAVIGATION,
203 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800204
205 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700206 };
207
208 DeviceType deviceType;
209 bool hasAssociatedDisplay;
210 bool associatedDisplayIsExternal;
211 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700212
213 enum class Orientation : int32_t {
214 ORIENTATION_0 = DISPLAY_ORIENTATION_0,
215 ORIENTATION_90 = DISPLAY_ORIENTATION_90,
216 ORIENTATION_180 = DISPLAY_ORIENTATION_180,
217 ORIENTATION_270 = DISPLAY_ORIENTATION_270,
Dominik Laskowski75788452021-02-09 18:51:25 -0800218
219 ftl_last = ORIENTATION_270
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700220 };
221 Orientation orientation;
222
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700223 bool hasButtonUnderPad;
224 std::string uniqueDisplayId;
225
Michael Wright227c5542020-07-02 18:30:52 +0100226 enum class GestureMode {
227 SINGLE_TOUCH,
228 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800229
230 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700231 };
232 GestureMode gestureMode;
233
234 bool wake;
235 } mParameters;
236
237 // Immutable calibration parameters in parsed form.
238 struct Calibration {
239 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100240 enum class SizeCalibration {
241 DEFAULT,
242 NONE,
243 GEOMETRIC,
244 DIAMETER,
245 BOX,
246 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800247 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700248 };
249
250 SizeCalibration sizeCalibration;
251
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700252 std::optional<float> sizeScale;
253 std::optional<float> sizeBias;
254 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255
256 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100257 enum class PressureCalibration {
258 DEFAULT,
259 NONE,
260 PHYSICAL,
261 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700262 };
263
264 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700265 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266
267 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100268 enum class OrientationCalibration {
269 DEFAULT,
270 NONE,
271 INTERPOLATED,
272 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700273 };
274
275 OrientationCalibration orientationCalibration;
276
277 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100278 enum class DistanceCalibration {
279 DEFAULT,
280 NONE,
281 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700282 };
283
284 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700285 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700286
Michael Wright227c5542020-07-02 18:30:52 +0100287 enum class CoverageCalibration {
288 DEFAULT,
289 NONE,
290 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700291 };
292
293 CoverageCalibration coverageCalibration;
294
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700295 inline void applySizeScaleAndBias(float& outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700296 if (sizeScale) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700297 outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700298 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700299 if (sizeBias) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700300 outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700301 }
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700302 if (outSize < 0) {
303 outSize = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700304 }
305 }
306 } mCalibration;
307
308 // Affine location transformation/calibration
309 struct TouchAffineTransformation mAffineTransform;
310
311 RawPointerAxes mRawPointerAxes;
312
313 struct RawState {
314 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000315 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316
317 // Raw pointer sample data.
318 RawPointerData rawPointerData;
319
320 int32_t buttonState;
321
322 // Scroll state.
323 int32_t rawVScroll;
324 int32_t rawHScroll;
325
Prabir Pradhanafabcde2022-09-27 19:32:43 +0000326 explicit inline RawState() { clear(); }
327
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700328 void copyFrom(const RawState& other) {
329 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000330 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700331 rawPointerData.copyFrom(other.rawPointerData);
332 buttonState = other.buttonState;
333 rawVScroll = other.rawVScroll;
334 rawHScroll = other.rawHScroll;
335 }
336
337 void clear() {
338 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000339 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 rawPointerData.clear();
341 buttonState = 0;
342 rawVScroll = 0;
343 rawHScroll = 0;
344 }
345 };
346
347 struct CookedState {
348 // Cooked pointer sample data.
349 CookedPointerData cookedPointerData;
350
351 // Id bits used to differentiate fingers, stylus and mouse tools.
352 BitSet32 fingerIdBits;
353 BitSet32 stylusIdBits;
354 BitSet32 mouseIdBits;
355
356 int32_t buttonState;
357
358 void copyFrom(const CookedState& other) {
359 cookedPointerData.copyFrom(other.cookedPointerData);
360 fingerIdBits = other.fingerIdBits;
361 stylusIdBits = other.stylusIdBits;
362 mouseIdBits = other.mouseIdBits;
363 buttonState = other.buttonState;
364 }
365
366 void clear() {
367 cookedPointerData.clear();
368 fingerIdBits.clear();
369 stylusIdBits.clear();
370 mouseIdBits.clear();
371 buttonState = 0;
372 }
373 };
374
375 std::vector<RawState> mRawStatesPending;
376 RawState mCurrentRawState;
377 CookedState mCurrentCookedState;
378 RawState mLastRawState;
379 CookedState mLastCookedState;
380
381 // State provided by an external stylus
382 StylusState mExternalStylusState;
383 int64_t mExternalStylusId;
384 nsecs_t mExternalStylusFusionTimeout;
385 bool mExternalStylusDataPending;
386
387 // True if we sent a HOVER_ENTER event.
388 bool mSentHoverEnter;
389
390 // Have we assigned pointer IDs for this stream
391 bool mHavePointerIds;
392
393 // Is the current stream of direct touch events aborted
394 bool mCurrentMotionAborted;
395
396 // The time the primary pointer last went down.
397 nsecs_t mDownTime;
398
399 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100400 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700401
402 std::vector<VirtualKey> mVirtualKeys;
403
404 virtual void configureParameters();
405 virtual void dumpParameters(std::string& dump);
406 virtual void configureRawPointerAxes();
407 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700408 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
409 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700410 virtual void configureVirtualKeys();
411 virtual void dumpVirtualKeys(std::string& dump);
412 virtual void parseCalibration();
413 virtual void resolveCalibration();
414 virtual void dumpCalibration(std::string& dump);
415 virtual void updateAffineTransformation();
416 virtual void dumpAffineTransformation(std::string& dump);
417 virtual void resolveExternalStylusPresence();
418 virtual bool hasStylus() const = 0;
419 virtual bool hasExternalStylus() const;
420
421 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
422
423private:
424 // The current viewport.
425 // The components of the viewport are specified in the display's rotated orientation.
426 DisplayViewport mViewport;
427
Prabir Pradhan1728b212021-10-19 16:00:03 -0700428 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700430 int32_t mDisplayWidth;
431 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800432
Prabir Pradhan1728b212021-10-19 16:00:03 -0700433 // The physical frame is the rectangle in the display's coordinate space that maps to the
434 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700435 int32_t mPhysicalWidth;
436 int32_t mPhysicalHeight;
437 int32_t mPhysicalLeft;
438 int32_t mPhysicalTop;
439
Prabir Pradhan1728b212021-10-19 16:00:03 -0700440 // The orientation of the input device relative to that of the display panel. It specifies
441 // the rotation of the input device coordinates required to produce the display panel
442 // orientation, so it will depend on whether the device is orientation aware.
443 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444
445 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446 float mXScale;
447 float mXPrecision;
448
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700449 float mYScale;
450 float mYPrecision;
451
452 float mGeometricScale;
453
454 float mPressureScale;
455
456 float mSizeScale;
457
458 float mOrientationScale;
459
460 float mDistanceScale;
461
462 bool mHaveTilt;
463 float mTiltXCenter;
464 float mTiltXScale;
465 float mTiltYCenter;
466 float mTiltYScale;
467
468 bool mExternalStylusConnected;
469
470 // Oriented motion ranges for input device info.
471 struct OrientedRanges {
472 InputDeviceInfo::MotionRange x;
473 InputDeviceInfo::MotionRange y;
474 InputDeviceInfo::MotionRange pressure;
475
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700476 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700477
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700478 std::optional<InputDeviceInfo::MotionRange> touchMajor;
479 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700480
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700481 std::optional<InputDeviceInfo::MotionRange> toolMajor;
482 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700483
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700484 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700485
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700486 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700487
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700488 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700489
490 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700491 size = std::nullopt;
492 touchMajor = std::nullopt;
493 touchMinor = std::nullopt;
494 toolMajor = std::nullopt;
495 toolMinor = std::nullopt;
496 orientation = std::nullopt;
497 distance = std::nullopt;
498 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700499 }
500 } mOrientedRanges;
501
502 // Oriented dimensions and precision.
503 float mOrientedXPrecision;
504 float mOrientedYPrecision;
505
506 struct CurrentVirtualKeyState {
507 bool down;
508 bool ignored;
509 nsecs_t downTime;
510 int32_t keyCode;
511 int32_t scanCode;
512 } mCurrentVirtualKey;
513
514 // Scale factor for gesture or mouse based pointer movements.
515 float mPointerXMovementScale;
516 float mPointerYMovementScale;
517
518 // Scale factor for gesture based zooming and other freeform motions.
519 float mPointerXZoomScale;
520 float mPointerYZoomScale;
521
HQ Liue6983c72022-04-19 22:14:56 +0000522 // The maximum swipe width between pointers to detect a swipe gesture
523 // in the number of pixels.Touches that are wider than this are translated
524 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700525 float mPointerGestureMaxSwipeWidth;
526
527 struct PointerDistanceHeapElement {
528 uint32_t currentPointerIndex : 8;
529 uint32_t lastPointerIndex : 8;
530 uint64_t distance : 48; // squared distance
531 };
532
Michael Wright227c5542020-07-02 18:30:52 +0100533 enum class PointerUsage {
534 NONE,
535 GESTURES,
536 STYLUS,
537 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700538 };
539 PointerUsage mPointerUsage;
540
541 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100542 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700543 // No fingers, button is not pressed.
544 // Nothing happening.
545 NEUTRAL,
546
547 // No fingers, button is not pressed.
548 // Tap detected.
549 // Emits DOWN and UP events at the pointer location.
550 TAP,
551
552 // Exactly one finger dragging following a tap.
553 // Pointer follows the active finger.
554 // Emits DOWN, MOVE and UP events at the pointer location.
555 //
556 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
557 TAP_DRAG,
558
559 // Button is pressed.
560 // Pointer follows the active finger if there is one. Other fingers are ignored.
561 // Emits DOWN, MOVE and UP events at the pointer location.
562 BUTTON_CLICK_OR_DRAG,
563
564 // Exactly one finger, button is not pressed.
565 // Pointer follows the active finger.
566 // Emits HOVER_MOVE events at the pointer location.
567 //
568 // Detect taps when the finger goes up while in HOVER mode.
569 HOVER,
570
571 // Exactly two fingers but neither have moved enough to clearly indicate
572 // whether a swipe or freeform gesture was intended. We consider the
573 // pointer to be pressed so this enables clicking or long-pressing on buttons.
574 // Pointer does not move.
575 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
576 PRESS,
577
578 // Exactly two fingers moving in the same direction, button is not pressed.
579 // Pointer does not move.
580 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
581 // follows the midpoint between both fingers.
582 SWIPE,
583
584 // Two or more fingers moving in arbitrary directions, button is not pressed.
585 // Pointer does not move.
586 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
587 // each finger individually relative to the initial centroid of the finger.
588 FREEFORM,
589
590 // Waiting for quiet time to end before starting the next gesture.
591 QUIET,
592 };
593
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800594 // When a gesture is sent to an unfocused window, return true if it can bring that window
595 // into focus, false otherwise.
596 static bool canGestureAffectWindowFocus(Mode mode) {
597 switch (mode) {
598 case Mode::TAP:
599 case Mode::TAP_DRAG:
600 case Mode::BUTTON_CLICK_OR_DRAG:
601 // Taps can affect window focus.
602 return true;
603 case Mode::FREEFORM:
604 case Mode::HOVER:
605 case Mode::NEUTRAL:
606 case Mode::PRESS:
607 case Mode::QUIET:
608 case Mode::SWIPE:
609 // Most gestures can be performed on an unfocused window, so they should not
610 // not affect window focus.
611 return false;
612 }
613 }
614
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700615 // Time the first finger went down.
616 nsecs_t firstTouchTime;
617
618 // The active pointer id from the raw touch data.
619 int32_t activeTouchId; // -1 if none
620
621 // The active pointer id from the gesture last delivered to the application.
622 int32_t activeGestureId; // -1 if none
623
624 // Pointer coords and ids for the current and previous pointer gesture.
625 Mode currentGestureMode;
626 BitSet32 currentGestureIdBits;
627 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
628 PointerProperties currentGestureProperties[MAX_POINTERS];
629 PointerCoords currentGestureCoords[MAX_POINTERS];
630
631 Mode lastGestureMode;
632 BitSet32 lastGestureIdBits;
633 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
634 PointerProperties lastGestureProperties[MAX_POINTERS];
635 PointerCoords lastGestureCoords[MAX_POINTERS];
636
637 // Time the pointer gesture last went down.
638 nsecs_t downTime;
639
640 // Time when the pointer went down for a TAP.
641 nsecs_t tapDownTime;
642
643 // Time when the pointer went up for a TAP.
644 nsecs_t tapUpTime;
645
646 // Location of initial tap.
647 float tapX, tapY;
648
649 // Time we started waiting for quiescence.
650 nsecs_t quietTime;
651
652 // Reference points for multitouch gestures.
653 float referenceTouchX; // reference touch X/Y coordinates in surface units
654 float referenceTouchY;
655 float referenceGestureX; // reference gesture X/Y coordinates in pixels
656 float referenceGestureY;
657
658 // Distance that each pointer has traveled which has not yet been
659 // subsumed into the reference gesture position.
660 BitSet32 referenceIdBits;
661 struct Delta {
662 float dx, dy;
663 };
664 Delta referenceDeltas[MAX_POINTER_ID + 1];
665
666 // Describes how touch ids are mapped to gesture ids for freeform gestures.
667 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
668
669 // A velocity tracker for determining whether to switch active pointers during drags.
670 VelocityTracker velocityTracker;
671
672 void reset() {
673 firstTouchTime = LLONG_MIN;
674 activeTouchId = -1;
675 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100676 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700677 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100678 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700679 lastGestureIdBits.clear();
680 downTime = 0;
681 velocityTracker.clear();
682 resetTap();
683 resetQuietTime();
684 }
685
686 void resetTap() {
687 tapDownTime = LLONG_MIN;
688 tapUpTime = LLONG_MIN;
689 }
690
691 void resetQuietTime() { quietTime = LLONG_MIN; }
692 } mPointerGesture;
693
694 struct PointerSimple {
695 PointerCoords currentCoords;
696 PointerProperties currentProperties;
697 PointerCoords lastCoords;
698 PointerProperties lastProperties;
699
700 // True if the pointer is down.
701 bool down;
702
703 // True if the pointer is hovering.
704 bool hovering;
705
706 // Time the pointer last went down.
707 nsecs_t downTime;
708
709 void reset() {
710 currentCoords.clear();
711 currentProperties.clear();
712 lastCoords.clear();
713 lastProperties.clear();
714 down = false;
715 hovering = false;
716 downTime = 0;
717 }
718 } mPointerSimple;
719
720 // The pointer and scroll velocity controls.
721 VelocityControl mPointerVelocityControl;
722 VelocityControl mWheelXVelocityControl;
723 VelocityControl mWheelYVelocityControl;
724
725 std::optional<DisplayViewport> findViewport();
726
727 void resetExternalStylus();
728 void clearStylusDataPendingFlags();
729
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800730 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800731 void initializeOrientedRanges();
732 void initializeSizeRanges();
733
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700734 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700735
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700736 [[nodiscard]] std::list<NotifyArgs> consumeRawTouches(nsecs_t when, nsecs_t readTime,
737 uint32_t policyFlags, bool& outConsumed);
738 [[nodiscard]] std::list<NotifyArgs> processRawTouches(bool timeout);
739 [[nodiscard]] std::list<NotifyArgs> cookAndDispatch(nsecs_t when, nsecs_t readTime);
740 [[nodiscard]] NotifyKeyArgs dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
741 uint32_t policyFlags, int32_t keyEventAction,
742 int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700743
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700744 [[nodiscard]] std::list<NotifyArgs> dispatchTouches(nsecs_t when, nsecs_t readTime,
745 uint32_t policyFlags);
746 [[nodiscard]] std::list<NotifyArgs> dispatchHoverExit(nsecs_t when, nsecs_t readTime,
747 uint32_t policyFlags);
748 [[nodiscard]] std::list<NotifyArgs> dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
749 uint32_t policyFlags);
750 [[nodiscard]] std::list<NotifyArgs> dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
751 uint32_t policyFlags);
752 [[nodiscard]] std::list<NotifyArgs> dispatchButtonPress(nsecs_t when, nsecs_t readTime,
753 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700754 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
755 void cookPointerData();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700756 [[nodiscard]] std::list<NotifyArgs> abortTouches(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> dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
760 uint32_t policyFlags,
761 PointerUsage pointerUsage);
762 [[nodiscard]] std::list<NotifyArgs> abortPointerUsage(nsecs_t when, nsecs_t readTime,
763 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700764
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700765 [[nodiscard]] std::list<NotifyArgs> dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
766 uint32_t policyFlags,
767 bool isTimeout);
768 [[nodiscard]] std::list<NotifyArgs> abortPointerGestures(nsecs_t when, nsecs_t readTime,
769 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700770 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
771 bool* outFinishPreviousGesture, bool isTimeout);
772
Harry Cutts714d1ad2022-08-24 16:36:43 +0000773 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
774 // between the last and current events. Uses a relative motion.
775 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
776
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700777 [[nodiscard]] std::list<NotifyArgs> dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
778 uint32_t policyFlags);
779 [[nodiscard]] std::list<NotifyArgs> abortPointerStylus(nsecs_t when, nsecs_t readTime,
780 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700781
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700782 [[nodiscard]] std::list<NotifyArgs> dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
783 uint32_t policyFlags);
784 [[nodiscard]] std::list<NotifyArgs> abortPointerMouse(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> dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
788 uint32_t policyFlags, bool down,
789 bool hovering);
790 [[nodiscard]] std::list<NotifyArgs> abortPointerSimple(nsecs_t when, nsecs_t readTime,
791 uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700792
793 bool assignExternalStylusId(const RawState& state, bool timeout);
794 void applyExternalStylusButtonState(nsecs_t when);
795 void applyExternalStylusTouchState(nsecs_t when);
796
797 // Dispatches a motion event.
798 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
799 // method will take care of setting the index and transmuting the action to DOWN or UP
800 // it is the first / last pointer to go down / up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700801 [[nodiscard]] NotifyMotionArgs dispatchMotion(
802 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
803 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
804 int32_t edgeFlags, const PointerProperties* properties, const PointerCoords* coords,
805 const uint32_t* idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
806 float yPrecision, nsecs_t downTime, MotionClassification classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700807
808 // Updates pointer coords and properties for pointers with specified ids that have moved.
809 // Returns true if any of them changed.
810 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
811 const uint32_t* inIdToIndex, PointerProperties* outProperties,
812 PointerCoords* outCoords, const uint32_t* outIdToIndex,
813 BitSet32 idBits) const;
814
Garfield Tanc734e4f2021-01-15 20:01:39 -0800815 // Returns if this touch device is a touch screen with an associated display.
816 bool isTouchScreen();
817 // Updates touch spots if they are enabled. Should only be used when this device is a
818 // touchscreen.
819 void updateTouchSpots();
820
Prabir Pradhan1728b212021-10-19 16:00:03 -0700821 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700822 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
823
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000824 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700825
826 const char* modeToString(DeviceMode deviceMode);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700827 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700828};
829
830} // namespace android