blob: 31806e12e3f5d6c9301ec34e336fe723f91b2cec [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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
Michael Wright227c5542020-07-02 18:30:52 +010019#include <stdint.h>
20
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070021#include "CursorButtonAccumulator.h"
22#include "CursorScrollAccumulator.h"
23#include "EventHub.h"
24#include "InputMapper.h"
25#include "InputReaderBase.h"
26#include "TouchButtonAccumulator.h"
27
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028namespace android {
29
30/* Raw axis information from the driver. */
31struct RawPointerAxes {
32 RawAbsoluteAxisInfo x;
33 RawAbsoluteAxisInfo y;
34 RawAbsoluteAxisInfo pressure;
35 RawAbsoluteAxisInfo touchMajor;
36 RawAbsoluteAxisInfo touchMinor;
37 RawAbsoluteAxisInfo toolMajor;
38 RawAbsoluteAxisInfo toolMinor;
39 RawAbsoluteAxisInfo orientation;
40 RawAbsoluteAxisInfo distance;
41 RawAbsoluteAxisInfo tiltX;
42 RawAbsoluteAxisInfo tiltY;
43 RawAbsoluteAxisInfo trackingId;
44 RawAbsoluteAxisInfo slot;
45
46 RawPointerAxes();
47 inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; }
48 inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; }
49 void clear();
50};
51
52/* Raw data for a collection of pointers including a pointer id mapping table. */
53struct RawPointerData {
54 struct Pointer {
55 uint32_t id;
56 int32_t x;
57 int32_t y;
58 int32_t pressure;
59 int32_t touchMajor;
60 int32_t touchMinor;
61 int32_t toolMajor;
62 int32_t toolMinor;
63 int32_t orientation;
64 int32_t distance;
65 int32_t tiltX;
66 int32_t tiltY;
67 int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
68 bool isHovering;
69 };
70
71 uint32_t pointerCount;
72 Pointer pointers[MAX_POINTERS];
arthurhungcc7f9802020-04-30 17:55:40 +080073 BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074 uint32_t idToIndex[MAX_POINTER_ID + 1];
75
76 RawPointerData();
77 void clear();
78 void copyFrom(const RawPointerData& other);
79 void getCentroidOfTouchingPointers(float* outX, float* outY) const;
80
81 inline void markIdBit(uint32_t id, bool isHovering) {
82 if (isHovering) {
83 hoveringIdBits.markBit(id);
84 } else {
85 touchingIdBits.markBit(id);
86 }
87 }
88
89 inline void clearIdBits() {
90 hoveringIdBits.clear();
91 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +080092 canceledIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 }
94
95 inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; }
96
97 inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; }
98};
99
100/* Cooked data for a collection of pointers including a pointer id mapping table. */
101struct CookedPointerData {
102 uint32_t pointerCount;
103 PointerProperties pointerProperties[MAX_POINTERS];
104 PointerCoords pointerCoords[MAX_POINTERS];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000105 BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits, validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 uint32_t idToIndex[MAX_POINTER_ID + 1];
107
108 CookedPointerData();
109 void clear();
110 void copyFrom(const CookedPointerData& other);
111
112 inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
113 return pointerCoords[idToIndex[id]];
114 }
115
116 inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
117 return pointerCoords[idToIndex[id]];
118 }
119
120 inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
121 return pointerProperties[idToIndex[id]];
122 }
123
124 inline bool isHovering(uint32_t pointerIndex) const {
125 return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
126 }
127
128 inline bool isTouching(uint32_t pointerIndex) const {
129 return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
130 }
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000131
132 inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133};
134
135class TouchInputMapper : public InputMapper {
136public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800137 explicit TouchInputMapper(InputDeviceContext& deviceContext);
Michael Wright227c5542020-07-02 18:30:52 +0100138 ~TouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139
Philip Junker4af3b3d2021-12-14 10:36:55 +0100140 uint32_t getSources() const override;
Michael Wright227c5542020-07-02 18:30:52 +0100141 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
142 void dump(std::string& dump) override;
143 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) override;
144 void reset(nsecs_t when) override;
145 void process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700146
Michael Wright227c5542020-07-02 18:30:52 +0100147 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
148 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700149 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
Michael Wright227c5542020-07-02 18:30:52 +0100150 uint8_t* outFlags) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000152 void cancelTouch(nsecs_t when, nsecs_t readTime) override;
Michael Wright227c5542020-07-02 18:30:52 +0100153 void timeoutExpired(nsecs_t when) override;
154 void updateExternalStylusState(const StylusState& state) override;
155 std::optional<int32_t> getAssociatedDisplayId() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700156
157protected:
158 CursorButtonAccumulator mCursorButtonAccumulator;
159 CursorScrollAccumulator mCursorScrollAccumulator;
160 TouchButtonAccumulator mTouchButtonAccumulator;
161
162 struct VirtualKey {
163 int32_t keyCode;
164 int32_t scanCode;
165 uint32_t flags;
166
167 // computed hit box, specified in touch screen coords based on known display size
168 int32_t hitLeft;
169 int32_t hitTop;
170 int32_t hitRight;
171 int32_t hitBottom;
172
173 inline bool isHit(int32_t x, int32_t y) const {
174 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
175 }
176 };
177
178 // Input sources and device mode.
179 uint32_t mSource;
180
Michael Wright227c5542020-07-02 18:30:52 +0100181 enum class DeviceMode {
182 DISABLED, // input is disabled
183 DIRECT, // direct mapping (touchscreen)
184 UNSCALED, // unscaled mapping (touchpad)
185 NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
186 POINTER, // pointer mapping (pointer)
Dominik Laskowski75788452021-02-09 18:51:25 -0800187
188 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700189 };
190 DeviceMode mDeviceMode;
191
192 // The reader's configuration.
193 InputReaderConfiguration mConfig;
194
195 // Immutable configuration parameters.
196 struct Parameters {
Michael Wright227c5542020-07-02 18:30:52 +0100197 enum class DeviceType {
198 TOUCH_SCREEN,
Michael Wright227c5542020-07-02 18:30:52 +0100199 TOUCH_NAVIGATION,
200 POINTER,
Dominik Laskowski75788452021-02-09 18:51:25 -0800201
202 ftl_last = POINTER
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700203 };
204
205 DeviceType deviceType;
206 bool hasAssociatedDisplay;
207 bool associatedDisplayIsExternal;
208 bool orientationAware;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700209
210 enum class Orientation : int32_t {
211 ORIENTATION_0 = DISPLAY_ORIENTATION_0,
212 ORIENTATION_90 = DISPLAY_ORIENTATION_90,
213 ORIENTATION_180 = DISPLAY_ORIENTATION_180,
214 ORIENTATION_270 = DISPLAY_ORIENTATION_270,
Dominik Laskowski75788452021-02-09 18:51:25 -0800215
216 ftl_last = ORIENTATION_270
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700217 };
218 Orientation orientation;
219
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700220 bool hasButtonUnderPad;
221 std::string uniqueDisplayId;
222
Michael Wright227c5542020-07-02 18:30:52 +0100223 enum class GestureMode {
224 SINGLE_TOUCH,
225 MULTI_TOUCH,
Dominik Laskowski75788452021-02-09 18:51:25 -0800226
227 ftl_last = MULTI_TOUCH
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700228 };
229 GestureMode gestureMode;
230
231 bool wake;
232 } mParameters;
233
234 // Immutable calibration parameters in parsed form.
235 struct Calibration {
236 // Size
Michael Wright227c5542020-07-02 18:30:52 +0100237 enum class SizeCalibration {
238 DEFAULT,
239 NONE,
240 GEOMETRIC,
241 DIAMETER,
242 BOX,
243 AREA,
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800244 ftl_last = AREA
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700245 };
246
247 SizeCalibration sizeCalibration;
248
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700249 std::optional<float> sizeScale;
250 std::optional<float> sizeBias;
251 std::optional<bool> sizeIsSummed;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252
253 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +0100254 enum class PressureCalibration {
255 DEFAULT,
256 NONE,
257 PHYSICAL,
258 AMPLITUDE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700259 };
260
261 PressureCalibration pressureCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700262 std::optional<float> pressureScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263
264 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +0100265 enum class OrientationCalibration {
266 DEFAULT,
267 NONE,
268 INTERPOLATED,
269 VECTOR,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270 };
271
272 OrientationCalibration orientationCalibration;
273
274 // Distance
Michael Wright227c5542020-07-02 18:30:52 +0100275 enum class DistanceCalibration {
276 DEFAULT,
277 NONE,
278 SCALED,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700279 };
280
281 DistanceCalibration distanceCalibration;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700282 std::optional<float> distanceScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700283
Michael Wright227c5542020-07-02 18:30:52 +0100284 enum class CoverageCalibration {
285 DEFAULT,
286 NONE,
287 BOX,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700288 };
289
290 CoverageCalibration coverageCalibration;
291
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700292 inline void applySizeScaleAndBias(float& outSize) const {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700293 if (sizeScale) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700294 outSize *= *sizeScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295 }
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700296 if (sizeBias) {
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700297 outSize += *sizeBias;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700298 }
Siarhei Vishniakou07247342022-07-15 14:27:37 -0700299 if (outSize < 0) {
300 outSize = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700301 }
302 }
303 } mCalibration;
304
305 // Affine location transformation/calibration
306 struct TouchAffineTransformation mAffineTransform;
307
308 RawPointerAxes mRawPointerAxes;
309
310 struct RawState {
311 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000312 nsecs_t readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313
314 // Raw pointer sample data.
315 RawPointerData rawPointerData;
316
317 int32_t buttonState;
318
319 // Scroll state.
320 int32_t rawVScroll;
321 int32_t rawHScroll;
322
323 void copyFrom(const RawState& other) {
324 when = other.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000325 readTime = other.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326 rawPointerData.copyFrom(other.rawPointerData);
327 buttonState = other.buttonState;
328 rawVScroll = other.rawVScroll;
329 rawHScroll = other.rawHScroll;
330 }
331
332 void clear() {
333 when = 0;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000334 readTime = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700335 rawPointerData.clear();
336 buttonState = 0;
337 rawVScroll = 0;
338 rawHScroll = 0;
339 }
340 };
341
342 struct CookedState {
343 // Cooked pointer sample data.
344 CookedPointerData cookedPointerData;
345
346 // Id bits used to differentiate fingers, stylus and mouse tools.
347 BitSet32 fingerIdBits;
348 BitSet32 stylusIdBits;
349 BitSet32 mouseIdBits;
350
351 int32_t buttonState;
352
353 void copyFrom(const CookedState& other) {
354 cookedPointerData.copyFrom(other.cookedPointerData);
355 fingerIdBits = other.fingerIdBits;
356 stylusIdBits = other.stylusIdBits;
357 mouseIdBits = other.mouseIdBits;
358 buttonState = other.buttonState;
359 }
360
361 void clear() {
362 cookedPointerData.clear();
363 fingerIdBits.clear();
364 stylusIdBits.clear();
365 mouseIdBits.clear();
366 buttonState = 0;
367 }
368 };
369
370 std::vector<RawState> mRawStatesPending;
371 RawState mCurrentRawState;
372 CookedState mCurrentCookedState;
373 RawState mLastRawState;
374 CookedState mLastCookedState;
375
376 // State provided by an external stylus
377 StylusState mExternalStylusState;
378 int64_t mExternalStylusId;
379 nsecs_t mExternalStylusFusionTimeout;
380 bool mExternalStylusDataPending;
381
382 // True if we sent a HOVER_ENTER event.
383 bool mSentHoverEnter;
384
385 // Have we assigned pointer IDs for this stream
386 bool mHavePointerIds;
387
388 // Is the current stream of direct touch events aborted
389 bool mCurrentMotionAborted;
390
391 // The time the primary pointer last went down.
392 nsecs_t mDownTime;
393
394 // The pointer controller, or null if the device is not a pointer.
Michael Wright17db18e2020-06-26 20:51:44 +0100395 std::shared_ptr<PointerControllerInterface> mPointerController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700396
397 std::vector<VirtualKey> mVirtualKeys;
398
399 virtual void configureParameters();
400 virtual void dumpParameters(std::string& dump);
401 virtual void configureRawPointerAxes();
402 virtual void dumpRawPointerAxes(std::string& dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700403 virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded);
404 virtual void dumpDisplay(std::string& dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405 virtual void configureVirtualKeys();
406 virtual void dumpVirtualKeys(std::string& dump);
407 virtual void parseCalibration();
408 virtual void resolveCalibration();
409 virtual void dumpCalibration(std::string& dump);
410 virtual void updateAffineTransformation();
411 virtual void dumpAffineTransformation(std::string& dump);
412 virtual void resolveExternalStylusPresence();
413 virtual bool hasStylus() const = 0;
414 virtual bool hasExternalStylus() const;
415
416 virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
417
418private:
419 // The current viewport.
420 // The components of the viewport are specified in the display's rotated orientation.
421 DisplayViewport mViewport;
422
Prabir Pradhan1728b212021-10-19 16:00:03 -0700423 // The width and height are obtained from the viewport and are specified
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700424 // in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700425 int32_t mDisplayWidth;
426 int32_t mDisplayHeight;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800427
Prabir Pradhan1728b212021-10-19 16:00:03 -0700428 // The physical frame is the rectangle in the display's coordinate space that maps to the
429 // the logical display frame.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430 int32_t mPhysicalWidth;
431 int32_t mPhysicalHeight;
432 int32_t mPhysicalLeft;
433 int32_t mPhysicalTop;
434
Prabir Pradhan1728b212021-10-19 16:00:03 -0700435 // The orientation of the input device relative to that of the display panel. It specifies
436 // the rotation of the input device coordinates required to produce the display panel
437 // orientation, so it will depend on whether the device is orientation aware.
438 int32_t mInputDeviceOrientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439
440 // Translation and scaling factors, orientation-independent.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700441 float mXScale;
442 float mXPrecision;
443
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444 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
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700471 std::optional<InputDeviceInfo::MotionRange> size;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700472
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700473 std::optional<InputDeviceInfo::MotionRange> touchMajor;
474 std::optional<InputDeviceInfo::MotionRange> touchMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700475
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700476 std::optional<InputDeviceInfo::MotionRange> toolMajor;
477 std::optional<InputDeviceInfo::MotionRange> toolMinor;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700478
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700479 std::optional<InputDeviceInfo::MotionRange> orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700480
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700481 std::optional<InputDeviceInfo::MotionRange> distance;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700482
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700483 std::optional<InputDeviceInfo::MotionRange> tilt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700484
485 void clear() {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700486 size = std::nullopt;
487 touchMajor = std::nullopt;
488 touchMinor = std::nullopt;
489 toolMajor = std::nullopt;
490 toolMinor = std::nullopt;
491 orientation = std::nullopt;
492 distance = std::nullopt;
493 tilt = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700494 }
495 } mOrientedRanges;
496
497 // Oriented dimensions and precision.
498 float mOrientedXPrecision;
499 float mOrientedYPrecision;
500
501 struct CurrentVirtualKeyState {
502 bool down;
503 bool ignored;
504 nsecs_t downTime;
505 int32_t keyCode;
506 int32_t scanCode;
507 } mCurrentVirtualKey;
508
509 // Scale factor for gesture or mouse based pointer movements.
510 float mPointerXMovementScale;
511 float mPointerYMovementScale;
512
513 // Scale factor for gesture based zooming and other freeform motions.
514 float mPointerXZoomScale;
515 float mPointerYZoomScale;
516
HQ Liue6983c72022-04-19 22:14:56 +0000517 // The maximum swipe width between pointers to detect a swipe gesture
518 // in the number of pixels.Touches that are wider than this are translated
519 // into freeform gestures.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700520 float mPointerGestureMaxSwipeWidth;
521
522 struct PointerDistanceHeapElement {
523 uint32_t currentPointerIndex : 8;
524 uint32_t lastPointerIndex : 8;
525 uint64_t distance : 48; // squared distance
526 };
527
Michael Wright227c5542020-07-02 18:30:52 +0100528 enum class PointerUsage {
529 NONE,
530 GESTURES,
531 STYLUS,
532 MOUSE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700533 };
534 PointerUsage mPointerUsage;
535
536 struct PointerGesture {
Michael Wright227c5542020-07-02 18:30:52 +0100537 enum class Mode {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700538 // No fingers, button is not pressed.
539 // Nothing happening.
540 NEUTRAL,
541
542 // No fingers, button is not pressed.
543 // Tap detected.
544 // Emits DOWN and UP events at the pointer location.
545 TAP,
546
547 // Exactly one finger dragging following a tap.
548 // Pointer follows the active finger.
549 // Emits DOWN, MOVE and UP events at the pointer location.
550 //
551 // Detect double-taps when the finger goes up while in TAP_DRAG mode.
552 TAP_DRAG,
553
554 // Button is pressed.
555 // Pointer follows the active finger if there is one. Other fingers are ignored.
556 // Emits DOWN, MOVE and UP events at the pointer location.
557 BUTTON_CLICK_OR_DRAG,
558
559 // Exactly one finger, button is not pressed.
560 // Pointer follows the active finger.
561 // Emits HOVER_MOVE events at the pointer location.
562 //
563 // Detect taps when the finger goes up while in HOVER mode.
564 HOVER,
565
566 // Exactly two fingers but neither have moved enough to clearly indicate
567 // whether a swipe or freeform gesture was intended. We consider the
568 // pointer to be pressed so this enables clicking or long-pressing on buttons.
569 // Pointer does not move.
570 // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
571 PRESS,
572
573 // Exactly two fingers moving in the same direction, button is not pressed.
574 // Pointer does not move.
575 // Emits DOWN, MOVE and UP events with a single pointer coordinate that
576 // follows the midpoint between both fingers.
577 SWIPE,
578
579 // Two or more fingers moving in arbitrary directions, button is not pressed.
580 // Pointer does not move.
581 // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
582 // each finger individually relative to the initial centroid of the finger.
583 FREEFORM,
584
585 // Waiting for quiet time to end before starting the next gesture.
586 QUIET,
587 };
588
Prabir Pradhan47cf0a02021-03-11 20:30:57 -0800589 // When a gesture is sent to an unfocused window, return true if it can bring that window
590 // into focus, false otherwise.
591 static bool canGestureAffectWindowFocus(Mode mode) {
592 switch (mode) {
593 case Mode::TAP:
594 case Mode::TAP_DRAG:
595 case Mode::BUTTON_CLICK_OR_DRAG:
596 // Taps can affect window focus.
597 return true;
598 case Mode::FREEFORM:
599 case Mode::HOVER:
600 case Mode::NEUTRAL:
601 case Mode::PRESS:
602 case Mode::QUIET:
603 case Mode::SWIPE:
604 // Most gestures can be performed on an unfocused window, so they should not
605 // not affect window focus.
606 return false;
607 }
608 }
609
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700610 // Time the first finger went down.
611 nsecs_t firstTouchTime;
612
613 // The active pointer id from the raw touch data.
614 int32_t activeTouchId; // -1 if none
615
616 // The active pointer id from the gesture last delivered to the application.
617 int32_t activeGestureId; // -1 if none
618
619 // Pointer coords and ids for the current and previous pointer gesture.
620 Mode currentGestureMode;
621 BitSet32 currentGestureIdBits;
622 uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
623 PointerProperties currentGestureProperties[MAX_POINTERS];
624 PointerCoords currentGestureCoords[MAX_POINTERS];
625
626 Mode lastGestureMode;
627 BitSet32 lastGestureIdBits;
628 uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
629 PointerProperties lastGestureProperties[MAX_POINTERS];
630 PointerCoords lastGestureCoords[MAX_POINTERS];
631
632 // Time the pointer gesture last went down.
633 nsecs_t downTime;
634
635 // Time when the pointer went down for a TAP.
636 nsecs_t tapDownTime;
637
638 // Time when the pointer went up for a TAP.
639 nsecs_t tapUpTime;
640
641 // Location of initial tap.
642 float tapX, tapY;
643
644 // Time we started waiting for quiescence.
645 nsecs_t quietTime;
646
647 // Reference points for multitouch gestures.
648 float referenceTouchX; // reference touch X/Y coordinates in surface units
649 float referenceTouchY;
650 float referenceGestureX; // reference gesture X/Y coordinates in pixels
651 float referenceGestureY;
652
653 // Distance that each pointer has traveled which has not yet been
654 // subsumed into the reference gesture position.
655 BitSet32 referenceIdBits;
656 struct Delta {
657 float dx, dy;
658 };
659 Delta referenceDeltas[MAX_POINTER_ID + 1];
660
661 // Describes how touch ids are mapped to gesture ids for freeform gestures.
662 uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
663
664 // A velocity tracker for determining whether to switch active pointers during drags.
665 VelocityTracker velocityTracker;
666
667 void reset() {
668 firstTouchTime = LLONG_MIN;
669 activeTouchId = -1;
670 activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +0100671 currentGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700672 currentGestureIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +0100673 lastGestureMode = Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700674 lastGestureIdBits.clear();
675 downTime = 0;
676 velocityTracker.clear();
677 resetTap();
678 resetQuietTime();
679 }
680
681 void resetTap() {
682 tapDownTime = LLONG_MIN;
683 tapUpTime = LLONG_MIN;
684 }
685
686 void resetQuietTime() { quietTime = LLONG_MIN; }
687 } mPointerGesture;
688
689 struct PointerSimple {
690 PointerCoords currentCoords;
691 PointerProperties currentProperties;
692 PointerCoords lastCoords;
693 PointerProperties lastProperties;
694
695 // True if the pointer is down.
696 bool down;
697
698 // True if the pointer is hovering.
699 bool hovering;
700
701 // Time the pointer last went down.
702 nsecs_t downTime;
703
704 void reset() {
705 currentCoords.clear();
706 currentProperties.clear();
707 lastCoords.clear();
708 lastProperties.clear();
709 down = false;
710 hovering = false;
711 downTime = 0;
712 }
713 } mPointerSimple;
714
715 // The pointer and scroll velocity controls.
716 VelocityControl mPointerVelocityControl;
717 VelocityControl mWheelXVelocityControl;
718 VelocityControl mWheelYVelocityControl;
719
720 std::optional<DisplayViewport> findViewport();
721
722 void resetExternalStylus();
723 void clearStylusDataPendingFlags();
724
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800725 int32_t clampResolution(const char* axisName, int32_t resolution) const;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800726 void initializeOrientedRanges();
727 void initializeSizeRanges();
728
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000729 void sync(nsecs_t when, nsecs_t readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000731 bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700732 void processRawTouches(bool timeout);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000733 void cookAndDispatch(nsecs_t when, nsecs_t readTime);
734 void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
735 int32_t keyEventAction, int32_t keyEventFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700736
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000737 void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
738 void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
739 void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
740 void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
741 void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700742 const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
743 void cookPointerData();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000744 void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000746 void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
747 PointerUsage pointerUsage);
748 void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000750 void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
751 bool isTimeout);
752 void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700753 bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
754 bool* outFinishPreviousGesture, bool isTimeout);
755
Harry Cutts714d1ad2022-08-24 16:36:43 +0000756 // Moves the on-screen mouse pointer based on the movement of the pointer of the given ID
757 // between the last and current events. Uses a relative motion.
758 void moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId);
759
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000760 void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
761 void abortPointerStylus(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 dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
764 void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000766 void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down,
767 bool hovering);
768 void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700769
770 bool assignExternalStylusId(const RawState& state, bool timeout);
771 void applyExternalStylusButtonState(nsecs_t when);
772 void applyExternalStylusTouchState(nsecs_t when);
773
774 // Dispatches a motion event.
775 // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
776 // method will take care of setting the index and transmuting the action to DOWN or UP
777 // it is the first / last pointer to go down / up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000778 void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source,
779 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
780 int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700781 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
Harry Cutts2800fb02022-09-15 13:49:23 +0000782 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime,
783 MotionClassification classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700784
785 // Updates pointer coords and properties for pointers with specified ids that have moved.
786 // Returns true if any of them changed.
787 bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords,
788 const uint32_t* inIdToIndex, PointerProperties* outProperties,
789 PointerCoords* outCoords, const uint32_t* outIdToIndex,
790 BitSet32 idBits) const;
791
Garfield Tanc734e4f2021-01-15 20:01:39 -0800792 // Returns if this touch device is a touch screen with an associated display.
793 bool isTouchScreen();
794 // Updates touch spots if they are enabled. Should only be used when this device is a
795 // touchscreen.
796 void updateTouchSpots();
797
Prabir Pradhan1728b212021-10-19 16:00:03 -0700798 bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700799 const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
800
Siarhei Vishniakou57479982021-03-03 01:32:21 +0000801 static void assignPointerIds(const RawState& last, RawState& current);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700802
803 const char* modeToString(DeviceMode deviceMode);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700804 void rotateAndScale(float& x, float& y) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700805};
806
807} // namespace android