blob: 0a5de279dcbce12858eb991da1c671ddd293e438 [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
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800612int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
613 if (resolution < 0) {
614 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
615 getDeviceName().c_str());
616 return 0;
617 }
618 return resolution;
619}
620
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800621void TouchInputMapper::initializeSizeRanges() {
622 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
623 mSizeScale = 0.0f;
624 return;
625 }
626
627 // Size of diagonal axis.
628 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
629
630 // Size factors.
631 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
632 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
633 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
634 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
635 } else {
636 mSizeScale = 0.0f;
637 }
638
639 mOrientedRanges.haveTouchSize = true;
640 mOrientedRanges.haveToolSize = true;
641 mOrientedRanges.haveSize = true;
642
643 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
644 mOrientedRanges.touchMajor.source = mSource;
645 mOrientedRanges.touchMajor.min = 0;
646 mOrientedRanges.touchMajor.max = diagonalSize;
647 mOrientedRanges.touchMajor.flat = 0;
648 mOrientedRanges.touchMajor.fuzz = 0;
649 mOrientedRanges.touchMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800650 if (mRawPointerAxes.touchMajor.valid) {
651 mRawPointerAxes.touchMajor.resolution =
652 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
653 mOrientedRanges.touchMajor.resolution = mRawPointerAxes.touchMajor.resolution;
654 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800655
656 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
657 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800658 if (mRawPointerAxes.touchMinor.valid) {
659 mRawPointerAxes.touchMinor.resolution =
660 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
661 mOrientedRanges.touchMinor.resolution = mRawPointerAxes.touchMinor.resolution;
662 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800663
664 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
665 mOrientedRanges.toolMajor.source = mSource;
666 mOrientedRanges.toolMajor.min = 0;
667 mOrientedRanges.toolMajor.max = diagonalSize;
668 mOrientedRanges.toolMajor.flat = 0;
669 mOrientedRanges.toolMajor.fuzz = 0;
670 mOrientedRanges.toolMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800671 if (mRawPointerAxes.toolMajor.valid) {
672 mRawPointerAxes.toolMajor.resolution =
673 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
674 mOrientedRanges.toolMajor.resolution = mRawPointerAxes.toolMajor.resolution;
675 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800676
677 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
678 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800679 if (mRawPointerAxes.toolMinor.valid) {
680 mRawPointerAxes.toolMinor.resolution =
681 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
682 mOrientedRanges.toolMinor.resolution = mRawPointerAxes.toolMinor.resolution;
683 }
684
685 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
686 mOrientedRanges.touchMajor.resolution *= mGeometricScale;
687 mOrientedRanges.touchMinor.resolution *= mGeometricScale;
688 mOrientedRanges.toolMajor.resolution *= mGeometricScale;
689 mOrientedRanges.toolMinor.resolution *= mGeometricScale;
690 } else {
691 // Support for other calibrations can be added here.
692 ALOGW("%s calibration is not supported for size ranges at the moment. "
693 "Using raw resolution instead",
694 ftl::enum_string(mCalibration.sizeCalibration).c_str());
695 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800696
697 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
698 mOrientedRanges.size.source = mSource;
699 mOrientedRanges.size.min = 0;
700 mOrientedRanges.size.max = 1.0;
701 mOrientedRanges.size.flat = 0;
702 mOrientedRanges.size.fuzz = 0;
703 mOrientedRanges.size.resolution = 0;
704}
705
706void TouchInputMapper::initializeOrientedRanges() {
707 // Configure X and Y factors.
708 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
709 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
710 mXPrecision = 1.0f / mXScale;
711 mYPrecision = 1.0f / mYScale;
712
713 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
714 mOrientedRanges.x.source = mSource;
715 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
716 mOrientedRanges.y.source = mSource;
717
718 // Scale factor for terms that are not oriented in a particular axis.
719 // If the pixels are square then xScale == yScale otherwise we fake it
720 // by choosing an average.
721 mGeometricScale = avg(mXScale, mYScale);
722
723 initializeSizeRanges();
724
725 // Pressure factors.
726 mPressureScale = 0;
727 float pressureMax = 1.0;
728 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
729 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
730 if (mCalibration.havePressureScale) {
731 mPressureScale = mCalibration.pressureScale;
732 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
733 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
734 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
735 }
736 }
737
738 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
739 mOrientedRanges.pressure.source = mSource;
740 mOrientedRanges.pressure.min = 0;
741 mOrientedRanges.pressure.max = pressureMax;
742 mOrientedRanges.pressure.flat = 0;
743 mOrientedRanges.pressure.fuzz = 0;
744 mOrientedRanges.pressure.resolution = 0;
745
746 // Tilt
747 mTiltXCenter = 0;
748 mTiltXScale = 0;
749 mTiltYCenter = 0;
750 mTiltYScale = 0;
751 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
752 if (mHaveTilt) {
753 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
754 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
755 mTiltXScale = M_PI / 180;
756 mTiltYScale = M_PI / 180;
757
758 if (mRawPointerAxes.tiltX.resolution) {
759 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
760 }
761 if (mRawPointerAxes.tiltY.resolution) {
762 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
763 }
764
765 mOrientedRanges.haveTilt = true;
766
767 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
768 mOrientedRanges.tilt.source = mSource;
769 mOrientedRanges.tilt.min = 0;
770 mOrientedRanges.tilt.max = M_PI_2;
771 mOrientedRanges.tilt.flat = 0;
772 mOrientedRanges.tilt.fuzz = 0;
773 mOrientedRanges.tilt.resolution = 0;
774 }
775
776 // Orientation
777 mOrientationScale = 0;
778 if (mHaveTilt) {
779 mOrientedRanges.haveOrientation = true;
780
781 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
782 mOrientedRanges.orientation.source = mSource;
783 mOrientedRanges.orientation.min = -M_PI;
784 mOrientedRanges.orientation.max = M_PI;
785 mOrientedRanges.orientation.flat = 0;
786 mOrientedRanges.orientation.fuzz = 0;
787 mOrientedRanges.orientation.resolution = 0;
788 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
789 if (mCalibration.orientationCalibration ==
790 Calibration::OrientationCalibration::INTERPOLATED) {
791 if (mRawPointerAxes.orientation.valid) {
792 if (mRawPointerAxes.orientation.maxValue > 0) {
793 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
794 } else if (mRawPointerAxes.orientation.minValue < 0) {
795 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
796 } else {
797 mOrientationScale = 0;
798 }
799 }
800 }
801
802 mOrientedRanges.haveOrientation = true;
803
804 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
805 mOrientedRanges.orientation.source = mSource;
806 mOrientedRanges.orientation.min = -M_PI_2;
807 mOrientedRanges.orientation.max = M_PI_2;
808 mOrientedRanges.orientation.flat = 0;
809 mOrientedRanges.orientation.fuzz = 0;
810 mOrientedRanges.orientation.resolution = 0;
811 }
812
813 // Distance
814 mDistanceScale = 0;
815 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
816 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
817 if (mCalibration.haveDistanceScale) {
818 mDistanceScale = mCalibration.distanceScale;
819 } else {
820 mDistanceScale = 1.0f;
821 }
822 }
823
824 mOrientedRanges.haveDistance = true;
825
826 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
827 mOrientedRanges.distance.source = mSource;
828 mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
829 mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
830 mOrientedRanges.distance.flat = 0;
831 mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
832 mOrientedRanges.distance.resolution = 0;
833 }
834
835 // Compute oriented precision, scales and ranges.
836 // Note that the maximum value reported is an inclusive maximum value so it is one
837 // unit less than the total width or height of the display.
838 switch (mInputDeviceOrientation) {
839 case DISPLAY_ORIENTATION_90:
840 case DISPLAY_ORIENTATION_270:
841 mOrientedXPrecision = mYPrecision;
842 mOrientedYPrecision = mXPrecision;
843
844 mOrientedRanges.x.min = 0;
845 mOrientedRanges.x.max = mDisplayHeight - 1;
846 mOrientedRanges.x.flat = 0;
847 mOrientedRanges.x.fuzz = 0;
848 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
849
850 mOrientedRanges.y.min = 0;
851 mOrientedRanges.y.max = mDisplayWidth - 1;
852 mOrientedRanges.y.flat = 0;
853 mOrientedRanges.y.fuzz = 0;
854 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
855 break;
856
857 default:
858 mOrientedXPrecision = mXPrecision;
859 mOrientedYPrecision = mYPrecision;
860
861 mOrientedRanges.x.min = 0;
862 mOrientedRanges.x.max = mDisplayWidth - 1;
863 mOrientedRanges.x.flat = 0;
864 mOrientedRanges.x.fuzz = 0;
865 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
866
867 mOrientedRanges.y.min = 0;
868 mOrientedRanges.y.max = mDisplayHeight - 1;
869 mOrientedRanges.y.flat = 0;
870 mOrientedRanges.y.fuzz = 0;
871 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
872 break;
873 }
874}
875
Prabir Pradhan1728b212021-10-19 16:00:03 -0700876void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100877 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700878
879 resolveExternalStylusPresence();
880
881 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100882 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000883 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700884 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100885 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700886 if (hasStylus()) {
887 mSource |= AINPUT_SOURCE_STYLUS;
888 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800889 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700890 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100891 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700892 if (hasStylus()) {
893 mSource |= AINPUT_SOURCE_STYLUS;
894 }
895 if (hasExternalStylus()) {
896 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
897 }
Michael Wright227c5542020-07-02 18:30:52 +0100898 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700899 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100900 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700901 } else {
902 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100903 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700904 }
905
906 // Ensure we have valid X and Y axes.
907 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
908 ALOGW("Touch device '%s' did not report support for X or Y axis! "
909 "The device will be inoperable.",
910 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100911 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700912 return;
913 }
914
915 // Get associated display dimensions.
916 std::optional<DisplayViewport> newViewport = findViewport();
917 if (!newViewport) {
918 ALOGI("Touch device '%s' could not query the properties of its associated "
919 "display. The device will be inoperable until the display size "
920 "becomes available.",
921 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100922 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700923 return;
924 }
925
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000926 if (!newViewport->isActive) {
927 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
928 getDeviceName().c_str(), getDeviceId());
929 mDeviceMode = DeviceMode::DISABLED;
930 return;
931 }
932
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700933 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700934 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
935 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700936
Prabir Pradhan1728b212021-10-19 16:00:03 -0700937 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700938 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700939 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700940 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700941 mViewport = *newViewport;
942
Michael Wright227c5542020-07-02 18:30:52 +0100943 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700944 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700945 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
946 int32_t naturalPhysicalLeft, naturalPhysicalTop;
947 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700948
Prabir Pradhan1728b212021-10-19 16:00:03 -0700949 // Apply the inverse of the input device orientation so that the input device is
950 // configured in the same orientation as the viewport. The input device orientation will
951 // be re-applied by mInputDeviceOrientation.
952 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700953 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700954 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700955 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700956 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
957 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800958 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700959 naturalPhysicalTop = mViewport.physicalLeft;
960 naturalDeviceWidth = mViewport.deviceHeight;
961 naturalDeviceHeight = mViewport.deviceWidth;
962 break;
963 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700964 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
965 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
966 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
967 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
968 naturalDeviceWidth = mViewport.deviceWidth;
969 naturalDeviceHeight = mViewport.deviceHeight;
970 break;
971 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700972 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
973 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
974 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800975 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700976 naturalDeviceWidth = mViewport.deviceHeight;
977 naturalDeviceHeight = mViewport.deviceWidth;
978 break;
979 case DISPLAY_ORIENTATION_0:
980 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700981 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
982 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
983 naturalPhysicalLeft = mViewport.physicalLeft;
984 naturalPhysicalTop = mViewport.physicalTop;
985 naturalDeviceWidth = mViewport.deviceWidth;
986 naturalDeviceHeight = mViewport.deviceHeight;
987 break;
988 }
989
990 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
991 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
992 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
993 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
994 }
995
996 mPhysicalWidth = naturalPhysicalWidth;
997 mPhysicalHeight = naturalPhysicalHeight;
998 mPhysicalLeft = naturalPhysicalLeft;
999 mPhysicalTop = naturalPhysicalTop;
1000
Prabir Pradhan1728b212021-10-19 16:00:03 -07001001 const int32_t oldDisplayWidth = mDisplayWidth;
1002 const int32_t oldDisplayHeight = mDisplayHeight;
1003 mDisplayWidth = naturalDeviceWidth;
1004 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001005
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001006 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1007 // anything if the device is already orientation-aware. If the device is not
1008 // orientation-aware, then we need to apply the inverse rotation of the display so that
1009 // when the display rotation is applied later as a part of the per-window transform, we
1010 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001011 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001012 ? DISPLAY_ORIENTATION_0
1013 : getInverseRotation(mViewport.orientation);
1014 // For orientation-aware devices that work in the un-rotated coordinate space, the
1015 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001016 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
1017 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001018
1019 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001020 mInputDeviceOrientation =
1021 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001022 } else {
1023 mPhysicalWidth = rawWidth;
1024 mPhysicalHeight = rawHeight;
1025 mPhysicalLeft = 0;
1026 mPhysicalTop = 0;
1027
Prabir Pradhan1728b212021-10-19 16:00:03 -07001028 mDisplayWidth = rawWidth;
1029 mDisplayHeight = rawHeight;
1030 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001031 }
1032 }
1033
1034 // If moving between pointer modes, need to reset some state.
1035 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1036 if (deviceModeChanged) {
1037 mOrientedRanges.clear();
1038 }
1039
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001040 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1041 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001042 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001043 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001044 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1045 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001046 if (mPointerController == nullptr) {
1047 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001048 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001049 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001050 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1051 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001052 } else {
Michael Wright17db18e2020-06-26 20:51:44 +01001053 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001054 }
1055
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001056 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001057 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1058 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001059 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1060 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001061
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001062 configureVirtualKeys();
1063
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001064 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001065
1066 // Location
1067 updateAffineTransformation();
1068
Michael Wright227c5542020-07-02 18:30:52 +01001069 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001070 // Compute pointer gesture detection parameters.
1071 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001072 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001073
1074 // Scale movements such that one whole swipe of the touch pad covers a
1075 // given area relative to the diagonal size of the display when no acceleration
1076 // is applied.
1077 // Assume that the touch pad has a square aspect ratio such that movements in
1078 // X and Y of the same number of raw units cover the same physical distance.
1079 mPointerXMovementScale =
1080 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1081 mPointerYMovementScale = mPointerXMovementScale;
1082
1083 // Scale zooms to cover a smaller range of the display than movements do.
1084 // This value determines the area around the pointer that is affected by freeform
1085 // pointer gestures.
1086 mPointerXZoomScale =
1087 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1088 mPointerYZoomScale = mPointerXZoomScale;
1089
1090 // Max width between pointers to detect a swipe gesture is more than some fraction
1091 // of the diagonal axis of the touch pad. Touches that are wider than this are
1092 // translated into freeform gestures.
1093 mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
1094
1095 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001096 const nsecs_t readTime = when; // synthetic event
1097 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001098 }
1099
1100 // Inform the dispatcher about the changes.
1101 *outResetNeeded = true;
1102 bumpGeneration();
1103 }
1104}
1105
Prabir Pradhan1728b212021-10-19 16:00:03 -07001106void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001107 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001108 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1109 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001110 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1111 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1112 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1113 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001114 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001115}
1116
1117void TouchInputMapper::configureVirtualKeys() {
1118 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001119 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001120
1121 mVirtualKeys.clear();
1122
1123 if (virtualKeyDefinitions.size() == 0) {
1124 return;
1125 }
1126
1127 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1128 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1129 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1130 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1131
1132 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1133 VirtualKey virtualKey;
1134
1135 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1136 int32_t keyCode;
1137 int32_t dummyKeyMetaState;
1138 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001139 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1140 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001141 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1142 continue; // drop the key
1143 }
1144
1145 virtualKey.keyCode = keyCode;
1146 virtualKey.flags = flags;
1147
1148 // convert the key definition's display coordinates into touch coordinates for a hit box
1149 int32_t halfWidth = virtualKeyDefinition.width / 2;
1150 int32_t halfHeight = virtualKeyDefinition.height / 2;
1151
1152 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001153 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001154 touchScreenLeft;
1155 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001156 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001157 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001158 virtualKey.hitTop =
1159 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001160 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001161 virtualKey.hitBottom =
1162 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001163 touchScreenTop;
1164 mVirtualKeys.push_back(virtualKey);
1165 }
1166}
1167
1168void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1169 if (!mVirtualKeys.empty()) {
1170 dump += INDENT3 "Virtual Keys:\n";
1171
1172 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1173 const VirtualKey& virtualKey = mVirtualKeys[i];
1174 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1175 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1176 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1177 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1178 }
1179 }
1180}
1181
1182void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001183 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001184 Calibration& out = mCalibration;
1185
1186 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001187 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001188 String8 sizeCalibrationString;
1189 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
1190 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001191 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001192 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001193 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001194 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001195 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001196 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001197 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001198 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001199 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001200 } else if (sizeCalibrationString != "default") {
1201 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string());
1202 }
1203 }
1204
1205 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale);
1206 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias);
1207 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed);
1208
1209 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001210 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001211 String8 pressureCalibrationString;
1212 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
1213 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001214 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001215 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001216 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001218 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001219 } else if (pressureCalibrationString != "default") {
1220 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
1221 pressureCalibrationString.string());
1222 }
1223 }
1224
1225 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale);
1226
1227 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001228 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001229 String8 orientationCalibrationString;
1230 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
1231 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001232 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001233 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001234 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001235 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001236 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001237 } else if (orientationCalibrationString != "default") {
1238 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
1239 orientationCalibrationString.string());
1240 }
1241 }
1242
1243 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001244 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001245 String8 distanceCalibrationString;
1246 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
1247 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001248 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001249 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001250 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001251 } else if (distanceCalibrationString != "default") {
1252 ALOGW("Invalid value for touch.distance.calibration: '%s'",
1253 distanceCalibrationString.string());
1254 }
1255 }
1256
1257 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale);
1258
Michael Wright227c5542020-07-02 18:30:52 +01001259 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260 String8 coverageCalibrationString;
1261 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
1262 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001263 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001264 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001265 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001266 } else if (coverageCalibrationString != "default") {
1267 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
1268 coverageCalibrationString.string());
1269 }
1270 }
1271}
1272
1273void TouchInputMapper::resolveCalibration() {
1274 // Size
1275 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001276 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1277 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001278 }
1279 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001280 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001281 }
1282
1283 // Pressure
1284 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001285 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1286 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001287 }
1288 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001289 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001290 }
1291
1292 // Orientation
1293 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001294 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1295 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001296 }
1297 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001298 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001299 }
1300
1301 // Distance
1302 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001303 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1304 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001305 }
1306 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001307 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001308 }
1309
1310 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001311 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1312 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001313 }
1314}
1315
1316void TouchInputMapper::dumpCalibration(std::string& dump) {
1317 dump += INDENT3 "Calibration:\n";
1318
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001319 dump += INDENT4 "touch.size.calibration: ";
1320 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001321
1322 if (mCalibration.haveSizeScale) {
1323 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
1324 }
1325
1326 if (mCalibration.haveSizeBias) {
1327 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
1328 }
1329
1330 if (mCalibration.haveSizeIsSummed) {
1331 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
1332 toString(mCalibration.sizeIsSummed));
1333 }
1334
1335 // Pressure
1336 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001337 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001338 dump += INDENT4 "touch.pressure.calibration: none\n";
1339 break;
Michael Wright227c5542020-07-02 18:30:52 +01001340 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001341 dump += INDENT4 "touch.pressure.calibration: physical\n";
1342 break;
Michael Wright227c5542020-07-02 18:30:52 +01001343 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001344 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1345 break;
1346 default:
1347 ALOG_ASSERT(false);
1348 }
1349
1350 if (mCalibration.havePressureScale) {
1351 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
1352 }
1353
1354 // Orientation
1355 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001356 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001357 dump += INDENT4 "touch.orientation.calibration: none\n";
1358 break;
Michael Wright227c5542020-07-02 18:30:52 +01001359 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001360 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1361 break;
Michael Wright227c5542020-07-02 18:30:52 +01001362 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001363 dump += INDENT4 "touch.orientation.calibration: vector\n";
1364 break;
1365 default:
1366 ALOG_ASSERT(false);
1367 }
1368
1369 // Distance
1370 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001371 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001372 dump += INDENT4 "touch.distance.calibration: none\n";
1373 break;
Michael Wright227c5542020-07-02 18:30:52 +01001374 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001375 dump += INDENT4 "touch.distance.calibration: scaled\n";
1376 break;
1377 default:
1378 ALOG_ASSERT(false);
1379 }
1380
1381 if (mCalibration.haveDistanceScale) {
1382 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
1383 }
1384
1385 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001386 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001387 dump += INDENT4 "touch.coverage.calibration: none\n";
1388 break;
Michael Wright227c5542020-07-02 18:30:52 +01001389 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001390 dump += INDENT4 "touch.coverage.calibration: box\n";
1391 break;
1392 default:
1393 ALOG_ASSERT(false);
1394 }
1395}
1396
1397void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1398 dump += INDENT3 "Affine Transformation:\n";
1399
1400 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1401 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1402 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1403 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1404 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1405 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1406}
1407
1408void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001409 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001410 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001411}
1412
1413void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001414 mCursorButtonAccumulator.reset(getDeviceContext());
1415 mCursorScrollAccumulator.reset(getDeviceContext());
1416 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001417
1418 mPointerVelocityControl.reset();
1419 mWheelXVelocityControl.reset();
1420 mWheelYVelocityControl.reset();
1421
1422 mRawStatesPending.clear();
1423 mCurrentRawState.clear();
1424 mCurrentCookedState.clear();
1425 mLastRawState.clear();
1426 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001427 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001428 mSentHoverEnter = false;
1429 mHavePointerIds = false;
1430 mCurrentMotionAborted = false;
1431 mDownTime = 0;
1432
1433 mCurrentVirtualKey.down = false;
1434
1435 mPointerGesture.reset();
1436 mPointerSimple.reset();
1437 resetExternalStylus();
1438
1439 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001440 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001441 mPointerController->clearSpots();
1442 }
1443
1444 InputMapper::reset(when);
1445}
1446
1447void TouchInputMapper::resetExternalStylus() {
1448 mExternalStylusState.clear();
1449 mExternalStylusId = -1;
1450 mExternalStylusFusionTimeout = LLONG_MAX;
1451 mExternalStylusDataPending = false;
1452}
1453
1454void TouchInputMapper::clearStylusDataPendingFlags() {
1455 mExternalStylusDataPending = false;
1456 mExternalStylusFusionTimeout = LLONG_MAX;
1457}
1458
1459void TouchInputMapper::process(const RawEvent* rawEvent) {
1460 mCursorButtonAccumulator.process(rawEvent);
1461 mCursorScrollAccumulator.process(rawEvent);
1462 mTouchButtonAccumulator.process(rawEvent);
1463
1464 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001465 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001466 }
1467}
1468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001469void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001470 // Push a new state.
1471 mRawStatesPending.emplace_back();
1472
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001473 RawState& next = mRawStatesPending.back();
1474 next.clear();
1475 next.when = when;
1476 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001477
1478 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001479 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001480 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1481
1482 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001483 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1484 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001485 mCursorScrollAccumulator.finishSync();
1486
1487 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001488 syncTouch(when, &next);
1489
1490 // The last RawState is the actually second to last, since we just added a new state
1491 const RawState& last =
1492 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001493
1494 // Assign pointer ids.
1495 if (!mHavePointerIds) {
1496 assignPointerIds(last, next);
1497 }
1498
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001499 if (DEBUG_RAW_EVENTS) {
1500 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1501 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1502 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1503 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1504 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1505 next.rawPointerData.canceledIdBits.value);
1506 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001507
Arthur Hung9ad18942021-06-19 02:04:46 +00001508 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1509 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1510 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1511 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1512 next.rawPointerData.hoveringIdBits.value);
1513 }
1514
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001515 processRawTouches(false /*timeout*/);
1516}
1517
1518void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001519 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001520 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001521 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001522 mCurrentCookedState.clear();
1523 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001524 return;
1525 }
1526
1527 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1528 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1529 // touching the current state will only observe the events that have been dispatched to the
1530 // rest of the pipeline.
1531 const size_t N = mRawStatesPending.size();
1532 size_t count;
1533 for (count = 0; count < N; count++) {
1534 const RawState& next = mRawStatesPending[count];
1535
1536 // A failure to assign the stylus id means that we're waiting on stylus data
1537 // and so should defer the rest of the pipeline.
1538 if (assignExternalStylusId(next, timeout)) {
1539 break;
1540 }
1541
1542 // All ready to go.
1543 clearStylusDataPendingFlags();
1544 mCurrentRawState.copyFrom(next);
1545 if (mCurrentRawState.when < mLastRawState.when) {
1546 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001547 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001548 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001549 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001550 }
1551 if (count != 0) {
1552 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1553 }
1554
1555 if (mExternalStylusDataPending) {
1556 if (timeout) {
1557 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1558 clearStylusDataPendingFlags();
1559 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001560 if (DEBUG_STYLUS_FUSION) {
1561 ALOGD("Timeout expired, synthesizing event with new stylus data");
1562 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001563 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1564 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001565 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1566 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1567 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1568 }
1569 }
1570}
1571
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001572void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001573 // Always start with a clean state.
1574 mCurrentCookedState.clear();
1575
1576 // Apply stylus buttons to current raw state.
1577 applyExternalStylusButtonState(when);
1578
1579 // Handle policy on initial down or hover events.
1580 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1581 mCurrentRawState.rawPointerData.pointerCount != 0;
1582
1583 uint32_t policyFlags = 0;
1584 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1585 if (initialDown || buttonsPressed) {
1586 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001587 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001588 getContext()->fadePointer();
1589 }
1590
1591 if (mParameters.wake) {
1592 policyFlags |= POLICY_FLAG_WAKE;
1593 }
1594 }
1595
1596 // Consume raw off-screen touches before cooking pointer data.
1597 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001598 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001599 mCurrentRawState.rawPointerData.clear();
1600 }
1601
1602 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1603 // with cooked pointer data that has the same ids and indices as the raw data.
1604 // The following code can use either the raw or cooked data, as needed.
1605 cookPointerData();
1606
1607 // Apply stylus pressure to current cooked state.
1608 applyExternalStylusTouchState(when);
1609
1610 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001611 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1612 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001613 mCurrentCookedState.buttonState);
1614
1615 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001616 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001617 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1618 uint32_t id = idBits.clearFirstMarkedBit();
1619 const RawPointerData::Pointer& pointer =
1620 mCurrentRawState.rawPointerData.pointerForId(id);
1621 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1622 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1623 mCurrentCookedState.stylusIdBits.markBit(id);
1624 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1625 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1626 mCurrentCookedState.fingerIdBits.markBit(id);
1627 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1628 mCurrentCookedState.mouseIdBits.markBit(id);
1629 }
1630 }
1631 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1632 uint32_t id = idBits.clearFirstMarkedBit();
1633 const RawPointerData::Pointer& pointer =
1634 mCurrentRawState.rawPointerData.pointerForId(id);
1635 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1636 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1637 mCurrentCookedState.stylusIdBits.markBit(id);
1638 }
1639 }
1640
1641 // Stylus takes precedence over all tools, then mouse, then finger.
1642 PointerUsage pointerUsage = mPointerUsage;
1643 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1644 mCurrentCookedState.mouseIdBits.clear();
1645 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001646 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001647 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1648 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001649 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001650 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1651 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001652 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001653 }
1654
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001655 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656 } else {
Garfield Tanc734e4f2021-01-15 20:01:39 -08001657 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001658
1659 if (!mCurrentMotionAborted) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001660 dispatchButtonRelease(when, readTime, policyFlags);
1661 dispatchHoverExit(when, readTime, policyFlags);
1662 dispatchTouches(when, readTime, policyFlags);
1663 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1664 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001665 }
1666
1667 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1668 mCurrentMotionAborted = false;
1669 }
1670 }
1671
1672 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001673 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001674 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1675 mCurrentCookedState.buttonState);
1676
1677 // Clear some transient state.
1678 mCurrentRawState.rawVScroll = 0;
1679 mCurrentRawState.rawHScroll = 0;
1680
1681 // Copy current touch to last touch in preparation for the next cycle.
1682 mLastRawState.copyFrom(mCurrentRawState);
1683 mLastCookedState.copyFrom(mCurrentCookedState);
1684}
1685
Garfield Tanc734e4f2021-01-15 20:01:39 -08001686void TouchInputMapper::updateTouchSpots() {
1687 if (!mConfig.showTouches || mPointerController == nullptr) {
1688 return;
1689 }
1690
1691 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1692 // clear touch spots.
1693 if (mDeviceMode != DeviceMode::DIRECT &&
1694 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1695 return;
1696 }
1697
1698 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1699 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1700
1701 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001702 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1703 mCurrentCookedState.cookedPointerData.idToIndex,
1704 mCurrentCookedState.cookedPointerData.touchingIdBits,
1705 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001706}
1707
1708bool TouchInputMapper::isTouchScreen() {
1709 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1710 mParameters.hasAssociatedDisplay;
1711}
1712
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001713void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001714 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001715 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1716 }
1717}
1718
1719void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1720 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1721 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1722
1723 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1724 float pressure = mExternalStylusState.pressure;
1725 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1726 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1727 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1728 }
1729 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1730 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1731
1732 PointerProperties& properties =
1733 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1734 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1735 properties.toolType = mExternalStylusState.toolType;
1736 }
1737 }
1738}
1739
1740bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001741 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001742 return false;
1743 }
1744
1745 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1746 state.rawPointerData.pointerCount != 0;
1747 if (initialDown) {
1748 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001749 if (DEBUG_STYLUS_FUSION) {
1750 ALOGD("Have both stylus and touch data, beginning fusion");
1751 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001752 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1753 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001754 if (DEBUG_STYLUS_FUSION) {
1755 ALOGD("Timeout expired, assuming touch is not a stylus.");
1756 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001757 resetExternalStylus();
1758 } else {
1759 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1760 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1761 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001762 if (DEBUG_STYLUS_FUSION) {
1763 ALOGD("No stylus data but stylus is connected, requesting timeout "
1764 "(%" PRId64 "ms)",
1765 mExternalStylusFusionTimeout);
1766 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001767 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1768 return true;
1769 }
1770 }
1771
1772 // Check if the stylus pointer has gone up.
1773 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001774 if (DEBUG_STYLUS_FUSION) {
1775 ALOGD("Stylus pointer is going up");
1776 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001777 mExternalStylusId = -1;
1778 }
1779
1780 return false;
1781}
1782
1783void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001784 if (mDeviceMode == DeviceMode::POINTER) {
1785 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001786 // Since this is a synthetic event, we can consider its latency to be zero
1787 const nsecs_t readTime = when;
1788 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001789 }
Michael Wright227c5542020-07-02 18:30:52 +01001790 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001791 if (mExternalStylusFusionTimeout < when) {
1792 processRawTouches(true /*timeout*/);
1793 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1794 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1795 }
1796 }
1797}
1798
1799void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1800 mExternalStylusState.copyFrom(state);
1801 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1802 // We're either in the middle of a fused stream of data or we're waiting on data before
1803 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1804 // data.
1805 mExternalStylusDataPending = true;
1806 processRawTouches(false /*timeout*/);
1807 }
1808}
1809
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001810bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001811 // Check for release of a virtual key.
1812 if (mCurrentVirtualKey.down) {
1813 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1814 // Pointer went up while virtual key was down.
1815 mCurrentVirtualKey.down = false;
1816 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001817 if (DEBUG_VIRTUAL_KEYS) {
1818 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1819 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1820 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001821 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001822 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1823 }
1824 return true;
1825 }
1826
1827 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1828 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1829 const RawPointerData::Pointer& pointer =
1830 mCurrentRawState.rawPointerData.pointerForId(id);
1831 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1832 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1833 // Pointer is still within the space of the virtual key.
1834 return true;
1835 }
1836 }
1837
1838 // Pointer left virtual key area or another pointer also went down.
1839 // Send key cancellation but do not consume the touch yet.
1840 // This is useful when the user swipes through from the virtual key area
1841 // into the main display surface.
1842 mCurrentVirtualKey.down = false;
1843 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001844 if (DEBUG_VIRTUAL_KEYS) {
1845 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1846 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1847 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001848 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001849 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1850 AKEY_EVENT_FLAG_CANCELED);
1851 }
1852 }
1853
1854 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1855 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1856 // Pointer just went down. Check for virtual key press or off-screen touches.
1857 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1858 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001859 // Skip checking whether the pointer is inside the physical frame if the device is in
1860 // unscaled mode.
1861 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1862 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001863 // If exactly one pointer went down, check for virtual key hit.
1864 // Otherwise we will drop the entire stroke.
1865 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1866 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1867 if (virtualKey) {
1868 mCurrentVirtualKey.down = true;
1869 mCurrentVirtualKey.downTime = when;
1870 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1871 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1872 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001873 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1874 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001875
1876 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001877 if (DEBUG_VIRTUAL_KEYS) {
1878 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1879 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1880 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001881 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001882 AKEY_EVENT_FLAG_FROM_SYSTEM |
1883 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1884 }
1885 }
1886 }
1887 return true;
1888 }
1889 }
1890
1891 // Disable all virtual key touches that happen within a short time interval of the
1892 // most recent touch within the screen area. The idea is to filter out stray
1893 // virtual key presses when interacting with the touch screen.
1894 //
1895 // Problems we're trying to solve:
1896 //
1897 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1898 // virtual key area that is implemented by a separate touch panel and accidentally
1899 // triggers a virtual key.
1900 //
1901 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1902 // area and accidentally triggers a virtual key. This often happens when virtual keys
1903 // are layed out below the screen near to where the on screen keyboard's space bar
1904 // is displayed.
1905 if (mConfig.virtualKeyQuietTime > 0 &&
1906 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001907 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001908 }
1909 return false;
1910}
1911
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001912void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001913 int32_t keyEventAction, int32_t keyEventFlags) {
1914 int32_t keyCode = mCurrentVirtualKey.keyCode;
1915 int32_t scanCode = mCurrentVirtualKey.scanCode;
1916 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001917 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001918 policyFlags |= POLICY_FLAG_VIRTUAL;
1919
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001920 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1921 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1922 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001923 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001924}
1925
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001926void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001927 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1928 if (!currentIdBits.isEmpty()) {
1929 int32_t metaState = getContext()->getGlobalMetaState();
1930 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1932 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001933 mCurrentCookedState.cookedPointerData.pointerProperties,
1934 mCurrentCookedState.cookedPointerData.pointerCoords,
1935 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1936 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1937 mCurrentMotionAborted = true;
1938 }
1939}
1940
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001941void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001942 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1943 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1944 int32_t metaState = getContext()->getGlobalMetaState();
1945 int32_t buttonState = mCurrentCookedState.buttonState;
1946
1947 if (currentIdBits == lastIdBits) {
1948 if (!currentIdBits.isEmpty()) {
1949 // No pointer id changes so this is a move event.
1950 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001951 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1952 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001953 mCurrentCookedState.cookedPointerData.pointerProperties,
1954 mCurrentCookedState.cookedPointerData.pointerCoords,
1955 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1956 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1957 }
1958 } else {
1959 // There may be pointers going up and pointers going down and pointers moving
1960 // all at the same time.
1961 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1962 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1963 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1964 BitSet32 dispatchedIdBits(lastIdBits.value);
1965
1966 // Update last coordinates of pointers that have moved so that we observe the new
1967 // pointer positions at the same time as other pointers that have just gone up.
1968 bool moveNeeded =
1969 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1970 mCurrentCookedState.cookedPointerData.pointerCoords,
1971 mCurrentCookedState.cookedPointerData.idToIndex,
1972 mLastCookedState.cookedPointerData.pointerProperties,
1973 mLastCookedState.cookedPointerData.pointerCoords,
1974 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1975 if (buttonState != mLastCookedState.buttonState) {
1976 moveNeeded = true;
1977 }
1978
1979 // Dispatch pointer up events.
1980 while (!upIdBits.isEmpty()) {
1981 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001982 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001983 if (isCanceled) {
1984 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1985 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001986 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001987 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001988 mLastCookedState.cookedPointerData.pointerProperties,
1989 mLastCookedState.cookedPointerData.pointerCoords,
1990 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
1991 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1992 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08001993 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001994 }
1995
1996 // Dispatch move events if any of the remaining pointers moved from their old locations.
1997 // Although applications receive new locations as part of individual pointer up
1998 // events, they do not generally handle them except when presented in a move event.
1999 if (moveNeeded && !moveIdBits.isEmpty()) {
2000 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002001 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2002 metaState, buttonState, 0,
2003 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002004 mCurrentCookedState.cookedPointerData.pointerCoords,
2005 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2006 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2007 }
2008
2009 // Dispatch pointer down events using the new pointer locations.
2010 while (!downIdBits.isEmpty()) {
2011 uint32_t downId = downIdBits.clearFirstMarkedBit();
2012 dispatchedIdBits.markBit(downId);
2013
2014 if (dispatchedIdBits.count() == 1) {
2015 // First pointer is going down. Set down time.
2016 mDownTime = when;
2017 }
2018
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002019 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2020 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002021 mCurrentCookedState.cookedPointerData.pointerProperties,
2022 mCurrentCookedState.cookedPointerData.pointerCoords,
2023 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2024 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2025 }
2026 }
2027}
2028
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002029void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002030 if (mSentHoverEnter &&
2031 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2032 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2033 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002034 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2035 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002036 mLastCookedState.cookedPointerData.pointerProperties,
2037 mLastCookedState.cookedPointerData.pointerCoords,
2038 mLastCookedState.cookedPointerData.idToIndex,
2039 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2040 mOrientedYPrecision, mDownTime);
2041 mSentHoverEnter = false;
2042 }
2043}
2044
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002045void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2046 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002047 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2048 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2049 int32_t metaState = getContext()->getGlobalMetaState();
2050 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002051 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2052 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002053 mCurrentCookedState.cookedPointerData.pointerProperties,
2054 mCurrentCookedState.cookedPointerData.pointerCoords,
2055 mCurrentCookedState.cookedPointerData.idToIndex,
2056 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2057 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2058 mSentHoverEnter = true;
2059 }
2060
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002061 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2062 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002063 mCurrentCookedState.cookedPointerData.pointerProperties,
2064 mCurrentCookedState.cookedPointerData.pointerCoords,
2065 mCurrentCookedState.cookedPointerData.idToIndex,
2066 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2067 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2068 }
2069}
2070
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002071void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002072 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2073 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2074 const int32_t metaState = getContext()->getGlobalMetaState();
2075 int32_t buttonState = mLastCookedState.buttonState;
2076 while (!releasedButtons.isEmpty()) {
2077 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2078 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002079 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002080 actionButton, 0, metaState, buttonState, 0,
2081 mCurrentCookedState.cookedPointerData.pointerProperties,
2082 mCurrentCookedState.cookedPointerData.pointerCoords,
2083 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2084 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2085 }
2086}
2087
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002088void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002089 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2090 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2091 const int32_t metaState = getContext()->getGlobalMetaState();
2092 int32_t buttonState = mLastCookedState.buttonState;
2093 while (!pressedButtons.isEmpty()) {
2094 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2095 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002096 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2097 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002098 mCurrentCookedState.cookedPointerData.pointerProperties,
2099 mCurrentCookedState.cookedPointerData.pointerCoords,
2100 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2101 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2102 }
2103}
2104
2105const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2106 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2107 return cookedPointerData.touchingIdBits;
2108 }
2109 return cookedPointerData.hoveringIdBits;
2110}
2111
2112void TouchInputMapper::cookPointerData() {
2113 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2114
2115 mCurrentCookedState.cookedPointerData.clear();
2116 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2117 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2118 mCurrentRawState.rawPointerData.hoveringIdBits;
2119 mCurrentCookedState.cookedPointerData.touchingIdBits =
2120 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002121 mCurrentCookedState.cookedPointerData.canceledIdBits =
2122 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002123
2124 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2125 mCurrentCookedState.buttonState = 0;
2126 } else {
2127 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2128 }
2129
2130 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002131 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002132 for (uint32_t i = 0; i < currentPointerCount; i++) {
2133 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2134
2135 // Size
2136 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2137 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002138 case Calibration::SizeCalibration::GEOMETRIC:
2139 case Calibration::SizeCalibration::DIAMETER:
2140 case Calibration::SizeCalibration::BOX:
2141 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002142 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2143 touchMajor = in.touchMajor;
2144 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2145 toolMajor = in.toolMajor;
2146 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2147 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2148 : in.touchMajor;
2149 } else if (mRawPointerAxes.touchMajor.valid) {
2150 toolMajor = touchMajor = in.touchMajor;
2151 toolMinor = touchMinor =
2152 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2153 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2154 : in.touchMajor;
2155 } else if (mRawPointerAxes.toolMajor.valid) {
2156 touchMajor = toolMajor = in.toolMajor;
2157 touchMinor = toolMinor =
2158 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2159 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2160 : in.toolMajor;
2161 } else {
2162 ALOG_ASSERT(false,
2163 "No touch or tool axes. "
2164 "Size calibration should have been resolved to NONE.");
2165 touchMajor = 0;
2166 touchMinor = 0;
2167 toolMajor = 0;
2168 toolMinor = 0;
2169 size = 0;
2170 }
2171
2172 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2173 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2174 if (touchingCount > 1) {
2175 touchMajor /= touchingCount;
2176 touchMinor /= touchingCount;
2177 toolMajor /= touchingCount;
2178 toolMinor /= touchingCount;
2179 size /= touchingCount;
2180 }
2181 }
2182
Michael Wright227c5542020-07-02 18:30:52 +01002183 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002184 touchMajor *= mGeometricScale;
2185 touchMinor *= mGeometricScale;
2186 toolMajor *= mGeometricScale;
2187 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002188 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002189 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2190 touchMinor = touchMajor;
2191 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2192 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002193 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002194 touchMinor = touchMajor;
2195 toolMinor = toolMajor;
2196 }
2197
2198 mCalibration.applySizeScaleAndBias(&touchMajor);
2199 mCalibration.applySizeScaleAndBias(&touchMinor);
2200 mCalibration.applySizeScaleAndBias(&toolMajor);
2201 mCalibration.applySizeScaleAndBias(&toolMinor);
2202 size *= mSizeScale;
2203 break;
2204 default:
2205 touchMajor = 0;
2206 touchMinor = 0;
2207 toolMajor = 0;
2208 toolMinor = 0;
2209 size = 0;
2210 break;
2211 }
2212
2213 // Pressure
2214 float pressure;
2215 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002216 case Calibration::PressureCalibration::PHYSICAL:
2217 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002218 pressure = in.pressure * mPressureScale;
2219 break;
2220 default:
2221 pressure = in.isHovering ? 0 : 1;
2222 break;
2223 }
2224
2225 // Tilt and Orientation
2226 float tilt;
2227 float orientation;
2228 if (mHaveTilt) {
2229 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2230 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2231 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2232 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2233 } else {
2234 tilt = 0;
2235
2236 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002237 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002238 orientation = in.orientation * mOrientationScale;
2239 break;
Michael Wright227c5542020-07-02 18:30:52 +01002240 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002241 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2242 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2243 if (c1 != 0 || c2 != 0) {
2244 orientation = atan2f(c1, c2) * 0.5f;
2245 float confidence = hypotf(c1, c2);
2246 float scale = 1.0f + confidence / 16.0f;
2247 touchMajor *= scale;
2248 touchMinor /= scale;
2249 toolMajor *= scale;
2250 toolMinor /= scale;
2251 } else {
2252 orientation = 0;
2253 }
2254 break;
2255 }
2256 default:
2257 orientation = 0;
2258 }
2259 }
2260
2261 // Distance
2262 float distance;
2263 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002264 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002265 distance = in.distance * mDistanceScale;
2266 break;
2267 default:
2268 distance = 0;
2269 }
2270
2271 // Coverage
2272 int32_t rawLeft, rawTop, rawRight, rawBottom;
2273 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002274 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002275 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2276 rawRight = in.toolMinor & 0x0000ffff;
2277 rawBottom = in.toolMajor & 0x0000ffff;
2278 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2279 break;
2280 default:
2281 rawLeft = rawTop = rawRight = rawBottom = 0;
2282 break;
2283 }
2284
2285 // Adjust X,Y coords for device calibration
2286 // TODO: Adjust coverage coords?
2287 float xTransformed = in.x, yTransformed = in.y;
2288 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002289 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002290
Prabir Pradhan1728b212021-10-19 16:00:03 -07002291 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002292 float left, top, right, bottom;
2293
Prabir Pradhan1728b212021-10-19 16:00:03 -07002294 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002295 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002296 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2297 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2298 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2299 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002300 orientation -= M_PI_2;
2301 if (mOrientedRanges.haveOrientation &&
2302 orientation < mOrientedRanges.orientation.min) {
2303 orientation +=
2304 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2305 }
2306 break;
2307 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002308 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2309 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002310 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2311 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002312 orientation -= M_PI;
2313 if (mOrientedRanges.haveOrientation &&
2314 orientation < mOrientedRanges.orientation.min) {
2315 orientation +=
2316 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2317 }
2318 break;
2319 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002320 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2321 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002322 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2323 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002324 orientation += M_PI_2;
2325 if (mOrientedRanges.haveOrientation &&
2326 orientation > mOrientedRanges.orientation.max) {
2327 orientation -=
2328 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2329 }
2330 break;
2331 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002332 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2333 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2334 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2335 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002336 break;
2337 }
2338
2339 // Write output coords.
2340 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2341 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002342 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2343 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002344 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2345 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2346 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2347 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2348 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2349 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2350 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002351 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002352 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2353 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2354 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2355 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2356 } else {
2357 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2358 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2359 }
2360
Chris Ye364fdb52020-08-05 15:07:56 -07002361 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002362 uint32_t id = in.id;
2363 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2364 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2365 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2366 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2367 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2368 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2369 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2370 }
2371
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002372 // Write output properties.
2373 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002374 properties.clear();
2375 properties.id = id;
2376 properties.toolType = in.toolType;
2377
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002378 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002379 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002380 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002381 }
2382}
2383
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002384void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002385 PointerUsage pointerUsage) {
2386 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002387 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002388 mPointerUsage = pointerUsage;
2389 }
2390
2391 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002392 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002393 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002394 break;
Michael Wright227c5542020-07-02 18:30:52 +01002395 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002396 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002397 break;
Michael Wright227c5542020-07-02 18:30:52 +01002398 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002399 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002400 break;
Michael Wright227c5542020-07-02 18:30:52 +01002401 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002402 break;
2403 }
2404}
2405
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002406void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002407 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002408 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002409 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002410 break;
Michael Wright227c5542020-07-02 18:30:52 +01002411 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002412 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002413 break;
Michael Wright227c5542020-07-02 18:30:52 +01002414 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002415 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002416 break;
Michael Wright227c5542020-07-02 18:30:52 +01002417 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002418 break;
2419 }
2420
Michael Wright227c5542020-07-02 18:30:52 +01002421 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002422}
2423
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002424void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2425 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002426 // Update current gesture coordinates.
2427 bool cancelPreviousGesture, finishPreviousGesture;
2428 bool sendEvents =
2429 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2430 if (!sendEvents) {
2431 return;
2432 }
2433 if (finishPreviousGesture) {
2434 cancelPreviousGesture = false;
2435 }
2436
2437 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002438 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002439 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002440 if (finishPreviousGesture || cancelPreviousGesture) {
2441 mPointerController->clearSpots();
2442 }
2443
Michael Wright227c5542020-07-02 18:30:52 +01002444 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002445 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2446 mPointerGesture.currentGestureIdToIndex,
2447 mPointerGesture.currentGestureIdBits,
2448 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002449 }
2450 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002451 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002452 }
2453
2454 // Show or hide the pointer if needed.
2455 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002456 case PointerGesture::Mode::NEUTRAL:
2457 case PointerGesture::Mode::QUIET:
2458 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2459 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002460 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002461 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002462 }
2463 break;
Michael Wright227c5542020-07-02 18:30:52 +01002464 case PointerGesture::Mode::TAP:
2465 case PointerGesture::Mode::TAP_DRAG:
2466 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2467 case PointerGesture::Mode::HOVER:
2468 case PointerGesture::Mode::PRESS:
2469 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002470 // Unfade the pointer when the current gesture manipulates the
2471 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002472 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002473 break;
Michael Wright227c5542020-07-02 18:30:52 +01002474 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002475 // Fade the pointer when the current gesture manipulates a different
2476 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002477 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002478 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002479 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002480 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002481 }
2482 break;
2483 }
2484
2485 // Send events!
2486 int32_t metaState = getContext()->getGlobalMetaState();
2487 int32_t buttonState = mCurrentCookedState.buttonState;
2488
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002489 uint32_t flags = 0;
2490
2491 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2492 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2493 }
2494
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002495 // Update last coordinates of pointers that have moved so that we observe the new
2496 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002497 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2498 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2499 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2500 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2501 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2502 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002503 bool moveNeeded = false;
2504 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2505 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2506 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2507 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2508 mPointerGesture.lastGestureIdBits.value);
2509 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2510 mPointerGesture.currentGestureCoords,
2511 mPointerGesture.currentGestureIdToIndex,
2512 mPointerGesture.lastGestureProperties,
2513 mPointerGesture.lastGestureCoords,
2514 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2515 if (buttonState != mLastCookedState.buttonState) {
2516 moveNeeded = true;
2517 }
2518 }
2519
2520 // Send motion events for all pointers that went up or were canceled.
2521 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2522 if (!dispatchedGestureIdBits.isEmpty()) {
2523 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002524 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2525 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002526 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2527 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2528 mPointerGesture.downTime);
2529
2530 dispatchedGestureIdBits.clear();
2531 } else {
2532 BitSet32 upGestureIdBits;
2533 if (finishPreviousGesture) {
2534 upGestureIdBits = dispatchedGestureIdBits;
2535 } else {
2536 upGestureIdBits.value =
2537 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2538 }
2539 while (!upGestureIdBits.isEmpty()) {
2540 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2541
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002542 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002543 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002544 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002545 mPointerGesture.lastGestureCoords,
2546 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2547 0, mPointerGesture.downTime);
2548
2549 dispatchedGestureIdBits.clearBit(id);
2550 }
2551 }
2552 }
2553
2554 // Send motion events for all pointers that moved.
2555 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002556 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002557 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002558 mPointerGesture.currentGestureProperties,
2559 mPointerGesture.currentGestureCoords,
2560 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2561 mPointerGesture.downTime);
2562 }
2563
2564 // Send motion events for all pointers that went down.
2565 if (down) {
2566 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2567 ~dispatchedGestureIdBits.value);
2568 while (!downGestureIdBits.isEmpty()) {
2569 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2570 dispatchedGestureIdBits.markBit(id);
2571
2572 if (dispatchedGestureIdBits.count() == 1) {
2573 mPointerGesture.downTime = when;
2574 }
2575
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002576 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002577 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002578 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002579 mPointerGesture.currentGestureCoords,
2580 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2581 0, mPointerGesture.downTime);
2582 }
2583 }
2584
2585 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002586 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002587 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2588 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002589 mPointerGesture.currentGestureProperties,
2590 mPointerGesture.currentGestureCoords,
2591 mPointerGesture.currentGestureIdToIndex,
2592 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2593 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2594 // Synthesize a hover move event after all pointers go up to indicate that
2595 // the pointer is hovering again even if the user is not currently touching
2596 // the touch pad. This ensures that a view will receive a fresh hover enter
2597 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002598 float x, y;
2599 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002600
2601 PointerProperties pointerProperties;
2602 pointerProperties.clear();
2603 pointerProperties.id = 0;
2604 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2605
2606 PointerCoords pointerCoords;
2607 pointerCoords.clear();
2608 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2609 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2610
2611 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002612 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002613 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002614 metaState, buttonState, MotionClassification::NONE,
2615 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2616 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002617 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002618 }
2619
2620 // Update state.
2621 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2622 if (!down) {
2623 mPointerGesture.lastGestureIdBits.clear();
2624 } else {
2625 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2626 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2627 uint32_t id = idBits.clearFirstMarkedBit();
2628 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2629 mPointerGesture.lastGestureProperties[index].copyFrom(
2630 mPointerGesture.currentGestureProperties[index]);
2631 mPointerGesture.lastGestureCoords[index].copyFrom(
2632 mPointerGesture.currentGestureCoords[index]);
2633 mPointerGesture.lastGestureIdToIndex[id] = index;
2634 }
2635 }
2636}
2637
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002638void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002639 // Cancel previously dispatches pointers.
2640 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2641 int32_t metaState = getContext()->getGlobalMetaState();
2642 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002643 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2644 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002645 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2646 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2647 0, 0, mPointerGesture.downTime);
2648 }
2649
2650 // Reset the current pointer gesture.
2651 mPointerGesture.reset();
2652 mPointerVelocityControl.reset();
2653
2654 // Remove any current spots.
2655 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002656 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002657 mPointerController->clearSpots();
2658 }
2659}
2660
2661bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2662 bool* outFinishPreviousGesture, bool isTimeout) {
2663 *outCancelPreviousGesture = false;
2664 *outFinishPreviousGesture = false;
2665
2666 // Handle TAP timeout.
2667 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002668 if (DEBUG_GESTURES) {
2669 ALOGD("Gestures: Processing timeout");
2670 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002671
Michael Wright227c5542020-07-02 18:30:52 +01002672 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002673 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2674 // The tap/drag timeout has not yet expired.
2675 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2676 mConfig.pointerGestureTapDragInterval);
2677 } else {
2678 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002679 if (DEBUG_GESTURES) {
2680 ALOGD("Gestures: TAP finished");
2681 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002682 *outFinishPreviousGesture = true;
2683
2684 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002685 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002686 mPointerGesture.currentGestureIdBits.clear();
2687
2688 mPointerVelocityControl.reset();
2689 return true;
2690 }
2691 }
2692
2693 // We did not handle this timeout.
2694 return false;
2695 }
2696
2697 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2698 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2699
2700 // Update the velocity tracker.
2701 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002702 std::vector<VelocityTracker::Position> positions;
2703 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002704 uint32_t id = idBits.clearFirstMarkedBit();
2705 const RawPointerData::Pointer& pointer =
2706 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002707 float x = pointer.x * mPointerXMovementScale;
2708 float y = pointer.y * mPointerYMovementScale;
2709 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002710 }
2711 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2712 positions);
2713 }
2714
2715 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2716 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002717 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2718 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2719 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002720 mPointerGesture.resetTap();
2721 }
2722
2723 // Pick a new active touch id if needed.
2724 // Choose an arbitrary pointer that just went down, if there is one.
2725 // Otherwise choose an arbitrary remaining pointer.
2726 // This guarantees we always have an active touch id when there is at least one pointer.
2727 // We keep the same active touch id for as long as possible.
2728 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2729 int32_t activeTouchId = lastActiveTouchId;
2730 if (activeTouchId < 0) {
2731 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2732 activeTouchId = mPointerGesture.activeTouchId =
2733 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2734 mPointerGesture.firstTouchTime = when;
2735 }
2736 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2737 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2738 activeTouchId = mPointerGesture.activeTouchId =
2739 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2740 } else {
2741 activeTouchId = mPointerGesture.activeTouchId = -1;
2742 }
2743 }
2744
2745 // Determine whether we are in quiet time.
2746 bool isQuietTime = false;
2747 if (activeTouchId < 0) {
2748 mPointerGesture.resetQuietTime();
2749 } else {
2750 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2751 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002752 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2753 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2754 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002755 currentFingerCount < 2) {
2756 // Enter quiet time when exiting swipe or freeform state.
2757 // This is to prevent accidentally entering the hover state and flinging the
2758 // pointer when finishing a swipe and there is still one pointer left onscreen.
2759 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002760 } else if (mPointerGesture.lastGestureMode ==
2761 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002762 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2763 // Enter quiet time when releasing the button and there are still two or more
2764 // fingers down. This may indicate that one finger was used to press the button
2765 // but it has not gone up yet.
2766 isQuietTime = true;
2767 }
2768 if (isQuietTime) {
2769 mPointerGesture.quietTime = when;
2770 }
2771 }
2772 }
2773
2774 // Switch states based on button and pointer state.
2775 if (isQuietTime) {
2776 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002777 if (DEBUG_GESTURES) {
2778 ALOGD("Gestures: QUIET for next %0.3fms",
2779 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2780 0.000001f);
2781 }
Michael Wright227c5542020-07-02 18:30:52 +01002782 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002783 *outFinishPreviousGesture = true;
2784 }
2785
2786 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002787 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002788 mPointerGesture.currentGestureIdBits.clear();
2789
2790 mPointerVelocityControl.reset();
2791 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2792 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2793 // The pointer follows the active touch point.
2794 // Emit DOWN, MOVE, UP events at the pointer location.
2795 //
2796 // Only the active touch matters; other fingers are ignored. This policy helps
2797 // to handle the case where the user places a second finger on the touch pad
2798 // to apply the necessary force to depress an integrated button below the surface.
2799 // We don't want the second finger to be delivered to applications.
2800 //
2801 // For this to work well, we need to make sure to track the pointer that is really
2802 // active. If the user first puts one finger down to click then adds another
2803 // finger to drag then the active pointer should switch to the finger that is
2804 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002805 if (DEBUG_GESTURES) {
2806 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2807 "currentFingerCount=%d",
2808 activeTouchId, currentFingerCount);
2809 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002810 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002811 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002812 *outFinishPreviousGesture = true;
2813 mPointerGesture.activeGestureId = 0;
2814 }
2815
2816 // Switch pointers if needed.
2817 // Find the fastest pointer and follow it.
2818 if (activeTouchId >= 0 && currentFingerCount > 1) {
2819 int32_t bestId = -1;
2820 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2821 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2822 uint32_t id = idBits.clearFirstMarkedBit();
2823 float vx, vy;
2824 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2825 float speed = hypotf(vx, vy);
2826 if (speed > bestSpeed) {
2827 bestId = id;
2828 bestSpeed = speed;
2829 }
2830 }
2831 }
2832 if (bestId >= 0 && bestId != activeTouchId) {
2833 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002834 if (DEBUG_GESTURES) {
2835 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2836 "bestId=%d, bestSpeed=%0.3f",
2837 bestId, bestSpeed);
2838 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002839 }
2840 }
2841
2842 float deltaX = 0, deltaY = 0;
2843 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2844 const RawPointerData::Pointer& currentPointer =
2845 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2846 const RawPointerData::Pointer& lastPointer =
2847 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2848 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2849 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2850
Prabir Pradhan1728b212021-10-19 16:00:03 -07002851 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002852 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2853
2854 // Move the pointer using a relative motion.
2855 // When using spots, the click will occur at the position of the anchor
2856 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002857 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002858 } else {
2859 mPointerVelocityControl.reset();
2860 }
2861
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002862 float x, y;
2863 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002864
Michael Wright227c5542020-07-02 18:30:52 +01002865 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002866 mPointerGesture.currentGestureIdBits.clear();
2867 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2868 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2869 mPointerGesture.currentGestureProperties[0].clear();
2870 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2871 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2872 mPointerGesture.currentGestureCoords[0].clear();
2873 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2874 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2875 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2876 } else if (currentFingerCount == 0) {
2877 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002878 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002879 *outFinishPreviousGesture = true;
2880 }
2881
2882 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2883 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2884 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002885 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2886 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002887 lastFingerCount == 1) {
2888 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002889 float x, y;
2890 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002891 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2892 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002893 if (DEBUG_GESTURES) {
2894 ALOGD("Gestures: TAP");
2895 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002896
2897 mPointerGesture.tapUpTime = when;
2898 getContext()->requestTimeoutAtTime(when +
2899 mConfig.pointerGestureTapDragInterval);
2900
2901 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002902 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002903 mPointerGesture.currentGestureIdBits.clear();
2904 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2905 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2906 mPointerGesture.currentGestureProperties[0].clear();
2907 mPointerGesture.currentGestureProperties[0].id =
2908 mPointerGesture.activeGestureId;
2909 mPointerGesture.currentGestureProperties[0].toolType =
2910 AMOTION_EVENT_TOOL_TYPE_FINGER;
2911 mPointerGesture.currentGestureCoords[0].clear();
2912 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2913 mPointerGesture.tapX);
2914 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2915 mPointerGesture.tapY);
2916 mPointerGesture.currentGestureCoords[0]
2917 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2918
2919 tapped = true;
2920 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002921 if (DEBUG_GESTURES) {
2922 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2923 y - mPointerGesture.tapY);
2924 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002925 }
2926 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002927 if (DEBUG_GESTURES) {
2928 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2929 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2930 (when - mPointerGesture.tapDownTime) * 0.000001f);
2931 } else {
2932 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2933 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002934 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002935 }
2936 }
2937
2938 mPointerVelocityControl.reset();
2939
2940 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002941 if (DEBUG_GESTURES) {
2942 ALOGD("Gestures: NEUTRAL");
2943 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002944 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002945 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002946 mPointerGesture.currentGestureIdBits.clear();
2947 }
2948 } else if (currentFingerCount == 1) {
2949 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2950 // The pointer follows the active touch point.
2951 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2952 // When in TAP_DRAG, emit MOVE events at the pointer location.
2953 ALOG_ASSERT(activeTouchId >= 0);
2954
Michael Wright227c5542020-07-02 18:30:52 +01002955 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2956 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002957 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002958 float x, y;
2959 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002960 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2961 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002962 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002963 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002964 if (DEBUG_GESTURES) {
2965 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2966 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
2967 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002968 }
2969 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002970 if (DEBUG_GESTURES) {
2971 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2972 (when - mPointerGesture.tapUpTime) * 0.000001f);
2973 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002974 }
Michael Wright227c5542020-07-02 18:30:52 +01002975 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2976 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002977 }
2978
2979 float deltaX = 0, deltaY = 0;
2980 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2981 const RawPointerData::Pointer& currentPointer =
2982 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2983 const RawPointerData::Pointer& lastPointer =
2984 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2985 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2986 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2987
Prabir Pradhan1728b212021-10-19 16:00:03 -07002988 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002989 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2990
2991 // Move the pointer using a relative motion.
2992 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002993 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002994 } else {
2995 mPointerVelocityControl.reset();
2996 }
2997
2998 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01002999 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003000 if (DEBUG_GESTURES) {
3001 ALOGD("Gestures: TAP_DRAG");
3002 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003003 down = true;
3004 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003005 if (DEBUG_GESTURES) {
3006 ALOGD("Gestures: HOVER");
3007 }
Michael Wright227c5542020-07-02 18:30:52 +01003008 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003009 *outFinishPreviousGesture = true;
3010 }
3011 mPointerGesture.activeGestureId = 0;
3012 down = false;
3013 }
3014
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003015 float x, y;
3016 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003017
3018 mPointerGesture.currentGestureIdBits.clear();
3019 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3020 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3021 mPointerGesture.currentGestureProperties[0].clear();
3022 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3023 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3024 mPointerGesture.currentGestureCoords[0].clear();
3025 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3026 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3027 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3028 down ? 1.0f : 0.0f);
3029
3030 if (lastFingerCount == 0 && currentFingerCount != 0) {
3031 mPointerGesture.resetTap();
3032 mPointerGesture.tapDownTime = when;
3033 mPointerGesture.tapX = x;
3034 mPointerGesture.tapY = y;
3035 }
3036 } else {
3037 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3038 // We need to provide feedback for each finger that goes down so we cannot wait
3039 // for the fingers to move before deciding what to do.
3040 //
3041 // The ambiguous case is deciding what to do when there are two fingers down but they
3042 // have not moved enough to determine whether they are part of a drag or part of a
3043 // freeform gesture, or just a press or long-press at the pointer location.
3044 //
3045 // When there are two fingers we start with the PRESS hypothesis and we generate a
3046 // down at the pointer location.
3047 //
3048 // When the two fingers move enough or when additional fingers are added, we make
3049 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3050 ALOG_ASSERT(activeTouchId >= 0);
3051
3052 bool settled = when >=
3053 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003054 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3055 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3056 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003057 *outFinishPreviousGesture = true;
3058 } else if (!settled && currentFingerCount > lastFingerCount) {
3059 // Additional pointers have gone down but not yet settled.
3060 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003061 if (DEBUG_GESTURES) {
3062 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3063 "MULTITOUCH, settle time remaining %0.3fms",
3064 (mPointerGesture.firstTouchTime +
3065 mConfig.pointerGestureMultitouchSettleInterval - when) *
3066 0.000001f);
3067 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003068 *outCancelPreviousGesture = true;
3069 } else {
3070 // Continue previous gesture.
3071 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3072 }
3073
3074 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003075 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003076 mPointerGesture.activeGestureId = 0;
3077 mPointerGesture.referenceIdBits.clear();
3078 mPointerVelocityControl.reset();
3079
3080 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003081 if (DEBUG_GESTURES) {
3082 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3083 "settle time remaining %0.3fms",
3084 (mPointerGesture.firstTouchTime +
3085 mConfig.pointerGestureMultitouchSettleInterval - when) *
3086 0.000001f);
3087 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003088 mCurrentRawState.rawPointerData
3089 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3090 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003091 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3092 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003093 }
3094
3095 // Clear the reference deltas for fingers not yet included in the reference calculation.
3096 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3097 ~mPointerGesture.referenceIdBits.value);
3098 !idBits.isEmpty();) {
3099 uint32_t id = idBits.clearFirstMarkedBit();
3100 mPointerGesture.referenceDeltas[id].dx = 0;
3101 mPointerGesture.referenceDeltas[id].dy = 0;
3102 }
3103 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3104
3105 // Add delta for all fingers and calculate a common movement delta.
3106 float commonDeltaX = 0, commonDeltaY = 0;
3107 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3108 mCurrentCookedState.fingerIdBits.value);
3109 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3110 bool first = (idBits == commonIdBits);
3111 uint32_t id = idBits.clearFirstMarkedBit();
3112 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3113 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3114 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3115 delta.dx += cpd.x - lpd.x;
3116 delta.dy += cpd.y - lpd.y;
3117
3118 if (first) {
3119 commonDeltaX = delta.dx;
3120 commonDeltaY = delta.dy;
3121 } else {
3122 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3123 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3124 }
3125 }
3126
3127 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003128 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003129 float dist[MAX_POINTER_ID + 1];
3130 int32_t distOverThreshold = 0;
3131 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3132 uint32_t id = idBits.clearFirstMarkedBit();
3133 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3134 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3135 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3136 distOverThreshold += 1;
3137 }
3138 }
3139
3140 // Only transition when at least two pointers have moved further than
3141 // the minimum distance threshold.
3142 if (distOverThreshold >= 2) {
3143 if (currentFingerCount > 2) {
3144 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003145 if (DEBUG_GESTURES) {
3146 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3147 currentFingerCount);
3148 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003149 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003150 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003151 } else {
3152 // There are exactly two pointers.
3153 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3154 uint32_t id1 = idBits.clearFirstMarkedBit();
3155 uint32_t id2 = idBits.firstMarkedBit();
3156 const RawPointerData::Pointer& p1 =
3157 mCurrentRawState.rawPointerData.pointerForId(id1);
3158 const RawPointerData::Pointer& p2 =
3159 mCurrentRawState.rawPointerData.pointerForId(id2);
3160 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3161 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3162 // There are two pointers but they are too far apart for a SWIPE,
3163 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003164 if (DEBUG_GESTURES) {
3165 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3166 "%0.3f",
3167 mutualDistance, mPointerGestureMaxSwipeWidth);
3168 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003169 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003170 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003171 } else {
3172 // There are two pointers. Wait for both pointers to start moving
3173 // before deciding whether this is a SWIPE or FREEFORM gesture.
3174 float dist1 = dist[id1];
3175 float dist2 = dist[id2];
3176 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3177 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3178 // Calculate the dot product of the displacement vectors.
3179 // When the vectors are oriented in approximately the same direction,
3180 // the angle betweeen them is near zero and the cosine of the angle
3181 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3182 // mag(v2).
3183 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3184 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3185 float dx1 = delta1.dx * mPointerXZoomScale;
3186 float dy1 = delta1.dy * mPointerYZoomScale;
3187 float dx2 = delta2.dx * mPointerXZoomScale;
3188 float dy2 = delta2.dy * mPointerYZoomScale;
3189 float dot = dx1 * dx2 + dy1 * dy2;
3190 float cosine = dot / (dist1 * dist2); // denominator always > 0
3191 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3192 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003193 if (DEBUG_GESTURES) {
3194 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3195 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3196 "cosine %0.3f >= %0.3f",
3197 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3198 mConfig.pointerGestureMultitouchMinDistance, cosine,
3199 mConfig.pointerGestureSwipeTransitionAngleCosine);
3200 }
Michael Wright227c5542020-07-02 18:30:52 +01003201 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003202 } else {
3203 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003204 if (DEBUG_GESTURES) {
3205 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3206 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3207 "cosine %0.3f < %0.3f",
3208 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3209 mConfig.pointerGestureMultitouchMinDistance, cosine,
3210 mConfig.pointerGestureSwipeTransitionAngleCosine);
3211 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003212 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003213 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003214 }
3215 }
3216 }
3217 }
3218 }
Michael Wright227c5542020-07-02 18:30:52 +01003219 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003220 // Switch from SWIPE to FREEFORM if additional pointers go down.
3221 // Cancel previous gesture.
3222 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003223 if (DEBUG_GESTURES) {
3224 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3225 currentFingerCount);
3226 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003227 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003228 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003229 }
3230 }
3231
3232 // Move the reference points based on the overall group motion of the fingers
3233 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003234 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003235 (commonDeltaX || commonDeltaY)) {
3236 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3237 uint32_t id = idBits.clearFirstMarkedBit();
3238 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3239 delta.dx = 0;
3240 delta.dy = 0;
3241 }
3242
3243 mPointerGesture.referenceTouchX += commonDeltaX;
3244 mPointerGesture.referenceTouchY += commonDeltaY;
3245
3246 commonDeltaX *= mPointerXMovementScale;
3247 commonDeltaY *= mPointerYMovementScale;
3248
Prabir Pradhan1728b212021-10-19 16:00:03 -07003249 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003250 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3251
3252 mPointerGesture.referenceGestureX += commonDeltaX;
3253 mPointerGesture.referenceGestureY += commonDeltaY;
3254 }
3255
3256 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003257 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3258 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003259 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003260 if (DEBUG_GESTURES) {
3261 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3262 "activeGestureId=%d, currentTouchPointerCount=%d",
3263 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3264 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003265 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3266
3267 mPointerGesture.currentGestureIdBits.clear();
3268 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3269 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3270 mPointerGesture.currentGestureProperties[0].clear();
3271 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3272 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3273 mPointerGesture.currentGestureCoords[0].clear();
3274 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3275 mPointerGesture.referenceGestureX);
3276 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3277 mPointerGesture.referenceGestureY);
3278 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003279 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003280 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003281 if (DEBUG_GESTURES) {
3282 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3283 "activeGestureId=%d, currentTouchPointerCount=%d",
3284 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3285 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003286 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3287
3288 mPointerGesture.currentGestureIdBits.clear();
3289
3290 BitSet32 mappedTouchIdBits;
3291 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003292 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003293 // Initially, assign the active gesture id to the active touch point
3294 // if there is one. No other touch id bits are mapped yet.
3295 if (!*outCancelPreviousGesture) {
3296 mappedTouchIdBits.markBit(activeTouchId);
3297 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3298 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3299 mPointerGesture.activeGestureId;
3300 } else {
3301 mPointerGesture.activeGestureId = -1;
3302 }
3303 } else {
3304 // Otherwise, assume we mapped all touches from the previous frame.
3305 // Reuse all mappings that are still applicable.
3306 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3307 mCurrentCookedState.fingerIdBits.value;
3308 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3309
3310 // Check whether we need to choose a new active gesture id because the
3311 // current went went up.
3312 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3313 ~mCurrentCookedState.fingerIdBits.value);
3314 !upTouchIdBits.isEmpty();) {
3315 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3316 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3317 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3318 mPointerGesture.activeGestureId = -1;
3319 break;
3320 }
3321 }
3322 }
3323
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003324 if (DEBUG_GESTURES) {
3325 ALOGD("Gestures: FREEFORM follow up "
3326 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3327 "activeGestureId=%d",
3328 mappedTouchIdBits.value, usedGestureIdBits.value,
3329 mPointerGesture.activeGestureId);
3330 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003331
3332 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3333 for (uint32_t i = 0; i < currentFingerCount; i++) {
3334 uint32_t touchId = idBits.clearFirstMarkedBit();
3335 uint32_t gestureId;
3336 if (!mappedTouchIdBits.hasBit(touchId)) {
3337 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3338 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003339 if (DEBUG_GESTURES) {
3340 ALOGD("Gestures: FREEFORM "
3341 "new mapping for touch id %d -> gesture id %d",
3342 touchId, gestureId);
3343 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003344 } else {
3345 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003346 if (DEBUG_GESTURES) {
3347 ALOGD("Gestures: FREEFORM "
3348 "existing mapping for touch id %d -> gesture id %d",
3349 touchId, gestureId);
3350 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003351 }
3352 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3353 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3354
3355 const RawPointerData::Pointer& pointer =
3356 mCurrentRawState.rawPointerData.pointerForId(touchId);
3357 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3358 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003359 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003360
3361 mPointerGesture.currentGestureProperties[i].clear();
3362 mPointerGesture.currentGestureProperties[i].id = gestureId;
3363 mPointerGesture.currentGestureProperties[i].toolType =
3364 AMOTION_EVENT_TOOL_TYPE_FINGER;
3365 mPointerGesture.currentGestureCoords[i].clear();
3366 mPointerGesture.currentGestureCoords[i]
3367 .setAxisValue(AMOTION_EVENT_AXIS_X,
3368 mPointerGesture.referenceGestureX + deltaX);
3369 mPointerGesture.currentGestureCoords[i]
3370 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3371 mPointerGesture.referenceGestureY + deltaY);
3372 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3373 1.0f);
3374 }
3375
3376 if (mPointerGesture.activeGestureId < 0) {
3377 mPointerGesture.activeGestureId =
3378 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003379 if (DEBUG_GESTURES) {
3380 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3381 mPointerGesture.activeGestureId);
3382 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003383 }
3384 }
3385 }
3386
3387 mPointerController->setButtonState(mCurrentRawState.buttonState);
3388
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003389 if (DEBUG_GESTURES) {
3390 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3391 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3392 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3393 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3394 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3395 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3396 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3397 uint32_t id = idBits.clearFirstMarkedBit();
3398 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3399 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3400 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3401 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3402 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3403 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3404 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3405 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3406 }
3407 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3408 uint32_t id = idBits.clearFirstMarkedBit();
3409 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3410 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3411 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3412 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3413 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3414 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3415 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3416 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3417 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003418 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003419 return true;
3420}
3421
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003423 mPointerSimple.currentCoords.clear();
3424 mPointerSimple.currentProperties.clear();
3425
3426 bool down, hovering;
3427 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3428 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3429 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003430 mPointerController
3431 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3432 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003433
3434 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3435 down = !hovering;
3436
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003437 float x, y;
3438 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003439 mPointerSimple.currentCoords.copyFrom(
3440 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3441 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3442 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3443 mPointerSimple.currentProperties.id = 0;
3444 mPointerSimple.currentProperties.toolType =
3445 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3446 } else {
3447 down = false;
3448 hovering = false;
3449 }
3450
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003451 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003452}
3453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3455 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003456}
3457
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003459 mPointerSimple.currentCoords.clear();
3460 mPointerSimple.currentProperties.clear();
3461
3462 bool down, hovering;
3463 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3464 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3465 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3466 float deltaX = 0, deltaY = 0;
3467 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3468 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3469 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3470 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3471 mPointerXMovementScale;
3472 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3473 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3474 mPointerYMovementScale;
3475
Prabir Pradhan1728b212021-10-19 16:00:03 -07003476 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003477 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3478
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003479 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003480 } else {
3481 mPointerVelocityControl.reset();
3482 }
3483
3484 down = isPointerDown(mCurrentRawState.buttonState);
3485 hovering = !down;
3486
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003487 float x, y;
3488 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003489 mPointerSimple.currentCoords.copyFrom(
3490 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3491 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3492 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3493 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3494 hovering ? 0.0f : 1.0f);
3495 mPointerSimple.currentProperties.id = 0;
3496 mPointerSimple.currentProperties.toolType =
3497 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3498 } else {
3499 mPointerVelocityControl.reset();
3500
3501 down = false;
3502 hovering = false;
3503 }
3504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003506}
3507
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3509 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003510
3511 mPointerVelocityControl.reset();
3512}
3513
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3515 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003516 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003517
3518 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003519 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003520 mPointerController->clearSpots();
3521 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003522 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003523 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003524 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003525 }
Garfield Tan9514d782020-11-10 16:37:23 -08003526 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003527
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003528 float xCursorPosition, yCursorPosition;
3529 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003530
3531 if (mPointerSimple.down && !down) {
3532 mPointerSimple.down = false;
3533
3534 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3536 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003537 mLastRawState.buttonState, MotionClassification::NONE,
3538 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3539 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3540 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3541 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003542 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003543 }
3544
3545 if (mPointerSimple.hovering && !hovering) {
3546 mPointerSimple.hovering = false;
3547
3548 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003549 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3550 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3551 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003552 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3553 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3554 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3555 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003556 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003557 }
3558
3559 if (down) {
3560 if (!mPointerSimple.down) {
3561 mPointerSimple.down = true;
3562 mPointerSimple.downTime = when;
3563
3564 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003566 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3567 metaState, mCurrentRawState.buttonState,
3568 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3569 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3570 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3571 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003572 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003573 }
3574
3575 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003576 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3577 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003578 mCurrentRawState.buttonState, MotionClassification::NONE,
3579 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3580 &mPointerSimple.currentCoords, mOrientedXPrecision,
3581 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3582 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003583 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003584 }
3585
3586 if (hovering) {
3587 if (!mPointerSimple.hovering) {
3588 mPointerSimple.hovering = true;
3589
3590 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003591 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003592 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3593 metaState, mCurrentRawState.buttonState,
3594 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3595 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3596 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3597 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003598 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003599 }
3600
3601 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003602 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3603 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3604 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003605 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3606 &mPointerSimple.currentCoords, mOrientedXPrecision,
3607 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3608 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003609 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003610 }
3611
3612 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3613 float vscroll = mCurrentRawState.rawVScroll;
3614 float hscroll = mCurrentRawState.rawHScroll;
3615 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3616 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3617
3618 // Send scroll.
3619 PointerCoords pointerCoords;
3620 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3621 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3622 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3623
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003624 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3625 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003626 mCurrentRawState.buttonState, MotionClassification::NONE,
3627 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3628 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3629 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3630 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003631 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003632 }
3633
3634 // Save state.
3635 if (down || hovering) {
3636 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3637 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3638 } else {
3639 mPointerSimple.reset();
3640 }
3641}
3642
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003644 mPointerSimple.currentCoords.clear();
3645 mPointerSimple.currentProperties.clear();
3646
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003648}
3649
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003650void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3651 uint32_t source, int32_t action, int32_t actionButton,
3652 int32_t flags, int32_t metaState, int32_t buttonState,
3653 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003654 const PointerCoords* coords, const uint32_t* idToIndex,
3655 BitSet32 idBits, int32_t changedId, float xPrecision,
3656 float yPrecision, nsecs_t downTime) {
3657 PointerCoords pointerCoords[MAX_POINTERS];
3658 PointerProperties pointerProperties[MAX_POINTERS];
3659 uint32_t pointerCount = 0;
3660 while (!idBits.isEmpty()) {
3661 uint32_t id = idBits.clearFirstMarkedBit();
3662 uint32_t index = idToIndex[id];
3663 pointerProperties[pointerCount].copyFrom(properties[index]);
3664 pointerCoords[pointerCount].copyFrom(coords[index]);
3665
3666 if (changedId >= 0 && id == uint32_t(changedId)) {
3667 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3668 }
3669
3670 pointerCount += 1;
3671 }
3672
3673 ALOG_ASSERT(pointerCount != 0);
3674
3675 if (changedId >= 0 && pointerCount == 1) {
3676 // Replace initial down and final up action.
3677 // We can compare the action without masking off the changed pointer index
3678 // because we know the index is 0.
3679 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3680 action = AMOTION_EVENT_ACTION_DOWN;
3681 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003682 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3683 action = AMOTION_EVENT_ACTION_CANCEL;
3684 } else {
3685 action = AMOTION_EVENT_ACTION_UP;
3686 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003687 } else {
3688 // Can't happen.
3689 ALOG_ASSERT(false);
3690 }
3691 }
3692 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3693 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003694 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003695 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003696 }
3697 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3698 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003699 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003700 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003701 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003702 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3703 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003704 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3705 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3706 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003707 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003708}
3709
3710bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3711 const PointerCoords* inCoords,
3712 const uint32_t* inIdToIndex,
3713 PointerProperties* outProperties,
3714 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3715 BitSet32 idBits) const {
3716 bool changed = false;
3717 while (!idBits.isEmpty()) {
3718 uint32_t id = idBits.clearFirstMarkedBit();
3719 uint32_t inIndex = inIdToIndex[id];
3720 uint32_t outIndex = outIdToIndex[id];
3721
3722 const PointerProperties& curInProperties = inProperties[inIndex];
3723 const PointerCoords& curInCoords = inCoords[inIndex];
3724 PointerProperties& curOutProperties = outProperties[outIndex];
3725 PointerCoords& curOutCoords = outCoords[outIndex];
3726
3727 if (curInProperties != curOutProperties) {
3728 curOutProperties.copyFrom(curInProperties);
3729 changed = true;
3730 }
3731
3732 if (curInCoords != curOutCoords) {
3733 curOutCoords.copyFrom(curInCoords);
3734 changed = true;
3735 }
3736 }
3737 return changed;
3738}
3739
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003740void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3741 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3742 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003743}
3744
Prabir Pradhan1728b212021-10-19 16:00:03 -07003745// Transform input device coordinates to display panel coordinates.
3746void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003747 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3748 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3749
arthurhunga36b28e2020-12-29 20:28:15 +08003750 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3751 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3752
Prabir Pradhan1728b212021-10-19 16:00:03 -07003753 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003754 // 0 - no swap and reverse.
3755 // 90 - swap x/y and reverse y.
3756 // 180 - reverse x, y.
3757 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003758 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003759 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003760 x = xScaled;
3761 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003762 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003763 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003764 y = xScaledMax;
3765 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003766 break;
3767 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003768 x = xScaledMax;
3769 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003770 break;
3771 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003772 y = xScaled;
3773 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003774 break;
3775 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003776 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003777 }
3778}
3779
Prabir Pradhan1728b212021-10-19 16:00:03 -07003780bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003781 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3782 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3783
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003784 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003785 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003786 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003787 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003788}
3789
3790const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3791 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003792 if (DEBUG_VIRTUAL_KEYS) {
3793 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3794 "left=%d, top=%d, right=%d, bottom=%d",
3795 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3796 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3797 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003798
3799 if (virtualKey.isHit(x, y)) {
3800 return &virtualKey;
3801 }
3802 }
3803
3804 return nullptr;
3805}
3806
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003807void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3808 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3809 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003810
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003811 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003812
3813 if (currentPointerCount == 0) {
3814 // No pointers to assign.
3815 return;
3816 }
3817
3818 if (lastPointerCount == 0) {
3819 // All pointers are new.
3820 for (uint32_t i = 0; i < currentPointerCount; i++) {
3821 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003822 current.rawPointerData.pointers[i].id = id;
3823 current.rawPointerData.idToIndex[id] = i;
3824 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003825 }
3826 return;
3827 }
3828
3829 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003830 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003831 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003832 uint32_t id = last.rawPointerData.pointers[0].id;
3833 current.rawPointerData.pointers[0].id = id;
3834 current.rawPointerData.idToIndex[id] = 0;
3835 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003836 return;
3837 }
3838
3839 // General case.
3840 // We build a heap of squared euclidean distances between current and last pointers
3841 // associated with the current and last pointer indices. Then, we find the best
3842 // match (by distance) for each current pointer.
3843 // The pointers must have the same tool type but it is possible for them to
3844 // transition from hovering to touching or vice-versa while retaining the same id.
3845 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3846
3847 uint32_t heapSize = 0;
3848 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3849 currentPointerIndex++) {
3850 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3851 lastPointerIndex++) {
3852 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003853 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003854 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003855 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003856 if (currentPointer.toolType == lastPointer.toolType) {
3857 int64_t deltaX = currentPointer.x - lastPointer.x;
3858 int64_t deltaY = currentPointer.y - lastPointer.y;
3859
3860 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3861
3862 // Insert new element into the heap (sift up).
3863 heap[heapSize].currentPointerIndex = currentPointerIndex;
3864 heap[heapSize].lastPointerIndex = lastPointerIndex;
3865 heap[heapSize].distance = distance;
3866 heapSize += 1;
3867 }
3868 }
3869 }
3870
3871 // Heapify
3872 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3873 startIndex -= 1;
3874 for (uint32_t parentIndex = startIndex;;) {
3875 uint32_t childIndex = parentIndex * 2 + 1;
3876 if (childIndex >= heapSize) {
3877 break;
3878 }
3879
3880 if (childIndex + 1 < heapSize &&
3881 heap[childIndex + 1].distance < heap[childIndex].distance) {
3882 childIndex += 1;
3883 }
3884
3885 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3886 break;
3887 }
3888
3889 swap(heap[parentIndex], heap[childIndex]);
3890 parentIndex = childIndex;
3891 }
3892 }
3893
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003894 if (DEBUG_POINTER_ASSIGNMENT) {
3895 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3896 for (size_t i = 0; i < heapSize; i++) {
3897 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3898 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3899 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003900 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003901
3902 // Pull matches out by increasing order of distance.
3903 // To avoid reassigning pointers that have already been matched, the loop keeps track
3904 // of which last and current pointers have been matched using the matchedXXXBits variables.
3905 // It also tracks the used pointer id bits.
3906 BitSet32 matchedLastBits(0);
3907 BitSet32 matchedCurrentBits(0);
3908 BitSet32 usedIdBits(0);
3909 bool first = true;
3910 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3911 while (heapSize > 0) {
3912 if (first) {
3913 // The first time through the loop, we just consume the root element of
3914 // the heap (the one with smallest distance).
3915 first = false;
3916 } else {
3917 // Previous iterations consumed the root element of the heap.
3918 // Pop root element off of the heap (sift down).
3919 heap[0] = heap[heapSize];
3920 for (uint32_t parentIndex = 0;;) {
3921 uint32_t childIndex = parentIndex * 2 + 1;
3922 if (childIndex >= heapSize) {
3923 break;
3924 }
3925
3926 if (childIndex + 1 < heapSize &&
3927 heap[childIndex + 1].distance < heap[childIndex].distance) {
3928 childIndex += 1;
3929 }
3930
3931 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3932 break;
3933 }
3934
3935 swap(heap[parentIndex], heap[childIndex]);
3936 parentIndex = childIndex;
3937 }
3938
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003939 if (DEBUG_POINTER_ASSIGNMENT) {
3940 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3941 for (size_t j = 0; j < heapSize; j++) {
3942 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3943 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3944 heap[j].distance);
3945 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003946 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003947 }
3948
3949 heapSize -= 1;
3950
3951 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3952 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3953
3954 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3955 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3956
3957 matchedCurrentBits.markBit(currentPointerIndex);
3958 matchedLastBits.markBit(lastPointerIndex);
3959
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003960 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3961 current.rawPointerData.pointers[currentPointerIndex].id = id;
3962 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3963 current.rawPointerData.markIdBit(id,
3964 current.rawPointerData.isHovering(
3965 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003966 usedIdBits.markBit(id);
3967
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003968 if (DEBUG_POINTER_ASSIGNMENT) {
3969 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3970 ", distance=%" PRIu64,
3971 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3972 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003973 break;
3974 }
3975 }
3976
3977 // Assign fresh ids to pointers that were not matched in the process.
3978 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3979 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3980 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3981
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003982 current.rawPointerData.pointers[currentPointerIndex].id = id;
3983 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3984 current.rawPointerData.markIdBit(id,
3985 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003986
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003987 if (DEBUG_POINTER_ASSIGNMENT) {
3988 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3989 id);
3990 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003991 }
3992}
3993
3994int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3995 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3996 return AKEY_STATE_VIRTUAL;
3997 }
3998
3999 for (const VirtualKey& virtualKey : mVirtualKeys) {
4000 if (virtualKey.keyCode == keyCode) {
4001 return AKEY_STATE_UP;
4002 }
4003 }
4004
4005 return AKEY_STATE_UNKNOWN;
4006}
4007
4008int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4009 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4010 return AKEY_STATE_VIRTUAL;
4011 }
4012
4013 for (const VirtualKey& virtualKey : mVirtualKeys) {
4014 if (virtualKey.scanCode == scanCode) {
4015 return AKEY_STATE_UP;
4016 }
4017 }
4018
4019 return AKEY_STATE_UNKNOWN;
4020}
4021
4022bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4023 const int32_t* keyCodes, uint8_t* outFlags) {
4024 for (const VirtualKey& virtualKey : mVirtualKeys) {
4025 for (size_t i = 0; i < numCodes; i++) {
4026 if (virtualKey.keyCode == keyCodes[i]) {
4027 outFlags[i] = 1;
4028 }
4029 }
4030 }
4031
4032 return true;
4033}
4034
4035std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4036 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004037 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004038 return std::make_optional(mPointerController->getDisplayId());
4039 } else {
4040 return std::make_optional(mViewport.displayId);
4041 }
4042 }
4043 return std::nullopt;
4044}
4045
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004046} // namespace android