blob: 334067202196287e68a1f09ffb531fa57a142ac2 [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
Michael Wright227c5542020-07-02 18:30:52 +0100141 uint32_t getSources() override;
142 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;
150 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
151 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,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 };
247
248 SizeCalibration sizeCalibration;
249
250 bool haveSizeScale;
251 float sizeScale;
252 bool haveSizeBias;
253 float sizeBias;
254 bool haveSizeIsSummed;
255 bool sizeIsSummed;
256
257 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100258 enum class PressureCalibration {
259 DEFAULT,
260 NONE,
261 PHYSICAL,
262 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 };
264
265 PressureCalibration pressureCalibration;
266 bool havePressureScale;
267 float pressureScale;
268
269 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100270 enum class OrientationCalibration {
271 DEFAULT,
272 NONE,
273 INTERPOLATED,
274 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700275 };
276
277 OrientationCalibration orientationCalibration;
278
279 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100280 enum class DistanceCalibration {
281 DEFAULT,
282 NONE,
283 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700284 };
285
286 DistanceCalibration distanceCalibration;
287 bool haveDistanceScale;
288 float distanceScale;
289
Michael Wright227c5542020-07-02 18:30:52 +0100290 enum class CoverageCalibration {
291 DEFAULT,
292 NONE,
293 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700294 };
295
296 CoverageCalibration coverageCalibration;
297
298 inline void applySizeScaleAndBias(float* outSize) const {
299 if (haveSizeScale) {
300 *outSize *= sizeScale;
301 }
302 if (haveSizeBias) {
303 *outSize += sizeBias;
304 }
305 if (*outSize < 0) {
306 *outSize = 0;
307 }
308 }
309 } mCalibration;
310
311 // Affine location transformation/calibration
312 struct TouchAffineTransformation mAffineTransform;
313
314 RawPointerAxes mRawPointerAxes;
315
316 struct RawState {
317 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000318 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700319
320 // Raw pointer sample data.
321 RawPointerData rawPointerData;
322
323 int32_t buttonState;
324
325 // Scroll state.
326 int32_t rawVScroll;
327 int32_t rawHScroll;
328
329 void copyFrom(const RawState& other) {
330 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000331 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700332 rawPointerData.copyFrom(other.rawPointerData);
333 buttonState = other.buttonState;
334 rawVScroll = other.rawVScroll;
335 rawHScroll = other.rawHScroll;
336 }
337
338 void clear() {
339 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000340 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341 rawPointerData.clear();
342 buttonState = 0;
343 rawVScroll = 0;
344 rawHScroll = 0;
345 }
346 };
347
348 struct CookedState {
349 // Cooked pointer sample data.
350 CookedPointerData cookedPointerData;
351
352 // Id bits used to differentiate fingers, stylus and mouse tools.
353 BitSet32 fingerIdBits;
354 BitSet32 stylusIdBits;
355 BitSet32 mouseIdBits;
356
357 int32_t buttonState;
358
359 void copyFrom(const CookedState& other) {
360 cookedPointerData.copyFrom(other.cookedPointerData);
361 fingerIdBits = other.fingerIdBits;
362 stylusIdBits = other.stylusIdBits;
363 mouseIdBits = other.mouseIdBits;
364 buttonState = other.buttonState;
365 }
366
367 void clear() {
368 cookedPointerData.clear();
369 fingerIdBits.clear();
370 stylusIdBits.clear();
371 mouseIdBits.clear();
372 buttonState = 0;
373 }
374 };
375
376 std::vector<RawState> mRawStatesPending;
377 RawState mCurrentRawState;
378 CookedState mCurrentCookedState;
379 RawState mLastRawState;
380 CookedState mLastCookedState;
381
382 // State provided by an external stylus
383 StylusState mExternalStylusState;
384 int64_t mExternalStylusId;
385 nsecs_t mExternalStylusFusionTimeout;
386 bool mExternalStylusDataPending;
387
388 // True if we sent a HOVER_ENTER event.
389 bool mSentHoverEnter;
390
391 // Have we assigned pointer IDs for this stream
392 bool mHavePointerIds;
393
394 // Is the current stream of direct touch events aborted
395 bool mCurrentMotionAborted;
396
397 // The time the primary pointer last went down.
398 nsecs_t mDownTime;
399
400 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100401 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402
403 std::vector<VirtualKey> mVirtualKeys;
404
405 virtual void configureParameters();
406 virtual void dumpParameters(std::string& dump);
407 virtual void configureRawPointerAxes();
408 virtual void dumpRawPointerAxes(std::string& dump);
409 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
410 virtual void dumpSurface(std::string& dump);
411 virtual void configureVirtualKeys();
412 virtual void dumpVirtualKeys(std::string& dump);
413 virtual void parseCalibration();
414 virtual void resolveCalibration();
415 virtual void dumpCalibration(std::string& dump);
416 virtual void updateAffineTransformation();
417 virtual void dumpAffineTransformation(std::string& dump);
418 virtual void resolveExternalStylusPresence();
419 virtual bool hasStylus() const = 0;
420 virtual bool hasExternalStylus() const;
421
422 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
423
424private:
425 // The current viewport.
426 // The components of the viewport are specified in the display's rotated orientation.
427 DisplayViewport mViewport;
428
429 // The surface orientation, width and height set by configureSurface().
430 // The width and height are derived from the viewport but are specified
431 // in the natural orientation.
Arthur Hung4197f6b2020-03-16 15:39:59 +0800432 // They could be used for calculating diagonal, scaling factors, and virtual keys.
433 int32_t mRawSurfaceWidth;
434 int32_t mRawSurfaceHeight;
435
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700436 // The surface origin specifies how the surface coordinates should be translated
437 // to align with the logical display coordinate space.
Prabir Pradhan5632d622021-09-06 07:57:20 -0700438 // TODO(b/188939842): Remove surface coordinates when Per-Window Input Rotation is enabled.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439 int32_t mSurfaceLeft;
440 int32_t mSurfaceTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800441 int32_t mSurfaceRight;
442 int32_t mSurfaceBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443
444 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
445 // the logical coordinate space.
446 int32_t mPhysicalWidth;
447 int32_t mPhysicalHeight;
448 int32_t mPhysicalLeft;
449 int32_t mPhysicalTop;
450
451 // The orientation may be different from the viewport orientation as it specifies
452 // the rotation of the surface coordinates required to produce the viewport's
453 // requested orientation, so it will depend on whether the device is orientation aware.
454 int32_t mSurfaceOrientation;
455
456 // Translation and scaling factors, orientation-independent.
457 float mXTranslate;
458 float mXScale;
459 float mXPrecision;
460
461 float mYTranslate;
462 float mYScale;
463 float mYPrecision;
464
465 float mGeometricScale;
466
467 float mPressureScale;
468
469 float mSizeScale;
470
471 float mOrientationScale;
472
473 float mDistanceScale;
474
475 bool mHaveTilt;
476 float mTiltXCenter;
477 float mTiltXScale;
478 float mTiltYCenter;
479 float mTiltYScale;
480
481 bool mExternalStylusConnected;
482
483 // Oriented motion ranges for input device info.
484 struct OrientedRanges {
485 InputDeviceInfo::MotionRange x;
486 InputDeviceInfo::MotionRange y;
487 InputDeviceInfo::MotionRange pressure;
488
489 bool haveSize;
490 InputDeviceInfo::MotionRange size;
491
492 bool haveTouchSize;
493 InputDeviceInfo::MotionRange touchMajor;
494 InputDeviceInfo::MotionRange touchMinor;
495
496 bool haveToolSize;
497 InputDeviceInfo::MotionRange toolMajor;
498 InputDeviceInfo::MotionRange toolMinor;
499
500 bool haveOrientation;
501 InputDeviceInfo::MotionRange orientation;
502
503 bool haveDistance;
504 InputDeviceInfo::MotionRange distance;
505
506 bool haveTilt;
507 InputDeviceInfo::MotionRange tilt;
508
509 OrientedRanges() { clear(); }
510
511 void clear() {
512 haveSize = false;
513 haveTouchSize = false;
514 haveToolSize = false;
515 haveOrientation = false;
516 haveDistance = false;
517 haveTilt = false;
518 }
519 } mOrientedRanges;
520
521 // Oriented dimensions and precision.
522 float mOrientedXPrecision;
523 float mOrientedYPrecision;
524
525 struct CurrentVirtualKeyState {
526 bool down;
527 bool ignored;
528 nsecs_t downTime;
529 int32_t keyCode;
530 int32_t scanCode;
531 } mCurrentVirtualKey;
532
533 // Scale factor for gesture or mouse based pointer movements.
534 float mPointerXMovementScale;
535 float mPointerYMovementScale;
536
537 // Scale factor for gesture based zooming and other freeform motions.
538 float mPointerXZoomScale;
539 float mPointerYZoomScale;
540
541 // The maximum swipe width.
542 float mPointerGestureMaxSwipeWidth;
543
544 struct PointerDistanceHeapElement {
545 uint32_t currentPointerIndex : 8;
546 uint32_t lastPointerIndex : 8;
547 uint64_t distance : 48; // squared distance
548 };
549
Michael Wright227c5542020-07-02 18:30:52 +0100550 enum class PointerUsage {
551 NONE,
552 GESTURES,
553 STYLUS,
554 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700555 };
556 PointerUsage mPointerUsage;
557
558 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100559 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700560 // No fingers, button is not pressed.
561 // Nothing happening.
562 NEUTRAL,
563
564 // No fingers, button is not pressed.
565 // Tap detected.
566 // Emits DOWN and UP events at the pointer location.
567 TAP,
568
569 // Exactly one finger dragging following a tap.
570 // Pointer follows the active finger.
571 // Emits DOWN, MOVE and UP events at the pointer location.
572 //
573 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
574 TAP_DRAG,
575
576 // Button is pressed.
577 // Pointer follows the active finger if there is one. Other fingers are ignored.
578 // Emits DOWN, MOVE and UP events at the pointer location.
579 BUTTON_CLICK_OR_DRAG,
580
581 // Exactly one finger, button is not pressed.
582 // Pointer follows the active finger.
583 // Emits HOVER_MOVE events at the pointer location.
584 //
585 // Detect taps when the finger goes up while in HOVER mode.
586 HOVER,
587
588 // Exactly two fingers but neither have moved enough to clearly indicate
589 // whether a swipe or freeform gesture was intended. We consider the
590 // pointer to be pressed so this enables clicking or long-pressing on buttons.
591 // Pointer does not move.
592 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
593 PRESS,
594
595 // Exactly two fingers moving in the same direction, button is not pressed.
596 // Pointer does not move.
597 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
598 // follows the midpoint between both fingers.
599 SWIPE,
600
601 // Two or more fingers moving in arbitrary directions, button is not pressed.
602 // Pointer does not move.
603 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
604 // each finger individually relative to the initial centroid of the finger.
605 FREEFORM,
606
607 // Waiting for quiet time to end before starting the next gesture.
608 QUIET,
609 };
610
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800611 // When a gesture is sent to an unfocused window, return true if it can bring that window
612 // into focus, false otherwise.
613 static bool canGestureAffectWindowFocus(Mode mode) {
614 switch (mode) {
615 case Mode::TAP:
616 case Mode::TAP_DRAG:
617 case Mode::BUTTON_CLICK_OR_DRAG:
618 // Taps can affect window focus.
619 return true;
620 case Mode::FREEFORM:
621 case Mode::HOVER:
622 case Mode::NEUTRAL:
623 case Mode::PRESS:
624 case Mode::QUIET:
625 case Mode::SWIPE:
626 // Most gestures can be performed on an unfocused window, so they should not
627 // not affect window focus.
628 return false;
629 }
630 }
631
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700632 // Time the first finger went down.
633 nsecs_t firstTouchTime;
634
635 // The active pointer id from the raw touch data.
636 int32_t activeTouchId; // -1 if none
637
638 // The active pointer id from the gesture last delivered to the application.
639 int32_t activeGestureId; // -1 if none
640
641 // Pointer coords and ids for the current and previous pointer gesture.
642 Mode currentGestureMode;
643 BitSet32 currentGestureIdBits;
644 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
645 PointerProperties currentGestureProperties[MAX_POINTERS];
646 PointerCoords currentGestureCoords[MAX_POINTERS];
647
648 Mode lastGestureMode;
649 BitSet32 lastGestureIdBits;
650 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
651 PointerProperties lastGestureProperties[MAX_POINTERS];
652 PointerCoords lastGestureCoords[MAX_POINTERS];
653
654 // Time the pointer gesture last went down.
655 nsecs_t downTime;
656
657 // Time when the pointer went down for a TAP.
658 nsecs_t tapDownTime;
659
660 // Time when the pointer went up for a TAP.
661 nsecs_t tapUpTime;
662
663 // Location of initial tap.
664 float tapX, tapY;
665
666 // Time we started waiting for quiescence.
667 nsecs_t quietTime;
668
669 // Reference points for multitouch gestures.
670 float referenceTouchX; // reference touch X/Y coordinates in surface units
671 float referenceTouchY;
672 float referenceGestureX; // reference gesture X/Y coordinates in pixels
673 float referenceGestureY;
674
675 // Distance that each pointer has traveled which has not yet been
676 // subsumed into the reference gesture position.
677 BitSet32 referenceIdBits;
678 struct Delta {
679 float dx, dy;
680 };
681 Delta referenceDeltas[MAX_POINTER_ID + 1];
682
683 // Describes how touch ids are mapped to gesture ids for freeform gestures.
684 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
685
686 // A velocity tracker for determining whether to switch active pointers during drags.
687 VelocityTracker velocityTracker;
688
689 void reset() {
690 firstTouchTime = LLONG_MIN;
691 activeTouchId = -1;
692 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100693 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700694 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100695 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700696 lastGestureIdBits.clear();
697 downTime = 0;
698 velocityTracker.clear();
699 resetTap();
700 resetQuietTime();
701 }
702
703 void resetTap() {
704 tapDownTime = LLONG_MIN;
705 tapUpTime = LLONG_MIN;
706 }
707
708 void resetQuietTime() { quietTime = LLONG_MIN; }
709 } mPointerGesture;
710
711 struct PointerSimple {
712 PointerCoords currentCoords;
713 PointerProperties currentProperties;
714 PointerCoords lastCoords;
715 PointerProperties lastProperties;
716
717 // True if the pointer is down.
718 bool down;
719
720 // True if the pointer is hovering.
721 bool hovering;
722
723 // Time the pointer last went down.
724 nsecs_t downTime;
725
726 void reset() {
727 currentCoords.clear();
728 currentProperties.clear();
729 lastCoords.clear();
730 lastProperties.clear();
731 down = false;
732 hovering = false;
733 downTime = 0;
734 }
735 } mPointerSimple;
736
737 // The pointer and scroll velocity controls.
738 VelocityControl mPointerVelocityControl;
739 VelocityControl mWheelXVelocityControl;
740 VelocityControl mWheelYVelocityControl;
741
742 std::optional<DisplayViewport> findViewport();
743
744 void resetExternalStylus();
745 void clearStylusDataPendingFlags();
746
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000747 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000749 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700750 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000751 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
752 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
753 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000755 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
756 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
757 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
758 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
759 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700760 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
761 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000762 void abortTouches(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 dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
765 PointerUsage pointerUsage);
766 void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000768 void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
769 bool isTimeout);
770 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700771 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
772 bool* outFinishPreviousGesture, bool isTimeout);
773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000774 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
775 void abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000777 void dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
778 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000780 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
781 bool hovering);
782 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700783
784 bool assignExternalStylusId(const RawState& state, bool timeout);
785 void applyExternalStylusButtonState(nsecs_t when);
786 void applyExternalStylusTouchState(nsecs_t when);
787
788 // Dispatches a motion event.
789 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
790 // method will take care of setting the index and transmuting the action to DOWN or UP
791 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000792 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
793 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
794 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700795 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
796 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
797
798 // Updates pointer coords and properties for pointers with specified ids that have moved.
799 // Returns true if any of them changed.
800 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
801 const uint32_t* inIdToIndex, PointerProperties* outProperties,
802 PointerCoords* outCoords, const uint32_t* outIdToIndex,
803 BitSet32 idBits) const;
804
Garfield Tanc734e4f2021-01-15 20:01:39 -0800805 // Returns if this touch device is a touch screen with an associated display.
806 bool isTouchScreen();
807 // Updates touch spots if they are enabled. Should only be used when this device is a
808 // touchscreen.
809 void updateTouchSpots();
810
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700811 bool isPointInsideSurface(int32_t x, int32_t y);
812 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
813
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000814 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700815
816 const char* modeToString(DeviceMode deviceMode);
Arthur Hung05de5772019-09-26 18:31:26 +0800817 void rotateAndScale(float& x, float& y);
Prabir Pradhand7482e72021-03-09 13:54:55 -0800818
819 // Wrapper methods for interfacing with PointerController. These are used to convert points
820 // between the coordinate spaces used by InputReader and PointerController, if they differ.
821 void moveMouseCursor(float dx, float dy) const;
822 std::pair<float, float> getMouseCursorPosition() const;
823 void setMouseCursorPosition(float x, float y) const;
824 void setTouchSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
825 BitSet32 spotIdBits, int32_t displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700826};
827
828} // namespace android
829
Dominik Laskowski75788452021-02-09 18:51:25 -0800830#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H