blob: c069248013781a199d28499dd6b96526f4447893 [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 Pradhan3e5ec702022-07-29 16:26:24 +00001034 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
1035 mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight &&
1036 viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001037
1038 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001039 mInputDeviceOrientation =
1040 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001041 } else {
1042 mPhysicalWidth = rawWidth;
1043 mPhysicalHeight = rawHeight;
1044 mPhysicalLeft = 0;
1045 mPhysicalTop = 0;
1046
Prabir Pradhan1728b212021-10-19 16:00:03 -07001047 mDisplayWidth = rawWidth;
1048 mDisplayHeight = rawHeight;
1049 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001050 }
1051 }
1052
1053 // If moving between pointer modes, need to reset some state.
1054 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1055 if (deviceModeChanged) {
1056 mOrientedRanges.clear();
1057 }
1058
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001059 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1060 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001061 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001062 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001063 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1064 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001065 if (mPointerController == nullptr) {
1066 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001067 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001068 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001069 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1070 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001071 } else {
lilinnandef700b2022-06-17 19:32:01 +08001072 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1073 !mConfig.showTouches) {
1074 mPointerController->clearSpots();
1075 }
Michael Wright17db18e2020-06-26 20:51:44 +01001076 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001077 }
1078
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001079 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001080 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1081 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001082 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1083 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001084
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001085 configureVirtualKeys();
1086
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001087 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001088
1089 // Location
1090 updateAffineTransformation();
1091
Michael Wright227c5542020-07-02 18:30:52 +01001092 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001093 // Compute pointer gesture detection parameters.
1094 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001095 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001096
1097 // Scale movements such that one whole swipe of the touch pad covers a
1098 // given area relative to the diagonal size of the display when no acceleration
1099 // is applied.
1100 // Assume that the touch pad has a square aspect ratio such that movements in
1101 // X and Y of the same number of raw units cover the same physical distance.
1102 mPointerXMovementScale =
1103 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1104 mPointerYMovementScale = mPointerXMovementScale;
1105
1106 // Scale zooms to cover a smaller range of the display than movements do.
1107 // This value determines the area around the pointer that is affected by freeform
1108 // pointer gestures.
1109 mPointerXZoomScale =
1110 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1111 mPointerYZoomScale = mPointerXZoomScale;
1112
HQ Liue6983c72022-04-19 22:14:56 +00001113 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1114 // axis is non positive value.
1115 const float minFreeformGestureWidth =
1116 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1117
1118 mPointerGestureMaxSwipeWidth =
1119 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1120 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001121
1122 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001123 const nsecs_t readTime = when; // synthetic event
1124 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001125 }
1126
1127 // Inform the dispatcher about the changes.
1128 *outResetNeeded = true;
1129 bumpGeneration();
1130 }
1131}
1132
Prabir Pradhan1728b212021-10-19 16:00:03 -07001133void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001134 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001135 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1136 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001137 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1138 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1139 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1140 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001141 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001142}
1143
1144void TouchInputMapper::configureVirtualKeys() {
1145 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001146 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001147
1148 mVirtualKeys.clear();
1149
1150 if (virtualKeyDefinitions.size() == 0) {
1151 return;
1152 }
1153
1154 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1155 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1156 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1157 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1158
1159 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1160 VirtualKey virtualKey;
1161
1162 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1163 int32_t keyCode;
1164 int32_t dummyKeyMetaState;
1165 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001166 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1167 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001168 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1169 continue; // drop the key
1170 }
1171
1172 virtualKey.keyCode = keyCode;
1173 virtualKey.flags = flags;
1174
1175 // convert the key definition's display coordinates into touch coordinates for a hit box
1176 int32_t halfWidth = virtualKeyDefinition.width / 2;
1177 int32_t halfHeight = virtualKeyDefinition.height / 2;
1178
1179 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001180 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001181 touchScreenLeft;
1182 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001183 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001184 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001185 virtualKey.hitTop =
1186 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001187 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001188 virtualKey.hitBottom =
1189 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001190 touchScreenTop;
1191 mVirtualKeys.push_back(virtualKey);
1192 }
1193}
1194
1195void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1196 if (!mVirtualKeys.empty()) {
1197 dump += INDENT3 "Virtual Keys:\n";
1198
1199 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1200 const VirtualKey& virtualKey = mVirtualKeys[i];
1201 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1202 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1203 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1204 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1205 }
1206 }
1207}
1208
1209void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001210 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001211 Calibration& out = mCalibration;
1212
1213 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001214 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001215 std::string sizeCalibrationString;
1216 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001218 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001219 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001220 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001221 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001222 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001223 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001224 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001225 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001226 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001227 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001228 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001229 }
1230 }
1231
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001232 float sizeScale;
1233
1234 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1235 out.sizeScale = sizeScale;
1236 }
1237 float sizeBias;
1238 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1239 out.sizeBias = sizeBias;
1240 }
1241 bool sizeIsSummed;
1242 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1243 out.sizeIsSummed = sizeIsSummed;
1244 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001245
1246 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001247 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001248 std::string pressureCalibrationString;
1249 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001250 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001251 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001252 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001253 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001254 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001255 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001256 } else if (pressureCalibrationString != "default") {
1257 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001258 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001259 }
1260 }
1261
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001262 float pressureScale;
1263 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1264 out.pressureScale = pressureScale;
1265 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001266
1267 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001268 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001269 std::string orientationCalibrationString;
1270 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001271 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001272 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001273 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001274 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001275 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001276 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001277 } else if (orientationCalibrationString != "default") {
1278 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001279 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001280 }
1281 }
1282
1283 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001284 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001285 std::string distanceCalibrationString;
1286 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001287 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001288 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001289 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001290 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001291 } else if (distanceCalibrationString != "default") {
1292 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001293 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001294 }
1295 }
1296
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001297 float distanceScale;
1298 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1299 out.distanceScale = distanceScale;
1300 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001301
Michael Wright227c5542020-07-02 18:30:52 +01001302 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001303 std::string coverageCalibrationString;
1304 if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001305 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001306 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001307 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001308 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001309 } else if (coverageCalibrationString != "default") {
1310 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001311 coverageCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001312 }
1313 }
1314}
1315
1316void TouchInputMapper::resolveCalibration() {
1317 // Size
1318 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001319 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1320 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001321 }
1322 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001323 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001324 }
1325
1326 // Pressure
1327 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001328 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1329 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001330 }
1331 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001332 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001333 }
1334
1335 // Orientation
1336 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001337 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1338 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001339 }
1340 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001341 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001342 }
1343
1344 // Distance
1345 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001346 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1347 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001348 }
1349 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001350 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001351 }
1352
1353 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001354 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1355 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001356 }
1357}
1358
1359void TouchInputMapper::dumpCalibration(std::string& dump) {
1360 dump += INDENT3 "Calibration:\n";
1361
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001362 dump += INDENT4 "touch.size.calibration: ";
1363 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001364
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001365 if (mCalibration.sizeScale) {
1366 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001367 }
1368
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001369 if (mCalibration.sizeBias) {
1370 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001371 }
1372
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001373 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001374 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001375 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001376 }
1377
1378 // Pressure
1379 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001380 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001381 dump += INDENT4 "touch.pressure.calibration: none\n";
1382 break;
Michael Wright227c5542020-07-02 18:30:52 +01001383 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001384 dump += INDENT4 "touch.pressure.calibration: physical\n";
1385 break;
Michael Wright227c5542020-07-02 18:30:52 +01001386 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001387 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1388 break;
1389 default:
1390 ALOG_ASSERT(false);
1391 }
1392
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001393 if (mCalibration.pressureScale) {
1394 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001395 }
1396
1397 // Orientation
1398 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001399 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001400 dump += INDENT4 "touch.orientation.calibration: none\n";
1401 break;
Michael Wright227c5542020-07-02 18:30:52 +01001402 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001403 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1404 break;
Michael Wright227c5542020-07-02 18:30:52 +01001405 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001406 dump += INDENT4 "touch.orientation.calibration: vector\n";
1407 break;
1408 default:
1409 ALOG_ASSERT(false);
1410 }
1411
1412 // Distance
1413 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001414 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001415 dump += INDENT4 "touch.distance.calibration: none\n";
1416 break;
Michael Wright227c5542020-07-02 18:30:52 +01001417 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001418 dump += INDENT4 "touch.distance.calibration: scaled\n";
1419 break;
1420 default:
1421 ALOG_ASSERT(false);
1422 }
1423
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001424 if (mCalibration.distanceScale) {
1425 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001426 }
1427
1428 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001429 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001430 dump += INDENT4 "touch.coverage.calibration: none\n";
1431 break;
Michael Wright227c5542020-07-02 18:30:52 +01001432 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001433 dump += INDENT4 "touch.coverage.calibration: box\n";
1434 break;
1435 default:
1436 ALOG_ASSERT(false);
1437 }
1438}
1439
1440void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1441 dump += INDENT3 "Affine Transformation:\n";
1442
1443 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1444 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1445 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1446 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1447 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1448 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1449}
1450
1451void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001452 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001453 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001454}
1455
1456void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001457 mCursorButtonAccumulator.reset(getDeviceContext());
1458 mCursorScrollAccumulator.reset(getDeviceContext());
1459 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001460
1461 mPointerVelocityControl.reset();
1462 mWheelXVelocityControl.reset();
1463 mWheelYVelocityControl.reset();
1464
1465 mRawStatesPending.clear();
1466 mCurrentRawState.clear();
1467 mCurrentCookedState.clear();
1468 mLastRawState.clear();
1469 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001470 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001471 mSentHoverEnter = false;
1472 mHavePointerIds = false;
1473 mCurrentMotionAborted = false;
1474 mDownTime = 0;
1475
1476 mCurrentVirtualKey.down = false;
1477
1478 mPointerGesture.reset();
1479 mPointerSimple.reset();
1480 resetExternalStylus();
1481
1482 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001483 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001484 mPointerController->clearSpots();
1485 }
1486
1487 InputMapper::reset(when);
1488}
1489
1490void TouchInputMapper::resetExternalStylus() {
1491 mExternalStylusState.clear();
1492 mExternalStylusId = -1;
1493 mExternalStylusFusionTimeout = LLONG_MAX;
1494 mExternalStylusDataPending = false;
1495}
1496
1497void TouchInputMapper::clearStylusDataPendingFlags() {
1498 mExternalStylusDataPending = false;
1499 mExternalStylusFusionTimeout = LLONG_MAX;
1500}
1501
1502void TouchInputMapper::process(const RawEvent* rawEvent) {
1503 mCursorButtonAccumulator.process(rawEvent);
1504 mCursorScrollAccumulator.process(rawEvent);
1505 mTouchButtonAccumulator.process(rawEvent);
1506
1507 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001508 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001509 }
1510}
1511
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001512void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001513 // Push a new state.
1514 mRawStatesPending.emplace_back();
1515
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001516 RawState& next = mRawStatesPending.back();
1517 next.clear();
1518 next.when = when;
1519 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001520
1521 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001522 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001523 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1524
1525 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001526 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1527 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001528 mCursorScrollAccumulator.finishSync();
1529
1530 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001531 syncTouch(when, &next);
1532
1533 // The last RawState is the actually second to last, since we just added a new state
1534 const RawState& last =
1535 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001536
1537 // Assign pointer ids.
1538 if (!mHavePointerIds) {
1539 assignPointerIds(last, next);
1540 }
1541
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001542 if (DEBUG_RAW_EVENTS) {
1543 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1544 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1545 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1546 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1547 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1548 next.rawPointerData.canceledIdBits.value);
1549 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001550
Arthur Hung9ad18942021-06-19 02:04:46 +00001551 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1552 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1553 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1554 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1555 next.rawPointerData.hoveringIdBits.value);
1556 }
1557
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001558 processRawTouches(false /*timeout*/);
1559}
1560
1561void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001562 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001563 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001564 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001565 mCurrentCookedState.clear();
1566 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001567 return;
1568 }
1569
1570 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1571 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1572 // touching the current state will only observe the events that have been dispatched to the
1573 // rest of the pipeline.
1574 const size_t N = mRawStatesPending.size();
1575 size_t count;
1576 for (count = 0; count < N; count++) {
1577 const RawState& next = mRawStatesPending[count];
1578
1579 // A failure to assign the stylus id means that we're waiting on stylus data
1580 // and so should defer the rest of the pipeline.
1581 if (assignExternalStylusId(next, timeout)) {
1582 break;
1583 }
1584
1585 // All ready to go.
1586 clearStylusDataPendingFlags();
1587 mCurrentRawState.copyFrom(next);
1588 if (mCurrentRawState.when < mLastRawState.when) {
1589 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001590 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001591 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001592 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001593 }
1594 if (count != 0) {
1595 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1596 }
1597
1598 if (mExternalStylusDataPending) {
1599 if (timeout) {
1600 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1601 clearStylusDataPendingFlags();
1602 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001603 if (DEBUG_STYLUS_FUSION) {
1604 ALOGD("Timeout expired, synthesizing event with new stylus data");
1605 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001606 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1607 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001608 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1609 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1610 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1611 }
1612 }
1613}
1614
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001615void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001616 // Always start with a clean state.
1617 mCurrentCookedState.clear();
1618
1619 // Apply stylus buttons to current raw state.
1620 applyExternalStylusButtonState(when);
1621
1622 // Handle policy on initial down or hover events.
1623 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1624 mCurrentRawState.rawPointerData.pointerCount != 0;
1625
1626 uint32_t policyFlags = 0;
1627 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1628 if (initialDown || buttonsPressed) {
1629 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001630 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001631 getContext()->fadePointer();
1632 }
1633
1634 if (mParameters.wake) {
1635 policyFlags |= POLICY_FLAG_WAKE;
1636 }
1637 }
1638
1639 // Consume raw off-screen touches before cooking pointer data.
1640 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001641 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001642 mCurrentRawState.rawPointerData.clear();
1643 }
1644
1645 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1646 // with cooked pointer data that has the same ids and indices as the raw data.
1647 // The following code can use either the raw or cooked data, as needed.
1648 cookPointerData();
1649
1650 // Apply stylus pressure to current cooked state.
1651 applyExternalStylusTouchState(when);
1652
1653 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001654 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1655 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656 mCurrentCookedState.buttonState);
1657
1658 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001659 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001660 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1661 uint32_t id = idBits.clearFirstMarkedBit();
1662 const RawPointerData::Pointer& pointer =
1663 mCurrentRawState.rawPointerData.pointerForId(id);
1664 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1665 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1666 mCurrentCookedState.stylusIdBits.markBit(id);
1667 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1668 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1669 mCurrentCookedState.fingerIdBits.markBit(id);
1670 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1671 mCurrentCookedState.mouseIdBits.markBit(id);
1672 }
1673 }
1674 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1675 uint32_t id = idBits.clearFirstMarkedBit();
1676 const RawPointerData::Pointer& pointer =
1677 mCurrentRawState.rawPointerData.pointerForId(id);
1678 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1679 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1680 mCurrentCookedState.stylusIdBits.markBit(id);
1681 }
1682 }
1683
1684 // Stylus takes precedence over all tools, then mouse, then finger.
1685 PointerUsage pointerUsage = mPointerUsage;
1686 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1687 mCurrentCookedState.mouseIdBits.clear();
1688 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001689 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001690 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1691 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001692 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001693 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1694 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001695 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001696 }
1697
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001698 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001699 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001700 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001701 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001702 dispatchButtonRelease(when, readTime, policyFlags);
1703 dispatchHoverExit(when, readTime, policyFlags);
1704 dispatchTouches(when, readTime, policyFlags);
1705 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1706 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001707 }
1708
1709 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1710 mCurrentMotionAborted = false;
1711 }
1712 }
1713
1714 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001715 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001716 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1717 mCurrentCookedState.buttonState);
1718
1719 // Clear some transient state.
1720 mCurrentRawState.rawVScroll = 0;
1721 mCurrentRawState.rawHScroll = 0;
1722
1723 // Copy current touch to last touch in preparation for the next cycle.
1724 mLastRawState.copyFrom(mCurrentRawState);
1725 mLastCookedState.copyFrom(mCurrentCookedState);
1726}
1727
Garfield Tanc734e4f2021-01-15 20:01:39 -08001728void TouchInputMapper::updateTouchSpots() {
1729 if (!mConfig.showTouches || mPointerController == nullptr) {
1730 return;
1731 }
1732
1733 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1734 // clear touch spots.
1735 if (mDeviceMode != DeviceMode::DIRECT &&
1736 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1737 return;
1738 }
1739
1740 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1741 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1742
1743 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001744 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1745 mCurrentCookedState.cookedPointerData.idToIndex,
1746 mCurrentCookedState.cookedPointerData.touchingIdBits,
1747 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001748}
1749
1750bool TouchInputMapper::isTouchScreen() {
1751 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1752 mParameters.hasAssociatedDisplay;
1753}
1754
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001755void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001756 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001757 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1758 }
1759}
1760
1761void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1762 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1763 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1764
1765 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1766 float pressure = mExternalStylusState.pressure;
1767 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1768 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1769 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1770 }
1771 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1772 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1773
1774 PointerProperties& properties =
1775 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1776 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1777 properties.toolType = mExternalStylusState.toolType;
1778 }
1779 }
1780}
1781
1782bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001783 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001784 return false;
1785 }
1786
1787 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1788 state.rawPointerData.pointerCount != 0;
1789 if (initialDown) {
1790 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001791 if (DEBUG_STYLUS_FUSION) {
1792 ALOGD("Have both stylus and touch data, beginning fusion");
1793 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001794 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1795 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001796 if (DEBUG_STYLUS_FUSION) {
1797 ALOGD("Timeout expired, assuming touch is not a stylus.");
1798 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001799 resetExternalStylus();
1800 } else {
1801 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1802 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1803 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001804 if (DEBUG_STYLUS_FUSION) {
1805 ALOGD("No stylus data but stylus is connected, requesting timeout "
1806 "(%" PRId64 "ms)",
1807 mExternalStylusFusionTimeout);
1808 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001809 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1810 return true;
1811 }
1812 }
1813
1814 // Check if the stylus pointer has gone up.
1815 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001816 if (DEBUG_STYLUS_FUSION) {
1817 ALOGD("Stylus pointer is going up");
1818 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001819 mExternalStylusId = -1;
1820 }
1821
1822 return false;
1823}
1824
1825void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001826 if (mDeviceMode == DeviceMode::POINTER) {
1827 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001828 // Since this is a synthetic event, we can consider its latency to be zero
1829 const nsecs_t readTime = when;
1830 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001831 }
Michael Wright227c5542020-07-02 18:30:52 +01001832 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001833 if (mExternalStylusFusionTimeout < when) {
1834 processRawTouches(true /*timeout*/);
1835 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1836 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1837 }
1838 }
1839}
1840
1841void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1842 mExternalStylusState.copyFrom(state);
1843 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1844 // We're either in the middle of a fused stream of data or we're waiting on data before
1845 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1846 // data.
1847 mExternalStylusDataPending = true;
1848 processRawTouches(false /*timeout*/);
1849 }
1850}
1851
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001852bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001853 // Check for release of a virtual key.
1854 if (mCurrentVirtualKey.down) {
1855 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1856 // Pointer went up while virtual key was down.
1857 mCurrentVirtualKey.down = false;
1858 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001859 if (DEBUG_VIRTUAL_KEYS) {
1860 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1861 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1862 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001863 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001864 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1865 }
1866 return true;
1867 }
1868
1869 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1870 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1871 const RawPointerData::Pointer& pointer =
1872 mCurrentRawState.rawPointerData.pointerForId(id);
1873 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1874 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1875 // Pointer is still within the space of the virtual key.
1876 return true;
1877 }
1878 }
1879
1880 // Pointer left virtual key area or another pointer also went down.
1881 // Send key cancellation but do not consume the touch yet.
1882 // This is useful when the user swipes through from the virtual key area
1883 // into the main display surface.
1884 mCurrentVirtualKey.down = false;
1885 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001886 if (DEBUG_VIRTUAL_KEYS) {
1887 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1888 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1889 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001890 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001891 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1892 AKEY_EVENT_FLAG_CANCELED);
1893 }
1894 }
1895
1896 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1897 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1898 // Pointer just went down. Check for virtual key press or off-screen touches.
1899 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1900 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001901 // Skip checking whether the pointer is inside the physical frame if the device is in
1902 // unscaled mode.
1903 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1904 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001905 // If exactly one pointer went down, check for virtual key hit.
1906 // Otherwise we will drop the entire stroke.
1907 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1908 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1909 if (virtualKey) {
1910 mCurrentVirtualKey.down = true;
1911 mCurrentVirtualKey.downTime = when;
1912 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1913 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1914 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001915 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1916 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001917
1918 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001919 if (DEBUG_VIRTUAL_KEYS) {
1920 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1921 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1922 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001923 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001924 AKEY_EVENT_FLAG_FROM_SYSTEM |
1925 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1926 }
1927 }
1928 }
1929 return true;
1930 }
1931 }
1932
1933 // Disable all virtual key touches that happen within a short time interval of the
1934 // most recent touch within the screen area. The idea is to filter out stray
1935 // virtual key presses when interacting with the touch screen.
1936 //
1937 // Problems we're trying to solve:
1938 //
1939 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1940 // virtual key area that is implemented by a separate touch panel and accidentally
1941 // triggers a virtual key.
1942 //
1943 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1944 // area and accidentally triggers a virtual key. This often happens when virtual keys
1945 // are layed out below the screen near to where the on screen keyboard's space bar
1946 // is displayed.
1947 if (mConfig.virtualKeyQuietTime > 0 &&
1948 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001949 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001950 }
1951 return false;
1952}
1953
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001954void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001955 int32_t keyEventAction, int32_t keyEventFlags) {
1956 int32_t keyCode = mCurrentVirtualKey.keyCode;
1957 int32_t scanCode = mCurrentVirtualKey.scanCode;
1958 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001959 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001960 policyFlags |= POLICY_FLAG_VIRTUAL;
1961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001962 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1963 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1964 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001965 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001966}
1967
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001968void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
lilinnan687e58f2022-07-19 16:00:50 +08001969 if (mCurrentMotionAborted) {
1970 // Current motion event was already aborted.
1971 return;
1972 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001973 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1974 if (!currentIdBits.isEmpty()) {
1975 int32_t metaState = getContext()->getGlobalMetaState();
1976 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001977 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1978 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001979 mCurrentCookedState.cookedPointerData.pointerProperties,
1980 mCurrentCookedState.cookedPointerData.pointerCoords,
1981 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1982 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1983 mCurrentMotionAborted = true;
1984 }
1985}
1986
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001987void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001988 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1989 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1990 int32_t metaState = getContext()->getGlobalMetaState();
1991 int32_t buttonState = mCurrentCookedState.buttonState;
1992
1993 if (currentIdBits == lastIdBits) {
1994 if (!currentIdBits.isEmpty()) {
1995 // No pointer id changes so this is a move event.
1996 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001997 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1998 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001999 mCurrentCookedState.cookedPointerData.pointerProperties,
2000 mCurrentCookedState.cookedPointerData.pointerCoords,
2001 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
2002 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2003 }
2004 } else {
2005 // There may be pointers going up and pointers going down and pointers moving
2006 // all at the same time.
2007 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
2008 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
2009 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2010 BitSet32 dispatchedIdBits(lastIdBits.value);
2011
2012 // Update last coordinates of pointers that have moved so that we observe the new
2013 // pointer positions at the same time as other pointers that have just gone up.
2014 bool moveNeeded =
2015 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
2016 mCurrentCookedState.cookedPointerData.pointerCoords,
2017 mCurrentCookedState.cookedPointerData.idToIndex,
2018 mLastCookedState.cookedPointerData.pointerProperties,
2019 mLastCookedState.cookedPointerData.pointerCoords,
2020 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
2021 if (buttonState != mLastCookedState.buttonState) {
2022 moveNeeded = true;
2023 }
2024
2025 // Dispatch pointer up events.
2026 while (!upIdBits.isEmpty()) {
2027 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002028 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002029 if (isCanceled) {
2030 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2031 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002032 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08002033 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002034 mLastCookedState.cookedPointerData.pointerProperties,
2035 mLastCookedState.cookedPointerData.pointerCoords,
2036 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
2037 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2038 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002039 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002040 }
2041
2042 // Dispatch move events if any of the remaining pointers moved from their old locations.
2043 // Although applications receive new locations as part of individual pointer up
2044 // events, they do not generally handle them except when presented in a move event.
2045 if (moveNeeded && !moveIdBits.isEmpty()) {
2046 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002047 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2048 metaState, buttonState, 0,
2049 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002050 mCurrentCookedState.cookedPointerData.pointerCoords,
2051 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2052 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2053 }
2054
2055 // Dispatch pointer down events using the new pointer locations.
2056 while (!downIdBits.isEmpty()) {
2057 uint32_t downId = downIdBits.clearFirstMarkedBit();
2058 dispatchedIdBits.markBit(downId);
2059
2060 if (dispatchedIdBits.count() == 1) {
2061 // First pointer is going down. Set down time.
2062 mDownTime = when;
2063 }
2064
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002065 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2066 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002067 mCurrentCookedState.cookedPointerData.pointerProperties,
2068 mCurrentCookedState.cookedPointerData.pointerCoords,
2069 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2070 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2071 }
2072 }
2073}
2074
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002075void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002076 if (mSentHoverEnter &&
2077 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2078 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2079 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002080 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2081 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002082 mLastCookedState.cookedPointerData.pointerProperties,
2083 mLastCookedState.cookedPointerData.pointerCoords,
2084 mLastCookedState.cookedPointerData.idToIndex,
2085 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2086 mOrientedYPrecision, mDownTime);
2087 mSentHoverEnter = false;
2088 }
2089}
2090
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002091void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2092 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002093 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2094 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2095 int32_t metaState = getContext()->getGlobalMetaState();
2096 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002097 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2098 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002099 mCurrentCookedState.cookedPointerData.pointerProperties,
2100 mCurrentCookedState.cookedPointerData.pointerCoords,
2101 mCurrentCookedState.cookedPointerData.idToIndex,
2102 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2103 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2104 mSentHoverEnter = true;
2105 }
2106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002107 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2108 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002109 mCurrentCookedState.cookedPointerData.pointerProperties,
2110 mCurrentCookedState.cookedPointerData.pointerCoords,
2111 mCurrentCookedState.cookedPointerData.idToIndex,
2112 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2113 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2114 }
2115}
2116
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002117void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002118 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2119 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2120 const int32_t metaState = getContext()->getGlobalMetaState();
2121 int32_t buttonState = mLastCookedState.buttonState;
2122 while (!releasedButtons.isEmpty()) {
2123 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2124 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002125 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002126 actionButton, 0, metaState, buttonState, 0,
2127 mCurrentCookedState.cookedPointerData.pointerProperties,
2128 mCurrentCookedState.cookedPointerData.pointerCoords,
2129 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2130 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2131 }
2132}
2133
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002134void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002135 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2136 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2137 const int32_t metaState = getContext()->getGlobalMetaState();
2138 int32_t buttonState = mLastCookedState.buttonState;
2139 while (!pressedButtons.isEmpty()) {
2140 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2141 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002142 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2143 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002144 mCurrentCookedState.cookedPointerData.pointerProperties,
2145 mCurrentCookedState.cookedPointerData.pointerCoords,
2146 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2147 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2148 }
2149}
2150
2151const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2152 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2153 return cookedPointerData.touchingIdBits;
2154 }
2155 return cookedPointerData.hoveringIdBits;
2156}
2157
2158void TouchInputMapper::cookPointerData() {
2159 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2160
2161 mCurrentCookedState.cookedPointerData.clear();
2162 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2163 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2164 mCurrentRawState.rawPointerData.hoveringIdBits;
2165 mCurrentCookedState.cookedPointerData.touchingIdBits =
2166 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002167 mCurrentCookedState.cookedPointerData.canceledIdBits =
2168 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002169
2170 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2171 mCurrentCookedState.buttonState = 0;
2172 } else {
2173 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2174 }
2175
2176 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002177 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002178 for (uint32_t i = 0; i < currentPointerCount; i++) {
2179 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2180
2181 // Size
2182 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2183 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002184 case Calibration::SizeCalibration::GEOMETRIC:
2185 case Calibration::SizeCalibration::DIAMETER:
2186 case Calibration::SizeCalibration::BOX:
2187 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002188 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2189 touchMajor = in.touchMajor;
2190 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2191 toolMajor = in.toolMajor;
2192 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2193 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2194 : in.touchMajor;
2195 } else if (mRawPointerAxes.touchMajor.valid) {
2196 toolMajor = touchMajor = in.touchMajor;
2197 toolMinor = touchMinor =
2198 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2199 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2200 : in.touchMajor;
2201 } else if (mRawPointerAxes.toolMajor.valid) {
2202 touchMajor = toolMajor = in.toolMajor;
2203 touchMinor = toolMinor =
2204 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2205 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2206 : in.toolMajor;
2207 } else {
2208 ALOG_ASSERT(false,
2209 "No touch or tool axes. "
2210 "Size calibration should have been resolved to NONE.");
2211 touchMajor = 0;
2212 touchMinor = 0;
2213 toolMajor = 0;
2214 toolMinor = 0;
2215 size = 0;
2216 }
2217
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002218 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002219 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2220 if (touchingCount > 1) {
2221 touchMajor /= touchingCount;
2222 touchMinor /= touchingCount;
2223 toolMajor /= touchingCount;
2224 toolMinor /= touchingCount;
2225 size /= touchingCount;
2226 }
2227 }
2228
Michael Wright227c5542020-07-02 18:30:52 +01002229 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002230 touchMajor *= mGeometricScale;
2231 touchMinor *= mGeometricScale;
2232 toolMajor *= mGeometricScale;
2233 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002234 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002235 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2236 touchMinor = touchMajor;
2237 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2238 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002239 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002240 touchMinor = touchMajor;
2241 toolMinor = toolMajor;
2242 }
2243
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002244 mCalibration.applySizeScaleAndBias(touchMajor);
2245 mCalibration.applySizeScaleAndBias(touchMinor);
2246 mCalibration.applySizeScaleAndBias(toolMajor);
2247 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002248 size *= mSizeScale;
2249 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002250 case Calibration::SizeCalibration::DEFAULT:
2251 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2252 break;
2253 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002254 touchMajor = 0;
2255 touchMinor = 0;
2256 toolMajor = 0;
2257 toolMinor = 0;
2258 size = 0;
2259 break;
2260 }
2261
2262 // Pressure
2263 float pressure;
2264 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002265 case Calibration::PressureCalibration::PHYSICAL:
2266 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002267 pressure = in.pressure * mPressureScale;
2268 break;
2269 default:
2270 pressure = in.isHovering ? 0 : 1;
2271 break;
2272 }
2273
2274 // Tilt and Orientation
2275 float tilt;
2276 float orientation;
2277 if (mHaveTilt) {
2278 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2279 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2280 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2281 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2282 } else {
2283 tilt = 0;
2284
2285 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002286 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002287 orientation = in.orientation * mOrientationScale;
2288 break;
Michael Wright227c5542020-07-02 18:30:52 +01002289 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002290 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2291 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2292 if (c1 != 0 || c2 != 0) {
2293 orientation = atan2f(c1, c2) * 0.5f;
2294 float confidence = hypotf(c1, c2);
2295 float scale = 1.0f + confidence / 16.0f;
2296 touchMajor *= scale;
2297 touchMinor /= scale;
2298 toolMajor *= scale;
2299 toolMinor /= scale;
2300 } else {
2301 orientation = 0;
2302 }
2303 break;
2304 }
2305 default:
2306 orientation = 0;
2307 }
2308 }
2309
2310 // Distance
2311 float distance;
2312 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002313 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002314 distance = in.distance * mDistanceScale;
2315 break;
2316 default:
2317 distance = 0;
2318 }
2319
2320 // Coverage
2321 int32_t rawLeft, rawTop, rawRight, rawBottom;
2322 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002323 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002324 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2325 rawRight = in.toolMinor & 0x0000ffff;
2326 rawBottom = in.toolMajor & 0x0000ffff;
2327 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2328 break;
2329 default:
2330 rawLeft = rawTop = rawRight = rawBottom = 0;
2331 break;
2332 }
2333
2334 // Adjust X,Y coords for device calibration
2335 // TODO: Adjust coverage coords?
2336 float xTransformed = in.x, yTransformed = in.y;
2337 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002338 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002339
Prabir Pradhan1728b212021-10-19 16:00:03 -07002340 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002341 float left, top, right, bottom;
2342
Prabir Pradhan1728b212021-10-19 16:00:03 -07002343 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002344 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002345 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2346 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2347 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2348 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002349 orientation -= M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002350 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002351 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002352 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002353 }
2354 break;
2355 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002356 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2357 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002358 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2359 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002360 orientation -= M_PI;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002361 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002362 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002363 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002364 }
2365 break;
2366 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002367 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2368 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002369 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2370 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002371 orientation += M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002372 if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002373 orientation -=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002374 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002375 }
2376 break;
2377 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002378 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2379 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2380 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2381 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002382 break;
2383 }
2384
2385 // Write output coords.
2386 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2387 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002388 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2389 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002390 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2391 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2392 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2393 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2394 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2395 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2396 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002397 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002398 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2399 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2400 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2401 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2402 } else {
2403 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2404 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2405 }
2406
Chris Ye364fdb52020-08-05 15:07:56 -07002407 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002408 uint32_t id = in.id;
2409 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2410 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2411 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2412 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2413 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2414 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2415 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2416 }
2417
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002418 // Write output properties.
2419 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002420 properties.clear();
2421 properties.id = id;
2422 properties.toolType = in.toolType;
2423
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002424 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002425 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002426 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002427 }
2428}
2429
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002430void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002431 PointerUsage pointerUsage) {
2432 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002433 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002434 mPointerUsage = pointerUsage;
2435 }
2436
2437 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002438 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002439 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002440 break;
Michael Wright227c5542020-07-02 18:30:52 +01002441 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002442 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002443 break;
Michael Wright227c5542020-07-02 18:30:52 +01002444 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002445 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002446 break;
Michael Wright227c5542020-07-02 18:30:52 +01002447 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002448 break;
2449 }
2450}
2451
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002452void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002453 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002454 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002455 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002456 break;
Michael Wright227c5542020-07-02 18:30:52 +01002457 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002458 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002459 break;
Michael Wright227c5542020-07-02 18:30:52 +01002460 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002461 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002462 break;
Michael Wright227c5542020-07-02 18:30:52 +01002463 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002464 break;
2465 }
2466
Michael Wright227c5542020-07-02 18:30:52 +01002467 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002468}
2469
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002470void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2471 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002472 // Update current gesture coordinates.
2473 bool cancelPreviousGesture, finishPreviousGesture;
2474 bool sendEvents =
2475 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2476 if (!sendEvents) {
2477 return;
2478 }
2479 if (finishPreviousGesture) {
2480 cancelPreviousGesture = false;
2481 }
2482
2483 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002484 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002485 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002486 if (finishPreviousGesture || cancelPreviousGesture) {
2487 mPointerController->clearSpots();
2488 }
2489
Michael Wright227c5542020-07-02 18:30:52 +01002490 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002491 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2492 mPointerGesture.currentGestureIdToIndex,
2493 mPointerGesture.currentGestureIdBits,
2494 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002495 }
2496 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002497 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002498 }
2499
2500 // Show or hide the pointer if needed.
2501 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002502 case PointerGesture::Mode::NEUTRAL:
2503 case PointerGesture::Mode::QUIET:
2504 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2505 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002506 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002507 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002508 }
2509 break;
Michael Wright227c5542020-07-02 18:30:52 +01002510 case PointerGesture::Mode::TAP:
2511 case PointerGesture::Mode::TAP_DRAG:
2512 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2513 case PointerGesture::Mode::HOVER:
2514 case PointerGesture::Mode::PRESS:
2515 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002516 // Unfade the pointer when the current gesture manipulates the
2517 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002518 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002519 break;
Michael Wright227c5542020-07-02 18:30:52 +01002520 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002521 // Fade the pointer when the current gesture manipulates a different
2522 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002523 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002524 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002525 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002526 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002527 }
2528 break;
2529 }
2530
2531 // Send events!
2532 int32_t metaState = getContext()->getGlobalMetaState();
2533 int32_t buttonState = mCurrentCookedState.buttonState;
2534
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002535 uint32_t flags = 0;
2536
2537 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2538 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2539 }
2540
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002541 // Update last coordinates of pointers that have moved so that we observe the new
2542 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002543 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2544 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2545 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2546 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2547 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2548 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002549 bool moveNeeded = false;
2550 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2551 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2552 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2553 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2554 mPointerGesture.lastGestureIdBits.value);
2555 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2556 mPointerGesture.currentGestureCoords,
2557 mPointerGesture.currentGestureIdToIndex,
2558 mPointerGesture.lastGestureProperties,
2559 mPointerGesture.lastGestureCoords,
2560 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2561 if (buttonState != mLastCookedState.buttonState) {
2562 moveNeeded = true;
2563 }
2564 }
2565
2566 // Send motion events for all pointers that went up or were canceled.
2567 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2568 if (!dispatchedGestureIdBits.isEmpty()) {
2569 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002570 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2571 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002572 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2573 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2574 mPointerGesture.downTime);
2575
2576 dispatchedGestureIdBits.clear();
2577 } else {
2578 BitSet32 upGestureIdBits;
2579 if (finishPreviousGesture) {
2580 upGestureIdBits = dispatchedGestureIdBits;
2581 } else {
2582 upGestureIdBits.value =
2583 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2584 }
2585 while (!upGestureIdBits.isEmpty()) {
2586 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2587
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002588 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002589 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002590 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002591 mPointerGesture.lastGestureCoords,
2592 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2593 0, mPointerGesture.downTime);
2594
2595 dispatchedGestureIdBits.clearBit(id);
2596 }
2597 }
2598 }
2599
2600 // Send motion events for all pointers that moved.
2601 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002602 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002603 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002604 mPointerGesture.currentGestureProperties,
2605 mPointerGesture.currentGestureCoords,
2606 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2607 mPointerGesture.downTime);
2608 }
2609
2610 // Send motion events for all pointers that went down.
2611 if (down) {
2612 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2613 ~dispatchedGestureIdBits.value);
2614 while (!downGestureIdBits.isEmpty()) {
2615 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2616 dispatchedGestureIdBits.markBit(id);
2617
2618 if (dispatchedGestureIdBits.count() == 1) {
2619 mPointerGesture.downTime = when;
2620 }
2621
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002622 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002623 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002624 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002625 mPointerGesture.currentGestureCoords,
2626 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2627 0, mPointerGesture.downTime);
2628 }
2629 }
2630
2631 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002632 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002633 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2634 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002635 mPointerGesture.currentGestureProperties,
2636 mPointerGesture.currentGestureCoords,
2637 mPointerGesture.currentGestureIdToIndex,
2638 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2639 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2640 // Synthesize a hover move event after all pointers go up to indicate that
2641 // the pointer is hovering again even if the user is not currently touching
2642 // the touch pad. This ensures that a view will receive a fresh hover enter
2643 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002644 float x, y;
2645 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002646
2647 PointerProperties pointerProperties;
2648 pointerProperties.clear();
2649 pointerProperties.id = 0;
2650 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2651
2652 PointerCoords pointerCoords;
2653 pointerCoords.clear();
2654 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2655 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2656
2657 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002658 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002659 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002660 metaState, buttonState, MotionClassification::NONE,
2661 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2662 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002663 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002664 }
2665
2666 // Update state.
2667 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2668 if (!down) {
2669 mPointerGesture.lastGestureIdBits.clear();
2670 } else {
2671 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2672 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2673 uint32_t id = idBits.clearFirstMarkedBit();
2674 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2675 mPointerGesture.lastGestureProperties[index].copyFrom(
2676 mPointerGesture.currentGestureProperties[index]);
2677 mPointerGesture.lastGestureCoords[index].copyFrom(
2678 mPointerGesture.currentGestureCoords[index]);
2679 mPointerGesture.lastGestureIdToIndex[id] = index;
2680 }
2681 }
2682}
2683
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002684void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002685 // Cancel previously dispatches pointers.
2686 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2687 int32_t metaState = getContext()->getGlobalMetaState();
2688 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002689 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2690 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002691 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2692 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2693 0, 0, mPointerGesture.downTime);
2694 }
2695
2696 // Reset the current pointer gesture.
2697 mPointerGesture.reset();
2698 mPointerVelocityControl.reset();
2699
2700 // Remove any current spots.
2701 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002702 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002703 mPointerController->clearSpots();
2704 }
2705}
2706
2707bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2708 bool* outFinishPreviousGesture, bool isTimeout) {
2709 *outCancelPreviousGesture = false;
2710 *outFinishPreviousGesture = false;
2711
2712 // Handle TAP timeout.
2713 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002714 if (DEBUG_GESTURES) {
2715 ALOGD("Gestures: Processing timeout");
2716 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002717
Michael Wright227c5542020-07-02 18:30:52 +01002718 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002719 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2720 // The tap/drag timeout has not yet expired.
2721 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2722 mConfig.pointerGestureTapDragInterval);
2723 } else {
2724 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002725 if (DEBUG_GESTURES) {
2726 ALOGD("Gestures: TAP finished");
2727 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002728 *outFinishPreviousGesture = true;
2729
2730 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002731 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002732 mPointerGesture.currentGestureIdBits.clear();
2733
2734 mPointerVelocityControl.reset();
2735 return true;
2736 }
2737 }
2738
2739 // We did not handle this timeout.
2740 return false;
2741 }
2742
2743 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2744 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2745
2746 // Update the velocity tracker.
2747 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002748 std::vector<VelocityTracker::Position> positions;
2749 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002750 uint32_t id = idBits.clearFirstMarkedBit();
2751 const RawPointerData::Pointer& pointer =
2752 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002753 float x = pointer.x * mPointerXMovementScale;
2754 float y = pointer.y * mPointerYMovementScale;
2755 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002756 }
2757 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2758 positions);
2759 }
2760
2761 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2762 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002763 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2764 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2765 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002766 mPointerGesture.resetTap();
2767 }
2768
2769 // Pick a new active touch id if needed.
2770 // Choose an arbitrary pointer that just went down, if there is one.
2771 // Otherwise choose an arbitrary remaining pointer.
2772 // This guarantees we always have an active touch id when there is at least one pointer.
2773 // We keep the same active touch id for as long as possible.
2774 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2775 int32_t activeTouchId = lastActiveTouchId;
2776 if (activeTouchId < 0) {
2777 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2778 activeTouchId = mPointerGesture.activeTouchId =
2779 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2780 mPointerGesture.firstTouchTime = when;
2781 }
2782 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2783 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2784 activeTouchId = mPointerGesture.activeTouchId =
2785 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2786 } else {
2787 activeTouchId = mPointerGesture.activeTouchId = -1;
2788 }
2789 }
2790
2791 // Determine whether we are in quiet time.
2792 bool isQuietTime = false;
2793 if (activeTouchId < 0) {
2794 mPointerGesture.resetQuietTime();
2795 } else {
2796 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2797 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002798 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2799 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2800 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002801 currentFingerCount < 2) {
2802 // Enter quiet time when exiting swipe or freeform state.
2803 // This is to prevent accidentally entering the hover state and flinging the
2804 // pointer when finishing a swipe and there is still one pointer left onscreen.
2805 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002806 } else if (mPointerGesture.lastGestureMode ==
2807 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002808 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2809 // Enter quiet time when releasing the button and there are still two or more
2810 // fingers down. This may indicate that one finger was used to press the button
2811 // but it has not gone up yet.
2812 isQuietTime = true;
2813 }
2814 if (isQuietTime) {
2815 mPointerGesture.quietTime = when;
2816 }
2817 }
2818 }
2819
2820 // Switch states based on button and pointer state.
2821 if (isQuietTime) {
2822 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002823 if (DEBUG_GESTURES) {
2824 ALOGD("Gestures: QUIET for next %0.3fms",
2825 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2826 0.000001f);
2827 }
Michael Wright227c5542020-07-02 18:30:52 +01002828 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002829 *outFinishPreviousGesture = true;
2830 }
2831
2832 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002833 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002834 mPointerGesture.currentGestureIdBits.clear();
2835
2836 mPointerVelocityControl.reset();
2837 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2838 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2839 // The pointer follows the active touch point.
2840 // Emit DOWN, MOVE, UP events at the pointer location.
2841 //
2842 // Only the active touch matters; other fingers are ignored. This policy helps
2843 // to handle the case where the user places a second finger on the touch pad
2844 // to apply the necessary force to depress an integrated button below the surface.
2845 // We don't want the second finger to be delivered to applications.
2846 //
2847 // For this to work well, we need to make sure to track the pointer that is really
2848 // active. If the user first puts one finger down to click then adds another
2849 // finger to drag then the active pointer should switch to the finger that is
2850 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002851 if (DEBUG_GESTURES) {
2852 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2853 "currentFingerCount=%d",
2854 activeTouchId, currentFingerCount);
2855 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002856 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002857 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002858 *outFinishPreviousGesture = true;
2859 mPointerGesture.activeGestureId = 0;
2860 }
2861
2862 // Switch pointers if needed.
2863 // Find the fastest pointer and follow it.
2864 if (activeTouchId >= 0 && currentFingerCount > 1) {
2865 int32_t bestId = -1;
2866 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2867 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2868 uint32_t id = idBits.clearFirstMarkedBit();
2869 float vx, vy;
2870 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2871 float speed = hypotf(vx, vy);
2872 if (speed > bestSpeed) {
2873 bestId = id;
2874 bestSpeed = speed;
2875 }
2876 }
2877 }
2878 if (bestId >= 0 && bestId != activeTouchId) {
2879 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002880 if (DEBUG_GESTURES) {
2881 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2882 "bestId=%d, bestSpeed=%0.3f",
2883 bestId, bestSpeed);
2884 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002885 }
2886 }
2887
2888 float deltaX = 0, deltaY = 0;
2889 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2890 const RawPointerData::Pointer& currentPointer =
2891 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2892 const RawPointerData::Pointer& lastPointer =
2893 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2894 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2895 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2896
Prabir Pradhan1728b212021-10-19 16:00:03 -07002897 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002898 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2899
2900 // Move the pointer using a relative motion.
2901 // When using spots, the click will occur at the position of the anchor
2902 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002903 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002904 } else {
2905 mPointerVelocityControl.reset();
2906 }
2907
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002908 float x, y;
2909 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002910
Michael Wright227c5542020-07-02 18:30:52 +01002911 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002912 mPointerGesture.currentGestureIdBits.clear();
2913 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2914 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2915 mPointerGesture.currentGestureProperties[0].clear();
2916 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2917 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2918 mPointerGesture.currentGestureCoords[0].clear();
2919 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2920 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2921 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2922 } else if (currentFingerCount == 0) {
2923 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002924 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002925 *outFinishPreviousGesture = true;
2926 }
2927
2928 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2929 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2930 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002931 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2932 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002933 lastFingerCount == 1) {
2934 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002935 float x, y;
2936 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002937 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2938 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002939 if (DEBUG_GESTURES) {
2940 ALOGD("Gestures: TAP");
2941 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002942
2943 mPointerGesture.tapUpTime = when;
2944 getContext()->requestTimeoutAtTime(when +
2945 mConfig.pointerGestureTapDragInterval);
2946
2947 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002948 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002949 mPointerGesture.currentGestureIdBits.clear();
2950 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2951 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2952 mPointerGesture.currentGestureProperties[0].clear();
2953 mPointerGesture.currentGestureProperties[0].id =
2954 mPointerGesture.activeGestureId;
2955 mPointerGesture.currentGestureProperties[0].toolType =
2956 AMOTION_EVENT_TOOL_TYPE_FINGER;
2957 mPointerGesture.currentGestureCoords[0].clear();
2958 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2959 mPointerGesture.tapX);
2960 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2961 mPointerGesture.tapY);
2962 mPointerGesture.currentGestureCoords[0]
2963 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2964
2965 tapped = true;
2966 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002967 if (DEBUG_GESTURES) {
2968 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2969 y - mPointerGesture.tapY);
2970 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002971 }
2972 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002973 if (DEBUG_GESTURES) {
2974 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2975 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2976 (when - mPointerGesture.tapDownTime) * 0.000001f);
2977 } else {
2978 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2979 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002980 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002981 }
2982 }
2983
2984 mPointerVelocityControl.reset();
2985
2986 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002987 if (DEBUG_GESTURES) {
2988 ALOGD("Gestures: NEUTRAL");
2989 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002990 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002991 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002992 mPointerGesture.currentGestureIdBits.clear();
2993 }
2994 } else if (currentFingerCount == 1) {
2995 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2996 // The pointer follows the active touch point.
2997 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2998 // When in TAP_DRAG, emit MOVE events at the pointer location.
2999 ALOG_ASSERT(activeTouchId >= 0);
3000
Michael Wright227c5542020-07-02 18:30:52 +01003001 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
3002 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003003 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003004 float x, y;
3005 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003006 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
3007 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01003008 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003009 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003010 if (DEBUG_GESTURES) {
3011 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3012 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
3013 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003014 }
3015 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003016 if (DEBUG_GESTURES) {
3017 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3018 (when - mPointerGesture.tapUpTime) * 0.000001f);
3019 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003020 }
Michael Wright227c5542020-07-02 18:30:52 +01003021 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
3022 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003023 }
3024
3025 float deltaX = 0, deltaY = 0;
3026 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
3027 const RawPointerData::Pointer& currentPointer =
3028 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
3029 const RawPointerData::Pointer& lastPointer =
3030 mLastRawState.rawPointerData.pointerForId(activeTouchId);
3031 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3032 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3033
Prabir Pradhan1728b212021-10-19 16:00:03 -07003034 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003035 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3036
3037 // Move the pointer using a relative motion.
3038 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003039 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003040 } else {
3041 mPointerVelocityControl.reset();
3042 }
3043
3044 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003045 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003046 if (DEBUG_GESTURES) {
3047 ALOGD("Gestures: TAP_DRAG");
3048 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003049 down = true;
3050 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003051 if (DEBUG_GESTURES) {
3052 ALOGD("Gestures: HOVER");
3053 }
Michael Wright227c5542020-07-02 18:30:52 +01003054 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003055 *outFinishPreviousGesture = true;
3056 }
3057 mPointerGesture.activeGestureId = 0;
3058 down = false;
3059 }
3060
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003061 float x, y;
3062 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003063
3064 mPointerGesture.currentGestureIdBits.clear();
3065 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3066 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3067 mPointerGesture.currentGestureProperties[0].clear();
3068 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3069 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3070 mPointerGesture.currentGestureCoords[0].clear();
3071 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3072 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3073 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3074 down ? 1.0f : 0.0f);
3075
3076 if (lastFingerCount == 0 && currentFingerCount != 0) {
3077 mPointerGesture.resetTap();
3078 mPointerGesture.tapDownTime = when;
3079 mPointerGesture.tapX = x;
3080 mPointerGesture.tapY = y;
3081 }
3082 } else {
3083 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3084 // We need to provide feedback for each finger that goes down so we cannot wait
3085 // for the fingers to move before deciding what to do.
3086 //
3087 // The ambiguous case is deciding what to do when there are two fingers down but they
3088 // have not moved enough to determine whether they are part of a drag or part of a
3089 // freeform gesture, or just a press or long-press at the pointer location.
3090 //
3091 // When there are two fingers we start with the PRESS hypothesis and we generate a
3092 // down at the pointer location.
3093 //
3094 // When the two fingers move enough or when additional fingers are added, we make
3095 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3096 ALOG_ASSERT(activeTouchId >= 0);
3097
3098 bool settled = when >=
3099 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003100 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3101 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3102 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003103 *outFinishPreviousGesture = true;
3104 } else if (!settled && currentFingerCount > lastFingerCount) {
3105 // Additional pointers have gone down but not yet settled.
3106 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003107 if (DEBUG_GESTURES) {
3108 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3109 "MULTITOUCH, settle time remaining %0.3fms",
3110 (mPointerGesture.firstTouchTime +
3111 mConfig.pointerGestureMultitouchSettleInterval - when) *
3112 0.000001f);
3113 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003114 *outCancelPreviousGesture = true;
3115 } else {
3116 // Continue previous gesture.
3117 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3118 }
3119
3120 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003121 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003122 mPointerGesture.activeGestureId = 0;
3123 mPointerGesture.referenceIdBits.clear();
3124 mPointerVelocityControl.reset();
3125
3126 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003127 if (DEBUG_GESTURES) {
3128 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3129 "settle time remaining %0.3fms",
3130 (mPointerGesture.firstTouchTime +
3131 mConfig.pointerGestureMultitouchSettleInterval - when) *
3132 0.000001f);
3133 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003134 mCurrentRawState.rawPointerData
3135 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3136 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003137 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3138 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003139 }
3140
3141 // Clear the reference deltas for fingers not yet included in the reference calculation.
3142 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3143 ~mPointerGesture.referenceIdBits.value);
3144 !idBits.isEmpty();) {
3145 uint32_t id = idBits.clearFirstMarkedBit();
3146 mPointerGesture.referenceDeltas[id].dx = 0;
3147 mPointerGesture.referenceDeltas[id].dy = 0;
3148 }
3149 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3150
3151 // Add delta for all fingers and calculate a common movement delta.
3152 float commonDeltaX = 0, commonDeltaY = 0;
3153 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3154 mCurrentCookedState.fingerIdBits.value);
3155 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3156 bool first = (idBits == commonIdBits);
3157 uint32_t id = idBits.clearFirstMarkedBit();
3158 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3159 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3160 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3161 delta.dx += cpd.x - lpd.x;
3162 delta.dy += cpd.y - lpd.y;
3163
3164 if (first) {
3165 commonDeltaX = delta.dx;
3166 commonDeltaY = delta.dy;
3167 } else {
3168 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3169 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3170 }
3171 }
3172
3173 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003174 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003175 float dist[MAX_POINTER_ID + 1];
3176 int32_t distOverThreshold = 0;
3177 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3178 uint32_t id = idBits.clearFirstMarkedBit();
3179 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3180 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3181 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3182 distOverThreshold += 1;
3183 }
3184 }
3185
3186 // Only transition when at least two pointers have moved further than
3187 // the minimum distance threshold.
3188 if (distOverThreshold >= 2) {
3189 if (currentFingerCount > 2) {
3190 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003191 if (DEBUG_GESTURES) {
3192 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3193 currentFingerCount);
3194 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003195 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003196 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003197 } else {
3198 // There are exactly two pointers.
3199 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3200 uint32_t id1 = idBits.clearFirstMarkedBit();
3201 uint32_t id2 = idBits.firstMarkedBit();
3202 const RawPointerData::Pointer& p1 =
3203 mCurrentRawState.rawPointerData.pointerForId(id1);
3204 const RawPointerData::Pointer& p2 =
3205 mCurrentRawState.rawPointerData.pointerForId(id2);
3206 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3207 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3208 // There are two pointers but they are too far apart for a SWIPE,
3209 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003210 if (DEBUG_GESTURES) {
3211 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3212 "%0.3f",
3213 mutualDistance, mPointerGestureMaxSwipeWidth);
3214 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003215 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003216 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003217 } else {
3218 // There are two pointers. Wait for both pointers to start moving
3219 // before deciding whether this is a SWIPE or FREEFORM gesture.
3220 float dist1 = dist[id1];
3221 float dist2 = dist[id2];
3222 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3223 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3224 // Calculate the dot product of the displacement vectors.
3225 // When the vectors are oriented in approximately the same direction,
3226 // the angle betweeen them is near zero and the cosine of the angle
3227 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3228 // mag(v2).
3229 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3230 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3231 float dx1 = delta1.dx * mPointerXZoomScale;
3232 float dy1 = delta1.dy * mPointerYZoomScale;
3233 float dx2 = delta2.dx * mPointerXZoomScale;
3234 float dy2 = delta2.dy * mPointerYZoomScale;
3235 float dot = dx1 * dx2 + dy1 * dy2;
3236 float cosine = dot / (dist1 * dist2); // denominator always > 0
3237 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3238 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003239 if (DEBUG_GESTURES) {
3240 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3241 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3242 "cosine %0.3f >= %0.3f",
3243 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3244 mConfig.pointerGestureMultitouchMinDistance, cosine,
3245 mConfig.pointerGestureSwipeTransitionAngleCosine);
3246 }
Michael Wright227c5542020-07-02 18:30:52 +01003247 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003248 } else {
3249 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003250 if (DEBUG_GESTURES) {
3251 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3252 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3253 "cosine %0.3f < %0.3f",
3254 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3255 mConfig.pointerGestureMultitouchMinDistance, cosine,
3256 mConfig.pointerGestureSwipeTransitionAngleCosine);
3257 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003258 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003259 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003260 }
3261 }
3262 }
3263 }
3264 }
Michael Wright227c5542020-07-02 18:30:52 +01003265 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003266 // Switch from SWIPE to FREEFORM if additional pointers go down.
3267 // Cancel previous gesture.
3268 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003269 if (DEBUG_GESTURES) {
3270 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3271 currentFingerCount);
3272 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003273 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003274 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003275 }
3276 }
3277
3278 // Move the reference points based on the overall group motion of the fingers
3279 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003280 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003281 (commonDeltaX || commonDeltaY)) {
3282 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3283 uint32_t id = idBits.clearFirstMarkedBit();
3284 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3285 delta.dx = 0;
3286 delta.dy = 0;
3287 }
3288
3289 mPointerGesture.referenceTouchX += commonDeltaX;
3290 mPointerGesture.referenceTouchY += commonDeltaY;
3291
3292 commonDeltaX *= mPointerXMovementScale;
3293 commonDeltaY *= mPointerYMovementScale;
3294
Prabir Pradhan1728b212021-10-19 16:00:03 -07003295 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003296 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3297
3298 mPointerGesture.referenceGestureX += commonDeltaX;
3299 mPointerGesture.referenceGestureY += commonDeltaY;
3300 }
3301
3302 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003303 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3304 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003305 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003306 if (DEBUG_GESTURES) {
3307 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3308 "activeGestureId=%d, currentTouchPointerCount=%d",
3309 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3310 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003311 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3312
3313 mPointerGesture.currentGestureIdBits.clear();
3314 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3315 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3316 mPointerGesture.currentGestureProperties[0].clear();
3317 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3318 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3319 mPointerGesture.currentGestureCoords[0].clear();
3320 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3321 mPointerGesture.referenceGestureX);
3322 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3323 mPointerGesture.referenceGestureY);
3324 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003325 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003326 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003327 if (DEBUG_GESTURES) {
3328 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3329 "activeGestureId=%d, currentTouchPointerCount=%d",
3330 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3331 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003332 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3333
3334 mPointerGesture.currentGestureIdBits.clear();
3335
3336 BitSet32 mappedTouchIdBits;
3337 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003338 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003339 // Initially, assign the active gesture id to the active touch point
3340 // if there is one. No other touch id bits are mapped yet.
3341 if (!*outCancelPreviousGesture) {
3342 mappedTouchIdBits.markBit(activeTouchId);
3343 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3344 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3345 mPointerGesture.activeGestureId;
3346 } else {
3347 mPointerGesture.activeGestureId = -1;
3348 }
3349 } else {
3350 // Otherwise, assume we mapped all touches from the previous frame.
3351 // Reuse all mappings that are still applicable.
3352 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3353 mCurrentCookedState.fingerIdBits.value;
3354 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3355
3356 // Check whether we need to choose a new active gesture id because the
3357 // current went went up.
3358 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3359 ~mCurrentCookedState.fingerIdBits.value);
3360 !upTouchIdBits.isEmpty();) {
3361 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3362 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3363 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3364 mPointerGesture.activeGestureId = -1;
3365 break;
3366 }
3367 }
3368 }
3369
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003370 if (DEBUG_GESTURES) {
3371 ALOGD("Gestures: FREEFORM follow up "
3372 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3373 "activeGestureId=%d",
3374 mappedTouchIdBits.value, usedGestureIdBits.value,
3375 mPointerGesture.activeGestureId);
3376 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003377
3378 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3379 for (uint32_t i = 0; i < currentFingerCount; i++) {
3380 uint32_t touchId = idBits.clearFirstMarkedBit();
3381 uint32_t gestureId;
3382 if (!mappedTouchIdBits.hasBit(touchId)) {
3383 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3384 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003385 if (DEBUG_GESTURES) {
3386 ALOGD("Gestures: FREEFORM "
3387 "new mapping for touch id %d -> gesture id %d",
3388 touchId, gestureId);
3389 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003390 } else {
3391 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003392 if (DEBUG_GESTURES) {
3393 ALOGD("Gestures: FREEFORM "
3394 "existing mapping for touch id %d -> gesture id %d",
3395 touchId, gestureId);
3396 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003397 }
3398 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3399 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3400
3401 const RawPointerData::Pointer& pointer =
3402 mCurrentRawState.rawPointerData.pointerForId(touchId);
3403 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3404 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003405 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003406
3407 mPointerGesture.currentGestureProperties[i].clear();
3408 mPointerGesture.currentGestureProperties[i].id = gestureId;
3409 mPointerGesture.currentGestureProperties[i].toolType =
3410 AMOTION_EVENT_TOOL_TYPE_FINGER;
3411 mPointerGesture.currentGestureCoords[i].clear();
3412 mPointerGesture.currentGestureCoords[i]
3413 .setAxisValue(AMOTION_EVENT_AXIS_X,
3414 mPointerGesture.referenceGestureX + deltaX);
3415 mPointerGesture.currentGestureCoords[i]
3416 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3417 mPointerGesture.referenceGestureY + deltaY);
3418 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3419 1.0f);
3420 }
3421
3422 if (mPointerGesture.activeGestureId < 0) {
3423 mPointerGesture.activeGestureId =
3424 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003425 if (DEBUG_GESTURES) {
3426 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3427 mPointerGesture.activeGestureId);
3428 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003429 }
3430 }
3431 }
3432
3433 mPointerController->setButtonState(mCurrentRawState.buttonState);
3434
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003435 if (DEBUG_GESTURES) {
3436 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3437 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3438 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3439 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3440 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3441 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3442 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3443 uint32_t id = idBits.clearFirstMarkedBit();
3444 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3445 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3446 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3447 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3448 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3449 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3450 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3451 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3452 }
3453 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3454 uint32_t id = idBits.clearFirstMarkedBit();
3455 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3456 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3457 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3458 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3459 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3460 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3461 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3462 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3463 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003464 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003465 return true;
3466}
3467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003469 mPointerSimple.currentCoords.clear();
3470 mPointerSimple.currentProperties.clear();
3471
3472 bool down, hovering;
3473 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3474 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3475 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003476 mPointerController
3477 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3478 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003479
3480 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3481 down = !hovering;
3482
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003483 float x, y;
3484 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003485 mPointerSimple.currentCoords.copyFrom(
3486 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3487 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3488 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3489 mPointerSimple.currentProperties.id = 0;
3490 mPointerSimple.currentProperties.toolType =
3491 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3492 } else {
3493 down = false;
3494 hovering = false;
3495 }
3496
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003498}
3499
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3501 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003502}
3503
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003504void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003505 mPointerSimple.currentCoords.clear();
3506 mPointerSimple.currentProperties.clear();
3507
3508 bool down, hovering;
3509 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3510 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3511 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3512 float deltaX = 0, deltaY = 0;
3513 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3514 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3515 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3516 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3517 mPointerXMovementScale;
3518 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3519 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3520 mPointerYMovementScale;
3521
Prabir Pradhan1728b212021-10-19 16:00:03 -07003522 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003523 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3524
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003525 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003526 } else {
3527 mPointerVelocityControl.reset();
3528 }
3529
3530 down = isPointerDown(mCurrentRawState.buttonState);
3531 hovering = !down;
3532
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003533 float x, y;
3534 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003535 mPointerSimple.currentCoords.copyFrom(
3536 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3537 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3538 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3539 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3540 hovering ? 0.0f : 1.0f);
3541 mPointerSimple.currentProperties.id = 0;
3542 mPointerSimple.currentProperties.toolType =
3543 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3544 } else {
3545 mPointerVelocityControl.reset();
3546
3547 down = false;
3548 hovering = false;
3549 }
3550
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003551 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003552}
3553
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003554void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3555 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003556
3557 mPointerVelocityControl.reset();
3558}
3559
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3561 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003562 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003563
3564 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003565 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003566 mPointerController->clearSpots();
3567 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003568 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003569 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003570 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003571 }
Garfield Tan9514d782020-11-10 16:37:23 -08003572 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003573
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003574 float xCursorPosition, yCursorPosition;
3575 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003576
3577 if (mPointerSimple.down && !down) {
3578 mPointerSimple.down = false;
3579
3580 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003581 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3582 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003583 mLastRawState.buttonState, MotionClassification::NONE,
3584 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3585 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3586 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3587 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003588 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003589 }
3590
3591 if (mPointerSimple.hovering && !hovering) {
3592 mPointerSimple.hovering = false;
3593
3594 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003595 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3596 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3597 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003598 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3599 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3600 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3601 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003602 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003603 }
3604
3605 if (down) {
3606 if (!mPointerSimple.down) {
3607 mPointerSimple.down = true;
3608 mPointerSimple.downTime = when;
3609
3610 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003611 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003612 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3613 metaState, mCurrentRawState.buttonState,
3614 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3615 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3616 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3617 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003618 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003619 }
3620
3621 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3623 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003624 mCurrentRawState.buttonState, MotionClassification::NONE,
3625 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3626 &mPointerSimple.currentCoords, mOrientedXPrecision,
3627 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3628 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003629 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003630 }
3631
3632 if (hovering) {
3633 if (!mPointerSimple.hovering) {
3634 mPointerSimple.hovering = true;
3635
3636 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003637 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003638 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3639 metaState, mCurrentRawState.buttonState,
3640 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3641 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3642 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3643 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003644 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003645 }
3646
3647 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3649 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3650 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003651 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3652 &mPointerSimple.currentCoords, mOrientedXPrecision,
3653 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3654 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003655 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003656 }
3657
3658 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3659 float vscroll = mCurrentRawState.rawVScroll;
3660 float hscroll = mCurrentRawState.rawHScroll;
3661 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3662 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3663
3664 // Send scroll.
3665 PointerCoords pointerCoords;
3666 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3667 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3668 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3669
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3671 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003672 mCurrentRawState.buttonState, MotionClassification::NONE,
3673 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3674 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3675 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3676 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003677 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003678 }
3679
3680 // Save state.
3681 if (down || hovering) {
3682 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3683 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3684 } else {
3685 mPointerSimple.reset();
3686 }
3687}
3688
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003690 mPointerSimple.currentCoords.clear();
3691 mPointerSimple.currentProperties.clear();
3692
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003693 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003694}
3695
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3697 uint32_t source, int32_t action, int32_t actionButton,
3698 int32_t flags, int32_t metaState, int32_t buttonState,
3699 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003700 const PointerCoords* coords, const uint32_t* idToIndex,
3701 BitSet32 idBits, int32_t changedId, float xPrecision,
3702 float yPrecision, nsecs_t downTime) {
3703 PointerCoords pointerCoords[MAX_POINTERS];
3704 PointerProperties pointerProperties[MAX_POINTERS];
3705 uint32_t pointerCount = 0;
3706 while (!idBits.isEmpty()) {
3707 uint32_t id = idBits.clearFirstMarkedBit();
3708 uint32_t index = idToIndex[id];
3709 pointerProperties[pointerCount].copyFrom(properties[index]);
3710 pointerCoords[pointerCount].copyFrom(coords[index]);
3711
3712 if (changedId >= 0 && id == uint32_t(changedId)) {
3713 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3714 }
3715
3716 pointerCount += 1;
3717 }
3718
3719 ALOG_ASSERT(pointerCount != 0);
3720
3721 if (changedId >= 0 && pointerCount == 1) {
3722 // Replace initial down and final up action.
3723 // We can compare the action without masking off the changed pointer index
3724 // because we know the index is 0.
3725 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3726 action = AMOTION_EVENT_ACTION_DOWN;
3727 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003728 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3729 action = AMOTION_EVENT_ACTION_CANCEL;
3730 } else {
3731 action = AMOTION_EVENT_ACTION_UP;
3732 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003733 } else {
3734 // Can't happen.
3735 ALOG_ASSERT(false);
3736 }
3737 }
3738 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3739 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003740 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003741 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003742 }
3743 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3744 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003745 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003746 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003747 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003748 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3749 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003750 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3751 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3752 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003753 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003754}
3755
3756bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3757 const PointerCoords* inCoords,
3758 const uint32_t* inIdToIndex,
3759 PointerProperties* outProperties,
3760 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3761 BitSet32 idBits) const {
3762 bool changed = false;
3763 while (!idBits.isEmpty()) {
3764 uint32_t id = idBits.clearFirstMarkedBit();
3765 uint32_t inIndex = inIdToIndex[id];
3766 uint32_t outIndex = outIdToIndex[id];
3767
3768 const PointerProperties& curInProperties = inProperties[inIndex];
3769 const PointerCoords& curInCoords = inCoords[inIndex];
3770 PointerProperties& curOutProperties = outProperties[outIndex];
3771 PointerCoords& curOutCoords = outCoords[outIndex];
3772
3773 if (curInProperties != curOutProperties) {
3774 curOutProperties.copyFrom(curInProperties);
3775 changed = true;
3776 }
3777
3778 if (curInCoords != curOutCoords) {
3779 curOutCoords.copyFrom(curInCoords);
3780 changed = true;
3781 }
3782 }
3783 return changed;
3784}
3785
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3787 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3788 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003789}
3790
Prabir Pradhan1728b212021-10-19 16:00:03 -07003791// Transform input device coordinates to display panel coordinates.
3792void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003793 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3794 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3795
arthurhunga36b28e2020-12-29 20:28:15 +08003796 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3797 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3798
Prabir Pradhan1728b212021-10-19 16:00:03 -07003799 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003800 // 0 - no swap and reverse.
3801 // 90 - swap x/y and reverse y.
3802 // 180 - reverse x, y.
3803 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003804 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003805 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003806 x = xScaled;
3807 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003808 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003809 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003810 y = xScaledMax;
3811 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003812 break;
3813 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003814 x = xScaledMax;
3815 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003816 break;
3817 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003818 y = xScaled;
3819 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003820 break;
3821 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003822 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003823 }
3824}
3825
Prabir Pradhan1728b212021-10-19 16:00:03 -07003826bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003827 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3828 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3829
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003830 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003831 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003832 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003833 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003834}
3835
3836const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3837 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003838 if (DEBUG_VIRTUAL_KEYS) {
3839 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3840 "left=%d, top=%d, right=%d, bottom=%d",
3841 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3842 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3843 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003844
3845 if (virtualKey.isHit(x, y)) {
3846 return &virtualKey;
3847 }
3848 }
3849
3850 return nullptr;
3851}
3852
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003853void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3854 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3855 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003856
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003857 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003858
3859 if (currentPointerCount == 0) {
3860 // No pointers to assign.
3861 return;
3862 }
3863
3864 if (lastPointerCount == 0) {
3865 // All pointers are new.
3866 for (uint32_t i = 0; i < currentPointerCount; i++) {
3867 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003868 current.rawPointerData.pointers[i].id = id;
3869 current.rawPointerData.idToIndex[id] = i;
3870 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003871 }
3872 return;
3873 }
3874
3875 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003876 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003877 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003878 uint32_t id = last.rawPointerData.pointers[0].id;
3879 current.rawPointerData.pointers[0].id = id;
3880 current.rawPointerData.idToIndex[id] = 0;
3881 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003882 return;
3883 }
3884
3885 // General case.
3886 // We build a heap of squared euclidean distances between current and last pointers
3887 // associated with the current and last pointer indices. Then, we find the best
3888 // match (by distance) for each current pointer.
3889 // The pointers must have the same tool type but it is possible for them to
3890 // transition from hovering to touching or vice-versa while retaining the same id.
3891 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3892
3893 uint32_t heapSize = 0;
3894 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3895 currentPointerIndex++) {
3896 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3897 lastPointerIndex++) {
3898 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003899 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003900 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003901 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003902 if (currentPointer.toolType == lastPointer.toolType) {
3903 int64_t deltaX = currentPointer.x - lastPointer.x;
3904 int64_t deltaY = currentPointer.y - lastPointer.y;
3905
3906 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3907
3908 // Insert new element into the heap (sift up).
3909 heap[heapSize].currentPointerIndex = currentPointerIndex;
3910 heap[heapSize].lastPointerIndex = lastPointerIndex;
3911 heap[heapSize].distance = distance;
3912 heapSize += 1;
3913 }
3914 }
3915 }
3916
3917 // Heapify
3918 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3919 startIndex -= 1;
3920 for (uint32_t parentIndex = startIndex;;) {
3921 uint32_t childIndex = parentIndex * 2 + 1;
3922 if (childIndex >= heapSize) {
3923 break;
3924 }
3925
3926 if (childIndex + 1 < heapSize &&
3927 heap[childIndex + 1].distance < heap[childIndex].distance) {
3928 childIndex += 1;
3929 }
3930
3931 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3932 break;
3933 }
3934
3935 swap(heap[parentIndex], heap[childIndex]);
3936 parentIndex = childIndex;
3937 }
3938 }
3939
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003940 if (DEBUG_POINTER_ASSIGNMENT) {
3941 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3942 for (size_t i = 0; i < heapSize; i++) {
3943 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3944 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3945 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003946 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003947
3948 // Pull matches out by increasing order of distance.
3949 // To avoid reassigning pointers that have already been matched, the loop keeps track
3950 // of which last and current pointers have been matched using the matchedXXXBits variables.
3951 // It also tracks the used pointer id bits.
3952 BitSet32 matchedLastBits(0);
3953 BitSet32 matchedCurrentBits(0);
3954 BitSet32 usedIdBits(0);
3955 bool first = true;
3956 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3957 while (heapSize > 0) {
3958 if (first) {
3959 // The first time through the loop, we just consume the root element of
3960 // the heap (the one with smallest distance).
3961 first = false;
3962 } else {
3963 // Previous iterations consumed the root element of the heap.
3964 // Pop root element off of the heap (sift down).
3965 heap[0] = heap[heapSize];
3966 for (uint32_t parentIndex = 0;;) {
3967 uint32_t childIndex = parentIndex * 2 + 1;
3968 if (childIndex >= heapSize) {
3969 break;
3970 }
3971
3972 if (childIndex + 1 < heapSize &&
3973 heap[childIndex + 1].distance < heap[childIndex].distance) {
3974 childIndex += 1;
3975 }
3976
3977 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3978 break;
3979 }
3980
3981 swap(heap[parentIndex], heap[childIndex]);
3982 parentIndex = childIndex;
3983 }
3984
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003985 if (DEBUG_POINTER_ASSIGNMENT) {
3986 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3987 for (size_t j = 0; j < heapSize; j++) {
3988 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3989 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3990 heap[j].distance);
3991 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003992 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003993 }
3994
3995 heapSize -= 1;
3996
3997 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3998 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3999
4000 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4001 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4002
4003 matchedCurrentBits.markBit(currentPointerIndex);
4004 matchedLastBits.markBit(lastPointerIndex);
4005
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004006 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
4007 current.rawPointerData.pointers[currentPointerIndex].id = id;
4008 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4009 current.rawPointerData.markIdBit(id,
4010 current.rawPointerData.isHovering(
4011 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004012 usedIdBits.markBit(id);
4013
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004014 if (DEBUG_POINTER_ASSIGNMENT) {
4015 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
4016 ", distance=%" PRIu64,
4017 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4018 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004019 break;
4020 }
4021 }
4022
4023 // Assign fresh ids to pointers that were not matched in the process.
4024 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4025 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4026 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4027
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004028 current.rawPointerData.pointers[currentPointerIndex].id = id;
4029 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4030 current.rawPointerData.markIdBit(id,
4031 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004032
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004033 if (DEBUG_POINTER_ASSIGNMENT) {
4034 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
4035 id);
4036 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004037 }
4038}
4039
4040int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4041 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4042 return AKEY_STATE_VIRTUAL;
4043 }
4044
4045 for (const VirtualKey& virtualKey : mVirtualKeys) {
4046 if (virtualKey.keyCode == keyCode) {
4047 return AKEY_STATE_UP;
4048 }
4049 }
4050
4051 return AKEY_STATE_UNKNOWN;
4052}
4053
4054int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4055 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4056 return AKEY_STATE_VIRTUAL;
4057 }
4058
4059 for (const VirtualKey& virtualKey : mVirtualKeys) {
4060 if (virtualKey.scanCode == scanCode) {
4061 return AKEY_STATE_UP;
4062 }
4063 }
4064
4065 return AKEY_STATE_UNKNOWN;
4066}
4067
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004068bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
4069 const std::vector<int32_t>& keyCodes,
4070 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004071 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004072 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004073 if (virtualKey.keyCode == keyCodes[i]) {
4074 outFlags[i] = 1;
4075 }
4076 }
4077 }
4078
4079 return true;
4080}
4081
4082std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4083 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004084 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004085 return std::make_optional(mPointerController->getDisplayId());
4086 } else {
4087 return std::make_optional(mViewport.displayId);
4088 }
4089 }
4090 return std::nullopt;
4091}
4092
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004093} // namespace android