blob: 4cd2cce9f9a81efbf5836a924018c86eb7a97251 [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
Michael Wright227c5542020-07-02 18:30:52 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wright227c5542020-07-02 18:30:52 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "TouchInputMapper.h"
22
Dominik Laskowski75788452021-02-09 18:51:25 -080023#include <ftl/enum.h>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorButtonAccumulator.h"
26#include "CursorScrollAccumulator.h"
27#include "TouchButtonAccumulator.h"
28#include "TouchCursorInputMapperCommon.h"
29
30namespace android {
31
32// --- Constants ---
33
34// Maximum amount of latency to add to touch events while waiting for data from an
35// external stylus.
36static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
37
38// Maximum amount of time to wait on touch data before pushing out new pressure data.
39static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
40
41// Artificial latency on synthetic events created from stylus data without corresponding touch
42// data.
43static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
44
HQ Liue6983c72022-04-19 22:14:56 +000045// Minimum width between two pointers to determine a gesture as freeform gesture in mm
46static const float MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER = 30;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070047// --- Static Definitions ---
48
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000049static const DisplayViewport kUninitializedViewport;
50
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051template <typename T>
52inline static void swap(T& a, T& b) {
53 T temp = a;
54 a = b;
55 b = temp;
56}
57
58static float calculateCommonVector(float a, float b) {
59 if (a > 0 && b > 0) {
60 return a < b ? a : b;
61 } else if (a < 0 && b < 0) {
62 return a > b ? a : b;
63 } else {
64 return 0;
65 }
66}
67
68inline static float distance(float x1, float y1, float x2, float y2) {
69 return hypotf(x1 - x2, y1 - y2);
70}
71
72inline static int32_t signExtendNybble(int32_t value) {
73 return value >= 8 ? value - 16 : value;
74}
75
76// --- RawPointerAxes ---
77
78RawPointerAxes::RawPointerAxes() {
79 clear();
80}
81
82void RawPointerAxes::clear() {
83 x.clear();
84 y.clear();
85 pressure.clear();
86 touchMajor.clear();
87 touchMinor.clear();
88 toolMajor.clear();
89 toolMinor.clear();
90 orientation.clear();
91 distance.clear();
92 tiltX.clear();
93 tiltY.clear();
94 trackingId.clear();
95 slot.clear();
96}
97
98// --- RawPointerData ---
99
100RawPointerData::RawPointerData() {
101 clear();
102}
103
104void RawPointerData::clear() {
105 pointerCount = 0;
106 clearIdBits();
107}
108
109void RawPointerData::copyFrom(const RawPointerData& other) {
110 pointerCount = other.pointerCount;
111 hoveringIdBits = other.hoveringIdBits;
112 touchingIdBits = other.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +0800113 canceledIdBits = other.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114
115 for (uint32_t i = 0; i < pointerCount; i++) {
116 pointers[i] = other.pointers[i];
117
118 int id = pointers[i].id;
119 idToIndex[id] = other.idToIndex[id];
120 }
121}
122
123void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
124 float x = 0, y = 0;
125 uint32_t count = touchingIdBits.count();
126 if (count) {
127 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
128 uint32_t id = idBits.clearFirstMarkedBit();
129 const Pointer& pointer = pointerForId(id);
130 x += pointer.x;
131 y += pointer.y;
132 }
133 x /= count;
134 y /= count;
135 }
136 *outX = x;
137 *outY = y;
138}
139
140// --- CookedPointerData ---
141
142CookedPointerData::CookedPointerData() {
143 clear();
144}
145
146void CookedPointerData::clear() {
147 pointerCount = 0;
148 hoveringIdBits.clear();
149 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800150 canceledIdBits.clear();
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000151 validIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152}
153
154void CookedPointerData::copyFrom(const CookedPointerData& other) {
155 pointerCount = other.pointerCount;
156 hoveringIdBits = other.hoveringIdBits;
157 touchingIdBits = other.touchingIdBits;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000158 validIdBits = other.validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159
160 for (uint32_t i = 0; i < pointerCount; i++) {
161 pointerProperties[i].copyFrom(other.pointerProperties[i]);
162 pointerCoords[i].copyFrom(other.pointerCoords[i]);
163
164 int id = pointerProperties[i].id;
165 idToIndex[id] = other.idToIndex[id];
166 }
167}
168
169// --- TouchInputMapper ---
170
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800171TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
172 : InputMapper(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100174 mDeviceMode(DeviceMode::DISABLED),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700175 mDisplayWidth(-1),
176 mDisplayHeight(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700177 mPhysicalWidth(-1),
178 mPhysicalHeight(-1),
179 mPhysicalLeft(0),
180 mPhysicalTop(0),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700181 mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182
183TouchInputMapper::~TouchInputMapper() {}
184
Philip Junker4af3b3d2021-12-14 10:36:55 +0100185uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700186 return mSource;
187}
188
189void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
190 InputMapper::populateDeviceInfo(info);
191
Michael Wright227c5542020-07-02 18:30:52 +0100192 if (mDeviceMode != DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700193 info->addMotionRange(mOrientedRanges.x);
194 info->addMotionRange(mOrientedRanges.y);
195 info->addMotionRange(mOrientedRanges.pressure);
196
Chris Yef74dc422020-09-02 22:41:50 -0700197 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
Chris Ye8fa17282020-09-15 17:17:34 -0700198 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
199 //
200 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
201 // motion, i.e. the hardware dimensions, as the finger could move completely across the
202 // touchpad in one sample cycle.
Chris Yef74dc422020-09-02 22:41:50 -0700203 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
204 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
205 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat,
206 x.fuzz, x.resolution);
207 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat,
208 y.fuzz, y.resolution);
209 }
210
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700211 if (mOrientedRanges.size) {
212 info->addMotionRange(*mOrientedRanges.size);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700213 }
214
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700215 if (mOrientedRanges.touchMajor) {
216 info->addMotionRange(*mOrientedRanges.touchMajor);
217 info->addMotionRange(*mOrientedRanges.touchMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218 }
219
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700220 if (mOrientedRanges.toolMajor) {
221 info->addMotionRange(*mOrientedRanges.toolMajor);
222 info->addMotionRange(*mOrientedRanges.toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700223 }
224
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700225 if (mOrientedRanges.orientation) {
226 info->addMotionRange(*mOrientedRanges.orientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700227 }
228
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700229 if (mOrientedRanges.distance) {
230 info->addMotionRange(*mOrientedRanges.distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700231 }
232
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700233 if (mOrientedRanges.tilt) {
234 info->addMotionRange(*mOrientedRanges.tilt);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700235 }
236
237 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
238 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
239 0.0f);
240 }
241 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
242 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
243 0.0f);
244 }
Michael Wright227c5542020-07-02 18:30:52 +0100245 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
247 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
248 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
249 x.fuzz, x.resolution);
250 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
251 y.fuzz, y.resolution);
252 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
253 x.fuzz, x.resolution);
254 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
255 y.fuzz, y.resolution);
256 }
257 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
258 }
259}
260
261void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700262 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800263 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 dumpParameters(dump);
265 dumpVirtualKeys(dump);
266 dumpRawPointerAxes(dump);
267 dumpCalibration(dump);
268 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700269 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270
271 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale);
273 dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale);
274 dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
275 dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
276 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
277 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
278 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
279 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
280 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
281 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
282 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
283 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
284 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
285 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
286
287 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
288 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
289 mLastRawState.rawPointerData.pointerCount);
290 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
291 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
292 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
293 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
294 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
295 "toolType=%d, isHovering=%s\n",
296 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
297 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
298 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
299 pointer.distance, pointer.toolType, toString(pointer.isHovering));
300 }
301
302 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
303 mLastCookedState.buttonState);
304 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
305 mLastCookedState.cookedPointerData.pointerCount);
306 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
307 const PointerProperties& pointerProperties =
308 mLastCookedState.cookedPointerData.pointerProperties[i];
309 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000310 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
311 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
312 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
314 "toolType=%d, isHovering=%s\n",
315 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000316 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
317 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
319 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
320 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
321 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
322 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
323 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
324 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
325 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
326 pointerProperties.toolType,
327 toString(mLastCookedState.cookedPointerData.isHovering(i)));
328 }
329
330 dump += INDENT3 "Stylus Fusion:\n";
331 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
332 toString(mExternalStylusConnected));
333 dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
334 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
335 mExternalStylusFusionTimeout);
336 dump += INDENT3 "External Stylus State:\n";
337 dumpStylusState(dump, mExternalStylusState);
338
Michael Wright227c5542020-07-02 18:30:52 +0100339 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
341 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
342 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
343 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
344 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
345 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
346 }
347}
348
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
350 uint32_t changes) {
351 InputMapper::configure(when, config, changes);
352
353 mConfig = *config;
354
355 if (!changes) { // first time only
356 // Configure basic parameters.
357 configureParameters();
358
359 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800360 mCursorScrollAccumulator.configure(getDeviceContext());
361 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362
363 // Configure absolute axis information.
364 configureRawPointerAxes();
365
366 // Prepare input device calibration.
367 parseCalibration();
368 resolveCalibration();
369 }
370
371 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
372 // Update location calibration to reflect current settings
373 updateAffineTransformation();
374 }
375
376 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
377 // Update pointer speed.
378 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
379 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
380 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
381 }
382
383 bool resetNeeded = false;
384 if (!changes ||
385 (changes &
386 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800387 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
389 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
390 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700391 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700393 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394 }
395
396 if (changes && resetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000397 // If the device needs to be reset, cancel any ongoing gestures and reset the state.
398 cancelTouch(when, when);
399 reset(when);
400
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700401 // Send reset, unless this is the first time the device has been configured,
402 // in which case the reader will call reset itself after all mappers are ready.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000403 NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId());
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700404 getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405 }
406}
407
408void TouchInputMapper::resolveExternalStylusPresence() {
409 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800410 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700411 mExternalStylusConnected = !devices.empty();
412
413 if (!mExternalStylusConnected) {
414 resetExternalStylus();
415 }
416}
417
418void TouchInputMapper::configureParameters() {
419 // Use the pointer presentation mode for devices that do not support distinct
420 // multitouch. The spot-based presentation relies on being able to accurately
421 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800422 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100423 ? Parameters::GestureMode::SINGLE_TOUCH
424 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700426 std::string gestureModeString;
427 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800428 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100430 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100432 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700434 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700435 }
436 }
437
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800438 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100440 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800441 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100443 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444 } else {
445 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100446 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447 }
448
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800449 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700451 std::string deviceTypeString;
452 if (getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800453 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700454 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100455 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700456 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100457 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700458 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100459 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700460 } else if (deviceTypeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700461 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700462 }
463 }
464
Michael Wright227c5542020-07-02 18:30:52 +0100465 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700466 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800467 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700468
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700469 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700470 std::string orientationString;
471 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700472 orientationString)) {
473 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
474 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
475 } else if (orientationString == "ORIENTATION_90") {
476 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
477 } else if (orientationString == "ORIENTATION_180") {
478 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
479 } else if (orientationString == "ORIENTATION_270") {
480 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
481 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700482 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700483 }
484 }
485
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700486 mParameters.hasAssociatedDisplay = false;
487 mParameters.associatedDisplayIsExternal = false;
488 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100489 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
490 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700491 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100492 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800493 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700494 std::string uniqueDisplayId;
495 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800496 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700497 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
498 }
499 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800500 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700501 mParameters.hasAssociatedDisplay = true;
502 }
503
504 // Initial downs on external touch devices should wake the device.
505 // Normally we don't do this for internal touch screens to prevent them from waking
506 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800507 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700508 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700509}
510
511void TouchInputMapper::dumpParameters(std::string& dump) {
512 dump += INDENT3 "Parameters:\n";
513
Dominik Laskowski75788452021-02-09 18:51:25 -0800514 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700515
Dominik Laskowski75788452021-02-09 18:51:25 -0800516 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700517
518 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
519 "displayId='%s'\n",
520 toString(mParameters.hasAssociatedDisplay),
521 toString(mParameters.associatedDisplayIsExternal),
522 mParameters.uniqueDisplayId.c_str());
523 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800524 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700525}
526
527void TouchInputMapper::configureRawPointerAxes() {
528 mRawPointerAxes.clear();
529}
530
531void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
532 dump += INDENT3 "Raw Touch Axes:\n";
533 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
534 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
535 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
536 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
537 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
538 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
539 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
545 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
546}
547
548bool TouchInputMapper::hasExternalStylus() const {
549 return mExternalStylusConnected;
550}
551
552/**
553 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000554 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800555 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000556 * 3. Get the matching viewport by either unique id in idc file or by the display type
557 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800558 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700559 */
560std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800561 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000562 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800563 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700564 }
565
Christine Franks2a2293c2022-01-18 11:51:16 -0800566 const std::optional<std::string> associatedDisplayUniqueId =
567 getDeviceContext().getAssociatedDisplayUniqueId();
568 if (associatedDisplayUniqueId) {
569 return getDeviceContext().getAssociatedViewport();
570 }
571
Michael Wright227c5542020-07-02 18:30:52 +0100572 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800573 std::optional<DisplayViewport> viewport =
574 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
575 if (viewport) {
576 return viewport;
577 } else {
578 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
579 mConfig.defaultPointerDisplayId);
580 }
581 }
582
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700583 // Check if uniqueDisplayId is specified in idc file.
584 if (!mParameters.uniqueDisplayId.empty()) {
585 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
586 }
587
588 ViewportType viewportTypeToUse;
589 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100590 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700591 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100592 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700593 }
594
595 std::optional<DisplayViewport> viewport =
596 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100597 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700598 ALOGW("Input device %s should be associated with external display, "
599 "fallback to internal one for the external viewport is not found.",
600 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100601 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700602 }
603
604 return viewport;
605 }
606
607 // No associated display, return a non-display viewport.
608 DisplayViewport newViewport;
609 // Raw width and height in the natural orientation.
610 int32_t rawWidth = mRawPointerAxes.getRawWidth();
611 int32_t rawHeight = mRawPointerAxes.getRawHeight();
612 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
613 return std::make_optional(newViewport);
614}
615
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800616int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
617 if (resolution < 0) {
618 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
619 getDeviceName().c_str());
620 return 0;
621 }
622 return resolution;
623}
624
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800625void TouchInputMapper::initializeSizeRanges() {
626 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
627 mSizeScale = 0.0f;
628 return;
629 }
630
631 // Size of diagonal axis.
632 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
633
634 // Size factors.
635 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
636 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
637 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
638 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
639 } else {
640 mSizeScale = 0.0f;
641 }
642
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700643 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
644 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
645 .source = mSource,
646 .min = 0,
647 .max = diagonalSize,
648 .flat = 0,
649 .fuzz = 0,
650 .resolution = 0,
651 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800652
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800653 if (mRawPointerAxes.touchMajor.valid) {
654 mRawPointerAxes.touchMajor.resolution =
655 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700656 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800657 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800658
659 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700660 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800661 if (mRawPointerAxes.touchMinor.valid) {
662 mRawPointerAxes.touchMinor.resolution =
663 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700664 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800665 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800666
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700667 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
668 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
669 .source = mSource,
670 .min = 0,
671 .max = diagonalSize,
672 .flat = 0,
673 .fuzz = 0,
674 .resolution = 0,
675 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800676 if (mRawPointerAxes.toolMajor.valid) {
677 mRawPointerAxes.toolMajor.resolution =
678 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700679 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800680 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800681
682 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700683 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800684 if (mRawPointerAxes.toolMinor.valid) {
685 mRawPointerAxes.toolMinor.resolution =
686 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700687 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800688 }
689
690 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700691 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
692 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
693 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
694 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800695 } else {
696 // Support for other calibrations can be added here.
697 ALOGW("%s calibration is not supported for size ranges at the moment. "
698 "Using raw resolution instead",
699 ftl::enum_string(mCalibration.sizeCalibration).c_str());
700 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800701
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700702 mOrientedRanges.size = InputDeviceInfo::MotionRange{
703 .axis = AMOTION_EVENT_AXIS_SIZE,
704 .source = mSource,
705 .min = 0,
706 .max = 1.0,
707 .flat = 0,
708 .fuzz = 0,
709 .resolution = 0,
710 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800711}
712
713void TouchInputMapper::initializeOrientedRanges() {
714 // Configure X and Y factors.
715 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
716 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
717 mXPrecision = 1.0f / mXScale;
718 mYPrecision = 1.0f / mYScale;
719
720 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
721 mOrientedRanges.x.source = mSource;
722 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
723 mOrientedRanges.y.source = mSource;
724
725 // Scale factor for terms that are not oriented in a particular axis.
726 // If the pixels are square then xScale == yScale otherwise we fake it
727 // by choosing an average.
728 mGeometricScale = avg(mXScale, mYScale);
729
730 initializeSizeRanges();
731
732 // Pressure factors.
733 mPressureScale = 0;
734 float pressureMax = 1.0;
735 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
736 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700737 if (mCalibration.pressureScale) {
738 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800739 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
740 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
741 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
742 }
743 }
744
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700745 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
746 .axis = AMOTION_EVENT_AXIS_PRESSURE,
747 .source = mSource,
748 .min = 0,
749 .max = pressureMax,
750 .flat = 0,
751 .fuzz = 0,
752 .resolution = 0,
753 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800754
755 // Tilt
756 mTiltXCenter = 0;
757 mTiltXScale = 0;
758 mTiltYCenter = 0;
759 mTiltYScale = 0;
760 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
761 if (mHaveTilt) {
762 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
763 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
764 mTiltXScale = M_PI / 180;
765 mTiltYScale = M_PI / 180;
766
767 if (mRawPointerAxes.tiltX.resolution) {
768 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
769 }
770 if (mRawPointerAxes.tiltY.resolution) {
771 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
772 }
773
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700774 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
775 .axis = AMOTION_EVENT_AXIS_TILT,
776 .source = mSource,
777 .min = 0,
778 .max = M_PI_2,
779 .flat = 0,
780 .fuzz = 0,
781 .resolution = 0,
782 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800783 }
784
785 // Orientation
786 mOrientationScale = 0;
787 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700788 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
789 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
790 .source = mSource,
791 .min = -M_PI,
792 .max = M_PI,
793 .flat = 0,
794 .fuzz = 0,
795 .resolution = 0,
796 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800797
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800798 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
799 if (mCalibration.orientationCalibration ==
800 Calibration::OrientationCalibration::INTERPOLATED) {
801 if (mRawPointerAxes.orientation.valid) {
802 if (mRawPointerAxes.orientation.maxValue > 0) {
803 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
804 } else if (mRawPointerAxes.orientation.minValue < 0) {
805 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
806 } else {
807 mOrientationScale = 0;
808 }
809 }
810 }
811
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700812 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
813 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
814 .source = mSource,
815 .min = -M_PI_2,
816 .max = M_PI_2,
817 .flat = 0,
818 .fuzz = 0,
819 .resolution = 0,
820 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800821 }
822
823 // Distance
824 mDistanceScale = 0;
825 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
826 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700827 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800828 }
829
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700830 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800831
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700832 .axis = AMOTION_EVENT_AXIS_DISTANCE,
833 .source = mSource,
834 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
835 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
836 .flat = 0,
837 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
838 .resolution = 0,
839 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800840 }
841
842 // Compute oriented precision, scales and ranges.
843 // Note that the maximum value reported is an inclusive maximum value so it is one
844 // unit less than the total width or height of the display.
845 switch (mInputDeviceOrientation) {
846 case DISPLAY_ORIENTATION_90:
847 case DISPLAY_ORIENTATION_270:
848 mOrientedXPrecision = mYPrecision;
849 mOrientedYPrecision = mXPrecision;
850
851 mOrientedRanges.x.min = 0;
852 mOrientedRanges.x.max = mDisplayHeight - 1;
853 mOrientedRanges.x.flat = 0;
854 mOrientedRanges.x.fuzz = 0;
855 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
856
857 mOrientedRanges.y.min = 0;
858 mOrientedRanges.y.max = mDisplayWidth - 1;
859 mOrientedRanges.y.flat = 0;
860 mOrientedRanges.y.fuzz = 0;
861 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
862 break;
863
864 default:
865 mOrientedXPrecision = mXPrecision;
866 mOrientedYPrecision = mYPrecision;
867
868 mOrientedRanges.x.min = 0;
869 mOrientedRanges.x.max = mDisplayWidth - 1;
870 mOrientedRanges.x.flat = 0;
871 mOrientedRanges.x.fuzz = 0;
872 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
873
874 mOrientedRanges.y.min = 0;
875 mOrientedRanges.y.max = mDisplayHeight - 1;
876 mOrientedRanges.y.flat = 0;
877 mOrientedRanges.y.fuzz = 0;
878 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
879 break;
880 }
881}
882
Prabir Pradhan1728b212021-10-19 16:00:03 -0700883void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000884 const DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700885
886 resolveExternalStylusPresence();
887
888 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100889 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000890 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700891 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100892 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700893 if (hasStylus()) {
894 mSource |= AINPUT_SOURCE_STYLUS;
895 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800896 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700897 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100898 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700899 if (hasStylus()) {
900 mSource |= AINPUT_SOURCE_STYLUS;
901 }
902 if (hasExternalStylus()) {
903 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
904 }
Michael Wright227c5542020-07-02 18:30:52 +0100905 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700906 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100907 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700908 } else {
909 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100910 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700911 }
912
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000913 const std::optional<DisplayViewport> newViewportOpt = findViewport();
914
915 // Ensure the device is valid and can be used.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700916 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
917 ALOGW("Touch device '%s' did not report support for X or Y axis! "
918 "The device will be inoperable.",
919 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100920 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000921 } else if (!newViewportOpt) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700922 ALOGI("Touch device '%s' could not query the properties of its associated "
923 "display. The device will be inoperable until the display size "
924 "becomes available.",
925 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100926 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000927 } else if (!newViewportOpt->isActive) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000928 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
929 getDeviceName().c_str(), getDeviceId());
930 mDeviceMode = DeviceMode::DISABLED;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000931 }
932
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700933 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700934 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
935 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
HQ Liue6983c72022-04-19 22:14:56 +0000936 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
937 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
938 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
939 const float rawMeanResolution =
940 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700941
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000942 const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport);
943 const bool viewportChanged = mViewport != newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700944 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700945 if (viewportChanged) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000946 const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation;
947 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId;
948 mViewport = newViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700949
Michael Wright227c5542020-07-02 18:30:52 +0100950 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700951 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700952 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
953 int32_t naturalPhysicalLeft, naturalPhysicalTop;
954 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700955
Prabir Pradhan1728b212021-10-19 16:00:03 -0700956 // Apply the inverse of the input device orientation so that the input device is
957 // configured in the same orientation as the viewport. The input device orientation will
958 // be re-applied by mInputDeviceOrientation.
959 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700960 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700961 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700962 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700963 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
964 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800965 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700966 naturalPhysicalTop = mViewport.physicalLeft;
967 naturalDeviceWidth = mViewport.deviceHeight;
968 naturalDeviceHeight = mViewport.deviceWidth;
969 break;
970 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700971 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
972 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
973 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
974 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
975 naturalDeviceWidth = mViewport.deviceWidth;
976 naturalDeviceHeight = mViewport.deviceHeight;
977 break;
978 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700979 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
980 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
981 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800982 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700983 naturalDeviceWidth = mViewport.deviceHeight;
984 naturalDeviceHeight = mViewport.deviceWidth;
985 break;
986 case DISPLAY_ORIENTATION_0:
987 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700988 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
989 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
990 naturalPhysicalLeft = mViewport.physicalLeft;
991 naturalPhysicalTop = mViewport.physicalTop;
992 naturalDeviceWidth = mViewport.deviceWidth;
993 naturalDeviceHeight = mViewport.deviceHeight;
994 break;
995 }
996
997 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
998 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
999 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
1000 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1001 }
1002
1003 mPhysicalWidth = naturalPhysicalWidth;
1004 mPhysicalHeight = naturalPhysicalHeight;
1005 mPhysicalLeft = naturalPhysicalLeft;
1006 mPhysicalTop = naturalPhysicalTop;
1007
Prabir Pradhan1728b212021-10-19 16:00:03 -07001008 const int32_t oldDisplayWidth = mDisplayWidth;
1009 const int32_t oldDisplayHeight = mDisplayHeight;
1010 mDisplayWidth = naturalDeviceWidth;
1011 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001012
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001013 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1014 // anything if the device is already orientation-aware. If the device is not
1015 // orientation-aware, then we need to apply the inverse rotation of the display so that
1016 // when the display rotation is applied later as a part of the per-window transform, we
1017 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001018 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001019 ? DISPLAY_ORIENTATION_0
1020 : getInverseRotation(mViewport.orientation);
1021 // For orientation-aware devices that work in the un-rotated coordinate space, the
1022 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00001023 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
1024 mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight &&
1025 viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001026
1027 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001028 mInputDeviceOrientation =
1029 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001030 } else {
1031 mPhysicalWidth = rawWidth;
1032 mPhysicalHeight = rawHeight;
1033 mPhysicalLeft = 0;
1034 mPhysicalTop = 0;
1035
Prabir Pradhan1728b212021-10-19 16:00:03 -07001036 mDisplayWidth = rawWidth;
1037 mDisplayHeight = rawHeight;
1038 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001039 }
1040 }
1041
1042 // If moving between pointer modes, need to reset some state.
1043 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1044 if (deviceModeChanged) {
1045 mOrientedRanges.clear();
1046 }
1047
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001048 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1049 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001050 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001051 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001052 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1053 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001054 if (mPointerController == nullptr) {
1055 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001056 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001057 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001058 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1059 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001060 } else {
lilinnandef700b2022-06-17 19:32:01 +08001061 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1062 !mConfig.showTouches) {
1063 mPointerController->clearSpots();
1064 }
Michael Wright17db18e2020-06-26 20:51:44 +01001065 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001066 }
1067
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001068 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001069 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1070 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001071 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1072 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001073
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001074 configureVirtualKeys();
1075
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001076 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001077
1078 // Location
1079 updateAffineTransformation();
1080
Michael Wright227c5542020-07-02 18:30:52 +01001081 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001082 // Compute pointer gesture detection parameters.
1083 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001084 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001085
1086 // Scale movements such that one whole swipe of the touch pad covers a
1087 // given area relative to the diagonal size of the display when no acceleration
1088 // is applied.
1089 // Assume that the touch pad has a square aspect ratio such that movements in
1090 // X and Y of the same number of raw units cover the same physical distance.
1091 mPointerXMovementScale =
1092 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1093 mPointerYMovementScale = mPointerXMovementScale;
1094
1095 // Scale zooms to cover a smaller range of the display than movements do.
1096 // This value determines the area around the pointer that is affected by freeform
1097 // pointer gestures.
1098 mPointerXZoomScale =
1099 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1100 mPointerYZoomScale = mPointerXZoomScale;
1101
HQ Liue6983c72022-04-19 22:14:56 +00001102 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1103 // axis is non positive value.
1104 const float minFreeformGestureWidth =
1105 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1106
1107 mPointerGestureMaxSwipeWidth =
1108 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1109 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001110 }
1111
1112 // Inform the dispatcher about the changes.
1113 *outResetNeeded = true;
1114 bumpGeneration();
1115 }
1116}
1117
Prabir Pradhan1728b212021-10-19 16:00:03 -07001118void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001119 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001120 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1121 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001122 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1123 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1124 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1125 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001126 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001127}
1128
1129void TouchInputMapper::configureVirtualKeys() {
1130 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001131 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001132
1133 mVirtualKeys.clear();
1134
1135 if (virtualKeyDefinitions.size() == 0) {
1136 return;
1137 }
1138
1139 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1140 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1141 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1142 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1143
1144 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1145 VirtualKey virtualKey;
1146
1147 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1148 int32_t keyCode;
1149 int32_t dummyKeyMetaState;
1150 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001151 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1152 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001153 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1154 continue; // drop the key
1155 }
1156
1157 virtualKey.keyCode = keyCode;
1158 virtualKey.flags = flags;
1159
1160 // convert the key definition's display coordinates into touch coordinates for a hit box
1161 int32_t halfWidth = virtualKeyDefinition.width / 2;
1162 int32_t halfHeight = virtualKeyDefinition.height / 2;
1163
1164 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001165 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 touchScreenLeft;
1167 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001168 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001169 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001170 virtualKey.hitTop =
1171 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001172 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001173 virtualKey.hitBottom =
1174 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001175 touchScreenTop;
1176 mVirtualKeys.push_back(virtualKey);
1177 }
1178}
1179
1180void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1181 if (!mVirtualKeys.empty()) {
1182 dump += INDENT3 "Virtual Keys:\n";
1183
1184 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1185 const VirtualKey& virtualKey = mVirtualKeys[i];
1186 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1187 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1188 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1189 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1190 }
1191 }
1192}
1193
1194void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001195 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001196 Calibration& out = mCalibration;
1197
1198 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001199 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001200 std::string sizeCalibrationString;
1201 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001202 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001203 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001204 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001205 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001206 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001207 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001208 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001209 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001210 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001211 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001212 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001213 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001214 }
1215 }
1216
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001217 float sizeScale;
1218
1219 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1220 out.sizeScale = sizeScale;
1221 }
1222 float sizeBias;
1223 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1224 out.sizeBias = sizeBias;
1225 }
1226 bool sizeIsSummed;
1227 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1228 out.sizeIsSummed = sizeIsSummed;
1229 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001230
1231 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001232 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001233 std::string pressureCalibrationString;
1234 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001235 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001236 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001237 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001238 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001239 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001240 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001241 } else if (pressureCalibrationString != "default") {
1242 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001243 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001244 }
1245 }
1246
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001247 float pressureScale;
1248 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1249 out.pressureScale = pressureScale;
1250 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001251
1252 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001253 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001254 std::string orientationCalibrationString;
1255 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001256 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001257 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001258 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001259 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001261 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001262 } else if (orientationCalibrationString != "default") {
1263 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001264 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001265 }
1266 }
1267
1268 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001269 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001270 std::string distanceCalibrationString;
1271 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001272 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001273 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001274 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001275 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001276 } else if (distanceCalibrationString != "default") {
1277 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001278 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001279 }
1280 }
1281
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001282 float distanceScale;
1283 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1284 out.distanceScale = distanceScale;
1285 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001286
Michael Wright227c5542020-07-02 18:30:52 +01001287 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001288 std::string coverageCalibrationString;
1289 if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001290 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001291 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001292 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001293 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001294 } else if (coverageCalibrationString != "default") {
1295 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001296 coverageCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001297 }
1298 }
1299}
1300
1301void TouchInputMapper::resolveCalibration() {
1302 // Size
1303 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001304 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1305 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001306 }
1307 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001308 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001309 }
1310
1311 // Pressure
1312 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001313 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1314 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001315 }
1316 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001317 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001318 }
1319
1320 // Orientation
1321 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001322 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1323 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001324 }
1325 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001326 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001327 }
1328
1329 // Distance
1330 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001331 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1332 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001333 }
1334 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001335 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001336 }
1337
1338 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001339 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1340 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001341 }
1342}
1343
1344void TouchInputMapper::dumpCalibration(std::string& dump) {
1345 dump += INDENT3 "Calibration:\n";
1346
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001347 dump += INDENT4 "touch.size.calibration: ";
1348 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001349
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001350 if (mCalibration.sizeScale) {
1351 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001352 }
1353
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001354 if (mCalibration.sizeBias) {
1355 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001356 }
1357
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001358 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001359 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001360 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001361 }
1362
1363 // Pressure
1364 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001365 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001366 dump += INDENT4 "touch.pressure.calibration: none\n";
1367 break;
Michael Wright227c5542020-07-02 18:30:52 +01001368 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001369 dump += INDENT4 "touch.pressure.calibration: physical\n";
1370 break;
Michael Wright227c5542020-07-02 18:30:52 +01001371 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001372 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1373 break;
1374 default:
1375 ALOG_ASSERT(false);
1376 }
1377
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001378 if (mCalibration.pressureScale) {
1379 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001380 }
1381
1382 // Orientation
1383 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001384 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001385 dump += INDENT4 "touch.orientation.calibration: none\n";
1386 break;
Michael Wright227c5542020-07-02 18:30:52 +01001387 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001388 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1389 break;
Michael Wright227c5542020-07-02 18:30:52 +01001390 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001391 dump += INDENT4 "touch.orientation.calibration: vector\n";
1392 break;
1393 default:
1394 ALOG_ASSERT(false);
1395 }
1396
1397 // Distance
1398 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001399 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001400 dump += INDENT4 "touch.distance.calibration: none\n";
1401 break;
Michael Wright227c5542020-07-02 18:30:52 +01001402 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001403 dump += INDENT4 "touch.distance.calibration: scaled\n";
1404 break;
1405 default:
1406 ALOG_ASSERT(false);
1407 }
1408
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001409 if (mCalibration.distanceScale) {
1410 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001411 }
1412
1413 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001414 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001415 dump += INDENT4 "touch.coverage.calibration: none\n";
1416 break;
Michael Wright227c5542020-07-02 18:30:52 +01001417 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001418 dump += INDENT4 "touch.coverage.calibration: box\n";
1419 break;
1420 default:
1421 ALOG_ASSERT(false);
1422 }
1423}
1424
1425void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1426 dump += INDENT3 "Affine Transformation:\n";
1427
1428 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1429 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1430 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1431 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1432 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1433 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1434}
1435
1436void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001437 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001438 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001439}
1440
1441void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001442 mCursorButtonAccumulator.reset(getDeviceContext());
1443 mCursorScrollAccumulator.reset(getDeviceContext());
1444 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001445
1446 mPointerVelocityControl.reset();
1447 mWheelXVelocityControl.reset();
1448 mWheelYVelocityControl.reset();
1449
1450 mRawStatesPending.clear();
1451 mCurrentRawState.clear();
1452 mCurrentCookedState.clear();
1453 mLastRawState.clear();
1454 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001455 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001456 mSentHoverEnter = false;
1457 mHavePointerIds = false;
1458 mCurrentMotionAborted = false;
1459 mDownTime = 0;
1460
1461 mCurrentVirtualKey.down = false;
1462
1463 mPointerGesture.reset();
1464 mPointerSimple.reset();
1465 resetExternalStylus();
1466
1467 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001468 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001469 mPointerController->clearSpots();
1470 }
1471
1472 InputMapper::reset(when);
1473}
1474
1475void TouchInputMapper::resetExternalStylus() {
1476 mExternalStylusState.clear();
1477 mExternalStylusId = -1;
1478 mExternalStylusFusionTimeout = LLONG_MAX;
1479 mExternalStylusDataPending = false;
1480}
1481
1482void TouchInputMapper::clearStylusDataPendingFlags() {
1483 mExternalStylusDataPending = false;
1484 mExternalStylusFusionTimeout = LLONG_MAX;
1485}
1486
1487void TouchInputMapper::process(const RawEvent* rawEvent) {
1488 mCursorButtonAccumulator.process(rawEvent);
1489 mCursorScrollAccumulator.process(rawEvent);
1490 mTouchButtonAccumulator.process(rawEvent);
1491
1492 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001493 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001494 }
1495}
1496
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001497void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001498 // Push a new state.
1499 mRawStatesPending.emplace_back();
1500
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001501 RawState& next = mRawStatesPending.back();
1502 next.clear();
1503 next.when = when;
1504 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001505
1506 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001507 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001508 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1509
1510 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001511 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1512 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001513 mCursorScrollAccumulator.finishSync();
1514
1515 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001516 syncTouch(when, &next);
1517
1518 // The last RawState is the actually second to last, since we just added a new state
1519 const RawState& last =
1520 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001521
1522 // Assign pointer ids.
1523 if (!mHavePointerIds) {
1524 assignPointerIds(last, next);
1525 }
1526
Harry Cutts45483602022-08-24 14:36:48 +00001527 ALOGD_IF(DEBUG_RAW_EVENTS,
1528 "syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1529 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1530 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1531 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1532 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1533 next.rawPointerData.canceledIdBits.value);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001534
Arthur Hung9ad18942021-06-19 02:04:46 +00001535 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1536 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1537 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1538 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1539 next.rawPointerData.hoveringIdBits.value);
1540 }
1541
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001542 processRawTouches(false /*timeout*/);
1543}
1544
1545void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001546 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001547 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001548 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001549 mCurrentCookedState.clear();
1550 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001551 return;
1552 }
1553
1554 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1555 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1556 // touching the current state will only observe the events that have been dispatched to the
1557 // rest of the pipeline.
1558 const size_t N = mRawStatesPending.size();
1559 size_t count;
1560 for (count = 0; count < N; count++) {
1561 const RawState& next = mRawStatesPending[count];
1562
1563 // A failure to assign the stylus id means that we're waiting on stylus data
1564 // and so should defer the rest of the pipeline.
1565 if (assignExternalStylusId(next, timeout)) {
1566 break;
1567 }
1568
1569 // All ready to go.
1570 clearStylusDataPendingFlags();
1571 mCurrentRawState.copyFrom(next);
1572 if (mCurrentRawState.when < mLastRawState.when) {
1573 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001574 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001575 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001576 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001577 }
1578 if (count != 0) {
1579 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1580 }
1581
1582 if (mExternalStylusDataPending) {
1583 if (timeout) {
1584 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1585 clearStylusDataPendingFlags();
1586 mCurrentRawState.copyFrom(mLastRawState);
Harry Cutts45483602022-08-24 14:36:48 +00001587 ALOGD_IF(DEBUG_STYLUS_FUSION,
1588 "Timeout expired, synthesizing event with new stylus data");
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001589 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1590 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001591 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1592 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1593 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1594 }
1595 }
1596}
1597
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001598void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001599 // Always start with a clean state.
1600 mCurrentCookedState.clear();
1601
1602 // Apply stylus buttons to current raw state.
1603 applyExternalStylusButtonState(when);
1604
1605 // Handle policy on initial down or hover events.
1606 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1607 mCurrentRawState.rawPointerData.pointerCount != 0;
1608
1609 uint32_t policyFlags = 0;
1610 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1611 if (initialDown || buttonsPressed) {
1612 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001613 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001614 getContext()->fadePointer();
1615 }
1616
1617 if (mParameters.wake) {
1618 policyFlags |= POLICY_FLAG_WAKE;
1619 }
1620 }
1621
1622 // Consume raw off-screen touches before cooking pointer data.
1623 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001624 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001625 mCurrentRawState.rawPointerData.clear();
1626 }
1627
1628 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1629 // with cooked pointer data that has the same ids and indices as the raw data.
1630 // The following code can use either the raw or cooked data, as needed.
1631 cookPointerData();
1632
1633 // Apply stylus pressure to current cooked state.
1634 applyExternalStylusTouchState(when);
1635
1636 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001637 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1638 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001639 mCurrentCookedState.buttonState);
1640
1641 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001642 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001643 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1644 uint32_t id = idBits.clearFirstMarkedBit();
1645 const RawPointerData::Pointer& pointer =
1646 mCurrentRawState.rawPointerData.pointerForId(id);
1647 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1648 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1649 mCurrentCookedState.stylusIdBits.markBit(id);
1650 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1651 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1652 mCurrentCookedState.fingerIdBits.markBit(id);
1653 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1654 mCurrentCookedState.mouseIdBits.markBit(id);
1655 }
1656 }
1657 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1658 uint32_t id = idBits.clearFirstMarkedBit();
1659 const RawPointerData::Pointer& pointer =
1660 mCurrentRawState.rawPointerData.pointerForId(id);
1661 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1662 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1663 mCurrentCookedState.stylusIdBits.markBit(id);
1664 }
1665 }
1666
1667 // Stylus takes precedence over all tools, then mouse, then finger.
1668 PointerUsage pointerUsage = mPointerUsage;
1669 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1670 mCurrentCookedState.mouseIdBits.clear();
1671 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001672 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001673 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1674 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001675 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001676 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1677 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001678 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001679 }
1680
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001681 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001682 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001683 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001684 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001685 dispatchButtonRelease(when, readTime, policyFlags);
1686 dispatchHoverExit(when, readTime, policyFlags);
1687 dispatchTouches(when, readTime, policyFlags);
1688 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1689 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001690 }
1691
1692 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1693 mCurrentMotionAborted = false;
1694 }
1695 }
1696
1697 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001698 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001699 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1700 mCurrentCookedState.buttonState);
1701
1702 // Clear some transient state.
1703 mCurrentRawState.rawVScroll = 0;
1704 mCurrentRawState.rawHScroll = 0;
1705
1706 // Copy current touch to last touch in preparation for the next cycle.
1707 mLastRawState.copyFrom(mCurrentRawState);
1708 mLastCookedState.copyFrom(mCurrentCookedState);
1709}
1710
Garfield Tanc734e4f2021-01-15 20:01:39 -08001711void TouchInputMapper::updateTouchSpots() {
1712 if (!mConfig.showTouches || mPointerController == nullptr) {
1713 return;
1714 }
1715
1716 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1717 // clear touch spots.
1718 if (mDeviceMode != DeviceMode::DIRECT &&
1719 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1720 return;
1721 }
1722
1723 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1724 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1725
1726 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001727 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1728 mCurrentCookedState.cookedPointerData.idToIndex,
1729 mCurrentCookedState.cookedPointerData.touchingIdBits,
1730 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001731}
1732
1733bool TouchInputMapper::isTouchScreen() {
1734 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1735 mParameters.hasAssociatedDisplay;
1736}
1737
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001738void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001739 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001740 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1741 }
1742}
1743
1744void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1745 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1746 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1747
1748 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1749 float pressure = mExternalStylusState.pressure;
1750 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1751 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1752 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1753 }
1754 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1755 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1756
1757 PointerProperties& properties =
1758 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1759 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1760 properties.toolType = mExternalStylusState.toolType;
1761 }
1762 }
1763}
1764
1765bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001766 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001767 return false;
1768 }
1769
1770 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1771 state.rawPointerData.pointerCount != 0;
1772 if (initialDown) {
1773 if (mExternalStylusState.pressure != 0.0f) {
Harry Cutts45483602022-08-24 14:36:48 +00001774 ALOGD_IF(DEBUG_STYLUS_FUSION, "Have both stylus and touch data, beginning fusion");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001775 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1776 } else if (timeout) {
Harry Cutts45483602022-08-24 14:36:48 +00001777 ALOGD_IF(DEBUG_STYLUS_FUSION, "Timeout expired, assuming touch is not a stylus.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001778 resetExternalStylus();
1779 } else {
1780 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1781 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1782 }
Harry Cutts45483602022-08-24 14:36:48 +00001783 ALOGD_IF(DEBUG_STYLUS_FUSION,
1784 "No stylus data but stylus is connected, requesting timeout (%" PRId64 "ms)",
1785 mExternalStylusFusionTimeout);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001786 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1787 return true;
1788 }
1789 }
1790
1791 // Check if the stylus pointer has gone up.
1792 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Harry Cutts45483602022-08-24 14:36:48 +00001793 ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus pointer is going up");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001794 mExternalStylusId = -1;
1795 }
1796
1797 return false;
1798}
1799
1800void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001801 if (mDeviceMode == DeviceMode::POINTER) {
1802 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001803 // Since this is a synthetic event, we can consider its latency to be zero
1804 const nsecs_t readTime = when;
1805 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001806 }
Michael Wright227c5542020-07-02 18:30:52 +01001807 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001808 if (mExternalStylusFusionTimeout < when) {
1809 processRawTouches(true /*timeout*/);
1810 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1811 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1812 }
1813 }
1814}
1815
1816void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1817 mExternalStylusState.copyFrom(state);
1818 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1819 // We're either in the middle of a fused stream of data or we're waiting on data before
1820 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1821 // data.
1822 mExternalStylusDataPending = true;
1823 processRawTouches(false /*timeout*/);
1824 }
1825}
1826
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001827bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001828 // Check for release of a virtual key.
1829 if (mCurrentVirtualKey.down) {
1830 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1831 // Pointer went up while virtual key was down.
1832 mCurrentVirtualKey.down = false;
1833 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001834 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1835 "VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1836 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001837 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001838 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1839 }
1840 return true;
1841 }
1842
1843 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1844 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1845 const RawPointerData::Pointer& pointer =
1846 mCurrentRawState.rawPointerData.pointerForId(id);
1847 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1848 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1849 // Pointer is still within the space of the virtual key.
1850 return true;
1851 }
1852 }
1853
1854 // Pointer left virtual key area or another pointer also went down.
1855 // Send key cancellation but do not consume the touch yet.
1856 // This is useful when the user swipes through from the virtual key area
1857 // into the main display surface.
1858 mCurrentVirtualKey.down = false;
1859 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001860 ALOGD_IF(DEBUG_VIRTUAL_KEYS, "VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1861 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001862 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001863 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1864 AKEY_EVENT_FLAG_CANCELED);
1865 }
1866 }
1867
1868 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1869 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1870 // Pointer just went down. Check for virtual key press or off-screen touches.
1871 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1872 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001873 // Skip checking whether the pointer is inside the physical frame if the device is in
1874 // unscaled mode.
1875 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1876 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001877 // If exactly one pointer went down, check for virtual key hit.
1878 // Otherwise we will drop the entire stroke.
1879 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1880 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1881 if (virtualKey) {
1882 mCurrentVirtualKey.down = true;
1883 mCurrentVirtualKey.downTime = when;
1884 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1885 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1886 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001887 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1888 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001889
1890 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001891 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1892 "VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1893 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001894 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001895 AKEY_EVENT_FLAG_FROM_SYSTEM |
1896 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1897 }
1898 }
1899 }
1900 return true;
1901 }
1902 }
1903
1904 // Disable all virtual key touches that happen within a short time interval of the
1905 // most recent touch within the screen area. The idea is to filter out stray
1906 // virtual key presses when interacting with the touch screen.
1907 //
1908 // Problems we're trying to solve:
1909 //
1910 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1911 // virtual key area that is implemented by a separate touch panel and accidentally
1912 // triggers a virtual key.
1913 //
1914 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1915 // area and accidentally triggers a virtual key. This often happens when virtual keys
1916 // are layed out below the screen near to where the on screen keyboard's space bar
1917 // is displayed.
1918 if (mConfig.virtualKeyQuietTime > 0 &&
1919 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001920 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001921 }
1922 return false;
1923}
1924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001925void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001926 int32_t keyEventAction, int32_t keyEventFlags) {
1927 int32_t keyCode = mCurrentVirtualKey.keyCode;
1928 int32_t scanCode = mCurrentVirtualKey.scanCode;
1929 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001930 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001931 policyFlags |= POLICY_FLAG_VIRTUAL;
1932
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001933 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1934 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1935 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001936 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001937}
1938
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001939void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
lilinnan687e58f2022-07-19 16:00:50 +08001940 if (mCurrentMotionAborted) {
1941 // Current motion event was already aborted.
1942 return;
1943 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001944 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1945 if (!currentIdBits.isEmpty()) {
1946 int32_t metaState = getContext()->getGlobalMetaState();
1947 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001948 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1949 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001950 mCurrentCookedState.cookedPointerData.pointerProperties,
1951 mCurrentCookedState.cookedPointerData.pointerCoords,
1952 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00001953 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1954 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001955 mCurrentMotionAborted = true;
1956 }
1957}
1958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001959void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001960 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1961 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1962 int32_t metaState = getContext()->getGlobalMetaState();
1963 int32_t buttonState = mCurrentCookedState.buttonState;
1964
1965 if (currentIdBits == lastIdBits) {
1966 if (!currentIdBits.isEmpty()) {
1967 // No pointer id changes so this is a move event.
1968 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001969 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1970 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001971 mCurrentCookedState.cookedPointerData.pointerProperties,
1972 mCurrentCookedState.cookedPointerData.pointerCoords,
1973 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00001974 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1975 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001976 }
1977 } else {
1978 // There may be pointers going up and pointers going down and pointers moving
1979 // all at the same time.
1980 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1981 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1982 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1983 BitSet32 dispatchedIdBits(lastIdBits.value);
1984
1985 // Update last coordinates of pointers that have moved so that we observe the new
1986 // pointer positions at the same time as other pointers that have just gone up.
1987 bool moveNeeded =
1988 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1989 mCurrentCookedState.cookedPointerData.pointerCoords,
1990 mCurrentCookedState.cookedPointerData.idToIndex,
1991 mLastCookedState.cookedPointerData.pointerProperties,
1992 mLastCookedState.cookedPointerData.pointerCoords,
1993 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1994 if (buttonState != mLastCookedState.buttonState) {
1995 moveNeeded = true;
1996 }
1997
1998 // Dispatch pointer up events.
1999 while (!upIdBits.isEmpty()) {
2000 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002001 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002002 if (isCanceled) {
2003 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2004 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002005 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08002006 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002007 mLastCookedState.cookedPointerData.pointerProperties,
2008 mLastCookedState.cookedPointerData.pointerCoords,
2009 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
Harry Cutts2800fb02022-09-15 13:49:23 +00002010 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2011 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002012 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002013 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002014 }
2015
2016 // Dispatch move events if any of the remaining pointers moved from their old locations.
2017 // Although applications receive new locations as part of individual pointer up
2018 // events, they do not generally handle them except when presented in a move event.
2019 if (moveNeeded && !moveIdBits.isEmpty()) {
2020 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002021 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2022 metaState, buttonState, 0,
2023 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002024 mCurrentCookedState.cookedPointerData.pointerCoords,
2025 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00002026 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2027 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002028 }
2029
2030 // Dispatch pointer down events using the new pointer locations.
2031 while (!downIdBits.isEmpty()) {
2032 uint32_t downId = downIdBits.clearFirstMarkedBit();
2033 dispatchedIdBits.markBit(downId);
2034
2035 if (dispatchedIdBits.count() == 1) {
2036 // First pointer is going down. Set down time.
2037 mDownTime = when;
2038 }
2039
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002040 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2041 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002042 mCurrentCookedState.cookedPointerData.pointerProperties,
2043 mCurrentCookedState.cookedPointerData.pointerCoords,
2044 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
Harry Cutts2800fb02022-09-15 13:49:23 +00002045 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2046 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002047 }
2048 }
2049}
2050
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002051void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002052 if (mSentHoverEnter &&
2053 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2054 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2055 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002056 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2057 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002058 mLastCookedState.cookedPointerData.pointerProperties,
2059 mLastCookedState.cookedPointerData.pointerCoords,
2060 mLastCookedState.cookedPointerData.idToIndex,
2061 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
Harry Cutts2800fb02022-09-15 13:49:23 +00002062 mOrientedYPrecision, mDownTime, MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002063 mSentHoverEnter = false;
2064 }
2065}
2066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002067void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2068 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002069 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2070 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2071 int32_t metaState = getContext()->getGlobalMetaState();
2072 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002073 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2074 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002075 mCurrentCookedState.cookedPointerData.pointerProperties,
2076 mCurrentCookedState.cookedPointerData.pointerCoords,
2077 mCurrentCookedState.cookedPointerData.idToIndex,
2078 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00002079 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2080 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002081 mSentHoverEnter = true;
2082 }
2083
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002084 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2085 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002086 mCurrentCookedState.cookedPointerData.pointerProperties,
2087 mCurrentCookedState.cookedPointerData.pointerCoords,
2088 mCurrentCookedState.cookedPointerData.idToIndex,
2089 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00002090 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2091 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002092 }
2093}
2094
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002095void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002096 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2097 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2098 const int32_t metaState = getContext()->getGlobalMetaState();
2099 int32_t buttonState = mLastCookedState.buttonState;
2100 while (!releasedButtons.isEmpty()) {
2101 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2102 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002103 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002104 actionButton, 0, metaState, buttonState, 0,
2105 mCurrentCookedState.cookedPointerData.pointerProperties,
2106 mCurrentCookedState.cookedPointerData.pointerCoords,
2107 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00002108 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2109 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002110 }
2111}
2112
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002113void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002114 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2115 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2116 const int32_t metaState = getContext()->getGlobalMetaState();
2117 int32_t buttonState = mLastCookedState.buttonState;
2118 while (!pressedButtons.isEmpty()) {
2119 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2120 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002121 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2122 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002123 mCurrentCookedState.cookedPointerData.pointerProperties,
2124 mCurrentCookedState.cookedPointerData.pointerCoords,
2125 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00002126 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2127 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002128 }
2129}
2130
2131const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2132 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2133 return cookedPointerData.touchingIdBits;
2134 }
2135 return cookedPointerData.hoveringIdBits;
2136}
2137
2138void TouchInputMapper::cookPointerData() {
2139 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2140
2141 mCurrentCookedState.cookedPointerData.clear();
2142 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2143 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2144 mCurrentRawState.rawPointerData.hoveringIdBits;
2145 mCurrentCookedState.cookedPointerData.touchingIdBits =
2146 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002147 mCurrentCookedState.cookedPointerData.canceledIdBits =
2148 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002149
2150 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2151 mCurrentCookedState.buttonState = 0;
2152 } else {
2153 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2154 }
2155
2156 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002157 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002158 for (uint32_t i = 0; i < currentPointerCount; i++) {
2159 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2160
2161 // Size
2162 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2163 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002164 case Calibration::SizeCalibration::GEOMETRIC:
2165 case Calibration::SizeCalibration::DIAMETER:
2166 case Calibration::SizeCalibration::BOX:
2167 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002168 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2169 touchMajor = in.touchMajor;
2170 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2171 toolMajor = in.toolMajor;
2172 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2173 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2174 : in.touchMajor;
2175 } else if (mRawPointerAxes.touchMajor.valid) {
2176 toolMajor = touchMajor = in.touchMajor;
2177 toolMinor = touchMinor =
2178 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2179 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2180 : in.touchMajor;
2181 } else if (mRawPointerAxes.toolMajor.valid) {
2182 touchMajor = toolMajor = in.toolMajor;
2183 touchMinor = toolMinor =
2184 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2185 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2186 : in.toolMajor;
2187 } else {
2188 ALOG_ASSERT(false,
2189 "No touch or tool axes. "
2190 "Size calibration should have been resolved to NONE.");
2191 touchMajor = 0;
2192 touchMinor = 0;
2193 toolMajor = 0;
2194 toolMinor = 0;
2195 size = 0;
2196 }
2197
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002198 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002199 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2200 if (touchingCount > 1) {
2201 touchMajor /= touchingCount;
2202 touchMinor /= touchingCount;
2203 toolMajor /= touchingCount;
2204 toolMinor /= touchingCount;
2205 size /= touchingCount;
2206 }
2207 }
2208
Michael Wright227c5542020-07-02 18:30:52 +01002209 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002210 touchMajor *= mGeometricScale;
2211 touchMinor *= mGeometricScale;
2212 toolMajor *= mGeometricScale;
2213 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002214 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002215 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2216 touchMinor = touchMajor;
2217 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2218 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002219 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002220 touchMinor = touchMajor;
2221 toolMinor = toolMajor;
2222 }
2223
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002224 mCalibration.applySizeScaleAndBias(touchMajor);
2225 mCalibration.applySizeScaleAndBias(touchMinor);
2226 mCalibration.applySizeScaleAndBias(toolMajor);
2227 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002228 size *= mSizeScale;
2229 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002230 case Calibration::SizeCalibration::DEFAULT:
2231 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2232 break;
2233 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002234 touchMajor = 0;
2235 touchMinor = 0;
2236 toolMajor = 0;
2237 toolMinor = 0;
2238 size = 0;
2239 break;
2240 }
2241
2242 // Pressure
2243 float pressure;
2244 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002245 case Calibration::PressureCalibration::PHYSICAL:
2246 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002247 pressure = in.pressure * mPressureScale;
2248 break;
2249 default:
2250 pressure = in.isHovering ? 0 : 1;
2251 break;
2252 }
2253
2254 // Tilt and Orientation
2255 float tilt;
2256 float orientation;
2257 if (mHaveTilt) {
2258 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2259 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2260 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2261 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2262 } else {
2263 tilt = 0;
2264
2265 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002266 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002267 orientation = in.orientation * mOrientationScale;
2268 break;
Michael Wright227c5542020-07-02 18:30:52 +01002269 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002270 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2271 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2272 if (c1 != 0 || c2 != 0) {
2273 orientation = atan2f(c1, c2) * 0.5f;
2274 float confidence = hypotf(c1, c2);
2275 float scale = 1.0f + confidence / 16.0f;
2276 touchMajor *= scale;
2277 touchMinor /= scale;
2278 toolMajor *= scale;
2279 toolMinor /= scale;
2280 } else {
2281 orientation = 0;
2282 }
2283 break;
2284 }
2285 default:
2286 orientation = 0;
2287 }
2288 }
2289
2290 // Distance
2291 float distance;
2292 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002293 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002294 distance = in.distance * mDistanceScale;
2295 break;
2296 default:
2297 distance = 0;
2298 }
2299
2300 // Coverage
2301 int32_t rawLeft, rawTop, rawRight, rawBottom;
2302 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002303 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002304 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2305 rawRight = in.toolMinor & 0x0000ffff;
2306 rawBottom = in.toolMajor & 0x0000ffff;
2307 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2308 break;
2309 default:
2310 rawLeft = rawTop = rawRight = rawBottom = 0;
2311 break;
2312 }
2313
2314 // Adjust X,Y coords for device calibration
2315 // TODO: Adjust coverage coords?
2316 float xTransformed = in.x, yTransformed = in.y;
2317 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002318 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002319
Prabir Pradhan1728b212021-10-19 16:00:03 -07002320 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002321 float left, top, right, bottom;
2322
Prabir Pradhan1728b212021-10-19 16:00:03 -07002323 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002324 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002325 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2326 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2327 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2328 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002329 orientation -= M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002330 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002331 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002332 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002333 }
2334 break;
2335 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002336 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2337 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002338 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2339 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002340 orientation -= M_PI;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002341 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002342 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002343 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002344 }
2345 break;
2346 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002347 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2348 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002349 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2350 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002351 orientation += M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002352 if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002353 orientation -=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002354 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002355 }
2356 break;
2357 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002358 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2359 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2360 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2361 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002362 break;
2363 }
2364
2365 // Write output coords.
2366 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2367 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002368 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2369 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002370 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2371 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2372 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2373 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2374 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2375 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2376 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002377 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002378 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2379 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2380 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2381 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2382 } else {
2383 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2384 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2385 }
2386
Chris Ye364fdb52020-08-05 15:07:56 -07002387 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002388 uint32_t id = in.id;
2389 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2390 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2391 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2392 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2393 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2394 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2395 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2396 }
2397
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002398 // Write output properties.
2399 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002400 properties.clear();
2401 properties.id = id;
2402 properties.toolType = in.toolType;
2403
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002404 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002405 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002406 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002407 }
2408}
2409
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002410void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002411 PointerUsage pointerUsage) {
2412 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002413 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002414 mPointerUsage = pointerUsage;
2415 }
2416
2417 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002418 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002419 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002420 break;
Michael Wright227c5542020-07-02 18:30:52 +01002421 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002422 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002423 break;
Michael Wright227c5542020-07-02 18:30:52 +01002424 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002425 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002426 break;
Michael Wright227c5542020-07-02 18:30:52 +01002427 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002428 break;
2429 }
2430}
2431
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002432void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002433 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002434 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002435 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002436 break;
Michael Wright227c5542020-07-02 18:30:52 +01002437 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002438 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002439 break;
Michael Wright227c5542020-07-02 18:30:52 +01002440 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002441 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002442 break;
Michael Wright227c5542020-07-02 18:30:52 +01002443 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002444 break;
2445 }
2446
Michael Wright227c5542020-07-02 18:30:52 +01002447 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002448}
2449
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002450void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2451 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002452 // Update current gesture coordinates.
2453 bool cancelPreviousGesture, finishPreviousGesture;
2454 bool sendEvents =
2455 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2456 if (!sendEvents) {
2457 return;
2458 }
2459 if (finishPreviousGesture) {
2460 cancelPreviousGesture = false;
2461 }
2462
2463 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002464 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002465 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002466 if (finishPreviousGesture || cancelPreviousGesture) {
2467 mPointerController->clearSpots();
2468 }
2469
Michael Wright227c5542020-07-02 18:30:52 +01002470 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002471 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2472 mPointerGesture.currentGestureIdToIndex,
2473 mPointerGesture.currentGestureIdBits,
2474 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002475 }
2476 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002477 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002478 }
2479
2480 // Show or hide the pointer if needed.
2481 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002482 case PointerGesture::Mode::NEUTRAL:
2483 case PointerGesture::Mode::QUIET:
2484 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2485 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002486 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002487 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002488 }
2489 break;
Michael Wright227c5542020-07-02 18:30:52 +01002490 case PointerGesture::Mode::TAP:
2491 case PointerGesture::Mode::TAP_DRAG:
2492 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2493 case PointerGesture::Mode::HOVER:
2494 case PointerGesture::Mode::PRESS:
2495 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002496 // Unfade the pointer when the current gesture manipulates the
2497 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002498 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002499 break;
Michael Wright227c5542020-07-02 18:30:52 +01002500 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002501 // Fade the pointer when the current gesture manipulates a different
2502 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002503 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002504 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002505 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002506 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002507 }
2508 break;
2509 }
2510
2511 // Send events!
2512 int32_t metaState = getContext()->getGlobalMetaState();
2513 int32_t buttonState = mCurrentCookedState.buttonState;
Harry Cutts2800fb02022-09-15 13:49:23 +00002514 const MotionClassification classification =
2515 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE
2516 ? MotionClassification::TWO_FINGER_SWIPE
2517 : MotionClassification::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002518
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002519 uint32_t flags = 0;
2520
2521 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2522 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2523 }
2524
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002525 // Update last coordinates of pointers that have moved so that we observe the new
2526 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002527 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2528 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2529 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2530 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2531 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2532 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002533 bool moveNeeded = false;
2534 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2535 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2536 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2537 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2538 mPointerGesture.lastGestureIdBits.value);
2539 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2540 mPointerGesture.currentGestureCoords,
2541 mPointerGesture.currentGestureIdToIndex,
2542 mPointerGesture.lastGestureProperties,
2543 mPointerGesture.lastGestureCoords,
2544 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2545 if (buttonState != mLastCookedState.buttonState) {
2546 moveNeeded = true;
2547 }
2548 }
2549
2550 // Send motion events for all pointers that went up or were canceled.
2551 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2552 if (!dispatchedGestureIdBits.isEmpty()) {
2553 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002554 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2555 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002556 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2557 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
Harry Cutts2800fb02022-09-15 13:49:23 +00002558 mPointerGesture.downTime, classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002559
2560 dispatchedGestureIdBits.clear();
2561 } else {
2562 BitSet32 upGestureIdBits;
2563 if (finishPreviousGesture) {
2564 upGestureIdBits = dispatchedGestureIdBits;
2565 } else {
2566 upGestureIdBits.value =
2567 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2568 }
2569 while (!upGestureIdBits.isEmpty()) {
2570 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2571
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002572 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002573 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002574 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002575 mPointerGesture.lastGestureCoords,
2576 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
Harry Cutts2800fb02022-09-15 13:49:23 +00002577 0, mPointerGesture.downTime, classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002578
2579 dispatchedGestureIdBits.clearBit(id);
2580 }
2581 }
2582 }
2583
2584 // Send motion events for all pointers that moved.
2585 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002586 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002587 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002588 mPointerGesture.currentGestureProperties,
2589 mPointerGesture.currentGestureCoords,
2590 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
Harry Cutts2800fb02022-09-15 13:49:23 +00002591 mPointerGesture.downTime, classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002592 }
2593
2594 // Send motion events for all pointers that went down.
2595 if (down) {
2596 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2597 ~dispatchedGestureIdBits.value);
2598 while (!downGestureIdBits.isEmpty()) {
2599 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2600 dispatchedGestureIdBits.markBit(id);
2601
2602 if (dispatchedGestureIdBits.count() == 1) {
2603 mPointerGesture.downTime = when;
2604 }
2605
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002606 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002607 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002608 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002609 mPointerGesture.currentGestureCoords,
2610 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
Harry Cutts2800fb02022-09-15 13:49:23 +00002611 0, mPointerGesture.downTime, classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002612 }
2613 }
2614
2615 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002616 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002617 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2618 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002619 mPointerGesture.currentGestureProperties,
2620 mPointerGesture.currentGestureCoords,
2621 mPointerGesture.currentGestureIdToIndex,
Harry Cutts2800fb02022-09-15 13:49:23 +00002622 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime,
2623 MotionClassification::NONE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002624 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2625 // Synthesize a hover move event after all pointers go up to indicate that
2626 // the pointer is hovering again even if the user is not currently touching
2627 // the touch pad. This ensures that a view will receive a fresh hover enter
2628 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002629 float x, y;
2630 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002631
2632 PointerProperties pointerProperties;
2633 pointerProperties.clear();
2634 pointerProperties.id = 0;
2635 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2636
2637 PointerCoords pointerCoords;
2638 pointerCoords.clear();
2639 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2640 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2641
2642 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002643 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002644 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002645 metaState, buttonState, MotionClassification::NONE,
2646 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2647 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002648 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002649 }
2650
2651 // Update state.
2652 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2653 if (!down) {
2654 mPointerGesture.lastGestureIdBits.clear();
2655 } else {
2656 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2657 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2658 uint32_t id = idBits.clearFirstMarkedBit();
2659 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2660 mPointerGesture.lastGestureProperties[index].copyFrom(
2661 mPointerGesture.currentGestureProperties[index]);
2662 mPointerGesture.lastGestureCoords[index].copyFrom(
2663 mPointerGesture.currentGestureCoords[index]);
2664 mPointerGesture.lastGestureIdToIndex[id] = index;
2665 }
2666 }
2667}
2668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002669void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Harry Cutts2800fb02022-09-15 13:49:23 +00002670 const MotionClassification classification =
2671 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE
2672 ? MotionClassification::TWO_FINGER_SWIPE
2673 : MotionClassification::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002674 // Cancel previously dispatches pointers.
2675 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2676 int32_t metaState = getContext()->getGlobalMetaState();
2677 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002678 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2679 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002680 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2681 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
Harry Cutts2800fb02022-09-15 13:49:23 +00002682 0, 0, mPointerGesture.downTime, classification);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002683 }
2684
2685 // Reset the current pointer gesture.
2686 mPointerGesture.reset();
2687 mPointerVelocityControl.reset();
2688
2689 // Remove any current spots.
2690 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002691 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002692 mPointerController->clearSpots();
2693 }
2694}
2695
2696bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2697 bool* outFinishPreviousGesture, bool isTimeout) {
2698 *outCancelPreviousGesture = false;
2699 *outFinishPreviousGesture = false;
2700
2701 // Handle TAP timeout.
2702 if (isTimeout) {
Harry Cutts45483602022-08-24 14:36:48 +00002703 ALOGD_IF(DEBUG_GESTURES, "Gestures: Processing timeout");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002704
Michael Wright227c5542020-07-02 18:30:52 +01002705 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002706 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2707 // The tap/drag timeout has not yet expired.
2708 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2709 mConfig.pointerGestureTapDragInterval);
2710 } else {
2711 // The tap is finished.
Harry Cutts45483602022-08-24 14:36:48 +00002712 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP finished");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002713 *outFinishPreviousGesture = true;
2714
2715 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002716 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002717 mPointerGesture.currentGestureIdBits.clear();
2718
2719 mPointerVelocityControl.reset();
2720 return true;
2721 }
2722 }
2723
2724 // We did not handle this timeout.
2725 return false;
2726 }
2727
2728 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2729 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2730
2731 // Update the velocity tracker.
2732 {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002733 std::vector<float> positionsX;
2734 std::vector<float> positionsY;
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002735 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002736 uint32_t id = idBits.clearFirstMarkedBit();
2737 const RawPointerData::Pointer& pointer =
2738 mCurrentRawState.rawPointerData.pointerForId(id);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002739 positionsX.push_back(pointer.x * mPointerXMovementScale);
2740 positionsY.push_back(pointer.y * mPointerYMovementScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002741 }
2742 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002743 {{AMOTION_EVENT_AXIS_X, positionsX},
2744 {AMOTION_EVENT_AXIS_Y, positionsY}});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002745 }
2746
2747 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2748 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002749 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2750 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2751 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002752 mPointerGesture.resetTap();
2753 }
2754
2755 // Pick a new active touch id if needed.
2756 // Choose an arbitrary pointer that just went down, if there is one.
2757 // Otherwise choose an arbitrary remaining pointer.
2758 // This guarantees we always have an active touch id when there is at least one pointer.
2759 // We keep the same active touch id for as long as possible.
2760 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2761 int32_t activeTouchId = lastActiveTouchId;
2762 if (activeTouchId < 0) {
2763 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2764 activeTouchId = mPointerGesture.activeTouchId =
2765 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2766 mPointerGesture.firstTouchTime = when;
2767 }
2768 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2769 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2770 activeTouchId = mPointerGesture.activeTouchId =
2771 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2772 } else {
2773 activeTouchId = mPointerGesture.activeTouchId = -1;
2774 }
2775 }
2776
2777 // Determine whether we are in quiet time.
2778 bool isQuietTime = false;
2779 if (activeTouchId < 0) {
2780 mPointerGesture.resetQuietTime();
2781 } else {
2782 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2783 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002784 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2785 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2786 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002787 currentFingerCount < 2) {
2788 // Enter quiet time when exiting swipe or freeform state.
2789 // This is to prevent accidentally entering the hover state and flinging the
2790 // pointer when finishing a swipe and there is still one pointer left onscreen.
2791 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002792 } else if (mPointerGesture.lastGestureMode ==
2793 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002794 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2795 // Enter quiet time when releasing the button and there are still two or more
2796 // fingers down. This may indicate that one finger was used to press the button
2797 // but it has not gone up yet.
2798 isQuietTime = true;
2799 }
2800 if (isQuietTime) {
2801 mPointerGesture.quietTime = when;
2802 }
2803 }
2804 }
2805
2806 // Switch states based on button and pointer state.
2807 if (isQuietTime) {
2808 // Case 1: Quiet time. (QUIET)
Harry Cutts45483602022-08-24 14:36:48 +00002809 ALOGD_IF(DEBUG_GESTURES, "Gestures: QUIET for next %0.3fms",
2810 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2811 0.000001f);
Michael Wright227c5542020-07-02 18:30:52 +01002812 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002813 *outFinishPreviousGesture = true;
2814 }
2815
2816 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002817 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002818 mPointerGesture.currentGestureIdBits.clear();
2819
2820 mPointerVelocityControl.reset();
2821 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2822 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2823 // The pointer follows the active touch point.
2824 // Emit DOWN, MOVE, UP events at the pointer location.
2825 //
2826 // Only the active touch matters; other fingers are ignored. This policy helps
2827 // to handle the case where the user places a second finger on the touch pad
2828 // to apply the necessary force to depress an integrated button below the surface.
2829 // We don't want the second finger to be delivered to applications.
2830 //
2831 // For this to work well, we need to make sure to track the pointer that is really
2832 // active. If the user first puts one finger down to click then adds another
2833 // finger to drag then the active pointer should switch to the finger that is
2834 // being dragged.
Harry Cutts45483602022-08-24 14:36:48 +00002835 ALOGD_IF(DEBUG_GESTURES,
2836 "Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, currentFingerCount=%d",
2837 activeTouchId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002838 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002839 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002840 *outFinishPreviousGesture = true;
2841 mPointerGesture.activeGestureId = 0;
2842 }
2843
2844 // Switch pointers if needed.
2845 // Find the fastest pointer and follow it.
2846 if (activeTouchId >= 0 && currentFingerCount > 1) {
2847 int32_t bestId = -1;
2848 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2849 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2850 uint32_t id = idBits.clearFirstMarkedBit();
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002851 std::optional<float> vx =
2852 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, id);
2853 std::optional<float> vy =
2854 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, id);
2855 if (vx && vy) {
2856 float speed = hypotf(*vx, *vy);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002857 if (speed > bestSpeed) {
2858 bestId = id;
2859 bestSpeed = speed;
2860 }
2861 }
2862 }
2863 if (bestId >= 0 && bestId != activeTouchId) {
2864 mPointerGesture.activeTouchId = activeTouchId = bestId;
Harry Cutts45483602022-08-24 14:36:48 +00002865 ALOGD_IF(DEBUG_GESTURES,
2866 "Gestures: BUTTON_CLICK_OR_DRAG switched pointers, bestId=%d, "
2867 "bestSpeed=%0.3f",
2868 bestId, bestSpeed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002869 }
2870 }
2871
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002872 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002873 // When using spots, the click will occur at the position of the anchor
2874 // spot and all other spots will move there.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002875 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002876 } else {
2877 mPointerVelocityControl.reset();
2878 }
2879
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002880 float x, y;
2881 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002882
Michael Wright227c5542020-07-02 18:30:52 +01002883 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002884 mPointerGesture.currentGestureIdBits.clear();
2885 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2886 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2887 mPointerGesture.currentGestureProperties[0].clear();
2888 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2889 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2890 mPointerGesture.currentGestureCoords[0].clear();
2891 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2892 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2893 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2894 } else if (currentFingerCount == 0) {
2895 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002896 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002897 *outFinishPreviousGesture = true;
2898 }
2899
2900 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2901 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2902 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002903 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2904 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002905 lastFingerCount == 1) {
2906 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002907 float x, y;
2908 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002909 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2910 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Harry Cutts45483602022-08-24 14:36:48 +00002911 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002912
2913 mPointerGesture.tapUpTime = when;
2914 getContext()->requestTimeoutAtTime(when +
2915 mConfig.pointerGestureTapDragInterval);
2916
2917 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002918 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002919 mPointerGesture.currentGestureIdBits.clear();
2920 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2921 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2922 mPointerGesture.currentGestureProperties[0].clear();
2923 mPointerGesture.currentGestureProperties[0].id =
2924 mPointerGesture.activeGestureId;
2925 mPointerGesture.currentGestureProperties[0].toolType =
2926 AMOTION_EVENT_TOOL_TYPE_FINGER;
2927 mPointerGesture.currentGestureCoords[0].clear();
2928 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2929 mPointerGesture.tapX);
2930 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2931 mPointerGesture.tapY);
2932 mPointerGesture.currentGestureCoords[0]
2933 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2934
2935 tapped = true;
2936 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002937 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP, deltaX=%f, deltaY=%f",
2938 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002939 }
2940 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002941 if (DEBUG_GESTURES) {
2942 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2943 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2944 (when - mPointerGesture.tapDownTime) * 0.000001f);
2945 } else {
2946 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2947 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002948 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002949 }
2950 }
2951
2952 mPointerVelocityControl.reset();
2953
2954 if (!tapped) {
Harry Cutts45483602022-08-24 14:36:48 +00002955 ALOGD_IF(DEBUG_GESTURES, "Gestures: NEUTRAL");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002956 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002957 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002958 mPointerGesture.currentGestureIdBits.clear();
2959 }
2960 } else if (currentFingerCount == 1) {
2961 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2962 // The pointer follows the active touch point.
2963 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2964 // When in TAP_DRAG, emit MOVE events at the pointer location.
2965 ALOG_ASSERT(activeTouchId >= 0);
2966
Michael Wright227c5542020-07-02 18:30:52 +01002967 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2968 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002969 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002970 float x, y;
2971 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002972 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2973 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002974 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002975 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002976 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2977 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002978 }
2979 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002980 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, %0.3fms time since up",
2981 (when - mPointerGesture.tapUpTime) * 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002982 }
Michael Wright227c5542020-07-02 18:30:52 +01002983 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2984 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002985 }
2986
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002987 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002988 // When using spots, the hover or drag will occur at the position of the anchor spot.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002989 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002990 } else {
2991 mPointerVelocityControl.reset();
2992 }
2993
2994 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01002995 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Harry Cutts45483602022-08-24 14:36:48 +00002996 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP_DRAG");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002997 down = true;
2998 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002999 ALOGD_IF(DEBUG_GESTURES, "Gestures: HOVER");
Michael Wright227c5542020-07-02 18:30:52 +01003000 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003001 *outFinishPreviousGesture = true;
3002 }
3003 mPointerGesture.activeGestureId = 0;
3004 down = false;
3005 }
3006
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003007 float x, y;
3008 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003009
3010 mPointerGesture.currentGestureIdBits.clear();
3011 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3012 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3013 mPointerGesture.currentGestureProperties[0].clear();
3014 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3015 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3016 mPointerGesture.currentGestureCoords[0].clear();
3017 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3018 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3019 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3020 down ? 1.0f : 0.0f);
3021
3022 if (lastFingerCount == 0 && currentFingerCount != 0) {
3023 mPointerGesture.resetTap();
3024 mPointerGesture.tapDownTime = when;
3025 mPointerGesture.tapX = x;
3026 mPointerGesture.tapY = y;
3027 }
3028 } else {
3029 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3030 // We need to provide feedback for each finger that goes down so we cannot wait
3031 // for the fingers to move before deciding what to do.
3032 //
3033 // The ambiguous case is deciding what to do when there are two fingers down but they
3034 // have not moved enough to determine whether they are part of a drag or part of a
3035 // freeform gesture, or just a press or long-press at the pointer location.
3036 //
3037 // When there are two fingers we start with the PRESS hypothesis and we generate a
3038 // down at the pointer location.
3039 //
3040 // When the two fingers move enough or when additional fingers are added, we make
3041 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3042 ALOG_ASSERT(activeTouchId >= 0);
3043
3044 bool settled = when >=
3045 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003046 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3047 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3048 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003049 *outFinishPreviousGesture = true;
3050 } else if (!settled && currentFingerCount > lastFingerCount) {
3051 // Additional pointers have gone down but not yet settled.
3052 // Reset the gesture.
Harry Cutts45483602022-08-24 14:36:48 +00003053 ALOGD_IF(DEBUG_GESTURES,
3054 "Gestures: Resetting gesture since additional pointers went down for "
3055 "MULTITOUCH, settle time remaining %0.3fms",
3056 (mPointerGesture.firstTouchTime +
3057 mConfig.pointerGestureMultitouchSettleInterval - when) *
3058 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003059 *outCancelPreviousGesture = true;
3060 } else {
3061 // Continue previous gesture.
3062 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3063 }
3064
3065 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003066 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003067 mPointerGesture.activeGestureId = 0;
3068 mPointerGesture.referenceIdBits.clear();
3069 mPointerVelocityControl.reset();
3070
3071 // Use the centroid and pointer location as the reference points for the gesture.
Harry Cutts45483602022-08-24 14:36:48 +00003072 ALOGD_IF(DEBUG_GESTURES,
3073 "Gestures: Using centroid as reference for MULTITOUCH, settle time remaining "
3074 "%0.3fms",
3075 (mPointerGesture.firstTouchTime +
3076 mConfig.pointerGestureMultitouchSettleInterval - when) *
3077 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003078 mCurrentRawState.rawPointerData
3079 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3080 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003081 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3082 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003083 }
3084
3085 // Clear the reference deltas for fingers not yet included in the reference calculation.
3086 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3087 ~mPointerGesture.referenceIdBits.value);
3088 !idBits.isEmpty();) {
3089 uint32_t id = idBits.clearFirstMarkedBit();
3090 mPointerGesture.referenceDeltas[id].dx = 0;
3091 mPointerGesture.referenceDeltas[id].dy = 0;
3092 }
3093 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3094
3095 // Add delta for all fingers and calculate a common movement delta.
3096 float commonDeltaX = 0, commonDeltaY = 0;
3097 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3098 mCurrentCookedState.fingerIdBits.value);
3099 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3100 bool first = (idBits == commonIdBits);
3101 uint32_t id = idBits.clearFirstMarkedBit();
3102 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3103 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3104 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3105 delta.dx += cpd.x - lpd.x;
3106 delta.dy += cpd.y - lpd.y;
3107
3108 if (first) {
3109 commonDeltaX = delta.dx;
3110 commonDeltaY = delta.dy;
3111 } else {
3112 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3113 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3114 }
3115 }
3116
3117 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003118 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003119 float dist[MAX_POINTER_ID + 1];
3120 int32_t distOverThreshold = 0;
3121 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3122 uint32_t id = idBits.clearFirstMarkedBit();
3123 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3124 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3125 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3126 distOverThreshold += 1;
3127 }
3128 }
3129
3130 // Only transition when at least two pointers have moved further than
3131 // the minimum distance threshold.
3132 if (distOverThreshold >= 2) {
3133 if (currentFingerCount > 2) {
3134 // There are more than two pointers, switch to FREEFORM.
Harry Cutts45483602022-08-24 14:36:48 +00003135 ALOGD_IF(DEBUG_GESTURES,
3136 "Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3137 currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003138 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003139 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003140 } else {
3141 // There are exactly two pointers.
3142 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3143 uint32_t id1 = idBits.clearFirstMarkedBit();
3144 uint32_t id2 = idBits.firstMarkedBit();
3145 const RawPointerData::Pointer& p1 =
3146 mCurrentRawState.rawPointerData.pointerForId(id1);
3147 const RawPointerData::Pointer& p2 =
3148 mCurrentRawState.rawPointerData.pointerForId(id2);
3149 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3150 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3151 // There are two pointers but they are too far apart for a SWIPE,
3152 // switch to FREEFORM.
Harry Cutts45483602022-08-24 14:36:48 +00003153 ALOGD_IF(DEBUG_GESTURES,
3154 "Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
3155 mutualDistance, mPointerGestureMaxSwipeWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003156 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003157 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003158 } else {
3159 // There are two pointers. Wait for both pointers to start moving
3160 // before deciding whether this is a SWIPE or FREEFORM gesture.
3161 float dist1 = dist[id1];
3162 float dist2 = dist[id2];
3163 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3164 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3165 // Calculate the dot product of the displacement vectors.
3166 // When the vectors are oriented in approximately the same direction,
3167 // the angle betweeen them is near zero and the cosine of the angle
3168 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3169 // mag(v2).
3170 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3171 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3172 float dx1 = delta1.dx * mPointerXZoomScale;
3173 float dy1 = delta1.dy * mPointerYZoomScale;
3174 float dx2 = delta2.dx * mPointerXZoomScale;
3175 float dy2 = delta2.dy * mPointerYZoomScale;
3176 float dot = dx1 * dx2 + dy1 * dy2;
3177 float cosine = dot / (dist1 * dist2); // denominator always > 0
3178 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3179 // Pointers are moving in the same direction. Switch to SWIPE.
Harry Cutts45483602022-08-24 14:36:48 +00003180 ALOGD_IF(DEBUG_GESTURES,
3181 "Gestures: PRESS transitioned to SWIPE, "
3182 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3183 "cosine %0.3f >= %0.3f",
3184 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3185 mConfig.pointerGestureMultitouchMinDistance, cosine,
3186 mConfig.pointerGestureSwipeTransitionAngleCosine);
Michael Wright227c5542020-07-02 18:30:52 +01003187 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003188 } else {
3189 // Pointers are moving in different directions. Switch to FREEFORM.
Harry Cutts45483602022-08-24 14:36:48 +00003190 ALOGD_IF(DEBUG_GESTURES,
3191 "Gestures: PRESS transitioned to FREEFORM, "
3192 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3193 "cosine %0.3f < %0.3f",
3194 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3195 mConfig.pointerGestureMultitouchMinDistance, cosine,
3196 mConfig.pointerGestureSwipeTransitionAngleCosine);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003197 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003198 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003199 }
3200 }
3201 }
3202 }
3203 }
Michael Wright227c5542020-07-02 18:30:52 +01003204 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003205 // Switch from SWIPE to FREEFORM if additional pointers go down.
3206 // Cancel previous gesture.
3207 if (currentFingerCount > 2) {
Harry Cutts45483602022-08-24 14:36:48 +00003208 ALOGD_IF(DEBUG_GESTURES,
3209 "Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3210 currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003211 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003212 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003213 }
3214 }
3215
3216 // Move the reference points based on the overall group motion of the fingers
3217 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003218 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003219 (commonDeltaX || commonDeltaY)) {
3220 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3221 uint32_t id = idBits.clearFirstMarkedBit();
3222 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3223 delta.dx = 0;
3224 delta.dy = 0;
3225 }
3226
3227 mPointerGesture.referenceTouchX += commonDeltaX;
3228 mPointerGesture.referenceTouchY += commonDeltaY;
3229
3230 commonDeltaX *= mPointerXMovementScale;
3231 commonDeltaY *= mPointerYMovementScale;
3232
Prabir Pradhan1728b212021-10-19 16:00:03 -07003233 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003234 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3235
3236 mPointerGesture.referenceGestureX += commonDeltaX;
3237 mPointerGesture.referenceGestureY += commonDeltaY;
3238 }
3239
3240 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003241 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3242 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003243 // PRESS or SWIPE mode.
Harry Cutts45483602022-08-24 14:36:48 +00003244 ALOGD_IF(DEBUG_GESTURES,
3245 "Gestures: PRESS or SWIPE activeTouchId=%d, activeGestureId=%d, "
3246 "currentTouchPointerCount=%d",
3247 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003248 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3249
3250 mPointerGesture.currentGestureIdBits.clear();
3251 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3252 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3253 mPointerGesture.currentGestureProperties[0].clear();
3254 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3255 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3256 mPointerGesture.currentGestureCoords[0].clear();
3257 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3258 mPointerGesture.referenceGestureX);
3259 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3260 mPointerGesture.referenceGestureY);
3261 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003262 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003263 // FREEFORM mode.
Harry Cutts45483602022-08-24 14:36:48 +00003264 ALOGD_IF(DEBUG_GESTURES,
3265 "Gestures: FREEFORM activeTouchId=%d, activeGestureId=%d, "
3266 "currentTouchPointerCount=%d",
3267 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003268 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3269
3270 mPointerGesture.currentGestureIdBits.clear();
3271
3272 BitSet32 mappedTouchIdBits;
3273 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003274 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003275 // Initially, assign the active gesture id to the active touch point
3276 // if there is one. No other touch id bits are mapped yet.
3277 if (!*outCancelPreviousGesture) {
3278 mappedTouchIdBits.markBit(activeTouchId);
3279 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3280 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3281 mPointerGesture.activeGestureId;
3282 } else {
3283 mPointerGesture.activeGestureId = -1;
3284 }
3285 } else {
3286 // Otherwise, assume we mapped all touches from the previous frame.
3287 // Reuse all mappings that are still applicable.
3288 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3289 mCurrentCookedState.fingerIdBits.value;
3290 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3291
3292 // Check whether we need to choose a new active gesture id because the
3293 // current went went up.
3294 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3295 ~mCurrentCookedState.fingerIdBits.value);
3296 !upTouchIdBits.isEmpty();) {
3297 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3298 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3299 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3300 mPointerGesture.activeGestureId = -1;
3301 break;
3302 }
3303 }
3304 }
3305
Harry Cutts45483602022-08-24 14:36:48 +00003306 ALOGD_IF(DEBUG_GESTURES,
3307 "Gestures: FREEFORM follow up mappedTouchIdBits=0x%08x, "
3308 "usedGestureIdBits=0x%08x, activeGestureId=%d",
3309 mappedTouchIdBits.value, usedGestureIdBits.value,
3310 mPointerGesture.activeGestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003311
3312 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3313 for (uint32_t i = 0; i < currentFingerCount; i++) {
3314 uint32_t touchId = idBits.clearFirstMarkedBit();
3315 uint32_t gestureId;
3316 if (!mappedTouchIdBits.hasBit(touchId)) {
3317 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3318 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Harry Cutts45483602022-08-24 14:36:48 +00003319 ALOGD_IF(DEBUG_GESTURES,
3320 "Gestures: FREEFORM new mapping for touch id %d -> gesture id %d",
3321 touchId, gestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003322 } else {
3323 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Harry Cutts45483602022-08-24 14:36:48 +00003324 ALOGD_IF(DEBUG_GESTURES,
3325 "Gestures: FREEFORM existing mapping for touch id %d -> gesture id %d",
3326 touchId, gestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003327 }
3328 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3329 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3330
3331 const RawPointerData::Pointer& pointer =
3332 mCurrentRawState.rawPointerData.pointerForId(touchId);
3333 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3334 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003335 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003336
3337 mPointerGesture.currentGestureProperties[i].clear();
3338 mPointerGesture.currentGestureProperties[i].id = gestureId;
3339 mPointerGesture.currentGestureProperties[i].toolType =
3340 AMOTION_EVENT_TOOL_TYPE_FINGER;
3341 mPointerGesture.currentGestureCoords[i].clear();
3342 mPointerGesture.currentGestureCoords[i]
3343 .setAxisValue(AMOTION_EVENT_AXIS_X,
3344 mPointerGesture.referenceGestureX + deltaX);
3345 mPointerGesture.currentGestureCoords[i]
3346 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3347 mPointerGesture.referenceGestureY + deltaY);
3348 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3349 1.0f);
3350 }
3351
3352 if (mPointerGesture.activeGestureId < 0) {
3353 mPointerGesture.activeGestureId =
3354 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Harry Cutts45483602022-08-24 14:36:48 +00003355 ALOGD_IF(DEBUG_GESTURES, "Gestures: FREEFORM new activeGestureId=%d",
3356 mPointerGesture.activeGestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003357 }
3358 }
3359 }
3360
3361 mPointerController->setButtonState(mCurrentRawState.buttonState);
3362
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003363 if (DEBUG_GESTURES) {
3364 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3365 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3366 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3367 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3368 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3369 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3370 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3371 uint32_t id = idBits.clearFirstMarkedBit();
3372 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3373 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3374 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3375 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3376 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3377 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3378 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3379 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3380 }
3381 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3382 uint32_t id = idBits.clearFirstMarkedBit();
3383 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3384 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3385 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3386 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3387 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3388 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3389 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3390 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3391 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003392 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003393 return true;
3394}
3395
Harry Cutts714d1ad2022-08-24 16:36:43 +00003396void TouchInputMapper::moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId) {
3397 const RawPointerData::Pointer& currentPointer =
3398 mCurrentRawState.rawPointerData.pointerForId(pointerId);
3399 const RawPointerData::Pointer& lastPointer =
3400 mLastRawState.rawPointerData.pointerForId(pointerId);
3401 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3402 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3403
3404 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
3405 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3406
3407 mPointerController->move(deltaX, deltaY);
3408}
3409
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003411 mPointerSimple.currentCoords.clear();
3412 mPointerSimple.currentProperties.clear();
3413
3414 bool down, hovering;
3415 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3416 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3417 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003418 mPointerController
3419 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3420 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003421
3422 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3423 down = !hovering;
3424
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003425 float x, y;
3426 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003427 mPointerSimple.currentCoords.copyFrom(
3428 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3429 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3430 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3431 mPointerSimple.currentProperties.id = 0;
3432 mPointerSimple.currentProperties.toolType =
3433 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3434 } else {
3435 down = false;
3436 hovering = false;
3437 }
3438
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003440}
3441
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3443 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003444}
3445
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003446void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003447 mPointerSimple.currentCoords.clear();
3448 mPointerSimple.currentProperties.clear();
3449
3450 bool down, hovering;
3451 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3452 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003453 if (mLastCookedState.mouseIdBits.hasBit(id)) {
Harry Cutts714d1ad2022-08-24 16:36:43 +00003454 moveMousePointerFromPointerDelta(when, id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003455 } else {
3456 mPointerVelocityControl.reset();
3457 }
3458
3459 down = isPointerDown(mCurrentRawState.buttonState);
3460 hovering = !down;
3461
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003462 float x, y;
3463 mPointerController->getPosition(&x, &y);
Harry Cutts714d1ad2022-08-24 16:36:43 +00003464 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003465 mPointerSimple.currentCoords.copyFrom(
3466 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3467 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3468 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3469 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3470 hovering ? 0.0f : 1.0f);
3471 mPointerSimple.currentProperties.id = 0;
3472 mPointerSimple.currentProperties.toolType =
3473 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3474 } else {
3475 mPointerVelocityControl.reset();
3476
3477 down = false;
3478 hovering = false;
3479 }
3480
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003481 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003482}
3483
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003484void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3485 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003486
3487 mPointerVelocityControl.reset();
3488}
3489
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003490void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3491 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003492 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003493
3494 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003495 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003496 mPointerController->clearSpots();
3497 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003498 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003499 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003500 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003501 }
Garfield Tan9514d782020-11-10 16:37:23 -08003502 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003503
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003504 float xCursorPosition, yCursorPosition;
3505 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003506
3507 if (mPointerSimple.down && !down) {
3508 mPointerSimple.down = false;
3509
3510 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3512 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003513 mLastRawState.buttonState, MotionClassification::NONE,
3514 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3515 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3516 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3517 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003518 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003519 }
3520
3521 if (mPointerSimple.hovering && !hovering) {
3522 mPointerSimple.hovering = false;
3523
3524 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3526 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3527 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003528 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3529 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3530 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3531 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003532 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003533 }
3534
3535 if (down) {
3536 if (!mPointerSimple.down) {
3537 mPointerSimple.down = true;
3538 mPointerSimple.downTime = when;
3539
3540 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003541 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003542 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3543 metaState, mCurrentRawState.buttonState,
3544 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3545 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3546 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3547 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003548 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003549 }
3550
3551 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3553 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003554 mCurrentRawState.buttonState, MotionClassification::NONE,
3555 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3556 &mPointerSimple.currentCoords, mOrientedXPrecision,
3557 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3558 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003559 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003560 }
3561
3562 if (hovering) {
3563 if (!mPointerSimple.hovering) {
3564 mPointerSimple.hovering = true;
3565
3566 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003567 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003568 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3569 metaState, mCurrentRawState.buttonState,
3570 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3571 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3572 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3573 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003574 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003575 }
3576
3577 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003578 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3579 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3580 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003581 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3582 &mPointerSimple.currentCoords, mOrientedXPrecision,
3583 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3584 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003585 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003586 }
3587
3588 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3589 float vscroll = mCurrentRawState.rawVScroll;
3590 float hscroll = mCurrentRawState.rawHScroll;
3591 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3592 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3593
3594 // Send scroll.
3595 PointerCoords pointerCoords;
3596 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3597 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3598 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3599
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3601 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003602 mCurrentRawState.buttonState, MotionClassification::NONE,
3603 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3604 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3605 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3606 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003607 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003608 }
3609
3610 // Save state.
3611 if (down || hovering) {
3612 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3613 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3614 } else {
3615 mPointerSimple.reset();
3616 }
3617}
3618
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003620 mPointerSimple.currentCoords.clear();
3621 mPointerSimple.currentProperties.clear();
3622
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003624}
3625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3627 uint32_t source, int32_t action, int32_t actionButton,
3628 int32_t flags, int32_t metaState, int32_t buttonState,
3629 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003630 const PointerCoords* coords, const uint32_t* idToIndex,
3631 BitSet32 idBits, int32_t changedId, float xPrecision,
Harry Cutts2800fb02022-09-15 13:49:23 +00003632 float yPrecision, nsecs_t downTime,
3633 MotionClassification classification) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003634 PointerCoords pointerCoords[MAX_POINTERS];
3635 PointerProperties pointerProperties[MAX_POINTERS];
3636 uint32_t pointerCount = 0;
3637 while (!idBits.isEmpty()) {
3638 uint32_t id = idBits.clearFirstMarkedBit();
3639 uint32_t index = idToIndex[id];
3640 pointerProperties[pointerCount].copyFrom(properties[index]);
3641 pointerCoords[pointerCount].copyFrom(coords[index]);
3642
3643 if (changedId >= 0 && id == uint32_t(changedId)) {
3644 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3645 }
3646
3647 pointerCount += 1;
3648 }
3649
3650 ALOG_ASSERT(pointerCount != 0);
3651
3652 if (changedId >= 0 && pointerCount == 1) {
3653 // Replace initial down and final up action.
3654 // We can compare the action without masking off the changed pointer index
3655 // because we know the index is 0.
3656 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3657 action = AMOTION_EVENT_ACTION_DOWN;
3658 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003659 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3660 action = AMOTION_EVENT_ACTION_CANCEL;
3661 } else {
3662 action = AMOTION_EVENT_ACTION_UP;
3663 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003664 } else {
3665 // Can't happen.
3666 ALOG_ASSERT(false);
3667 }
3668 }
3669 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3670 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003671 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003672 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003673 }
3674 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3675 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003676 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003677 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003678 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3680 policyFlags, action, actionButton, flags, metaState, buttonState,
Harry Cutts2800fb02022-09-15 13:49:23 +00003681 classification, edgeFlags, pointerCount, pointerProperties, pointerCoords,
3682 xPrecision, yPrecision, xCursorPosition, yCursorPosition, downTime,
3683 std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003684 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003685}
3686
3687bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3688 const PointerCoords* inCoords,
3689 const uint32_t* inIdToIndex,
3690 PointerProperties* outProperties,
3691 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3692 BitSet32 idBits) const {
3693 bool changed = false;
3694 while (!idBits.isEmpty()) {
3695 uint32_t id = idBits.clearFirstMarkedBit();
3696 uint32_t inIndex = inIdToIndex[id];
3697 uint32_t outIndex = outIdToIndex[id];
3698
3699 const PointerProperties& curInProperties = inProperties[inIndex];
3700 const PointerCoords& curInCoords = inCoords[inIndex];
3701 PointerProperties& curOutProperties = outProperties[outIndex];
3702 PointerCoords& curOutCoords = outCoords[outIndex];
3703
3704 if (curInProperties != curOutProperties) {
3705 curOutProperties.copyFrom(curInProperties);
3706 changed = true;
3707 }
3708
3709 if (curInCoords != curOutCoords) {
3710 curOutCoords.copyFrom(curInCoords);
3711 changed = true;
3712 }
3713 }
3714 return changed;
3715}
3716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3718 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3719 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003720}
3721
Prabir Pradhan1728b212021-10-19 16:00:03 -07003722// Transform input device coordinates to display panel coordinates.
3723void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003724 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3725 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3726
arthurhunga36b28e2020-12-29 20:28:15 +08003727 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3728 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3729
Prabir Pradhan1728b212021-10-19 16:00:03 -07003730 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003731 // 0 - no swap and reverse.
3732 // 90 - swap x/y and reverse y.
3733 // 180 - reverse x, y.
3734 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003735 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003736 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003737 x = xScaled;
3738 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003739 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003740 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003741 y = xScaledMax;
3742 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003743 break;
3744 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003745 x = xScaledMax;
3746 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003747 break;
3748 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003749 y = xScaled;
3750 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003751 break;
3752 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003753 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003754 }
3755}
3756
Prabir Pradhan1728b212021-10-19 16:00:03 -07003757bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003758 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3759 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3760
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003761 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003762 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003763 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003764 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003765}
3766
3767const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3768 for (const VirtualKey& virtualKey : mVirtualKeys) {
Harry Cutts45483602022-08-24 14:36:48 +00003769 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
3770 "VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3771 "left=%d, top=%d, right=%d, bottom=%d",
3772 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3773 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003774
3775 if (virtualKey.isHit(x, y)) {
3776 return &virtualKey;
3777 }
3778 }
3779
3780 return nullptr;
3781}
3782
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003783void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3784 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3785 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003786
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003787 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003788
3789 if (currentPointerCount == 0) {
3790 // No pointers to assign.
3791 return;
3792 }
3793
3794 if (lastPointerCount == 0) {
3795 // All pointers are new.
3796 for (uint32_t i = 0; i < currentPointerCount; i++) {
3797 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003798 current.rawPointerData.pointers[i].id = id;
3799 current.rawPointerData.idToIndex[id] = i;
3800 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003801 }
3802 return;
3803 }
3804
3805 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003806 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003807 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003808 uint32_t id = last.rawPointerData.pointers[0].id;
3809 current.rawPointerData.pointers[0].id = id;
3810 current.rawPointerData.idToIndex[id] = 0;
3811 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003812 return;
3813 }
3814
3815 // General case.
3816 // We build a heap of squared euclidean distances between current and last pointers
3817 // associated with the current and last pointer indices. Then, we find the best
3818 // match (by distance) for each current pointer.
3819 // The pointers must have the same tool type but it is possible for them to
3820 // transition from hovering to touching or vice-versa while retaining the same id.
3821 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3822
3823 uint32_t heapSize = 0;
3824 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3825 currentPointerIndex++) {
3826 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3827 lastPointerIndex++) {
3828 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003829 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003830 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003831 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003832 if (currentPointer.toolType == lastPointer.toolType) {
3833 int64_t deltaX = currentPointer.x - lastPointer.x;
3834 int64_t deltaY = currentPointer.y - lastPointer.y;
3835
3836 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3837
3838 // Insert new element into the heap (sift up).
3839 heap[heapSize].currentPointerIndex = currentPointerIndex;
3840 heap[heapSize].lastPointerIndex = lastPointerIndex;
3841 heap[heapSize].distance = distance;
3842 heapSize += 1;
3843 }
3844 }
3845 }
3846
3847 // Heapify
3848 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3849 startIndex -= 1;
3850 for (uint32_t parentIndex = startIndex;;) {
3851 uint32_t childIndex = parentIndex * 2 + 1;
3852 if (childIndex >= heapSize) {
3853 break;
3854 }
3855
3856 if (childIndex + 1 < heapSize &&
3857 heap[childIndex + 1].distance < heap[childIndex].distance) {
3858 childIndex += 1;
3859 }
3860
3861 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3862 break;
3863 }
3864
3865 swap(heap[parentIndex], heap[childIndex]);
3866 parentIndex = childIndex;
3867 }
3868 }
3869
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003870 if (DEBUG_POINTER_ASSIGNMENT) {
3871 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3872 for (size_t i = 0; i < heapSize; i++) {
3873 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3874 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3875 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003876 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003877
3878 // Pull matches out by increasing order of distance.
3879 // To avoid reassigning pointers that have already been matched, the loop keeps track
3880 // of which last and current pointers have been matched using the matchedXXXBits variables.
3881 // It also tracks the used pointer id bits.
3882 BitSet32 matchedLastBits(0);
3883 BitSet32 matchedCurrentBits(0);
3884 BitSet32 usedIdBits(0);
3885 bool first = true;
3886 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3887 while (heapSize > 0) {
3888 if (first) {
3889 // The first time through the loop, we just consume the root element of
3890 // the heap (the one with smallest distance).
3891 first = false;
3892 } else {
3893 // Previous iterations consumed the root element of the heap.
3894 // Pop root element off of the heap (sift down).
3895 heap[0] = heap[heapSize];
3896 for (uint32_t parentIndex = 0;;) {
3897 uint32_t childIndex = parentIndex * 2 + 1;
3898 if (childIndex >= heapSize) {
3899 break;
3900 }
3901
3902 if (childIndex + 1 < heapSize &&
3903 heap[childIndex + 1].distance < heap[childIndex].distance) {
3904 childIndex += 1;
3905 }
3906
3907 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3908 break;
3909 }
3910
3911 swap(heap[parentIndex], heap[childIndex]);
3912 parentIndex = childIndex;
3913 }
3914
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003915 if (DEBUG_POINTER_ASSIGNMENT) {
3916 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3917 for (size_t j = 0; j < heapSize; j++) {
3918 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3919 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3920 heap[j].distance);
3921 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003922 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003923 }
3924
3925 heapSize -= 1;
3926
3927 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3928 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3929
3930 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3931 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3932
3933 matchedCurrentBits.markBit(currentPointerIndex);
3934 matchedLastBits.markBit(lastPointerIndex);
3935
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003936 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3937 current.rawPointerData.pointers[currentPointerIndex].id = id;
3938 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3939 current.rawPointerData.markIdBit(id,
3940 current.rawPointerData.isHovering(
3941 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003942 usedIdBits.markBit(id);
3943
Harry Cutts45483602022-08-24 14:36:48 +00003944 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
3945 "assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3946 ", distance=%" PRIu64,
3947 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003948 break;
3949 }
3950 }
3951
3952 // Assign fresh ids to pointers that were not matched in the process.
3953 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3954 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3955 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3956
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003957 current.rawPointerData.pointers[currentPointerIndex].id = id;
3958 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3959 current.rawPointerData.markIdBit(id,
3960 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003961
Harry Cutts45483602022-08-24 14:36:48 +00003962 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
3963 "assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3964 id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003965 }
3966}
3967
3968int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3969 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3970 return AKEY_STATE_VIRTUAL;
3971 }
3972
3973 for (const VirtualKey& virtualKey : mVirtualKeys) {
3974 if (virtualKey.keyCode == keyCode) {
3975 return AKEY_STATE_UP;
3976 }
3977 }
3978
3979 return AKEY_STATE_UNKNOWN;
3980}
3981
3982int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
3983 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
3984 return AKEY_STATE_VIRTUAL;
3985 }
3986
3987 for (const VirtualKey& virtualKey : mVirtualKeys) {
3988 if (virtualKey.scanCode == scanCode) {
3989 return AKEY_STATE_UP;
3990 }
3991 }
3992
3993 return AKEY_STATE_UNKNOWN;
3994}
3995
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003996bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
3997 const std::vector<int32_t>& keyCodes,
3998 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003999 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004000 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004001 if (virtualKey.keyCode == keyCodes[i]) {
4002 outFlags[i] = 1;
4003 }
4004 }
4005 }
4006
4007 return true;
4008}
4009
4010std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4011 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004012 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004013 return std::make_optional(mPointerController->getDisplayId());
4014 } else {
4015 return std::make_optional(mViewport.displayId);
4016 }
4017 }
4018 return std::nullopt;
4019}
4020
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004021} // namespace android