blob: f729ba958ac97882ed5452978c54c7757bafd33a [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.
553 * 1. If display port is specified, return the matching viewport. If matching viewport not
554 * found, then return.
Garfield Tan888a6a42020-01-09 11:39:16 -0800555 * 2. Always use the suggested viewport from WindowManagerService for pointers.
556 * 3. If a device has associated display, get the matching viewport by either unique id or by
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700557 * the display type (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800558 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700559 */
560std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800561 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800562 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700563 if (displayPort) {
564 // Find the viewport that contains the same port
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800565 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700566 }
567
Christine Franks2a2293c2022-01-18 11:51:16 -0800568 const std::optional<std::string> associatedDisplayUniqueId =
569 getDeviceContext().getAssociatedDisplayUniqueId();
570 if (associatedDisplayUniqueId) {
571 return getDeviceContext().getAssociatedViewport();
572 }
573
Michael Wright227c5542020-07-02 18:30:52 +0100574 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800575 std::optional<DisplayViewport> viewport =
576 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
577 if (viewport) {
578 return viewport;
579 } else {
580 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
581 mConfig.defaultPointerDisplayId);
582 }
583 }
584
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700585 // Check if uniqueDisplayId is specified in idc file.
586 if (!mParameters.uniqueDisplayId.empty()) {
587 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
588 }
589
590 ViewportType viewportTypeToUse;
591 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100592 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700593 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100594 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700595 }
596
597 std::optional<DisplayViewport> viewport =
598 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100599 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700600 ALOGW("Input device %s should be associated with external display, "
601 "fallback to internal one for the external viewport is not found.",
602 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100603 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700604 }
605
606 return viewport;
607 }
608
609 // No associated display, return a non-display viewport.
610 DisplayViewport newViewport;
611 // Raw width and height in the natural orientation.
612 int32_t rawWidth = mRawPointerAxes.getRawWidth();
613 int32_t rawHeight = mRawPointerAxes.getRawHeight();
614 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
615 return std::make_optional(newViewport);
616}
617
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800618int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
619 if (resolution < 0) {
620 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
621 getDeviceName().c_str());
622 return 0;
623 }
624 return resolution;
625}
626
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800627void TouchInputMapper::initializeSizeRanges() {
628 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
629 mSizeScale = 0.0f;
630 return;
631 }
632
633 // Size of diagonal axis.
634 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
635
636 // Size factors.
637 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
638 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
639 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
640 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
641 } else {
642 mSizeScale = 0.0f;
643 }
644
645 mOrientedRanges.haveTouchSize = true;
646 mOrientedRanges.haveToolSize = true;
647 mOrientedRanges.haveSize = true;
648
649 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
650 mOrientedRanges.touchMajor.source = mSource;
651 mOrientedRanges.touchMajor.min = 0;
652 mOrientedRanges.touchMajor.max = diagonalSize;
653 mOrientedRanges.touchMajor.flat = 0;
654 mOrientedRanges.touchMajor.fuzz = 0;
655 mOrientedRanges.touchMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800656 if (mRawPointerAxes.touchMajor.valid) {
657 mRawPointerAxes.touchMajor.resolution =
658 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
659 mOrientedRanges.touchMajor.resolution = mRawPointerAxes.touchMajor.resolution;
660 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800661
662 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
663 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800664 if (mRawPointerAxes.touchMinor.valid) {
665 mRawPointerAxes.touchMinor.resolution =
666 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
667 mOrientedRanges.touchMinor.resolution = mRawPointerAxes.touchMinor.resolution;
668 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800669
670 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
671 mOrientedRanges.toolMajor.source = mSource;
672 mOrientedRanges.toolMajor.min = 0;
673 mOrientedRanges.toolMajor.max = diagonalSize;
674 mOrientedRanges.toolMajor.flat = 0;
675 mOrientedRanges.toolMajor.fuzz = 0;
676 mOrientedRanges.toolMajor.resolution = 0;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800677 if (mRawPointerAxes.toolMajor.valid) {
678 mRawPointerAxes.toolMajor.resolution =
679 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
680 mOrientedRanges.toolMajor.resolution = mRawPointerAxes.toolMajor.resolution;
681 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800682
683 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
684 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800685 if (mRawPointerAxes.toolMinor.valid) {
686 mRawPointerAxes.toolMinor.resolution =
687 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
688 mOrientedRanges.toolMinor.resolution = mRawPointerAxes.toolMinor.resolution;
689 }
690
691 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
692 mOrientedRanges.touchMajor.resolution *= mGeometricScale;
693 mOrientedRanges.touchMinor.resolution *= mGeometricScale;
694 mOrientedRanges.toolMajor.resolution *= mGeometricScale;
695 mOrientedRanges.toolMinor.resolution *= mGeometricScale;
696 } else {
697 // Support for other calibrations can be added here.
698 ALOGW("%s calibration is not supported for size ranges at the moment. "
699 "Using raw resolution instead",
700 ftl::enum_string(mCalibration.sizeCalibration).c_str());
701 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800702
703 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
704 mOrientedRanges.size.source = mSource;
705 mOrientedRanges.size.min = 0;
706 mOrientedRanges.size.max = 1.0;
707 mOrientedRanges.size.flat = 0;
708 mOrientedRanges.size.fuzz = 0;
709 mOrientedRanges.size.resolution = 0;
710}
711
712void TouchInputMapper::initializeOrientedRanges() {
713 // Configure X and Y factors.
714 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
715 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
716 mXPrecision = 1.0f / mXScale;
717 mYPrecision = 1.0f / mYScale;
718
719 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
720 mOrientedRanges.x.source = mSource;
721 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
722 mOrientedRanges.y.source = mSource;
723
724 // Scale factor for terms that are not oriented in a particular axis.
725 // If the pixels are square then xScale == yScale otherwise we fake it
726 // by choosing an average.
727 mGeometricScale = avg(mXScale, mYScale);
728
729 initializeSizeRanges();
730
731 // Pressure factors.
732 mPressureScale = 0;
733 float pressureMax = 1.0;
734 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
735 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
736 if (mCalibration.havePressureScale) {
737 mPressureScale = mCalibration.pressureScale;
738 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
739 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
740 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
741 }
742 }
743
744 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
745 mOrientedRanges.pressure.source = mSource;
746 mOrientedRanges.pressure.min = 0;
747 mOrientedRanges.pressure.max = pressureMax;
748 mOrientedRanges.pressure.flat = 0;
749 mOrientedRanges.pressure.fuzz = 0;
750 mOrientedRanges.pressure.resolution = 0;
751
752 // Tilt
753 mTiltXCenter = 0;
754 mTiltXScale = 0;
755 mTiltYCenter = 0;
756 mTiltYScale = 0;
757 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
758 if (mHaveTilt) {
759 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
760 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
761 mTiltXScale = M_PI / 180;
762 mTiltYScale = M_PI / 180;
763
764 if (mRawPointerAxes.tiltX.resolution) {
765 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
766 }
767 if (mRawPointerAxes.tiltY.resolution) {
768 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
769 }
770
771 mOrientedRanges.haveTilt = true;
772
773 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
774 mOrientedRanges.tilt.source = mSource;
775 mOrientedRanges.tilt.min = 0;
776 mOrientedRanges.tilt.max = M_PI_2;
777 mOrientedRanges.tilt.flat = 0;
778 mOrientedRanges.tilt.fuzz = 0;
779 mOrientedRanges.tilt.resolution = 0;
780 }
781
782 // Orientation
783 mOrientationScale = 0;
784 if (mHaveTilt) {
785 mOrientedRanges.haveOrientation = true;
786
787 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
788 mOrientedRanges.orientation.source = mSource;
789 mOrientedRanges.orientation.min = -M_PI;
790 mOrientedRanges.orientation.max = M_PI;
791 mOrientedRanges.orientation.flat = 0;
792 mOrientedRanges.orientation.fuzz = 0;
793 mOrientedRanges.orientation.resolution = 0;
794 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
795 if (mCalibration.orientationCalibration ==
796 Calibration::OrientationCalibration::INTERPOLATED) {
797 if (mRawPointerAxes.orientation.valid) {
798 if (mRawPointerAxes.orientation.maxValue > 0) {
799 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
800 } else if (mRawPointerAxes.orientation.minValue < 0) {
801 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
802 } else {
803 mOrientationScale = 0;
804 }
805 }
806 }
807
808 mOrientedRanges.haveOrientation = true;
809
810 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
811 mOrientedRanges.orientation.source = mSource;
812 mOrientedRanges.orientation.min = -M_PI_2;
813 mOrientedRanges.orientation.max = M_PI_2;
814 mOrientedRanges.orientation.flat = 0;
815 mOrientedRanges.orientation.fuzz = 0;
816 mOrientedRanges.orientation.resolution = 0;
817 }
818
819 // Distance
820 mDistanceScale = 0;
821 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
822 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
823 if (mCalibration.haveDistanceScale) {
824 mDistanceScale = mCalibration.distanceScale;
825 } else {
826 mDistanceScale = 1.0f;
827 }
828 }
829
830 mOrientedRanges.haveDistance = true;
831
832 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
833 mOrientedRanges.distance.source = mSource;
834 mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
835 mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
836 mOrientedRanges.distance.flat = 0;
837 mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
838 mOrientedRanges.distance.resolution = 0;
839 }
840
841 // Compute oriented precision, scales and ranges.
842 // Note that the maximum value reported is an inclusive maximum value so it is one
843 // unit less than the total width or height of the display.
844 switch (mInputDeviceOrientation) {
845 case DISPLAY_ORIENTATION_90:
846 case DISPLAY_ORIENTATION_270:
847 mOrientedXPrecision = mYPrecision;
848 mOrientedYPrecision = mXPrecision;
849
850 mOrientedRanges.x.min = 0;
851 mOrientedRanges.x.max = mDisplayHeight - 1;
852 mOrientedRanges.x.flat = 0;
853 mOrientedRanges.x.fuzz = 0;
854 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
855
856 mOrientedRanges.y.min = 0;
857 mOrientedRanges.y.max = mDisplayWidth - 1;
858 mOrientedRanges.y.flat = 0;
859 mOrientedRanges.y.fuzz = 0;
860 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
861 break;
862
863 default:
864 mOrientedXPrecision = mXPrecision;
865 mOrientedYPrecision = mYPrecision;
866
867 mOrientedRanges.x.min = 0;
868 mOrientedRanges.x.max = mDisplayWidth - 1;
869 mOrientedRanges.x.flat = 0;
870 mOrientedRanges.x.fuzz = 0;
871 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
872
873 mOrientedRanges.y.min = 0;
874 mOrientedRanges.y.max = mDisplayHeight - 1;
875 mOrientedRanges.y.flat = 0;
876 mOrientedRanges.y.fuzz = 0;
877 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
878 break;
879 }
880}
881
Prabir Pradhan1728b212021-10-19 16:00:03 -0700882void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Michael Wright227c5542020-07-02 18:30:52 +0100883 DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700884
885 resolveExternalStylusPresence();
886
887 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100888 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000889 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700890 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100891 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700892 if (hasStylus()) {
893 mSource |= AINPUT_SOURCE_STYLUS;
894 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800895 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700896 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100897 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700898 if (hasStylus()) {
899 mSource |= AINPUT_SOURCE_STYLUS;
900 }
901 if (hasExternalStylus()) {
902 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
903 }
Michael Wright227c5542020-07-02 18:30:52 +0100904 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700905 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100906 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700907 } else {
908 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100909 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700910 }
911
912 // Ensure we have valid X and Y axes.
913 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
914 ALOGW("Touch device '%s' did not report support for X or Y axis! "
915 "The device will be inoperable.",
916 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100917 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700918 return;
919 }
920
921 // Get associated display dimensions.
922 std::optional<DisplayViewport> newViewport = findViewport();
923 if (!newViewport) {
924 ALOGI("Touch device '%s' could not query the properties of its associated "
925 "display. The device will be inoperable until the display size "
926 "becomes available.",
927 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100928 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700929 return;
930 }
931
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000932 if (!newViewport->isActive) {
933 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
934 getDeviceName().c_str(), getDeviceId());
935 mDeviceMode = DeviceMode::DISABLED;
936 return;
937 }
938
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700939 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700940 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
941 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700942
Prabir Pradhan1728b212021-10-19 16:00:03 -0700943 const bool viewportChanged = mViewport != *newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700944 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700945 if (viewportChanged) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700946 const bool viewportOrientationChanged = mViewport.orientation != newViewport->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700947 mViewport = *newViewport;
948
Michael Wright227c5542020-07-02 18:30:52 +0100949 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700950 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700951 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
952 int32_t naturalPhysicalLeft, naturalPhysicalTop;
953 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700954
Prabir Pradhan1728b212021-10-19 16:00:03 -0700955 // Apply the inverse of the input device orientation so that the input device is
956 // configured in the same orientation as the viewport. The input device orientation will
957 // be re-applied by mInputDeviceOrientation.
958 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700959 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700960 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700961 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700962 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
963 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800964 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700965 naturalPhysicalTop = mViewport.physicalLeft;
966 naturalDeviceWidth = mViewport.deviceHeight;
967 naturalDeviceHeight = mViewport.deviceWidth;
968 break;
969 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700970 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
971 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
972 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
973 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
974 naturalDeviceWidth = mViewport.deviceWidth;
975 naturalDeviceHeight = mViewport.deviceHeight;
976 break;
977 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700978 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
979 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
980 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800981 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700982 naturalDeviceWidth = mViewport.deviceHeight;
983 naturalDeviceHeight = mViewport.deviceWidth;
984 break;
985 case DISPLAY_ORIENTATION_0:
986 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700987 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
988 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
989 naturalPhysicalLeft = mViewport.physicalLeft;
990 naturalPhysicalTop = mViewport.physicalTop;
991 naturalDeviceWidth = mViewport.deviceWidth;
992 naturalDeviceHeight = mViewport.deviceHeight;
993 break;
994 }
995
996 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
997 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
998 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
999 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1000 }
1001
1002 mPhysicalWidth = naturalPhysicalWidth;
1003 mPhysicalHeight = naturalPhysicalHeight;
1004 mPhysicalLeft = naturalPhysicalLeft;
1005 mPhysicalTop = naturalPhysicalTop;
1006
Prabir Pradhan1728b212021-10-19 16:00:03 -07001007 const int32_t oldDisplayWidth = mDisplayWidth;
1008 const int32_t oldDisplayHeight = mDisplayHeight;
1009 mDisplayWidth = naturalDeviceWidth;
1010 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001011
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001012 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1013 // anything if the device is already orientation-aware. If the device is not
1014 // orientation-aware, then we need to apply the inverse rotation of the display so that
1015 // when the display rotation is applied later as a part of the per-window transform, we
1016 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001017 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001018 ? DISPLAY_ORIENTATION_0
1019 : getInverseRotation(mViewport.orientation);
1020 // For orientation-aware devices that work in the un-rotated coordinate space, the
1021 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001022 skipViewportUpdate = mParameters.orientationAware && mDisplayWidth == oldDisplayWidth &&
1023 mDisplayHeight == oldDisplayHeight && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001024
1025 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001026 mInputDeviceOrientation =
1027 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001028 } else {
1029 mPhysicalWidth = rawWidth;
1030 mPhysicalHeight = rawHeight;
1031 mPhysicalLeft = 0;
1032 mPhysicalTop = 0;
1033
Prabir Pradhan1728b212021-10-19 16:00:03 -07001034 mDisplayWidth = rawWidth;
1035 mDisplayHeight = rawHeight;
1036 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001037 }
1038 }
1039
1040 // If moving between pointer modes, need to reset some state.
1041 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1042 if (deviceModeChanged) {
1043 mOrientedRanges.clear();
1044 }
1045
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001046 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1047 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001048 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001049 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001050 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1051 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001052 if (mPointerController == nullptr) {
1053 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001054 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001055 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001056 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1057 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001058 } else {
Michael Wright17db18e2020-06-26 20:51:44 +01001059 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001060 }
1061
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001062 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001063 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1064 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001065 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1066 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001067
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001068 configureVirtualKeys();
1069
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001070 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001071
1072 // Location
1073 updateAffineTransformation();
1074
Michael Wright227c5542020-07-02 18:30:52 +01001075 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001076 // Compute pointer gesture detection parameters.
1077 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001078 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001079
1080 // Scale movements such that one whole swipe of the touch pad covers a
1081 // given area relative to the diagonal size of the display when no acceleration
1082 // is applied.
1083 // Assume that the touch pad has a square aspect ratio such that movements in
1084 // X and Y of the same number of raw units cover the same physical distance.
1085 mPointerXMovementScale =
1086 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1087 mPointerYMovementScale = mPointerXMovementScale;
1088
1089 // Scale zooms to cover a smaller range of the display than movements do.
1090 // This value determines the area around the pointer that is affected by freeform
1091 // pointer gestures.
1092 mPointerXZoomScale =
1093 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1094 mPointerYZoomScale = mPointerXZoomScale;
1095
1096 // Max width between pointers to detect a swipe gesture is more than some fraction
1097 // of the diagonal axis of the touch pad. Touches that are wider than this are
1098 // translated into freeform gestures.
1099 mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
1100
1101 // Abort current pointer usages because the state has changed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001102 const nsecs_t readTime = when; // synthetic event
1103 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001104 }
1105
1106 // Inform the dispatcher about the changes.
1107 *outResetNeeded = true;
1108 bumpGeneration();
1109 }
1110}
1111
Prabir Pradhan1728b212021-10-19 16:00:03 -07001112void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001113 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001114 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1115 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001116 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1117 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1118 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1119 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001120 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001121}
1122
1123void TouchInputMapper::configureVirtualKeys() {
1124 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001125 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001126
1127 mVirtualKeys.clear();
1128
1129 if (virtualKeyDefinitions.size() == 0) {
1130 return;
1131 }
1132
1133 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1134 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1135 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1136 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1137
1138 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1139 VirtualKey virtualKey;
1140
1141 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1142 int32_t keyCode;
1143 int32_t dummyKeyMetaState;
1144 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001145 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1146 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001147 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1148 continue; // drop the key
1149 }
1150
1151 virtualKey.keyCode = keyCode;
1152 virtualKey.flags = flags;
1153
1154 // convert the key definition's display coordinates into touch coordinates for a hit box
1155 int32_t halfWidth = virtualKeyDefinition.width / 2;
1156 int32_t halfHeight = virtualKeyDefinition.height / 2;
1157
1158 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001159 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001160 touchScreenLeft;
1161 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001162 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001163 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001164 virtualKey.hitTop =
1165 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001167 virtualKey.hitBottom =
1168 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001169 touchScreenTop;
1170 mVirtualKeys.push_back(virtualKey);
1171 }
1172}
1173
1174void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1175 if (!mVirtualKeys.empty()) {
1176 dump += INDENT3 "Virtual Keys:\n";
1177
1178 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1179 const VirtualKey& virtualKey = mVirtualKeys[i];
1180 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1181 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1182 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1183 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1184 }
1185 }
1186}
1187
1188void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001189 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001190 Calibration& out = mCalibration;
1191
1192 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001193 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001194 String8 sizeCalibrationString;
1195 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
1196 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001197 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001198 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001199 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001200 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001201 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001202 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001203 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001204 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001205 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001206 } else if (sizeCalibrationString != "default") {
1207 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string());
1208 }
1209 }
1210
1211 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale);
1212 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias);
1213 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed);
1214
1215 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001216 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 String8 pressureCalibrationString;
1218 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
1219 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001220 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001221 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001222 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001223 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001224 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001225 } else if (pressureCalibrationString != "default") {
1226 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
1227 pressureCalibrationString.string());
1228 }
1229 }
1230
1231 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale);
1232
1233 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001234 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001235 String8 orientationCalibrationString;
1236 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
1237 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001238 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001239 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001240 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001241 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001242 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001243 } else if (orientationCalibrationString != "default") {
1244 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
1245 orientationCalibrationString.string());
1246 }
1247 }
1248
1249 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001250 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001251 String8 distanceCalibrationString;
1252 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
1253 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001254 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001255 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001256 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001257 } else if (distanceCalibrationString != "default") {
1258 ALOGW("Invalid value for touch.distance.calibration: '%s'",
1259 distanceCalibrationString.string());
1260 }
1261 }
1262
1263 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale);
1264
Michael Wright227c5542020-07-02 18:30:52 +01001265 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001266 String8 coverageCalibrationString;
1267 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
1268 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001269 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001270 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001271 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001272 } else if (coverageCalibrationString != "default") {
1273 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
1274 coverageCalibrationString.string());
1275 }
1276 }
1277}
1278
1279void TouchInputMapper::resolveCalibration() {
1280 // Size
1281 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001282 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1283 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001284 }
1285 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001286 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001287 }
1288
1289 // Pressure
1290 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001291 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1292 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001293 }
1294 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001295 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001296 }
1297
1298 // Orientation
1299 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001300 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1301 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001302 }
1303 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001304 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001305 }
1306
1307 // Distance
1308 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001309 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1310 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001311 }
1312 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001313 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001314 }
1315
1316 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001317 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1318 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001319 }
1320}
1321
1322void TouchInputMapper::dumpCalibration(std::string& dump) {
1323 dump += INDENT3 "Calibration:\n";
1324
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001325 dump += INDENT4 "touch.size.calibration: ";
1326 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001327
1328 if (mCalibration.haveSizeScale) {
1329 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
1330 }
1331
1332 if (mCalibration.haveSizeBias) {
1333 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
1334 }
1335
1336 if (mCalibration.haveSizeIsSummed) {
1337 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
1338 toString(mCalibration.sizeIsSummed));
1339 }
1340
1341 // Pressure
1342 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001343 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001344 dump += INDENT4 "touch.pressure.calibration: none\n";
1345 break;
Michael Wright227c5542020-07-02 18:30:52 +01001346 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001347 dump += INDENT4 "touch.pressure.calibration: physical\n";
1348 break;
Michael Wright227c5542020-07-02 18:30:52 +01001349 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001350 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1351 break;
1352 default:
1353 ALOG_ASSERT(false);
1354 }
1355
1356 if (mCalibration.havePressureScale) {
1357 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
1358 }
1359
1360 // Orientation
1361 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001362 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001363 dump += INDENT4 "touch.orientation.calibration: none\n";
1364 break;
Michael Wright227c5542020-07-02 18:30:52 +01001365 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001366 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1367 break;
Michael Wright227c5542020-07-02 18:30:52 +01001368 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001369 dump += INDENT4 "touch.orientation.calibration: vector\n";
1370 break;
1371 default:
1372 ALOG_ASSERT(false);
1373 }
1374
1375 // Distance
1376 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001377 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001378 dump += INDENT4 "touch.distance.calibration: none\n";
1379 break;
Michael Wright227c5542020-07-02 18:30:52 +01001380 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001381 dump += INDENT4 "touch.distance.calibration: scaled\n";
1382 break;
1383 default:
1384 ALOG_ASSERT(false);
1385 }
1386
1387 if (mCalibration.haveDistanceScale) {
1388 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
1389 }
1390
1391 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001392 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001393 dump += INDENT4 "touch.coverage.calibration: none\n";
1394 break;
Michael Wright227c5542020-07-02 18:30:52 +01001395 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001396 dump += INDENT4 "touch.coverage.calibration: box\n";
1397 break;
1398 default:
1399 ALOG_ASSERT(false);
1400 }
1401}
1402
1403void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1404 dump += INDENT3 "Affine Transformation:\n";
1405
1406 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1407 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1408 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1409 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1410 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1411 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1412}
1413
1414void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001415 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001416 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001417}
1418
1419void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001420 mCursorButtonAccumulator.reset(getDeviceContext());
1421 mCursorScrollAccumulator.reset(getDeviceContext());
1422 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001423
1424 mPointerVelocityControl.reset();
1425 mWheelXVelocityControl.reset();
1426 mWheelYVelocityControl.reset();
1427
1428 mRawStatesPending.clear();
1429 mCurrentRawState.clear();
1430 mCurrentCookedState.clear();
1431 mLastRawState.clear();
1432 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001433 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001434 mSentHoverEnter = false;
1435 mHavePointerIds = false;
1436 mCurrentMotionAborted = false;
1437 mDownTime = 0;
1438
1439 mCurrentVirtualKey.down = false;
1440
1441 mPointerGesture.reset();
1442 mPointerSimple.reset();
1443 resetExternalStylus();
1444
1445 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001446 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001447 mPointerController->clearSpots();
1448 }
1449
1450 InputMapper::reset(when);
1451}
1452
1453void TouchInputMapper::resetExternalStylus() {
1454 mExternalStylusState.clear();
1455 mExternalStylusId = -1;
1456 mExternalStylusFusionTimeout = LLONG_MAX;
1457 mExternalStylusDataPending = false;
1458}
1459
1460void TouchInputMapper::clearStylusDataPendingFlags() {
1461 mExternalStylusDataPending = false;
1462 mExternalStylusFusionTimeout = LLONG_MAX;
1463}
1464
1465void TouchInputMapper::process(const RawEvent* rawEvent) {
1466 mCursorButtonAccumulator.process(rawEvent);
1467 mCursorScrollAccumulator.process(rawEvent);
1468 mTouchButtonAccumulator.process(rawEvent);
1469
1470 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001471 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001472 }
1473}
1474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001475void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001476 // Push a new state.
1477 mRawStatesPending.emplace_back();
1478
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001479 RawState& next = mRawStatesPending.back();
1480 next.clear();
1481 next.when = when;
1482 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001483
1484 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001485 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001486 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1487
1488 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001489 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1490 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001491 mCursorScrollAccumulator.finishSync();
1492
1493 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001494 syncTouch(when, &next);
1495
1496 // The last RawState is the actually second to last, since we just added a new state
1497 const RawState& last =
1498 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001499
1500 // Assign pointer ids.
1501 if (!mHavePointerIds) {
1502 assignPointerIds(last, next);
1503 }
1504
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001505 if (DEBUG_RAW_EVENTS) {
1506 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1507 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1508 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1509 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1510 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1511 next.rawPointerData.canceledIdBits.value);
1512 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001513
Arthur Hung9ad18942021-06-19 02:04:46 +00001514 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1515 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1516 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1517 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1518 next.rawPointerData.hoveringIdBits.value);
1519 }
1520
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001521 processRawTouches(false /*timeout*/);
1522}
1523
1524void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001525 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001526 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001527 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001528 mCurrentCookedState.clear();
1529 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001530 return;
1531 }
1532
1533 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1534 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1535 // touching the current state will only observe the events that have been dispatched to the
1536 // rest of the pipeline.
1537 const size_t N = mRawStatesPending.size();
1538 size_t count;
1539 for (count = 0; count < N; count++) {
1540 const RawState& next = mRawStatesPending[count];
1541
1542 // A failure to assign the stylus id means that we're waiting on stylus data
1543 // and so should defer the rest of the pipeline.
1544 if (assignExternalStylusId(next, timeout)) {
1545 break;
1546 }
1547
1548 // All ready to go.
1549 clearStylusDataPendingFlags();
1550 mCurrentRawState.copyFrom(next);
1551 if (mCurrentRawState.when < mLastRawState.when) {
1552 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001553 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001554 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001555 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001556 }
1557 if (count != 0) {
1558 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1559 }
1560
1561 if (mExternalStylusDataPending) {
1562 if (timeout) {
1563 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1564 clearStylusDataPendingFlags();
1565 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001566 if (DEBUG_STYLUS_FUSION) {
1567 ALOGD("Timeout expired, synthesizing event with new stylus data");
1568 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001569 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1570 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001571 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1572 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1573 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1574 }
1575 }
1576}
1577
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001578void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001579 // Always start with a clean state.
1580 mCurrentCookedState.clear();
1581
1582 // Apply stylus buttons to current raw state.
1583 applyExternalStylusButtonState(when);
1584
1585 // Handle policy on initial down or hover events.
1586 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1587 mCurrentRawState.rawPointerData.pointerCount != 0;
1588
1589 uint32_t policyFlags = 0;
1590 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1591 if (initialDown || buttonsPressed) {
1592 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001593 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001594 getContext()->fadePointer();
1595 }
1596
1597 if (mParameters.wake) {
1598 policyFlags |= POLICY_FLAG_WAKE;
1599 }
1600 }
1601
1602 // Consume raw off-screen touches before cooking pointer data.
1603 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001604 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001605 mCurrentRawState.rawPointerData.clear();
1606 }
1607
1608 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1609 // with cooked pointer data that has the same ids and indices as the raw data.
1610 // The following code can use either the raw or cooked data, as needed.
1611 cookPointerData();
1612
1613 // Apply stylus pressure to current cooked state.
1614 applyExternalStylusTouchState(when);
1615
1616 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001617 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1618 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001619 mCurrentCookedState.buttonState);
1620
1621 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001622 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001623 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1624 uint32_t id = idBits.clearFirstMarkedBit();
1625 const RawPointerData::Pointer& pointer =
1626 mCurrentRawState.rawPointerData.pointerForId(id);
1627 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1628 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1629 mCurrentCookedState.stylusIdBits.markBit(id);
1630 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1631 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1632 mCurrentCookedState.fingerIdBits.markBit(id);
1633 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1634 mCurrentCookedState.mouseIdBits.markBit(id);
1635 }
1636 }
1637 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1638 uint32_t id = idBits.clearFirstMarkedBit();
1639 const RawPointerData::Pointer& pointer =
1640 mCurrentRawState.rawPointerData.pointerForId(id);
1641 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1642 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1643 mCurrentCookedState.stylusIdBits.markBit(id);
1644 }
1645 }
1646
1647 // Stylus takes precedence over all tools, then mouse, then finger.
1648 PointerUsage pointerUsage = mPointerUsage;
1649 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1650 mCurrentCookedState.mouseIdBits.clear();
1651 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001652 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001653 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1654 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001655 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1657 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001658 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001659 }
1660
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001661 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001662 } else {
Garfield Tanc734e4f2021-01-15 20:01:39 -08001663 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001664
1665 if (!mCurrentMotionAborted) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001666 dispatchButtonRelease(when, readTime, policyFlags);
1667 dispatchHoverExit(when, readTime, policyFlags);
1668 dispatchTouches(when, readTime, policyFlags);
1669 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1670 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001671 }
1672
1673 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1674 mCurrentMotionAborted = false;
1675 }
1676 }
1677
1678 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001679 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001680 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1681 mCurrentCookedState.buttonState);
1682
1683 // Clear some transient state.
1684 mCurrentRawState.rawVScroll = 0;
1685 mCurrentRawState.rawHScroll = 0;
1686
1687 // Copy current touch to last touch in preparation for the next cycle.
1688 mLastRawState.copyFrom(mCurrentRawState);
1689 mLastCookedState.copyFrom(mCurrentCookedState);
1690}
1691
Garfield Tanc734e4f2021-01-15 20:01:39 -08001692void TouchInputMapper::updateTouchSpots() {
1693 if (!mConfig.showTouches || mPointerController == nullptr) {
1694 return;
1695 }
1696
1697 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1698 // clear touch spots.
1699 if (mDeviceMode != DeviceMode::DIRECT &&
1700 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1701 return;
1702 }
1703
1704 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1705 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1706
1707 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001708 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1709 mCurrentCookedState.cookedPointerData.idToIndex,
1710 mCurrentCookedState.cookedPointerData.touchingIdBits,
1711 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001712}
1713
1714bool TouchInputMapper::isTouchScreen() {
1715 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1716 mParameters.hasAssociatedDisplay;
1717}
1718
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001719void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001720 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001721 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1722 }
1723}
1724
1725void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1726 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1727 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1728
1729 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1730 float pressure = mExternalStylusState.pressure;
1731 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1732 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1733 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1734 }
1735 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1736 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1737
1738 PointerProperties& properties =
1739 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1740 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1741 properties.toolType = mExternalStylusState.toolType;
1742 }
1743 }
1744}
1745
1746bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001747 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001748 return false;
1749 }
1750
1751 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1752 state.rawPointerData.pointerCount != 0;
1753 if (initialDown) {
1754 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001755 if (DEBUG_STYLUS_FUSION) {
1756 ALOGD("Have both stylus and touch data, beginning fusion");
1757 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001758 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1759 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001760 if (DEBUG_STYLUS_FUSION) {
1761 ALOGD("Timeout expired, assuming touch is not a stylus.");
1762 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001763 resetExternalStylus();
1764 } else {
1765 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1766 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1767 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001768 if (DEBUG_STYLUS_FUSION) {
1769 ALOGD("No stylus data but stylus is connected, requesting timeout "
1770 "(%" PRId64 "ms)",
1771 mExternalStylusFusionTimeout);
1772 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001773 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1774 return true;
1775 }
1776 }
1777
1778 // Check if the stylus pointer has gone up.
1779 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001780 if (DEBUG_STYLUS_FUSION) {
1781 ALOGD("Stylus pointer is going up");
1782 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001783 mExternalStylusId = -1;
1784 }
1785
1786 return false;
1787}
1788
1789void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001790 if (mDeviceMode == DeviceMode::POINTER) {
1791 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001792 // Since this is a synthetic event, we can consider its latency to be zero
1793 const nsecs_t readTime = when;
1794 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001795 }
Michael Wright227c5542020-07-02 18:30:52 +01001796 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001797 if (mExternalStylusFusionTimeout < when) {
1798 processRawTouches(true /*timeout*/);
1799 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1800 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1801 }
1802 }
1803}
1804
1805void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1806 mExternalStylusState.copyFrom(state);
1807 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1808 // We're either in the middle of a fused stream of data or we're waiting on data before
1809 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1810 // data.
1811 mExternalStylusDataPending = true;
1812 processRawTouches(false /*timeout*/);
1813 }
1814}
1815
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001816bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001817 // Check for release of a virtual key.
1818 if (mCurrentVirtualKey.down) {
1819 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1820 // Pointer went up while virtual key was down.
1821 mCurrentVirtualKey.down = false;
1822 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001823 if (DEBUG_VIRTUAL_KEYS) {
1824 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1825 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1826 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001827 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001828 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1829 }
1830 return true;
1831 }
1832
1833 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1834 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1835 const RawPointerData::Pointer& pointer =
1836 mCurrentRawState.rawPointerData.pointerForId(id);
1837 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1838 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1839 // Pointer is still within the space of the virtual key.
1840 return true;
1841 }
1842 }
1843
1844 // Pointer left virtual key area or another pointer also went down.
1845 // Send key cancellation but do not consume the touch yet.
1846 // This is useful when the user swipes through from the virtual key area
1847 // into the main display surface.
1848 mCurrentVirtualKey.down = false;
1849 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001850 if (DEBUG_VIRTUAL_KEYS) {
1851 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1852 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1853 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001854 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001855 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1856 AKEY_EVENT_FLAG_CANCELED);
1857 }
1858 }
1859
1860 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1861 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1862 // Pointer just went down. Check for virtual key press or off-screen touches.
1863 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1864 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001865 // Skip checking whether the pointer is inside the physical frame if the device is in
1866 // unscaled mode.
1867 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1868 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001869 // If exactly one pointer went down, check for virtual key hit.
1870 // Otherwise we will drop the entire stroke.
1871 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1872 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1873 if (virtualKey) {
1874 mCurrentVirtualKey.down = true;
1875 mCurrentVirtualKey.downTime = when;
1876 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1877 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1878 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001879 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1880 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001881
1882 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001883 if (DEBUG_VIRTUAL_KEYS) {
1884 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1885 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1886 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001887 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001888 AKEY_EVENT_FLAG_FROM_SYSTEM |
1889 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1890 }
1891 }
1892 }
1893 return true;
1894 }
1895 }
1896
1897 // Disable all virtual key touches that happen within a short time interval of the
1898 // most recent touch within the screen area. The idea is to filter out stray
1899 // virtual key presses when interacting with the touch screen.
1900 //
1901 // Problems we're trying to solve:
1902 //
1903 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1904 // virtual key area that is implemented by a separate touch panel and accidentally
1905 // triggers a virtual key.
1906 //
1907 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1908 // area and accidentally triggers a virtual key. This often happens when virtual keys
1909 // are layed out below the screen near to where the on screen keyboard's space bar
1910 // is displayed.
1911 if (mConfig.virtualKeyQuietTime > 0 &&
1912 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001913 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001914 }
1915 return false;
1916}
1917
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001918void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001919 int32_t keyEventAction, int32_t keyEventFlags) {
1920 int32_t keyCode = mCurrentVirtualKey.keyCode;
1921 int32_t scanCode = mCurrentVirtualKey.scanCode;
1922 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001923 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001924 policyFlags |= POLICY_FLAG_VIRTUAL;
1925
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001926 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1927 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1928 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001929 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001930}
1931
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001932void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001933 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1934 if (!currentIdBits.isEmpty()) {
1935 int32_t metaState = getContext()->getGlobalMetaState();
1936 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001937 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1938 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001939 mCurrentCookedState.cookedPointerData.pointerProperties,
1940 mCurrentCookedState.cookedPointerData.pointerCoords,
1941 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1942 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1943 mCurrentMotionAborted = true;
1944 }
1945}
1946
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001947void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001948 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1949 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1950 int32_t metaState = getContext()->getGlobalMetaState();
1951 int32_t buttonState = mCurrentCookedState.buttonState;
1952
1953 if (currentIdBits == lastIdBits) {
1954 if (!currentIdBits.isEmpty()) {
1955 // No pointer id changes so this is a move event.
1956 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001957 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1958 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001959 mCurrentCookedState.cookedPointerData.pointerProperties,
1960 mCurrentCookedState.cookedPointerData.pointerCoords,
1961 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1962 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1963 }
1964 } else {
1965 // There may be pointers going up and pointers going down and pointers moving
1966 // all at the same time.
1967 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1968 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1969 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1970 BitSet32 dispatchedIdBits(lastIdBits.value);
1971
1972 // Update last coordinates of pointers that have moved so that we observe the new
1973 // pointer positions at the same time as other pointers that have just gone up.
1974 bool moveNeeded =
1975 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1976 mCurrentCookedState.cookedPointerData.pointerCoords,
1977 mCurrentCookedState.cookedPointerData.idToIndex,
1978 mLastCookedState.cookedPointerData.pointerProperties,
1979 mLastCookedState.cookedPointerData.pointerCoords,
1980 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1981 if (buttonState != mLastCookedState.buttonState) {
1982 moveNeeded = true;
1983 }
1984
1985 // Dispatch pointer up events.
1986 while (!upIdBits.isEmpty()) {
1987 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001988 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001989 if (isCanceled) {
1990 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1991 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001992 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08001993 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001994 mLastCookedState.cookedPointerData.pointerProperties,
1995 mLastCookedState.cookedPointerData.pointerCoords,
1996 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
1997 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1998 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08001999 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002000 }
2001
2002 // Dispatch move events if any of the remaining pointers moved from their old locations.
2003 // Although applications receive new locations as part of individual pointer up
2004 // events, they do not generally handle them except when presented in a move event.
2005 if (moveNeeded && !moveIdBits.isEmpty()) {
2006 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002007 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2008 metaState, buttonState, 0,
2009 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002010 mCurrentCookedState.cookedPointerData.pointerCoords,
2011 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2012 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2013 }
2014
2015 // Dispatch pointer down events using the new pointer locations.
2016 while (!downIdBits.isEmpty()) {
2017 uint32_t downId = downIdBits.clearFirstMarkedBit();
2018 dispatchedIdBits.markBit(downId);
2019
2020 if (dispatchedIdBits.count() == 1) {
2021 // First pointer is going down. Set down time.
2022 mDownTime = when;
2023 }
2024
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002025 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2026 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002027 mCurrentCookedState.cookedPointerData.pointerProperties,
2028 mCurrentCookedState.cookedPointerData.pointerCoords,
2029 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2030 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2031 }
2032 }
2033}
2034
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002035void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002036 if (mSentHoverEnter &&
2037 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2038 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2039 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002040 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2041 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002042 mLastCookedState.cookedPointerData.pointerProperties,
2043 mLastCookedState.cookedPointerData.pointerCoords,
2044 mLastCookedState.cookedPointerData.idToIndex,
2045 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2046 mOrientedYPrecision, mDownTime);
2047 mSentHoverEnter = false;
2048 }
2049}
2050
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002051void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2052 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002053 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2054 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2055 int32_t metaState = getContext()->getGlobalMetaState();
2056 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002057 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2058 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002059 mCurrentCookedState.cookedPointerData.pointerProperties,
2060 mCurrentCookedState.cookedPointerData.pointerCoords,
2061 mCurrentCookedState.cookedPointerData.idToIndex,
2062 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2063 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2064 mSentHoverEnter = true;
2065 }
2066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002067 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2068 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002069 mCurrentCookedState.cookedPointerData.pointerProperties,
2070 mCurrentCookedState.cookedPointerData.pointerCoords,
2071 mCurrentCookedState.cookedPointerData.idToIndex,
2072 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2073 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2074 }
2075}
2076
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002077void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002078 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2079 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2080 const int32_t metaState = getContext()->getGlobalMetaState();
2081 int32_t buttonState = mLastCookedState.buttonState;
2082 while (!releasedButtons.isEmpty()) {
2083 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2084 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002085 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002086 actionButton, 0, metaState, buttonState, 0,
2087 mCurrentCookedState.cookedPointerData.pointerProperties,
2088 mCurrentCookedState.cookedPointerData.pointerCoords,
2089 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2090 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2091 }
2092}
2093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002094void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002095 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2096 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2097 const int32_t metaState = getContext()->getGlobalMetaState();
2098 int32_t buttonState = mLastCookedState.buttonState;
2099 while (!pressedButtons.isEmpty()) {
2100 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2101 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002102 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2103 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002104 mCurrentCookedState.cookedPointerData.pointerProperties,
2105 mCurrentCookedState.cookedPointerData.pointerCoords,
2106 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2107 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2108 }
2109}
2110
2111const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2112 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2113 return cookedPointerData.touchingIdBits;
2114 }
2115 return cookedPointerData.hoveringIdBits;
2116}
2117
2118void TouchInputMapper::cookPointerData() {
2119 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2120
2121 mCurrentCookedState.cookedPointerData.clear();
2122 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2123 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2124 mCurrentRawState.rawPointerData.hoveringIdBits;
2125 mCurrentCookedState.cookedPointerData.touchingIdBits =
2126 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002127 mCurrentCookedState.cookedPointerData.canceledIdBits =
2128 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002129
2130 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2131 mCurrentCookedState.buttonState = 0;
2132 } else {
2133 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2134 }
2135
2136 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002137 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002138 for (uint32_t i = 0; i < currentPointerCount; i++) {
2139 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2140
2141 // Size
2142 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2143 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002144 case Calibration::SizeCalibration::GEOMETRIC:
2145 case Calibration::SizeCalibration::DIAMETER:
2146 case Calibration::SizeCalibration::BOX:
2147 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002148 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2149 touchMajor = in.touchMajor;
2150 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2151 toolMajor = in.toolMajor;
2152 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2153 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2154 : in.touchMajor;
2155 } else if (mRawPointerAxes.touchMajor.valid) {
2156 toolMajor = touchMajor = in.touchMajor;
2157 toolMinor = touchMinor =
2158 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2159 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2160 : in.touchMajor;
2161 } else if (mRawPointerAxes.toolMajor.valid) {
2162 touchMajor = toolMajor = in.toolMajor;
2163 touchMinor = toolMinor =
2164 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2165 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2166 : in.toolMajor;
2167 } else {
2168 ALOG_ASSERT(false,
2169 "No touch or tool axes. "
2170 "Size calibration should have been resolved to NONE.");
2171 touchMajor = 0;
2172 touchMinor = 0;
2173 toolMajor = 0;
2174 toolMinor = 0;
2175 size = 0;
2176 }
2177
2178 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
2179 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2180 if (touchingCount > 1) {
2181 touchMajor /= touchingCount;
2182 touchMinor /= touchingCount;
2183 toolMajor /= touchingCount;
2184 toolMinor /= touchingCount;
2185 size /= touchingCount;
2186 }
2187 }
2188
Michael Wright227c5542020-07-02 18:30:52 +01002189 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002190 touchMajor *= mGeometricScale;
2191 touchMinor *= mGeometricScale;
2192 toolMajor *= mGeometricScale;
2193 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002194 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002195 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2196 touchMinor = touchMajor;
2197 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2198 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002199 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002200 touchMinor = touchMajor;
2201 toolMinor = toolMajor;
2202 }
2203
2204 mCalibration.applySizeScaleAndBias(&touchMajor);
2205 mCalibration.applySizeScaleAndBias(&touchMinor);
2206 mCalibration.applySizeScaleAndBias(&toolMajor);
2207 mCalibration.applySizeScaleAndBias(&toolMinor);
2208 size *= mSizeScale;
2209 break;
2210 default:
2211 touchMajor = 0;
2212 touchMinor = 0;
2213 toolMajor = 0;
2214 toolMinor = 0;
2215 size = 0;
2216 break;
2217 }
2218
2219 // Pressure
2220 float pressure;
2221 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002222 case Calibration::PressureCalibration::PHYSICAL:
2223 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002224 pressure = in.pressure * mPressureScale;
2225 break;
2226 default:
2227 pressure = in.isHovering ? 0 : 1;
2228 break;
2229 }
2230
2231 // Tilt and Orientation
2232 float tilt;
2233 float orientation;
2234 if (mHaveTilt) {
2235 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2236 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2237 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2238 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2239 } else {
2240 tilt = 0;
2241
2242 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002243 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002244 orientation = in.orientation * mOrientationScale;
2245 break;
Michael Wright227c5542020-07-02 18:30:52 +01002246 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002247 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2248 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2249 if (c1 != 0 || c2 != 0) {
2250 orientation = atan2f(c1, c2) * 0.5f;
2251 float confidence = hypotf(c1, c2);
2252 float scale = 1.0f + confidence / 16.0f;
2253 touchMajor *= scale;
2254 touchMinor /= scale;
2255 toolMajor *= scale;
2256 toolMinor /= scale;
2257 } else {
2258 orientation = 0;
2259 }
2260 break;
2261 }
2262 default:
2263 orientation = 0;
2264 }
2265 }
2266
2267 // Distance
2268 float distance;
2269 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002270 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002271 distance = in.distance * mDistanceScale;
2272 break;
2273 default:
2274 distance = 0;
2275 }
2276
2277 // Coverage
2278 int32_t rawLeft, rawTop, rawRight, rawBottom;
2279 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002280 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002281 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2282 rawRight = in.toolMinor & 0x0000ffff;
2283 rawBottom = in.toolMajor & 0x0000ffff;
2284 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2285 break;
2286 default:
2287 rawLeft = rawTop = rawRight = rawBottom = 0;
2288 break;
2289 }
2290
2291 // Adjust X,Y coords for device calibration
2292 // TODO: Adjust coverage coords?
2293 float xTransformed = in.x, yTransformed = in.y;
2294 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002295 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002296
Prabir Pradhan1728b212021-10-19 16:00:03 -07002297 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002298 float left, top, right, bottom;
2299
Prabir Pradhan1728b212021-10-19 16:00:03 -07002300 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002301 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002302 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2303 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2304 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2305 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002306 orientation -= M_PI_2;
2307 if (mOrientedRanges.haveOrientation &&
2308 orientation < mOrientedRanges.orientation.min) {
2309 orientation +=
2310 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2311 }
2312 break;
2313 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002314 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2315 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002316 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2317 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002318 orientation -= M_PI;
2319 if (mOrientedRanges.haveOrientation &&
2320 orientation < mOrientedRanges.orientation.min) {
2321 orientation +=
2322 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2323 }
2324 break;
2325 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002326 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2327 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002328 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2329 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002330 orientation += M_PI_2;
2331 if (mOrientedRanges.haveOrientation &&
2332 orientation > mOrientedRanges.orientation.max) {
2333 orientation -=
2334 (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
2335 }
2336 break;
2337 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002338 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2339 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2340 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2341 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002342 break;
2343 }
2344
2345 // Write output coords.
2346 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2347 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002348 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2349 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002350 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2351 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2352 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2353 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2354 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2355 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2356 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002357 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002358 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2359 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2360 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2361 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2362 } else {
2363 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2364 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2365 }
2366
Chris Ye364fdb52020-08-05 15:07:56 -07002367 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002368 uint32_t id = in.id;
2369 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2370 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2371 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2372 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2373 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2374 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2375 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2376 }
2377
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002378 // Write output properties.
2379 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002380 properties.clear();
2381 properties.id = id;
2382 properties.toolType = in.toolType;
2383
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002384 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002385 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002386 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002387 }
2388}
2389
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002390void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002391 PointerUsage pointerUsage) {
2392 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002393 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002394 mPointerUsage = pointerUsage;
2395 }
2396
2397 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002398 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002399 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002400 break;
Michael Wright227c5542020-07-02 18:30:52 +01002401 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002402 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002403 break;
Michael Wright227c5542020-07-02 18:30:52 +01002404 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002405 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002406 break;
Michael Wright227c5542020-07-02 18:30:52 +01002407 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002408 break;
2409 }
2410}
2411
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002412void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002413 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002414 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002415 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002416 break;
Michael Wright227c5542020-07-02 18:30:52 +01002417 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002418 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002419 break;
Michael Wright227c5542020-07-02 18:30:52 +01002420 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002421 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002422 break;
Michael Wright227c5542020-07-02 18:30:52 +01002423 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002424 break;
2425 }
2426
Michael Wright227c5542020-07-02 18:30:52 +01002427 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002428}
2429
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002430void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2431 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002432 // Update current gesture coordinates.
2433 bool cancelPreviousGesture, finishPreviousGesture;
2434 bool sendEvents =
2435 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2436 if (!sendEvents) {
2437 return;
2438 }
2439 if (finishPreviousGesture) {
2440 cancelPreviousGesture = false;
2441 }
2442
2443 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002444 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002445 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002446 if (finishPreviousGesture || cancelPreviousGesture) {
2447 mPointerController->clearSpots();
2448 }
2449
Michael Wright227c5542020-07-02 18:30:52 +01002450 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002451 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2452 mPointerGesture.currentGestureIdToIndex,
2453 mPointerGesture.currentGestureIdBits,
2454 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002455 }
2456 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002457 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002458 }
2459
2460 // Show or hide the pointer if needed.
2461 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002462 case PointerGesture::Mode::NEUTRAL:
2463 case PointerGesture::Mode::QUIET:
2464 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2465 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002466 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002467 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002468 }
2469 break;
Michael Wright227c5542020-07-02 18:30:52 +01002470 case PointerGesture::Mode::TAP:
2471 case PointerGesture::Mode::TAP_DRAG:
2472 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2473 case PointerGesture::Mode::HOVER:
2474 case PointerGesture::Mode::PRESS:
2475 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002476 // Unfade the pointer when the current gesture manipulates the
2477 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002478 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002479 break;
Michael Wright227c5542020-07-02 18:30:52 +01002480 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002481 // Fade the pointer when the current gesture manipulates a different
2482 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002483 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002484 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002485 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002486 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002487 }
2488 break;
2489 }
2490
2491 // Send events!
2492 int32_t metaState = getContext()->getGlobalMetaState();
2493 int32_t buttonState = mCurrentCookedState.buttonState;
2494
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002495 uint32_t flags = 0;
2496
2497 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2498 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2499 }
2500
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002501 // Update last coordinates of pointers that have moved so that we observe the new
2502 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002503 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2504 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2505 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2506 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2507 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2508 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002509 bool moveNeeded = false;
2510 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2511 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2512 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2513 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2514 mPointerGesture.lastGestureIdBits.value);
2515 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2516 mPointerGesture.currentGestureCoords,
2517 mPointerGesture.currentGestureIdToIndex,
2518 mPointerGesture.lastGestureProperties,
2519 mPointerGesture.lastGestureCoords,
2520 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2521 if (buttonState != mLastCookedState.buttonState) {
2522 moveNeeded = true;
2523 }
2524 }
2525
2526 // Send motion events for all pointers that went up or were canceled.
2527 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2528 if (!dispatchedGestureIdBits.isEmpty()) {
2529 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002530 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2531 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002532 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2533 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2534 mPointerGesture.downTime);
2535
2536 dispatchedGestureIdBits.clear();
2537 } else {
2538 BitSet32 upGestureIdBits;
2539 if (finishPreviousGesture) {
2540 upGestureIdBits = dispatchedGestureIdBits;
2541 } else {
2542 upGestureIdBits.value =
2543 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2544 }
2545 while (!upGestureIdBits.isEmpty()) {
2546 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2547
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002548 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002549 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002550 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002551 mPointerGesture.lastGestureCoords,
2552 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2553 0, mPointerGesture.downTime);
2554
2555 dispatchedGestureIdBits.clearBit(id);
2556 }
2557 }
2558 }
2559
2560 // Send motion events for all pointers that moved.
2561 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002562 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002563 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002564 mPointerGesture.currentGestureProperties,
2565 mPointerGesture.currentGestureCoords,
2566 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2567 mPointerGesture.downTime);
2568 }
2569
2570 // Send motion events for all pointers that went down.
2571 if (down) {
2572 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2573 ~dispatchedGestureIdBits.value);
2574 while (!downGestureIdBits.isEmpty()) {
2575 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2576 dispatchedGestureIdBits.markBit(id);
2577
2578 if (dispatchedGestureIdBits.count() == 1) {
2579 mPointerGesture.downTime = when;
2580 }
2581
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002582 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002583 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002584 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002585 mPointerGesture.currentGestureCoords,
2586 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2587 0, mPointerGesture.downTime);
2588 }
2589 }
2590
2591 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002592 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002593 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2594 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002595 mPointerGesture.currentGestureProperties,
2596 mPointerGesture.currentGestureCoords,
2597 mPointerGesture.currentGestureIdToIndex,
2598 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2599 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2600 // Synthesize a hover move event after all pointers go up to indicate that
2601 // the pointer is hovering again even if the user is not currently touching
2602 // the touch pad. This ensures that a view will receive a fresh hover enter
2603 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002604 float x, y;
2605 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002606
2607 PointerProperties pointerProperties;
2608 pointerProperties.clear();
2609 pointerProperties.id = 0;
2610 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2611
2612 PointerCoords pointerCoords;
2613 pointerCoords.clear();
2614 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2615 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2616
2617 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002618 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002619 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002620 metaState, buttonState, MotionClassification::NONE,
2621 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2622 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002623 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002624 }
2625
2626 // Update state.
2627 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2628 if (!down) {
2629 mPointerGesture.lastGestureIdBits.clear();
2630 } else {
2631 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2632 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2633 uint32_t id = idBits.clearFirstMarkedBit();
2634 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2635 mPointerGesture.lastGestureProperties[index].copyFrom(
2636 mPointerGesture.currentGestureProperties[index]);
2637 mPointerGesture.lastGestureCoords[index].copyFrom(
2638 mPointerGesture.currentGestureCoords[index]);
2639 mPointerGesture.lastGestureIdToIndex[id] = index;
2640 }
2641 }
2642}
2643
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002644void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002645 // Cancel previously dispatches pointers.
2646 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2647 int32_t metaState = getContext()->getGlobalMetaState();
2648 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002649 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2650 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002651 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2652 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2653 0, 0, mPointerGesture.downTime);
2654 }
2655
2656 // Reset the current pointer gesture.
2657 mPointerGesture.reset();
2658 mPointerVelocityControl.reset();
2659
2660 // Remove any current spots.
2661 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002662 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002663 mPointerController->clearSpots();
2664 }
2665}
2666
2667bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2668 bool* outFinishPreviousGesture, bool isTimeout) {
2669 *outCancelPreviousGesture = false;
2670 *outFinishPreviousGesture = false;
2671
2672 // Handle TAP timeout.
2673 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002674 if (DEBUG_GESTURES) {
2675 ALOGD("Gestures: Processing timeout");
2676 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002677
Michael Wright227c5542020-07-02 18:30:52 +01002678 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002679 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2680 // The tap/drag timeout has not yet expired.
2681 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2682 mConfig.pointerGestureTapDragInterval);
2683 } else {
2684 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002685 if (DEBUG_GESTURES) {
2686 ALOGD("Gestures: TAP finished");
2687 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002688 *outFinishPreviousGesture = true;
2689
2690 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002691 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002692 mPointerGesture.currentGestureIdBits.clear();
2693
2694 mPointerVelocityControl.reset();
2695 return true;
2696 }
2697 }
2698
2699 // We did not handle this timeout.
2700 return false;
2701 }
2702
2703 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2704 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2705
2706 // Update the velocity tracker.
2707 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002708 std::vector<VelocityTracker::Position> positions;
2709 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002710 uint32_t id = idBits.clearFirstMarkedBit();
2711 const RawPointerData::Pointer& pointer =
2712 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002713 float x = pointer.x * mPointerXMovementScale;
2714 float y = pointer.y * mPointerYMovementScale;
2715 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002716 }
2717 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2718 positions);
2719 }
2720
2721 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2722 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002723 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2724 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2725 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002726 mPointerGesture.resetTap();
2727 }
2728
2729 // Pick a new active touch id if needed.
2730 // Choose an arbitrary pointer that just went down, if there is one.
2731 // Otherwise choose an arbitrary remaining pointer.
2732 // This guarantees we always have an active touch id when there is at least one pointer.
2733 // We keep the same active touch id for as long as possible.
2734 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2735 int32_t activeTouchId = lastActiveTouchId;
2736 if (activeTouchId < 0) {
2737 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2738 activeTouchId = mPointerGesture.activeTouchId =
2739 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2740 mPointerGesture.firstTouchTime = when;
2741 }
2742 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2743 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2744 activeTouchId = mPointerGesture.activeTouchId =
2745 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2746 } else {
2747 activeTouchId = mPointerGesture.activeTouchId = -1;
2748 }
2749 }
2750
2751 // Determine whether we are in quiet time.
2752 bool isQuietTime = false;
2753 if (activeTouchId < 0) {
2754 mPointerGesture.resetQuietTime();
2755 } else {
2756 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2757 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002758 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2759 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2760 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002761 currentFingerCount < 2) {
2762 // Enter quiet time when exiting swipe or freeform state.
2763 // This is to prevent accidentally entering the hover state and flinging the
2764 // pointer when finishing a swipe and there is still one pointer left onscreen.
2765 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002766 } else if (mPointerGesture.lastGestureMode ==
2767 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002768 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2769 // Enter quiet time when releasing the button and there are still two or more
2770 // fingers down. This may indicate that one finger was used to press the button
2771 // but it has not gone up yet.
2772 isQuietTime = true;
2773 }
2774 if (isQuietTime) {
2775 mPointerGesture.quietTime = when;
2776 }
2777 }
2778 }
2779
2780 // Switch states based on button and pointer state.
2781 if (isQuietTime) {
2782 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002783 if (DEBUG_GESTURES) {
2784 ALOGD("Gestures: QUIET for next %0.3fms",
2785 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2786 0.000001f);
2787 }
Michael Wright227c5542020-07-02 18:30:52 +01002788 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002789 *outFinishPreviousGesture = true;
2790 }
2791
2792 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002793 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002794 mPointerGesture.currentGestureIdBits.clear();
2795
2796 mPointerVelocityControl.reset();
2797 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2798 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2799 // The pointer follows the active touch point.
2800 // Emit DOWN, MOVE, UP events at the pointer location.
2801 //
2802 // Only the active touch matters; other fingers are ignored. This policy helps
2803 // to handle the case where the user places a second finger on the touch pad
2804 // to apply the necessary force to depress an integrated button below the surface.
2805 // We don't want the second finger to be delivered to applications.
2806 //
2807 // For this to work well, we need to make sure to track the pointer that is really
2808 // active. If the user first puts one finger down to click then adds another
2809 // finger to drag then the active pointer should switch to the finger that is
2810 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002811 if (DEBUG_GESTURES) {
2812 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2813 "currentFingerCount=%d",
2814 activeTouchId, currentFingerCount);
2815 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002816 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002817 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002818 *outFinishPreviousGesture = true;
2819 mPointerGesture.activeGestureId = 0;
2820 }
2821
2822 // Switch pointers if needed.
2823 // Find the fastest pointer and follow it.
2824 if (activeTouchId >= 0 && currentFingerCount > 1) {
2825 int32_t bestId = -1;
2826 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2827 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2828 uint32_t id = idBits.clearFirstMarkedBit();
2829 float vx, vy;
2830 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2831 float speed = hypotf(vx, vy);
2832 if (speed > bestSpeed) {
2833 bestId = id;
2834 bestSpeed = speed;
2835 }
2836 }
2837 }
2838 if (bestId >= 0 && bestId != activeTouchId) {
2839 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002840 if (DEBUG_GESTURES) {
2841 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2842 "bestId=%d, bestSpeed=%0.3f",
2843 bestId, bestSpeed);
2844 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002845 }
2846 }
2847
2848 float deltaX = 0, deltaY = 0;
2849 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2850 const RawPointerData::Pointer& currentPointer =
2851 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2852 const RawPointerData::Pointer& lastPointer =
2853 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2854 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2855 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2856
Prabir Pradhan1728b212021-10-19 16:00:03 -07002857 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002858 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2859
2860 // Move the pointer using a relative motion.
2861 // When using spots, the click will occur at the position of the anchor
2862 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002863 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002864 } else {
2865 mPointerVelocityControl.reset();
2866 }
2867
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002868 float x, y;
2869 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002870
Michael Wright227c5542020-07-02 18:30:52 +01002871 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002872 mPointerGesture.currentGestureIdBits.clear();
2873 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2874 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2875 mPointerGesture.currentGestureProperties[0].clear();
2876 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2877 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2878 mPointerGesture.currentGestureCoords[0].clear();
2879 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2880 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2881 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2882 } else if (currentFingerCount == 0) {
2883 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002884 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002885 *outFinishPreviousGesture = true;
2886 }
2887
2888 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2889 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2890 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002891 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2892 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002893 lastFingerCount == 1) {
2894 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002895 float x, y;
2896 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002897 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2898 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002899 if (DEBUG_GESTURES) {
2900 ALOGD("Gestures: TAP");
2901 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002902
2903 mPointerGesture.tapUpTime = when;
2904 getContext()->requestTimeoutAtTime(when +
2905 mConfig.pointerGestureTapDragInterval);
2906
2907 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002908 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002909 mPointerGesture.currentGestureIdBits.clear();
2910 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2911 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2912 mPointerGesture.currentGestureProperties[0].clear();
2913 mPointerGesture.currentGestureProperties[0].id =
2914 mPointerGesture.activeGestureId;
2915 mPointerGesture.currentGestureProperties[0].toolType =
2916 AMOTION_EVENT_TOOL_TYPE_FINGER;
2917 mPointerGesture.currentGestureCoords[0].clear();
2918 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2919 mPointerGesture.tapX);
2920 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2921 mPointerGesture.tapY);
2922 mPointerGesture.currentGestureCoords[0]
2923 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2924
2925 tapped = true;
2926 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002927 if (DEBUG_GESTURES) {
2928 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2929 y - mPointerGesture.tapY);
2930 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002931 }
2932 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002933 if (DEBUG_GESTURES) {
2934 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2935 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2936 (when - mPointerGesture.tapDownTime) * 0.000001f);
2937 } else {
2938 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2939 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002940 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002941 }
2942 }
2943
2944 mPointerVelocityControl.reset();
2945
2946 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002947 if (DEBUG_GESTURES) {
2948 ALOGD("Gestures: NEUTRAL");
2949 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002950 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002951 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002952 mPointerGesture.currentGestureIdBits.clear();
2953 }
2954 } else if (currentFingerCount == 1) {
2955 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2956 // The pointer follows the active touch point.
2957 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2958 // When in TAP_DRAG, emit MOVE events at the pointer location.
2959 ALOG_ASSERT(activeTouchId >= 0);
2960
Michael Wright227c5542020-07-02 18:30:52 +01002961 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2962 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002963 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002964 float x, y;
2965 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002966 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2967 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002968 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002969 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002970 if (DEBUG_GESTURES) {
2971 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2972 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
2973 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002974 }
2975 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002976 if (DEBUG_GESTURES) {
2977 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
2978 (when - mPointerGesture.tapUpTime) * 0.000001f);
2979 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002980 }
Michael Wright227c5542020-07-02 18:30:52 +01002981 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2982 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002983 }
2984
2985 float deltaX = 0, deltaY = 0;
2986 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2987 const RawPointerData::Pointer& currentPointer =
2988 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2989 const RawPointerData::Pointer& lastPointer =
2990 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2991 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2992 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2993
Prabir Pradhan1728b212021-10-19 16:00:03 -07002994 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002995 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2996
2997 // Move the pointer using a relative motion.
2998 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002999 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003000 } else {
3001 mPointerVelocityControl.reset();
3002 }
3003
3004 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003005 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003006 if (DEBUG_GESTURES) {
3007 ALOGD("Gestures: TAP_DRAG");
3008 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003009 down = true;
3010 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003011 if (DEBUG_GESTURES) {
3012 ALOGD("Gestures: HOVER");
3013 }
Michael Wright227c5542020-07-02 18:30:52 +01003014 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003015 *outFinishPreviousGesture = true;
3016 }
3017 mPointerGesture.activeGestureId = 0;
3018 down = false;
3019 }
3020
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003021 float x, y;
3022 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003023
3024 mPointerGesture.currentGestureIdBits.clear();
3025 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3026 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3027 mPointerGesture.currentGestureProperties[0].clear();
3028 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3029 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3030 mPointerGesture.currentGestureCoords[0].clear();
3031 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3032 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3033 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3034 down ? 1.0f : 0.0f);
3035
3036 if (lastFingerCount == 0 && currentFingerCount != 0) {
3037 mPointerGesture.resetTap();
3038 mPointerGesture.tapDownTime = when;
3039 mPointerGesture.tapX = x;
3040 mPointerGesture.tapY = y;
3041 }
3042 } else {
3043 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3044 // We need to provide feedback for each finger that goes down so we cannot wait
3045 // for the fingers to move before deciding what to do.
3046 //
3047 // The ambiguous case is deciding what to do when there are two fingers down but they
3048 // have not moved enough to determine whether they are part of a drag or part of a
3049 // freeform gesture, or just a press or long-press at the pointer location.
3050 //
3051 // When there are two fingers we start with the PRESS hypothesis and we generate a
3052 // down at the pointer location.
3053 //
3054 // When the two fingers move enough or when additional fingers are added, we make
3055 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3056 ALOG_ASSERT(activeTouchId >= 0);
3057
3058 bool settled = when >=
3059 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003060 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3061 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3062 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003063 *outFinishPreviousGesture = true;
3064 } else if (!settled && currentFingerCount > lastFingerCount) {
3065 // Additional pointers have gone down but not yet settled.
3066 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003067 if (DEBUG_GESTURES) {
3068 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3069 "MULTITOUCH, settle time remaining %0.3fms",
3070 (mPointerGesture.firstTouchTime +
3071 mConfig.pointerGestureMultitouchSettleInterval - when) *
3072 0.000001f);
3073 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003074 *outCancelPreviousGesture = true;
3075 } else {
3076 // Continue previous gesture.
3077 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3078 }
3079
3080 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003081 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003082 mPointerGesture.activeGestureId = 0;
3083 mPointerGesture.referenceIdBits.clear();
3084 mPointerVelocityControl.reset();
3085
3086 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003087 if (DEBUG_GESTURES) {
3088 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3089 "settle time remaining %0.3fms",
3090 (mPointerGesture.firstTouchTime +
3091 mConfig.pointerGestureMultitouchSettleInterval - when) *
3092 0.000001f);
3093 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003094 mCurrentRawState.rawPointerData
3095 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3096 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003097 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3098 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003099 }
3100
3101 // Clear the reference deltas for fingers not yet included in the reference calculation.
3102 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3103 ~mPointerGesture.referenceIdBits.value);
3104 !idBits.isEmpty();) {
3105 uint32_t id = idBits.clearFirstMarkedBit();
3106 mPointerGesture.referenceDeltas[id].dx = 0;
3107 mPointerGesture.referenceDeltas[id].dy = 0;
3108 }
3109 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3110
3111 // Add delta for all fingers and calculate a common movement delta.
3112 float commonDeltaX = 0, commonDeltaY = 0;
3113 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3114 mCurrentCookedState.fingerIdBits.value);
3115 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3116 bool first = (idBits == commonIdBits);
3117 uint32_t id = idBits.clearFirstMarkedBit();
3118 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3119 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3120 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3121 delta.dx += cpd.x - lpd.x;
3122 delta.dy += cpd.y - lpd.y;
3123
3124 if (first) {
3125 commonDeltaX = delta.dx;
3126 commonDeltaY = delta.dy;
3127 } else {
3128 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3129 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3130 }
3131 }
3132
3133 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003134 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003135 float dist[MAX_POINTER_ID + 1];
3136 int32_t distOverThreshold = 0;
3137 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3138 uint32_t id = idBits.clearFirstMarkedBit();
3139 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3140 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3141 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3142 distOverThreshold += 1;
3143 }
3144 }
3145
3146 // Only transition when at least two pointers have moved further than
3147 // the minimum distance threshold.
3148 if (distOverThreshold >= 2) {
3149 if (currentFingerCount > 2) {
3150 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003151 if (DEBUG_GESTURES) {
3152 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3153 currentFingerCount);
3154 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003155 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003156 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003157 } else {
3158 // There are exactly two pointers.
3159 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3160 uint32_t id1 = idBits.clearFirstMarkedBit();
3161 uint32_t id2 = idBits.firstMarkedBit();
3162 const RawPointerData::Pointer& p1 =
3163 mCurrentRawState.rawPointerData.pointerForId(id1);
3164 const RawPointerData::Pointer& p2 =
3165 mCurrentRawState.rawPointerData.pointerForId(id2);
3166 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3167 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3168 // There are two pointers but they are too far apart for a SWIPE,
3169 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003170 if (DEBUG_GESTURES) {
3171 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3172 "%0.3f",
3173 mutualDistance, mPointerGestureMaxSwipeWidth);
3174 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003175 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003176 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003177 } else {
3178 // There are two pointers. Wait for both pointers to start moving
3179 // before deciding whether this is a SWIPE or FREEFORM gesture.
3180 float dist1 = dist[id1];
3181 float dist2 = dist[id2];
3182 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3183 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3184 // Calculate the dot product of the displacement vectors.
3185 // When the vectors are oriented in approximately the same direction,
3186 // the angle betweeen them is near zero and the cosine of the angle
3187 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3188 // mag(v2).
3189 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3190 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3191 float dx1 = delta1.dx * mPointerXZoomScale;
3192 float dy1 = delta1.dy * mPointerYZoomScale;
3193 float dx2 = delta2.dx * mPointerXZoomScale;
3194 float dy2 = delta2.dy * mPointerYZoomScale;
3195 float dot = dx1 * dx2 + dy1 * dy2;
3196 float cosine = dot / (dist1 * dist2); // denominator always > 0
3197 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3198 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003199 if (DEBUG_GESTURES) {
3200 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3201 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3202 "cosine %0.3f >= %0.3f",
3203 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3204 mConfig.pointerGestureMultitouchMinDistance, cosine,
3205 mConfig.pointerGestureSwipeTransitionAngleCosine);
3206 }
Michael Wright227c5542020-07-02 18:30:52 +01003207 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003208 } else {
3209 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003210 if (DEBUG_GESTURES) {
3211 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3212 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3213 "cosine %0.3f < %0.3f",
3214 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3215 mConfig.pointerGestureMultitouchMinDistance, cosine,
3216 mConfig.pointerGestureSwipeTransitionAngleCosine);
3217 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003218 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003219 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003220 }
3221 }
3222 }
3223 }
3224 }
Michael Wright227c5542020-07-02 18:30:52 +01003225 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003226 // Switch from SWIPE to FREEFORM if additional pointers go down.
3227 // Cancel previous gesture.
3228 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003229 if (DEBUG_GESTURES) {
3230 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3231 currentFingerCount);
3232 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003233 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003234 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003235 }
3236 }
3237
3238 // Move the reference points based on the overall group motion of the fingers
3239 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003240 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003241 (commonDeltaX || commonDeltaY)) {
3242 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3243 uint32_t id = idBits.clearFirstMarkedBit();
3244 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3245 delta.dx = 0;
3246 delta.dy = 0;
3247 }
3248
3249 mPointerGesture.referenceTouchX += commonDeltaX;
3250 mPointerGesture.referenceTouchY += commonDeltaY;
3251
3252 commonDeltaX *= mPointerXMovementScale;
3253 commonDeltaY *= mPointerYMovementScale;
3254
Prabir Pradhan1728b212021-10-19 16:00:03 -07003255 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003256 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3257
3258 mPointerGesture.referenceGestureX += commonDeltaX;
3259 mPointerGesture.referenceGestureY += commonDeltaY;
3260 }
3261
3262 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003263 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3264 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003265 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003266 if (DEBUG_GESTURES) {
3267 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3268 "activeGestureId=%d, currentTouchPointerCount=%d",
3269 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3270 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003271 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3272
3273 mPointerGesture.currentGestureIdBits.clear();
3274 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3275 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3276 mPointerGesture.currentGestureProperties[0].clear();
3277 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3278 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3279 mPointerGesture.currentGestureCoords[0].clear();
3280 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3281 mPointerGesture.referenceGestureX);
3282 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3283 mPointerGesture.referenceGestureY);
3284 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003285 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003286 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003287 if (DEBUG_GESTURES) {
3288 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3289 "activeGestureId=%d, currentTouchPointerCount=%d",
3290 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3291 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003292 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3293
3294 mPointerGesture.currentGestureIdBits.clear();
3295
3296 BitSet32 mappedTouchIdBits;
3297 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003298 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003299 // Initially, assign the active gesture id to the active touch point
3300 // if there is one. No other touch id bits are mapped yet.
3301 if (!*outCancelPreviousGesture) {
3302 mappedTouchIdBits.markBit(activeTouchId);
3303 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3304 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3305 mPointerGesture.activeGestureId;
3306 } else {
3307 mPointerGesture.activeGestureId = -1;
3308 }
3309 } else {
3310 // Otherwise, assume we mapped all touches from the previous frame.
3311 // Reuse all mappings that are still applicable.
3312 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3313 mCurrentCookedState.fingerIdBits.value;
3314 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3315
3316 // Check whether we need to choose a new active gesture id because the
3317 // current went went up.
3318 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3319 ~mCurrentCookedState.fingerIdBits.value);
3320 !upTouchIdBits.isEmpty();) {
3321 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3322 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3323 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3324 mPointerGesture.activeGestureId = -1;
3325 break;
3326 }
3327 }
3328 }
3329
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003330 if (DEBUG_GESTURES) {
3331 ALOGD("Gestures: FREEFORM follow up "
3332 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3333 "activeGestureId=%d",
3334 mappedTouchIdBits.value, usedGestureIdBits.value,
3335 mPointerGesture.activeGestureId);
3336 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003337
3338 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3339 for (uint32_t i = 0; i < currentFingerCount; i++) {
3340 uint32_t touchId = idBits.clearFirstMarkedBit();
3341 uint32_t gestureId;
3342 if (!mappedTouchIdBits.hasBit(touchId)) {
3343 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3344 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003345 if (DEBUG_GESTURES) {
3346 ALOGD("Gestures: FREEFORM "
3347 "new mapping for touch id %d -> gesture id %d",
3348 touchId, gestureId);
3349 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003350 } else {
3351 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003352 if (DEBUG_GESTURES) {
3353 ALOGD("Gestures: FREEFORM "
3354 "existing mapping for touch id %d -> gesture id %d",
3355 touchId, gestureId);
3356 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003357 }
3358 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3359 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3360
3361 const RawPointerData::Pointer& pointer =
3362 mCurrentRawState.rawPointerData.pointerForId(touchId);
3363 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3364 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003365 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003366
3367 mPointerGesture.currentGestureProperties[i].clear();
3368 mPointerGesture.currentGestureProperties[i].id = gestureId;
3369 mPointerGesture.currentGestureProperties[i].toolType =
3370 AMOTION_EVENT_TOOL_TYPE_FINGER;
3371 mPointerGesture.currentGestureCoords[i].clear();
3372 mPointerGesture.currentGestureCoords[i]
3373 .setAxisValue(AMOTION_EVENT_AXIS_X,
3374 mPointerGesture.referenceGestureX + deltaX);
3375 mPointerGesture.currentGestureCoords[i]
3376 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3377 mPointerGesture.referenceGestureY + deltaY);
3378 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3379 1.0f);
3380 }
3381
3382 if (mPointerGesture.activeGestureId < 0) {
3383 mPointerGesture.activeGestureId =
3384 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003385 if (DEBUG_GESTURES) {
3386 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3387 mPointerGesture.activeGestureId);
3388 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003389 }
3390 }
3391 }
3392
3393 mPointerController->setButtonState(mCurrentRawState.buttonState);
3394
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003395 if (DEBUG_GESTURES) {
3396 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3397 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3398 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3399 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3400 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3401 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3402 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3403 uint32_t id = idBits.clearFirstMarkedBit();
3404 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3405 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3406 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3407 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3408 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3409 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3410 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3411 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3412 }
3413 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3414 uint32_t id = idBits.clearFirstMarkedBit();
3415 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3416 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3417 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3418 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3419 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3420 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3421 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3422 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3423 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003424 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003425 return true;
3426}
3427
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003428void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003429 mPointerSimple.currentCoords.clear();
3430 mPointerSimple.currentProperties.clear();
3431
3432 bool down, hovering;
3433 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3434 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3435 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003436 mPointerController
3437 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3438 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003439
3440 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3441 down = !hovering;
3442
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003443 float x, y;
3444 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003445 mPointerSimple.currentCoords.copyFrom(
3446 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3447 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3448 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3449 mPointerSimple.currentProperties.id = 0;
3450 mPointerSimple.currentProperties.toolType =
3451 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3452 } else {
3453 down = false;
3454 hovering = false;
3455 }
3456
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003458}
3459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3461 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003462}
3463
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003464void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003465 mPointerSimple.currentCoords.clear();
3466 mPointerSimple.currentProperties.clear();
3467
3468 bool down, hovering;
3469 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3470 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3471 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3472 float deltaX = 0, deltaY = 0;
3473 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3474 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3475 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3476 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3477 mPointerXMovementScale;
3478 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3479 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3480 mPointerYMovementScale;
3481
Prabir Pradhan1728b212021-10-19 16:00:03 -07003482 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003483 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3484
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003485 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003486 } else {
3487 mPointerVelocityControl.reset();
3488 }
3489
3490 down = isPointerDown(mCurrentRawState.buttonState);
3491 hovering = !down;
3492
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003493 float x, y;
3494 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003495 mPointerSimple.currentCoords.copyFrom(
3496 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3497 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3498 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3499 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3500 hovering ? 0.0f : 1.0f);
3501 mPointerSimple.currentProperties.id = 0;
3502 mPointerSimple.currentProperties.toolType =
3503 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3504 } else {
3505 mPointerVelocityControl.reset();
3506
3507 down = false;
3508 hovering = false;
3509 }
3510
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003512}
3513
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3515 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003516
3517 mPointerVelocityControl.reset();
3518}
3519
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3521 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003522 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003523
3524 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003525 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003526 mPointerController->clearSpots();
3527 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003528 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003529 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003530 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003531 }
Garfield Tan9514d782020-11-10 16:37:23 -08003532 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003533
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003534 float xCursorPosition, yCursorPosition;
3535 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003536
3537 if (mPointerSimple.down && !down) {
3538 mPointerSimple.down = false;
3539
3540 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003541 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3542 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003543 mLastRawState.buttonState, MotionClassification::NONE,
3544 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3545 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3546 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3547 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003548 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003549 }
3550
3551 if (mPointerSimple.hovering && !hovering) {
3552 mPointerSimple.hovering = false;
3553
3554 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3556 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3557 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003558 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3559 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3560 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3561 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003562 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003563 }
3564
3565 if (down) {
3566 if (!mPointerSimple.down) {
3567 mPointerSimple.down = true;
3568 mPointerSimple.downTime = when;
3569
3570 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003572 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3573 metaState, mCurrentRawState.buttonState,
3574 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3575 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3576 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3577 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003578 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003579 }
3580
3581 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3583 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003584 mCurrentRawState.buttonState, MotionClassification::NONE,
3585 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3586 &mPointerSimple.currentCoords, mOrientedXPrecision,
3587 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3588 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003589 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003590 }
3591
3592 if (hovering) {
3593 if (!mPointerSimple.hovering) {
3594 mPointerSimple.hovering = true;
3595
3596 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003597 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003598 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3599 metaState, mCurrentRawState.buttonState,
3600 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3601 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3602 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3603 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003604 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003605 }
3606
3607 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003608 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3609 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3610 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003611 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3612 &mPointerSimple.currentCoords, mOrientedXPrecision,
3613 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3614 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003615 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003616 }
3617
3618 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3619 float vscroll = mCurrentRawState.rawVScroll;
3620 float hscroll = mCurrentRawState.rawHScroll;
3621 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3622 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3623
3624 // Send scroll.
3625 PointerCoords pointerCoords;
3626 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3627 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3628 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3629
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003630 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3631 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003632 mCurrentRawState.buttonState, MotionClassification::NONE,
3633 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3634 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3635 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3636 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003637 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003638 }
3639
3640 // Save state.
3641 if (down || hovering) {
3642 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3643 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3644 } else {
3645 mPointerSimple.reset();
3646 }
3647}
3648
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003650 mPointerSimple.currentCoords.clear();
3651 mPointerSimple.currentProperties.clear();
3652
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003653 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003654}
3655
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3657 uint32_t source, int32_t action, int32_t actionButton,
3658 int32_t flags, int32_t metaState, int32_t buttonState,
3659 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003660 const PointerCoords* coords, const uint32_t* idToIndex,
3661 BitSet32 idBits, int32_t changedId, float xPrecision,
3662 float yPrecision, nsecs_t downTime) {
3663 PointerCoords pointerCoords[MAX_POINTERS];
3664 PointerProperties pointerProperties[MAX_POINTERS];
3665 uint32_t pointerCount = 0;
3666 while (!idBits.isEmpty()) {
3667 uint32_t id = idBits.clearFirstMarkedBit();
3668 uint32_t index = idToIndex[id];
3669 pointerProperties[pointerCount].copyFrom(properties[index]);
3670 pointerCoords[pointerCount].copyFrom(coords[index]);
3671
3672 if (changedId >= 0 && id == uint32_t(changedId)) {
3673 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3674 }
3675
3676 pointerCount += 1;
3677 }
3678
3679 ALOG_ASSERT(pointerCount != 0);
3680
3681 if (changedId >= 0 && pointerCount == 1) {
3682 // Replace initial down and final up action.
3683 // We can compare the action without masking off the changed pointer index
3684 // because we know the index is 0.
3685 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3686 action = AMOTION_EVENT_ACTION_DOWN;
3687 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003688 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3689 action = AMOTION_EVENT_ACTION_CANCEL;
3690 } else {
3691 action = AMOTION_EVENT_ACTION_UP;
3692 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003693 } else {
3694 // Can't happen.
3695 ALOG_ASSERT(false);
3696 }
3697 }
3698 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3699 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003700 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003701 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003702 }
3703 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3704 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003705 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003706 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003707 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003708 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3709 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003710 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3711 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3712 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003713 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003714}
3715
3716bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3717 const PointerCoords* inCoords,
3718 const uint32_t* inIdToIndex,
3719 PointerProperties* outProperties,
3720 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3721 BitSet32 idBits) const {
3722 bool changed = false;
3723 while (!idBits.isEmpty()) {
3724 uint32_t id = idBits.clearFirstMarkedBit();
3725 uint32_t inIndex = inIdToIndex[id];
3726 uint32_t outIndex = outIdToIndex[id];
3727
3728 const PointerProperties& curInProperties = inProperties[inIndex];
3729 const PointerCoords& curInCoords = inCoords[inIndex];
3730 PointerProperties& curOutProperties = outProperties[outIndex];
3731 PointerCoords& curOutCoords = outCoords[outIndex];
3732
3733 if (curInProperties != curOutProperties) {
3734 curOutProperties.copyFrom(curInProperties);
3735 changed = true;
3736 }
3737
3738 if (curInCoords != curOutCoords) {
3739 curOutCoords.copyFrom(curInCoords);
3740 changed = true;
3741 }
3742 }
3743 return changed;
3744}
3745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3747 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3748 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003749}
3750
Prabir Pradhan1728b212021-10-19 16:00:03 -07003751// Transform input device coordinates to display panel coordinates.
3752void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003753 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3754 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3755
arthurhunga36b28e2020-12-29 20:28:15 +08003756 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3757 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3758
Prabir Pradhan1728b212021-10-19 16:00:03 -07003759 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003760 // 0 - no swap and reverse.
3761 // 90 - swap x/y and reverse y.
3762 // 180 - reverse x, y.
3763 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003764 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003765 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003766 x = xScaled;
3767 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003768 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003769 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003770 y = xScaledMax;
3771 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003772 break;
3773 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003774 x = xScaledMax;
3775 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003776 break;
3777 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003778 y = xScaled;
3779 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003780 break;
3781 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003782 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003783 }
3784}
3785
Prabir Pradhan1728b212021-10-19 16:00:03 -07003786bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003787 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3788 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3789
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003790 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003791 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003792 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003793 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003794}
3795
3796const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3797 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003798 if (DEBUG_VIRTUAL_KEYS) {
3799 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3800 "left=%d, top=%d, right=%d, bottom=%d",
3801 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3802 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3803 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003804
3805 if (virtualKey.isHit(x, y)) {
3806 return &virtualKey;
3807 }
3808 }
3809
3810 return nullptr;
3811}
3812
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003813void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3814 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3815 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003816
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003817 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003818
3819 if (currentPointerCount == 0) {
3820 // No pointers to assign.
3821 return;
3822 }
3823
3824 if (lastPointerCount == 0) {
3825 // All pointers are new.
3826 for (uint32_t i = 0; i < currentPointerCount; i++) {
3827 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003828 current.rawPointerData.pointers[i].id = id;
3829 current.rawPointerData.idToIndex[id] = i;
3830 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003831 }
3832 return;
3833 }
3834
3835 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003836 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003837 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003838 uint32_t id = last.rawPointerData.pointers[0].id;
3839 current.rawPointerData.pointers[0].id = id;
3840 current.rawPointerData.idToIndex[id] = 0;
3841 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003842 return;
3843 }
3844
3845 // General case.
3846 // We build a heap of squared euclidean distances between current and last pointers
3847 // associated with the current and last pointer indices. Then, we find the best
3848 // match (by distance) for each current pointer.
3849 // The pointers must have the same tool type but it is possible for them to
3850 // transition from hovering to touching or vice-versa while retaining the same id.
3851 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3852
3853 uint32_t heapSize = 0;
3854 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3855 currentPointerIndex++) {
3856 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3857 lastPointerIndex++) {
3858 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003859 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003860 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003861 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003862 if (currentPointer.toolType == lastPointer.toolType) {
3863 int64_t deltaX = currentPointer.x - lastPointer.x;
3864 int64_t deltaY = currentPointer.y - lastPointer.y;
3865
3866 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3867
3868 // Insert new element into the heap (sift up).
3869 heap[heapSize].currentPointerIndex = currentPointerIndex;
3870 heap[heapSize].lastPointerIndex = lastPointerIndex;
3871 heap[heapSize].distance = distance;
3872 heapSize += 1;
3873 }
3874 }
3875 }
3876
3877 // Heapify
3878 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3879 startIndex -= 1;
3880 for (uint32_t parentIndex = startIndex;;) {
3881 uint32_t childIndex = parentIndex * 2 + 1;
3882 if (childIndex >= heapSize) {
3883 break;
3884 }
3885
3886 if (childIndex + 1 < heapSize &&
3887 heap[childIndex + 1].distance < heap[childIndex].distance) {
3888 childIndex += 1;
3889 }
3890
3891 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3892 break;
3893 }
3894
3895 swap(heap[parentIndex], heap[childIndex]);
3896 parentIndex = childIndex;
3897 }
3898 }
3899
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003900 if (DEBUG_POINTER_ASSIGNMENT) {
3901 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3902 for (size_t i = 0; i < heapSize; i++) {
3903 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3904 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3905 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003906 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003907
3908 // Pull matches out by increasing order of distance.
3909 // To avoid reassigning pointers that have already been matched, the loop keeps track
3910 // of which last and current pointers have been matched using the matchedXXXBits variables.
3911 // It also tracks the used pointer id bits.
3912 BitSet32 matchedLastBits(0);
3913 BitSet32 matchedCurrentBits(0);
3914 BitSet32 usedIdBits(0);
3915 bool first = true;
3916 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3917 while (heapSize > 0) {
3918 if (first) {
3919 // The first time through the loop, we just consume the root element of
3920 // the heap (the one with smallest distance).
3921 first = false;
3922 } else {
3923 // Previous iterations consumed the root element of the heap.
3924 // Pop root element off of the heap (sift down).
3925 heap[0] = heap[heapSize];
3926 for (uint32_t parentIndex = 0;;) {
3927 uint32_t childIndex = parentIndex * 2 + 1;
3928 if (childIndex >= heapSize) {
3929 break;
3930 }
3931
3932 if (childIndex + 1 < heapSize &&
3933 heap[childIndex + 1].distance < heap[childIndex].distance) {
3934 childIndex += 1;
3935 }
3936
3937 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3938 break;
3939 }
3940
3941 swap(heap[parentIndex], heap[childIndex]);
3942 parentIndex = childIndex;
3943 }
3944
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003945 if (DEBUG_POINTER_ASSIGNMENT) {
3946 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3947 for (size_t j = 0; j < heapSize; j++) {
3948 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3949 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3950 heap[j].distance);
3951 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003952 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003953 }
3954
3955 heapSize -= 1;
3956
3957 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3958 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3959
3960 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3961 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3962
3963 matchedCurrentBits.markBit(currentPointerIndex);
3964 matchedLastBits.markBit(lastPointerIndex);
3965
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003966 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3967 current.rawPointerData.pointers[currentPointerIndex].id = id;
3968 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3969 current.rawPointerData.markIdBit(id,
3970 current.rawPointerData.isHovering(
3971 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003972 usedIdBits.markBit(id);
3973
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003974 if (DEBUG_POINTER_ASSIGNMENT) {
3975 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3976 ", distance=%" PRIu64,
3977 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3978 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003979 break;
3980 }
3981 }
3982
3983 // Assign fresh ids to pointers that were not matched in the process.
3984 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3985 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3986 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3987
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003988 current.rawPointerData.pointers[currentPointerIndex].id = id;
3989 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3990 current.rawPointerData.markIdBit(id,
3991 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003992
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003993 if (DEBUG_POINTER_ASSIGNMENT) {
3994 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3995 id);
3996 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003997 }
3998}
3999
4000int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4001 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4002 return AKEY_STATE_VIRTUAL;
4003 }
4004
4005 for (const VirtualKey& virtualKey : mVirtualKeys) {
4006 if (virtualKey.keyCode == keyCode) {
4007 return AKEY_STATE_UP;
4008 }
4009 }
4010
4011 return AKEY_STATE_UNKNOWN;
4012}
4013
4014int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4015 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4016 return AKEY_STATE_VIRTUAL;
4017 }
4018
4019 for (const VirtualKey& virtualKey : mVirtualKeys) {
4020 if (virtualKey.scanCode == scanCode) {
4021 return AKEY_STATE_UP;
4022 }
4023 }
4024
4025 return AKEY_STATE_UNKNOWN;
4026}
4027
4028bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4029 const int32_t* keyCodes, uint8_t* outFlags) {
4030 for (const VirtualKey& virtualKey : mVirtualKeys) {
4031 for (size_t i = 0; i < numCodes; i++) {
4032 if (virtualKey.keyCode == keyCodes[i]) {
4033 outFlags[i] = 1;
4034 }
4035 }
4036 }
4037
4038 return true;
4039}
4040
4041std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4042 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004043 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004044 return std::make_optional(mPointerController->getDisplayId());
4045 } else {
4046 return std::make_optional(mViewport.displayId);
4047 }
4048 }
4049 return std::nullopt;
4050}
4051
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004052} // namespace android