blob: a56468f445b2762da5c6da4af9d518c6aa28cd83 [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 Pradhanbaa5c822019-08-30 15:27:05 -0700438 int32_t mSurfaceLeft;
439 int32_t mSurfaceTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800440 int32_t mSurfaceRight;
441 int32_t mSurfaceBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442
443 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
444 // the logical coordinate space.
445 int32_t mPhysicalWidth;
446 int32_t mPhysicalHeight;
447 int32_t mPhysicalLeft;
448 int32_t mPhysicalTop;
449
450 // The orientation may be different from the viewport orientation as it specifies
451 // the rotation of the surface coordinates required to produce the viewport's
452 // requested orientation, so it will depend on whether the device is orientation aware.
453 int32_t mSurfaceOrientation;
454
455 // Translation and scaling factors, orientation-independent.
456 float mXTranslate;
457 float mXScale;
458 float mXPrecision;
459
460 float mYTranslate;
461 float mYScale;
462 float mYPrecision;
463
464 float mGeometricScale;
465
466 float mPressureScale;
467
468 float mSizeScale;
469
470 float mOrientationScale;
471
472 float mDistanceScale;
473
474 bool mHaveTilt;
475 float mTiltXCenter;
476 float mTiltXScale;
477 float mTiltYCenter;
478 float mTiltYScale;
479
480 bool mExternalStylusConnected;
481
482 // Oriented motion ranges for input device info.
483 struct OrientedRanges {
484 InputDeviceInfo::MotionRange x;
485 InputDeviceInfo::MotionRange y;
486 InputDeviceInfo::MotionRange pressure;
487
488 bool haveSize;
489 InputDeviceInfo::MotionRange size;
490
491 bool haveTouchSize;
492 InputDeviceInfo::MotionRange touchMajor;
493 InputDeviceInfo::MotionRange touchMinor;
494
495 bool haveToolSize;
496 InputDeviceInfo::MotionRange toolMajor;
497 InputDeviceInfo::MotionRange toolMinor;
498
499 bool haveOrientation;
500 InputDeviceInfo::MotionRange orientation;
501
502 bool haveDistance;
503 InputDeviceInfo::MotionRange distance;
504
505 bool haveTilt;
506 InputDeviceInfo::MotionRange tilt;
507
508 OrientedRanges() { clear(); }
509
510 void clear() {
511 haveSize = false;
512 haveTouchSize = false;
513 haveToolSize = false;
514 haveOrientation = false;
515 haveDistance = false;
516 haveTilt = false;
517 }
518 } mOrientedRanges;
519
520 // Oriented dimensions and precision.
521 float mOrientedXPrecision;
522 float mOrientedYPrecision;
523
524 struct CurrentVirtualKeyState {
525 bool down;
526 bool ignored;
527 nsecs_t downTime;
528 int32_t keyCode;
529 int32_t scanCode;
530 } mCurrentVirtualKey;
531
532 // Scale factor for gesture or mouse based pointer movements.
533 float mPointerXMovementScale;
534 float mPointerYMovementScale;
535
536 // Scale factor for gesture based zooming and other freeform motions.
537 float mPointerXZoomScale;
538 float mPointerYZoomScale;
539
540 // The maximum swipe width.
541 float mPointerGestureMaxSwipeWidth;
542
543 struct PointerDistanceHeapElement {
544 uint32_t currentPointerIndex : 8;
545 uint32_t lastPointerIndex : 8;
546 uint64_t distance : 48; // squared distance
547 };
548
Michael Wright227c5542020-07-02 18:30:52 +0100549 enum class PointerUsage {
550 NONE,
551 GESTURES,
552 STYLUS,
553 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700554 };
555 PointerUsage mPointerUsage;
556
557 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100558 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700559 // No fingers, button is not pressed.
560 // Nothing happening.
561 NEUTRAL,
562
563 // No fingers, button is not pressed.
564 // Tap detected.
565 // Emits DOWN and UP events at the pointer location.
566 TAP,
567
568 // Exactly one finger dragging following a tap.
569 // Pointer follows the active finger.
570 // Emits DOWN, MOVE and UP events at the pointer location.
571 //
572 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
573 TAP_DRAG,
574
575 // Button is pressed.
576 // Pointer follows the active finger if there is one. Other fingers are ignored.
577 // Emits DOWN, MOVE and UP events at the pointer location.
578 BUTTON_CLICK_OR_DRAG,
579
580 // Exactly one finger, button is not pressed.
581 // Pointer follows the active finger.
582 // Emits HOVER_MOVE events at the pointer location.
583 //
584 // Detect taps when the finger goes up while in HOVER mode.
585 HOVER,
586
587 // Exactly two fingers but neither have moved enough to clearly indicate
588 // whether a swipe or freeform gesture was intended. We consider the
589 // pointer to be pressed so this enables clicking or long-pressing on buttons.
590 // Pointer does not move.
591 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
592 PRESS,
593
594 // Exactly two fingers moving in the same direction, button is not pressed.
595 // Pointer does not move.
596 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
597 // follows the midpoint between both fingers.
598 SWIPE,
599
600 // Two or more fingers moving in arbitrary directions, button is not pressed.
601 // Pointer does not move.
602 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
603 // each finger individually relative to the initial centroid of the finger.
604 FREEFORM,
605
606 // Waiting for quiet time to end before starting the next gesture.
607 QUIET,
608 };
609
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800610 // When a gesture is sent to an unfocused window, return true if it can bring that window
611 // into focus, false otherwise.
612 static bool canGestureAffectWindowFocus(Mode mode) {
613 switch (mode) {
614 case Mode::TAP:
615 case Mode::TAP_DRAG:
616 case Mode::BUTTON_CLICK_OR_DRAG:
617 // Taps can affect window focus.
618 return true;
619 case Mode::FREEFORM:
620 case Mode::HOVER:
621 case Mode::NEUTRAL:
622 case Mode::PRESS:
623 case Mode::QUIET:
624 case Mode::SWIPE:
625 // Most gestures can be performed on an unfocused window, so they should not
626 // not affect window focus.
627 return false;
628 }
629 }
630
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700631 // Time the first finger went down.
632 nsecs_t firstTouchTime;
633
634 // The active pointer id from the raw touch data.
635 int32_t activeTouchId; // -1 if none
636
637 // The active pointer id from the gesture last delivered to the application.
638 int32_t activeGestureId; // -1 if none
639
640 // Pointer coords and ids for the current and previous pointer gesture.
641 Mode currentGestureMode;
642 BitSet32 currentGestureIdBits;
643 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
644 PointerProperties currentGestureProperties[MAX_POINTERS];
645 PointerCoords currentGestureCoords[MAX_POINTERS];
646
647 Mode lastGestureMode;
648 BitSet32 lastGestureIdBits;
649 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
650 PointerProperties lastGestureProperties[MAX_POINTERS];
651 PointerCoords lastGestureCoords[MAX_POINTERS];
652
653 // Time the pointer gesture last went down.
654 nsecs_t downTime;
655
656 // Time when the pointer went down for a TAP.
657 nsecs_t tapDownTime;
658
659 // Time when the pointer went up for a TAP.
660 nsecs_t tapUpTime;
661
662 // Location of initial tap.
663 float tapX, tapY;
664
665 // Time we started waiting for quiescence.
666 nsecs_t quietTime;
667
668 // Reference points for multitouch gestures.
669 float referenceTouchX; // reference touch X/Y coordinates in surface units
670 float referenceTouchY;
671 float referenceGestureX; // reference gesture X/Y coordinates in pixels
672 float referenceGestureY;
673
674 // Distance that each pointer has traveled which has not yet been
675 // subsumed into the reference gesture position.
676 BitSet32 referenceIdBits;
677 struct Delta {
678 float dx, dy;
679 };
680 Delta referenceDeltas[MAX_POINTER_ID + 1];
681
682 // Describes how touch ids are mapped to gesture ids for freeform gestures.
683 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
684
685 // A velocity tracker for determining whether to switch active pointers during drags.
686 VelocityTracker velocityTracker;
687
688 void reset() {
689 firstTouchTime = LLONG_MIN;
690 activeTouchId = -1;
691 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100692 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700693 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100694 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700695 lastGestureIdBits.clear();
696 downTime = 0;
697 velocityTracker.clear();
698 resetTap();
699 resetQuietTime();
700 }
701
702 void resetTap() {
703 tapDownTime = LLONG_MIN;
704 tapUpTime = LLONG_MIN;
705 }
706
707 void resetQuietTime() { quietTime = LLONG_MIN; }
708 } mPointerGesture;
709
710 struct PointerSimple {
711 PointerCoords currentCoords;
712 PointerProperties currentProperties;
713 PointerCoords lastCoords;
714 PointerProperties lastProperties;
715
716 // True if the pointer is down.
717 bool down;
718
719 // True if the pointer is hovering.
720 bool hovering;
721
722 // Time the pointer last went down.
723 nsecs_t downTime;
724
725 void reset() {
726 currentCoords.clear();
727 currentProperties.clear();
728 lastCoords.clear();
729 lastProperties.clear();
730 down = false;
731 hovering = false;
732 downTime = 0;
733 }
734 } mPointerSimple;
735
736 // The pointer and scroll velocity controls.
737 VelocityControl mPointerVelocityControl;
738 VelocityControl mWheelXVelocityControl;
739 VelocityControl mWheelYVelocityControl;
740
741 std::optional<DisplayViewport> findViewport();
742
743 void resetExternalStylus();
744 void clearStylusDataPendingFlags();
745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000746 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700747
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000748 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700749 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000750 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
751 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
752 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700753
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000754 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
755 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
756 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
757 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
758 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700759 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
760 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000761 void abortTouches(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 dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
764 PointerUsage pointerUsage);
765 void abortPointerUsage(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 dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
768 bool isTimeout);
769 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700770 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
771 bool* outFinishPreviousGesture, bool isTimeout);
772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000773 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
774 void abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000776 void dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
777 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700778
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000779 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
780 bool hovering);
781 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700782
783 bool assignExternalStylusId(const RawState& state, bool timeout);
784 void applyExternalStylusButtonState(nsecs_t when);
785 void applyExternalStylusTouchState(nsecs_t when);
786
787 // Dispatches a motion event.
788 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
789 // method will take care of setting the index and transmuting the action to DOWN or UP
790 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000791 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
792 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
793 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700794 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
795 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
796
797 // Updates pointer coords and properties for pointers with specified ids that have moved.
798 // Returns true if any of them changed.
799 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
800 const uint32_t* inIdToIndex, PointerProperties* outProperties,
801 PointerCoords* outCoords, const uint32_t* outIdToIndex,
802 BitSet32 idBits) const;
803
Garfield Tanc734e4f2021-01-15 20:01:39 -0800804 // Returns if this touch device is a touch screen with an associated display.
805 bool isTouchScreen();
806 // Updates touch spots if they are enabled. Should only be used when this device is a
807 // touchscreen.
808 void updateTouchSpots();
809
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700810 bool isPointInsideSurface(int32_t x, int32_t y);
811 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
812
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000813 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700814
815 const char* modeToString(DeviceMode deviceMode);
Arthur Hung05de5772019-09-26 18:31:26 +0800816 void rotateAndScale(float& x, float& y);
Prabir Pradhand7482e72021-03-09 13:54:55 -0800817
818 // Wrapper methods for interfacing with PointerController. These are used to convert points
819 // between the coordinate spaces used by InputReader and PointerController, if they differ.
820 void moveMouseCursor(float dx, float dy) const;
821 std::pair<float, float> getMouseCursorPosition() const;
822 void setMouseCursorPosition(float x, float y) const;
823 void setTouchSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
824 BitSet32 spotIdBits, int32_t displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700825};
826
827} // namespace android
828
Dominik Laskowski75788452021-02-09 18:51:25 -0800829#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H