blob: 7b8f6a1c7c54f3c38169b17b9b908ae11bf87fcf [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
17#ifndef _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H
18#define _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H
19
Michael Wright227c5542020-07-02 18:30:52 +010020#include <stdint.h>
21
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070022#include "CursorButtonAccumulator.h"
23#include "CursorScrollAccumulator.h"
24#include "EventHub.h"
25#include "InputMapper.h"
26#include "InputReaderBase.h"
27#include "TouchButtonAccumulator.h"
28
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029namespace android {
30
31/* Raw axis information from the driver. */
32struct RawPointerAxes {
33 RawAbsoluteAxisInfo x;
34 RawAbsoluteAxisInfo y;
35 RawAbsoluteAxisInfo pressure;
36 RawAbsoluteAxisInfo touchMajor;
37 RawAbsoluteAxisInfo touchMinor;
38 RawAbsoluteAxisInfo toolMajor;
39 RawAbsoluteAxisInfo toolMinor;
40 RawAbsoluteAxisInfo orientation;
41 RawAbsoluteAxisInfo distance;
42 RawAbsoluteAxisInfo tiltX;
43 RawAbsoluteAxisInfo tiltY;
44 RawAbsoluteAxisInfo trackingId;
45 RawAbsoluteAxisInfo slot;
46
47 RawPointerAxes();
48 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
49 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
50 void clear();
51};
52
53/* Raw data for a collection of pointers including a pointer id mapping table. */
54struct RawPointerData {
55 struct Pointer {
56 uint32_t id;
57 int32_t x;
58 int32_t y;
59 int32_t pressure;
60 int32_t touchMajor;
61 int32_t touchMinor;
62 int32_t toolMajor;
63 int32_t toolMinor;
64 int32_t orientation;
65 int32_t distance;
66 int32_t tiltX;
67 int32_t tiltY;
68 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
69 bool isHovering;
70 };
71
72 uint32_t pointerCount;
73 Pointer pointers[MAX_POINTERS];
arthurhungcc7f9802020-04-30 17:55:40 +080074 BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 uint32_t idToIndex[MAX_POINTER_ID + 1];
76
77 RawPointerData();
78 void clear();
79 void copyFrom(const RawPointerData& other);
80 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
81
82 inline void markIdBit(uint32_t id, bool isHovering) {
83 if (isHovering) {
84 hoveringIdBits.markBit(id);
85 } else {
86 touchingIdBits.markBit(id);
87 }
88 }
89
90 inline void clearIdBits() {
91 hoveringIdBits.clear();
92 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +080093 canceledIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 }
95
96 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
97
98 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
99};
100
101/* Cooked data for a collection of pointers including a pointer id mapping table. */
102struct CookedPointerData {
103 uint32_t pointerCount;
104 PointerProperties pointerProperties[MAX_POINTERS];
105 PointerCoords pointerCoords[MAX_POINTERS];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000106 BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits, validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700107 uint32_t idToIndex[MAX_POINTER_ID + 1];
108
109 CookedPointerData();
110 void clear();
111 void copyFrom(const CookedPointerData& other);
112
113 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
114 return pointerCoords[idToIndex[id]];
115 }
116
117 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
118 return pointerCoords[idToIndex[id]];
119 }
120
121 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
122 return pointerProperties[idToIndex[id]];
123 }
124
125 inline bool isHovering(uint32_t pointerIndex) const {
126 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
127 }
128
129 inline bool isTouching(uint32_t pointerIndex) const {
130 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
131 }
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000132
133 inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700134};
135
136class TouchInputMapper : public InputMapper {
137public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800138 explicit TouchInputMapper(InputDeviceContext& deviceContext);
Michael Wright227c5542020-07-02 18:30:52 +0100139 ~TouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140
Philip Junker4af3b3d2021-12-14 10:36:55 +0100141 uint32_t getSources() const override;
Michael Wright227c5542020-07-02 18:30:52 +0100142 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
143 void dump(std::string& dump) override;
144 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) override;
145 void reset(nsecs_t when) override;
146 void process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147
Michael Wright227c5542020-07-02 18:30:52 +0100148 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
149 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700150 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
Michael Wright227c5542020-07-02 18:30:52 +0100151 uint8_t* outFlags) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000153 void cancelTouch(nsecs_t when, nsecs_t readTime) override;
Michael Wright227c5542020-07-02 18:30:52 +0100154 void timeoutExpired(nsecs_t when) override;
155 void updateExternalStylusState(const StylusState& state) override;
156 std::optional<int32_t> getAssociatedDisplayId() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700157
158protected:
159 CursorButtonAccumulator mCursorButtonAccumulator;
160 CursorScrollAccumulator mCursorScrollAccumulator;
161 TouchButtonAccumulator mTouchButtonAccumulator;
162
163 struct VirtualKey {
164 int32_t keyCode;
165 int32_t scanCode;
166 uint32_t flags;
167
168 // computed hit box, specified in touch screen coords based on known display size
169 int32_t hitLeft;
170 int32_t hitTop;
171 int32_t hitRight;
172 int32_t hitBottom;
173
174 inline bool isHit(int32_t x, int32_t y) const {
175 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
176 }
177 };
178
179 // Input sources and device mode.
180 uint32_t mSource;
181
Michael Wright227c5542020-07-02 18:30:52 +0100182 enum class DeviceMode {
183 DISABLED, // input is disabled
184 DIRECT, // direct mapping (touchscreen)
185 UNSCALED, // unscaled mapping (touchpad)
186 NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
187 POINTER, // pointer mapping (pointer)
Dominik Laskowski75788452021-02-09 18:51:25 -0800188
189 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700190 };
191 DeviceMode mDeviceMode;
192
193 // The reader's configuration.
194 InputReaderConfiguration mConfig;
195
196 // Immutable configuration parameters.
197 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100198 enum class DeviceType {
199 TOUCH_SCREEN,
200 TOUCH_PAD,
201 TOUCH_NAVIGATION,
202 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800203
204 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700205 };
206
207 DeviceType deviceType;
208 bool hasAssociatedDisplay;
209 bool associatedDisplayIsExternal;
210 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700211
212 enum class Orientation : int32_t {
213 ORIENTATION_0 = DISPLAY_ORIENTATION_0,
214 ORIENTATION_90 = DISPLAY_ORIENTATION_90,
215 ORIENTATION_180 = DISPLAY_ORIENTATION_180,
216 ORIENTATION_270 = DISPLAY_ORIENTATION_270,
Dominik Laskowski75788452021-02-09 18:51:25 -0800217
218 ftl_last = ORIENTATION_270
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700219 };
220 Orientation orientation;
221
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700222 bool hasButtonUnderPad;
223 std::string uniqueDisplayId;
224
Michael Wright227c5542020-07-02 18:30:52 +0100225 enum class GestureMode {
226 SINGLE_TOUCH,
227 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800228
229 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700230 };
231 GestureMode gestureMode;
232
233 bool wake;
234 } mParameters;
235
236 // Immutable calibration parameters in parsed form.
237 struct Calibration {
238 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100239 enum class SizeCalibration {
240 DEFAULT,
241 NONE,
242 GEOMETRIC,
243 DIAMETER,
244 BOX,
245 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800246 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700247 };
248
249 SizeCalibration sizeCalibration;
250
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700251 std::optional<float> sizeScale;
252 std::optional<float> sizeBias;
253 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700254
255 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100256 enum class PressureCalibration {
257 DEFAULT,
258 NONE,
259 PHYSICAL,
260 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700261 };
262
263 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700264 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700265
266 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100267 enum class OrientationCalibration {
268 DEFAULT,
269 NONE,
270 INTERPOLATED,
271 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 };
273
274 OrientationCalibration orientationCalibration;
275
276 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100277 enum class DistanceCalibration {
278 DEFAULT,
279 NONE,
280 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700281 };
282
283 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700284 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285
Michael Wright227c5542020-07-02 18:30:52 +0100286 enum class CoverageCalibration {
287 DEFAULT,
288 NONE,
289 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700290 };
291
292 CoverageCalibration coverageCalibration;
293
294 inline void applySizeScaleAndBias(float* outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700295 if (sizeScale) {
296 *outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700297 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700298 if (sizeBias) {
299 *outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300 }
301 if (*outSize < 0) {
302 *outSize = 0;
303 }
304 }
305 } mCalibration;
306
307 // Affine location transformation/calibration
308 struct TouchAffineTransformation mAffineTransform;
309
310 RawPointerAxes mRawPointerAxes;
311
312 struct RawState {
313 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000314 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700315
316 // Raw pointer sample data.
317 RawPointerData rawPointerData;
318
319 int32_t buttonState;
320
321 // Scroll state.
322 int32_t rawVScroll;
323 int32_t rawHScroll;
324
325 void copyFrom(const RawState& other) {
326 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000327 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700328 rawPointerData.copyFrom(other.rawPointerData);
329 buttonState = other.buttonState;
330 rawVScroll = other.rawVScroll;
331 rawHScroll = other.rawHScroll;
332 }
333
334 void clear() {
335 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000336 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337 rawPointerData.clear();
338 buttonState = 0;
339 rawVScroll = 0;
340 rawHScroll = 0;
341 }
342 };
343
344 struct CookedState {
345 // Cooked pointer sample data.
346 CookedPointerData cookedPointerData;
347
348 // Id bits used to differentiate fingers, stylus and mouse tools.
349 BitSet32 fingerIdBits;
350 BitSet32 stylusIdBits;
351 BitSet32 mouseIdBits;
352
353 int32_t buttonState;
354
355 void copyFrom(const CookedState& other) {
356 cookedPointerData.copyFrom(other.cookedPointerData);
357 fingerIdBits = other.fingerIdBits;
358 stylusIdBits = other.stylusIdBits;
359 mouseIdBits = other.mouseIdBits;
360 buttonState = other.buttonState;
361 }
362
363 void clear() {
364 cookedPointerData.clear();
365 fingerIdBits.clear();
366 stylusIdBits.clear();
367 mouseIdBits.clear();
368 buttonState = 0;
369 }
370 };
371
372 std::vector<RawState> mRawStatesPending;
373 RawState mCurrentRawState;
374 CookedState mCurrentCookedState;
375 RawState mLastRawState;
376 CookedState mLastCookedState;
377
378 // State provided by an external stylus
379 StylusState mExternalStylusState;
380 int64_t mExternalStylusId;
381 nsecs_t mExternalStylusFusionTimeout;
382 bool mExternalStylusDataPending;
383
384 // True if we sent a HOVER_ENTER event.
385 bool mSentHoverEnter;
386
387 // Have we assigned pointer IDs for this stream
388 bool mHavePointerIds;
389
390 // Is the current stream of direct touch events aborted
391 bool mCurrentMotionAborted;
392
393 // The time the primary pointer last went down.
394 nsecs_t mDownTime;
395
396 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100397 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398
399 std::vector<VirtualKey> mVirtualKeys;
400
401 virtual void configureParameters();
402 virtual void dumpParameters(std::string& dump);
403 virtual void configureRawPointerAxes();
404 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700405 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
406 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700407 virtual void configureVirtualKeys();
408 virtual void dumpVirtualKeys(std::string& dump);
409 virtual void parseCalibration();
410 virtual void resolveCalibration();
411 virtual void dumpCalibration(std::string& dump);
412 virtual void updateAffineTransformation();
413 virtual void dumpAffineTransformation(std::string& dump);
414 virtual void resolveExternalStylusPresence();
415 virtual bool hasStylus() const = 0;
416 virtual bool hasExternalStylus() const;
417
418 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
419
420private:
421 // The current viewport.
422 // The components of the viewport are specified in the display's rotated orientation.
423 DisplayViewport mViewport;
424
Prabir Pradhan1728b212021-10-19 16:00:03 -0700425 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700426 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700427 int32_t mDisplayWidth;
428 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800429
Prabir Pradhan1728b212021-10-19 16:00:03 -0700430 // The physical frame is the rectangle in the display's coordinate space that maps to the
431 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700432 int32_t mPhysicalWidth;
433 int32_t mPhysicalHeight;
434 int32_t mPhysicalLeft;
435 int32_t mPhysicalTop;
436
Prabir Pradhan1728b212021-10-19 16:00:03 -0700437 // The orientation of the input device relative to that of the display panel. It specifies
438 // the rotation of the input device coordinates required to produce the display panel
439 // orientation, so it will depend on whether the device is orientation aware.
440 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700441
442 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 float mXScale;
444 float mXPrecision;
445
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446 float mYScale;
447 float mYPrecision;
448
449 float mGeometricScale;
450
451 float mPressureScale;
452
453 float mSizeScale;
454
455 float mOrientationScale;
456
457 float mDistanceScale;
458
459 bool mHaveTilt;
460 float mTiltXCenter;
461 float mTiltXScale;
462 float mTiltYCenter;
463 float mTiltYScale;
464
465 bool mExternalStylusConnected;
466
467 // Oriented motion ranges for input device info.
468 struct OrientedRanges {
469 InputDeviceInfo::MotionRange x;
470 InputDeviceInfo::MotionRange y;
471 InputDeviceInfo::MotionRange pressure;
472
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700473 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700474
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700475 std::optional<InputDeviceInfo::MotionRange> touchMajor;
476 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700477
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700478 std::optional<InputDeviceInfo::MotionRange> toolMajor;
479 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700480
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700481 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700482
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700483 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700484
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700485 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700486
487 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700488 size = std::nullopt;
489 touchMajor = std::nullopt;
490 touchMinor = std::nullopt;
491 toolMajor = std::nullopt;
492 toolMinor = std::nullopt;
493 orientation = std::nullopt;
494 distance = std::nullopt;
495 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700496 }
497 } mOrientedRanges;
498
499 // Oriented dimensions and precision.
500 float mOrientedXPrecision;
501 float mOrientedYPrecision;
502
503 struct CurrentVirtualKeyState {
504 bool down;
505 bool ignored;
506 nsecs_t downTime;
507 int32_t keyCode;
508 int32_t scanCode;
509 } mCurrentVirtualKey;
510
511 // Scale factor for gesture or mouse based pointer movements.
512 float mPointerXMovementScale;
513 float mPointerYMovementScale;
514
515 // Scale factor for gesture based zooming and other freeform motions.
516 float mPointerXZoomScale;
517 float mPointerYZoomScale;
518
HQ Liue6983c72022-04-19 22:14:56 +0000519 // The maximum swipe width between pointers to detect a swipe gesture
520 // in the number of pixels.Touches that are wider than this are translated
521 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700522 float mPointerGestureMaxSwipeWidth;
523
524 struct PointerDistanceHeapElement {
525 uint32_t currentPointerIndex : 8;
526 uint32_t lastPointerIndex : 8;
527 uint64_t distance : 48; // squared distance
528 };
529
Michael Wright227c5542020-07-02 18:30:52 +0100530 enum class PointerUsage {
531 NONE,
532 GESTURES,
533 STYLUS,
534 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700535 };
536 PointerUsage mPointerUsage;
537
538 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100539 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700540 // No fingers, button is not pressed.
541 // Nothing happening.
542 NEUTRAL,
543
544 // No fingers, button is not pressed.
545 // Tap detected.
546 // Emits DOWN and UP events at the pointer location.
547 TAP,
548
549 // Exactly one finger dragging following a tap.
550 // Pointer follows the active finger.
551 // Emits DOWN, MOVE and UP events at the pointer location.
552 //
553 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
554 TAP_DRAG,
555
556 // Button is pressed.
557 // Pointer follows the active finger if there is one. Other fingers are ignored.
558 // Emits DOWN, MOVE and UP events at the pointer location.
559 BUTTON_CLICK_OR_DRAG,
560
561 // Exactly one finger, button is not pressed.
562 // Pointer follows the active finger.
563 // Emits HOVER_MOVE events at the pointer location.
564 //
565 // Detect taps when the finger goes up while in HOVER mode.
566 HOVER,
567
568 // Exactly two fingers but neither have moved enough to clearly indicate
569 // whether a swipe or freeform gesture was intended. We consider the
570 // pointer to be pressed so this enables clicking or long-pressing on buttons.
571 // Pointer does not move.
572 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
573 PRESS,
574
575 // Exactly two fingers moving in the same direction, button is not pressed.
576 // Pointer does not move.
577 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
578 // follows the midpoint between both fingers.
579 SWIPE,
580
581 // Two or more fingers moving in arbitrary directions, button is not pressed.
582 // Pointer does not move.
583 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
584 // each finger individually relative to the initial centroid of the finger.
585 FREEFORM,
586
587 // Waiting for quiet time to end before starting the next gesture.
588 QUIET,
589 };
590
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800591 // When a gesture is sent to an unfocused window, return true if it can bring that window
592 // into focus, false otherwise.
593 static bool canGestureAffectWindowFocus(Mode mode) {
594 switch (mode) {
595 case Mode::TAP:
596 case Mode::TAP_DRAG:
597 case Mode::BUTTON_CLICK_OR_DRAG:
598 // Taps can affect window focus.
599 return true;
600 case Mode::FREEFORM:
601 case Mode::HOVER:
602 case Mode::NEUTRAL:
603 case Mode::PRESS:
604 case Mode::QUIET:
605 case Mode::SWIPE:
606 // Most gestures can be performed on an unfocused window, so they should not
607 // not affect window focus.
608 return false;
609 }
610 }
611
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700612 // Time the first finger went down.
613 nsecs_t firstTouchTime;
614
615 // The active pointer id from the raw touch data.
616 int32_t activeTouchId; // -1 if none
617
618 // The active pointer id from the gesture last delivered to the application.
619 int32_t activeGestureId; // -1 if none
620
621 // Pointer coords and ids for the current and previous pointer gesture.
622 Mode currentGestureMode;
623 BitSet32 currentGestureIdBits;
624 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
625 PointerProperties currentGestureProperties[MAX_POINTERS];
626 PointerCoords currentGestureCoords[MAX_POINTERS];
627
628 Mode lastGestureMode;
629 BitSet32 lastGestureIdBits;
630 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
631 PointerProperties lastGestureProperties[MAX_POINTERS];
632 PointerCoords lastGestureCoords[MAX_POINTERS];
633
634 // Time the pointer gesture last went down.
635 nsecs_t downTime;
636
637 // Time when the pointer went down for a TAP.
638 nsecs_t tapDownTime;
639
640 // Time when the pointer went up for a TAP.
641 nsecs_t tapUpTime;
642
643 // Location of initial tap.
644 float tapX, tapY;
645
646 // Time we started waiting for quiescence.
647 nsecs_t quietTime;
648
649 // Reference points for multitouch gestures.
650 float referenceTouchX; // reference touch X/Y coordinates in surface units
651 float referenceTouchY;
652 float referenceGestureX; // reference gesture X/Y coordinates in pixels
653 float referenceGestureY;
654
655 // Distance that each pointer has traveled which has not yet been
656 // subsumed into the reference gesture position.
657 BitSet32 referenceIdBits;
658 struct Delta {
659 float dx, dy;
660 };
661 Delta referenceDeltas[MAX_POINTER_ID + 1];
662
663 // Describes how touch ids are mapped to gesture ids for freeform gestures.
664 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
665
666 // A velocity tracker for determining whether to switch active pointers during drags.
667 VelocityTracker velocityTracker;
668
669 void reset() {
670 firstTouchTime = LLONG_MIN;
671 activeTouchId = -1;
672 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100673 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700674 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100675 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700676 lastGestureIdBits.clear();
677 downTime = 0;
678 velocityTracker.clear();
679 resetTap();
680 resetQuietTime();
681 }
682
683 void resetTap() {
684 tapDownTime = LLONG_MIN;
685 tapUpTime = LLONG_MIN;
686 }
687
688 void resetQuietTime() { quietTime = LLONG_MIN; }
689 } mPointerGesture;
690
691 struct PointerSimple {
692 PointerCoords currentCoords;
693 PointerProperties currentProperties;
694 PointerCoords lastCoords;
695 PointerProperties lastProperties;
696
697 // True if the pointer is down.
698 bool down;
699
700 // True if the pointer is hovering.
701 bool hovering;
702
703 // Time the pointer last went down.
704 nsecs_t downTime;
705
706 void reset() {
707 currentCoords.clear();
708 currentProperties.clear();
709 lastCoords.clear();
710 lastProperties.clear();
711 down = false;
712 hovering = false;
713 downTime = 0;
714 }
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 Vishniakou58ba3d12021-02-11 01:31:07 +0000731 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700732
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000733 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700734 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000735 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
736 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
737 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700738
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000739 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
740 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
741 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
742 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
743 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700744 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
745 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000746 void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700747
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000748 void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
749 PointerUsage pointerUsage);
750 void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000752 void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
753 bool isTimeout);
754 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700755 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
756 bool* outFinishPreviousGesture, bool isTimeout);
757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000758 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
759 void abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700760
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000761 void dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
762 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700763
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000764 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
765 bool hovering);
766 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700767
768 bool assignExternalStylusId(const RawState& state, bool timeout);
769 void applyExternalStylusButtonState(nsecs_t when);
770 void applyExternalStylusTouchState(nsecs_t when);
771
772 // Dispatches a motion event.
773 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
774 // method will take care of setting the index and transmuting the action to DOWN or UP
775 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000776 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
777 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
778 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700779 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
780 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
781
782 // Updates pointer coords and properties for pointers with specified ids that have moved.
783 // Returns true if any of them changed.
784 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
785 const uint32_t* inIdToIndex, PointerProperties* outProperties,
786 PointerCoords* outCoords, const uint32_t* outIdToIndex,
787 BitSet32 idBits) const;
788
Garfield Tanc734e4f2021-01-15 20:01:39 -0800789 // Returns if this touch device is a touch screen with an associated display.
790 bool isTouchScreen();
791 // Updates touch spots if they are enabled. Should only be used when this device is a
792 // touchscreen.
793 void updateTouchSpots();
794
Prabir Pradhan1728b212021-10-19 16:00:03 -0700795 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700796 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
797
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000798 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700799
800 const char* modeToString(DeviceMode deviceMode);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700801 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700802};
803
804} // namespace android
805
Dominik Laskowski75788452021-02-09 18:51:25 -0800806#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H