blob: fe1700612e3219ad0f95adf748c3d095acdd27d4 [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,
Michael Wright227c5542020-07-02 18:30:52 +0100200 TOUCH_NAVIGATION,
201 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800202
203 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700204 };
205
206 DeviceType deviceType;
207 bool hasAssociatedDisplay;
208 bool associatedDisplayIsExternal;
209 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700210
211 enum class Orientation : int32_t {
212 ORIENTATION_0 = DISPLAY_ORIENTATION_0,
213 ORIENTATION_90 = DISPLAY_ORIENTATION_90,
214 ORIENTATION_180 = DISPLAY_ORIENTATION_180,
215 ORIENTATION_270 = DISPLAY_ORIENTATION_270,
Dominik Laskowski75788452021-02-09 18:51:25 -0800216
217 ftl_last = ORIENTATION_270
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700218 };
219 Orientation orientation;
220
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 bool hasButtonUnderPad;
222 std::string uniqueDisplayId;
223
Michael Wright227c5542020-07-02 18:30:52 +0100224 enum class GestureMode {
225 SINGLE_TOUCH,
226 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800227
228 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 };
230 GestureMode gestureMode;
231
232 bool wake;
233 } mParameters;
234
235 // Immutable calibration parameters in parsed form.
236 struct Calibration {
237 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100238 enum class SizeCalibration {
239 DEFAULT,
240 NONE,
241 GEOMETRIC,
242 DIAMETER,
243 BOX,
244 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800245 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 };
247
248 SizeCalibration sizeCalibration;
249
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700250 std::optional<float> sizeScale;
251 std::optional<float> sizeBias;
252 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700253
254 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100255 enum class PressureCalibration {
256 DEFAULT,
257 NONE,
258 PHYSICAL,
259 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700260 };
261
262 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700263 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264
265 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100266 enum class OrientationCalibration {
267 DEFAULT,
268 NONE,
269 INTERPOLATED,
270 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 };
272
273 OrientationCalibration orientationCalibration;
274
275 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100276 enum class DistanceCalibration {
277 DEFAULT,
278 NONE,
279 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 };
281
282 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700283 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700284
Michael Wright227c5542020-07-02 18:30:52 +0100285 enum class CoverageCalibration {
286 DEFAULT,
287 NONE,
288 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700289 };
290
291 CoverageCalibration coverageCalibration;
292
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700293 inline void applySizeScaleAndBias(float& outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700294 if (sizeScale) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700295 outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700296 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700297 if (sizeBias) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700298 outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299 }
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700300 if (outSize < 0) {
301 outSize = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700302 }
303 }
304 } mCalibration;
305
306 // Affine location transformation/calibration
307 struct TouchAffineTransformation mAffineTransform;
308
309 RawPointerAxes mRawPointerAxes;
310
311 struct RawState {
312 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000313 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314
315 // Raw pointer sample data.
316 RawPointerData rawPointerData;
317
318 int32_t buttonState;
319
320 // Scroll state.
321 int32_t rawVScroll;
322 int32_t rawHScroll;
323
324 void copyFrom(const RawState& other) {
325 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000326 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700327 rawPointerData.copyFrom(other.rawPointerData);
328 buttonState = other.buttonState;
329 rawVScroll = other.rawVScroll;
330 rawHScroll = other.rawHScroll;
331 }
332
333 void clear() {
334 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000335 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 rawPointerData.clear();
337 buttonState = 0;
338 rawVScroll = 0;
339 rawHScroll = 0;
340 }
341 };
342
343 struct CookedState {
344 // Cooked pointer sample data.
345 CookedPointerData cookedPointerData;
346
347 // Id bits used to differentiate fingers, stylus and mouse tools.
348 BitSet32 fingerIdBits;
349 BitSet32 stylusIdBits;
350 BitSet32 mouseIdBits;
351
352 int32_t buttonState;
353
354 void copyFrom(const CookedState& other) {
355 cookedPointerData.copyFrom(other.cookedPointerData);
356 fingerIdBits = other.fingerIdBits;
357 stylusIdBits = other.stylusIdBits;
358 mouseIdBits = other.mouseIdBits;
359 buttonState = other.buttonState;
360 }
361
362 void clear() {
363 cookedPointerData.clear();
364 fingerIdBits.clear();
365 stylusIdBits.clear();
366 mouseIdBits.clear();
367 buttonState = 0;
368 }
369 };
370
371 std::vector<RawState> mRawStatesPending;
372 RawState mCurrentRawState;
373 CookedState mCurrentCookedState;
374 RawState mLastRawState;
375 CookedState mLastCookedState;
376
377 // State provided by an external stylus
378 StylusState mExternalStylusState;
379 int64_t mExternalStylusId;
380 nsecs_t mExternalStylusFusionTimeout;
381 bool mExternalStylusDataPending;
382
383 // True if we sent a HOVER_ENTER event.
384 bool mSentHoverEnter;
385
386 // Have we assigned pointer IDs for this stream
387 bool mHavePointerIds;
388
389 // Is the current stream of direct touch events aborted
390 bool mCurrentMotionAborted;
391
392 // The time the primary pointer last went down.
393 nsecs_t mDownTime;
394
395 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100396 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700397
398 std::vector<VirtualKey> mVirtualKeys;
399
400 virtual void configureParameters();
401 virtual void dumpParameters(std::string& dump);
402 virtual void configureRawPointerAxes();
403 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700404 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
405 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700406 virtual void configureVirtualKeys();
407 virtual void dumpVirtualKeys(std::string& dump);
408 virtual void parseCalibration();
409 virtual void resolveCalibration();
410 virtual void dumpCalibration(std::string& dump);
411 virtual void updateAffineTransformation();
412 virtual void dumpAffineTransformation(std::string& dump);
413 virtual void resolveExternalStylusPresence();
414 virtual bool hasStylus() const = 0;
415 virtual bool hasExternalStylus() const;
416
417 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
418
419private:
420 // The current viewport.
421 // The components of the viewport are specified in the display's rotated orientation.
422 DisplayViewport mViewport;
423
Prabir Pradhan1728b212021-10-19 16:00:03 -0700424 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700426 int32_t mDisplayWidth;
427 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800428
Prabir Pradhan1728b212021-10-19 16:00:03 -0700429 // The physical frame is the rectangle in the display's coordinate space that maps to the
430 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 int32_t mPhysicalWidth;
432 int32_t mPhysicalHeight;
433 int32_t mPhysicalLeft;
434 int32_t mPhysicalTop;
435
Prabir Pradhan1728b212021-10-19 16:00:03 -0700436 // The orientation of the input device relative to that of the display panel. It specifies
437 // the rotation of the input device coordinates required to produce the display panel
438 // orientation, so it will depend on whether the device is orientation aware.
439 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700440
441 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442 float mXScale;
443 float mXPrecision;
444
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700445 float mYScale;
446 float mYPrecision;
447
448 float mGeometricScale;
449
450 float mPressureScale;
451
452 float mSizeScale;
453
454 float mOrientationScale;
455
456 float mDistanceScale;
457
458 bool mHaveTilt;
459 float mTiltXCenter;
460 float mTiltXScale;
461 float mTiltYCenter;
462 float mTiltYScale;
463
464 bool mExternalStylusConnected;
465
466 // Oriented motion ranges for input device info.
467 struct OrientedRanges {
468 InputDeviceInfo::MotionRange x;
469 InputDeviceInfo::MotionRange y;
470 InputDeviceInfo::MotionRange pressure;
471
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700472 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700474 std::optional<InputDeviceInfo::MotionRange> touchMajor;
475 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700476
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700477 std::optional<InputDeviceInfo::MotionRange> toolMajor;
478 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700479
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700480 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700481
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700482 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700483
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700484 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700485
486 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700487 size = std::nullopt;
488 touchMajor = std::nullopt;
489 touchMinor = std::nullopt;
490 toolMajor = std::nullopt;
491 toolMinor = std::nullopt;
492 orientation = std::nullopt;
493 distance = std::nullopt;
494 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700495 }
496 } mOrientedRanges;
497
498 // Oriented dimensions and precision.
499 float mOrientedXPrecision;
500 float mOrientedYPrecision;
501
502 struct CurrentVirtualKeyState {
503 bool down;
504 bool ignored;
505 nsecs_t downTime;
506 int32_t keyCode;
507 int32_t scanCode;
508 } mCurrentVirtualKey;
509
510 // Scale factor for gesture or mouse based pointer movements.
511 float mPointerXMovementScale;
512 float mPointerYMovementScale;
513
514 // Scale factor for gesture based zooming and other freeform motions.
515 float mPointerXZoomScale;
516 float mPointerYZoomScale;
517
HQ Liue6983c72022-04-19 22:14:56 +0000518 // The maximum swipe width between pointers to detect a swipe gesture
519 // in the number of pixels.Touches that are wider than this are translated
520 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700521 float mPointerGestureMaxSwipeWidth;
522
523 struct PointerDistanceHeapElement {
524 uint32_t currentPointerIndex : 8;
525 uint32_t lastPointerIndex : 8;
526 uint64_t distance : 48; // squared distance
527 };
528
Michael Wright227c5542020-07-02 18:30:52 +0100529 enum class PointerUsage {
530 NONE,
531 GESTURES,
532 STYLUS,
533 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700534 };
535 PointerUsage mPointerUsage;
536
537 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100538 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700539 // No fingers, button is not pressed.
540 // Nothing happening.
541 NEUTRAL,
542
543 // No fingers, button is not pressed.
544 // Tap detected.
545 // Emits DOWN and UP events at the pointer location.
546 TAP,
547
548 // Exactly one finger dragging following a tap.
549 // Pointer follows the active finger.
550 // Emits DOWN, MOVE and UP events at the pointer location.
551 //
552 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
553 TAP_DRAG,
554
555 // Button is pressed.
556 // Pointer follows the active finger if there is one. Other fingers are ignored.
557 // Emits DOWN, MOVE and UP events at the pointer location.
558 BUTTON_CLICK_OR_DRAG,
559
560 // Exactly one finger, button is not pressed.
561 // Pointer follows the active finger.
562 // Emits HOVER_MOVE events at the pointer location.
563 //
564 // Detect taps when the finger goes up while in HOVER mode.
565 HOVER,
566
567 // Exactly two fingers but neither have moved enough to clearly indicate
568 // whether a swipe or freeform gesture was intended. We consider the
569 // pointer to be pressed so this enables clicking or long-pressing on buttons.
570 // Pointer does not move.
571 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
572 PRESS,
573
574 // Exactly two fingers moving in the same direction, button is not pressed.
575 // Pointer does not move.
576 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
577 // follows the midpoint between both fingers.
578 SWIPE,
579
580 // Two or more fingers moving in arbitrary directions, button is not pressed.
581 // Pointer does not move.
582 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
583 // each finger individually relative to the initial centroid of the finger.
584 FREEFORM,
585
586 // Waiting for quiet time to end before starting the next gesture.
587 QUIET,
588 };
589
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800590 // When a gesture is sent to an unfocused window, return true if it can bring that window
591 // into focus, false otherwise.
592 static bool canGestureAffectWindowFocus(Mode mode) {
593 switch (mode) {
594 case Mode::TAP:
595 case Mode::TAP_DRAG:
596 case Mode::BUTTON_CLICK_OR_DRAG:
597 // Taps can affect window focus.
598 return true;
599 case Mode::FREEFORM:
600 case Mode::HOVER:
601 case Mode::NEUTRAL:
602 case Mode::PRESS:
603 case Mode::QUIET:
604 case Mode::SWIPE:
605 // Most gestures can be performed on an unfocused window, so they should not
606 // not affect window focus.
607 return false;
608 }
609 }
610
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700611 // Time the first finger went down.
612 nsecs_t firstTouchTime;
613
614 // The active pointer id from the raw touch data.
615 int32_t activeTouchId; // -1 if none
616
617 // The active pointer id from the gesture last delivered to the application.
618 int32_t activeGestureId; // -1 if none
619
620 // Pointer coords and ids for the current and previous pointer gesture.
621 Mode currentGestureMode;
622 BitSet32 currentGestureIdBits;
623 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
624 PointerProperties currentGestureProperties[MAX_POINTERS];
625 PointerCoords currentGestureCoords[MAX_POINTERS];
626
627 Mode lastGestureMode;
628 BitSet32 lastGestureIdBits;
629 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
630 PointerProperties lastGestureProperties[MAX_POINTERS];
631 PointerCoords lastGestureCoords[MAX_POINTERS];
632
633 // Time the pointer gesture last went down.
634 nsecs_t downTime;
635
636 // Time when the pointer went down for a TAP.
637 nsecs_t tapDownTime;
638
639 // Time when the pointer went up for a TAP.
640 nsecs_t tapUpTime;
641
642 // Location of initial tap.
643 float tapX, tapY;
644
645 // Time we started waiting for quiescence.
646 nsecs_t quietTime;
647
648 // Reference points for multitouch gestures.
649 float referenceTouchX; // reference touch X/Y coordinates in surface units
650 float referenceTouchY;
651 float referenceGestureX; // reference gesture X/Y coordinates in pixels
652 float referenceGestureY;
653
654 // Distance that each pointer has traveled which has not yet been
655 // subsumed into the reference gesture position.
656 BitSet32 referenceIdBits;
657 struct Delta {
658 float dx, dy;
659 };
660 Delta referenceDeltas[MAX_POINTER_ID + 1];
661
662 // Describes how touch ids are mapped to gesture ids for freeform gestures.
663 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
664
665 // A velocity tracker for determining whether to switch active pointers during drags.
666 VelocityTracker velocityTracker;
667
668 void reset() {
669 firstTouchTime = LLONG_MIN;
670 activeTouchId = -1;
671 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100672 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700673 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100674 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700675 lastGestureIdBits.clear();
676 downTime = 0;
677 velocityTracker.clear();
678 resetTap();
679 resetQuietTime();
680 }
681
682 void resetTap() {
683 tapDownTime = LLONG_MIN;
684 tapUpTime = LLONG_MIN;
685 }
686
687 void resetQuietTime() { quietTime = LLONG_MIN; }
688 } mPointerGesture;
689
690 struct PointerSimple {
691 PointerCoords currentCoords;
692 PointerProperties currentProperties;
693 PointerCoords lastCoords;
694 PointerProperties lastProperties;
695
696 // True if the pointer is down.
697 bool down;
698
699 // True if the pointer is hovering.
700 bool hovering;
701
702 // Time the pointer last went down.
703 nsecs_t downTime;
704
705 void reset() {
706 currentCoords.clear();
707 currentProperties.clear();
708 lastCoords.clear();
709 lastProperties.clear();
710 down = false;
711 hovering = false;
712 downTime = 0;
713 }
714 } mPointerSimple;
715
716 // The pointer and scroll velocity controls.
717 VelocityControl mPointerVelocityControl;
718 VelocityControl mWheelXVelocityControl;
719 VelocityControl mWheelYVelocityControl;
720
721 std::optional<DisplayViewport> findViewport();
722
723 void resetExternalStylus();
724 void clearStylusDataPendingFlags();
725
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800726 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800727 void initializeOrientedRanges();
728 void initializeSizeRanges();
729
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000730 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000732 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700733 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000734 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
735 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
736 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700737
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000738 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
739 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
740 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
741 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
742 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700743 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
744 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000745 void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700746
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000747 void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
748 PointerUsage pointerUsage);
749 void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700750
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000751 void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
752 bool isTimeout);
753 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700754 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
755 bool* outFinishPreviousGesture, bool isTimeout);
756
Harry Cutts714d1ad2022-08-24 16:36:43 +0000757 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
758 // between the last and current events. Uses a relative motion.
759 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
760
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000761 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
762 void abortPointerStylus(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 dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
765 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700766
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000767 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
768 bool hovering);
769 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700770
771 bool assignExternalStylusId(const RawState& state, bool timeout);
772 void applyExternalStylusButtonState(nsecs_t when);
773 void applyExternalStylusTouchState(nsecs_t when);
774
775 // Dispatches a motion event.
776 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
777 // method will take care of setting the index and transmuting the action to DOWN or UP
778 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000779 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
780 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
781 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700782 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
783 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
784
785 // Updates pointer coords and properties for pointers with specified ids that have moved.
786 // Returns true if any of them changed.
787 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
788 const uint32_t* inIdToIndex, PointerProperties* outProperties,
789 PointerCoords* outCoords, const uint32_t* outIdToIndex,
790 BitSet32 idBits) const;
791
Garfield Tanc734e4f2021-01-15 20:01:39 -0800792 // Returns if this touch device is a touch screen with an associated display.
793 bool isTouchScreen();
794 // Updates touch spots if they are enabled. Should only be used when this device is a
795 // touchscreen.
796 void updateTouchSpots();
797
Prabir Pradhan1728b212021-10-19 16:00:03 -0700798 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700799 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
800
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000801 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700802
803 const char* modeToString(DeviceMode deviceMode);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700804 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700805};
806
807} // namespace android
808
Dominik Laskowski75788452021-02-09 18:51:25 -0800809#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H