blob: 2ddacef02d754711e74e998f199672453ba3e0d8 [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
45// --- Static Definitions ---
46
Prabir Pradhanf670dad2022-08-05 22:32:11 +000047static const DisplayViewport kUninitializedViewport;
48
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070049template <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
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209 if (mOrientedRanges.haveSize) {
210 info->addMotionRange(mOrientedRanges.size);
211 }
212
213 if (mOrientedRanges.haveTouchSize) {
214 info->addMotionRange(mOrientedRanges.touchMajor);
215 info->addMotionRange(mOrientedRanges.touchMinor);
216 }
217
218 if (mOrientedRanges.haveToolSize) {
219 info->addMotionRange(mOrientedRanges.toolMajor);
220 info->addMotionRange(mOrientedRanges.toolMinor);
221 }
222
223 if (mOrientedRanges.haveOrientation) {
224 info->addMotionRange(mOrientedRanges.orientation);
225 }
226
227 if (mOrientedRanges.haveDistance) {
228 info->addMotionRange(mOrientedRanges.distance);
229 }
230
231 if (mOrientedRanges.haveTilt) {
232 info->addMotionRange(mOrientedRanges.tilt);
233 }
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) {
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000395 // If the device needs to be reset, cancel any ongoing gestures and reset the state.
396 cancelTouch(when, when);
397 reset(when);
398
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
424 String8 gestureModeString;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800425 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.gestureMode"),
426 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") {
432 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
433 }
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
454 String8 deviceTypeString;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800455 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.deviceType"),
456 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") {
466 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
467 }
468 }
469
Michael Wright227c5542020-07-02 18:30:52 +0100470 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800471 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.orientationAware"),
472 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700474 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
475 String8 orientationString;
476 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.orientation"),
477 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") {
487 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.string());
488 }
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();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700499 String8 uniqueDisplayId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800500 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.displayId"),
501 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();
513 getDeviceContext().getConfiguration().tryGetProperty(String8("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
648 mOrientedRanges.haveTouchSize = true;
649 mOrientedRanges.haveToolSize = true;
650 mOrientedRanges.haveSize = true;
651
652 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
653 mOrientedRanges.touchMajor.source = mSource;
654 mOrientedRanges.touchMajor.min = 0;
655 mOrientedRanges.touchMajor.max = diagonalSize;
656 mOrientedRanges.touchMajor.flat = 0;
657 mOrientedRanges.touchMajor.fuzz = 0;
658 mOrientedRanges.touchMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800659 if (mRawPointerAxes.touchMajor.valid) {
660 mRawPointerAxes.touchMajor.resolution =
661 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
662 mOrientedRanges.touchMajor.resolution = mRawPointerAxes.touchMajor.resolution;
663 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800664
665 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
666 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800667 if (mRawPointerAxes.touchMinor.valid) {
668 mRawPointerAxes.touchMinor.resolution =
669 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
670 mOrientedRanges.touchMinor.resolution = mRawPointerAxes.touchMinor.resolution;
671 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800672
673 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
674 mOrientedRanges.toolMajor.source = mSource;
675 mOrientedRanges.toolMajor.min = 0;
676 mOrientedRanges.toolMajor.max = diagonalSize;
677 mOrientedRanges.toolMajor.flat = 0;
678 mOrientedRanges.toolMajor.fuzz = 0;
679 mOrientedRanges.toolMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800680 if (mRawPointerAxes.toolMajor.valid) {
681 mRawPointerAxes.toolMajor.resolution =
682 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
683 mOrientedRanges.toolMajor.resolution = mRawPointerAxes.toolMajor.resolution;
684 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800685
686 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
687 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800688 if (mRawPointerAxes.toolMinor.valid) {
689 mRawPointerAxes.toolMinor.resolution =
690 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
691 mOrientedRanges.toolMinor.resolution = mRawPointerAxes.toolMinor.resolution;
692 }
693
694 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
695 mOrientedRanges.touchMajor.resolution *= mGeometricScale;
696 mOrientedRanges.touchMinor.resolution *= mGeometricScale;
697 mOrientedRanges.toolMajor.resolution *= mGeometricScale;
698 mOrientedRanges.toolMinor.resolution *= mGeometricScale;
699 } else {
700 // Support for other calibrations can be added here.
701 ALOGW("%s calibration is not supported for size ranges at the moment. "
702 "Using raw resolution instead",
703 ftl::enum_string(mCalibration.sizeCalibration).c_str());
704 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800705
706 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
707 mOrientedRanges.size.source = mSource;
708 mOrientedRanges.size.min = 0;
709 mOrientedRanges.size.max = 1.0;
710 mOrientedRanges.size.flat = 0;
711 mOrientedRanges.size.fuzz = 0;
712 mOrientedRanges.size.resolution = 0;
713}
714
715void TouchInputMapper::initializeOrientedRanges() {
716 // Configure X and Y factors.
717 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
718 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
719 mXPrecision = 1.0f / mXScale;
720 mYPrecision = 1.0f / mYScale;
721
722 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
723 mOrientedRanges.x.source = mSource;
724 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
725 mOrientedRanges.y.source = mSource;
726
727 // Scale factor for terms that are not oriented in a particular axis.
728 // If the pixels are square then xScale == yScale otherwise we fake it
729 // by choosing an average.
730 mGeometricScale = avg(mXScale, mYScale);
731
732 initializeSizeRanges();
733
734 // Pressure factors.
735 mPressureScale = 0;
736 float pressureMax = 1.0;
737 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
738 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
739 if (mCalibration.havePressureScale) {
740 mPressureScale = mCalibration.pressureScale;
741 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
742 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
743 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
744 }
745 }
746
747 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
748 mOrientedRanges.pressure.source = mSource;
749 mOrientedRanges.pressure.min = 0;
750 mOrientedRanges.pressure.max = pressureMax;
751 mOrientedRanges.pressure.flat = 0;
752 mOrientedRanges.pressure.fuzz = 0;
753 mOrientedRanges.pressure.resolution = 0;
754
755 // Tilt
756 mTiltXCenter = 0;
757 mTiltXScale = 0;
758 mTiltYCenter = 0;
759 mTiltYScale = 0;
760 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
761 if (mHaveTilt) {
762 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
763 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
764 mTiltXScale = M_PI / 180;
765 mTiltYScale = M_PI / 180;
766
767 if (mRawPointerAxes.tiltX.resolution) {
768 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
769 }
770 if (mRawPointerAxes.tiltY.resolution) {
771 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
772 }
773
774 mOrientedRanges.haveTilt = true;
775
776 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
777 mOrientedRanges.tilt.source = mSource;
778 mOrientedRanges.tilt.min = 0;
779 mOrientedRanges.tilt.max = M_PI_2;
780 mOrientedRanges.tilt.flat = 0;
781 mOrientedRanges.tilt.fuzz = 0;
782 mOrientedRanges.tilt.resolution = 0;
783 }
784
785 // Orientation
786 mOrientationScale = 0;
787 if (mHaveTilt) {
788 mOrientedRanges.haveOrientation = true;
789
790 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
791 mOrientedRanges.orientation.source = mSource;
792 mOrientedRanges.orientation.min = -M_PI;
793 mOrientedRanges.orientation.max = M_PI;
794 mOrientedRanges.orientation.flat = 0;
795 mOrientedRanges.orientation.fuzz = 0;
796 mOrientedRanges.orientation.resolution = 0;
797 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
798 if (mCalibration.orientationCalibration ==
799 Calibration::OrientationCalibration::INTERPOLATED) {
800 if (mRawPointerAxes.orientation.valid) {
801 if (mRawPointerAxes.orientation.maxValue > 0) {
802 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
803 } else if (mRawPointerAxes.orientation.minValue < 0) {
804 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
805 } else {
806 mOrientationScale = 0;
807 }
808 }
809 }
810
811 mOrientedRanges.haveOrientation = true;
812
813 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
814 mOrientedRanges.orientation.source = mSource;
815 mOrientedRanges.orientation.min = -M_PI_2;
816 mOrientedRanges.orientation.max = M_PI_2;
817 mOrientedRanges.orientation.flat = 0;
818 mOrientedRanges.orientation.fuzz = 0;
819 mOrientedRanges.orientation.resolution = 0;
820 }
821
822 // Distance
823 mDistanceScale = 0;
824 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
825 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
826 if (mCalibration.haveDistanceScale) {
827 mDistanceScale = mCalibration.distanceScale;
828 } else {
829 mDistanceScale = 1.0f;
830 }
831 }
832
833 mOrientedRanges.haveDistance = true;
834
835 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
836 mOrientedRanges.distance.source = mSource;
837 mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
838 mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
839 mOrientedRanges.distance.flat = 0;
840 mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
841 mOrientedRanges.distance.resolution = 0;
842 }
843
844 // Compute oriented precision, scales and ranges.
845 // Note that the maximum value reported is an inclusive maximum value so it is one
846 // unit less than the total width or height of the display.
847 switch (mInputDeviceOrientation) {
848 case DISPLAY_ORIENTATION_90:
849 case DISPLAY_ORIENTATION_270:
850 mOrientedXPrecision = mYPrecision;
851 mOrientedYPrecision = mXPrecision;
852
853 mOrientedRanges.x.min = 0;
854 mOrientedRanges.x.max = mDisplayHeight - 1;
855 mOrientedRanges.x.flat = 0;
856 mOrientedRanges.x.fuzz = 0;
857 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
858
859 mOrientedRanges.y.min = 0;
860 mOrientedRanges.y.max = mDisplayWidth - 1;
861 mOrientedRanges.y.flat = 0;
862 mOrientedRanges.y.fuzz = 0;
863 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
864 break;
865
866 default:
867 mOrientedXPrecision = mXPrecision;
868 mOrientedYPrecision = mYPrecision;
869
870 mOrientedRanges.x.min = 0;
871 mOrientedRanges.x.max = mDisplayWidth - 1;
872 mOrientedRanges.x.flat = 0;
873 mOrientedRanges.x.fuzz = 0;
874 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
875
876 mOrientedRanges.y.min = 0;
877 mOrientedRanges.y.max = mDisplayHeight - 1;
878 mOrientedRanges.y.flat = 0;
879 mOrientedRanges.y.fuzz = 0;
880 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
881 break;
882 }
883}
884
Prabir Pradhan1728b212021-10-19 16:00:03 -0700885void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000886 const DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700887
888 resolveExternalStylusPresence();
889
890 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100891 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000892 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700893 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100894 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700895 if (hasStylus()) {
896 mSource |= AINPUT_SOURCE_STYLUS;
897 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800898 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700899 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100900 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700901 if (hasStylus()) {
902 mSource |= AINPUT_SOURCE_STYLUS;
903 }
904 if (hasExternalStylus()) {
905 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
906 }
Michael Wright227c5542020-07-02 18:30:52 +0100907 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700908 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100909 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700910 } else {
911 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100912 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700913 }
914
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000915 const std::optional<DisplayViewport> newViewportOpt = findViewport();
916
917 // Ensure the device is valid and can be used.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700918 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
919 ALOGW("Touch device '%s' did not report support for X or Y axis! "
920 "The device will be inoperable.",
921 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100922 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000923 } else if (!newViewportOpt) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700924 ALOGI("Touch device '%s' could not query the properties of its associated "
925 "display. The device will be inoperable until the display size "
926 "becomes available.",
927 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100928 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000929 } else if (!newViewportOpt->isActive) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000930 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
931 getDeviceName().c_str(), getDeviceId());
932 mDeviceMode = DeviceMode::DISABLED;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000933 }
934
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700935 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700936 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
937 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700938
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000939 const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport);
940 const bool viewportChanged = mViewport != newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700941 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700942 if (viewportChanged) {
Prabir Pradhanf670dad2022-08-05 22:32:11 +0000943 const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation;
944 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId;
945 mViewport = newViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700946
Michael Wright227c5542020-07-02 18:30:52 +0100947 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700948 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700949 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
950 int32_t naturalPhysicalLeft, naturalPhysicalTop;
951 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700952
Prabir Pradhan1728b212021-10-19 16:00:03 -0700953 // Apply the inverse of the input device orientation so that the input device is
954 // configured in the same orientation as the viewport. The input device orientation will
955 // be re-applied by mInputDeviceOrientation.
956 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700957 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700958 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700959 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700960 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
961 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800962 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700963 naturalPhysicalTop = mViewport.physicalLeft;
964 naturalDeviceWidth = mViewport.deviceHeight;
965 naturalDeviceHeight = mViewport.deviceWidth;
966 break;
967 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700968 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
969 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
970 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
971 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
972 naturalDeviceWidth = mViewport.deviceWidth;
973 naturalDeviceHeight = mViewport.deviceHeight;
974 break;
975 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700976 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
977 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
978 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800979 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700980 naturalDeviceWidth = mViewport.deviceHeight;
981 naturalDeviceHeight = mViewport.deviceWidth;
982 break;
983 case DISPLAY_ORIENTATION_0:
984 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700985 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
986 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
987 naturalPhysicalLeft = mViewport.physicalLeft;
988 naturalPhysicalTop = mViewport.physicalTop;
989 naturalDeviceWidth = mViewport.deviceWidth;
990 naturalDeviceHeight = mViewport.deviceHeight;
991 break;
992 }
993
994 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
995 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
996 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
997 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
998 }
999
1000 mPhysicalWidth = naturalPhysicalWidth;
1001 mPhysicalHeight = naturalPhysicalHeight;
1002 mPhysicalLeft = naturalPhysicalLeft;
1003 mPhysicalTop = naturalPhysicalTop;
1004
Prabir Pradhan1728b212021-10-19 16:00:03 -07001005 const int32_t oldDisplayWidth = mDisplayWidth;
1006 const int32_t oldDisplayHeight = mDisplayHeight;
1007 mDisplayWidth = naturalDeviceWidth;
1008 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001009
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001010 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1011 // anything if the device is already orientation-aware. If the device is not
1012 // orientation-aware, then we need to apply the inverse rotation of the display so that
1013 // when the display rotation is applied later as a part of the per-window transform, we
1014 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001015 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001016 ? DISPLAY_ORIENTATION_0
1017 : getInverseRotation(mViewport.orientation);
1018 // For orientation-aware devices that work in the un-rotated coordinate space, the
1019 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001020 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
1021 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001022
1023 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001024 mInputDeviceOrientation =
1025 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001026 } else {
1027 mPhysicalWidth = rawWidth;
1028 mPhysicalHeight = rawHeight;
1029 mPhysicalLeft = 0;
1030 mPhysicalTop = 0;
1031
Prabir Pradhan1728b212021-10-19 16:00:03 -07001032 mDisplayWidth = rawWidth;
1033 mDisplayHeight = rawHeight;
1034 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001035 }
lilinnan687e58f2022-07-19 16:00:50 +08001036 // If displayId changed, do not skip viewport update.
1037 skipViewportUpdate &= !viewportDisplayIdChanged;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001038 }
1039
1040 // If moving between pointer modes, need to reset some state.
1041 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1042 if (deviceModeChanged) {
1043 mOrientedRanges.clear();
1044 }
1045
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001046 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1047 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001048 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001049 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001050 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1051 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001052 if (mPointerController == nullptr) {
1053 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001054 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001055 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001056 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1057 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001058 } else {
lilinnandef700b2022-06-17 19:32:01 +08001059 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1060 !mConfig.showTouches) {
1061 mPointerController->clearSpots();
1062 }
Michael Wright17db18e2020-06-26 20:51:44 +01001063 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001064 }
1065
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001066 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001067 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1068 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001069 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1070 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001071
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001072 configureVirtualKeys();
1073
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001074 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001075
1076 // Location
1077 updateAffineTransformation();
1078
Michael Wright227c5542020-07-02 18:30:52 +01001079 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001080 // Compute pointer gesture detection parameters.
1081 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001082 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001083
1084 // Scale movements such that one whole swipe of the touch pad covers a
1085 // given area relative to the diagonal size of the display when no acceleration
1086 // is applied.
1087 // Assume that the touch pad has a square aspect ratio such that movements in
1088 // X and Y of the same number of raw units cover the same physical distance.
1089 mPointerXMovementScale =
1090 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1091 mPointerYMovementScale = mPointerXMovementScale;
1092
1093 // Scale zooms to cover a smaller range of the display than movements do.
1094 // This value determines the area around the pointer that is affected by freeform
1095 // pointer gestures.
1096 mPointerXZoomScale =
1097 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1098 mPointerYZoomScale = mPointerXZoomScale;
1099
1100 // Max width between pointers to detect a swipe gesture is more than some fraction
1101 // of the diagonal axis of the touch pad. Touches that are wider than this are
1102 // translated into freeform gestures.
1103 mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001104 }
1105
1106 // Inform the dispatcher about the changes.
1107 *outResetNeeded = true;
1108 bumpGeneration();
1109 }
1110}
1111
Prabir Pradhan1728b212021-10-19 16:00:03 -07001112void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001113 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001114 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1115 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001116 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1117 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1118 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1119 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001120 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001121}
1122
1123void TouchInputMapper::configureVirtualKeys() {
1124 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001125 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001126
1127 mVirtualKeys.clear();
1128
1129 if (virtualKeyDefinitions.size() == 0) {
1130 return;
1131 }
1132
1133 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1134 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1135 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1136 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1137
1138 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1139 VirtualKey virtualKey;
1140
1141 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1142 int32_t keyCode;
1143 int32_t dummyKeyMetaState;
1144 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001145 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1146 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001147 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1148 continue; // drop the key
1149 }
1150
1151 virtualKey.keyCode = keyCode;
1152 virtualKey.flags = flags;
1153
1154 // convert the key definition's display coordinates into touch coordinates for a hit box
1155 int32_t halfWidth = virtualKeyDefinition.width / 2;
1156 int32_t halfHeight = virtualKeyDefinition.height / 2;
1157
1158 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001159 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001160 touchScreenLeft;
1161 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001162 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001163 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001164 virtualKey.hitTop =
1165 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001167 virtualKey.hitBottom =
1168 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001169 touchScreenTop;
1170 mVirtualKeys.push_back(virtualKey);
1171 }
1172}
1173
1174void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1175 if (!mVirtualKeys.empty()) {
1176 dump += INDENT3 "Virtual Keys:\n";
1177
1178 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1179 const VirtualKey& virtualKey = mVirtualKeys[i];
1180 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1181 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1182 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1183 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1184 }
1185 }
1186}
1187
1188void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001189 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001190 Calibration& out = mCalibration;
1191
1192 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001193 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001194 String8 sizeCalibrationString;
1195 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
1196 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001197 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001198 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001199 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001200 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001201 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001202 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001203 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001204 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001205 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001206 } else if (sizeCalibrationString != "default") {
1207 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string());
1208 }
1209 }
1210
1211 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale);
1212 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias);
1213 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed);
1214
1215 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001216 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 String8 pressureCalibrationString;
1218 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
1219 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001220 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001221 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001222 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001223 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001224 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001225 } else if (pressureCalibrationString != "default") {
1226 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
1227 pressureCalibrationString.string());
1228 }
1229 }
1230
1231 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale);
1232
1233 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001234 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001235 String8 orientationCalibrationString;
1236 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
1237 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001238 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001239 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001240 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001241 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001242 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001243 } else if (orientationCalibrationString != "default") {
1244 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
1245 orientationCalibrationString.string());
1246 }
1247 }
1248
1249 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001250 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001251 String8 distanceCalibrationString;
1252 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
1253 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001254 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001255 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001256 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001257 } else if (distanceCalibrationString != "default") {
1258 ALOGW("Invalid value for touch.distance.calibration: '%s'",
1259 distanceCalibrationString.string());
1260 }
1261 }
1262
1263 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale);
1264
Michael Wright227c5542020-07-02 18:30:52 +01001265 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001266 String8 coverageCalibrationString;
1267 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
1268 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001269 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001270 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001271 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001272 } else if (coverageCalibrationString != "default") {
1273 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
1274 coverageCalibrationString.string());
1275 }
1276 }
1277}
1278
1279void TouchInputMapper::resolveCalibration() {
1280 // Size
1281 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001282 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1283 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001284 }
1285 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001286 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001287 }
1288
1289 // Pressure
1290 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001291 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1292 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001293 }
1294 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001295 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001296 }
1297
1298 // Orientation
1299 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001300 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1301 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001302 }
1303 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001304 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001305 }
1306
1307 // Distance
1308 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001309 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1310 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001311 }
1312 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001313 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001314 }
1315
1316 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001317 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1318 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001319 }
1320}
1321
1322void TouchInputMapper::dumpCalibration(std::string& dump) {
1323 dump += INDENT3 "Calibration:\n";
1324
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001325 dump += INDENT4 "touch.size.calibration: ";
1326 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001327
1328 if (mCalibration.haveSizeScale) {
1329 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
1330 }
1331
1332 if (mCalibration.haveSizeBias) {
1333 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
1334 }
1335
1336 if (mCalibration.haveSizeIsSummed) {
1337 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
1338 toString(mCalibration.sizeIsSummed));
1339 }
1340
1341 // Pressure
1342 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001343 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001344 dump += INDENT4 "touch.pressure.calibration: none\n";
1345 break;
Michael Wright227c5542020-07-02 18:30:52 +01001346 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001347 dump += INDENT4 "touch.pressure.calibration: physical\n";
1348 break;
Michael Wright227c5542020-07-02 18:30:52 +01001349 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001350 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1351 break;
1352 default:
1353 ALOG_ASSERT(false);
1354 }
1355
1356 if (mCalibration.havePressureScale) {
1357 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
1358 }
1359
1360 // Orientation
1361 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001362 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001363 dump += INDENT4 "touch.orientation.calibration: none\n";
1364 break;
Michael Wright227c5542020-07-02 18:30:52 +01001365 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001366 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1367 break;
Michael Wright227c5542020-07-02 18:30:52 +01001368 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001369 dump += INDENT4 "touch.orientation.calibration: vector\n";
1370 break;
1371 default:
1372 ALOG_ASSERT(false);
1373 }
1374
1375 // Distance
1376 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001377 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001378 dump += INDENT4 "touch.distance.calibration: none\n";
1379 break;
Michael Wright227c5542020-07-02 18:30:52 +01001380 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001381 dump += INDENT4 "touch.distance.calibration: scaled\n";
1382 break;
1383 default:
1384 ALOG_ASSERT(false);
1385 }
1386
1387 if (mCalibration.haveDistanceScale) {
1388 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
1389 }
1390
1391 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001392 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001393 dump += INDENT4 "touch.coverage.calibration: none\n";
1394 break;
Michael Wright227c5542020-07-02 18:30:52 +01001395 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001396 dump += INDENT4 "touch.coverage.calibration: box\n";
1397 break;
1398 default:
1399 ALOG_ASSERT(false);
1400 }
1401}
1402
1403void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1404 dump += INDENT3 "Affine Transformation:\n";
1405
1406 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1407 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1408 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1409 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1410 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1411 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1412}
1413
1414void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001415 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001416 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001417}
1418
1419void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001420 mCursorButtonAccumulator.reset(getDeviceContext());
1421 mCursorScrollAccumulator.reset(getDeviceContext());
1422 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001423
1424 mPointerVelocityControl.reset();
1425 mWheelXVelocityControl.reset();
1426 mWheelYVelocityControl.reset();
1427
1428 mRawStatesPending.clear();
1429 mCurrentRawState.clear();
1430 mCurrentCookedState.clear();
1431 mLastRawState.clear();
1432 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001433 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001434 mSentHoverEnter = false;
1435 mHavePointerIds = false;
1436 mCurrentMotionAborted = false;
1437 mDownTime = 0;
1438
1439 mCurrentVirtualKey.down = false;
1440
1441 mPointerGesture.reset();
1442 mPointerSimple.reset();
1443 resetExternalStylus();
1444
1445 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001446 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001447 mPointerController->clearSpots();
1448 }
1449
1450 InputMapper::reset(when);
1451}
1452
1453void TouchInputMapper::resetExternalStylus() {
1454 mExternalStylusState.clear();
1455 mExternalStylusId = -1;
1456 mExternalStylusFusionTimeout = LLONG_MAX;
1457 mExternalStylusDataPending = false;
1458}
1459
1460void TouchInputMapper::clearStylusDataPendingFlags() {
1461 mExternalStylusDataPending = false;
1462 mExternalStylusFusionTimeout = LLONG_MAX;
1463}
1464
1465void TouchInputMapper::process(const RawEvent* rawEvent) {
1466 mCursorButtonAccumulator.process(rawEvent);
1467 mCursorScrollAccumulator.process(rawEvent);
1468 mTouchButtonAccumulator.process(rawEvent);
1469
1470 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001471 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001472 }
1473}
1474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001475void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001476 // Push a new state.
1477 mRawStatesPending.emplace_back();
1478
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001479 RawState& next = mRawStatesPending.back();
1480 next.clear();
1481 next.when = when;
1482 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001483
1484 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001485 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001486 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1487
1488 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001489 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1490 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001491 mCursorScrollAccumulator.finishSync();
1492
1493 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001494 syncTouch(when, &next);
1495
1496 // The last RawState is the actually second to last, since we just added a new state
1497 const RawState& last =
1498 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001499
1500 // Assign pointer ids.
1501 if (!mHavePointerIds) {
1502 assignPointerIds(last, next);
1503 }
1504
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001505 if (DEBUG_RAW_EVENTS) {
1506 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1507 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1508 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1509 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1510 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1511 next.rawPointerData.canceledIdBits.value);
1512 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001513
Arthur Hung9ad18942021-06-19 02:04:46 +00001514 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1515 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1516 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1517 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1518 next.rawPointerData.hoveringIdBits.value);
1519 }
1520
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001521 processRawTouches(false /*timeout*/);
1522}
1523
1524void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001525 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001526 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001527 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001528 mCurrentCookedState.clear();
1529 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001530 return;
1531 }
1532
1533 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1534 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1535 // touching the current state will only observe the events that have been dispatched to the
1536 // rest of the pipeline.
1537 const size_t N = mRawStatesPending.size();
1538 size_t count;
1539 for (count = 0; count < N; count++) {
1540 const RawState& next = mRawStatesPending[count];
1541
1542 // A failure to assign the stylus id means that we're waiting on stylus data
1543 // and so should defer the rest of the pipeline.
1544 if (assignExternalStylusId(next, timeout)) {
1545 break;
1546 }
1547
1548 // All ready to go.
1549 clearStylusDataPendingFlags();
1550 mCurrentRawState.copyFrom(next);
1551 if (mCurrentRawState.when < mLastRawState.when) {
1552 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001553 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001554 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001555 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001556 }
1557 if (count != 0) {
1558 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1559 }
1560
1561 if (mExternalStylusDataPending) {
1562 if (timeout) {
1563 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1564 clearStylusDataPendingFlags();
1565 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001566 if (DEBUG_STYLUS_FUSION) {
1567 ALOGD("Timeout expired, synthesizing event with new stylus data");
1568 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001569 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1570 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001571 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1572 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1573 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1574 }
1575 }
1576}
1577
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001578void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001579 // Always start with a clean state.
1580 mCurrentCookedState.clear();
1581
1582 // Apply stylus buttons to current raw state.
1583 applyExternalStylusButtonState(when);
1584
1585 // Handle policy on initial down or hover events.
1586 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1587 mCurrentRawState.rawPointerData.pointerCount != 0;
1588
1589 uint32_t policyFlags = 0;
1590 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1591 if (initialDown || buttonsPressed) {
1592 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001593 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001594 getContext()->fadePointer();
1595 }
1596
1597 if (mParameters.wake) {
1598 policyFlags |= POLICY_FLAG_WAKE;
1599 }
1600 }
1601
1602 // Consume raw off-screen touches before cooking pointer data.
1603 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001604 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001605 mCurrentRawState.rawPointerData.clear();
1606 }
1607
1608 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1609 // with cooked pointer data that has the same ids and indices as the raw data.
1610 // The following code can use either the raw or cooked data, as needed.
1611 cookPointerData();
1612
1613 // Apply stylus pressure to current cooked state.
1614 applyExternalStylusTouchState(when);
1615
1616 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001617 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1618 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001619 mCurrentCookedState.buttonState);
1620
1621 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001622 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001623 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1624 uint32_t id = idBits.clearFirstMarkedBit();
1625 const RawPointerData::Pointer& pointer =
1626 mCurrentRawState.rawPointerData.pointerForId(id);
1627 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1628 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1629 mCurrentCookedState.stylusIdBits.markBit(id);
1630 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1631 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1632 mCurrentCookedState.fingerIdBits.markBit(id);
1633 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1634 mCurrentCookedState.mouseIdBits.markBit(id);
1635 }
1636 }
1637 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1638 uint32_t id = idBits.clearFirstMarkedBit();
1639 const RawPointerData::Pointer& pointer =
1640 mCurrentRawState.rawPointerData.pointerForId(id);
1641 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1642 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1643 mCurrentCookedState.stylusIdBits.markBit(id);
1644 }
1645 }
1646
1647 // Stylus takes precedence over all tools, then mouse, then finger.
1648 PointerUsage pointerUsage = mPointerUsage;
1649 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1650 mCurrentCookedState.mouseIdBits.clear();
1651 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001652 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001653 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1654 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001655 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1657 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001658 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001659 }
1660
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001661 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001662 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001663 if (!mCurrentMotionAborted) {
Prabir Pradhand4206712022-04-27 13:19:15 +00001664 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001665 dispatchButtonRelease(when, readTime, policyFlags);
1666 dispatchHoverExit(when, readTime, policyFlags);
1667 dispatchTouches(when, readTime, policyFlags);
1668 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1669 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001670 }
1671
1672 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1673 mCurrentMotionAborted = false;
1674 }
1675 }
1676
1677 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001678 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001679 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1680 mCurrentCookedState.buttonState);
1681
1682 // Clear some transient state.
1683 mCurrentRawState.rawVScroll = 0;
1684 mCurrentRawState.rawHScroll = 0;
1685
1686 // Copy current touch to last touch in preparation for the next cycle.
1687 mLastRawState.copyFrom(mCurrentRawState);
1688 mLastCookedState.copyFrom(mCurrentCookedState);
1689}
1690
Garfield Tanc734e4f2021-01-15 20:01:39 -08001691void TouchInputMapper::updateTouchSpots() {
1692 if (!mConfig.showTouches || mPointerController == nullptr) {
1693 return;
1694 }
1695
1696 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1697 // clear touch spots.
1698 if (mDeviceMode != DeviceMode::DIRECT &&
1699 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1700 return;
1701 }
1702
1703 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1704 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1705
1706 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001707 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1708 mCurrentCookedState.cookedPointerData.idToIndex,
1709 mCurrentCookedState.cookedPointerData.touchingIdBits,
1710 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001711}
1712
1713bool TouchInputMapper::isTouchScreen() {
1714 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1715 mParameters.hasAssociatedDisplay;
1716}
1717
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001718void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001719 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001720 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1721 }
1722}
1723
1724void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1725 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1726 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1727
1728 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1729 float pressure = mExternalStylusState.pressure;
1730 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1731 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1732 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1733 }
1734 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1735 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1736
1737 PointerProperties& properties =
1738 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1739 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1740 properties.toolType = mExternalStylusState.toolType;
1741 }
1742 }
1743}
1744
1745bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001746 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001747 return false;
1748 }
1749
1750 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1751 state.rawPointerData.pointerCount != 0;
1752 if (initialDown) {
1753 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001754 if (DEBUG_STYLUS_FUSION) {
1755 ALOGD("Have both stylus and touch data, beginning fusion");
1756 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001757 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1758 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001759 if (DEBUG_STYLUS_FUSION) {
1760 ALOGD("Timeout expired, assuming touch is not a stylus.");
1761 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001762 resetExternalStylus();
1763 } else {
1764 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1765 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1766 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001767 if (DEBUG_STYLUS_FUSION) {
1768 ALOGD("No stylus data but stylus is connected, requesting timeout "
1769 "(%" PRId64 "ms)",
1770 mExternalStylusFusionTimeout);
1771 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001772 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1773 return true;
1774 }
1775 }
1776
1777 // Check if the stylus pointer has gone up.
1778 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001779 if (DEBUG_STYLUS_FUSION) {
1780 ALOGD("Stylus pointer is going up");
1781 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001782 mExternalStylusId = -1;
1783 }
1784
1785 return false;
1786}
1787
1788void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001789 if (mDeviceMode == DeviceMode::POINTER) {
1790 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001791 // Since this is a synthetic event, we can consider its latency to be zero
1792 const nsecs_t readTime = when;
1793 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001794 }
Michael Wright227c5542020-07-02 18:30:52 +01001795 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001796 if (mExternalStylusFusionTimeout < when) {
1797 processRawTouches(true /*timeout*/);
1798 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1799 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1800 }
1801 }
1802}
1803
1804void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1805 mExternalStylusState.copyFrom(state);
1806 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1807 // We're either in the middle of a fused stream of data or we're waiting on data before
1808 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1809 // data.
1810 mExternalStylusDataPending = true;
1811 processRawTouches(false /*timeout*/);
1812 }
1813}
1814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001815bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001816 // Check for release of a virtual key.
1817 if (mCurrentVirtualKey.down) {
1818 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1819 // Pointer went up while virtual key was down.
1820 mCurrentVirtualKey.down = false;
1821 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001822 if (DEBUG_VIRTUAL_KEYS) {
1823 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1824 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1825 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001826 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001827 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1828 }
1829 return true;
1830 }
1831
1832 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1833 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1834 const RawPointerData::Pointer& pointer =
1835 mCurrentRawState.rawPointerData.pointerForId(id);
1836 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1837 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1838 // Pointer is still within the space of the virtual key.
1839 return true;
1840 }
1841 }
1842
1843 // Pointer left virtual key area or another pointer also went down.
1844 // Send key cancellation but do not consume the touch yet.
1845 // This is useful when the user swipes through from the virtual key area
1846 // into the main display surface.
1847 mCurrentVirtualKey.down = false;
1848 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001849 if (DEBUG_VIRTUAL_KEYS) {
1850 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1851 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1852 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001853 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001854 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1855 AKEY_EVENT_FLAG_CANCELED);
1856 }
1857 }
1858
1859 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1860 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1861 // Pointer just went down. Check for virtual key press or off-screen touches.
1862 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1863 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001864 // Skip checking whether the pointer is inside the physical frame if the device is in
1865 // unscaled mode.
1866 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1867 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001868 // If exactly one pointer went down, check for virtual key hit.
1869 // Otherwise we will drop the entire stroke.
1870 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1871 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1872 if (virtualKey) {
1873 mCurrentVirtualKey.down = true;
1874 mCurrentVirtualKey.downTime = when;
1875 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1876 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1877 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001878 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1879 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001880
1881 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001882 if (DEBUG_VIRTUAL_KEYS) {
1883 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1884 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1885 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001886 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001887 AKEY_EVENT_FLAG_FROM_SYSTEM |
1888 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1889 }
1890 }
1891 }
1892 return true;
1893 }
1894 }
1895
1896 // Disable all virtual key touches that happen within a short time interval of the
1897 // most recent touch within the screen area. The idea is to filter out stray
1898 // virtual key presses when interacting with the touch screen.
1899 //
1900 // Problems we're trying to solve:
1901 //
1902 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1903 // virtual key area that is implemented by a separate touch panel and accidentally
1904 // triggers a virtual key.
1905 //
1906 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1907 // area and accidentally triggers a virtual key. This often happens when virtual keys
1908 // are layed out below the screen near to where the on screen keyboard's space bar
1909 // is displayed.
1910 if (mConfig.virtualKeyQuietTime > 0 &&
1911 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001912 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001913 }
1914 return false;
1915}
1916
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001917void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001918 int32_t keyEventAction, int32_t keyEventFlags) {
1919 int32_t keyCode = mCurrentVirtualKey.keyCode;
1920 int32_t scanCode = mCurrentVirtualKey.scanCode;
1921 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001922 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001923 policyFlags |= POLICY_FLAG_VIRTUAL;
1924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001925 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1926 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1927 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001928 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001929}
1930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
lilinnan687e58f2022-07-19 16:00:50 +08001932 if (mCurrentMotionAborted) {
1933 // Current motion event was already aborted.
1934 return;
1935 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001936 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1937 if (!currentIdBits.isEmpty()) {
1938 int32_t metaState = getContext()->getGlobalMetaState();
1939 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001940 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1941 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001942 mCurrentCookedState.cookedPointerData.pointerProperties,
1943 mCurrentCookedState.cookedPointerData.pointerCoords,
1944 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1945 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1946 mCurrentMotionAborted = true;
1947 }
1948}
1949
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001950void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001951 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1952 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1953 int32_t metaState = getContext()->getGlobalMetaState();
1954 int32_t buttonState = mCurrentCookedState.buttonState;
1955
1956 if (currentIdBits == lastIdBits) {
1957 if (!currentIdBits.isEmpty()) {
1958 // No pointer id changes so this is a move event.
1959 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001960 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1961 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001962 mCurrentCookedState.cookedPointerData.pointerProperties,
1963 mCurrentCookedState.cookedPointerData.pointerCoords,
1964 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1965 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1966 }
1967 } else {
1968 // There may be pointers going up and pointers going down and pointers moving
1969 // all at the same time.
1970 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1971 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1972 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1973 BitSet32 dispatchedIdBits(lastIdBits.value);
1974
1975 // Update last coordinates of pointers that have moved so that we observe the new
1976 // pointer positions at the same time as other pointers that have just gone up.
1977 bool moveNeeded =
1978 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1979 mCurrentCookedState.cookedPointerData.pointerCoords,
1980 mCurrentCookedState.cookedPointerData.idToIndex,
1981 mLastCookedState.cookedPointerData.pointerProperties,
1982 mLastCookedState.cookedPointerData.pointerCoords,
1983 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1984 if (buttonState != mLastCookedState.buttonState) {
1985 moveNeeded = true;
1986 }
1987
1988 // Dispatch pointer up events.
1989 while (!upIdBits.isEmpty()) {
1990 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001991 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001992 if (isCanceled) {
1993 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1994 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001995 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001996 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001997 mLastCookedState.cookedPointerData.pointerProperties,
1998 mLastCookedState.cookedPointerData.pointerCoords,
1999 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
2000 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2001 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002002 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002003 }
2004
2005 // Dispatch move events if any of the remaining pointers moved from their old locations.
2006 // Although applications receive new locations as part of individual pointer up
2007 // events, they do not generally handle them except when presented in a move event.
2008 if (moveNeeded && !moveIdBits.isEmpty()) {
2009 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002010 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2011 metaState, buttonState, 0,
2012 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002013 mCurrentCookedState.cookedPointerData.pointerCoords,
2014 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2015 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2016 }
2017
2018 // Dispatch pointer down events using the new pointer locations.
2019 while (!downIdBits.isEmpty()) {
2020 uint32_t downId = downIdBits.clearFirstMarkedBit();
2021 dispatchedIdBits.markBit(downId);
2022
2023 if (dispatchedIdBits.count() == 1) {
2024 // First pointer is going down. Set down time.
2025 mDownTime = when;
2026 }
2027
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002028 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2029 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002030 mCurrentCookedState.cookedPointerData.pointerProperties,
2031 mCurrentCookedState.cookedPointerData.pointerCoords,
2032 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2033 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2034 }
2035 }
2036}
2037
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002038void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002039 if (mSentHoverEnter &&
2040 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2041 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2042 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002043 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2044 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002045 mLastCookedState.cookedPointerData.pointerProperties,
2046 mLastCookedState.cookedPointerData.pointerCoords,
2047 mLastCookedState.cookedPointerData.idToIndex,
2048 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2049 mOrientedYPrecision, mDownTime);
2050 mSentHoverEnter = false;
2051 }
2052}
2053
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002054void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2055 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002056 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2057 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2058 int32_t metaState = getContext()->getGlobalMetaState();
2059 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002060 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2061 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002062 mCurrentCookedState.cookedPointerData.pointerProperties,
2063 mCurrentCookedState.cookedPointerData.pointerCoords,
2064 mCurrentCookedState.cookedPointerData.idToIndex,
2065 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2066 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2067 mSentHoverEnter = true;
2068 }
2069
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002070 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2071 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002072 mCurrentCookedState.cookedPointerData.pointerProperties,
2073 mCurrentCookedState.cookedPointerData.pointerCoords,
2074 mCurrentCookedState.cookedPointerData.idToIndex,
2075 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2076 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2077 }
2078}
2079
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002080void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002081 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2082 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2083 const int32_t metaState = getContext()->getGlobalMetaState();
2084 int32_t buttonState = mLastCookedState.buttonState;
2085 while (!releasedButtons.isEmpty()) {
2086 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2087 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002088 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002089 actionButton, 0, metaState, buttonState, 0,
2090 mCurrentCookedState.cookedPointerData.pointerProperties,
2091 mCurrentCookedState.cookedPointerData.pointerCoords,
2092 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2093 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2094 }
2095}
2096
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002097void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002098 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2099 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2100 const int32_t metaState = getContext()->getGlobalMetaState();
2101 int32_t buttonState = mLastCookedState.buttonState;
2102 while (!pressedButtons.isEmpty()) {
2103 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2104 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002105 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2106 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002107 mCurrentCookedState.cookedPointerData.pointerProperties,
2108 mCurrentCookedState.cookedPointerData.pointerCoords,
2109 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2110 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2111 }
2112}
2113
2114const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2115 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2116 return cookedPointerData.touchingIdBits;
2117 }
2118 return cookedPointerData.hoveringIdBits;
2119}
2120
2121void TouchInputMapper::cookPointerData() {
2122 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2123
2124 mCurrentCookedState.cookedPointerData.clear();
2125 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2126 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2127 mCurrentRawState.rawPointerData.hoveringIdBits;
2128 mCurrentCookedState.cookedPointerData.touchingIdBits =
2129 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002130 mCurrentCookedState.cookedPointerData.canceledIdBits =
2131 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002132
2133 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2134 mCurrentCookedState.buttonState = 0;
2135 } else {
2136 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2137 }
2138
2139 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002140 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002141 for (uint32_t i = 0; i < currentPointerCount; i++) {
2142 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2143
2144 // Size
2145 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2146 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002147 case Calibration::SizeCalibration::GEOMETRIC:
2148 case Calibration::SizeCalibration::DIAMETER:
2149 case Calibration::SizeCalibration::BOX:
2150 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002151 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2152 touchMajor = in.touchMajor;
2153 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2154 toolMajor = in.toolMajor;
2155 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2156 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2157 : in.touchMajor;
2158 } else if (mRawPointerAxes.touchMajor.valid) {
2159 toolMajor = touchMajor = in.touchMajor;
2160 toolMinor = touchMinor =
2161 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2162 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2163 : in.touchMajor;
2164 } else if (mRawPointerAxes.toolMajor.valid) {
2165 touchMajor = toolMajor = in.toolMajor;
2166 touchMinor = toolMinor =
2167 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2168 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2169 : in.toolMajor;
2170 } else {
2171 ALOG_ASSERT(false,
2172 "No touch or tool axes. "
2173 "Size calibration should have been resolved to NONE.");
2174 touchMajor = 0;
2175 touchMinor = 0;
2176 toolMajor = 0;
2177 toolMinor = 0;
2178 size = 0;
2179 }
2180
2181 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2182 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2183 if (touchingCount > 1) {
2184 touchMajor /= touchingCount;
2185 touchMinor /= touchingCount;
2186 toolMajor /= touchingCount;
2187 toolMinor /= touchingCount;
2188 size /= touchingCount;
2189 }
2190 }
2191
Michael Wright227c5542020-07-02 18:30:52 +01002192 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002193 touchMajor *= mGeometricScale;
2194 touchMinor *= mGeometricScale;
2195 toolMajor *= mGeometricScale;
2196 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002197 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002198 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2199 touchMinor = touchMajor;
2200 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2201 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002202 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002203 touchMinor = touchMajor;
2204 toolMinor = toolMajor;
2205 }
2206
2207 mCalibration.applySizeScaleAndBias(&touchMajor);
2208 mCalibration.applySizeScaleAndBias(&touchMinor);
2209 mCalibration.applySizeScaleAndBias(&toolMajor);
2210 mCalibration.applySizeScaleAndBias(&toolMinor);
2211 size *= mSizeScale;
2212 break;
2213 default:
2214 touchMajor = 0;
2215 touchMinor = 0;
2216 toolMajor = 0;
2217 toolMinor = 0;
2218 size = 0;
2219 break;
2220 }
2221
2222 // Pressure
2223 float pressure;
2224 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002225 case Calibration::PressureCalibration::PHYSICAL:
2226 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002227 pressure = in.pressure * mPressureScale;
2228 break;
2229 default:
2230 pressure = in.isHovering ? 0 : 1;
2231 break;
2232 }
2233
2234 // Tilt and Orientation
2235 float tilt;
2236 float orientation;
2237 if (mHaveTilt) {
2238 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2239 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2240 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2241 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2242 } else {
2243 tilt = 0;
2244
2245 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002246 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002247 orientation = in.orientation * mOrientationScale;
2248 break;
Michael Wright227c5542020-07-02 18:30:52 +01002249 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002250 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2251 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2252 if (c1 != 0 || c2 != 0) {
2253 orientation = atan2f(c1, c2) * 0.5f;
2254 float confidence = hypotf(c1, c2);
2255 float scale = 1.0f + confidence / 16.0f;
2256 touchMajor *= scale;
2257 touchMinor /= scale;
2258 toolMajor *= scale;
2259 toolMinor /= scale;
2260 } else {
2261 orientation = 0;
2262 }
2263 break;
2264 }
2265 default:
2266 orientation = 0;
2267 }
2268 }
2269
2270 // Distance
2271 float distance;
2272 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002273 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002274 distance = in.distance * mDistanceScale;
2275 break;
2276 default:
2277 distance = 0;
2278 }
2279
2280 // Coverage
2281 int32_t rawLeft, rawTop, rawRight, rawBottom;
2282 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002283 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002284 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2285 rawRight = in.toolMinor & 0x0000ffff;
2286 rawBottom = in.toolMajor & 0x0000ffff;
2287 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2288 break;
2289 default:
2290 rawLeft = rawTop = rawRight = rawBottom = 0;
2291 break;
2292 }
2293
2294 // Adjust X,Y coords for device calibration
2295 // TODO: Adjust coverage coords?
2296 float xTransformed = in.x, yTransformed = in.y;
2297 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002298 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002299
Prabir Pradhan1728b212021-10-19 16:00:03 -07002300 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002301 float left, top, right, bottom;
2302
Prabir Pradhan1728b212021-10-19 16:00:03 -07002303 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002304 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002305 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2306 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2307 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2308 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002309 orientation -= M_PI_2;
2310 if (mOrientedRanges.haveOrientation &&
2311 orientation < mOrientedRanges.orientation.min) {
2312 orientation +=
2313 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2314 }
2315 break;
2316 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002317 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2318 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002319 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2320 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002321 orientation -= M_PI;
2322 if (mOrientedRanges.haveOrientation &&
2323 orientation < mOrientedRanges.orientation.min) {
2324 orientation +=
2325 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2326 }
2327 break;
2328 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002329 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2330 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002331 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2332 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002333 orientation += M_PI_2;
2334 if (mOrientedRanges.haveOrientation &&
2335 orientation > mOrientedRanges.orientation.max) {
2336 orientation -=
2337 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2338 }
2339 break;
2340 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002341 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2342 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2343 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2344 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002345 break;
2346 }
2347
2348 // Write output coords.
2349 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2350 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002351 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2352 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002353 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2354 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2355 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2356 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2357 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2358 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2359 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002360 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002361 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2362 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2363 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2364 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2365 } else {
2366 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2367 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2368 }
2369
Chris Ye364fdb52020-08-05 15:07:56 -07002370 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002371 uint32_t id = in.id;
2372 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2373 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2374 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2375 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2376 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2377 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2378 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2379 }
2380
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002381 // Write output properties.
2382 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002383 properties.clear();
2384 properties.id = id;
2385 properties.toolType = in.toolType;
2386
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002387 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002388 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002389 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002390 }
2391}
2392
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002393void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002394 PointerUsage pointerUsage) {
2395 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002396 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002397 mPointerUsage = pointerUsage;
2398 }
2399
2400 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002401 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002402 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002403 break;
Michael Wright227c5542020-07-02 18:30:52 +01002404 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002405 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002406 break;
Michael Wright227c5542020-07-02 18:30:52 +01002407 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002408 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002409 break;
Michael Wright227c5542020-07-02 18:30:52 +01002410 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002411 break;
2412 }
2413}
2414
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002415void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002416 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002417 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002418 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002419 break;
Michael Wright227c5542020-07-02 18:30:52 +01002420 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002421 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002422 break;
Michael Wright227c5542020-07-02 18:30:52 +01002423 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002424 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002425 break;
Michael Wright227c5542020-07-02 18:30:52 +01002426 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002427 break;
2428 }
2429
Michael Wright227c5542020-07-02 18:30:52 +01002430 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002431}
2432
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002433void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2434 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002435 // Update current gesture coordinates.
2436 bool cancelPreviousGesture, finishPreviousGesture;
2437 bool sendEvents =
2438 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2439 if (!sendEvents) {
2440 return;
2441 }
2442 if (finishPreviousGesture) {
2443 cancelPreviousGesture = false;
2444 }
2445
2446 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002447 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002448 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002449 if (finishPreviousGesture || cancelPreviousGesture) {
2450 mPointerController->clearSpots();
2451 }
2452
Michael Wright227c5542020-07-02 18:30:52 +01002453 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002454 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2455 mPointerGesture.currentGestureIdToIndex,
2456 mPointerGesture.currentGestureIdBits,
2457 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002458 }
2459 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002460 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002461 }
2462
2463 // Show or hide the pointer if needed.
2464 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002465 case PointerGesture::Mode::NEUTRAL:
2466 case PointerGesture::Mode::QUIET:
2467 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2468 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002469 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002470 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002471 }
2472 break;
Michael Wright227c5542020-07-02 18:30:52 +01002473 case PointerGesture::Mode::TAP:
2474 case PointerGesture::Mode::TAP_DRAG:
2475 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2476 case PointerGesture::Mode::HOVER:
2477 case PointerGesture::Mode::PRESS:
2478 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002479 // Unfade the pointer when the current gesture manipulates the
2480 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002481 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002482 break;
Michael Wright227c5542020-07-02 18:30:52 +01002483 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002484 // Fade the pointer when the current gesture manipulates a different
2485 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002486 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002487 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002488 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002489 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002490 }
2491 break;
2492 }
2493
2494 // Send events!
2495 int32_t metaState = getContext()->getGlobalMetaState();
2496 int32_t buttonState = mCurrentCookedState.buttonState;
2497
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002498 uint32_t flags = 0;
2499
2500 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2501 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2502 }
2503
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002504 // Update last coordinates of pointers that have moved so that we observe the new
2505 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002506 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2507 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2508 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2509 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2510 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2511 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002512 bool moveNeeded = false;
2513 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2514 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2515 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2516 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2517 mPointerGesture.lastGestureIdBits.value);
2518 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2519 mPointerGesture.currentGestureCoords,
2520 mPointerGesture.currentGestureIdToIndex,
2521 mPointerGesture.lastGestureProperties,
2522 mPointerGesture.lastGestureCoords,
2523 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2524 if (buttonState != mLastCookedState.buttonState) {
2525 moveNeeded = true;
2526 }
2527 }
2528
2529 // Send motion events for all pointers that went up or were canceled.
2530 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2531 if (!dispatchedGestureIdBits.isEmpty()) {
2532 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002533 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2534 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002535 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2536 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2537 mPointerGesture.downTime);
2538
2539 dispatchedGestureIdBits.clear();
2540 } else {
2541 BitSet32 upGestureIdBits;
2542 if (finishPreviousGesture) {
2543 upGestureIdBits = dispatchedGestureIdBits;
2544 } else {
2545 upGestureIdBits.value =
2546 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2547 }
2548 while (!upGestureIdBits.isEmpty()) {
2549 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2550
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002551 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002552 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002553 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002554 mPointerGesture.lastGestureCoords,
2555 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2556 0, mPointerGesture.downTime);
2557
2558 dispatchedGestureIdBits.clearBit(id);
2559 }
2560 }
2561 }
2562
2563 // Send motion events for all pointers that moved.
2564 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002565 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002566 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002567 mPointerGesture.currentGestureProperties,
2568 mPointerGesture.currentGestureCoords,
2569 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2570 mPointerGesture.downTime);
2571 }
2572
2573 // Send motion events for all pointers that went down.
2574 if (down) {
2575 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2576 ~dispatchedGestureIdBits.value);
2577 while (!downGestureIdBits.isEmpty()) {
2578 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2579 dispatchedGestureIdBits.markBit(id);
2580
2581 if (dispatchedGestureIdBits.count() == 1) {
2582 mPointerGesture.downTime = when;
2583 }
2584
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002585 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002586 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002587 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002588 mPointerGesture.currentGestureCoords,
2589 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2590 0, mPointerGesture.downTime);
2591 }
2592 }
2593
2594 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002595 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002596 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2597 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002598 mPointerGesture.currentGestureProperties,
2599 mPointerGesture.currentGestureCoords,
2600 mPointerGesture.currentGestureIdToIndex,
2601 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2602 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2603 // Synthesize a hover move event after all pointers go up to indicate that
2604 // the pointer is hovering again even if the user is not currently touching
2605 // the touch pad. This ensures that a view will receive a fresh hover enter
2606 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002607 float x, y;
2608 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002609
2610 PointerProperties pointerProperties;
2611 pointerProperties.clear();
2612 pointerProperties.id = 0;
2613 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2614
2615 PointerCoords pointerCoords;
2616 pointerCoords.clear();
2617 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2618 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2619
2620 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002621 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002622 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002623 metaState, buttonState, MotionClassification::NONE,
2624 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2625 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002626 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002627 }
2628
2629 // Update state.
2630 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2631 if (!down) {
2632 mPointerGesture.lastGestureIdBits.clear();
2633 } else {
2634 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2635 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2636 uint32_t id = idBits.clearFirstMarkedBit();
2637 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2638 mPointerGesture.lastGestureProperties[index].copyFrom(
2639 mPointerGesture.currentGestureProperties[index]);
2640 mPointerGesture.lastGestureCoords[index].copyFrom(
2641 mPointerGesture.currentGestureCoords[index]);
2642 mPointerGesture.lastGestureIdToIndex[id] = index;
2643 }
2644 }
2645}
2646
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002647void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002648 // Cancel previously dispatches pointers.
2649 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2650 int32_t metaState = getContext()->getGlobalMetaState();
2651 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002652 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2653 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002654 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2655 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2656 0, 0, mPointerGesture.downTime);
2657 }
2658
2659 // Reset the current pointer gesture.
2660 mPointerGesture.reset();
2661 mPointerVelocityControl.reset();
2662
2663 // Remove any current spots.
2664 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002665 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002666 mPointerController->clearSpots();
2667 }
2668}
2669
2670bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2671 bool* outFinishPreviousGesture, bool isTimeout) {
2672 *outCancelPreviousGesture = false;
2673 *outFinishPreviousGesture = false;
2674
2675 // Handle TAP timeout.
2676 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002677 if (DEBUG_GESTURES) {
2678 ALOGD("Gestures: Processing timeout");
2679 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002680
Michael Wright227c5542020-07-02 18:30:52 +01002681 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002682 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2683 // The tap/drag timeout has not yet expired.
2684 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2685 mConfig.pointerGestureTapDragInterval);
2686 } else {
2687 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002688 if (DEBUG_GESTURES) {
2689 ALOGD("Gestures: TAP finished");
2690 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002691 *outFinishPreviousGesture = true;
2692
2693 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002694 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002695 mPointerGesture.currentGestureIdBits.clear();
2696
2697 mPointerVelocityControl.reset();
2698 return true;
2699 }
2700 }
2701
2702 // We did not handle this timeout.
2703 return false;
2704 }
2705
2706 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2707 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2708
2709 // Update the velocity tracker.
2710 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002711 std::vector<VelocityTracker::Position> positions;
2712 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002713 uint32_t id = idBits.clearFirstMarkedBit();
2714 const RawPointerData::Pointer& pointer =
2715 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002716 float x = pointer.x * mPointerXMovementScale;
2717 float y = pointer.y * mPointerYMovementScale;
2718 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002719 }
2720 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2721 positions);
2722 }
2723
2724 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2725 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002726 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2727 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2728 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002729 mPointerGesture.resetTap();
2730 }
2731
2732 // Pick a new active touch id if needed.
2733 // Choose an arbitrary pointer that just went down, if there is one.
2734 // Otherwise choose an arbitrary remaining pointer.
2735 // This guarantees we always have an active touch id when there is at least one pointer.
2736 // We keep the same active touch id for as long as possible.
2737 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2738 int32_t activeTouchId = lastActiveTouchId;
2739 if (activeTouchId < 0) {
2740 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2741 activeTouchId = mPointerGesture.activeTouchId =
2742 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2743 mPointerGesture.firstTouchTime = when;
2744 }
2745 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2746 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2747 activeTouchId = mPointerGesture.activeTouchId =
2748 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2749 } else {
2750 activeTouchId = mPointerGesture.activeTouchId = -1;
2751 }
2752 }
2753
2754 // Determine whether we are in quiet time.
2755 bool isQuietTime = false;
2756 if (activeTouchId < 0) {
2757 mPointerGesture.resetQuietTime();
2758 } else {
2759 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2760 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002761 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2762 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2763 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002764 currentFingerCount < 2) {
2765 // Enter quiet time when exiting swipe or freeform state.
2766 // This is to prevent accidentally entering the hover state and flinging the
2767 // pointer when finishing a swipe and there is still one pointer left onscreen.
2768 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002769 } else if (mPointerGesture.lastGestureMode ==
2770 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002771 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2772 // Enter quiet time when releasing the button and there are still two or more
2773 // fingers down. This may indicate that one finger was used to press the button
2774 // but it has not gone up yet.
2775 isQuietTime = true;
2776 }
2777 if (isQuietTime) {
2778 mPointerGesture.quietTime = when;
2779 }
2780 }
2781 }
2782
2783 // Switch states based on button and pointer state.
2784 if (isQuietTime) {
2785 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002786 if (DEBUG_GESTURES) {
2787 ALOGD("Gestures: QUIET for next %0.3fms",
2788 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2789 0.000001f);
2790 }
Michael Wright227c5542020-07-02 18:30:52 +01002791 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002792 *outFinishPreviousGesture = true;
2793 }
2794
2795 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002796 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002797 mPointerGesture.currentGestureIdBits.clear();
2798
2799 mPointerVelocityControl.reset();
2800 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2801 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2802 // The pointer follows the active touch point.
2803 // Emit DOWN, MOVE, UP events at the pointer location.
2804 //
2805 // Only the active touch matters; other fingers are ignored. This policy helps
2806 // to handle the case where the user places a second finger on the touch pad
2807 // to apply the necessary force to depress an integrated button below the surface.
2808 // We don't want the second finger to be delivered to applications.
2809 //
2810 // For this to work well, we need to make sure to track the pointer that is really
2811 // active. If the user first puts one finger down to click then adds another
2812 // finger to drag then the active pointer should switch to the finger that is
2813 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002814 if (DEBUG_GESTURES) {
2815 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2816 "currentFingerCount=%d",
2817 activeTouchId, currentFingerCount);
2818 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002819 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002820 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002821 *outFinishPreviousGesture = true;
2822 mPointerGesture.activeGestureId = 0;
2823 }
2824
2825 // Switch pointers if needed.
2826 // Find the fastest pointer and follow it.
2827 if (activeTouchId >= 0 && currentFingerCount > 1) {
2828 int32_t bestId = -1;
2829 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2830 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2831 uint32_t id = idBits.clearFirstMarkedBit();
2832 float vx, vy;
2833 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2834 float speed = hypotf(vx, vy);
2835 if (speed > bestSpeed) {
2836 bestId = id;
2837 bestSpeed = speed;
2838 }
2839 }
2840 }
2841 if (bestId >= 0 && bestId != activeTouchId) {
2842 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002843 if (DEBUG_GESTURES) {
2844 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2845 "bestId=%d, bestSpeed=%0.3f",
2846 bestId, bestSpeed);
2847 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002848 }
2849 }
2850
2851 float deltaX = 0, deltaY = 0;
2852 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2853 const RawPointerData::Pointer& currentPointer =
2854 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2855 const RawPointerData::Pointer& lastPointer =
2856 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2857 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2858 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2859
Prabir Pradhan1728b212021-10-19 16:00:03 -07002860 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002861 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2862
2863 // Move the pointer using a relative motion.
2864 // When using spots, the click will occur at the position of the anchor
2865 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002866 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002867 } else {
2868 mPointerVelocityControl.reset();
2869 }
2870
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002871 float x, y;
2872 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002873
Michael Wright227c5542020-07-02 18:30:52 +01002874 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002875 mPointerGesture.currentGestureIdBits.clear();
2876 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2877 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2878 mPointerGesture.currentGestureProperties[0].clear();
2879 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2880 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2881 mPointerGesture.currentGestureCoords[0].clear();
2882 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2883 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2884 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2885 } else if (currentFingerCount == 0) {
2886 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002887 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002888 *outFinishPreviousGesture = true;
2889 }
2890
2891 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2892 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2893 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002894 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2895 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002896 lastFingerCount == 1) {
2897 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002898 float x, y;
2899 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002900 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2901 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002902 if (DEBUG_GESTURES) {
2903 ALOGD("Gestures: TAP");
2904 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002905
2906 mPointerGesture.tapUpTime = when;
2907 getContext()->requestTimeoutAtTime(when +
2908 mConfig.pointerGestureTapDragInterval);
2909
2910 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002911 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
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 =
2917 mPointerGesture.activeGestureId;
2918 mPointerGesture.currentGestureProperties[0].toolType =
2919 AMOTION_EVENT_TOOL_TYPE_FINGER;
2920 mPointerGesture.currentGestureCoords[0].clear();
2921 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2922 mPointerGesture.tapX);
2923 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2924 mPointerGesture.tapY);
2925 mPointerGesture.currentGestureCoords[0]
2926 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2927
2928 tapped = true;
2929 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002930 if (DEBUG_GESTURES) {
2931 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2932 y - mPointerGesture.tapY);
2933 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002934 }
2935 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002936 if (DEBUG_GESTURES) {
2937 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2938 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2939 (when - mPointerGesture.tapDownTime) * 0.000001f);
2940 } else {
2941 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2942 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002943 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002944 }
2945 }
2946
2947 mPointerVelocityControl.reset();
2948
2949 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002950 if (DEBUG_GESTURES) {
2951 ALOGD("Gestures: NEUTRAL");
2952 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002953 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002954 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002955 mPointerGesture.currentGestureIdBits.clear();
2956 }
2957 } else if (currentFingerCount == 1) {
2958 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2959 // The pointer follows the active touch point.
2960 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2961 // When in TAP_DRAG, emit MOVE events at the pointer location.
2962 ALOG_ASSERT(activeTouchId >= 0);
2963
Michael Wright227c5542020-07-02 18:30:52 +01002964 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2965 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002966 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002967 float x, y;
2968 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002969 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2970 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002971 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002972 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002973 if (DEBUG_GESTURES) {
2974 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2975 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
2976 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002977 }
2978 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002979 if (DEBUG_GESTURES) {
2980 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2981 (when - mPointerGesture.tapUpTime) * 0.000001f);
2982 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002983 }
Michael Wright227c5542020-07-02 18:30:52 +01002984 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2985 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002986 }
2987
2988 float deltaX = 0, deltaY = 0;
2989 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2990 const RawPointerData::Pointer& currentPointer =
2991 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2992 const RawPointerData::Pointer& lastPointer =
2993 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2994 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2995 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2996
Prabir Pradhan1728b212021-10-19 16:00:03 -07002997 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002998 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2999
3000 // Move the pointer using a relative motion.
3001 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003002 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003003 } else {
3004 mPointerVelocityControl.reset();
3005 }
3006
3007 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003008 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003009 if (DEBUG_GESTURES) {
3010 ALOGD("Gestures: TAP_DRAG");
3011 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003012 down = true;
3013 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003014 if (DEBUG_GESTURES) {
3015 ALOGD("Gestures: HOVER");
3016 }
Michael Wright227c5542020-07-02 18:30:52 +01003017 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003018 *outFinishPreviousGesture = true;
3019 }
3020 mPointerGesture.activeGestureId = 0;
3021 down = false;
3022 }
3023
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003024 float x, y;
3025 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003026
3027 mPointerGesture.currentGestureIdBits.clear();
3028 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3029 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3030 mPointerGesture.currentGestureProperties[0].clear();
3031 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3032 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3033 mPointerGesture.currentGestureCoords[0].clear();
3034 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3035 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3036 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3037 down ? 1.0f : 0.0f);
3038
3039 if (lastFingerCount == 0 && currentFingerCount != 0) {
3040 mPointerGesture.resetTap();
3041 mPointerGesture.tapDownTime = when;
3042 mPointerGesture.tapX = x;
3043 mPointerGesture.tapY = y;
3044 }
3045 } else {
3046 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3047 // We need to provide feedback for each finger that goes down so we cannot wait
3048 // for the fingers to move before deciding what to do.
3049 //
3050 // The ambiguous case is deciding what to do when there are two fingers down but they
3051 // have not moved enough to determine whether they are part of a drag or part of a
3052 // freeform gesture, or just a press or long-press at the pointer location.
3053 //
3054 // When there are two fingers we start with the PRESS hypothesis and we generate a
3055 // down at the pointer location.
3056 //
3057 // When the two fingers move enough or when additional fingers are added, we make
3058 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3059 ALOG_ASSERT(activeTouchId >= 0);
3060
3061 bool settled = when >=
3062 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003063 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3064 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3065 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003066 *outFinishPreviousGesture = true;
3067 } else if (!settled && currentFingerCount > lastFingerCount) {
3068 // Additional pointers have gone down but not yet settled.
3069 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003070 if (DEBUG_GESTURES) {
3071 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3072 "MULTITOUCH, settle time remaining %0.3fms",
3073 (mPointerGesture.firstTouchTime +
3074 mConfig.pointerGestureMultitouchSettleInterval - when) *
3075 0.000001f);
3076 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003077 *outCancelPreviousGesture = true;
3078 } else {
3079 // Continue previous gesture.
3080 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3081 }
3082
3083 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003084 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003085 mPointerGesture.activeGestureId = 0;
3086 mPointerGesture.referenceIdBits.clear();
3087 mPointerVelocityControl.reset();
3088
3089 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003090 if (DEBUG_GESTURES) {
3091 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3092 "settle time remaining %0.3fms",
3093 (mPointerGesture.firstTouchTime +
3094 mConfig.pointerGestureMultitouchSettleInterval - when) *
3095 0.000001f);
3096 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003097 mCurrentRawState.rawPointerData
3098 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3099 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003100 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3101 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003102 }
3103
3104 // Clear the reference deltas for fingers not yet included in the reference calculation.
3105 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3106 ~mPointerGesture.referenceIdBits.value);
3107 !idBits.isEmpty();) {
3108 uint32_t id = idBits.clearFirstMarkedBit();
3109 mPointerGesture.referenceDeltas[id].dx = 0;
3110 mPointerGesture.referenceDeltas[id].dy = 0;
3111 }
3112 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3113
3114 // Add delta for all fingers and calculate a common movement delta.
3115 float commonDeltaX = 0, commonDeltaY = 0;
3116 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3117 mCurrentCookedState.fingerIdBits.value);
3118 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3119 bool first = (idBits == commonIdBits);
3120 uint32_t id = idBits.clearFirstMarkedBit();
3121 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3122 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3123 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3124 delta.dx += cpd.x - lpd.x;
3125 delta.dy += cpd.y - lpd.y;
3126
3127 if (first) {
3128 commonDeltaX = delta.dx;
3129 commonDeltaY = delta.dy;
3130 } else {
3131 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3132 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3133 }
3134 }
3135
3136 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003137 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003138 float dist[MAX_POINTER_ID + 1];
3139 int32_t distOverThreshold = 0;
3140 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3141 uint32_t id = idBits.clearFirstMarkedBit();
3142 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3143 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3144 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3145 distOverThreshold += 1;
3146 }
3147 }
3148
3149 // Only transition when at least two pointers have moved further than
3150 // the minimum distance threshold.
3151 if (distOverThreshold >= 2) {
3152 if (currentFingerCount > 2) {
3153 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003154 if (DEBUG_GESTURES) {
3155 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3156 currentFingerCount);
3157 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003158 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003159 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003160 } else {
3161 // There are exactly two pointers.
3162 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3163 uint32_t id1 = idBits.clearFirstMarkedBit();
3164 uint32_t id2 = idBits.firstMarkedBit();
3165 const RawPointerData::Pointer& p1 =
3166 mCurrentRawState.rawPointerData.pointerForId(id1);
3167 const RawPointerData::Pointer& p2 =
3168 mCurrentRawState.rawPointerData.pointerForId(id2);
3169 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3170 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3171 // There are two pointers but they are too far apart for a SWIPE,
3172 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003173 if (DEBUG_GESTURES) {
3174 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3175 "%0.3f",
3176 mutualDistance, mPointerGestureMaxSwipeWidth);
3177 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003178 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003179 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003180 } else {
3181 // There are two pointers. Wait for both pointers to start moving
3182 // before deciding whether this is a SWIPE or FREEFORM gesture.
3183 float dist1 = dist[id1];
3184 float dist2 = dist[id2];
3185 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3186 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3187 // Calculate the dot product of the displacement vectors.
3188 // When the vectors are oriented in approximately the same direction,
3189 // the angle betweeen them is near zero and the cosine of the angle
3190 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3191 // mag(v2).
3192 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3193 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3194 float dx1 = delta1.dx * mPointerXZoomScale;
3195 float dy1 = delta1.dy * mPointerYZoomScale;
3196 float dx2 = delta2.dx * mPointerXZoomScale;
3197 float dy2 = delta2.dy * mPointerYZoomScale;
3198 float dot = dx1 * dx2 + dy1 * dy2;
3199 float cosine = dot / (dist1 * dist2); // denominator always > 0
3200 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3201 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003202 if (DEBUG_GESTURES) {
3203 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3204 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3205 "cosine %0.3f >= %0.3f",
3206 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3207 mConfig.pointerGestureMultitouchMinDistance, cosine,
3208 mConfig.pointerGestureSwipeTransitionAngleCosine);
3209 }
Michael Wright227c5542020-07-02 18:30:52 +01003210 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003211 } else {
3212 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003213 if (DEBUG_GESTURES) {
3214 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3215 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3216 "cosine %0.3f < %0.3f",
3217 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3218 mConfig.pointerGestureMultitouchMinDistance, cosine,
3219 mConfig.pointerGestureSwipeTransitionAngleCosine);
3220 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003221 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003222 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003223 }
3224 }
3225 }
3226 }
3227 }
Michael Wright227c5542020-07-02 18:30:52 +01003228 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003229 // Switch from SWIPE to FREEFORM if additional pointers go down.
3230 // Cancel previous gesture.
3231 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003232 if (DEBUG_GESTURES) {
3233 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3234 currentFingerCount);
3235 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003236 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003237 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003238 }
3239 }
3240
3241 // Move the reference points based on the overall group motion of the fingers
3242 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003243 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003244 (commonDeltaX || commonDeltaY)) {
3245 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3246 uint32_t id = idBits.clearFirstMarkedBit();
3247 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3248 delta.dx = 0;
3249 delta.dy = 0;
3250 }
3251
3252 mPointerGesture.referenceTouchX += commonDeltaX;
3253 mPointerGesture.referenceTouchY += commonDeltaY;
3254
3255 commonDeltaX *= mPointerXMovementScale;
3256 commonDeltaY *= mPointerYMovementScale;
3257
Prabir Pradhan1728b212021-10-19 16:00:03 -07003258 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003259 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3260
3261 mPointerGesture.referenceGestureX += commonDeltaX;
3262 mPointerGesture.referenceGestureY += commonDeltaY;
3263 }
3264
3265 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003266 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3267 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003268 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003269 if (DEBUG_GESTURES) {
3270 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3271 "activeGestureId=%d, currentTouchPointerCount=%d",
3272 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3273 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003274 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3275
3276 mPointerGesture.currentGestureIdBits.clear();
3277 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3278 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3279 mPointerGesture.currentGestureProperties[0].clear();
3280 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3281 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3282 mPointerGesture.currentGestureCoords[0].clear();
3283 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3284 mPointerGesture.referenceGestureX);
3285 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3286 mPointerGesture.referenceGestureY);
3287 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003288 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003289 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003290 if (DEBUG_GESTURES) {
3291 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3292 "activeGestureId=%d, currentTouchPointerCount=%d",
3293 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3294 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003295 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3296
3297 mPointerGesture.currentGestureIdBits.clear();
3298
3299 BitSet32 mappedTouchIdBits;
3300 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003301 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003302 // Initially, assign the active gesture id to the active touch point
3303 // if there is one. No other touch id bits are mapped yet.
3304 if (!*outCancelPreviousGesture) {
3305 mappedTouchIdBits.markBit(activeTouchId);
3306 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3307 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3308 mPointerGesture.activeGestureId;
3309 } else {
3310 mPointerGesture.activeGestureId = -1;
3311 }
3312 } else {
3313 // Otherwise, assume we mapped all touches from the previous frame.
3314 // Reuse all mappings that are still applicable.
3315 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3316 mCurrentCookedState.fingerIdBits.value;
3317 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3318
3319 // Check whether we need to choose a new active gesture id because the
3320 // current went went up.
3321 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3322 ~mCurrentCookedState.fingerIdBits.value);
3323 !upTouchIdBits.isEmpty();) {
3324 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3325 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3326 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3327 mPointerGesture.activeGestureId = -1;
3328 break;
3329 }
3330 }
3331 }
3332
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003333 if (DEBUG_GESTURES) {
3334 ALOGD("Gestures: FREEFORM follow up "
3335 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3336 "activeGestureId=%d",
3337 mappedTouchIdBits.value, usedGestureIdBits.value,
3338 mPointerGesture.activeGestureId);
3339 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003340
3341 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3342 for (uint32_t i = 0; i < currentFingerCount; i++) {
3343 uint32_t touchId = idBits.clearFirstMarkedBit();
3344 uint32_t gestureId;
3345 if (!mappedTouchIdBits.hasBit(touchId)) {
3346 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3347 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003348 if (DEBUG_GESTURES) {
3349 ALOGD("Gestures: FREEFORM "
3350 "new mapping for touch id %d -> gesture id %d",
3351 touchId, gestureId);
3352 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003353 } else {
3354 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003355 if (DEBUG_GESTURES) {
3356 ALOGD("Gestures: FREEFORM "
3357 "existing mapping for touch id %d -> gesture id %d",
3358 touchId, gestureId);
3359 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003360 }
3361 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3362 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3363
3364 const RawPointerData::Pointer& pointer =
3365 mCurrentRawState.rawPointerData.pointerForId(touchId);
3366 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3367 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003368 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003369
3370 mPointerGesture.currentGestureProperties[i].clear();
3371 mPointerGesture.currentGestureProperties[i].id = gestureId;
3372 mPointerGesture.currentGestureProperties[i].toolType =
3373 AMOTION_EVENT_TOOL_TYPE_FINGER;
3374 mPointerGesture.currentGestureCoords[i].clear();
3375 mPointerGesture.currentGestureCoords[i]
3376 .setAxisValue(AMOTION_EVENT_AXIS_X,
3377 mPointerGesture.referenceGestureX + deltaX);
3378 mPointerGesture.currentGestureCoords[i]
3379 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3380 mPointerGesture.referenceGestureY + deltaY);
3381 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3382 1.0f);
3383 }
3384
3385 if (mPointerGesture.activeGestureId < 0) {
3386 mPointerGesture.activeGestureId =
3387 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003388 if (DEBUG_GESTURES) {
3389 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3390 mPointerGesture.activeGestureId);
3391 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003392 }
3393 }
3394 }
3395
3396 mPointerController->setButtonState(mCurrentRawState.buttonState);
3397
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003398 if (DEBUG_GESTURES) {
3399 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3400 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3401 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3402 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3403 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3404 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3405 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3406 uint32_t id = idBits.clearFirstMarkedBit();
3407 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3408 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3409 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3410 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3411 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3412 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3413 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3414 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3415 }
3416 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3417 uint32_t id = idBits.clearFirstMarkedBit();
3418 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3419 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3420 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3421 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3422 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3423 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3424 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3425 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3426 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003427 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003428 return true;
3429}
3430
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003432 mPointerSimple.currentCoords.clear();
3433 mPointerSimple.currentProperties.clear();
3434
3435 bool down, hovering;
3436 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3437 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3438 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003439 mPointerController
3440 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3441 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003442
3443 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3444 down = !hovering;
3445
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003446 float x, y;
3447 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003448 mPointerSimple.currentCoords.copyFrom(
3449 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3450 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3451 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3452 mPointerSimple.currentProperties.id = 0;
3453 mPointerSimple.currentProperties.toolType =
3454 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3455 } else {
3456 down = false;
3457 hovering = false;
3458 }
3459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003461}
3462
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003463void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3464 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003465}
3466
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003468 mPointerSimple.currentCoords.clear();
3469 mPointerSimple.currentProperties.clear();
3470
3471 bool down, hovering;
3472 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3473 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3474 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3475 float deltaX = 0, deltaY = 0;
3476 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3477 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3478 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3479 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3480 mPointerXMovementScale;
3481 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3482 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3483 mPointerYMovementScale;
3484
Prabir Pradhan1728b212021-10-19 16:00:03 -07003485 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003486 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3487
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003488 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003489 } else {
3490 mPointerVelocityControl.reset();
3491 }
3492
3493 down = isPointerDown(mCurrentRawState.buttonState);
3494 hovering = !down;
3495
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003496 float x, y;
3497 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003498 mPointerSimple.currentCoords.copyFrom(
3499 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3500 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3501 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3502 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3503 hovering ? 0.0f : 1.0f);
3504 mPointerSimple.currentProperties.id = 0;
3505 mPointerSimple.currentProperties.toolType =
3506 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3507 } else {
3508 mPointerVelocityControl.reset();
3509
3510 down = false;
3511 hovering = false;
3512 }
3513
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003515}
3516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3518 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003519
3520 mPointerVelocityControl.reset();
3521}
3522
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003523void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3524 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003525 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003526
3527 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003528 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003529 mPointerController->clearSpots();
3530 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003531 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003532 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003533 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003534 }
Garfield Tan9514d782020-11-10 16:37:23 -08003535 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003536
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003537 float xCursorPosition, yCursorPosition;
3538 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003539
3540 if (mPointerSimple.down && !down) {
3541 mPointerSimple.down = false;
3542
3543 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3545 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003546 mLastRawState.buttonState, MotionClassification::NONE,
3547 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3548 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3549 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3550 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003551 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003552 }
3553
3554 if (mPointerSimple.hovering && !hovering) {
3555 mPointerSimple.hovering = false;
3556
3557 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003558 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3559 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3560 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003561 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3562 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3563 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3564 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003565 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003566 }
3567
3568 if (down) {
3569 if (!mPointerSimple.down) {
3570 mPointerSimple.down = true;
3571 mPointerSimple.downTime = when;
3572
3573 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003574 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003575 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3576 metaState, mCurrentRawState.buttonState,
3577 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3578 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3579 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3580 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003581 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003582 }
3583
3584 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3586 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003587 mCurrentRawState.buttonState, MotionClassification::NONE,
3588 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3589 &mPointerSimple.currentCoords, mOrientedXPrecision,
3590 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3591 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003592 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003593 }
3594
3595 if (hovering) {
3596 if (!mPointerSimple.hovering) {
3597 mPointerSimple.hovering = true;
3598
3599 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003601 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3602 metaState, mCurrentRawState.buttonState,
3603 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3604 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3605 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3606 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003607 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003608 }
3609
3610 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003611 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3612 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3613 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003614 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3615 &mPointerSimple.currentCoords, mOrientedXPrecision,
3616 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3617 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003618 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003619 }
3620
3621 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3622 float vscroll = mCurrentRawState.rawVScroll;
3623 float hscroll = mCurrentRawState.rawHScroll;
3624 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3625 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3626
3627 // Send scroll.
3628 PointerCoords pointerCoords;
3629 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3630 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3631 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3632
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003633 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3634 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003635 mCurrentRawState.buttonState, MotionClassification::NONE,
3636 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3637 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3638 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3639 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003640 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003641 }
3642
3643 // Save state.
3644 if (down || hovering) {
3645 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3646 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3647 } else {
3648 mPointerSimple.reset();
3649 }
3650}
3651
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003653 mPointerSimple.currentCoords.clear();
3654 mPointerSimple.currentProperties.clear();
3655
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003657}
3658
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3660 uint32_t source, int32_t action, int32_t actionButton,
3661 int32_t flags, int32_t metaState, int32_t buttonState,
3662 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003663 const PointerCoords* coords, const uint32_t* idToIndex,
3664 BitSet32 idBits, int32_t changedId, float xPrecision,
3665 float yPrecision, nsecs_t downTime) {
3666 PointerCoords pointerCoords[MAX_POINTERS];
3667 PointerProperties pointerProperties[MAX_POINTERS];
3668 uint32_t pointerCount = 0;
3669 while (!idBits.isEmpty()) {
3670 uint32_t id = idBits.clearFirstMarkedBit();
3671 uint32_t index = idToIndex[id];
3672 pointerProperties[pointerCount].copyFrom(properties[index]);
3673 pointerCoords[pointerCount].copyFrom(coords[index]);
3674
3675 if (changedId >= 0 && id == uint32_t(changedId)) {
3676 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3677 }
3678
3679 pointerCount += 1;
3680 }
3681
3682 ALOG_ASSERT(pointerCount != 0);
3683
3684 if (changedId >= 0 && pointerCount == 1) {
3685 // Replace initial down and final up action.
3686 // We can compare the action without masking off the changed pointer index
3687 // because we know the index is 0.
3688 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3689 action = AMOTION_EVENT_ACTION_DOWN;
3690 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003691 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3692 action = AMOTION_EVENT_ACTION_CANCEL;
3693 } else {
3694 action = AMOTION_EVENT_ACTION_UP;
3695 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003696 } else {
3697 // Can't happen.
3698 ALOG_ASSERT(false);
3699 }
3700 }
3701 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3702 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003703 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003704 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003705 }
3706 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3707 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003708 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003709 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003710 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3712 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003713 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3714 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3715 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003716 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003717}
3718
3719bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3720 const PointerCoords* inCoords,
3721 const uint32_t* inIdToIndex,
3722 PointerProperties* outProperties,
3723 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3724 BitSet32 idBits) const {
3725 bool changed = false;
3726 while (!idBits.isEmpty()) {
3727 uint32_t id = idBits.clearFirstMarkedBit();
3728 uint32_t inIndex = inIdToIndex[id];
3729 uint32_t outIndex = outIdToIndex[id];
3730
3731 const PointerProperties& curInProperties = inProperties[inIndex];
3732 const PointerCoords& curInCoords = inCoords[inIndex];
3733 PointerProperties& curOutProperties = outProperties[outIndex];
3734 PointerCoords& curOutCoords = outCoords[outIndex];
3735
3736 if (curInProperties != curOutProperties) {
3737 curOutProperties.copyFrom(curInProperties);
3738 changed = true;
3739 }
3740
3741 if (curInCoords != curOutCoords) {
3742 curOutCoords.copyFrom(curInCoords);
3743 changed = true;
3744 }
3745 }
3746 return changed;
3747}
3748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3750 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3751 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003752}
3753
Prabir Pradhan1728b212021-10-19 16:00:03 -07003754// Transform input device coordinates to display panel coordinates.
3755void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003756 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3757 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3758
arthurhunga36b28e2020-12-29 20:28:15 +08003759 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3760 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3761
Prabir Pradhan1728b212021-10-19 16:00:03 -07003762 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003763 // 0 - no swap and reverse.
3764 // 90 - swap x/y and reverse y.
3765 // 180 - reverse x, y.
3766 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003767 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003768 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003769 x = xScaled;
3770 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003771 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003772 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003773 y = xScaledMax;
3774 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003775 break;
3776 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003777 x = xScaledMax;
3778 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003779 break;
3780 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003781 y = xScaled;
3782 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003783 break;
3784 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003785 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003786 }
3787}
3788
Prabir Pradhan1728b212021-10-19 16:00:03 -07003789bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003790 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3791 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3792
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003793 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003794 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003795 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003796 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003797}
3798
3799const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3800 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003801 if (DEBUG_VIRTUAL_KEYS) {
3802 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3803 "left=%d, top=%d, right=%d, bottom=%d",
3804 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3805 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3806 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003807
3808 if (virtualKey.isHit(x, y)) {
3809 return &virtualKey;
3810 }
3811 }
3812
3813 return nullptr;
3814}
3815
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003816void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3817 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3818 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003819
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003820 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003821
3822 if (currentPointerCount == 0) {
3823 // No pointers to assign.
3824 return;
3825 }
3826
3827 if (lastPointerCount == 0) {
3828 // All pointers are new.
3829 for (uint32_t i = 0; i < currentPointerCount; i++) {
3830 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003831 current.rawPointerData.pointers[i].id = id;
3832 current.rawPointerData.idToIndex[id] = i;
3833 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003834 }
3835 return;
3836 }
3837
3838 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003839 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003840 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003841 uint32_t id = last.rawPointerData.pointers[0].id;
3842 current.rawPointerData.pointers[0].id = id;
3843 current.rawPointerData.idToIndex[id] = 0;
3844 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003845 return;
3846 }
3847
3848 // General case.
3849 // We build a heap of squared euclidean distances between current and last pointers
3850 // associated with the current and last pointer indices. Then, we find the best
3851 // match (by distance) for each current pointer.
3852 // The pointers must have the same tool type but it is possible for them to
3853 // transition from hovering to touching or vice-versa while retaining the same id.
3854 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3855
3856 uint32_t heapSize = 0;
3857 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3858 currentPointerIndex++) {
3859 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3860 lastPointerIndex++) {
3861 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003862 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003863 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003864 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003865 if (currentPointer.toolType == lastPointer.toolType) {
3866 int64_t deltaX = currentPointer.x - lastPointer.x;
3867 int64_t deltaY = currentPointer.y - lastPointer.y;
3868
3869 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3870
3871 // Insert new element into the heap (sift up).
3872 heap[heapSize].currentPointerIndex = currentPointerIndex;
3873 heap[heapSize].lastPointerIndex = lastPointerIndex;
3874 heap[heapSize].distance = distance;
3875 heapSize += 1;
3876 }
3877 }
3878 }
3879
3880 // Heapify
3881 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3882 startIndex -= 1;
3883 for (uint32_t parentIndex = startIndex;;) {
3884 uint32_t childIndex = parentIndex * 2 + 1;
3885 if (childIndex >= heapSize) {
3886 break;
3887 }
3888
3889 if (childIndex + 1 < heapSize &&
3890 heap[childIndex + 1].distance < heap[childIndex].distance) {
3891 childIndex += 1;
3892 }
3893
3894 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3895 break;
3896 }
3897
3898 swap(heap[parentIndex], heap[childIndex]);
3899 parentIndex = childIndex;
3900 }
3901 }
3902
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003903 if (DEBUG_POINTER_ASSIGNMENT) {
3904 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3905 for (size_t i = 0; i < heapSize; i++) {
3906 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3907 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3908 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003909 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003910
3911 // Pull matches out by increasing order of distance.
3912 // To avoid reassigning pointers that have already been matched, the loop keeps track
3913 // of which last and current pointers have been matched using the matchedXXXBits variables.
3914 // It also tracks the used pointer id bits.
3915 BitSet32 matchedLastBits(0);
3916 BitSet32 matchedCurrentBits(0);
3917 BitSet32 usedIdBits(0);
3918 bool first = true;
3919 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3920 while (heapSize > 0) {
3921 if (first) {
3922 // The first time through the loop, we just consume the root element of
3923 // the heap (the one with smallest distance).
3924 first = false;
3925 } else {
3926 // Previous iterations consumed the root element of the heap.
3927 // Pop root element off of the heap (sift down).
3928 heap[0] = heap[heapSize];
3929 for (uint32_t parentIndex = 0;;) {
3930 uint32_t childIndex = parentIndex * 2 + 1;
3931 if (childIndex >= heapSize) {
3932 break;
3933 }
3934
3935 if (childIndex + 1 < heapSize &&
3936 heap[childIndex + 1].distance < heap[childIndex].distance) {
3937 childIndex += 1;
3938 }
3939
3940 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3941 break;
3942 }
3943
3944 swap(heap[parentIndex], heap[childIndex]);
3945 parentIndex = childIndex;
3946 }
3947
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003948 if (DEBUG_POINTER_ASSIGNMENT) {
3949 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3950 for (size_t j = 0; j < heapSize; j++) {
3951 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3952 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3953 heap[j].distance);
3954 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003955 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003956 }
3957
3958 heapSize -= 1;
3959
3960 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3961 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3962
3963 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3964 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3965
3966 matchedCurrentBits.markBit(currentPointerIndex);
3967 matchedLastBits.markBit(lastPointerIndex);
3968
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003969 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3970 current.rawPointerData.pointers[currentPointerIndex].id = id;
3971 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3972 current.rawPointerData.markIdBit(id,
3973 current.rawPointerData.isHovering(
3974 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003975 usedIdBits.markBit(id);
3976
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003977 if (DEBUG_POINTER_ASSIGNMENT) {
3978 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3979 ", distance=%" PRIu64,
3980 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3981 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003982 break;
3983 }
3984 }
3985
3986 // Assign fresh ids to pointers that were not matched in the process.
3987 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3988 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3989 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3990
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003991 current.rawPointerData.pointers[currentPointerIndex].id = id;
3992 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3993 current.rawPointerData.markIdBit(id,
3994 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003995
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003996 if (DEBUG_POINTER_ASSIGNMENT) {
3997 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3998 id);
3999 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004000 }
4001}
4002
4003int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4004 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4005 return AKEY_STATE_VIRTUAL;
4006 }
4007
4008 for (const VirtualKey& virtualKey : mVirtualKeys) {
4009 if (virtualKey.keyCode == keyCode) {
4010 return AKEY_STATE_UP;
4011 }
4012 }
4013
4014 return AKEY_STATE_UNKNOWN;
4015}
4016
4017int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4018 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4019 return AKEY_STATE_VIRTUAL;
4020 }
4021
4022 for (const VirtualKey& virtualKey : mVirtualKeys) {
4023 if (virtualKey.scanCode == scanCode) {
4024 return AKEY_STATE_UP;
4025 }
4026 }
4027
4028 return AKEY_STATE_UNKNOWN;
4029}
4030
4031bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4032 const int32_t* keyCodes, uint8_t* outFlags) {
4033 for (const VirtualKey& virtualKey : mVirtualKeys) {
4034 for (size_t i = 0; i < numCodes; i++) {
4035 if (virtualKey.keyCode == keyCodes[i]) {
4036 outFlags[i] = 1;
4037 }
4038 }
4039 }
4040
4041 return true;
4042}
4043
4044std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4045 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004046 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004047 return std::make_optional(mPointerController->getDisplayId());
4048 } else {
4049 return std::make_optional(mViewport.displayId);
4050 }
4051 }
4052 return std::nullopt;
4053}
4054
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004055} // namespace android