blob: cd0e3537cfd735bcfec5e3e0c78242f64e504296 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Wright227c5542020-07-02 18:30:52 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wright227c5542020-07-02 18:30:52 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "TouchInputMapper.h"
22
Dominik Laskowski75788452021-02-09 18:51:25 -080023#include <ftl/enum.h>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorButtonAccumulator.h"
26#include "CursorScrollAccumulator.h"
27#include "TouchButtonAccumulator.h"
28#include "TouchCursorInputMapperCommon.h"
29
30namespace android {
31
32// --- Constants ---
33
34// Maximum amount of latency to add to touch events while waiting for data from an
35// external stylus.
36static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
37
38// Maximum amount of time to wait on touch data before pushing out new pressure data.
39static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
40
41// Artificial latency on synthetic events created from stylus data without corresponding touch
42// data.
43static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
44
HQ Liue6983c72022-04-19 22:14:56 +000045// Minimum width between two pointers to determine a gesture as freeform gesture in mm
46static const float MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER = 30;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070047// --- Static Definitions ---
48
49template <typename T>
50inline static void swap(T& a, T& b) {
51 T temp = a;
52 a = b;
53 b = temp;
54}
55
56static float calculateCommonVector(float a, float b) {
57 if (a > 0 && b > 0) {
58 return a < b ? a : b;
59 } else if (a < 0 && b < 0) {
60 return a > b ? a : b;
61 } else {
62 return 0;
63 }
64}
65
66inline static float distance(float x1, float y1, float x2, float y2) {
67 return hypotf(x1 - x2, y1 - y2);
68}
69
70inline static int32_t signExtendNybble(int32_t value) {
71 return value >= 8 ? value - 16 : value;
72}
73
74// --- RawPointerAxes ---
75
76RawPointerAxes::RawPointerAxes() {
77 clear();
78}
79
80void RawPointerAxes::clear() {
81 x.clear();
82 y.clear();
83 pressure.clear();
84 touchMajor.clear();
85 touchMinor.clear();
86 toolMajor.clear();
87 toolMinor.clear();
88 orientation.clear();
89 distance.clear();
90 tiltX.clear();
91 tiltY.clear();
92 trackingId.clear();
93 slot.clear();
94}
95
96// --- RawPointerData ---
97
98RawPointerData::RawPointerData() {
99 clear();
100}
101
102void RawPointerData::clear() {
103 pointerCount = 0;
104 clearIdBits();
105}
106
107void RawPointerData::copyFrom(const RawPointerData& other) {
108 pointerCount = other.pointerCount;
109 hoveringIdBits = other.hoveringIdBits;
110 touchingIdBits = other.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +0800111 canceledIdBits = other.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112
113 for (uint32_t i = 0; i < pointerCount; i++) {
114 pointers[i] = other.pointers[i];
115
116 int id = pointers[i].id;
117 idToIndex[id] = other.idToIndex[id];
118 }
119}
120
121void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
122 float x = 0, y = 0;
123 uint32_t count = touchingIdBits.count();
124 if (count) {
125 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
126 uint32_t id = idBits.clearFirstMarkedBit();
127 const Pointer& pointer = pointerForId(id);
128 x += pointer.x;
129 y += pointer.y;
130 }
131 x /= count;
132 y /= count;
133 }
134 *outX = x;
135 *outY = y;
136}
137
138// --- CookedPointerData ---
139
140CookedPointerData::CookedPointerData() {
141 clear();
142}
143
144void CookedPointerData::clear() {
145 pointerCount = 0;
146 hoveringIdBits.clear();
147 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800148 canceledIdBits.clear();
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000149 validIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700150}
151
152void CookedPointerData::copyFrom(const CookedPointerData& other) {
153 pointerCount = other.pointerCount;
154 hoveringIdBits = other.hoveringIdBits;
155 touchingIdBits = other.touchingIdBits;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000156 validIdBits = other.validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700157
158 for (uint32_t i = 0; i < pointerCount; i++) {
159 pointerProperties[i].copyFrom(other.pointerProperties[i]);
160 pointerCoords[i].copyFrom(other.pointerCoords[i]);
161
162 int id = pointerProperties[i].id;
163 idToIndex[id] = other.idToIndex[id];
164 }
165}
166
167// --- TouchInputMapper ---
168
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800169TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
170 : InputMapper(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700171 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100172 mDeviceMode(DeviceMode::DISABLED),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700173 mDisplayWidth(-1),
174 mDisplayHeight(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700175 mPhysicalWidth(-1),
176 mPhysicalHeight(-1),
177 mPhysicalLeft(0),
178 mPhysicalTop(0),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700179 mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700180
181TouchInputMapper::~TouchInputMapper() {}
182
Philip Junker4af3b3d2021-12-14 10:36:55 +0100183uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700184 return mSource;
185}
186
187void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
188 InputMapper::populateDeviceInfo(info);
189
Michael Wright227c5542020-07-02 18:30:52 +0100190 if (mDeviceMode != DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191 info->addMotionRange(mOrientedRanges.x);
192 info->addMotionRange(mOrientedRanges.y);
193 info->addMotionRange(mOrientedRanges.pressure);
194
Chris Yef74dc422020-09-02 22:41:50 -0700195 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
Chris Ye8fa17282020-09-15 17:17:34 -0700196 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
197 //
198 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
199 // motion, i.e. the hardware dimensions, as the finger could move completely across the
200 // touchpad in one sample cycle.
Chris Yef74dc422020-09-02 22:41:50 -0700201 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
202 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
203 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat,
204 x.fuzz, x.resolution);
205 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat,
206 y.fuzz, y.resolution);
207 }
208
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700209 if (mOrientedRanges.size) {
210 info->addMotionRange(*mOrientedRanges.size);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700211 }
212
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700213 if (mOrientedRanges.touchMajor) {
214 info->addMotionRange(*mOrientedRanges.touchMajor);
215 info->addMotionRange(*mOrientedRanges.touchMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700216 }
217
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700218 if (mOrientedRanges.toolMajor) {
219 info->addMotionRange(*mOrientedRanges.toolMajor);
220 info->addMotionRange(*mOrientedRanges.toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 }
222
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700223 if (mOrientedRanges.orientation) {
224 info->addMotionRange(*mOrientedRanges.orientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700225 }
226
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700227 if (mOrientedRanges.distance) {
228 info->addMotionRange(*mOrientedRanges.distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 }
230
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700231 if (mOrientedRanges.tilt) {
232 info->addMotionRange(*mOrientedRanges.tilt);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700233 }
234
235 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
236 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
237 0.0f);
238 }
239 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
240 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
241 0.0f);
242 }
Michael Wright227c5542020-07-02 18:30:52 +0100243 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700244 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
245 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
246 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
247 x.fuzz, x.resolution);
248 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
249 y.fuzz, y.resolution);
250 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
251 x.fuzz, x.resolution);
252 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
253 y.fuzz, y.resolution);
254 }
255 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
256 }
257}
258
259void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700260 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800261 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700262 dumpParameters(dump);
263 dumpVirtualKeys(dump);
264 dumpRawPointerAxes(dump);
265 dumpCalibration(dump);
266 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700267 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268
269 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270 dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale);
271 dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale);
272 dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
273 dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
274 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
275 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
276 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
277 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
278 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
279 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
280 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
281 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
282 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
283 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
284
285 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
286 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
287 mLastRawState.rawPointerData.pointerCount);
288 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
289 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
290 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
291 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
292 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
293 "toolType=%d, isHovering=%s\n",
294 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
295 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
296 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
297 pointer.distance, pointer.toolType, toString(pointer.isHovering));
298 }
299
300 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
301 mLastCookedState.buttonState);
302 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
303 mLastCookedState.cookedPointerData.pointerCount);
304 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
305 const PointerProperties& pointerProperties =
306 mLastCookedState.cookedPointerData.pointerProperties[i];
307 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000308 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
309 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
310 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
312 "toolType=%d, isHovering=%s\n",
313 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000314 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
315 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
317 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
318 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
319 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
320 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
321 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
322 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
323 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
324 pointerProperties.toolType,
325 toString(mLastCookedState.cookedPointerData.isHovering(i)));
326 }
327
328 dump += INDENT3 "Stylus Fusion:\n";
329 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
330 toString(mExternalStylusConnected));
331 dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
332 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
333 mExternalStylusFusionTimeout);
334 dump += INDENT3 "External Stylus State:\n";
335 dumpStylusState(dump, mExternalStylusState);
336
Michael Wright227c5542020-07-02 18:30:52 +0100337 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700338 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
339 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
340 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
341 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
342 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
343 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
344 }
345}
346
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
348 uint32_t changes) {
349 InputMapper::configure(when, config, changes);
350
351 mConfig = *config;
352
353 if (!changes) { // first time only
354 // Configure basic parameters.
355 configureParameters();
356
357 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800358 mCursorScrollAccumulator.configure(getDeviceContext());
359 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700360
361 // Configure absolute axis information.
362 configureRawPointerAxes();
363
364 // Prepare input device calibration.
365 parseCalibration();
366 resolveCalibration();
367 }
368
369 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
370 // Update location calibration to reflect current settings
371 updateAffineTransformation();
372 }
373
374 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
375 // Update pointer speed.
376 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
377 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
378 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
379 }
380
381 bool resetNeeded = false;
382 if (!changes ||
383 (changes &
384 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800385 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700386 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
387 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
388 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700389 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700391 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392 }
393
394 if (changes && resetNeeded) {
395 // Send reset, unless this is the first time the device has been configured,
396 // in which case the reader will call reset itself after all mappers are ready.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000397 NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId());
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700398 getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700399 }
400}
401
402void TouchInputMapper::resolveExternalStylusPresence() {
403 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800404 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405 mExternalStylusConnected = !devices.empty();
406
407 if (!mExternalStylusConnected) {
408 resetExternalStylus();
409 }
410}
411
412void TouchInputMapper::configureParameters() {
413 // Use the pointer presentation mode for devices that do not support distinct
414 // multitouch. The spot-based presentation relies on being able to accurately
415 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800416 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100417 ? Parameters::GestureMode::SINGLE_TOUCH
418 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700419
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700420 std::string gestureModeString;
421 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800422 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700423 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100424 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100426 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700427 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700428 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 }
430 }
431
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800432 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100434 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800435 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700436 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100437 mParameters.deviceType = Parameters::DeviceType::POINTER;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800438 } else if (getDeviceContext().hasRelativeAxis(REL_X) ||
439 getDeviceContext().hasRelativeAxis(REL_Y)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700440 // The device is a cursor device with a touch pad attached.
441 // By default don't use the touch pad to move the pointer.
Michael Wright227c5542020-07-02 18:30:52 +0100442 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 } else {
444 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100445 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446 }
447
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800448 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700449
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700450 std::string deviceTypeString;
451 if (getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800452 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100454 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700455 } else if (deviceTypeString == "touchPad") {
Michael Wright227c5542020-07-02 18:30:52 +0100456 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100458 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100460 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461 } else if (deviceTypeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700462 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463 }
464 }
465
Michael Wright227c5542020-07-02 18:30:52 +0100466 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700467 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800468 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700469
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700470 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700471 std::string orientationString;
472 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700473 orientationString)) {
474 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
475 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
476 } else if (orientationString == "ORIENTATION_90") {
477 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
478 } else if (orientationString == "ORIENTATION_180") {
479 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
480 } else if (orientationString == "ORIENTATION_270") {
481 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
482 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700483 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700484 }
485 }
486
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700487 mParameters.hasAssociatedDisplay = false;
488 mParameters.associatedDisplayIsExternal = false;
489 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100490 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
491 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700492 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100493 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800494 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700495 std::string uniqueDisplayId;
496 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800497 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700498 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
499 }
500 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800501 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700502 mParameters.hasAssociatedDisplay = true;
503 }
504
505 // Initial downs on external touch devices should wake the device.
506 // Normally we don't do this for internal touch screens to prevent them from waking
507 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800508 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700509 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700510}
511
512void TouchInputMapper::dumpParameters(std::string& dump) {
513 dump += INDENT3 "Parameters:\n";
514
Dominik Laskowski75788452021-02-09 18:51:25 -0800515 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700516
Dominik Laskowski75788452021-02-09 18:51:25 -0800517 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700518
519 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
520 "displayId='%s'\n",
521 toString(mParameters.hasAssociatedDisplay),
522 toString(mParameters.associatedDisplayIsExternal),
523 mParameters.uniqueDisplayId.c_str());
524 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800525 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700526}
527
528void TouchInputMapper::configureRawPointerAxes() {
529 mRawPointerAxes.clear();
530}
531
532void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
533 dump += INDENT3 "Raw Touch Axes:\n";
534 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
535 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
536 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
537 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
538 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
539 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
545 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
546 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
547}
548
549bool TouchInputMapper::hasExternalStylus() const {
550 return mExternalStylusConnected;
551}
552
553/**
554 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000555 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800556 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000557 * 3. Get the matching viewport by either unique id in idc file or by the display type
558 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800559 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700560 */
561std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800562 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000563 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800564 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700565 }
566
Christine Franks2a2293c2022-01-18 11:51:16 -0800567 const std::optional<std::string> associatedDisplayUniqueId =
568 getDeviceContext().getAssociatedDisplayUniqueId();
569 if (associatedDisplayUniqueId) {
570 return getDeviceContext().getAssociatedViewport();
571 }
572
Michael Wright227c5542020-07-02 18:30:52 +0100573 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800574 std::optional<DisplayViewport> viewport =
575 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
576 if (viewport) {
577 return viewport;
578 } else {
579 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
580 mConfig.defaultPointerDisplayId);
581 }
582 }
583
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700584 // Check if uniqueDisplayId is specified in idc file.
585 if (!mParameters.uniqueDisplayId.empty()) {
586 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
587 }
588
589 ViewportType viewportTypeToUse;
590 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100591 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700592 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100593 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700594 }
595
596 std::optional<DisplayViewport> viewport =
597 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100598 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700599 ALOGW("Input device %s should be associated with external display, "
600 "fallback to internal one for the external viewport is not found.",
601 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100602 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700603 }
604
605 return viewport;
606 }
607
608 // No associated display, return a non-display viewport.
609 DisplayViewport newViewport;
610 // Raw width and height in the natural orientation.
611 int32_t rawWidth = mRawPointerAxes.getRawWidth();
612 int32_t rawHeight = mRawPointerAxes.getRawHeight();
613 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
614 return std::make_optional(newViewport);
615}
616
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800617int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
618 if (resolution < 0) {
619 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
620 getDeviceName().c_str());
621 return 0;
622 }
623 return resolution;
624}
625
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800626void TouchInputMapper::initializeSizeRanges() {
627 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
628 mSizeScale = 0.0f;
629 return;
630 }
631
632 // Size of diagonal axis.
633 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
634
635 // Size factors.
636 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
637 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
638 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
639 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
640 } else {
641 mSizeScale = 0.0f;
642 }
643
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700644 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
645 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
646 .source = mSource,
647 .min = 0,
648 .max = diagonalSize,
649 .flat = 0,
650 .fuzz = 0,
651 .resolution = 0,
652 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800653
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800654 if (mRawPointerAxes.touchMajor.valid) {
655 mRawPointerAxes.touchMajor.resolution =
656 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700657 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800658 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800659
660 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700661 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800662 if (mRawPointerAxes.touchMinor.valid) {
663 mRawPointerAxes.touchMinor.resolution =
664 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700665 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800666 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800667
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700668 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
669 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
670 .source = mSource,
671 .min = 0,
672 .max = diagonalSize,
673 .flat = 0,
674 .fuzz = 0,
675 .resolution = 0,
676 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800677 if (mRawPointerAxes.toolMajor.valid) {
678 mRawPointerAxes.toolMajor.resolution =
679 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700680 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800681 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800682
683 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700684 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800685 if (mRawPointerAxes.toolMinor.valid) {
686 mRawPointerAxes.toolMinor.resolution =
687 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700688 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800689 }
690
691 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700692 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
693 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
694 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
695 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800696 } else {
697 // Support for other calibrations can be added here.
698 ALOGW("%s calibration is not supported for size ranges at the moment. "
699 "Using raw resolution instead",
700 ftl::enum_string(mCalibration.sizeCalibration).c_str());
701 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800702
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700703 mOrientedRanges.size = InputDeviceInfo::MotionRange{
704 .axis = AMOTION_EVENT_AXIS_SIZE,
705 .source = mSource,
706 .min = 0,
707 .max = 1.0,
708 .flat = 0,
709 .fuzz = 0,
710 .resolution = 0,
711 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800712}
713
714void TouchInputMapper::initializeOrientedRanges() {
715 // Configure X and Y factors.
716 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
717 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
718 mXPrecision = 1.0f / mXScale;
719 mYPrecision = 1.0f / mYScale;
720
721 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
722 mOrientedRanges.x.source = mSource;
723 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
724 mOrientedRanges.y.source = mSource;
725
726 // Scale factor for terms that are not oriented in a particular axis.
727 // If the pixels are square then xScale == yScale otherwise we fake it
728 // by choosing an average.
729 mGeometricScale = avg(mXScale, mYScale);
730
731 initializeSizeRanges();
732
733 // Pressure factors.
734 mPressureScale = 0;
735 float pressureMax = 1.0;
736 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
737 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700738 if (mCalibration.pressureScale) {
739 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800740 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
741 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
742 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
743 }
744 }
745
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700746 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
747 .axis = AMOTION_EVENT_AXIS_PRESSURE,
748 .source = mSource,
749 .min = 0,
750 .max = pressureMax,
751 .flat = 0,
752 .fuzz = 0,
753 .resolution = 0,
754 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800755
756 // Tilt
757 mTiltXCenter = 0;
758 mTiltXScale = 0;
759 mTiltYCenter = 0;
760 mTiltYScale = 0;
761 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
762 if (mHaveTilt) {
763 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
764 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
765 mTiltXScale = M_PI / 180;
766 mTiltYScale = M_PI / 180;
767
768 if (mRawPointerAxes.tiltX.resolution) {
769 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
770 }
771 if (mRawPointerAxes.tiltY.resolution) {
772 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
773 }
774
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700775 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
776 .axis = AMOTION_EVENT_AXIS_TILT,
777 .source = mSource,
778 .min = 0,
779 .max = M_PI_2,
780 .flat = 0,
781 .fuzz = 0,
782 .resolution = 0,
783 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800784 }
785
786 // Orientation
787 mOrientationScale = 0;
788 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700789 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
790 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
791 .source = mSource,
792 .min = -M_PI,
793 .max = M_PI,
794 .flat = 0,
795 .fuzz = 0,
796 .resolution = 0,
797 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800798
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800799 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
800 if (mCalibration.orientationCalibration ==
801 Calibration::OrientationCalibration::INTERPOLATED) {
802 if (mRawPointerAxes.orientation.valid) {
803 if (mRawPointerAxes.orientation.maxValue > 0) {
804 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
805 } else if (mRawPointerAxes.orientation.minValue < 0) {
806 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
807 } else {
808 mOrientationScale = 0;
809 }
810 }
811 }
812
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700813 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
814 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
815 .source = mSource,
816 .min = -M_PI_2,
817 .max = M_PI_2,
818 .flat = 0,
819 .fuzz = 0,
820 .resolution = 0,
821 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800822 }
823
824 // Distance
825 mDistanceScale = 0;
826 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
827 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700828 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800829 }
830
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700831 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800832
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700833 .axis = AMOTION_EVENT_AXIS_DISTANCE,
834 .source = mSource,
835 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
836 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
837 .flat = 0,
838 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
839 .resolution = 0,
840 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800841 }
842
843 // Compute oriented precision, scales and ranges.
844 // Note that the maximum value reported is an inclusive maximum value so it is one
845 // unit less than the total width or height of the display.
846 switch (mInputDeviceOrientation) {
847 case DISPLAY_ORIENTATION_90:
848 case DISPLAY_ORIENTATION_270:
849 mOrientedXPrecision = mYPrecision;
850 mOrientedYPrecision = mXPrecision;
851
852 mOrientedRanges.x.min = 0;
853 mOrientedRanges.x.max = mDisplayHeight - 1;
854 mOrientedRanges.x.flat = 0;
855 mOrientedRanges.x.fuzz = 0;
856 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
857
858 mOrientedRanges.y.min = 0;
859 mOrientedRanges.y.max = mDisplayWidth - 1;
860 mOrientedRanges.y.flat = 0;
861 mOrientedRanges.y.fuzz = 0;
862 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
863 break;
864
865 default:
866 mOrientedXPrecision = mXPrecision;
867 mOrientedYPrecision = mYPrecision;
868
869 mOrientedRanges.x.min = 0;
870 mOrientedRanges.x.max = mDisplayWidth - 1;
871 mOrientedRanges.x.flat = 0;
872 mOrientedRanges.x.fuzz = 0;
873 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
874
875 mOrientedRanges.y.min = 0;
876 mOrientedRanges.y.max = mDisplayHeight - 1;
877 mOrientedRanges.y.flat = 0;
878 mOrientedRanges.y.fuzz = 0;
879 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
880 break;
881 }
882}
883
Prabir Pradhan1728b212021-10-19 16:00:03 -0700884void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100885 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700886
887 resolveExternalStylusPresence();
888
889 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100890 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000891 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700892 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100893 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700894 if (hasStylus()) {
895 mSource |= AINPUT_SOURCE_STYLUS;
896 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800897 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700898 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100899 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700900 if (hasStylus()) {
901 mSource |= AINPUT_SOURCE_STYLUS;
902 }
903 if (hasExternalStylus()) {
904 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
905 }
Michael Wright227c5542020-07-02 18:30:52 +0100906 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700907 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100908 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700909 } else {
910 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100911 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700912 }
913
914 // Ensure we have valid X and Y axes.
915 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
916 ALOGW("Touch device '%s' did not report support for X or Y axis! "
917 "The device will be inoperable.",
918 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100919 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700920 return;
921 }
922
923 // Get associated display dimensions.
924 std::optional<DisplayViewport> newViewport = findViewport();
925 if (!newViewport) {
926 ALOGI("Touch device '%s' could not query the properties of its associated "
927 "display. The device will be inoperable until the display size "
928 "becomes available.",
929 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100930 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700931 return;
932 }
933
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000934 if (!newViewport->isActive) {
935 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
936 getDeviceName().c_str(), getDeviceId());
937 mDeviceMode = DeviceMode::DISABLED;
938 return;
939 }
940
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700941 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700942 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
943 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
HQ Liue6983c72022-04-19 22:14:56 +0000944 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
945 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
946 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
947 const float rawMeanResolution =
948 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700949
Prabir Pradhan1728b212021-10-19 16:00:03 -0700950 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700951 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700952 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700953 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700954 mViewport = *newViewport;
955
Michael Wright227c5542020-07-02 18:30:52 +0100956 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700957 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700958 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
959 int32_t naturalPhysicalLeft, naturalPhysicalTop;
960 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700961
Prabir Pradhan1728b212021-10-19 16:00:03 -0700962 // Apply the inverse of the input device orientation so that the input device is
963 // configured in the same orientation as the viewport. The input device orientation will
964 // be re-applied by mInputDeviceOrientation.
965 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700966 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700967 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700968 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700969 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
970 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800971 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700972 naturalPhysicalTop = mViewport.physicalLeft;
973 naturalDeviceWidth = mViewport.deviceHeight;
974 naturalDeviceHeight = mViewport.deviceWidth;
975 break;
976 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700977 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
978 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
979 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
980 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
981 naturalDeviceWidth = mViewport.deviceWidth;
982 naturalDeviceHeight = mViewport.deviceHeight;
983 break;
984 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700985 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
986 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
987 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800988 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700989 naturalDeviceWidth = mViewport.deviceHeight;
990 naturalDeviceHeight = mViewport.deviceWidth;
991 break;
992 case DISPLAY_ORIENTATION_0:
993 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700994 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
995 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
996 naturalPhysicalLeft = mViewport.physicalLeft;
997 naturalPhysicalTop = mViewport.physicalTop;
998 naturalDeviceWidth = mViewport.deviceWidth;
999 naturalDeviceHeight = mViewport.deviceHeight;
1000 break;
1001 }
1002
1003 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
1004 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
1005 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
1006 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1007 }
1008
1009 mPhysicalWidth = naturalPhysicalWidth;
1010 mPhysicalHeight = naturalPhysicalHeight;
1011 mPhysicalLeft = naturalPhysicalLeft;
1012 mPhysicalTop = naturalPhysicalTop;
1013
Prabir Pradhan1728b212021-10-19 16:00:03 -07001014 const int32_t oldDisplayWidth = mDisplayWidth;
1015 const int32_t oldDisplayHeight = mDisplayHeight;
1016 mDisplayWidth = naturalDeviceWidth;
1017 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001018
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001019 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1020 // anything if the device is already orientation-aware. If the device is not
1021 // orientation-aware, then we need to apply the inverse rotation of the display so that
1022 // when the display rotation is applied later as a part of the per-window transform, we
1023 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001024 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001025 ? DISPLAY_ORIENTATION_0
1026 : getInverseRotation(mViewport.orientation);
1027 // For orientation-aware devices that work in the un-rotated coordinate space, the
1028 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001029 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
1030 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001031
1032 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001033 mInputDeviceOrientation =
1034 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001035 } else {
1036 mPhysicalWidth = rawWidth;
1037 mPhysicalHeight = rawHeight;
1038 mPhysicalLeft = 0;
1039 mPhysicalTop = 0;
1040
Prabir Pradhan1728b212021-10-19 16:00:03 -07001041 mDisplayWidth = rawWidth;
1042 mDisplayHeight = rawHeight;
1043 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001044 }
1045 }
1046
1047 // If moving between pointer modes, need to reset some state.
1048 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1049 if (deviceModeChanged) {
1050 mOrientedRanges.clear();
1051 }
1052
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001053 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1054 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001055 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001056 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001057 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1058 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001059 if (mPointerController == nullptr) {
1060 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001061 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001062 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001063 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1064 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001065 } else {
lilinnandef700b2022-06-17 19:32:01 +08001066 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1067 !mConfig.showTouches) {
1068 mPointerController->clearSpots();
1069 }
Michael Wright17db18e2020-06-26 20:51:44 +01001070 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001071 }
1072
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001073 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001074 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1075 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001076 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1077 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001078
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001079 configureVirtualKeys();
1080
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001081 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001082
1083 // Location
1084 updateAffineTransformation();
1085
Michael Wright227c5542020-07-02 18:30:52 +01001086 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001087 // Compute pointer gesture detection parameters.
1088 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001089 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001090
1091 // Scale movements such that one whole swipe of the touch pad covers a
1092 // given area relative to the diagonal size of the display when no acceleration
1093 // is applied.
1094 // Assume that the touch pad has a square aspect ratio such that movements in
1095 // X and Y of the same number of raw units cover the same physical distance.
1096 mPointerXMovementScale =
1097 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1098 mPointerYMovementScale = mPointerXMovementScale;
1099
1100 // Scale zooms to cover a smaller range of the display than movements do.
1101 // This value determines the area around the pointer that is affected by freeform
1102 // pointer gestures.
1103 mPointerXZoomScale =
1104 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1105 mPointerYZoomScale = mPointerXZoomScale;
1106
HQ Liue6983c72022-04-19 22:14:56 +00001107 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1108 // axis is non positive value.
1109 const float minFreeformGestureWidth =
1110 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1111
1112 mPointerGestureMaxSwipeWidth =
1113 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1114 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001115
1116 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001117 const nsecs_t readTime = when; // synthetic event
1118 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001119 }
1120
1121 // Inform the dispatcher about the changes.
1122 *outResetNeeded = true;
1123 bumpGeneration();
1124 }
1125}
1126
Prabir Pradhan1728b212021-10-19 16:00:03 -07001127void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001128 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001129 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1130 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001131 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1132 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1133 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1134 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001135 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001136}
1137
1138void TouchInputMapper::configureVirtualKeys() {
1139 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001140 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001141
1142 mVirtualKeys.clear();
1143
1144 if (virtualKeyDefinitions.size() == 0) {
1145 return;
1146 }
1147
1148 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1149 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1150 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1151 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1152
1153 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1154 VirtualKey virtualKey;
1155
1156 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1157 int32_t keyCode;
1158 int32_t dummyKeyMetaState;
1159 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001160 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1161 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001162 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1163 continue; // drop the key
1164 }
1165
1166 virtualKey.keyCode = keyCode;
1167 virtualKey.flags = flags;
1168
1169 // convert the key definition's display coordinates into touch coordinates for a hit box
1170 int32_t halfWidth = virtualKeyDefinition.width / 2;
1171 int32_t halfHeight = virtualKeyDefinition.height / 2;
1172
1173 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001174 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001175 touchScreenLeft;
1176 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001177 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001178 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001179 virtualKey.hitTop =
1180 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001181 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001182 virtualKey.hitBottom =
1183 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001184 touchScreenTop;
1185 mVirtualKeys.push_back(virtualKey);
1186 }
1187}
1188
1189void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1190 if (!mVirtualKeys.empty()) {
1191 dump += INDENT3 "Virtual Keys:\n";
1192
1193 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1194 const VirtualKey& virtualKey = mVirtualKeys[i];
1195 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1196 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1197 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1198 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1199 }
1200 }
1201}
1202
1203void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001204 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001205 Calibration& out = mCalibration;
1206
1207 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001208 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001209 std::string sizeCalibrationString;
1210 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001211 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001212 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001213 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001214 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001215 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001216 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001218 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001219 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001220 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001221 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001222 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001223 }
1224 }
1225
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001226 float sizeScale;
1227
1228 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1229 out.sizeScale = sizeScale;
1230 }
1231 float sizeBias;
1232 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1233 out.sizeBias = sizeBias;
1234 }
1235 bool sizeIsSummed;
1236 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1237 out.sizeIsSummed = sizeIsSummed;
1238 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001239
1240 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001241 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001242 std::string pressureCalibrationString;
1243 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001244 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001245 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001246 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001247 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001248 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001249 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001250 } else if (pressureCalibrationString != "default") {
1251 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001252 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001253 }
1254 }
1255
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001256 float pressureScale;
1257 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1258 out.pressureScale = pressureScale;
1259 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260
1261 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001262 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001263 std::string orientationCalibrationString;
1264 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001265 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001266 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001267 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001268 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001269 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001270 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001271 } else if (orientationCalibrationString != "default") {
1272 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001273 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001274 }
1275 }
1276
1277 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001278 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001279 std::string distanceCalibrationString;
1280 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001281 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001282 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001283 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001284 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001285 } else if (distanceCalibrationString != "default") {
1286 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001287 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001288 }
1289 }
1290
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001291 float distanceScale;
1292 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1293 out.distanceScale = distanceScale;
1294 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001295
Michael Wright227c5542020-07-02 18:30:52 +01001296 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001297 std::string coverageCalibrationString;
1298 if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001299 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001300 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001301 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001302 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001303 } else if (coverageCalibrationString != "default") {
1304 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001305 coverageCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001306 }
1307 }
1308}
1309
1310void TouchInputMapper::resolveCalibration() {
1311 // Size
1312 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001313 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1314 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001315 }
1316 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001317 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001318 }
1319
1320 // Pressure
1321 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001322 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1323 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001324 }
1325 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001326 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001327 }
1328
1329 // Orientation
1330 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001331 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1332 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001333 }
1334 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001335 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001336 }
1337
1338 // Distance
1339 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001340 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1341 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001342 }
1343 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001344 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001345 }
1346
1347 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001348 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1349 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001350 }
1351}
1352
1353void TouchInputMapper::dumpCalibration(std::string& dump) {
1354 dump += INDENT3 "Calibration:\n";
1355
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001356 dump += INDENT4 "touch.size.calibration: ";
1357 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001358
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001359 if (mCalibration.sizeScale) {
1360 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001361 }
1362
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001363 if (mCalibration.sizeBias) {
1364 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001365 }
1366
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001367 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001368 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001369 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001370 }
1371
1372 // Pressure
1373 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001374 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001375 dump += INDENT4 "touch.pressure.calibration: none\n";
1376 break;
Michael Wright227c5542020-07-02 18:30:52 +01001377 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001378 dump += INDENT4 "touch.pressure.calibration: physical\n";
1379 break;
Michael Wright227c5542020-07-02 18:30:52 +01001380 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001381 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1382 break;
1383 default:
1384 ALOG_ASSERT(false);
1385 }
1386
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001387 if (mCalibration.pressureScale) {
1388 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001389 }
1390
1391 // Orientation
1392 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001393 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001394 dump += INDENT4 "touch.orientation.calibration: none\n";
1395 break;
Michael Wright227c5542020-07-02 18:30:52 +01001396 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001397 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1398 break;
Michael Wright227c5542020-07-02 18:30:52 +01001399 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001400 dump += INDENT4 "touch.orientation.calibration: vector\n";
1401 break;
1402 default:
1403 ALOG_ASSERT(false);
1404 }
1405
1406 // Distance
1407 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001408 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001409 dump += INDENT4 "touch.distance.calibration: none\n";
1410 break;
Michael Wright227c5542020-07-02 18:30:52 +01001411 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001412 dump += INDENT4 "touch.distance.calibration: scaled\n";
1413 break;
1414 default:
1415 ALOG_ASSERT(false);
1416 }
1417
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001418 if (mCalibration.distanceScale) {
1419 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001420 }
1421
1422 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001423 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001424 dump += INDENT4 "touch.coverage.calibration: none\n";
1425 break;
Michael Wright227c5542020-07-02 18:30:52 +01001426 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001427 dump += INDENT4 "touch.coverage.calibration: box\n";
1428 break;
1429 default:
1430 ALOG_ASSERT(false);
1431 }
1432}
1433
1434void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1435 dump += INDENT3 "Affine Transformation:\n";
1436
1437 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1438 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1439 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1440 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1441 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1442 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1443}
1444
1445void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001446 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001447 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001448}
1449
1450void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001451 mCursorButtonAccumulator.reset(getDeviceContext());
1452 mCursorScrollAccumulator.reset(getDeviceContext());
1453 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001454
1455 mPointerVelocityControl.reset();
1456 mWheelXVelocityControl.reset();
1457 mWheelYVelocityControl.reset();
1458
1459 mRawStatesPending.clear();
1460 mCurrentRawState.clear();
1461 mCurrentCookedState.clear();
1462 mLastRawState.clear();
1463 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001464 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001465 mSentHoverEnter = false;
1466 mHavePointerIds = false;
1467 mCurrentMotionAborted = false;
1468 mDownTime = 0;
1469
1470 mCurrentVirtualKey.down = false;
1471
1472 mPointerGesture.reset();
1473 mPointerSimple.reset();
1474 resetExternalStylus();
1475
1476 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001477 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001478 mPointerController->clearSpots();
1479 }
1480
1481 InputMapper::reset(when);
1482}
1483
1484void TouchInputMapper::resetExternalStylus() {
1485 mExternalStylusState.clear();
1486 mExternalStylusId = -1;
1487 mExternalStylusFusionTimeout = LLONG_MAX;
1488 mExternalStylusDataPending = false;
1489}
1490
1491void TouchInputMapper::clearStylusDataPendingFlags() {
1492 mExternalStylusDataPending = false;
1493 mExternalStylusFusionTimeout = LLONG_MAX;
1494}
1495
1496void TouchInputMapper::process(const RawEvent* rawEvent) {
1497 mCursorButtonAccumulator.process(rawEvent);
1498 mCursorScrollAccumulator.process(rawEvent);
1499 mTouchButtonAccumulator.process(rawEvent);
1500
1501 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001502 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001503 }
1504}
1505
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001506void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001507 // Push a new state.
1508 mRawStatesPending.emplace_back();
1509
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001510 RawState& next = mRawStatesPending.back();
1511 next.clear();
1512 next.when = when;
1513 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001514
1515 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001516 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001517 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1518
1519 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001520 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1521 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001522 mCursorScrollAccumulator.finishSync();
1523
1524 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001525 syncTouch(when, &next);
1526
1527 // The last RawState is the actually second to last, since we just added a new state
1528 const RawState& last =
1529 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001530
1531 // Assign pointer ids.
1532 if (!mHavePointerIds) {
1533 assignPointerIds(last, next);
1534 }
1535
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001536 if (DEBUG_RAW_EVENTS) {
1537 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1538 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1539 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1540 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1541 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1542 next.rawPointerData.canceledIdBits.value);
1543 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001544
Arthur Hung9ad18942021-06-19 02:04:46 +00001545 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1546 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1547 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1548 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1549 next.rawPointerData.hoveringIdBits.value);
1550 }
1551
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001552 processRawTouches(false /*timeout*/);
1553}
1554
1555void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001556 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001557 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001558 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001559 mCurrentCookedState.clear();
1560 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001561 return;
1562 }
1563
1564 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1565 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1566 // touching the current state will only observe the events that have been dispatched to the
1567 // rest of the pipeline.
1568 const size_t N = mRawStatesPending.size();
1569 size_t count;
1570 for (count = 0; count < N; count++) {
1571 const RawState& next = mRawStatesPending[count];
1572
1573 // A failure to assign the stylus id means that we're waiting on stylus data
1574 // and so should defer the rest of the pipeline.
1575 if (assignExternalStylusId(next, timeout)) {
1576 break;
1577 }
1578
1579 // All ready to go.
1580 clearStylusDataPendingFlags();
1581 mCurrentRawState.copyFrom(next);
1582 if (mCurrentRawState.when < mLastRawState.when) {
1583 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001584 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001585 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001586 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001587 }
1588 if (count != 0) {
1589 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1590 }
1591
1592 if (mExternalStylusDataPending) {
1593 if (timeout) {
1594 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1595 clearStylusDataPendingFlags();
1596 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001597 if (DEBUG_STYLUS_FUSION) {
1598 ALOGD("Timeout expired, synthesizing event with new stylus data");
1599 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001600 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1601 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001602 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1603 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1604 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1605 }
1606 }
1607}
1608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001609void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001610 // Always start with a clean state.
1611 mCurrentCookedState.clear();
1612
1613 // Apply stylus buttons to current raw state.
1614 applyExternalStylusButtonState(when);
1615
1616 // Handle policy on initial down or hover events.
1617 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1618 mCurrentRawState.rawPointerData.pointerCount != 0;
1619
1620 uint32_t policyFlags = 0;
1621 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1622 if (initialDown || buttonsPressed) {
1623 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001624 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001625 getContext()->fadePointer();
1626 }
1627
1628 if (mParameters.wake) {
1629 policyFlags |= POLICY_FLAG_WAKE;
1630 }
1631 }
1632
1633 // Consume raw off-screen touches before cooking pointer data.
1634 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001635 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001636 mCurrentRawState.rawPointerData.clear();
1637 }
1638
1639 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1640 // with cooked pointer data that has the same ids and indices as the raw data.
1641 // The following code can use either the raw or cooked data, as needed.
1642 cookPointerData();
1643
1644 // Apply stylus pressure to current cooked state.
1645 applyExternalStylusTouchState(when);
1646
1647 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001648 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1649 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001650 mCurrentCookedState.buttonState);
1651
1652 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001653 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001654 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1655 uint32_t id = idBits.clearFirstMarkedBit();
1656 const RawPointerData::Pointer& pointer =
1657 mCurrentRawState.rawPointerData.pointerForId(id);
1658 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1659 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1660 mCurrentCookedState.stylusIdBits.markBit(id);
1661 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1662 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1663 mCurrentCookedState.fingerIdBits.markBit(id);
1664 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1665 mCurrentCookedState.mouseIdBits.markBit(id);
1666 }
1667 }
1668 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1669 uint32_t id = idBits.clearFirstMarkedBit();
1670 const RawPointerData::Pointer& pointer =
1671 mCurrentRawState.rawPointerData.pointerForId(id);
1672 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1673 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1674 mCurrentCookedState.stylusIdBits.markBit(id);
1675 }
1676 }
1677
1678 // Stylus takes precedence over all tools, then mouse, then finger.
1679 PointerUsage pointerUsage = mPointerUsage;
1680 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1681 mCurrentCookedState.mouseIdBits.clear();
1682 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001683 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001684 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1685 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001686 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001687 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1688 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001689 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001690 }
1691
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001692 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001693 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001694 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001695 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001696 dispatchButtonRelease(when, readTime, policyFlags);
1697 dispatchHoverExit(when, readTime, policyFlags);
1698 dispatchTouches(when, readTime, policyFlags);
1699 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1700 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001701 }
1702
1703 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1704 mCurrentMotionAborted = false;
1705 }
1706 }
1707
1708 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001709 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001710 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1711 mCurrentCookedState.buttonState);
1712
1713 // Clear some transient state.
1714 mCurrentRawState.rawVScroll = 0;
1715 mCurrentRawState.rawHScroll = 0;
1716
1717 // Copy current touch to last touch in preparation for the next cycle.
1718 mLastRawState.copyFrom(mCurrentRawState);
1719 mLastCookedState.copyFrom(mCurrentCookedState);
1720}
1721
Garfield Tanc734e4f2021-01-15 20:01:39 -08001722void TouchInputMapper::updateTouchSpots() {
1723 if (!mConfig.showTouches || mPointerController == nullptr) {
1724 return;
1725 }
1726
1727 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1728 // clear touch spots.
1729 if (mDeviceMode != DeviceMode::DIRECT &&
1730 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1731 return;
1732 }
1733
1734 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1735 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1736
1737 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001738 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1739 mCurrentCookedState.cookedPointerData.idToIndex,
1740 mCurrentCookedState.cookedPointerData.touchingIdBits,
1741 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001742}
1743
1744bool TouchInputMapper::isTouchScreen() {
1745 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1746 mParameters.hasAssociatedDisplay;
1747}
1748
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001749void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001750 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001751 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1752 }
1753}
1754
1755void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1756 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1757 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1758
1759 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1760 float pressure = mExternalStylusState.pressure;
1761 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1762 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1763 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1764 }
1765 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1766 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1767
1768 PointerProperties& properties =
1769 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1770 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1771 properties.toolType = mExternalStylusState.toolType;
1772 }
1773 }
1774}
1775
1776bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001777 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001778 return false;
1779 }
1780
1781 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1782 state.rawPointerData.pointerCount != 0;
1783 if (initialDown) {
1784 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001785 if (DEBUG_STYLUS_FUSION) {
1786 ALOGD("Have both stylus and touch data, beginning fusion");
1787 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001788 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1789 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001790 if (DEBUG_STYLUS_FUSION) {
1791 ALOGD("Timeout expired, assuming touch is not a stylus.");
1792 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001793 resetExternalStylus();
1794 } else {
1795 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1796 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1797 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001798 if (DEBUG_STYLUS_FUSION) {
1799 ALOGD("No stylus data but stylus is connected, requesting timeout "
1800 "(%" PRId64 "ms)",
1801 mExternalStylusFusionTimeout);
1802 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001803 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1804 return true;
1805 }
1806 }
1807
1808 // Check if the stylus pointer has gone up.
1809 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001810 if (DEBUG_STYLUS_FUSION) {
1811 ALOGD("Stylus pointer is going up");
1812 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001813 mExternalStylusId = -1;
1814 }
1815
1816 return false;
1817}
1818
1819void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001820 if (mDeviceMode == DeviceMode::POINTER) {
1821 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001822 // Since this is a synthetic event, we can consider its latency to be zero
1823 const nsecs_t readTime = when;
1824 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001825 }
Michael Wright227c5542020-07-02 18:30:52 +01001826 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001827 if (mExternalStylusFusionTimeout < when) {
1828 processRawTouches(true /*timeout*/);
1829 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1830 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1831 }
1832 }
1833}
1834
1835void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1836 mExternalStylusState.copyFrom(state);
1837 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1838 // We're either in the middle of a fused stream of data or we're waiting on data before
1839 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1840 // data.
1841 mExternalStylusDataPending = true;
1842 processRawTouches(false /*timeout*/);
1843 }
1844}
1845
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001846bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001847 // Check for release of a virtual key.
1848 if (mCurrentVirtualKey.down) {
1849 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1850 // Pointer went up while virtual key was down.
1851 mCurrentVirtualKey.down = false;
1852 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001853 if (DEBUG_VIRTUAL_KEYS) {
1854 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1855 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1856 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001857 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001858 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1859 }
1860 return true;
1861 }
1862
1863 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1864 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1865 const RawPointerData::Pointer& pointer =
1866 mCurrentRawState.rawPointerData.pointerForId(id);
1867 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1868 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1869 // Pointer is still within the space of the virtual key.
1870 return true;
1871 }
1872 }
1873
1874 // Pointer left virtual key area or another pointer also went down.
1875 // Send key cancellation but do not consume the touch yet.
1876 // This is useful when the user swipes through from the virtual key area
1877 // into the main display surface.
1878 mCurrentVirtualKey.down = false;
1879 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001880 if (DEBUG_VIRTUAL_KEYS) {
1881 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1882 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1883 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001884 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001885 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1886 AKEY_EVENT_FLAG_CANCELED);
1887 }
1888 }
1889
1890 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1891 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1892 // Pointer just went down. Check for virtual key press or off-screen touches.
1893 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1894 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001895 // Skip checking whether the pointer is inside the physical frame if the device is in
1896 // unscaled mode.
1897 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1898 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001899 // If exactly one pointer went down, check for virtual key hit.
1900 // Otherwise we will drop the entire stroke.
1901 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1902 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1903 if (virtualKey) {
1904 mCurrentVirtualKey.down = true;
1905 mCurrentVirtualKey.downTime = when;
1906 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1907 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1908 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001909 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1910 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001911
1912 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001913 if (DEBUG_VIRTUAL_KEYS) {
1914 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1915 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1916 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001917 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001918 AKEY_EVENT_FLAG_FROM_SYSTEM |
1919 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1920 }
1921 }
1922 }
1923 return true;
1924 }
1925 }
1926
1927 // Disable all virtual key touches that happen within a short time interval of the
1928 // most recent touch within the screen area. The idea is to filter out stray
1929 // virtual key presses when interacting with the touch screen.
1930 //
1931 // Problems we're trying to solve:
1932 //
1933 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1934 // virtual key area that is implemented by a separate touch panel and accidentally
1935 // triggers a virtual key.
1936 //
1937 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1938 // area and accidentally triggers a virtual key. This often happens when virtual keys
1939 // are layed out below the screen near to where the on screen keyboard's space bar
1940 // is displayed.
1941 if (mConfig.virtualKeyQuietTime > 0 &&
1942 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001943 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001944 }
1945 return false;
1946}
1947
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001948void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001949 int32_t keyEventAction, int32_t keyEventFlags) {
1950 int32_t keyCode = mCurrentVirtualKey.keyCode;
1951 int32_t scanCode = mCurrentVirtualKey.scanCode;
1952 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001953 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001954 policyFlags |= POLICY_FLAG_VIRTUAL;
1955
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001956 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1957 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1958 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001959 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001960}
1961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001962void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001963 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1964 if (!currentIdBits.isEmpty()) {
1965 int32_t metaState = getContext()->getGlobalMetaState();
1966 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001967 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1968 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001969 mCurrentCookedState.cookedPointerData.pointerProperties,
1970 mCurrentCookedState.cookedPointerData.pointerCoords,
1971 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1972 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1973 mCurrentMotionAborted = true;
1974 }
1975}
1976
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001977void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001978 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1979 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1980 int32_t metaState = getContext()->getGlobalMetaState();
1981 int32_t buttonState = mCurrentCookedState.buttonState;
1982
1983 if (currentIdBits == lastIdBits) {
1984 if (!currentIdBits.isEmpty()) {
1985 // No pointer id changes so this is a move event.
1986 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001987 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1988 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001989 mCurrentCookedState.cookedPointerData.pointerProperties,
1990 mCurrentCookedState.cookedPointerData.pointerCoords,
1991 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1992 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1993 }
1994 } else {
1995 // There may be pointers going up and pointers going down and pointers moving
1996 // all at the same time.
1997 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1998 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1999 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2000 BitSet32 dispatchedIdBits(lastIdBits.value);
2001
2002 // Update last coordinates of pointers that have moved so that we observe the new
2003 // pointer positions at the same time as other pointers that have just gone up.
2004 bool moveNeeded =
2005 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
2006 mCurrentCookedState.cookedPointerData.pointerCoords,
2007 mCurrentCookedState.cookedPointerData.idToIndex,
2008 mLastCookedState.cookedPointerData.pointerProperties,
2009 mLastCookedState.cookedPointerData.pointerCoords,
2010 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
2011 if (buttonState != mLastCookedState.buttonState) {
2012 moveNeeded = true;
2013 }
2014
2015 // Dispatch pointer up events.
2016 while (!upIdBits.isEmpty()) {
2017 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002018 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002019 if (isCanceled) {
2020 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2021 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002022 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08002023 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002024 mLastCookedState.cookedPointerData.pointerProperties,
2025 mLastCookedState.cookedPointerData.pointerCoords,
2026 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
2027 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2028 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002029 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002030 }
2031
2032 // Dispatch move events if any of the remaining pointers moved from their old locations.
2033 // Although applications receive new locations as part of individual pointer up
2034 // events, they do not generally handle them except when presented in a move event.
2035 if (moveNeeded && !moveIdBits.isEmpty()) {
2036 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002037 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2038 metaState, buttonState, 0,
2039 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002040 mCurrentCookedState.cookedPointerData.pointerCoords,
2041 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2042 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2043 }
2044
2045 // Dispatch pointer down events using the new pointer locations.
2046 while (!downIdBits.isEmpty()) {
2047 uint32_t downId = downIdBits.clearFirstMarkedBit();
2048 dispatchedIdBits.markBit(downId);
2049
2050 if (dispatchedIdBits.count() == 1) {
2051 // First pointer is going down. Set down time.
2052 mDownTime = when;
2053 }
2054
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002055 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2056 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002057 mCurrentCookedState.cookedPointerData.pointerProperties,
2058 mCurrentCookedState.cookedPointerData.pointerCoords,
2059 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2060 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2061 }
2062 }
2063}
2064
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002065void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002066 if (mSentHoverEnter &&
2067 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2068 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2069 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002070 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2071 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002072 mLastCookedState.cookedPointerData.pointerProperties,
2073 mLastCookedState.cookedPointerData.pointerCoords,
2074 mLastCookedState.cookedPointerData.idToIndex,
2075 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2076 mOrientedYPrecision, mDownTime);
2077 mSentHoverEnter = false;
2078 }
2079}
2080
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002081void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2082 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002083 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2084 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2085 int32_t metaState = getContext()->getGlobalMetaState();
2086 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002087 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2088 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002089 mCurrentCookedState.cookedPointerData.pointerProperties,
2090 mCurrentCookedState.cookedPointerData.pointerCoords,
2091 mCurrentCookedState.cookedPointerData.idToIndex,
2092 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2093 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2094 mSentHoverEnter = true;
2095 }
2096
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002097 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2098 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002099 mCurrentCookedState.cookedPointerData.pointerProperties,
2100 mCurrentCookedState.cookedPointerData.pointerCoords,
2101 mCurrentCookedState.cookedPointerData.idToIndex,
2102 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2103 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2104 }
2105}
2106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002107void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002108 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2109 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2110 const int32_t metaState = getContext()->getGlobalMetaState();
2111 int32_t buttonState = mLastCookedState.buttonState;
2112 while (!releasedButtons.isEmpty()) {
2113 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2114 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002115 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002116 actionButton, 0, metaState, buttonState, 0,
2117 mCurrentCookedState.cookedPointerData.pointerProperties,
2118 mCurrentCookedState.cookedPointerData.pointerCoords,
2119 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2120 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2121 }
2122}
2123
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002124void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002125 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2126 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2127 const int32_t metaState = getContext()->getGlobalMetaState();
2128 int32_t buttonState = mLastCookedState.buttonState;
2129 while (!pressedButtons.isEmpty()) {
2130 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2131 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002132 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2133 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002134 mCurrentCookedState.cookedPointerData.pointerProperties,
2135 mCurrentCookedState.cookedPointerData.pointerCoords,
2136 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2137 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2138 }
2139}
2140
2141const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2142 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2143 return cookedPointerData.touchingIdBits;
2144 }
2145 return cookedPointerData.hoveringIdBits;
2146}
2147
2148void TouchInputMapper::cookPointerData() {
2149 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2150
2151 mCurrentCookedState.cookedPointerData.clear();
2152 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2153 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2154 mCurrentRawState.rawPointerData.hoveringIdBits;
2155 mCurrentCookedState.cookedPointerData.touchingIdBits =
2156 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002157 mCurrentCookedState.cookedPointerData.canceledIdBits =
2158 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002159
2160 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2161 mCurrentCookedState.buttonState = 0;
2162 } else {
2163 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2164 }
2165
2166 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002167 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002168 for (uint32_t i = 0; i < currentPointerCount; i++) {
2169 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2170
2171 // Size
2172 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2173 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002174 case Calibration::SizeCalibration::GEOMETRIC:
2175 case Calibration::SizeCalibration::DIAMETER:
2176 case Calibration::SizeCalibration::BOX:
2177 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002178 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2179 touchMajor = in.touchMajor;
2180 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2181 toolMajor = in.toolMajor;
2182 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2183 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2184 : in.touchMajor;
2185 } else if (mRawPointerAxes.touchMajor.valid) {
2186 toolMajor = touchMajor = in.touchMajor;
2187 toolMinor = touchMinor =
2188 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2189 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2190 : in.touchMajor;
2191 } else if (mRawPointerAxes.toolMajor.valid) {
2192 touchMajor = toolMajor = in.toolMajor;
2193 touchMinor = toolMinor =
2194 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2195 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2196 : in.toolMajor;
2197 } else {
2198 ALOG_ASSERT(false,
2199 "No touch or tool axes. "
2200 "Size calibration should have been resolved to NONE.");
2201 touchMajor = 0;
2202 touchMinor = 0;
2203 toolMajor = 0;
2204 toolMinor = 0;
2205 size = 0;
2206 }
2207
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002208 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002209 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2210 if (touchingCount > 1) {
2211 touchMajor /= touchingCount;
2212 touchMinor /= touchingCount;
2213 toolMajor /= touchingCount;
2214 toolMinor /= touchingCount;
2215 size /= touchingCount;
2216 }
2217 }
2218
Michael Wright227c5542020-07-02 18:30:52 +01002219 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002220 touchMajor *= mGeometricScale;
2221 touchMinor *= mGeometricScale;
2222 toolMajor *= mGeometricScale;
2223 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002224 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002225 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2226 touchMinor = touchMajor;
2227 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2228 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002229 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002230 touchMinor = touchMajor;
2231 toolMinor = toolMajor;
2232 }
2233
2234 mCalibration.applySizeScaleAndBias(&touchMajor);
2235 mCalibration.applySizeScaleAndBias(&touchMinor);
2236 mCalibration.applySizeScaleAndBias(&toolMajor);
2237 mCalibration.applySizeScaleAndBias(&toolMinor);
2238 size *= mSizeScale;
2239 break;
2240 default:
2241 touchMajor = 0;
2242 touchMinor = 0;
2243 toolMajor = 0;
2244 toolMinor = 0;
2245 size = 0;
2246 break;
2247 }
2248
2249 // Pressure
2250 float pressure;
2251 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002252 case Calibration::PressureCalibration::PHYSICAL:
2253 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002254 pressure = in.pressure * mPressureScale;
2255 break;
2256 default:
2257 pressure = in.isHovering ? 0 : 1;
2258 break;
2259 }
2260
2261 // Tilt and Orientation
2262 float tilt;
2263 float orientation;
2264 if (mHaveTilt) {
2265 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2266 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2267 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2268 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2269 } else {
2270 tilt = 0;
2271
2272 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002273 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002274 orientation = in.orientation * mOrientationScale;
2275 break;
Michael Wright227c5542020-07-02 18:30:52 +01002276 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002277 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2278 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2279 if (c1 != 0 || c2 != 0) {
2280 orientation = atan2f(c1, c2) * 0.5f;
2281 float confidence = hypotf(c1, c2);
2282 float scale = 1.0f + confidence / 16.0f;
2283 touchMajor *= scale;
2284 touchMinor /= scale;
2285 toolMajor *= scale;
2286 toolMinor /= scale;
2287 } else {
2288 orientation = 0;
2289 }
2290 break;
2291 }
2292 default:
2293 orientation = 0;
2294 }
2295 }
2296
2297 // Distance
2298 float distance;
2299 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002300 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002301 distance = in.distance * mDistanceScale;
2302 break;
2303 default:
2304 distance = 0;
2305 }
2306
2307 // Coverage
2308 int32_t rawLeft, rawTop, rawRight, rawBottom;
2309 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002310 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002311 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2312 rawRight = in.toolMinor & 0x0000ffff;
2313 rawBottom = in.toolMajor & 0x0000ffff;
2314 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2315 break;
2316 default:
2317 rawLeft = rawTop = rawRight = rawBottom = 0;
2318 break;
2319 }
2320
2321 // Adjust X,Y coords for device calibration
2322 // TODO: Adjust coverage coords?
2323 float xTransformed = in.x, yTransformed = in.y;
2324 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002325 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002326
Prabir Pradhan1728b212021-10-19 16:00:03 -07002327 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002328 float left, top, right, bottom;
2329
Prabir Pradhan1728b212021-10-19 16:00:03 -07002330 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002331 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002332 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2333 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2334 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2335 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002336 orientation -= M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002337 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002338 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002339 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002340 }
2341 break;
2342 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002343 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2344 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002345 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2346 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002347 orientation -= M_PI;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002348 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002349 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002350 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002351 }
2352 break;
2353 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002354 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2355 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002356 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2357 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002358 orientation += M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002359 if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002360 orientation -=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002361 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002362 }
2363 break;
2364 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002365 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2366 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2367 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2368 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002369 break;
2370 }
2371
2372 // Write output coords.
2373 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2374 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002375 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2376 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002377 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2378 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2379 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2380 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2381 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2382 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2383 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002384 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002385 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2386 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2387 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2388 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2389 } else {
2390 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2391 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2392 }
2393
Chris Ye364fdb52020-08-05 15:07:56 -07002394 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002395 uint32_t id = in.id;
2396 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2397 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2398 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2399 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2400 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2401 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2402 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2403 }
2404
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002405 // Write output properties.
2406 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002407 properties.clear();
2408 properties.id = id;
2409 properties.toolType = in.toolType;
2410
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002411 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002412 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002413 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002414 }
2415}
2416
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002417void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002418 PointerUsage pointerUsage) {
2419 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002420 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002421 mPointerUsage = pointerUsage;
2422 }
2423
2424 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002425 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002426 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002427 break;
Michael Wright227c5542020-07-02 18:30:52 +01002428 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002429 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002430 break;
Michael Wright227c5542020-07-02 18:30:52 +01002431 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002432 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002433 break;
Michael Wright227c5542020-07-02 18:30:52 +01002434 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002435 break;
2436 }
2437}
2438
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002439void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002440 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002441 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002442 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002443 break;
Michael Wright227c5542020-07-02 18:30:52 +01002444 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002445 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002446 break;
Michael Wright227c5542020-07-02 18:30:52 +01002447 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002448 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002449 break;
Michael Wright227c5542020-07-02 18:30:52 +01002450 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002451 break;
2452 }
2453
Michael Wright227c5542020-07-02 18:30:52 +01002454 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002455}
2456
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002457void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2458 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002459 // Update current gesture coordinates.
2460 bool cancelPreviousGesture, finishPreviousGesture;
2461 bool sendEvents =
2462 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2463 if (!sendEvents) {
2464 return;
2465 }
2466 if (finishPreviousGesture) {
2467 cancelPreviousGesture = false;
2468 }
2469
2470 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002471 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002472 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002473 if (finishPreviousGesture || cancelPreviousGesture) {
2474 mPointerController->clearSpots();
2475 }
2476
Michael Wright227c5542020-07-02 18:30:52 +01002477 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002478 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2479 mPointerGesture.currentGestureIdToIndex,
2480 mPointerGesture.currentGestureIdBits,
2481 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002482 }
2483 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002484 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002485 }
2486
2487 // Show or hide the pointer if needed.
2488 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002489 case PointerGesture::Mode::NEUTRAL:
2490 case PointerGesture::Mode::QUIET:
2491 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2492 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002493 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002494 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002495 }
2496 break;
Michael Wright227c5542020-07-02 18:30:52 +01002497 case PointerGesture::Mode::TAP:
2498 case PointerGesture::Mode::TAP_DRAG:
2499 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2500 case PointerGesture::Mode::HOVER:
2501 case PointerGesture::Mode::PRESS:
2502 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002503 // Unfade the pointer when the current gesture manipulates the
2504 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002505 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002506 break;
Michael Wright227c5542020-07-02 18:30:52 +01002507 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002508 // Fade the pointer when the current gesture manipulates a different
2509 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002510 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002511 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002512 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002513 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002514 }
2515 break;
2516 }
2517
2518 // Send events!
2519 int32_t metaState = getContext()->getGlobalMetaState();
2520 int32_t buttonState = mCurrentCookedState.buttonState;
2521
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002522 uint32_t flags = 0;
2523
2524 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2525 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2526 }
2527
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002528 // Update last coordinates of pointers that have moved so that we observe the new
2529 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002530 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2531 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2532 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2533 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2534 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2535 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002536 bool moveNeeded = false;
2537 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2538 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2539 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2540 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2541 mPointerGesture.lastGestureIdBits.value);
2542 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2543 mPointerGesture.currentGestureCoords,
2544 mPointerGesture.currentGestureIdToIndex,
2545 mPointerGesture.lastGestureProperties,
2546 mPointerGesture.lastGestureCoords,
2547 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2548 if (buttonState != mLastCookedState.buttonState) {
2549 moveNeeded = true;
2550 }
2551 }
2552
2553 // Send motion events for all pointers that went up or were canceled.
2554 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2555 if (!dispatchedGestureIdBits.isEmpty()) {
2556 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002557 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2558 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002559 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2560 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2561 mPointerGesture.downTime);
2562
2563 dispatchedGestureIdBits.clear();
2564 } else {
2565 BitSet32 upGestureIdBits;
2566 if (finishPreviousGesture) {
2567 upGestureIdBits = dispatchedGestureIdBits;
2568 } else {
2569 upGestureIdBits.value =
2570 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2571 }
2572 while (!upGestureIdBits.isEmpty()) {
2573 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2574
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002575 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002576 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002577 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002578 mPointerGesture.lastGestureCoords,
2579 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2580 0, mPointerGesture.downTime);
2581
2582 dispatchedGestureIdBits.clearBit(id);
2583 }
2584 }
2585 }
2586
2587 // Send motion events for all pointers that moved.
2588 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002589 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002590 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002591 mPointerGesture.currentGestureProperties,
2592 mPointerGesture.currentGestureCoords,
2593 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2594 mPointerGesture.downTime);
2595 }
2596
2597 // Send motion events for all pointers that went down.
2598 if (down) {
2599 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2600 ~dispatchedGestureIdBits.value);
2601 while (!downGestureIdBits.isEmpty()) {
2602 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2603 dispatchedGestureIdBits.markBit(id);
2604
2605 if (dispatchedGestureIdBits.count() == 1) {
2606 mPointerGesture.downTime = when;
2607 }
2608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002609 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002610 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002611 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002612 mPointerGesture.currentGestureCoords,
2613 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2614 0, mPointerGesture.downTime);
2615 }
2616 }
2617
2618 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002619 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002620 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2621 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002622 mPointerGesture.currentGestureProperties,
2623 mPointerGesture.currentGestureCoords,
2624 mPointerGesture.currentGestureIdToIndex,
2625 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2626 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2627 // Synthesize a hover move event after all pointers go up to indicate that
2628 // the pointer is hovering again even if the user is not currently touching
2629 // the touch pad. This ensures that a view will receive a fresh hover enter
2630 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002631 float x, y;
2632 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002633
2634 PointerProperties pointerProperties;
2635 pointerProperties.clear();
2636 pointerProperties.id = 0;
2637 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2638
2639 PointerCoords pointerCoords;
2640 pointerCoords.clear();
2641 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2642 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2643
2644 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002645 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002646 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002647 metaState, buttonState, MotionClassification::NONE,
2648 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2649 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002651 }
2652
2653 // Update state.
2654 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2655 if (!down) {
2656 mPointerGesture.lastGestureIdBits.clear();
2657 } else {
2658 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2659 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2660 uint32_t id = idBits.clearFirstMarkedBit();
2661 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2662 mPointerGesture.lastGestureProperties[index].copyFrom(
2663 mPointerGesture.currentGestureProperties[index]);
2664 mPointerGesture.lastGestureCoords[index].copyFrom(
2665 mPointerGesture.currentGestureCoords[index]);
2666 mPointerGesture.lastGestureIdToIndex[id] = index;
2667 }
2668 }
2669}
2670
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002671void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002672 // Cancel previously dispatches pointers.
2673 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2674 int32_t metaState = getContext()->getGlobalMetaState();
2675 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002676 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2677 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002678 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2679 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2680 0, 0, mPointerGesture.downTime);
2681 }
2682
2683 // Reset the current pointer gesture.
2684 mPointerGesture.reset();
2685 mPointerVelocityControl.reset();
2686
2687 // Remove any current spots.
2688 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002689 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002690 mPointerController->clearSpots();
2691 }
2692}
2693
2694bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2695 bool* outFinishPreviousGesture, bool isTimeout) {
2696 *outCancelPreviousGesture = false;
2697 *outFinishPreviousGesture = false;
2698
2699 // Handle TAP timeout.
2700 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002701 if (DEBUG_GESTURES) {
2702 ALOGD("Gestures: Processing timeout");
2703 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002704
Michael Wright227c5542020-07-02 18:30:52 +01002705 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002706 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2707 // The tap/drag timeout has not yet expired.
2708 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2709 mConfig.pointerGestureTapDragInterval);
2710 } else {
2711 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002712 if (DEBUG_GESTURES) {
2713 ALOGD("Gestures: TAP finished");
2714 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002715 *outFinishPreviousGesture = true;
2716
2717 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002718 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002719 mPointerGesture.currentGestureIdBits.clear();
2720
2721 mPointerVelocityControl.reset();
2722 return true;
2723 }
2724 }
2725
2726 // We did not handle this timeout.
2727 return false;
2728 }
2729
2730 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2731 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2732
2733 // Update the velocity tracker.
2734 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002735 std::vector<VelocityTracker::Position> positions;
2736 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002737 uint32_t id = idBits.clearFirstMarkedBit();
2738 const RawPointerData::Pointer& pointer =
2739 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002740 float x = pointer.x * mPointerXMovementScale;
2741 float y = pointer.y * mPointerYMovementScale;
2742 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002743 }
2744 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2745 positions);
2746 }
2747
2748 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2749 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002750 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2751 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2752 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002753 mPointerGesture.resetTap();
2754 }
2755
2756 // Pick a new active touch id if needed.
2757 // Choose an arbitrary pointer that just went down, if there is one.
2758 // Otherwise choose an arbitrary remaining pointer.
2759 // This guarantees we always have an active touch id when there is at least one pointer.
2760 // We keep the same active touch id for as long as possible.
2761 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2762 int32_t activeTouchId = lastActiveTouchId;
2763 if (activeTouchId < 0) {
2764 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2765 activeTouchId = mPointerGesture.activeTouchId =
2766 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2767 mPointerGesture.firstTouchTime = when;
2768 }
2769 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2770 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2771 activeTouchId = mPointerGesture.activeTouchId =
2772 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2773 } else {
2774 activeTouchId = mPointerGesture.activeTouchId = -1;
2775 }
2776 }
2777
2778 // Determine whether we are in quiet time.
2779 bool isQuietTime = false;
2780 if (activeTouchId < 0) {
2781 mPointerGesture.resetQuietTime();
2782 } else {
2783 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2784 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002785 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2786 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2787 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002788 currentFingerCount < 2) {
2789 // Enter quiet time when exiting swipe or freeform state.
2790 // This is to prevent accidentally entering the hover state and flinging the
2791 // pointer when finishing a swipe and there is still one pointer left onscreen.
2792 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002793 } else if (mPointerGesture.lastGestureMode ==
2794 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002795 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2796 // Enter quiet time when releasing the button and there are still two or more
2797 // fingers down. This may indicate that one finger was used to press the button
2798 // but it has not gone up yet.
2799 isQuietTime = true;
2800 }
2801 if (isQuietTime) {
2802 mPointerGesture.quietTime = when;
2803 }
2804 }
2805 }
2806
2807 // Switch states based on button and pointer state.
2808 if (isQuietTime) {
2809 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002810 if (DEBUG_GESTURES) {
2811 ALOGD("Gestures: QUIET for next %0.3fms",
2812 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2813 0.000001f);
2814 }
Michael Wright227c5542020-07-02 18:30:52 +01002815 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002816 *outFinishPreviousGesture = true;
2817 }
2818
2819 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002820 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002821 mPointerGesture.currentGestureIdBits.clear();
2822
2823 mPointerVelocityControl.reset();
2824 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2825 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2826 // The pointer follows the active touch point.
2827 // Emit DOWN, MOVE, UP events at the pointer location.
2828 //
2829 // Only the active touch matters; other fingers are ignored. This policy helps
2830 // to handle the case where the user places a second finger on the touch pad
2831 // to apply the necessary force to depress an integrated button below the surface.
2832 // We don't want the second finger to be delivered to applications.
2833 //
2834 // For this to work well, we need to make sure to track the pointer that is really
2835 // active. If the user first puts one finger down to click then adds another
2836 // finger to drag then the active pointer should switch to the finger that is
2837 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002838 if (DEBUG_GESTURES) {
2839 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2840 "currentFingerCount=%d",
2841 activeTouchId, currentFingerCount);
2842 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002843 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002844 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002845 *outFinishPreviousGesture = true;
2846 mPointerGesture.activeGestureId = 0;
2847 }
2848
2849 // Switch pointers if needed.
2850 // Find the fastest pointer and follow it.
2851 if (activeTouchId >= 0 && currentFingerCount > 1) {
2852 int32_t bestId = -1;
2853 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2854 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2855 uint32_t id = idBits.clearFirstMarkedBit();
2856 float vx, vy;
2857 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2858 float speed = hypotf(vx, vy);
2859 if (speed > bestSpeed) {
2860 bestId = id;
2861 bestSpeed = speed;
2862 }
2863 }
2864 }
2865 if (bestId >= 0 && bestId != activeTouchId) {
2866 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002867 if (DEBUG_GESTURES) {
2868 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2869 "bestId=%d, bestSpeed=%0.3f",
2870 bestId, bestSpeed);
2871 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002872 }
2873 }
2874
2875 float deltaX = 0, deltaY = 0;
2876 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2877 const RawPointerData::Pointer& currentPointer =
2878 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2879 const RawPointerData::Pointer& lastPointer =
2880 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2881 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2882 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2883
Prabir Pradhan1728b212021-10-19 16:00:03 -07002884 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002885 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2886
2887 // Move the pointer using a relative motion.
2888 // When using spots, the click will occur at the position of the anchor
2889 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002890 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002891 } else {
2892 mPointerVelocityControl.reset();
2893 }
2894
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002895 float x, y;
2896 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002897
Michael Wright227c5542020-07-02 18:30:52 +01002898 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002899 mPointerGesture.currentGestureIdBits.clear();
2900 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2901 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2902 mPointerGesture.currentGestureProperties[0].clear();
2903 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2904 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2905 mPointerGesture.currentGestureCoords[0].clear();
2906 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2907 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2908 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2909 } else if (currentFingerCount == 0) {
2910 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002911 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002912 *outFinishPreviousGesture = true;
2913 }
2914
2915 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2916 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2917 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002918 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2919 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002920 lastFingerCount == 1) {
2921 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002922 float x, y;
2923 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002924 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2925 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002926 if (DEBUG_GESTURES) {
2927 ALOGD("Gestures: TAP");
2928 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002929
2930 mPointerGesture.tapUpTime = when;
2931 getContext()->requestTimeoutAtTime(when +
2932 mConfig.pointerGestureTapDragInterval);
2933
2934 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002935 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002936 mPointerGesture.currentGestureIdBits.clear();
2937 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2938 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2939 mPointerGesture.currentGestureProperties[0].clear();
2940 mPointerGesture.currentGestureProperties[0].id =
2941 mPointerGesture.activeGestureId;
2942 mPointerGesture.currentGestureProperties[0].toolType =
2943 AMOTION_EVENT_TOOL_TYPE_FINGER;
2944 mPointerGesture.currentGestureCoords[0].clear();
2945 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2946 mPointerGesture.tapX);
2947 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2948 mPointerGesture.tapY);
2949 mPointerGesture.currentGestureCoords[0]
2950 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2951
2952 tapped = true;
2953 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002954 if (DEBUG_GESTURES) {
2955 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2956 y - mPointerGesture.tapY);
2957 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002958 }
2959 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002960 if (DEBUG_GESTURES) {
2961 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2962 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2963 (when - mPointerGesture.tapDownTime) * 0.000001f);
2964 } else {
2965 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2966 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002967 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002968 }
2969 }
2970
2971 mPointerVelocityControl.reset();
2972
2973 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002974 if (DEBUG_GESTURES) {
2975 ALOGD("Gestures: NEUTRAL");
2976 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002977 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002978 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002979 mPointerGesture.currentGestureIdBits.clear();
2980 }
2981 } else if (currentFingerCount == 1) {
2982 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2983 // The pointer follows the active touch point.
2984 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2985 // When in TAP_DRAG, emit MOVE events at the pointer location.
2986 ALOG_ASSERT(activeTouchId >= 0);
2987
Michael Wright227c5542020-07-02 18:30:52 +01002988 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2989 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002990 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002991 float x, y;
2992 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002993 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2994 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002995 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002996 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002997 if (DEBUG_GESTURES) {
2998 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2999 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
3000 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003001 }
3002 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003003 if (DEBUG_GESTURES) {
3004 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3005 (when - mPointerGesture.tapUpTime) * 0.000001f);
3006 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003007 }
Michael Wright227c5542020-07-02 18:30:52 +01003008 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
3009 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003010 }
3011
3012 float deltaX = 0, deltaY = 0;
3013 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
3014 const RawPointerData::Pointer& currentPointer =
3015 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
3016 const RawPointerData::Pointer& lastPointer =
3017 mLastRawState.rawPointerData.pointerForId(activeTouchId);
3018 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3019 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3020
Prabir Pradhan1728b212021-10-19 16:00:03 -07003021 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003022 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3023
3024 // Move the pointer using a relative motion.
3025 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003026 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003027 } else {
3028 mPointerVelocityControl.reset();
3029 }
3030
3031 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003032 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003033 if (DEBUG_GESTURES) {
3034 ALOGD("Gestures: TAP_DRAG");
3035 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003036 down = true;
3037 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003038 if (DEBUG_GESTURES) {
3039 ALOGD("Gestures: HOVER");
3040 }
Michael Wright227c5542020-07-02 18:30:52 +01003041 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003042 *outFinishPreviousGesture = true;
3043 }
3044 mPointerGesture.activeGestureId = 0;
3045 down = false;
3046 }
3047
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003048 float x, y;
3049 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003050
3051 mPointerGesture.currentGestureIdBits.clear();
3052 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3053 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3054 mPointerGesture.currentGestureProperties[0].clear();
3055 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3056 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3057 mPointerGesture.currentGestureCoords[0].clear();
3058 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3059 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3060 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3061 down ? 1.0f : 0.0f);
3062
3063 if (lastFingerCount == 0 && currentFingerCount != 0) {
3064 mPointerGesture.resetTap();
3065 mPointerGesture.tapDownTime = when;
3066 mPointerGesture.tapX = x;
3067 mPointerGesture.tapY = y;
3068 }
3069 } else {
3070 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3071 // We need to provide feedback for each finger that goes down so we cannot wait
3072 // for the fingers to move before deciding what to do.
3073 //
3074 // The ambiguous case is deciding what to do when there are two fingers down but they
3075 // have not moved enough to determine whether they are part of a drag or part of a
3076 // freeform gesture, or just a press or long-press at the pointer location.
3077 //
3078 // When there are two fingers we start with the PRESS hypothesis and we generate a
3079 // down at the pointer location.
3080 //
3081 // When the two fingers move enough or when additional fingers are added, we make
3082 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3083 ALOG_ASSERT(activeTouchId >= 0);
3084
3085 bool settled = when >=
3086 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003087 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3088 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3089 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003090 *outFinishPreviousGesture = true;
3091 } else if (!settled && currentFingerCount > lastFingerCount) {
3092 // Additional pointers have gone down but not yet settled.
3093 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003094 if (DEBUG_GESTURES) {
3095 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3096 "MULTITOUCH, settle time remaining %0.3fms",
3097 (mPointerGesture.firstTouchTime +
3098 mConfig.pointerGestureMultitouchSettleInterval - when) *
3099 0.000001f);
3100 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003101 *outCancelPreviousGesture = true;
3102 } else {
3103 // Continue previous gesture.
3104 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3105 }
3106
3107 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003108 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003109 mPointerGesture.activeGestureId = 0;
3110 mPointerGesture.referenceIdBits.clear();
3111 mPointerVelocityControl.reset();
3112
3113 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003114 if (DEBUG_GESTURES) {
3115 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3116 "settle time remaining %0.3fms",
3117 (mPointerGesture.firstTouchTime +
3118 mConfig.pointerGestureMultitouchSettleInterval - when) *
3119 0.000001f);
3120 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003121 mCurrentRawState.rawPointerData
3122 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3123 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003124 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3125 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003126 }
3127
3128 // Clear the reference deltas for fingers not yet included in the reference calculation.
3129 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3130 ~mPointerGesture.referenceIdBits.value);
3131 !idBits.isEmpty();) {
3132 uint32_t id = idBits.clearFirstMarkedBit();
3133 mPointerGesture.referenceDeltas[id].dx = 0;
3134 mPointerGesture.referenceDeltas[id].dy = 0;
3135 }
3136 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3137
3138 // Add delta for all fingers and calculate a common movement delta.
3139 float commonDeltaX = 0, commonDeltaY = 0;
3140 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3141 mCurrentCookedState.fingerIdBits.value);
3142 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3143 bool first = (idBits == commonIdBits);
3144 uint32_t id = idBits.clearFirstMarkedBit();
3145 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3146 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3147 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3148 delta.dx += cpd.x - lpd.x;
3149 delta.dy += cpd.y - lpd.y;
3150
3151 if (first) {
3152 commonDeltaX = delta.dx;
3153 commonDeltaY = delta.dy;
3154 } else {
3155 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3156 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3157 }
3158 }
3159
3160 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003161 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003162 float dist[MAX_POINTER_ID + 1];
3163 int32_t distOverThreshold = 0;
3164 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3165 uint32_t id = idBits.clearFirstMarkedBit();
3166 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3167 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3168 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3169 distOverThreshold += 1;
3170 }
3171 }
3172
3173 // Only transition when at least two pointers have moved further than
3174 // the minimum distance threshold.
3175 if (distOverThreshold >= 2) {
3176 if (currentFingerCount > 2) {
3177 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003178 if (DEBUG_GESTURES) {
3179 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3180 currentFingerCount);
3181 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003182 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003183 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003184 } else {
3185 // There are exactly two pointers.
3186 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3187 uint32_t id1 = idBits.clearFirstMarkedBit();
3188 uint32_t id2 = idBits.firstMarkedBit();
3189 const RawPointerData::Pointer& p1 =
3190 mCurrentRawState.rawPointerData.pointerForId(id1);
3191 const RawPointerData::Pointer& p2 =
3192 mCurrentRawState.rawPointerData.pointerForId(id2);
3193 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3194 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3195 // There are two pointers but they are too far apart for a SWIPE,
3196 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003197 if (DEBUG_GESTURES) {
3198 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3199 "%0.3f",
3200 mutualDistance, mPointerGestureMaxSwipeWidth);
3201 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003202 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003203 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003204 } else {
3205 // There are two pointers. Wait for both pointers to start moving
3206 // before deciding whether this is a SWIPE or FREEFORM gesture.
3207 float dist1 = dist[id1];
3208 float dist2 = dist[id2];
3209 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3210 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3211 // Calculate the dot product of the displacement vectors.
3212 // When the vectors are oriented in approximately the same direction,
3213 // the angle betweeen them is near zero and the cosine of the angle
3214 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3215 // mag(v2).
3216 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3217 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3218 float dx1 = delta1.dx * mPointerXZoomScale;
3219 float dy1 = delta1.dy * mPointerYZoomScale;
3220 float dx2 = delta2.dx * mPointerXZoomScale;
3221 float dy2 = delta2.dy * mPointerYZoomScale;
3222 float dot = dx1 * dx2 + dy1 * dy2;
3223 float cosine = dot / (dist1 * dist2); // denominator always > 0
3224 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3225 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003226 if (DEBUG_GESTURES) {
3227 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3228 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3229 "cosine %0.3f >= %0.3f",
3230 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3231 mConfig.pointerGestureMultitouchMinDistance, cosine,
3232 mConfig.pointerGestureSwipeTransitionAngleCosine);
3233 }
Michael Wright227c5542020-07-02 18:30:52 +01003234 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003235 } else {
3236 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003237 if (DEBUG_GESTURES) {
3238 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3239 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3240 "cosine %0.3f < %0.3f",
3241 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3242 mConfig.pointerGestureMultitouchMinDistance, cosine,
3243 mConfig.pointerGestureSwipeTransitionAngleCosine);
3244 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003245 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003246 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003247 }
3248 }
3249 }
3250 }
3251 }
Michael Wright227c5542020-07-02 18:30:52 +01003252 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003253 // Switch from SWIPE to FREEFORM if additional pointers go down.
3254 // Cancel previous gesture.
3255 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003256 if (DEBUG_GESTURES) {
3257 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3258 currentFingerCount);
3259 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003260 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003261 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003262 }
3263 }
3264
3265 // Move the reference points based on the overall group motion of the fingers
3266 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003267 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003268 (commonDeltaX || commonDeltaY)) {
3269 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3270 uint32_t id = idBits.clearFirstMarkedBit();
3271 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3272 delta.dx = 0;
3273 delta.dy = 0;
3274 }
3275
3276 mPointerGesture.referenceTouchX += commonDeltaX;
3277 mPointerGesture.referenceTouchY += commonDeltaY;
3278
3279 commonDeltaX *= mPointerXMovementScale;
3280 commonDeltaY *= mPointerYMovementScale;
3281
Prabir Pradhan1728b212021-10-19 16:00:03 -07003282 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003283 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3284
3285 mPointerGesture.referenceGestureX += commonDeltaX;
3286 mPointerGesture.referenceGestureY += commonDeltaY;
3287 }
3288
3289 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003290 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3291 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003292 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003293 if (DEBUG_GESTURES) {
3294 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3295 "activeGestureId=%d, currentTouchPointerCount=%d",
3296 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3297 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003298 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3299
3300 mPointerGesture.currentGestureIdBits.clear();
3301 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3302 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3303 mPointerGesture.currentGestureProperties[0].clear();
3304 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3305 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3306 mPointerGesture.currentGestureCoords[0].clear();
3307 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3308 mPointerGesture.referenceGestureX);
3309 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3310 mPointerGesture.referenceGestureY);
3311 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003312 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003313 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003314 if (DEBUG_GESTURES) {
3315 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3316 "activeGestureId=%d, currentTouchPointerCount=%d",
3317 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3318 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003319 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3320
3321 mPointerGesture.currentGestureIdBits.clear();
3322
3323 BitSet32 mappedTouchIdBits;
3324 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003325 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003326 // Initially, assign the active gesture id to the active touch point
3327 // if there is one. No other touch id bits are mapped yet.
3328 if (!*outCancelPreviousGesture) {
3329 mappedTouchIdBits.markBit(activeTouchId);
3330 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3331 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3332 mPointerGesture.activeGestureId;
3333 } else {
3334 mPointerGesture.activeGestureId = -1;
3335 }
3336 } else {
3337 // Otherwise, assume we mapped all touches from the previous frame.
3338 // Reuse all mappings that are still applicable.
3339 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3340 mCurrentCookedState.fingerIdBits.value;
3341 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3342
3343 // Check whether we need to choose a new active gesture id because the
3344 // current went went up.
3345 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3346 ~mCurrentCookedState.fingerIdBits.value);
3347 !upTouchIdBits.isEmpty();) {
3348 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3349 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3350 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3351 mPointerGesture.activeGestureId = -1;
3352 break;
3353 }
3354 }
3355 }
3356
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003357 if (DEBUG_GESTURES) {
3358 ALOGD("Gestures: FREEFORM follow up "
3359 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3360 "activeGestureId=%d",
3361 mappedTouchIdBits.value, usedGestureIdBits.value,
3362 mPointerGesture.activeGestureId);
3363 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003364
3365 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3366 for (uint32_t i = 0; i < currentFingerCount; i++) {
3367 uint32_t touchId = idBits.clearFirstMarkedBit();
3368 uint32_t gestureId;
3369 if (!mappedTouchIdBits.hasBit(touchId)) {
3370 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3371 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003372 if (DEBUG_GESTURES) {
3373 ALOGD("Gestures: FREEFORM "
3374 "new mapping for touch id %d -> gesture id %d",
3375 touchId, gestureId);
3376 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003377 } else {
3378 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003379 if (DEBUG_GESTURES) {
3380 ALOGD("Gestures: FREEFORM "
3381 "existing mapping for touch id %d -> gesture id %d",
3382 touchId, gestureId);
3383 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003384 }
3385 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3386 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3387
3388 const RawPointerData::Pointer& pointer =
3389 mCurrentRawState.rawPointerData.pointerForId(touchId);
3390 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3391 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003392 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003393
3394 mPointerGesture.currentGestureProperties[i].clear();
3395 mPointerGesture.currentGestureProperties[i].id = gestureId;
3396 mPointerGesture.currentGestureProperties[i].toolType =
3397 AMOTION_EVENT_TOOL_TYPE_FINGER;
3398 mPointerGesture.currentGestureCoords[i].clear();
3399 mPointerGesture.currentGestureCoords[i]
3400 .setAxisValue(AMOTION_EVENT_AXIS_X,
3401 mPointerGesture.referenceGestureX + deltaX);
3402 mPointerGesture.currentGestureCoords[i]
3403 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3404 mPointerGesture.referenceGestureY + deltaY);
3405 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3406 1.0f);
3407 }
3408
3409 if (mPointerGesture.activeGestureId < 0) {
3410 mPointerGesture.activeGestureId =
3411 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003412 if (DEBUG_GESTURES) {
3413 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3414 mPointerGesture.activeGestureId);
3415 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003416 }
3417 }
3418 }
3419
3420 mPointerController->setButtonState(mCurrentRawState.buttonState);
3421
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003422 if (DEBUG_GESTURES) {
3423 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3424 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3425 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3426 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3427 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3428 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3429 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3430 uint32_t id = idBits.clearFirstMarkedBit();
3431 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3432 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3433 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3434 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3435 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3436 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3437 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3438 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3439 }
3440 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3441 uint32_t id = idBits.clearFirstMarkedBit();
3442 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3443 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3444 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3445 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3446 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3447 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3448 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3449 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3450 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003451 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003452 return true;
3453}
3454
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003456 mPointerSimple.currentCoords.clear();
3457 mPointerSimple.currentProperties.clear();
3458
3459 bool down, hovering;
3460 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3461 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3462 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003463 mPointerController
3464 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3465 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003466
3467 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3468 down = !hovering;
3469
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003470 float x, y;
3471 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003472 mPointerSimple.currentCoords.copyFrom(
3473 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3474 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3475 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3476 mPointerSimple.currentProperties.id = 0;
3477 mPointerSimple.currentProperties.toolType =
3478 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3479 } else {
3480 down = false;
3481 hovering = false;
3482 }
3483
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003484 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003485}
3486
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003487void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3488 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003489}
3490
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003491void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003492 mPointerSimple.currentCoords.clear();
3493 mPointerSimple.currentProperties.clear();
3494
3495 bool down, hovering;
3496 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3497 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3498 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3499 float deltaX = 0, deltaY = 0;
3500 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3501 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3502 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3503 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3504 mPointerXMovementScale;
3505 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3506 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3507 mPointerYMovementScale;
3508
Prabir Pradhan1728b212021-10-19 16:00:03 -07003509 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003510 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3511
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003512 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003513 } else {
3514 mPointerVelocityControl.reset();
3515 }
3516
3517 down = isPointerDown(mCurrentRawState.buttonState);
3518 hovering = !down;
3519
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003520 float x, y;
3521 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003522 mPointerSimple.currentCoords.copyFrom(
3523 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3524 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3525 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3526 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3527 hovering ? 0.0f : 1.0f);
3528 mPointerSimple.currentProperties.id = 0;
3529 mPointerSimple.currentProperties.toolType =
3530 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3531 } else {
3532 mPointerVelocityControl.reset();
3533
3534 down = false;
3535 hovering = false;
3536 }
3537
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003538 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003539}
3540
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003541void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3542 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003543
3544 mPointerVelocityControl.reset();
3545}
3546
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3548 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003549 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003550
3551 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003552 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003553 mPointerController->clearSpots();
3554 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003555 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003556 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003557 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003558 }
Garfield Tan9514d782020-11-10 16:37:23 -08003559 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003560
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003561 float xCursorPosition, yCursorPosition;
3562 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003563
3564 if (mPointerSimple.down && !down) {
3565 mPointerSimple.down = false;
3566
3567 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003568 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3569 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003570 mLastRawState.buttonState, MotionClassification::NONE,
3571 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3572 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3573 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3574 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003575 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003576 }
3577
3578 if (mPointerSimple.hovering && !hovering) {
3579 mPointerSimple.hovering = false;
3580
3581 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3583 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3584 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003585 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3586 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3587 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3588 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003589 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003590 }
3591
3592 if (down) {
3593 if (!mPointerSimple.down) {
3594 mPointerSimple.down = true;
3595 mPointerSimple.downTime = when;
3596
3597 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003599 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3600 metaState, mCurrentRawState.buttonState,
3601 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3602 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3603 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3604 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003605 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003606 }
3607
3608 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003609 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3610 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003611 mCurrentRawState.buttonState, MotionClassification::NONE,
3612 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3613 &mPointerSimple.currentCoords, mOrientedXPrecision,
3614 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3615 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003616 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003617 }
3618
3619 if (hovering) {
3620 if (!mPointerSimple.hovering) {
3621 mPointerSimple.hovering = true;
3622
3623 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003624 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003625 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3626 metaState, mCurrentRawState.buttonState,
3627 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3628 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3629 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3630 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003631 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003632 }
3633
3634 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003635 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3636 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3637 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003638 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3639 &mPointerSimple.currentCoords, mOrientedXPrecision,
3640 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3641 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003642 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003643 }
3644
3645 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3646 float vscroll = mCurrentRawState.rawVScroll;
3647 float hscroll = mCurrentRawState.rawHScroll;
3648 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3649 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3650
3651 // Send scroll.
3652 PointerCoords pointerCoords;
3653 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3654 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3655 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3656
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3658 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003659 mCurrentRawState.buttonState, MotionClassification::NONE,
3660 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3661 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3662 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3663 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003664 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003665 }
3666
3667 // Save state.
3668 if (down || hovering) {
3669 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3670 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3671 } else {
3672 mPointerSimple.reset();
3673 }
3674}
3675
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003676void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003677 mPointerSimple.currentCoords.clear();
3678 mPointerSimple.currentProperties.clear();
3679
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003680 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003681}
3682
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3684 uint32_t source, int32_t action, int32_t actionButton,
3685 int32_t flags, int32_t metaState, int32_t buttonState,
3686 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003687 const PointerCoords* coords, const uint32_t* idToIndex,
3688 BitSet32 idBits, int32_t changedId, float xPrecision,
3689 float yPrecision, nsecs_t downTime) {
3690 PointerCoords pointerCoords[MAX_POINTERS];
3691 PointerProperties pointerProperties[MAX_POINTERS];
3692 uint32_t pointerCount = 0;
3693 while (!idBits.isEmpty()) {
3694 uint32_t id = idBits.clearFirstMarkedBit();
3695 uint32_t index = idToIndex[id];
3696 pointerProperties[pointerCount].copyFrom(properties[index]);
3697 pointerCoords[pointerCount].copyFrom(coords[index]);
3698
3699 if (changedId >= 0 && id == uint32_t(changedId)) {
3700 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3701 }
3702
3703 pointerCount += 1;
3704 }
3705
3706 ALOG_ASSERT(pointerCount != 0);
3707
3708 if (changedId >= 0 && pointerCount == 1) {
3709 // Replace initial down and final up action.
3710 // We can compare the action without masking off the changed pointer index
3711 // because we know the index is 0.
3712 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3713 action = AMOTION_EVENT_ACTION_DOWN;
3714 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003715 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3716 action = AMOTION_EVENT_ACTION_CANCEL;
3717 } else {
3718 action = AMOTION_EVENT_ACTION_UP;
3719 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003720 } else {
3721 // Can't happen.
3722 ALOG_ASSERT(false);
3723 }
3724 }
3725 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3726 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003727 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003728 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003729 }
3730 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3731 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003732 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003733 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003734 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3736 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003737 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3738 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3739 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003740 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003741}
3742
3743bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3744 const PointerCoords* inCoords,
3745 const uint32_t* inIdToIndex,
3746 PointerProperties* outProperties,
3747 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3748 BitSet32 idBits) const {
3749 bool changed = false;
3750 while (!idBits.isEmpty()) {
3751 uint32_t id = idBits.clearFirstMarkedBit();
3752 uint32_t inIndex = inIdToIndex[id];
3753 uint32_t outIndex = outIdToIndex[id];
3754
3755 const PointerProperties& curInProperties = inProperties[inIndex];
3756 const PointerCoords& curInCoords = inCoords[inIndex];
3757 PointerProperties& curOutProperties = outProperties[outIndex];
3758 PointerCoords& curOutCoords = outCoords[outIndex];
3759
3760 if (curInProperties != curOutProperties) {
3761 curOutProperties.copyFrom(curInProperties);
3762 changed = true;
3763 }
3764
3765 if (curInCoords != curOutCoords) {
3766 curOutCoords.copyFrom(curInCoords);
3767 changed = true;
3768 }
3769 }
3770 return changed;
3771}
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3774 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3775 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003776}
3777
Prabir Pradhan1728b212021-10-19 16:00:03 -07003778// Transform input device coordinates to display panel coordinates.
3779void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003780 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3781 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3782
arthurhunga36b28e2020-12-29 20:28:15 +08003783 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3784 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3785
Prabir Pradhan1728b212021-10-19 16:00:03 -07003786 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003787 // 0 - no swap and reverse.
3788 // 90 - swap x/y and reverse y.
3789 // 180 - reverse x, y.
3790 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003791 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003792 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003793 x = xScaled;
3794 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003795 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003796 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003797 y = xScaledMax;
3798 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003799 break;
3800 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003801 x = xScaledMax;
3802 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003803 break;
3804 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003805 y = xScaled;
3806 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003807 break;
3808 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003809 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003810 }
3811}
3812
Prabir Pradhan1728b212021-10-19 16:00:03 -07003813bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003814 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3815 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3816
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003817 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003818 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003819 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003820 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003821}
3822
3823const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3824 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003825 if (DEBUG_VIRTUAL_KEYS) {
3826 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3827 "left=%d, top=%d, right=%d, bottom=%d",
3828 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3829 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3830 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003831
3832 if (virtualKey.isHit(x, y)) {
3833 return &virtualKey;
3834 }
3835 }
3836
3837 return nullptr;
3838}
3839
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003840void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3841 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3842 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003843
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003844 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003845
3846 if (currentPointerCount == 0) {
3847 // No pointers to assign.
3848 return;
3849 }
3850
3851 if (lastPointerCount == 0) {
3852 // All pointers are new.
3853 for (uint32_t i = 0; i < currentPointerCount; i++) {
3854 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003855 current.rawPointerData.pointers[i].id = id;
3856 current.rawPointerData.idToIndex[id] = i;
3857 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003858 }
3859 return;
3860 }
3861
3862 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003863 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003864 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003865 uint32_t id = last.rawPointerData.pointers[0].id;
3866 current.rawPointerData.pointers[0].id = id;
3867 current.rawPointerData.idToIndex[id] = 0;
3868 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003869 return;
3870 }
3871
3872 // General case.
3873 // We build a heap of squared euclidean distances between current and last pointers
3874 // associated with the current and last pointer indices. Then, we find the best
3875 // match (by distance) for each current pointer.
3876 // The pointers must have the same tool type but it is possible for them to
3877 // transition from hovering to touching or vice-versa while retaining the same id.
3878 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3879
3880 uint32_t heapSize = 0;
3881 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3882 currentPointerIndex++) {
3883 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3884 lastPointerIndex++) {
3885 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003886 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003887 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003888 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003889 if (currentPointer.toolType == lastPointer.toolType) {
3890 int64_t deltaX = currentPointer.x - lastPointer.x;
3891 int64_t deltaY = currentPointer.y - lastPointer.y;
3892
3893 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3894
3895 // Insert new element into the heap (sift up).
3896 heap[heapSize].currentPointerIndex = currentPointerIndex;
3897 heap[heapSize].lastPointerIndex = lastPointerIndex;
3898 heap[heapSize].distance = distance;
3899 heapSize += 1;
3900 }
3901 }
3902 }
3903
3904 // Heapify
3905 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3906 startIndex -= 1;
3907 for (uint32_t parentIndex = startIndex;;) {
3908 uint32_t childIndex = parentIndex * 2 + 1;
3909 if (childIndex >= heapSize) {
3910 break;
3911 }
3912
3913 if (childIndex + 1 < heapSize &&
3914 heap[childIndex + 1].distance < heap[childIndex].distance) {
3915 childIndex += 1;
3916 }
3917
3918 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3919 break;
3920 }
3921
3922 swap(heap[parentIndex], heap[childIndex]);
3923 parentIndex = childIndex;
3924 }
3925 }
3926
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003927 if (DEBUG_POINTER_ASSIGNMENT) {
3928 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3929 for (size_t i = 0; i < heapSize; i++) {
3930 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3931 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3932 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003933 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003934
3935 // Pull matches out by increasing order of distance.
3936 // To avoid reassigning pointers that have already been matched, the loop keeps track
3937 // of which last and current pointers have been matched using the matchedXXXBits variables.
3938 // It also tracks the used pointer id bits.
3939 BitSet32 matchedLastBits(0);
3940 BitSet32 matchedCurrentBits(0);
3941 BitSet32 usedIdBits(0);
3942 bool first = true;
3943 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3944 while (heapSize > 0) {
3945 if (first) {
3946 // The first time through the loop, we just consume the root element of
3947 // the heap (the one with smallest distance).
3948 first = false;
3949 } else {
3950 // Previous iterations consumed the root element of the heap.
3951 // Pop root element off of the heap (sift down).
3952 heap[0] = heap[heapSize];
3953 for (uint32_t parentIndex = 0;;) {
3954 uint32_t childIndex = parentIndex * 2 + 1;
3955 if (childIndex >= heapSize) {
3956 break;
3957 }
3958
3959 if (childIndex + 1 < heapSize &&
3960 heap[childIndex + 1].distance < heap[childIndex].distance) {
3961 childIndex += 1;
3962 }
3963
3964 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3965 break;
3966 }
3967
3968 swap(heap[parentIndex], heap[childIndex]);
3969 parentIndex = childIndex;
3970 }
3971
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003972 if (DEBUG_POINTER_ASSIGNMENT) {
3973 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3974 for (size_t j = 0; j < heapSize; j++) {
3975 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3976 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3977 heap[j].distance);
3978 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003979 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003980 }
3981
3982 heapSize -= 1;
3983
3984 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3985 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3986
3987 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3988 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3989
3990 matchedCurrentBits.markBit(currentPointerIndex);
3991 matchedLastBits.markBit(lastPointerIndex);
3992
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003993 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3994 current.rawPointerData.pointers[currentPointerIndex].id = id;
3995 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3996 current.rawPointerData.markIdBit(id,
3997 current.rawPointerData.isHovering(
3998 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003999 usedIdBits.markBit(id);
4000
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004001 if (DEBUG_POINTER_ASSIGNMENT) {
4002 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
4003 ", distance=%" PRIu64,
4004 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4005 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004006 break;
4007 }
4008 }
4009
4010 // Assign fresh ids to pointers that were not matched in the process.
4011 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4012 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4013 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4014
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004015 current.rawPointerData.pointers[currentPointerIndex].id = id;
4016 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4017 current.rawPointerData.markIdBit(id,
4018 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004019
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004020 if (DEBUG_POINTER_ASSIGNMENT) {
4021 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
4022 id);
4023 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004024 }
4025}
4026
4027int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4028 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4029 return AKEY_STATE_VIRTUAL;
4030 }
4031
4032 for (const VirtualKey& virtualKey : mVirtualKeys) {
4033 if (virtualKey.keyCode == keyCode) {
4034 return AKEY_STATE_UP;
4035 }
4036 }
4037
4038 return AKEY_STATE_UNKNOWN;
4039}
4040
4041int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4042 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4043 return AKEY_STATE_VIRTUAL;
4044 }
4045
4046 for (const VirtualKey& virtualKey : mVirtualKeys) {
4047 if (virtualKey.scanCode == scanCode) {
4048 return AKEY_STATE_UP;
4049 }
4050 }
4051
4052 return AKEY_STATE_UNKNOWN;
4053}
4054
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004055bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
4056 const std::vector<int32_t>& keyCodes,
4057 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004058 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004059 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004060 if (virtualKey.keyCode == keyCodes[i]) {
4061 outFlags[i] = 1;
4062 }
4063 }
4064 }
4065
4066 return true;
4067}
4068
4069std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4070 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004071 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004072 return std::make_optional(mPointerController->getDisplayId());
4073 } else {
4074 return std::make_optional(mViewport.displayId);
4075 }
4076 }
4077 return std::nullopt;
4078}
4079
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004080} // namespace android