blob: c2454ac6d4f60f3e58541c15a7132b9faf08116b [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
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000192 if (mDeviceMode == DeviceMode::DISABLED) {
193 return;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700194 }
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000195
196 info->addMotionRange(mOrientedRanges.x);
197 info->addMotionRange(mOrientedRanges.y);
198 info->addMotionRange(mOrientedRanges.pressure);
199
200 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
201 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
202 //
203 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
204 // motion, i.e. the hardware dimensions, as the finger could move completely across the
205 // touchpad in one sample cycle.
206 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
207 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
208 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat, x.fuzz,
209 x.resolution);
210 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat, y.fuzz,
211 y.resolution);
212 }
213
214 if (mOrientedRanges.size) {
215 info->addMotionRange(*mOrientedRanges.size);
216 }
217
218 if (mOrientedRanges.touchMajor) {
219 info->addMotionRange(*mOrientedRanges.touchMajor);
220 info->addMotionRange(*mOrientedRanges.touchMinor);
221 }
222
223 if (mOrientedRanges.toolMajor) {
224 info->addMotionRange(*mOrientedRanges.toolMajor);
225 info->addMotionRange(*mOrientedRanges.toolMinor);
226 }
227
228 if (mOrientedRanges.orientation) {
229 info->addMotionRange(*mOrientedRanges.orientation);
230 }
231
232 if (mOrientedRanges.distance) {
233 info->addMotionRange(*mOrientedRanges.distance);
234 }
235
236 if (mOrientedRanges.tilt) {
237 info->addMotionRange(*mOrientedRanges.tilt);
238 }
239
240 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
241 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
242 }
243 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
244 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
245 }
246 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
247 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
248 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
249 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat, x.fuzz,
250 x.resolution);
251 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat, y.fuzz,
252 y.resolution);
253 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat, x.fuzz,
254 x.resolution);
255 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat, y.fuzz,
256 y.resolution);
257 }
258 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700259}
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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700349std::list<NotifyArgs> TouchInputMapper::configure(nsecs_t when,
350 const InputReaderConfiguration* config,
351 uint32_t changes) {
352 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700353
354 mConfig = *config;
355
356 if (!changes) { // first time only
357 // Configure basic parameters.
358 configureParameters();
359
360 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800361 mCursorScrollAccumulator.configure(getDeviceContext());
362 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700363
364 // Configure absolute axis information.
365 configureRawPointerAxes();
366
367 // Prepare input device calibration.
368 parseCalibration();
369 resolveCalibration();
370 }
371
372 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
373 // Update location calibration to reflect current settings
374 updateAffineTransformation();
375 }
376
377 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
378 // Update pointer speed.
379 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
380 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
381 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
382 }
383
384 bool resetNeeded = false;
385 if (!changes ||
386 (changes &
387 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800388 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
390 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
391 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700392 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700393 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700394 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395 }
396
397 if (changes && resetNeeded) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700398 out += reset(when);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000399
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700400 // Send reset, unless this is the first time the device has been configured,
401 // in which case the reader will call reset itself after all mappers are ready.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +0000402 out.emplace_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId()));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700404 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405}
406
407void TouchInputMapper::resolveExternalStylusPresence() {
408 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800409 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700410 mExternalStylusConnected = !devices.empty();
411
412 if (!mExternalStylusConnected) {
413 resetExternalStylus();
414 }
415}
416
417void TouchInputMapper::configureParameters() {
418 // Use the pointer presentation mode for devices that do not support distinct
419 // multitouch. The spot-based presentation relies on being able to accurately
420 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800421 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100422 ? Parameters::GestureMode::SINGLE_TOUCH
423 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700424
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700425 std::string gestureModeString;
426 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800427 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700428 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100429 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100431 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700432 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700433 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 }
435 }
436
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800437 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100439 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800440 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700441 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100442 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 } else {
444 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100445 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446 }
447
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800448 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700449
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700450 std::string deviceTypeString;
451 if (getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800452 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100454 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700455 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100456 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100458 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 } else if (deviceTypeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700460 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461 }
462 }
463
Michael Wright227c5542020-07-02 18:30:52 +0100464 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700465 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800466 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700468 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700469 std::string orientationString;
470 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700471 orientationString)) {
472 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
473 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
474 } else if (orientationString == "ORIENTATION_90") {
475 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
476 } else if (orientationString == "ORIENTATION_180") {
477 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
478 } else if (orientationString == "ORIENTATION_270") {
479 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
480 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700481 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700482 }
483 }
484
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700485 mParameters.hasAssociatedDisplay = false;
486 mParameters.associatedDisplayIsExternal = false;
487 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100488 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
489 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700490 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100491 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800492 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700493 std::string uniqueDisplayId;
494 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800495 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700496 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
497 }
498 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800499 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700500 mParameters.hasAssociatedDisplay = true;
501 }
502
503 // Initial downs on external touch devices should wake the device.
504 // Normally we don't do this for internal touch screens to prevent them from waking
505 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800506 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700507 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700508}
509
510void TouchInputMapper::dumpParameters(std::string& dump) {
511 dump += INDENT3 "Parameters:\n";
512
Dominik Laskowski75788452021-02-09 18:51:25 -0800513 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700514
Dominik Laskowski75788452021-02-09 18:51:25 -0800515 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700516
517 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
518 "displayId='%s'\n",
519 toString(mParameters.hasAssociatedDisplay),
520 toString(mParameters.associatedDisplayIsExternal),
521 mParameters.uniqueDisplayId.c_str());
522 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800523 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700524}
525
526void TouchInputMapper::configureRawPointerAxes() {
527 mRawPointerAxes.clear();
528}
529
530void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
531 dump += INDENT3 "Raw Touch Axes:\n";
532 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
533 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
534 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
535 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
536 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
537 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
538 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
539 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
545}
546
547bool TouchInputMapper::hasExternalStylus() const {
548 return mExternalStylusConnected;
549}
550
551/**
552 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000553 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800554 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000555 * 3. Get the matching viewport by either unique id in idc file or by the display type
556 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800557 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700558 */
559std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800560 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000561 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800562 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700563 }
564
Christine Franks2a2293c2022-01-18 11:51:16 -0800565 const std::optional<std::string> associatedDisplayUniqueId =
566 getDeviceContext().getAssociatedDisplayUniqueId();
567 if (associatedDisplayUniqueId) {
568 return getDeviceContext().getAssociatedViewport();
569 }
570
Michael Wright227c5542020-07-02 18:30:52 +0100571 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800572 std::optional<DisplayViewport> viewport =
573 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
574 if (viewport) {
575 return viewport;
576 } else {
577 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
578 mConfig.defaultPointerDisplayId);
579 }
580 }
581
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700582 // Check if uniqueDisplayId is specified in idc file.
583 if (!mParameters.uniqueDisplayId.empty()) {
584 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
585 }
586
587 ViewportType viewportTypeToUse;
588 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100589 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700590 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100591 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700592 }
593
594 std::optional<DisplayViewport> viewport =
595 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100596 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700597 ALOGW("Input device %s should be associated with external display, "
598 "fallback to internal one for the external viewport is not found.",
599 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100600 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700601 }
602
603 return viewport;
604 }
605
606 // No associated display, return a non-display viewport.
607 DisplayViewport newViewport;
608 // Raw width and height in the natural orientation.
609 int32_t rawWidth = mRawPointerAxes.getRawWidth();
610 int32_t rawHeight = mRawPointerAxes.getRawHeight();
611 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
612 return std::make_optional(newViewport);
613}
614
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800615int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
616 if (resolution < 0) {
617 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
618 getDeviceName().c_str());
619 return 0;
620 }
621 return resolution;
622}
623
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800624void TouchInputMapper::initializeSizeRanges() {
625 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
626 mSizeScale = 0.0f;
627 return;
628 }
629
630 // Size of diagonal axis.
631 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
632
633 // Size factors.
634 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
635 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
636 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
637 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
638 } else {
639 mSizeScale = 0.0f;
640 }
641
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700642 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
643 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
644 .source = mSource,
645 .min = 0,
646 .max = diagonalSize,
647 .flat = 0,
648 .fuzz = 0,
649 .resolution = 0,
650 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800651
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800652 if (mRawPointerAxes.touchMajor.valid) {
653 mRawPointerAxes.touchMajor.resolution =
654 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700655 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800656 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800657
658 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700659 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800660 if (mRawPointerAxes.touchMinor.valid) {
661 mRawPointerAxes.touchMinor.resolution =
662 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700663 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800664 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800665
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700666 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
667 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
668 .source = mSource,
669 .min = 0,
670 .max = diagonalSize,
671 .flat = 0,
672 .fuzz = 0,
673 .resolution = 0,
674 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800675 if (mRawPointerAxes.toolMajor.valid) {
676 mRawPointerAxes.toolMajor.resolution =
677 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700678 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800679 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800680
681 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700682 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800683 if (mRawPointerAxes.toolMinor.valid) {
684 mRawPointerAxes.toolMinor.resolution =
685 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700686 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800687 }
688
689 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700690 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
691 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
692 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
693 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800694 } else {
695 // Support for other calibrations can be added here.
696 ALOGW("%s calibration is not supported for size ranges at the moment. "
697 "Using raw resolution instead",
698 ftl::enum_string(mCalibration.sizeCalibration).c_str());
699 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800700
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700701 mOrientedRanges.size = InputDeviceInfo::MotionRange{
702 .axis = AMOTION_EVENT_AXIS_SIZE,
703 .source = mSource,
704 .min = 0,
705 .max = 1.0,
706 .flat = 0,
707 .fuzz = 0,
708 .resolution = 0,
709 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800710}
711
712void TouchInputMapper::initializeOrientedRanges() {
713 // Configure X and Y factors.
714 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
715 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
716 mXPrecision = 1.0f / mXScale;
717 mYPrecision = 1.0f / mYScale;
718
719 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
720 mOrientedRanges.x.source = mSource;
721 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
722 mOrientedRanges.y.source = mSource;
723
724 // Scale factor for terms that are not oriented in a particular axis.
725 // If the pixels are square then xScale == yScale otherwise we fake it
726 // by choosing an average.
727 mGeometricScale = avg(mXScale, mYScale);
728
729 initializeSizeRanges();
730
731 // Pressure factors.
732 mPressureScale = 0;
733 float pressureMax = 1.0;
734 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
735 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700736 if (mCalibration.pressureScale) {
737 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800738 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
739 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
740 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
741 }
742 }
743
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700744 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
745 .axis = AMOTION_EVENT_AXIS_PRESSURE,
746 .source = mSource,
747 .min = 0,
748 .max = pressureMax,
749 .flat = 0,
750 .fuzz = 0,
751 .resolution = 0,
752 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800753
754 // Tilt
755 mTiltXCenter = 0;
756 mTiltXScale = 0;
757 mTiltYCenter = 0;
758 mTiltYScale = 0;
759 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
760 if (mHaveTilt) {
761 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
762 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
763 mTiltXScale = M_PI / 180;
764 mTiltYScale = M_PI / 180;
765
766 if (mRawPointerAxes.tiltX.resolution) {
767 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
768 }
769 if (mRawPointerAxes.tiltY.resolution) {
770 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
771 }
772
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700773 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
774 .axis = AMOTION_EVENT_AXIS_TILT,
775 .source = mSource,
776 .min = 0,
777 .max = M_PI_2,
778 .flat = 0,
779 .fuzz = 0,
780 .resolution = 0,
781 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800782 }
783
784 // Orientation
785 mOrientationScale = 0;
786 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700787 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
788 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
789 .source = mSource,
790 .min = -M_PI,
791 .max = M_PI,
792 .flat = 0,
793 .fuzz = 0,
794 .resolution = 0,
795 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800796
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800797 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
798 if (mCalibration.orientationCalibration ==
799 Calibration::OrientationCalibration::INTERPOLATED) {
800 if (mRawPointerAxes.orientation.valid) {
801 if (mRawPointerAxes.orientation.maxValue > 0) {
802 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
803 } else if (mRawPointerAxes.orientation.minValue < 0) {
804 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
805 } else {
806 mOrientationScale = 0;
807 }
808 }
809 }
810
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700811 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
812 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
813 .source = mSource,
814 .min = -M_PI_2,
815 .max = M_PI_2,
816 .flat = 0,
817 .fuzz = 0,
818 .resolution = 0,
819 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800820 }
821
822 // Distance
823 mDistanceScale = 0;
824 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
825 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700826 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800827 }
828
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700829 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800830
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700831 .axis = AMOTION_EVENT_AXIS_DISTANCE,
832 .source = mSource,
833 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
834 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
835 .flat = 0,
836 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
837 .resolution = 0,
838 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800839 }
840
841 // Compute oriented precision, scales and ranges.
842 // Note that the maximum value reported is an inclusive maximum value so it is one
843 // unit less than the total width or height of the display.
844 switch (mInputDeviceOrientation) {
845 case DISPLAY_ORIENTATION_90:
846 case DISPLAY_ORIENTATION_270:
847 mOrientedXPrecision = mYPrecision;
848 mOrientedYPrecision = mXPrecision;
849
850 mOrientedRanges.x.min = 0;
851 mOrientedRanges.x.max = mDisplayHeight - 1;
852 mOrientedRanges.x.flat = 0;
853 mOrientedRanges.x.fuzz = 0;
854 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
855
856 mOrientedRanges.y.min = 0;
857 mOrientedRanges.y.max = mDisplayWidth - 1;
858 mOrientedRanges.y.flat = 0;
859 mOrientedRanges.y.fuzz = 0;
860 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
861 break;
862
863 default:
864 mOrientedXPrecision = mXPrecision;
865 mOrientedYPrecision = mYPrecision;
866
867 mOrientedRanges.x.min = 0;
868 mOrientedRanges.x.max = mDisplayWidth - 1;
869 mOrientedRanges.x.flat = 0;
870 mOrientedRanges.x.fuzz = 0;
871 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
872
873 mOrientedRanges.y.min = 0;
874 mOrientedRanges.y.max = mDisplayHeight - 1;
875 mOrientedRanges.y.flat = 0;
876 mOrientedRanges.y.fuzz = 0;
877 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
878 break;
879 }
880}
881
Prabir Pradhan1728b212021-10-19 16:00:03 -0700882void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000883 const DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700884
885 resolveExternalStylusPresence();
886
887 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100888 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000889 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700890 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100891 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700892 if (hasStylus()) {
893 mSource |= AINPUT_SOURCE_STYLUS;
894 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800895 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700896 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100897 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700898 if (hasStylus()) {
899 mSource |= AINPUT_SOURCE_STYLUS;
900 }
901 if (hasExternalStylus()) {
902 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
903 }
Michael Wright227c5542020-07-02 18:30:52 +0100904 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700905 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100906 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700907 } else {
908 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100909 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700910 }
911
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000912 const std::optional<DisplayViewport> newViewportOpt = findViewport();
913
914 // Ensure the device is valid and can be used.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700915 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
916 ALOGW("Touch device '%s' did not report support for X or Y axis! "
917 "The device will be inoperable.",
918 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100919 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000920 } else if (!newViewportOpt) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700921 ALOGI("Touch device '%s' could not query the properties of its associated "
922 "display. The device will be inoperable until the display size "
923 "becomes available.",
924 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100925 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000926 } else if (!newViewportOpt->isActive) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000927 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
928 getDeviceName().c_str(), getDeviceId());
929 mDeviceMode = DeviceMode::DISABLED;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000930 }
931
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700932 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700933 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
934 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
HQ Liue6983c72022-04-19 22:14:56 +0000935 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
936 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
937 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
938 const float rawMeanResolution =
939 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700940
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000941 const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport);
942 const bool viewportChanged = mViewport != newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700943 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700944 if (viewportChanged) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000945 const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation;
946 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId;
947 mViewport = newViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700948
Michael Wright227c5542020-07-02 18:30:52 +0100949 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700950 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700951 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
952 int32_t naturalPhysicalLeft, naturalPhysicalTop;
953 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700954
Prabir Pradhan1728b212021-10-19 16:00:03 -0700955 // Apply the inverse of the input device orientation so that the input device is
956 // configured in the same orientation as the viewport. The input device orientation will
957 // be re-applied by mInputDeviceOrientation.
958 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700959 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700960 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700961 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700962 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
963 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800964 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700965 naturalPhysicalTop = mViewport.physicalLeft;
966 naturalDeviceWidth = mViewport.deviceHeight;
967 naturalDeviceHeight = mViewport.deviceWidth;
968 break;
969 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700970 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
971 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
972 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
973 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
974 naturalDeviceWidth = mViewport.deviceWidth;
975 naturalDeviceHeight = mViewport.deviceHeight;
976 break;
977 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700978 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
979 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
980 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800981 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700982 naturalDeviceWidth = mViewport.deviceHeight;
983 naturalDeviceHeight = mViewport.deviceWidth;
984 break;
985 case DISPLAY_ORIENTATION_0:
986 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700987 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
988 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
989 naturalPhysicalLeft = mViewport.physicalLeft;
990 naturalPhysicalTop = mViewport.physicalTop;
991 naturalDeviceWidth = mViewport.deviceWidth;
992 naturalDeviceHeight = mViewport.deviceHeight;
993 break;
994 }
995
996 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
997 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
998 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
999 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1000 }
1001
1002 mPhysicalWidth = naturalPhysicalWidth;
1003 mPhysicalHeight = naturalPhysicalHeight;
1004 mPhysicalLeft = naturalPhysicalLeft;
1005 mPhysicalTop = naturalPhysicalTop;
1006
Prabir Pradhan1728b212021-10-19 16:00:03 -07001007 const int32_t oldDisplayWidth = mDisplayWidth;
1008 const int32_t oldDisplayHeight = mDisplayHeight;
1009 mDisplayWidth = naturalDeviceWidth;
1010 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001011
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001012 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1013 // anything if the device is already orientation-aware. If the device is not
1014 // orientation-aware, then we need to apply the inverse rotation of the display so that
1015 // when the display rotation is applied later as a part of the per-window transform, we
1016 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001017 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001018 ? DISPLAY_ORIENTATION_0
1019 : getInverseRotation(mViewport.orientation);
1020 // For orientation-aware devices that work in the un-rotated coordinate space, the
1021 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00001022 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
1023 mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight &&
1024 viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001025
1026 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001027 mInputDeviceOrientation =
1028 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001029 } else {
1030 mPhysicalWidth = rawWidth;
1031 mPhysicalHeight = rawHeight;
1032 mPhysicalLeft = 0;
1033 mPhysicalTop = 0;
1034
Prabir Pradhan1728b212021-10-19 16:00:03 -07001035 mDisplayWidth = rawWidth;
1036 mDisplayHeight = rawHeight;
1037 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001038 }
1039 }
1040
1041 // If moving between pointer modes, need to reset some state.
1042 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1043 if (deviceModeChanged) {
1044 mOrientedRanges.clear();
1045 }
1046
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001047 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1048 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001049 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001050 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001051 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1052 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001053 if (mPointerController == nullptr) {
1054 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001055 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001056 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001057 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1058 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001059 } else {
lilinnandef700b2022-06-17 19:32:01 +08001060 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1061 !mConfig.showTouches) {
1062 mPointerController->clearSpots();
1063 }
Michael Wright17db18e2020-06-26 20:51:44 +01001064 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001065 }
1066
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001067 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001068 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1069 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001070 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1071 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001072
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001073 configureVirtualKeys();
1074
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001075 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001076
1077 // Location
1078 updateAffineTransformation();
1079
Michael Wright227c5542020-07-02 18:30:52 +01001080 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001081 // Compute pointer gesture detection parameters.
1082 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001083 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001084
1085 // Scale movements such that one whole swipe of the touch pad covers a
1086 // given area relative to the diagonal size of the display when no acceleration
1087 // is applied.
1088 // Assume that the touch pad has a square aspect ratio such that movements in
1089 // X and Y of the same number of raw units cover the same physical distance.
1090 mPointerXMovementScale =
1091 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1092 mPointerYMovementScale = mPointerXMovementScale;
1093
1094 // Scale zooms to cover a smaller range of the display than movements do.
1095 // This value determines the area around the pointer that is affected by freeform
1096 // pointer gestures.
1097 mPointerXZoomScale =
1098 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1099 mPointerYZoomScale = mPointerXZoomScale;
1100
HQ Liue6983c72022-04-19 22:14:56 +00001101 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1102 // axis is non positive value.
1103 const float minFreeformGestureWidth =
1104 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1105
1106 mPointerGestureMaxSwipeWidth =
1107 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1108 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001109 }
1110
1111 // Inform the dispatcher about the changes.
1112 *outResetNeeded = true;
1113 bumpGeneration();
1114 }
1115}
1116
Prabir Pradhan1728b212021-10-19 16:00:03 -07001117void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001118 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001119 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1120 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001121 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1122 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1123 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1124 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001125 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001126}
1127
1128void TouchInputMapper::configureVirtualKeys() {
1129 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001130 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001131
1132 mVirtualKeys.clear();
1133
1134 if (virtualKeyDefinitions.size() == 0) {
1135 return;
1136 }
1137
1138 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1139 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1140 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1141 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1142
1143 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1144 VirtualKey virtualKey;
1145
1146 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1147 int32_t keyCode;
1148 int32_t dummyKeyMetaState;
1149 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001150 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1151 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001152 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1153 continue; // drop the key
1154 }
1155
1156 virtualKey.keyCode = keyCode;
1157 virtualKey.flags = flags;
1158
1159 // convert the key definition's display coordinates into touch coordinates for a hit box
1160 int32_t halfWidth = virtualKeyDefinition.width / 2;
1161 int32_t halfHeight = virtualKeyDefinition.height / 2;
1162
1163 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001164 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001165 touchScreenLeft;
1166 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001167 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001168 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001169 virtualKey.hitTop =
1170 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001171 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001172 virtualKey.hitBottom =
1173 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001174 touchScreenTop;
1175 mVirtualKeys.push_back(virtualKey);
1176 }
1177}
1178
1179void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1180 if (!mVirtualKeys.empty()) {
1181 dump += INDENT3 "Virtual Keys:\n";
1182
1183 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1184 const VirtualKey& virtualKey = mVirtualKeys[i];
1185 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1186 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1187 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1188 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1189 }
1190 }
1191}
1192
1193void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001194 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001195 Calibration& out = mCalibration;
1196
1197 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001198 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001199 std::string sizeCalibrationString;
1200 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001201 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001202 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001203 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001204 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001205 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001206 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001207 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001208 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001209 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001210 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001211 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001212 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001213 }
1214 }
1215
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001216 float sizeScale;
1217
1218 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1219 out.sizeScale = sizeScale;
1220 }
1221 float sizeBias;
1222 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1223 out.sizeBias = sizeBias;
1224 }
1225 bool sizeIsSummed;
1226 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1227 out.sizeIsSummed = sizeIsSummed;
1228 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001229
1230 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001231 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001232 std::string pressureCalibrationString;
1233 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001234 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001235 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001236 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001237 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001238 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001239 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001240 } else if (pressureCalibrationString != "default") {
1241 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001242 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001243 }
1244 }
1245
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001246 float pressureScale;
1247 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1248 out.pressureScale = pressureScale;
1249 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001250
1251 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001252 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001253 std::string orientationCalibrationString;
1254 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001255 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001256 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001257 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001258 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001259 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001260 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001261 } else if (orientationCalibrationString != "default") {
1262 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001263 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001264 }
1265 }
1266
1267 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001268 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001269 std::string distanceCalibrationString;
1270 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001271 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001272 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001273 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001274 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001275 } else if (distanceCalibrationString != "default") {
1276 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001277 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001278 }
1279 }
1280
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001281 float distanceScale;
1282 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1283 out.distanceScale = distanceScale;
1284 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001285
Michael Wright227c5542020-07-02 18:30:52 +01001286 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001287 std::string coverageCalibrationString;
1288 if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001289 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001290 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001291 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001292 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001293 } else if (coverageCalibrationString != "default") {
1294 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001295 coverageCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001296 }
1297 }
1298}
1299
1300void TouchInputMapper::resolveCalibration() {
1301 // Size
1302 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001303 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1304 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001305 }
1306 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001307 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001308 }
1309
1310 // Pressure
1311 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001312 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1313 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001314 }
1315 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001316 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001317 }
1318
1319 // Orientation
1320 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001321 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1322 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001323 }
1324 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001325 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001326 }
1327
1328 // Distance
1329 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001330 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1331 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001332 }
1333 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001334 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001335 }
1336
1337 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001338 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1339 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001340 }
1341}
1342
1343void TouchInputMapper::dumpCalibration(std::string& dump) {
1344 dump += INDENT3 "Calibration:\n";
1345
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001346 dump += INDENT4 "touch.size.calibration: ";
1347 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001348
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001349 if (mCalibration.sizeScale) {
1350 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001351 }
1352
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001353 if (mCalibration.sizeBias) {
1354 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001355 }
1356
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001357 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001358 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001359 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001360 }
1361
1362 // Pressure
1363 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001364 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001365 dump += INDENT4 "touch.pressure.calibration: none\n";
1366 break;
Michael Wright227c5542020-07-02 18:30:52 +01001367 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001368 dump += INDENT4 "touch.pressure.calibration: physical\n";
1369 break;
Michael Wright227c5542020-07-02 18:30:52 +01001370 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001371 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1372 break;
1373 default:
1374 ALOG_ASSERT(false);
1375 }
1376
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001377 if (mCalibration.pressureScale) {
1378 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001379 }
1380
1381 // Orientation
1382 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001383 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001384 dump += INDENT4 "touch.orientation.calibration: none\n";
1385 break;
Michael Wright227c5542020-07-02 18:30:52 +01001386 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001387 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1388 break;
Michael Wright227c5542020-07-02 18:30:52 +01001389 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001390 dump += INDENT4 "touch.orientation.calibration: vector\n";
1391 break;
1392 default:
1393 ALOG_ASSERT(false);
1394 }
1395
1396 // Distance
1397 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001398 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001399 dump += INDENT4 "touch.distance.calibration: none\n";
1400 break;
Michael Wright227c5542020-07-02 18:30:52 +01001401 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001402 dump += INDENT4 "touch.distance.calibration: scaled\n";
1403 break;
1404 default:
1405 ALOG_ASSERT(false);
1406 }
1407
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001408 if (mCalibration.distanceScale) {
1409 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001410 }
1411
1412 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001413 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001414 dump += INDENT4 "touch.coverage.calibration: none\n";
1415 break;
Michael Wright227c5542020-07-02 18:30:52 +01001416 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001417 dump += INDENT4 "touch.coverage.calibration: box\n";
1418 break;
1419 default:
1420 ALOG_ASSERT(false);
1421 }
1422}
1423
1424void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1425 dump += INDENT3 "Affine Transformation:\n";
1426
1427 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1428 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1429 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1430 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1431 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1432 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1433}
1434
1435void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001436 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001437 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001438}
1439
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001440std::list<NotifyArgs> TouchInputMapper::reset(nsecs_t when) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001441 std::list<NotifyArgs> out = cancelTouch(when, when);
1442 updateTouchSpots();
1443
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001444 mCursorButtonAccumulator.reset(getDeviceContext());
1445 mCursorScrollAccumulator.reset(getDeviceContext());
1446 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001447
1448 mPointerVelocityControl.reset();
1449 mWheelXVelocityControl.reset();
1450 mWheelYVelocityControl.reset();
1451
1452 mRawStatesPending.clear();
1453 mCurrentRawState.clear();
1454 mCurrentCookedState.clear();
1455 mLastRawState.clear();
1456 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001457 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001458 mSentHoverEnter = false;
1459 mHavePointerIds = false;
1460 mCurrentMotionAborted = false;
1461 mDownTime = 0;
1462
1463 mCurrentVirtualKey.down = false;
1464
1465 mPointerGesture.reset();
1466 mPointerSimple.reset();
1467 resetExternalStylus();
1468
1469 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001470 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001471 mPointerController->clearSpots();
1472 }
1473
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001474 return out += InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001475}
1476
1477void TouchInputMapper::resetExternalStylus() {
1478 mExternalStylusState.clear();
1479 mExternalStylusId = -1;
1480 mExternalStylusFusionTimeout = LLONG_MAX;
1481 mExternalStylusDataPending = false;
1482}
1483
1484void TouchInputMapper::clearStylusDataPendingFlags() {
1485 mExternalStylusDataPending = false;
1486 mExternalStylusFusionTimeout = LLONG_MAX;
1487}
1488
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001489std::list<NotifyArgs> TouchInputMapper::process(const RawEvent* rawEvent) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001490 mCursorButtonAccumulator.process(rawEvent);
1491 mCursorScrollAccumulator.process(rawEvent);
1492 mTouchButtonAccumulator.process(rawEvent);
1493
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001494 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001495 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001496 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001497 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001498 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001499}
1500
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001501std::list<NotifyArgs> TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
1502 std::list<NotifyArgs> out;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00001503 if (mDeviceMode == DeviceMode::DISABLED) {
1504 // Only save the last pending state when the device is disabled.
1505 mRawStatesPending.clear();
1506 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001507 // Push a new state.
1508 mRawStatesPending.emplace_back();
1509
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001510 RawState& next = mRawStatesPending.back();
1511 next.clear();
1512 next.when = when;
1513 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001514
1515 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001516 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001517 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1518
1519 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001520 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1521 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001522 mCursorScrollAccumulator.finishSync();
1523
1524 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001525 syncTouch(when, &next);
1526
1527 // The last RawState is the actually second to last, since we just added a new state
1528 const RawState& last =
1529 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001530
1531 // Assign pointer ids.
1532 if (!mHavePointerIds) {
1533 assignPointerIds(last, next);
1534 }
1535
Harry Cutts45483602022-08-24 14:36:48 +00001536 ALOGD_IF(DEBUG_RAW_EVENTS,
1537 "syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1538 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1539 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1540 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1541 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1542 next.rawPointerData.canceledIdBits.value);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001543
Arthur Hung9ad18942021-06-19 02:04:46 +00001544 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1545 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1546 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1547 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1548 next.rawPointerData.hoveringIdBits.value);
1549 }
1550
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001551 out += processRawTouches(false /*timeout*/);
1552 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001553}
1554
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001555std::list<NotifyArgs> TouchInputMapper::processRawTouches(bool timeout) {
1556 std::list<NotifyArgs> out;
Michael Wright227c5542020-07-02 18:30:52 +01001557 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001558 // Do not process raw event while the device is disabled.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001559 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001560 }
1561
1562 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1563 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1564 // touching the current state will only observe the events that have been dispatched to the
1565 // rest of the pipeline.
1566 const size_t N = mRawStatesPending.size();
1567 size_t count;
1568 for (count = 0; count < N; count++) {
1569 const RawState& next = mRawStatesPending[count];
1570
1571 // A failure to assign the stylus id means that we're waiting on stylus data
1572 // and so should defer the rest of the pipeline.
1573 if (assignExternalStylusId(next, timeout)) {
1574 break;
1575 }
1576
1577 // All ready to go.
1578 clearStylusDataPendingFlags();
1579 mCurrentRawState.copyFrom(next);
1580 if (mCurrentRawState.when < mLastRawState.when) {
1581 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001582 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001583 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001584 out += cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001585 }
1586 if (count != 0) {
1587 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1588 }
1589
1590 if (mExternalStylusDataPending) {
1591 if (timeout) {
1592 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1593 clearStylusDataPendingFlags();
1594 mCurrentRawState.copyFrom(mLastRawState);
Harry Cutts45483602022-08-24 14:36:48 +00001595 ALOGD_IF(DEBUG_STYLUS_FUSION,
1596 "Timeout expired, synthesizing event with new stylus data");
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001597 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001598 out += cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001599 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1600 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1601 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1602 }
1603 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001604 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001605}
1606
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001607std::list<NotifyArgs> TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
1608 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001609 // Always start with a clean state.
1610 mCurrentCookedState.clear();
1611
1612 // Apply stylus buttons to current raw state.
1613 applyExternalStylusButtonState(when);
1614
1615 // Handle policy on initial down or hover events.
1616 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1617 mCurrentRawState.rawPointerData.pointerCount != 0;
1618
1619 uint32_t policyFlags = 0;
1620 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1621 if (initialDown || buttonsPressed) {
1622 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001623 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001624 getContext()->fadePointer();
1625 }
1626
1627 if (mParameters.wake) {
1628 policyFlags |= POLICY_FLAG_WAKE;
1629 }
1630 }
1631
1632 // Consume raw off-screen touches before cooking pointer data.
1633 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001634 bool consumed;
1635 out += consumeRawTouches(when, readTime, policyFlags, consumed /*byref*/);
1636 if (consumed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001637 mCurrentRawState.rawPointerData.clear();
1638 }
1639
1640 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1641 // with cooked pointer data that has the same ids and indices as the raw data.
1642 // The following code can use either the raw or cooked data, as needed.
1643 cookPointerData();
1644
1645 // Apply stylus pressure to current cooked state.
1646 applyExternalStylusTouchState(when);
1647
1648 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001649 out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1650 mSource, mViewport.displayId, policyFlags,
1651 mLastCookedState.buttonState, mCurrentCookedState.buttonState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001652
1653 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001654 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001655 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1656 uint32_t id = idBits.clearFirstMarkedBit();
1657 const RawPointerData::Pointer& pointer =
1658 mCurrentRawState.rawPointerData.pointerForId(id);
1659 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1660 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1661 mCurrentCookedState.stylusIdBits.markBit(id);
1662 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1663 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1664 mCurrentCookedState.fingerIdBits.markBit(id);
1665 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1666 mCurrentCookedState.mouseIdBits.markBit(id);
1667 }
1668 }
1669 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1670 uint32_t id = idBits.clearFirstMarkedBit();
1671 const RawPointerData::Pointer& pointer =
1672 mCurrentRawState.rawPointerData.pointerForId(id);
1673 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1674 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1675 mCurrentCookedState.stylusIdBits.markBit(id);
1676 }
1677 }
1678
1679 // Stylus takes precedence over all tools, then mouse, then finger.
1680 PointerUsage pointerUsage = mPointerUsage;
1681 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1682 mCurrentCookedState.mouseIdBits.clear();
1683 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001684 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001685 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1686 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001687 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001688 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1689 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001690 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001691 }
1692
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001693 out += dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001694 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001695 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001696 updateTouchSpots();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001697 out += dispatchButtonRelease(when, readTime, policyFlags);
1698 out += dispatchHoverExit(when, readTime, policyFlags);
1699 out += dispatchTouches(when, readTime, policyFlags);
1700 out += dispatchHoverEnterAndMove(when, readTime, policyFlags);
1701 out += dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001702 }
1703
1704 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1705 mCurrentMotionAborted = false;
1706 }
1707 }
1708
1709 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001710 out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(),
1711 mSource, mViewport.displayId, policyFlags,
1712 mLastCookedState.buttonState, mCurrentCookedState.buttonState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001713
1714 // Clear some transient state.
1715 mCurrentRawState.rawVScroll = 0;
1716 mCurrentRawState.rawHScroll = 0;
1717
1718 // Copy current touch to last touch in preparation for the next cycle.
1719 mLastRawState.copyFrom(mCurrentRawState);
1720 mLastCookedState.copyFrom(mCurrentCookedState);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001721 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001722}
1723
Garfield Tanc734e4f2021-01-15 20:01:39 -08001724void TouchInputMapper::updateTouchSpots() {
1725 if (!mConfig.showTouches || mPointerController == nullptr) {
1726 return;
1727 }
1728
1729 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1730 // clear touch spots.
1731 if (mDeviceMode != DeviceMode::DIRECT &&
1732 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1733 return;
1734 }
1735
1736 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1737 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1738
1739 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001740 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1741 mCurrentCookedState.cookedPointerData.idToIndex,
1742 mCurrentCookedState.cookedPointerData.touchingIdBits,
1743 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001744}
1745
1746bool TouchInputMapper::isTouchScreen() {
1747 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1748 mParameters.hasAssociatedDisplay;
1749}
1750
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001751void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001752 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001753 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1754 }
1755}
1756
1757void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1758 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1759 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1760
1761 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1762 float pressure = mExternalStylusState.pressure;
1763 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1764 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1765 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1766 }
1767 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1768 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1769
1770 PointerProperties& properties =
1771 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1772 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1773 properties.toolType = mExternalStylusState.toolType;
1774 }
1775 }
1776}
1777
1778bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001779 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001780 return false;
1781 }
1782
1783 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1784 state.rawPointerData.pointerCount != 0;
1785 if (initialDown) {
1786 if (mExternalStylusState.pressure != 0.0f) {
Harry Cutts45483602022-08-24 14:36:48 +00001787 ALOGD_IF(DEBUG_STYLUS_FUSION, "Have both stylus and touch data, beginning fusion");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001788 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1789 } else if (timeout) {
Harry Cutts45483602022-08-24 14:36:48 +00001790 ALOGD_IF(DEBUG_STYLUS_FUSION, "Timeout expired, assuming touch is not a stylus.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001791 resetExternalStylus();
1792 } else {
1793 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1794 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1795 }
Harry Cutts45483602022-08-24 14:36:48 +00001796 ALOGD_IF(DEBUG_STYLUS_FUSION,
1797 "No stylus data but stylus is connected, requesting timeout (%" PRId64 "ms)",
1798 mExternalStylusFusionTimeout);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001799 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1800 return true;
1801 }
1802 }
1803
1804 // Check if the stylus pointer has gone up.
1805 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Harry Cutts45483602022-08-24 14:36:48 +00001806 ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus pointer is going up");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001807 mExternalStylusId = -1;
1808 }
1809
1810 return false;
1811}
1812
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001813std::list<NotifyArgs> TouchInputMapper::timeoutExpired(nsecs_t when) {
1814 std::list<NotifyArgs> out;
Michael Wright227c5542020-07-02 18:30:52 +01001815 if (mDeviceMode == DeviceMode::POINTER) {
1816 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001817 // Since this is a synthetic event, we can consider its latency to be zero
1818 const nsecs_t readTime = when;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001819 out += dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001820 }
Michael Wright227c5542020-07-02 18:30:52 +01001821 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001822 if (mExternalStylusFusionTimeout < when) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001823 out += processRawTouches(true /*timeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001824 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1825 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1826 }
1827 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001828 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001829}
1830
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001831std::list<NotifyArgs> TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1832 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001833 mExternalStylusState.copyFrom(state);
1834 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1835 // We're either in the middle of a fused stream of data or we're waiting on data before
1836 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1837 // data.
1838 mExternalStylusDataPending = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001839 out += processRawTouches(false /*timeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001840 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001841 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001842}
1843
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001844std::list<NotifyArgs> TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime,
1845 uint32_t policyFlags, bool& outConsumed) {
1846 outConsumed = false;
1847 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001848 // Check for release of a virtual key.
1849 if (mCurrentVirtualKey.down) {
1850 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1851 // Pointer went up while virtual key was down.
1852 mCurrentVirtualKey.down = false;
1853 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001854 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1855 "VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1856 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001857 out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
1858 AKEY_EVENT_FLAG_FROM_SYSTEM |
1859 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001860 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001861 outConsumed = true;
1862 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001863 }
1864
1865 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1866 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1867 const RawPointerData::Pointer& pointer =
1868 mCurrentRawState.rawPointerData.pointerForId(id);
1869 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1870 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1871 // Pointer is still within the space of the virtual key.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001872 outConsumed = true;
1873 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001874 }
1875 }
1876
1877 // Pointer left virtual key area or another pointer also went down.
1878 // Send key cancellation but do not consume the touch yet.
1879 // This is useful when the user swipes through from the virtual key area
1880 // into the main display surface.
1881 mCurrentVirtualKey.down = false;
1882 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001883 ALOGD_IF(DEBUG_VIRTUAL_KEYS, "VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1884 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001885 out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
1886 AKEY_EVENT_FLAG_FROM_SYSTEM |
1887 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1888 AKEY_EVENT_FLAG_CANCELED));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001889 }
1890 }
1891
1892 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1893 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1894 // Pointer just went down. Check for virtual key press or off-screen touches.
1895 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1896 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001897 // Skip checking whether the pointer is inside the physical frame if the device is in
1898 // unscaled mode.
1899 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1900 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001901 // If exactly one pointer went down, check for virtual key hit.
1902 // Otherwise we will drop the entire stroke.
1903 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1904 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1905 if (virtualKey) {
1906 mCurrentVirtualKey.down = true;
1907 mCurrentVirtualKey.downTime = when;
1908 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1909 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1910 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001911 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1912 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001913
1914 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001915 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1916 "VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1917 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001918 out.push_back(dispatchVirtualKey(when, readTime, policyFlags,
1919 AKEY_EVENT_ACTION_DOWN,
1920 AKEY_EVENT_FLAG_FROM_SYSTEM |
1921 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001922 }
1923 }
1924 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001925 outConsumed = true;
1926 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001927 }
1928 }
1929
1930 // Disable all virtual key touches that happen within a short time interval of the
1931 // most recent touch within the screen area. The idea is to filter out stray
1932 // virtual key presses when interacting with the touch screen.
1933 //
1934 // Problems we're trying to solve:
1935 //
1936 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1937 // virtual key area that is implemented by a separate touch panel and accidentally
1938 // triggers a virtual key.
1939 //
1940 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1941 // area and accidentally triggers a virtual key. This often happens when virtual keys
1942 // are layed out below the screen near to where the on screen keyboard's space bar
1943 // is displayed.
1944 if (mConfig.virtualKeyQuietTime > 0 &&
1945 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001946 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001947 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001948 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001949}
1950
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001951NotifyKeyArgs TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
1952 uint32_t policyFlags, int32_t keyEventAction,
1953 int32_t keyEventFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001954 int32_t keyCode = mCurrentVirtualKey.keyCode;
1955 int32_t scanCode = mCurrentVirtualKey.scanCode;
1956 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001957 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001958 policyFlags |= POLICY_FLAG_VIRTUAL;
1959
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001960 return NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
1961 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1962 keyEventFlags, keyCode, scanCode, metaState, downTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001963}
1964
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001965std::list<NotifyArgs> TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime,
1966 uint32_t policyFlags) {
1967 std::list<NotifyArgs> out;
lilinnan687e58f2022-07-19 16:00:50 +08001968 if (mCurrentMotionAborted) {
1969 // Current motion event was already aborted.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001970 return out;
lilinnan687e58f2022-07-19 16:00:50 +08001971 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001972 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1973 if (!currentIdBits.isEmpty()) {
1974 int32_t metaState = getContext()->getGlobalMetaState();
1975 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001976 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001977 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
1978 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001979 mCurrentCookedState.cookedPointerData.pointerProperties,
1980 mCurrentCookedState.cookedPointerData.pointerCoords,
1981 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits,
1982 -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1983 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001984 mCurrentMotionAborted = true;
1985 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001986 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001987}
1988
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001989std::list<NotifyArgs> TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime,
1990 uint32_t policyFlags) {
1991 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001992 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1993 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1994 int32_t metaState = getContext()->getGlobalMetaState();
1995 int32_t buttonState = mCurrentCookedState.buttonState;
1996
1997 if (currentIdBits == lastIdBits) {
1998 if (!currentIdBits.isEmpty()) {
1999 // No pointer id changes so this is a move event.
2000 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002001 out.push_back(
2002 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE,
2003 0, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2004 mCurrentCookedState.cookedPointerData.pointerProperties,
2005 mCurrentCookedState.cookedPointerData.pointerCoords,
2006 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits,
2007 -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2008 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002009 }
2010 } else {
2011 // There may be pointers going up and pointers going down and pointers moving
2012 // all at the same time.
2013 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
2014 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
2015 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2016 BitSet32 dispatchedIdBits(lastIdBits.value);
2017
2018 // Update last coordinates of pointers that have moved so that we observe the new
2019 // pointer positions at the same time as other pointers that have just gone up.
2020 bool moveNeeded =
2021 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
2022 mCurrentCookedState.cookedPointerData.pointerCoords,
2023 mCurrentCookedState.cookedPointerData.idToIndex,
2024 mLastCookedState.cookedPointerData.pointerProperties,
2025 mLastCookedState.cookedPointerData.pointerCoords,
2026 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
2027 if (buttonState != mLastCookedState.buttonState) {
2028 moveNeeded = true;
2029 }
2030
2031 // Dispatch pointer up events.
2032 while (!upIdBits.isEmpty()) {
2033 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002034 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002035 if (isCanceled) {
2036 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2037 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002038 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2039 AMOTION_EVENT_ACTION_POINTER_UP, 0,
2040 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState,
2041 buttonState, 0,
2042 mLastCookedState.cookedPointerData.pointerProperties,
2043 mLastCookedState.cookedPointerData.pointerCoords,
2044 mLastCookedState.cookedPointerData.idToIndex,
2045 dispatchedIdBits, upId, mOrientedXPrecision,
2046 mOrientedYPrecision, mDownTime,
2047 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002048 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002049 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002050 }
2051
2052 // Dispatch move events if any of the remaining pointers moved from their old locations.
2053 // Although applications receive new locations as part of individual pointer up
2054 // events, they do not generally handle them except when presented in a move event.
2055 if (moveNeeded && !moveIdBits.isEmpty()) {
2056 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002057 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2058 AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, 0,
2059 mCurrentCookedState.cookedPointerData.pointerProperties,
2060 mCurrentCookedState.cookedPointerData.pointerCoords,
2061 mCurrentCookedState.cookedPointerData.idToIndex,
2062 dispatchedIdBits, -1, mOrientedXPrecision,
2063 mOrientedYPrecision, mDownTime,
2064 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002065 }
2066
2067 // Dispatch pointer down events using the new pointer locations.
2068 while (!downIdBits.isEmpty()) {
2069 uint32_t downId = downIdBits.clearFirstMarkedBit();
2070 dispatchedIdBits.markBit(downId);
2071
2072 if (dispatchedIdBits.count() == 1) {
2073 // First pointer is going down. Set down time.
2074 mDownTime = when;
2075 }
2076
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002077 out.push_back(
2078 dispatchMotion(when, readTime, policyFlags, mSource,
2079 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, metaState, buttonState,
2080 0, mCurrentCookedState.cookedPointerData.pointerProperties,
2081 mCurrentCookedState.cookedPointerData.pointerCoords,
2082 mCurrentCookedState.cookedPointerData.idToIndex,
2083 dispatchedIdBits, downId, mOrientedXPrecision,
2084 mOrientedYPrecision, mDownTime, MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002085 }
2086 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002087 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002088}
2089
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002090std::list<NotifyArgs> TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime,
2091 uint32_t policyFlags) {
2092 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002093 if (mSentHoverEnter &&
2094 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2095 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2096 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002097 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2098 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState,
2099 mLastCookedState.buttonState, 0,
2100 mLastCookedState.cookedPointerData.pointerProperties,
2101 mLastCookedState.cookedPointerData.pointerCoords,
2102 mLastCookedState.cookedPointerData.idToIndex,
2103 mLastCookedState.cookedPointerData.hoveringIdBits, -1,
2104 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2105 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002106 mSentHoverEnter = false;
2107 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002108 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002109}
2110
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002111std::list<NotifyArgs> TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2112 uint32_t policyFlags) {
2113 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002114 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2115 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2116 int32_t metaState = getContext()->getGlobalMetaState();
2117 if (!mSentHoverEnter) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002118 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2119 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
2120 mCurrentRawState.buttonState, 0,
2121 mCurrentCookedState.cookedPointerData.pointerProperties,
2122 mCurrentCookedState.cookedPointerData.pointerCoords,
2123 mCurrentCookedState.cookedPointerData.idToIndex,
2124 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2125 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2126 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002127 mSentHoverEnter = true;
2128 }
2129
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002130 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2131 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState,
2132 mCurrentRawState.buttonState, 0,
2133 mCurrentCookedState.cookedPointerData.pointerProperties,
2134 mCurrentCookedState.cookedPointerData.pointerCoords,
2135 mCurrentCookedState.cookedPointerData.idToIndex,
2136 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2137 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2138 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002139 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002140 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002141}
2142
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002143std::list<NotifyArgs> TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
2144 uint32_t policyFlags) {
2145 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002146 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2147 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2148 const int32_t metaState = getContext()->getGlobalMetaState();
2149 int32_t buttonState = mLastCookedState.buttonState;
2150 while (!releasedButtons.isEmpty()) {
2151 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2152 buttonState &= ~actionButton;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002153 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2154 AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0,
2155 metaState, buttonState, 0,
2156 mCurrentCookedState.cookedPointerData.pointerProperties,
2157 mCurrentCookedState.cookedPointerData.pointerCoords,
2158 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2159 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2160 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002161 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002162 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002163}
2164
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002165std::list<NotifyArgs> TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime,
2166 uint32_t policyFlags) {
2167 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002168 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2169 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2170 const int32_t metaState = getContext()->getGlobalMetaState();
2171 int32_t buttonState = mLastCookedState.buttonState;
2172 while (!pressedButtons.isEmpty()) {
2173 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2174 buttonState |= actionButton;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002175 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2176 AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0, metaState,
2177 buttonState, 0,
2178 mCurrentCookedState.cookedPointerData.pointerProperties,
2179 mCurrentCookedState.cookedPointerData.pointerCoords,
2180 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2181 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2182 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002183 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002184 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002185}
2186
2187const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2188 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2189 return cookedPointerData.touchingIdBits;
2190 }
2191 return cookedPointerData.hoveringIdBits;
2192}
2193
2194void TouchInputMapper::cookPointerData() {
2195 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2196
2197 mCurrentCookedState.cookedPointerData.clear();
2198 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2199 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2200 mCurrentRawState.rawPointerData.hoveringIdBits;
2201 mCurrentCookedState.cookedPointerData.touchingIdBits =
2202 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002203 mCurrentCookedState.cookedPointerData.canceledIdBits =
2204 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002205
2206 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2207 mCurrentCookedState.buttonState = 0;
2208 } else {
2209 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2210 }
2211
2212 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002213 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002214 for (uint32_t i = 0; i < currentPointerCount; i++) {
2215 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2216
2217 // Size
2218 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2219 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002220 case Calibration::SizeCalibration::GEOMETRIC:
2221 case Calibration::SizeCalibration::DIAMETER:
2222 case Calibration::SizeCalibration::BOX:
2223 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002224 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2225 touchMajor = in.touchMajor;
2226 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2227 toolMajor = in.toolMajor;
2228 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2229 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2230 : in.touchMajor;
2231 } else if (mRawPointerAxes.touchMajor.valid) {
2232 toolMajor = touchMajor = in.touchMajor;
2233 toolMinor = touchMinor =
2234 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2235 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2236 : in.touchMajor;
2237 } else if (mRawPointerAxes.toolMajor.valid) {
2238 touchMajor = toolMajor = in.toolMajor;
2239 touchMinor = toolMinor =
2240 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2241 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2242 : in.toolMajor;
2243 } else {
2244 ALOG_ASSERT(false,
2245 "No touch or tool axes. "
2246 "Size calibration should have been resolved to NONE.");
2247 touchMajor = 0;
2248 touchMinor = 0;
2249 toolMajor = 0;
2250 toolMinor = 0;
2251 size = 0;
2252 }
2253
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002254 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002255 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2256 if (touchingCount > 1) {
2257 touchMajor /= touchingCount;
2258 touchMinor /= touchingCount;
2259 toolMajor /= touchingCount;
2260 toolMinor /= touchingCount;
2261 size /= touchingCount;
2262 }
2263 }
2264
Michael Wright227c5542020-07-02 18:30:52 +01002265 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002266 touchMajor *= mGeometricScale;
2267 touchMinor *= mGeometricScale;
2268 toolMajor *= mGeometricScale;
2269 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002270 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002271 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2272 touchMinor = touchMajor;
2273 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2274 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002275 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002276 touchMinor = touchMajor;
2277 toolMinor = toolMajor;
2278 }
2279
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002280 mCalibration.applySizeScaleAndBias(touchMajor);
2281 mCalibration.applySizeScaleAndBias(touchMinor);
2282 mCalibration.applySizeScaleAndBias(toolMajor);
2283 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002284 size *= mSizeScale;
2285 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002286 case Calibration::SizeCalibration::DEFAULT:
2287 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2288 break;
2289 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002290 touchMajor = 0;
2291 touchMinor = 0;
2292 toolMajor = 0;
2293 toolMinor = 0;
2294 size = 0;
2295 break;
2296 }
2297
2298 // Pressure
2299 float pressure;
2300 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002301 case Calibration::PressureCalibration::PHYSICAL:
2302 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002303 pressure = in.pressure * mPressureScale;
2304 break;
2305 default:
2306 pressure = in.isHovering ? 0 : 1;
2307 break;
2308 }
2309
2310 // Tilt and Orientation
2311 float tilt;
2312 float orientation;
2313 if (mHaveTilt) {
2314 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2315 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2316 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2317 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2318 } else {
2319 tilt = 0;
2320
2321 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002322 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002323 orientation = in.orientation * mOrientationScale;
2324 break;
Michael Wright227c5542020-07-02 18:30:52 +01002325 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002326 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2327 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2328 if (c1 != 0 || c2 != 0) {
2329 orientation = atan2f(c1, c2) * 0.5f;
2330 float confidence = hypotf(c1, c2);
2331 float scale = 1.0f + confidence / 16.0f;
2332 touchMajor *= scale;
2333 touchMinor /= scale;
2334 toolMajor *= scale;
2335 toolMinor /= scale;
2336 } else {
2337 orientation = 0;
2338 }
2339 break;
2340 }
2341 default:
2342 orientation = 0;
2343 }
2344 }
2345
2346 // Distance
2347 float distance;
2348 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002349 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002350 distance = in.distance * mDistanceScale;
2351 break;
2352 default:
2353 distance = 0;
2354 }
2355
2356 // Coverage
2357 int32_t rawLeft, rawTop, rawRight, rawBottom;
2358 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002359 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002360 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2361 rawRight = in.toolMinor & 0x0000ffff;
2362 rawBottom = in.toolMajor & 0x0000ffff;
2363 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2364 break;
2365 default:
2366 rawLeft = rawTop = rawRight = rawBottom = 0;
2367 break;
2368 }
2369
2370 // Adjust X,Y coords for device calibration
2371 // TODO: Adjust coverage coords?
2372 float xTransformed = in.x, yTransformed = in.y;
2373 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002374 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002375
Prabir Pradhan1728b212021-10-19 16:00:03 -07002376 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002377 float left, top, right, bottom;
2378
Prabir Pradhan1728b212021-10-19 16:00:03 -07002379 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002380 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002381 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2382 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2383 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2384 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002385 orientation -= M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002386 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002387 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002388 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002389 }
2390 break;
2391 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002392 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2393 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002394 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2395 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002396 orientation -= M_PI;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002397 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002398 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002399 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002400 }
2401 break;
2402 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002403 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2404 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002405 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2406 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002407 orientation += M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002408 if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002409 orientation -=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002410 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002411 }
2412 break;
2413 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002414 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2415 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2416 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2417 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002418 break;
2419 }
2420
2421 // Write output coords.
2422 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2423 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002424 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2425 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002426 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2427 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2428 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2429 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2430 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2431 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2432 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002433 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002434 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2435 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2436 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2437 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2438 } else {
2439 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2440 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2441 }
2442
Chris Ye364fdb52020-08-05 15:07:56 -07002443 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002444 uint32_t id = in.id;
2445 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2446 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2447 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2448 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2449 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2450 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2451 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2452 }
2453
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002454 // Write output properties.
2455 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002456 properties.clear();
2457 properties.id = id;
2458 properties.toolType = in.toolType;
2459
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002460 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002461 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002462 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002463 }
2464}
2465
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002466std::list<NotifyArgs> TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
2467 uint32_t policyFlags,
2468 PointerUsage pointerUsage) {
2469 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002470 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002471 out += abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002472 mPointerUsage = pointerUsage;
2473 }
2474
2475 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002476 case PointerUsage::GESTURES:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002477 out += dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002478 break;
Michael Wright227c5542020-07-02 18:30:52 +01002479 case PointerUsage::STYLUS:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002480 out += dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002481 break;
Michael Wright227c5542020-07-02 18:30:52 +01002482 case PointerUsage::MOUSE:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002483 out += dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002484 break;
Michael Wright227c5542020-07-02 18:30:52 +01002485 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002486 break;
2487 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002488 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002489}
2490
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002491std::list<NotifyArgs> TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime,
2492 uint32_t policyFlags) {
2493 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002494 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002495 case PointerUsage::GESTURES:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002496 out += abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002497 break;
Michael Wright227c5542020-07-02 18:30:52 +01002498 case PointerUsage::STYLUS:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002499 out += abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002500 break;
Michael Wright227c5542020-07-02 18:30:52 +01002501 case PointerUsage::MOUSE:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002502 out += abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002503 break;
Michael Wright227c5542020-07-02 18:30:52 +01002504 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002505 break;
2506 }
2507
Michael Wright227c5542020-07-02 18:30:52 +01002508 mPointerUsage = PointerUsage::NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002509 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002510}
2511
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002512std::list<NotifyArgs> TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
2513 uint32_t policyFlags,
2514 bool isTimeout) {
2515 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002516 // Update current gesture coordinates.
2517 bool cancelPreviousGesture, finishPreviousGesture;
2518 bool sendEvents =
2519 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2520 if (!sendEvents) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002521 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002522 }
2523 if (finishPreviousGesture) {
2524 cancelPreviousGesture = false;
2525 }
2526
2527 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002528 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002529 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002530 if (finishPreviousGesture || cancelPreviousGesture) {
2531 mPointerController->clearSpots();
2532 }
2533
Michael Wright227c5542020-07-02 18:30:52 +01002534 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002535 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2536 mPointerGesture.currentGestureIdToIndex,
2537 mPointerGesture.currentGestureIdBits,
2538 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002539 }
2540 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002541 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002542 }
2543
2544 // Show or hide the pointer if needed.
2545 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002546 case PointerGesture::Mode::NEUTRAL:
2547 case PointerGesture::Mode::QUIET:
2548 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2549 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002550 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002551 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002552 }
2553 break;
Michael Wright227c5542020-07-02 18:30:52 +01002554 case PointerGesture::Mode::TAP:
2555 case PointerGesture::Mode::TAP_DRAG:
2556 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2557 case PointerGesture::Mode::HOVER:
2558 case PointerGesture::Mode::PRESS:
2559 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002560 // Unfade the pointer when the current gesture manipulates the
2561 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002562 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002563 break;
Michael Wright227c5542020-07-02 18:30:52 +01002564 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002565 // Fade the pointer when the current gesture manipulates a different
2566 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002567 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002568 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002569 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002570 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002571 }
2572 break;
2573 }
2574
2575 // Send events!
2576 int32_t metaState = getContext()->getGlobalMetaState();
2577 int32_t buttonState = mCurrentCookedState.buttonState;
Harry Cutts2800fb02022-09-15 13:49:23 +00002578 const MotionClassification classification =
2579 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE
2580 ? MotionClassification::TWO_FINGER_SWIPE
2581 : MotionClassification::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002582
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002583 uint32_t flags = 0;
2584
2585 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2586 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2587 }
2588
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002589 // Update last coordinates of pointers that have moved so that we observe the new
2590 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002591 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2592 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2593 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2594 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2595 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2596 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002597 bool moveNeeded = false;
2598 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2599 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2600 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2601 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2602 mPointerGesture.lastGestureIdBits.value);
2603 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2604 mPointerGesture.currentGestureCoords,
2605 mPointerGesture.currentGestureIdToIndex,
2606 mPointerGesture.lastGestureProperties,
2607 mPointerGesture.lastGestureCoords,
2608 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2609 if (buttonState != mLastCookedState.buttonState) {
2610 moveNeeded = true;
2611 }
2612 }
2613
2614 // Send motion events for all pointers that went up or were canceled.
2615 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2616 if (!dispatchedGestureIdBits.isEmpty()) {
2617 if (cancelPreviousGesture) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002618 const uint32_t cancelFlags = flags | AMOTION_EVENT_FLAG_CANCELED;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002619 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002620 AMOTION_EVENT_ACTION_CANCEL, 0, cancelFlags, metaState,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002621 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2622 mPointerGesture.lastGestureProperties,
2623 mPointerGesture.lastGestureCoords,
2624 mPointerGesture.lastGestureIdToIndex,
2625 dispatchedGestureIdBits, -1, 0, 0,
2626 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002627
2628 dispatchedGestureIdBits.clear();
2629 } else {
2630 BitSet32 upGestureIdBits;
2631 if (finishPreviousGesture) {
2632 upGestureIdBits = dispatchedGestureIdBits;
2633 } else {
2634 upGestureIdBits.value =
2635 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2636 }
2637 while (!upGestureIdBits.isEmpty()) {
2638 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2639
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002640 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2641 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState,
2642 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2643 mPointerGesture.lastGestureProperties,
2644 mPointerGesture.lastGestureCoords,
2645 mPointerGesture.lastGestureIdToIndex,
2646 dispatchedGestureIdBits, id, 0, 0,
2647 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002648
2649 dispatchedGestureIdBits.clearBit(id);
2650 }
2651 }
2652 }
2653
2654 // Send motion events for all pointers that moved.
2655 if (moveNeeded) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002656 out.push_back(
2657 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0,
2658 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2659 mPointerGesture.currentGestureProperties,
2660 mPointerGesture.currentGestureCoords,
2661 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1,
2662 0, 0, mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002663 }
2664
2665 // Send motion events for all pointers that went down.
2666 if (down) {
2667 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2668 ~dispatchedGestureIdBits.value);
2669 while (!downGestureIdBits.isEmpty()) {
2670 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2671 dispatchedGestureIdBits.markBit(id);
2672
2673 if (dispatchedGestureIdBits.count() == 1) {
2674 mPointerGesture.downTime = when;
2675 }
2676
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002677 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2678 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, flags, metaState,
2679 buttonState, 0, mPointerGesture.currentGestureProperties,
2680 mPointerGesture.currentGestureCoords,
2681 mPointerGesture.currentGestureIdToIndex,
2682 dispatchedGestureIdBits, id, 0, 0,
2683 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002684 }
2685 }
2686
2687 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002688 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002689 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2690 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState,
2691 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2692 mPointerGesture.currentGestureProperties,
2693 mPointerGesture.currentGestureCoords,
2694 mPointerGesture.currentGestureIdToIndex,
2695 mPointerGesture.currentGestureIdBits, -1, 0, 0,
2696 mPointerGesture.downTime, MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002697 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2698 // Synthesize a hover move event after all pointers go up to indicate that
2699 // the pointer is hovering again even if the user is not currently touching
2700 // the touch pad. This ensures that a view will receive a fresh hover enter
2701 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002702 float x, y;
2703 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002704
2705 PointerProperties pointerProperties;
2706 pointerProperties.clear();
2707 pointerProperties.id = 0;
2708 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2709
2710 PointerCoords pointerCoords;
2711 pointerCoords.clear();
2712 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2713 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2714
2715 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002716 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
2717 mSource, displayId, policyFlags,
2718 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState,
2719 buttonState, MotionClassification::NONE,
2720 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
2721 &pointerCoords, 0, 0, x, y, mPointerGesture.downTime,
2722 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002723 }
2724
2725 // Update state.
2726 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2727 if (!down) {
2728 mPointerGesture.lastGestureIdBits.clear();
2729 } else {
2730 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2731 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2732 uint32_t id = idBits.clearFirstMarkedBit();
2733 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2734 mPointerGesture.lastGestureProperties[index].copyFrom(
2735 mPointerGesture.currentGestureProperties[index]);
2736 mPointerGesture.lastGestureCoords[index].copyFrom(
2737 mPointerGesture.currentGestureCoords[index]);
2738 mPointerGesture.lastGestureIdToIndex[id] = index;
2739 }
2740 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002741 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002742}
2743
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002744std::list<NotifyArgs> TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime,
2745 uint32_t policyFlags) {
Harry Cutts2800fb02022-09-15 13:49:23 +00002746 const MotionClassification classification =
2747 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE
2748 ? MotionClassification::TWO_FINGER_SWIPE
2749 : MotionClassification::NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002750 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002751 // Cancel previously dispatches pointers.
2752 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2753 int32_t metaState = getContext()->getGlobalMetaState();
2754 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002755 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002756 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
2757 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002758 mPointerGesture.lastGestureProperties,
2759 mPointerGesture.lastGestureCoords,
2760 mPointerGesture.lastGestureIdToIndex,
2761 mPointerGesture.lastGestureIdBits, -1, 0, 0,
2762 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002763 }
2764
2765 // Reset the current pointer gesture.
2766 mPointerGesture.reset();
2767 mPointerVelocityControl.reset();
2768
2769 // Remove any current spots.
2770 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002771 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002772 mPointerController->clearSpots();
2773 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002774 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002775}
2776
2777bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2778 bool* outFinishPreviousGesture, bool isTimeout) {
2779 *outCancelPreviousGesture = false;
2780 *outFinishPreviousGesture = false;
2781
2782 // Handle TAP timeout.
2783 if (isTimeout) {
Harry Cutts45483602022-08-24 14:36:48 +00002784 ALOGD_IF(DEBUG_GESTURES, "Gestures: Processing timeout");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002785
Michael Wright227c5542020-07-02 18:30:52 +01002786 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002787 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2788 // The tap/drag timeout has not yet expired.
2789 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2790 mConfig.pointerGestureTapDragInterval);
2791 } else {
2792 // The tap is finished.
Harry Cutts45483602022-08-24 14:36:48 +00002793 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP finished");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002794 *outFinishPreviousGesture = true;
2795
2796 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002797 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002798 mPointerGesture.currentGestureIdBits.clear();
2799
2800 mPointerVelocityControl.reset();
2801 return true;
2802 }
2803 }
2804
2805 // We did not handle this timeout.
2806 return false;
2807 }
2808
2809 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2810 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2811
2812 // Update the velocity tracker.
2813 {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002814 std::vector<float> positionsX;
2815 std::vector<float> positionsY;
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002816 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002817 uint32_t id = idBits.clearFirstMarkedBit();
2818 const RawPointerData::Pointer& pointer =
2819 mCurrentRawState.rawPointerData.pointerForId(id);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002820 positionsX.push_back(pointer.x * mPointerXMovementScale);
2821 positionsY.push_back(pointer.y * mPointerYMovementScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002822 }
2823 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002824 {{AMOTION_EVENT_AXIS_X, positionsX},
2825 {AMOTION_EVENT_AXIS_Y, positionsY}});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002826 }
2827
2828 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2829 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002830 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2831 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2832 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002833 mPointerGesture.resetTap();
2834 }
2835
2836 // Pick a new active touch id if needed.
2837 // Choose an arbitrary pointer that just went down, if there is one.
2838 // Otherwise choose an arbitrary remaining pointer.
2839 // This guarantees we always have an active touch id when there is at least one pointer.
2840 // We keep the same active touch id for as long as possible.
2841 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2842 int32_t activeTouchId = lastActiveTouchId;
2843 if (activeTouchId < 0) {
2844 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2845 activeTouchId = mPointerGesture.activeTouchId =
2846 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2847 mPointerGesture.firstTouchTime = when;
2848 }
2849 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2850 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2851 activeTouchId = mPointerGesture.activeTouchId =
2852 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2853 } else {
2854 activeTouchId = mPointerGesture.activeTouchId = -1;
2855 }
2856 }
2857
2858 // Determine whether we are in quiet time.
2859 bool isQuietTime = false;
2860 if (activeTouchId < 0) {
2861 mPointerGesture.resetQuietTime();
2862 } else {
2863 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2864 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002865 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2866 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2867 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002868 currentFingerCount < 2) {
2869 // Enter quiet time when exiting swipe or freeform state.
2870 // This is to prevent accidentally entering the hover state and flinging the
2871 // pointer when finishing a swipe and there is still one pointer left onscreen.
2872 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002873 } else if (mPointerGesture.lastGestureMode ==
2874 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002875 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2876 // Enter quiet time when releasing the button and there are still two or more
2877 // fingers down. This may indicate that one finger was used to press the button
2878 // but it has not gone up yet.
2879 isQuietTime = true;
2880 }
2881 if (isQuietTime) {
2882 mPointerGesture.quietTime = when;
2883 }
2884 }
2885 }
2886
2887 // Switch states based on button and pointer state.
2888 if (isQuietTime) {
2889 // Case 1: Quiet time. (QUIET)
Harry Cutts45483602022-08-24 14:36:48 +00002890 ALOGD_IF(DEBUG_GESTURES, "Gestures: QUIET for next %0.3fms",
2891 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2892 0.000001f);
Michael Wright227c5542020-07-02 18:30:52 +01002893 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002894 *outFinishPreviousGesture = true;
2895 }
2896
2897 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002898 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002899 mPointerGesture.currentGestureIdBits.clear();
2900
2901 mPointerVelocityControl.reset();
2902 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2903 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2904 // The pointer follows the active touch point.
2905 // Emit DOWN, MOVE, UP events at the pointer location.
2906 //
2907 // Only the active touch matters; other fingers are ignored. This policy helps
2908 // to handle the case where the user places a second finger on the touch pad
2909 // to apply the necessary force to depress an integrated button below the surface.
2910 // We don't want the second finger to be delivered to applications.
2911 //
2912 // For this to work well, we need to make sure to track the pointer that is really
2913 // active. If the user first puts one finger down to click then adds another
2914 // finger to drag then the active pointer should switch to the finger that is
2915 // being dragged.
Harry Cutts45483602022-08-24 14:36:48 +00002916 ALOGD_IF(DEBUG_GESTURES,
2917 "Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, currentFingerCount=%d",
2918 activeTouchId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002919 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002920 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002921 *outFinishPreviousGesture = true;
2922 mPointerGesture.activeGestureId = 0;
2923 }
2924
2925 // Switch pointers if needed.
2926 // Find the fastest pointer and follow it.
2927 if (activeTouchId >= 0 && currentFingerCount > 1) {
2928 int32_t bestId = -1;
2929 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2930 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2931 uint32_t id = idBits.clearFirstMarkedBit();
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00002932 std::optional<float> vx =
2933 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, id);
2934 std::optional<float> vy =
2935 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, id);
2936 if (vx && vy) {
2937 float speed = hypotf(*vx, *vy);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002938 if (speed > bestSpeed) {
2939 bestId = id;
2940 bestSpeed = speed;
2941 }
2942 }
2943 }
2944 if (bestId >= 0 && bestId != activeTouchId) {
2945 mPointerGesture.activeTouchId = activeTouchId = bestId;
Harry Cutts45483602022-08-24 14:36:48 +00002946 ALOGD_IF(DEBUG_GESTURES,
2947 "Gestures: BUTTON_CLICK_OR_DRAG switched pointers, bestId=%d, "
2948 "bestSpeed=%0.3f",
2949 bestId, bestSpeed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002950 }
2951 }
2952
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002953 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002954 // When using spots, the click will occur at the position of the anchor
2955 // spot and all other spots will move there.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002956 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002957 } else {
2958 mPointerVelocityControl.reset();
2959 }
2960
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002961 float x, y;
2962 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002963
Michael Wright227c5542020-07-02 18:30:52 +01002964 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002965 mPointerGesture.currentGestureIdBits.clear();
2966 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2967 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2968 mPointerGesture.currentGestureProperties[0].clear();
2969 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2970 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2971 mPointerGesture.currentGestureCoords[0].clear();
2972 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2973 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2974 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2975 } else if (currentFingerCount == 0) {
2976 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002977 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002978 *outFinishPreviousGesture = true;
2979 }
2980
2981 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2982 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2983 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002984 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2985 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002986 lastFingerCount == 1) {
2987 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002988 float x, y;
2989 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002990 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2991 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Harry Cutts45483602022-08-24 14:36:48 +00002992 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002993
2994 mPointerGesture.tapUpTime = when;
2995 getContext()->requestTimeoutAtTime(when +
2996 mConfig.pointerGestureTapDragInterval);
2997
2998 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002999 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003000 mPointerGesture.currentGestureIdBits.clear();
3001 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3002 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3003 mPointerGesture.currentGestureProperties[0].clear();
3004 mPointerGesture.currentGestureProperties[0].id =
3005 mPointerGesture.activeGestureId;
3006 mPointerGesture.currentGestureProperties[0].toolType =
3007 AMOTION_EVENT_TOOL_TYPE_FINGER;
3008 mPointerGesture.currentGestureCoords[0].clear();
3009 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3010 mPointerGesture.tapX);
3011 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3012 mPointerGesture.tapY);
3013 mPointerGesture.currentGestureCoords[0]
3014 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3015
3016 tapped = true;
3017 } else {
Harry Cutts45483602022-08-24 14:36:48 +00003018 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP, deltaX=%f, deltaY=%f",
3019 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003020 }
3021 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003022 if (DEBUG_GESTURES) {
3023 if (mPointerGesture.tapDownTime != LLONG_MIN) {
3024 ALOGD("Gestures: Not a TAP, %0.3fms since down",
3025 (when - mPointerGesture.tapDownTime) * 0.000001f);
3026 } else {
3027 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
3028 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003029 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003030 }
3031 }
3032
3033 mPointerVelocityControl.reset();
3034
3035 if (!tapped) {
Harry Cutts45483602022-08-24 14:36:48 +00003036 ALOGD_IF(DEBUG_GESTURES, "Gestures: NEUTRAL");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003037 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01003038 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003039 mPointerGesture.currentGestureIdBits.clear();
3040 }
3041 } else if (currentFingerCount == 1) {
3042 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
3043 // The pointer follows the active touch point.
3044 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3045 // When in TAP_DRAG, emit MOVE events at the pointer location.
3046 ALOG_ASSERT(activeTouchId >= 0);
3047
Michael Wright227c5542020-07-02 18:30:52 +01003048 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
3049 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003050 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003051 float x, y;
3052 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003053 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
3054 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01003055 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003056 } else {
Harry Cutts45483602022-08-24 14:36:48 +00003057 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3058 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003059 }
3060 } else {
Harry Cutts45483602022-08-24 14:36:48 +00003061 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, %0.3fms time since up",
3062 (when - mPointerGesture.tapUpTime) * 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003063 }
Michael Wright227c5542020-07-02 18:30:52 +01003064 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
3065 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003066 }
3067
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003068 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003069 // When using spots, the hover or drag will occur at the position of the anchor spot.
Harry Cutts714d1ad2022-08-24 16:36:43 +00003070 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003071 } else {
3072 mPointerVelocityControl.reset();
3073 }
3074
3075 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003076 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Harry Cutts45483602022-08-24 14:36:48 +00003077 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP_DRAG");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003078 down = true;
3079 } else {
Harry Cutts45483602022-08-24 14:36:48 +00003080 ALOGD_IF(DEBUG_GESTURES, "Gestures: HOVER");
Michael Wright227c5542020-07-02 18:30:52 +01003081 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003082 *outFinishPreviousGesture = true;
3083 }
3084 mPointerGesture.activeGestureId = 0;
3085 down = false;
3086 }
3087
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003088 float x, y;
3089 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003090
3091 mPointerGesture.currentGestureIdBits.clear();
3092 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3093 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3094 mPointerGesture.currentGestureProperties[0].clear();
3095 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3096 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3097 mPointerGesture.currentGestureCoords[0].clear();
3098 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3099 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3100 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3101 down ? 1.0f : 0.0f);
3102
3103 if (lastFingerCount == 0 && currentFingerCount != 0) {
3104 mPointerGesture.resetTap();
3105 mPointerGesture.tapDownTime = when;
3106 mPointerGesture.tapX = x;
3107 mPointerGesture.tapY = y;
3108 }
3109 } else {
3110 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3111 // We need to provide feedback for each finger that goes down so we cannot wait
3112 // for the fingers to move before deciding what to do.
3113 //
3114 // The ambiguous case is deciding what to do when there are two fingers down but they
3115 // have not moved enough to determine whether they are part of a drag or part of a
3116 // freeform gesture, or just a press or long-press at the pointer location.
3117 //
3118 // When there are two fingers we start with the PRESS hypothesis and we generate a
3119 // down at the pointer location.
3120 //
3121 // When the two fingers move enough or when additional fingers are added, we make
3122 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3123 ALOG_ASSERT(activeTouchId >= 0);
3124
3125 bool settled = when >=
3126 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003127 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3128 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3129 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003130 *outFinishPreviousGesture = true;
3131 } else if (!settled && currentFingerCount > lastFingerCount) {
3132 // Additional pointers have gone down but not yet settled.
3133 // Reset the gesture.
Harry Cutts45483602022-08-24 14:36:48 +00003134 ALOGD_IF(DEBUG_GESTURES,
3135 "Gestures: Resetting gesture since additional pointers went down for "
3136 "MULTITOUCH, settle time remaining %0.3fms",
3137 (mPointerGesture.firstTouchTime +
3138 mConfig.pointerGestureMultitouchSettleInterval - when) *
3139 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003140 *outCancelPreviousGesture = true;
3141 } else {
3142 // Continue previous gesture.
3143 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3144 }
3145
3146 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003147 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003148 mPointerGesture.activeGestureId = 0;
3149 mPointerGesture.referenceIdBits.clear();
3150 mPointerVelocityControl.reset();
3151
3152 // Use the centroid and pointer location as the reference points for the gesture.
Harry Cutts45483602022-08-24 14:36:48 +00003153 ALOGD_IF(DEBUG_GESTURES,
3154 "Gestures: Using centroid as reference for MULTITOUCH, settle time remaining "
3155 "%0.3fms",
3156 (mPointerGesture.firstTouchTime +
3157 mConfig.pointerGestureMultitouchSettleInterval - when) *
3158 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003159 mCurrentRawState.rawPointerData
3160 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3161 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003162 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3163 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003164 }
3165
3166 // Clear the reference deltas for fingers not yet included in the reference calculation.
3167 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3168 ~mPointerGesture.referenceIdBits.value);
3169 !idBits.isEmpty();) {
3170 uint32_t id = idBits.clearFirstMarkedBit();
3171 mPointerGesture.referenceDeltas[id].dx = 0;
3172 mPointerGesture.referenceDeltas[id].dy = 0;
3173 }
3174 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3175
3176 // Add delta for all fingers and calculate a common movement delta.
3177 float commonDeltaX = 0, commonDeltaY = 0;
3178 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3179 mCurrentCookedState.fingerIdBits.value);
3180 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3181 bool first = (idBits == commonIdBits);
3182 uint32_t id = idBits.clearFirstMarkedBit();
3183 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3184 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3185 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3186 delta.dx += cpd.x - lpd.x;
3187 delta.dy += cpd.y - lpd.y;
3188
3189 if (first) {
3190 commonDeltaX = delta.dx;
3191 commonDeltaY = delta.dy;
3192 } else {
3193 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3194 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3195 }
3196 }
3197
3198 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003199 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003200 float dist[MAX_POINTER_ID + 1];
3201 int32_t distOverThreshold = 0;
3202 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3203 uint32_t id = idBits.clearFirstMarkedBit();
3204 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3205 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3206 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3207 distOverThreshold += 1;
3208 }
3209 }
3210
3211 // Only transition when at least two pointers have moved further than
3212 // the minimum distance threshold.
3213 if (distOverThreshold >= 2) {
3214 if (currentFingerCount > 2) {
3215 // There are more than two pointers, switch to FREEFORM.
Harry Cutts45483602022-08-24 14:36:48 +00003216 ALOGD_IF(DEBUG_GESTURES,
3217 "Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3218 currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003219 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003220 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003221 } else {
3222 // There are exactly two pointers.
3223 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3224 uint32_t id1 = idBits.clearFirstMarkedBit();
3225 uint32_t id2 = idBits.firstMarkedBit();
3226 const RawPointerData::Pointer& p1 =
3227 mCurrentRawState.rawPointerData.pointerForId(id1);
3228 const RawPointerData::Pointer& p2 =
3229 mCurrentRawState.rawPointerData.pointerForId(id2);
3230 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3231 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3232 // There are two pointers but they are too far apart for a SWIPE,
3233 // switch to FREEFORM.
Harry Cutts45483602022-08-24 14:36:48 +00003234 ALOGD_IF(DEBUG_GESTURES,
3235 "Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
3236 mutualDistance, mPointerGestureMaxSwipeWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003237 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003238 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003239 } else {
3240 // There are two pointers. Wait for both pointers to start moving
3241 // before deciding whether this is a SWIPE or FREEFORM gesture.
3242 float dist1 = dist[id1];
3243 float dist2 = dist[id2];
3244 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3245 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3246 // Calculate the dot product of the displacement vectors.
3247 // When the vectors are oriented in approximately the same direction,
3248 // the angle betweeen them is near zero and the cosine of the angle
3249 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3250 // mag(v2).
3251 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3252 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3253 float dx1 = delta1.dx * mPointerXZoomScale;
3254 float dy1 = delta1.dy * mPointerYZoomScale;
3255 float dx2 = delta2.dx * mPointerXZoomScale;
3256 float dy2 = delta2.dy * mPointerYZoomScale;
3257 float dot = dx1 * dx2 + dy1 * dy2;
3258 float cosine = dot / (dist1 * dist2); // denominator always > 0
3259 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3260 // Pointers are moving in the same direction. Switch to SWIPE.
Harry Cutts45483602022-08-24 14:36:48 +00003261 ALOGD_IF(DEBUG_GESTURES,
3262 "Gestures: PRESS transitioned to SWIPE, "
3263 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3264 "cosine %0.3f >= %0.3f",
3265 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3266 mConfig.pointerGestureMultitouchMinDistance, cosine,
3267 mConfig.pointerGestureSwipeTransitionAngleCosine);
Michael Wright227c5542020-07-02 18:30:52 +01003268 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003269 } else {
3270 // Pointers are moving in different directions. Switch to FREEFORM.
Harry Cutts45483602022-08-24 14:36:48 +00003271 ALOGD_IF(DEBUG_GESTURES,
3272 "Gestures: PRESS transitioned to FREEFORM, "
3273 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3274 "cosine %0.3f < %0.3f",
3275 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3276 mConfig.pointerGestureMultitouchMinDistance, cosine,
3277 mConfig.pointerGestureSwipeTransitionAngleCosine);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003278 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003279 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003280 }
3281 }
3282 }
3283 }
3284 }
Michael Wright227c5542020-07-02 18:30:52 +01003285 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003286 // Switch from SWIPE to FREEFORM if additional pointers go down.
3287 // Cancel previous gesture.
3288 if (currentFingerCount > 2) {
Harry Cutts45483602022-08-24 14:36:48 +00003289 ALOGD_IF(DEBUG_GESTURES,
3290 "Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3291 currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003292 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003293 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003294 }
3295 }
3296
3297 // Move the reference points based on the overall group motion of the fingers
3298 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003299 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003300 (commonDeltaX || commonDeltaY)) {
3301 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3302 uint32_t id = idBits.clearFirstMarkedBit();
3303 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3304 delta.dx = 0;
3305 delta.dy = 0;
3306 }
3307
3308 mPointerGesture.referenceTouchX += commonDeltaX;
3309 mPointerGesture.referenceTouchY += commonDeltaY;
3310
3311 commonDeltaX *= mPointerXMovementScale;
3312 commonDeltaY *= mPointerYMovementScale;
3313
Prabir Pradhan1728b212021-10-19 16:00:03 -07003314 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003315 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3316
3317 mPointerGesture.referenceGestureX += commonDeltaX;
3318 mPointerGesture.referenceGestureY += commonDeltaY;
3319 }
3320
3321 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003322 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3323 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003324 // PRESS or SWIPE mode.
Harry Cutts45483602022-08-24 14:36:48 +00003325 ALOGD_IF(DEBUG_GESTURES,
3326 "Gestures: PRESS or SWIPE activeTouchId=%d, activeGestureId=%d, "
3327 "currentTouchPointerCount=%d",
3328 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003329 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3330
3331 mPointerGesture.currentGestureIdBits.clear();
3332 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3333 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3334 mPointerGesture.currentGestureProperties[0].clear();
3335 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3336 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3337 mPointerGesture.currentGestureCoords[0].clear();
3338 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3339 mPointerGesture.referenceGestureX);
3340 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3341 mPointerGesture.referenceGestureY);
3342 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003343 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003344 // FREEFORM mode.
Harry Cutts45483602022-08-24 14:36:48 +00003345 ALOGD_IF(DEBUG_GESTURES,
3346 "Gestures: FREEFORM activeTouchId=%d, activeGestureId=%d, "
3347 "currentTouchPointerCount=%d",
3348 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003349 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3350
3351 mPointerGesture.currentGestureIdBits.clear();
3352
3353 BitSet32 mappedTouchIdBits;
3354 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003355 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003356 // Initially, assign the active gesture id to the active touch point
3357 // if there is one. No other touch id bits are mapped yet.
3358 if (!*outCancelPreviousGesture) {
3359 mappedTouchIdBits.markBit(activeTouchId);
3360 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3361 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3362 mPointerGesture.activeGestureId;
3363 } else {
3364 mPointerGesture.activeGestureId = -1;
3365 }
3366 } else {
3367 // Otherwise, assume we mapped all touches from the previous frame.
3368 // Reuse all mappings that are still applicable.
3369 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3370 mCurrentCookedState.fingerIdBits.value;
3371 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3372
3373 // Check whether we need to choose a new active gesture id because the
3374 // current went went up.
3375 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3376 ~mCurrentCookedState.fingerIdBits.value);
3377 !upTouchIdBits.isEmpty();) {
3378 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3379 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3380 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3381 mPointerGesture.activeGestureId = -1;
3382 break;
3383 }
3384 }
3385 }
3386
Harry Cutts45483602022-08-24 14:36:48 +00003387 ALOGD_IF(DEBUG_GESTURES,
3388 "Gestures: FREEFORM follow up mappedTouchIdBits=0x%08x, "
3389 "usedGestureIdBits=0x%08x, activeGestureId=%d",
3390 mappedTouchIdBits.value, usedGestureIdBits.value,
3391 mPointerGesture.activeGestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003392
3393 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3394 for (uint32_t i = 0; i < currentFingerCount; i++) {
3395 uint32_t touchId = idBits.clearFirstMarkedBit();
3396 uint32_t gestureId;
3397 if (!mappedTouchIdBits.hasBit(touchId)) {
3398 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3399 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Harry Cutts45483602022-08-24 14:36:48 +00003400 ALOGD_IF(DEBUG_GESTURES,
3401 "Gestures: FREEFORM new mapping for touch id %d -> gesture id %d",
3402 touchId, gestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003403 } else {
3404 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Harry Cutts45483602022-08-24 14:36:48 +00003405 ALOGD_IF(DEBUG_GESTURES,
3406 "Gestures: FREEFORM existing mapping for touch id %d -> gesture id %d",
3407 touchId, gestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003408 }
3409 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3410 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3411
3412 const RawPointerData::Pointer& pointer =
3413 mCurrentRawState.rawPointerData.pointerForId(touchId);
3414 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3415 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003416 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003417
3418 mPointerGesture.currentGestureProperties[i].clear();
3419 mPointerGesture.currentGestureProperties[i].id = gestureId;
3420 mPointerGesture.currentGestureProperties[i].toolType =
3421 AMOTION_EVENT_TOOL_TYPE_FINGER;
3422 mPointerGesture.currentGestureCoords[i].clear();
3423 mPointerGesture.currentGestureCoords[i]
3424 .setAxisValue(AMOTION_EVENT_AXIS_X,
3425 mPointerGesture.referenceGestureX + deltaX);
3426 mPointerGesture.currentGestureCoords[i]
3427 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3428 mPointerGesture.referenceGestureY + deltaY);
3429 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3430 1.0f);
3431 }
3432
3433 if (mPointerGesture.activeGestureId < 0) {
3434 mPointerGesture.activeGestureId =
3435 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Harry Cutts45483602022-08-24 14:36:48 +00003436 ALOGD_IF(DEBUG_GESTURES, "Gestures: FREEFORM new activeGestureId=%d",
3437 mPointerGesture.activeGestureId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003438 }
3439 }
3440 }
3441
3442 mPointerController->setButtonState(mCurrentRawState.buttonState);
3443
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003444 if (DEBUG_GESTURES) {
3445 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3446 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3447 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3448 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3449 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3450 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3451 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3452 uint32_t id = idBits.clearFirstMarkedBit();
3453 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3454 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3455 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3456 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3457 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3458 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3459 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3460 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3461 }
3462 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3463 uint32_t id = idBits.clearFirstMarkedBit();
3464 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3465 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3466 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3467 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3468 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3469 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3470 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3471 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3472 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003473 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003474 return true;
3475}
3476
Harry Cutts714d1ad2022-08-24 16:36:43 +00003477void TouchInputMapper::moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId) {
3478 const RawPointerData::Pointer& currentPointer =
3479 mCurrentRawState.rawPointerData.pointerForId(pointerId);
3480 const RawPointerData::Pointer& lastPointer =
3481 mLastRawState.rawPointerData.pointerForId(pointerId);
3482 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3483 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3484
3485 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
3486 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3487
3488 mPointerController->move(deltaX, deltaY);
3489}
3490
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003491std::list<NotifyArgs> TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
3492 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003493 mPointerSimple.currentCoords.clear();
3494 mPointerSimple.currentProperties.clear();
3495
3496 bool down, hovering;
3497 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3498 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3499 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003500 mPointerController
3501 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3502 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003503
3504 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3505 down = !hovering;
3506
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003507 float x, y;
3508 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003509 mPointerSimple.currentCoords.copyFrom(
3510 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3511 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3512 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3513 mPointerSimple.currentProperties.id = 0;
3514 mPointerSimple.currentProperties.toolType =
3515 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3516 } else {
3517 down = false;
3518 hovering = false;
3519 }
3520
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003521 return dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003522}
3523
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003524std::list<NotifyArgs> TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime,
3525 uint32_t policyFlags) {
3526 return abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003527}
3528
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003529std::list<NotifyArgs> TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
3530 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003531 mPointerSimple.currentCoords.clear();
3532 mPointerSimple.currentProperties.clear();
3533
3534 bool down, hovering;
3535 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3536 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003537 if (mLastCookedState.mouseIdBits.hasBit(id)) {
Harry Cutts714d1ad2022-08-24 16:36:43 +00003538 moveMousePointerFromPointerDelta(when, id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003539 } else {
3540 mPointerVelocityControl.reset();
3541 }
3542
3543 down = isPointerDown(mCurrentRawState.buttonState);
3544 hovering = !down;
3545
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003546 float x, y;
3547 mPointerController->getPosition(&x, &y);
Harry Cutts714d1ad2022-08-24 16:36:43 +00003548 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003549 mPointerSimple.currentCoords.copyFrom(
3550 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3551 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3552 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3553 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3554 hovering ? 0.0f : 1.0f);
3555 mPointerSimple.currentProperties.id = 0;
3556 mPointerSimple.currentProperties.toolType =
3557 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3558 } else {
3559 mPointerVelocityControl.reset();
3560
3561 down = false;
3562 hovering = false;
3563 }
3564
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003565 return dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003566}
3567
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003568std::list<NotifyArgs> TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime,
3569 uint32_t policyFlags) {
3570 std::list<NotifyArgs> out = abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003571
3572 mPointerVelocityControl.reset();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003573
3574 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003575}
3576
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003577std::list<NotifyArgs> TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
3578 uint32_t policyFlags, bool down,
3579 bool hovering) {
3580 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003581 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003582
3583 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003584 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003585 mPointerController->clearSpots();
3586 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003587 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003588 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003589 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003590 }
Garfield Tan9514d782020-11-10 16:37:23 -08003591 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003592
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003593 float xCursorPosition, yCursorPosition;
3594 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003595
3596 if (mPointerSimple.down && !down) {
3597 mPointerSimple.down = false;
3598
3599 // Send up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003600 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3601 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0,
3602 0, metaState, mLastRawState.buttonState,
3603 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3604 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3605 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3606 yCursorPosition, mPointerSimple.downTime,
3607 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003608 }
3609
3610 if (mPointerSimple.hovering && !hovering) {
3611 mPointerSimple.hovering = false;
3612
3613 // Send hover exit.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003614 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3615 mSource, displayId, policyFlags,
3616 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState,
3617 mLastRawState.buttonState, MotionClassification::NONE,
3618 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3619 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3620 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3621 yCursorPosition, mPointerSimple.downTime,
3622 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003623 }
3624
3625 if (down) {
3626 if (!mPointerSimple.down) {
3627 mPointerSimple.down = true;
3628 mPointerSimple.downTime = when;
3629
3630 // Send down.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003631 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3632 mSource, displayId, policyFlags,
3633 AMOTION_EVENT_ACTION_DOWN, 0, 0, metaState,
3634 mCurrentRawState.buttonState, MotionClassification::NONE,
3635 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3636 &mPointerSimple.currentProperties,
3637 &mPointerSimple.currentCoords, mOrientedXPrecision,
3638 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3639 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003640 }
3641
3642 // Send move.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003643 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3644 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE,
3645 0, 0, metaState, mCurrentRawState.buttonState,
3646 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3647 &mPointerSimple.currentProperties,
3648 &mPointerSimple.currentCoords, mOrientedXPrecision,
3649 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3650 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003651 }
3652
3653 if (hovering) {
3654 if (!mPointerSimple.hovering) {
3655 mPointerSimple.hovering = true;
3656
3657 // Send hover enter.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003658 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3659 mSource, displayId, policyFlags,
3660 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
3661 mCurrentRawState.buttonState, MotionClassification::NONE,
3662 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3663 &mPointerSimple.currentProperties,
3664 &mPointerSimple.currentCoords, mOrientedXPrecision,
3665 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3666 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003667 }
3668
3669 // Send hover move.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003670 out.push_back(
3671 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3672 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3673 metaState, mCurrentRawState.buttonState,
3674 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3675 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3676 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3677 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003678 }
3679
3680 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3681 float vscroll = mCurrentRawState.rawVScroll;
3682 float hscroll = mCurrentRawState.rawHScroll;
3683 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3684 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3685
3686 // Send scroll.
3687 PointerCoords pointerCoords;
3688 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3689 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3690 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3691
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003692 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3693 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL,
3694 0, 0, metaState, mCurrentRawState.buttonState,
3695 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3696 &mPointerSimple.currentProperties, &pointerCoords,
3697 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3698 yCursorPosition, mPointerSimple.downTime,
3699 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003700 }
3701
3702 // Save state.
3703 if (down || hovering) {
3704 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3705 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3706 } else {
3707 mPointerSimple.reset();
3708 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003709 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003710}
3711
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003712std::list<NotifyArgs> TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime,
3713 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003714 mPointerSimple.currentCoords.clear();
3715 mPointerSimple.currentProperties.clear();
3716
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003717 return dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003718}
3719
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003720NotifyMotionArgs TouchInputMapper::dispatchMotion(
3721 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
3722 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
3723 int32_t edgeFlags, const PointerProperties* properties, const PointerCoords* coords,
3724 const uint32_t* idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
3725 float yPrecision, nsecs_t downTime, MotionClassification classification) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003726 PointerCoords pointerCoords[MAX_POINTERS];
3727 PointerProperties pointerProperties[MAX_POINTERS];
3728 uint32_t pointerCount = 0;
3729 while (!idBits.isEmpty()) {
3730 uint32_t id = idBits.clearFirstMarkedBit();
3731 uint32_t index = idToIndex[id];
3732 pointerProperties[pointerCount].copyFrom(properties[index]);
3733 pointerCoords[pointerCount].copyFrom(coords[index]);
3734
3735 if (changedId >= 0 && id == uint32_t(changedId)) {
3736 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3737 }
3738
3739 pointerCount += 1;
3740 }
3741
3742 ALOG_ASSERT(pointerCount != 0);
3743
3744 if (changedId >= 0 && pointerCount == 1) {
3745 // Replace initial down and final up action.
3746 // We can compare the action without masking off the changed pointer index
3747 // because we know the index is 0.
3748 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3749 action = AMOTION_EVENT_ACTION_DOWN;
3750 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003751 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3752 action = AMOTION_EVENT_ACTION_CANCEL;
3753 } else {
3754 action = AMOTION_EVENT_ACTION_UP;
3755 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003756 } else {
3757 // Can't happen.
3758 ALOG_ASSERT(false);
3759 }
3760 }
3761 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3762 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003763 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003764 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003765 }
3766 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3767 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003768 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003769 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003770 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003771 return NotifyMotionArgs(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3772 policyFlags, action, actionButton, flags, metaState, buttonState,
3773 classification, edgeFlags, pointerCount, pointerProperties,
3774 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3775 downTime, std::move(frames));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003776}
3777
3778bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3779 const PointerCoords* inCoords,
3780 const uint32_t* inIdToIndex,
3781 PointerProperties* outProperties,
3782 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3783 BitSet32 idBits) const {
3784 bool changed = false;
3785 while (!idBits.isEmpty()) {
3786 uint32_t id = idBits.clearFirstMarkedBit();
3787 uint32_t inIndex = inIdToIndex[id];
3788 uint32_t outIndex = outIdToIndex[id];
3789
3790 const PointerProperties& curInProperties = inProperties[inIndex];
3791 const PointerCoords& curInCoords = inCoords[inIndex];
3792 PointerProperties& curOutProperties = outProperties[outIndex];
3793 PointerCoords& curOutCoords = outCoords[outIndex];
3794
3795 if (curInProperties != curOutProperties) {
3796 curOutProperties.copyFrom(curInProperties);
3797 changed = true;
3798 }
3799
3800 if (curInCoords != curOutCoords) {
3801 curOutCoords.copyFrom(curInCoords);
3802 changed = true;
3803 }
3804 }
3805 return changed;
3806}
3807
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003808std::list<NotifyArgs> TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3809 std::list<NotifyArgs> out;
3810 out += abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3811 out += abortTouches(when, readTime, 0 /* policyFlags*/);
3812 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003813}
3814
Prabir Pradhan1728b212021-10-19 16:00:03 -07003815// Transform input device coordinates to display panel coordinates.
3816void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003817 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3818 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3819
arthurhunga36b28e2020-12-29 20:28:15 +08003820 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3821 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3822
Prabir Pradhan1728b212021-10-19 16:00:03 -07003823 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003824 // 0 - no swap and reverse.
3825 // 90 - swap x/y and reverse y.
3826 // 180 - reverse x, y.
3827 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003828 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003829 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003830 x = xScaled;
3831 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003832 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003833 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003834 y = xScaledMax;
3835 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003836 break;
3837 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003838 x = xScaledMax;
3839 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003840 break;
3841 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003842 y = xScaled;
3843 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003844 break;
3845 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003846 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003847 }
3848}
3849
Prabir Pradhan1728b212021-10-19 16:00:03 -07003850bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003851 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3852 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3853
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003854 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003855 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003856 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003857 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003858}
3859
3860const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3861 for (const VirtualKey& virtualKey : mVirtualKeys) {
Harry Cutts45483602022-08-24 14:36:48 +00003862 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
3863 "VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3864 "left=%d, top=%d, right=%d, bottom=%d",
3865 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3866 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003867
3868 if (virtualKey.isHit(x, y)) {
3869 return &virtualKey;
3870 }
3871 }
3872
3873 return nullptr;
3874}
3875
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003876void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3877 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3878 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003879
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003880 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003881
3882 if (currentPointerCount == 0) {
3883 // No pointers to assign.
3884 return;
3885 }
3886
3887 if (lastPointerCount == 0) {
3888 // All pointers are new.
3889 for (uint32_t i = 0; i < currentPointerCount; i++) {
3890 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003891 current.rawPointerData.pointers[i].id = id;
3892 current.rawPointerData.idToIndex[id] = i;
3893 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003894 }
3895 return;
3896 }
3897
3898 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003899 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003900 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003901 uint32_t id = last.rawPointerData.pointers[0].id;
3902 current.rawPointerData.pointers[0].id = id;
3903 current.rawPointerData.idToIndex[id] = 0;
3904 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003905 return;
3906 }
3907
3908 // General case.
3909 // We build a heap of squared euclidean distances between current and last pointers
3910 // associated with the current and last pointer indices. Then, we find the best
3911 // match (by distance) for each current pointer.
3912 // The pointers must have the same tool type but it is possible for them to
3913 // transition from hovering to touching or vice-versa while retaining the same id.
3914 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3915
3916 uint32_t heapSize = 0;
3917 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3918 currentPointerIndex++) {
3919 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3920 lastPointerIndex++) {
3921 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003922 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003923 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003924 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003925 if (currentPointer.toolType == lastPointer.toolType) {
3926 int64_t deltaX = currentPointer.x - lastPointer.x;
3927 int64_t deltaY = currentPointer.y - lastPointer.y;
3928
3929 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3930
3931 // Insert new element into the heap (sift up).
3932 heap[heapSize].currentPointerIndex = currentPointerIndex;
3933 heap[heapSize].lastPointerIndex = lastPointerIndex;
3934 heap[heapSize].distance = distance;
3935 heapSize += 1;
3936 }
3937 }
3938 }
3939
3940 // Heapify
3941 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3942 startIndex -= 1;
3943 for (uint32_t parentIndex = startIndex;;) {
3944 uint32_t childIndex = parentIndex * 2 + 1;
3945 if (childIndex >= heapSize) {
3946 break;
3947 }
3948
3949 if (childIndex + 1 < heapSize &&
3950 heap[childIndex + 1].distance < heap[childIndex].distance) {
3951 childIndex += 1;
3952 }
3953
3954 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3955 break;
3956 }
3957
3958 swap(heap[parentIndex], heap[childIndex]);
3959 parentIndex = childIndex;
3960 }
3961 }
3962
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003963 if (DEBUG_POINTER_ASSIGNMENT) {
3964 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3965 for (size_t i = 0; i < heapSize; i++) {
3966 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3967 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3968 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003969 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003970
3971 // Pull matches out by increasing order of distance.
3972 // To avoid reassigning pointers that have already been matched, the loop keeps track
3973 // of which last and current pointers have been matched using the matchedXXXBits variables.
3974 // It also tracks the used pointer id bits.
3975 BitSet32 matchedLastBits(0);
3976 BitSet32 matchedCurrentBits(0);
3977 BitSet32 usedIdBits(0);
3978 bool first = true;
3979 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3980 while (heapSize > 0) {
3981 if (first) {
3982 // The first time through the loop, we just consume the root element of
3983 // the heap (the one with smallest distance).
3984 first = false;
3985 } else {
3986 // Previous iterations consumed the root element of the heap.
3987 // Pop root element off of the heap (sift down).
3988 heap[0] = heap[heapSize];
3989 for (uint32_t parentIndex = 0;;) {
3990 uint32_t childIndex = parentIndex * 2 + 1;
3991 if (childIndex >= heapSize) {
3992 break;
3993 }
3994
3995 if (childIndex + 1 < heapSize &&
3996 heap[childIndex + 1].distance < heap[childIndex].distance) {
3997 childIndex += 1;
3998 }
3999
4000 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4001 break;
4002 }
4003
4004 swap(heap[parentIndex], heap[childIndex]);
4005 parentIndex = childIndex;
4006 }
4007
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004008 if (DEBUG_POINTER_ASSIGNMENT) {
4009 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
4010 for (size_t j = 0; j < heapSize; j++) {
4011 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
4012 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
4013 heap[j].distance);
4014 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004015 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004016 }
4017
4018 heapSize -= 1;
4019
4020 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4021 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4022
4023 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4024 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4025
4026 matchedCurrentBits.markBit(currentPointerIndex);
4027 matchedLastBits.markBit(lastPointerIndex);
4028
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004029 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
4030 current.rawPointerData.pointers[currentPointerIndex].id = id;
4031 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4032 current.rawPointerData.markIdBit(id,
4033 current.rawPointerData.isHovering(
4034 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004035 usedIdBits.markBit(id);
4036
Harry Cutts45483602022-08-24 14:36:48 +00004037 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
4038 "assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
4039 ", distance=%" PRIu64,
4040 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004041 break;
4042 }
4043 }
4044
4045 // Assign fresh ids to pointers that were not matched in the process.
4046 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4047 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4048 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4049
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004050 current.rawPointerData.pointers[currentPointerIndex].id = id;
4051 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4052 current.rawPointerData.markIdBit(id,
4053 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004054
Harry Cutts45483602022-08-24 14:36:48 +00004055 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
4056 "assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
4057 id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004058 }
4059}
4060
4061int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4062 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4063 return AKEY_STATE_VIRTUAL;
4064 }
4065
4066 for (const VirtualKey& virtualKey : mVirtualKeys) {
4067 if (virtualKey.keyCode == keyCode) {
4068 return AKEY_STATE_UP;
4069 }
4070 }
4071
4072 return AKEY_STATE_UNKNOWN;
4073}
4074
4075int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4076 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4077 return AKEY_STATE_VIRTUAL;
4078 }
4079
4080 for (const VirtualKey& virtualKey : mVirtualKeys) {
4081 if (virtualKey.scanCode == scanCode) {
4082 return AKEY_STATE_UP;
4083 }
4084 }
4085
4086 return AKEY_STATE_UNKNOWN;
4087}
4088
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004089bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
4090 const std::vector<int32_t>& keyCodes,
4091 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004092 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004093 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004094 if (virtualKey.keyCode == keyCodes[i]) {
4095 outFlags[i] = 1;
4096 }
4097 }
4098 }
4099
4100 return true;
4101}
4102
4103std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4104 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004105 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004106 return std::make_optional(mPointerController->getDisplayId());
4107 } else {
4108 return std::make_optional(mViewport.displayId);
4109 }
4110 }
4111 return std::nullopt;
4112}
4113
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004114} // namespace android