blob: b5b2b457638562bbfd9512e16e892538fee486bf [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Wright227c5542020-07-02 18:30:52 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wright227c5542020-07-02 18:30:52 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "TouchInputMapper.h"
22
Dominik Laskowski75788452021-02-09 18:51:25 -080023#include <ftl/enum.h>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorButtonAccumulator.h"
26#include "CursorScrollAccumulator.h"
27#include "TouchButtonAccumulator.h"
28#include "TouchCursorInputMapperCommon.h"
29
30namespace android {
31
32// --- Constants ---
33
34// Maximum amount of latency to add to touch events while waiting for data from an
35// external stylus.
36static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
37
38// Maximum amount of time to wait on touch data before pushing out new pressure data.
39static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
40
41// Artificial latency on synthetic events created from stylus data without corresponding touch
42// data.
43static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
44
45// --- Static Definitions ---
46
47template <typename T>
48inline static void swap(T& a, T& b) {
49 T temp = a;
50 a = b;
51 b = temp;
52}
53
54static float calculateCommonVector(float a, float b) {
55 if (a > 0 && b > 0) {
56 return a < b ? a : b;
57 } else if (a < 0 && b < 0) {
58 return a > b ? a : b;
59 } else {
60 return 0;
61 }
62}
63
64inline static float distance(float x1, float y1, float x2, float y2) {
65 return hypotf(x1 - x2, y1 - y2);
66}
67
68inline static int32_t signExtendNybble(int32_t value) {
69 return value >= 8 ? value - 16 : value;
70}
71
72// --- RawPointerAxes ---
73
74RawPointerAxes::RawPointerAxes() {
75 clear();
76}
77
78void RawPointerAxes::clear() {
79 x.clear();
80 y.clear();
81 pressure.clear();
82 touchMajor.clear();
83 touchMinor.clear();
84 toolMajor.clear();
85 toolMinor.clear();
86 orientation.clear();
87 distance.clear();
88 tiltX.clear();
89 tiltY.clear();
90 trackingId.clear();
91 slot.clear();
92}
93
94// --- RawPointerData ---
95
96RawPointerData::RawPointerData() {
97 clear();
98}
99
100void RawPointerData::clear() {
101 pointerCount = 0;
102 clearIdBits();
103}
104
105void RawPointerData::copyFrom(const RawPointerData& other) {
106 pointerCount = other.pointerCount;
107 hoveringIdBits = other.hoveringIdBits;
108 touchingIdBits = other.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +0800109 canceledIdBits = other.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
111 for (uint32_t i = 0; i < pointerCount; i++) {
112 pointers[i] = other.pointers[i];
113
114 int id = pointers[i].id;
115 idToIndex[id] = other.idToIndex[id];
116 }
117}
118
119void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
120 float x = 0, y = 0;
121 uint32_t count = touchingIdBits.count();
122 if (count) {
123 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
124 uint32_t id = idBits.clearFirstMarkedBit();
125 const Pointer& pointer = pointerForId(id);
126 x += pointer.x;
127 y += pointer.y;
128 }
129 x /= count;
130 y /= count;
131 }
132 *outX = x;
133 *outY = y;
134}
135
136// --- CookedPointerData ---
137
138CookedPointerData::CookedPointerData() {
139 clear();
140}
141
142void CookedPointerData::clear() {
143 pointerCount = 0;
144 hoveringIdBits.clear();
145 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800146 canceledIdBits.clear();
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000147 validIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700148}
149
150void CookedPointerData::copyFrom(const CookedPointerData& other) {
151 pointerCount = other.pointerCount;
152 hoveringIdBits = other.hoveringIdBits;
153 touchingIdBits = other.touchingIdBits;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000154 validIdBits = other.validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700155
156 for (uint32_t i = 0; i < pointerCount; i++) {
157 pointerProperties[i].copyFrom(other.pointerProperties[i]);
158 pointerCoords[i].copyFrom(other.pointerCoords[i]);
159
160 int id = pointerProperties[i].id;
161 idToIndex[id] = other.idToIndex[id];
162 }
163}
164
165// --- TouchInputMapper ---
166
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800167TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
168 : InputMapper(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700169 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100170 mDeviceMode(DeviceMode::DISABLED),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700171 mDisplayWidth(-1),
172 mDisplayHeight(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173 mPhysicalWidth(-1),
174 mPhysicalHeight(-1),
175 mPhysicalLeft(0),
176 mPhysicalTop(0),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700177 mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700178
179TouchInputMapper::~TouchInputMapper() {}
180
Philip Junker4af3b3d2021-12-14 10:36:55 +0100181uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182 return mSource;
183}
184
185void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
186 InputMapper::populateDeviceInfo(info);
187
Michael Wright227c5542020-07-02 18:30:52 +0100188 if (mDeviceMode != DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700189 info->addMotionRange(mOrientedRanges.x);
190 info->addMotionRange(mOrientedRanges.y);
191 info->addMotionRange(mOrientedRanges.pressure);
192
Chris Yef74dc422020-09-02 22:41:50 -0700193 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
Chris Ye8fa17282020-09-15 17:17:34 -0700194 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
195 //
196 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
197 // motion, i.e. the hardware dimensions, as the finger could move completely across the
198 // touchpad in one sample cycle.
Chris Yef74dc422020-09-02 22:41:50 -0700199 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
200 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
201 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat,
202 x.fuzz, x.resolution);
203 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat,
204 y.fuzz, y.resolution);
205 }
206
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700207 if (mOrientedRanges.haveSize) {
208 info->addMotionRange(mOrientedRanges.size);
209 }
210
211 if (mOrientedRanges.haveTouchSize) {
212 info->addMotionRange(mOrientedRanges.touchMajor);
213 info->addMotionRange(mOrientedRanges.touchMinor);
214 }
215
216 if (mOrientedRanges.haveToolSize) {
217 info->addMotionRange(mOrientedRanges.toolMajor);
218 info->addMotionRange(mOrientedRanges.toolMinor);
219 }
220
221 if (mOrientedRanges.haveOrientation) {
222 info->addMotionRange(mOrientedRanges.orientation);
223 }
224
225 if (mOrientedRanges.haveDistance) {
226 info->addMotionRange(mOrientedRanges.distance);
227 }
228
229 if (mOrientedRanges.haveTilt) {
230 info->addMotionRange(mOrientedRanges.tilt);
231 }
232
233 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
234 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
235 0.0f);
236 }
237 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
238 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
239 0.0f);
240 }
Michael Wright227c5542020-07-02 18:30:52 +0100241 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700242 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
243 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
244 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
245 x.fuzz, x.resolution);
246 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
247 y.fuzz, y.resolution);
248 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
249 x.fuzz, x.resolution);
250 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
251 y.fuzz, y.resolution);
252 }
253 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
254 }
255}
256
257void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700258 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800259 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700260 dumpParameters(dump);
261 dumpVirtualKeys(dump);
262 dumpRawPointerAxes(dump);
263 dumpCalibration(dump);
264 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700265 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266
267 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268 dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale);
269 dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale);
270 dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
271 dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
272 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
273 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
274 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
275 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
276 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
277 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
278 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
279 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
280 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
281 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
282
283 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
284 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
285 mLastRawState.rawPointerData.pointerCount);
286 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
287 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
288 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
289 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
290 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
291 "toolType=%d, isHovering=%s\n",
292 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
293 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
294 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
295 pointer.distance, pointer.toolType, toString(pointer.isHovering));
296 }
297
298 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
299 mLastCookedState.buttonState);
300 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
301 mLastCookedState.cookedPointerData.pointerCount);
302 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
303 const PointerProperties& pointerProperties =
304 mLastCookedState.cookedPointerData.pointerProperties[i];
305 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000306 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
307 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
308 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700309 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
310 "toolType=%d, isHovering=%s\n",
311 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000312 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
313 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
315 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
316 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
317 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
318 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
319 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
320 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
321 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
322 pointerProperties.toolType,
323 toString(mLastCookedState.cookedPointerData.isHovering(i)));
324 }
325
326 dump += INDENT3 "Stylus Fusion:\n";
327 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
328 toString(mExternalStylusConnected));
329 dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
330 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
331 mExternalStylusFusionTimeout);
332 dump += INDENT3 "External Stylus State:\n";
333 dumpStylusState(dump, mExternalStylusState);
334
Michael Wright227c5542020-07-02 18:30:52 +0100335 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
337 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
338 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
339 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
340 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
341 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
342 }
343}
344
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
346 uint32_t changes) {
347 InputMapper::configure(when, config, changes);
348
349 mConfig = *config;
350
351 if (!changes) { // first time only
352 // Configure basic parameters.
353 configureParameters();
354
355 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800356 mCursorScrollAccumulator.configure(getDeviceContext());
357 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700358
359 // Configure absolute axis information.
360 configureRawPointerAxes();
361
362 // Prepare input device calibration.
363 parseCalibration();
364 resolveCalibration();
365 }
366
367 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
368 // Update location calibration to reflect current settings
369 updateAffineTransformation();
370 }
371
372 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
373 // Update pointer speed.
374 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
375 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
376 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
377 }
378
379 bool resetNeeded = false;
380 if (!changes ||
381 (changes &
382 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800383 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
385 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
386 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700387 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700389 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390 }
391
392 if (changes && resetNeeded) {
lilinnane74b35f2022-07-19 16:00:50 +0800393 // If device was reset, cancel touch event and update touch spot state.
394 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
395 mCurrentCookedState.clear();
396 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700397 // Send reset, unless this is the first time the device has been configured,
398 // in which case the reader will call reset itself after all mappers are ready.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000399 NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId());
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700400 getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700401 }
402}
403
404void TouchInputMapper::resolveExternalStylusPresence() {
405 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800406 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700407 mExternalStylusConnected = !devices.empty();
408
409 if (!mExternalStylusConnected) {
410 resetExternalStylus();
411 }
412}
413
414void TouchInputMapper::configureParameters() {
415 // Use the pointer presentation mode for devices that do not support distinct
416 // multitouch. The spot-based presentation relies on being able to accurately
417 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800418 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100419 ? Parameters::GestureMode::SINGLE_TOUCH
420 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700421
422 String8 gestureModeString;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800423 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.gestureMode"),
424 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100426 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700427 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100428 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 } else if (gestureModeString != "default") {
430 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
431 }
432 }
433
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800434 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700435 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100436 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800437 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100439 mParameters.deviceType = Parameters::DeviceType::POINTER;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800440 } else if (getDeviceContext().hasRelativeAxis(REL_X) ||
441 getDeviceContext().hasRelativeAxis(REL_Y)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442 // The device is a cursor device with a touch pad attached.
443 // By default don't use the touch pad to move the pointer.
Michael Wright227c5542020-07-02 18:30:52 +0100444 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700445 } else {
446 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100447 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700448 }
449
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800450 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700451
452 String8 deviceTypeString;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800453 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.deviceType"),
454 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700455 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100456 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 } else if (deviceTypeString == "touchPad") {
Michael Wright227c5542020-07-02 18:30:52 +0100458 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100460 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100462 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463 } else if (deviceTypeString != "default") {
464 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
465 }
466 }
467
Michael Wright227c5542020-07-02 18:30:52 +0100468 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800469 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.orientationAware"),
470 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700471
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700472 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
473 String8 orientationString;
474 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.orientation"),
475 orientationString)) {
476 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
477 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
478 } else if (orientationString == "ORIENTATION_90") {
479 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
480 } else if (orientationString == "ORIENTATION_180") {
481 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
482 } else if (orientationString == "ORIENTATION_270") {
483 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
484 } else if (orientationString != "ORIENTATION_0") {
485 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.string());
486 }
487 }
488
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700489 mParameters.hasAssociatedDisplay = false;
490 mParameters.associatedDisplayIsExternal = false;
491 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100492 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
493 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700494 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100495 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800496 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700497 String8 uniqueDisplayId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800498 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.displayId"),
499 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700500 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
501 }
502 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800503 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700504 mParameters.hasAssociatedDisplay = true;
505 }
506
507 // Initial downs on external touch devices should wake the device.
508 // Normally we don't do this for internal touch screens to prevent them from waking
509 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800510 mParameters.wake = getDeviceContext().isExternal();
511 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.wake"), mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700512}
513
514void TouchInputMapper::dumpParameters(std::string& dump) {
515 dump += INDENT3 "Parameters:\n";
516
Dominik Laskowski75788452021-02-09 18:51:25 -0800517 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700518
Dominik Laskowski75788452021-02-09 18:51:25 -0800519 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700520
521 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
522 "displayId='%s'\n",
523 toString(mParameters.hasAssociatedDisplay),
524 toString(mParameters.associatedDisplayIsExternal),
525 mParameters.uniqueDisplayId.c_str());
526 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800527 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700528}
529
530void TouchInputMapper::configureRawPointerAxes() {
531 mRawPointerAxes.clear();
532}
533
534void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
535 dump += INDENT3 "Raw Touch Axes:\n";
536 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
537 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
538 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
539 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
545 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
546 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
547 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
548 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
549}
550
551bool TouchInputMapper::hasExternalStylus() const {
552 return mExternalStylusConnected;
553}
554
555/**
556 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000557 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800558 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000559 * 3. Get the matching viewport by either unique id in idc file or by the display type
560 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800561 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700562 */
563std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800564 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000565 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800566 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700567 }
568
Christine Franks2a2293c2022-01-18 11:51:16 -0800569 const std::optional<std::string> associatedDisplayUniqueId =
570 getDeviceContext().getAssociatedDisplayUniqueId();
571 if (associatedDisplayUniqueId) {
572 return getDeviceContext().getAssociatedViewport();
573 }
574
Michael Wright227c5542020-07-02 18:30:52 +0100575 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800576 std::optional<DisplayViewport> viewport =
577 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
578 if (viewport) {
579 return viewport;
580 } else {
581 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
582 mConfig.defaultPointerDisplayId);
583 }
584 }
585
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700586 // Check if uniqueDisplayId is specified in idc file.
587 if (!mParameters.uniqueDisplayId.empty()) {
588 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
589 }
590
591 ViewportType viewportTypeToUse;
592 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100593 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700594 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100595 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700596 }
597
598 std::optional<DisplayViewport> viewport =
599 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100600 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700601 ALOGW("Input device %s should be associated with external display, "
602 "fallback to internal one for the external viewport is not found.",
603 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100604 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700605 }
606
607 return viewport;
608 }
609
610 // No associated display, return a non-display viewport.
611 DisplayViewport newViewport;
612 // Raw width and height in the natural orientation.
613 int32_t rawWidth = mRawPointerAxes.getRawWidth();
614 int32_t rawHeight = mRawPointerAxes.getRawHeight();
615 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
616 return std::make_optional(newViewport);
617}
618
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800619int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
620 if (resolution < 0) {
621 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
622 getDeviceName().c_str());
623 return 0;
624 }
625 return resolution;
626}
627
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800628void TouchInputMapper::initializeSizeRanges() {
629 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
630 mSizeScale = 0.0f;
631 return;
632 }
633
634 // Size of diagonal axis.
635 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
636
637 // Size factors.
638 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
639 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
640 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
641 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
642 } else {
643 mSizeScale = 0.0f;
644 }
645
646 mOrientedRanges.haveTouchSize = true;
647 mOrientedRanges.haveToolSize = true;
648 mOrientedRanges.haveSize = true;
649
650 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
651 mOrientedRanges.touchMajor.source = mSource;
652 mOrientedRanges.touchMajor.min = 0;
653 mOrientedRanges.touchMajor.max = diagonalSize;
654 mOrientedRanges.touchMajor.flat = 0;
655 mOrientedRanges.touchMajor.fuzz = 0;
656 mOrientedRanges.touchMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800657 if (mRawPointerAxes.touchMajor.valid) {
658 mRawPointerAxes.touchMajor.resolution =
659 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
660 mOrientedRanges.touchMajor.resolution = mRawPointerAxes.touchMajor.resolution;
661 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800662
663 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
664 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800665 if (mRawPointerAxes.touchMinor.valid) {
666 mRawPointerAxes.touchMinor.resolution =
667 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
668 mOrientedRanges.touchMinor.resolution = mRawPointerAxes.touchMinor.resolution;
669 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800670
671 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
672 mOrientedRanges.toolMajor.source = mSource;
673 mOrientedRanges.toolMajor.min = 0;
674 mOrientedRanges.toolMajor.max = diagonalSize;
675 mOrientedRanges.toolMajor.flat = 0;
676 mOrientedRanges.toolMajor.fuzz = 0;
677 mOrientedRanges.toolMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800678 if (mRawPointerAxes.toolMajor.valid) {
679 mRawPointerAxes.toolMajor.resolution =
680 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
681 mOrientedRanges.toolMajor.resolution = mRawPointerAxes.toolMajor.resolution;
682 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800683
684 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
685 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800686 if (mRawPointerAxes.toolMinor.valid) {
687 mRawPointerAxes.toolMinor.resolution =
688 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
689 mOrientedRanges.toolMinor.resolution = mRawPointerAxes.toolMinor.resolution;
690 }
691
692 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
693 mOrientedRanges.touchMajor.resolution *= mGeometricScale;
694 mOrientedRanges.touchMinor.resolution *= mGeometricScale;
695 mOrientedRanges.toolMajor.resolution *= mGeometricScale;
696 mOrientedRanges.toolMinor.resolution *= mGeometricScale;
697 } else {
698 // Support for other calibrations can be added here.
699 ALOGW("%s calibration is not supported for size ranges at the moment. "
700 "Using raw resolution instead",
701 ftl::enum_string(mCalibration.sizeCalibration).c_str());
702 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800703
704 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
705 mOrientedRanges.size.source = mSource;
706 mOrientedRanges.size.min = 0;
707 mOrientedRanges.size.max = 1.0;
708 mOrientedRanges.size.flat = 0;
709 mOrientedRanges.size.fuzz = 0;
710 mOrientedRanges.size.resolution = 0;
711}
712
713void TouchInputMapper::initializeOrientedRanges() {
714 // Configure X and Y factors.
715 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
716 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
717 mXPrecision = 1.0f / mXScale;
718 mYPrecision = 1.0f / mYScale;
719
720 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
721 mOrientedRanges.x.source = mSource;
722 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
723 mOrientedRanges.y.source = mSource;
724
725 // Scale factor for terms that are not oriented in a particular axis.
726 // If the pixels are square then xScale == yScale otherwise we fake it
727 // by choosing an average.
728 mGeometricScale = avg(mXScale, mYScale);
729
730 initializeSizeRanges();
731
732 // Pressure factors.
733 mPressureScale = 0;
734 float pressureMax = 1.0;
735 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
736 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
737 if (mCalibration.havePressureScale) {
738 mPressureScale = mCalibration.pressureScale;
739 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
740 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
741 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
742 }
743 }
744
745 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
746 mOrientedRanges.pressure.source = mSource;
747 mOrientedRanges.pressure.min = 0;
748 mOrientedRanges.pressure.max = pressureMax;
749 mOrientedRanges.pressure.flat = 0;
750 mOrientedRanges.pressure.fuzz = 0;
751 mOrientedRanges.pressure.resolution = 0;
752
753 // Tilt
754 mTiltXCenter = 0;
755 mTiltXScale = 0;
756 mTiltYCenter = 0;
757 mTiltYScale = 0;
758 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
759 if (mHaveTilt) {
760 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
761 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
762 mTiltXScale = M_PI / 180;
763 mTiltYScale = M_PI / 180;
764
765 if (mRawPointerAxes.tiltX.resolution) {
766 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
767 }
768 if (mRawPointerAxes.tiltY.resolution) {
769 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
770 }
771
772 mOrientedRanges.haveTilt = true;
773
774 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
775 mOrientedRanges.tilt.source = mSource;
776 mOrientedRanges.tilt.min = 0;
777 mOrientedRanges.tilt.max = M_PI_2;
778 mOrientedRanges.tilt.flat = 0;
779 mOrientedRanges.tilt.fuzz = 0;
780 mOrientedRanges.tilt.resolution = 0;
781 }
782
783 // Orientation
784 mOrientationScale = 0;
785 if (mHaveTilt) {
786 mOrientedRanges.haveOrientation = true;
787
788 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
789 mOrientedRanges.orientation.source = mSource;
790 mOrientedRanges.orientation.min = -M_PI;
791 mOrientedRanges.orientation.max = M_PI;
792 mOrientedRanges.orientation.flat = 0;
793 mOrientedRanges.orientation.fuzz = 0;
794 mOrientedRanges.orientation.resolution = 0;
795 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
796 if (mCalibration.orientationCalibration ==
797 Calibration::OrientationCalibration::INTERPOLATED) {
798 if (mRawPointerAxes.orientation.valid) {
799 if (mRawPointerAxes.orientation.maxValue > 0) {
800 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
801 } else if (mRawPointerAxes.orientation.minValue < 0) {
802 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
803 } else {
804 mOrientationScale = 0;
805 }
806 }
807 }
808
809 mOrientedRanges.haveOrientation = true;
810
811 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
812 mOrientedRanges.orientation.source = mSource;
813 mOrientedRanges.orientation.min = -M_PI_2;
814 mOrientedRanges.orientation.max = M_PI_2;
815 mOrientedRanges.orientation.flat = 0;
816 mOrientedRanges.orientation.fuzz = 0;
817 mOrientedRanges.orientation.resolution = 0;
818 }
819
820 // Distance
821 mDistanceScale = 0;
822 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
823 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
824 if (mCalibration.haveDistanceScale) {
825 mDistanceScale = mCalibration.distanceScale;
826 } else {
827 mDistanceScale = 1.0f;
828 }
829 }
830
831 mOrientedRanges.haveDistance = true;
832
833 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
834 mOrientedRanges.distance.source = mSource;
835 mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
836 mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
837 mOrientedRanges.distance.flat = 0;
838 mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
839 mOrientedRanges.distance.resolution = 0;
840 }
841
842 // Compute oriented precision, scales and ranges.
843 // Note that the maximum value reported is an inclusive maximum value so it is one
844 // unit less than the total width or height of the display.
845 switch (mInputDeviceOrientation) {
846 case DISPLAY_ORIENTATION_90:
847 case DISPLAY_ORIENTATION_270:
848 mOrientedXPrecision = mYPrecision;
849 mOrientedYPrecision = mXPrecision;
850
851 mOrientedRanges.x.min = 0;
852 mOrientedRanges.x.max = mDisplayHeight - 1;
853 mOrientedRanges.x.flat = 0;
854 mOrientedRanges.x.fuzz = 0;
855 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
856
857 mOrientedRanges.y.min = 0;
858 mOrientedRanges.y.max = mDisplayWidth - 1;
859 mOrientedRanges.y.flat = 0;
860 mOrientedRanges.y.fuzz = 0;
861 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
862 break;
863
864 default:
865 mOrientedXPrecision = mXPrecision;
866 mOrientedYPrecision = mYPrecision;
867
868 mOrientedRanges.x.min = 0;
869 mOrientedRanges.x.max = mDisplayWidth - 1;
870 mOrientedRanges.x.flat = 0;
871 mOrientedRanges.x.fuzz = 0;
872 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
873
874 mOrientedRanges.y.min = 0;
875 mOrientedRanges.y.max = mDisplayHeight - 1;
876 mOrientedRanges.y.flat = 0;
877 mOrientedRanges.y.fuzz = 0;
878 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
879 break;
880 }
881}
882
Prabir Pradhan1728b212021-10-19 16:00:03 -0700883void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100884 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700885
886 resolveExternalStylusPresence();
887
888 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100889 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000890 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700891 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100892 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700893 if (hasStylus()) {
894 mSource |= AINPUT_SOURCE_STYLUS;
895 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800896 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700897 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100898 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700899 if (hasStylus()) {
900 mSource |= AINPUT_SOURCE_STYLUS;
901 }
902 if (hasExternalStylus()) {
903 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
904 }
Michael Wright227c5542020-07-02 18:30:52 +0100905 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700906 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100907 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700908 } else {
909 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100910 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700911 }
912
913 // Ensure we have valid X and Y axes.
914 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
915 ALOGW("Touch device '%s' did not report support for X or Y axis! "
916 "The device will be inoperable.",
917 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100918 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700919 return;
920 }
921
922 // Get associated display dimensions.
923 std::optional<DisplayViewport> newViewport = findViewport();
924 if (!newViewport) {
925 ALOGI("Touch device '%s' could not query the properties of its associated "
926 "display. The device will be inoperable until the display size "
927 "becomes available.",
928 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100929 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700930 return;
931 }
932
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000933 if (!newViewport->isActive) {
934 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
935 getDeviceName().c_str(), getDeviceId());
936 mDeviceMode = DeviceMode::DISABLED;
937 return;
938 }
939
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700940 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700941 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
942 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700943
Prabir Pradhan1728b212021-10-19 16:00:03 -0700944 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700945 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700946 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700947 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
lilinnane74b35f2022-07-19 16:00:50 +0800948 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport->displayId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700949 mViewport = *newViewport;
950
Michael Wright227c5542020-07-02 18:30:52 +0100951 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700952 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700953 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
954 int32_t naturalPhysicalLeft, naturalPhysicalTop;
955 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700956
Prabir Pradhan1728b212021-10-19 16:00:03 -0700957 // Apply the inverse of the input device orientation so that the input device is
958 // configured in the same orientation as the viewport. The input device orientation will
959 // be re-applied by mInputDeviceOrientation.
960 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700961 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700962 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700963 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700964 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
965 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800966 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700967 naturalPhysicalTop = mViewport.physicalLeft;
968 naturalDeviceWidth = mViewport.deviceHeight;
969 naturalDeviceHeight = mViewport.deviceWidth;
970 break;
971 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700972 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
973 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
974 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
975 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
976 naturalDeviceWidth = mViewport.deviceWidth;
977 naturalDeviceHeight = mViewport.deviceHeight;
978 break;
979 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700980 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
981 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
982 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800983 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700984 naturalDeviceWidth = mViewport.deviceHeight;
985 naturalDeviceHeight = mViewport.deviceWidth;
986 break;
987 case DISPLAY_ORIENTATION_0:
988 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700989 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
990 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
991 naturalPhysicalLeft = mViewport.physicalLeft;
992 naturalPhysicalTop = mViewport.physicalTop;
993 naturalDeviceWidth = mViewport.deviceWidth;
994 naturalDeviceHeight = mViewport.deviceHeight;
995 break;
996 }
997
998 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
999 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
1000 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
1001 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1002 }
1003
1004 mPhysicalWidth = naturalPhysicalWidth;
1005 mPhysicalHeight = naturalPhysicalHeight;
1006 mPhysicalLeft = naturalPhysicalLeft;
1007 mPhysicalTop = naturalPhysicalTop;
1008
Prabir Pradhan1728b212021-10-19 16:00:03 -07001009 const int32_t oldDisplayWidth = mDisplayWidth;
1010 const int32_t oldDisplayHeight = mDisplayHeight;
1011 mDisplayWidth = naturalDeviceWidth;
1012 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001013
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001014 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1015 // anything if the device is already orientation-aware. If the device is not
1016 // orientation-aware, then we need to apply the inverse rotation of the display so that
1017 // when the display rotation is applied later as a part of the per-window transform, we
1018 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001019 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001020 ? DISPLAY_ORIENTATION_0
1021 : getInverseRotation(mViewport.orientation);
1022 // For orientation-aware devices that work in the un-rotated coordinate space, the
1023 // viewport update should be skipped if it is only a change in the orientation.
lilinnane74b35f2022-07-19 16:00:50 +08001024 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
1025 mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight &&
1026 viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001027
1028 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001029 mInputDeviceOrientation =
1030 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001031 } else {
1032 mPhysicalWidth = rawWidth;
1033 mPhysicalHeight = rawHeight;
1034 mPhysicalLeft = 0;
1035 mPhysicalTop = 0;
1036
Prabir Pradhan1728b212021-10-19 16:00:03 -07001037 mDisplayWidth = rawWidth;
1038 mDisplayHeight = rawHeight;
1039 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001040 }
1041 }
1042
1043 // If moving between pointer modes, need to reset some state.
1044 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1045 if (deviceModeChanged) {
1046 mOrientedRanges.clear();
1047 }
1048
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001049 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1050 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001051 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001052 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001053 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1054 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001055 if (mPointerController == nullptr) {
1056 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001057 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001058 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001059 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1060 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001061 } else {
Michael Wright17db18e2020-06-26 20:51:44 +01001062 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001063 }
1064
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001065 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001066 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1067 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001068 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1069 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001070
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001071 configureVirtualKeys();
1072
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001073 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001074
1075 // Location
1076 updateAffineTransformation();
1077
Michael Wright227c5542020-07-02 18:30:52 +01001078 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001079 // Compute pointer gesture detection parameters.
1080 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001081 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001082
1083 // Scale movements such that one whole swipe of the touch pad covers a
1084 // given area relative to the diagonal size of the display when no acceleration
1085 // is applied.
1086 // Assume that the touch pad has a square aspect ratio such that movements in
1087 // X and Y of the same number of raw units cover the same physical distance.
1088 mPointerXMovementScale =
1089 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1090 mPointerYMovementScale = mPointerXMovementScale;
1091
1092 // Scale zooms to cover a smaller range of the display than movements do.
1093 // This value determines the area around the pointer that is affected by freeform
1094 // pointer gestures.
1095 mPointerXZoomScale =
1096 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1097 mPointerYZoomScale = mPointerXZoomScale;
1098
1099 // Max width between pointers to detect a swipe gesture is more than some fraction
1100 // of the diagonal axis of the touch pad. Touches that are wider than this are
1101 // translated into freeform gestures.
1102 mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
1103
1104 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001105 const nsecs_t readTime = when; // synthetic event
1106 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001107 }
1108
1109 // Inform the dispatcher about the changes.
1110 *outResetNeeded = true;
1111 bumpGeneration();
1112 }
1113}
1114
Prabir Pradhan1728b212021-10-19 16:00:03 -07001115void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001116 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001117 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1118 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001119 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1120 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1121 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1122 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001123 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001124}
1125
1126void TouchInputMapper::configureVirtualKeys() {
1127 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001128 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001129
1130 mVirtualKeys.clear();
1131
1132 if (virtualKeyDefinitions.size() == 0) {
1133 return;
1134 }
1135
1136 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1137 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1138 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1139 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1140
1141 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1142 VirtualKey virtualKey;
1143
1144 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1145 int32_t keyCode;
1146 int32_t dummyKeyMetaState;
1147 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001148 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1149 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001150 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1151 continue; // drop the key
1152 }
1153
1154 virtualKey.keyCode = keyCode;
1155 virtualKey.flags = flags;
1156
1157 // convert the key definition's display coordinates into touch coordinates for a hit box
1158 int32_t halfWidth = virtualKeyDefinition.width / 2;
1159 int32_t halfHeight = virtualKeyDefinition.height / 2;
1160
1161 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001162 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001163 touchScreenLeft;
1164 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001165 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001167 virtualKey.hitTop =
1168 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001169 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001170 virtualKey.hitBottom =
1171 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001172 touchScreenTop;
1173 mVirtualKeys.push_back(virtualKey);
1174 }
1175}
1176
1177void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1178 if (!mVirtualKeys.empty()) {
1179 dump += INDENT3 "Virtual Keys:\n";
1180
1181 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1182 const VirtualKey& virtualKey = mVirtualKeys[i];
1183 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1184 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1185 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1186 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1187 }
1188 }
1189}
1190
1191void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001192 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001193 Calibration& out = mCalibration;
1194
1195 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001196 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001197 String8 sizeCalibrationString;
1198 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
1199 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001200 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001201 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001202 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001203 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001204 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001205 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001206 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001207 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001208 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001209 } else if (sizeCalibrationString != "default") {
1210 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string());
1211 }
1212 }
1213
1214 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale);
1215 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias);
1216 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed);
1217
1218 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001219 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001220 String8 pressureCalibrationString;
1221 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
1222 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001223 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001224 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001225 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001226 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001227 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001228 } else if (pressureCalibrationString != "default") {
1229 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
1230 pressureCalibrationString.string());
1231 }
1232 }
1233
1234 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale);
1235
1236 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001237 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001238 String8 orientationCalibrationString;
1239 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
1240 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001241 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001242 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001243 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001244 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001245 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001246 } else if (orientationCalibrationString != "default") {
1247 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
1248 orientationCalibrationString.string());
1249 }
1250 }
1251
1252 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001253 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001254 String8 distanceCalibrationString;
1255 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
1256 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001257 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001258 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001259 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260 } else if (distanceCalibrationString != "default") {
1261 ALOGW("Invalid value for touch.distance.calibration: '%s'",
1262 distanceCalibrationString.string());
1263 }
1264 }
1265
1266 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale);
1267
Michael Wright227c5542020-07-02 18:30:52 +01001268 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001269 String8 coverageCalibrationString;
1270 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
1271 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001272 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001273 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001274 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001275 } else if (coverageCalibrationString != "default") {
1276 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
1277 coverageCalibrationString.string());
1278 }
1279 }
1280}
1281
1282void TouchInputMapper::resolveCalibration() {
1283 // Size
1284 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001285 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1286 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001287 }
1288 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001289 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001290 }
1291
1292 // Pressure
1293 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001294 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1295 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001296 }
1297 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001298 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001299 }
1300
1301 // Orientation
1302 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001303 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1304 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001305 }
1306 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001307 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001308 }
1309
1310 // Distance
1311 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001312 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1313 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001314 }
1315 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001316 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001317 }
1318
1319 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001320 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1321 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001322 }
1323}
1324
1325void TouchInputMapper::dumpCalibration(std::string& dump) {
1326 dump += INDENT3 "Calibration:\n";
1327
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001328 dump += INDENT4 "touch.size.calibration: ";
1329 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001330
1331 if (mCalibration.haveSizeScale) {
1332 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
1333 }
1334
1335 if (mCalibration.haveSizeBias) {
1336 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
1337 }
1338
1339 if (mCalibration.haveSizeIsSummed) {
1340 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
1341 toString(mCalibration.sizeIsSummed));
1342 }
1343
1344 // Pressure
1345 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001346 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001347 dump += INDENT4 "touch.pressure.calibration: none\n";
1348 break;
Michael Wright227c5542020-07-02 18:30:52 +01001349 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001350 dump += INDENT4 "touch.pressure.calibration: physical\n";
1351 break;
Michael Wright227c5542020-07-02 18:30:52 +01001352 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001353 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1354 break;
1355 default:
1356 ALOG_ASSERT(false);
1357 }
1358
1359 if (mCalibration.havePressureScale) {
1360 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
1361 }
1362
1363 // Orientation
1364 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001365 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001366 dump += INDENT4 "touch.orientation.calibration: none\n";
1367 break;
Michael Wright227c5542020-07-02 18:30:52 +01001368 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001369 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1370 break;
Michael Wright227c5542020-07-02 18:30:52 +01001371 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001372 dump += INDENT4 "touch.orientation.calibration: vector\n";
1373 break;
1374 default:
1375 ALOG_ASSERT(false);
1376 }
1377
1378 // Distance
1379 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001380 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001381 dump += INDENT4 "touch.distance.calibration: none\n";
1382 break;
Michael Wright227c5542020-07-02 18:30:52 +01001383 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001384 dump += INDENT4 "touch.distance.calibration: scaled\n";
1385 break;
1386 default:
1387 ALOG_ASSERT(false);
1388 }
1389
1390 if (mCalibration.haveDistanceScale) {
1391 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
1392 }
1393
1394 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001395 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001396 dump += INDENT4 "touch.coverage.calibration: none\n";
1397 break;
Michael Wright227c5542020-07-02 18:30:52 +01001398 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001399 dump += INDENT4 "touch.coverage.calibration: box\n";
1400 break;
1401 default:
1402 ALOG_ASSERT(false);
1403 }
1404}
1405
1406void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1407 dump += INDENT3 "Affine Transformation:\n";
1408
1409 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1410 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1411 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1412 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1413 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1414 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1415}
1416
1417void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001418 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001419 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001420}
1421
1422void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001423 mCursorButtonAccumulator.reset(getDeviceContext());
1424 mCursorScrollAccumulator.reset(getDeviceContext());
1425 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001426
1427 mPointerVelocityControl.reset();
1428 mWheelXVelocityControl.reset();
1429 mWheelYVelocityControl.reset();
1430
1431 mRawStatesPending.clear();
1432 mCurrentRawState.clear();
1433 mCurrentCookedState.clear();
1434 mLastRawState.clear();
1435 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001436 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001437 mSentHoverEnter = false;
1438 mHavePointerIds = false;
1439 mCurrentMotionAborted = false;
1440 mDownTime = 0;
1441
1442 mCurrentVirtualKey.down = false;
1443
1444 mPointerGesture.reset();
1445 mPointerSimple.reset();
1446 resetExternalStylus();
1447
1448 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001449 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001450 mPointerController->clearSpots();
1451 }
1452
1453 InputMapper::reset(when);
1454}
1455
1456void TouchInputMapper::resetExternalStylus() {
1457 mExternalStylusState.clear();
1458 mExternalStylusId = -1;
1459 mExternalStylusFusionTimeout = LLONG_MAX;
1460 mExternalStylusDataPending = false;
1461}
1462
1463void TouchInputMapper::clearStylusDataPendingFlags() {
1464 mExternalStylusDataPending = false;
1465 mExternalStylusFusionTimeout = LLONG_MAX;
1466}
1467
1468void TouchInputMapper::process(const RawEvent* rawEvent) {
1469 mCursorButtonAccumulator.process(rawEvent);
1470 mCursorScrollAccumulator.process(rawEvent);
1471 mTouchButtonAccumulator.process(rawEvent);
1472
1473 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001474 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001475 }
1476}
1477
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001478void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001479 // Push a new state.
1480 mRawStatesPending.emplace_back();
1481
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001482 RawState& next = mRawStatesPending.back();
1483 next.clear();
1484 next.when = when;
1485 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001486
1487 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001488 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001489 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1490
1491 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001492 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1493 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001494 mCursorScrollAccumulator.finishSync();
1495
1496 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001497 syncTouch(when, &next);
1498
1499 // The last RawState is the actually second to last, since we just added a new state
1500 const RawState& last =
1501 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001502
1503 // Assign pointer ids.
1504 if (!mHavePointerIds) {
1505 assignPointerIds(last, next);
1506 }
1507
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001508 if (DEBUG_RAW_EVENTS) {
1509 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1510 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1511 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1512 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1513 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1514 next.rawPointerData.canceledIdBits.value);
1515 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001516
Arthur Hung9ad18942021-06-19 02:04:46 +00001517 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1518 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1519 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1520 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1521 next.rawPointerData.hoveringIdBits.value);
1522 }
1523
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001524 processRawTouches(false /*timeout*/);
1525}
1526
1527void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001528 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001529 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001530 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001531 mCurrentCookedState.clear();
1532 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001533 return;
1534 }
1535
1536 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1537 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1538 // touching the current state will only observe the events that have been dispatched to the
1539 // rest of the pipeline.
1540 const size_t N = mRawStatesPending.size();
1541 size_t count;
1542 for (count = 0; count < N; count++) {
1543 const RawState& next = mRawStatesPending[count];
1544
1545 // A failure to assign the stylus id means that we're waiting on stylus data
1546 // and so should defer the rest of the pipeline.
1547 if (assignExternalStylusId(next, timeout)) {
1548 break;
1549 }
1550
1551 // All ready to go.
1552 clearStylusDataPendingFlags();
1553 mCurrentRawState.copyFrom(next);
1554 if (mCurrentRawState.when < mLastRawState.when) {
1555 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001556 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001557 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001558 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001559 }
1560 if (count != 0) {
1561 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1562 }
1563
1564 if (mExternalStylusDataPending) {
1565 if (timeout) {
1566 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1567 clearStylusDataPendingFlags();
1568 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001569 if (DEBUG_STYLUS_FUSION) {
1570 ALOGD("Timeout expired, synthesizing event with new stylus data");
1571 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001572 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1573 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001574 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1575 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1576 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1577 }
1578 }
1579}
1580
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001581void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001582 // Always start with a clean state.
1583 mCurrentCookedState.clear();
1584
1585 // Apply stylus buttons to current raw state.
1586 applyExternalStylusButtonState(when);
1587
1588 // Handle policy on initial down or hover events.
1589 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1590 mCurrentRawState.rawPointerData.pointerCount != 0;
1591
1592 uint32_t policyFlags = 0;
1593 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1594 if (initialDown || buttonsPressed) {
1595 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001596 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001597 getContext()->fadePointer();
1598 }
1599
1600 if (mParameters.wake) {
1601 policyFlags |= POLICY_FLAG_WAKE;
1602 }
1603 }
1604
1605 // Consume raw off-screen touches before cooking pointer data.
1606 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001607 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001608 mCurrentRawState.rawPointerData.clear();
1609 }
1610
1611 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1612 // with cooked pointer data that has the same ids and indices as the raw data.
1613 // The following code can use either the raw or cooked data, as needed.
1614 cookPointerData();
1615
1616 // Apply stylus pressure to current cooked state.
1617 applyExternalStylusTouchState(when);
1618
1619 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001620 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1621 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001622 mCurrentCookedState.buttonState);
1623
1624 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001625 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001626 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1627 uint32_t id = idBits.clearFirstMarkedBit();
1628 const RawPointerData::Pointer& pointer =
1629 mCurrentRawState.rawPointerData.pointerForId(id);
1630 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1631 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1632 mCurrentCookedState.stylusIdBits.markBit(id);
1633 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1634 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1635 mCurrentCookedState.fingerIdBits.markBit(id);
1636 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1637 mCurrentCookedState.mouseIdBits.markBit(id);
1638 }
1639 }
1640 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1641 uint32_t id = idBits.clearFirstMarkedBit();
1642 const RawPointerData::Pointer& pointer =
1643 mCurrentRawState.rawPointerData.pointerForId(id);
1644 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1645 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1646 mCurrentCookedState.stylusIdBits.markBit(id);
1647 }
1648 }
1649
1650 // Stylus takes precedence over all tools, then mouse, then finger.
1651 PointerUsage pointerUsage = mPointerUsage;
1652 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1653 mCurrentCookedState.mouseIdBits.clear();
1654 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001655 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1657 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001658 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001659 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1660 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001661 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001662 }
1663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001664 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001665 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001666 if (!mCurrentMotionAborted) {
Prabir Pradhand4206712022-04-27 13:19:15 +00001667 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001668 dispatchButtonRelease(when, readTime, policyFlags);
1669 dispatchHoverExit(when, readTime, policyFlags);
1670 dispatchTouches(when, readTime, policyFlags);
1671 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1672 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001673 }
1674
1675 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1676 mCurrentMotionAborted = false;
1677 }
1678 }
1679
1680 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001681 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001682 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1683 mCurrentCookedState.buttonState);
1684
1685 // Clear some transient state.
1686 mCurrentRawState.rawVScroll = 0;
1687 mCurrentRawState.rawHScroll = 0;
1688
1689 // Copy current touch to last touch in preparation for the next cycle.
1690 mLastRawState.copyFrom(mCurrentRawState);
1691 mLastCookedState.copyFrom(mCurrentCookedState);
1692}
1693
Garfield Tanc734e4f2021-01-15 20:01:39 -08001694void TouchInputMapper::updateTouchSpots() {
1695 if (!mConfig.showTouches || mPointerController == nullptr) {
1696 return;
1697 }
1698
1699 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1700 // clear touch spots.
1701 if (mDeviceMode != DeviceMode::DIRECT &&
1702 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1703 return;
1704 }
1705
1706 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1707 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1708
1709 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001710 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1711 mCurrentCookedState.cookedPointerData.idToIndex,
1712 mCurrentCookedState.cookedPointerData.touchingIdBits,
1713 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001714}
1715
1716bool TouchInputMapper::isTouchScreen() {
1717 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1718 mParameters.hasAssociatedDisplay;
1719}
1720
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001721void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001722 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001723 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1724 }
1725}
1726
1727void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1728 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1729 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1730
1731 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1732 float pressure = mExternalStylusState.pressure;
1733 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1734 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1735 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1736 }
1737 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1738 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1739
1740 PointerProperties& properties =
1741 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1742 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1743 properties.toolType = mExternalStylusState.toolType;
1744 }
1745 }
1746}
1747
1748bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001749 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001750 return false;
1751 }
1752
1753 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1754 state.rawPointerData.pointerCount != 0;
1755 if (initialDown) {
1756 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001757 if (DEBUG_STYLUS_FUSION) {
1758 ALOGD("Have both stylus and touch data, beginning fusion");
1759 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001760 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1761 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001762 if (DEBUG_STYLUS_FUSION) {
1763 ALOGD("Timeout expired, assuming touch is not a stylus.");
1764 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001765 resetExternalStylus();
1766 } else {
1767 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1768 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1769 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001770 if (DEBUG_STYLUS_FUSION) {
1771 ALOGD("No stylus data but stylus is connected, requesting timeout "
1772 "(%" PRId64 "ms)",
1773 mExternalStylusFusionTimeout);
1774 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001775 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1776 return true;
1777 }
1778 }
1779
1780 // Check if the stylus pointer has gone up.
1781 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001782 if (DEBUG_STYLUS_FUSION) {
1783 ALOGD("Stylus pointer is going up");
1784 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001785 mExternalStylusId = -1;
1786 }
1787
1788 return false;
1789}
1790
1791void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001792 if (mDeviceMode == DeviceMode::POINTER) {
1793 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001794 // Since this is a synthetic event, we can consider its latency to be zero
1795 const nsecs_t readTime = when;
1796 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001797 }
Michael Wright227c5542020-07-02 18:30:52 +01001798 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001799 if (mExternalStylusFusionTimeout < when) {
1800 processRawTouches(true /*timeout*/);
1801 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1802 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1803 }
1804 }
1805}
1806
1807void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1808 mExternalStylusState.copyFrom(state);
1809 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1810 // We're either in the middle of a fused stream of data or we're waiting on data before
1811 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1812 // data.
1813 mExternalStylusDataPending = true;
1814 processRawTouches(false /*timeout*/);
1815 }
1816}
1817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001818bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001819 // Check for release of a virtual key.
1820 if (mCurrentVirtualKey.down) {
1821 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1822 // Pointer went up while virtual key was down.
1823 mCurrentVirtualKey.down = false;
1824 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001825 if (DEBUG_VIRTUAL_KEYS) {
1826 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1827 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1828 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001829 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001830 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1831 }
1832 return true;
1833 }
1834
1835 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1836 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1837 const RawPointerData::Pointer& pointer =
1838 mCurrentRawState.rawPointerData.pointerForId(id);
1839 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1840 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1841 // Pointer is still within the space of the virtual key.
1842 return true;
1843 }
1844 }
1845
1846 // Pointer left virtual key area or another pointer also went down.
1847 // Send key cancellation but do not consume the touch yet.
1848 // This is useful when the user swipes through from the virtual key area
1849 // into the main display surface.
1850 mCurrentVirtualKey.down = false;
1851 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001852 if (DEBUG_VIRTUAL_KEYS) {
1853 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1854 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1855 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001856 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001857 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1858 AKEY_EVENT_FLAG_CANCELED);
1859 }
1860 }
1861
1862 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1863 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1864 // Pointer just went down. Check for virtual key press or off-screen touches.
1865 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1866 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001867 // Skip checking whether the pointer is inside the physical frame if the device is in
1868 // unscaled mode.
1869 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1870 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001871 // If exactly one pointer went down, check for virtual key hit.
1872 // Otherwise we will drop the entire stroke.
1873 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1874 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1875 if (virtualKey) {
1876 mCurrentVirtualKey.down = true;
1877 mCurrentVirtualKey.downTime = when;
1878 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1879 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1880 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001881 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1882 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001883
1884 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001885 if (DEBUG_VIRTUAL_KEYS) {
1886 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1887 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1888 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001889 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001890 AKEY_EVENT_FLAG_FROM_SYSTEM |
1891 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1892 }
1893 }
1894 }
1895 return true;
1896 }
1897 }
1898
1899 // Disable all virtual key touches that happen within a short time interval of the
1900 // most recent touch within the screen area. The idea is to filter out stray
1901 // virtual key presses when interacting with the touch screen.
1902 //
1903 // Problems we're trying to solve:
1904 //
1905 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1906 // virtual key area that is implemented by a separate touch panel and accidentally
1907 // triggers a virtual key.
1908 //
1909 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1910 // area and accidentally triggers a virtual key. This often happens when virtual keys
1911 // are layed out below the screen near to where the on screen keyboard's space bar
1912 // is displayed.
1913 if (mConfig.virtualKeyQuietTime > 0 &&
1914 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001915 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001916 }
1917 return false;
1918}
1919
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001920void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001921 int32_t keyEventAction, int32_t keyEventFlags) {
1922 int32_t keyCode = mCurrentVirtualKey.keyCode;
1923 int32_t scanCode = mCurrentVirtualKey.scanCode;
1924 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001925 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001926 policyFlags |= POLICY_FLAG_VIRTUAL;
1927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001928 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1929 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1930 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001931 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001932}
1933
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001934void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
lilinnane74b35f2022-07-19 16:00:50 +08001935 if (mCurrentMotionAborted) {
1936 // Current motion event was already aborted.
1937 return;
1938 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001939 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1940 if (!currentIdBits.isEmpty()) {
1941 int32_t metaState = getContext()->getGlobalMetaState();
1942 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001943 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1944 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001945 mCurrentCookedState.cookedPointerData.pointerProperties,
1946 mCurrentCookedState.cookedPointerData.pointerCoords,
1947 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1948 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1949 mCurrentMotionAborted = true;
1950 }
1951}
1952
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001953void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001954 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1955 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1956 int32_t metaState = getContext()->getGlobalMetaState();
1957 int32_t buttonState = mCurrentCookedState.buttonState;
1958
1959 if (currentIdBits == lastIdBits) {
1960 if (!currentIdBits.isEmpty()) {
1961 // No pointer id changes so this is a move event.
1962 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001963 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1964 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001965 mCurrentCookedState.cookedPointerData.pointerProperties,
1966 mCurrentCookedState.cookedPointerData.pointerCoords,
1967 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1968 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1969 }
1970 } else {
1971 // There may be pointers going up and pointers going down and pointers moving
1972 // all at the same time.
1973 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1974 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1975 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1976 BitSet32 dispatchedIdBits(lastIdBits.value);
1977
1978 // Update last coordinates of pointers that have moved so that we observe the new
1979 // pointer positions at the same time as other pointers that have just gone up.
1980 bool moveNeeded =
1981 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1982 mCurrentCookedState.cookedPointerData.pointerCoords,
1983 mCurrentCookedState.cookedPointerData.idToIndex,
1984 mLastCookedState.cookedPointerData.pointerProperties,
1985 mLastCookedState.cookedPointerData.pointerCoords,
1986 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1987 if (buttonState != mLastCookedState.buttonState) {
1988 moveNeeded = true;
1989 }
1990
1991 // Dispatch pointer up events.
1992 while (!upIdBits.isEmpty()) {
1993 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001994 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001995 if (isCanceled) {
1996 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1997 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001999 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002000 mLastCookedState.cookedPointerData.pointerProperties,
2001 mLastCookedState.cookedPointerData.pointerCoords,
2002 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
2003 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2004 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002005 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002006 }
2007
2008 // Dispatch move events if any of the remaining pointers moved from their old locations.
2009 // Although applications receive new locations as part of individual pointer up
2010 // events, they do not generally handle them except when presented in a move event.
2011 if (moveNeeded && !moveIdBits.isEmpty()) {
2012 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002013 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2014 metaState, buttonState, 0,
2015 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002016 mCurrentCookedState.cookedPointerData.pointerCoords,
2017 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2018 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2019 }
2020
2021 // Dispatch pointer down events using the new pointer locations.
2022 while (!downIdBits.isEmpty()) {
2023 uint32_t downId = downIdBits.clearFirstMarkedBit();
2024 dispatchedIdBits.markBit(downId);
2025
2026 if (dispatchedIdBits.count() == 1) {
2027 // First pointer is going down. Set down time.
2028 mDownTime = when;
2029 }
2030
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002031 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2032 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002033 mCurrentCookedState.cookedPointerData.pointerProperties,
2034 mCurrentCookedState.cookedPointerData.pointerCoords,
2035 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2036 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2037 }
2038 }
2039}
2040
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002041void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002042 if (mSentHoverEnter &&
2043 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2044 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2045 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002046 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2047 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002048 mLastCookedState.cookedPointerData.pointerProperties,
2049 mLastCookedState.cookedPointerData.pointerCoords,
2050 mLastCookedState.cookedPointerData.idToIndex,
2051 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2052 mOrientedYPrecision, mDownTime);
2053 mSentHoverEnter = false;
2054 }
2055}
2056
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002057void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2058 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002059 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2060 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2061 int32_t metaState = getContext()->getGlobalMetaState();
2062 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002063 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2064 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002065 mCurrentCookedState.cookedPointerData.pointerProperties,
2066 mCurrentCookedState.cookedPointerData.pointerCoords,
2067 mCurrentCookedState.cookedPointerData.idToIndex,
2068 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2069 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2070 mSentHoverEnter = true;
2071 }
2072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002073 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2074 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002075 mCurrentCookedState.cookedPointerData.pointerProperties,
2076 mCurrentCookedState.cookedPointerData.pointerCoords,
2077 mCurrentCookedState.cookedPointerData.idToIndex,
2078 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2079 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2080 }
2081}
2082
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002083void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002084 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2085 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2086 const int32_t metaState = getContext()->getGlobalMetaState();
2087 int32_t buttonState = mLastCookedState.buttonState;
2088 while (!releasedButtons.isEmpty()) {
2089 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2090 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002091 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002092 actionButton, 0, metaState, buttonState, 0,
2093 mCurrentCookedState.cookedPointerData.pointerProperties,
2094 mCurrentCookedState.cookedPointerData.pointerCoords,
2095 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2096 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2097 }
2098}
2099
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002100void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002101 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2102 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2103 const int32_t metaState = getContext()->getGlobalMetaState();
2104 int32_t buttonState = mLastCookedState.buttonState;
2105 while (!pressedButtons.isEmpty()) {
2106 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2107 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002108 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2109 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002110 mCurrentCookedState.cookedPointerData.pointerProperties,
2111 mCurrentCookedState.cookedPointerData.pointerCoords,
2112 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2113 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2114 }
2115}
2116
2117const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2118 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2119 return cookedPointerData.touchingIdBits;
2120 }
2121 return cookedPointerData.hoveringIdBits;
2122}
2123
2124void TouchInputMapper::cookPointerData() {
2125 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2126
2127 mCurrentCookedState.cookedPointerData.clear();
2128 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2129 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2130 mCurrentRawState.rawPointerData.hoveringIdBits;
2131 mCurrentCookedState.cookedPointerData.touchingIdBits =
2132 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002133 mCurrentCookedState.cookedPointerData.canceledIdBits =
2134 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002135
2136 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2137 mCurrentCookedState.buttonState = 0;
2138 } else {
2139 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2140 }
2141
2142 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002143 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002144 for (uint32_t i = 0; i < currentPointerCount; i++) {
2145 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2146
2147 // Size
2148 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2149 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002150 case Calibration::SizeCalibration::GEOMETRIC:
2151 case Calibration::SizeCalibration::DIAMETER:
2152 case Calibration::SizeCalibration::BOX:
2153 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002154 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2155 touchMajor = in.touchMajor;
2156 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2157 toolMajor = in.toolMajor;
2158 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2159 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2160 : in.touchMajor;
2161 } else if (mRawPointerAxes.touchMajor.valid) {
2162 toolMajor = touchMajor = in.touchMajor;
2163 toolMinor = touchMinor =
2164 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2165 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2166 : in.touchMajor;
2167 } else if (mRawPointerAxes.toolMajor.valid) {
2168 touchMajor = toolMajor = in.toolMajor;
2169 touchMinor = toolMinor =
2170 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2171 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2172 : in.toolMajor;
2173 } else {
2174 ALOG_ASSERT(false,
2175 "No touch or tool axes. "
2176 "Size calibration should have been resolved to NONE.");
2177 touchMajor = 0;
2178 touchMinor = 0;
2179 toolMajor = 0;
2180 toolMinor = 0;
2181 size = 0;
2182 }
2183
2184 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2185 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2186 if (touchingCount > 1) {
2187 touchMajor /= touchingCount;
2188 touchMinor /= touchingCount;
2189 toolMajor /= touchingCount;
2190 toolMinor /= touchingCount;
2191 size /= touchingCount;
2192 }
2193 }
2194
Michael Wright227c5542020-07-02 18:30:52 +01002195 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002196 touchMajor *= mGeometricScale;
2197 touchMinor *= mGeometricScale;
2198 toolMajor *= mGeometricScale;
2199 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002200 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002201 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2202 touchMinor = touchMajor;
2203 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2204 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002205 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002206 touchMinor = touchMajor;
2207 toolMinor = toolMajor;
2208 }
2209
2210 mCalibration.applySizeScaleAndBias(&touchMajor);
2211 mCalibration.applySizeScaleAndBias(&touchMinor);
2212 mCalibration.applySizeScaleAndBias(&toolMajor);
2213 mCalibration.applySizeScaleAndBias(&toolMinor);
2214 size *= mSizeScale;
2215 break;
2216 default:
2217 touchMajor = 0;
2218 touchMinor = 0;
2219 toolMajor = 0;
2220 toolMinor = 0;
2221 size = 0;
2222 break;
2223 }
2224
2225 // Pressure
2226 float pressure;
2227 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002228 case Calibration::PressureCalibration::PHYSICAL:
2229 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002230 pressure = in.pressure * mPressureScale;
2231 break;
2232 default:
2233 pressure = in.isHovering ? 0 : 1;
2234 break;
2235 }
2236
2237 // Tilt and Orientation
2238 float tilt;
2239 float orientation;
2240 if (mHaveTilt) {
2241 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2242 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2243 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2244 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2245 } else {
2246 tilt = 0;
2247
2248 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002249 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002250 orientation = in.orientation * mOrientationScale;
2251 break;
Michael Wright227c5542020-07-02 18:30:52 +01002252 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002253 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2254 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2255 if (c1 != 0 || c2 != 0) {
2256 orientation = atan2f(c1, c2) * 0.5f;
2257 float confidence = hypotf(c1, c2);
2258 float scale = 1.0f + confidence / 16.0f;
2259 touchMajor *= scale;
2260 touchMinor /= scale;
2261 toolMajor *= scale;
2262 toolMinor /= scale;
2263 } else {
2264 orientation = 0;
2265 }
2266 break;
2267 }
2268 default:
2269 orientation = 0;
2270 }
2271 }
2272
2273 // Distance
2274 float distance;
2275 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002276 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002277 distance = in.distance * mDistanceScale;
2278 break;
2279 default:
2280 distance = 0;
2281 }
2282
2283 // Coverage
2284 int32_t rawLeft, rawTop, rawRight, rawBottom;
2285 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002286 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002287 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2288 rawRight = in.toolMinor & 0x0000ffff;
2289 rawBottom = in.toolMajor & 0x0000ffff;
2290 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2291 break;
2292 default:
2293 rawLeft = rawTop = rawRight = rawBottom = 0;
2294 break;
2295 }
2296
2297 // Adjust X,Y coords for device calibration
2298 // TODO: Adjust coverage coords?
2299 float xTransformed = in.x, yTransformed = in.y;
2300 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002301 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002302
Prabir Pradhan1728b212021-10-19 16:00:03 -07002303 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002304 float left, top, right, bottom;
2305
Prabir Pradhan1728b212021-10-19 16:00:03 -07002306 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002307 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002308 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2309 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2310 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2311 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002312 orientation -= M_PI_2;
2313 if (mOrientedRanges.haveOrientation &&
2314 orientation < mOrientedRanges.orientation.min) {
2315 orientation +=
2316 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2317 }
2318 break;
2319 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002320 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2321 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002322 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2323 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002324 orientation -= M_PI;
2325 if (mOrientedRanges.haveOrientation &&
2326 orientation < mOrientedRanges.orientation.min) {
2327 orientation +=
2328 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2329 }
2330 break;
2331 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002332 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2333 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002334 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2335 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002336 orientation += M_PI_2;
2337 if (mOrientedRanges.haveOrientation &&
2338 orientation > mOrientedRanges.orientation.max) {
2339 orientation -=
2340 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2341 }
2342 break;
2343 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002344 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2345 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2346 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2347 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002348 break;
2349 }
2350
2351 // Write output coords.
2352 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2353 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002354 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2355 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002356 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2357 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2358 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2359 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2360 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2361 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2362 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002363 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002364 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2365 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2366 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2367 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2368 } else {
2369 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2370 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2371 }
2372
Chris Ye364fdb52020-08-05 15:07:56 -07002373 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002374 uint32_t id = in.id;
2375 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2376 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2377 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2378 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2379 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2380 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2381 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2382 }
2383
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002384 // Write output properties.
2385 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002386 properties.clear();
2387 properties.id = id;
2388 properties.toolType = in.toolType;
2389
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002390 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002391 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002392 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002393 }
2394}
2395
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002396void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002397 PointerUsage pointerUsage) {
2398 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002399 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002400 mPointerUsage = pointerUsage;
2401 }
2402
2403 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002404 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002405 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002406 break;
Michael Wright227c5542020-07-02 18:30:52 +01002407 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002408 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002409 break;
Michael Wright227c5542020-07-02 18:30:52 +01002410 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002411 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002412 break;
Michael Wright227c5542020-07-02 18:30:52 +01002413 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002414 break;
2415 }
2416}
2417
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002418void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002419 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002420 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002421 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002422 break;
Michael Wright227c5542020-07-02 18:30:52 +01002423 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002424 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002425 break;
Michael Wright227c5542020-07-02 18:30:52 +01002426 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002427 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002428 break;
Michael Wright227c5542020-07-02 18:30:52 +01002429 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002430 break;
2431 }
2432
Michael Wright227c5542020-07-02 18:30:52 +01002433 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002434}
2435
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002436void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2437 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002438 // Update current gesture coordinates.
2439 bool cancelPreviousGesture, finishPreviousGesture;
2440 bool sendEvents =
2441 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2442 if (!sendEvents) {
2443 return;
2444 }
2445 if (finishPreviousGesture) {
2446 cancelPreviousGesture = false;
2447 }
2448
2449 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002450 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002451 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002452 if (finishPreviousGesture || cancelPreviousGesture) {
2453 mPointerController->clearSpots();
2454 }
2455
Michael Wright227c5542020-07-02 18:30:52 +01002456 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002457 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2458 mPointerGesture.currentGestureIdToIndex,
2459 mPointerGesture.currentGestureIdBits,
2460 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002461 }
2462 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002463 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002464 }
2465
2466 // Show or hide the pointer if needed.
2467 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002468 case PointerGesture::Mode::NEUTRAL:
2469 case PointerGesture::Mode::QUIET:
2470 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2471 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002472 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002473 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002474 }
2475 break;
Michael Wright227c5542020-07-02 18:30:52 +01002476 case PointerGesture::Mode::TAP:
2477 case PointerGesture::Mode::TAP_DRAG:
2478 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2479 case PointerGesture::Mode::HOVER:
2480 case PointerGesture::Mode::PRESS:
2481 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002482 // Unfade the pointer when the current gesture manipulates the
2483 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002484 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002485 break;
Michael Wright227c5542020-07-02 18:30:52 +01002486 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002487 // Fade the pointer when the current gesture manipulates a different
2488 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002489 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002490 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002491 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002492 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002493 }
2494 break;
2495 }
2496
2497 // Send events!
2498 int32_t metaState = getContext()->getGlobalMetaState();
2499 int32_t buttonState = mCurrentCookedState.buttonState;
2500
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002501 uint32_t flags = 0;
2502
2503 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2504 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2505 }
2506
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002507 // Update last coordinates of pointers that have moved so that we observe the new
2508 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002509 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2510 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2511 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2512 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2513 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2514 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002515 bool moveNeeded = false;
2516 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2517 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2518 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2519 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2520 mPointerGesture.lastGestureIdBits.value);
2521 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2522 mPointerGesture.currentGestureCoords,
2523 mPointerGesture.currentGestureIdToIndex,
2524 mPointerGesture.lastGestureProperties,
2525 mPointerGesture.lastGestureCoords,
2526 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2527 if (buttonState != mLastCookedState.buttonState) {
2528 moveNeeded = true;
2529 }
2530 }
2531
2532 // Send motion events for all pointers that went up or were canceled.
2533 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2534 if (!dispatchedGestureIdBits.isEmpty()) {
2535 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002536 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2537 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002538 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2539 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2540 mPointerGesture.downTime);
2541
2542 dispatchedGestureIdBits.clear();
2543 } else {
2544 BitSet32 upGestureIdBits;
2545 if (finishPreviousGesture) {
2546 upGestureIdBits = dispatchedGestureIdBits;
2547 } else {
2548 upGestureIdBits.value =
2549 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2550 }
2551 while (!upGestureIdBits.isEmpty()) {
2552 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2553
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002554 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002555 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002556 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002557 mPointerGesture.lastGestureCoords,
2558 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2559 0, mPointerGesture.downTime);
2560
2561 dispatchedGestureIdBits.clearBit(id);
2562 }
2563 }
2564 }
2565
2566 // Send motion events for all pointers that moved.
2567 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002568 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002569 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002570 mPointerGesture.currentGestureProperties,
2571 mPointerGesture.currentGestureCoords,
2572 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2573 mPointerGesture.downTime);
2574 }
2575
2576 // Send motion events for all pointers that went down.
2577 if (down) {
2578 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2579 ~dispatchedGestureIdBits.value);
2580 while (!downGestureIdBits.isEmpty()) {
2581 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2582 dispatchedGestureIdBits.markBit(id);
2583
2584 if (dispatchedGestureIdBits.count() == 1) {
2585 mPointerGesture.downTime = when;
2586 }
2587
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002588 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002589 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002590 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002591 mPointerGesture.currentGestureCoords,
2592 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2593 0, mPointerGesture.downTime);
2594 }
2595 }
2596
2597 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002598 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002599 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2600 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002601 mPointerGesture.currentGestureProperties,
2602 mPointerGesture.currentGestureCoords,
2603 mPointerGesture.currentGestureIdToIndex,
2604 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2605 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2606 // Synthesize a hover move event after all pointers go up to indicate that
2607 // the pointer is hovering again even if the user is not currently touching
2608 // the touch pad. This ensures that a view will receive a fresh hover enter
2609 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002610 float x, y;
2611 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002612
2613 PointerProperties pointerProperties;
2614 pointerProperties.clear();
2615 pointerProperties.id = 0;
2616 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2617
2618 PointerCoords pointerCoords;
2619 pointerCoords.clear();
2620 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2621 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2622
2623 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002624 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002625 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002626 metaState, buttonState, MotionClassification::NONE,
2627 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2628 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002629 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002630 }
2631
2632 // Update state.
2633 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2634 if (!down) {
2635 mPointerGesture.lastGestureIdBits.clear();
2636 } else {
2637 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2638 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2639 uint32_t id = idBits.clearFirstMarkedBit();
2640 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2641 mPointerGesture.lastGestureProperties[index].copyFrom(
2642 mPointerGesture.currentGestureProperties[index]);
2643 mPointerGesture.lastGestureCoords[index].copyFrom(
2644 mPointerGesture.currentGestureCoords[index]);
2645 mPointerGesture.lastGestureIdToIndex[id] = index;
2646 }
2647 }
2648}
2649
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002650void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002651 // Cancel previously dispatches pointers.
2652 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2653 int32_t metaState = getContext()->getGlobalMetaState();
2654 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002655 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2656 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002657 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2658 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2659 0, 0, mPointerGesture.downTime);
2660 }
2661
2662 // Reset the current pointer gesture.
2663 mPointerGesture.reset();
2664 mPointerVelocityControl.reset();
2665
2666 // Remove any current spots.
2667 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002668 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002669 mPointerController->clearSpots();
2670 }
2671}
2672
2673bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2674 bool* outFinishPreviousGesture, bool isTimeout) {
2675 *outCancelPreviousGesture = false;
2676 *outFinishPreviousGesture = false;
2677
2678 // Handle TAP timeout.
2679 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002680 if (DEBUG_GESTURES) {
2681 ALOGD("Gestures: Processing timeout");
2682 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002683
Michael Wright227c5542020-07-02 18:30:52 +01002684 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002685 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2686 // The tap/drag timeout has not yet expired.
2687 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2688 mConfig.pointerGestureTapDragInterval);
2689 } else {
2690 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002691 if (DEBUG_GESTURES) {
2692 ALOGD("Gestures: TAP finished");
2693 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002694 *outFinishPreviousGesture = true;
2695
2696 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002697 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002698 mPointerGesture.currentGestureIdBits.clear();
2699
2700 mPointerVelocityControl.reset();
2701 return true;
2702 }
2703 }
2704
2705 // We did not handle this timeout.
2706 return false;
2707 }
2708
2709 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2710 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2711
2712 // Update the velocity tracker.
2713 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002714 std::vector<VelocityTracker::Position> positions;
2715 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002716 uint32_t id = idBits.clearFirstMarkedBit();
2717 const RawPointerData::Pointer& pointer =
2718 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002719 float x = pointer.x * mPointerXMovementScale;
2720 float y = pointer.y * mPointerYMovementScale;
2721 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002722 }
2723 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2724 positions);
2725 }
2726
2727 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2728 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002729 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2730 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2731 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002732 mPointerGesture.resetTap();
2733 }
2734
2735 // Pick a new active touch id if needed.
2736 // Choose an arbitrary pointer that just went down, if there is one.
2737 // Otherwise choose an arbitrary remaining pointer.
2738 // This guarantees we always have an active touch id when there is at least one pointer.
2739 // We keep the same active touch id for as long as possible.
2740 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2741 int32_t activeTouchId = lastActiveTouchId;
2742 if (activeTouchId < 0) {
2743 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2744 activeTouchId = mPointerGesture.activeTouchId =
2745 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2746 mPointerGesture.firstTouchTime = when;
2747 }
2748 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2749 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2750 activeTouchId = mPointerGesture.activeTouchId =
2751 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2752 } else {
2753 activeTouchId = mPointerGesture.activeTouchId = -1;
2754 }
2755 }
2756
2757 // Determine whether we are in quiet time.
2758 bool isQuietTime = false;
2759 if (activeTouchId < 0) {
2760 mPointerGesture.resetQuietTime();
2761 } else {
2762 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2763 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002764 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2765 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2766 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002767 currentFingerCount < 2) {
2768 // Enter quiet time when exiting swipe or freeform state.
2769 // This is to prevent accidentally entering the hover state and flinging the
2770 // pointer when finishing a swipe and there is still one pointer left onscreen.
2771 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002772 } else if (mPointerGesture.lastGestureMode ==
2773 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002774 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2775 // Enter quiet time when releasing the button and there are still two or more
2776 // fingers down. This may indicate that one finger was used to press the button
2777 // but it has not gone up yet.
2778 isQuietTime = true;
2779 }
2780 if (isQuietTime) {
2781 mPointerGesture.quietTime = when;
2782 }
2783 }
2784 }
2785
2786 // Switch states based on button and pointer state.
2787 if (isQuietTime) {
2788 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002789 if (DEBUG_GESTURES) {
2790 ALOGD("Gestures: QUIET for next %0.3fms",
2791 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2792 0.000001f);
2793 }
Michael Wright227c5542020-07-02 18:30:52 +01002794 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002795 *outFinishPreviousGesture = true;
2796 }
2797
2798 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002799 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002800 mPointerGesture.currentGestureIdBits.clear();
2801
2802 mPointerVelocityControl.reset();
2803 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2804 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2805 // The pointer follows the active touch point.
2806 // Emit DOWN, MOVE, UP events at the pointer location.
2807 //
2808 // Only the active touch matters; other fingers are ignored. This policy helps
2809 // to handle the case where the user places a second finger on the touch pad
2810 // to apply the necessary force to depress an integrated button below the surface.
2811 // We don't want the second finger to be delivered to applications.
2812 //
2813 // For this to work well, we need to make sure to track the pointer that is really
2814 // active. If the user first puts one finger down to click then adds another
2815 // finger to drag then the active pointer should switch to the finger that is
2816 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002817 if (DEBUG_GESTURES) {
2818 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2819 "currentFingerCount=%d",
2820 activeTouchId, currentFingerCount);
2821 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002822 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002823 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002824 *outFinishPreviousGesture = true;
2825 mPointerGesture.activeGestureId = 0;
2826 }
2827
2828 // Switch pointers if needed.
2829 // Find the fastest pointer and follow it.
2830 if (activeTouchId >= 0 && currentFingerCount > 1) {
2831 int32_t bestId = -1;
2832 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2833 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2834 uint32_t id = idBits.clearFirstMarkedBit();
2835 float vx, vy;
2836 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2837 float speed = hypotf(vx, vy);
2838 if (speed > bestSpeed) {
2839 bestId = id;
2840 bestSpeed = speed;
2841 }
2842 }
2843 }
2844 if (bestId >= 0 && bestId != activeTouchId) {
2845 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002846 if (DEBUG_GESTURES) {
2847 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2848 "bestId=%d, bestSpeed=%0.3f",
2849 bestId, bestSpeed);
2850 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002851 }
2852 }
2853
2854 float deltaX = 0, deltaY = 0;
2855 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2856 const RawPointerData::Pointer& currentPointer =
2857 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2858 const RawPointerData::Pointer& lastPointer =
2859 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2860 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2861 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2862
Prabir Pradhan1728b212021-10-19 16:00:03 -07002863 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002864 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2865
2866 // Move the pointer using a relative motion.
2867 // When using spots, the click will occur at the position of the anchor
2868 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002869 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002870 } else {
2871 mPointerVelocityControl.reset();
2872 }
2873
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002874 float x, y;
2875 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002876
Michael Wright227c5542020-07-02 18:30:52 +01002877 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002878 mPointerGesture.currentGestureIdBits.clear();
2879 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2880 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2881 mPointerGesture.currentGestureProperties[0].clear();
2882 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2883 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2884 mPointerGesture.currentGestureCoords[0].clear();
2885 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2886 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2887 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2888 } else if (currentFingerCount == 0) {
2889 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002890 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002891 *outFinishPreviousGesture = true;
2892 }
2893
2894 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2895 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2896 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002897 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2898 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002899 lastFingerCount == 1) {
2900 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002901 float x, y;
2902 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002903 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2904 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002905 if (DEBUG_GESTURES) {
2906 ALOGD("Gestures: TAP");
2907 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002908
2909 mPointerGesture.tapUpTime = when;
2910 getContext()->requestTimeoutAtTime(when +
2911 mConfig.pointerGestureTapDragInterval);
2912
2913 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002914 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002915 mPointerGesture.currentGestureIdBits.clear();
2916 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2917 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2918 mPointerGesture.currentGestureProperties[0].clear();
2919 mPointerGesture.currentGestureProperties[0].id =
2920 mPointerGesture.activeGestureId;
2921 mPointerGesture.currentGestureProperties[0].toolType =
2922 AMOTION_EVENT_TOOL_TYPE_FINGER;
2923 mPointerGesture.currentGestureCoords[0].clear();
2924 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2925 mPointerGesture.tapX);
2926 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2927 mPointerGesture.tapY);
2928 mPointerGesture.currentGestureCoords[0]
2929 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2930
2931 tapped = true;
2932 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002933 if (DEBUG_GESTURES) {
2934 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2935 y - mPointerGesture.tapY);
2936 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002937 }
2938 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002939 if (DEBUG_GESTURES) {
2940 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2941 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2942 (when - mPointerGesture.tapDownTime) * 0.000001f);
2943 } else {
2944 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2945 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002946 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002947 }
2948 }
2949
2950 mPointerVelocityControl.reset();
2951
2952 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002953 if (DEBUG_GESTURES) {
2954 ALOGD("Gestures: NEUTRAL");
2955 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002956 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002957 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002958 mPointerGesture.currentGestureIdBits.clear();
2959 }
2960 } else if (currentFingerCount == 1) {
2961 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2962 // The pointer follows the active touch point.
2963 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2964 // When in TAP_DRAG, emit MOVE events at the pointer location.
2965 ALOG_ASSERT(activeTouchId >= 0);
2966
Michael Wright227c5542020-07-02 18:30:52 +01002967 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2968 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002969 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002970 float x, y;
2971 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002972 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2973 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002974 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002975 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002976 if (DEBUG_GESTURES) {
2977 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2978 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
2979 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002980 }
2981 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002982 if (DEBUG_GESTURES) {
2983 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2984 (when - mPointerGesture.tapUpTime) * 0.000001f);
2985 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002986 }
Michael Wright227c5542020-07-02 18:30:52 +01002987 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2988 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002989 }
2990
2991 float deltaX = 0, deltaY = 0;
2992 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2993 const RawPointerData::Pointer& currentPointer =
2994 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2995 const RawPointerData::Pointer& lastPointer =
2996 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2997 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2998 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2999
Prabir Pradhan1728b212021-10-19 16:00:03 -07003000 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003001 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3002
3003 // Move the pointer using a relative motion.
3004 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003005 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003006 } else {
3007 mPointerVelocityControl.reset();
3008 }
3009
3010 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003011 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003012 if (DEBUG_GESTURES) {
3013 ALOGD("Gestures: TAP_DRAG");
3014 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003015 down = true;
3016 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003017 if (DEBUG_GESTURES) {
3018 ALOGD("Gestures: HOVER");
3019 }
Michael Wright227c5542020-07-02 18:30:52 +01003020 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003021 *outFinishPreviousGesture = true;
3022 }
3023 mPointerGesture.activeGestureId = 0;
3024 down = false;
3025 }
3026
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003027 float x, y;
3028 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003029
3030 mPointerGesture.currentGestureIdBits.clear();
3031 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3032 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3033 mPointerGesture.currentGestureProperties[0].clear();
3034 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3035 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3036 mPointerGesture.currentGestureCoords[0].clear();
3037 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3038 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3039 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3040 down ? 1.0f : 0.0f);
3041
3042 if (lastFingerCount == 0 && currentFingerCount != 0) {
3043 mPointerGesture.resetTap();
3044 mPointerGesture.tapDownTime = when;
3045 mPointerGesture.tapX = x;
3046 mPointerGesture.tapY = y;
3047 }
3048 } else {
3049 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3050 // We need to provide feedback for each finger that goes down so we cannot wait
3051 // for the fingers to move before deciding what to do.
3052 //
3053 // The ambiguous case is deciding what to do when there are two fingers down but they
3054 // have not moved enough to determine whether they are part of a drag or part of a
3055 // freeform gesture, or just a press or long-press at the pointer location.
3056 //
3057 // When there are two fingers we start with the PRESS hypothesis and we generate a
3058 // down at the pointer location.
3059 //
3060 // When the two fingers move enough or when additional fingers are added, we make
3061 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3062 ALOG_ASSERT(activeTouchId >= 0);
3063
3064 bool settled = when >=
3065 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003066 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3067 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3068 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003069 *outFinishPreviousGesture = true;
3070 } else if (!settled && currentFingerCount > lastFingerCount) {
3071 // Additional pointers have gone down but not yet settled.
3072 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003073 if (DEBUG_GESTURES) {
3074 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3075 "MULTITOUCH, settle time remaining %0.3fms",
3076 (mPointerGesture.firstTouchTime +
3077 mConfig.pointerGestureMultitouchSettleInterval - when) *
3078 0.000001f);
3079 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003080 *outCancelPreviousGesture = true;
3081 } else {
3082 // Continue previous gesture.
3083 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3084 }
3085
3086 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003087 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003088 mPointerGesture.activeGestureId = 0;
3089 mPointerGesture.referenceIdBits.clear();
3090 mPointerVelocityControl.reset();
3091
3092 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003093 if (DEBUG_GESTURES) {
3094 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3095 "settle time remaining %0.3fms",
3096 (mPointerGesture.firstTouchTime +
3097 mConfig.pointerGestureMultitouchSettleInterval - when) *
3098 0.000001f);
3099 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003100 mCurrentRawState.rawPointerData
3101 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3102 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003103 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3104 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003105 }
3106
3107 // Clear the reference deltas for fingers not yet included in the reference calculation.
3108 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3109 ~mPointerGesture.referenceIdBits.value);
3110 !idBits.isEmpty();) {
3111 uint32_t id = idBits.clearFirstMarkedBit();
3112 mPointerGesture.referenceDeltas[id].dx = 0;
3113 mPointerGesture.referenceDeltas[id].dy = 0;
3114 }
3115 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3116
3117 // Add delta for all fingers and calculate a common movement delta.
3118 float commonDeltaX = 0, commonDeltaY = 0;
3119 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3120 mCurrentCookedState.fingerIdBits.value);
3121 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3122 bool first = (idBits == commonIdBits);
3123 uint32_t id = idBits.clearFirstMarkedBit();
3124 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3125 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3126 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3127 delta.dx += cpd.x - lpd.x;
3128 delta.dy += cpd.y - lpd.y;
3129
3130 if (first) {
3131 commonDeltaX = delta.dx;
3132 commonDeltaY = delta.dy;
3133 } else {
3134 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3135 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3136 }
3137 }
3138
3139 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003140 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003141 float dist[MAX_POINTER_ID + 1];
3142 int32_t distOverThreshold = 0;
3143 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3144 uint32_t id = idBits.clearFirstMarkedBit();
3145 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3146 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3147 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3148 distOverThreshold += 1;
3149 }
3150 }
3151
3152 // Only transition when at least two pointers have moved further than
3153 // the minimum distance threshold.
3154 if (distOverThreshold >= 2) {
3155 if (currentFingerCount > 2) {
3156 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003157 if (DEBUG_GESTURES) {
3158 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3159 currentFingerCount);
3160 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003161 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003162 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003163 } else {
3164 // There are exactly two pointers.
3165 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3166 uint32_t id1 = idBits.clearFirstMarkedBit();
3167 uint32_t id2 = idBits.firstMarkedBit();
3168 const RawPointerData::Pointer& p1 =
3169 mCurrentRawState.rawPointerData.pointerForId(id1);
3170 const RawPointerData::Pointer& p2 =
3171 mCurrentRawState.rawPointerData.pointerForId(id2);
3172 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3173 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3174 // There are two pointers but they are too far apart for a SWIPE,
3175 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003176 if (DEBUG_GESTURES) {
3177 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3178 "%0.3f",
3179 mutualDistance, mPointerGestureMaxSwipeWidth);
3180 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003181 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003182 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003183 } else {
3184 // There are two pointers. Wait for both pointers to start moving
3185 // before deciding whether this is a SWIPE or FREEFORM gesture.
3186 float dist1 = dist[id1];
3187 float dist2 = dist[id2];
3188 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3189 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3190 // Calculate the dot product of the displacement vectors.
3191 // When the vectors are oriented in approximately the same direction,
3192 // the angle betweeen them is near zero and the cosine of the angle
3193 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3194 // mag(v2).
3195 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3196 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3197 float dx1 = delta1.dx * mPointerXZoomScale;
3198 float dy1 = delta1.dy * mPointerYZoomScale;
3199 float dx2 = delta2.dx * mPointerXZoomScale;
3200 float dy2 = delta2.dy * mPointerYZoomScale;
3201 float dot = dx1 * dx2 + dy1 * dy2;
3202 float cosine = dot / (dist1 * dist2); // denominator always > 0
3203 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3204 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003205 if (DEBUG_GESTURES) {
3206 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3207 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3208 "cosine %0.3f >= %0.3f",
3209 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3210 mConfig.pointerGestureMultitouchMinDistance, cosine,
3211 mConfig.pointerGestureSwipeTransitionAngleCosine);
3212 }
Michael Wright227c5542020-07-02 18:30:52 +01003213 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003214 } else {
3215 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003216 if (DEBUG_GESTURES) {
3217 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3218 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3219 "cosine %0.3f < %0.3f",
3220 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3221 mConfig.pointerGestureMultitouchMinDistance, cosine,
3222 mConfig.pointerGestureSwipeTransitionAngleCosine);
3223 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003224 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003225 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003226 }
3227 }
3228 }
3229 }
3230 }
Michael Wright227c5542020-07-02 18:30:52 +01003231 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003232 // Switch from SWIPE to FREEFORM if additional pointers go down.
3233 // Cancel previous gesture.
3234 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003235 if (DEBUG_GESTURES) {
3236 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3237 currentFingerCount);
3238 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003239 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003240 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003241 }
3242 }
3243
3244 // Move the reference points based on the overall group motion of the fingers
3245 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003246 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003247 (commonDeltaX || commonDeltaY)) {
3248 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3249 uint32_t id = idBits.clearFirstMarkedBit();
3250 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3251 delta.dx = 0;
3252 delta.dy = 0;
3253 }
3254
3255 mPointerGesture.referenceTouchX += commonDeltaX;
3256 mPointerGesture.referenceTouchY += commonDeltaY;
3257
3258 commonDeltaX *= mPointerXMovementScale;
3259 commonDeltaY *= mPointerYMovementScale;
3260
Prabir Pradhan1728b212021-10-19 16:00:03 -07003261 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003262 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3263
3264 mPointerGesture.referenceGestureX += commonDeltaX;
3265 mPointerGesture.referenceGestureY += commonDeltaY;
3266 }
3267
3268 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003269 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3270 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003271 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003272 if (DEBUG_GESTURES) {
3273 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3274 "activeGestureId=%d, currentTouchPointerCount=%d",
3275 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3276 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003277 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3278
3279 mPointerGesture.currentGestureIdBits.clear();
3280 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3281 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3282 mPointerGesture.currentGestureProperties[0].clear();
3283 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3284 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3285 mPointerGesture.currentGestureCoords[0].clear();
3286 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3287 mPointerGesture.referenceGestureX);
3288 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3289 mPointerGesture.referenceGestureY);
3290 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003291 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003292 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003293 if (DEBUG_GESTURES) {
3294 ALOGD("Gestures: FREEFORM 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
3302 BitSet32 mappedTouchIdBits;
3303 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003304 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003305 // Initially, assign the active gesture id to the active touch point
3306 // if there is one. No other touch id bits are mapped yet.
3307 if (!*outCancelPreviousGesture) {
3308 mappedTouchIdBits.markBit(activeTouchId);
3309 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3310 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3311 mPointerGesture.activeGestureId;
3312 } else {
3313 mPointerGesture.activeGestureId = -1;
3314 }
3315 } else {
3316 // Otherwise, assume we mapped all touches from the previous frame.
3317 // Reuse all mappings that are still applicable.
3318 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3319 mCurrentCookedState.fingerIdBits.value;
3320 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3321
3322 // Check whether we need to choose a new active gesture id because the
3323 // current went went up.
3324 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3325 ~mCurrentCookedState.fingerIdBits.value);
3326 !upTouchIdBits.isEmpty();) {
3327 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3328 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3329 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3330 mPointerGesture.activeGestureId = -1;
3331 break;
3332 }
3333 }
3334 }
3335
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003336 if (DEBUG_GESTURES) {
3337 ALOGD("Gestures: FREEFORM follow up "
3338 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3339 "activeGestureId=%d",
3340 mappedTouchIdBits.value, usedGestureIdBits.value,
3341 mPointerGesture.activeGestureId);
3342 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003343
3344 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3345 for (uint32_t i = 0; i < currentFingerCount; i++) {
3346 uint32_t touchId = idBits.clearFirstMarkedBit();
3347 uint32_t gestureId;
3348 if (!mappedTouchIdBits.hasBit(touchId)) {
3349 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3350 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003351 if (DEBUG_GESTURES) {
3352 ALOGD("Gestures: FREEFORM "
3353 "new mapping for touch id %d -> gesture id %d",
3354 touchId, gestureId);
3355 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003356 } else {
3357 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003358 if (DEBUG_GESTURES) {
3359 ALOGD("Gestures: FREEFORM "
3360 "existing mapping for touch id %d -> gesture id %d",
3361 touchId, gestureId);
3362 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003363 }
3364 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3365 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3366
3367 const RawPointerData::Pointer& pointer =
3368 mCurrentRawState.rawPointerData.pointerForId(touchId);
3369 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3370 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003371 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003372
3373 mPointerGesture.currentGestureProperties[i].clear();
3374 mPointerGesture.currentGestureProperties[i].id = gestureId;
3375 mPointerGesture.currentGestureProperties[i].toolType =
3376 AMOTION_EVENT_TOOL_TYPE_FINGER;
3377 mPointerGesture.currentGestureCoords[i].clear();
3378 mPointerGesture.currentGestureCoords[i]
3379 .setAxisValue(AMOTION_EVENT_AXIS_X,
3380 mPointerGesture.referenceGestureX + deltaX);
3381 mPointerGesture.currentGestureCoords[i]
3382 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3383 mPointerGesture.referenceGestureY + deltaY);
3384 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3385 1.0f);
3386 }
3387
3388 if (mPointerGesture.activeGestureId < 0) {
3389 mPointerGesture.activeGestureId =
3390 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003391 if (DEBUG_GESTURES) {
3392 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3393 mPointerGesture.activeGestureId);
3394 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003395 }
3396 }
3397 }
3398
3399 mPointerController->setButtonState(mCurrentRawState.buttonState);
3400
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003401 if (DEBUG_GESTURES) {
3402 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3403 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3404 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3405 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3406 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3407 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3408 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3409 uint32_t id = idBits.clearFirstMarkedBit();
3410 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3411 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3412 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3413 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3414 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3415 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3416 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3417 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3418 }
3419 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3420 uint32_t id = idBits.clearFirstMarkedBit();
3421 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3422 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3423 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3424 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3425 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3426 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3427 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3428 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3429 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003430 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003431 return true;
3432}
3433
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003435 mPointerSimple.currentCoords.clear();
3436 mPointerSimple.currentProperties.clear();
3437
3438 bool down, hovering;
3439 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3440 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3441 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003442 mPointerController
3443 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3444 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003445
3446 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3447 down = !hovering;
3448
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003449 float x, y;
3450 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003451 mPointerSimple.currentCoords.copyFrom(
3452 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3453 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3454 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3455 mPointerSimple.currentProperties.id = 0;
3456 mPointerSimple.currentProperties.toolType =
3457 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3458 } else {
3459 down = false;
3460 hovering = false;
3461 }
3462
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003463 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003464}
3465
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3467 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003468}
3469
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003471 mPointerSimple.currentCoords.clear();
3472 mPointerSimple.currentProperties.clear();
3473
3474 bool down, hovering;
3475 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3476 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3477 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3478 float deltaX = 0, deltaY = 0;
3479 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3480 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3481 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3482 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3483 mPointerXMovementScale;
3484 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3485 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3486 mPointerYMovementScale;
3487
Prabir Pradhan1728b212021-10-19 16:00:03 -07003488 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003489 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3490
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003491 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003492 } else {
3493 mPointerVelocityControl.reset();
3494 }
3495
3496 down = isPointerDown(mCurrentRawState.buttonState);
3497 hovering = !down;
3498
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003499 float x, y;
3500 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003501 mPointerSimple.currentCoords.copyFrom(
3502 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3503 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3504 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3505 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3506 hovering ? 0.0f : 1.0f);
3507 mPointerSimple.currentProperties.id = 0;
3508 mPointerSimple.currentProperties.toolType =
3509 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3510 } else {
3511 mPointerVelocityControl.reset();
3512
3513 down = false;
3514 hovering = false;
3515 }
3516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003518}
3519
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3521 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003522
3523 mPointerVelocityControl.reset();
3524}
3525
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3527 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003528 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003529
3530 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003531 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003532 mPointerController->clearSpots();
3533 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003534 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003535 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003536 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003537 }
Garfield Tan9514d782020-11-10 16:37:23 -08003538 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003539
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003540 float xCursorPosition, yCursorPosition;
3541 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003542
3543 if (mPointerSimple.down && !down) {
3544 mPointerSimple.down = false;
3545
3546 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3548 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003549 mLastRawState.buttonState, MotionClassification::NONE,
3550 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3551 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3552 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3553 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003554 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003555 }
3556
3557 if (mPointerSimple.hovering && !hovering) {
3558 mPointerSimple.hovering = false;
3559
3560 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003561 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3562 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3563 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003564 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3565 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3566 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3567 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003568 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003569 }
3570
3571 if (down) {
3572 if (!mPointerSimple.down) {
3573 mPointerSimple.down = true;
3574 mPointerSimple.downTime = when;
3575
3576 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003577 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003578 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3579 metaState, mCurrentRawState.buttonState,
3580 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3581 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3582 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3583 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003584 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003585 }
3586
3587 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003588 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3589 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003590 mCurrentRawState.buttonState, MotionClassification::NONE,
3591 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3592 &mPointerSimple.currentCoords, mOrientedXPrecision,
3593 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3594 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003595 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003596 }
3597
3598 if (hovering) {
3599 if (!mPointerSimple.hovering) {
3600 mPointerSimple.hovering = true;
3601
3602 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003603 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003604 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3605 metaState, mCurrentRawState.buttonState,
3606 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3607 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3608 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3609 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003610 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003611 }
3612
3613 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3615 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3616 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003617 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3618 &mPointerSimple.currentCoords, mOrientedXPrecision,
3619 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3620 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003621 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003622 }
3623
3624 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3625 float vscroll = mCurrentRawState.rawVScroll;
3626 float hscroll = mCurrentRawState.rawHScroll;
3627 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3628 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3629
3630 // Send scroll.
3631 PointerCoords pointerCoords;
3632 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3633 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3634 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3635
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3637 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003638 mCurrentRawState.buttonState, MotionClassification::NONE,
3639 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3640 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3641 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3642 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003643 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003644 }
3645
3646 // Save state.
3647 if (down || hovering) {
3648 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3649 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3650 } else {
3651 mPointerSimple.reset();
3652 }
3653}
3654
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003655void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003656 mPointerSimple.currentCoords.clear();
3657 mPointerSimple.currentProperties.clear();
3658
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003660}
3661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3663 uint32_t source, int32_t action, int32_t actionButton,
3664 int32_t flags, int32_t metaState, int32_t buttonState,
3665 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003666 const PointerCoords* coords, const uint32_t* idToIndex,
3667 BitSet32 idBits, int32_t changedId, float xPrecision,
3668 float yPrecision, nsecs_t downTime) {
3669 PointerCoords pointerCoords[MAX_POINTERS];
3670 PointerProperties pointerProperties[MAX_POINTERS];
3671 uint32_t pointerCount = 0;
3672 while (!idBits.isEmpty()) {
3673 uint32_t id = idBits.clearFirstMarkedBit();
3674 uint32_t index = idToIndex[id];
3675 pointerProperties[pointerCount].copyFrom(properties[index]);
3676 pointerCoords[pointerCount].copyFrom(coords[index]);
3677
3678 if (changedId >= 0 && id == uint32_t(changedId)) {
3679 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3680 }
3681
3682 pointerCount += 1;
3683 }
3684
3685 ALOG_ASSERT(pointerCount != 0);
3686
3687 if (changedId >= 0 && pointerCount == 1) {
3688 // Replace initial down and final up action.
3689 // We can compare the action without masking off the changed pointer index
3690 // because we know the index is 0.
3691 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3692 action = AMOTION_EVENT_ACTION_DOWN;
3693 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003694 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3695 action = AMOTION_EVENT_ACTION_CANCEL;
3696 } else {
3697 action = AMOTION_EVENT_ACTION_UP;
3698 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003699 } else {
3700 // Can't happen.
3701 ALOG_ASSERT(false);
3702 }
3703 }
3704 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3705 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003706 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003707 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003708 }
3709 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3710 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003711 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003712 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003713 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003714 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3715 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003716 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3717 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3718 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003719 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003720}
3721
3722bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3723 const PointerCoords* inCoords,
3724 const uint32_t* inIdToIndex,
3725 PointerProperties* outProperties,
3726 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3727 BitSet32 idBits) const {
3728 bool changed = false;
3729 while (!idBits.isEmpty()) {
3730 uint32_t id = idBits.clearFirstMarkedBit();
3731 uint32_t inIndex = inIdToIndex[id];
3732 uint32_t outIndex = outIdToIndex[id];
3733
3734 const PointerProperties& curInProperties = inProperties[inIndex];
3735 const PointerCoords& curInCoords = inCoords[inIndex];
3736 PointerProperties& curOutProperties = outProperties[outIndex];
3737 PointerCoords& curOutCoords = outCoords[outIndex];
3738
3739 if (curInProperties != curOutProperties) {
3740 curOutProperties.copyFrom(curInProperties);
3741 changed = true;
3742 }
3743
3744 if (curInCoords != curOutCoords) {
3745 curOutCoords.copyFrom(curInCoords);
3746 changed = true;
3747 }
3748 }
3749 return changed;
3750}
3751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3753 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3754 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003755}
3756
Prabir Pradhan1728b212021-10-19 16:00:03 -07003757// Transform input device coordinates to display panel coordinates.
3758void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003759 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3760 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3761
arthurhunga36b28e2020-12-29 20:28:15 +08003762 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3763 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3764
Prabir Pradhan1728b212021-10-19 16:00:03 -07003765 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003766 // 0 - no swap and reverse.
3767 // 90 - swap x/y and reverse y.
3768 // 180 - reverse x, y.
3769 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003770 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003771 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003772 x = xScaled;
3773 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003774 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003775 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003776 y = xScaledMax;
3777 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003778 break;
3779 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003780 x = xScaledMax;
3781 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003782 break;
3783 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003784 y = xScaled;
3785 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003786 break;
3787 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003788 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003789 }
3790}
3791
Prabir Pradhan1728b212021-10-19 16:00:03 -07003792bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003793 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3794 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3795
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003796 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003797 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003798 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003799 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003800}
3801
3802const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3803 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003804 if (DEBUG_VIRTUAL_KEYS) {
3805 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3806 "left=%d, top=%d, right=%d, bottom=%d",
3807 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3808 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3809 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003810
3811 if (virtualKey.isHit(x, y)) {
3812 return &virtualKey;
3813 }
3814 }
3815
3816 return nullptr;
3817}
3818
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003819void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3820 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3821 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003822
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003823 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003824
3825 if (currentPointerCount == 0) {
3826 // No pointers to assign.
3827 return;
3828 }
3829
3830 if (lastPointerCount == 0) {
3831 // All pointers are new.
3832 for (uint32_t i = 0; i < currentPointerCount; i++) {
3833 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003834 current.rawPointerData.pointers[i].id = id;
3835 current.rawPointerData.idToIndex[id] = i;
3836 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003837 }
3838 return;
3839 }
3840
3841 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003842 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003843 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003844 uint32_t id = last.rawPointerData.pointers[0].id;
3845 current.rawPointerData.pointers[0].id = id;
3846 current.rawPointerData.idToIndex[id] = 0;
3847 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003848 return;
3849 }
3850
3851 // General case.
3852 // We build a heap of squared euclidean distances between current and last pointers
3853 // associated with the current and last pointer indices. Then, we find the best
3854 // match (by distance) for each current pointer.
3855 // The pointers must have the same tool type but it is possible for them to
3856 // transition from hovering to touching or vice-versa while retaining the same id.
3857 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3858
3859 uint32_t heapSize = 0;
3860 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3861 currentPointerIndex++) {
3862 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3863 lastPointerIndex++) {
3864 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003865 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003866 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003867 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003868 if (currentPointer.toolType == lastPointer.toolType) {
3869 int64_t deltaX = currentPointer.x - lastPointer.x;
3870 int64_t deltaY = currentPointer.y - lastPointer.y;
3871
3872 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3873
3874 // Insert new element into the heap (sift up).
3875 heap[heapSize].currentPointerIndex = currentPointerIndex;
3876 heap[heapSize].lastPointerIndex = lastPointerIndex;
3877 heap[heapSize].distance = distance;
3878 heapSize += 1;
3879 }
3880 }
3881 }
3882
3883 // Heapify
3884 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3885 startIndex -= 1;
3886 for (uint32_t parentIndex = startIndex;;) {
3887 uint32_t childIndex = parentIndex * 2 + 1;
3888 if (childIndex >= heapSize) {
3889 break;
3890 }
3891
3892 if (childIndex + 1 < heapSize &&
3893 heap[childIndex + 1].distance < heap[childIndex].distance) {
3894 childIndex += 1;
3895 }
3896
3897 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3898 break;
3899 }
3900
3901 swap(heap[parentIndex], heap[childIndex]);
3902 parentIndex = childIndex;
3903 }
3904 }
3905
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003906 if (DEBUG_POINTER_ASSIGNMENT) {
3907 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3908 for (size_t i = 0; i < heapSize; i++) {
3909 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3910 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3911 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003912 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003913
3914 // Pull matches out by increasing order of distance.
3915 // To avoid reassigning pointers that have already been matched, the loop keeps track
3916 // of which last and current pointers have been matched using the matchedXXXBits variables.
3917 // It also tracks the used pointer id bits.
3918 BitSet32 matchedLastBits(0);
3919 BitSet32 matchedCurrentBits(0);
3920 BitSet32 usedIdBits(0);
3921 bool first = true;
3922 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3923 while (heapSize > 0) {
3924 if (first) {
3925 // The first time through the loop, we just consume the root element of
3926 // the heap (the one with smallest distance).
3927 first = false;
3928 } else {
3929 // Previous iterations consumed the root element of the heap.
3930 // Pop root element off of the heap (sift down).
3931 heap[0] = heap[heapSize];
3932 for (uint32_t parentIndex = 0;;) {
3933 uint32_t childIndex = parentIndex * 2 + 1;
3934 if (childIndex >= heapSize) {
3935 break;
3936 }
3937
3938 if (childIndex + 1 < heapSize &&
3939 heap[childIndex + 1].distance < heap[childIndex].distance) {
3940 childIndex += 1;
3941 }
3942
3943 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3944 break;
3945 }
3946
3947 swap(heap[parentIndex], heap[childIndex]);
3948 parentIndex = childIndex;
3949 }
3950
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003951 if (DEBUG_POINTER_ASSIGNMENT) {
3952 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3953 for (size_t j = 0; j < heapSize; j++) {
3954 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3955 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3956 heap[j].distance);
3957 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003958 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003959 }
3960
3961 heapSize -= 1;
3962
3963 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3964 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3965
3966 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3967 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3968
3969 matchedCurrentBits.markBit(currentPointerIndex);
3970 matchedLastBits.markBit(lastPointerIndex);
3971
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003972 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3973 current.rawPointerData.pointers[currentPointerIndex].id = id;
3974 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3975 current.rawPointerData.markIdBit(id,
3976 current.rawPointerData.isHovering(
3977 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003978 usedIdBits.markBit(id);
3979
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003980 if (DEBUG_POINTER_ASSIGNMENT) {
3981 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3982 ", distance=%" PRIu64,
3983 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3984 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003985 break;
3986 }
3987 }
3988
3989 // Assign fresh ids to pointers that were not matched in the process.
3990 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3991 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3992 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3993
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003994 current.rawPointerData.pointers[currentPointerIndex].id = id;
3995 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3996 current.rawPointerData.markIdBit(id,
3997 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003998
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003999 if (DEBUG_POINTER_ASSIGNMENT) {
4000 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
4001 id);
4002 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004003 }
4004}
4005
4006int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4007 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4008 return AKEY_STATE_VIRTUAL;
4009 }
4010
4011 for (const VirtualKey& virtualKey : mVirtualKeys) {
4012 if (virtualKey.keyCode == keyCode) {
4013 return AKEY_STATE_UP;
4014 }
4015 }
4016
4017 return AKEY_STATE_UNKNOWN;
4018}
4019
4020int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4021 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4022 return AKEY_STATE_VIRTUAL;
4023 }
4024
4025 for (const VirtualKey& virtualKey : mVirtualKeys) {
4026 if (virtualKey.scanCode == scanCode) {
4027 return AKEY_STATE_UP;
4028 }
4029 }
4030
4031 return AKEY_STATE_UNKNOWN;
4032}
4033
4034bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4035 const int32_t* keyCodes, uint8_t* outFlags) {
4036 for (const VirtualKey& virtualKey : mVirtualKeys) {
4037 for (size_t i = 0; i < numCodes; i++) {
4038 if (virtualKey.keyCode == keyCodes[i]) {
4039 outFlags[i] = 1;
4040 }
4041 }
4042 }
4043
4044 return true;
4045}
4046
4047std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4048 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004049 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004050 return std::make_optional(mPointerController->getDisplayId());
4051 } else {
4052 return std::make_optional(mViewport.displayId);
4053 }
4054 }
4055 return std::nullopt;
4056}
4057
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004058} // namespace android