blob: c1934ffbbf96c85ed1083b02bcfbb83b7cafaa71 [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 {
Garfield Tanc734e4f2021-01-15 20:01:39 -08001660 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001661
1662 if (!mCurrentMotionAborted) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001663 dispatchButtonRelease(when, readTime, policyFlags);
1664 dispatchHoverExit(when, readTime, policyFlags);
1665 dispatchTouches(when, readTime, policyFlags);
1666 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1667 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001668 }
1669
1670 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1671 mCurrentMotionAborted = false;
1672 }
1673 }
1674
1675 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001676 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001677 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1678 mCurrentCookedState.buttonState);
1679
1680 // Clear some transient state.
1681 mCurrentRawState.rawVScroll = 0;
1682 mCurrentRawState.rawHScroll = 0;
1683
1684 // Copy current touch to last touch in preparation for the next cycle.
1685 mLastRawState.copyFrom(mCurrentRawState);
1686 mLastCookedState.copyFrom(mCurrentCookedState);
1687}
1688
Garfield Tanc734e4f2021-01-15 20:01:39 -08001689void TouchInputMapper::updateTouchSpots() {
1690 if (!mConfig.showTouches || mPointerController == nullptr) {
1691 return;
1692 }
1693
1694 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1695 // clear touch spots.
1696 if (mDeviceMode != DeviceMode::DIRECT &&
1697 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1698 return;
1699 }
1700
1701 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1702 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1703
1704 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001705 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1706 mCurrentCookedState.cookedPointerData.idToIndex,
1707 mCurrentCookedState.cookedPointerData.touchingIdBits,
1708 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001709}
1710
1711bool TouchInputMapper::isTouchScreen() {
1712 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1713 mParameters.hasAssociatedDisplay;
1714}
1715
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001716void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001717 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001718 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1719 }
1720}
1721
1722void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1723 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1724 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1725
1726 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1727 float pressure = mExternalStylusState.pressure;
1728 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1729 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1730 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1731 }
1732 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1733 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1734
1735 PointerProperties& properties =
1736 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1737 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1738 properties.toolType = mExternalStylusState.toolType;
1739 }
1740 }
1741}
1742
1743bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001744 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001745 return false;
1746 }
1747
1748 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1749 state.rawPointerData.pointerCount != 0;
1750 if (initialDown) {
1751 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001752 if (DEBUG_STYLUS_FUSION) {
1753 ALOGD("Have both stylus and touch data, beginning fusion");
1754 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001755 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1756 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001757 if (DEBUG_STYLUS_FUSION) {
1758 ALOGD("Timeout expired, assuming touch is not a stylus.");
1759 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001760 resetExternalStylus();
1761 } else {
1762 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1763 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1764 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001765 if (DEBUG_STYLUS_FUSION) {
1766 ALOGD("No stylus data but stylus is connected, requesting timeout "
1767 "(%" PRId64 "ms)",
1768 mExternalStylusFusionTimeout);
1769 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001770 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1771 return true;
1772 }
1773 }
1774
1775 // Check if the stylus pointer has gone up.
1776 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001777 if (DEBUG_STYLUS_FUSION) {
1778 ALOGD("Stylus pointer is going up");
1779 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001780 mExternalStylusId = -1;
1781 }
1782
1783 return false;
1784}
1785
1786void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001787 if (mDeviceMode == DeviceMode::POINTER) {
1788 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001789 // Since this is a synthetic event, we can consider its latency to be zero
1790 const nsecs_t readTime = when;
1791 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001792 }
Michael Wright227c5542020-07-02 18:30:52 +01001793 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001794 if (mExternalStylusFusionTimeout < when) {
1795 processRawTouches(true /*timeout*/);
1796 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1797 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1798 }
1799 }
1800}
1801
1802void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1803 mExternalStylusState.copyFrom(state);
1804 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1805 // We're either in the middle of a fused stream of data or we're waiting on data before
1806 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1807 // data.
1808 mExternalStylusDataPending = true;
1809 processRawTouches(false /*timeout*/);
1810 }
1811}
1812
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001813bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001814 // Check for release of a virtual key.
1815 if (mCurrentVirtualKey.down) {
1816 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1817 // Pointer went up while virtual key was down.
1818 mCurrentVirtualKey.down = false;
1819 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001820 if (DEBUG_VIRTUAL_KEYS) {
1821 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1822 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1823 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001824 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001825 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1826 }
1827 return true;
1828 }
1829
1830 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1831 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1832 const RawPointerData::Pointer& pointer =
1833 mCurrentRawState.rawPointerData.pointerForId(id);
1834 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1835 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1836 // Pointer is still within the space of the virtual key.
1837 return true;
1838 }
1839 }
1840
1841 // Pointer left virtual key area or another pointer also went down.
1842 // Send key cancellation but do not consume the touch yet.
1843 // This is useful when the user swipes through from the virtual key area
1844 // into the main display surface.
1845 mCurrentVirtualKey.down = false;
1846 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001847 if (DEBUG_VIRTUAL_KEYS) {
1848 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1849 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1850 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001851 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001852 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1853 AKEY_EVENT_FLAG_CANCELED);
1854 }
1855 }
1856
1857 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1858 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1859 // Pointer just went down. Check for virtual key press or off-screen touches.
1860 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1861 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001862 // Skip checking whether the pointer is inside the physical frame if the device is in
1863 // unscaled mode.
1864 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1865 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001866 // If exactly one pointer went down, check for virtual key hit.
1867 // Otherwise we will drop the entire stroke.
1868 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1869 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1870 if (virtualKey) {
1871 mCurrentVirtualKey.down = true;
1872 mCurrentVirtualKey.downTime = when;
1873 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1874 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1875 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001876 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1877 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001878
1879 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001880 if (DEBUG_VIRTUAL_KEYS) {
1881 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1882 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1883 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001884 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001885 AKEY_EVENT_FLAG_FROM_SYSTEM |
1886 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1887 }
1888 }
1889 }
1890 return true;
1891 }
1892 }
1893
1894 // Disable all virtual key touches that happen within a short time interval of the
1895 // most recent touch within the screen area. The idea is to filter out stray
1896 // virtual key presses when interacting with the touch screen.
1897 //
1898 // Problems we're trying to solve:
1899 //
1900 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1901 // virtual key area that is implemented by a separate touch panel and accidentally
1902 // triggers a virtual key.
1903 //
1904 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1905 // area and accidentally triggers a virtual key. This often happens when virtual keys
1906 // are layed out below the screen near to where the on screen keyboard's space bar
1907 // is displayed.
1908 if (mConfig.virtualKeyQuietTime > 0 &&
1909 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001910 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001911 }
1912 return false;
1913}
1914
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001915void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001916 int32_t keyEventAction, int32_t keyEventFlags) {
1917 int32_t keyCode = mCurrentVirtualKey.keyCode;
1918 int32_t scanCode = mCurrentVirtualKey.scanCode;
1919 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001920 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001921 policyFlags |= POLICY_FLAG_VIRTUAL;
1922
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001923 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1924 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1925 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001926 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001927}
1928
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001929void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001930 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1931 if (!currentIdBits.isEmpty()) {
1932 int32_t metaState = getContext()->getGlobalMetaState();
1933 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001934 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1935 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001936 mCurrentCookedState.cookedPointerData.pointerProperties,
1937 mCurrentCookedState.cookedPointerData.pointerCoords,
1938 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1939 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1940 mCurrentMotionAborted = true;
1941 }
1942}
1943
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001944void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001945 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1946 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1947 int32_t metaState = getContext()->getGlobalMetaState();
1948 int32_t buttonState = mCurrentCookedState.buttonState;
1949
1950 if (currentIdBits == lastIdBits) {
1951 if (!currentIdBits.isEmpty()) {
1952 // No pointer id changes so this is a move event.
1953 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001954 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1955 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001956 mCurrentCookedState.cookedPointerData.pointerProperties,
1957 mCurrentCookedState.cookedPointerData.pointerCoords,
1958 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1959 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1960 }
1961 } else {
1962 // There may be pointers going up and pointers going down and pointers moving
1963 // all at the same time.
1964 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1965 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1966 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1967 BitSet32 dispatchedIdBits(lastIdBits.value);
1968
1969 // Update last coordinates of pointers that have moved so that we observe the new
1970 // pointer positions at the same time as other pointers that have just gone up.
1971 bool moveNeeded =
1972 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1973 mCurrentCookedState.cookedPointerData.pointerCoords,
1974 mCurrentCookedState.cookedPointerData.idToIndex,
1975 mLastCookedState.cookedPointerData.pointerProperties,
1976 mLastCookedState.cookedPointerData.pointerCoords,
1977 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1978 if (buttonState != mLastCookedState.buttonState) {
1979 moveNeeded = true;
1980 }
1981
1982 // Dispatch pointer up events.
1983 while (!upIdBits.isEmpty()) {
1984 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001985 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001986 if (isCanceled) {
1987 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1988 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001989 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001990 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001991 mLastCookedState.cookedPointerData.pointerProperties,
1992 mLastCookedState.cookedPointerData.pointerCoords,
1993 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
1994 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1995 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08001996 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001997 }
1998
1999 // Dispatch move events if any of the remaining pointers moved from their old locations.
2000 // Although applications receive new locations as part of individual pointer up
2001 // events, they do not generally handle them except when presented in a move event.
2002 if (moveNeeded && !moveIdBits.isEmpty()) {
2003 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002004 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2005 metaState, buttonState, 0,
2006 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002007 mCurrentCookedState.cookedPointerData.pointerCoords,
2008 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2009 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2010 }
2011
2012 // Dispatch pointer down events using the new pointer locations.
2013 while (!downIdBits.isEmpty()) {
2014 uint32_t downId = downIdBits.clearFirstMarkedBit();
2015 dispatchedIdBits.markBit(downId);
2016
2017 if (dispatchedIdBits.count() == 1) {
2018 // First pointer is going down. Set down time.
2019 mDownTime = when;
2020 }
2021
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002022 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2023 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002024 mCurrentCookedState.cookedPointerData.pointerProperties,
2025 mCurrentCookedState.cookedPointerData.pointerCoords,
2026 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2027 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2028 }
2029 }
2030}
2031
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002032void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002033 if (mSentHoverEnter &&
2034 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2035 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2036 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002037 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2038 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002039 mLastCookedState.cookedPointerData.pointerProperties,
2040 mLastCookedState.cookedPointerData.pointerCoords,
2041 mLastCookedState.cookedPointerData.idToIndex,
2042 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2043 mOrientedYPrecision, mDownTime);
2044 mSentHoverEnter = false;
2045 }
2046}
2047
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002048void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2049 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002050 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2051 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2052 int32_t metaState = getContext()->getGlobalMetaState();
2053 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002054 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2055 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002056 mCurrentCookedState.cookedPointerData.pointerProperties,
2057 mCurrentCookedState.cookedPointerData.pointerCoords,
2058 mCurrentCookedState.cookedPointerData.idToIndex,
2059 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2060 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2061 mSentHoverEnter = true;
2062 }
2063
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002064 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2065 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002066 mCurrentCookedState.cookedPointerData.pointerProperties,
2067 mCurrentCookedState.cookedPointerData.pointerCoords,
2068 mCurrentCookedState.cookedPointerData.idToIndex,
2069 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2070 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2071 }
2072}
2073
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002074void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002075 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2076 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2077 const int32_t metaState = getContext()->getGlobalMetaState();
2078 int32_t buttonState = mLastCookedState.buttonState;
2079 while (!releasedButtons.isEmpty()) {
2080 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2081 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002082 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002083 actionButton, 0, metaState, buttonState, 0,
2084 mCurrentCookedState.cookedPointerData.pointerProperties,
2085 mCurrentCookedState.cookedPointerData.pointerCoords,
2086 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2087 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2088 }
2089}
2090
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002091void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002092 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2093 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2094 const int32_t metaState = getContext()->getGlobalMetaState();
2095 int32_t buttonState = mLastCookedState.buttonState;
2096 while (!pressedButtons.isEmpty()) {
2097 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2098 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002099 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2100 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002101 mCurrentCookedState.cookedPointerData.pointerProperties,
2102 mCurrentCookedState.cookedPointerData.pointerCoords,
2103 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2104 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2105 }
2106}
2107
2108const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2109 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2110 return cookedPointerData.touchingIdBits;
2111 }
2112 return cookedPointerData.hoveringIdBits;
2113}
2114
2115void TouchInputMapper::cookPointerData() {
2116 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2117
2118 mCurrentCookedState.cookedPointerData.clear();
2119 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2120 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2121 mCurrentRawState.rawPointerData.hoveringIdBits;
2122 mCurrentCookedState.cookedPointerData.touchingIdBits =
2123 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002124 mCurrentCookedState.cookedPointerData.canceledIdBits =
2125 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002126
2127 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2128 mCurrentCookedState.buttonState = 0;
2129 } else {
2130 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2131 }
2132
2133 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002134 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002135 for (uint32_t i = 0; i < currentPointerCount; i++) {
2136 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2137
2138 // Size
2139 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2140 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002141 case Calibration::SizeCalibration::GEOMETRIC:
2142 case Calibration::SizeCalibration::DIAMETER:
2143 case Calibration::SizeCalibration::BOX:
2144 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002145 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2146 touchMajor = in.touchMajor;
2147 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2148 toolMajor = in.toolMajor;
2149 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2150 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2151 : in.touchMajor;
2152 } else if (mRawPointerAxes.touchMajor.valid) {
2153 toolMajor = touchMajor = in.touchMajor;
2154 toolMinor = touchMinor =
2155 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2156 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2157 : in.touchMajor;
2158 } else if (mRawPointerAxes.toolMajor.valid) {
2159 touchMajor = toolMajor = in.toolMajor;
2160 touchMinor = toolMinor =
2161 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2162 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2163 : in.toolMajor;
2164 } else {
2165 ALOG_ASSERT(false,
2166 "No touch or tool axes. "
2167 "Size calibration should have been resolved to NONE.");
2168 touchMajor = 0;
2169 touchMinor = 0;
2170 toolMajor = 0;
2171 toolMinor = 0;
2172 size = 0;
2173 }
2174
2175 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2176 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2177 if (touchingCount > 1) {
2178 touchMajor /= touchingCount;
2179 touchMinor /= touchingCount;
2180 toolMajor /= touchingCount;
2181 toolMinor /= touchingCount;
2182 size /= touchingCount;
2183 }
2184 }
2185
Michael Wright227c5542020-07-02 18:30:52 +01002186 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002187 touchMajor *= mGeometricScale;
2188 touchMinor *= mGeometricScale;
2189 toolMajor *= mGeometricScale;
2190 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002191 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002192 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2193 touchMinor = touchMajor;
2194 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2195 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002196 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002197 touchMinor = touchMajor;
2198 toolMinor = toolMajor;
2199 }
2200
2201 mCalibration.applySizeScaleAndBias(&touchMajor);
2202 mCalibration.applySizeScaleAndBias(&touchMinor);
2203 mCalibration.applySizeScaleAndBias(&toolMajor);
2204 mCalibration.applySizeScaleAndBias(&toolMinor);
2205 size *= mSizeScale;
2206 break;
2207 default:
2208 touchMajor = 0;
2209 touchMinor = 0;
2210 toolMajor = 0;
2211 toolMinor = 0;
2212 size = 0;
2213 break;
2214 }
2215
2216 // Pressure
2217 float pressure;
2218 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002219 case Calibration::PressureCalibration::PHYSICAL:
2220 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002221 pressure = in.pressure * mPressureScale;
2222 break;
2223 default:
2224 pressure = in.isHovering ? 0 : 1;
2225 break;
2226 }
2227
2228 // Tilt and Orientation
2229 float tilt;
2230 float orientation;
2231 if (mHaveTilt) {
2232 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2233 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2234 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2235 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2236 } else {
2237 tilt = 0;
2238
2239 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002240 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002241 orientation = in.orientation * mOrientationScale;
2242 break;
Michael Wright227c5542020-07-02 18:30:52 +01002243 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002244 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2245 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2246 if (c1 != 0 || c2 != 0) {
2247 orientation = atan2f(c1, c2) * 0.5f;
2248 float confidence = hypotf(c1, c2);
2249 float scale = 1.0f + confidence / 16.0f;
2250 touchMajor *= scale;
2251 touchMinor /= scale;
2252 toolMajor *= scale;
2253 toolMinor /= scale;
2254 } else {
2255 orientation = 0;
2256 }
2257 break;
2258 }
2259 default:
2260 orientation = 0;
2261 }
2262 }
2263
2264 // Distance
2265 float distance;
2266 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002267 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002268 distance = in.distance * mDistanceScale;
2269 break;
2270 default:
2271 distance = 0;
2272 }
2273
2274 // Coverage
2275 int32_t rawLeft, rawTop, rawRight, rawBottom;
2276 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002277 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002278 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2279 rawRight = in.toolMinor & 0x0000ffff;
2280 rawBottom = in.toolMajor & 0x0000ffff;
2281 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2282 break;
2283 default:
2284 rawLeft = rawTop = rawRight = rawBottom = 0;
2285 break;
2286 }
2287
2288 // Adjust X,Y coords for device calibration
2289 // TODO: Adjust coverage coords?
2290 float xTransformed = in.x, yTransformed = in.y;
2291 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002292 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002293
Prabir Pradhan1728b212021-10-19 16:00:03 -07002294 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002295 float left, top, right, bottom;
2296
Prabir Pradhan1728b212021-10-19 16:00:03 -07002297 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002298 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002299 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2300 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2301 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2302 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002303 orientation -= M_PI_2;
2304 if (mOrientedRanges.haveOrientation &&
2305 orientation < mOrientedRanges.orientation.min) {
2306 orientation +=
2307 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2308 }
2309 break;
2310 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002311 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2312 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002313 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2314 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002315 orientation -= M_PI;
2316 if (mOrientedRanges.haveOrientation &&
2317 orientation < mOrientedRanges.orientation.min) {
2318 orientation +=
2319 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2320 }
2321 break;
2322 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002323 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2324 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002325 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2326 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002327 orientation += M_PI_2;
2328 if (mOrientedRanges.haveOrientation &&
2329 orientation > mOrientedRanges.orientation.max) {
2330 orientation -=
2331 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2332 }
2333 break;
2334 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002335 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2336 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2337 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2338 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002339 break;
2340 }
2341
2342 // Write output coords.
2343 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2344 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002345 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2346 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002347 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2348 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2349 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2350 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2351 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2352 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2353 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002354 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002355 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2356 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2357 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2358 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2359 } else {
2360 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2361 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2362 }
2363
Chris Ye364fdb52020-08-05 15:07:56 -07002364 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002365 uint32_t id = in.id;
2366 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2367 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2368 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2369 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2370 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2371 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2372 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2373 }
2374
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002375 // Write output properties.
2376 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002377 properties.clear();
2378 properties.id = id;
2379 properties.toolType = in.toolType;
2380
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002381 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002382 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002383 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002384 }
2385}
2386
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002387void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002388 PointerUsage pointerUsage) {
2389 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002390 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002391 mPointerUsage = pointerUsage;
2392 }
2393
2394 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002395 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002396 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002397 break;
Michael Wright227c5542020-07-02 18:30:52 +01002398 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002399 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002400 break;
Michael Wright227c5542020-07-02 18:30:52 +01002401 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002402 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002403 break;
Michael Wright227c5542020-07-02 18:30:52 +01002404 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002405 break;
2406 }
2407}
2408
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002409void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002410 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002411 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002412 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002413 break;
Michael Wright227c5542020-07-02 18:30:52 +01002414 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002415 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002416 break;
Michael Wright227c5542020-07-02 18:30:52 +01002417 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002418 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002419 break;
Michael Wright227c5542020-07-02 18:30:52 +01002420 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002421 break;
2422 }
2423
Michael Wright227c5542020-07-02 18:30:52 +01002424 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002425}
2426
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002427void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2428 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002429 // Update current gesture coordinates.
2430 bool cancelPreviousGesture, finishPreviousGesture;
2431 bool sendEvents =
2432 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2433 if (!sendEvents) {
2434 return;
2435 }
2436 if (finishPreviousGesture) {
2437 cancelPreviousGesture = false;
2438 }
2439
2440 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002441 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002442 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002443 if (finishPreviousGesture || cancelPreviousGesture) {
2444 mPointerController->clearSpots();
2445 }
2446
Michael Wright227c5542020-07-02 18:30:52 +01002447 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002448 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2449 mPointerGesture.currentGestureIdToIndex,
2450 mPointerGesture.currentGestureIdBits,
2451 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002452 }
2453 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002454 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002455 }
2456
2457 // Show or hide the pointer if needed.
2458 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002459 case PointerGesture::Mode::NEUTRAL:
2460 case PointerGesture::Mode::QUIET:
2461 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2462 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002463 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002464 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002465 }
2466 break;
Michael Wright227c5542020-07-02 18:30:52 +01002467 case PointerGesture::Mode::TAP:
2468 case PointerGesture::Mode::TAP_DRAG:
2469 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2470 case PointerGesture::Mode::HOVER:
2471 case PointerGesture::Mode::PRESS:
2472 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002473 // Unfade the pointer when the current gesture manipulates the
2474 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002475 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002476 break;
Michael Wright227c5542020-07-02 18:30:52 +01002477 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002478 // Fade the pointer when the current gesture manipulates a different
2479 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002480 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002481 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002482 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002483 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002484 }
2485 break;
2486 }
2487
2488 // Send events!
2489 int32_t metaState = getContext()->getGlobalMetaState();
2490 int32_t buttonState = mCurrentCookedState.buttonState;
2491
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002492 uint32_t flags = 0;
2493
2494 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2495 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2496 }
2497
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002498 // Update last coordinates of pointers that have moved so that we observe the new
2499 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002500 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2501 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2502 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2503 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2504 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2505 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002506 bool moveNeeded = false;
2507 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2508 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2509 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2510 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2511 mPointerGesture.lastGestureIdBits.value);
2512 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2513 mPointerGesture.currentGestureCoords,
2514 mPointerGesture.currentGestureIdToIndex,
2515 mPointerGesture.lastGestureProperties,
2516 mPointerGesture.lastGestureCoords,
2517 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2518 if (buttonState != mLastCookedState.buttonState) {
2519 moveNeeded = true;
2520 }
2521 }
2522
2523 // Send motion events for all pointers that went up or were canceled.
2524 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2525 if (!dispatchedGestureIdBits.isEmpty()) {
2526 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002527 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2528 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002529 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2530 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2531 mPointerGesture.downTime);
2532
2533 dispatchedGestureIdBits.clear();
2534 } else {
2535 BitSet32 upGestureIdBits;
2536 if (finishPreviousGesture) {
2537 upGestureIdBits = dispatchedGestureIdBits;
2538 } else {
2539 upGestureIdBits.value =
2540 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2541 }
2542 while (!upGestureIdBits.isEmpty()) {
2543 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2544
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002545 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002546 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002547 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002548 mPointerGesture.lastGestureCoords,
2549 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2550 0, mPointerGesture.downTime);
2551
2552 dispatchedGestureIdBits.clearBit(id);
2553 }
2554 }
2555 }
2556
2557 // Send motion events for all pointers that moved.
2558 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002559 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002560 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002561 mPointerGesture.currentGestureProperties,
2562 mPointerGesture.currentGestureCoords,
2563 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2564 mPointerGesture.downTime);
2565 }
2566
2567 // Send motion events for all pointers that went down.
2568 if (down) {
2569 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2570 ~dispatchedGestureIdBits.value);
2571 while (!downGestureIdBits.isEmpty()) {
2572 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2573 dispatchedGestureIdBits.markBit(id);
2574
2575 if (dispatchedGestureIdBits.count() == 1) {
2576 mPointerGesture.downTime = when;
2577 }
2578
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002579 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002580 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002581 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002582 mPointerGesture.currentGestureCoords,
2583 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2584 0, mPointerGesture.downTime);
2585 }
2586 }
2587
2588 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002589 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002590 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2591 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002592 mPointerGesture.currentGestureProperties,
2593 mPointerGesture.currentGestureCoords,
2594 mPointerGesture.currentGestureIdToIndex,
2595 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2596 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2597 // Synthesize a hover move event after all pointers go up to indicate that
2598 // the pointer is hovering again even if the user is not currently touching
2599 // the touch pad. This ensures that a view will receive a fresh hover enter
2600 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002601 float x, y;
2602 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002603
2604 PointerProperties pointerProperties;
2605 pointerProperties.clear();
2606 pointerProperties.id = 0;
2607 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2608
2609 PointerCoords pointerCoords;
2610 pointerCoords.clear();
2611 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2612 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2613
2614 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002615 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002616 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002617 metaState, buttonState, MotionClassification::NONE,
2618 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2619 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002620 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002621 }
2622
2623 // Update state.
2624 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2625 if (!down) {
2626 mPointerGesture.lastGestureIdBits.clear();
2627 } else {
2628 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2629 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2630 uint32_t id = idBits.clearFirstMarkedBit();
2631 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2632 mPointerGesture.lastGestureProperties[index].copyFrom(
2633 mPointerGesture.currentGestureProperties[index]);
2634 mPointerGesture.lastGestureCoords[index].copyFrom(
2635 mPointerGesture.currentGestureCoords[index]);
2636 mPointerGesture.lastGestureIdToIndex[id] = index;
2637 }
2638 }
2639}
2640
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002641void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002642 // Cancel previously dispatches pointers.
2643 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2644 int32_t metaState = getContext()->getGlobalMetaState();
2645 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002646 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2647 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002648 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2649 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2650 0, 0, mPointerGesture.downTime);
2651 }
2652
2653 // Reset the current pointer gesture.
2654 mPointerGesture.reset();
2655 mPointerVelocityControl.reset();
2656
2657 // Remove any current spots.
2658 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002659 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002660 mPointerController->clearSpots();
2661 }
2662}
2663
2664bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2665 bool* outFinishPreviousGesture, bool isTimeout) {
2666 *outCancelPreviousGesture = false;
2667 *outFinishPreviousGesture = false;
2668
2669 // Handle TAP timeout.
2670 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002671 if (DEBUG_GESTURES) {
2672 ALOGD("Gestures: Processing timeout");
2673 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002674
Michael Wright227c5542020-07-02 18:30:52 +01002675 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002676 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2677 // The tap/drag timeout has not yet expired.
2678 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2679 mConfig.pointerGestureTapDragInterval);
2680 } else {
2681 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002682 if (DEBUG_GESTURES) {
2683 ALOGD("Gestures: TAP finished");
2684 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002685 *outFinishPreviousGesture = true;
2686
2687 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002688 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002689 mPointerGesture.currentGestureIdBits.clear();
2690
2691 mPointerVelocityControl.reset();
2692 return true;
2693 }
2694 }
2695
2696 // We did not handle this timeout.
2697 return false;
2698 }
2699
2700 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2701 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2702
2703 // Update the velocity tracker.
2704 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002705 std::vector<VelocityTracker::Position> positions;
2706 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002707 uint32_t id = idBits.clearFirstMarkedBit();
2708 const RawPointerData::Pointer& pointer =
2709 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002710 float x = pointer.x * mPointerXMovementScale;
2711 float y = pointer.y * mPointerYMovementScale;
2712 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002713 }
2714 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2715 positions);
2716 }
2717
2718 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2719 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002720 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2721 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2722 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002723 mPointerGesture.resetTap();
2724 }
2725
2726 // Pick a new active touch id if needed.
2727 // Choose an arbitrary pointer that just went down, if there is one.
2728 // Otherwise choose an arbitrary remaining pointer.
2729 // This guarantees we always have an active touch id when there is at least one pointer.
2730 // We keep the same active touch id for as long as possible.
2731 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2732 int32_t activeTouchId = lastActiveTouchId;
2733 if (activeTouchId < 0) {
2734 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2735 activeTouchId = mPointerGesture.activeTouchId =
2736 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2737 mPointerGesture.firstTouchTime = when;
2738 }
2739 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2740 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2741 activeTouchId = mPointerGesture.activeTouchId =
2742 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2743 } else {
2744 activeTouchId = mPointerGesture.activeTouchId = -1;
2745 }
2746 }
2747
2748 // Determine whether we are in quiet time.
2749 bool isQuietTime = false;
2750 if (activeTouchId < 0) {
2751 mPointerGesture.resetQuietTime();
2752 } else {
2753 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2754 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002755 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2756 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2757 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002758 currentFingerCount < 2) {
2759 // Enter quiet time when exiting swipe or freeform state.
2760 // This is to prevent accidentally entering the hover state and flinging the
2761 // pointer when finishing a swipe and there is still one pointer left onscreen.
2762 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002763 } else if (mPointerGesture.lastGestureMode ==
2764 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002765 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2766 // Enter quiet time when releasing the button and there are still two or more
2767 // fingers down. This may indicate that one finger was used to press the button
2768 // but it has not gone up yet.
2769 isQuietTime = true;
2770 }
2771 if (isQuietTime) {
2772 mPointerGesture.quietTime = when;
2773 }
2774 }
2775 }
2776
2777 // Switch states based on button and pointer state.
2778 if (isQuietTime) {
2779 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002780 if (DEBUG_GESTURES) {
2781 ALOGD("Gestures: QUIET for next %0.3fms",
2782 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2783 0.000001f);
2784 }
Michael Wright227c5542020-07-02 18:30:52 +01002785 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002786 *outFinishPreviousGesture = true;
2787 }
2788
2789 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002790 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002791 mPointerGesture.currentGestureIdBits.clear();
2792
2793 mPointerVelocityControl.reset();
2794 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2795 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2796 // The pointer follows the active touch point.
2797 // Emit DOWN, MOVE, UP events at the pointer location.
2798 //
2799 // Only the active touch matters; other fingers are ignored. This policy helps
2800 // to handle the case where the user places a second finger on the touch pad
2801 // to apply the necessary force to depress an integrated button below the surface.
2802 // We don't want the second finger to be delivered to applications.
2803 //
2804 // For this to work well, we need to make sure to track the pointer that is really
2805 // active. If the user first puts one finger down to click then adds another
2806 // finger to drag then the active pointer should switch to the finger that is
2807 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002808 if (DEBUG_GESTURES) {
2809 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2810 "currentFingerCount=%d",
2811 activeTouchId, currentFingerCount);
2812 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002813 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002814 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002815 *outFinishPreviousGesture = true;
2816 mPointerGesture.activeGestureId = 0;
2817 }
2818
2819 // Switch pointers if needed.
2820 // Find the fastest pointer and follow it.
2821 if (activeTouchId >= 0 && currentFingerCount > 1) {
2822 int32_t bestId = -1;
2823 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2824 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2825 uint32_t id = idBits.clearFirstMarkedBit();
2826 float vx, vy;
2827 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2828 float speed = hypotf(vx, vy);
2829 if (speed > bestSpeed) {
2830 bestId = id;
2831 bestSpeed = speed;
2832 }
2833 }
2834 }
2835 if (bestId >= 0 && bestId != activeTouchId) {
2836 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002837 if (DEBUG_GESTURES) {
2838 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2839 "bestId=%d, bestSpeed=%0.3f",
2840 bestId, bestSpeed);
2841 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002842 }
2843 }
2844
2845 float deltaX = 0, deltaY = 0;
2846 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2847 const RawPointerData::Pointer& currentPointer =
2848 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2849 const RawPointerData::Pointer& lastPointer =
2850 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2851 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2852 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2853
Prabir Pradhan1728b212021-10-19 16:00:03 -07002854 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002855 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2856
2857 // Move the pointer using a relative motion.
2858 // When using spots, the click will occur at the position of the anchor
2859 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002860 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002861 } else {
2862 mPointerVelocityControl.reset();
2863 }
2864
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002865 float x, y;
2866 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002867
Michael Wright227c5542020-07-02 18:30:52 +01002868 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002869 mPointerGesture.currentGestureIdBits.clear();
2870 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2871 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2872 mPointerGesture.currentGestureProperties[0].clear();
2873 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2874 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2875 mPointerGesture.currentGestureCoords[0].clear();
2876 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2877 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2878 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2879 } else if (currentFingerCount == 0) {
2880 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002881 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002882 *outFinishPreviousGesture = true;
2883 }
2884
2885 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2886 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2887 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002888 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2889 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002890 lastFingerCount == 1) {
2891 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002892 float x, y;
2893 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002894 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2895 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002896 if (DEBUG_GESTURES) {
2897 ALOGD("Gestures: TAP");
2898 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002899
2900 mPointerGesture.tapUpTime = when;
2901 getContext()->requestTimeoutAtTime(when +
2902 mConfig.pointerGestureTapDragInterval);
2903
2904 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002905 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002906 mPointerGesture.currentGestureIdBits.clear();
2907 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2908 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2909 mPointerGesture.currentGestureProperties[0].clear();
2910 mPointerGesture.currentGestureProperties[0].id =
2911 mPointerGesture.activeGestureId;
2912 mPointerGesture.currentGestureProperties[0].toolType =
2913 AMOTION_EVENT_TOOL_TYPE_FINGER;
2914 mPointerGesture.currentGestureCoords[0].clear();
2915 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2916 mPointerGesture.tapX);
2917 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2918 mPointerGesture.tapY);
2919 mPointerGesture.currentGestureCoords[0]
2920 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2921
2922 tapped = true;
2923 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002924 if (DEBUG_GESTURES) {
2925 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2926 y - mPointerGesture.tapY);
2927 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002928 }
2929 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002930 if (DEBUG_GESTURES) {
2931 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2932 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2933 (when - mPointerGesture.tapDownTime) * 0.000001f);
2934 } else {
2935 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2936 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002937 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002938 }
2939 }
2940
2941 mPointerVelocityControl.reset();
2942
2943 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002944 if (DEBUG_GESTURES) {
2945 ALOGD("Gestures: NEUTRAL");
2946 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002947 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002948 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002949 mPointerGesture.currentGestureIdBits.clear();
2950 }
2951 } else if (currentFingerCount == 1) {
2952 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2953 // The pointer follows the active touch point.
2954 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2955 // When in TAP_DRAG, emit MOVE events at the pointer location.
2956 ALOG_ASSERT(activeTouchId >= 0);
2957
Michael Wright227c5542020-07-02 18:30:52 +01002958 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2959 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002960 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002961 float x, y;
2962 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002963 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2964 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002965 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002966 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002967 if (DEBUG_GESTURES) {
2968 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2969 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
2970 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002971 }
2972 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002973 if (DEBUG_GESTURES) {
2974 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2975 (when - mPointerGesture.tapUpTime) * 0.000001f);
2976 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002977 }
Michael Wright227c5542020-07-02 18:30:52 +01002978 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2979 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002980 }
2981
2982 float deltaX = 0, deltaY = 0;
2983 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2984 const RawPointerData::Pointer& currentPointer =
2985 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2986 const RawPointerData::Pointer& lastPointer =
2987 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2988 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2989 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2990
Prabir Pradhan1728b212021-10-19 16:00:03 -07002991 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002992 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2993
2994 // Move the pointer using a relative motion.
2995 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002996 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002997 } else {
2998 mPointerVelocityControl.reset();
2999 }
3000
3001 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003002 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003003 if (DEBUG_GESTURES) {
3004 ALOGD("Gestures: TAP_DRAG");
3005 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003006 down = true;
3007 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003008 if (DEBUG_GESTURES) {
3009 ALOGD("Gestures: HOVER");
3010 }
Michael Wright227c5542020-07-02 18:30:52 +01003011 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003012 *outFinishPreviousGesture = true;
3013 }
3014 mPointerGesture.activeGestureId = 0;
3015 down = false;
3016 }
3017
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003018 float x, y;
3019 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003020
3021 mPointerGesture.currentGestureIdBits.clear();
3022 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3023 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3024 mPointerGesture.currentGestureProperties[0].clear();
3025 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3026 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3027 mPointerGesture.currentGestureCoords[0].clear();
3028 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3029 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3030 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3031 down ? 1.0f : 0.0f);
3032
3033 if (lastFingerCount == 0 && currentFingerCount != 0) {
3034 mPointerGesture.resetTap();
3035 mPointerGesture.tapDownTime = when;
3036 mPointerGesture.tapX = x;
3037 mPointerGesture.tapY = y;
3038 }
3039 } else {
3040 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3041 // We need to provide feedback for each finger that goes down so we cannot wait
3042 // for the fingers to move before deciding what to do.
3043 //
3044 // The ambiguous case is deciding what to do when there are two fingers down but they
3045 // have not moved enough to determine whether they are part of a drag or part of a
3046 // freeform gesture, or just a press or long-press at the pointer location.
3047 //
3048 // When there are two fingers we start with the PRESS hypothesis and we generate a
3049 // down at the pointer location.
3050 //
3051 // When the two fingers move enough or when additional fingers are added, we make
3052 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3053 ALOG_ASSERT(activeTouchId >= 0);
3054
3055 bool settled = when >=
3056 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003057 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3058 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3059 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003060 *outFinishPreviousGesture = true;
3061 } else if (!settled && currentFingerCount > lastFingerCount) {
3062 // Additional pointers have gone down but not yet settled.
3063 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003064 if (DEBUG_GESTURES) {
3065 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3066 "MULTITOUCH, settle time remaining %0.3fms",
3067 (mPointerGesture.firstTouchTime +
3068 mConfig.pointerGestureMultitouchSettleInterval - when) *
3069 0.000001f);
3070 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003071 *outCancelPreviousGesture = true;
3072 } else {
3073 // Continue previous gesture.
3074 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3075 }
3076
3077 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003078 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003079 mPointerGesture.activeGestureId = 0;
3080 mPointerGesture.referenceIdBits.clear();
3081 mPointerVelocityControl.reset();
3082
3083 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003084 if (DEBUG_GESTURES) {
3085 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3086 "settle time remaining %0.3fms",
3087 (mPointerGesture.firstTouchTime +
3088 mConfig.pointerGestureMultitouchSettleInterval - when) *
3089 0.000001f);
3090 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003091 mCurrentRawState.rawPointerData
3092 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3093 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003094 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3095 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003096 }
3097
3098 // Clear the reference deltas for fingers not yet included in the reference calculation.
3099 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3100 ~mPointerGesture.referenceIdBits.value);
3101 !idBits.isEmpty();) {
3102 uint32_t id = idBits.clearFirstMarkedBit();
3103 mPointerGesture.referenceDeltas[id].dx = 0;
3104 mPointerGesture.referenceDeltas[id].dy = 0;
3105 }
3106 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3107
3108 // Add delta for all fingers and calculate a common movement delta.
3109 float commonDeltaX = 0, commonDeltaY = 0;
3110 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3111 mCurrentCookedState.fingerIdBits.value);
3112 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3113 bool first = (idBits == commonIdBits);
3114 uint32_t id = idBits.clearFirstMarkedBit();
3115 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3116 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3117 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3118 delta.dx += cpd.x - lpd.x;
3119 delta.dy += cpd.y - lpd.y;
3120
3121 if (first) {
3122 commonDeltaX = delta.dx;
3123 commonDeltaY = delta.dy;
3124 } else {
3125 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3126 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3127 }
3128 }
3129
3130 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003131 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003132 float dist[MAX_POINTER_ID + 1];
3133 int32_t distOverThreshold = 0;
3134 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3135 uint32_t id = idBits.clearFirstMarkedBit();
3136 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3137 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3138 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3139 distOverThreshold += 1;
3140 }
3141 }
3142
3143 // Only transition when at least two pointers have moved further than
3144 // the minimum distance threshold.
3145 if (distOverThreshold >= 2) {
3146 if (currentFingerCount > 2) {
3147 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003148 if (DEBUG_GESTURES) {
3149 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3150 currentFingerCount);
3151 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003152 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003153 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003154 } else {
3155 // There are exactly two pointers.
3156 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3157 uint32_t id1 = idBits.clearFirstMarkedBit();
3158 uint32_t id2 = idBits.firstMarkedBit();
3159 const RawPointerData::Pointer& p1 =
3160 mCurrentRawState.rawPointerData.pointerForId(id1);
3161 const RawPointerData::Pointer& p2 =
3162 mCurrentRawState.rawPointerData.pointerForId(id2);
3163 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3164 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3165 // There are two pointers but they are too far apart for a SWIPE,
3166 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003167 if (DEBUG_GESTURES) {
3168 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3169 "%0.3f",
3170 mutualDistance, mPointerGestureMaxSwipeWidth);
3171 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003172 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003173 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003174 } else {
3175 // There are two pointers. Wait for both pointers to start moving
3176 // before deciding whether this is a SWIPE or FREEFORM gesture.
3177 float dist1 = dist[id1];
3178 float dist2 = dist[id2];
3179 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3180 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3181 // Calculate the dot product of the displacement vectors.
3182 // When the vectors are oriented in approximately the same direction,
3183 // the angle betweeen them is near zero and the cosine of the angle
3184 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3185 // mag(v2).
3186 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3187 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3188 float dx1 = delta1.dx * mPointerXZoomScale;
3189 float dy1 = delta1.dy * mPointerYZoomScale;
3190 float dx2 = delta2.dx * mPointerXZoomScale;
3191 float dy2 = delta2.dy * mPointerYZoomScale;
3192 float dot = dx1 * dx2 + dy1 * dy2;
3193 float cosine = dot / (dist1 * dist2); // denominator always > 0
3194 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3195 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003196 if (DEBUG_GESTURES) {
3197 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3198 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3199 "cosine %0.3f >= %0.3f",
3200 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3201 mConfig.pointerGestureMultitouchMinDistance, cosine,
3202 mConfig.pointerGestureSwipeTransitionAngleCosine);
3203 }
Michael Wright227c5542020-07-02 18:30:52 +01003204 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003205 } else {
3206 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003207 if (DEBUG_GESTURES) {
3208 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3209 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3210 "cosine %0.3f < %0.3f",
3211 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3212 mConfig.pointerGestureMultitouchMinDistance, cosine,
3213 mConfig.pointerGestureSwipeTransitionAngleCosine);
3214 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003215 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003216 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003217 }
3218 }
3219 }
3220 }
3221 }
Michael Wright227c5542020-07-02 18:30:52 +01003222 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003223 // Switch from SWIPE to FREEFORM if additional pointers go down.
3224 // Cancel previous gesture.
3225 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003226 if (DEBUG_GESTURES) {
3227 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3228 currentFingerCount);
3229 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003230 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003231 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003232 }
3233 }
3234
3235 // Move the reference points based on the overall group motion of the fingers
3236 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003237 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003238 (commonDeltaX || commonDeltaY)) {
3239 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3240 uint32_t id = idBits.clearFirstMarkedBit();
3241 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3242 delta.dx = 0;
3243 delta.dy = 0;
3244 }
3245
3246 mPointerGesture.referenceTouchX += commonDeltaX;
3247 mPointerGesture.referenceTouchY += commonDeltaY;
3248
3249 commonDeltaX *= mPointerXMovementScale;
3250 commonDeltaY *= mPointerYMovementScale;
3251
Prabir Pradhan1728b212021-10-19 16:00:03 -07003252 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003253 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3254
3255 mPointerGesture.referenceGestureX += commonDeltaX;
3256 mPointerGesture.referenceGestureY += commonDeltaY;
3257 }
3258
3259 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003260 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3261 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003262 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003263 if (DEBUG_GESTURES) {
3264 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3265 "activeGestureId=%d, currentTouchPointerCount=%d",
3266 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3267 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003268 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3269
3270 mPointerGesture.currentGestureIdBits.clear();
3271 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3272 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3273 mPointerGesture.currentGestureProperties[0].clear();
3274 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3275 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3276 mPointerGesture.currentGestureCoords[0].clear();
3277 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3278 mPointerGesture.referenceGestureX);
3279 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3280 mPointerGesture.referenceGestureY);
3281 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003282 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003283 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003284 if (DEBUG_GESTURES) {
3285 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3286 "activeGestureId=%d, currentTouchPointerCount=%d",
3287 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3288 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003289 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3290
3291 mPointerGesture.currentGestureIdBits.clear();
3292
3293 BitSet32 mappedTouchIdBits;
3294 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003295 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003296 // Initially, assign the active gesture id to the active touch point
3297 // if there is one. No other touch id bits are mapped yet.
3298 if (!*outCancelPreviousGesture) {
3299 mappedTouchIdBits.markBit(activeTouchId);
3300 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3301 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3302 mPointerGesture.activeGestureId;
3303 } else {
3304 mPointerGesture.activeGestureId = -1;
3305 }
3306 } else {
3307 // Otherwise, assume we mapped all touches from the previous frame.
3308 // Reuse all mappings that are still applicable.
3309 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3310 mCurrentCookedState.fingerIdBits.value;
3311 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3312
3313 // Check whether we need to choose a new active gesture id because the
3314 // current went went up.
3315 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3316 ~mCurrentCookedState.fingerIdBits.value);
3317 !upTouchIdBits.isEmpty();) {
3318 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3319 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3320 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3321 mPointerGesture.activeGestureId = -1;
3322 break;
3323 }
3324 }
3325 }
3326
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003327 if (DEBUG_GESTURES) {
3328 ALOGD("Gestures: FREEFORM follow up "
3329 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3330 "activeGestureId=%d",
3331 mappedTouchIdBits.value, usedGestureIdBits.value,
3332 mPointerGesture.activeGestureId);
3333 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003334
3335 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3336 for (uint32_t i = 0; i < currentFingerCount; i++) {
3337 uint32_t touchId = idBits.clearFirstMarkedBit();
3338 uint32_t gestureId;
3339 if (!mappedTouchIdBits.hasBit(touchId)) {
3340 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3341 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003342 if (DEBUG_GESTURES) {
3343 ALOGD("Gestures: FREEFORM "
3344 "new mapping for touch id %d -> gesture id %d",
3345 touchId, gestureId);
3346 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003347 } else {
3348 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003349 if (DEBUG_GESTURES) {
3350 ALOGD("Gestures: FREEFORM "
3351 "existing mapping for touch id %d -> gesture id %d",
3352 touchId, gestureId);
3353 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003354 }
3355 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3356 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3357
3358 const RawPointerData::Pointer& pointer =
3359 mCurrentRawState.rawPointerData.pointerForId(touchId);
3360 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3361 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003362 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003363
3364 mPointerGesture.currentGestureProperties[i].clear();
3365 mPointerGesture.currentGestureProperties[i].id = gestureId;
3366 mPointerGesture.currentGestureProperties[i].toolType =
3367 AMOTION_EVENT_TOOL_TYPE_FINGER;
3368 mPointerGesture.currentGestureCoords[i].clear();
3369 mPointerGesture.currentGestureCoords[i]
3370 .setAxisValue(AMOTION_EVENT_AXIS_X,
3371 mPointerGesture.referenceGestureX + deltaX);
3372 mPointerGesture.currentGestureCoords[i]
3373 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3374 mPointerGesture.referenceGestureY + deltaY);
3375 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3376 1.0f);
3377 }
3378
3379 if (mPointerGesture.activeGestureId < 0) {
3380 mPointerGesture.activeGestureId =
3381 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003382 if (DEBUG_GESTURES) {
3383 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3384 mPointerGesture.activeGestureId);
3385 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003386 }
3387 }
3388 }
3389
3390 mPointerController->setButtonState(mCurrentRawState.buttonState);
3391
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003392 if (DEBUG_GESTURES) {
3393 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3394 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3395 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3396 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3397 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3398 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3399 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3400 uint32_t id = idBits.clearFirstMarkedBit();
3401 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3402 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3403 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3404 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3405 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3406 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3407 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3408 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3409 }
3410 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3411 uint32_t id = idBits.clearFirstMarkedBit();
3412 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3413 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3414 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3415 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3416 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3417 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3418 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3419 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3420 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003421 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003422 return true;
3423}
3424
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003426 mPointerSimple.currentCoords.clear();
3427 mPointerSimple.currentProperties.clear();
3428
3429 bool down, hovering;
3430 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3431 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3432 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003433 mPointerController
3434 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3435 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003436
3437 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3438 down = !hovering;
3439
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003440 float x, y;
3441 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003442 mPointerSimple.currentCoords.copyFrom(
3443 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3444 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3445 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3446 mPointerSimple.currentProperties.id = 0;
3447 mPointerSimple.currentProperties.toolType =
3448 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3449 } else {
3450 down = false;
3451 hovering = false;
3452 }
3453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003455}
3456
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3458 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003459}
3460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003461void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003462 mPointerSimple.currentCoords.clear();
3463 mPointerSimple.currentProperties.clear();
3464
3465 bool down, hovering;
3466 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3467 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3468 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3469 float deltaX = 0, deltaY = 0;
3470 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3471 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3472 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3473 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3474 mPointerXMovementScale;
3475 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3476 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3477 mPointerYMovementScale;
3478
Prabir Pradhan1728b212021-10-19 16:00:03 -07003479 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003480 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3481
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003482 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003483 } else {
3484 mPointerVelocityControl.reset();
3485 }
3486
3487 down = isPointerDown(mCurrentRawState.buttonState);
3488 hovering = !down;
3489
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003490 float x, y;
3491 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003492 mPointerSimple.currentCoords.copyFrom(
3493 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3494 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3495 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3496 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3497 hovering ? 0.0f : 1.0f);
3498 mPointerSimple.currentProperties.id = 0;
3499 mPointerSimple.currentProperties.toolType =
3500 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3501 } else {
3502 mPointerVelocityControl.reset();
3503
3504 down = false;
3505 hovering = false;
3506 }
3507
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003509}
3510
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3512 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003513
3514 mPointerVelocityControl.reset();
3515}
3516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3518 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003519 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003520
3521 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003522 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003523 mPointerController->clearSpots();
3524 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003525 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003526 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003527 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003528 }
Garfield Tan9514d782020-11-10 16:37:23 -08003529 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003530
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003531 float xCursorPosition, yCursorPosition;
3532 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003533
3534 if (mPointerSimple.down && !down) {
3535 mPointerSimple.down = false;
3536
3537 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003538 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3539 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003540 mLastRawState.buttonState, MotionClassification::NONE,
3541 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3542 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3543 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3544 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003545 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003546 }
3547
3548 if (mPointerSimple.hovering && !hovering) {
3549 mPointerSimple.hovering = false;
3550
3551 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3553 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3554 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003555 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3556 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3557 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3558 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003559 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003560 }
3561
3562 if (down) {
3563 if (!mPointerSimple.down) {
3564 mPointerSimple.down = true;
3565 mPointerSimple.downTime = when;
3566
3567 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003568 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003569 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3570 metaState, mCurrentRawState.buttonState,
3571 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3572 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3573 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3574 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003575 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003576 }
3577
3578 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003579 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3580 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003581 mCurrentRawState.buttonState, MotionClassification::NONE,
3582 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3583 &mPointerSimple.currentCoords, mOrientedXPrecision,
3584 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3585 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003586 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003587 }
3588
3589 if (hovering) {
3590 if (!mPointerSimple.hovering) {
3591 mPointerSimple.hovering = true;
3592
3593 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003594 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003595 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3596 metaState, mCurrentRawState.buttonState,
3597 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3598 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3599 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3600 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003601 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003602 }
3603
3604 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3606 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3607 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003608 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3609 &mPointerSimple.currentCoords, mOrientedXPrecision,
3610 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3611 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003612 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003613 }
3614
3615 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3616 float vscroll = mCurrentRawState.rawVScroll;
3617 float hscroll = mCurrentRawState.rawHScroll;
3618 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3619 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3620
3621 // Send scroll.
3622 PointerCoords pointerCoords;
3623 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3624 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3625 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3626
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3628 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003629 mCurrentRawState.buttonState, MotionClassification::NONE,
3630 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3631 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3632 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3633 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003634 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003635 }
3636
3637 // Save state.
3638 if (down || hovering) {
3639 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3640 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3641 } else {
3642 mPointerSimple.reset();
3643 }
3644}
3645
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003646void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003647 mPointerSimple.currentCoords.clear();
3648 mPointerSimple.currentProperties.clear();
3649
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003650 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003651}
3652
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003653void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3654 uint32_t source, int32_t action, int32_t actionButton,
3655 int32_t flags, int32_t metaState, int32_t buttonState,
3656 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003657 const PointerCoords* coords, const uint32_t* idToIndex,
3658 BitSet32 idBits, int32_t changedId, float xPrecision,
3659 float yPrecision, nsecs_t downTime) {
3660 PointerCoords pointerCoords[MAX_POINTERS];
3661 PointerProperties pointerProperties[MAX_POINTERS];
3662 uint32_t pointerCount = 0;
3663 while (!idBits.isEmpty()) {
3664 uint32_t id = idBits.clearFirstMarkedBit();
3665 uint32_t index = idToIndex[id];
3666 pointerProperties[pointerCount].copyFrom(properties[index]);
3667 pointerCoords[pointerCount].copyFrom(coords[index]);
3668
3669 if (changedId >= 0 && id == uint32_t(changedId)) {
3670 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3671 }
3672
3673 pointerCount += 1;
3674 }
3675
3676 ALOG_ASSERT(pointerCount != 0);
3677
3678 if (changedId >= 0 && pointerCount == 1) {
3679 // Replace initial down and final up action.
3680 // We can compare the action without masking off the changed pointer index
3681 // because we know the index is 0.
3682 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3683 action = AMOTION_EVENT_ACTION_DOWN;
3684 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003685 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3686 action = AMOTION_EVENT_ACTION_CANCEL;
3687 } else {
3688 action = AMOTION_EVENT_ACTION_UP;
3689 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003690 } else {
3691 // Can't happen.
3692 ALOG_ASSERT(false);
3693 }
3694 }
3695 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3696 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003697 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003698 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003699 }
3700 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3701 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003702 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003703 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003704 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003705 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3706 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003707 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3708 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3709 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003710 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003711}
3712
3713bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3714 const PointerCoords* inCoords,
3715 const uint32_t* inIdToIndex,
3716 PointerProperties* outProperties,
3717 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3718 BitSet32 idBits) const {
3719 bool changed = false;
3720 while (!idBits.isEmpty()) {
3721 uint32_t id = idBits.clearFirstMarkedBit();
3722 uint32_t inIndex = inIdToIndex[id];
3723 uint32_t outIndex = outIdToIndex[id];
3724
3725 const PointerProperties& curInProperties = inProperties[inIndex];
3726 const PointerCoords& curInCoords = inCoords[inIndex];
3727 PointerProperties& curOutProperties = outProperties[outIndex];
3728 PointerCoords& curOutCoords = outCoords[outIndex];
3729
3730 if (curInProperties != curOutProperties) {
3731 curOutProperties.copyFrom(curInProperties);
3732 changed = true;
3733 }
3734
3735 if (curInCoords != curOutCoords) {
3736 curOutCoords.copyFrom(curInCoords);
3737 changed = true;
3738 }
3739 }
3740 return changed;
3741}
3742
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003743void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3744 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3745 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003746}
3747
Prabir Pradhan1728b212021-10-19 16:00:03 -07003748// Transform input device coordinates to display panel coordinates.
3749void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003750 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3751 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3752
arthurhunga36b28e2020-12-29 20:28:15 +08003753 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3754 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3755
Prabir Pradhan1728b212021-10-19 16:00:03 -07003756 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003757 // 0 - no swap and reverse.
3758 // 90 - swap x/y and reverse y.
3759 // 180 - reverse x, y.
3760 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003761 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003762 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003763 x = xScaled;
3764 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003765 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003766 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003767 y = xScaledMax;
3768 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003769 break;
3770 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003771 x = xScaledMax;
3772 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003773 break;
3774 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003775 y = xScaled;
3776 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003777 break;
3778 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003779 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003780 }
3781}
3782
Prabir Pradhan1728b212021-10-19 16:00:03 -07003783bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003784 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3785 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3786
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003787 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003788 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003789 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003790 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003791}
3792
3793const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3794 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003795 if (DEBUG_VIRTUAL_KEYS) {
3796 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3797 "left=%d, top=%d, right=%d, bottom=%d",
3798 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3799 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3800 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003801
3802 if (virtualKey.isHit(x, y)) {
3803 return &virtualKey;
3804 }
3805 }
3806
3807 return nullptr;
3808}
3809
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003810void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3811 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3812 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003813
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003814 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003815
3816 if (currentPointerCount == 0) {
3817 // No pointers to assign.
3818 return;
3819 }
3820
3821 if (lastPointerCount == 0) {
3822 // All pointers are new.
3823 for (uint32_t i = 0; i < currentPointerCount; i++) {
3824 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003825 current.rawPointerData.pointers[i].id = id;
3826 current.rawPointerData.idToIndex[id] = i;
3827 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003828 }
3829 return;
3830 }
3831
3832 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003833 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003834 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003835 uint32_t id = last.rawPointerData.pointers[0].id;
3836 current.rawPointerData.pointers[0].id = id;
3837 current.rawPointerData.idToIndex[id] = 0;
3838 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003839 return;
3840 }
3841
3842 // General case.
3843 // We build a heap of squared euclidean distances between current and last pointers
3844 // associated with the current and last pointer indices. Then, we find the best
3845 // match (by distance) for each current pointer.
3846 // The pointers must have the same tool type but it is possible for them to
3847 // transition from hovering to touching or vice-versa while retaining the same id.
3848 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3849
3850 uint32_t heapSize = 0;
3851 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3852 currentPointerIndex++) {
3853 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3854 lastPointerIndex++) {
3855 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003856 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003857 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003858 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003859 if (currentPointer.toolType == lastPointer.toolType) {
3860 int64_t deltaX = currentPointer.x - lastPointer.x;
3861 int64_t deltaY = currentPointer.y - lastPointer.y;
3862
3863 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3864
3865 // Insert new element into the heap (sift up).
3866 heap[heapSize].currentPointerIndex = currentPointerIndex;
3867 heap[heapSize].lastPointerIndex = lastPointerIndex;
3868 heap[heapSize].distance = distance;
3869 heapSize += 1;
3870 }
3871 }
3872 }
3873
3874 // Heapify
3875 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3876 startIndex -= 1;
3877 for (uint32_t parentIndex = startIndex;;) {
3878 uint32_t childIndex = parentIndex * 2 + 1;
3879 if (childIndex >= heapSize) {
3880 break;
3881 }
3882
3883 if (childIndex + 1 < heapSize &&
3884 heap[childIndex + 1].distance < heap[childIndex].distance) {
3885 childIndex += 1;
3886 }
3887
3888 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3889 break;
3890 }
3891
3892 swap(heap[parentIndex], heap[childIndex]);
3893 parentIndex = childIndex;
3894 }
3895 }
3896
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003897 if (DEBUG_POINTER_ASSIGNMENT) {
3898 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3899 for (size_t i = 0; i < heapSize; i++) {
3900 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3901 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3902 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003903 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003904
3905 // Pull matches out by increasing order of distance.
3906 // To avoid reassigning pointers that have already been matched, the loop keeps track
3907 // of which last and current pointers have been matched using the matchedXXXBits variables.
3908 // It also tracks the used pointer id bits.
3909 BitSet32 matchedLastBits(0);
3910 BitSet32 matchedCurrentBits(0);
3911 BitSet32 usedIdBits(0);
3912 bool first = true;
3913 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3914 while (heapSize > 0) {
3915 if (first) {
3916 // The first time through the loop, we just consume the root element of
3917 // the heap (the one with smallest distance).
3918 first = false;
3919 } else {
3920 // Previous iterations consumed the root element of the heap.
3921 // Pop root element off of the heap (sift down).
3922 heap[0] = heap[heapSize];
3923 for (uint32_t parentIndex = 0;;) {
3924 uint32_t childIndex = parentIndex * 2 + 1;
3925 if (childIndex >= heapSize) {
3926 break;
3927 }
3928
3929 if (childIndex + 1 < heapSize &&
3930 heap[childIndex + 1].distance < heap[childIndex].distance) {
3931 childIndex += 1;
3932 }
3933
3934 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3935 break;
3936 }
3937
3938 swap(heap[parentIndex], heap[childIndex]);
3939 parentIndex = childIndex;
3940 }
3941
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003942 if (DEBUG_POINTER_ASSIGNMENT) {
3943 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3944 for (size_t j = 0; j < heapSize; j++) {
3945 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3946 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3947 heap[j].distance);
3948 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003949 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003950 }
3951
3952 heapSize -= 1;
3953
3954 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3955 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3956
3957 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3958 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3959
3960 matchedCurrentBits.markBit(currentPointerIndex);
3961 matchedLastBits.markBit(lastPointerIndex);
3962
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003963 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3964 current.rawPointerData.pointers[currentPointerIndex].id = id;
3965 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3966 current.rawPointerData.markIdBit(id,
3967 current.rawPointerData.isHovering(
3968 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003969 usedIdBits.markBit(id);
3970
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003971 if (DEBUG_POINTER_ASSIGNMENT) {
3972 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3973 ", distance=%" PRIu64,
3974 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3975 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003976 break;
3977 }
3978 }
3979
3980 // Assign fresh ids to pointers that were not matched in the process.
3981 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3982 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3983 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3984
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003985 current.rawPointerData.pointers[currentPointerIndex].id = id;
3986 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3987 current.rawPointerData.markIdBit(id,
3988 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003989
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003990 if (DEBUG_POINTER_ASSIGNMENT) {
3991 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3992 id);
3993 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003994 }
3995}
3996
3997int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3998 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3999 return AKEY_STATE_VIRTUAL;
4000 }
4001
4002 for (const VirtualKey& virtualKey : mVirtualKeys) {
4003 if (virtualKey.keyCode == keyCode) {
4004 return AKEY_STATE_UP;
4005 }
4006 }
4007
4008 return AKEY_STATE_UNKNOWN;
4009}
4010
4011int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4012 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4013 return AKEY_STATE_VIRTUAL;
4014 }
4015
4016 for (const VirtualKey& virtualKey : mVirtualKeys) {
4017 if (virtualKey.scanCode == scanCode) {
4018 return AKEY_STATE_UP;
4019 }
4020 }
4021
4022 return AKEY_STATE_UNKNOWN;
4023}
4024
4025bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4026 const int32_t* keyCodes, uint8_t* outFlags) {
4027 for (const VirtualKey& virtualKey : mVirtualKeys) {
4028 for (size_t i = 0; i < numCodes; i++) {
4029 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