blob: 6f49f31aa8dbd459b347343597aaacbdd5216f0c [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
181uint32_t TouchInputMapper::getSources() {
182 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) {
393 // Send reset, unless this is the first time the device has been configured,
394 // in which case the reader will call reset itself after all mappers are ready.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000395 NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId());
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700396 getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700397 }
398}
399
400void TouchInputMapper::resolveExternalStylusPresence() {
401 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800402 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403 mExternalStylusConnected = !devices.empty();
404
405 if (!mExternalStylusConnected) {
406 resetExternalStylus();
407 }
408}
409
410void TouchInputMapper::configureParameters() {
411 // Use the pointer presentation mode for devices that do not support distinct
412 // multitouch. The spot-based presentation relies on being able to accurately
413 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800414 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100415 ? Parameters::GestureMode::SINGLE_TOUCH
416 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700417
418 String8 gestureModeString;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800419 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.gestureMode"),
420 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700421 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100422 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700423 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100424 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 } else if (gestureModeString != "default") {
426 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
427 }
428 }
429
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800430 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100432 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800433 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100435 mParameters.deviceType = Parameters::DeviceType::POINTER;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800436 } else if (getDeviceContext().hasRelativeAxis(REL_X) ||
437 getDeviceContext().hasRelativeAxis(REL_Y)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438 // The device is a cursor device with a touch pad attached.
439 // By default don't use the touch pad to move the pointer.
Michael Wright227c5542020-07-02 18:30:52 +0100440 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700441 } else {
442 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100443 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444 }
445
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800446 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447
448 String8 deviceTypeString;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800449 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.deviceType"),
450 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700451 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100452 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453 } else if (deviceTypeString == "touchPad") {
Michael Wright227c5542020-07-02 18:30:52 +0100454 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700455 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100456 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100458 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 } else if (deviceTypeString != "default") {
460 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
461 }
462 }
463
Michael Wright227c5542020-07-02 18:30:52 +0100464 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800465 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.orientationAware"),
466 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700468 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
469 String8 orientationString;
470 if (getDeviceContext().getConfiguration().tryGetProperty(String8("touch.orientation"),
471 orientationString)) {
472 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
473 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
474 } else if (orientationString == "ORIENTATION_90") {
475 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
476 } else if (orientationString == "ORIENTATION_180") {
477 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
478 } else if (orientationString == "ORIENTATION_270") {
479 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
480 } else if (orientationString != "ORIENTATION_0") {
481 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.string());
482 }
483 }
484
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700485 mParameters.hasAssociatedDisplay = false;
486 mParameters.associatedDisplayIsExternal = false;
487 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100488 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
489 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700490 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100491 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800492 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700493 String8 uniqueDisplayId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800494 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.displayId"),
495 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700496 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
497 }
498 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800499 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700500 mParameters.hasAssociatedDisplay = true;
501 }
502
503 // Initial downs on external touch devices should wake the device.
504 // Normally we don't do this for internal touch screens to prevent them from waking
505 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800506 mParameters.wake = getDeviceContext().isExternal();
507 getDeviceContext().getConfiguration().tryGetProperty(String8("touch.wake"), mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700508}
509
510void TouchInputMapper::dumpParameters(std::string& dump) {
511 dump += INDENT3 "Parameters:\n";
512
Dominik Laskowski75788452021-02-09 18:51:25 -0800513 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700514
Dominik Laskowski75788452021-02-09 18:51:25 -0800515 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700516
517 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
518 "displayId='%s'\n",
519 toString(mParameters.hasAssociatedDisplay),
520 toString(mParameters.associatedDisplayIsExternal),
521 mParameters.uniqueDisplayId.c_str());
522 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800523 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700524}
525
526void TouchInputMapper::configureRawPointerAxes() {
527 mRawPointerAxes.clear();
528}
529
530void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
531 dump += INDENT3 "Raw Touch Axes:\n";
532 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
533 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
534 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
535 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
536 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
537 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
538 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
539 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
545}
546
547bool TouchInputMapper::hasExternalStylus() const {
548 return mExternalStylusConnected;
549}
550
551/**
552 * Determine which DisplayViewport to use.
553 * 1. If display port is specified, return the matching viewport. If matching viewport not
554 * found, then return.
Garfield Tan888a6a42020-01-09 11:39:16 -0800555 * 2. Always use the suggested viewport from WindowManagerService for pointers.
556 * 3. If a device has associated display, get the matching viewport by either unique id or by
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700557 * the display type (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800558 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700559 */
560std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800561 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800562 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700563 if (displayPort) {
564 // Find the viewport that contains the same port
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800565 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700566 }
567
Michael Wright227c5542020-07-02 18:30:52 +0100568 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800569 std::optional<DisplayViewport> viewport =
570 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
571 if (viewport) {
572 return viewport;
573 } else {
574 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
575 mConfig.defaultPointerDisplayId);
576 }
577 }
578
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700579 // Check if uniqueDisplayId is specified in idc file.
580 if (!mParameters.uniqueDisplayId.empty()) {
581 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
582 }
583
584 ViewportType viewportTypeToUse;
585 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100586 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700587 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100588 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700589 }
590
591 std::optional<DisplayViewport> viewport =
592 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100593 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700594 ALOGW("Input device %s should be associated with external display, "
595 "fallback to internal one for the external viewport is not found.",
596 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100597 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700598 }
599
600 return viewport;
601 }
602
603 // No associated display, return a non-display viewport.
604 DisplayViewport newViewport;
605 // Raw width and height in the natural orientation.
606 int32_t rawWidth = mRawPointerAxes.getRawWidth();
607 int32_t rawHeight = mRawPointerAxes.getRawHeight();
608 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
609 return std::make_optional(newViewport);
610}
611
Prabir Pradhan1728b212021-10-19 16:00:03 -0700612void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100613 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700614
615 resolveExternalStylusPresence();
616
617 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100618 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000619 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700620 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100621 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700622 if (hasStylus()) {
623 mSource |= AINPUT_SOURCE_STYLUS;
624 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800625 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700626 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100627 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700628 if (hasStylus()) {
629 mSource |= AINPUT_SOURCE_STYLUS;
630 }
631 if (hasExternalStylus()) {
632 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
633 }
Michael Wright227c5542020-07-02 18:30:52 +0100634 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700635 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100636 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700637 } else {
638 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100639 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700640 }
641
642 // Ensure we have valid X and Y axes.
643 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
644 ALOGW("Touch device '%s' did not report support for X or Y axis! "
645 "The device will be inoperable.",
646 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100647 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700648 return;
649 }
650
651 // Get associated display dimensions.
652 std::optional<DisplayViewport> newViewport = findViewport();
653 if (!newViewport) {
654 ALOGI("Touch device '%s' could not query the properties of its associated "
655 "display. The device will be inoperable until the display size "
656 "becomes available.",
657 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100658 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700659 return;
660 }
661
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000662 if (!newViewport->isActive) {
663 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
664 getDeviceName().c_str(), getDeviceId());
665 mDeviceMode = DeviceMode::DISABLED;
666 return;
667 }
668
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700669 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700670 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
671 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700672
Prabir Pradhan1728b212021-10-19 16:00:03 -0700673 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700674 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700675 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700676 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700677 mViewport = *newViewport;
678
Michael Wright227c5542020-07-02 18:30:52 +0100679 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700680 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700681 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
682 int32_t naturalPhysicalLeft, naturalPhysicalTop;
683 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700684
Prabir Pradhan1728b212021-10-19 16:00:03 -0700685 // Apply the inverse of the input device orientation so that the input device is
686 // configured in the same orientation as the viewport. The input device orientation will
687 // be re-applied by mInputDeviceOrientation.
688 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700689 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700690 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700691 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700692 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
693 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800694 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700695 naturalPhysicalTop = mViewport.physicalLeft;
696 naturalDeviceWidth = mViewport.deviceHeight;
697 naturalDeviceHeight = mViewport.deviceWidth;
698 break;
699 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700700 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
701 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
702 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
703 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
704 naturalDeviceWidth = mViewport.deviceWidth;
705 naturalDeviceHeight = mViewport.deviceHeight;
706 break;
707 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700708 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
709 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
710 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800711 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700712 naturalDeviceWidth = mViewport.deviceHeight;
713 naturalDeviceHeight = mViewport.deviceWidth;
714 break;
715 case DISPLAY_ORIENTATION_0:
716 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700717 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
718 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
719 naturalPhysicalLeft = mViewport.physicalLeft;
720 naturalPhysicalTop = mViewport.physicalTop;
721 naturalDeviceWidth = mViewport.deviceWidth;
722 naturalDeviceHeight = mViewport.deviceHeight;
723 break;
724 }
725
726 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
727 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
728 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
729 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
730 }
731
732 mPhysicalWidth = naturalPhysicalWidth;
733 mPhysicalHeight = naturalPhysicalHeight;
734 mPhysicalLeft = naturalPhysicalLeft;
735 mPhysicalTop = naturalPhysicalTop;
736
Prabir Pradhan1728b212021-10-19 16:00:03 -0700737 const int32_t oldDisplayWidth = mDisplayWidth;
738 const int32_t oldDisplayHeight = mDisplayHeight;
739 mDisplayWidth = naturalDeviceWidth;
740 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -0700741
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +0000742 // InputReader works in the un-rotated display coordinate space, so we don't need to do
743 // anything if the device is already orientation-aware. If the device is not
744 // orientation-aware, then we need to apply the inverse rotation of the display so that
745 // when the display rotation is applied later as a part of the per-window transform, we
746 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700747 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +0000748 ? DISPLAY_ORIENTATION_0
749 : getInverseRotation(mViewport.orientation);
750 // For orientation-aware devices that work in the un-rotated coordinate space, the
751 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700752 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
753 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700754
755 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700756 mInputDeviceOrientation =
757 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700758 } else {
759 mPhysicalWidth = rawWidth;
760 mPhysicalHeight = rawHeight;
761 mPhysicalLeft = 0;
762 mPhysicalTop = 0;
763
Prabir Pradhan1728b212021-10-19 16:00:03 -0700764 mDisplayWidth = rawWidth;
765 mDisplayHeight = rawHeight;
766 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700767 }
768 }
769
770 // If moving between pointer modes, need to reset some state.
771 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
772 if (deviceModeChanged) {
773 mOrientedRanges.clear();
774 }
775
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800776 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
777 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +0100778 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800779 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000780 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
781 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800782 if (mPointerController == nullptr) {
783 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700784 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000785 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800786 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
787 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700788 } else {
Michael Wright17db18e2020-06-26 20:51:44 +0100789 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700790 }
791
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700792 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700793 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
794 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -0700795 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
796 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700797
798 // Configure X and Y factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700799 mXScale = float(mDisplayWidth) / rawWidth;
800 mYScale = float(mDisplayHeight) / rawHeight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700801 mXPrecision = 1.0f / mXScale;
802 mYPrecision = 1.0f / mYScale;
803
804 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
805 mOrientedRanges.x.source = mSource;
806 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
807 mOrientedRanges.y.source = mSource;
808
809 configureVirtualKeys();
810
811 // Scale factor for terms that are not oriented in a particular axis.
812 // If the pixels are square then xScale == yScale otherwise we fake it
813 // by choosing an average.
814 mGeometricScale = avg(mXScale, mYScale);
815
816 // Size of diagonal axis.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700817 float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700818
819 // Size factors.
Michael Wright227c5542020-07-02 18:30:52 +0100820 if (mCalibration.sizeCalibration != Calibration::SizeCalibration::NONE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700821 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
822 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
823 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
824 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
825 } else {
826 mSizeScale = 0.0f;
827 }
828
829 mOrientedRanges.haveTouchSize = true;
830 mOrientedRanges.haveToolSize = true;
831 mOrientedRanges.haveSize = true;
832
833 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
834 mOrientedRanges.touchMajor.source = mSource;
835 mOrientedRanges.touchMajor.min = 0;
836 mOrientedRanges.touchMajor.max = diagonalSize;
837 mOrientedRanges.touchMajor.flat = 0;
838 mOrientedRanges.touchMajor.fuzz = 0;
839 mOrientedRanges.touchMajor.resolution = 0;
840
841 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
842 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
843
844 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
845 mOrientedRanges.toolMajor.source = mSource;
846 mOrientedRanges.toolMajor.min = 0;
847 mOrientedRanges.toolMajor.max = diagonalSize;
848 mOrientedRanges.toolMajor.flat = 0;
849 mOrientedRanges.toolMajor.fuzz = 0;
850 mOrientedRanges.toolMajor.resolution = 0;
851
852 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
853 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
854
855 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
856 mOrientedRanges.size.source = mSource;
857 mOrientedRanges.size.min = 0;
858 mOrientedRanges.size.max = 1.0;
859 mOrientedRanges.size.flat = 0;
860 mOrientedRanges.size.fuzz = 0;
861 mOrientedRanges.size.resolution = 0;
862 } else {
863 mSizeScale = 0.0f;
864 }
865
866 // Pressure factors.
867 mPressureScale = 0;
868 float pressureMax = 1.0;
Michael Wright227c5542020-07-02 18:30:52 +0100869 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
870 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700871 if (mCalibration.havePressureScale) {
872 mPressureScale = mCalibration.pressureScale;
873 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
874 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
875 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
876 }
877 }
878
879 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
880 mOrientedRanges.pressure.source = mSource;
881 mOrientedRanges.pressure.min = 0;
882 mOrientedRanges.pressure.max = pressureMax;
883 mOrientedRanges.pressure.flat = 0;
884 mOrientedRanges.pressure.fuzz = 0;
885 mOrientedRanges.pressure.resolution = 0;
886
887 // Tilt
888 mTiltXCenter = 0;
889 mTiltXScale = 0;
890 mTiltYCenter = 0;
891 mTiltYScale = 0;
892 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
893 if (mHaveTilt) {
894 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
895 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
896 mTiltXScale = M_PI / 180;
897 mTiltYScale = M_PI / 180;
898
Tatsunosuke Tobita35e05532021-06-30 14:49:32 +0900899 if (mRawPointerAxes.tiltX.resolution) {
900 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
901 }
902 if (mRawPointerAxes.tiltY.resolution) {
903 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
904 }
905
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700906 mOrientedRanges.haveTilt = true;
907
908 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
909 mOrientedRanges.tilt.source = mSource;
910 mOrientedRanges.tilt.min = 0;
911 mOrientedRanges.tilt.max = M_PI_2;
912 mOrientedRanges.tilt.flat = 0;
913 mOrientedRanges.tilt.fuzz = 0;
914 mOrientedRanges.tilt.resolution = 0;
915 }
916
917 // Orientation
918 mOrientationScale = 0;
919 if (mHaveTilt) {
920 mOrientedRanges.haveOrientation = true;
921
922 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
923 mOrientedRanges.orientation.source = mSource;
924 mOrientedRanges.orientation.min = -M_PI;
925 mOrientedRanges.orientation.max = M_PI;
926 mOrientedRanges.orientation.flat = 0;
927 mOrientedRanges.orientation.fuzz = 0;
928 mOrientedRanges.orientation.resolution = 0;
929 } else if (mCalibration.orientationCalibration !=
Michael Wright227c5542020-07-02 18:30:52 +0100930 Calibration::OrientationCalibration::NONE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700931 if (mCalibration.orientationCalibration ==
Michael Wright227c5542020-07-02 18:30:52 +0100932 Calibration::OrientationCalibration::INTERPOLATED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700933 if (mRawPointerAxes.orientation.valid) {
934 if (mRawPointerAxes.orientation.maxValue > 0) {
935 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
936 } else if (mRawPointerAxes.orientation.minValue < 0) {
937 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
938 } else {
939 mOrientationScale = 0;
940 }
941 }
942 }
943
944 mOrientedRanges.haveOrientation = true;
945
946 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
947 mOrientedRanges.orientation.source = mSource;
948 mOrientedRanges.orientation.min = -M_PI_2;
949 mOrientedRanges.orientation.max = M_PI_2;
950 mOrientedRanges.orientation.flat = 0;
951 mOrientedRanges.orientation.fuzz = 0;
952 mOrientedRanges.orientation.resolution = 0;
953 }
954
955 // Distance
956 mDistanceScale = 0;
Michael Wright227c5542020-07-02 18:30:52 +0100957 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
958 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700959 if (mCalibration.haveDistanceScale) {
960 mDistanceScale = mCalibration.distanceScale;
961 } else {
962 mDistanceScale = 1.0f;
963 }
964 }
965
966 mOrientedRanges.haveDistance = true;
967
968 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
969 mOrientedRanges.distance.source = mSource;
970 mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
971 mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
972 mOrientedRanges.distance.flat = 0;
973 mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
974 mOrientedRanges.distance.resolution = 0;
975 }
976
977 // Compute oriented precision, scales and ranges.
978 // Note that the maximum value reported is an inclusive maximum value so it is one
Prabir Pradhan1728b212021-10-19 16:00:03 -0700979 // unit less than the total width or height of the display.
980 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700981 case DISPLAY_ORIENTATION_90:
982 case DISPLAY_ORIENTATION_270:
983 mOrientedXPrecision = mYPrecision;
984 mOrientedYPrecision = mXPrecision;
985
Prabir Pradhan1728b212021-10-19 16:00:03 -0700986 mOrientedRanges.x.min = 0;
987 mOrientedRanges.x.max = mDisplayHeight - 1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700988 mOrientedRanges.x.flat = 0;
989 mOrientedRanges.x.fuzz = 0;
990 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
991
Prabir Pradhan1728b212021-10-19 16:00:03 -0700992 mOrientedRanges.y.min = 0;
993 mOrientedRanges.y.max = mDisplayWidth - 1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700994 mOrientedRanges.y.flat = 0;
995 mOrientedRanges.y.fuzz = 0;
996 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
997 break;
998
999 default:
1000 mOrientedXPrecision = mXPrecision;
1001 mOrientedYPrecision = mYPrecision;
1002
Prabir Pradhan1728b212021-10-19 16:00:03 -07001003 mOrientedRanges.x.min = 0;
1004 mOrientedRanges.x.max = mDisplayWidth - 1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001005 mOrientedRanges.x.flat = 0;
1006 mOrientedRanges.x.fuzz = 0;
1007 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
1008
Prabir Pradhan1728b212021-10-19 16:00:03 -07001009 mOrientedRanges.y.min = 0;
1010 mOrientedRanges.y.max = mDisplayHeight - 1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001011 mOrientedRanges.y.flat = 0;
1012 mOrientedRanges.y.fuzz = 0;
1013 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
1014 break;
1015 }
1016
1017 // Location
1018 updateAffineTransformation();
1019
Michael Wright227c5542020-07-02 18:30:52 +01001020 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001021 // Compute pointer gesture detection parameters.
1022 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001023 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001024
1025 // Scale movements such that one whole swipe of the touch pad covers a
1026 // given area relative to the diagonal size of the display when no acceleration
1027 // is applied.
1028 // Assume that the touch pad has a square aspect ratio such that movements in
1029 // X and Y of the same number of raw units cover the same physical distance.
1030 mPointerXMovementScale =
1031 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1032 mPointerYMovementScale = mPointerXMovementScale;
1033
1034 // Scale zooms to cover a smaller range of the display than movements do.
1035 // This value determines the area around the pointer that is affected by freeform
1036 // pointer gestures.
1037 mPointerXZoomScale =
1038 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1039 mPointerYZoomScale = mPointerXZoomScale;
1040
1041 // Max width between pointers to detect a swipe gesture is more than some fraction
1042 // of the diagonal axis of the touch pad. Touches that are wider than this are
1043 // translated into freeform gestures.
1044 mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
1045
1046 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001047 const nsecs_t readTime = when; // synthetic event
1048 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001049 }
1050
1051 // Inform the dispatcher about the changes.
1052 *outResetNeeded = true;
1053 bumpGeneration();
1054 }
1055}
1056
Prabir Pradhan1728b212021-10-19 16:00:03 -07001057void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001058 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001059 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1060 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001061 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1062 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1063 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1064 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001065 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001066}
1067
1068void TouchInputMapper::configureVirtualKeys() {
1069 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001070 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001071
1072 mVirtualKeys.clear();
1073
1074 if (virtualKeyDefinitions.size() == 0) {
1075 return;
1076 }
1077
1078 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1079 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1080 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1081 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1082
1083 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1084 VirtualKey virtualKey;
1085
1086 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1087 int32_t keyCode;
1088 int32_t dummyKeyMetaState;
1089 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001090 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1091 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001092 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1093 continue; // drop the key
1094 }
1095
1096 virtualKey.keyCode = keyCode;
1097 virtualKey.flags = flags;
1098
1099 // convert the key definition's display coordinates into touch coordinates for a hit box
1100 int32_t halfWidth = virtualKeyDefinition.width / 2;
1101 int32_t halfHeight = virtualKeyDefinition.height / 2;
1102
1103 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001104 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001105 touchScreenLeft;
1106 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001107 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001108 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001109 virtualKey.hitTop =
1110 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001111 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001112 virtualKey.hitBottom =
1113 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001114 touchScreenTop;
1115 mVirtualKeys.push_back(virtualKey);
1116 }
1117}
1118
1119void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1120 if (!mVirtualKeys.empty()) {
1121 dump += INDENT3 "Virtual Keys:\n";
1122
1123 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1124 const VirtualKey& virtualKey = mVirtualKeys[i];
1125 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1126 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1127 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1128 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1129 }
1130 }
1131}
1132
1133void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001134 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001135 Calibration& out = mCalibration;
1136
1137 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001138 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001139 String8 sizeCalibrationString;
1140 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
1141 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001142 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001143 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001144 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001145 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001146 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001147 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001148 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001149 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001150 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001151 } else if (sizeCalibrationString != "default") {
1152 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string());
1153 }
1154 }
1155
1156 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale);
1157 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias);
1158 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed);
1159
1160 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001161 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001162 String8 pressureCalibrationString;
1163 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
1164 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001165 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001167 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001168 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001169 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001170 } else if (pressureCalibrationString != "default") {
1171 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
1172 pressureCalibrationString.string());
1173 }
1174 }
1175
1176 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale);
1177
1178 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001179 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001180 String8 orientationCalibrationString;
1181 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
1182 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001183 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001184 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001185 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001186 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001187 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001188 } else if (orientationCalibrationString != "default") {
1189 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
1190 orientationCalibrationString.string());
1191 }
1192 }
1193
1194 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001195 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001196 String8 distanceCalibrationString;
1197 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
1198 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001199 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001200 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001201 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001202 } else if (distanceCalibrationString != "default") {
1203 ALOGW("Invalid value for touch.distance.calibration: '%s'",
1204 distanceCalibrationString.string());
1205 }
1206 }
1207
1208 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale);
1209
Michael Wright227c5542020-07-02 18:30:52 +01001210 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001211 String8 coverageCalibrationString;
1212 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
1213 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001214 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001215 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001216 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 } else if (coverageCalibrationString != "default") {
1218 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
1219 coverageCalibrationString.string());
1220 }
1221 }
1222}
1223
1224void TouchInputMapper::resolveCalibration() {
1225 // Size
1226 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001227 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1228 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001229 }
1230 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001231 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001232 }
1233
1234 // Pressure
1235 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001236 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1237 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001238 }
1239 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001240 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001241 }
1242
1243 // Orientation
1244 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001245 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1246 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001247 }
1248 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001249 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001250 }
1251
1252 // Distance
1253 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001254 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1255 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001256 }
1257 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001258 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001259 }
1260
1261 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001262 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1263 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001264 }
1265}
1266
1267void TouchInputMapper::dumpCalibration(std::string& dump) {
1268 dump += INDENT3 "Calibration:\n";
1269
1270 // Size
1271 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001272 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001273 dump += INDENT4 "touch.size.calibration: none\n";
1274 break;
Michael Wright227c5542020-07-02 18:30:52 +01001275 case Calibration::SizeCalibration::GEOMETRIC:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001276 dump += INDENT4 "touch.size.calibration: geometric\n";
1277 break;
Michael Wright227c5542020-07-02 18:30:52 +01001278 case Calibration::SizeCalibration::DIAMETER:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001279 dump += INDENT4 "touch.size.calibration: diameter\n";
1280 break;
Michael Wright227c5542020-07-02 18:30:52 +01001281 case Calibration::SizeCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001282 dump += INDENT4 "touch.size.calibration: box\n";
1283 break;
Michael Wright227c5542020-07-02 18:30:52 +01001284 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001285 dump += INDENT4 "touch.size.calibration: area\n";
1286 break;
1287 default:
1288 ALOG_ASSERT(false);
1289 }
1290
1291 if (mCalibration.haveSizeScale) {
1292 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
1293 }
1294
1295 if (mCalibration.haveSizeBias) {
1296 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
1297 }
1298
1299 if (mCalibration.haveSizeIsSummed) {
1300 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
1301 toString(mCalibration.sizeIsSummed));
1302 }
1303
1304 // Pressure
1305 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001306 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001307 dump += INDENT4 "touch.pressure.calibration: none\n";
1308 break;
Michael Wright227c5542020-07-02 18:30:52 +01001309 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001310 dump += INDENT4 "touch.pressure.calibration: physical\n";
1311 break;
Michael Wright227c5542020-07-02 18:30:52 +01001312 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001313 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1314 break;
1315 default:
1316 ALOG_ASSERT(false);
1317 }
1318
1319 if (mCalibration.havePressureScale) {
1320 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
1321 }
1322
1323 // Orientation
1324 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001325 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001326 dump += INDENT4 "touch.orientation.calibration: none\n";
1327 break;
Michael Wright227c5542020-07-02 18:30:52 +01001328 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001329 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1330 break;
Michael Wright227c5542020-07-02 18:30:52 +01001331 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001332 dump += INDENT4 "touch.orientation.calibration: vector\n";
1333 break;
1334 default:
1335 ALOG_ASSERT(false);
1336 }
1337
1338 // Distance
1339 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001340 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001341 dump += INDENT4 "touch.distance.calibration: none\n";
1342 break;
Michael Wright227c5542020-07-02 18:30:52 +01001343 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001344 dump += INDENT4 "touch.distance.calibration: scaled\n";
1345 break;
1346 default:
1347 ALOG_ASSERT(false);
1348 }
1349
1350 if (mCalibration.haveDistanceScale) {
1351 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
1352 }
1353
1354 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001355 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001356 dump += INDENT4 "touch.coverage.calibration: none\n";
1357 break;
Michael Wright227c5542020-07-02 18:30:52 +01001358 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001359 dump += INDENT4 "touch.coverage.calibration: box\n";
1360 break;
1361 default:
1362 ALOG_ASSERT(false);
1363 }
1364}
1365
1366void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1367 dump += INDENT3 "Affine Transformation:\n";
1368
1369 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1370 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1371 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1372 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1373 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1374 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1375}
1376
1377void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001378 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001379 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001380}
1381
1382void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001383 mCursorButtonAccumulator.reset(getDeviceContext());
1384 mCursorScrollAccumulator.reset(getDeviceContext());
1385 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001386
1387 mPointerVelocityControl.reset();
1388 mWheelXVelocityControl.reset();
1389 mWheelYVelocityControl.reset();
1390
1391 mRawStatesPending.clear();
1392 mCurrentRawState.clear();
1393 mCurrentCookedState.clear();
1394 mLastRawState.clear();
1395 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001396 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001397 mSentHoverEnter = false;
1398 mHavePointerIds = false;
1399 mCurrentMotionAborted = false;
1400 mDownTime = 0;
1401
1402 mCurrentVirtualKey.down = false;
1403
1404 mPointerGesture.reset();
1405 mPointerSimple.reset();
1406 resetExternalStylus();
1407
1408 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001409 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001410 mPointerController->clearSpots();
1411 }
1412
1413 InputMapper::reset(when);
1414}
1415
1416void TouchInputMapper::resetExternalStylus() {
1417 mExternalStylusState.clear();
1418 mExternalStylusId = -1;
1419 mExternalStylusFusionTimeout = LLONG_MAX;
1420 mExternalStylusDataPending = false;
1421}
1422
1423void TouchInputMapper::clearStylusDataPendingFlags() {
1424 mExternalStylusDataPending = false;
1425 mExternalStylusFusionTimeout = LLONG_MAX;
1426}
1427
1428void TouchInputMapper::process(const RawEvent* rawEvent) {
1429 mCursorButtonAccumulator.process(rawEvent);
1430 mCursorScrollAccumulator.process(rawEvent);
1431 mTouchButtonAccumulator.process(rawEvent);
1432
1433 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001435 }
1436}
1437
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001438void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001439 // Push a new state.
1440 mRawStatesPending.emplace_back();
1441
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001442 RawState& next = mRawStatesPending.back();
1443 next.clear();
1444 next.when = when;
1445 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001446
1447 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001448 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001449 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1450
1451 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001452 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1453 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001454 mCursorScrollAccumulator.finishSync();
1455
1456 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001457 syncTouch(when, &next);
1458
1459 // The last RawState is the actually second to last, since we just added a new state
1460 const RawState& last =
1461 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001462
1463 // Assign pointer ids.
1464 if (!mHavePointerIds) {
1465 assignPointerIds(last, next);
1466 }
1467
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001468 if (DEBUG_RAW_EVENTS) {
1469 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1470 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1471 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1472 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1473 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1474 next.rawPointerData.canceledIdBits.value);
1475 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001476
Arthur Hung9ad18942021-06-19 02:04:46 +00001477 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1478 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1479 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1480 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1481 next.rawPointerData.hoveringIdBits.value);
1482 }
1483
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001484 processRawTouches(false /*timeout*/);
1485}
1486
1487void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001488 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001489 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001490 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001491 mCurrentCookedState.clear();
1492 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001493 return;
1494 }
1495
1496 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1497 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1498 // touching the current state will only observe the events that have been dispatched to the
1499 // rest of the pipeline.
1500 const size_t N = mRawStatesPending.size();
1501 size_t count;
1502 for (count = 0; count < N; count++) {
1503 const RawState& next = mRawStatesPending[count];
1504
1505 // A failure to assign the stylus id means that we're waiting on stylus data
1506 // and so should defer the rest of the pipeline.
1507 if (assignExternalStylusId(next, timeout)) {
1508 break;
1509 }
1510
1511 // All ready to go.
1512 clearStylusDataPendingFlags();
1513 mCurrentRawState.copyFrom(next);
1514 if (mCurrentRawState.when < mLastRawState.when) {
1515 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001516 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001517 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001518 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001519 }
1520 if (count != 0) {
1521 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1522 }
1523
1524 if (mExternalStylusDataPending) {
1525 if (timeout) {
1526 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1527 clearStylusDataPendingFlags();
1528 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001529 if (DEBUG_STYLUS_FUSION) {
1530 ALOGD("Timeout expired, synthesizing event with new stylus data");
1531 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001532 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1533 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001534 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1535 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1536 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1537 }
1538 }
1539}
1540
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001541void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001542 // Always start with a clean state.
1543 mCurrentCookedState.clear();
1544
1545 // Apply stylus buttons to current raw state.
1546 applyExternalStylusButtonState(when);
1547
1548 // Handle policy on initial down or hover events.
1549 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1550 mCurrentRawState.rawPointerData.pointerCount != 0;
1551
1552 uint32_t policyFlags = 0;
1553 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1554 if (initialDown || buttonsPressed) {
1555 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001556 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001557 getContext()->fadePointer();
1558 }
1559
1560 if (mParameters.wake) {
1561 policyFlags |= POLICY_FLAG_WAKE;
1562 }
1563 }
1564
1565 // Consume raw off-screen touches before cooking pointer data.
1566 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001567 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001568 mCurrentRawState.rawPointerData.clear();
1569 }
1570
1571 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1572 // with cooked pointer data that has the same ids and indices as the raw data.
1573 // The following code can use either the raw or cooked data, as needed.
1574 cookPointerData();
1575
1576 // Apply stylus pressure to current cooked state.
1577 applyExternalStylusTouchState(when);
1578
1579 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001580 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1581 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001582 mCurrentCookedState.buttonState);
1583
1584 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001585 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001586 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1587 uint32_t id = idBits.clearFirstMarkedBit();
1588 const RawPointerData::Pointer& pointer =
1589 mCurrentRawState.rawPointerData.pointerForId(id);
1590 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1591 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1592 mCurrentCookedState.stylusIdBits.markBit(id);
1593 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1594 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1595 mCurrentCookedState.fingerIdBits.markBit(id);
1596 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1597 mCurrentCookedState.mouseIdBits.markBit(id);
1598 }
1599 }
1600 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1601 uint32_t id = idBits.clearFirstMarkedBit();
1602 const RawPointerData::Pointer& pointer =
1603 mCurrentRawState.rawPointerData.pointerForId(id);
1604 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1605 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1606 mCurrentCookedState.stylusIdBits.markBit(id);
1607 }
1608 }
1609
1610 // Stylus takes precedence over all tools, then mouse, then finger.
1611 PointerUsage pointerUsage = mPointerUsage;
1612 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1613 mCurrentCookedState.mouseIdBits.clear();
1614 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001615 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001616 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1617 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001618 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001619 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1620 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001621 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001622 }
1623
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001624 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001625 } else {
Garfield Tanc734e4f2021-01-15 20:01:39 -08001626 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001627
1628 if (!mCurrentMotionAborted) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001629 dispatchButtonRelease(when, readTime, policyFlags);
1630 dispatchHoverExit(when, readTime, policyFlags);
1631 dispatchTouches(when, readTime, policyFlags);
1632 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1633 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001634 }
1635
1636 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1637 mCurrentMotionAborted = false;
1638 }
1639 }
1640
1641 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001642 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001643 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1644 mCurrentCookedState.buttonState);
1645
1646 // Clear some transient state.
1647 mCurrentRawState.rawVScroll = 0;
1648 mCurrentRawState.rawHScroll = 0;
1649
1650 // Copy current touch to last touch in preparation for the next cycle.
1651 mLastRawState.copyFrom(mCurrentRawState);
1652 mLastCookedState.copyFrom(mCurrentCookedState);
1653}
1654
Garfield Tanc734e4f2021-01-15 20:01:39 -08001655void TouchInputMapper::updateTouchSpots() {
1656 if (!mConfig.showTouches || mPointerController == nullptr) {
1657 return;
1658 }
1659
1660 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1661 // clear touch spots.
1662 if (mDeviceMode != DeviceMode::DIRECT &&
1663 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1664 return;
1665 }
1666
1667 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1668 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1669
1670 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001671 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1672 mCurrentCookedState.cookedPointerData.idToIndex,
1673 mCurrentCookedState.cookedPointerData.touchingIdBits,
1674 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001675}
1676
1677bool TouchInputMapper::isTouchScreen() {
1678 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1679 mParameters.hasAssociatedDisplay;
1680}
1681
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001682void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001683 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001684 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1685 }
1686}
1687
1688void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1689 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1690 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1691
1692 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1693 float pressure = mExternalStylusState.pressure;
1694 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1695 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1696 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1697 }
1698 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1699 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1700
1701 PointerProperties& properties =
1702 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1703 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1704 properties.toolType = mExternalStylusState.toolType;
1705 }
1706 }
1707}
1708
1709bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001710 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001711 return false;
1712 }
1713
1714 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1715 state.rawPointerData.pointerCount != 0;
1716 if (initialDown) {
1717 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001718 if (DEBUG_STYLUS_FUSION) {
1719 ALOGD("Have both stylus and touch data, beginning fusion");
1720 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001721 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1722 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001723 if (DEBUG_STYLUS_FUSION) {
1724 ALOGD("Timeout expired, assuming touch is not a stylus.");
1725 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001726 resetExternalStylus();
1727 } else {
1728 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1729 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1730 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001731 if (DEBUG_STYLUS_FUSION) {
1732 ALOGD("No stylus data but stylus is connected, requesting timeout "
1733 "(%" PRId64 "ms)",
1734 mExternalStylusFusionTimeout);
1735 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001736 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1737 return true;
1738 }
1739 }
1740
1741 // Check if the stylus pointer has gone up.
1742 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001743 if (DEBUG_STYLUS_FUSION) {
1744 ALOGD("Stylus pointer is going up");
1745 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001746 mExternalStylusId = -1;
1747 }
1748
1749 return false;
1750}
1751
1752void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001753 if (mDeviceMode == DeviceMode::POINTER) {
1754 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001755 // Since this is a synthetic event, we can consider its latency to be zero
1756 const nsecs_t readTime = when;
1757 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001758 }
Michael Wright227c5542020-07-02 18:30:52 +01001759 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001760 if (mExternalStylusFusionTimeout < when) {
1761 processRawTouches(true /*timeout*/);
1762 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1763 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1764 }
1765 }
1766}
1767
1768void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1769 mExternalStylusState.copyFrom(state);
1770 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1771 // We're either in the middle of a fused stream of data or we're waiting on data before
1772 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1773 // data.
1774 mExternalStylusDataPending = true;
1775 processRawTouches(false /*timeout*/);
1776 }
1777}
1778
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001779bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001780 // Check for release of a virtual key.
1781 if (mCurrentVirtualKey.down) {
1782 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1783 // Pointer went up while virtual key was down.
1784 mCurrentVirtualKey.down = false;
1785 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001786 if (DEBUG_VIRTUAL_KEYS) {
1787 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1788 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1789 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001790 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001791 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1792 }
1793 return true;
1794 }
1795
1796 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1797 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1798 const RawPointerData::Pointer& pointer =
1799 mCurrentRawState.rawPointerData.pointerForId(id);
1800 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1801 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1802 // Pointer is still within the space of the virtual key.
1803 return true;
1804 }
1805 }
1806
1807 // Pointer left virtual key area or another pointer also went down.
1808 // Send key cancellation but do not consume the touch yet.
1809 // This is useful when the user swipes through from the virtual key area
1810 // into the main display surface.
1811 mCurrentVirtualKey.down = false;
1812 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001813 if (DEBUG_VIRTUAL_KEYS) {
1814 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1815 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1816 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001817 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001818 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1819 AKEY_EVENT_FLAG_CANCELED);
1820 }
1821 }
1822
1823 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1824 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1825 // Pointer just went down. Check for virtual key press or off-screen touches.
1826 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1827 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001828 // Skip checking whether the pointer is inside the physical frame if the device is in
1829 // unscaled mode.
1830 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1831 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001832 // If exactly one pointer went down, check for virtual key hit.
1833 // Otherwise we will drop the entire stroke.
1834 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1835 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1836 if (virtualKey) {
1837 mCurrentVirtualKey.down = true;
1838 mCurrentVirtualKey.downTime = when;
1839 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1840 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1841 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001842 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1843 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001844
1845 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001846 if (DEBUG_VIRTUAL_KEYS) {
1847 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1848 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1849 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001850 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001851 AKEY_EVENT_FLAG_FROM_SYSTEM |
1852 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1853 }
1854 }
1855 }
1856 return true;
1857 }
1858 }
1859
1860 // Disable all virtual key touches that happen within a short time interval of the
1861 // most recent touch within the screen area. The idea is to filter out stray
1862 // virtual key presses when interacting with the touch screen.
1863 //
1864 // Problems we're trying to solve:
1865 //
1866 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1867 // virtual key area that is implemented by a separate touch panel and accidentally
1868 // triggers a virtual key.
1869 //
1870 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1871 // area and accidentally triggers a virtual key. This often happens when virtual keys
1872 // are layed out below the screen near to where the on screen keyboard's space bar
1873 // is displayed.
1874 if (mConfig.virtualKeyQuietTime > 0 &&
1875 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001876 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001877 }
1878 return false;
1879}
1880
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001881void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001882 int32_t keyEventAction, int32_t keyEventFlags) {
1883 int32_t keyCode = mCurrentVirtualKey.keyCode;
1884 int32_t scanCode = mCurrentVirtualKey.scanCode;
1885 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001886 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001887 policyFlags |= POLICY_FLAG_VIRTUAL;
1888
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001889 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1890 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1891 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001892 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001893}
1894
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001895void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001896 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1897 if (!currentIdBits.isEmpty()) {
1898 int32_t metaState = getContext()->getGlobalMetaState();
1899 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001900 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1901 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001902 mCurrentCookedState.cookedPointerData.pointerProperties,
1903 mCurrentCookedState.cookedPointerData.pointerCoords,
1904 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1905 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1906 mCurrentMotionAborted = true;
1907 }
1908}
1909
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001910void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001911 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1912 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1913 int32_t metaState = getContext()->getGlobalMetaState();
1914 int32_t buttonState = mCurrentCookedState.buttonState;
1915
1916 if (currentIdBits == lastIdBits) {
1917 if (!currentIdBits.isEmpty()) {
1918 // No pointer id changes so this is a move event.
1919 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001920 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1921 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001922 mCurrentCookedState.cookedPointerData.pointerProperties,
1923 mCurrentCookedState.cookedPointerData.pointerCoords,
1924 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1925 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1926 }
1927 } else {
1928 // There may be pointers going up and pointers going down and pointers moving
1929 // all at the same time.
1930 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1931 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1932 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1933 BitSet32 dispatchedIdBits(lastIdBits.value);
1934
1935 // Update last coordinates of pointers that have moved so that we observe the new
1936 // pointer positions at the same time as other pointers that have just gone up.
1937 bool moveNeeded =
1938 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1939 mCurrentCookedState.cookedPointerData.pointerCoords,
1940 mCurrentCookedState.cookedPointerData.idToIndex,
1941 mLastCookedState.cookedPointerData.pointerProperties,
1942 mLastCookedState.cookedPointerData.pointerCoords,
1943 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1944 if (buttonState != mLastCookedState.buttonState) {
1945 moveNeeded = true;
1946 }
1947
1948 // Dispatch pointer up events.
1949 while (!upIdBits.isEmpty()) {
1950 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001951 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001952 if (isCanceled) {
1953 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1954 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001955 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001956 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001957 mLastCookedState.cookedPointerData.pointerProperties,
1958 mLastCookedState.cookedPointerData.pointerCoords,
1959 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
1960 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1961 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08001962 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001963 }
1964
1965 // Dispatch move events if any of the remaining pointers moved from their old locations.
1966 // Although applications receive new locations as part of individual pointer up
1967 // events, they do not generally handle them except when presented in a move event.
1968 if (moveNeeded && !moveIdBits.isEmpty()) {
1969 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001970 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1971 metaState, buttonState, 0,
1972 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001973 mCurrentCookedState.cookedPointerData.pointerCoords,
1974 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
1975 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1976 }
1977
1978 // Dispatch pointer down events using the new pointer locations.
1979 while (!downIdBits.isEmpty()) {
1980 uint32_t downId = downIdBits.clearFirstMarkedBit();
1981 dispatchedIdBits.markBit(downId);
1982
1983 if (dispatchedIdBits.count() == 1) {
1984 // First pointer is going down. Set down time.
1985 mDownTime = when;
1986 }
1987
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001988 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
1989 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001990 mCurrentCookedState.cookedPointerData.pointerProperties,
1991 mCurrentCookedState.cookedPointerData.pointerCoords,
1992 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
1993 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1994 }
1995 }
1996}
1997
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001999 if (mSentHoverEnter &&
2000 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2001 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2002 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002003 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2004 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002005 mLastCookedState.cookedPointerData.pointerProperties,
2006 mLastCookedState.cookedPointerData.pointerCoords,
2007 mLastCookedState.cookedPointerData.idToIndex,
2008 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2009 mOrientedYPrecision, mDownTime);
2010 mSentHoverEnter = false;
2011 }
2012}
2013
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002014void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2015 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002016 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2017 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2018 int32_t metaState = getContext()->getGlobalMetaState();
2019 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002020 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2021 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002022 mCurrentCookedState.cookedPointerData.pointerProperties,
2023 mCurrentCookedState.cookedPointerData.pointerCoords,
2024 mCurrentCookedState.cookedPointerData.idToIndex,
2025 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2026 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2027 mSentHoverEnter = true;
2028 }
2029
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002030 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2031 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002032 mCurrentCookedState.cookedPointerData.pointerProperties,
2033 mCurrentCookedState.cookedPointerData.pointerCoords,
2034 mCurrentCookedState.cookedPointerData.idToIndex,
2035 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2036 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2037 }
2038}
2039
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002040void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002041 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2042 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2043 const int32_t metaState = getContext()->getGlobalMetaState();
2044 int32_t buttonState = mLastCookedState.buttonState;
2045 while (!releasedButtons.isEmpty()) {
2046 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2047 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002048 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002049 actionButton, 0, metaState, buttonState, 0,
2050 mCurrentCookedState.cookedPointerData.pointerProperties,
2051 mCurrentCookedState.cookedPointerData.pointerCoords,
2052 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2053 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2054 }
2055}
2056
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002057void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002058 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2059 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2060 const int32_t metaState = getContext()->getGlobalMetaState();
2061 int32_t buttonState = mLastCookedState.buttonState;
2062 while (!pressedButtons.isEmpty()) {
2063 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2064 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002065 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2066 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002067 mCurrentCookedState.cookedPointerData.pointerProperties,
2068 mCurrentCookedState.cookedPointerData.pointerCoords,
2069 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2070 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2071 }
2072}
2073
2074const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2075 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2076 return cookedPointerData.touchingIdBits;
2077 }
2078 return cookedPointerData.hoveringIdBits;
2079}
2080
2081void TouchInputMapper::cookPointerData() {
2082 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2083
2084 mCurrentCookedState.cookedPointerData.clear();
2085 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2086 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2087 mCurrentRawState.rawPointerData.hoveringIdBits;
2088 mCurrentCookedState.cookedPointerData.touchingIdBits =
2089 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002090 mCurrentCookedState.cookedPointerData.canceledIdBits =
2091 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002092
2093 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2094 mCurrentCookedState.buttonState = 0;
2095 } else {
2096 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2097 }
2098
2099 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002100 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002101 for (uint32_t i = 0; i < currentPointerCount; i++) {
2102 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2103
2104 // Size
2105 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2106 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002107 case Calibration::SizeCalibration::GEOMETRIC:
2108 case Calibration::SizeCalibration::DIAMETER:
2109 case Calibration::SizeCalibration::BOX:
2110 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002111 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2112 touchMajor = in.touchMajor;
2113 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2114 toolMajor = in.toolMajor;
2115 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2116 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2117 : in.touchMajor;
2118 } else if (mRawPointerAxes.touchMajor.valid) {
2119 toolMajor = touchMajor = in.touchMajor;
2120 toolMinor = touchMinor =
2121 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2122 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2123 : in.touchMajor;
2124 } else if (mRawPointerAxes.toolMajor.valid) {
2125 touchMajor = toolMajor = in.toolMajor;
2126 touchMinor = toolMinor =
2127 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2128 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2129 : in.toolMajor;
2130 } else {
2131 ALOG_ASSERT(false,
2132 "No touch or tool axes. "
2133 "Size calibration should have been resolved to NONE.");
2134 touchMajor = 0;
2135 touchMinor = 0;
2136 toolMajor = 0;
2137 toolMinor = 0;
2138 size = 0;
2139 }
2140
2141 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2142 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2143 if (touchingCount > 1) {
2144 touchMajor /= touchingCount;
2145 touchMinor /= touchingCount;
2146 toolMajor /= touchingCount;
2147 toolMinor /= touchingCount;
2148 size /= touchingCount;
2149 }
2150 }
2151
Michael Wright227c5542020-07-02 18:30:52 +01002152 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002153 touchMajor *= mGeometricScale;
2154 touchMinor *= mGeometricScale;
2155 toolMajor *= mGeometricScale;
2156 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002157 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002158 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2159 touchMinor = touchMajor;
2160 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2161 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002162 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002163 touchMinor = touchMajor;
2164 toolMinor = toolMajor;
2165 }
2166
2167 mCalibration.applySizeScaleAndBias(&touchMajor);
2168 mCalibration.applySizeScaleAndBias(&touchMinor);
2169 mCalibration.applySizeScaleAndBias(&toolMajor);
2170 mCalibration.applySizeScaleAndBias(&toolMinor);
2171 size *= mSizeScale;
2172 break;
2173 default:
2174 touchMajor = 0;
2175 touchMinor = 0;
2176 toolMajor = 0;
2177 toolMinor = 0;
2178 size = 0;
2179 break;
2180 }
2181
2182 // Pressure
2183 float pressure;
2184 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002185 case Calibration::PressureCalibration::PHYSICAL:
2186 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002187 pressure = in.pressure * mPressureScale;
2188 break;
2189 default:
2190 pressure = in.isHovering ? 0 : 1;
2191 break;
2192 }
2193
2194 // Tilt and Orientation
2195 float tilt;
2196 float orientation;
2197 if (mHaveTilt) {
2198 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2199 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2200 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2201 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2202 } else {
2203 tilt = 0;
2204
2205 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002206 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002207 orientation = in.orientation * mOrientationScale;
2208 break;
Michael Wright227c5542020-07-02 18:30:52 +01002209 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002210 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2211 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2212 if (c1 != 0 || c2 != 0) {
2213 orientation = atan2f(c1, c2) * 0.5f;
2214 float confidence = hypotf(c1, c2);
2215 float scale = 1.0f + confidence / 16.0f;
2216 touchMajor *= scale;
2217 touchMinor /= scale;
2218 toolMajor *= scale;
2219 toolMinor /= scale;
2220 } else {
2221 orientation = 0;
2222 }
2223 break;
2224 }
2225 default:
2226 orientation = 0;
2227 }
2228 }
2229
2230 // Distance
2231 float distance;
2232 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002233 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002234 distance = in.distance * mDistanceScale;
2235 break;
2236 default:
2237 distance = 0;
2238 }
2239
2240 // Coverage
2241 int32_t rawLeft, rawTop, rawRight, rawBottom;
2242 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002243 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002244 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2245 rawRight = in.toolMinor & 0x0000ffff;
2246 rawBottom = in.toolMajor & 0x0000ffff;
2247 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2248 break;
2249 default:
2250 rawLeft = rawTop = rawRight = rawBottom = 0;
2251 break;
2252 }
2253
2254 // Adjust X,Y coords for device calibration
2255 // TODO: Adjust coverage coords?
2256 float xTransformed = in.x, yTransformed = in.y;
2257 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002258 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002259
Prabir Pradhan1728b212021-10-19 16:00:03 -07002260 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002261 float left, top, right, bottom;
2262
Prabir Pradhan1728b212021-10-19 16:00:03 -07002263 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002264 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002265 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2266 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2267 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2268 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002269 orientation -= M_PI_2;
2270 if (mOrientedRanges.haveOrientation &&
2271 orientation < mOrientedRanges.orientation.min) {
2272 orientation +=
2273 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2274 }
2275 break;
2276 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002277 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2278 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002279 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2280 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002281 orientation -= M_PI;
2282 if (mOrientedRanges.haveOrientation &&
2283 orientation < mOrientedRanges.orientation.min) {
2284 orientation +=
2285 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2286 }
2287 break;
2288 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002289 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2290 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002291 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2292 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002293 orientation += M_PI_2;
2294 if (mOrientedRanges.haveOrientation &&
2295 orientation > mOrientedRanges.orientation.max) {
2296 orientation -=
2297 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2298 }
2299 break;
2300 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002301 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2302 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2303 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2304 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002305 break;
2306 }
2307
2308 // Write output coords.
2309 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2310 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002311 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2312 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002313 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2314 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2315 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2316 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2317 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2318 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2319 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002320 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002321 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2322 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2323 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2324 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2325 } else {
2326 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2327 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2328 }
2329
Chris Ye364fdb52020-08-05 15:07:56 -07002330 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002331 uint32_t id = in.id;
2332 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2333 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2334 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2335 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2336 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2337 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2338 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2339 }
2340
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002341 // Write output properties.
2342 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002343 properties.clear();
2344 properties.id = id;
2345 properties.toolType = in.toolType;
2346
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002347 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002348 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002349 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002350 }
2351}
2352
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002353void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002354 PointerUsage pointerUsage) {
2355 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002356 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002357 mPointerUsage = pointerUsage;
2358 }
2359
2360 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002361 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002362 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002363 break;
Michael Wright227c5542020-07-02 18:30:52 +01002364 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002365 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002366 break;
Michael Wright227c5542020-07-02 18:30:52 +01002367 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002368 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002369 break;
Michael Wright227c5542020-07-02 18:30:52 +01002370 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002371 break;
2372 }
2373}
2374
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002375void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002376 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002377 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002378 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002379 break;
Michael Wright227c5542020-07-02 18:30:52 +01002380 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002381 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002382 break;
Michael Wright227c5542020-07-02 18:30:52 +01002383 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002384 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002385 break;
Michael Wright227c5542020-07-02 18:30:52 +01002386 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002387 break;
2388 }
2389
Michael Wright227c5542020-07-02 18:30:52 +01002390 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002391}
2392
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002393void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2394 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002395 // Update current gesture coordinates.
2396 bool cancelPreviousGesture, finishPreviousGesture;
2397 bool sendEvents =
2398 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2399 if (!sendEvents) {
2400 return;
2401 }
2402 if (finishPreviousGesture) {
2403 cancelPreviousGesture = false;
2404 }
2405
2406 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002407 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002408 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002409 if (finishPreviousGesture || cancelPreviousGesture) {
2410 mPointerController->clearSpots();
2411 }
2412
Michael Wright227c5542020-07-02 18:30:52 +01002413 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002414 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2415 mPointerGesture.currentGestureIdToIndex,
2416 mPointerGesture.currentGestureIdBits,
2417 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002418 }
2419 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002420 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002421 }
2422
2423 // Show or hide the pointer if needed.
2424 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002425 case PointerGesture::Mode::NEUTRAL:
2426 case PointerGesture::Mode::QUIET:
2427 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2428 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002429 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002430 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002431 }
2432 break;
Michael Wright227c5542020-07-02 18:30:52 +01002433 case PointerGesture::Mode::TAP:
2434 case PointerGesture::Mode::TAP_DRAG:
2435 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2436 case PointerGesture::Mode::HOVER:
2437 case PointerGesture::Mode::PRESS:
2438 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002439 // Unfade the pointer when the current gesture manipulates the
2440 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002441 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002442 break;
Michael Wright227c5542020-07-02 18:30:52 +01002443 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002444 // Fade the pointer when the current gesture manipulates a different
2445 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002446 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002447 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002448 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002449 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002450 }
2451 break;
2452 }
2453
2454 // Send events!
2455 int32_t metaState = getContext()->getGlobalMetaState();
2456 int32_t buttonState = mCurrentCookedState.buttonState;
2457
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002458 uint32_t flags = 0;
2459
2460 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2461 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2462 }
2463
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002464 // Update last coordinates of pointers that have moved so that we observe the new
2465 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002466 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2467 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2468 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2469 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2470 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2471 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002472 bool moveNeeded = false;
2473 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2474 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2475 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2476 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2477 mPointerGesture.lastGestureIdBits.value);
2478 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2479 mPointerGesture.currentGestureCoords,
2480 mPointerGesture.currentGestureIdToIndex,
2481 mPointerGesture.lastGestureProperties,
2482 mPointerGesture.lastGestureCoords,
2483 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2484 if (buttonState != mLastCookedState.buttonState) {
2485 moveNeeded = true;
2486 }
2487 }
2488
2489 // Send motion events for all pointers that went up or were canceled.
2490 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2491 if (!dispatchedGestureIdBits.isEmpty()) {
2492 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002493 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2494 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002495 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2496 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2497 mPointerGesture.downTime);
2498
2499 dispatchedGestureIdBits.clear();
2500 } else {
2501 BitSet32 upGestureIdBits;
2502 if (finishPreviousGesture) {
2503 upGestureIdBits = dispatchedGestureIdBits;
2504 } else {
2505 upGestureIdBits.value =
2506 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2507 }
2508 while (!upGestureIdBits.isEmpty()) {
2509 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2510
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002511 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002512 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002513 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002514 mPointerGesture.lastGestureCoords,
2515 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2516 0, mPointerGesture.downTime);
2517
2518 dispatchedGestureIdBits.clearBit(id);
2519 }
2520 }
2521 }
2522
2523 // Send motion events for all pointers that moved.
2524 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002525 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002526 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002527 mPointerGesture.currentGestureProperties,
2528 mPointerGesture.currentGestureCoords,
2529 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2530 mPointerGesture.downTime);
2531 }
2532
2533 // Send motion events for all pointers that went down.
2534 if (down) {
2535 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2536 ~dispatchedGestureIdBits.value);
2537 while (!downGestureIdBits.isEmpty()) {
2538 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2539 dispatchedGestureIdBits.markBit(id);
2540
2541 if (dispatchedGestureIdBits.count() == 1) {
2542 mPointerGesture.downTime = when;
2543 }
2544
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002545 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002546 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002547 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002548 mPointerGesture.currentGestureCoords,
2549 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2550 0, mPointerGesture.downTime);
2551 }
2552 }
2553
2554 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002555 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002556 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2557 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002558 mPointerGesture.currentGestureProperties,
2559 mPointerGesture.currentGestureCoords,
2560 mPointerGesture.currentGestureIdToIndex,
2561 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2562 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2563 // Synthesize a hover move event after all pointers go up to indicate that
2564 // the pointer is hovering again even if the user is not currently touching
2565 // the touch pad. This ensures that a view will receive a fresh hover enter
2566 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002567 float x, y;
2568 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002569
2570 PointerProperties pointerProperties;
2571 pointerProperties.clear();
2572 pointerProperties.id = 0;
2573 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2574
2575 PointerCoords pointerCoords;
2576 pointerCoords.clear();
2577 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2578 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2579
2580 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002581 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002582 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002583 metaState, buttonState, MotionClassification::NONE,
2584 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2585 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002586 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002587 }
2588
2589 // Update state.
2590 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2591 if (!down) {
2592 mPointerGesture.lastGestureIdBits.clear();
2593 } else {
2594 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2595 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2596 uint32_t id = idBits.clearFirstMarkedBit();
2597 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2598 mPointerGesture.lastGestureProperties[index].copyFrom(
2599 mPointerGesture.currentGestureProperties[index]);
2600 mPointerGesture.lastGestureCoords[index].copyFrom(
2601 mPointerGesture.currentGestureCoords[index]);
2602 mPointerGesture.lastGestureIdToIndex[id] = index;
2603 }
2604 }
2605}
2606
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002607void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002608 // Cancel previously dispatches pointers.
2609 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2610 int32_t metaState = getContext()->getGlobalMetaState();
2611 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002612 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2613 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002614 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2615 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2616 0, 0, mPointerGesture.downTime);
2617 }
2618
2619 // Reset the current pointer gesture.
2620 mPointerGesture.reset();
2621 mPointerVelocityControl.reset();
2622
2623 // Remove any current spots.
2624 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002625 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002626 mPointerController->clearSpots();
2627 }
2628}
2629
2630bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2631 bool* outFinishPreviousGesture, bool isTimeout) {
2632 *outCancelPreviousGesture = false;
2633 *outFinishPreviousGesture = false;
2634
2635 // Handle TAP timeout.
2636 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002637 if (DEBUG_GESTURES) {
2638 ALOGD("Gestures: Processing timeout");
2639 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002640
Michael Wright227c5542020-07-02 18:30:52 +01002641 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002642 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2643 // The tap/drag timeout has not yet expired.
2644 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2645 mConfig.pointerGestureTapDragInterval);
2646 } else {
2647 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002648 if (DEBUG_GESTURES) {
2649 ALOGD("Gestures: TAP finished");
2650 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002651 *outFinishPreviousGesture = true;
2652
2653 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002654 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002655 mPointerGesture.currentGestureIdBits.clear();
2656
2657 mPointerVelocityControl.reset();
2658 return true;
2659 }
2660 }
2661
2662 // We did not handle this timeout.
2663 return false;
2664 }
2665
2666 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2667 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2668
2669 // Update the velocity tracker.
2670 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002671 std::vector<VelocityTracker::Position> positions;
2672 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002673 uint32_t id = idBits.clearFirstMarkedBit();
2674 const RawPointerData::Pointer& pointer =
2675 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002676 float x = pointer.x * mPointerXMovementScale;
2677 float y = pointer.y * mPointerYMovementScale;
2678 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002679 }
2680 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2681 positions);
2682 }
2683
2684 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2685 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002686 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2687 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2688 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002689 mPointerGesture.resetTap();
2690 }
2691
2692 // Pick a new active touch id if needed.
2693 // Choose an arbitrary pointer that just went down, if there is one.
2694 // Otherwise choose an arbitrary remaining pointer.
2695 // This guarantees we always have an active touch id when there is at least one pointer.
2696 // We keep the same active touch id for as long as possible.
2697 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2698 int32_t activeTouchId = lastActiveTouchId;
2699 if (activeTouchId < 0) {
2700 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2701 activeTouchId = mPointerGesture.activeTouchId =
2702 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2703 mPointerGesture.firstTouchTime = when;
2704 }
2705 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2706 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2707 activeTouchId = mPointerGesture.activeTouchId =
2708 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2709 } else {
2710 activeTouchId = mPointerGesture.activeTouchId = -1;
2711 }
2712 }
2713
2714 // Determine whether we are in quiet time.
2715 bool isQuietTime = false;
2716 if (activeTouchId < 0) {
2717 mPointerGesture.resetQuietTime();
2718 } else {
2719 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2720 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002721 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2722 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2723 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002724 currentFingerCount < 2) {
2725 // Enter quiet time when exiting swipe or freeform state.
2726 // This is to prevent accidentally entering the hover state and flinging the
2727 // pointer when finishing a swipe and there is still one pointer left onscreen.
2728 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002729 } else if (mPointerGesture.lastGestureMode ==
2730 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002731 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2732 // Enter quiet time when releasing the button and there are still two or more
2733 // fingers down. This may indicate that one finger was used to press the button
2734 // but it has not gone up yet.
2735 isQuietTime = true;
2736 }
2737 if (isQuietTime) {
2738 mPointerGesture.quietTime = when;
2739 }
2740 }
2741 }
2742
2743 // Switch states based on button and pointer state.
2744 if (isQuietTime) {
2745 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002746 if (DEBUG_GESTURES) {
2747 ALOGD("Gestures: QUIET for next %0.3fms",
2748 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2749 0.000001f);
2750 }
Michael Wright227c5542020-07-02 18:30:52 +01002751 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002752 *outFinishPreviousGesture = true;
2753 }
2754
2755 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002756 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002757 mPointerGesture.currentGestureIdBits.clear();
2758
2759 mPointerVelocityControl.reset();
2760 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2761 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2762 // The pointer follows the active touch point.
2763 // Emit DOWN, MOVE, UP events at the pointer location.
2764 //
2765 // Only the active touch matters; other fingers are ignored. This policy helps
2766 // to handle the case where the user places a second finger on the touch pad
2767 // to apply the necessary force to depress an integrated button below the surface.
2768 // We don't want the second finger to be delivered to applications.
2769 //
2770 // For this to work well, we need to make sure to track the pointer that is really
2771 // active. If the user first puts one finger down to click then adds another
2772 // finger to drag then the active pointer should switch to the finger that is
2773 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002774 if (DEBUG_GESTURES) {
2775 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2776 "currentFingerCount=%d",
2777 activeTouchId, currentFingerCount);
2778 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002779 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002780 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002781 *outFinishPreviousGesture = true;
2782 mPointerGesture.activeGestureId = 0;
2783 }
2784
2785 // Switch pointers if needed.
2786 // Find the fastest pointer and follow it.
2787 if (activeTouchId >= 0 && currentFingerCount > 1) {
2788 int32_t bestId = -1;
2789 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2790 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2791 uint32_t id = idBits.clearFirstMarkedBit();
2792 float vx, vy;
2793 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2794 float speed = hypotf(vx, vy);
2795 if (speed > bestSpeed) {
2796 bestId = id;
2797 bestSpeed = speed;
2798 }
2799 }
2800 }
2801 if (bestId >= 0 && bestId != activeTouchId) {
2802 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002803 if (DEBUG_GESTURES) {
2804 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2805 "bestId=%d, bestSpeed=%0.3f",
2806 bestId, bestSpeed);
2807 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002808 }
2809 }
2810
2811 float deltaX = 0, deltaY = 0;
2812 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2813 const RawPointerData::Pointer& currentPointer =
2814 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2815 const RawPointerData::Pointer& lastPointer =
2816 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2817 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2818 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2819
Prabir Pradhan1728b212021-10-19 16:00:03 -07002820 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002821 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2822
2823 // Move the pointer using a relative motion.
2824 // When using spots, the click will occur at the position of the anchor
2825 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002826 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002827 } else {
2828 mPointerVelocityControl.reset();
2829 }
2830
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002831 float x, y;
2832 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002833
Michael Wright227c5542020-07-02 18:30:52 +01002834 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002835 mPointerGesture.currentGestureIdBits.clear();
2836 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2837 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2838 mPointerGesture.currentGestureProperties[0].clear();
2839 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2840 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2841 mPointerGesture.currentGestureCoords[0].clear();
2842 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2843 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2844 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2845 } else if (currentFingerCount == 0) {
2846 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002847 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002848 *outFinishPreviousGesture = true;
2849 }
2850
2851 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2852 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2853 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002854 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2855 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002856 lastFingerCount == 1) {
2857 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002858 float x, y;
2859 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002860 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2861 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002862 if (DEBUG_GESTURES) {
2863 ALOGD("Gestures: TAP");
2864 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002865
2866 mPointerGesture.tapUpTime = when;
2867 getContext()->requestTimeoutAtTime(when +
2868 mConfig.pointerGestureTapDragInterval);
2869
2870 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002871 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002872 mPointerGesture.currentGestureIdBits.clear();
2873 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2874 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2875 mPointerGesture.currentGestureProperties[0].clear();
2876 mPointerGesture.currentGestureProperties[0].id =
2877 mPointerGesture.activeGestureId;
2878 mPointerGesture.currentGestureProperties[0].toolType =
2879 AMOTION_EVENT_TOOL_TYPE_FINGER;
2880 mPointerGesture.currentGestureCoords[0].clear();
2881 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2882 mPointerGesture.tapX);
2883 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2884 mPointerGesture.tapY);
2885 mPointerGesture.currentGestureCoords[0]
2886 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2887
2888 tapped = true;
2889 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002890 if (DEBUG_GESTURES) {
2891 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2892 y - mPointerGesture.tapY);
2893 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002894 }
2895 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002896 if (DEBUG_GESTURES) {
2897 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2898 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2899 (when - mPointerGesture.tapDownTime) * 0.000001f);
2900 } else {
2901 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2902 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002903 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002904 }
2905 }
2906
2907 mPointerVelocityControl.reset();
2908
2909 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002910 if (DEBUG_GESTURES) {
2911 ALOGD("Gestures: NEUTRAL");
2912 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002913 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002914 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002915 mPointerGesture.currentGestureIdBits.clear();
2916 }
2917 } else if (currentFingerCount == 1) {
2918 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2919 // The pointer follows the active touch point.
2920 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2921 // When in TAP_DRAG, emit MOVE events at the pointer location.
2922 ALOG_ASSERT(activeTouchId >= 0);
2923
Michael Wright227c5542020-07-02 18:30:52 +01002924 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2925 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002926 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002927 float x, y;
2928 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002929 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2930 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002931 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002932 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002933 if (DEBUG_GESTURES) {
2934 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2935 x - mPointerGesture.tapX, 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 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2941 (when - mPointerGesture.tapUpTime) * 0.000001f);
2942 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002943 }
Michael Wright227c5542020-07-02 18:30:52 +01002944 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2945 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002946 }
2947
2948 float deltaX = 0, deltaY = 0;
2949 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2950 const RawPointerData::Pointer& currentPointer =
2951 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2952 const RawPointerData::Pointer& lastPointer =
2953 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2954 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2955 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2956
Prabir Pradhan1728b212021-10-19 16:00:03 -07002957 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002958 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2959
2960 // Move the pointer using a relative motion.
2961 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002962 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002963 } else {
2964 mPointerVelocityControl.reset();
2965 }
2966
2967 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01002968 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002969 if (DEBUG_GESTURES) {
2970 ALOGD("Gestures: TAP_DRAG");
2971 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002972 down = true;
2973 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002974 if (DEBUG_GESTURES) {
2975 ALOGD("Gestures: HOVER");
2976 }
Michael Wright227c5542020-07-02 18:30:52 +01002977 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002978 *outFinishPreviousGesture = true;
2979 }
2980 mPointerGesture.activeGestureId = 0;
2981 down = false;
2982 }
2983
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002984 float x, y;
2985 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002986
2987 mPointerGesture.currentGestureIdBits.clear();
2988 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2989 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2990 mPointerGesture.currentGestureProperties[0].clear();
2991 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2992 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2993 mPointerGesture.currentGestureCoords[0].clear();
2994 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2995 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2996 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
2997 down ? 1.0f : 0.0f);
2998
2999 if (lastFingerCount == 0 && currentFingerCount != 0) {
3000 mPointerGesture.resetTap();
3001 mPointerGesture.tapDownTime = when;
3002 mPointerGesture.tapX = x;
3003 mPointerGesture.tapY = y;
3004 }
3005 } else {
3006 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3007 // We need to provide feedback for each finger that goes down so we cannot wait
3008 // for the fingers to move before deciding what to do.
3009 //
3010 // The ambiguous case is deciding what to do when there are two fingers down but they
3011 // have not moved enough to determine whether they are part of a drag or part of a
3012 // freeform gesture, or just a press or long-press at the pointer location.
3013 //
3014 // When there are two fingers we start with the PRESS hypothesis and we generate a
3015 // down at the pointer location.
3016 //
3017 // When the two fingers move enough or when additional fingers are added, we make
3018 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3019 ALOG_ASSERT(activeTouchId >= 0);
3020
3021 bool settled = when >=
3022 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003023 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3024 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3025 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003026 *outFinishPreviousGesture = true;
3027 } else if (!settled && currentFingerCount > lastFingerCount) {
3028 // Additional pointers have gone down but not yet settled.
3029 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003030 if (DEBUG_GESTURES) {
3031 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3032 "MULTITOUCH, settle time remaining %0.3fms",
3033 (mPointerGesture.firstTouchTime +
3034 mConfig.pointerGestureMultitouchSettleInterval - when) *
3035 0.000001f);
3036 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003037 *outCancelPreviousGesture = true;
3038 } else {
3039 // Continue previous gesture.
3040 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3041 }
3042
3043 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003044 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003045 mPointerGesture.activeGestureId = 0;
3046 mPointerGesture.referenceIdBits.clear();
3047 mPointerVelocityControl.reset();
3048
3049 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003050 if (DEBUG_GESTURES) {
3051 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3052 "settle time remaining %0.3fms",
3053 (mPointerGesture.firstTouchTime +
3054 mConfig.pointerGestureMultitouchSettleInterval - when) *
3055 0.000001f);
3056 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003057 mCurrentRawState.rawPointerData
3058 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3059 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003060 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3061 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003062 }
3063
3064 // Clear the reference deltas for fingers not yet included in the reference calculation.
3065 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3066 ~mPointerGesture.referenceIdBits.value);
3067 !idBits.isEmpty();) {
3068 uint32_t id = idBits.clearFirstMarkedBit();
3069 mPointerGesture.referenceDeltas[id].dx = 0;
3070 mPointerGesture.referenceDeltas[id].dy = 0;
3071 }
3072 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3073
3074 // Add delta for all fingers and calculate a common movement delta.
3075 float commonDeltaX = 0, commonDeltaY = 0;
3076 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3077 mCurrentCookedState.fingerIdBits.value);
3078 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3079 bool first = (idBits == commonIdBits);
3080 uint32_t id = idBits.clearFirstMarkedBit();
3081 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3082 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3083 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3084 delta.dx += cpd.x - lpd.x;
3085 delta.dy += cpd.y - lpd.y;
3086
3087 if (first) {
3088 commonDeltaX = delta.dx;
3089 commonDeltaY = delta.dy;
3090 } else {
3091 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3092 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3093 }
3094 }
3095
3096 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003097 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003098 float dist[MAX_POINTER_ID + 1];
3099 int32_t distOverThreshold = 0;
3100 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3101 uint32_t id = idBits.clearFirstMarkedBit();
3102 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3103 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3104 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3105 distOverThreshold += 1;
3106 }
3107 }
3108
3109 // Only transition when at least two pointers have moved further than
3110 // the minimum distance threshold.
3111 if (distOverThreshold >= 2) {
3112 if (currentFingerCount > 2) {
3113 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003114 if (DEBUG_GESTURES) {
3115 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3116 currentFingerCount);
3117 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003118 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003119 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003120 } else {
3121 // There are exactly two pointers.
3122 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3123 uint32_t id1 = idBits.clearFirstMarkedBit();
3124 uint32_t id2 = idBits.firstMarkedBit();
3125 const RawPointerData::Pointer& p1 =
3126 mCurrentRawState.rawPointerData.pointerForId(id1);
3127 const RawPointerData::Pointer& p2 =
3128 mCurrentRawState.rawPointerData.pointerForId(id2);
3129 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3130 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3131 // There are two pointers but they are too far apart for a SWIPE,
3132 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003133 if (DEBUG_GESTURES) {
3134 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3135 "%0.3f",
3136 mutualDistance, mPointerGestureMaxSwipeWidth);
3137 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003138 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003139 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003140 } else {
3141 // There are two pointers. Wait for both pointers to start moving
3142 // before deciding whether this is a SWIPE or FREEFORM gesture.
3143 float dist1 = dist[id1];
3144 float dist2 = dist[id2];
3145 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3146 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3147 // Calculate the dot product of the displacement vectors.
3148 // When the vectors are oriented in approximately the same direction,
3149 // the angle betweeen them is near zero and the cosine of the angle
3150 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3151 // mag(v2).
3152 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3153 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3154 float dx1 = delta1.dx * mPointerXZoomScale;
3155 float dy1 = delta1.dy * mPointerYZoomScale;
3156 float dx2 = delta2.dx * mPointerXZoomScale;
3157 float dy2 = delta2.dy * mPointerYZoomScale;
3158 float dot = dx1 * dx2 + dy1 * dy2;
3159 float cosine = dot / (dist1 * dist2); // denominator always > 0
3160 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3161 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003162 if (DEBUG_GESTURES) {
3163 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3164 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3165 "cosine %0.3f >= %0.3f",
3166 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3167 mConfig.pointerGestureMultitouchMinDistance, cosine,
3168 mConfig.pointerGestureSwipeTransitionAngleCosine);
3169 }
Michael Wright227c5542020-07-02 18:30:52 +01003170 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003171 } else {
3172 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003173 if (DEBUG_GESTURES) {
3174 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3175 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3176 "cosine %0.3f < %0.3f",
3177 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3178 mConfig.pointerGestureMultitouchMinDistance, cosine,
3179 mConfig.pointerGestureSwipeTransitionAngleCosine);
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 }
3184 }
3185 }
3186 }
3187 }
Michael Wright227c5542020-07-02 18:30:52 +01003188 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003189 // Switch from SWIPE to FREEFORM if additional pointers go down.
3190 // Cancel previous gesture.
3191 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003192 if (DEBUG_GESTURES) {
3193 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3194 currentFingerCount);
3195 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003196 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003197 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003198 }
3199 }
3200
3201 // Move the reference points based on the overall group motion of the fingers
3202 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003203 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003204 (commonDeltaX || commonDeltaY)) {
3205 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3206 uint32_t id = idBits.clearFirstMarkedBit();
3207 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3208 delta.dx = 0;
3209 delta.dy = 0;
3210 }
3211
3212 mPointerGesture.referenceTouchX += commonDeltaX;
3213 mPointerGesture.referenceTouchY += commonDeltaY;
3214
3215 commonDeltaX *= mPointerXMovementScale;
3216 commonDeltaY *= mPointerYMovementScale;
3217
Prabir Pradhan1728b212021-10-19 16:00:03 -07003218 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003219 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3220
3221 mPointerGesture.referenceGestureX += commonDeltaX;
3222 mPointerGesture.referenceGestureY += commonDeltaY;
3223 }
3224
3225 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003226 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3227 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003228 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003229 if (DEBUG_GESTURES) {
3230 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3231 "activeGestureId=%d, currentTouchPointerCount=%d",
3232 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3233 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003234 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3235
3236 mPointerGesture.currentGestureIdBits.clear();
3237 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3238 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3239 mPointerGesture.currentGestureProperties[0].clear();
3240 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3241 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3242 mPointerGesture.currentGestureCoords[0].clear();
3243 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3244 mPointerGesture.referenceGestureX);
3245 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3246 mPointerGesture.referenceGestureY);
3247 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003248 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003249 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003250 if (DEBUG_GESTURES) {
3251 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3252 "activeGestureId=%d, currentTouchPointerCount=%d",
3253 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3254 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003255 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3256
3257 mPointerGesture.currentGestureIdBits.clear();
3258
3259 BitSet32 mappedTouchIdBits;
3260 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003261 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003262 // Initially, assign the active gesture id to the active touch point
3263 // if there is one. No other touch id bits are mapped yet.
3264 if (!*outCancelPreviousGesture) {
3265 mappedTouchIdBits.markBit(activeTouchId);
3266 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3267 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3268 mPointerGesture.activeGestureId;
3269 } else {
3270 mPointerGesture.activeGestureId = -1;
3271 }
3272 } else {
3273 // Otherwise, assume we mapped all touches from the previous frame.
3274 // Reuse all mappings that are still applicable.
3275 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3276 mCurrentCookedState.fingerIdBits.value;
3277 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3278
3279 // Check whether we need to choose a new active gesture id because the
3280 // current went went up.
3281 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3282 ~mCurrentCookedState.fingerIdBits.value);
3283 !upTouchIdBits.isEmpty();) {
3284 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3285 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3286 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3287 mPointerGesture.activeGestureId = -1;
3288 break;
3289 }
3290 }
3291 }
3292
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003293 if (DEBUG_GESTURES) {
3294 ALOGD("Gestures: FREEFORM follow up "
3295 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3296 "activeGestureId=%d",
3297 mappedTouchIdBits.value, usedGestureIdBits.value,
3298 mPointerGesture.activeGestureId);
3299 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003300
3301 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3302 for (uint32_t i = 0; i < currentFingerCount; i++) {
3303 uint32_t touchId = idBits.clearFirstMarkedBit();
3304 uint32_t gestureId;
3305 if (!mappedTouchIdBits.hasBit(touchId)) {
3306 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3307 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003308 if (DEBUG_GESTURES) {
3309 ALOGD("Gestures: FREEFORM "
3310 "new mapping for touch id %d -> gesture id %d",
3311 touchId, gestureId);
3312 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003313 } else {
3314 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003315 if (DEBUG_GESTURES) {
3316 ALOGD("Gestures: FREEFORM "
3317 "existing mapping for touch id %d -> gesture id %d",
3318 touchId, gestureId);
3319 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003320 }
3321 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3322 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3323
3324 const RawPointerData::Pointer& pointer =
3325 mCurrentRawState.rawPointerData.pointerForId(touchId);
3326 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3327 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003328 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003329
3330 mPointerGesture.currentGestureProperties[i].clear();
3331 mPointerGesture.currentGestureProperties[i].id = gestureId;
3332 mPointerGesture.currentGestureProperties[i].toolType =
3333 AMOTION_EVENT_TOOL_TYPE_FINGER;
3334 mPointerGesture.currentGestureCoords[i].clear();
3335 mPointerGesture.currentGestureCoords[i]
3336 .setAxisValue(AMOTION_EVENT_AXIS_X,
3337 mPointerGesture.referenceGestureX + deltaX);
3338 mPointerGesture.currentGestureCoords[i]
3339 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3340 mPointerGesture.referenceGestureY + deltaY);
3341 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3342 1.0f);
3343 }
3344
3345 if (mPointerGesture.activeGestureId < 0) {
3346 mPointerGesture.activeGestureId =
3347 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003348 if (DEBUG_GESTURES) {
3349 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3350 mPointerGesture.activeGestureId);
3351 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003352 }
3353 }
3354 }
3355
3356 mPointerController->setButtonState(mCurrentRawState.buttonState);
3357
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003358 if (DEBUG_GESTURES) {
3359 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3360 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3361 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3362 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3363 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3364 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3365 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3366 uint32_t id = idBits.clearFirstMarkedBit();
3367 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3368 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3369 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3370 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3371 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3372 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3373 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3374 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3375 }
3376 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3377 uint32_t id = idBits.clearFirstMarkedBit();
3378 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3379 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3380 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3381 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3382 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3383 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3384 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3385 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3386 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003387 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003388 return true;
3389}
3390
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003391void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003392 mPointerSimple.currentCoords.clear();
3393 mPointerSimple.currentProperties.clear();
3394
3395 bool down, hovering;
3396 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3397 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3398 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003399 mPointerController
3400 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3401 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003402
3403 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3404 down = !hovering;
3405
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003406 float x, y;
3407 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003408 mPointerSimple.currentCoords.copyFrom(
3409 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3410 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3411 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3412 mPointerSimple.currentProperties.id = 0;
3413 mPointerSimple.currentProperties.toolType =
3414 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3415 } else {
3416 down = false;
3417 hovering = false;
3418 }
3419
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003421}
3422
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3424 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003425}
3426
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003428 mPointerSimple.currentCoords.clear();
3429 mPointerSimple.currentProperties.clear();
3430
3431 bool down, hovering;
3432 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3433 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3434 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3435 float deltaX = 0, deltaY = 0;
3436 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3437 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3438 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3439 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3440 mPointerXMovementScale;
3441 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3442 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3443 mPointerYMovementScale;
3444
Prabir Pradhan1728b212021-10-19 16:00:03 -07003445 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003446 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3447
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003448 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003449 } else {
3450 mPointerVelocityControl.reset();
3451 }
3452
3453 down = isPointerDown(mCurrentRawState.buttonState);
3454 hovering = !down;
3455
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003456 float x, y;
3457 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003458 mPointerSimple.currentCoords.copyFrom(
3459 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3460 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3461 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3462 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3463 hovering ? 0.0f : 1.0f);
3464 mPointerSimple.currentProperties.id = 0;
3465 mPointerSimple.currentProperties.toolType =
3466 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3467 } else {
3468 mPointerVelocityControl.reset();
3469
3470 down = false;
3471 hovering = false;
3472 }
3473
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003475}
3476
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003477void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3478 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003479
3480 mPointerVelocityControl.reset();
3481}
3482
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003483void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3484 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003485 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003486
3487 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003488 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003489 mPointerController->clearSpots();
3490 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003491 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003492 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003493 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003494 }
Garfield Tan9514d782020-11-10 16:37:23 -08003495 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003496
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003497 float xCursorPosition, yCursorPosition;
3498 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003499
3500 if (mPointerSimple.down && !down) {
3501 mPointerSimple.down = false;
3502
3503 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003504 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3505 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003506 mLastRawState.buttonState, MotionClassification::NONE,
3507 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3508 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3509 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3510 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003511 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003512 }
3513
3514 if (mPointerSimple.hovering && !hovering) {
3515 mPointerSimple.hovering = false;
3516
3517 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3519 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3520 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003521 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3522 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3523 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3524 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003525 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003526 }
3527
3528 if (down) {
3529 if (!mPointerSimple.down) {
3530 mPointerSimple.down = true;
3531 mPointerSimple.downTime = when;
3532
3533 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003535 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3536 metaState, mCurrentRawState.buttonState,
3537 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3538 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3539 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3540 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003541 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003542 }
3543
3544 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3546 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003547 mCurrentRawState.buttonState, MotionClassification::NONE,
3548 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3549 &mPointerSimple.currentCoords, mOrientedXPrecision,
3550 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3551 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003552 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003553 }
3554
3555 if (hovering) {
3556 if (!mPointerSimple.hovering) {
3557 mPointerSimple.hovering = true;
3558
3559 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003561 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3562 metaState, mCurrentRawState.buttonState,
3563 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3564 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3565 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3566 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003567 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003568 }
3569
3570 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3572 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3573 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003574 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3575 &mPointerSimple.currentCoords, mOrientedXPrecision,
3576 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3577 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003578 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003579 }
3580
3581 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3582 float vscroll = mCurrentRawState.rawVScroll;
3583 float hscroll = mCurrentRawState.rawHScroll;
3584 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3585 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3586
3587 // Send scroll.
3588 PointerCoords pointerCoords;
3589 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3590 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3591 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3592
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003593 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3594 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003595 mCurrentRawState.buttonState, MotionClassification::NONE,
3596 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3597 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3598 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3599 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003600 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003601 }
3602
3603 // Save state.
3604 if (down || hovering) {
3605 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3606 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3607 } else {
3608 mPointerSimple.reset();
3609 }
3610}
3611
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003613 mPointerSimple.currentCoords.clear();
3614 mPointerSimple.currentProperties.clear();
3615
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003616 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003617}
3618
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3620 uint32_t source, int32_t action, int32_t actionButton,
3621 int32_t flags, int32_t metaState, int32_t buttonState,
3622 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003623 const PointerCoords* coords, const uint32_t* idToIndex,
3624 BitSet32 idBits, int32_t changedId, float xPrecision,
3625 float yPrecision, nsecs_t downTime) {
3626 PointerCoords pointerCoords[MAX_POINTERS];
3627 PointerProperties pointerProperties[MAX_POINTERS];
3628 uint32_t pointerCount = 0;
3629 while (!idBits.isEmpty()) {
3630 uint32_t id = idBits.clearFirstMarkedBit();
3631 uint32_t index = idToIndex[id];
3632 pointerProperties[pointerCount].copyFrom(properties[index]);
3633 pointerCoords[pointerCount].copyFrom(coords[index]);
3634
3635 if (changedId >= 0 && id == uint32_t(changedId)) {
3636 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3637 }
3638
3639 pointerCount += 1;
3640 }
3641
3642 ALOG_ASSERT(pointerCount != 0);
3643
3644 if (changedId >= 0 && pointerCount == 1) {
3645 // Replace initial down and final up action.
3646 // We can compare the action without masking off the changed pointer index
3647 // because we know the index is 0.
3648 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3649 action = AMOTION_EVENT_ACTION_DOWN;
3650 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003651 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3652 action = AMOTION_EVENT_ACTION_CANCEL;
3653 } else {
3654 action = AMOTION_EVENT_ACTION_UP;
3655 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003656 } else {
3657 // Can't happen.
3658 ALOG_ASSERT(false);
3659 }
3660 }
3661 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3662 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003663 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003664 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003665 }
3666 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3667 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003668 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003669 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003670 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003671 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3672 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003673 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3674 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3675 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003676 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003677}
3678
3679bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3680 const PointerCoords* inCoords,
3681 const uint32_t* inIdToIndex,
3682 PointerProperties* outProperties,
3683 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3684 BitSet32 idBits) const {
3685 bool changed = false;
3686 while (!idBits.isEmpty()) {
3687 uint32_t id = idBits.clearFirstMarkedBit();
3688 uint32_t inIndex = inIdToIndex[id];
3689 uint32_t outIndex = outIdToIndex[id];
3690
3691 const PointerProperties& curInProperties = inProperties[inIndex];
3692 const PointerCoords& curInCoords = inCoords[inIndex];
3693 PointerProperties& curOutProperties = outProperties[outIndex];
3694 PointerCoords& curOutCoords = outCoords[outIndex];
3695
3696 if (curInProperties != curOutProperties) {
3697 curOutProperties.copyFrom(curInProperties);
3698 changed = true;
3699 }
3700
3701 if (curInCoords != curOutCoords) {
3702 curOutCoords.copyFrom(curInCoords);
3703 changed = true;
3704 }
3705 }
3706 return changed;
3707}
3708
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003709void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3710 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3711 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003712}
3713
Prabir Pradhan1728b212021-10-19 16:00:03 -07003714// Transform input device coordinates to display panel coordinates.
3715void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003716 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3717 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3718
arthurhunga36b28e2020-12-29 20:28:15 +08003719 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3720 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3721
Prabir Pradhan1728b212021-10-19 16:00:03 -07003722 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003723 // 0 - no swap and reverse.
3724 // 90 - swap x/y and reverse y.
3725 // 180 - reverse x, y.
3726 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003727 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003728 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003729 x = xScaled;
3730 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003731 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003732 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003733 y = xScaledMax;
3734 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003735 break;
3736 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003737 x = xScaledMax;
3738 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003739 break;
3740 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003741 y = xScaled;
3742 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003743 break;
3744 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003745 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003746 }
3747}
3748
Prabir Pradhan1728b212021-10-19 16:00:03 -07003749bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003750 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3751 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3752
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003753 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003754 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003755 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003756 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003757}
3758
3759const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3760 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003761 if (DEBUG_VIRTUAL_KEYS) {
3762 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3763 "left=%d, top=%d, right=%d, bottom=%d",
3764 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3765 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3766 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003767
3768 if (virtualKey.isHit(x, y)) {
3769 return &virtualKey;
3770 }
3771 }
3772
3773 return nullptr;
3774}
3775
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003776void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3777 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3778 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003779
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003780 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003781
3782 if (currentPointerCount == 0) {
3783 // No pointers to assign.
3784 return;
3785 }
3786
3787 if (lastPointerCount == 0) {
3788 // All pointers are new.
3789 for (uint32_t i = 0; i < currentPointerCount; i++) {
3790 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003791 current.rawPointerData.pointers[i].id = id;
3792 current.rawPointerData.idToIndex[id] = i;
3793 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003794 }
3795 return;
3796 }
3797
3798 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003799 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003800 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003801 uint32_t id = last.rawPointerData.pointers[0].id;
3802 current.rawPointerData.pointers[0].id = id;
3803 current.rawPointerData.idToIndex[id] = 0;
3804 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003805 return;
3806 }
3807
3808 // General case.
3809 // We build a heap of squared euclidean distances between current and last pointers
3810 // associated with the current and last pointer indices. Then, we find the best
3811 // match (by distance) for each current pointer.
3812 // The pointers must have the same tool type but it is possible for them to
3813 // transition from hovering to touching or vice-versa while retaining the same id.
3814 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3815
3816 uint32_t heapSize = 0;
3817 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3818 currentPointerIndex++) {
3819 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3820 lastPointerIndex++) {
3821 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003822 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003823 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003824 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003825 if (currentPointer.toolType == lastPointer.toolType) {
3826 int64_t deltaX = currentPointer.x - lastPointer.x;
3827 int64_t deltaY = currentPointer.y - lastPointer.y;
3828
3829 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3830
3831 // Insert new element into the heap (sift up).
3832 heap[heapSize].currentPointerIndex = currentPointerIndex;
3833 heap[heapSize].lastPointerIndex = lastPointerIndex;
3834 heap[heapSize].distance = distance;
3835 heapSize += 1;
3836 }
3837 }
3838 }
3839
3840 // Heapify
3841 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3842 startIndex -= 1;
3843 for (uint32_t parentIndex = startIndex;;) {
3844 uint32_t childIndex = parentIndex * 2 + 1;
3845 if (childIndex >= heapSize) {
3846 break;
3847 }
3848
3849 if (childIndex + 1 < heapSize &&
3850 heap[childIndex + 1].distance < heap[childIndex].distance) {
3851 childIndex += 1;
3852 }
3853
3854 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3855 break;
3856 }
3857
3858 swap(heap[parentIndex], heap[childIndex]);
3859 parentIndex = childIndex;
3860 }
3861 }
3862
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003863 if (DEBUG_POINTER_ASSIGNMENT) {
3864 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3865 for (size_t i = 0; i < heapSize; i++) {
3866 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3867 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3868 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003869 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003870
3871 // Pull matches out by increasing order of distance.
3872 // To avoid reassigning pointers that have already been matched, the loop keeps track
3873 // of which last and current pointers have been matched using the matchedXXXBits variables.
3874 // It also tracks the used pointer id bits.
3875 BitSet32 matchedLastBits(0);
3876 BitSet32 matchedCurrentBits(0);
3877 BitSet32 usedIdBits(0);
3878 bool first = true;
3879 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3880 while (heapSize > 0) {
3881 if (first) {
3882 // The first time through the loop, we just consume the root element of
3883 // the heap (the one with smallest distance).
3884 first = false;
3885 } else {
3886 // Previous iterations consumed the root element of the heap.
3887 // Pop root element off of the heap (sift down).
3888 heap[0] = heap[heapSize];
3889 for (uint32_t parentIndex = 0;;) {
3890 uint32_t childIndex = parentIndex * 2 + 1;
3891 if (childIndex >= heapSize) {
3892 break;
3893 }
3894
3895 if (childIndex + 1 < heapSize &&
3896 heap[childIndex + 1].distance < heap[childIndex].distance) {
3897 childIndex += 1;
3898 }
3899
3900 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3901 break;
3902 }
3903
3904 swap(heap[parentIndex], heap[childIndex]);
3905 parentIndex = childIndex;
3906 }
3907
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003908 if (DEBUG_POINTER_ASSIGNMENT) {
3909 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3910 for (size_t j = 0; j < heapSize; j++) {
3911 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3912 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3913 heap[j].distance);
3914 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003915 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003916 }
3917
3918 heapSize -= 1;
3919
3920 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3921 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3922
3923 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3924 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3925
3926 matchedCurrentBits.markBit(currentPointerIndex);
3927 matchedLastBits.markBit(lastPointerIndex);
3928
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003929 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3930 current.rawPointerData.pointers[currentPointerIndex].id = id;
3931 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3932 current.rawPointerData.markIdBit(id,
3933 current.rawPointerData.isHovering(
3934 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003935 usedIdBits.markBit(id);
3936
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003937 if (DEBUG_POINTER_ASSIGNMENT) {
3938 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3939 ", distance=%" PRIu64,
3940 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3941 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003942 break;
3943 }
3944 }
3945
3946 // Assign fresh ids to pointers that were not matched in the process.
3947 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3948 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3949 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3950
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003951 current.rawPointerData.pointers[currentPointerIndex].id = id;
3952 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3953 current.rawPointerData.markIdBit(id,
3954 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003955
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003956 if (DEBUG_POINTER_ASSIGNMENT) {
3957 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3958 id);
3959 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003960 }
3961}
3962
3963int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3964 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3965 return AKEY_STATE_VIRTUAL;
3966 }
3967
3968 for (const VirtualKey& virtualKey : mVirtualKeys) {
3969 if (virtualKey.keyCode == keyCode) {
3970 return AKEY_STATE_UP;
3971 }
3972 }
3973
3974 return AKEY_STATE_UNKNOWN;
3975}
3976
3977int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
3978 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
3979 return AKEY_STATE_VIRTUAL;
3980 }
3981
3982 for (const VirtualKey& virtualKey : mVirtualKeys) {
3983 if (virtualKey.scanCode == scanCode) {
3984 return AKEY_STATE_UP;
3985 }
3986 }
3987
3988 return AKEY_STATE_UNKNOWN;
3989}
3990
3991bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3992 const int32_t* keyCodes, uint8_t* outFlags) {
3993 for (const VirtualKey& virtualKey : mVirtualKeys) {
3994 for (size_t i = 0; i < numCodes; i++) {
3995 if (virtualKey.keyCode == keyCodes[i]) {
3996 outFlags[i] = 1;
3997 }
3998 }
3999 }
4000
4001 return true;
4002}
4003
4004std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4005 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004006 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004007 return std::make_optional(mPointerController->getDisplayId());
4008 } else {
4009 return std::make_optional(mViewport.displayId);
4010 }
4011 }
4012 return std::nullopt;
4013}
4014
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004015} // namespace android