blob: 3042be68c1895e2b89a6e13cb2d8053addae4588 [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
251 bool haveSizeScale;
252 float sizeScale;
253 bool haveSizeBias;
254 float sizeBias;
255 bool haveSizeIsSummed;
256 bool sizeIsSummed;
257
258 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100259 enum class PressureCalibration {
260 DEFAULT,
261 NONE,
262 PHYSICAL,
263 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 };
265
266 PressureCalibration pressureCalibration;
267 bool havePressureScale;
268 float pressureScale;
269
270 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100271 enum class OrientationCalibration {
272 DEFAULT,
273 NONE,
274 INTERPOLATED,
275 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 };
277
278 OrientationCalibration orientationCalibration;
279
280 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100281 enum class DistanceCalibration {
282 DEFAULT,
283 NONE,
284 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285 };
286
287 DistanceCalibration distanceCalibration;
288 bool haveDistanceScale;
289 float distanceScale;
290
Michael Wright227c5542020-07-02 18:30:52 +0100291 enum class CoverageCalibration {
292 DEFAULT,
293 NONE,
294 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295 };
296
297 CoverageCalibration coverageCalibration;
298
299 inline void applySizeScaleAndBias(float* outSize) const {
300 if (haveSizeScale) {
301 *outSize *= sizeScale;
302 }
303 if (haveSizeBias) {
304 *outSize += sizeBias;
305 }
306 if (*outSize < 0) {
307 *outSize = 0;
308 }
309 }
310 } mCalibration;
311
312 // Affine location transformation/calibration
313 struct TouchAffineTransformation mAffineTransform;
314
315 RawPointerAxes mRawPointerAxes;
316
317 struct RawState {
318 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000319 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700320
321 // Raw pointer sample data.
322 RawPointerData rawPointerData;
323
324 int32_t buttonState;
325
326 // Scroll state.
327 int32_t rawVScroll;
328 int32_t rawHScroll;
329
330 void copyFrom(const RawState& other) {
331 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000332 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 rawPointerData.copyFrom(other.rawPointerData);
334 buttonState = other.buttonState;
335 rawVScroll = other.rawVScroll;
336 rawHScroll = other.rawHScroll;
337 }
338
339 void clear() {
340 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000341 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700342 rawPointerData.clear();
343 buttonState = 0;
344 rawVScroll = 0;
345 rawHScroll = 0;
346 }
347 };
348
349 struct CookedState {
350 // Cooked pointer sample data.
351 CookedPointerData cookedPointerData;
352
353 // Id bits used to differentiate fingers, stylus and mouse tools.
354 BitSet32 fingerIdBits;
355 BitSet32 stylusIdBits;
356 BitSet32 mouseIdBits;
357
358 int32_t buttonState;
359
360 void copyFrom(const CookedState& other) {
361 cookedPointerData.copyFrom(other.cookedPointerData);
362 fingerIdBits = other.fingerIdBits;
363 stylusIdBits = other.stylusIdBits;
364 mouseIdBits = other.mouseIdBits;
365 buttonState = other.buttonState;
366 }
367
368 void clear() {
369 cookedPointerData.clear();
370 fingerIdBits.clear();
371 stylusIdBits.clear();
372 mouseIdBits.clear();
373 buttonState = 0;
374 }
375 };
376
377 std::vector<RawState> mRawStatesPending;
378 RawState mCurrentRawState;
379 CookedState mCurrentCookedState;
380 RawState mLastRawState;
381 CookedState mLastCookedState;
382
383 // State provided by an external stylus
384 StylusState mExternalStylusState;
385 int64_t mExternalStylusId;
386 nsecs_t mExternalStylusFusionTimeout;
387 bool mExternalStylusDataPending;
388
389 // True if we sent a HOVER_ENTER event.
390 bool mSentHoverEnter;
391
392 // Have we assigned pointer IDs for this stream
393 bool mHavePointerIds;
394
395 // Is the current stream of direct touch events aborted
396 bool mCurrentMotionAborted;
397
398 // The time the primary pointer last went down.
399 nsecs_t mDownTime;
400
401 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100402 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403
404 std::vector<VirtualKey> mVirtualKeys;
405
406 virtual void configureParameters();
407 virtual void dumpParameters(std::string& dump);
408 virtual void configureRawPointerAxes();
409 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700410 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
411 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700412 virtual void configureVirtualKeys();
413 virtual void dumpVirtualKeys(std::string& dump);
414 virtual void parseCalibration();
415 virtual void resolveCalibration();
416 virtual void dumpCalibration(std::string& dump);
417 virtual void updateAffineTransformation();
418 virtual void dumpAffineTransformation(std::string& dump);
419 virtual void resolveExternalStylusPresence();
420 virtual bool hasStylus() const = 0;
421 virtual bool hasExternalStylus() const;
422
423 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
424
425private:
426 // The current viewport.
427 // The components of the viewport are specified in the display's rotated orientation.
428 DisplayViewport mViewport;
429
Prabir Pradhan1728b212021-10-19 16:00:03 -0700430 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700432 int32_t mDisplayWidth;
433 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800434
Prabir Pradhan1728b212021-10-19 16:00:03 -0700435 // The physical frame is the rectangle in the display's coordinate space that maps to the
436 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437 int32_t mPhysicalWidth;
438 int32_t mPhysicalHeight;
439 int32_t mPhysicalLeft;
440 int32_t mPhysicalTop;
441
Prabir Pradhan1728b212021-10-19 16:00:03 -0700442 // The orientation of the input device relative to that of the display panel. It specifies
443 // the rotation of the input device coordinates required to produce the display panel
444 // orientation, so it will depend on whether the device is orientation aware.
445 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446
447 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700448 float mXScale;
449 float mXPrecision;
450
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700451 float mYScale;
452 float mYPrecision;
453
454 float mGeometricScale;
455
456 float mPressureScale;
457
458 float mSizeScale;
459
460 float mOrientationScale;
461
462 float mDistanceScale;
463
464 bool mHaveTilt;
465 float mTiltXCenter;
466 float mTiltXScale;
467 float mTiltYCenter;
468 float mTiltYScale;
469
470 bool mExternalStylusConnected;
471
472 // Oriented motion ranges for input device info.
473 struct OrientedRanges {
474 InputDeviceInfo::MotionRange x;
475 InputDeviceInfo::MotionRange y;
476 InputDeviceInfo::MotionRange pressure;
477
478 bool haveSize;
479 InputDeviceInfo::MotionRange size;
480
481 bool haveTouchSize;
482 InputDeviceInfo::MotionRange touchMajor;
483 InputDeviceInfo::MotionRange touchMinor;
484
485 bool haveToolSize;
486 InputDeviceInfo::MotionRange toolMajor;
487 InputDeviceInfo::MotionRange toolMinor;
488
489 bool haveOrientation;
490 InputDeviceInfo::MotionRange orientation;
491
492 bool haveDistance;
493 InputDeviceInfo::MotionRange distance;
494
495 bool haveTilt;
496 InputDeviceInfo::MotionRange tilt;
497
498 OrientedRanges() { clear(); }
499
500 void clear() {
501 haveSize = false;
502 haveTouchSize = false;
503 haveToolSize = false;
504 haveOrientation = false;
505 haveDistance = false;
506 haveTilt = false;
507 }
508 } mOrientedRanges;
509
510 // Oriented dimensions and precision.
511 float mOrientedXPrecision;
512 float mOrientedYPrecision;
513
514 struct CurrentVirtualKeyState {
515 bool down;
516 bool ignored;
517 nsecs_t downTime;
518 int32_t keyCode;
519 int32_t scanCode;
520 } mCurrentVirtualKey;
521
522 // Scale factor for gesture or mouse based pointer movements.
523 float mPointerXMovementScale;
524 float mPointerYMovementScale;
525
526 // Scale factor for gesture based zooming and other freeform motions.
527 float mPointerXZoomScale;
528 float mPointerYZoomScale;
529
HQ Liue6983c72022-04-19 22:14:56 +0000530 // The maximum swipe width between pointers to detect a swipe gesture
531 // in the number of pixels.Touches that are wider than this are translated
532 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700533 float mPointerGestureMaxSwipeWidth;
534
535 struct PointerDistanceHeapElement {
536 uint32_t currentPointerIndex : 8;
537 uint32_t lastPointerIndex : 8;
538 uint64_t distance : 48; // squared distance
539 };
540
Michael Wright227c5542020-07-02 18:30:52 +0100541 enum class PointerUsage {
542 NONE,
543 GESTURES,
544 STYLUS,
545 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700546 };
547 PointerUsage mPointerUsage;
548
549 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100550 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700551 // No fingers, button is not pressed.
552 // Nothing happening.
553 NEUTRAL,
554
555 // No fingers, button is not pressed.
556 // Tap detected.
557 // Emits DOWN and UP events at the pointer location.
558 TAP,
559
560 // Exactly one finger dragging following a tap.
561 // Pointer follows the active finger.
562 // Emits DOWN, MOVE and UP events at the pointer location.
563 //
564 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
565 TAP_DRAG,
566
567 // Button is pressed.
568 // Pointer follows the active finger if there is one. Other fingers are ignored.
569 // Emits DOWN, MOVE and UP events at the pointer location.
570 BUTTON_CLICK_OR_DRAG,
571
572 // Exactly one finger, button is not pressed.
573 // Pointer follows the active finger.
574 // Emits HOVER_MOVE events at the pointer location.
575 //
576 // Detect taps when the finger goes up while in HOVER mode.
577 HOVER,
578
579 // Exactly two fingers but neither have moved enough to clearly indicate
580 // whether a swipe or freeform gesture was intended. We consider the
581 // pointer to be pressed so this enables clicking or long-pressing on buttons.
582 // Pointer does not move.
583 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
584 PRESS,
585
586 // Exactly two fingers moving in the same direction, button is not pressed.
587 // Pointer does not move.
588 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
589 // follows the midpoint between both fingers.
590 SWIPE,
591
592 // Two or more fingers moving in arbitrary directions, button is not pressed.
593 // Pointer does not move.
594 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
595 // each finger individually relative to the initial centroid of the finger.
596 FREEFORM,
597
598 // Waiting for quiet time to end before starting the next gesture.
599 QUIET,
600 };
601
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800602 // When a gesture is sent to an unfocused window, return true if it can bring that window
603 // into focus, false otherwise.
604 static bool canGestureAffectWindowFocus(Mode mode) {
605 switch (mode) {
606 case Mode::TAP:
607 case Mode::TAP_DRAG:
608 case Mode::BUTTON_CLICK_OR_DRAG:
609 // Taps can affect window focus.
610 return true;
611 case Mode::FREEFORM:
612 case Mode::HOVER:
613 case Mode::NEUTRAL:
614 case Mode::PRESS:
615 case Mode::QUIET:
616 case Mode::SWIPE:
617 // Most gestures can be performed on an unfocused window, so they should not
618 // not affect window focus.
619 return false;
620 }
621 }
622
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700623 // Time the first finger went down.
624 nsecs_t firstTouchTime;
625
626 // The active pointer id from the raw touch data.
627 int32_t activeTouchId; // -1 if none
628
629 // The active pointer id from the gesture last delivered to the application.
630 int32_t activeGestureId; // -1 if none
631
632 // Pointer coords and ids for the current and previous pointer gesture.
633 Mode currentGestureMode;
634 BitSet32 currentGestureIdBits;
635 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
636 PointerProperties currentGestureProperties[MAX_POINTERS];
637 PointerCoords currentGestureCoords[MAX_POINTERS];
638
639 Mode lastGestureMode;
640 BitSet32 lastGestureIdBits;
641 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
642 PointerProperties lastGestureProperties[MAX_POINTERS];
643 PointerCoords lastGestureCoords[MAX_POINTERS];
644
645 // Time the pointer gesture last went down.
646 nsecs_t downTime;
647
648 // Time when the pointer went down for a TAP.
649 nsecs_t tapDownTime;
650
651 // Time when the pointer went up for a TAP.
652 nsecs_t tapUpTime;
653
654 // Location of initial tap.
655 float tapX, tapY;
656
657 // Time we started waiting for quiescence.
658 nsecs_t quietTime;
659
660 // Reference points for multitouch gestures.
661 float referenceTouchX; // reference touch X/Y coordinates in surface units
662 float referenceTouchY;
663 float referenceGestureX; // reference gesture X/Y coordinates in pixels
664 float referenceGestureY;
665
666 // Distance that each pointer has traveled which has not yet been
667 // subsumed into the reference gesture position.
668 BitSet32 referenceIdBits;
669 struct Delta {
670 float dx, dy;
671 };
672 Delta referenceDeltas[MAX_POINTER_ID + 1];
673
674 // Describes how touch ids are mapped to gesture ids for freeform gestures.
675 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
676
677 // A velocity tracker for determining whether to switch active pointers during drags.
678 VelocityTracker velocityTracker;
679
680 void reset() {
681 firstTouchTime = LLONG_MIN;
682 activeTouchId = -1;
683 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100684 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700685 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100686 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700687 lastGestureIdBits.clear();
688 downTime = 0;
689 velocityTracker.clear();
690 resetTap();
691 resetQuietTime();
692 }
693
694 void resetTap() {
695 tapDownTime = LLONG_MIN;
696 tapUpTime = LLONG_MIN;
697 }
698
699 void resetQuietTime() { quietTime = LLONG_MIN; }
700 } mPointerGesture;
701
702 struct PointerSimple {
703 PointerCoords currentCoords;
704 PointerProperties currentProperties;
705 PointerCoords lastCoords;
706 PointerProperties lastProperties;
707
708 // True if the pointer is down.
709 bool down;
710
711 // True if the pointer is hovering.
712 bool hovering;
713
714 // Time the pointer last went down.
715 nsecs_t downTime;
716
717 void reset() {
718 currentCoords.clear();
719 currentProperties.clear();
720 lastCoords.clear();
721 lastProperties.clear();
722 down = false;
723 hovering = false;
724 downTime = 0;
725 }
726 } mPointerSimple;
727
728 // The pointer and scroll velocity controls.
729 VelocityControl mPointerVelocityControl;
730 VelocityControl mWheelXVelocityControl;
731 VelocityControl mWheelYVelocityControl;
732
733 std::optional<DisplayViewport> findViewport();
734
735 void resetExternalStylus();
736 void clearStylusDataPendingFlags();
737
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800738 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800739 void initializeOrientedRanges();
740 void initializeSizeRanges();
741
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000742 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700743
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000744 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700745 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000746 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
747 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
748 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000750 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
751 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
752 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
753 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
754 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700755 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
756 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000757 void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700758
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000759 void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
760 PointerUsage pointerUsage);
761 void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700762
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000763 void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
764 bool isTimeout);
765 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700766 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
767 bool* outFinishPreviousGesture, bool isTimeout);
768
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000769 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
770 void abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000772 void dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
773 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700774
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000775 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
776 bool hovering);
777 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700778
779 bool assignExternalStylusId(const RawState& state, bool timeout);
780 void applyExternalStylusButtonState(nsecs_t when);
781 void applyExternalStylusTouchState(nsecs_t when);
782
783 // Dispatches a motion event.
784 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
785 // method will take care of setting the index and transmuting the action to DOWN or UP
786 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000787 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
788 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
789 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700790 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
791 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
792
793 // Updates pointer coords and properties for pointers with specified ids that have moved.
794 // Returns true if any of them changed.
795 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
796 const uint32_t* inIdToIndex, PointerProperties* outProperties,
797 PointerCoords* outCoords, const uint32_t* outIdToIndex,
798 BitSet32 idBits) const;
799
Garfield Tanc734e4f2021-01-15 20:01:39 -0800800 // Returns if this touch device is a touch screen with an associated display.
801 bool isTouchScreen();
802 // Updates touch spots if they are enabled. Should only be used when this device is a
803 // touchscreen.
804 void updateTouchSpots();
805
Prabir Pradhan1728b212021-10-19 16:00:03 -0700806 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700807 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
808
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000809 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700810
811 const char* modeToString(DeviceMode deviceMode);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700812 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700813};
814
815} // namespace android
816
Dominik Laskowski75788452021-02-09 18:51:25 -0800817#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H