blob: ba019f60b8f341416e30edb9599d127b57a60801 [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
49template <typename T>
50inline static void swap(T& a, T& b) {
51 T temp = a;
52 a = b;
53 b = temp;
54}
55
56static float calculateCommonVector(float a, float b) {
57 if (a > 0 && b > 0) {
58 return a < b ? a : b;
59 } else if (a < 0 && b < 0) {
60 return a > b ? a : b;
61 } else {
62 return 0;
63 }
64}
65
66inline static float distance(float x1, float y1, float x2, float y2) {
67 return hypotf(x1 - x2, y1 - y2);
68}
69
70inline static int32_t signExtendNybble(int32_t value) {
71 return value >= 8 ? value - 16 : value;
72}
73
74// --- RawPointerAxes ---
75
76RawPointerAxes::RawPointerAxes() {
77 clear();
78}
79
80void RawPointerAxes::clear() {
81 x.clear();
82 y.clear();
83 pressure.clear();
84 touchMajor.clear();
85 touchMinor.clear();
86 toolMajor.clear();
87 toolMinor.clear();
88 orientation.clear();
89 distance.clear();
90 tiltX.clear();
91 tiltY.clear();
92 trackingId.clear();
93 slot.clear();
94}
95
96// --- RawPointerData ---
97
98RawPointerData::RawPointerData() {
99 clear();
100}
101
102void RawPointerData::clear() {
103 pointerCount = 0;
104 clearIdBits();
105}
106
107void RawPointerData::copyFrom(const RawPointerData& other) {
108 pointerCount = other.pointerCount;
109 hoveringIdBits = other.hoveringIdBits;
110 touchingIdBits = other.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +0800111 canceledIdBits = other.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112
113 for (uint32_t i = 0; i < pointerCount; i++) {
114 pointers[i] = other.pointers[i];
115
116 int id = pointers[i].id;
117 idToIndex[id] = other.idToIndex[id];
118 }
119}
120
121void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
122 float x = 0, y = 0;
123 uint32_t count = touchingIdBits.count();
124 if (count) {
125 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
126 uint32_t id = idBits.clearFirstMarkedBit();
127 const Pointer& pointer = pointerForId(id);
128 x += pointer.x;
129 y += pointer.y;
130 }
131 x /= count;
132 y /= count;
133 }
134 *outX = x;
135 *outY = y;
136}
137
138// --- CookedPointerData ---
139
140CookedPointerData::CookedPointerData() {
141 clear();
142}
143
144void CookedPointerData::clear() {
145 pointerCount = 0;
146 hoveringIdBits.clear();
147 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800148 canceledIdBits.clear();
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000149 validIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700150}
151
152void CookedPointerData::copyFrom(const CookedPointerData& other) {
153 pointerCount = other.pointerCount;
154 hoveringIdBits = other.hoveringIdBits;
155 touchingIdBits = other.touchingIdBits;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000156 validIdBits = other.validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700157
158 for (uint32_t i = 0; i < pointerCount; i++) {
159 pointerProperties[i].copyFrom(other.pointerProperties[i]);
160 pointerCoords[i].copyFrom(other.pointerCoords[i]);
161
162 int id = pointerProperties[i].id;
163 idToIndex[id] = other.idToIndex[id];
164 }
165}
166
167// --- TouchInputMapper ---
168
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800169TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
170 : InputMapper(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700171 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100172 mDeviceMode(DeviceMode::DISABLED),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700173 mDisplayWidth(-1),
174 mDisplayHeight(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700175 mPhysicalWidth(-1),
176 mPhysicalHeight(-1),
177 mPhysicalLeft(0),
178 mPhysicalTop(0),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700179 mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700180
181TouchInputMapper::~TouchInputMapper() {}
182
Philip Junker4af3b3d2021-12-14 10:36:55 +0100183uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700184 return mSource;
185}
186
187void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
188 InputMapper::populateDeviceInfo(info);
189
Michael Wright227c5542020-07-02 18:30:52 +0100190 if (mDeviceMode != DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191 info->addMotionRange(mOrientedRanges.x);
192 info->addMotionRange(mOrientedRanges.y);
193 info->addMotionRange(mOrientedRanges.pressure);
194
Chris Yef74dc422020-09-02 22:41:50 -0700195 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
Chris Ye8fa17282020-09-15 17:17:34 -0700196 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
197 //
198 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
199 // motion, i.e. the hardware dimensions, as the finger could move completely across the
200 // touchpad in one sample cycle.
Chris Yef74dc422020-09-02 22:41:50 -0700201 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
202 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
203 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat,
204 x.fuzz, x.resolution);
205 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat,
206 y.fuzz, y.resolution);
207 }
208
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700209 if (mOrientedRanges.size) {
210 info->addMotionRange(*mOrientedRanges.size);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700211 }
212
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700213 if (mOrientedRanges.touchMajor) {
214 info->addMotionRange(*mOrientedRanges.touchMajor);
215 info->addMotionRange(*mOrientedRanges.touchMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700216 }
217
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700218 if (mOrientedRanges.toolMajor) {
219 info->addMotionRange(*mOrientedRanges.toolMajor);
220 info->addMotionRange(*mOrientedRanges.toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 }
222
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700223 if (mOrientedRanges.orientation) {
224 info->addMotionRange(*mOrientedRanges.orientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700225 }
226
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700227 if (mOrientedRanges.distance) {
228 info->addMotionRange(*mOrientedRanges.distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 }
230
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700231 if (mOrientedRanges.tilt) {
232 info->addMotionRange(*mOrientedRanges.tilt);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700233 }
234
235 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
236 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
237 0.0f);
238 }
239 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
240 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
241 0.0f);
242 }
Michael Wright227c5542020-07-02 18:30:52 +0100243 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700244 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
245 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
246 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
247 x.fuzz, x.resolution);
248 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
249 y.fuzz, y.resolution);
250 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
251 x.fuzz, x.resolution);
252 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
253 y.fuzz, y.resolution);
254 }
255 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
256 }
257}
258
259void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700260 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800261 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700262 dumpParameters(dump);
263 dumpVirtualKeys(dump);
264 dumpRawPointerAxes(dump);
265 dumpCalibration(dump);
266 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700267 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268
269 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270 dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale);
271 dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale);
272 dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
273 dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
274 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
275 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
276 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
277 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
278 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
279 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
280 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
281 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
282 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
283 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
284
285 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
286 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
287 mLastRawState.rawPointerData.pointerCount);
288 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
289 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
290 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
291 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
292 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
293 "toolType=%d, isHovering=%s\n",
294 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
295 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
296 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
297 pointer.distance, pointer.toolType, toString(pointer.isHovering));
298 }
299
300 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
301 mLastCookedState.buttonState);
302 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
303 mLastCookedState.cookedPointerData.pointerCount);
304 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
305 const PointerProperties& pointerProperties =
306 mLastCookedState.cookedPointerData.pointerProperties[i];
307 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000308 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
309 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
310 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
312 "toolType=%d, isHovering=%s\n",
313 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000314 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
315 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
317 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
318 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
319 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
320 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
321 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
322 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
323 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
324 pointerProperties.toolType,
325 toString(mLastCookedState.cookedPointerData.isHovering(i)));
326 }
327
328 dump += INDENT3 "Stylus Fusion:\n";
329 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
330 toString(mExternalStylusConnected));
331 dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
332 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
333 mExternalStylusFusionTimeout);
334 dump += INDENT3 "External Stylus State:\n";
335 dumpStylusState(dump, mExternalStylusState);
336
Michael Wright227c5542020-07-02 18:30:52 +0100337 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700338 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
339 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
340 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
341 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
342 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
343 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
344 }
345}
346
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
348 uint32_t changes) {
349 InputMapper::configure(when, config, changes);
350
351 mConfig = *config;
352
353 if (!changes) { // first time only
354 // Configure basic parameters.
355 configureParameters();
356
357 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800358 mCursorScrollAccumulator.configure(getDeviceContext());
359 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700360
361 // Configure absolute axis information.
362 configureRawPointerAxes();
363
364 // Prepare input device calibration.
365 parseCalibration();
366 resolveCalibration();
367 }
368
369 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
370 // Update location calibration to reflect current settings
371 updateAffineTransformation();
372 }
373
374 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
375 // Update pointer speed.
376 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
377 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
378 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
379 }
380
381 bool resetNeeded = false;
382 if (!changes ||
383 (changes &
384 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800385 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700386 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
387 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
388 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700389 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700391 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392 }
393
394 if (changes && resetNeeded) {
lilinnan687e58f2022-07-19 16:00:50 +0800395 // If device was reset, cancel touch event and update touch spot state.
396 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
397 mCurrentCookedState.clear();
398 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700399 // Send reset, unless this is the first time the device has been configured,
400 // in which case the reader will call reset itself after all mappers are ready.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000401 NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId());
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700402 getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403 }
404}
405
406void TouchInputMapper::resolveExternalStylusPresence() {
407 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800408 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 mExternalStylusConnected = !devices.empty();
410
411 if (!mExternalStylusConnected) {
412 resetExternalStylus();
413 }
414}
415
416void TouchInputMapper::configureParameters() {
417 // Use the pointer presentation mode for devices that do not support distinct
418 // multitouch. The spot-based presentation relies on being able to accurately
419 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800420 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100421 ? Parameters::GestureMode::SINGLE_TOUCH
422 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700423
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700424 std::string gestureModeString;
425 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800426 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700427 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100428 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100430 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700432 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 }
434 }
435
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800436 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100438 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800439 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700440 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100441 mParameters.deviceType = Parameters::DeviceType::POINTER;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800442 } else if (getDeviceContext().hasRelativeAxis(REL_X) ||
443 getDeviceContext().hasRelativeAxis(REL_Y)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444 // The device is a cursor device with a touch pad attached.
445 // By default don't use the touch pad to move the pointer.
Michael Wright227c5542020-07-02 18:30:52 +0100446 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447 } else {
448 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100449 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450 }
451
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800452 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700454 std::string deviceTypeString;
455 if (getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800456 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100458 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 } else if (deviceTypeString == "touchPad") {
Michael Wright227c5542020-07-02 18:30:52 +0100460 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100462 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100464 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700465 } else if (deviceTypeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700466 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467 }
468 }
469
Michael Wright227c5542020-07-02 18:30:52 +0100470 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700471 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800472 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700474 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700475 std::string orientationString;
476 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700477 orientationString)) {
478 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
479 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
480 } else if (orientationString == "ORIENTATION_90") {
481 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
482 } else if (orientationString == "ORIENTATION_180") {
483 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
484 } else if (orientationString == "ORIENTATION_270") {
485 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
486 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700487 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700488 }
489 }
490
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700491 mParameters.hasAssociatedDisplay = false;
492 mParameters.associatedDisplayIsExternal = false;
493 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100494 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
495 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700496 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100497 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800498 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700499 std::string uniqueDisplayId;
500 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800501 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700502 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
503 }
504 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800505 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700506 mParameters.hasAssociatedDisplay = true;
507 }
508
509 // Initial downs on external touch devices should wake the device.
510 // Normally we don't do this for internal touch screens to prevent them from waking
511 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800512 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700513 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700514}
515
516void TouchInputMapper::dumpParameters(std::string& dump) {
517 dump += INDENT3 "Parameters:\n";
518
Dominik Laskowski75788452021-02-09 18:51:25 -0800519 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700520
Dominik Laskowski75788452021-02-09 18:51:25 -0800521 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700522
523 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
524 "displayId='%s'\n",
525 toString(mParameters.hasAssociatedDisplay),
526 toString(mParameters.associatedDisplayIsExternal),
527 mParameters.uniqueDisplayId.c_str());
528 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800529 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700530}
531
532void TouchInputMapper::configureRawPointerAxes() {
533 mRawPointerAxes.clear();
534}
535
536void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
537 dump += INDENT3 "Raw Touch Axes:\n";
538 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
539 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
545 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
546 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
547 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
548 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
549 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
550 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
551}
552
553bool TouchInputMapper::hasExternalStylus() const {
554 return mExternalStylusConnected;
555}
556
557/**
558 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000559 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800560 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000561 * 3. Get the matching viewport by either unique id in idc file or by the display type
562 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800563 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700564 */
565std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800566 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000567 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800568 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700569 }
570
Christine Franks2a2293c2022-01-18 11:51:16 -0800571 const std::optional<std::string> associatedDisplayUniqueId =
572 getDeviceContext().getAssociatedDisplayUniqueId();
573 if (associatedDisplayUniqueId) {
574 return getDeviceContext().getAssociatedViewport();
575 }
576
Michael Wright227c5542020-07-02 18:30:52 +0100577 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800578 std::optional<DisplayViewport> viewport =
579 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
580 if (viewport) {
581 return viewport;
582 } else {
583 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
584 mConfig.defaultPointerDisplayId);
585 }
586 }
587
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700588 // Check if uniqueDisplayId is specified in idc file.
589 if (!mParameters.uniqueDisplayId.empty()) {
590 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
591 }
592
593 ViewportType viewportTypeToUse;
594 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100595 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700596 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100597 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700598 }
599
600 std::optional<DisplayViewport> viewport =
601 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100602 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700603 ALOGW("Input device %s should be associated with external display, "
604 "fallback to internal one for the external viewport is not found.",
605 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100606 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700607 }
608
609 return viewport;
610 }
611
612 // No associated display, return a non-display viewport.
613 DisplayViewport newViewport;
614 // Raw width and height in the natural orientation.
615 int32_t rawWidth = mRawPointerAxes.getRawWidth();
616 int32_t rawHeight = mRawPointerAxes.getRawHeight();
617 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
618 return std::make_optional(newViewport);
619}
620
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800621int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
622 if (resolution < 0) {
623 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
624 getDeviceName().c_str());
625 return 0;
626 }
627 return resolution;
628}
629
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800630void TouchInputMapper::initializeSizeRanges() {
631 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
632 mSizeScale = 0.0f;
633 return;
634 }
635
636 // Size of diagonal axis.
637 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
638
639 // Size factors.
640 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
641 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
642 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
643 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
644 } else {
645 mSizeScale = 0.0f;
646 }
647
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700648 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
649 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
650 .source = mSource,
651 .min = 0,
652 .max = diagonalSize,
653 .flat = 0,
654 .fuzz = 0,
655 .resolution = 0,
656 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800657
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800658 if (mRawPointerAxes.touchMajor.valid) {
659 mRawPointerAxes.touchMajor.resolution =
660 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700661 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800662 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800663
664 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700665 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800666 if (mRawPointerAxes.touchMinor.valid) {
667 mRawPointerAxes.touchMinor.resolution =
668 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700669 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800670 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800671
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700672 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
673 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
674 .source = mSource,
675 .min = 0,
676 .max = diagonalSize,
677 .flat = 0,
678 .fuzz = 0,
679 .resolution = 0,
680 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800681 if (mRawPointerAxes.toolMajor.valid) {
682 mRawPointerAxes.toolMajor.resolution =
683 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700684 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800685 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800686
687 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700688 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800689 if (mRawPointerAxes.toolMinor.valid) {
690 mRawPointerAxes.toolMinor.resolution =
691 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700692 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800693 }
694
695 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700696 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
697 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
698 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
699 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800700 } else {
701 // Support for other calibrations can be added here.
702 ALOGW("%s calibration is not supported for size ranges at the moment. "
703 "Using raw resolution instead",
704 ftl::enum_string(mCalibration.sizeCalibration).c_str());
705 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800706
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700707 mOrientedRanges.size = InputDeviceInfo::MotionRange{
708 .axis = AMOTION_EVENT_AXIS_SIZE,
709 .source = mSource,
710 .min = 0,
711 .max = 1.0,
712 .flat = 0,
713 .fuzz = 0,
714 .resolution = 0,
715 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800716}
717
718void TouchInputMapper::initializeOrientedRanges() {
719 // Configure X and Y factors.
720 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
721 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
722 mXPrecision = 1.0f / mXScale;
723 mYPrecision = 1.0f / mYScale;
724
725 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
726 mOrientedRanges.x.source = mSource;
727 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
728 mOrientedRanges.y.source = mSource;
729
730 // Scale factor for terms that are not oriented in a particular axis.
731 // If the pixels are square then xScale == yScale otherwise we fake it
732 // by choosing an average.
733 mGeometricScale = avg(mXScale, mYScale);
734
735 initializeSizeRanges();
736
737 // Pressure factors.
738 mPressureScale = 0;
739 float pressureMax = 1.0;
740 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
741 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700742 if (mCalibration.pressureScale) {
743 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800744 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
745 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
746 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
747 }
748 }
749
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700750 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
751 .axis = AMOTION_EVENT_AXIS_PRESSURE,
752 .source = mSource,
753 .min = 0,
754 .max = pressureMax,
755 .flat = 0,
756 .fuzz = 0,
757 .resolution = 0,
758 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800759
760 // Tilt
761 mTiltXCenter = 0;
762 mTiltXScale = 0;
763 mTiltYCenter = 0;
764 mTiltYScale = 0;
765 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
766 if (mHaveTilt) {
767 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
768 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
769 mTiltXScale = M_PI / 180;
770 mTiltYScale = M_PI / 180;
771
772 if (mRawPointerAxes.tiltX.resolution) {
773 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
774 }
775 if (mRawPointerAxes.tiltY.resolution) {
776 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
777 }
778
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700779 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
780 .axis = AMOTION_EVENT_AXIS_TILT,
781 .source = mSource,
782 .min = 0,
783 .max = M_PI_2,
784 .flat = 0,
785 .fuzz = 0,
786 .resolution = 0,
787 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800788 }
789
790 // Orientation
791 mOrientationScale = 0;
792 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700793 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
794 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
795 .source = mSource,
796 .min = -M_PI,
797 .max = M_PI,
798 .flat = 0,
799 .fuzz = 0,
800 .resolution = 0,
801 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800802
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800803 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
804 if (mCalibration.orientationCalibration ==
805 Calibration::OrientationCalibration::INTERPOLATED) {
806 if (mRawPointerAxes.orientation.valid) {
807 if (mRawPointerAxes.orientation.maxValue > 0) {
808 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
809 } else if (mRawPointerAxes.orientation.minValue < 0) {
810 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
811 } else {
812 mOrientationScale = 0;
813 }
814 }
815 }
816
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700817 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
818 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
819 .source = mSource,
820 .min = -M_PI_2,
821 .max = M_PI_2,
822 .flat = 0,
823 .fuzz = 0,
824 .resolution = 0,
825 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800826 }
827
828 // Distance
829 mDistanceScale = 0;
830 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
831 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700832 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800833 }
834
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700835 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800836
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700837 .axis = AMOTION_EVENT_AXIS_DISTANCE,
838 .source = mSource,
839 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
840 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
841 .flat = 0,
842 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
843 .resolution = 0,
844 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800845 }
846
847 // Compute oriented precision, scales and ranges.
848 // Note that the maximum value reported is an inclusive maximum value so it is one
849 // unit less than the total width or height of the display.
850 switch (mInputDeviceOrientation) {
851 case DISPLAY_ORIENTATION_90:
852 case DISPLAY_ORIENTATION_270:
853 mOrientedXPrecision = mYPrecision;
854 mOrientedYPrecision = mXPrecision;
855
856 mOrientedRanges.x.min = 0;
857 mOrientedRanges.x.max = mDisplayHeight - 1;
858 mOrientedRanges.x.flat = 0;
859 mOrientedRanges.x.fuzz = 0;
860 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
861
862 mOrientedRanges.y.min = 0;
863 mOrientedRanges.y.max = mDisplayWidth - 1;
864 mOrientedRanges.y.flat = 0;
865 mOrientedRanges.y.fuzz = 0;
866 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
867 break;
868
869 default:
870 mOrientedXPrecision = mXPrecision;
871 mOrientedYPrecision = mYPrecision;
872
873 mOrientedRanges.x.min = 0;
874 mOrientedRanges.x.max = mDisplayWidth - 1;
875 mOrientedRanges.x.flat = 0;
876 mOrientedRanges.x.fuzz = 0;
877 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
878
879 mOrientedRanges.y.min = 0;
880 mOrientedRanges.y.max = mDisplayHeight - 1;
881 mOrientedRanges.y.flat = 0;
882 mOrientedRanges.y.fuzz = 0;
883 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
884 break;
885 }
886}
887
Prabir Pradhan1728b212021-10-19 16:00:03 -0700888void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100889 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700890
891 resolveExternalStylusPresence();
892
893 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100894 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000895 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700896 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100897 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700898 if (hasStylus()) {
899 mSource |= AINPUT_SOURCE_STYLUS;
900 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800901 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700902 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100903 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700904 if (hasStylus()) {
905 mSource |= AINPUT_SOURCE_STYLUS;
906 }
907 if (hasExternalStylus()) {
908 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
909 }
Michael Wright227c5542020-07-02 18:30:52 +0100910 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700911 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100912 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700913 } else {
914 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100915 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700916 }
917
918 // Ensure we have valid X and Y axes.
919 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
920 ALOGW("Touch device '%s' did not report support for X or Y axis! "
921 "The device will be inoperable.",
922 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100923 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700924 return;
925 }
926
927 // Get associated display dimensions.
928 std::optional<DisplayViewport> newViewport = findViewport();
929 if (!newViewport) {
930 ALOGI("Touch device '%s' could not query the properties of its associated "
931 "display. The device will be inoperable until the display size "
932 "becomes available.",
933 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100934 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700935 return;
936 }
937
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000938 if (!newViewport->isActive) {
939 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
940 getDeviceName().c_str(), getDeviceId());
941 mDeviceMode = DeviceMode::DISABLED;
942 return;
943 }
944
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700945 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700946 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
947 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
HQ Liue6983c72022-04-19 22:14:56 +0000948 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
949 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
950 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
951 const float rawMeanResolution =
952 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700953
Prabir Pradhan1728b212021-10-19 16:00:03 -0700954 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700955 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700956 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700957 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
lilinnan687e58f2022-07-19 16:00:50 +0800958 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport->displayId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700959 mViewport = *newViewport;
960
Michael Wright227c5542020-07-02 18:30:52 +0100961 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700962 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700963 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
964 int32_t naturalPhysicalLeft, naturalPhysicalTop;
965 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700966
Prabir Pradhan1728b212021-10-19 16:00:03 -0700967 // Apply the inverse of the input device orientation so that the input device is
968 // configured in the same orientation as the viewport. The input device orientation will
969 // be re-applied by mInputDeviceOrientation.
970 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700971 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700972 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700973 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700974 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
975 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800976 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700977 naturalPhysicalTop = mViewport.physicalLeft;
978 naturalDeviceWidth = mViewport.deviceHeight;
979 naturalDeviceHeight = mViewport.deviceWidth;
980 break;
981 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700982 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
983 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
984 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
985 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
986 naturalDeviceWidth = mViewport.deviceWidth;
987 naturalDeviceHeight = mViewport.deviceHeight;
988 break;
989 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700990 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
991 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
992 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800993 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700994 naturalDeviceWidth = mViewport.deviceHeight;
995 naturalDeviceHeight = mViewport.deviceWidth;
996 break;
997 case DISPLAY_ORIENTATION_0:
998 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700999 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
1000 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
1001 naturalPhysicalLeft = mViewport.physicalLeft;
1002 naturalPhysicalTop = mViewport.physicalTop;
1003 naturalDeviceWidth = mViewport.deviceWidth;
1004 naturalDeviceHeight = mViewport.deviceHeight;
1005 break;
1006 }
1007
1008 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
1009 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
1010 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
1011 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1012 }
1013
1014 mPhysicalWidth = naturalPhysicalWidth;
1015 mPhysicalHeight = naturalPhysicalHeight;
1016 mPhysicalLeft = naturalPhysicalLeft;
1017 mPhysicalTop = naturalPhysicalTop;
1018
Prabir Pradhan1728b212021-10-19 16:00:03 -07001019 const int32_t oldDisplayWidth = mDisplayWidth;
1020 const int32_t oldDisplayHeight = mDisplayHeight;
1021 mDisplayWidth = naturalDeviceWidth;
1022 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001023
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001024 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1025 // anything if the device is already orientation-aware. If the device is not
1026 // orientation-aware, then we need to apply the inverse rotation of the display so that
1027 // when the display rotation is applied later as a part of the per-window transform, we
1028 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001029 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001030 ? DISPLAY_ORIENTATION_0
1031 : getInverseRotation(mViewport.orientation);
1032 // For orientation-aware devices that work in the un-rotated coordinate space, the
1033 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001034 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
1035 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001036
1037 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001038 mInputDeviceOrientation =
1039 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001040 } else {
1041 mPhysicalWidth = rawWidth;
1042 mPhysicalHeight = rawHeight;
1043 mPhysicalLeft = 0;
1044 mPhysicalTop = 0;
1045
Prabir Pradhan1728b212021-10-19 16:00:03 -07001046 mDisplayWidth = rawWidth;
1047 mDisplayHeight = rawHeight;
1048 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001049 }
lilinnan687e58f2022-07-19 16:00:50 +08001050 // If displayId changed, do not skip viewport update.
1051 skipViewportUpdate &= !viewportDisplayIdChanged;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001052 }
1053
1054 // If moving between pointer modes, need to reset some state.
1055 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1056 if (deviceModeChanged) {
1057 mOrientedRanges.clear();
1058 }
1059
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001060 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1061 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001062 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001063 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001064 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1065 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001066 if (mPointerController == nullptr) {
1067 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001068 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001069 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001070 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1071 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001072 } else {
lilinnandef700b2022-06-17 19:32:01 +08001073 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1074 !mConfig.showTouches) {
1075 mPointerController->clearSpots();
1076 }
Michael Wright17db18e2020-06-26 20:51:44 +01001077 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001078 }
1079
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001080 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001081 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1082 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001083 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1084 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001085
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001086 configureVirtualKeys();
1087
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001088 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001089
1090 // Location
1091 updateAffineTransformation();
1092
Michael Wright227c5542020-07-02 18:30:52 +01001093 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001094 // Compute pointer gesture detection parameters.
1095 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001096 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001097
1098 // Scale movements such that one whole swipe of the touch pad covers a
1099 // given area relative to the diagonal size of the display when no acceleration
1100 // is applied.
1101 // Assume that the touch pad has a square aspect ratio such that movements in
1102 // X and Y of the same number of raw units cover the same physical distance.
1103 mPointerXMovementScale =
1104 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1105 mPointerYMovementScale = mPointerXMovementScale;
1106
1107 // Scale zooms to cover a smaller range of the display than movements do.
1108 // This value determines the area around the pointer that is affected by freeform
1109 // pointer gestures.
1110 mPointerXZoomScale =
1111 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1112 mPointerYZoomScale = mPointerXZoomScale;
1113
HQ Liue6983c72022-04-19 22:14:56 +00001114 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1115 // axis is non positive value.
1116 const float minFreeformGestureWidth =
1117 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1118
1119 mPointerGestureMaxSwipeWidth =
1120 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1121 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001122
1123 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001124 const nsecs_t readTime = when; // synthetic event
1125 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001126 }
1127
1128 // Inform the dispatcher about the changes.
1129 *outResetNeeded = true;
1130 bumpGeneration();
1131 }
1132}
1133
Prabir Pradhan1728b212021-10-19 16:00:03 -07001134void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001135 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001136 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1137 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001138 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1139 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1140 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1141 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001142 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001143}
1144
1145void TouchInputMapper::configureVirtualKeys() {
1146 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001147 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001148
1149 mVirtualKeys.clear();
1150
1151 if (virtualKeyDefinitions.size() == 0) {
1152 return;
1153 }
1154
1155 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1156 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1157 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1158 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1159
1160 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1161 VirtualKey virtualKey;
1162
1163 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1164 int32_t keyCode;
1165 int32_t dummyKeyMetaState;
1166 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001167 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1168 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001169 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1170 continue; // drop the key
1171 }
1172
1173 virtualKey.keyCode = keyCode;
1174 virtualKey.flags = flags;
1175
1176 // convert the key definition's display coordinates into touch coordinates for a hit box
1177 int32_t halfWidth = virtualKeyDefinition.width / 2;
1178 int32_t halfHeight = virtualKeyDefinition.height / 2;
1179
1180 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001181 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001182 touchScreenLeft;
1183 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001184 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001185 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001186 virtualKey.hitTop =
1187 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001188 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001189 virtualKey.hitBottom =
1190 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001191 touchScreenTop;
1192 mVirtualKeys.push_back(virtualKey);
1193 }
1194}
1195
1196void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1197 if (!mVirtualKeys.empty()) {
1198 dump += INDENT3 "Virtual Keys:\n";
1199
1200 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1201 const VirtualKey& virtualKey = mVirtualKeys[i];
1202 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1203 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1204 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1205 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1206 }
1207 }
1208}
1209
1210void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001211 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001212 Calibration& out = mCalibration;
1213
1214 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001215 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001216 std::string sizeCalibrationString;
1217 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001218 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001219 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001220 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001221 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001222 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001223 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001224 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001225 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001226 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001227 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001228 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001229 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001230 }
1231 }
1232
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001233 float sizeScale;
1234
1235 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1236 out.sizeScale = sizeScale;
1237 }
1238 float sizeBias;
1239 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1240 out.sizeBias = sizeBias;
1241 }
1242 bool sizeIsSummed;
1243 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1244 out.sizeIsSummed = sizeIsSummed;
1245 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001246
1247 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001248 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001249 std::string pressureCalibrationString;
1250 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001251 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001252 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001253 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001254 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001255 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001256 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001257 } else if (pressureCalibrationString != "default") {
1258 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001259 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260 }
1261 }
1262
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001263 float pressureScale;
1264 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1265 out.pressureScale = pressureScale;
1266 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001267
1268 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001269 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001270 std::string orientationCalibrationString;
1271 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001272 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001273 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001274 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001275 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001276 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001277 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001278 } else if (orientationCalibrationString != "default") {
1279 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001280 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001281 }
1282 }
1283
1284 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001285 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001286 std::string distanceCalibrationString;
1287 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001288 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001289 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001290 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001291 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001292 } else if (distanceCalibrationString != "default") {
1293 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001294 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001295 }
1296 }
1297
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001298 float distanceScale;
1299 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1300 out.distanceScale = distanceScale;
1301 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001302
Michael Wright227c5542020-07-02 18:30:52 +01001303 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001304 std::string coverageCalibrationString;
1305 if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001306 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001307 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001308 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001309 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001310 } else if (coverageCalibrationString != "default") {
1311 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001312 coverageCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001313 }
1314 }
1315}
1316
1317void TouchInputMapper::resolveCalibration() {
1318 // Size
1319 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001320 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1321 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001322 }
1323 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001324 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001325 }
1326
1327 // Pressure
1328 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001329 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1330 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001331 }
1332 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001333 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001334 }
1335
1336 // Orientation
1337 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001338 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1339 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001340 }
1341 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001342 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001343 }
1344
1345 // Distance
1346 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001347 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1348 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001349 }
1350 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001351 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001352 }
1353
1354 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001355 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1356 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001357 }
1358}
1359
1360void TouchInputMapper::dumpCalibration(std::string& dump) {
1361 dump += INDENT3 "Calibration:\n";
1362
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001363 dump += INDENT4 "touch.size.calibration: ";
1364 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001365
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001366 if (mCalibration.sizeScale) {
1367 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001368 }
1369
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001370 if (mCalibration.sizeBias) {
1371 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001372 }
1373
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001374 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001375 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001376 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001377 }
1378
1379 // Pressure
1380 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001381 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001382 dump += INDENT4 "touch.pressure.calibration: none\n";
1383 break;
Michael Wright227c5542020-07-02 18:30:52 +01001384 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001385 dump += INDENT4 "touch.pressure.calibration: physical\n";
1386 break;
Michael Wright227c5542020-07-02 18:30:52 +01001387 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001388 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1389 break;
1390 default:
1391 ALOG_ASSERT(false);
1392 }
1393
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001394 if (mCalibration.pressureScale) {
1395 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001396 }
1397
1398 // Orientation
1399 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001400 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001401 dump += INDENT4 "touch.orientation.calibration: none\n";
1402 break;
Michael Wright227c5542020-07-02 18:30:52 +01001403 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001404 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1405 break;
Michael Wright227c5542020-07-02 18:30:52 +01001406 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001407 dump += INDENT4 "touch.orientation.calibration: vector\n";
1408 break;
1409 default:
1410 ALOG_ASSERT(false);
1411 }
1412
1413 // Distance
1414 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001415 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001416 dump += INDENT4 "touch.distance.calibration: none\n";
1417 break;
Michael Wright227c5542020-07-02 18:30:52 +01001418 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001419 dump += INDENT4 "touch.distance.calibration: scaled\n";
1420 break;
1421 default:
1422 ALOG_ASSERT(false);
1423 }
1424
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001425 if (mCalibration.distanceScale) {
1426 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001427 }
1428
1429 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001430 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001431 dump += INDENT4 "touch.coverage.calibration: none\n";
1432 break;
Michael Wright227c5542020-07-02 18:30:52 +01001433 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001434 dump += INDENT4 "touch.coverage.calibration: box\n";
1435 break;
1436 default:
1437 ALOG_ASSERT(false);
1438 }
1439}
1440
1441void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1442 dump += INDENT3 "Affine Transformation:\n";
1443
1444 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1445 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1446 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1447 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1448 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1449 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1450}
1451
1452void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001453 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001454 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001455}
1456
1457void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001458 mCursorButtonAccumulator.reset(getDeviceContext());
1459 mCursorScrollAccumulator.reset(getDeviceContext());
1460 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001461
1462 mPointerVelocityControl.reset();
1463 mWheelXVelocityControl.reset();
1464 mWheelYVelocityControl.reset();
1465
1466 mRawStatesPending.clear();
1467 mCurrentRawState.clear();
1468 mCurrentCookedState.clear();
1469 mLastRawState.clear();
1470 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001471 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001472 mSentHoverEnter = false;
1473 mHavePointerIds = false;
1474 mCurrentMotionAborted = false;
1475 mDownTime = 0;
1476
1477 mCurrentVirtualKey.down = false;
1478
1479 mPointerGesture.reset();
1480 mPointerSimple.reset();
1481 resetExternalStylus();
1482
1483 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001484 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001485 mPointerController->clearSpots();
1486 }
1487
1488 InputMapper::reset(when);
1489}
1490
1491void TouchInputMapper::resetExternalStylus() {
1492 mExternalStylusState.clear();
1493 mExternalStylusId = -1;
1494 mExternalStylusFusionTimeout = LLONG_MAX;
1495 mExternalStylusDataPending = false;
1496}
1497
1498void TouchInputMapper::clearStylusDataPendingFlags() {
1499 mExternalStylusDataPending = false;
1500 mExternalStylusFusionTimeout = LLONG_MAX;
1501}
1502
1503void TouchInputMapper::process(const RawEvent* rawEvent) {
1504 mCursorButtonAccumulator.process(rawEvent);
1505 mCursorScrollAccumulator.process(rawEvent);
1506 mTouchButtonAccumulator.process(rawEvent);
1507
1508 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001509 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001510 }
1511}
1512
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001513void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001514 // Push a new state.
1515 mRawStatesPending.emplace_back();
1516
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001517 RawState& next = mRawStatesPending.back();
1518 next.clear();
1519 next.when = when;
1520 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001521
1522 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001523 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001524 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1525
1526 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001527 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1528 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001529 mCursorScrollAccumulator.finishSync();
1530
1531 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001532 syncTouch(when, &next);
1533
1534 // The last RawState is the actually second to last, since we just added a new state
1535 const RawState& last =
1536 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001537
1538 // Assign pointer ids.
1539 if (!mHavePointerIds) {
1540 assignPointerIds(last, next);
1541 }
1542
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001543 if (DEBUG_RAW_EVENTS) {
1544 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1545 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1546 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1547 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1548 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1549 next.rawPointerData.canceledIdBits.value);
1550 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001551
Arthur Hung9ad18942021-06-19 02:04:46 +00001552 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1553 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1554 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1555 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1556 next.rawPointerData.hoveringIdBits.value);
1557 }
1558
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001559 processRawTouches(false /*timeout*/);
1560}
1561
1562void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001563 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001564 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001565 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001566 mCurrentCookedState.clear();
1567 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001568 return;
1569 }
1570
1571 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1572 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1573 // touching the current state will only observe the events that have been dispatched to the
1574 // rest of the pipeline.
1575 const size_t N = mRawStatesPending.size();
1576 size_t count;
1577 for (count = 0; count < N; count++) {
1578 const RawState& next = mRawStatesPending[count];
1579
1580 // A failure to assign the stylus id means that we're waiting on stylus data
1581 // and so should defer the rest of the pipeline.
1582 if (assignExternalStylusId(next, timeout)) {
1583 break;
1584 }
1585
1586 // All ready to go.
1587 clearStylusDataPendingFlags();
1588 mCurrentRawState.copyFrom(next);
1589 if (mCurrentRawState.when < mLastRawState.when) {
1590 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001591 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001592 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001593 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001594 }
1595 if (count != 0) {
1596 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1597 }
1598
1599 if (mExternalStylusDataPending) {
1600 if (timeout) {
1601 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1602 clearStylusDataPendingFlags();
1603 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001604 if (DEBUG_STYLUS_FUSION) {
1605 ALOGD("Timeout expired, synthesizing event with new stylus data");
1606 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001607 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1608 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001609 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1610 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1611 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1612 }
1613 }
1614}
1615
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001616void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001617 // Always start with a clean state.
1618 mCurrentCookedState.clear();
1619
1620 // Apply stylus buttons to current raw state.
1621 applyExternalStylusButtonState(when);
1622
1623 // Handle policy on initial down or hover events.
1624 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1625 mCurrentRawState.rawPointerData.pointerCount != 0;
1626
1627 uint32_t policyFlags = 0;
1628 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1629 if (initialDown || buttonsPressed) {
1630 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001631 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001632 getContext()->fadePointer();
1633 }
1634
1635 if (mParameters.wake) {
1636 policyFlags |= POLICY_FLAG_WAKE;
1637 }
1638 }
1639
1640 // Consume raw off-screen touches before cooking pointer data.
1641 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001642 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001643 mCurrentRawState.rawPointerData.clear();
1644 }
1645
1646 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1647 // with cooked pointer data that has the same ids and indices as the raw data.
1648 // The following code can use either the raw or cooked data, as needed.
1649 cookPointerData();
1650
1651 // Apply stylus pressure to current cooked state.
1652 applyExternalStylusTouchState(when);
1653
1654 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001655 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1656 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001657 mCurrentCookedState.buttonState);
1658
1659 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001660 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001661 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1662 uint32_t id = idBits.clearFirstMarkedBit();
1663 const RawPointerData::Pointer& pointer =
1664 mCurrentRawState.rawPointerData.pointerForId(id);
1665 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1666 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1667 mCurrentCookedState.stylusIdBits.markBit(id);
1668 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1669 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1670 mCurrentCookedState.fingerIdBits.markBit(id);
1671 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1672 mCurrentCookedState.mouseIdBits.markBit(id);
1673 }
1674 }
1675 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1676 uint32_t id = idBits.clearFirstMarkedBit();
1677 const RawPointerData::Pointer& pointer =
1678 mCurrentRawState.rawPointerData.pointerForId(id);
1679 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1680 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1681 mCurrentCookedState.stylusIdBits.markBit(id);
1682 }
1683 }
1684
1685 // Stylus takes precedence over all tools, then mouse, then finger.
1686 PointerUsage pointerUsage = mPointerUsage;
1687 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1688 mCurrentCookedState.mouseIdBits.clear();
1689 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001690 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001691 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1692 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001693 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001694 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1695 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001696 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001697 }
1698
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001699 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001700 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001701 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001702 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001703 dispatchButtonRelease(when, readTime, policyFlags);
1704 dispatchHoverExit(when, readTime, policyFlags);
1705 dispatchTouches(when, readTime, policyFlags);
1706 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1707 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001708 }
1709
1710 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1711 mCurrentMotionAborted = false;
1712 }
1713 }
1714
1715 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001716 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001717 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1718 mCurrentCookedState.buttonState);
1719
1720 // Clear some transient state.
1721 mCurrentRawState.rawVScroll = 0;
1722 mCurrentRawState.rawHScroll = 0;
1723
1724 // Copy current touch to last touch in preparation for the next cycle.
1725 mLastRawState.copyFrom(mCurrentRawState);
1726 mLastCookedState.copyFrom(mCurrentCookedState);
1727}
1728
Garfield Tanc734e4f2021-01-15 20:01:39 -08001729void TouchInputMapper::updateTouchSpots() {
1730 if (!mConfig.showTouches || mPointerController == nullptr) {
1731 return;
1732 }
1733
1734 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1735 // clear touch spots.
1736 if (mDeviceMode != DeviceMode::DIRECT &&
1737 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1738 return;
1739 }
1740
1741 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1742 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1743
1744 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001745 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1746 mCurrentCookedState.cookedPointerData.idToIndex,
1747 mCurrentCookedState.cookedPointerData.touchingIdBits,
1748 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001749}
1750
1751bool TouchInputMapper::isTouchScreen() {
1752 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1753 mParameters.hasAssociatedDisplay;
1754}
1755
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001756void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001757 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001758 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1759 }
1760}
1761
1762void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1763 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1764 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1765
1766 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1767 float pressure = mExternalStylusState.pressure;
1768 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1769 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1770 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1771 }
1772 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1773 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1774
1775 PointerProperties& properties =
1776 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1777 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1778 properties.toolType = mExternalStylusState.toolType;
1779 }
1780 }
1781}
1782
1783bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001784 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001785 return false;
1786 }
1787
1788 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1789 state.rawPointerData.pointerCount != 0;
1790 if (initialDown) {
1791 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001792 if (DEBUG_STYLUS_FUSION) {
1793 ALOGD("Have both stylus and touch data, beginning fusion");
1794 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001795 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1796 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001797 if (DEBUG_STYLUS_FUSION) {
1798 ALOGD("Timeout expired, assuming touch is not a stylus.");
1799 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001800 resetExternalStylus();
1801 } else {
1802 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1803 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1804 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001805 if (DEBUG_STYLUS_FUSION) {
1806 ALOGD("No stylus data but stylus is connected, requesting timeout "
1807 "(%" PRId64 "ms)",
1808 mExternalStylusFusionTimeout);
1809 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001810 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1811 return true;
1812 }
1813 }
1814
1815 // Check if the stylus pointer has gone up.
1816 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001817 if (DEBUG_STYLUS_FUSION) {
1818 ALOGD("Stylus pointer is going up");
1819 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001820 mExternalStylusId = -1;
1821 }
1822
1823 return false;
1824}
1825
1826void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001827 if (mDeviceMode == DeviceMode::POINTER) {
1828 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001829 // Since this is a synthetic event, we can consider its latency to be zero
1830 const nsecs_t readTime = when;
1831 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001832 }
Michael Wright227c5542020-07-02 18:30:52 +01001833 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001834 if (mExternalStylusFusionTimeout < when) {
1835 processRawTouches(true /*timeout*/);
1836 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1837 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1838 }
1839 }
1840}
1841
1842void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1843 mExternalStylusState.copyFrom(state);
1844 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1845 // We're either in the middle of a fused stream of data or we're waiting on data before
1846 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1847 // data.
1848 mExternalStylusDataPending = true;
1849 processRawTouches(false /*timeout*/);
1850 }
1851}
1852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001853bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001854 // Check for release of a virtual key.
1855 if (mCurrentVirtualKey.down) {
1856 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1857 // Pointer went up while virtual key was down.
1858 mCurrentVirtualKey.down = false;
1859 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001860 if (DEBUG_VIRTUAL_KEYS) {
1861 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1862 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1863 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001864 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001865 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1866 }
1867 return true;
1868 }
1869
1870 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1871 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1872 const RawPointerData::Pointer& pointer =
1873 mCurrentRawState.rawPointerData.pointerForId(id);
1874 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1875 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1876 // Pointer is still within the space of the virtual key.
1877 return true;
1878 }
1879 }
1880
1881 // Pointer left virtual key area or another pointer also went down.
1882 // Send key cancellation but do not consume the touch yet.
1883 // This is useful when the user swipes through from the virtual key area
1884 // into the main display surface.
1885 mCurrentVirtualKey.down = false;
1886 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001887 if (DEBUG_VIRTUAL_KEYS) {
1888 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1889 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1890 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001891 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001892 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1893 AKEY_EVENT_FLAG_CANCELED);
1894 }
1895 }
1896
1897 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1898 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1899 // Pointer just went down. Check for virtual key press or off-screen touches.
1900 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1901 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001902 // Skip checking whether the pointer is inside the physical frame if the device is in
1903 // unscaled mode.
1904 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1905 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001906 // If exactly one pointer went down, check for virtual key hit.
1907 // Otherwise we will drop the entire stroke.
1908 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1909 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1910 if (virtualKey) {
1911 mCurrentVirtualKey.down = true;
1912 mCurrentVirtualKey.downTime = when;
1913 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1914 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1915 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001916 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1917 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001918
1919 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001920 if (DEBUG_VIRTUAL_KEYS) {
1921 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1922 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1923 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001924 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001925 AKEY_EVENT_FLAG_FROM_SYSTEM |
1926 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1927 }
1928 }
1929 }
1930 return true;
1931 }
1932 }
1933
1934 // Disable all virtual key touches that happen within a short time interval of the
1935 // most recent touch within the screen area. The idea is to filter out stray
1936 // virtual key presses when interacting with the touch screen.
1937 //
1938 // Problems we're trying to solve:
1939 //
1940 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1941 // virtual key area that is implemented by a separate touch panel and accidentally
1942 // triggers a virtual key.
1943 //
1944 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1945 // area and accidentally triggers a virtual key. This often happens when virtual keys
1946 // are layed out below the screen near to where the on screen keyboard's space bar
1947 // is displayed.
1948 if (mConfig.virtualKeyQuietTime > 0 &&
1949 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001950 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001951 }
1952 return false;
1953}
1954
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001955void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001956 int32_t keyEventAction, int32_t keyEventFlags) {
1957 int32_t keyCode = mCurrentVirtualKey.keyCode;
1958 int32_t scanCode = mCurrentVirtualKey.scanCode;
1959 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001960 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001961 policyFlags |= POLICY_FLAG_VIRTUAL;
1962
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001963 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1964 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1965 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001966 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001967}
1968
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001969void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
lilinnan687e58f2022-07-19 16:00:50 +08001970 if (mCurrentMotionAborted) {
1971 // Current motion event was already aborted.
1972 return;
1973 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001974 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1975 if (!currentIdBits.isEmpty()) {
1976 int32_t metaState = getContext()->getGlobalMetaState();
1977 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001978 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1979 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001980 mCurrentCookedState.cookedPointerData.pointerProperties,
1981 mCurrentCookedState.cookedPointerData.pointerCoords,
1982 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1983 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1984 mCurrentMotionAborted = true;
1985 }
1986}
1987
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001988void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001989 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1990 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1991 int32_t metaState = getContext()->getGlobalMetaState();
1992 int32_t buttonState = mCurrentCookedState.buttonState;
1993
1994 if (currentIdBits == lastIdBits) {
1995 if (!currentIdBits.isEmpty()) {
1996 // No pointer id changes so this is a move event.
1997 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1999 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002000 mCurrentCookedState.cookedPointerData.pointerProperties,
2001 mCurrentCookedState.cookedPointerData.pointerCoords,
2002 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
2003 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2004 }
2005 } else {
2006 // There may be pointers going up and pointers going down and pointers moving
2007 // all at the same time.
2008 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
2009 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
2010 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2011 BitSet32 dispatchedIdBits(lastIdBits.value);
2012
2013 // Update last coordinates of pointers that have moved so that we observe the new
2014 // pointer positions at the same time as other pointers that have just gone up.
2015 bool moveNeeded =
2016 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
2017 mCurrentCookedState.cookedPointerData.pointerCoords,
2018 mCurrentCookedState.cookedPointerData.idToIndex,
2019 mLastCookedState.cookedPointerData.pointerProperties,
2020 mLastCookedState.cookedPointerData.pointerCoords,
2021 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
2022 if (buttonState != mLastCookedState.buttonState) {
2023 moveNeeded = true;
2024 }
2025
2026 // Dispatch pointer up events.
2027 while (!upIdBits.isEmpty()) {
2028 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002029 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002030 if (isCanceled) {
2031 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2032 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002033 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08002034 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002035 mLastCookedState.cookedPointerData.pointerProperties,
2036 mLastCookedState.cookedPointerData.pointerCoords,
2037 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
2038 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2039 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002040 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002041 }
2042
2043 // Dispatch move events if any of the remaining pointers moved from their old locations.
2044 // Although applications receive new locations as part of individual pointer up
2045 // events, they do not generally handle them except when presented in a move event.
2046 if (moveNeeded && !moveIdBits.isEmpty()) {
2047 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002048 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2049 metaState, buttonState, 0,
2050 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002051 mCurrentCookedState.cookedPointerData.pointerCoords,
2052 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2053 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2054 }
2055
2056 // Dispatch pointer down events using the new pointer locations.
2057 while (!downIdBits.isEmpty()) {
2058 uint32_t downId = downIdBits.clearFirstMarkedBit();
2059 dispatchedIdBits.markBit(downId);
2060
2061 if (dispatchedIdBits.count() == 1) {
2062 // First pointer is going down. Set down time.
2063 mDownTime = when;
2064 }
2065
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002066 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2067 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002068 mCurrentCookedState.cookedPointerData.pointerProperties,
2069 mCurrentCookedState.cookedPointerData.pointerCoords,
2070 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2071 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2072 }
2073 }
2074}
2075
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002076void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002077 if (mSentHoverEnter &&
2078 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2079 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2080 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002081 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2082 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002083 mLastCookedState.cookedPointerData.pointerProperties,
2084 mLastCookedState.cookedPointerData.pointerCoords,
2085 mLastCookedState.cookedPointerData.idToIndex,
2086 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2087 mOrientedYPrecision, mDownTime);
2088 mSentHoverEnter = false;
2089 }
2090}
2091
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002092void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2093 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002094 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2095 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2096 int32_t metaState = getContext()->getGlobalMetaState();
2097 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002098 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2099 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002100 mCurrentCookedState.cookedPointerData.pointerProperties,
2101 mCurrentCookedState.cookedPointerData.pointerCoords,
2102 mCurrentCookedState.cookedPointerData.idToIndex,
2103 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2104 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2105 mSentHoverEnter = true;
2106 }
2107
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002108 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2109 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002110 mCurrentCookedState.cookedPointerData.pointerProperties,
2111 mCurrentCookedState.cookedPointerData.pointerCoords,
2112 mCurrentCookedState.cookedPointerData.idToIndex,
2113 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2114 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2115 }
2116}
2117
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002118void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002119 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2120 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2121 const int32_t metaState = getContext()->getGlobalMetaState();
2122 int32_t buttonState = mLastCookedState.buttonState;
2123 while (!releasedButtons.isEmpty()) {
2124 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2125 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002126 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002127 actionButton, 0, metaState, buttonState, 0,
2128 mCurrentCookedState.cookedPointerData.pointerProperties,
2129 mCurrentCookedState.cookedPointerData.pointerCoords,
2130 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2131 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2132 }
2133}
2134
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002135void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002136 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2137 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2138 const int32_t metaState = getContext()->getGlobalMetaState();
2139 int32_t buttonState = mLastCookedState.buttonState;
2140 while (!pressedButtons.isEmpty()) {
2141 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2142 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002143 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2144 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002145 mCurrentCookedState.cookedPointerData.pointerProperties,
2146 mCurrentCookedState.cookedPointerData.pointerCoords,
2147 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2148 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2149 }
2150}
2151
2152const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2153 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2154 return cookedPointerData.touchingIdBits;
2155 }
2156 return cookedPointerData.hoveringIdBits;
2157}
2158
2159void TouchInputMapper::cookPointerData() {
2160 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2161
2162 mCurrentCookedState.cookedPointerData.clear();
2163 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2164 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2165 mCurrentRawState.rawPointerData.hoveringIdBits;
2166 mCurrentCookedState.cookedPointerData.touchingIdBits =
2167 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002168 mCurrentCookedState.cookedPointerData.canceledIdBits =
2169 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002170
2171 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2172 mCurrentCookedState.buttonState = 0;
2173 } else {
2174 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2175 }
2176
2177 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002178 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002179 for (uint32_t i = 0; i < currentPointerCount; i++) {
2180 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2181
2182 // Size
2183 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2184 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002185 case Calibration::SizeCalibration::GEOMETRIC:
2186 case Calibration::SizeCalibration::DIAMETER:
2187 case Calibration::SizeCalibration::BOX:
2188 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002189 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2190 touchMajor = in.touchMajor;
2191 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2192 toolMajor = in.toolMajor;
2193 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2194 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2195 : in.touchMajor;
2196 } else if (mRawPointerAxes.touchMajor.valid) {
2197 toolMajor = touchMajor = in.touchMajor;
2198 toolMinor = touchMinor =
2199 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2200 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2201 : in.touchMajor;
2202 } else if (mRawPointerAxes.toolMajor.valid) {
2203 touchMajor = toolMajor = in.toolMajor;
2204 touchMinor = toolMinor =
2205 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2206 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2207 : in.toolMajor;
2208 } else {
2209 ALOG_ASSERT(false,
2210 "No touch or tool axes. "
2211 "Size calibration should have been resolved to NONE.");
2212 touchMajor = 0;
2213 touchMinor = 0;
2214 toolMajor = 0;
2215 toolMinor = 0;
2216 size = 0;
2217 }
2218
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002219 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002220 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2221 if (touchingCount > 1) {
2222 touchMajor /= touchingCount;
2223 touchMinor /= touchingCount;
2224 toolMajor /= touchingCount;
2225 toolMinor /= touchingCount;
2226 size /= touchingCount;
2227 }
2228 }
2229
Michael Wright227c5542020-07-02 18:30:52 +01002230 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002231 touchMajor *= mGeometricScale;
2232 touchMinor *= mGeometricScale;
2233 toolMajor *= mGeometricScale;
2234 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002235 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002236 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2237 touchMinor = touchMajor;
2238 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2239 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002240 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002241 touchMinor = touchMajor;
2242 toolMinor = toolMajor;
2243 }
2244
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002245 mCalibration.applySizeScaleAndBias(touchMajor);
2246 mCalibration.applySizeScaleAndBias(touchMinor);
2247 mCalibration.applySizeScaleAndBias(toolMajor);
2248 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002249 size *= mSizeScale;
2250 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002251 case Calibration::SizeCalibration::DEFAULT:
2252 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2253 break;
2254 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002255 touchMajor = 0;
2256 touchMinor = 0;
2257 toolMajor = 0;
2258 toolMinor = 0;
2259 size = 0;
2260 break;
2261 }
2262
2263 // Pressure
2264 float pressure;
2265 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002266 case Calibration::PressureCalibration::PHYSICAL:
2267 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002268 pressure = in.pressure * mPressureScale;
2269 break;
2270 default:
2271 pressure = in.isHovering ? 0 : 1;
2272 break;
2273 }
2274
2275 // Tilt and Orientation
2276 float tilt;
2277 float orientation;
2278 if (mHaveTilt) {
2279 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2280 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2281 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2282 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2283 } else {
2284 tilt = 0;
2285
2286 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002287 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002288 orientation = in.orientation * mOrientationScale;
2289 break;
Michael Wright227c5542020-07-02 18:30:52 +01002290 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002291 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2292 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2293 if (c1 != 0 || c2 != 0) {
2294 orientation = atan2f(c1, c2) * 0.5f;
2295 float confidence = hypotf(c1, c2);
2296 float scale = 1.0f + confidence / 16.0f;
2297 touchMajor *= scale;
2298 touchMinor /= scale;
2299 toolMajor *= scale;
2300 toolMinor /= scale;
2301 } else {
2302 orientation = 0;
2303 }
2304 break;
2305 }
2306 default:
2307 orientation = 0;
2308 }
2309 }
2310
2311 // Distance
2312 float distance;
2313 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002314 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002315 distance = in.distance * mDistanceScale;
2316 break;
2317 default:
2318 distance = 0;
2319 }
2320
2321 // Coverage
2322 int32_t rawLeft, rawTop, rawRight, rawBottom;
2323 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002324 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002325 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2326 rawRight = in.toolMinor & 0x0000ffff;
2327 rawBottom = in.toolMajor & 0x0000ffff;
2328 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2329 break;
2330 default:
2331 rawLeft = rawTop = rawRight = rawBottom = 0;
2332 break;
2333 }
2334
2335 // Adjust X,Y coords for device calibration
2336 // TODO: Adjust coverage coords?
2337 float xTransformed = in.x, yTransformed = in.y;
2338 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002339 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002340
Prabir Pradhan1728b212021-10-19 16:00:03 -07002341 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002342 float left, top, right, bottom;
2343
Prabir Pradhan1728b212021-10-19 16:00:03 -07002344 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002345 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002346 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2347 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2348 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2349 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002350 orientation -= M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002351 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002352 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002353 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002354 }
2355 break;
2356 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002357 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2358 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002359 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2360 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002361 orientation -= M_PI;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002362 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002363 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002364 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002365 }
2366 break;
2367 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002368 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2369 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002370 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2371 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002372 orientation += M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002373 if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002374 orientation -=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002375 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002376 }
2377 break;
2378 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002379 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2380 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2381 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2382 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002383 break;
2384 }
2385
2386 // Write output coords.
2387 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2388 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002389 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2390 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002391 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2392 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2393 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2394 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2395 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2396 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2397 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002398 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002399 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2400 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2401 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2402 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2403 } else {
2404 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2405 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2406 }
2407
Chris Ye364fdb52020-08-05 15:07:56 -07002408 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002409 uint32_t id = in.id;
2410 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2411 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2412 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2413 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2414 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2415 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2416 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2417 }
2418
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002419 // Write output properties.
2420 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002421 properties.clear();
2422 properties.id = id;
2423 properties.toolType = in.toolType;
2424
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002425 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002426 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002427 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002428 }
2429}
2430
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002431void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002432 PointerUsage pointerUsage) {
2433 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002434 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002435 mPointerUsage = pointerUsage;
2436 }
2437
2438 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002439 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002440 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002441 break;
Michael Wright227c5542020-07-02 18:30:52 +01002442 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002443 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002444 break;
Michael Wright227c5542020-07-02 18:30:52 +01002445 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002446 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002447 break;
Michael Wright227c5542020-07-02 18:30:52 +01002448 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002449 break;
2450 }
2451}
2452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002453void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002454 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002455 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002456 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002457 break;
Michael Wright227c5542020-07-02 18:30:52 +01002458 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002459 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002460 break;
Michael Wright227c5542020-07-02 18:30:52 +01002461 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002462 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002463 break;
Michael Wright227c5542020-07-02 18:30:52 +01002464 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002465 break;
2466 }
2467
Michael Wright227c5542020-07-02 18:30:52 +01002468 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002469}
2470
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002471void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2472 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002473 // Update current gesture coordinates.
2474 bool cancelPreviousGesture, finishPreviousGesture;
2475 bool sendEvents =
2476 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2477 if (!sendEvents) {
2478 return;
2479 }
2480 if (finishPreviousGesture) {
2481 cancelPreviousGesture = false;
2482 }
2483
2484 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002485 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002486 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002487 if (finishPreviousGesture || cancelPreviousGesture) {
2488 mPointerController->clearSpots();
2489 }
2490
Michael Wright227c5542020-07-02 18:30:52 +01002491 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002492 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2493 mPointerGesture.currentGestureIdToIndex,
2494 mPointerGesture.currentGestureIdBits,
2495 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002496 }
2497 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002498 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002499 }
2500
2501 // Show or hide the pointer if needed.
2502 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002503 case PointerGesture::Mode::NEUTRAL:
2504 case PointerGesture::Mode::QUIET:
2505 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2506 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002507 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002508 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002509 }
2510 break;
Michael Wright227c5542020-07-02 18:30:52 +01002511 case PointerGesture::Mode::TAP:
2512 case PointerGesture::Mode::TAP_DRAG:
2513 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2514 case PointerGesture::Mode::HOVER:
2515 case PointerGesture::Mode::PRESS:
2516 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002517 // Unfade the pointer when the current gesture manipulates the
2518 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002519 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002520 break;
Michael Wright227c5542020-07-02 18:30:52 +01002521 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002522 // Fade the pointer when the current gesture manipulates a different
2523 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002524 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002525 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002526 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002527 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002528 }
2529 break;
2530 }
2531
2532 // Send events!
2533 int32_t metaState = getContext()->getGlobalMetaState();
2534 int32_t buttonState = mCurrentCookedState.buttonState;
2535
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002536 uint32_t flags = 0;
2537
2538 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2539 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2540 }
2541
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002542 // Update last coordinates of pointers that have moved so that we observe the new
2543 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002544 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2545 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2546 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2547 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2548 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2549 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002550 bool moveNeeded = false;
2551 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2552 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2553 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2554 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2555 mPointerGesture.lastGestureIdBits.value);
2556 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2557 mPointerGesture.currentGestureCoords,
2558 mPointerGesture.currentGestureIdToIndex,
2559 mPointerGesture.lastGestureProperties,
2560 mPointerGesture.lastGestureCoords,
2561 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2562 if (buttonState != mLastCookedState.buttonState) {
2563 moveNeeded = true;
2564 }
2565 }
2566
2567 // Send motion events for all pointers that went up or were canceled.
2568 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2569 if (!dispatchedGestureIdBits.isEmpty()) {
2570 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002571 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2572 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002573 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2574 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2575 mPointerGesture.downTime);
2576
2577 dispatchedGestureIdBits.clear();
2578 } else {
2579 BitSet32 upGestureIdBits;
2580 if (finishPreviousGesture) {
2581 upGestureIdBits = dispatchedGestureIdBits;
2582 } else {
2583 upGestureIdBits.value =
2584 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2585 }
2586 while (!upGestureIdBits.isEmpty()) {
2587 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2588
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002589 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002590 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002591 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002592 mPointerGesture.lastGestureCoords,
2593 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2594 0, mPointerGesture.downTime);
2595
2596 dispatchedGestureIdBits.clearBit(id);
2597 }
2598 }
2599 }
2600
2601 // Send motion events for all pointers that moved.
2602 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002603 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002604 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002605 mPointerGesture.currentGestureProperties,
2606 mPointerGesture.currentGestureCoords,
2607 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2608 mPointerGesture.downTime);
2609 }
2610
2611 // Send motion events for all pointers that went down.
2612 if (down) {
2613 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2614 ~dispatchedGestureIdBits.value);
2615 while (!downGestureIdBits.isEmpty()) {
2616 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2617 dispatchedGestureIdBits.markBit(id);
2618
2619 if (dispatchedGestureIdBits.count() == 1) {
2620 mPointerGesture.downTime = when;
2621 }
2622
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002623 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002624 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002625 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002626 mPointerGesture.currentGestureCoords,
2627 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2628 0, mPointerGesture.downTime);
2629 }
2630 }
2631
2632 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002633 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002634 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2635 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002636 mPointerGesture.currentGestureProperties,
2637 mPointerGesture.currentGestureCoords,
2638 mPointerGesture.currentGestureIdToIndex,
2639 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2640 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2641 // Synthesize a hover move event after all pointers go up to indicate that
2642 // the pointer is hovering again even if the user is not currently touching
2643 // the touch pad. This ensures that a view will receive a fresh hover enter
2644 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002645 float x, y;
2646 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002647
2648 PointerProperties pointerProperties;
2649 pointerProperties.clear();
2650 pointerProperties.id = 0;
2651 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2652
2653 PointerCoords pointerCoords;
2654 pointerCoords.clear();
2655 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2656 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2657
2658 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002659 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002660 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002661 metaState, buttonState, MotionClassification::NONE,
2662 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2663 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002664 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002665 }
2666
2667 // Update state.
2668 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2669 if (!down) {
2670 mPointerGesture.lastGestureIdBits.clear();
2671 } else {
2672 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2673 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2674 uint32_t id = idBits.clearFirstMarkedBit();
2675 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2676 mPointerGesture.lastGestureProperties[index].copyFrom(
2677 mPointerGesture.currentGestureProperties[index]);
2678 mPointerGesture.lastGestureCoords[index].copyFrom(
2679 mPointerGesture.currentGestureCoords[index]);
2680 mPointerGesture.lastGestureIdToIndex[id] = index;
2681 }
2682 }
2683}
2684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002685void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002686 // Cancel previously dispatches pointers.
2687 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2688 int32_t metaState = getContext()->getGlobalMetaState();
2689 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002690 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2691 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002692 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2693 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2694 0, 0, mPointerGesture.downTime);
2695 }
2696
2697 // Reset the current pointer gesture.
2698 mPointerGesture.reset();
2699 mPointerVelocityControl.reset();
2700
2701 // Remove any current spots.
2702 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002703 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002704 mPointerController->clearSpots();
2705 }
2706}
2707
2708bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2709 bool* outFinishPreviousGesture, bool isTimeout) {
2710 *outCancelPreviousGesture = false;
2711 *outFinishPreviousGesture = false;
2712
2713 // Handle TAP timeout.
2714 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002715 if (DEBUG_GESTURES) {
2716 ALOGD("Gestures: Processing timeout");
2717 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002718
Michael Wright227c5542020-07-02 18:30:52 +01002719 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002720 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2721 // The tap/drag timeout has not yet expired.
2722 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2723 mConfig.pointerGestureTapDragInterval);
2724 } else {
2725 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002726 if (DEBUG_GESTURES) {
2727 ALOGD("Gestures: TAP finished");
2728 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002729 *outFinishPreviousGesture = true;
2730
2731 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002732 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002733 mPointerGesture.currentGestureIdBits.clear();
2734
2735 mPointerVelocityControl.reset();
2736 return true;
2737 }
2738 }
2739
2740 // We did not handle this timeout.
2741 return false;
2742 }
2743
2744 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2745 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2746
2747 // Update the velocity tracker.
2748 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002749 std::vector<VelocityTracker::Position> positions;
2750 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002751 uint32_t id = idBits.clearFirstMarkedBit();
2752 const RawPointerData::Pointer& pointer =
2753 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002754 float x = pointer.x * mPointerXMovementScale;
2755 float y = pointer.y * mPointerYMovementScale;
2756 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002757 }
2758 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2759 positions);
2760 }
2761
2762 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2763 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002764 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2765 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2766 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002767 mPointerGesture.resetTap();
2768 }
2769
2770 // Pick a new active touch id if needed.
2771 // Choose an arbitrary pointer that just went down, if there is one.
2772 // Otherwise choose an arbitrary remaining pointer.
2773 // This guarantees we always have an active touch id when there is at least one pointer.
2774 // We keep the same active touch id for as long as possible.
2775 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2776 int32_t activeTouchId = lastActiveTouchId;
2777 if (activeTouchId < 0) {
2778 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2779 activeTouchId = mPointerGesture.activeTouchId =
2780 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2781 mPointerGesture.firstTouchTime = when;
2782 }
2783 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2784 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2785 activeTouchId = mPointerGesture.activeTouchId =
2786 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2787 } else {
2788 activeTouchId = mPointerGesture.activeTouchId = -1;
2789 }
2790 }
2791
2792 // Determine whether we are in quiet time.
2793 bool isQuietTime = false;
2794 if (activeTouchId < 0) {
2795 mPointerGesture.resetQuietTime();
2796 } else {
2797 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2798 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002799 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2800 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2801 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002802 currentFingerCount < 2) {
2803 // Enter quiet time when exiting swipe or freeform state.
2804 // This is to prevent accidentally entering the hover state and flinging the
2805 // pointer when finishing a swipe and there is still one pointer left onscreen.
2806 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002807 } else if (mPointerGesture.lastGestureMode ==
2808 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002809 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2810 // Enter quiet time when releasing the button and there are still two or more
2811 // fingers down. This may indicate that one finger was used to press the button
2812 // but it has not gone up yet.
2813 isQuietTime = true;
2814 }
2815 if (isQuietTime) {
2816 mPointerGesture.quietTime = when;
2817 }
2818 }
2819 }
2820
2821 // Switch states based on button and pointer state.
2822 if (isQuietTime) {
2823 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002824 if (DEBUG_GESTURES) {
2825 ALOGD("Gestures: QUIET for next %0.3fms",
2826 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2827 0.000001f);
2828 }
Michael Wright227c5542020-07-02 18:30:52 +01002829 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002830 *outFinishPreviousGesture = true;
2831 }
2832
2833 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002834 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002835 mPointerGesture.currentGestureIdBits.clear();
2836
2837 mPointerVelocityControl.reset();
2838 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2839 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2840 // The pointer follows the active touch point.
2841 // Emit DOWN, MOVE, UP events at the pointer location.
2842 //
2843 // Only the active touch matters; other fingers are ignored. This policy helps
2844 // to handle the case where the user places a second finger on the touch pad
2845 // to apply the necessary force to depress an integrated button below the surface.
2846 // We don't want the second finger to be delivered to applications.
2847 //
2848 // For this to work well, we need to make sure to track the pointer that is really
2849 // active. If the user first puts one finger down to click then adds another
2850 // finger to drag then the active pointer should switch to the finger that is
2851 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002852 if (DEBUG_GESTURES) {
2853 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2854 "currentFingerCount=%d",
2855 activeTouchId, currentFingerCount);
2856 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002857 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002858 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002859 *outFinishPreviousGesture = true;
2860 mPointerGesture.activeGestureId = 0;
2861 }
2862
2863 // Switch pointers if needed.
2864 // Find the fastest pointer and follow it.
2865 if (activeTouchId >= 0 && currentFingerCount > 1) {
2866 int32_t bestId = -1;
2867 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2868 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2869 uint32_t id = idBits.clearFirstMarkedBit();
2870 float vx, vy;
2871 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2872 float speed = hypotf(vx, vy);
2873 if (speed > bestSpeed) {
2874 bestId = id;
2875 bestSpeed = speed;
2876 }
2877 }
2878 }
2879 if (bestId >= 0 && bestId != activeTouchId) {
2880 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002881 if (DEBUG_GESTURES) {
2882 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2883 "bestId=%d, bestSpeed=%0.3f",
2884 bestId, bestSpeed);
2885 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002886 }
2887 }
2888
2889 float deltaX = 0, deltaY = 0;
2890 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2891 const RawPointerData::Pointer& currentPointer =
2892 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2893 const RawPointerData::Pointer& lastPointer =
2894 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2895 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2896 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2897
Prabir Pradhan1728b212021-10-19 16:00:03 -07002898 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002899 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2900
2901 // Move the pointer using a relative motion.
2902 // When using spots, the click will occur at the position of the anchor
2903 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002904 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002905 } else {
2906 mPointerVelocityControl.reset();
2907 }
2908
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002909 float x, y;
2910 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002911
Michael Wright227c5542020-07-02 18:30:52 +01002912 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002913 mPointerGesture.currentGestureIdBits.clear();
2914 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2915 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2916 mPointerGesture.currentGestureProperties[0].clear();
2917 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2918 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2919 mPointerGesture.currentGestureCoords[0].clear();
2920 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2921 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2922 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2923 } else if (currentFingerCount == 0) {
2924 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002925 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002926 *outFinishPreviousGesture = true;
2927 }
2928
2929 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2930 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2931 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002932 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2933 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002934 lastFingerCount == 1) {
2935 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002936 float x, y;
2937 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002938 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2939 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002940 if (DEBUG_GESTURES) {
2941 ALOGD("Gestures: TAP");
2942 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002943
2944 mPointerGesture.tapUpTime = when;
2945 getContext()->requestTimeoutAtTime(when +
2946 mConfig.pointerGestureTapDragInterval);
2947
2948 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002949 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002950 mPointerGesture.currentGestureIdBits.clear();
2951 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2952 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2953 mPointerGesture.currentGestureProperties[0].clear();
2954 mPointerGesture.currentGestureProperties[0].id =
2955 mPointerGesture.activeGestureId;
2956 mPointerGesture.currentGestureProperties[0].toolType =
2957 AMOTION_EVENT_TOOL_TYPE_FINGER;
2958 mPointerGesture.currentGestureCoords[0].clear();
2959 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2960 mPointerGesture.tapX);
2961 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2962 mPointerGesture.tapY);
2963 mPointerGesture.currentGestureCoords[0]
2964 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2965
2966 tapped = true;
2967 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002968 if (DEBUG_GESTURES) {
2969 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2970 y - mPointerGesture.tapY);
2971 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002972 }
2973 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002974 if (DEBUG_GESTURES) {
2975 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2976 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2977 (when - mPointerGesture.tapDownTime) * 0.000001f);
2978 } else {
2979 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2980 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002981 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002982 }
2983 }
2984
2985 mPointerVelocityControl.reset();
2986
2987 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002988 if (DEBUG_GESTURES) {
2989 ALOGD("Gestures: NEUTRAL");
2990 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002991 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002992 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002993 mPointerGesture.currentGestureIdBits.clear();
2994 }
2995 } else if (currentFingerCount == 1) {
2996 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2997 // The pointer follows the active touch point.
2998 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2999 // When in TAP_DRAG, emit MOVE events at the pointer location.
3000 ALOG_ASSERT(activeTouchId >= 0);
3001
Michael Wright227c5542020-07-02 18:30:52 +01003002 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
3003 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003004 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003005 float x, y;
3006 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003007 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
3008 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01003009 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003010 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003011 if (DEBUG_GESTURES) {
3012 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3013 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
3014 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003015 }
3016 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003017 if (DEBUG_GESTURES) {
3018 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3019 (when - mPointerGesture.tapUpTime) * 0.000001f);
3020 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003021 }
Michael Wright227c5542020-07-02 18:30:52 +01003022 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
3023 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003024 }
3025
3026 float deltaX = 0, deltaY = 0;
3027 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
3028 const RawPointerData::Pointer& currentPointer =
3029 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
3030 const RawPointerData::Pointer& lastPointer =
3031 mLastRawState.rawPointerData.pointerForId(activeTouchId);
3032 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3033 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3034
Prabir Pradhan1728b212021-10-19 16:00:03 -07003035 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003036 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3037
3038 // Move the pointer using a relative motion.
3039 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003040 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003041 } else {
3042 mPointerVelocityControl.reset();
3043 }
3044
3045 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003046 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003047 if (DEBUG_GESTURES) {
3048 ALOGD("Gestures: TAP_DRAG");
3049 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003050 down = true;
3051 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003052 if (DEBUG_GESTURES) {
3053 ALOGD("Gestures: HOVER");
3054 }
Michael Wright227c5542020-07-02 18:30:52 +01003055 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003056 *outFinishPreviousGesture = true;
3057 }
3058 mPointerGesture.activeGestureId = 0;
3059 down = false;
3060 }
3061
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003062 float x, y;
3063 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003064
3065 mPointerGesture.currentGestureIdBits.clear();
3066 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3067 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3068 mPointerGesture.currentGestureProperties[0].clear();
3069 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3070 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3071 mPointerGesture.currentGestureCoords[0].clear();
3072 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3073 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3074 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3075 down ? 1.0f : 0.0f);
3076
3077 if (lastFingerCount == 0 && currentFingerCount != 0) {
3078 mPointerGesture.resetTap();
3079 mPointerGesture.tapDownTime = when;
3080 mPointerGesture.tapX = x;
3081 mPointerGesture.tapY = y;
3082 }
3083 } else {
3084 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3085 // We need to provide feedback for each finger that goes down so we cannot wait
3086 // for the fingers to move before deciding what to do.
3087 //
3088 // The ambiguous case is deciding what to do when there are two fingers down but they
3089 // have not moved enough to determine whether they are part of a drag or part of a
3090 // freeform gesture, or just a press or long-press at the pointer location.
3091 //
3092 // When there are two fingers we start with the PRESS hypothesis and we generate a
3093 // down at the pointer location.
3094 //
3095 // When the two fingers move enough or when additional fingers are added, we make
3096 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3097 ALOG_ASSERT(activeTouchId >= 0);
3098
3099 bool settled = when >=
3100 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003101 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3102 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3103 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003104 *outFinishPreviousGesture = true;
3105 } else if (!settled && currentFingerCount > lastFingerCount) {
3106 // Additional pointers have gone down but not yet settled.
3107 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003108 if (DEBUG_GESTURES) {
3109 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3110 "MULTITOUCH, settle time remaining %0.3fms",
3111 (mPointerGesture.firstTouchTime +
3112 mConfig.pointerGestureMultitouchSettleInterval - when) *
3113 0.000001f);
3114 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003115 *outCancelPreviousGesture = true;
3116 } else {
3117 // Continue previous gesture.
3118 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3119 }
3120
3121 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003122 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003123 mPointerGesture.activeGestureId = 0;
3124 mPointerGesture.referenceIdBits.clear();
3125 mPointerVelocityControl.reset();
3126
3127 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003128 if (DEBUG_GESTURES) {
3129 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3130 "settle time remaining %0.3fms",
3131 (mPointerGesture.firstTouchTime +
3132 mConfig.pointerGestureMultitouchSettleInterval - when) *
3133 0.000001f);
3134 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003135 mCurrentRawState.rawPointerData
3136 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3137 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003138 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3139 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003140 }
3141
3142 // Clear the reference deltas for fingers not yet included in the reference calculation.
3143 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3144 ~mPointerGesture.referenceIdBits.value);
3145 !idBits.isEmpty();) {
3146 uint32_t id = idBits.clearFirstMarkedBit();
3147 mPointerGesture.referenceDeltas[id].dx = 0;
3148 mPointerGesture.referenceDeltas[id].dy = 0;
3149 }
3150 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3151
3152 // Add delta for all fingers and calculate a common movement delta.
3153 float commonDeltaX = 0, commonDeltaY = 0;
3154 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3155 mCurrentCookedState.fingerIdBits.value);
3156 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3157 bool first = (idBits == commonIdBits);
3158 uint32_t id = idBits.clearFirstMarkedBit();
3159 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3160 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3161 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3162 delta.dx += cpd.x - lpd.x;
3163 delta.dy += cpd.y - lpd.y;
3164
3165 if (first) {
3166 commonDeltaX = delta.dx;
3167 commonDeltaY = delta.dy;
3168 } else {
3169 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3170 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3171 }
3172 }
3173
3174 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003175 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003176 float dist[MAX_POINTER_ID + 1];
3177 int32_t distOverThreshold = 0;
3178 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3179 uint32_t id = idBits.clearFirstMarkedBit();
3180 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3181 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3182 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3183 distOverThreshold += 1;
3184 }
3185 }
3186
3187 // Only transition when at least two pointers have moved further than
3188 // the minimum distance threshold.
3189 if (distOverThreshold >= 2) {
3190 if (currentFingerCount > 2) {
3191 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003192 if (DEBUG_GESTURES) {
3193 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3194 currentFingerCount);
3195 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003196 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003197 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003198 } else {
3199 // There are exactly two pointers.
3200 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3201 uint32_t id1 = idBits.clearFirstMarkedBit();
3202 uint32_t id2 = idBits.firstMarkedBit();
3203 const RawPointerData::Pointer& p1 =
3204 mCurrentRawState.rawPointerData.pointerForId(id1);
3205 const RawPointerData::Pointer& p2 =
3206 mCurrentRawState.rawPointerData.pointerForId(id2);
3207 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3208 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3209 // There are two pointers but they are too far apart for a SWIPE,
3210 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003211 if (DEBUG_GESTURES) {
3212 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3213 "%0.3f",
3214 mutualDistance, mPointerGestureMaxSwipeWidth);
3215 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003216 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003217 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003218 } else {
3219 // There are two pointers. Wait for both pointers to start moving
3220 // before deciding whether this is a SWIPE or FREEFORM gesture.
3221 float dist1 = dist[id1];
3222 float dist2 = dist[id2];
3223 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3224 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3225 // Calculate the dot product of the displacement vectors.
3226 // When the vectors are oriented in approximately the same direction,
3227 // the angle betweeen them is near zero and the cosine of the angle
3228 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3229 // mag(v2).
3230 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3231 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3232 float dx1 = delta1.dx * mPointerXZoomScale;
3233 float dy1 = delta1.dy * mPointerYZoomScale;
3234 float dx2 = delta2.dx * mPointerXZoomScale;
3235 float dy2 = delta2.dy * mPointerYZoomScale;
3236 float dot = dx1 * dx2 + dy1 * dy2;
3237 float cosine = dot / (dist1 * dist2); // denominator always > 0
3238 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3239 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003240 if (DEBUG_GESTURES) {
3241 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3242 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3243 "cosine %0.3f >= %0.3f",
3244 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3245 mConfig.pointerGestureMultitouchMinDistance, cosine,
3246 mConfig.pointerGestureSwipeTransitionAngleCosine);
3247 }
Michael Wright227c5542020-07-02 18:30:52 +01003248 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003249 } else {
3250 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003251 if (DEBUG_GESTURES) {
3252 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3253 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3254 "cosine %0.3f < %0.3f",
3255 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3256 mConfig.pointerGestureMultitouchMinDistance, cosine,
3257 mConfig.pointerGestureSwipeTransitionAngleCosine);
3258 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003259 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003260 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003261 }
3262 }
3263 }
3264 }
3265 }
Michael Wright227c5542020-07-02 18:30:52 +01003266 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003267 // Switch from SWIPE to FREEFORM if additional pointers go down.
3268 // Cancel previous gesture.
3269 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003270 if (DEBUG_GESTURES) {
3271 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3272 currentFingerCount);
3273 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003274 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003275 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003276 }
3277 }
3278
3279 // Move the reference points based on the overall group motion of the fingers
3280 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003281 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003282 (commonDeltaX || commonDeltaY)) {
3283 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3284 uint32_t id = idBits.clearFirstMarkedBit();
3285 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3286 delta.dx = 0;
3287 delta.dy = 0;
3288 }
3289
3290 mPointerGesture.referenceTouchX += commonDeltaX;
3291 mPointerGesture.referenceTouchY += commonDeltaY;
3292
3293 commonDeltaX *= mPointerXMovementScale;
3294 commonDeltaY *= mPointerYMovementScale;
3295
Prabir Pradhan1728b212021-10-19 16:00:03 -07003296 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003297 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3298
3299 mPointerGesture.referenceGestureX += commonDeltaX;
3300 mPointerGesture.referenceGestureY += commonDeltaY;
3301 }
3302
3303 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003304 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3305 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003306 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003307 if (DEBUG_GESTURES) {
3308 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3309 "activeGestureId=%d, currentTouchPointerCount=%d",
3310 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3311 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003312 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3313
3314 mPointerGesture.currentGestureIdBits.clear();
3315 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3316 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3317 mPointerGesture.currentGestureProperties[0].clear();
3318 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3319 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3320 mPointerGesture.currentGestureCoords[0].clear();
3321 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3322 mPointerGesture.referenceGestureX);
3323 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3324 mPointerGesture.referenceGestureY);
3325 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003326 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003327 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003328 if (DEBUG_GESTURES) {
3329 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3330 "activeGestureId=%d, currentTouchPointerCount=%d",
3331 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3332 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003333 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3334
3335 mPointerGesture.currentGestureIdBits.clear();
3336
3337 BitSet32 mappedTouchIdBits;
3338 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003339 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003340 // Initially, assign the active gesture id to the active touch point
3341 // if there is one. No other touch id bits are mapped yet.
3342 if (!*outCancelPreviousGesture) {
3343 mappedTouchIdBits.markBit(activeTouchId);
3344 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3345 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3346 mPointerGesture.activeGestureId;
3347 } else {
3348 mPointerGesture.activeGestureId = -1;
3349 }
3350 } else {
3351 // Otherwise, assume we mapped all touches from the previous frame.
3352 // Reuse all mappings that are still applicable.
3353 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3354 mCurrentCookedState.fingerIdBits.value;
3355 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3356
3357 // Check whether we need to choose a new active gesture id because the
3358 // current went went up.
3359 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3360 ~mCurrentCookedState.fingerIdBits.value);
3361 !upTouchIdBits.isEmpty();) {
3362 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3363 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3364 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3365 mPointerGesture.activeGestureId = -1;
3366 break;
3367 }
3368 }
3369 }
3370
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003371 if (DEBUG_GESTURES) {
3372 ALOGD("Gestures: FREEFORM follow up "
3373 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3374 "activeGestureId=%d",
3375 mappedTouchIdBits.value, usedGestureIdBits.value,
3376 mPointerGesture.activeGestureId);
3377 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003378
3379 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3380 for (uint32_t i = 0; i < currentFingerCount; i++) {
3381 uint32_t touchId = idBits.clearFirstMarkedBit();
3382 uint32_t gestureId;
3383 if (!mappedTouchIdBits.hasBit(touchId)) {
3384 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3385 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003386 if (DEBUG_GESTURES) {
3387 ALOGD("Gestures: FREEFORM "
3388 "new mapping for touch id %d -> gesture id %d",
3389 touchId, gestureId);
3390 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003391 } else {
3392 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003393 if (DEBUG_GESTURES) {
3394 ALOGD("Gestures: FREEFORM "
3395 "existing mapping for touch id %d -> gesture id %d",
3396 touchId, gestureId);
3397 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003398 }
3399 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3400 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3401
3402 const RawPointerData::Pointer& pointer =
3403 mCurrentRawState.rawPointerData.pointerForId(touchId);
3404 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3405 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003406 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003407
3408 mPointerGesture.currentGestureProperties[i].clear();
3409 mPointerGesture.currentGestureProperties[i].id = gestureId;
3410 mPointerGesture.currentGestureProperties[i].toolType =
3411 AMOTION_EVENT_TOOL_TYPE_FINGER;
3412 mPointerGesture.currentGestureCoords[i].clear();
3413 mPointerGesture.currentGestureCoords[i]
3414 .setAxisValue(AMOTION_EVENT_AXIS_X,
3415 mPointerGesture.referenceGestureX + deltaX);
3416 mPointerGesture.currentGestureCoords[i]
3417 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3418 mPointerGesture.referenceGestureY + deltaY);
3419 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3420 1.0f);
3421 }
3422
3423 if (mPointerGesture.activeGestureId < 0) {
3424 mPointerGesture.activeGestureId =
3425 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003426 if (DEBUG_GESTURES) {
3427 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3428 mPointerGesture.activeGestureId);
3429 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003430 }
3431 }
3432 }
3433
3434 mPointerController->setButtonState(mCurrentRawState.buttonState);
3435
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003436 if (DEBUG_GESTURES) {
3437 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3438 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3439 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3440 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3441 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3442 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3443 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3444 uint32_t id = idBits.clearFirstMarkedBit();
3445 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3446 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3447 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3448 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3449 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3450 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3451 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3452 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3453 }
3454 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3455 uint32_t id = idBits.clearFirstMarkedBit();
3456 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3457 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3458 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3459 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3460 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3461 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3462 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3463 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3464 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003465 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003466 return true;
3467}
3468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003469void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003470 mPointerSimple.currentCoords.clear();
3471 mPointerSimple.currentProperties.clear();
3472
3473 bool down, hovering;
3474 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3475 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3476 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003477 mPointerController
3478 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3479 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003480
3481 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3482 down = !hovering;
3483
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003484 float x, y;
3485 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003486 mPointerSimple.currentCoords.copyFrom(
3487 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3488 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3489 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3490 mPointerSimple.currentProperties.id = 0;
3491 mPointerSimple.currentProperties.toolType =
3492 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3493 } else {
3494 down = false;
3495 hovering = false;
3496 }
3497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003499}
3500
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3502 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003503}
3504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003506 mPointerSimple.currentCoords.clear();
3507 mPointerSimple.currentProperties.clear();
3508
3509 bool down, hovering;
3510 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3511 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3512 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3513 float deltaX = 0, deltaY = 0;
3514 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3515 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3516 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3517 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3518 mPointerXMovementScale;
3519 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3520 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3521 mPointerYMovementScale;
3522
Prabir Pradhan1728b212021-10-19 16:00:03 -07003523 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003524 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3525
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003526 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003527 } else {
3528 mPointerVelocityControl.reset();
3529 }
3530
3531 down = isPointerDown(mCurrentRawState.buttonState);
3532 hovering = !down;
3533
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003534 float x, y;
3535 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003536 mPointerSimple.currentCoords.copyFrom(
3537 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3538 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3539 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3540 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3541 hovering ? 0.0f : 1.0f);
3542 mPointerSimple.currentProperties.id = 0;
3543 mPointerSimple.currentProperties.toolType =
3544 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3545 } else {
3546 mPointerVelocityControl.reset();
3547
3548 down = false;
3549 hovering = false;
3550 }
3551
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003553}
3554
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3556 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003557
3558 mPointerVelocityControl.reset();
3559}
3560
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003561void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3562 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003563 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003564
3565 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003566 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003567 mPointerController->clearSpots();
3568 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003569 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003570 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003571 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003572 }
Garfield Tan9514d782020-11-10 16:37:23 -08003573 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003574
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003575 float xCursorPosition, yCursorPosition;
3576 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003577
3578 if (mPointerSimple.down && !down) {
3579 mPointerSimple.down = false;
3580
3581 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3583 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003584 mLastRawState.buttonState, MotionClassification::NONE,
3585 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3586 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3587 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3588 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003589 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003590 }
3591
3592 if (mPointerSimple.hovering && !hovering) {
3593 mPointerSimple.hovering = false;
3594
3595 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003596 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3597 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3598 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003599 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3600 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3601 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3602 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003603 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003604 }
3605
3606 if (down) {
3607 if (!mPointerSimple.down) {
3608 mPointerSimple.down = true;
3609 mPointerSimple.downTime = when;
3610
3611 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003613 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3614 metaState, mCurrentRawState.buttonState,
3615 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3616 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3617 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3618 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003619 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003620 }
3621
3622 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3624 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003625 mCurrentRawState.buttonState, MotionClassification::NONE,
3626 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3627 &mPointerSimple.currentCoords, mOrientedXPrecision,
3628 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3629 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003630 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003631 }
3632
3633 if (hovering) {
3634 if (!mPointerSimple.hovering) {
3635 mPointerSimple.hovering = true;
3636
3637 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003638 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003639 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3640 metaState, mCurrentRawState.buttonState,
3641 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3642 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3643 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3644 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003645 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003646 }
3647
3648 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3650 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3651 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003652 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3653 &mPointerSimple.currentCoords, mOrientedXPrecision,
3654 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3655 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003656 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003657 }
3658
3659 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3660 float vscroll = mCurrentRawState.rawVScroll;
3661 float hscroll = mCurrentRawState.rawHScroll;
3662 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3663 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3664
3665 // Send scroll.
3666 PointerCoords pointerCoords;
3667 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3668 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3669 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3670
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003671 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3672 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003673 mCurrentRawState.buttonState, MotionClassification::NONE,
3674 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3675 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3676 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3677 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003678 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003679 }
3680
3681 // Save state.
3682 if (down || hovering) {
3683 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3684 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3685 } else {
3686 mPointerSimple.reset();
3687 }
3688}
3689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003691 mPointerSimple.currentCoords.clear();
3692 mPointerSimple.currentProperties.clear();
3693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003695}
3696
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003697void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3698 uint32_t source, int32_t action, int32_t actionButton,
3699 int32_t flags, int32_t metaState, int32_t buttonState,
3700 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003701 const PointerCoords* coords, const uint32_t* idToIndex,
3702 BitSet32 idBits, int32_t changedId, float xPrecision,
3703 float yPrecision, nsecs_t downTime) {
3704 PointerCoords pointerCoords[MAX_POINTERS];
3705 PointerProperties pointerProperties[MAX_POINTERS];
3706 uint32_t pointerCount = 0;
3707 while (!idBits.isEmpty()) {
3708 uint32_t id = idBits.clearFirstMarkedBit();
3709 uint32_t index = idToIndex[id];
3710 pointerProperties[pointerCount].copyFrom(properties[index]);
3711 pointerCoords[pointerCount].copyFrom(coords[index]);
3712
3713 if (changedId >= 0 && id == uint32_t(changedId)) {
3714 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3715 }
3716
3717 pointerCount += 1;
3718 }
3719
3720 ALOG_ASSERT(pointerCount != 0);
3721
3722 if (changedId >= 0 && pointerCount == 1) {
3723 // Replace initial down and final up action.
3724 // We can compare the action without masking off the changed pointer index
3725 // because we know the index is 0.
3726 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3727 action = AMOTION_EVENT_ACTION_DOWN;
3728 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003729 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3730 action = AMOTION_EVENT_ACTION_CANCEL;
3731 } else {
3732 action = AMOTION_EVENT_ACTION_UP;
3733 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003734 } else {
3735 // Can't happen.
3736 ALOG_ASSERT(false);
3737 }
3738 }
3739 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3740 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003741 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003742 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003743 }
3744 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3745 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003746 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003747 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003748 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3750 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003751 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3752 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3753 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003754 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003755}
3756
3757bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3758 const PointerCoords* inCoords,
3759 const uint32_t* inIdToIndex,
3760 PointerProperties* outProperties,
3761 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3762 BitSet32 idBits) const {
3763 bool changed = false;
3764 while (!idBits.isEmpty()) {
3765 uint32_t id = idBits.clearFirstMarkedBit();
3766 uint32_t inIndex = inIdToIndex[id];
3767 uint32_t outIndex = outIdToIndex[id];
3768
3769 const PointerProperties& curInProperties = inProperties[inIndex];
3770 const PointerCoords& curInCoords = inCoords[inIndex];
3771 PointerProperties& curOutProperties = outProperties[outIndex];
3772 PointerCoords& curOutCoords = outCoords[outIndex];
3773
3774 if (curInProperties != curOutProperties) {
3775 curOutProperties.copyFrom(curInProperties);
3776 changed = true;
3777 }
3778
3779 if (curInCoords != curOutCoords) {
3780 curOutCoords.copyFrom(curInCoords);
3781 changed = true;
3782 }
3783 }
3784 return changed;
3785}
3786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003787void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3788 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3789 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003790}
3791
Prabir Pradhan1728b212021-10-19 16:00:03 -07003792// Transform input device coordinates to display panel coordinates.
3793void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003794 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3795 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3796
arthurhunga36b28e2020-12-29 20:28:15 +08003797 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3798 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3799
Prabir Pradhan1728b212021-10-19 16:00:03 -07003800 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003801 // 0 - no swap and reverse.
3802 // 90 - swap x/y and reverse y.
3803 // 180 - reverse x, y.
3804 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003805 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003806 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003807 x = xScaled;
3808 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003809 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003810 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003811 y = xScaledMax;
3812 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003813 break;
3814 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003815 x = xScaledMax;
3816 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003817 break;
3818 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003819 y = xScaled;
3820 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003821 break;
3822 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003823 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003824 }
3825}
3826
Prabir Pradhan1728b212021-10-19 16:00:03 -07003827bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003828 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3829 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3830
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003831 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003832 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003833 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003834 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003835}
3836
3837const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3838 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003839 if (DEBUG_VIRTUAL_KEYS) {
3840 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3841 "left=%d, top=%d, right=%d, bottom=%d",
3842 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3843 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3844 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003845
3846 if (virtualKey.isHit(x, y)) {
3847 return &virtualKey;
3848 }
3849 }
3850
3851 return nullptr;
3852}
3853
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003854void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3855 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3856 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003857
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003858 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003859
3860 if (currentPointerCount == 0) {
3861 // No pointers to assign.
3862 return;
3863 }
3864
3865 if (lastPointerCount == 0) {
3866 // All pointers are new.
3867 for (uint32_t i = 0; i < currentPointerCount; i++) {
3868 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003869 current.rawPointerData.pointers[i].id = id;
3870 current.rawPointerData.idToIndex[id] = i;
3871 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003872 }
3873 return;
3874 }
3875
3876 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003877 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003878 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003879 uint32_t id = last.rawPointerData.pointers[0].id;
3880 current.rawPointerData.pointers[0].id = id;
3881 current.rawPointerData.idToIndex[id] = 0;
3882 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003883 return;
3884 }
3885
3886 // General case.
3887 // We build a heap of squared euclidean distances between current and last pointers
3888 // associated with the current and last pointer indices. Then, we find the best
3889 // match (by distance) for each current pointer.
3890 // The pointers must have the same tool type but it is possible for them to
3891 // transition from hovering to touching or vice-versa while retaining the same id.
3892 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3893
3894 uint32_t heapSize = 0;
3895 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3896 currentPointerIndex++) {
3897 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3898 lastPointerIndex++) {
3899 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003900 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003901 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003902 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003903 if (currentPointer.toolType == lastPointer.toolType) {
3904 int64_t deltaX = currentPointer.x - lastPointer.x;
3905 int64_t deltaY = currentPointer.y - lastPointer.y;
3906
3907 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3908
3909 // Insert new element into the heap (sift up).
3910 heap[heapSize].currentPointerIndex = currentPointerIndex;
3911 heap[heapSize].lastPointerIndex = lastPointerIndex;
3912 heap[heapSize].distance = distance;
3913 heapSize += 1;
3914 }
3915 }
3916 }
3917
3918 // Heapify
3919 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3920 startIndex -= 1;
3921 for (uint32_t parentIndex = startIndex;;) {
3922 uint32_t childIndex = parentIndex * 2 + 1;
3923 if (childIndex >= heapSize) {
3924 break;
3925 }
3926
3927 if (childIndex + 1 < heapSize &&
3928 heap[childIndex + 1].distance < heap[childIndex].distance) {
3929 childIndex += 1;
3930 }
3931
3932 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3933 break;
3934 }
3935
3936 swap(heap[parentIndex], heap[childIndex]);
3937 parentIndex = childIndex;
3938 }
3939 }
3940
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003941 if (DEBUG_POINTER_ASSIGNMENT) {
3942 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3943 for (size_t i = 0; i < heapSize; i++) {
3944 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3945 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3946 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003947 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003948
3949 // Pull matches out by increasing order of distance.
3950 // To avoid reassigning pointers that have already been matched, the loop keeps track
3951 // of which last and current pointers have been matched using the matchedXXXBits variables.
3952 // It also tracks the used pointer id bits.
3953 BitSet32 matchedLastBits(0);
3954 BitSet32 matchedCurrentBits(0);
3955 BitSet32 usedIdBits(0);
3956 bool first = true;
3957 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3958 while (heapSize > 0) {
3959 if (first) {
3960 // The first time through the loop, we just consume the root element of
3961 // the heap (the one with smallest distance).
3962 first = false;
3963 } else {
3964 // Previous iterations consumed the root element of the heap.
3965 // Pop root element off of the heap (sift down).
3966 heap[0] = heap[heapSize];
3967 for (uint32_t parentIndex = 0;;) {
3968 uint32_t childIndex = parentIndex * 2 + 1;
3969 if (childIndex >= heapSize) {
3970 break;
3971 }
3972
3973 if (childIndex + 1 < heapSize &&
3974 heap[childIndex + 1].distance < heap[childIndex].distance) {
3975 childIndex += 1;
3976 }
3977
3978 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3979 break;
3980 }
3981
3982 swap(heap[parentIndex], heap[childIndex]);
3983 parentIndex = childIndex;
3984 }
3985
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003986 if (DEBUG_POINTER_ASSIGNMENT) {
3987 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3988 for (size_t j = 0; j < heapSize; j++) {
3989 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3990 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3991 heap[j].distance);
3992 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003993 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003994 }
3995
3996 heapSize -= 1;
3997
3998 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3999 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4000
4001 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4002 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4003
4004 matchedCurrentBits.markBit(currentPointerIndex);
4005 matchedLastBits.markBit(lastPointerIndex);
4006
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004007 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
4008 current.rawPointerData.pointers[currentPointerIndex].id = id;
4009 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4010 current.rawPointerData.markIdBit(id,
4011 current.rawPointerData.isHovering(
4012 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004013 usedIdBits.markBit(id);
4014
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004015 if (DEBUG_POINTER_ASSIGNMENT) {
4016 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
4017 ", distance=%" PRIu64,
4018 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4019 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004020 break;
4021 }
4022 }
4023
4024 // Assign fresh ids to pointers that were not matched in the process.
4025 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4026 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4027 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4028
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004029 current.rawPointerData.pointers[currentPointerIndex].id = id;
4030 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4031 current.rawPointerData.markIdBit(id,
4032 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004033
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004034 if (DEBUG_POINTER_ASSIGNMENT) {
4035 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
4036 id);
4037 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004038 }
4039}
4040
4041int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4042 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4043 return AKEY_STATE_VIRTUAL;
4044 }
4045
4046 for (const VirtualKey& virtualKey : mVirtualKeys) {
4047 if (virtualKey.keyCode == keyCode) {
4048 return AKEY_STATE_UP;
4049 }
4050 }
4051
4052 return AKEY_STATE_UNKNOWN;
4053}
4054
4055int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4056 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4057 return AKEY_STATE_VIRTUAL;
4058 }
4059
4060 for (const VirtualKey& virtualKey : mVirtualKeys) {
4061 if (virtualKey.scanCode == scanCode) {
4062 return AKEY_STATE_UP;
4063 }
4064 }
4065
4066 return AKEY_STATE_UNKNOWN;
4067}
4068
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004069bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
4070 const std::vector<int32_t>& keyCodes,
4071 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004072 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004073 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004074 if (virtualKey.keyCode == keyCodes[i]) {
4075 outFlags[i] = 1;
4076 }
4077 }
4078 }
4079
4080 return true;
4081}
4082
4083std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4084 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004085 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004086 return std::make_optional(mPointerController->getDisplayId());
4087 } else {
4088 return std::make_optional(mViewport.displayId);
4089 }
4090 }
4091 return std::nullopt;
4092}
4093
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004094} // namespace android