blob: e104220e47d32d54eeb7f6671a02d518c12acddf [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;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700207
208 enum class Orientation : int32_t {
209 ORIENTATION_0 = DISPLAY_ORIENTATION_0,
210 ORIENTATION_90 = DISPLAY_ORIENTATION_90,
211 ORIENTATION_180 = DISPLAY_ORIENTATION_180,
212 ORIENTATION_270 = DISPLAY_ORIENTATION_270,
213 };
214 Orientation orientation;
215
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700216 bool hasButtonUnderPad;
217 std::string uniqueDisplayId;
218
Michael Wright227c5542020-07-02 18:30:52 +0100219 enum class GestureMode {
220 SINGLE_TOUCH,
221 MULTI_TOUCH,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700222 };
223 GestureMode gestureMode;
224
225 bool wake;
226 } mParameters;
227
228 // Immutable calibration parameters in parsed form.
229 struct Calibration {
230 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100231 enum class SizeCalibration {
232 DEFAULT,
233 NONE,
234 GEOMETRIC,
235 DIAMETER,
236 BOX,
237 AREA,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700238 };
239
240 SizeCalibration sizeCalibration;
241
242 bool haveSizeScale;
243 float sizeScale;
244 bool haveSizeBias;
245 float sizeBias;
246 bool haveSizeIsSummed;
247 bool sizeIsSummed;
248
249 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100250 enum class PressureCalibration {
251 DEFAULT,
252 NONE,
253 PHYSICAL,
254 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 };
256
257 PressureCalibration pressureCalibration;
258 bool havePressureScale;
259 float pressureScale;
260
261 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100262 enum class OrientationCalibration {
263 DEFAULT,
264 NONE,
265 INTERPOLATED,
266 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700267 };
268
269 OrientationCalibration orientationCalibration;
270
271 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100272 enum class DistanceCalibration {
273 DEFAULT,
274 NONE,
275 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 };
277
278 DistanceCalibration distanceCalibration;
279 bool haveDistanceScale;
280 float distanceScale;
281
Michael Wright227c5542020-07-02 18:30:52 +0100282 enum class CoverageCalibration {
283 DEFAULT,
284 NONE,
285 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700286 };
287
288 CoverageCalibration coverageCalibration;
289
290 inline void applySizeScaleAndBias(float* outSize) const {
291 if (haveSizeScale) {
292 *outSize *= sizeScale;
293 }
294 if (haveSizeBias) {
295 *outSize += sizeBias;
296 }
297 if (*outSize < 0) {
298 *outSize = 0;
299 }
300 }
301 } mCalibration;
302
303 // Affine location transformation/calibration
304 struct TouchAffineTransformation mAffineTransform;
305
306 RawPointerAxes mRawPointerAxes;
307
308 struct RawState {
309 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000310 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311
312 // Raw pointer sample data.
313 RawPointerData rawPointerData;
314
315 int32_t buttonState;
316
317 // Scroll state.
318 int32_t rawVScroll;
319 int32_t rawHScroll;
320
321 void copyFrom(const RawState& other) {
322 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000323 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700324 rawPointerData.copyFrom(other.rawPointerData);
325 buttonState = other.buttonState;
326 rawVScroll = other.rawVScroll;
327 rawHScroll = other.rawHScroll;
328 }
329
330 void clear() {
331 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000332 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 rawPointerData.clear();
334 buttonState = 0;
335 rawVScroll = 0;
336 rawHScroll = 0;
337 }
338 };
339
340 struct CookedState {
341 // Cooked pointer sample data.
342 CookedPointerData cookedPointerData;
343
344 // Id bits used to differentiate fingers, stylus and mouse tools.
345 BitSet32 fingerIdBits;
346 BitSet32 stylusIdBits;
347 BitSet32 mouseIdBits;
348
349 int32_t buttonState;
350
351 void copyFrom(const CookedState& other) {
352 cookedPointerData.copyFrom(other.cookedPointerData);
353 fingerIdBits = other.fingerIdBits;
354 stylusIdBits = other.stylusIdBits;
355 mouseIdBits = other.mouseIdBits;
356 buttonState = other.buttonState;
357 }
358
359 void clear() {
360 cookedPointerData.clear();
361 fingerIdBits.clear();
362 stylusIdBits.clear();
363 mouseIdBits.clear();
364 buttonState = 0;
365 }
366 };
367
368 std::vector<RawState> mRawStatesPending;
369 RawState mCurrentRawState;
370 CookedState mCurrentCookedState;
371 RawState mLastRawState;
372 CookedState mLastCookedState;
373
374 // State provided by an external stylus
375 StylusState mExternalStylusState;
376 int64_t mExternalStylusId;
377 nsecs_t mExternalStylusFusionTimeout;
378 bool mExternalStylusDataPending;
379
380 // True if we sent a HOVER_ENTER event.
381 bool mSentHoverEnter;
382
383 // Have we assigned pointer IDs for this stream
384 bool mHavePointerIds;
385
386 // Is the current stream of direct touch events aborted
387 bool mCurrentMotionAborted;
388
389 // The time the primary pointer last went down.
390 nsecs_t mDownTime;
391
392 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100393 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394
395 std::vector<VirtualKey> mVirtualKeys;
396
397 virtual void configureParameters();
398 virtual void dumpParameters(std::string& dump);
399 virtual void configureRawPointerAxes();
400 virtual void dumpRawPointerAxes(std::string& dump);
401 virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
402 virtual void dumpSurface(std::string& dump);
403 virtual void configureVirtualKeys();
404 virtual void dumpVirtualKeys(std::string& dump);
405 virtual void parseCalibration();
406 virtual void resolveCalibration();
407 virtual void dumpCalibration(std::string& dump);
408 virtual void updateAffineTransformation();
409 virtual void dumpAffineTransformation(std::string& dump);
410 virtual void resolveExternalStylusPresence();
411 virtual bool hasStylus() const = 0;
412 virtual bool hasExternalStylus() const;
413
414 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
415
416private:
417 // The current viewport.
418 // The components of the viewport are specified in the display's rotated orientation.
419 DisplayViewport mViewport;
420
421 // The surface orientation, width and height set by configureSurface().
422 // The width and height are derived from the viewport but are specified
423 // in the natural orientation.
Arthur Hung4197f6b2020-03-16 15:39:59 +0800424 // They could be used for calculating diagonal, scaling factors, and virtual keys.
425 int32_t mRawSurfaceWidth;
426 int32_t mRawSurfaceHeight;
427
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700428 // The surface origin specifies how the surface coordinates should be translated
429 // to align with the logical display coordinate space.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430 int32_t mSurfaceLeft;
431 int32_t mSurfaceTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800432 int32_t mSurfaceRight;
433 int32_t mSurfaceBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434
435 // Similar to the surface coordinates, but in the raw display coordinate space rather than in
436 // the logical coordinate space.
437 int32_t mPhysicalWidth;
438 int32_t mPhysicalHeight;
439 int32_t mPhysicalLeft;
440 int32_t mPhysicalTop;
441
442 // The orientation may be different from the viewport orientation as it specifies
443 // the rotation of the surface coordinates required to produce the viewport's
444 // requested orientation, so it will depend on whether the device is orientation aware.
445 int32_t mSurfaceOrientation;
446
447 // Translation and scaling factors, orientation-independent.
448 float mXTranslate;
449 float mXScale;
450 float mXPrecision;
451
452 float mYTranslate;
453 float mYScale;
454 float mYPrecision;
455
456 float mGeometricScale;
457
458 float mPressureScale;
459
460 float mSizeScale;
461
462 float mOrientationScale;
463
464 float mDistanceScale;
465
466 bool mHaveTilt;
467 float mTiltXCenter;
468 float mTiltXScale;
469 float mTiltYCenter;
470 float mTiltYScale;
471
472 bool mExternalStylusConnected;
473
474 // Oriented motion ranges for input device info.
475 struct OrientedRanges {
476 InputDeviceInfo::MotionRange x;
477 InputDeviceInfo::MotionRange y;
478 InputDeviceInfo::MotionRange pressure;
479
480 bool haveSize;
481 InputDeviceInfo::MotionRange size;
482
483 bool haveTouchSize;
484 InputDeviceInfo::MotionRange touchMajor;
485 InputDeviceInfo::MotionRange touchMinor;
486
487 bool haveToolSize;
488 InputDeviceInfo::MotionRange toolMajor;
489 InputDeviceInfo::MotionRange toolMinor;
490
491 bool haveOrientation;
492 InputDeviceInfo::MotionRange orientation;
493
494 bool haveDistance;
495 InputDeviceInfo::MotionRange distance;
496
497 bool haveTilt;
498 InputDeviceInfo::MotionRange tilt;
499
500 OrientedRanges() { clear(); }
501
502 void clear() {
503 haveSize = false;
504 haveTouchSize = false;
505 haveToolSize = false;
506 haveOrientation = false;
507 haveDistance = false;
508 haveTilt = false;
509 }
510 } mOrientedRanges;
511
512 // Oriented dimensions and precision.
513 float mOrientedXPrecision;
514 float mOrientedYPrecision;
515
516 struct CurrentVirtualKeyState {
517 bool down;
518 bool ignored;
519 nsecs_t downTime;
520 int32_t keyCode;
521 int32_t scanCode;
522 } mCurrentVirtualKey;
523
524 // Scale factor for gesture or mouse based pointer movements.
525 float mPointerXMovementScale;
526 float mPointerYMovementScale;
527
528 // Scale factor for gesture based zooming and other freeform motions.
529 float mPointerXZoomScale;
530 float mPointerYZoomScale;
531
532 // The maximum swipe width.
533 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 Vishniakou58ba3d12021-02-11 01:31:07 +0000738 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700739
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000740 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700741 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000742 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
743 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
744 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000746 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
747 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
748 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
749 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
750 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700751 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
752 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000753 void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000755 void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
756 PointerUsage pointerUsage);
757 void abortPointerUsage(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 dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
760 bool isTimeout);
761 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700762 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
763 bool* outFinishPreviousGesture, bool isTimeout);
764
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000765 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
766 void abortPointerStylus(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 dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
769 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700770
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000771 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
772 bool hovering);
773 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700774
775 bool assignExternalStylusId(const RawState& state, bool timeout);
776 void applyExternalStylusButtonState(nsecs_t when);
777 void applyExternalStylusTouchState(nsecs_t when);
778
779 // Dispatches a motion event.
780 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
781 // method will take care of setting the index and transmuting the action to DOWN or UP
782 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000783 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
784 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
785 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700786 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
787 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
788
789 // Updates pointer coords and properties for pointers with specified ids that have moved.
790 // Returns true if any of them changed.
791 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
792 const uint32_t* inIdToIndex, PointerProperties* outProperties,
793 PointerCoords* outCoords, const uint32_t* outIdToIndex,
794 BitSet32 idBits) const;
795
Garfield Tanc734e4f2021-01-15 20:01:39 -0800796 // Returns if this touch device is a touch screen with an associated display.
797 bool isTouchScreen();
798 // Updates touch spots if they are enabled. Should only be used when this device is a
799 // touchscreen.
800 void updateTouchSpots();
801
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700802 bool isPointInsideSurface(int32_t x, int32_t y);
803 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
804
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000805 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700806
807 const char* modeToString(DeviceMode deviceMode);
Arthur Hung05de5772019-09-26 18:31:26 +0800808 void rotateAndScale(float& x, float& y);
Prabir Pradhand7482e72021-03-09 13:54:55 -0800809
810 // Wrapper methods for interfacing with PointerController. These are used to convert points
811 // between the coordinate spaces used by InputReader and PointerController, if they differ.
812 void moveMouseCursor(float dx, float dy) const;
813 std::pair<float, float> getMouseCursorPosition() const;
814 void setMouseCursorPosition(float x, float y) const;
815 void setTouchSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
816 BitSet32 spotIdBits, int32_t displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700817};
818
819} // namespace android
820
821#endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H