blob: 17ee54f528aa733851a36987dab2dd98c02b5ef9 [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
HQ Liue6983c72022-04-19 22:14:56 +000045// Minimum width between two pointers to determine a gesture as freeform gesture in mm
46static const float MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER = 30;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070047// --- Static Definitions ---
48
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000049static const DisplayViewport kUninitializedViewport;
50
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051template <typename T>
52inline static void swap(T& a, T& b) {
53 T temp = a;
54 a = b;
55 b = temp;
56}
57
58static float calculateCommonVector(float a, float b) {
59 if (a > 0 && b > 0) {
60 return a < b ? a : b;
61 } else if (a < 0 && b < 0) {
62 return a > b ? a : b;
63 } else {
64 return 0;
65 }
66}
67
68inline static float distance(float x1, float y1, float x2, float y2) {
69 return hypotf(x1 - x2, y1 - y2);
70}
71
72inline static int32_t signExtendNybble(int32_t value) {
73 return value >= 8 ? value - 16 : value;
74}
75
76// --- RawPointerAxes ---
77
78RawPointerAxes::RawPointerAxes() {
79 clear();
80}
81
82void RawPointerAxes::clear() {
83 x.clear();
84 y.clear();
85 pressure.clear();
86 touchMajor.clear();
87 touchMinor.clear();
88 toolMajor.clear();
89 toolMinor.clear();
90 orientation.clear();
91 distance.clear();
92 tiltX.clear();
93 tiltY.clear();
94 trackingId.clear();
95 slot.clear();
96}
97
98// --- RawPointerData ---
99
100RawPointerData::RawPointerData() {
101 clear();
102}
103
104void RawPointerData::clear() {
105 pointerCount = 0;
106 clearIdBits();
107}
108
109void RawPointerData::copyFrom(const RawPointerData& other) {
110 pointerCount = other.pointerCount;
111 hoveringIdBits = other.hoveringIdBits;
112 touchingIdBits = other.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +0800113 canceledIdBits = other.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114
115 for (uint32_t i = 0; i < pointerCount; i++) {
116 pointers[i] = other.pointers[i];
117
118 int id = pointers[i].id;
119 idToIndex[id] = other.idToIndex[id];
120 }
121}
122
123void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
124 float x = 0, y = 0;
125 uint32_t count = touchingIdBits.count();
126 if (count) {
127 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
128 uint32_t id = idBits.clearFirstMarkedBit();
129 const Pointer& pointer = pointerForId(id);
130 x += pointer.x;
131 y += pointer.y;
132 }
133 x /= count;
134 y /= count;
135 }
136 *outX = x;
137 *outY = y;
138}
139
140// --- CookedPointerData ---
141
142CookedPointerData::CookedPointerData() {
143 clear();
144}
145
146void CookedPointerData::clear() {
147 pointerCount = 0;
148 hoveringIdBits.clear();
149 touchingIdBits.clear();
arthurhungcc7f9802020-04-30 17:55:40 +0800150 canceledIdBits.clear();
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000151 validIdBits.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152}
153
154void CookedPointerData::copyFrom(const CookedPointerData& other) {
155 pointerCount = other.pointerCount;
156 hoveringIdBits = other.hoveringIdBits;
157 touchingIdBits = other.touchingIdBits;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000158 validIdBits = other.validIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159
160 for (uint32_t i = 0; i < pointerCount; i++) {
161 pointerProperties[i].copyFrom(other.pointerProperties[i]);
162 pointerCoords[i].copyFrom(other.pointerCoords[i]);
163
164 int id = pointerProperties[i].id;
165 idToIndex[id] = other.idToIndex[id];
166 }
167}
168
169// --- TouchInputMapper ---
170
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800171TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
172 : InputMapper(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100174 mDeviceMode(DeviceMode::DISABLED),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700175 mDisplayWidth(-1),
176 mDisplayHeight(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700177 mPhysicalWidth(-1),
178 mPhysicalHeight(-1),
179 mPhysicalLeft(0),
180 mPhysicalTop(0),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700181 mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182
183TouchInputMapper::~TouchInputMapper() {}
184
Philip Junker4af3b3d2021-12-14 10:36:55 +0100185uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700186 return mSource;
187}
188
189void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
190 InputMapper::populateDeviceInfo(info);
191
Michael Wright227c5542020-07-02 18:30:52 +0100192 if (mDeviceMode != DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700193 info->addMotionRange(mOrientedRanges.x);
194 info->addMotionRange(mOrientedRanges.y);
195 info->addMotionRange(mOrientedRanges.pressure);
196
Chris Yef74dc422020-09-02 22:41:50 -0700197 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
Chris Ye8fa17282020-09-15 17:17:34 -0700198 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
199 //
200 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
201 // motion, i.e. the hardware dimensions, as the finger could move completely across the
202 // touchpad in one sample cycle.
Chris Yef74dc422020-09-02 22:41:50 -0700203 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
204 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
205 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat,
206 x.fuzz, x.resolution);
207 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat,
208 y.fuzz, y.resolution);
209 }
210
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700211 if (mOrientedRanges.size) {
212 info->addMotionRange(*mOrientedRanges.size);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700213 }
214
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700215 if (mOrientedRanges.touchMajor) {
216 info->addMotionRange(*mOrientedRanges.touchMajor);
217 info->addMotionRange(*mOrientedRanges.touchMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218 }
219
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700220 if (mOrientedRanges.toolMajor) {
221 info->addMotionRange(*mOrientedRanges.toolMajor);
222 info->addMotionRange(*mOrientedRanges.toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700223 }
224
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700225 if (mOrientedRanges.orientation) {
226 info->addMotionRange(*mOrientedRanges.orientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700227 }
228
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700229 if (mOrientedRanges.distance) {
230 info->addMotionRange(*mOrientedRanges.distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700231 }
232
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700233 if (mOrientedRanges.tilt) {
234 info->addMotionRange(*mOrientedRanges.tilt);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700235 }
236
237 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
238 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
239 0.0f);
240 }
241 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
242 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
243 0.0f);
244 }
Michael Wright227c5542020-07-02 18:30:52 +0100245 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
247 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
248 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
249 x.fuzz, x.resolution);
250 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
251 y.fuzz, y.resolution);
252 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
253 x.fuzz, x.resolution);
254 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
255 y.fuzz, y.resolution);
256 }
257 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
258 }
259}
260
261void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700262 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800263 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 dumpParameters(dump);
265 dumpVirtualKeys(dump);
266 dumpRawPointerAxes(dump);
267 dumpCalibration(dump);
268 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700269 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270
271 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale);
273 dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale);
274 dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
275 dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
276 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
277 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
278 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
279 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
280 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
281 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
282 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
283 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
284 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
285 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
286
287 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
288 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
289 mLastRawState.rawPointerData.pointerCount);
290 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
291 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
292 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
293 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
294 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
295 "toolType=%d, isHovering=%s\n",
296 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
297 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
298 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
299 pointer.distance, pointer.toolType, toString(pointer.isHovering));
300 }
301
302 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
303 mLastCookedState.buttonState);
304 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
305 mLastCookedState.cookedPointerData.pointerCount);
306 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
307 const PointerProperties& pointerProperties =
308 mLastCookedState.cookedPointerData.pointerProperties[i];
309 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000310 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
311 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
312 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
314 "toolType=%d, isHovering=%s\n",
315 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000316 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
317 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
319 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
320 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
321 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
322 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
323 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
324 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
325 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
326 pointerProperties.toolType,
327 toString(mLastCookedState.cookedPointerData.isHovering(i)));
328 }
329
330 dump += INDENT3 "Stylus Fusion:\n";
331 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
332 toString(mExternalStylusConnected));
333 dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
334 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
335 mExternalStylusFusionTimeout);
336 dump += INDENT3 "External Stylus State:\n";
337 dumpStylusState(dump, mExternalStylusState);
338
Michael Wright227c5542020-07-02 18:30:52 +0100339 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
341 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
342 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
343 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
344 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
345 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
346 }
347}
348
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
350 uint32_t changes) {
351 InputMapper::configure(when, config, changes);
352
353 mConfig = *config;
354
355 if (!changes) { // first time only
356 // Configure basic parameters.
357 configureParameters();
358
359 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800360 mCursorScrollAccumulator.configure(getDeviceContext());
361 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362
363 // Configure absolute axis information.
364 configureRawPointerAxes();
365
366 // Prepare input device calibration.
367 parseCalibration();
368 resolveCalibration();
369 }
370
371 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
372 // Update location calibration to reflect current settings
373 updateAffineTransformation();
374 }
375
376 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
377 // Update pointer speed.
378 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
379 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
380 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
381 }
382
383 bool resetNeeded = false;
384 if (!changes ||
385 (changes &
386 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800387 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
389 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
390 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700391 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700393 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394 }
395
396 if (changes && resetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000397 // If the device needs to be reset, cancel any ongoing gestures and reset the state.
398 cancelTouch(when, when);
399 reset(when);
400
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700401 // Send reset, unless this is the first time the device has been configured,
402 // in which case the reader will call reset itself after all mappers are ready.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000403 NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId());
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700404 getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405 }
406}
407
408void TouchInputMapper::resolveExternalStylusPresence() {
409 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800410 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700411 mExternalStylusConnected = !devices.empty();
412
413 if (!mExternalStylusConnected) {
414 resetExternalStylus();
415 }
416}
417
418void TouchInputMapper::configureParameters() {
419 // Use the pointer presentation mode for devices that do not support distinct
420 // multitouch. The spot-based presentation relies on being able to accurately
421 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800422 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100423 ? Parameters::GestureMode::SINGLE_TOUCH
424 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700426 std::string gestureModeString;
427 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800428 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100430 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100432 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700434 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700435 }
436 }
437
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800438 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439 // The device is a touch screen.
Michael Wright227c5542020-07-02 18:30:52 +0100440 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800441 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442 // The device is a pointing device like a track pad.
Michael Wright227c5542020-07-02 18:30:52 +0100443 mParameters.deviceType = Parameters::DeviceType::POINTER;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800444 } else if (getDeviceContext().hasRelativeAxis(REL_X) ||
445 getDeviceContext().hasRelativeAxis(REL_Y)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446 // The device is a cursor device with a touch pad attached.
447 // By default don't use the touch pad to move the pointer.
Michael Wright227c5542020-07-02 18:30:52 +0100448 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700449 } else {
450 // The device is a touch pad of unknown purpose.
Michael Wright227c5542020-07-02 18:30:52 +0100451 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700452 }
453
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800454 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700455
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700456 std::string deviceTypeString;
457 if (getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800458 deviceTypeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 if (deviceTypeString == "touchScreen") {
Michael Wright227c5542020-07-02 18:30:52 +0100460 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461 } else if (deviceTypeString == "touchPad") {
Michael Wright227c5542020-07-02 18:30:52 +0100462 mParameters.deviceType = Parameters::DeviceType::TOUCH_PAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463 } else if (deviceTypeString == "touchNavigation") {
Michael Wright227c5542020-07-02 18:30:52 +0100464 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700465 } else if (deviceTypeString == "pointer") {
Michael Wright227c5542020-07-02 18:30:52 +0100466 mParameters.deviceType = Parameters::DeviceType::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467 } else if (deviceTypeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700468 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700469 }
470 }
471
Michael Wright227c5542020-07-02 18:30:52 +0100472 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700473 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800474 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700475
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700476 mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700477 std::string orientationString;
478 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700479 orientationString)) {
480 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
481 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
482 } else if (orientationString == "ORIENTATION_90") {
483 mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
484 } else if (orientationString == "ORIENTATION_180") {
485 mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
486 } else if (orientationString == "ORIENTATION_270") {
487 mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
488 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700489 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700490 }
491 }
492
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700493 mParameters.hasAssociatedDisplay = false;
494 mParameters.associatedDisplayIsExternal = false;
495 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100496 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
497 mParameters.deviceType == Parameters::DeviceType::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700498 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100499 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800500 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700501 std::string uniqueDisplayId;
502 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800503 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700504 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
505 }
506 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800507 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700508 mParameters.hasAssociatedDisplay = true;
509 }
510
511 // Initial downs on external touch devices should wake the device.
512 // Normally we don't do this for internal touch screens to prevent them from waking
513 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800514 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700515 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700516}
517
518void TouchInputMapper::dumpParameters(std::string& dump) {
519 dump += INDENT3 "Parameters:\n";
520
Dominik Laskowski75788452021-02-09 18:51:25 -0800521 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700522
Dominik Laskowski75788452021-02-09 18:51:25 -0800523 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700524
525 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
526 "displayId='%s'\n",
527 toString(mParameters.hasAssociatedDisplay),
528 toString(mParameters.associatedDisplayIsExternal),
529 mParameters.uniqueDisplayId.c_str());
530 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800531 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700532}
533
534void TouchInputMapper::configureRawPointerAxes() {
535 mRawPointerAxes.clear();
536}
537
538void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
539 dump += INDENT3 "Raw Touch Axes:\n";
540 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
541 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
542 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
543 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
544 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
545 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
546 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
547 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
548 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
549 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
550 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
551 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
552 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
553}
554
555bool TouchInputMapper::hasExternalStylus() const {
556 return mExternalStylusConnected;
557}
558
559/**
560 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000561 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800562 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000563 * 3. Get the matching viewport by either unique id in idc file or by the display type
564 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800565 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700566 */
567std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800568 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000569 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800570 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700571 }
572
Christine Franks2a2293c2022-01-18 11:51:16 -0800573 const std::optional<std::string> associatedDisplayUniqueId =
574 getDeviceContext().getAssociatedDisplayUniqueId();
575 if (associatedDisplayUniqueId) {
576 return getDeviceContext().getAssociatedViewport();
577 }
578
Michael Wright227c5542020-07-02 18:30:52 +0100579 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800580 std::optional<DisplayViewport> viewport =
581 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
582 if (viewport) {
583 return viewport;
584 } else {
585 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
586 mConfig.defaultPointerDisplayId);
587 }
588 }
589
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700590 // Check if uniqueDisplayId is specified in idc file.
591 if (!mParameters.uniqueDisplayId.empty()) {
592 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
593 }
594
595 ViewportType viewportTypeToUse;
596 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100597 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700598 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100599 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700600 }
601
602 std::optional<DisplayViewport> viewport =
603 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100604 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700605 ALOGW("Input device %s should be associated with external display, "
606 "fallback to internal one for the external viewport is not found.",
607 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100608 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700609 }
610
611 return viewport;
612 }
613
614 // No associated display, return a non-display viewport.
615 DisplayViewport newViewport;
616 // Raw width and height in the natural orientation.
617 int32_t rawWidth = mRawPointerAxes.getRawWidth();
618 int32_t rawHeight = mRawPointerAxes.getRawHeight();
619 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
620 return std::make_optional(newViewport);
621}
622
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800623int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
624 if (resolution < 0) {
625 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
626 getDeviceName().c_str());
627 return 0;
628 }
629 return resolution;
630}
631
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800632void TouchInputMapper::initializeSizeRanges() {
633 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
634 mSizeScale = 0.0f;
635 return;
636 }
637
638 // Size of diagonal axis.
639 const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
640
641 // Size factors.
642 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
643 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
644 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
645 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
646 } else {
647 mSizeScale = 0.0f;
648 }
649
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700650 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
651 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
652 .source = mSource,
653 .min = 0,
654 .max = diagonalSize,
655 .flat = 0,
656 .fuzz = 0,
657 .resolution = 0,
658 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800659
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800660 if (mRawPointerAxes.touchMajor.valid) {
661 mRawPointerAxes.touchMajor.resolution =
662 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700663 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800664 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800665
666 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700667 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800668 if (mRawPointerAxes.touchMinor.valid) {
669 mRawPointerAxes.touchMinor.resolution =
670 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700671 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800672 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800673
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700674 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
675 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
676 .source = mSource,
677 .min = 0,
678 .max = diagonalSize,
679 .flat = 0,
680 .fuzz = 0,
681 .resolution = 0,
682 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800683 if (mRawPointerAxes.toolMajor.valid) {
684 mRawPointerAxes.toolMajor.resolution =
685 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700686 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800687 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800688
689 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700690 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800691 if (mRawPointerAxes.toolMinor.valid) {
692 mRawPointerAxes.toolMinor.resolution =
693 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700694 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800695 }
696
697 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700698 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
699 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
700 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
701 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800702 } else {
703 // Support for other calibrations can be added here.
704 ALOGW("%s calibration is not supported for size ranges at the moment. "
705 "Using raw resolution instead",
706 ftl::enum_string(mCalibration.sizeCalibration).c_str());
707 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800708
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700709 mOrientedRanges.size = InputDeviceInfo::MotionRange{
710 .axis = AMOTION_EVENT_AXIS_SIZE,
711 .source = mSource,
712 .min = 0,
713 .max = 1.0,
714 .flat = 0,
715 .fuzz = 0,
716 .resolution = 0,
717 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800718}
719
720void TouchInputMapper::initializeOrientedRanges() {
721 // Configure X and Y factors.
722 mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
723 mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
724 mXPrecision = 1.0f / mXScale;
725 mYPrecision = 1.0f / mYScale;
726
727 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
728 mOrientedRanges.x.source = mSource;
729 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
730 mOrientedRanges.y.source = mSource;
731
732 // Scale factor for terms that are not oriented in a particular axis.
733 // If the pixels are square then xScale == yScale otherwise we fake it
734 // by choosing an average.
735 mGeometricScale = avg(mXScale, mYScale);
736
737 initializeSizeRanges();
738
739 // Pressure factors.
740 mPressureScale = 0;
741 float pressureMax = 1.0;
742 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
743 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700744 if (mCalibration.pressureScale) {
745 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800746 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
747 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
748 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
749 }
750 }
751
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700752 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
753 .axis = AMOTION_EVENT_AXIS_PRESSURE,
754 .source = mSource,
755 .min = 0,
756 .max = pressureMax,
757 .flat = 0,
758 .fuzz = 0,
759 .resolution = 0,
760 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800761
762 // Tilt
763 mTiltXCenter = 0;
764 mTiltXScale = 0;
765 mTiltYCenter = 0;
766 mTiltYScale = 0;
767 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
768 if (mHaveTilt) {
769 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
770 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
771 mTiltXScale = M_PI / 180;
772 mTiltYScale = M_PI / 180;
773
774 if (mRawPointerAxes.tiltX.resolution) {
775 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
776 }
777 if (mRawPointerAxes.tiltY.resolution) {
778 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
779 }
780
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700781 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
782 .axis = AMOTION_EVENT_AXIS_TILT,
783 .source = mSource,
784 .min = 0,
785 .max = M_PI_2,
786 .flat = 0,
787 .fuzz = 0,
788 .resolution = 0,
789 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800790 }
791
792 // Orientation
793 mOrientationScale = 0;
794 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700795 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
796 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
797 .source = mSource,
798 .min = -M_PI,
799 .max = M_PI,
800 .flat = 0,
801 .fuzz = 0,
802 .resolution = 0,
803 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800804
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800805 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
806 if (mCalibration.orientationCalibration ==
807 Calibration::OrientationCalibration::INTERPOLATED) {
808 if (mRawPointerAxes.orientation.valid) {
809 if (mRawPointerAxes.orientation.maxValue > 0) {
810 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
811 } else if (mRawPointerAxes.orientation.minValue < 0) {
812 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
813 } else {
814 mOrientationScale = 0;
815 }
816 }
817 }
818
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700819 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
820 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
821 .source = mSource,
822 .min = -M_PI_2,
823 .max = M_PI_2,
824 .flat = 0,
825 .fuzz = 0,
826 .resolution = 0,
827 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800828 }
829
830 // Distance
831 mDistanceScale = 0;
832 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
833 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700834 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800835 }
836
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700837 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800838
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700839 .axis = AMOTION_EVENT_AXIS_DISTANCE,
840 .source = mSource,
841 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
842 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
843 .flat = 0,
844 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
845 .resolution = 0,
846 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800847 }
848
849 // Compute oriented precision, scales and ranges.
850 // Note that the maximum value reported is an inclusive maximum value so it is one
851 // unit less than the total width or height of the display.
852 switch (mInputDeviceOrientation) {
853 case DISPLAY_ORIENTATION_90:
854 case DISPLAY_ORIENTATION_270:
855 mOrientedXPrecision = mYPrecision;
856 mOrientedYPrecision = mXPrecision;
857
858 mOrientedRanges.x.min = 0;
859 mOrientedRanges.x.max = mDisplayHeight - 1;
860 mOrientedRanges.x.flat = 0;
861 mOrientedRanges.x.fuzz = 0;
862 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
863
864 mOrientedRanges.y.min = 0;
865 mOrientedRanges.y.max = mDisplayWidth - 1;
866 mOrientedRanges.y.flat = 0;
867 mOrientedRanges.y.fuzz = 0;
868 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
869 break;
870
871 default:
872 mOrientedXPrecision = mXPrecision;
873 mOrientedYPrecision = mYPrecision;
874
875 mOrientedRanges.x.min = 0;
876 mOrientedRanges.x.max = mDisplayWidth - 1;
877 mOrientedRanges.x.flat = 0;
878 mOrientedRanges.x.fuzz = 0;
879 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
880
881 mOrientedRanges.y.min = 0;
882 mOrientedRanges.y.max = mDisplayHeight - 1;
883 mOrientedRanges.y.flat = 0;
884 mOrientedRanges.y.fuzz = 0;
885 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
886 break;
887 }
888}
889
Prabir Pradhan1728b212021-10-19 16:00:03 -0700890void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000891 const DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700892
893 resolveExternalStylusPresence();
894
895 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100896 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000897 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700898 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100899 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700900 if (hasStylus()) {
901 mSource |= AINPUT_SOURCE_STYLUS;
902 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800903 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700904 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100905 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700906 if (hasStylus()) {
907 mSource |= AINPUT_SOURCE_STYLUS;
908 }
909 if (hasExternalStylus()) {
910 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
911 }
Michael Wright227c5542020-07-02 18:30:52 +0100912 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700913 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100914 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700915 } else {
916 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100917 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700918 }
919
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000920 const std::optional<DisplayViewport> newViewportOpt = findViewport();
921
922 // Ensure the device is valid and can be used.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700923 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
924 ALOGW("Touch device '%s' did not report support for X or Y axis! "
925 "The device will be inoperable.",
926 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100927 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000928 } else if (!newViewportOpt) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700929 ALOGI("Touch device '%s' could not query the properties of its associated "
930 "display. The device will be inoperable until the display size "
931 "becomes available.",
932 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100933 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000934 } else if (!newViewportOpt->isActive) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000935 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
936 getDeviceName().c_str(), getDeviceId());
937 mDeviceMode = DeviceMode::DISABLED;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000938 }
939
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700940 // Raw width and height in the natural orientation.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700941 const int32_t rawWidth = mRawPointerAxes.getRawWidth();
942 const int32_t rawHeight = mRawPointerAxes.getRawHeight();
HQ Liue6983c72022-04-19 22:14:56 +0000943 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
944 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
945 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
946 const float rawMeanResolution =
947 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700948
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000949 const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport);
950 const bool viewportChanged = mViewport != newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700951 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700952 if (viewportChanged) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000953 const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation;
954 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId;
955 mViewport = newViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700956
Michael Wright227c5542020-07-02 18:30:52 +0100957 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700958 // Convert rotated viewport to the natural orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700959 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
960 int32_t naturalPhysicalLeft, naturalPhysicalTop;
961 int32_t naturalDeviceWidth, naturalDeviceHeight;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700962
Prabir Pradhan1728b212021-10-19 16:00:03 -0700963 // Apply the inverse of the input device orientation so that the input device is
964 // configured in the same orientation as the viewport. The input device orientation will
965 // be re-applied by mInputDeviceOrientation.
966 const int32_t naturalDeviceOrientation =
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700967 (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
Prabir Pradhan1728b212021-10-19 16:00:03 -0700968 switch (naturalDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700969 case DISPLAY_ORIENTATION_90:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700970 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
971 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800972 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700973 naturalPhysicalTop = mViewport.physicalLeft;
974 naturalDeviceWidth = mViewport.deviceHeight;
975 naturalDeviceHeight = mViewport.deviceWidth;
976 break;
977 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700978 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
979 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
980 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
981 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
982 naturalDeviceWidth = mViewport.deviceWidth;
983 naturalDeviceHeight = mViewport.deviceHeight;
984 break;
985 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700986 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
987 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
988 naturalPhysicalLeft = mViewport.physicalTop;
Arthur Hung4197f6b2020-03-16 15:39:59 +0800989 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700990 naturalDeviceWidth = mViewport.deviceHeight;
991 naturalDeviceHeight = mViewport.deviceWidth;
992 break;
993 case DISPLAY_ORIENTATION_0:
994 default:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700995 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
996 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
997 naturalPhysicalLeft = mViewport.physicalLeft;
998 naturalPhysicalTop = mViewport.physicalTop;
999 naturalDeviceWidth = mViewport.deviceWidth;
1000 naturalDeviceHeight = mViewport.deviceHeight;
1001 break;
1002 }
1003
1004 if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
1005 ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
1006 naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
1007 naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
1008 }
1009
1010 mPhysicalWidth = naturalPhysicalWidth;
1011 mPhysicalHeight = naturalPhysicalHeight;
1012 mPhysicalLeft = naturalPhysicalLeft;
1013 mPhysicalTop = naturalPhysicalTop;
1014
Prabir Pradhan1728b212021-10-19 16:00:03 -07001015 const int32_t oldDisplayWidth = mDisplayWidth;
1016 const int32_t oldDisplayHeight = mDisplayHeight;
1017 mDisplayWidth = naturalDeviceWidth;
1018 mDisplayHeight = naturalDeviceHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07001019
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001020 // InputReader works in the un-rotated display coordinate space, so we don't need to do
1021 // anything if the device is already orientation-aware. If the device is not
1022 // orientation-aware, then we need to apply the inverse rotation of the display so that
1023 // when the display rotation is applied later as a part of the per-window transform, we
1024 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001025 mInputDeviceOrientation = mParameters.orientationAware
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00001026 ? DISPLAY_ORIENTATION_0
1027 : getInverseRotation(mViewport.orientation);
1028 // For orientation-aware devices that work in the un-rotated coordinate space, the
1029 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00001030 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
1031 mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight &&
1032 viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07001033
1034 // Apply the input device orientation for the device.
Prabir Pradhan1728b212021-10-19 16:00:03 -07001035 mInputDeviceOrientation =
1036 (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001037 } else {
1038 mPhysicalWidth = rawWidth;
1039 mPhysicalHeight = rawHeight;
1040 mPhysicalLeft = 0;
1041 mPhysicalTop = 0;
1042
Prabir Pradhan1728b212021-10-19 16:00:03 -07001043 mDisplayWidth = rawWidth;
1044 mDisplayHeight = rawHeight;
1045 mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001046 }
1047 }
1048
1049 // If moving between pointer modes, need to reset some state.
1050 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
1051 if (deviceModeChanged) {
1052 mOrientedRanges.clear();
1053 }
1054
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001055 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
1056 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +01001057 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001058 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001059 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
1060 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001061 if (mPointerController == nullptr) {
1062 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001063 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001064 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -08001065 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
1066 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001067 } else {
lilinnandef700b2022-06-17 19:32:01 +08001068 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
1069 !mConfig.showTouches) {
1070 mPointerController->clearSpots();
1071 }
Michael Wright17db18e2020-06-26 20:51:44 +01001072 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001073 }
1074
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001075 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001076 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
1077 "display id %d",
Prabir Pradhan1728b212021-10-19 16:00:03 -07001078 getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
1079 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001080
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001081 configureVirtualKeys();
1082
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001083 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001084
1085 // Location
1086 updateAffineTransformation();
1087
Michael Wright227c5542020-07-02 18:30:52 +01001088 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001089 // Compute pointer gesture detection parameters.
1090 float rawDiagonal = hypotf(rawWidth, rawHeight);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001091 float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001092
1093 // Scale movements such that one whole swipe of the touch pad covers a
1094 // given area relative to the diagonal size of the display when no acceleration
1095 // is applied.
1096 // Assume that the touch pad has a square aspect ratio such that movements in
1097 // X and Y of the same number of raw units cover the same physical distance.
1098 mPointerXMovementScale =
1099 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1100 mPointerYMovementScale = mPointerXMovementScale;
1101
1102 // Scale zooms to cover a smaller range of the display than movements do.
1103 // This value determines the area around the pointer that is affected by freeform
1104 // pointer gestures.
1105 mPointerXZoomScale =
1106 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1107 mPointerYZoomScale = mPointerXZoomScale;
1108
HQ Liue6983c72022-04-19 22:14:56 +00001109 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1110 // axis is non positive value.
1111 const float minFreeformGestureWidth =
1112 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1113
1114 mPointerGestureMaxSwipeWidth =
1115 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1116 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001117 }
1118
1119 // Inform the dispatcher about the changes.
1120 *outResetNeeded = true;
1121 bumpGeneration();
1122 }
1123}
1124
Prabir Pradhan1728b212021-10-19 16:00:03 -07001125void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001126 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001127 dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
1128 dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001129 dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
1130 dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
1131 dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
1132 dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001133 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001134}
1135
1136void TouchInputMapper::configureVirtualKeys() {
1137 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001138 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001139
1140 mVirtualKeys.clear();
1141
1142 if (virtualKeyDefinitions.size() == 0) {
1143 return;
1144 }
1145
1146 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1147 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1148 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1149 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1150
1151 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1152 VirtualKey virtualKey;
1153
1154 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1155 int32_t keyCode;
1156 int32_t dummyKeyMetaState;
1157 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001158 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1159 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001160 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1161 continue; // drop the key
1162 }
1163
1164 virtualKey.keyCode = keyCode;
1165 virtualKey.flags = flags;
1166
1167 // convert the key definition's display coordinates into touch coordinates for a hit box
1168 int32_t halfWidth = virtualKeyDefinition.width / 2;
1169 int32_t halfHeight = virtualKeyDefinition.height / 2;
1170
1171 virtualKey.hitLeft =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001172 (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001173 touchScreenLeft;
1174 virtualKey.hitRight =
Prabir Pradhan1728b212021-10-19 16:00:03 -07001175 (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001176 touchScreenLeft;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001177 virtualKey.hitTop =
1178 (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001179 touchScreenTop;
Prabir Pradhan1728b212021-10-19 16:00:03 -07001180 virtualKey.hitBottom =
1181 (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001182 touchScreenTop;
1183 mVirtualKeys.push_back(virtualKey);
1184 }
1185}
1186
1187void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1188 if (!mVirtualKeys.empty()) {
1189 dump += INDENT3 "Virtual Keys:\n";
1190
1191 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1192 const VirtualKey& virtualKey = mVirtualKeys[i];
1193 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1194 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1195 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1196 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1197 }
1198 }
1199}
1200
1201void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001202 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001203 Calibration& out = mCalibration;
1204
1205 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001206 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001207 std::string sizeCalibrationString;
1208 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001209 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001210 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001211 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001212 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001213 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001214 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001215 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001216 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001217 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001218 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001219 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001220 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001221 }
1222 }
1223
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001224 float sizeScale;
1225
1226 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1227 out.sizeScale = sizeScale;
1228 }
1229 float sizeBias;
1230 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1231 out.sizeBias = sizeBias;
1232 }
1233 bool sizeIsSummed;
1234 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1235 out.sizeIsSummed = sizeIsSummed;
1236 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001237
1238 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001239 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001240 std::string pressureCalibrationString;
1241 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001242 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001243 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001244 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001245 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001246 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001247 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001248 } else if (pressureCalibrationString != "default") {
1249 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001250 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001251 }
1252 }
1253
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001254 float pressureScale;
1255 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1256 out.pressureScale = pressureScale;
1257 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001258
1259 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001260 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001261 std::string orientationCalibrationString;
1262 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001263 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001264 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001265 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001266 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001267 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001268 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001269 } else if (orientationCalibrationString != "default") {
1270 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001271 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001272 }
1273 }
1274
1275 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001276 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001277 std::string distanceCalibrationString;
1278 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001279 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001280 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001281 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001282 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001283 } else if (distanceCalibrationString != "default") {
1284 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001285 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001286 }
1287 }
1288
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001289 float distanceScale;
1290 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1291 out.distanceScale = distanceScale;
1292 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001293
Michael Wright227c5542020-07-02 18:30:52 +01001294 out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001295 std::string coverageCalibrationString;
1296 if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001297 if (coverageCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001298 out.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001299 } else if (coverageCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001300 out.coverageCalibration = Calibration::CoverageCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001301 } else if (coverageCalibrationString != "default") {
1302 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001303 coverageCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001304 }
1305 }
1306}
1307
1308void TouchInputMapper::resolveCalibration() {
1309 // Size
1310 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001311 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1312 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001313 }
1314 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001315 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001316 }
1317
1318 // Pressure
1319 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001320 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1321 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001322 }
1323 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001324 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001325 }
1326
1327 // Orientation
1328 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001329 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1330 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001331 }
1332 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001333 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001334 }
1335
1336 // Distance
1337 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001338 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1339 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001340 }
1341 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001342 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001343 }
1344
1345 // Coverage
Michael Wright227c5542020-07-02 18:30:52 +01001346 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) {
1347 mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001348 }
1349}
1350
1351void TouchInputMapper::dumpCalibration(std::string& dump) {
1352 dump += INDENT3 "Calibration:\n";
1353
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001354 dump += INDENT4 "touch.size.calibration: ";
1355 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001356
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001357 if (mCalibration.sizeScale) {
1358 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001359 }
1360
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001361 if (mCalibration.sizeBias) {
1362 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001363 }
1364
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001365 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001366 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001367 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001368 }
1369
1370 // Pressure
1371 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001372 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001373 dump += INDENT4 "touch.pressure.calibration: none\n";
1374 break;
Michael Wright227c5542020-07-02 18:30:52 +01001375 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001376 dump += INDENT4 "touch.pressure.calibration: physical\n";
1377 break;
Michael Wright227c5542020-07-02 18:30:52 +01001378 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001379 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1380 break;
1381 default:
1382 ALOG_ASSERT(false);
1383 }
1384
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001385 if (mCalibration.pressureScale) {
1386 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001387 }
1388
1389 // Orientation
1390 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001391 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001392 dump += INDENT4 "touch.orientation.calibration: none\n";
1393 break;
Michael Wright227c5542020-07-02 18:30:52 +01001394 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001395 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1396 break;
Michael Wright227c5542020-07-02 18:30:52 +01001397 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001398 dump += INDENT4 "touch.orientation.calibration: vector\n";
1399 break;
1400 default:
1401 ALOG_ASSERT(false);
1402 }
1403
1404 // Distance
1405 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001406 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001407 dump += INDENT4 "touch.distance.calibration: none\n";
1408 break;
Michael Wright227c5542020-07-02 18:30:52 +01001409 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001410 dump += INDENT4 "touch.distance.calibration: scaled\n";
1411 break;
1412 default:
1413 ALOG_ASSERT(false);
1414 }
1415
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001416 if (mCalibration.distanceScale) {
1417 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001418 }
1419
1420 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001421 case Calibration::CoverageCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001422 dump += INDENT4 "touch.coverage.calibration: none\n";
1423 break;
Michael Wright227c5542020-07-02 18:30:52 +01001424 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001425 dump += INDENT4 "touch.coverage.calibration: box\n";
1426 break;
1427 default:
1428 ALOG_ASSERT(false);
1429 }
1430}
1431
1432void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1433 dump += INDENT3 "Affine Transformation:\n";
1434
1435 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1436 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1437 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1438 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1439 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1440 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1441}
1442
1443void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001444 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001445 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001446}
1447
1448void TouchInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001449 mCursorButtonAccumulator.reset(getDeviceContext());
1450 mCursorScrollAccumulator.reset(getDeviceContext());
1451 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001452
1453 mPointerVelocityControl.reset();
1454 mWheelXVelocityControl.reset();
1455 mWheelYVelocityControl.reset();
1456
1457 mRawStatesPending.clear();
1458 mCurrentRawState.clear();
1459 mCurrentCookedState.clear();
1460 mLastRawState.clear();
1461 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001462 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001463 mSentHoverEnter = false;
1464 mHavePointerIds = false;
1465 mCurrentMotionAborted = false;
1466 mDownTime = 0;
1467
1468 mCurrentVirtualKey.down = false;
1469
1470 mPointerGesture.reset();
1471 mPointerSimple.reset();
1472 resetExternalStylus();
1473
1474 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001475 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001476 mPointerController->clearSpots();
1477 }
1478
1479 InputMapper::reset(when);
1480}
1481
1482void TouchInputMapper::resetExternalStylus() {
1483 mExternalStylusState.clear();
1484 mExternalStylusId = -1;
1485 mExternalStylusFusionTimeout = LLONG_MAX;
1486 mExternalStylusDataPending = false;
1487}
1488
1489void TouchInputMapper::clearStylusDataPendingFlags() {
1490 mExternalStylusDataPending = false;
1491 mExternalStylusFusionTimeout = LLONG_MAX;
1492}
1493
1494void TouchInputMapper::process(const RawEvent* rawEvent) {
1495 mCursorButtonAccumulator.process(rawEvent);
1496 mCursorScrollAccumulator.process(rawEvent);
1497 mTouchButtonAccumulator.process(rawEvent);
1498
1499 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001500 sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001501 }
1502}
1503
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001504void TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001505 // Push a new state.
1506 mRawStatesPending.emplace_back();
1507
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001508 RawState& next = mRawStatesPending.back();
1509 next.clear();
1510 next.when = when;
1511 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001512
1513 // Sync button state.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001514 next.buttonState =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001515 mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState();
1516
1517 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001518 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1519 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001520 mCursorScrollAccumulator.finishSync();
1521
1522 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001523 syncTouch(when, &next);
1524
1525 // The last RawState is the actually second to last, since we just added a new state
1526 const RawState& last =
1527 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001528
1529 // Assign pointer ids.
1530 if (!mHavePointerIds) {
1531 assignPointerIds(last, next);
1532 }
1533
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001534 if (DEBUG_RAW_EVENTS) {
1535 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1536 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1537 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1538 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1539 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1540 next.rawPointerData.canceledIdBits.value);
1541 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001542
Arthur Hung9ad18942021-06-19 02:04:46 +00001543 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1544 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1545 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1546 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1547 next.rawPointerData.hoveringIdBits.value);
1548 }
1549
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001550 processRawTouches(false /*timeout*/);
1551}
1552
1553void TouchInputMapper::processRawTouches(bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001554 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001555 // Drop all input if the device is disabled.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001556 cancelTouch(mCurrentRawState.when, mCurrentRawState.readTime);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001557 mCurrentCookedState.clear();
1558 updateTouchSpots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001559 return;
1560 }
1561
1562 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1563 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1564 // touching the current state will only observe the events that have been dispatched to the
1565 // rest of the pipeline.
1566 const size_t N = mRawStatesPending.size();
1567 size_t count;
1568 for (count = 0; count < N; count++) {
1569 const RawState& next = mRawStatesPending[count];
1570
1571 // A failure to assign the stylus id means that we're waiting on stylus data
1572 // and so should defer the rest of the pipeline.
1573 if (assignExternalStylusId(next, timeout)) {
1574 break;
1575 }
1576
1577 // All ready to go.
1578 clearStylusDataPendingFlags();
1579 mCurrentRawState.copyFrom(next);
1580 if (mCurrentRawState.when < mLastRawState.when) {
1581 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001582 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001583 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001584 cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001585 }
1586 if (count != 0) {
1587 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1588 }
1589
1590 if (mExternalStylusDataPending) {
1591 if (timeout) {
1592 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1593 clearStylusDataPendingFlags();
1594 mCurrentRawState.copyFrom(mLastRawState);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001595 if (DEBUG_STYLUS_FUSION) {
1596 ALOGD("Timeout expired, synthesizing event with new stylus data");
1597 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001598 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
1599 cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001600 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1601 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1602 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1603 }
1604 }
1605}
1606
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001607void TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001608 // Always start with a clean state.
1609 mCurrentCookedState.clear();
1610
1611 // Apply stylus buttons to current raw state.
1612 applyExternalStylusButtonState(when);
1613
1614 // Handle policy on initial down or hover events.
1615 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1616 mCurrentRawState.rawPointerData.pointerCount != 0;
1617
1618 uint32_t policyFlags = 0;
1619 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1620 if (initialDown || buttonsPressed) {
1621 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001622 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001623 getContext()->fadePointer();
1624 }
1625
1626 if (mParameters.wake) {
1627 policyFlags |= POLICY_FLAG_WAKE;
1628 }
1629 }
1630
1631 // Consume raw off-screen touches before cooking pointer data.
1632 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001633 if (consumeRawTouches(when, readTime, policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001634 mCurrentRawState.rawPointerData.clear();
1635 }
1636
1637 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1638 // with cooked pointer data that has the same ids and indices as the raw data.
1639 // The following code can use either the raw or cooked data, as needed.
1640 cookPointerData();
1641
1642 // Apply stylus pressure to current cooked state.
1643 applyExternalStylusTouchState(when);
1644
1645 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001646 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1647 mSource, mViewport.displayId, policyFlags, mLastCookedState.buttonState,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001648 mCurrentCookedState.buttonState);
1649
1650 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001651 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001652 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1653 uint32_t id = idBits.clearFirstMarkedBit();
1654 const RawPointerData::Pointer& pointer =
1655 mCurrentRawState.rawPointerData.pointerForId(id);
1656 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1657 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1658 mCurrentCookedState.stylusIdBits.markBit(id);
1659 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1660 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1661 mCurrentCookedState.fingerIdBits.markBit(id);
1662 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1663 mCurrentCookedState.mouseIdBits.markBit(id);
1664 }
1665 }
1666 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1667 uint32_t id = idBits.clearFirstMarkedBit();
1668 const RawPointerData::Pointer& pointer =
1669 mCurrentRawState.rawPointerData.pointerForId(id);
1670 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
1671 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
1672 mCurrentCookedState.stylusIdBits.markBit(id);
1673 }
1674 }
1675
1676 // Stylus takes precedence over all tools, then mouse, then finger.
1677 PointerUsage pointerUsage = mPointerUsage;
1678 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1679 mCurrentCookedState.mouseIdBits.clear();
1680 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001681 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001682 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1683 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001684 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001685 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1686 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001687 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001688 }
1689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001690 dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001691 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001692 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001693 updateTouchSpots();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001694 dispatchButtonRelease(when, readTime, policyFlags);
1695 dispatchHoverExit(when, readTime, policyFlags);
1696 dispatchTouches(when, readTime, policyFlags);
1697 dispatchHoverEnterAndMove(when, readTime, policyFlags);
1698 dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001699 }
1700
1701 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1702 mCurrentMotionAborted = false;
1703 }
1704 }
1705
1706 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001707 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), mSource,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001708 mViewport.displayId, policyFlags, mLastCookedState.buttonState,
1709 mCurrentCookedState.buttonState);
1710
1711 // Clear some transient state.
1712 mCurrentRawState.rawVScroll = 0;
1713 mCurrentRawState.rawHScroll = 0;
1714
1715 // Copy current touch to last touch in preparation for the next cycle.
1716 mLastRawState.copyFrom(mCurrentRawState);
1717 mLastCookedState.copyFrom(mCurrentCookedState);
1718}
1719
Garfield Tanc734e4f2021-01-15 20:01:39 -08001720void TouchInputMapper::updateTouchSpots() {
1721 if (!mConfig.showTouches || mPointerController == nullptr) {
1722 return;
1723 }
1724
1725 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1726 // clear touch spots.
1727 if (mDeviceMode != DeviceMode::DIRECT &&
1728 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1729 return;
1730 }
1731
1732 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1733 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1734
1735 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001736 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
1737 mCurrentCookedState.cookedPointerData.idToIndex,
1738 mCurrentCookedState.cookedPointerData.touchingIdBits,
1739 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001740}
1741
1742bool TouchInputMapper::isTouchScreen() {
1743 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1744 mParameters.hasAssociatedDisplay;
1745}
1746
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001747void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001748 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001749 mCurrentRawState.buttonState |= mExternalStylusState.buttons;
1750 }
1751}
1752
1753void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1754 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1755 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
1756
1757 if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
1758 float pressure = mExternalStylusState.pressure;
1759 if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
1760 const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
1761 pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
1762 }
1763 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
1764 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
1765
1766 PointerProperties& properties =
1767 currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
1768 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1769 properties.toolType = mExternalStylusState.toolType;
1770 }
1771 }
1772}
1773
1774bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001775 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001776 return false;
1777 }
1778
1779 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1780 state.rawPointerData.pointerCount != 0;
1781 if (initialDown) {
1782 if (mExternalStylusState.pressure != 0.0f) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001783 if (DEBUG_STYLUS_FUSION) {
1784 ALOGD("Have both stylus and touch data, beginning fusion");
1785 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001786 mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1787 } else if (timeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001788 if (DEBUG_STYLUS_FUSION) {
1789 ALOGD("Timeout expired, assuming touch is not a stylus.");
1790 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001791 resetExternalStylus();
1792 } else {
1793 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1794 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1795 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001796 if (DEBUG_STYLUS_FUSION) {
1797 ALOGD("No stylus data but stylus is connected, requesting timeout "
1798 "(%" PRId64 "ms)",
1799 mExternalStylusFusionTimeout);
1800 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001801 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1802 return true;
1803 }
1804 }
1805
1806 // Check if the stylus pointer has gone up.
1807 if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001808 if (DEBUG_STYLUS_FUSION) {
1809 ALOGD("Stylus pointer is going up");
1810 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001811 mExternalStylusId = -1;
1812 }
1813
1814 return false;
1815}
1816
1817void TouchInputMapper::timeoutExpired(nsecs_t when) {
Michael Wright227c5542020-07-02 18:30:52 +01001818 if (mDeviceMode == DeviceMode::POINTER) {
1819 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001820 // Since this is a synthetic event, we can consider its latency to be zero
1821 const nsecs_t readTime = when;
1822 dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001823 }
Michael Wright227c5542020-07-02 18:30:52 +01001824 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001825 if (mExternalStylusFusionTimeout < when) {
1826 processRawTouches(true /*timeout*/);
1827 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1828 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1829 }
1830 }
1831}
1832
1833void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1834 mExternalStylusState.copyFrom(state);
1835 if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
1836 // We're either in the middle of a fused stream of data or we're waiting on data before
1837 // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
1838 // data.
1839 mExternalStylusDataPending = true;
1840 processRawTouches(false /*timeout*/);
1841 }
1842}
1843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001844bool TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001845 // Check for release of a virtual key.
1846 if (mCurrentVirtualKey.down) {
1847 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1848 // Pointer went up while virtual key was down.
1849 mCurrentVirtualKey.down = false;
1850 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001851 if (DEBUG_VIRTUAL_KEYS) {
1852 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1853 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1854 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001855 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001856 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1857 }
1858 return true;
1859 }
1860
1861 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1862 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1863 const RawPointerData::Pointer& pointer =
1864 mCurrentRawState.rawPointerData.pointerForId(id);
1865 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1866 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1867 // Pointer is still within the space of the virtual key.
1868 return true;
1869 }
1870 }
1871
1872 // Pointer left virtual key area or another pointer also went down.
1873 // Send key cancellation but do not consume the touch yet.
1874 // This is useful when the user swipes through from the virtual key area
1875 // into the main display surface.
1876 mCurrentVirtualKey.down = false;
1877 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001878 if (DEBUG_VIRTUAL_KEYS) {
1879 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1880 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1881 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001882 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001883 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1884 AKEY_EVENT_FLAG_CANCELED);
1885 }
1886 }
1887
1888 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1889 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1890 // Pointer just went down. Check for virtual key press or off-screen touches.
1891 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1892 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001893 // Skip checking whether the pointer is inside the physical frame if the device is in
1894 // unscaled mode.
1895 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1896 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001897 // If exactly one pointer went down, check for virtual key hit.
1898 // Otherwise we will drop the entire stroke.
1899 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1900 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1901 if (virtualKey) {
1902 mCurrentVirtualKey.down = true;
1903 mCurrentVirtualKey.downTime = when;
1904 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1905 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1906 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001907 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1908 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001909
1910 if (!mCurrentVirtualKey.ignored) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08001911 if (DEBUG_VIRTUAL_KEYS) {
1912 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1913 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
1914 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001915 dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_DOWN,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001916 AKEY_EVENT_FLAG_FROM_SYSTEM |
1917 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1918 }
1919 }
1920 }
1921 return true;
1922 }
1923 }
1924
1925 // Disable all virtual key touches that happen within a short time interval of the
1926 // most recent touch within the screen area. The idea is to filter out stray
1927 // virtual key presses when interacting with the touch screen.
1928 //
1929 // Problems we're trying to solve:
1930 //
1931 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1932 // virtual key area that is implemented by a separate touch panel and accidentally
1933 // triggers a virtual key.
1934 //
1935 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1936 // area and accidentally triggers a virtual key. This often happens when virtual keys
1937 // are layed out below the screen near to where the on screen keyboard's space bar
1938 // is displayed.
1939 if (mConfig.virtualKeyQuietTime > 0 &&
1940 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001941 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001942 }
1943 return false;
1944}
1945
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001946void TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001947 int32_t keyEventAction, int32_t keyEventFlags) {
1948 int32_t keyCode = mCurrentVirtualKey.keyCode;
1949 int32_t scanCode = mCurrentVirtualKey.scanCode;
1950 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001951 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001952 policyFlags |= POLICY_FLAG_VIRTUAL;
1953
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001954 NotifyKeyArgs args(getContext()->getNextId(), when, readTime, getDeviceId(),
1955 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1956 keyEventFlags, keyCode, scanCode, metaState, downTime);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001957 getListener().notifyKey(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001958}
1959
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001960void TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
lilinnan687e58f2022-07-19 16:00:50 +08001961 if (mCurrentMotionAborted) {
1962 // Current motion event was already aborted.
1963 return;
1964 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001965 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1966 if (!currentIdBits.isEmpty()) {
1967 int32_t metaState = getContext()->getGlobalMetaState();
1968 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001969 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
1970 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001971 mCurrentCookedState.cookedPointerData.pointerProperties,
1972 mCurrentCookedState.cookedPointerData.pointerCoords,
1973 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1974 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1975 mCurrentMotionAborted = true;
1976 }
1977}
1978
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001979void TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001980 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1981 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1982 int32_t metaState = getContext()->getGlobalMetaState();
1983 int32_t buttonState = mCurrentCookedState.buttonState;
1984
1985 if (currentIdBits == lastIdBits) {
1986 if (!currentIdBits.isEmpty()) {
1987 // No pointer id changes so this is a move event.
1988 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001989 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
1990 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001991 mCurrentCookedState.cookedPointerData.pointerProperties,
1992 mCurrentCookedState.cookedPointerData.pointerCoords,
1993 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1,
1994 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
1995 }
1996 } else {
1997 // There may be pointers going up and pointers going down and pointers moving
1998 // all at the same time.
1999 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
2000 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
2001 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2002 BitSet32 dispatchedIdBits(lastIdBits.value);
2003
2004 // Update last coordinates of pointers that have moved so that we observe the new
2005 // pointer positions at the same time as other pointers that have just gone up.
2006 bool moveNeeded =
2007 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
2008 mCurrentCookedState.cookedPointerData.pointerCoords,
2009 mCurrentCookedState.cookedPointerData.idToIndex,
2010 mLastCookedState.cookedPointerData.pointerProperties,
2011 mLastCookedState.cookedPointerData.pointerCoords,
2012 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
2013 if (buttonState != mLastCookedState.buttonState) {
2014 moveNeeded = true;
2015 }
2016
2017 // Dispatch pointer up events.
2018 while (!upIdBits.isEmpty()) {
2019 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002020 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002021 if (isCanceled) {
2022 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2023 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002024 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0,
arthurhungcc7f9802020-04-30 17:55:40 +08002025 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002026 mLastCookedState.cookedPointerData.pointerProperties,
2027 mLastCookedState.cookedPointerData.pointerCoords,
2028 mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId,
2029 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2030 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002031 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002032 }
2033
2034 // Dispatch move events if any of the remaining pointers moved from their old locations.
2035 // Although applications receive new locations as part of individual pointer up
2036 // events, they do not generally handle them except when presented in a move event.
2037 if (moveNeeded && !moveIdBits.isEmpty()) {
2038 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002039 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0,
2040 metaState, buttonState, 0,
2041 mCurrentCookedState.cookedPointerData.pointerProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002042 mCurrentCookedState.cookedPointerData.pointerCoords,
2043 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1,
2044 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2045 }
2046
2047 // Dispatch pointer down events using the new pointer locations.
2048 while (!downIdBits.isEmpty()) {
2049 uint32_t downId = downIdBits.clearFirstMarkedBit();
2050 dispatchedIdBits.markBit(downId);
2051
2052 if (dispatchedIdBits.count() == 1) {
2053 // First pointer is going down. Set down time.
2054 mDownTime = when;
2055 }
2056
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002057 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
2058 0, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002059 mCurrentCookedState.cookedPointerData.pointerProperties,
2060 mCurrentCookedState.cookedPointerData.pointerCoords,
2061 mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits,
2062 downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2063 }
2064 }
2065}
2066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002067void TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002068 if (mSentHoverEnter &&
2069 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2070 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2071 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002072 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
2073 metaState, mLastCookedState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002074 mLastCookedState.cookedPointerData.pointerProperties,
2075 mLastCookedState.cookedPointerData.pointerCoords,
2076 mLastCookedState.cookedPointerData.idToIndex,
2077 mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision,
2078 mOrientedYPrecision, mDownTime);
2079 mSentHoverEnter = false;
2080 }
2081}
2082
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002083void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2084 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002085 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2086 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2087 int32_t metaState = getContext()->getGlobalMetaState();
2088 if (!mSentHoverEnter) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002089 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
2090 0, 0, metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002091 mCurrentCookedState.cookedPointerData.pointerProperties,
2092 mCurrentCookedState.cookedPointerData.pointerCoords,
2093 mCurrentCookedState.cookedPointerData.idToIndex,
2094 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2095 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2096 mSentHoverEnter = true;
2097 }
2098
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002099 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
2100 metaState, mCurrentRawState.buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002101 mCurrentCookedState.cookedPointerData.pointerProperties,
2102 mCurrentCookedState.cookedPointerData.pointerCoords,
2103 mCurrentCookedState.cookedPointerData.idToIndex,
2104 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2105 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2106 }
2107}
2108
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002109void TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002110 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2111 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2112 const int32_t metaState = getContext()->getGlobalMetaState();
2113 int32_t buttonState = mLastCookedState.buttonState;
2114 while (!releasedButtons.isEmpty()) {
2115 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2116 buttonState &= ~actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002117 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002118 actionButton, 0, metaState, buttonState, 0,
2119 mCurrentCookedState.cookedPointerData.pointerProperties,
2120 mCurrentCookedState.cookedPointerData.pointerCoords,
2121 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2122 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2123 }
2124}
2125
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002126void TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002127 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2128 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2129 const int32_t metaState = getContext()->getGlobalMetaState();
2130 int32_t buttonState = mLastCookedState.buttonState;
2131 while (!pressedButtons.isEmpty()) {
2132 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2133 buttonState |= actionButton;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002134 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2135 actionButton, 0, metaState, buttonState, 0,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002136 mCurrentCookedState.cookedPointerData.pointerProperties,
2137 mCurrentCookedState.cookedPointerData.pointerCoords,
2138 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2139 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
2140 }
2141}
2142
2143const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2144 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2145 return cookedPointerData.touchingIdBits;
2146 }
2147 return cookedPointerData.hoveringIdBits;
2148}
2149
2150void TouchInputMapper::cookPointerData() {
2151 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2152
2153 mCurrentCookedState.cookedPointerData.clear();
2154 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2155 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2156 mCurrentRawState.rawPointerData.hoveringIdBits;
2157 mCurrentCookedState.cookedPointerData.touchingIdBits =
2158 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002159 mCurrentCookedState.cookedPointerData.canceledIdBits =
2160 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002161
2162 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2163 mCurrentCookedState.buttonState = 0;
2164 } else {
2165 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2166 }
2167
2168 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002169 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002170 for (uint32_t i = 0; i < currentPointerCount; i++) {
2171 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2172
2173 // Size
2174 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2175 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002176 case Calibration::SizeCalibration::GEOMETRIC:
2177 case Calibration::SizeCalibration::DIAMETER:
2178 case Calibration::SizeCalibration::BOX:
2179 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002180 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2181 touchMajor = in.touchMajor;
2182 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2183 toolMajor = in.toolMajor;
2184 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2185 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2186 : in.touchMajor;
2187 } else if (mRawPointerAxes.touchMajor.valid) {
2188 toolMajor = touchMajor = in.touchMajor;
2189 toolMinor = touchMinor =
2190 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2191 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2192 : in.touchMajor;
2193 } else if (mRawPointerAxes.toolMajor.valid) {
2194 touchMajor = toolMajor = in.toolMajor;
2195 touchMinor = toolMinor =
2196 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2197 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2198 : in.toolMajor;
2199 } else {
2200 ALOG_ASSERT(false,
2201 "No touch or tool axes. "
2202 "Size calibration should have been resolved to NONE.");
2203 touchMajor = 0;
2204 touchMinor = 0;
2205 toolMajor = 0;
2206 toolMinor = 0;
2207 size = 0;
2208 }
2209
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002210 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002211 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2212 if (touchingCount > 1) {
2213 touchMajor /= touchingCount;
2214 touchMinor /= touchingCount;
2215 toolMajor /= touchingCount;
2216 toolMinor /= touchingCount;
2217 size /= touchingCount;
2218 }
2219 }
2220
Michael Wright227c5542020-07-02 18:30:52 +01002221 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002222 touchMajor *= mGeometricScale;
2223 touchMinor *= mGeometricScale;
2224 toolMajor *= mGeometricScale;
2225 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002226 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002227 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2228 touchMinor = touchMajor;
2229 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2230 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002231 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002232 touchMinor = touchMajor;
2233 toolMinor = toolMajor;
2234 }
2235
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002236 mCalibration.applySizeScaleAndBias(touchMajor);
2237 mCalibration.applySizeScaleAndBias(touchMinor);
2238 mCalibration.applySizeScaleAndBias(toolMajor);
2239 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002240 size *= mSizeScale;
2241 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002242 case Calibration::SizeCalibration::DEFAULT:
2243 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2244 break;
2245 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002246 touchMajor = 0;
2247 touchMinor = 0;
2248 toolMajor = 0;
2249 toolMinor = 0;
2250 size = 0;
2251 break;
2252 }
2253
2254 // Pressure
2255 float pressure;
2256 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002257 case Calibration::PressureCalibration::PHYSICAL:
2258 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002259 pressure = in.pressure * mPressureScale;
2260 break;
2261 default:
2262 pressure = in.isHovering ? 0 : 1;
2263 break;
2264 }
2265
2266 // Tilt and Orientation
2267 float tilt;
2268 float orientation;
2269 if (mHaveTilt) {
2270 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2271 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
2272 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
2273 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2274 } else {
2275 tilt = 0;
2276
2277 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002278 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002279 orientation = in.orientation * mOrientationScale;
2280 break;
Michael Wright227c5542020-07-02 18:30:52 +01002281 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002282 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2283 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2284 if (c1 != 0 || c2 != 0) {
2285 orientation = atan2f(c1, c2) * 0.5f;
2286 float confidence = hypotf(c1, c2);
2287 float scale = 1.0f + confidence / 16.0f;
2288 touchMajor *= scale;
2289 touchMinor /= scale;
2290 toolMajor *= scale;
2291 toolMinor /= scale;
2292 } else {
2293 orientation = 0;
2294 }
2295 break;
2296 }
2297 default:
2298 orientation = 0;
2299 }
2300 }
2301
2302 // Distance
2303 float distance;
2304 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002305 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002306 distance = in.distance * mDistanceScale;
2307 break;
2308 default:
2309 distance = 0;
2310 }
2311
2312 // Coverage
2313 int32_t rawLeft, rawTop, rawRight, rawBottom;
2314 switch (mCalibration.coverageCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002315 case Calibration::CoverageCalibration::BOX:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002316 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
2317 rawRight = in.toolMinor & 0x0000ffff;
2318 rawBottom = in.toolMajor & 0x0000ffff;
2319 rawTop = (in.toolMajor & 0xffff0000) >> 16;
2320 break;
2321 default:
2322 rawLeft = rawTop = rawRight = rawBottom = 0;
2323 break;
2324 }
2325
2326 // Adjust X,Y coords for device calibration
2327 // TODO: Adjust coverage coords?
2328 float xTransformed = in.x, yTransformed = in.y;
2329 mAffineTransform.applyTo(xTransformed, yTransformed);
Arthur Hung05de5772019-09-26 18:31:26 +08002330 rotateAndScale(xTransformed, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002331
Prabir Pradhan1728b212021-10-19 16:00:03 -07002332 // Adjust X, Y, and coverage coords for input device orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002333 float left, top, right, bottom;
2334
Prabir Pradhan1728b212021-10-19 16:00:03 -07002335 switch (mInputDeviceOrientation) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002336 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002337 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
2338 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2339 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
2340 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002341 orientation -= M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002342 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002343 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002344 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002345 }
2346 break;
2347 case DISPLAY_ORIENTATION_180:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002348 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
2349 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002350 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
2351 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002352 orientation -= M_PI;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002353 if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002354 orientation +=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002355 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002356 }
2357 break;
2358 case DISPLAY_ORIENTATION_270:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002359 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
2360 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07002361 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2362 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002363 orientation += M_PI_2;
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002364 if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002365 orientation -=
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002366 (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002367 }
2368 break;
2369 default:
Prabir Pradhan1728b212021-10-19 16:00:03 -07002370 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
2371 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
2372 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
2373 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002374 break;
2375 }
2376
2377 // Write output coords.
2378 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2379 out.clear();
Arthur Hung4197f6b2020-03-16 15:39:59 +08002380 out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed);
2381 out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002382 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2383 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2384 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2385 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2386 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2387 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2388 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright227c5542020-07-02 18:30:52 +01002389 if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002390 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
2391 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
2392 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
2393 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
2394 } else {
2395 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2396 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2397 }
2398
Chris Ye364fdb52020-08-05 15:07:56 -07002399 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002400 uint32_t id = in.id;
2401 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2402 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2403 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
2404 float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2405 float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
2406 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2407 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2408 }
2409
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002410 // Write output properties.
2411 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002412 properties.clear();
2413 properties.id = id;
2414 properties.toolType = in.toolType;
2415
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002416 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002417 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002418 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002419 }
2420}
2421
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002422void TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002423 PointerUsage pointerUsage) {
2424 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002425 abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002426 mPointerUsage = pointerUsage;
2427 }
2428
2429 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002430 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002431 dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002432 break;
Michael Wright227c5542020-07-02 18:30:52 +01002433 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002434 dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002435 break;
Michael Wright227c5542020-07-02 18:30:52 +01002436 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002437 dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002438 break;
Michael Wright227c5542020-07-02 18:30:52 +01002439 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002440 break;
2441 }
2442}
2443
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002444void TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002445 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002446 case PointerUsage::GESTURES:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002447 abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002448 break;
Michael Wright227c5542020-07-02 18:30:52 +01002449 case PointerUsage::STYLUS:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002450 abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002451 break;
Michael Wright227c5542020-07-02 18:30:52 +01002452 case PointerUsage::MOUSE:
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002453 abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002454 break;
Michael Wright227c5542020-07-02 18:30:52 +01002455 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002456 break;
2457 }
2458
Michael Wright227c5542020-07-02 18:30:52 +01002459 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002460}
2461
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002462void TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
2463 bool isTimeout) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002464 // Update current gesture coordinates.
2465 bool cancelPreviousGesture, finishPreviousGesture;
2466 bool sendEvents =
2467 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2468 if (!sendEvents) {
2469 return;
2470 }
2471 if (finishPreviousGesture) {
2472 cancelPreviousGesture = false;
2473 }
2474
2475 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002476 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002477 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002478 if (finishPreviousGesture || cancelPreviousGesture) {
2479 mPointerController->clearSpots();
2480 }
2481
Michael Wright227c5542020-07-02 18:30:52 +01002482 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002483 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
2484 mPointerGesture.currentGestureIdToIndex,
2485 mPointerGesture.currentGestureIdBits,
2486 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002487 }
2488 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002489 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002490 }
2491
2492 // Show or hide the pointer if needed.
2493 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002494 case PointerGesture::Mode::NEUTRAL:
2495 case PointerGesture::Mode::QUIET:
2496 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2497 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002498 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002499 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002500 }
2501 break;
Michael Wright227c5542020-07-02 18:30:52 +01002502 case PointerGesture::Mode::TAP:
2503 case PointerGesture::Mode::TAP_DRAG:
2504 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2505 case PointerGesture::Mode::HOVER:
2506 case PointerGesture::Mode::PRESS:
2507 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002508 // Unfade the pointer when the current gesture manipulates the
2509 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002510 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002511 break;
Michael Wright227c5542020-07-02 18:30:52 +01002512 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002513 // Fade the pointer when the current gesture manipulates a different
2514 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002515 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002516 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002517 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002518 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002519 }
2520 break;
2521 }
2522
2523 // Send events!
2524 int32_t metaState = getContext()->getGlobalMetaState();
2525 int32_t buttonState = mCurrentCookedState.buttonState;
2526
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002527 uint32_t flags = 0;
2528
2529 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2530 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2531 }
2532
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002533 // Update last coordinates of pointers that have moved so that we observe the new
2534 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002535 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2536 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2537 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2538 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2539 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2540 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002541 bool moveNeeded = false;
2542 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2543 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2544 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2545 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2546 mPointerGesture.lastGestureIdBits.value);
2547 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2548 mPointerGesture.currentGestureCoords,
2549 mPointerGesture.currentGestureIdToIndex,
2550 mPointerGesture.lastGestureProperties,
2551 mPointerGesture.lastGestureCoords,
2552 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2553 if (buttonState != mLastCookedState.buttonState) {
2554 moveNeeded = true;
2555 }
2556 }
2557
2558 // Send motion events for all pointers that went up or were canceled.
2559 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2560 if (!dispatchedGestureIdBits.isEmpty()) {
2561 if (cancelPreviousGesture) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002562 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0,
2563 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002564 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2565 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2566 mPointerGesture.downTime);
2567
2568 dispatchedGestureIdBits.clear();
2569 } else {
2570 BitSet32 upGestureIdBits;
2571 if (finishPreviousGesture) {
2572 upGestureIdBits = dispatchedGestureIdBits;
2573 } else {
2574 upGestureIdBits.value =
2575 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2576 }
2577 while (!upGestureIdBits.isEmpty()) {
2578 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2579
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002580 dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002581 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, buttonState,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002582 AMOTION_EVENT_EDGE_FLAG_NONE, mPointerGesture.lastGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002583 mPointerGesture.lastGestureCoords,
2584 mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2585 0, mPointerGesture.downTime);
2586
2587 dispatchedGestureIdBits.clearBit(id);
2588 }
2589 }
2590 }
2591
2592 // Send motion events for all pointers that moved.
2593 if (moveNeeded) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002594 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002595 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002596 mPointerGesture.currentGestureProperties,
2597 mPointerGesture.currentGestureCoords,
2598 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0,
2599 mPointerGesture.downTime);
2600 }
2601
2602 // Send motion events for all pointers that went down.
2603 if (down) {
2604 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2605 ~dispatchedGestureIdBits.value);
2606 while (!downGestureIdBits.isEmpty()) {
2607 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2608 dispatchedGestureIdBits.markBit(id);
2609
2610 if (dispatchedGestureIdBits.count() == 1) {
2611 mPointerGesture.downTime = when;
2612 }
2613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002614 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002615 0, flags, metaState, buttonState, 0,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002616 mPointerGesture.currentGestureProperties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002617 mPointerGesture.currentGestureCoords,
2618 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0,
2619 0, mPointerGesture.downTime);
2620 }
2621 }
2622
2623 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002624 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002625 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2626 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002627 mPointerGesture.currentGestureProperties,
2628 mPointerGesture.currentGestureCoords,
2629 mPointerGesture.currentGestureIdToIndex,
2630 mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime);
2631 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2632 // Synthesize a hover move event after all pointers go up to indicate that
2633 // the pointer is hovering again even if the user is not currently touching
2634 // the touch pad. This ensures that a view will receive a fresh hover enter
2635 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002636 float x, y;
2637 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002638
2639 PointerProperties pointerProperties;
2640 pointerProperties.clear();
2641 pointerProperties.id = 0;
2642 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2643
2644 PointerCoords pointerCoords;
2645 pointerCoords.clear();
2646 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2647 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2648
2649 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002650 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002651 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002652 metaState, buttonState, MotionClassification::NONE,
2653 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords,
2654 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002655 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002656 }
2657
2658 // Update state.
2659 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2660 if (!down) {
2661 mPointerGesture.lastGestureIdBits.clear();
2662 } else {
2663 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2664 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2665 uint32_t id = idBits.clearFirstMarkedBit();
2666 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2667 mPointerGesture.lastGestureProperties[index].copyFrom(
2668 mPointerGesture.currentGestureProperties[index]);
2669 mPointerGesture.lastGestureCoords[index].copyFrom(
2670 mPointerGesture.currentGestureCoords[index]);
2671 mPointerGesture.lastGestureIdToIndex[id] = index;
2672 }
2673 }
2674}
2675
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002676void TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002677 // Cancel previously dispatches pointers.
2678 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2679 int32_t metaState = getContext()->getGlobalMetaState();
2680 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002681 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
2682 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002683 mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords,
2684 mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1,
2685 0, 0, mPointerGesture.downTime);
2686 }
2687
2688 // Reset the current pointer gesture.
2689 mPointerGesture.reset();
2690 mPointerVelocityControl.reset();
2691
2692 // Remove any current spots.
2693 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002694 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002695 mPointerController->clearSpots();
2696 }
2697}
2698
2699bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2700 bool* outFinishPreviousGesture, bool isTimeout) {
2701 *outCancelPreviousGesture = false;
2702 *outFinishPreviousGesture = false;
2703
2704 // Handle TAP timeout.
2705 if (isTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002706 if (DEBUG_GESTURES) {
2707 ALOGD("Gestures: Processing timeout");
2708 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002709
Michael Wright227c5542020-07-02 18:30:52 +01002710 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002711 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2712 // The tap/drag timeout has not yet expired.
2713 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2714 mConfig.pointerGestureTapDragInterval);
2715 } else {
2716 // The tap is finished.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002717 if (DEBUG_GESTURES) {
2718 ALOGD("Gestures: TAP finished");
2719 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002720 *outFinishPreviousGesture = true;
2721
2722 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002723 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002724 mPointerGesture.currentGestureIdBits.clear();
2725
2726 mPointerVelocityControl.reset();
2727 return true;
2728 }
2729 }
2730
2731 // We did not handle this timeout.
2732 return false;
2733 }
2734
2735 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2736 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2737
2738 // Update the velocity tracker.
2739 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002740 std::vector<VelocityTracker::Position> positions;
2741 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002742 uint32_t id = idBits.clearFirstMarkedBit();
2743 const RawPointerData::Pointer& pointer =
2744 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002745 float x = pointer.x * mPointerXMovementScale;
2746 float y = pointer.y * mPointerYMovementScale;
2747 positions.push_back({x, y});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002748 }
2749 mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
2750 positions);
2751 }
2752
2753 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2754 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002755 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2756 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2757 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002758 mPointerGesture.resetTap();
2759 }
2760
2761 // Pick a new active touch id if needed.
2762 // Choose an arbitrary pointer that just went down, if there is one.
2763 // Otherwise choose an arbitrary remaining pointer.
2764 // This guarantees we always have an active touch id when there is at least one pointer.
2765 // We keep the same active touch id for as long as possible.
2766 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
2767 int32_t activeTouchId = lastActiveTouchId;
2768 if (activeTouchId < 0) {
2769 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2770 activeTouchId = mPointerGesture.activeTouchId =
2771 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2772 mPointerGesture.firstTouchTime = when;
2773 }
2774 } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
2775 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
2776 activeTouchId = mPointerGesture.activeTouchId =
2777 mCurrentCookedState.fingerIdBits.firstMarkedBit();
2778 } else {
2779 activeTouchId = mPointerGesture.activeTouchId = -1;
2780 }
2781 }
2782
2783 // Determine whether we are in quiet time.
2784 bool isQuietTime = false;
2785 if (activeTouchId < 0) {
2786 mPointerGesture.resetQuietTime();
2787 } else {
2788 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
2789 if (!isQuietTime) {
Michael Wright227c5542020-07-02 18:30:52 +01002790 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
2791 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
2792 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002793 currentFingerCount < 2) {
2794 // Enter quiet time when exiting swipe or freeform state.
2795 // This is to prevent accidentally entering the hover state and flinging the
2796 // pointer when finishing a swipe and there is still one pointer left onscreen.
2797 isQuietTime = true;
Michael Wright227c5542020-07-02 18:30:52 +01002798 } else if (mPointerGesture.lastGestureMode ==
2799 PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002800 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
2801 // Enter quiet time when releasing the button and there are still two or more
2802 // fingers down. This may indicate that one finger was used to press the button
2803 // but it has not gone up yet.
2804 isQuietTime = true;
2805 }
2806 if (isQuietTime) {
2807 mPointerGesture.quietTime = when;
2808 }
2809 }
2810 }
2811
2812 // Switch states based on button and pointer state.
2813 if (isQuietTime) {
2814 // Case 1: Quiet time. (QUIET)
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002815 if (DEBUG_GESTURES) {
2816 ALOGD("Gestures: QUIET for next %0.3fms",
2817 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2818 0.000001f);
2819 }
Michael Wright227c5542020-07-02 18:30:52 +01002820 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002821 *outFinishPreviousGesture = true;
2822 }
2823
2824 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002825 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002826 mPointerGesture.currentGestureIdBits.clear();
2827
2828 mPointerVelocityControl.reset();
2829 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2830 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2831 // The pointer follows the active touch point.
2832 // Emit DOWN, MOVE, UP events at the pointer location.
2833 //
2834 // Only the active touch matters; other fingers are ignored. This policy helps
2835 // to handle the case where the user places a second finger on the touch pad
2836 // to apply the necessary force to depress an integrated button below the surface.
2837 // We don't want the second finger to be delivered to applications.
2838 //
2839 // For this to work well, we need to make sure to track the pointer that is really
2840 // active. If the user first puts one finger down to click then adds another
2841 // finger to drag then the active pointer should switch to the finger that is
2842 // being dragged.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002843 if (DEBUG_GESTURES) {
2844 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
2845 "currentFingerCount=%d",
2846 activeTouchId, currentFingerCount);
2847 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002848 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002849 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002850 *outFinishPreviousGesture = true;
2851 mPointerGesture.activeGestureId = 0;
2852 }
2853
2854 // Switch pointers if needed.
2855 // Find the fastest pointer and follow it.
2856 if (activeTouchId >= 0 && currentFingerCount > 1) {
2857 int32_t bestId = -1;
2858 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
2859 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
2860 uint32_t id = idBits.clearFirstMarkedBit();
2861 float vx, vy;
2862 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
2863 float speed = hypotf(vx, vy);
2864 if (speed > bestSpeed) {
2865 bestId = id;
2866 bestSpeed = speed;
2867 }
2868 }
2869 }
2870 if (bestId >= 0 && bestId != activeTouchId) {
2871 mPointerGesture.activeTouchId = activeTouchId = bestId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002872 if (DEBUG_GESTURES) {
2873 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
2874 "bestId=%d, bestSpeed=%0.3f",
2875 bestId, bestSpeed);
2876 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002877 }
2878 }
2879
2880 float deltaX = 0, deltaY = 0;
2881 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
2882 const RawPointerData::Pointer& currentPointer =
2883 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
2884 const RawPointerData::Pointer& lastPointer =
2885 mLastRawState.rawPointerData.pointerForId(activeTouchId);
2886 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
2887 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
2888
Prabir Pradhan1728b212021-10-19 16:00:03 -07002889 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002890 mPointerVelocityControl.move(when, &deltaX, &deltaY);
2891
2892 // Move the pointer using a relative motion.
2893 // When using spots, the click will occur at the position of the anchor
2894 // spot and all other spots will move there.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002895 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002896 } else {
2897 mPointerVelocityControl.reset();
2898 }
2899
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002900 float x, y;
2901 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002902
Michael Wright227c5542020-07-02 18:30:52 +01002903 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002904 mPointerGesture.currentGestureIdBits.clear();
2905 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2906 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2907 mPointerGesture.currentGestureProperties[0].clear();
2908 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2909 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2910 mPointerGesture.currentGestureCoords[0].clear();
2911 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2912 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2913 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2914 } else if (currentFingerCount == 0) {
2915 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002916 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002917 *outFinishPreviousGesture = true;
2918 }
2919
2920 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2921 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2922 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002923 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2924 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002925 lastFingerCount == 1) {
2926 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002927 float x, y;
2928 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002929 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2930 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002931 if (DEBUG_GESTURES) {
2932 ALOGD("Gestures: TAP");
2933 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002934
2935 mPointerGesture.tapUpTime = when;
2936 getContext()->requestTimeoutAtTime(when +
2937 mConfig.pointerGestureTapDragInterval);
2938
2939 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002940 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002941 mPointerGesture.currentGestureIdBits.clear();
2942 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2943 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2944 mPointerGesture.currentGestureProperties[0].clear();
2945 mPointerGesture.currentGestureProperties[0].id =
2946 mPointerGesture.activeGestureId;
2947 mPointerGesture.currentGestureProperties[0].toolType =
2948 AMOTION_EVENT_TOOL_TYPE_FINGER;
2949 mPointerGesture.currentGestureCoords[0].clear();
2950 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2951 mPointerGesture.tapX);
2952 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2953 mPointerGesture.tapY);
2954 mPointerGesture.currentGestureCoords[0]
2955 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2956
2957 tapped = true;
2958 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002959 if (DEBUG_GESTURES) {
2960 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX,
2961 y - mPointerGesture.tapY);
2962 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002963 }
2964 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002965 if (DEBUG_GESTURES) {
2966 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2967 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2968 (when - mPointerGesture.tapDownTime) * 0.000001f);
2969 } else {
2970 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2971 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002972 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002973 }
2974 }
2975
2976 mPointerVelocityControl.reset();
2977
2978 if (!tapped) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002979 if (DEBUG_GESTURES) {
2980 ALOGD("Gestures: NEUTRAL");
2981 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002982 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002983 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002984 mPointerGesture.currentGestureIdBits.clear();
2985 }
2986 } else if (currentFingerCount == 1) {
2987 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2988 // The pointer follows the active touch point.
2989 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2990 // When in TAP_DRAG, emit MOVE events at the pointer location.
2991 ALOG_ASSERT(activeTouchId >= 0);
2992
Michael Wright227c5542020-07-02 18:30:52 +01002993 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2994 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002995 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002996 float x, y;
2997 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002998 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2999 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01003000 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003001 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003002 if (DEBUG_GESTURES) {
3003 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3004 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
3005 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003006 }
3007 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003008 if (DEBUG_GESTURES) {
3009 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3010 (when - mPointerGesture.tapUpTime) * 0.000001f);
3011 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003012 }
Michael Wright227c5542020-07-02 18:30:52 +01003013 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
3014 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003015 }
3016
3017 float deltaX = 0, deltaY = 0;
3018 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
3019 const RawPointerData::Pointer& currentPointer =
3020 mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
3021 const RawPointerData::Pointer& lastPointer =
3022 mLastRawState.rawPointerData.pointerForId(activeTouchId);
3023 deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3024 deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3025
Prabir Pradhan1728b212021-10-19 16:00:03 -07003026 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003027 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3028
3029 // Move the pointer using a relative motion.
3030 // When using spots, the hover or drag will occur at the position of the anchor spot.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003031 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003032 } else {
3033 mPointerVelocityControl.reset();
3034 }
3035
3036 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01003037 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003038 if (DEBUG_GESTURES) {
3039 ALOGD("Gestures: TAP_DRAG");
3040 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003041 down = true;
3042 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003043 if (DEBUG_GESTURES) {
3044 ALOGD("Gestures: HOVER");
3045 }
Michael Wright227c5542020-07-02 18:30:52 +01003046 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003047 *outFinishPreviousGesture = true;
3048 }
3049 mPointerGesture.activeGestureId = 0;
3050 down = false;
3051 }
3052
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003053 float x, y;
3054 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003055
3056 mPointerGesture.currentGestureIdBits.clear();
3057 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3058 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3059 mPointerGesture.currentGestureProperties[0].clear();
3060 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3061 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3062 mPointerGesture.currentGestureCoords[0].clear();
3063 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3064 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3065 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3066 down ? 1.0f : 0.0f);
3067
3068 if (lastFingerCount == 0 && currentFingerCount != 0) {
3069 mPointerGesture.resetTap();
3070 mPointerGesture.tapDownTime = when;
3071 mPointerGesture.tapX = x;
3072 mPointerGesture.tapY = y;
3073 }
3074 } else {
3075 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3076 // We need to provide feedback for each finger that goes down so we cannot wait
3077 // for the fingers to move before deciding what to do.
3078 //
3079 // The ambiguous case is deciding what to do when there are two fingers down but they
3080 // have not moved enough to determine whether they are part of a drag or part of a
3081 // freeform gesture, or just a press or long-press at the pointer location.
3082 //
3083 // When there are two fingers we start with the PRESS hypothesis and we generate a
3084 // down at the pointer location.
3085 //
3086 // When the two fingers move enough or when additional fingers are added, we make
3087 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3088 ALOG_ASSERT(activeTouchId >= 0);
3089
3090 bool settled = when >=
3091 mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
Michael Wright227c5542020-07-02 18:30:52 +01003092 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3093 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3094 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003095 *outFinishPreviousGesture = true;
3096 } else if (!settled && currentFingerCount > lastFingerCount) {
3097 // Additional pointers have gone down but not yet settled.
3098 // Reset the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003099 if (DEBUG_GESTURES) {
3100 ALOGD("Gestures: Resetting gesture since additional pointers went down for "
3101 "MULTITOUCH, settle time remaining %0.3fms",
3102 (mPointerGesture.firstTouchTime +
3103 mConfig.pointerGestureMultitouchSettleInterval - when) *
3104 0.000001f);
3105 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003106 *outCancelPreviousGesture = true;
3107 } else {
3108 // Continue previous gesture.
3109 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3110 }
3111
3112 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Michael Wright227c5542020-07-02 18:30:52 +01003113 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003114 mPointerGesture.activeGestureId = 0;
3115 mPointerGesture.referenceIdBits.clear();
3116 mPointerVelocityControl.reset();
3117
3118 // Use the centroid and pointer location as the reference points for the gesture.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003119 if (DEBUG_GESTURES) {
3120 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3121 "settle time remaining %0.3fms",
3122 (mPointerGesture.firstTouchTime +
3123 mConfig.pointerGestureMultitouchSettleInterval - when) *
3124 0.000001f);
3125 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003126 mCurrentRawState.rawPointerData
3127 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3128 &mPointerGesture.referenceTouchY);
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003129 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3130 &mPointerGesture.referenceGestureY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003131 }
3132
3133 // Clear the reference deltas for fingers not yet included in the reference calculation.
3134 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3135 ~mPointerGesture.referenceIdBits.value);
3136 !idBits.isEmpty();) {
3137 uint32_t id = idBits.clearFirstMarkedBit();
3138 mPointerGesture.referenceDeltas[id].dx = 0;
3139 mPointerGesture.referenceDeltas[id].dy = 0;
3140 }
3141 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3142
3143 // Add delta for all fingers and calculate a common movement delta.
3144 float commonDeltaX = 0, commonDeltaY = 0;
3145 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3146 mCurrentCookedState.fingerIdBits.value);
3147 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3148 bool first = (idBits == commonIdBits);
3149 uint32_t id = idBits.clearFirstMarkedBit();
3150 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3151 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3152 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3153 delta.dx += cpd.x - lpd.x;
3154 delta.dy += cpd.y - lpd.y;
3155
3156 if (first) {
3157 commonDeltaX = delta.dx;
3158 commonDeltaY = delta.dy;
3159 } else {
3160 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
3161 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
3162 }
3163 }
3164
3165 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
Michael Wright227c5542020-07-02 18:30:52 +01003166 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003167 float dist[MAX_POINTER_ID + 1];
3168 int32_t distOverThreshold = 0;
3169 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3170 uint32_t id = idBits.clearFirstMarkedBit();
3171 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3172 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3173 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3174 distOverThreshold += 1;
3175 }
3176 }
3177
3178 // Only transition when at least two pointers have moved further than
3179 // the minimum distance threshold.
3180 if (distOverThreshold >= 2) {
3181 if (currentFingerCount > 2) {
3182 // There are more than two pointers, switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003183 if (DEBUG_GESTURES) {
3184 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3185 currentFingerCount);
3186 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003187 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003188 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003189 } else {
3190 // There are exactly two pointers.
3191 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3192 uint32_t id1 = idBits.clearFirstMarkedBit();
3193 uint32_t id2 = idBits.firstMarkedBit();
3194 const RawPointerData::Pointer& p1 =
3195 mCurrentRawState.rawPointerData.pointerForId(id1);
3196 const RawPointerData::Pointer& p2 =
3197 mCurrentRawState.rawPointerData.pointerForId(id2);
3198 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3199 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3200 // There are two pointers but they are too far apart for a SWIPE,
3201 // switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003202 if (DEBUG_GESTURES) {
3203 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > "
3204 "%0.3f",
3205 mutualDistance, mPointerGestureMaxSwipeWidth);
3206 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003207 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003208 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003209 } else {
3210 // There are two pointers. Wait for both pointers to start moving
3211 // before deciding whether this is a SWIPE or FREEFORM gesture.
3212 float dist1 = dist[id1];
3213 float dist2 = dist[id2];
3214 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3215 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3216 // Calculate the dot product of the displacement vectors.
3217 // When the vectors are oriented in approximately the same direction,
3218 // the angle betweeen them is near zero and the cosine of the angle
3219 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3220 // mag(v2).
3221 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3222 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3223 float dx1 = delta1.dx * mPointerXZoomScale;
3224 float dy1 = delta1.dy * mPointerYZoomScale;
3225 float dx2 = delta2.dx * mPointerXZoomScale;
3226 float dy2 = delta2.dy * mPointerYZoomScale;
3227 float dot = dx1 * dx2 + dy1 * dy2;
3228 float cosine = dot / (dist1 * dist2); // denominator always > 0
3229 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3230 // Pointers are moving in the same direction. Switch to SWIPE.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003231 if (DEBUG_GESTURES) {
3232 ALOGD("Gestures: PRESS transitioned to SWIPE, "
3233 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3234 "cosine %0.3f >= %0.3f",
3235 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3236 mConfig.pointerGestureMultitouchMinDistance, cosine,
3237 mConfig.pointerGestureSwipeTransitionAngleCosine);
3238 }
Michael Wright227c5542020-07-02 18:30:52 +01003239 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003240 } else {
3241 // Pointers are moving in different directions. Switch to FREEFORM.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003242 if (DEBUG_GESTURES) {
3243 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
3244 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3245 "cosine %0.3f < %0.3f",
3246 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3247 mConfig.pointerGestureMultitouchMinDistance, cosine,
3248 mConfig.pointerGestureSwipeTransitionAngleCosine);
3249 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003250 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003251 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003252 }
3253 }
3254 }
3255 }
3256 }
Michael Wright227c5542020-07-02 18:30:52 +01003257 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003258 // Switch from SWIPE to FREEFORM if additional pointers go down.
3259 // Cancel previous gesture.
3260 if (currentFingerCount > 2) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003261 if (DEBUG_GESTURES) {
3262 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3263 currentFingerCount);
3264 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003265 *outCancelPreviousGesture = true;
Michael Wright227c5542020-07-02 18:30:52 +01003266 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003267 }
3268 }
3269
3270 // Move the reference points based on the overall group motion of the fingers
3271 // except in PRESS mode while waiting for a transition to occur.
Michael Wright227c5542020-07-02 18:30:52 +01003272 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003273 (commonDeltaX || commonDeltaY)) {
3274 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3275 uint32_t id = idBits.clearFirstMarkedBit();
3276 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3277 delta.dx = 0;
3278 delta.dy = 0;
3279 }
3280
3281 mPointerGesture.referenceTouchX += commonDeltaX;
3282 mPointerGesture.referenceTouchY += commonDeltaY;
3283
3284 commonDeltaX *= mPointerXMovementScale;
3285 commonDeltaY *= mPointerYMovementScale;
3286
Prabir Pradhan1728b212021-10-19 16:00:03 -07003287 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003288 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3289
3290 mPointerGesture.referenceGestureX += commonDeltaX;
3291 mPointerGesture.referenceGestureY += commonDeltaY;
3292 }
3293
3294 // Report gestures.
Michael Wright227c5542020-07-02 18:30:52 +01003295 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3296 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003297 // PRESS or SWIPE mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003298 if (DEBUG_GESTURES) {
3299 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
3300 "activeGestureId=%d, currentTouchPointerCount=%d",
3301 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3302 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003303 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3304
3305 mPointerGesture.currentGestureIdBits.clear();
3306 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3307 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3308 mPointerGesture.currentGestureProperties[0].clear();
3309 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3310 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3311 mPointerGesture.currentGestureCoords[0].clear();
3312 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3313 mPointerGesture.referenceGestureX);
3314 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3315 mPointerGesture.referenceGestureY);
3316 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Michael Wright227c5542020-07-02 18:30:52 +01003317 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003318 // FREEFORM mode.
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003319 if (DEBUG_GESTURES) {
3320 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
3321 "activeGestureId=%d, currentTouchPointerCount=%d",
3322 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3323 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003324 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3325
3326 mPointerGesture.currentGestureIdBits.clear();
3327
3328 BitSet32 mappedTouchIdBits;
3329 BitSet32 usedGestureIdBits;
Michael Wright227c5542020-07-02 18:30:52 +01003330 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003331 // Initially, assign the active gesture id to the active touch point
3332 // if there is one. No other touch id bits are mapped yet.
3333 if (!*outCancelPreviousGesture) {
3334 mappedTouchIdBits.markBit(activeTouchId);
3335 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3336 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3337 mPointerGesture.activeGestureId;
3338 } else {
3339 mPointerGesture.activeGestureId = -1;
3340 }
3341 } else {
3342 // Otherwise, assume we mapped all touches from the previous frame.
3343 // Reuse all mappings that are still applicable.
3344 mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value &
3345 mCurrentCookedState.fingerIdBits.value;
3346 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3347
3348 // Check whether we need to choose a new active gesture id because the
3349 // current went went up.
3350 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3351 ~mCurrentCookedState.fingerIdBits.value);
3352 !upTouchIdBits.isEmpty();) {
3353 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3354 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3355 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3356 mPointerGesture.activeGestureId = -1;
3357 break;
3358 }
3359 }
3360 }
3361
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003362 if (DEBUG_GESTURES) {
3363 ALOGD("Gestures: FREEFORM follow up "
3364 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3365 "activeGestureId=%d",
3366 mappedTouchIdBits.value, usedGestureIdBits.value,
3367 mPointerGesture.activeGestureId);
3368 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003369
3370 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3371 for (uint32_t i = 0; i < currentFingerCount; i++) {
3372 uint32_t touchId = idBits.clearFirstMarkedBit();
3373 uint32_t gestureId;
3374 if (!mappedTouchIdBits.hasBit(touchId)) {
3375 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3376 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003377 if (DEBUG_GESTURES) {
3378 ALOGD("Gestures: FREEFORM "
3379 "new mapping for touch id %d -> gesture id %d",
3380 touchId, gestureId);
3381 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003382 } else {
3383 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003384 if (DEBUG_GESTURES) {
3385 ALOGD("Gestures: FREEFORM "
3386 "existing mapping for touch id %d -> gesture id %d",
3387 touchId, gestureId);
3388 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003389 }
3390 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3391 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3392
3393 const RawPointerData::Pointer& pointer =
3394 mCurrentRawState.rawPointerData.pointerForId(touchId);
3395 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3396 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
Prabir Pradhan1728b212021-10-19 16:00:03 -07003397 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003398
3399 mPointerGesture.currentGestureProperties[i].clear();
3400 mPointerGesture.currentGestureProperties[i].id = gestureId;
3401 mPointerGesture.currentGestureProperties[i].toolType =
3402 AMOTION_EVENT_TOOL_TYPE_FINGER;
3403 mPointerGesture.currentGestureCoords[i].clear();
3404 mPointerGesture.currentGestureCoords[i]
3405 .setAxisValue(AMOTION_EVENT_AXIS_X,
3406 mPointerGesture.referenceGestureX + deltaX);
3407 mPointerGesture.currentGestureCoords[i]
3408 .setAxisValue(AMOTION_EVENT_AXIS_Y,
3409 mPointerGesture.referenceGestureY + deltaY);
3410 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3411 1.0f);
3412 }
3413
3414 if (mPointerGesture.activeGestureId < 0) {
3415 mPointerGesture.activeGestureId =
3416 mPointerGesture.currentGestureIdBits.firstMarkedBit();
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003417 if (DEBUG_GESTURES) {
3418 ALOGD("Gestures: FREEFORM new activeGestureId=%d",
3419 mPointerGesture.activeGestureId);
3420 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003421 }
3422 }
3423 }
3424
3425 mPointerController->setButtonState(mCurrentRawState.buttonState);
3426
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003427 if (DEBUG_GESTURES) {
3428 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
3429 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
3430 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
3431 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
3432 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
3433 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
3434 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
3435 uint32_t id = idBits.clearFirstMarkedBit();
3436 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3437 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
3438 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
3439 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
3440 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3441 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3442 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3443 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3444 }
3445 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
3446 uint32_t id = idBits.clearFirstMarkedBit();
3447 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
3448 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
3449 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
3450 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
3451 "x=%0.3f, y=%0.3f, pressure=%0.3f",
3452 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
3453 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
3454 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
3455 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003456 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003457 return true;
3458}
3459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460void TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003461 mPointerSimple.currentCoords.clear();
3462 mPointerSimple.currentProperties.clear();
3463
3464 bool down, hovering;
3465 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3466 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3467 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003468 mPointerController
3469 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3470 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003471
3472 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3473 down = !hovering;
3474
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003475 float x, y;
3476 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003477 mPointerSimple.currentCoords.copyFrom(
3478 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3479 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3480 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3481 mPointerSimple.currentProperties.id = 0;
3482 mPointerSimple.currentProperties.toolType =
3483 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3484 } else {
3485 down = false;
3486 hovering = false;
3487 }
3488
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003490}
3491
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003492void TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3493 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003494}
3495
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003496void TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003497 mPointerSimple.currentCoords.clear();
3498 mPointerSimple.currentProperties.clear();
3499
3500 bool down, hovering;
3501 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3502 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
3503 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3504 float deltaX = 0, deltaY = 0;
3505 if (mLastCookedState.mouseIdBits.hasBit(id)) {
3506 uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
3507 deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x -
3508 mLastRawState.rawPointerData.pointers[lastIndex].x) *
3509 mPointerXMovementScale;
3510 deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y -
3511 mLastRawState.rawPointerData.pointers[lastIndex].y) *
3512 mPointerYMovementScale;
3513
Prabir Pradhan1728b212021-10-19 16:00:03 -07003514 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003515 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3516
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003517 mPointerController->move(deltaX, deltaY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003518 } else {
3519 mPointerVelocityControl.reset();
3520 }
3521
3522 down = isPointerDown(mCurrentRawState.buttonState);
3523 hovering = !down;
3524
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003525 float x, y;
3526 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003527 mPointerSimple.currentCoords.copyFrom(
3528 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3529 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3530 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3531 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3532 hovering ? 0.0f : 1.0f);
3533 mPointerSimple.currentProperties.id = 0;
3534 mPointerSimple.currentProperties.toolType =
3535 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3536 } else {
3537 mPointerVelocityControl.reset();
3538
3539 down = false;
3540 hovering = false;
3541 }
3542
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003543 dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003544}
3545
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003546void TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
3547 abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003548
3549 mPointerVelocityControl.reset();
3550}
3551
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552void TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3553 bool down, bool hovering) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003554 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003555
3556 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003557 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003558 mPointerController->clearSpots();
3559 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003560 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003561 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003562 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003563 }
Garfield Tan9514d782020-11-10 16:37:23 -08003564 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003565
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003566 float xCursorPosition, yCursorPosition;
3567 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003568
3569 if (mPointerSimple.down && !down) {
3570 mPointerSimple.down = false;
3571
3572 // Send up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003573 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3574 displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003575 mLastRawState.buttonState, MotionClassification::NONE,
3576 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3577 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3578 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3579 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003580 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003581 }
3582
3583 if (mPointerSimple.hovering && !hovering) {
3584 mPointerSimple.hovering = false;
3585
3586 // Send hover exit.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003587 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3588 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0,
3589 metaState, mLastRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003590 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties,
3591 &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision,
3592 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3593 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003594 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003595 }
3596
3597 if (down) {
3598 if (!mPointerSimple.down) {
3599 mPointerSimple.down = true;
3600 mPointerSimple.downTime = when;
3601
3602 // Send down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003603 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003604 displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3605 metaState, mCurrentRawState.buttonState,
3606 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3607 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3608 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3609 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003610 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003611 }
3612
3613 // Send move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3615 displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003616 mCurrentRawState.buttonState, MotionClassification::NONE,
3617 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3618 &mPointerSimple.currentCoords, mOrientedXPrecision,
3619 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3620 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003621 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003622 }
3623
3624 if (hovering) {
3625 if (!mPointerSimple.hovering) {
3626 mPointerSimple.hovering = true;
3627
3628 // Send hover enter.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003629 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003630 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0,
3631 metaState, mCurrentRawState.buttonState,
3632 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3633 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3634 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3635 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003636 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003637 }
3638
3639 // Send hover move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3641 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3642 metaState, mCurrentRawState.buttonState, MotionClassification::NONE,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003643 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3644 &mPointerSimple.currentCoords, mOrientedXPrecision,
3645 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3646 mPointerSimple.downTime, /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003647 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003648 }
3649
3650 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3651 float vscroll = mCurrentRawState.rawVScroll;
3652 float hscroll = mCurrentRawState.rawHScroll;
3653 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3654 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3655
3656 // Send scroll.
3657 PointerCoords pointerCoords;
3658 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3659 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3660 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3663 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003664 mCurrentRawState.buttonState, MotionClassification::NONE,
3665 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties,
3666 &pointerCoords, mOrientedXPrecision, mOrientedYPrecision,
3667 xCursorPosition, yCursorPosition, mPointerSimple.downTime,
3668 /* videoFrames */ {});
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003669 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003670 }
3671
3672 // Save state.
3673 if (down || hovering) {
3674 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3675 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
3676 } else {
3677 mPointerSimple.reset();
3678 }
3679}
3680
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681void TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003682 mPointerSimple.currentCoords.clear();
3683 mPointerSimple.currentProperties.clear();
3684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 dispatchPointerSimple(when, readTime, policyFlags, false, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003686}
3687
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003688void TouchInputMapper::dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags,
3689 uint32_t source, int32_t action, int32_t actionButton,
3690 int32_t flags, int32_t metaState, int32_t buttonState,
3691 int32_t edgeFlags, const PointerProperties* properties,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003692 const PointerCoords* coords, const uint32_t* idToIndex,
3693 BitSet32 idBits, int32_t changedId, float xPrecision,
3694 float yPrecision, nsecs_t downTime) {
3695 PointerCoords pointerCoords[MAX_POINTERS];
3696 PointerProperties pointerProperties[MAX_POINTERS];
3697 uint32_t pointerCount = 0;
3698 while (!idBits.isEmpty()) {
3699 uint32_t id = idBits.clearFirstMarkedBit();
3700 uint32_t index = idToIndex[id];
3701 pointerProperties[pointerCount].copyFrom(properties[index]);
3702 pointerCoords[pointerCount].copyFrom(coords[index]);
3703
3704 if (changedId >= 0 && id == uint32_t(changedId)) {
3705 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3706 }
3707
3708 pointerCount += 1;
3709 }
3710
3711 ALOG_ASSERT(pointerCount != 0);
3712
3713 if (changedId >= 0 && pointerCount == 1) {
3714 // Replace initial down and final up action.
3715 // We can compare the action without masking off the changed pointer index
3716 // because we know the index is 0.
3717 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3718 action = AMOTION_EVENT_ACTION_DOWN;
3719 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003720 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3721 action = AMOTION_EVENT_ACTION_CANCEL;
3722 } else {
3723 action = AMOTION_EVENT_ACTION_UP;
3724 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003725 } else {
3726 // Can't happen.
3727 ALOG_ASSERT(false);
3728 }
3729 }
3730 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3731 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003732 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003733 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003734 }
3735 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3736 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003737 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003738 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003739 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003740 NotifyMotionArgs args(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3741 policyFlags, action, actionButton, flags, metaState, buttonState,
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00003742 MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties,
3743 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3744 downTime, std::move(frames));
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003745 getListener().notifyMotion(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003746}
3747
3748bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
3749 const PointerCoords* inCoords,
3750 const uint32_t* inIdToIndex,
3751 PointerProperties* outProperties,
3752 PointerCoords* outCoords, const uint32_t* outIdToIndex,
3753 BitSet32 idBits) const {
3754 bool changed = false;
3755 while (!idBits.isEmpty()) {
3756 uint32_t id = idBits.clearFirstMarkedBit();
3757 uint32_t inIndex = inIdToIndex[id];
3758 uint32_t outIndex = outIdToIndex[id];
3759
3760 const PointerProperties& curInProperties = inProperties[inIndex];
3761 const PointerCoords& curInCoords = inCoords[inIndex];
3762 PointerProperties& curOutProperties = outProperties[outIndex];
3763 PointerCoords& curOutCoords = outCoords[outIndex];
3764
3765 if (curInProperties != curOutProperties) {
3766 curOutProperties.copyFrom(curInProperties);
3767 changed = true;
3768 }
3769
3770 if (curInCoords != curOutCoords) {
3771 curOutCoords.copyFrom(curInCoords);
3772 changed = true;
3773 }
3774 }
3775 return changed;
3776}
3777
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778void TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3779 abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3780 abortTouches(when, readTime, 0 /* policyFlags*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003781}
3782
Prabir Pradhan1728b212021-10-19 16:00:03 -07003783// Transform input device coordinates to display panel coordinates.
3784void TouchInputMapper::rotateAndScale(float& x, float& y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003785 const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale;
3786 const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale;
3787
arthurhunga36b28e2020-12-29 20:28:15 +08003788 const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale;
3789 const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale;
3790
Prabir Pradhan1728b212021-10-19 16:00:03 -07003791 // Rotate to display coordinate.
Arthur Hung4197f6b2020-03-16 15:39:59 +08003792 // 0 - no swap and reverse.
3793 // 90 - swap x/y and reverse y.
3794 // 180 - reverse x, y.
3795 // 270 - swap x/y and reverse x.
Prabir Pradhan1728b212021-10-19 16:00:03 -07003796 switch (mInputDeviceOrientation) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003797 case DISPLAY_ORIENTATION_0:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003798 x = xScaled;
3799 y = yScaled;
Arthur Hung4197f6b2020-03-16 15:39:59 +08003800 break;
Arthur Hung05de5772019-09-26 18:31:26 +08003801 case DISPLAY_ORIENTATION_90:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003802 y = xScaledMax;
3803 x = yScaled;
Arthur Hung05de5772019-09-26 18:31:26 +08003804 break;
3805 case DISPLAY_ORIENTATION_180:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003806 x = xScaledMax;
3807 y = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003808 break;
3809 case DISPLAY_ORIENTATION_270:
Prabir Pradhan1728b212021-10-19 16:00:03 -07003810 y = xScaled;
3811 x = yScaledMax;
Arthur Hung05de5772019-09-26 18:31:26 +08003812 break;
3813 default:
Arthur Hung4197f6b2020-03-16 15:39:59 +08003814 assert(false);
Arthur Hung05de5772019-09-26 18:31:26 +08003815 }
3816}
3817
Prabir Pradhan1728b212021-10-19 16:00:03 -07003818bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Arthur Hung4197f6b2020-03-16 15:39:59 +08003819 const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale;
3820 const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;
3821
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003822 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003823 xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003824 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +00003825 yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003826}
3827
3828const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3829 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003830 if (DEBUG_VIRTUAL_KEYS) {
3831 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3832 "left=%d, top=%d, right=%d, bottom=%d",
3833 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3834 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
3835 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003836
3837 if (virtualKey.isHit(x, y)) {
3838 return &virtualKey;
3839 }
3840 }
3841
3842 return nullptr;
3843}
3844
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003845void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3846 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3847 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003848
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003849 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003850
3851 if (currentPointerCount == 0) {
3852 // No pointers to assign.
3853 return;
3854 }
3855
3856 if (lastPointerCount == 0) {
3857 // All pointers are new.
3858 for (uint32_t i = 0; i < currentPointerCount; i++) {
3859 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003860 current.rawPointerData.pointers[i].id = id;
3861 current.rawPointerData.idToIndex[id] = i;
3862 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003863 }
3864 return;
3865 }
3866
3867 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003868 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003869 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003870 uint32_t id = last.rawPointerData.pointers[0].id;
3871 current.rawPointerData.pointers[0].id = id;
3872 current.rawPointerData.idToIndex[id] = 0;
3873 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003874 return;
3875 }
3876
3877 // General case.
3878 // We build a heap of squared euclidean distances between current and last pointers
3879 // associated with the current and last pointer indices. Then, we find the best
3880 // match (by distance) for each current pointer.
3881 // The pointers must have the same tool type but it is possible for them to
3882 // transition from hovering to touching or vice-versa while retaining the same id.
3883 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3884
3885 uint32_t heapSize = 0;
3886 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3887 currentPointerIndex++) {
3888 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3889 lastPointerIndex++) {
3890 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003891 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003892 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003893 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003894 if (currentPointer.toolType == lastPointer.toolType) {
3895 int64_t deltaX = currentPointer.x - lastPointer.x;
3896 int64_t deltaY = currentPointer.y - lastPointer.y;
3897
3898 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3899
3900 // Insert new element into the heap (sift up).
3901 heap[heapSize].currentPointerIndex = currentPointerIndex;
3902 heap[heapSize].lastPointerIndex = lastPointerIndex;
3903 heap[heapSize].distance = distance;
3904 heapSize += 1;
3905 }
3906 }
3907 }
3908
3909 // Heapify
3910 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3911 startIndex -= 1;
3912 for (uint32_t parentIndex = startIndex;;) {
3913 uint32_t childIndex = parentIndex * 2 + 1;
3914 if (childIndex >= heapSize) {
3915 break;
3916 }
3917
3918 if (childIndex + 1 < heapSize &&
3919 heap[childIndex + 1].distance < heap[childIndex].distance) {
3920 childIndex += 1;
3921 }
3922
3923 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3924 break;
3925 }
3926
3927 swap(heap[parentIndex], heap[childIndex]);
3928 parentIndex = childIndex;
3929 }
3930 }
3931
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003932 if (DEBUG_POINTER_ASSIGNMENT) {
3933 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3934 for (size_t i = 0; i < heapSize; i++) {
3935 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3936 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3937 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003938 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003939
3940 // Pull matches out by increasing order of distance.
3941 // To avoid reassigning pointers that have already been matched, the loop keeps track
3942 // of which last and current pointers have been matched using the matchedXXXBits variables.
3943 // It also tracks the used pointer id bits.
3944 BitSet32 matchedLastBits(0);
3945 BitSet32 matchedCurrentBits(0);
3946 BitSet32 usedIdBits(0);
3947 bool first = true;
3948 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3949 while (heapSize > 0) {
3950 if (first) {
3951 // The first time through the loop, we just consume the root element of
3952 // the heap (the one with smallest distance).
3953 first = false;
3954 } else {
3955 // Previous iterations consumed the root element of the heap.
3956 // Pop root element off of the heap (sift down).
3957 heap[0] = heap[heapSize];
3958 for (uint32_t parentIndex = 0;;) {
3959 uint32_t childIndex = parentIndex * 2 + 1;
3960 if (childIndex >= heapSize) {
3961 break;
3962 }
3963
3964 if (childIndex + 1 < heapSize &&
3965 heap[childIndex + 1].distance < heap[childIndex].distance) {
3966 childIndex += 1;
3967 }
3968
3969 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3970 break;
3971 }
3972
3973 swap(heap[parentIndex], heap[childIndex]);
3974 parentIndex = childIndex;
3975 }
3976
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003977 if (DEBUG_POINTER_ASSIGNMENT) {
3978 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3979 for (size_t j = 0; j < heapSize; j++) {
3980 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3981 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3982 heap[j].distance);
3983 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003984 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003985 }
3986
3987 heapSize -= 1;
3988
3989 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3990 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3991
3992 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3993 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3994
3995 matchedCurrentBits.markBit(currentPointerIndex);
3996 matchedLastBits.markBit(lastPointerIndex);
3997
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003998 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3999 current.rawPointerData.pointers[currentPointerIndex].id = id;
4000 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4001 current.rawPointerData.markIdBit(id,
4002 current.rawPointerData.isHovering(
4003 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004004 usedIdBits.markBit(id);
4005
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004006 if (DEBUG_POINTER_ASSIGNMENT) {
4007 ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
4008 ", distance=%" PRIu64,
4009 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4010 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004011 break;
4012 }
4013 }
4014
4015 // Assign fresh ids to pointers that were not matched in the process.
4016 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4017 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4018 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4019
Siarhei Vishniakou57479982021-03-03 01:32:21 +00004020 current.rawPointerData.pointers[currentPointerIndex].id = id;
4021 current.rawPointerData.idToIndex[id] = currentPointerIndex;
4022 current.rawPointerData.markIdBit(id,
4023 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004024
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08004025 if (DEBUG_POINTER_ASSIGNMENT) {
4026 ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
4027 id);
4028 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004029 }
4030}
4031
4032int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
4033 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4034 return AKEY_STATE_VIRTUAL;
4035 }
4036
4037 for (const VirtualKey& virtualKey : mVirtualKeys) {
4038 if (virtualKey.keyCode == keyCode) {
4039 return AKEY_STATE_UP;
4040 }
4041 }
4042
4043 return AKEY_STATE_UNKNOWN;
4044}
4045
4046int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
4047 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4048 return AKEY_STATE_VIRTUAL;
4049 }
4050
4051 for (const VirtualKey& virtualKey : mVirtualKeys) {
4052 if (virtualKey.scanCode == scanCode) {
4053 return AKEY_STATE_UP;
4054 }
4055 }
4056
4057 return AKEY_STATE_UNKNOWN;
4058}
4059
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004060bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
4061 const std::vector<int32_t>& keyCodes,
4062 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004063 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004064 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004065 if (virtualKey.keyCode == keyCodes[i]) {
4066 outFlags[i] = 1;
4067 }
4068 }
4069 }
4070
4071 return true;
4072}
4073
4074std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
4075 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01004076 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004077 return std::make_optional(mPointerController->getDisplayId());
4078 } else {
4079 return std::make_optional(mViewport.displayId);
4080 }
4081 }
4082 return std::nullopt;
4083}
4084
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07004085} // namespace android