blob: d6b72ed2613415d3f18ff0e593e945f3d89280f7 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Wright227c5542020-07-02 18:30:52 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wright227c5542020-07-02 18:30:52 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "TouchInputMapper.h"
22
Dominik Laskowski75788452021-02-09 18:51:25 -080023#include <ftl/enum.h>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorButtonAccumulator.h"
26#include "CursorScrollAccumulator.h"
27#include "TouchButtonAccumulator.h"
28#include "TouchCursorInputMapperCommon.h"
29
30namespace android {
31
32// --- Constants ---
33
34// Maximum amount of latency to add to touch events while waiting for data from an
35// external stylus.
36static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
37
38// Maximum amount of time to wait on touch data before pushing out new pressure data.
39static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
40
41// Artificial latency on synthetic events created from stylus data without corresponding touch
42// data.
43static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
44
45// --- Static Definitions ---
46
47template <typename T>
48inline static void swap(T& a, T& b) {
49 T temp = a;
50 a = b;
51 b = temp;
52}
53
54static float calculateCommonVector(float a, float b) {
55 if (a > 0 && b > 0) {
56 return a < b ? a : b;
57 } else if (a < 0 && b < 0) {
58 return a > b ? a : b;
59 } else {
60 return 0;
61 }
62}
63
64inline static float distance(float x1, float y1, float x2, float y2) {
65 return hypotf(x1 - x2, y1 - y2);
66}
67
68inline static int32_t signExtendNybble(int32_t value) {
69 return value >= 8 ? value - 16 : value;
70}
71
72// --- RawPointerAxes ---
73
74RawPointerAxes::RawPointerAxes() {
75 clear();
76}
77
78void RawPointerAxes::clear() {
79 x.clear();
80 y.clear();
81 pressure.clear();
82 touchMajor.clear();
83 touchMinor.clear();
84 toolMajor.clear();
85 toolMinor.clear();
86 orientation.clear();
87 distance.clear();
88 tiltX.clear();
89 tiltY.clear();
90 trackingId.clear();
91 slot.clear();
92}
93
94// --- RawPointerData ---
95
96RawPointerData::RawPointerData() {
97 clear();
98}
99
100void RawPointerData::clear() {
101 pointerCount = 0;
102 clearIdBits();
103}
104
105void RawPointerData::copyFrom(const RawPointerData& other) {
106 pointerCount = other.pointerCount;
107 hoveringIdBits = other.hoveringIdBits;
108 touchingIdBits = other.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +0800109 canceledIdBits = other.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
111 for (uint32_t i = 0; i < pointerCount; i++) {
112 pointers[i] = other.pointers[i];
113
114 int id = pointers[i].id;
115 idToIndex[id] = other.idToIndex[id];
116 }
117}
118
119void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
120 float x = 0, y = 0;
121 uint32_t count = touchingIdBits.count();
122 if (count) {
123 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
124 uint32_t id = idBits.clearFirstMarkedBit();
125 const Pointer& pointer = pointerForId(id);
126 x += pointer.x;
127 y += pointer.y;
128 }
129 x /= count;
130 y /= count;
131 }
132 *outX = x;
133 *outY = y;
134}
135
136// --- CookedPointerData ---
137
138CookedPointerData::CookedPointerData() {
139 clear();
140}
141
142void CookedPointerData::clear() {
143 pointerCount = 0;
144 hoveringIdBits.clear();
145 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800146 canceledIdBits.clear();
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000147 validIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700148}
149
150void CookedPointerData::copyFrom(const CookedPointerData& other) {
151 pointerCount = other.pointerCount;
152 hoveringIdBits = other.hoveringIdBits;
153 touchingIdBits = other.touchingIdBits;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000154 validIdBits = other.validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700155
156 for (uint32_t i = 0; i < pointerCount; i++) {
157 pointerProperties[i].copyFrom(other.pointerProperties[i]);
158 pointerCoords[i].copyFrom(other.pointerCoords[i]);
159
160 int id = pointerProperties[i].id;
161 idToIndex[id] = other.idToIndex[id];
162 }
163}
164
165// --- TouchInputMapper ---
166
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800167TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
168 : InputMapper(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700169 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100170 mDeviceMode(DeviceMode::DISABLED),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700171 mDisplayWidth(-1),
172 mDisplayHeight(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173 mPhysicalWidth(-1),
174 mPhysicalHeight(-1),
175 mPhysicalLeft(0),
176 mPhysicalTop(0),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700177 mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700178
179TouchInputMapper::~TouchInputMapper() {}
180
Philip Junker4af3b3d2021-12-14 10:36:55 +0100181uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182 return mSource;
183}
184
185void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
186 InputMapper::populateDeviceInfo(info);
187
Michael Wright227c5542020-07-02 18:30:52 +0100188 if (mDeviceMode != DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700189 info->addMotionRange(mOrientedRanges.x);
190 info->addMotionRange(mOrientedRanges.y);
191 info->addMotionRange(mOrientedRanges.pressure);
192
Chris Yef74dc422020-09-02 22:41:50 -0700193 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
Chris Ye8fa17282020-09-15 17:17:34 -0700194 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
195 //
196 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
197 // motion, i.e. the hardware dimensions, as the finger could move completely across the
198 // touchpad in one sample cycle.
Chris Yef74dc422020-09-02 22:41:50 -0700199 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
200 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
201 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat,
202 x.fuzz, x.resolution);
203 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat,
204 y.fuzz, y.resolution);
205 }
206
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700207 if (mOrientedRanges.haveSize) {
208 info->addMotionRange(mOrientedRanges.size);
209 }
210
211 if (mOrientedRanges.haveTouchSize) {
212 info->addMotionRange(mOrientedRanges.touchMajor);
213 info->addMotionRange(mOrientedRanges.touchMinor);
214 }
215
216 if (mOrientedRanges.haveToolSize) {
217 info->addMotionRange(mOrientedRanges.toolMajor);
218 info->addMotionRange(mOrientedRanges.toolMinor);
219 }
220
221 if (mOrientedRanges.haveOrientation) {
222 info->addMotionRange(mOrientedRanges.orientation);
223 }
224
225 if (mOrientedRanges.haveDistance) {
226 info->addMotionRange(mOrientedRanges.distance);
227 }
228
229 if (mOrientedRanges.haveTilt) {
230 info->addMotionRange(mOrientedRanges.tilt);
231 }
232
233 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
234 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
235 0.0f);
236 }
237 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
238 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
239 0.0f);
240 }
Michael Wright227c5542020-07-02 18:30:52 +0100241 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700242 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
243 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
244 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
245 x.fuzz, x.resolution);
246 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
247 y.fuzz, y.resolution);
248 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
249 x.fuzz, x.resolution);
250 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
251 y.fuzz, y.resolution);
252 }
253 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
254 }
255}
256
257void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700258 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800259 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700260 dumpParameters(dump);
261 dumpVirtualKeys(dump);
262 dumpRawPointerAxes(dump);
263 dumpCalibration(dump);
264 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700265 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266
267 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268 dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale);
269 dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale);
270 dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
271 dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
272 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
273 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
274 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
275 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
276 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
277 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
278 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
279 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
280 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
281 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
282
283 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
284 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
285 mLastRawState.rawPointerData.pointerCount);
286 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
287 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
288 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
289 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
290 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
291 "toolType=%d, isHovering=%s\n",
292 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
293 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
294 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
295 pointer.distance, pointer.toolType, toString(pointer.isHovering));
296 }
297
298 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
299 mLastCookedState.buttonState);
300 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
301 mLastCookedState.cookedPointerData.pointerCount);
302 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
303 const PointerProperties& pointerProperties =
304 mLastCookedState.cookedPointerData.pointerProperties[i];
305 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000306 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
307 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
308 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700309 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
310 "toolType=%d, isHovering=%s\n",
311 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000312 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
313 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
315 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
316 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
317 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
318 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
319 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
320 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
321 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
322 pointerProperties.toolType,
323 toString(mLastCookedState.cookedPointerData.isHovering(i)));
324 }
325
326 dump += INDENT3 "Stylus Fusion:\n";
327 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
328 toString(mExternalStylusConnected));
329 dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
330 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
331 mExternalStylusFusionTimeout);
332 dump += INDENT3 "External Stylus State:\n";
333 dumpStylusState(dump, mExternalStylusState);
334
Michael Wright227c5542020-07-02 18:30:52 +0100335 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
337 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
338 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
339 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
340 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
341 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
342 }
343}
344
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
346 uint32_t changes) {
347 InputMapper::configure(when, config, changes);
348
349 mConfig = *config;
350
351 if (!changes) { // first time only
352 // Configure basic parameters.
353 configureParameters();
354
355 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800356 mCursorScrollAccumulator.configure(getDeviceContext());
357 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700358
359 // Configure absolute axis information.
360 configureRawPointerAxes();
361
362 // Prepare input device calibration.
363 parseCalibration();
364 resolveCalibration();
365 }
366
367 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
368 // Update location calibration to reflect current settings
369 updateAffineTransformation();
370 }
371
372 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
373 // Update pointer speed.
374 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
375 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
376 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
377 }
378
379 bool resetNeeded = false;
380 if (!changes ||
381 (changes &
382 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800383 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
385 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
386 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700387 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700389 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390 }
391
392 if (changes && resetNeeded) {
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.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000553 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800554 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000555 * 3. Get the matching viewport by either unique id in idc file or by the display type
556 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800557 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700558 */
559std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800560 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000561 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800562 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700563 }
564
Christine Franks2a2293c2022-01-18 11:51:16 -0800565 const std::optional<std::string> associatedDisplayUniqueId =
566 getDeviceContext().getAssociatedDisplayUniqueId();
567 if (associatedDisplayUniqueId) {
568 return getDeviceContext().getAssociatedViewport();
569 }
570
Michael Wright227c5542020-07-02 18:30:52 +0100571 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800572 std::optional<DisplayViewport> viewport =
573 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
574 if (viewport) {
575 return viewport;
576 } else {
577 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
578 mConfig.defaultPointerDisplayId);
579 }
580 }
581
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700582 // Check if uniqueDisplayId is specified in idc file.
583 if (!mParameters.uniqueDisplayId.empty()) {
584 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
585 }
586
587 ViewportType viewportTypeToUse;
588 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100589 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700590 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100591 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700592 }
593
594 std::optional<DisplayViewport> viewport =
595 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100596 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700597 ALOGW("Input device %s should be associated with external display, "
598 "fallback to internal one for the external viewport is not found.",
599 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100600 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700601 }
602
603 return viewport;
604 }
605
606 // No associated display, return a non-display viewport.
607 DisplayViewport newViewport;
608 // Raw width and height in the natural orientation.
609 int32_t rawWidth = mRawPointerAxes.getRawWidth();
610 int32_t rawHeight = mRawPointerAxes.getRawHeight();
611 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
612 return std::make_optional(newViewport);
613}
614
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800615int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
616 if (resolution < 0) {
617 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
618 getDeviceName().c_str());
619 return 0;
620 }
621 return resolution;
622}
623
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800624void TouchInputMapper::initializeSizeRanges() {
625 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
626 mSizeScale = 0.0f;
627 return;
628 }
629
630 // Size of diagonal axis.
631 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
632
633 // Size factors.
634 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
635 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
636 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
637 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
638 } else {
639 mSizeScale = 0.0f;
640 }
641
642 mOrientedRanges.haveTouchSize = true;
643 mOrientedRanges.haveToolSize = true;
644 mOrientedRanges.haveSize = true;
645
646 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
647 mOrientedRanges.touchMajor.source = mSource;
648 mOrientedRanges.touchMajor.min = 0;
649 mOrientedRanges.touchMajor.max = diagonalSize;
650 mOrientedRanges.touchMajor.flat = 0;
651 mOrientedRanges.touchMajor.fuzz = 0;
652 mOrientedRanges.touchMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800653 if (mRawPointerAxes.touchMajor.valid) {
654 mRawPointerAxes.touchMajor.resolution =
655 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
656 mOrientedRanges.touchMajor.resolution = mRawPointerAxes.touchMajor.resolution;
657 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800658
659 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
660 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800661 if (mRawPointerAxes.touchMinor.valid) {
662 mRawPointerAxes.touchMinor.resolution =
663 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
664 mOrientedRanges.touchMinor.resolution = mRawPointerAxes.touchMinor.resolution;
665 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800666
667 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
668 mOrientedRanges.toolMajor.source = mSource;
669 mOrientedRanges.toolMajor.min = 0;
670 mOrientedRanges.toolMajor.max = diagonalSize;
671 mOrientedRanges.toolMajor.flat = 0;
672 mOrientedRanges.toolMajor.fuzz = 0;
673 mOrientedRanges.toolMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800674 if (mRawPointerAxes.toolMajor.valid) {
675 mRawPointerAxes.toolMajor.resolution =
676 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
677 mOrientedRanges.toolMajor.resolution = mRawPointerAxes.toolMajor.resolution;
678 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800679
680 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
681 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800682 if (mRawPointerAxes.toolMinor.valid) {
683 mRawPointerAxes.toolMinor.resolution =
684 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
685 mOrientedRanges.toolMinor.resolution = mRawPointerAxes.toolMinor.resolution;
686 }
687
688 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
689 mOrientedRanges.touchMajor.resolution *= mGeometricScale;
690 mOrientedRanges.touchMinor.resolution *= mGeometricScale;
691 mOrientedRanges.toolMajor.resolution *= mGeometricScale;
692 mOrientedRanges.toolMinor.resolution *= mGeometricScale;
693 } else {
694 // Support for other calibrations can be added here.
695 ALOGW("%s calibration is not supported for size ranges at the moment. "
696 "Using raw resolution instead",
697 ftl::enum_string(mCalibration.sizeCalibration).c_str());
698 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800699
700 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
701 mOrientedRanges.size.source = mSource;
702 mOrientedRanges.size.min = 0;
703 mOrientedRanges.size.max = 1.0;
704 mOrientedRanges.size.flat = 0;
705 mOrientedRanges.size.fuzz = 0;
706 mOrientedRanges.size.resolution = 0;
707}
708
709void TouchInputMapper::initializeOrientedRanges() {
710 // Configure X and Y factors.
711 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
712 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
713 mXPrecision = 1.0f / mXScale;
714 mYPrecision = 1.0f / mYScale;
715
716 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
717 mOrientedRanges.x.source = mSource;
718 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
719 mOrientedRanges.y.source = mSource;
720
721 // Scale factor for terms that are not oriented in a particular axis.
722 // If the pixels are square then xScale == yScale otherwise we fake it
723 // by choosing an average.
724 mGeometricScale = avg(mXScale, mYScale);
725
726 initializeSizeRanges();
727
728 // Pressure factors.
729 mPressureScale = 0;
730 float pressureMax = 1.0;
731 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
732 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
733 if (mCalibration.havePressureScale) {
734 mPressureScale = mCalibration.pressureScale;
735 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
736 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
737 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
738 }
739 }
740
741 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
742 mOrientedRanges.pressure.source = mSource;
743 mOrientedRanges.pressure.min = 0;
744 mOrientedRanges.pressure.max = pressureMax;
745 mOrientedRanges.pressure.flat = 0;
746 mOrientedRanges.pressure.fuzz = 0;
747 mOrientedRanges.pressure.resolution = 0;
748
749 // Tilt
750 mTiltXCenter = 0;
751 mTiltXScale = 0;
752 mTiltYCenter = 0;
753 mTiltYScale = 0;
754 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
755 if (mHaveTilt) {
756 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
757 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
758 mTiltXScale = M_PI / 180;
759 mTiltYScale = M_PI / 180;
760
761 if (mRawPointerAxes.tiltX.resolution) {
762 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
763 }
764 if (mRawPointerAxes.tiltY.resolution) {
765 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
766 }
767
768 mOrientedRanges.haveTilt = true;
769
770 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
771 mOrientedRanges.tilt.source = mSource;
772 mOrientedRanges.tilt.min = 0;
773 mOrientedRanges.tilt.max = M_PI_2;
774 mOrientedRanges.tilt.flat = 0;
775 mOrientedRanges.tilt.fuzz = 0;
776 mOrientedRanges.tilt.resolution = 0;
777 }
778
779 // Orientation
780 mOrientationScale = 0;
781 if (mHaveTilt) {
782 mOrientedRanges.haveOrientation = true;
783
784 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
785 mOrientedRanges.orientation.source = mSource;
786 mOrientedRanges.orientation.min = -M_PI;
787 mOrientedRanges.orientation.max = M_PI;
788 mOrientedRanges.orientation.flat = 0;
789 mOrientedRanges.orientation.fuzz = 0;
790 mOrientedRanges.orientation.resolution = 0;
791 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
792 if (mCalibration.orientationCalibration ==
793 Calibration::OrientationCalibration::INTERPOLATED) {
794 if (mRawPointerAxes.orientation.valid) {
795 if (mRawPointerAxes.orientation.maxValue > 0) {
796 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
797 } else if (mRawPointerAxes.orientation.minValue < 0) {
798 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
799 } else {
800 mOrientationScale = 0;
801 }
802 }
803 }
804
805 mOrientedRanges.haveOrientation = true;
806
807 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
808 mOrientedRanges.orientation.source = mSource;
809 mOrientedRanges.orientation.min = -M_PI_2;
810 mOrientedRanges.orientation.max = M_PI_2;
811 mOrientedRanges.orientation.flat = 0;
812 mOrientedRanges.orientation.fuzz = 0;
813 mOrientedRanges.orientation.resolution = 0;
814 }
815
816 // Distance
817 mDistanceScale = 0;
818 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
819 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
820 if (mCalibration.haveDistanceScale) {
821 mDistanceScale = mCalibration.distanceScale;
822 } else {
823 mDistanceScale = 1.0f;
824 }
825 }
826
827 mOrientedRanges.haveDistance = true;
828
829 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
830 mOrientedRanges.distance.source = mSource;
831 mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
832 mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
833 mOrientedRanges.distance.flat = 0;
834 mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
835 mOrientedRanges.distance.resolution = 0;
836 }
837
838 // Compute oriented precision, scales and ranges.
839 // Note that the maximum value reported is an inclusive maximum value so it is one
840 // unit less than the total width or height of the display.
841 switch (mInputDeviceOrientation) {
842 case DISPLAY_ORIENTATION_90:
843 case DISPLAY_ORIENTATION_270:
844 mOrientedXPrecision = mYPrecision;
845 mOrientedYPrecision = mXPrecision;
846
847 mOrientedRanges.x.min = 0;
848 mOrientedRanges.x.max = mDisplayHeight - 1;
849 mOrientedRanges.x.flat = 0;
850 mOrientedRanges.x.fuzz = 0;
851 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
852
853 mOrientedRanges.y.min = 0;
854 mOrientedRanges.y.max = mDisplayWidth - 1;
855 mOrientedRanges.y.flat = 0;
856 mOrientedRanges.y.fuzz = 0;
857 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
858 break;
859
860 default:
861 mOrientedXPrecision = mXPrecision;
862 mOrientedYPrecision = mYPrecision;
863
864 mOrientedRanges.x.min = 0;
865 mOrientedRanges.x.max = mDisplayWidth - 1;
866 mOrientedRanges.x.flat = 0;
867 mOrientedRanges.x.fuzz = 0;
868 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
869
870 mOrientedRanges.y.min = 0;
871 mOrientedRanges.y.max = mDisplayHeight - 1;
872 mOrientedRanges.y.flat = 0;
873 mOrientedRanges.y.fuzz = 0;
874 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
875 break;
876 }
877}
878
Prabir Pradhan1728b212021-10-19 16:00:03 -0700879void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100880 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700881
882 resolveExternalStylusPresence();
883
884 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100885 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000886 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700887 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100888 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700889 if (hasStylus()) {
890 mSource |= AINPUT_SOURCE_STYLUS;
891 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800892 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700893 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100894 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700895 if (hasStylus()) {
896 mSource |= AINPUT_SOURCE_STYLUS;
897 }
898 if (hasExternalStylus()) {
899 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
900 }
Michael Wright227c5542020-07-02 18:30:52 +0100901 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700902 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100903 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700904 } else {
905 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100906 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700907 }
908
909 // Ensure we have valid X and Y axes.
910 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
911 ALOGW("Touch device '%s' did not report support for X or Y axis! "
912 "The device will be inoperable.",
913 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100914 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700915 return;
916 }
917
918 // Get associated display dimensions.
919 std::optional<DisplayViewport> newViewport = findViewport();
920 if (!newViewport) {
921 ALOGI("Touch device '%s' could not query the properties of its associated "
922 "display. The device will be inoperable until the display size "
923 "becomes available.",
924 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100925 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700926 return;
927 }
928
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000929 if (!newViewport->isActive) {
930 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
931 getDeviceName().c_str(), getDeviceId());
932 mDeviceMode = DeviceMode::DISABLED;
933 return;
934 }
935
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700936 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700937 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
938 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700939
Prabir Pradhan1728b212021-10-19 16:00:03 -0700940 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700941 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700942 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700943 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700944 mViewport = *newViewport;
945
Michael Wright227c5542020-07-02 18:30:52 +0100946 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700947 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700948 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
949 int32_t naturalPhysicalLeft, naturalPhysicalTop;
950 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700951
Prabir Pradhan1728b212021-10-19 16:00:03 -0700952 // Apply the inverse of the input device orientation so that the input device is
953 // configured in the same orientation as the viewport. The input device orientation will
954 // be re-applied by mInputDeviceOrientation.
955 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700956 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700957 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700958 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700959 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
960 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800961 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700962 naturalPhysicalTop = mViewport.physicalLeft;
963 naturalDeviceWidth = mViewport.deviceHeight;
964 naturalDeviceHeight = mViewport.deviceWidth;
965 break;
966 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700967 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
968 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
969 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
970 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
971 naturalDeviceWidth = mViewport.deviceWidth;
972 naturalDeviceHeight = mViewport.deviceHeight;
973 break;
974 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700975 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
976 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
977 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800978 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700979 naturalDeviceWidth = mViewport.deviceHeight;
980 naturalDeviceHeight = mViewport.deviceWidth;
981 break;
982 case DISPLAY_ORIENTATION_0:
983 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700984 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
985 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
986 naturalPhysicalLeft = mViewport.physicalLeft;
987 naturalPhysicalTop = mViewport.physicalTop;
988 naturalDeviceWidth = mViewport.deviceWidth;
989 naturalDeviceHeight = mViewport.deviceHeight;
990 break;
991 }
992
993 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
994 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
995 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
996 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
997 }
998
999 mPhysicalWidth = naturalPhysicalWidth;
1000 mPhysicalHeight = naturalPhysicalHeight;
1001 mPhysicalLeft = naturalPhysicalLeft;
1002 mPhysicalTop = naturalPhysicalTop;
1003
Prabir Pradhan1728b212021-10-19 16:00:03 -07001004 const int32_t oldDisplayWidth = mDisplayWidth;
1005 const int32_t oldDisplayHeight = mDisplayHeight;
1006 mDisplayWidth = naturalDeviceWidth;
1007 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001008
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001009 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1010 // anything if the device is already orientation-aware. If the device is not
1011 // orientation-aware, then we need to apply the inverse rotation of the display so that
1012 // when the display rotation is applied later as a part of the per-window transform, we
1013 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001014 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001015 ? DISPLAY_ORIENTATION_0
1016 : getInverseRotation(mViewport.orientation);
1017 // For orientation-aware devices that work in the un-rotated coordinate space, the
1018 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001019 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
1020 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001021
1022 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001023 mInputDeviceOrientation =
1024 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001025 } else {
1026 mPhysicalWidth = rawWidth;
1027 mPhysicalHeight = rawHeight;
1028 mPhysicalLeft = 0;
1029 mPhysicalTop = 0;
1030
Prabir Pradhan1728b212021-10-19 16:00:03 -07001031 mDisplayWidth = rawWidth;
1032 mDisplayHeight = rawHeight;
1033 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001034 }
1035 }
1036
1037 // If moving between pointer modes, need to reset some state.
1038 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1039 if (deviceModeChanged) {
1040 mOrientedRanges.clear();
1041 }
1042
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001043 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1044 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001045 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001046 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001047 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1048 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001049 if (mPointerController == nullptr) {
1050 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001051 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001052 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001053 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1054 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001055 } else {
Michael Wright17db18e2020-06-26 20:51:44 +01001056 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001057 }
1058
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001059 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001060 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1061 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001062 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1063 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001064
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001065 configureVirtualKeys();
1066
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001067 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001068
1069 // Location
1070 updateAffineTransformation();
1071
Michael Wright227c5542020-07-02 18:30:52 +01001072 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001073 // Compute pointer gesture detection parameters.
1074 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001075 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001076
1077 // Scale movements such that one whole swipe of the touch pad covers a
1078 // given area relative to the diagonal size of the display when no acceleration
1079 // is applied.
1080 // Assume that the touch pad has a square aspect ratio such that movements in
1081 // X and Y of the same number of raw units cover the same physical distance.
1082 mPointerXMovementScale =
1083 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1084 mPointerYMovementScale = mPointerXMovementScale;
1085
1086 // Scale zooms to cover a smaller range of the display than movements do.
1087 // This value determines the area around the pointer that is affected by freeform
1088 // pointer gestures.
1089 mPointerXZoomScale =
1090 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1091 mPointerYZoomScale = mPointerXZoomScale;
1092
1093 // Max width between pointers to detect a swipe gesture is more than some fraction
1094 // of the diagonal axis of the touch pad. Touches that are wider than this are
1095 // translated into freeform gestures.
1096 mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
1097
1098 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001099 const nsecs_t readTime = when; // synthetic event
1100 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001101 }
1102
1103 // Inform the dispatcher about the changes.
1104 *outResetNeeded = true;
1105 bumpGeneration();
1106 }
1107}
1108
Prabir Pradhan1728b212021-10-19 16:00:03 -07001109void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001110 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001111 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1112 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001113 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1114 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1115 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1116 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001117 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001118}
1119
1120void TouchInputMapper::configureVirtualKeys() {
1121 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001122 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001123
1124 mVirtualKeys.clear();
1125
1126 if (virtualKeyDefinitions.size() == 0) {
1127 return;
1128 }
1129
1130 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1131 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1132 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1133 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1134
1135 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1136 VirtualKey virtualKey;
1137
1138 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1139 int32_t keyCode;
1140 int32_t dummyKeyMetaState;
1141 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001142 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1143 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001144 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1145 continue; // drop the key
1146 }
1147
1148 virtualKey.keyCode = keyCode;
1149 virtualKey.flags = flags;
1150
1151 // convert the key definition's display coordinates into touch coordinates for a hit box
1152 int32_t halfWidth = virtualKeyDefinition.width / 2;
1153 int32_t halfHeight = virtualKeyDefinition.height / 2;
1154
1155 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001156 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001157 touchScreenLeft;
1158 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001159 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001160 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001161 virtualKey.hitTop =
1162 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001163 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001164 virtualKey.hitBottom =
1165 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 touchScreenTop;
1167 mVirtualKeys.push_back(virtualKey);
1168 }
1169}
1170
1171void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1172 if (!mVirtualKeys.empty()) {
1173 dump += INDENT3 "Virtual Keys:\n";
1174
1175 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1176 const VirtualKey& virtualKey = mVirtualKeys[i];
1177 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1178 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1179 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1180 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1181 }
1182 }
1183}
1184
1185void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001186 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001187 Calibration& out = mCalibration;
1188
1189 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001190 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001191 String8 sizeCalibrationString;
1192 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
1193 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001194 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001195 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001196 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001197 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001198 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001199 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001200 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001201 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001202 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001203 } else if (sizeCalibrationString != "default") {
1204 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string());
1205 }
1206 }
1207
1208 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale);
1209 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias);
1210 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed);
1211
1212 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001213 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001214 String8 pressureCalibrationString;
1215 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
1216 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001217 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001218 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001219 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001220 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001221 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001222 } else if (pressureCalibrationString != "default") {
1223 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
1224 pressureCalibrationString.string());
1225 }
1226 }
1227
1228 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale);
1229
1230 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001231 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001232 String8 orientationCalibrationString;
1233 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
1234 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001235 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001236 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001237 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001238 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001239 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001240 } else if (orientationCalibrationString != "default") {
1241 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
1242 orientationCalibrationString.string());
1243 }
1244 }
1245
1246 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001247 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001248 String8 distanceCalibrationString;
1249 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
1250 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001251 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001252 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001253 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001254 } else if (distanceCalibrationString != "default") {
1255 ALOGW("Invalid value for touch.distance.calibration: '%s'",
1256 distanceCalibrationString.string());
1257 }
1258 }
1259
1260 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale);
1261
Michael Wright227c5542020-07-02 18:30:52 +01001262 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001263 String8 coverageCalibrationString;
1264 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
1265 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001266 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001267 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001268 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001269 } else if (coverageCalibrationString != "default") {
1270 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
1271 coverageCalibrationString.string());
1272 }
1273 }
1274}
1275
1276void TouchInputMapper::resolveCalibration() {
1277 // Size
1278 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001279 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1280 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001281 }
1282 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001283 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001284 }
1285
1286 // Pressure
1287 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001288 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1289 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001290 }
1291 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001292 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001293 }
1294
1295 // Orientation
1296 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001297 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1298 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001299 }
1300 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001301 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001302 }
1303
1304 // Distance
1305 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001306 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1307 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001308 }
1309 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001310 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001311 }
1312
1313 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001314 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1315 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001316 }
1317}
1318
1319void TouchInputMapper::dumpCalibration(std::string& dump) {
1320 dump += INDENT3 "Calibration:\n";
1321
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001322 dump += INDENT4 "touch.size.calibration: ";
1323 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001324
1325 if (mCalibration.haveSizeScale) {
1326 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
1327 }
1328
1329 if (mCalibration.haveSizeBias) {
1330 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
1331 }
1332
1333 if (mCalibration.haveSizeIsSummed) {
1334 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
1335 toString(mCalibration.sizeIsSummed));
1336 }
1337
1338 // Pressure
1339 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001340 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001341 dump += INDENT4 "touch.pressure.calibration: none\n";
1342 break;
Michael Wright227c5542020-07-02 18:30:52 +01001343 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001344 dump += INDENT4 "touch.pressure.calibration: physical\n";
1345 break;
Michael Wright227c5542020-07-02 18:30:52 +01001346 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001347 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1348 break;
1349 default:
1350 ALOG_ASSERT(false);
1351 }
1352
1353 if (mCalibration.havePressureScale) {
1354 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
1355 }
1356
1357 // Orientation
1358 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001359 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001360 dump += INDENT4 "touch.orientation.calibration: none\n";
1361 break;
Michael Wright227c5542020-07-02 18:30:52 +01001362 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001363 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1364 break;
Michael Wright227c5542020-07-02 18:30:52 +01001365 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001366 dump += INDENT4 "touch.orientation.calibration: vector\n";
1367 break;
1368 default:
1369 ALOG_ASSERT(false);
1370 }
1371
1372 // Distance
1373 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001374 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001375 dump += INDENT4 "touch.distance.calibration: none\n";
1376 break;
Michael Wright227c5542020-07-02 18:30:52 +01001377 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001378 dump += INDENT4 "touch.distance.calibration: scaled\n";
1379 break;
1380 default:
1381 ALOG_ASSERT(false);
1382 }
1383
1384 if (mCalibration.haveDistanceScale) {
1385 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
1386 }
1387
1388 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001389 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001390 dump += INDENT4 "touch.coverage.calibration: none\n";
1391 break;
Michael Wright227c5542020-07-02 18:30:52 +01001392 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001393 dump += INDENT4 "touch.coverage.calibration: box\n";
1394 break;
1395 default:
1396 ALOG_ASSERT(false);
1397 }
1398}
1399
1400void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1401 dump += INDENT3 "Affine Transformation:\n";
1402
1403 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1404 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1405 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1406 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1407 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1408 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1409}
1410
1411void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001412 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001413 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001414}
1415
1416void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001417 mCursorButtonAccumulator.reset(getDeviceContext());
1418 mCursorScrollAccumulator.reset(getDeviceContext());
1419 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001420
1421 mPointerVelocityControl.reset();
1422 mWheelXVelocityControl.reset();
1423 mWheelYVelocityControl.reset();
1424
1425 mRawStatesPending.clear();
1426 mCurrentRawState.clear();
1427 mCurrentCookedState.clear();
1428 mLastRawState.clear();
1429 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001430 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001431 mSentHoverEnter = false;
1432 mHavePointerIds = false;
1433 mCurrentMotionAborted = false;
1434 mDownTime = 0;
1435
1436 mCurrentVirtualKey.down = false;
1437
1438 mPointerGesture.reset();
1439 mPointerSimple.reset();
1440 resetExternalStylus();
1441
1442 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001443 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001444 mPointerController->clearSpots();
1445 }
1446
1447 InputMapper::reset(when);
1448}
1449
1450void TouchInputMapper::resetExternalStylus() {
1451 mExternalStylusState.clear();
1452 mExternalStylusId = -1;
1453 mExternalStylusFusionTimeout = LLONG_MAX;
1454 mExternalStylusDataPending = false;
1455}
1456
1457void TouchInputMapper::clearStylusDataPendingFlags() {
1458 mExternalStylusDataPending = false;
1459 mExternalStylusFusionTimeout = LLONG_MAX;
1460}
1461
1462void TouchInputMapper::process(const RawEvent* rawEvent) {
1463 mCursorButtonAccumulator.process(rawEvent);
1464 mCursorScrollAccumulator.process(rawEvent);
1465 mTouchButtonAccumulator.process(rawEvent);
1466
1467 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001468 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001469 }
1470}
1471
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001472void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001473 // Push a new state.
1474 mRawStatesPending.emplace_back();
1475
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001476 RawState& next = mRawStatesPending.back();
1477 next.clear();
1478 next.when = when;
1479 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001480
1481 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001482 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001483 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1484
1485 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001486 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1487 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001488 mCursorScrollAccumulator.finishSync();
1489
1490 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001491 syncTouch(when, &next);
1492
1493 // The last RawState is the actually second to last, since we just added a new state
1494 const RawState& last =
1495 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001496
1497 // Assign pointer ids.
1498 if (!mHavePointerIds) {
1499 assignPointerIds(last, next);
1500 }
1501
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001502 if (DEBUG_RAW_EVENTS) {
1503 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1504 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1505 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1506 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1507 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1508 next.rawPointerData.canceledIdBits.value);
1509 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001510
Arthur Hung9ad18942021-06-19 02:04:46 +00001511 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1512 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1513 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1514 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1515 next.rawPointerData.hoveringIdBits.value);
1516 }
1517
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001518 processRawTouches(false /*timeout*/);
1519}
1520
1521void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001522 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001523 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001524 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001525 mCurrentCookedState.clear();
1526 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001527 return;
1528 }
1529
1530 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1531 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1532 // touching the current state will only observe the events that have been dispatched to the
1533 // rest of the pipeline.
1534 const size_t N = mRawStatesPending.size();
1535 size_t count;
1536 for (count = 0; count < N; count++) {
1537 const RawState& next = mRawStatesPending[count];
1538
1539 // A failure to assign the stylus id means that we're waiting on stylus data
1540 // and so should defer the rest of the pipeline.
1541 if (assignExternalStylusId(next, timeout)) {
1542 break;
1543 }
1544
1545 // All ready to go.
1546 clearStylusDataPendingFlags();
1547 mCurrentRawState.copyFrom(next);
1548 if (mCurrentRawState.when < mLastRawState.when) {
1549 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001550 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001551 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001552 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001553 }
1554 if (count != 0) {
1555 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1556 }
1557
1558 if (mExternalStylusDataPending) {
1559 if (timeout) {
1560 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1561 clearStylusDataPendingFlags();
1562 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001563 if (DEBUG_STYLUS_FUSION) {
1564 ALOGD("Timeout expired, synthesizing event with new stylus data");
1565 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001566 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1567 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001568 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1569 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1570 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1571 }
1572 }
1573}
1574
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001575void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001576 // Always start with a clean state.
1577 mCurrentCookedState.clear();
1578
1579 // Apply stylus buttons to current raw state.
1580 applyExternalStylusButtonState(when);
1581
1582 // Handle policy on initial down or hover events.
1583 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1584 mCurrentRawState.rawPointerData.pointerCount != 0;
1585
1586 uint32_t policyFlags = 0;
1587 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1588 if (initialDown || buttonsPressed) {
1589 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001590 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001591 getContext()->fadePointer();
1592 }
1593
1594 if (mParameters.wake) {
1595 policyFlags |= POLICY_FLAG_WAKE;
1596 }
1597 }
1598
1599 // Consume raw off-screen touches before cooking pointer data.
1600 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001601 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001602 mCurrentRawState.rawPointerData.clear();
1603 }
1604
1605 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1606 // with cooked pointer data that has the same ids and indices as the raw data.
1607 // The following code can use either the raw or cooked data, as needed.
1608 cookPointerData();
1609
1610 // Apply stylus pressure to current cooked state.
1611 applyExternalStylusTouchState(when);
1612
1613 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001614 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1615 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001616 mCurrentCookedState.buttonState);
1617
1618 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001619 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001620 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1621 uint32_t id = idBits.clearFirstMarkedBit();
1622 const RawPointerData::Pointer& pointer =
1623 mCurrentRawState.rawPointerData.pointerForId(id);
1624 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1625 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1626 mCurrentCookedState.stylusIdBits.markBit(id);
1627 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1628 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1629 mCurrentCookedState.fingerIdBits.markBit(id);
1630 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1631 mCurrentCookedState.mouseIdBits.markBit(id);
1632 }
1633 }
1634 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1635 uint32_t id = idBits.clearFirstMarkedBit();
1636 const RawPointerData::Pointer& pointer =
1637 mCurrentRawState.rawPointerData.pointerForId(id);
1638 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1639 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1640 mCurrentCookedState.stylusIdBits.markBit(id);
1641 }
1642 }
1643
1644 // Stylus takes precedence over all tools, then mouse, then finger.
1645 PointerUsage pointerUsage = mPointerUsage;
1646 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1647 mCurrentCookedState.mouseIdBits.clear();
1648 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001649 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001650 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1651 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001652 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001653 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1654 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001655 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656 }
1657
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001658 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001659 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001660 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001661 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001662 dispatchButtonRelease(when, readTime, policyFlags);
1663 dispatchHoverExit(when, readTime, policyFlags);
1664 dispatchTouches(when, readTime, policyFlags);
1665 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1666 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001667 }
1668
1669 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1670 mCurrentMotionAborted = false;
1671 }
1672 }
1673
1674 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001675 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001676 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1677 mCurrentCookedState.buttonState);
1678
1679 // Clear some transient state.
1680 mCurrentRawState.rawVScroll = 0;
1681 mCurrentRawState.rawHScroll = 0;
1682
1683 // Copy current touch to last touch in preparation for the next cycle.
1684 mLastRawState.copyFrom(mCurrentRawState);
1685 mLastCookedState.copyFrom(mCurrentCookedState);
1686}
1687
Garfield Tanc734e4f2021-01-15 20:01:39 -08001688void TouchInputMapper::updateTouchSpots() {
1689 if (!mConfig.showTouches || mPointerController == nullptr) {
1690 return;
1691 }
1692
1693 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1694 // clear touch spots.
1695 if (mDeviceMode != DeviceMode::DIRECT &&
1696 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1697 return;
1698 }
1699
1700 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1701 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1702
1703 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001704 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1705 mCurrentCookedState.cookedPointerData.idToIndex,
1706 mCurrentCookedState.cookedPointerData.touchingIdBits,
1707 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001708}
1709
1710bool TouchInputMapper::isTouchScreen() {
1711 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1712 mParameters.hasAssociatedDisplay;
1713}
1714
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001715void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001716 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001717 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1718 }
1719}
1720
1721void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1722 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1723 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1724
1725 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1726 float pressure = mExternalStylusState.pressure;
1727 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1728 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1729 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1730 }
1731 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1732 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1733
1734 PointerProperties& properties =
1735 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1736 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1737 properties.toolType = mExternalStylusState.toolType;
1738 }
1739 }
1740}
1741
1742bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001743 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001744 return false;
1745 }
1746
1747 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1748 state.rawPointerData.pointerCount != 0;
1749 if (initialDown) {
1750 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001751 if (DEBUG_STYLUS_FUSION) {
1752 ALOGD("Have both stylus and touch data, beginning fusion");
1753 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001754 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1755 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001756 if (DEBUG_STYLUS_FUSION) {
1757 ALOGD("Timeout expired, assuming touch is not a stylus.");
1758 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001759 resetExternalStylus();
1760 } else {
1761 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1762 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1763 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001764 if (DEBUG_STYLUS_FUSION) {
1765 ALOGD("No stylus data but stylus is connected, requesting timeout "
1766 "(%" PRId64 "ms)",
1767 mExternalStylusFusionTimeout);
1768 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001769 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1770 return true;
1771 }
1772 }
1773
1774 // Check if the stylus pointer has gone up.
1775 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001776 if (DEBUG_STYLUS_FUSION) {
1777 ALOGD("Stylus pointer is going up");
1778 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001779 mExternalStylusId = -1;
1780 }
1781
1782 return false;
1783}
1784
1785void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001786 if (mDeviceMode == DeviceMode::POINTER) {
1787 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001788 // Since this is a synthetic event, we can consider its latency to be zero
1789 const nsecs_t readTime = when;
1790 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001791 }
Michael Wright227c5542020-07-02 18:30:52 +01001792 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001793 if (mExternalStylusFusionTimeout < when) {
1794 processRawTouches(true /*timeout*/);
1795 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1796 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1797 }
1798 }
1799}
1800
1801void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1802 mExternalStylusState.copyFrom(state);
1803 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1804 // We're either in the middle of a fused stream of data or we're waiting on data before
1805 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1806 // data.
1807 mExternalStylusDataPending = true;
1808 processRawTouches(false /*timeout*/);
1809 }
1810}
1811
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001812bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001813 // Check for release of a virtual key.
1814 if (mCurrentVirtualKey.down) {
1815 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1816 // Pointer went up while virtual key was down.
1817 mCurrentVirtualKey.down = false;
1818 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001819 if (DEBUG_VIRTUAL_KEYS) {
1820 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1821 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1822 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001823 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001824 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1825 }
1826 return true;
1827 }
1828
1829 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1830 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1831 const RawPointerData::Pointer& pointer =
1832 mCurrentRawState.rawPointerData.pointerForId(id);
1833 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1834 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1835 // Pointer is still within the space of the virtual key.
1836 return true;
1837 }
1838 }
1839
1840 // Pointer left virtual key area or another pointer also went down.
1841 // Send key cancellation but do not consume the touch yet.
1842 // This is useful when the user swipes through from the virtual key area
1843 // into the main display surface.
1844 mCurrentVirtualKey.down = false;
1845 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001846 if (DEBUG_VIRTUAL_KEYS) {
1847 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1848 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1849 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001850 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001851 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1852 AKEY_EVENT_FLAG_CANCELED);
1853 }
1854 }
1855
1856 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1857 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1858 // Pointer just went down. Check for virtual key press or off-screen touches.
1859 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1860 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001861 // Skip checking whether the pointer is inside the physical frame if the device is in
1862 // unscaled mode.
1863 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1864 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001865 // If exactly one pointer went down, check for virtual key hit.
1866 // Otherwise we will drop the entire stroke.
1867 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1868 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1869 if (virtualKey) {
1870 mCurrentVirtualKey.down = true;
1871 mCurrentVirtualKey.downTime = when;
1872 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1873 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1874 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001875 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1876 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001877
1878 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001879 if (DEBUG_VIRTUAL_KEYS) {
1880 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1881 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1882 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001883 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001884 AKEY_EVENT_FLAG_FROM_SYSTEM |
1885 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1886 }
1887 }
1888 }
1889 return true;
1890 }
1891 }
1892
1893 // Disable all virtual key touches that happen within a short time interval of the
1894 // most recent touch within the screen area. The idea is to filter out stray
1895 // virtual key presses when interacting with the touch screen.
1896 //
1897 // Problems we're trying to solve:
1898 //
1899 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1900 // virtual key area that is implemented by a separate touch panel and accidentally
1901 // triggers a virtual key.
1902 //
1903 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1904 // area and accidentally triggers a virtual key. This often happens when virtual keys
1905 // are layed out below the screen near to where the on screen keyboard's space bar
1906 // is displayed.
1907 if (mConfig.virtualKeyQuietTime > 0 &&
1908 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001909 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001910 }
1911 return false;
1912}
1913
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001914void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001915 int32_t keyEventAction, int32_t keyEventFlags) {
1916 int32_t keyCode = mCurrentVirtualKey.keyCode;
1917 int32_t scanCode = mCurrentVirtualKey.scanCode;
1918 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001919 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001920 policyFlags |= POLICY_FLAG_VIRTUAL;
1921
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1923 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1924 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001925 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001926}
1927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001928void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001929 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1930 if (!currentIdBits.isEmpty()) {
1931 int32_t metaState = getContext()->getGlobalMetaState();
1932 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001933 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1934 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001935 mCurrentCookedState.cookedPointerData.pointerProperties,
1936 mCurrentCookedState.cookedPointerData.pointerCoords,
1937 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1938 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1939 mCurrentMotionAborted = true;
1940 }
1941}
1942
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001943void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001944 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1945 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1946 int32_t metaState = getContext()->getGlobalMetaState();
1947 int32_t buttonState = mCurrentCookedState.buttonState;
1948
1949 if (currentIdBits == lastIdBits) {
1950 if (!currentIdBits.isEmpty()) {
1951 // No pointer id changes so this is a move event.
1952 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001953 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1954 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001955 mCurrentCookedState.cookedPointerData.pointerProperties,
1956 mCurrentCookedState.cookedPointerData.pointerCoords,
1957 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1958 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1959 }
1960 } else {
1961 // There may be pointers going up and pointers going down and pointers moving
1962 // all at the same time.
1963 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1964 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1965 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1966 BitSet32 dispatchedIdBits(lastIdBits.value);
1967
1968 // Update last coordinates of pointers that have moved so that we observe the new
1969 // pointer positions at the same time as other pointers that have just gone up.
1970 bool moveNeeded =
1971 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1972 mCurrentCookedState.cookedPointerData.pointerCoords,
1973 mCurrentCookedState.cookedPointerData.idToIndex,
1974 mLastCookedState.cookedPointerData.pointerProperties,
1975 mLastCookedState.cookedPointerData.pointerCoords,
1976 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1977 if (buttonState != mLastCookedState.buttonState) {
1978 moveNeeded = true;
1979 }
1980
1981 // Dispatch pointer up events.
1982 while (!upIdBits.isEmpty()) {
1983 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001984 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001985 if (isCanceled) {
1986 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1987 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001988 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001989 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001990 mLastCookedState.cookedPointerData.pointerProperties,
1991 mLastCookedState.cookedPointerData.pointerCoords,
1992 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
1993 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1994 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08001995 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001996 }
1997
1998 // Dispatch move events if any of the remaining pointers moved from their old locations.
1999 // Although applications receive new locations as part of individual pointer up
2000 // events, they do not generally handle them except when presented in a move event.
2001 if (moveNeeded && !moveIdBits.isEmpty()) {
2002 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002003 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2004 metaState, buttonState, 0,
2005 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002006 mCurrentCookedState.cookedPointerData.pointerCoords,
2007 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2008 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2009 }
2010
2011 // Dispatch pointer down events using the new pointer locations.
2012 while (!downIdBits.isEmpty()) {
2013 uint32_t downId = downIdBits.clearFirstMarkedBit();
2014 dispatchedIdBits.markBit(downId);
2015
2016 if (dispatchedIdBits.count() == 1) {
2017 // First pointer is going down. Set down time.
2018 mDownTime = when;
2019 }
2020
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002021 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2022 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002023 mCurrentCookedState.cookedPointerData.pointerProperties,
2024 mCurrentCookedState.cookedPointerData.pointerCoords,
2025 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2026 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2027 }
2028 }
2029}
2030
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002031void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002032 if (mSentHoverEnter &&
2033 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2034 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2035 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002036 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2037 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002038 mLastCookedState.cookedPointerData.pointerProperties,
2039 mLastCookedState.cookedPointerData.pointerCoords,
2040 mLastCookedState.cookedPointerData.idToIndex,
2041 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2042 mOrientedYPrecision, mDownTime);
2043 mSentHoverEnter = false;
2044 }
2045}
2046
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002047void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2048 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002049 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2050 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2051 int32_t metaState = getContext()->getGlobalMetaState();
2052 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002053 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2054 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002055 mCurrentCookedState.cookedPointerData.pointerProperties,
2056 mCurrentCookedState.cookedPointerData.pointerCoords,
2057 mCurrentCookedState.cookedPointerData.idToIndex,
2058 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2059 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2060 mSentHoverEnter = true;
2061 }
2062
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002063 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2064 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002065 mCurrentCookedState.cookedPointerData.pointerProperties,
2066 mCurrentCookedState.cookedPointerData.pointerCoords,
2067 mCurrentCookedState.cookedPointerData.idToIndex,
2068 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2069 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2070 }
2071}
2072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002073void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002074 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2075 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2076 const int32_t metaState = getContext()->getGlobalMetaState();
2077 int32_t buttonState = mLastCookedState.buttonState;
2078 while (!releasedButtons.isEmpty()) {
2079 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2080 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002081 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002082 actionButton, 0, metaState, buttonState, 0,
2083 mCurrentCookedState.cookedPointerData.pointerProperties,
2084 mCurrentCookedState.cookedPointerData.pointerCoords,
2085 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2086 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2087 }
2088}
2089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002090void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002091 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2092 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2093 const int32_t metaState = getContext()->getGlobalMetaState();
2094 int32_t buttonState = mLastCookedState.buttonState;
2095 while (!pressedButtons.isEmpty()) {
2096 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2097 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002098 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2099 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002100 mCurrentCookedState.cookedPointerData.pointerProperties,
2101 mCurrentCookedState.cookedPointerData.pointerCoords,
2102 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2103 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2104 }
2105}
2106
2107const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2108 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2109 return cookedPointerData.touchingIdBits;
2110 }
2111 return cookedPointerData.hoveringIdBits;
2112}
2113
2114void TouchInputMapper::cookPointerData() {
2115 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2116
2117 mCurrentCookedState.cookedPointerData.clear();
2118 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2119 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2120 mCurrentRawState.rawPointerData.hoveringIdBits;
2121 mCurrentCookedState.cookedPointerData.touchingIdBits =
2122 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002123 mCurrentCookedState.cookedPointerData.canceledIdBits =
2124 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002125
2126 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2127 mCurrentCookedState.buttonState = 0;
2128 } else {
2129 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2130 }
2131
2132 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002133 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002134 for (uint32_t i = 0; i < currentPointerCount; i++) {
2135 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2136
2137 // Size
2138 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2139 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002140 case Calibration::SizeCalibration::GEOMETRIC:
2141 case Calibration::SizeCalibration::DIAMETER:
2142 case Calibration::SizeCalibration::BOX:
2143 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002144 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2145 touchMajor = in.touchMajor;
2146 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2147 toolMajor = in.toolMajor;
2148 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2149 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2150 : in.touchMajor;
2151 } else if (mRawPointerAxes.touchMajor.valid) {
2152 toolMajor = touchMajor = in.touchMajor;
2153 toolMinor = touchMinor =
2154 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2155 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2156 : in.touchMajor;
2157 } else if (mRawPointerAxes.toolMajor.valid) {
2158 touchMajor = toolMajor = in.toolMajor;
2159 touchMinor = toolMinor =
2160 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2161 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2162 : in.toolMajor;
2163 } else {
2164 ALOG_ASSERT(false,
2165 "No touch or tool axes. "
2166 "Size calibration should have been resolved to NONE.");
2167 touchMajor = 0;
2168 touchMinor = 0;
2169 toolMajor = 0;
2170 toolMinor = 0;
2171 size = 0;
2172 }
2173
2174 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2175 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2176 if (touchingCount > 1) {
2177 touchMajor /= touchingCount;
2178 touchMinor /= touchingCount;
2179 toolMajor /= touchingCount;
2180 toolMinor /= touchingCount;
2181 size /= touchingCount;
2182 }
2183 }
2184
Michael Wright227c5542020-07-02 18:30:52 +01002185 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002186 touchMajor *= mGeometricScale;
2187 touchMinor *= mGeometricScale;
2188 toolMajor *= mGeometricScale;
2189 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002190 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002191 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2192 touchMinor = touchMajor;
2193 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2194 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002195 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002196 touchMinor = touchMajor;
2197 toolMinor = toolMajor;
2198 }
2199
2200 mCalibration.applySizeScaleAndBias(&touchMajor);
2201 mCalibration.applySizeScaleAndBias(&touchMinor);
2202 mCalibration.applySizeScaleAndBias(&toolMajor);
2203 mCalibration.applySizeScaleAndBias(&toolMinor);
2204 size *= mSizeScale;
2205 break;
2206 default:
2207 touchMajor = 0;
2208 touchMinor = 0;
2209 toolMajor = 0;
2210 toolMinor = 0;
2211 size = 0;
2212 break;
2213 }
2214
2215 // Pressure
2216 float pressure;
2217 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002218 case Calibration::PressureCalibration::PHYSICAL:
2219 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002220 pressure = in.pressure * mPressureScale;
2221 break;
2222 default:
2223 pressure = in.isHovering ? 0 : 1;
2224 break;
2225 }
2226
2227 // Tilt and Orientation
2228 float tilt;
2229 float orientation;
2230 if (mHaveTilt) {
2231 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2232 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2233 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2234 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2235 } else {
2236 tilt = 0;
2237
2238 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002239 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002240 orientation = in.orientation * mOrientationScale;
2241 break;
Michael Wright227c5542020-07-02 18:30:52 +01002242 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002243 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2244 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2245 if (c1 != 0 || c2 != 0) {
2246 orientation = atan2f(c1, c2) * 0.5f;
2247 float confidence = hypotf(c1, c2);
2248 float scale = 1.0f + confidence / 16.0f;
2249 touchMajor *= scale;
2250 touchMinor /= scale;
2251 toolMajor *= scale;
2252 toolMinor /= scale;
2253 } else {
2254 orientation = 0;
2255 }
2256 break;
2257 }
2258 default:
2259 orientation = 0;
2260 }
2261 }
2262
2263 // Distance
2264 float distance;
2265 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002266 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002267 distance = in.distance * mDistanceScale;
2268 break;
2269 default:
2270 distance = 0;
2271 }
2272
2273 // Coverage
2274 int32_t rawLeft, rawTop, rawRight, rawBottom;
2275 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002276 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002277 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2278 rawRight = in.toolMinor & 0x0000ffff;
2279 rawBottom = in.toolMajor & 0x0000ffff;
2280 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2281 break;
2282 default:
2283 rawLeft = rawTop = rawRight = rawBottom = 0;
2284 break;
2285 }
2286
2287 // Adjust X,Y coords for device calibration
2288 // TODO: Adjust coverage coords?
2289 float xTransformed = in.x, yTransformed = in.y;
2290 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002291 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002292
Prabir Pradhan1728b212021-10-19 16:00:03 -07002293 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002294 float left, top, right, bottom;
2295
Prabir Pradhan1728b212021-10-19 16:00:03 -07002296 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002297 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002298 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2299 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2300 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2301 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002302 orientation -= M_PI_2;
2303 if (mOrientedRanges.haveOrientation &&
2304 orientation < mOrientedRanges.orientation.min) {
2305 orientation +=
2306 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2307 }
2308 break;
2309 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002310 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2311 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002312 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2313 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002314 orientation -= M_PI;
2315 if (mOrientedRanges.haveOrientation &&
2316 orientation < mOrientedRanges.orientation.min) {
2317 orientation +=
2318 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2319 }
2320 break;
2321 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002322 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2323 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002324 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2325 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002326 orientation += M_PI_2;
2327 if (mOrientedRanges.haveOrientation &&
2328 orientation > mOrientedRanges.orientation.max) {
2329 orientation -=
2330 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2331 }
2332 break;
2333 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002334 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2335 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2336 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2337 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002338 break;
2339 }
2340
2341 // Write output coords.
2342 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2343 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002344 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2345 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002346 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2347 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2348 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2349 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2350 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2351 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2352 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002353 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002354 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2355 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2356 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2357 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2358 } else {
2359 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2360 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2361 }
2362
Chris Ye364fdb52020-08-05 15:07:56 -07002363 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002364 uint32_t id = in.id;
2365 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2366 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2367 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2368 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2369 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2370 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2371 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2372 }
2373
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002374 // Write output properties.
2375 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002376 properties.clear();
2377 properties.id = id;
2378 properties.toolType = in.toolType;
2379
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002380 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002381 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002382 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002383 }
2384}
2385
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002386void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002387 PointerUsage pointerUsage) {
2388 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002389 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002390 mPointerUsage = pointerUsage;
2391 }
2392
2393 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002394 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002395 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002396 break;
Michael Wright227c5542020-07-02 18:30:52 +01002397 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002398 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002399 break;
Michael Wright227c5542020-07-02 18:30:52 +01002400 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002401 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002402 break;
Michael Wright227c5542020-07-02 18:30:52 +01002403 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002404 break;
2405 }
2406}
2407
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002408void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002409 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002410 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002411 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002412 break;
Michael Wright227c5542020-07-02 18:30:52 +01002413 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002414 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002415 break;
Michael Wright227c5542020-07-02 18:30:52 +01002416 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002417 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002418 break;
Michael Wright227c5542020-07-02 18:30:52 +01002419 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002420 break;
2421 }
2422
Michael Wright227c5542020-07-02 18:30:52 +01002423 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002424}
2425
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002426void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2427 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002428 // Update current gesture coordinates.
2429 bool cancelPreviousGesture, finishPreviousGesture;
2430 bool sendEvents =
2431 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2432 if (!sendEvents) {
2433 return;
2434 }
2435 if (finishPreviousGesture) {
2436 cancelPreviousGesture = false;
2437 }
2438
2439 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002440 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002441 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002442 if (finishPreviousGesture || cancelPreviousGesture) {
2443 mPointerController->clearSpots();
2444 }
2445
Michael Wright227c5542020-07-02 18:30:52 +01002446 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002447 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2448 mPointerGesture.currentGestureIdToIndex,
2449 mPointerGesture.currentGestureIdBits,
2450 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002451 }
2452 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002453 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002454 }
2455
2456 // Show or hide the pointer if needed.
2457 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002458 case PointerGesture::Mode::NEUTRAL:
2459 case PointerGesture::Mode::QUIET:
2460 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2461 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002462 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002463 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002464 }
2465 break;
Michael Wright227c5542020-07-02 18:30:52 +01002466 case PointerGesture::Mode::TAP:
2467 case PointerGesture::Mode::TAP_DRAG:
2468 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2469 case PointerGesture::Mode::HOVER:
2470 case PointerGesture::Mode::PRESS:
2471 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002472 // Unfade the pointer when the current gesture manipulates the
2473 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002474 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002475 break;
Michael Wright227c5542020-07-02 18:30:52 +01002476 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002477 // Fade the pointer when the current gesture manipulates a different
2478 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002479 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002480 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002481 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002482 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002483 }
2484 break;
2485 }
2486
2487 // Send events!
2488 int32_t metaState = getContext()->getGlobalMetaState();
2489 int32_t buttonState = mCurrentCookedState.buttonState;
2490
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002491 uint32_t flags = 0;
2492
2493 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2494 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2495 }
2496
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002497 // Update last coordinates of pointers that have moved so that we observe the new
2498 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002499 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2500 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2501 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2502 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2503 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2504 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002505 bool moveNeeded = false;
2506 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2507 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2508 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2509 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2510 mPointerGesture.lastGestureIdBits.value);
2511 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2512 mPointerGesture.currentGestureCoords,
2513 mPointerGesture.currentGestureIdToIndex,
2514 mPointerGesture.lastGestureProperties,
2515 mPointerGesture.lastGestureCoords,
2516 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2517 if (buttonState != mLastCookedState.buttonState) {
2518 moveNeeded = true;
2519 }
2520 }
2521
2522 // Send motion events for all pointers that went up or were canceled.
2523 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2524 if (!dispatchedGestureIdBits.isEmpty()) {
2525 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002526 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2527 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002528 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2529 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2530 mPointerGesture.downTime);
2531
2532 dispatchedGestureIdBits.clear();
2533 } else {
2534 BitSet32 upGestureIdBits;
2535 if (finishPreviousGesture) {
2536 upGestureIdBits = dispatchedGestureIdBits;
2537 } else {
2538 upGestureIdBits.value =
2539 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2540 }
2541 while (!upGestureIdBits.isEmpty()) {
2542 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2543
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002544 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002545 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002546 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002547 mPointerGesture.lastGestureCoords,
2548 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2549 0, mPointerGesture.downTime);
2550
2551 dispatchedGestureIdBits.clearBit(id);
2552 }
2553 }
2554 }
2555
2556 // Send motion events for all pointers that moved.
2557 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002558 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002559 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002560 mPointerGesture.currentGestureProperties,
2561 mPointerGesture.currentGestureCoords,
2562 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2563 mPointerGesture.downTime);
2564 }
2565
2566 // Send motion events for all pointers that went down.
2567 if (down) {
2568 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2569 ~dispatchedGestureIdBits.value);
2570 while (!downGestureIdBits.isEmpty()) {
2571 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2572 dispatchedGestureIdBits.markBit(id);
2573
2574 if (dispatchedGestureIdBits.count() == 1) {
2575 mPointerGesture.downTime = when;
2576 }
2577
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002578 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002579 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002580 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002581 mPointerGesture.currentGestureCoords,
2582 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2583 0, mPointerGesture.downTime);
2584 }
2585 }
2586
2587 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002588 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002589 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2590 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002591 mPointerGesture.currentGestureProperties,
2592 mPointerGesture.currentGestureCoords,
2593 mPointerGesture.currentGestureIdToIndex,
2594 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2595 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2596 // Synthesize a hover move event after all pointers go up to indicate that
2597 // the pointer is hovering again even if the user is not currently touching
2598 // the touch pad. This ensures that a view will receive a fresh hover enter
2599 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002600 float x, y;
2601 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002602
2603 PointerProperties pointerProperties;
2604 pointerProperties.clear();
2605 pointerProperties.id = 0;
2606 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2607
2608 PointerCoords pointerCoords;
2609 pointerCoords.clear();
2610 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2611 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2612
2613 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002614 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002615 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002616 metaState, buttonState, MotionClassification::NONE,
2617 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2618 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002619 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002620 }
2621
2622 // Update state.
2623 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2624 if (!down) {
2625 mPointerGesture.lastGestureIdBits.clear();
2626 } else {
2627 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2628 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2629 uint32_t id = idBits.clearFirstMarkedBit();
2630 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2631 mPointerGesture.lastGestureProperties[index].copyFrom(
2632 mPointerGesture.currentGestureProperties[index]);
2633 mPointerGesture.lastGestureCoords[index].copyFrom(
2634 mPointerGesture.currentGestureCoords[index]);
2635 mPointerGesture.lastGestureIdToIndex[id] = index;
2636 }
2637 }
2638}
2639
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002640void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002641 // Cancel previously dispatches pointers.
2642 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2643 int32_t metaState = getContext()->getGlobalMetaState();
2644 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002645 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2646 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002647 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2648 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2649 0, 0, mPointerGesture.downTime);
2650 }
2651
2652 // Reset the current pointer gesture.
2653 mPointerGesture.reset();
2654 mPointerVelocityControl.reset();
2655
2656 // Remove any current spots.
2657 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002658 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002659 mPointerController->clearSpots();
2660 }
2661}
2662
2663bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2664 bool* outFinishPreviousGesture, bool isTimeout) {
2665 *outCancelPreviousGesture = false;
2666 *outFinishPreviousGesture = false;
2667
2668 // Handle TAP timeout.
2669 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002670 if (DEBUG_GESTURES) {
2671 ALOGD("Gestures: Processing timeout");
2672 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002673
Michael Wright227c5542020-07-02 18:30:52 +01002674 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002675 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2676 // The tap/drag timeout has not yet expired.
2677 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2678 mConfig.pointerGestureTapDragInterval);
2679 } else {
2680 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002681 if (DEBUG_GESTURES) {
2682 ALOGD("Gestures: TAP finished");
2683 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002684 *outFinishPreviousGesture = true;
2685
2686 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002687 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002688 mPointerGesture.currentGestureIdBits.clear();
2689
2690 mPointerVelocityControl.reset();
2691 return true;
2692 }
2693 }
2694
2695 // We did not handle this timeout.
2696 return false;
2697 }
2698
2699 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2700 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2701
2702 // Update the velocity tracker.
2703 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002704 std::vector<VelocityTracker::Position> positions;
2705 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002706 uint32_t id = idBits.clearFirstMarkedBit();
2707 const RawPointerData::Pointer& pointer =
2708 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002709 float x = pointer.x * mPointerXMovementScale;
2710 float y = pointer.y * mPointerYMovementScale;
2711 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002712 }
2713 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2714 positions);
2715 }
2716
2717 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2718 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002719 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2720 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2721 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002722 mPointerGesture.resetTap();
2723 }
2724
2725 // Pick a new active touch id if needed.
2726 // Choose an arbitrary pointer that just went down, if there is one.
2727 // Otherwise choose an arbitrary remaining pointer.
2728 // This guarantees we always have an active touch id when there is at least one pointer.
2729 // We keep the same active touch id for as long as possible.
2730 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2731 int32_t activeTouchId = lastActiveTouchId;
2732 if (activeTouchId < 0) {
2733 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2734 activeTouchId = mPointerGesture.activeTouchId =
2735 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2736 mPointerGesture.firstTouchTime = when;
2737 }
2738 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2739 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2740 activeTouchId = mPointerGesture.activeTouchId =
2741 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2742 } else {
2743 activeTouchId = mPointerGesture.activeTouchId = -1;
2744 }
2745 }
2746
2747 // Determine whether we are in quiet time.
2748 bool isQuietTime = false;
2749 if (activeTouchId < 0) {
2750 mPointerGesture.resetQuietTime();
2751 } else {
2752 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2753 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002754 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2755 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2756 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002757 currentFingerCount < 2) {
2758 // Enter quiet time when exiting swipe or freeform state.
2759 // This is to prevent accidentally entering the hover state and flinging the
2760 // pointer when finishing a swipe and there is still one pointer left onscreen.
2761 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002762 } else if (mPointerGesture.lastGestureMode ==
2763 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002764 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2765 // Enter quiet time when releasing the button and there are still two or more
2766 // fingers down. This may indicate that one finger was used to press the button
2767 // but it has not gone up yet.
2768 isQuietTime = true;
2769 }
2770 if (isQuietTime) {
2771 mPointerGesture.quietTime = when;
2772 }
2773 }
2774 }
2775
2776 // Switch states based on button and pointer state.
2777 if (isQuietTime) {
2778 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002779 if (DEBUG_GESTURES) {
2780 ALOGD("Gestures: QUIET for next %0.3fms",
2781 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2782 0.000001f);
2783 }
Michael Wright227c5542020-07-02 18:30:52 +01002784 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002785 *outFinishPreviousGesture = true;
2786 }
2787
2788 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002789 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002790 mPointerGesture.currentGestureIdBits.clear();
2791
2792 mPointerVelocityControl.reset();
2793 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2794 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2795 // The pointer follows the active touch point.
2796 // Emit DOWN, MOVE, UP events at the pointer location.
2797 //
2798 // Only the active touch matters; other fingers are ignored. This policy helps
2799 // to handle the case where the user places a second finger on the touch pad
2800 // to apply the necessary force to depress an integrated button below the surface.
2801 // We don't want the second finger to be delivered to applications.
2802 //
2803 // For this to work well, we need to make sure to track the pointer that is really
2804 // active. If the user first puts one finger down to click then adds another
2805 // finger to drag then the active pointer should switch to the finger that is
2806 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002807 if (DEBUG_GESTURES) {
2808 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2809 "currentFingerCount=%d",
2810 activeTouchId, currentFingerCount);
2811 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002812 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002813 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002814 *outFinishPreviousGesture = true;
2815 mPointerGesture.activeGestureId = 0;
2816 }
2817
2818 // Switch pointers if needed.
2819 // Find the fastest pointer and follow it.
2820 if (activeTouchId >= 0 && currentFingerCount > 1) {
2821 int32_t bestId = -1;
2822 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2823 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2824 uint32_t id = idBits.clearFirstMarkedBit();
2825 float vx, vy;
2826 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2827 float speed = hypotf(vx, vy);
2828 if (speed > bestSpeed) {
2829 bestId = id;
2830 bestSpeed = speed;
2831 }
2832 }
2833 }
2834 if (bestId >= 0 && bestId != activeTouchId) {
2835 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002836 if (DEBUG_GESTURES) {
2837 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2838 "bestId=%d, bestSpeed=%0.3f",
2839 bestId, bestSpeed);
2840 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002841 }
2842 }
2843
2844 float deltaX = 0, deltaY = 0;
2845 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2846 const RawPointerData::Pointer& currentPointer =
2847 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2848 const RawPointerData::Pointer& lastPointer =
2849 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2850 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2851 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2852
Prabir Pradhan1728b212021-10-19 16:00:03 -07002853 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002854 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2855
2856 // Move the pointer using a relative motion.
2857 // When using spots, the click will occur at the position of the anchor
2858 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002859 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002860 } else {
2861 mPointerVelocityControl.reset();
2862 }
2863
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002864 float x, y;
2865 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002866
Michael Wright227c5542020-07-02 18:30:52 +01002867 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002868 mPointerGesture.currentGestureIdBits.clear();
2869 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2870 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2871 mPointerGesture.currentGestureProperties[0].clear();
2872 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2873 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2874 mPointerGesture.currentGestureCoords[0].clear();
2875 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2876 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2877 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2878 } else if (currentFingerCount == 0) {
2879 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002880 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002881 *outFinishPreviousGesture = true;
2882 }
2883
2884 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2885 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2886 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002887 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2888 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002889 lastFingerCount == 1) {
2890 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002891 float x, y;
2892 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002893 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2894 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002895 if (DEBUG_GESTURES) {
2896 ALOGD("Gestures: TAP");
2897 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002898
2899 mPointerGesture.tapUpTime = when;
2900 getContext()->requestTimeoutAtTime(when +
2901 mConfig.pointerGestureTapDragInterval);
2902
2903 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002904 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002905 mPointerGesture.currentGestureIdBits.clear();
2906 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2907 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2908 mPointerGesture.currentGestureProperties[0].clear();
2909 mPointerGesture.currentGestureProperties[0].id =
2910 mPointerGesture.activeGestureId;
2911 mPointerGesture.currentGestureProperties[0].toolType =
2912 AMOTION_EVENT_TOOL_TYPE_FINGER;
2913 mPointerGesture.currentGestureCoords[0].clear();
2914 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2915 mPointerGesture.tapX);
2916 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2917 mPointerGesture.tapY);
2918 mPointerGesture.currentGestureCoords[0]
2919 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2920
2921 tapped = true;
2922 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002923 if (DEBUG_GESTURES) {
2924 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2925 y - mPointerGesture.tapY);
2926 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002927 }
2928 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002929 if (DEBUG_GESTURES) {
2930 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2931 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2932 (when - mPointerGesture.tapDownTime) * 0.000001f);
2933 } else {
2934 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2935 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002936 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002937 }
2938 }
2939
2940 mPointerVelocityControl.reset();
2941
2942 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002943 if (DEBUG_GESTURES) {
2944 ALOGD("Gestures: NEUTRAL");
2945 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002946 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002947 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002948 mPointerGesture.currentGestureIdBits.clear();
2949 }
2950 } else if (currentFingerCount == 1) {
2951 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2952 // The pointer follows the active touch point.
2953 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2954 // When in TAP_DRAG, emit MOVE events at the pointer location.
2955 ALOG_ASSERT(activeTouchId >= 0);
2956
Michael Wright227c5542020-07-02 18:30:52 +01002957 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2958 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002959 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002960 float x, y;
2961 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002962 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2963 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002964 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002965 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002966 if (DEBUG_GESTURES) {
2967 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2968 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
2969 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002970 }
2971 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002972 if (DEBUG_GESTURES) {
2973 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2974 (when - mPointerGesture.tapUpTime) * 0.000001f);
2975 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002976 }
Michael Wright227c5542020-07-02 18:30:52 +01002977 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2978 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002979 }
2980
2981 float deltaX = 0, deltaY = 0;
2982 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2983 const RawPointerData::Pointer& currentPointer =
2984 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2985 const RawPointerData::Pointer& lastPointer =
2986 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2987 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2988 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2989
Prabir Pradhan1728b212021-10-19 16:00:03 -07002990 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002991 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2992
2993 // Move the pointer using a relative motion.
2994 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002995 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002996 } else {
2997 mPointerVelocityControl.reset();
2998 }
2999
3000 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003001 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003002 if (DEBUG_GESTURES) {
3003 ALOGD("Gestures: TAP_DRAG");
3004 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003005 down = true;
3006 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003007 if (DEBUG_GESTURES) {
3008 ALOGD("Gestures: HOVER");
3009 }
Michael Wright227c5542020-07-02 18:30:52 +01003010 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003011 *outFinishPreviousGesture = true;
3012 }
3013 mPointerGesture.activeGestureId = 0;
3014 down = false;
3015 }
3016
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003017 float x, y;
3018 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003019
3020 mPointerGesture.currentGestureIdBits.clear();
3021 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3022 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3023 mPointerGesture.currentGestureProperties[0].clear();
3024 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3025 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3026 mPointerGesture.currentGestureCoords[0].clear();
3027 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3028 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3029 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3030 down ? 1.0f : 0.0f);
3031
3032 if (lastFingerCount == 0 && currentFingerCount != 0) {
3033 mPointerGesture.resetTap();
3034 mPointerGesture.tapDownTime = when;
3035 mPointerGesture.tapX = x;
3036 mPointerGesture.tapY = y;
3037 }
3038 } else {
3039 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3040 // We need to provide feedback for each finger that goes down so we cannot wait
3041 // for the fingers to move before deciding what to do.
3042 //
3043 // The ambiguous case is deciding what to do when there are two fingers down but they
3044 // have not moved enough to determine whether they are part of a drag or part of a
3045 // freeform gesture, or just a press or long-press at the pointer location.
3046 //
3047 // When there are two fingers we start with the PRESS hypothesis and we generate a
3048 // down at the pointer location.
3049 //
3050 // When the two fingers move enough or when additional fingers are added, we make
3051 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3052 ALOG_ASSERT(activeTouchId >= 0);
3053
3054 bool settled = when >=
3055 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003056 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3057 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3058 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003059 *outFinishPreviousGesture = true;
3060 } else if (!settled && currentFingerCount > lastFingerCount) {
3061 // Additional pointers have gone down but not yet settled.
3062 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003063 if (DEBUG_GESTURES) {
3064 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3065 "MULTITOUCH, settle time remaining %0.3fms",
3066 (mPointerGesture.firstTouchTime +
3067 mConfig.pointerGestureMultitouchSettleInterval - when) *
3068 0.000001f);
3069 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003070 *outCancelPreviousGesture = true;
3071 } else {
3072 // Continue previous gesture.
3073 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3074 }
3075
3076 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003077 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003078 mPointerGesture.activeGestureId = 0;
3079 mPointerGesture.referenceIdBits.clear();
3080 mPointerVelocityControl.reset();
3081
3082 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003083 if (DEBUG_GESTURES) {
3084 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3085 "settle time remaining %0.3fms",
3086 (mPointerGesture.firstTouchTime +
3087 mConfig.pointerGestureMultitouchSettleInterval - when) *
3088 0.000001f);
3089 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003090 mCurrentRawState.rawPointerData
3091 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3092 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003093 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3094 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003095 }
3096
3097 // Clear the reference deltas for fingers not yet included in the reference calculation.
3098 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3099 ~mPointerGesture.referenceIdBits.value);
3100 !idBits.isEmpty();) {
3101 uint32_t id = idBits.clearFirstMarkedBit();
3102 mPointerGesture.referenceDeltas[id].dx = 0;
3103 mPointerGesture.referenceDeltas[id].dy = 0;
3104 }
3105 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3106
3107 // Add delta for all fingers and calculate a common movement delta.
3108 float commonDeltaX = 0, commonDeltaY = 0;
3109 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3110 mCurrentCookedState.fingerIdBits.value);
3111 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3112 bool first = (idBits == commonIdBits);
3113 uint32_t id = idBits.clearFirstMarkedBit();
3114 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3115 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3116 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3117 delta.dx += cpd.x - lpd.x;
3118 delta.dy += cpd.y - lpd.y;
3119
3120 if (first) {
3121 commonDeltaX = delta.dx;
3122 commonDeltaY = delta.dy;
3123 } else {
3124 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3125 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3126 }
3127 }
3128
3129 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003130 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003131 float dist[MAX_POINTER_ID + 1];
3132 int32_t distOverThreshold = 0;
3133 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3134 uint32_t id = idBits.clearFirstMarkedBit();
3135 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3136 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3137 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3138 distOverThreshold += 1;
3139 }
3140 }
3141
3142 // Only transition when at least two pointers have moved further than
3143 // the minimum distance threshold.
3144 if (distOverThreshold >= 2) {
3145 if (currentFingerCount > 2) {
3146 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003147 if (DEBUG_GESTURES) {
3148 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3149 currentFingerCount);
3150 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003151 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003152 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003153 } else {
3154 // There are exactly two pointers.
3155 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3156 uint32_t id1 = idBits.clearFirstMarkedBit();
3157 uint32_t id2 = idBits.firstMarkedBit();
3158 const RawPointerData::Pointer& p1 =
3159 mCurrentRawState.rawPointerData.pointerForId(id1);
3160 const RawPointerData::Pointer& p2 =
3161 mCurrentRawState.rawPointerData.pointerForId(id2);
3162 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3163 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3164 // There are two pointers but they are too far apart for a SWIPE,
3165 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003166 if (DEBUG_GESTURES) {
3167 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3168 "%0.3f",
3169 mutualDistance, mPointerGestureMaxSwipeWidth);
3170 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003171 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003172 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003173 } else {
3174 // There are two pointers. Wait for both pointers to start moving
3175 // before deciding whether this is a SWIPE or FREEFORM gesture.
3176 float dist1 = dist[id1];
3177 float dist2 = dist[id2];
3178 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3179 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3180 // Calculate the dot product of the displacement vectors.
3181 // When the vectors are oriented in approximately the same direction,
3182 // the angle betweeen them is near zero and the cosine of the angle
3183 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3184 // mag(v2).
3185 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3186 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3187 float dx1 = delta1.dx * mPointerXZoomScale;
3188 float dy1 = delta1.dy * mPointerYZoomScale;
3189 float dx2 = delta2.dx * mPointerXZoomScale;
3190 float dy2 = delta2.dy * mPointerYZoomScale;
3191 float dot = dx1 * dx2 + dy1 * dy2;
3192 float cosine = dot / (dist1 * dist2); // denominator always > 0
3193 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3194 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003195 if (DEBUG_GESTURES) {
3196 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3197 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3198 "cosine %0.3f >= %0.3f",
3199 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3200 mConfig.pointerGestureMultitouchMinDistance, cosine,
3201 mConfig.pointerGestureSwipeTransitionAngleCosine);
3202 }
Michael Wright227c5542020-07-02 18:30:52 +01003203 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003204 } else {
3205 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003206 if (DEBUG_GESTURES) {
3207 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3208 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3209 "cosine %0.3f < %0.3f",
3210 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3211 mConfig.pointerGestureMultitouchMinDistance, cosine,
3212 mConfig.pointerGestureSwipeTransitionAngleCosine);
3213 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003214 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003215 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003216 }
3217 }
3218 }
3219 }
3220 }
Michael Wright227c5542020-07-02 18:30:52 +01003221 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003222 // Switch from SWIPE to FREEFORM if additional pointers go down.
3223 // Cancel previous gesture.
3224 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003225 if (DEBUG_GESTURES) {
3226 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3227 currentFingerCount);
3228 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003229 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003230 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003231 }
3232 }
3233
3234 // Move the reference points based on the overall group motion of the fingers
3235 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003236 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003237 (commonDeltaX || commonDeltaY)) {
3238 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3239 uint32_t id = idBits.clearFirstMarkedBit();
3240 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3241 delta.dx = 0;
3242 delta.dy = 0;
3243 }
3244
3245 mPointerGesture.referenceTouchX += commonDeltaX;
3246 mPointerGesture.referenceTouchY += commonDeltaY;
3247
3248 commonDeltaX *= mPointerXMovementScale;
3249 commonDeltaY *= mPointerYMovementScale;
3250
Prabir Pradhan1728b212021-10-19 16:00:03 -07003251 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003252 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3253
3254 mPointerGesture.referenceGestureX += commonDeltaX;
3255 mPointerGesture.referenceGestureY += commonDeltaY;
3256 }
3257
3258 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003259 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3260 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003261 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003262 if (DEBUG_GESTURES) {
3263 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3264 "activeGestureId=%d, currentTouchPointerCount=%d",
3265 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3266 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003267 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3268
3269 mPointerGesture.currentGestureIdBits.clear();
3270 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3271 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3272 mPointerGesture.currentGestureProperties[0].clear();
3273 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3274 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3275 mPointerGesture.currentGestureCoords[0].clear();
3276 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3277 mPointerGesture.referenceGestureX);
3278 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3279 mPointerGesture.referenceGestureY);
3280 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003281 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003282 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003283 if (DEBUG_GESTURES) {
3284 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3285 "activeGestureId=%d, currentTouchPointerCount=%d",
3286 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3287 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003288 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3289
3290 mPointerGesture.currentGestureIdBits.clear();
3291
3292 BitSet32 mappedTouchIdBits;
3293 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003294 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003295 // Initially, assign the active gesture id to the active touch point
3296 // if there is one. No other touch id bits are mapped yet.
3297 if (!*outCancelPreviousGesture) {
3298 mappedTouchIdBits.markBit(activeTouchId);
3299 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3300 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3301 mPointerGesture.activeGestureId;
3302 } else {
3303 mPointerGesture.activeGestureId = -1;
3304 }
3305 } else {
3306 // Otherwise, assume we mapped all touches from the previous frame.
3307 // Reuse all mappings that are still applicable.
3308 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3309 mCurrentCookedState.fingerIdBits.value;
3310 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3311
3312 // Check whether we need to choose a new active gesture id because the
3313 // current went went up.
3314 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3315 ~mCurrentCookedState.fingerIdBits.value);
3316 !upTouchIdBits.isEmpty();) {
3317 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3318 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3319 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3320 mPointerGesture.activeGestureId = -1;
3321 break;
3322 }
3323 }
3324 }
3325
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003326 if (DEBUG_GESTURES) {
3327 ALOGD("Gestures: FREEFORM follow up "
3328 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3329 "activeGestureId=%d",
3330 mappedTouchIdBits.value, usedGestureIdBits.value,
3331 mPointerGesture.activeGestureId);
3332 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003333
3334 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3335 for (uint32_t i = 0; i < currentFingerCount; i++) {
3336 uint32_t touchId = idBits.clearFirstMarkedBit();
3337 uint32_t gestureId;
3338 if (!mappedTouchIdBits.hasBit(touchId)) {
3339 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3340 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003341 if (DEBUG_GESTURES) {
3342 ALOGD("Gestures: FREEFORM "
3343 "new mapping for touch id %d -> gesture id %d",
3344 touchId, gestureId);
3345 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003346 } else {
3347 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003348 if (DEBUG_GESTURES) {
3349 ALOGD("Gestures: FREEFORM "
3350 "existing mapping for touch id %d -> gesture id %d",
3351 touchId, gestureId);
3352 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003353 }
3354 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3355 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3356
3357 const RawPointerData::Pointer& pointer =
3358 mCurrentRawState.rawPointerData.pointerForId(touchId);
3359 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3360 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003361 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003362
3363 mPointerGesture.currentGestureProperties[i].clear();
3364 mPointerGesture.currentGestureProperties[i].id = gestureId;
3365 mPointerGesture.currentGestureProperties[i].toolType =
3366 AMOTION_EVENT_TOOL_TYPE_FINGER;
3367 mPointerGesture.currentGestureCoords[i].clear();
3368 mPointerGesture.currentGestureCoords[i]
3369 .setAxisValue(AMOTION_EVENT_AXIS_X,
3370 mPointerGesture.referenceGestureX + deltaX);
3371 mPointerGesture.currentGestureCoords[i]
3372 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3373 mPointerGesture.referenceGestureY + deltaY);
3374 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3375 1.0f);
3376 }
3377
3378 if (mPointerGesture.activeGestureId < 0) {
3379 mPointerGesture.activeGestureId =
3380 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003381 if (DEBUG_GESTURES) {
3382 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3383 mPointerGesture.activeGestureId);
3384 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003385 }
3386 }
3387 }
3388
3389 mPointerController->setButtonState(mCurrentRawState.buttonState);
3390
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003391 if (DEBUG_GESTURES) {
3392 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3393 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3394 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3395 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3396 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3397 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3398 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3399 uint32_t id = idBits.clearFirstMarkedBit();
3400 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3401 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3402 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3403 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3404 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3405 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3406 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3407 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3408 }
3409 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3410 uint32_t id = idBits.clearFirstMarkedBit();
3411 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3412 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3413 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3414 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3415 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3416 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3417 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3418 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3419 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003420 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003421 return true;
3422}
3423
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003425 mPointerSimple.currentCoords.clear();
3426 mPointerSimple.currentProperties.clear();
3427
3428 bool down, hovering;
3429 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3430 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3431 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003432 mPointerController
3433 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3434 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003435
3436 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3437 down = !hovering;
3438
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003439 float x, y;
3440 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003441 mPointerSimple.currentCoords.copyFrom(
3442 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3443 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3444 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3445 mPointerSimple.currentProperties.id = 0;
3446 mPointerSimple.currentProperties.toolType =
3447 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3448 } else {
3449 down = false;
3450 hovering = false;
3451 }
3452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003454}
3455
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3457 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003458}
3459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003461 mPointerSimple.currentCoords.clear();
3462 mPointerSimple.currentProperties.clear();
3463
3464 bool down, hovering;
3465 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3466 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3467 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3468 float deltaX = 0, deltaY = 0;
3469 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3470 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3471 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3472 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3473 mPointerXMovementScale;
3474 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3475 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3476 mPointerYMovementScale;
3477
Prabir Pradhan1728b212021-10-19 16:00:03 -07003478 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003479 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3480
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003481 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003482 } else {
3483 mPointerVelocityControl.reset();
3484 }
3485
3486 down = isPointerDown(mCurrentRawState.buttonState);
3487 hovering = !down;
3488
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003489 float x, y;
3490 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003491 mPointerSimple.currentCoords.copyFrom(
3492 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3493 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3494 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3495 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3496 hovering ? 0.0f : 1.0f);
3497 mPointerSimple.currentProperties.id = 0;
3498 mPointerSimple.currentProperties.toolType =
3499 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3500 } else {
3501 mPointerVelocityControl.reset();
3502
3503 down = false;
3504 hovering = false;
3505 }
3506
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003508}
3509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003510void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3511 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003512
3513 mPointerVelocityControl.reset();
3514}
3515
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3517 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003518 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003519
3520 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003521 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003522 mPointerController->clearSpots();
3523 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003524 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003525 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003526 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003527 }
Garfield Tan9514d782020-11-10 16:37:23 -08003528 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003529
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003530 float xCursorPosition, yCursorPosition;
3531 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003532
3533 if (mPointerSimple.down && !down) {
3534 mPointerSimple.down = false;
3535
3536 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003537 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3538 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003539 mLastRawState.buttonState, MotionClassification::NONE,
3540 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3541 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3542 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3543 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003544 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003545 }
3546
3547 if (mPointerSimple.hovering && !hovering) {
3548 mPointerSimple.hovering = false;
3549
3550 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003551 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3552 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3553 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003554 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3555 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3556 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3557 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003558 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003559 }
3560
3561 if (down) {
3562 if (!mPointerSimple.down) {
3563 mPointerSimple.down = true;
3564 mPointerSimple.downTime = when;
3565
3566 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003567 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003568 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3569 metaState, mCurrentRawState.buttonState,
3570 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3571 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3572 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3573 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003574 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003575 }
3576
3577 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003578 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3579 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003580 mCurrentRawState.buttonState, MotionClassification::NONE,
3581 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3582 &mPointerSimple.currentCoords, mOrientedXPrecision,
3583 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3584 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003585 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003586 }
3587
3588 if (hovering) {
3589 if (!mPointerSimple.hovering) {
3590 mPointerSimple.hovering = true;
3591
3592 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003593 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003594 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3595 metaState, mCurrentRawState.buttonState,
3596 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3597 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3598 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3599 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003600 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003601 }
3602
3603 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003604 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3605 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3606 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003607 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3608 &mPointerSimple.currentCoords, mOrientedXPrecision,
3609 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3610 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003611 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003612 }
3613
3614 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3615 float vscroll = mCurrentRawState.rawVScroll;
3616 float hscroll = mCurrentRawState.rawHScroll;
3617 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3618 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3619
3620 // Send scroll.
3621 PointerCoords pointerCoords;
3622 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3623 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3624 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3627 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003628 mCurrentRawState.buttonState, MotionClassification::NONE,
3629 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3630 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3631 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3632 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003633 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003634 }
3635
3636 // Save state.
3637 if (down || hovering) {
3638 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3639 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3640 } else {
3641 mPointerSimple.reset();
3642 }
3643}
3644
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003646 mPointerSimple.currentCoords.clear();
3647 mPointerSimple.currentProperties.clear();
3648
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003650}
3651
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3653 uint32_t source, int32_t action, int32_t actionButton,
3654 int32_t flags, int32_t metaState, int32_t buttonState,
3655 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003656 const PointerCoords* coords, const uint32_t* idToIndex,
3657 BitSet32 idBits, int32_t changedId, float xPrecision,
3658 float yPrecision, nsecs_t downTime) {
3659 PointerCoords pointerCoords[MAX_POINTERS];
3660 PointerProperties pointerProperties[MAX_POINTERS];
3661 uint32_t pointerCount = 0;
3662 while (!idBits.isEmpty()) {
3663 uint32_t id = idBits.clearFirstMarkedBit();
3664 uint32_t index = idToIndex[id];
3665 pointerProperties[pointerCount].copyFrom(properties[index]);
3666 pointerCoords[pointerCount].copyFrom(coords[index]);
3667
3668 if (changedId >= 0 && id == uint32_t(changedId)) {
3669 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3670 }
3671
3672 pointerCount += 1;
3673 }
3674
3675 ALOG_ASSERT(pointerCount != 0);
3676
3677 if (changedId >= 0 && pointerCount == 1) {
3678 // Replace initial down and final up action.
3679 // We can compare the action without masking off the changed pointer index
3680 // because we know the index is 0.
3681 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3682 action = AMOTION_EVENT_ACTION_DOWN;
3683 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003684 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3685 action = AMOTION_EVENT_ACTION_CANCEL;
3686 } else {
3687 action = AMOTION_EVENT_ACTION_UP;
3688 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003689 } else {
3690 // Can't happen.
3691 ALOG_ASSERT(false);
3692 }
3693 }
3694 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3695 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003696 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003697 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003698 }
3699 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3700 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003701 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003702 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003703 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003704 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3705 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003706 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3707 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3708 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003709 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003710}
3711
3712bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3713 const PointerCoords* inCoords,
3714 const uint32_t* inIdToIndex,
3715 PointerProperties* outProperties,
3716 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3717 BitSet32 idBits) const {
3718 bool changed = false;
3719 while (!idBits.isEmpty()) {
3720 uint32_t id = idBits.clearFirstMarkedBit();
3721 uint32_t inIndex = inIdToIndex[id];
3722 uint32_t outIndex = outIdToIndex[id];
3723
3724 const PointerProperties& curInProperties = inProperties[inIndex];
3725 const PointerCoords& curInCoords = inCoords[inIndex];
3726 PointerProperties& curOutProperties = outProperties[outIndex];
3727 PointerCoords& curOutCoords = outCoords[outIndex];
3728
3729 if (curInProperties != curOutProperties) {
3730 curOutProperties.copyFrom(curInProperties);
3731 changed = true;
3732 }
3733
3734 if (curInCoords != curOutCoords) {
3735 curOutCoords.copyFrom(curInCoords);
3736 changed = true;
3737 }
3738 }
3739 return changed;
3740}
3741
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3743 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3744 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003745}
3746
Prabir Pradhan1728b212021-10-19 16:00:03 -07003747// Transform input device coordinates to display panel coordinates.
3748void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003749 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3750 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3751
arthurhunga36b28e2020-12-29 20:28:15 +08003752 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3753 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3754
Prabir Pradhan1728b212021-10-19 16:00:03 -07003755 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003756 // 0 - no swap and reverse.
3757 // 90 - swap x/y and reverse y.
3758 // 180 - reverse x, y.
3759 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003760 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003761 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003762 x = xScaled;
3763 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003764 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003765 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003766 y = xScaledMax;
3767 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003768 break;
3769 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003770 x = xScaledMax;
3771 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003772 break;
3773 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003774 y = xScaled;
3775 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003776 break;
3777 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003778 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003779 }
3780}
3781
Prabir Pradhan1728b212021-10-19 16:00:03 -07003782bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003783 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3784 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3785
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003786 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003787 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003788 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003789 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003790}
3791
3792const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3793 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003794 if (DEBUG_VIRTUAL_KEYS) {
3795 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3796 "left=%d, top=%d, right=%d, bottom=%d",
3797 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3798 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3799 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003800
3801 if (virtualKey.isHit(x, y)) {
3802 return &virtualKey;
3803 }
3804 }
3805
3806 return nullptr;
3807}
3808
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003809void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3810 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3811 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003812
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003813 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003814
3815 if (currentPointerCount == 0) {
3816 // No pointers to assign.
3817 return;
3818 }
3819
3820 if (lastPointerCount == 0) {
3821 // All pointers are new.
3822 for (uint32_t i = 0; i < currentPointerCount; i++) {
3823 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003824 current.rawPointerData.pointers[i].id = id;
3825 current.rawPointerData.idToIndex[id] = i;
3826 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003827 }
3828 return;
3829 }
3830
3831 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003832 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003833 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003834 uint32_t id = last.rawPointerData.pointers[0].id;
3835 current.rawPointerData.pointers[0].id = id;
3836 current.rawPointerData.idToIndex[id] = 0;
3837 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003838 return;
3839 }
3840
3841 // General case.
3842 // We build a heap of squared euclidean distances between current and last pointers
3843 // associated with the current and last pointer indices. Then, we find the best
3844 // match (by distance) for each current pointer.
3845 // The pointers must have the same tool type but it is possible for them to
3846 // transition from hovering to touching or vice-versa while retaining the same id.
3847 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3848
3849 uint32_t heapSize = 0;
3850 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3851 currentPointerIndex++) {
3852 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3853 lastPointerIndex++) {
3854 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003855 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003856 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003857 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003858 if (currentPointer.toolType == lastPointer.toolType) {
3859 int64_t deltaX = currentPointer.x - lastPointer.x;
3860 int64_t deltaY = currentPointer.y - lastPointer.y;
3861
3862 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3863
3864 // Insert new element into the heap (sift up).
3865 heap[heapSize].currentPointerIndex = currentPointerIndex;
3866 heap[heapSize].lastPointerIndex = lastPointerIndex;
3867 heap[heapSize].distance = distance;
3868 heapSize += 1;
3869 }
3870 }
3871 }
3872
3873 // Heapify
3874 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3875 startIndex -= 1;
3876 for (uint32_t parentIndex = startIndex;;) {
3877 uint32_t childIndex = parentIndex * 2 + 1;
3878 if (childIndex >= heapSize) {
3879 break;
3880 }
3881
3882 if (childIndex + 1 < heapSize &&
3883 heap[childIndex + 1].distance < heap[childIndex].distance) {
3884 childIndex += 1;
3885 }
3886
3887 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3888 break;
3889 }
3890
3891 swap(heap[parentIndex], heap[childIndex]);
3892 parentIndex = childIndex;
3893 }
3894 }
3895
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003896 if (DEBUG_POINTER_ASSIGNMENT) {
3897 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3898 for (size_t i = 0; i < heapSize; i++) {
3899 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3900 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3901 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003902 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003903
3904 // Pull matches out by increasing order of distance.
3905 // To avoid reassigning pointers that have already been matched, the loop keeps track
3906 // of which last and current pointers have been matched using the matchedXXXBits variables.
3907 // It also tracks the used pointer id bits.
3908 BitSet32 matchedLastBits(0);
3909 BitSet32 matchedCurrentBits(0);
3910 BitSet32 usedIdBits(0);
3911 bool first = true;
3912 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3913 while (heapSize > 0) {
3914 if (first) {
3915 // The first time through the loop, we just consume the root element of
3916 // the heap (the one with smallest distance).
3917 first = false;
3918 } else {
3919 // Previous iterations consumed the root element of the heap.
3920 // Pop root element off of the heap (sift down).
3921 heap[0] = heap[heapSize];
3922 for (uint32_t parentIndex = 0;;) {
3923 uint32_t childIndex = parentIndex * 2 + 1;
3924 if (childIndex >= heapSize) {
3925 break;
3926 }
3927
3928 if (childIndex + 1 < heapSize &&
3929 heap[childIndex + 1].distance < heap[childIndex].distance) {
3930 childIndex += 1;
3931 }
3932
3933 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3934 break;
3935 }
3936
3937 swap(heap[parentIndex], heap[childIndex]);
3938 parentIndex = childIndex;
3939 }
3940
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003941 if (DEBUG_POINTER_ASSIGNMENT) {
3942 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3943 for (size_t j = 0; j < heapSize; j++) {
3944 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3945 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3946 heap[j].distance);
3947 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003948 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003949 }
3950
3951 heapSize -= 1;
3952
3953 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3954 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3955
3956 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3957 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3958
3959 matchedCurrentBits.markBit(currentPointerIndex);
3960 matchedLastBits.markBit(lastPointerIndex);
3961
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003962 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3963 current.rawPointerData.pointers[currentPointerIndex].id = id;
3964 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3965 current.rawPointerData.markIdBit(id,
3966 current.rawPointerData.isHovering(
3967 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003968 usedIdBits.markBit(id);
3969
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003970 if (DEBUG_POINTER_ASSIGNMENT) {
3971 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3972 ", distance=%" PRIu64,
3973 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3974 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003975 break;
3976 }
3977 }
3978
3979 // Assign fresh ids to pointers that were not matched in the process.
3980 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3981 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3982 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3983
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003984 current.rawPointerData.pointers[currentPointerIndex].id = id;
3985 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3986 current.rawPointerData.markIdBit(id,
3987 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003988
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003989 if (DEBUG_POINTER_ASSIGNMENT) {
3990 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3991 id);
3992 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003993 }
3994}
3995
3996int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3997 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3998 return AKEY_STATE_VIRTUAL;
3999 }
4000
4001 for (const VirtualKey& virtualKey : mVirtualKeys) {
4002 if (virtualKey.keyCode == keyCode) {
4003 return AKEY_STATE_UP;
4004 }
4005 }
4006
4007 return AKEY_STATE_UNKNOWN;
4008}
4009
4010int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4011 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4012 return AKEY_STATE_VIRTUAL;
4013 }
4014
4015 for (const VirtualKey& virtualKey : mVirtualKeys) {
4016 if (virtualKey.scanCode == scanCode) {
4017 return AKEY_STATE_UP;
4018 }
4019 }
4020
4021 return AKEY_STATE_UNKNOWN;
4022}
4023
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004024bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
4025 const std::vector<int32_t>& keyCodes,
4026 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004027 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004028 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004029 if (virtualKey.keyCode == keyCodes[i]) {
4030 outFlags[i] = 1;
4031 }
4032 }
4033 }
4034
4035 return true;
4036}
4037
4038std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4039 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004040 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004041 return std::make_optional(mPointerController->getDisplayId());
4042 } else {
4043 return std::make_optional(mViewport.displayId);
4044 }
4045 }
4046 return std::nullopt;
4047}
4048
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004049} // namespace android