blob: cb52e2d7740137dca8dbdbd4915b9f1eb090b419 [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)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700188 };
189 DeviceMode mDeviceMode;
190
191 // The reader's configuration.
192 InputReaderConfiguration mConfig;
193
194 // Immutable configuration parameters.
195 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100196 enum class DeviceType {
197 TOUCH_SCREEN,
198 TOUCH_PAD,
199 TOUCH_NAVIGATION,
200 POINTER,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700201 };
202
203 DeviceType deviceType;
204 bool hasAssociatedDisplay;
205 bool associatedDisplayIsExternal;
206 bool orientationAware;
207 bool hasButtonUnderPad;
208 std::string uniqueDisplayId;
209
Michael Wright227c5542020-07-02 18:30:52 +0100210 enum class GestureMode {
211 SINGLE_TOUCH,
212 MULTI_TOUCH,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700213 };
214 GestureMode gestureMode;
215
216 bool wake;
217 } mParameters;
218
219 // Immutable calibration parameters in parsed form.
220 struct Calibration {
221 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100222 enum class SizeCalibration {
223 DEFAULT,
224 NONE,
225 GEOMETRIC,
226 DIAMETER,
227 BOX,
228 AREA,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 };
230
231 SizeCalibration sizeCalibration;
232
233 bool haveSizeScale;
234 float sizeScale;
235 bool haveSizeBias;
236 float sizeBias;
237 bool haveSizeIsSummed;
238 bool sizeIsSummed;
239
240 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100241 enum class PressureCalibration {
242 DEFAULT,
243 NONE,
244 PHYSICAL,
245 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 };
247
248 PressureCalibration pressureCalibration;
249 bool havePressureScale;
250 float pressureScale;
251
252 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100253 enum class OrientationCalibration {
254 DEFAULT,
255 NONE,
256 INTERPOLATED,
257 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700258 };
259
260 OrientationCalibration orientationCalibration;
261
262 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100263 enum class DistanceCalibration {
264 DEFAULT,
265 NONE,
266 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700267 };
268
269 DistanceCalibration distanceCalibration;
270 bool haveDistanceScale;
271 float distanceScale;
272
Michael Wright227c5542020-07-02 18:30:52 +0100273 enum class CoverageCalibration {
274 DEFAULT,
275 NONE,
276 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700277 };
278
279 CoverageCalibration coverageCalibration;
280
281 inline void applySizeScaleAndBias(float* outSize) const {
282 if (haveSizeScale) {
283 *outSize *= sizeScale;
284 }
285 if (haveSizeBias) {
286 *outSize += sizeBias;
287 }
288 if (*outSize < 0) {
289 *outSize = 0;
290 }
291 }
292 } mCalibration;
293
294 // Affine location transformation/calibration
295 struct TouchAffineTransformation mAffineTransform;
296
297 RawPointerAxes mRawPointerAxes;
298
299 struct RawState {
300 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000301 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700302
303 // Raw pointer sample data.
304 RawPointerData rawPointerData;
305
306 int32_t buttonState;
307
308 // Scroll state.
309 int32_t rawVScroll;
310 int32_t rawHScroll;
311
312 void copyFrom(const RawState& other) {
313 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000314 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700315 rawPointerData.copyFrom(other.rawPointerData);
316 buttonState = other.buttonState;
317 rawVScroll = other.rawVScroll;
318 rawHScroll = other.rawHScroll;
319 }
320
321 void clear() {
322 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000323 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700324 rawPointerData.clear();
325 buttonState = 0;
326 rawVScroll = 0;
327 rawHScroll = 0;
328 }
329 };
330
331 struct CookedState {
332 // Cooked pointer sample data.
333 CookedPointerData cookedPointerData;
334
335 // Id bits used to differentiate fingers, stylus and mouse tools.
336 BitSet32 fingerIdBits;
337 BitSet32 stylusIdBits;
338 BitSet32 mouseIdBits;
339
340 int32_t buttonState;
341
342 void copyFrom(const CookedState& other) {
343 cookedPointerData.copyFrom(other.cookedPointerData);
344 fingerIdBits = other.fingerIdBits;
345 stylusIdBits = other.stylusIdBits;
346 mouseIdBits = other.mouseIdBits;
347 buttonState = other.buttonState;
348 }
349
350 void clear() {
351 cookedPointerData.clear();
352 fingerIdBits.clear();
353 stylusIdBits.clear();
354 mouseIdBits.clear();
355 buttonState = 0;
356 }
357 };
358
359 std::vector<RawState> mRawStatesPending;
360 RawState mCurrentRawState;
361 CookedState mCurrentCookedState;
362 RawState mLastRawState;
363 CookedState mLastCookedState;
364
365 // State provided by an external stylus
366 StylusState mExternalStylusState;
367 int64_t mExternalStylusId;
368 nsecs_t mExternalStylusFusionTimeout;
369 bool mExternalStylusDataPending;
370
371 // True if we sent a HOVER_ENTER event.
372 bool mSentHoverEnter;
373
374 // Have we assigned pointer IDs for this stream
375 bool mHavePointerIds;
376
377 // Is the current stream of direct touch events aborted
378 bool mCurrentMotionAborted;
379
380 // The time the primary pointer last went down.
381 nsecs_t mDownTime;
382
383 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100384 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700385
386 std::vector<VirtualKey> mVirtualKeys;
387
388 virtual void configureParameters();
389 virtual void dumpParameters(std::string& dump);
390 virtual void configureRawPointerAxes();
391 virtual void dumpRawPointerAxes(std::string& dump);
392 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
393 virtual void dumpSurface(std::string& dump);
394 virtual void configureVirtualKeys();
395 virtual void dumpVirtualKeys(std::string& dump);
396 virtual void parseCalibration();
397 virtual void resolveCalibration();
398 virtual void dumpCalibration(std::string& dump);
399 virtual void updateAffineTransformation();
400 virtual void dumpAffineTransformation(std::string& dump);
401 virtual void resolveExternalStylusPresence();
402 virtual bool hasStylus() const = 0;
403 virtual bool hasExternalStylus() const;
404
405 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
406
407private:
408 // The current viewport.
409 // The components of the viewport are specified in the display's rotated orientation.
410 DisplayViewport mViewport;
411
412 // The surface orientation, width and height set by configureSurface().
413 // The width and height are derived from the viewport but are specified
414 // in the natural orientation.
Arthur Hung4197f6b2020-03-16 15:39:59 +0800415 // They could be used for calculating diagonal, scaling factors, and virtual keys.
416 int32_t mRawSurfaceWidth;
417 int32_t mRawSurfaceHeight;
418
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700419 // The surface origin specifies how the surface coordinates should be translated
420 // to align with the logical display coordinate space.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700421 int32_t mSurfaceLeft;
422 int32_t mSurfaceTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800423 int32_t mSurfaceRight;
424 int32_t mSurfaceBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425
426 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
427 // the logical coordinate space.
428 int32_t mPhysicalWidth;
429 int32_t mPhysicalHeight;
430 int32_t mPhysicalLeft;
431 int32_t mPhysicalTop;
432
433 // The orientation may be different from the viewport orientation as it specifies
434 // the rotation of the surface coordinates required to produce the viewport's
435 // requested orientation, so it will depend on whether the device is orientation aware.
436 int32_t mSurfaceOrientation;
437
438 // Translation and scaling factors, orientation-independent.
439 float mXTranslate;
440 float mXScale;
441 float mXPrecision;
442
443 float mYTranslate;
444 float mYScale;
445 float mYPrecision;
446
447 float mGeometricScale;
448
449 float mPressureScale;
450
451 float mSizeScale;
452
453 float mOrientationScale;
454
455 float mDistanceScale;
456
457 bool mHaveTilt;
458 float mTiltXCenter;
459 float mTiltXScale;
460 float mTiltYCenter;
461 float mTiltYScale;
462
463 bool mExternalStylusConnected;
464
465 // Oriented motion ranges for input device info.
466 struct OrientedRanges {
467 InputDeviceInfo::MotionRange x;
468 InputDeviceInfo::MotionRange y;
469 InputDeviceInfo::MotionRange pressure;
470
471 bool haveSize;
472 InputDeviceInfo::MotionRange size;
473
474 bool haveTouchSize;
475 InputDeviceInfo::MotionRange touchMajor;
476 InputDeviceInfo::MotionRange touchMinor;
477
478 bool haveToolSize;
479 InputDeviceInfo::MotionRange toolMajor;
480 InputDeviceInfo::MotionRange toolMinor;
481
482 bool haveOrientation;
483 InputDeviceInfo::MotionRange orientation;
484
485 bool haveDistance;
486 InputDeviceInfo::MotionRange distance;
487
488 bool haveTilt;
489 InputDeviceInfo::MotionRange tilt;
490
491 OrientedRanges() { clear(); }
492
493 void clear() {
494 haveSize = false;
495 haveTouchSize = false;
496 haveToolSize = false;
497 haveOrientation = false;
498 haveDistance = false;
499 haveTilt = false;
500 }
501 } mOrientedRanges;
502
503 // Oriented dimensions and precision.
504 float mOrientedXPrecision;
505 float mOrientedYPrecision;
506
507 struct CurrentVirtualKeyState {
508 bool down;
509 bool ignored;
510 nsecs_t downTime;
511 int32_t keyCode;
512 int32_t scanCode;
513 } mCurrentVirtualKey;
514
515 // Scale factor for gesture or mouse based pointer movements.
516 float mPointerXMovementScale;
517 float mPointerYMovementScale;
518
519 // Scale factor for gesture based zooming and other freeform motions.
520 float mPointerXZoomScale;
521 float mPointerYZoomScale;
522
523 // The maximum swipe width.
524 float mPointerGestureMaxSwipeWidth;
525
526 struct PointerDistanceHeapElement {
527 uint32_t currentPointerIndex : 8;
528 uint32_t lastPointerIndex : 8;
529 uint64_t distance : 48; // squared distance
530 };
531
Michael Wright227c5542020-07-02 18:30:52 +0100532 enum class PointerUsage {
533 NONE,
534 GESTURES,
535 STYLUS,
536 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700537 };
538 PointerUsage mPointerUsage;
539
540 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100541 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700542 // No fingers, button is not pressed.
543 // Nothing happening.
544 NEUTRAL,
545
546 // No fingers, button is not pressed.
547 // Tap detected.
548 // Emits DOWN and UP events at the pointer location.
549 TAP,
550
551 // Exactly one finger dragging following a tap.
552 // Pointer follows the active finger.
553 // Emits DOWN, MOVE and UP events at the pointer location.
554 //
555 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
556 TAP_DRAG,
557
558 // Button is pressed.
559 // Pointer follows the active finger if there is one. Other fingers are ignored.
560 // Emits DOWN, MOVE and UP events at the pointer location.
561 BUTTON_CLICK_OR_DRAG,
562
563 // Exactly one finger, button is not pressed.
564 // Pointer follows the active finger.
565 // Emits HOVER_MOVE events at the pointer location.
566 //
567 // Detect taps when the finger goes up while in HOVER mode.
568 HOVER,
569
570 // Exactly two fingers but neither have moved enough to clearly indicate
571 // whether a swipe or freeform gesture was intended. We consider the
572 // pointer to be pressed so this enables clicking or long-pressing on buttons.
573 // Pointer does not move.
574 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
575 PRESS,
576
577 // Exactly two fingers moving in the same direction, button is not pressed.
578 // Pointer does not move.
579 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
580 // follows the midpoint between both fingers.
581 SWIPE,
582
583 // Two or more fingers moving in arbitrary directions, button is not pressed.
584 // Pointer does not move.
585 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
586 // each finger individually relative to the initial centroid of the finger.
587 FREEFORM,
588
589 // Waiting for quiet time to end before starting the next gesture.
590 QUIET,
591 };
592
593 // Time the first finger went down.
594 nsecs_t firstTouchTime;
595
596 // The active pointer id from the raw touch data.
597 int32_t activeTouchId; // -1 if none
598
599 // The active pointer id from the gesture last delivered to the application.
600 int32_t activeGestureId; // -1 if none
601
602 // Pointer coords and ids for the current and previous pointer gesture.
603 Mode currentGestureMode;
604 BitSet32 currentGestureIdBits;
605 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
606 PointerProperties currentGestureProperties[MAX_POINTERS];
607 PointerCoords currentGestureCoords[MAX_POINTERS];
608
609 Mode lastGestureMode;
610 BitSet32 lastGestureIdBits;
611 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
612 PointerProperties lastGestureProperties[MAX_POINTERS];
613 PointerCoords lastGestureCoords[MAX_POINTERS];
614
615 // Time the pointer gesture last went down.
616 nsecs_t downTime;
617
618 // Time when the pointer went down for a TAP.
619 nsecs_t tapDownTime;
620
621 // Time when the pointer went up for a TAP.
622 nsecs_t tapUpTime;
623
624 // Location of initial tap.
625 float tapX, tapY;
626
627 // Time we started waiting for quiescence.
628 nsecs_t quietTime;
629
630 // Reference points for multitouch gestures.
631 float referenceTouchX; // reference touch X/Y coordinates in surface units
632 float referenceTouchY;
633 float referenceGestureX; // reference gesture X/Y coordinates in pixels
634 float referenceGestureY;
635
636 // Distance that each pointer has traveled which has not yet been
637 // subsumed into the reference gesture position.
638 BitSet32 referenceIdBits;
639 struct Delta {
640 float dx, dy;
641 };
642 Delta referenceDeltas[MAX_POINTER_ID + 1];
643
644 // Describes how touch ids are mapped to gesture ids for freeform gestures.
645 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
646
647 // A velocity tracker for determining whether to switch active pointers during drags.
648 VelocityTracker velocityTracker;
649
650 void reset() {
651 firstTouchTime = LLONG_MIN;
652 activeTouchId = -1;
653 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100654 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700655 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100656 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700657 lastGestureIdBits.clear();
658 downTime = 0;
659 velocityTracker.clear();
660 resetTap();
661 resetQuietTime();
662 }
663
664 void resetTap() {
665 tapDownTime = LLONG_MIN;
666 tapUpTime = LLONG_MIN;
667 }
668
669 void resetQuietTime() { quietTime = LLONG_MIN; }
670 } mPointerGesture;
671
672 struct PointerSimple {
673 PointerCoords currentCoords;
674 PointerProperties currentProperties;
675 PointerCoords lastCoords;
676 PointerProperties lastProperties;
677
678 // True if the pointer is down.
679 bool down;
680
681 // True if the pointer is hovering.
682 bool hovering;
683
684 // Time the pointer last went down.
685 nsecs_t downTime;
686
687 void reset() {
688 currentCoords.clear();
689 currentProperties.clear();
690 lastCoords.clear();
691 lastProperties.clear();
692 down = false;
693 hovering = false;
694 downTime = 0;
695 }
696 } mPointerSimple;
697
698 // The pointer and scroll velocity controls.
699 VelocityControl mPointerVelocityControl;
700 VelocityControl mWheelXVelocityControl;
701 VelocityControl mWheelYVelocityControl;
702
703 std::optional<DisplayViewport> findViewport();
704
705 void resetExternalStylus();
706 void clearStylusDataPendingFlags();
707
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000708 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700709
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000710 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700711 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000712 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
713 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
714 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700715
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000716 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
717 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
718 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
719 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
720 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700721 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
722 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000723 void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000725 void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
726 PointerUsage pointerUsage);
727 void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700728
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000729 void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
730 bool isTimeout);
731 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700732 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
733 bool* outFinishPreviousGesture, bool isTimeout);
734
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000735 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
736 void abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700737
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000738 void dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
739 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000741 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
742 bool hovering);
743 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700744
745 bool assignExternalStylusId(const RawState& state, bool timeout);
746 void applyExternalStylusButtonState(nsecs_t when);
747 void applyExternalStylusTouchState(nsecs_t when);
748
749 // Dispatches a motion event.
750 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
751 // method will take care of setting the index and transmuting the action to DOWN or UP
752 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000753 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
754 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
755 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700756 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
757 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
758
759 // Updates pointer coords and properties for pointers with specified ids that have moved.
760 // Returns true if any of them changed.
761 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
762 const uint32_t* inIdToIndex, PointerProperties* outProperties,
763 PointerCoords* outCoords, const uint32_t* outIdToIndex,
764 BitSet32 idBits) const;
765
Garfield Tanc734e4f2021-01-15 20:01:39 -0800766 // Returns if this touch device is a touch screen with an associated display.
767 bool isTouchScreen();
768 // Updates touch spots if they are enabled. Should only be used when this device is a
769 // touchscreen.
770 void updateTouchSpots();
771
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700772 bool isPointInsideSurface(int32_t x, int32_t y);
773 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
774
775 static void assignPointerIds(const RawState* last, RawState* current);
776
777 const char* modeToString(DeviceMode deviceMode);
Arthur Hung05de5772019-09-26 18:31:26 +0800778 void rotateAndScale(float& x, float& y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700779};
780
781} // namespace android
782
783#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H