blob: 8cd2cf02554ef52d10420e99730efab3de55dbb6 [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>
Prabir Pradhan8d9ba912022-11-11 22:26:33 +000024#include <input/PrintTools.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080025
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070026#include "CursorButtonAccumulator.h"
27#include "CursorScrollAccumulator.h"
28#include "TouchButtonAccumulator.h"
29#include "TouchCursorInputMapperCommon.h"
Michael Wrighta9cf4192022-12-01 23:46:39 +000030#include "ui/Rotation.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070031
32namespace android {
33
34// --- Constants ---
35
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070036// Artificial latency on synthetic events created from stylus data without corresponding touch
37// data.
38static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
39
HQ Liue6983c72022-04-19 22:14:56 +000040// Minimum width between two pointers to determine a gesture as freeform gesture in mm
41static const float MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER = 30;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042// --- Static Definitions ---
43
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000044static const DisplayViewport kUninitializedViewport;
45
Prabir Pradhan7ddbc952022-11-09 22:03:40 +000046static std::string toString(const Rect& rect) {
47 return base::StringPrintf("Rect{%d, %d, %d, %d}", rect.left, rect.top, rect.right, rect.bottom);
48}
49
50static std::string toString(const ui::Size& size) {
51 return base::StringPrintf("%dx%d", size.width, size.height);
52}
53
Prabir Pradhan675f25a2022-11-10 22:04:07 +000054static bool isPointInRect(const Rect& rect, vec2 p) {
55 return p.x >= rect.left && p.x < rect.right && p.y >= rect.top && p.y < rect.bottom;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +000056}
57
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070058template <typename T>
59inline static void swap(T& a, T& b) {
60 T temp = a;
61 a = b;
62 b = temp;
63}
64
65static float calculateCommonVector(float a, float b) {
66 if (a > 0 && b > 0) {
67 return a < b ? a : b;
68 } else if (a < 0 && b < 0) {
69 return a > b ? a : b;
70 } else {
71 return 0;
72 }
73}
74
75inline static float distance(float x1, float y1, float x2, float y2) {
76 return hypotf(x1 - x2, y1 - y2);
77}
78
79inline static int32_t signExtendNybble(int32_t value) {
80 return value >= 8 ? value - 16 : value;
81}
82
Prabir Pradhan675f25a2022-11-10 22:04:07 +000083static ui::Size getNaturalDisplaySize(const DisplayViewport& viewport) {
Prabir Pradhan2d613f42022-11-10 20:22:06 +000084 ui::Size rotatedDisplaySize{viewport.deviceWidth, viewport.deviceHeight};
Prabir Pradhanea31d4f2022-11-10 20:48:01 +000085 if (viewport.orientation == ui::ROTATION_90 || viewport.orientation == ui::ROTATION_270) {
Prabir Pradhan2d613f42022-11-10 20:22:06 +000086 std::swap(rotatedDisplaySize.width, rotatedDisplaySize.height);
87 }
Prabir Pradhan675f25a2022-11-10 22:04:07 +000088 return rotatedDisplaySize;
Prabir Pradhan2d613f42022-11-10 20:22:06 +000089}
90
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +000091static int32_t filterButtonState(InputReaderConfiguration& config, int32_t buttonState) {
92 if (!config.stylusButtonMotionEventsEnabled) {
93 buttonState &=
94 ~(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY | AMOTION_EVENT_BUTTON_STYLUS_SECONDARY);
95 }
96 return buttonState;
97}
98
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099// --- RawPointerData ---
100
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
102 float x = 0, y = 0;
103 uint32_t count = touchingIdBits.count();
104 if (count) {
105 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
106 uint32_t id = idBits.clearFirstMarkedBit();
107 const Pointer& pointer = pointerForId(id);
108 x += pointer.x;
109 y += pointer.y;
110 }
111 x /= count;
112 y /= count;
113 }
114 *outX = x;
115 *outY = y;
116}
117
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118// --- TouchInputMapper ---
119
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800120TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
121 : InputMapper(deviceContext),
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +0000122 mTouchButtonAccumulator(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700123 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100124 mDeviceMode(DeviceMode::DISABLED),
Michael Wrighta9cf4192022-12-01 23:46:39 +0000125 mInputDeviceOrientation(ui::ROTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700126
127TouchInputMapper::~TouchInputMapper() {}
128
Philip Junker4af3b3d2021-12-14 10:36:55 +0100129uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130 return mSource;
131}
132
133void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
134 InputMapper::populateDeviceInfo(info);
135
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000136 if (mDeviceMode == DeviceMode::DISABLED) {
137 return;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138 }
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000139
140 info->addMotionRange(mOrientedRanges.x);
141 info->addMotionRange(mOrientedRanges.y);
142 info->addMotionRange(mOrientedRanges.pressure);
143
144 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
145 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
146 //
147 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
148 // motion, i.e. the hardware dimensions, as the finger could move completely across the
149 // touchpad in one sample cycle.
150 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
151 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
152 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat, x.fuzz,
153 x.resolution);
154 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat, y.fuzz,
155 y.resolution);
156 }
157
158 if (mOrientedRanges.size) {
159 info->addMotionRange(*mOrientedRanges.size);
160 }
161
162 if (mOrientedRanges.touchMajor) {
163 info->addMotionRange(*mOrientedRanges.touchMajor);
164 info->addMotionRange(*mOrientedRanges.touchMinor);
165 }
166
167 if (mOrientedRanges.toolMajor) {
168 info->addMotionRange(*mOrientedRanges.toolMajor);
169 info->addMotionRange(*mOrientedRanges.toolMinor);
170 }
171
172 if (mOrientedRanges.orientation) {
173 info->addMotionRange(*mOrientedRanges.orientation);
174 }
175
176 if (mOrientedRanges.distance) {
177 info->addMotionRange(*mOrientedRanges.distance);
178 }
179
180 if (mOrientedRanges.tilt) {
181 info->addMotionRange(*mOrientedRanges.tilt);
182 }
183
184 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
185 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
186 }
187 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
188 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
189 }
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000190 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
Prabir Pradhan167c2702022-09-14 00:37:24 +0000191 info->setSupportsUsi(mParameters.supportsUsi);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700192}
193
194void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700195 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800196 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700197 dumpParameters(dump);
198 dumpVirtualKeys(dump);
199 dumpRawPointerAxes(dump);
200 dumpCalibration(dump);
201 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700202 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700203
204 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000205 mRawToDisplay.dump(dump, "RawToDisplay Transform:", INDENT4);
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000206 mRawRotation.dump(dump, "RawRotation Transform:", INDENT4);
207 dump += StringPrintf(INDENT4 "OrientedXPrecision: %0.3f\n", mOrientedXPrecision);
208 dump += StringPrintf(INDENT4 "OrientedYPrecision: %0.3f\n", mOrientedYPrecision);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
210 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
211 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
212 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
213 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
214 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
215 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
216 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
217 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
218 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
219
220 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
221 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
222 mLastRawState.rawPointerData.pointerCount);
223 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
224 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
225 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
226 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
227 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
228 "toolType=%d, isHovering=%s\n",
229 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
230 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
231 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
232 pointer.distance, pointer.toolType, toString(pointer.isHovering));
233 }
234
235 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
236 mLastCookedState.buttonState);
237 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
238 mLastCookedState.cookedPointerData.pointerCount);
239 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
240 const PointerProperties& pointerProperties =
241 mLastCookedState.cookedPointerData.pointerProperties[i];
242 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000243 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
244 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
245 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
247 "toolType=%d, isHovering=%s\n",
248 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000249 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
250 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700251 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
252 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
253 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
254 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
255 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
256 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
257 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
258 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
259 pointerProperties.toolType,
260 toString(mLastCookedState.cookedPointerData.isHovering(i)));
261 }
262
263 dump += INDENT3 "Stylus Fusion:\n";
264 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
265 toString(mExternalStylusConnected));
Prabir Pradhan8d9ba912022-11-11 22:26:33 +0000266 dump += StringPrintf(INDENT4 "Fused External Stylus Pointer ID: %s\n",
267 toString(mFusedStylusPointerId).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
269 mExternalStylusFusionTimeout);
Prabir Pradhan124ea442022-10-28 20:27:44 +0000270 dump += StringPrintf(INDENT4 " External Stylus Buttons Applied: 0x%08x",
271 mExternalStylusButtonsApplied);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 dump += INDENT3 "External Stylus State:\n";
273 dumpStylusState(dump, mExternalStylusState);
274
Michael Wright227c5542020-07-02 18:30:52 +0100275 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
277 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
278 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
279 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
280 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
281 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
282 }
283}
284
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700285std::list<NotifyArgs> TouchInputMapper::configure(nsecs_t when,
286 const InputReaderConfiguration* config,
287 uint32_t changes) {
288 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700289
290 mConfig = *config;
291
292 if (!changes) { // first time only
293 // Configure basic parameters.
294 configureParameters();
295
296 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800297 mCursorScrollAccumulator.configure(getDeviceContext());
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +0000298 mTouchButtonAccumulator.configure();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299
300 // Configure absolute axis information.
301 configureRawPointerAxes();
302
303 // Prepare input device calibration.
304 parseCalibration();
305 resolveCalibration();
306 }
307
308 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
309 // Update location calibration to reflect current settings
310 updateAffineTransformation();
311 }
312
313 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
314 // Update pointer speed.
315 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
316 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
317 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
318 }
319
320 bool resetNeeded = false;
321 if (!changes ||
322 (changes &
323 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800324 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700325 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
326 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
327 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700328 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700330 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700331 }
332
333 if (changes && resetNeeded) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700334 out += reset(when);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000335
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 // Send reset, unless this is the first time the device has been configured,
337 // in which case the reader will call reset itself after all mappers are ready.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +0000338 out.emplace_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId()));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700339 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700340 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341}
342
343void TouchInputMapper::resolveExternalStylusPresence() {
344 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800345 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 mExternalStylusConnected = !devices.empty();
347
348 if (!mExternalStylusConnected) {
349 resetExternalStylus();
350 }
351}
352
353void TouchInputMapper::configureParameters() {
354 // Use the pointer presentation mode for devices that do not support distinct
355 // multitouch. The spot-based presentation relies on being able to accurately
356 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800357 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100358 ? Parameters::GestureMode::SINGLE_TOUCH
359 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700360
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700361 std::string gestureModeString;
362 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800363 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700364 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100365 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700366 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100367 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700368 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700369 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700370 }
371 }
372
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000373 configureDeviceType();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800375 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700376
Michael Wright227c5542020-07-02 18:30:52 +0100377 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700378 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800379 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380
Michael Wrighta9cf4192022-12-01 23:46:39 +0000381 mParameters.orientation = ui::ROTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700382 std::string orientationString;
383 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700384 orientationString)) {
385 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
386 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
387 } else if (orientationString == "ORIENTATION_90") {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000388 mParameters.orientation = ui::ROTATION_90;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700389 } else if (orientationString == "ORIENTATION_180") {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000390 mParameters.orientation = ui::ROTATION_180;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700391 } else if (orientationString == "ORIENTATION_270") {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000392 mParameters.orientation = ui::ROTATION_270;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700393 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700394 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700395 }
396 }
397
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398 mParameters.hasAssociatedDisplay = false;
399 mParameters.associatedDisplayIsExternal = false;
400 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100401 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000402 mParameters.deviceType == Parameters::DeviceType::POINTER ||
403 (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION &&
404 getDeviceContext().getAssociatedViewport())) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100406 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800407 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700408 std::string uniqueDisplayId;
409 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800410 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700411 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
412 }
413 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800414 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700415 mParameters.hasAssociatedDisplay = true;
416 }
417
418 // Initial downs on external touch devices should wake the device.
419 // Normally we don't do this for internal touch screens to prevent them from waking
420 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800421 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700422 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhan167c2702022-09-14 00:37:24 +0000423
424 mParameters.supportsUsi = false;
425 getDeviceContext().getConfiguration().tryGetProperty("touch.supportsUsi",
426 mParameters.supportsUsi);
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700427
428 mParameters.enableForInactiveViewport = false;
429 getDeviceContext().getConfiguration().tryGetProperty("touch.enableForInactiveViewport",
430 mParameters.enableForInactiveViewport);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431}
432
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000433void TouchInputMapper::configureDeviceType() {
434 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
435 // The device is a touch screen.
436 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
437 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
438 // The device is a pointing device like a track pad.
439 mParameters.deviceType = Parameters::DeviceType::POINTER;
440 } else {
441 // The device is a touch pad of unknown purpose.
442 mParameters.deviceType = Parameters::DeviceType::POINTER;
443 }
444
445 // Type association takes precedence over the device type found in the idc file.
446 std::string deviceTypeString = getDeviceContext().getDeviceTypeAssociation().value_or("");
447 if (deviceTypeString.empty()) {
448 getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType", deviceTypeString);
449 }
450 if (deviceTypeString == "touchScreen") {
451 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
452 } else if (deviceTypeString == "touchNavigation") {
453 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
454 } else if (deviceTypeString == "pointer") {
455 mParameters.deviceType = Parameters::DeviceType::POINTER;
456 } else if (deviceTypeString != "default" && deviceTypeString != "") {
457 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
458 }
459}
460
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461void TouchInputMapper::dumpParameters(std::string& dump) {
462 dump += INDENT3 "Parameters:\n";
463
Dominik Laskowski75788452021-02-09 18:51:25 -0800464 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700465
Dominik Laskowski75788452021-02-09 18:51:25 -0800466 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467
468 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
469 "displayId='%s'\n",
470 toString(mParameters.hasAssociatedDisplay),
471 toString(mParameters.associatedDisplayIsExternal),
472 mParameters.uniqueDisplayId.c_str());
473 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800474 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhan167c2702022-09-14 00:37:24 +0000475 dump += StringPrintf(INDENT4 "SupportsUsi: %s\n", toString(mParameters.supportsUsi));
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700476 dump += StringPrintf(INDENT4 "EnableForInactiveViewport: %s\n",
477 toString(mParameters.enableForInactiveViewport));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700478}
479
480void TouchInputMapper::configureRawPointerAxes() {
481 mRawPointerAxes.clear();
482}
483
484void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
485 dump += INDENT3 "Raw Touch Axes:\n";
486 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
487 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
488 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
489 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
490 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
491 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
492 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
493 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
494 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
495 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
496 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
497 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
498 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
499}
500
501bool TouchInputMapper::hasExternalStylus() const {
502 return mExternalStylusConnected;
503}
504
505/**
506 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000507 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800508 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000509 * 3. Get the matching viewport by either unique id in idc file or by the display type
510 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800511 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700512 */
513std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800514 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000515 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800516 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700517 }
518
Christine Franks2a2293c2022-01-18 11:51:16 -0800519 const std::optional<std::string> associatedDisplayUniqueId =
520 getDeviceContext().getAssociatedDisplayUniqueId();
521 if (associatedDisplayUniqueId) {
522 return getDeviceContext().getAssociatedViewport();
523 }
524
Michael Wright227c5542020-07-02 18:30:52 +0100525 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800526 std::optional<DisplayViewport> viewport =
527 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
528 if (viewport) {
529 return viewport;
530 } else {
531 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
532 mConfig.defaultPointerDisplayId);
533 }
534 }
535
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700536 // Check if uniqueDisplayId is specified in idc file.
537 if (!mParameters.uniqueDisplayId.empty()) {
538 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
539 }
540
541 ViewportType viewportTypeToUse;
542 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100543 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700544 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100545 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700546 }
547
548 std::optional<DisplayViewport> viewport =
549 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100550 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700551 ALOGW("Input device %s should be associated with external display, "
552 "fallback to internal one for the external viewport is not found.",
553 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100554 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700555 }
556
557 return viewport;
558 }
559
560 // No associated display, return a non-display viewport.
561 DisplayViewport newViewport;
562 // Raw width and height in the natural orientation.
563 int32_t rawWidth = mRawPointerAxes.getRawWidth();
564 int32_t rawHeight = mRawPointerAxes.getRawHeight();
565 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
566 return std::make_optional(newViewport);
567}
568
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800569int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
570 if (resolution < 0) {
571 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
572 getDeviceName().c_str());
573 return 0;
574 }
575 return resolution;
576}
577
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800578void TouchInputMapper::initializeSizeRanges() {
579 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
580 mSizeScale = 0.0f;
581 return;
582 }
583
584 // Size of diagonal axis.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000585 const float diagonalSize = hypotf(mDisplayBounds.width, mDisplayBounds.height);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800586
587 // Size factors.
588 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
589 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
590 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
591 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
592 } else {
593 mSizeScale = 0.0f;
594 }
595
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700596 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
597 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
598 .source = mSource,
599 .min = 0,
600 .max = diagonalSize,
601 .flat = 0,
602 .fuzz = 0,
603 .resolution = 0,
604 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800605
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800606 if (mRawPointerAxes.touchMajor.valid) {
607 mRawPointerAxes.touchMajor.resolution =
608 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700609 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800610 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800611
612 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700613 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800614 if (mRawPointerAxes.touchMinor.valid) {
615 mRawPointerAxes.touchMinor.resolution =
616 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700617 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800618 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800619
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700620 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
621 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
622 .source = mSource,
623 .min = 0,
624 .max = diagonalSize,
625 .flat = 0,
626 .fuzz = 0,
627 .resolution = 0,
628 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800629 if (mRawPointerAxes.toolMajor.valid) {
630 mRawPointerAxes.toolMajor.resolution =
631 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700632 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800633 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800634
635 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700636 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800637 if (mRawPointerAxes.toolMinor.valid) {
638 mRawPointerAxes.toolMinor.resolution =
639 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700640 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800641 }
642
643 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700644 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
645 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
646 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
647 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800648 } else {
649 // Support for other calibrations can be added here.
650 ALOGW("%s calibration is not supported for size ranges at the moment. "
651 "Using raw resolution instead",
652 ftl::enum_string(mCalibration.sizeCalibration).c_str());
653 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800654
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700655 mOrientedRanges.size = InputDeviceInfo::MotionRange{
656 .axis = AMOTION_EVENT_AXIS_SIZE,
657 .source = mSource,
658 .min = 0,
659 .max = 1.0,
660 .flat = 0,
661 .fuzz = 0,
662 .resolution = 0,
663 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800664}
665
666void TouchInputMapper::initializeOrientedRanges() {
667 // Configure X and Y factors.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000668 const float orientedScaleX = mRawToDisplay.getScaleX();
669 const float orientedScaleY = mRawToDisplay.getScaleY();
670 mOrientedXPrecision = 1.0f / orientedScaleX;
671 mOrientedYPrecision = 1.0f / orientedScaleY;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800672
673 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
674 mOrientedRanges.x.source = mSource;
675 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
676 mOrientedRanges.y.source = mSource;
677
678 // Scale factor for terms that are not oriented in a particular axis.
679 // If the pixels are square then xScale == yScale otherwise we fake it
680 // by choosing an average.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000681 mGeometricScale = avg(orientedScaleX, orientedScaleY);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800682
683 initializeSizeRanges();
684
685 // Pressure factors.
686 mPressureScale = 0;
687 float pressureMax = 1.0;
688 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
689 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700690 if (mCalibration.pressureScale) {
691 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800692 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
693 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
694 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
695 }
696 }
697
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700698 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
699 .axis = AMOTION_EVENT_AXIS_PRESSURE,
700 .source = mSource,
701 .min = 0,
702 .max = pressureMax,
703 .flat = 0,
704 .fuzz = 0,
705 .resolution = 0,
706 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800707
708 // Tilt
709 mTiltXCenter = 0;
710 mTiltXScale = 0;
711 mTiltYCenter = 0;
712 mTiltYScale = 0;
713 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
714 if (mHaveTilt) {
715 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
716 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
717 mTiltXScale = M_PI / 180;
718 mTiltYScale = M_PI / 180;
719
720 if (mRawPointerAxes.tiltX.resolution) {
721 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
722 }
723 if (mRawPointerAxes.tiltY.resolution) {
724 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
725 }
726
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700727 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
728 .axis = AMOTION_EVENT_AXIS_TILT,
729 .source = mSource,
730 .min = 0,
731 .max = M_PI_2,
732 .flat = 0,
733 .fuzz = 0,
734 .resolution = 0,
735 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800736 }
737
738 // Orientation
739 mOrientationScale = 0;
740 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700741 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
742 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
743 .source = mSource,
744 .min = -M_PI,
745 .max = M_PI,
746 .flat = 0,
747 .fuzz = 0,
748 .resolution = 0,
749 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800750
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800751 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
752 if (mCalibration.orientationCalibration ==
753 Calibration::OrientationCalibration::INTERPOLATED) {
754 if (mRawPointerAxes.orientation.valid) {
755 if (mRawPointerAxes.orientation.maxValue > 0) {
756 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
757 } else if (mRawPointerAxes.orientation.minValue < 0) {
758 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
759 } else {
760 mOrientationScale = 0;
761 }
762 }
763 }
764
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700765 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
766 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
767 .source = mSource,
768 .min = -M_PI_2,
769 .max = M_PI_2,
770 .flat = 0,
771 .fuzz = 0,
772 .resolution = 0,
773 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800774 }
775
776 // Distance
777 mDistanceScale = 0;
778 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
779 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700780 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800781 }
782
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700783 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800784
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700785 .axis = AMOTION_EVENT_AXIS_DISTANCE,
786 .source = mSource,
787 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
788 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
789 .flat = 0,
790 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
791 .resolution = 0,
792 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800793 }
794
795 // Compute oriented precision, scales and ranges.
796 // Note that the maximum value reported is an inclusive maximum value so it is one
797 // unit less than the total width or height of the display.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000798 // TODO(b/20508709): Calculate the oriented ranges using the input device's raw frame.
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800799 switch (mInputDeviceOrientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000800 case ui::ROTATION_90:
801 case ui::ROTATION_270:
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800802 mOrientedRanges.x.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000803 mOrientedRanges.x.max = mDisplayBounds.height - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800804 mOrientedRanges.x.flat = 0;
805 mOrientedRanges.x.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000806 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mRawToDisplay.getScaleY();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800807
808 mOrientedRanges.y.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000809 mOrientedRanges.y.max = mDisplayBounds.width - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800810 mOrientedRanges.y.flat = 0;
811 mOrientedRanges.y.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000812 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mRawToDisplay.getScaleX();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800813 break;
814
815 default:
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800816 mOrientedRanges.x.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000817 mOrientedRanges.x.max = mDisplayBounds.width - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800818 mOrientedRanges.x.flat = 0;
819 mOrientedRanges.x.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000820 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mRawToDisplay.getScaleX();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800821
822 mOrientedRanges.y.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000823 mOrientedRanges.y.max = mDisplayBounds.height - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800824 mOrientedRanges.y.flat = 0;
825 mOrientedRanges.y.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000826 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mRawToDisplay.getScaleY();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800827 break;
828 }
829}
830
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000831void TouchInputMapper::computeInputTransforms() {
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000832 const ui::Size rawSize{mRawPointerAxes.getRawWidth(), mRawPointerAxes.getRawHeight()};
833
834 ui::Size rotatedRawSize = rawSize;
835 if (mInputDeviceOrientation == ui::ROTATION_270 || mInputDeviceOrientation == ui::ROTATION_90) {
836 std::swap(rotatedRawSize.width, rotatedRawSize.height);
837 }
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000838 const auto rotationFlags = ui::Transform::toRotationFlags(-mInputDeviceOrientation);
839 mRawRotation = ui::Transform{rotationFlags};
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000840
841 // Step 1: Undo the raw offset so that the raw coordinate space now starts at (0, 0).
842 ui::Transform undoRawOffset;
843 undoRawOffset.set(-mRawPointerAxes.x.minValue, -mRawPointerAxes.y.minValue);
844
845 // Step 2: Rotate the raw coordinates to the expected orientation.
846 ui::Transform rotate;
847 // When rotating raw coordinates, the raw size will be used as an offset.
848 // Account for the extra unit added to the raw range when the raw size was calculated.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000849 rotate.set(rotationFlags, rotatedRawSize.width - 1, rotatedRawSize.height - 1);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000850
851 // Step 3: Scale the raw coordinates to the display space.
852 ui::Transform scaleToDisplay;
853 const float xScale = static_cast<float>(mDisplayBounds.width) / rotatedRawSize.width;
854 const float yScale = static_cast<float>(mDisplayBounds.height) / rotatedRawSize.height;
855 scaleToDisplay.set(xScale, 0, 0, yScale);
856
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000857 mRawToDisplay = (scaleToDisplay * (rotate * undoRawOffset));
858
859 // Calculate the transform that takes raw coordinates to the rotated display space.
860 ui::Transform displayToRotatedDisplay;
861 displayToRotatedDisplay.set(ui::Transform::toRotationFlags(-mViewport.orientation),
862 mViewport.deviceWidth, mViewport.deviceHeight);
863 mRawToRotatedDisplay = displayToRotatedDisplay * mRawToDisplay;
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000864}
865
Prabir Pradhan1728b212021-10-19 16:00:03 -0700866void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000867 const DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700868
869 resolveExternalStylusPresence();
870
871 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100872 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000873 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700874 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100875 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700876 if (hasStylus()) {
877 mSource |= AINPUT_SOURCE_STYLUS;
Harry Cutts16a24cc2022-10-26 15:22:19 +0000878 } else {
879 mSource |= AINPUT_SOURCE_TOUCHPAD;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700880 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800881 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700882 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100883 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700884 if (hasStylus()) {
885 mSource |= AINPUT_SOURCE_STYLUS;
886 }
887 if (hasExternalStylus()) {
888 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
889 }
Michael Wright227c5542020-07-02 18:30:52 +0100890 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700891 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100892 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700893 } else {
894 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100895 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700896 }
897
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000898 const std::optional<DisplayViewport> newViewportOpt = findViewport();
899
900 // Ensure the device is valid and can be used.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700901 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
902 ALOGW("Touch device '%s' did not report support for X or Y axis! "
903 "The device will be inoperable.",
904 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100905 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000906 } else if (!newViewportOpt) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700907 ALOGI("Touch device '%s' could not query the properties of its associated "
908 "display. The device will be inoperable until the display size "
909 "becomes available.",
910 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100911 mDeviceMode = DeviceMode::DISABLED;
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700912 } else if (!mParameters.enableForInactiveViewport && !newViewportOpt->isActive) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000913 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
914 getDeviceName().c_str(), getDeviceId());
915 mDeviceMode = DeviceMode::DISABLED;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000916 }
917
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700918 // Raw width and height in the natural orientation.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000919 const ui::Size rawSize{mRawPointerAxes.getRawWidth(), mRawPointerAxes.getRawHeight()};
HQ Liue6983c72022-04-19 22:14:56 +0000920 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
921 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
922 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
923 const float rawMeanResolution =
924 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700925
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000926 const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport);
927 const bool viewportChanged = mViewport != newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700928 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700929 if (viewportChanged) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000930 const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation;
931 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId;
932 mViewport = newViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700933
Michael Wright227c5542020-07-02 18:30:52 +0100934 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan2d613f42022-11-10 20:22:06 +0000935 const auto oldDisplayBounds = mDisplayBounds;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700936
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000937 mDisplayBounds = getNaturalDisplaySize(mViewport);
938 mPhysicalFrameInRotatedDisplay = {mViewport.physicalLeft, mViewport.physicalTop,
939 mViewport.physicalRight, mViewport.physicalBottom};
Prabir Pradhan5632d622021-09-06 07:57:20 -0700940
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +0000941 // InputReader works in the un-rotated display coordinate space, so we don't need to do
942 // anything if the device is already orientation-aware. If the device is not
943 // orientation-aware, then we need to apply the inverse rotation of the display so that
944 // when the display rotation is applied later as a part of the per-window transform, we
945 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700946 mInputDeviceOrientation = mParameters.orientationAware
Michael Wrighta9cf4192022-12-01 23:46:39 +0000947 ? ui::ROTATION_0
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +0000948 : getInverseRotation(mViewport.orientation);
949 // For orientation-aware devices that work in the un-rotated coordinate space, the
950 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +0000951 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000952 mDisplayBounds == oldDisplayBounds && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700953
954 // Apply the input device orientation for the device.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000955 mInputDeviceOrientation = mInputDeviceOrientation + mParameters.orientation;
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000956 computeInputTransforms();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700957 } else {
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000958 mDisplayBounds = rawSize;
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000959 mPhysicalFrameInRotatedDisplay = Rect{mDisplayBounds};
Michael Wrighta9cf4192022-12-01 23:46:39 +0000960 mInputDeviceOrientation = ui::ROTATION_0;
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000961 mRawToDisplay.reset();
962 mRawToDisplay.set(-mRawPointerAxes.x.minValue, -mRawPointerAxes.y.minValue);
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000963 mRawToRotatedDisplay = mRawToDisplay;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700964 }
965 }
966
967 // If moving between pointer modes, need to reset some state.
968 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
969 if (deviceModeChanged) {
970 mOrientedRanges.clear();
971 }
972
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800973 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
974 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +0100975 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800976 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000977 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
978 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800979 if (mPointerController == nullptr) {
980 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700981 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000982 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800983 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
984 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700985 } else {
lilinnandef700b2022-06-17 19:32:01 +0800986 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
987 !mConfig.showTouches) {
988 mPointerController->clearSpots();
989 }
Michael Wright17db18e2020-06-26 20:51:44 +0100990 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700991 }
992
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700993 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000994 ALOGI("Device reconfigured: id=%d, name='%s', size %s, orientation %d, mode %d, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700995 "display id %d",
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000996 getDeviceId(), getDeviceName().c_str(), toString(mDisplayBounds).c_str(),
Prabir Pradhan1728b212021-10-19 16:00:03 -0700997 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700998
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700999 configureVirtualKeys();
1000
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001001 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001002
1003 // Location
1004 updateAffineTransformation();
1005
Michael Wright227c5542020-07-02 18:30:52 +01001006 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001007 // Compute pointer gesture detection parameters.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001008 float rawDiagonal = hypotf(rawSize.width, rawSize.height);
1009 float displayDiagonal = hypotf(mDisplayBounds.width, mDisplayBounds.height);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001010
1011 // Scale movements such that one whole swipe of the touch pad covers a
1012 // given area relative to the diagonal size of the display when no acceleration
1013 // is applied.
1014 // Assume that the touch pad has a square aspect ratio such that movements in
1015 // X and Y of the same number of raw units cover the same physical distance.
1016 mPointerXMovementScale =
1017 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1018 mPointerYMovementScale = mPointerXMovementScale;
1019
1020 // Scale zooms to cover a smaller range of the display than movements do.
1021 // This value determines the area around the pointer that is affected by freeform
1022 // pointer gestures.
1023 mPointerXZoomScale =
1024 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1025 mPointerYZoomScale = mPointerXZoomScale;
1026
HQ Liue6983c72022-04-19 22:14:56 +00001027 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1028 // axis is non positive value.
1029 const float minFreeformGestureWidth =
1030 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1031
1032 mPointerGestureMaxSwipeWidth =
1033 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1034 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001035 }
1036
1037 // Inform the dispatcher about the changes.
1038 *outResetNeeded = true;
1039 bumpGeneration();
1040 }
1041}
1042
Prabir Pradhan1728b212021-10-19 16:00:03 -07001043void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001044 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001045 dump += StringPrintf(INDENT3 "DisplayBounds: %s\n", toString(mDisplayBounds).c_str());
Prabir Pradhan675f25a2022-11-10 22:04:07 +00001046 dump += StringPrintf(INDENT3 "PhysicalFrameInRotatedDisplay: %s\n",
1047 toString(mPhysicalFrameInRotatedDisplay).c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001048 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001049}
1050
1051void TouchInputMapper::configureVirtualKeys() {
1052 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001053 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001054
1055 mVirtualKeys.clear();
1056
1057 if (virtualKeyDefinitions.size() == 0) {
1058 return;
1059 }
1060
1061 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1062 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1063 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1064 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1065
1066 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1067 VirtualKey virtualKey;
1068
1069 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1070 int32_t keyCode;
1071 int32_t dummyKeyMetaState;
1072 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1074 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001075 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1076 continue; // drop the key
1077 }
1078
1079 virtualKey.keyCode = keyCode;
1080 virtualKey.flags = flags;
1081
1082 // convert the key definition's display coordinates into touch coordinates for a hit box
1083 int32_t halfWidth = virtualKeyDefinition.width / 2;
1084 int32_t halfHeight = virtualKeyDefinition.height / 2;
1085
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001086 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth /
1087 mDisplayBounds.width +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001088 touchScreenLeft;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001089 virtualKey.hitRight = (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth /
1090 mDisplayBounds.width +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001091 touchScreenLeft;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001092 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight /
1093 mDisplayBounds.height +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001094 touchScreenTop;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001095 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight /
1096 mDisplayBounds.height +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001097 touchScreenTop;
1098 mVirtualKeys.push_back(virtualKey);
1099 }
1100}
1101
1102void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1103 if (!mVirtualKeys.empty()) {
1104 dump += INDENT3 "Virtual Keys:\n";
1105
1106 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1107 const VirtualKey& virtualKey = mVirtualKeys[i];
1108 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1109 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1110 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1111 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1112 }
1113 }
1114}
1115
1116void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001117 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001118 Calibration& out = mCalibration;
1119
1120 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001121 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001122 std::string sizeCalibrationString;
1123 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001124 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001125 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001126 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001127 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001128 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001129 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001130 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001131 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001132 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001133 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001134 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001135 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001136 }
1137 }
1138
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001139 float sizeScale;
1140
1141 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1142 out.sizeScale = sizeScale;
1143 }
1144 float sizeBias;
1145 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1146 out.sizeBias = sizeBias;
1147 }
1148 bool sizeIsSummed;
1149 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1150 out.sizeIsSummed = sizeIsSummed;
1151 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001152
1153 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001154 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001155 std::string pressureCalibrationString;
1156 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001157 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001158 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001159 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001160 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001161 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001162 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001163 } else if (pressureCalibrationString != "default") {
1164 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001165 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 }
1167 }
1168
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001169 float pressureScale;
1170 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1171 out.pressureScale = pressureScale;
1172 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001173
1174 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001175 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001176 std::string orientationCalibrationString;
1177 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001178 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001179 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001180 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001181 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001182 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001183 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001184 } else if (orientationCalibrationString != "default") {
1185 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001186 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001187 }
1188 }
1189
1190 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001191 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001192 std::string distanceCalibrationString;
1193 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001194 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001195 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001196 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001197 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001198 } else if (distanceCalibrationString != "default") {
1199 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001200 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001201 }
1202 }
1203
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001204 float distanceScale;
1205 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1206 out.distanceScale = distanceScale;
1207 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001208}
1209
1210void TouchInputMapper::resolveCalibration() {
1211 // Size
1212 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001213 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1214 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001215 }
1216 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001217 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001218 }
1219
1220 // Pressure
1221 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001222 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1223 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001224 }
1225 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001226 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001227 }
1228
1229 // Orientation
1230 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001231 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1232 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001233 }
1234 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001235 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001236 }
1237
1238 // Distance
1239 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001240 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1241 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001242 }
1243 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001244 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001245 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001246}
1247
1248void TouchInputMapper::dumpCalibration(std::string& dump) {
1249 dump += INDENT3 "Calibration:\n";
1250
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001251 dump += INDENT4 "touch.size.calibration: ";
1252 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001253
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001254 if (mCalibration.sizeScale) {
1255 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001256 }
1257
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001258 if (mCalibration.sizeBias) {
1259 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260 }
1261
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001262 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001263 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001264 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001265 }
1266
1267 // Pressure
1268 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001269 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001270 dump += INDENT4 "touch.pressure.calibration: none\n";
1271 break;
Michael Wright227c5542020-07-02 18:30:52 +01001272 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001273 dump += INDENT4 "touch.pressure.calibration: physical\n";
1274 break;
Michael Wright227c5542020-07-02 18:30:52 +01001275 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001276 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1277 break;
1278 default:
1279 ALOG_ASSERT(false);
1280 }
1281
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001282 if (mCalibration.pressureScale) {
1283 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001284 }
1285
1286 // Orientation
1287 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001288 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001289 dump += INDENT4 "touch.orientation.calibration: none\n";
1290 break;
Michael Wright227c5542020-07-02 18:30:52 +01001291 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001292 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1293 break;
Michael Wright227c5542020-07-02 18:30:52 +01001294 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001295 dump += INDENT4 "touch.orientation.calibration: vector\n";
1296 break;
1297 default:
1298 ALOG_ASSERT(false);
1299 }
1300
1301 // Distance
1302 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001303 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001304 dump += INDENT4 "touch.distance.calibration: none\n";
1305 break;
Michael Wright227c5542020-07-02 18:30:52 +01001306 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001307 dump += INDENT4 "touch.distance.calibration: scaled\n";
1308 break;
1309 default:
1310 ALOG_ASSERT(false);
1311 }
1312
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001313 if (mCalibration.distanceScale) {
1314 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001315 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001316}
1317
1318void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1319 dump += INDENT3 "Affine Transformation:\n";
1320
1321 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1322 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1323 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1324 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1325 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1326 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1327}
1328
1329void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001330 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001331 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001332}
1333
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001334std::list<NotifyArgs> TouchInputMapper::reset(nsecs_t when) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001335 std::list<NotifyArgs> out = cancelTouch(when, when);
1336 updateTouchSpots();
1337
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001338 mCursorButtonAccumulator.reset(getDeviceContext());
1339 mCursorScrollAccumulator.reset(getDeviceContext());
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00001340 mTouchButtonAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001341
1342 mPointerVelocityControl.reset();
1343 mWheelXVelocityControl.reset();
1344 mWheelYVelocityControl.reset();
1345
1346 mRawStatesPending.clear();
1347 mCurrentRawState.clear();
1348 mCurrentCookedState.clear();
1349 mLastRawState.clear();
1350 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001351 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001352 mSentHoverEnter = false;
1353 mHavePointerIds = false;
1354 mCurrentMotionAborted = false;
1355 mDownTime = 0;
1356
1357 mCurrentVirtualKey.down = false;
1358
1359 mPointerGesture.reset();
1360 mPointerSimple.reset();
1361 resetExternalStylus();
1362
1363 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001364 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001365 mPointerController->clearSpots();
1366 }
1367
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001368 return out += InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001369}
1370
1371void TouchInputMapper::resetExternalStylus() {
1372 mExternalStylusState.clear();
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001373 mFusedStylusPointerId.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001374 mExternalStylusFusionTimeout = LLONG_MAX;
1375 mExternalStylusDataPending = false;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001376 mExternalStylusButtonsApplied = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001377}
1378
1379void TouchInputMapper::clearStylusDataPendingFlags() {
1380 mExternalStylusDataPending = false;
1381 mExternalStylusFusionTimeout = LLONG_MAX;
1382}
1383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001384std::list<NotifyArgs> TouchInputMapper::process(const RawEvent* rawEvent) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001385 mCursorButtonAccumulator.process(rawEvent);
1386 mCursorScrollAccumulator.process(rawEvent);
1387 mTouchButtonAccumulator.process(rawEvent);
1388
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001389 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001390 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001391 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001392 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001393 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001394}
1395
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001396std::list<NotifyArgs> TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
1397 std::list<NotifyArgs> out;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00001398 if (mDeviceMode == DeviceMode::DISABLED) {
1399 // Only save the last pending state when the device is disabled.
1400 mRawStatesPending.clear();
1401 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001402 // Push a new state.
1403 mRawStatesPending.emplace_back();
1404
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001405 RawState& next = mRawStatesPending.back();
1406 next.clear();
1407 next.when = when;
1408 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001409
1410 // Sync button state.
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00001411 next.buttonState = filterButtonState(mConfig,
1412 mTouchButtonAccumulator.getButtonState() |
1413 mCursorButtonAccumulator.getButtonState());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001414
1415 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001416 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1417 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001418 mCursorScrollAccumulator.finishSync();
1419
1420 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001421 syncTouch(when, &next);
1422
1423 // The last RawState is the actually second to last, since we just added a new state
1424 const RawState& last =
1425 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001426
Prabir Pradhan61a243a2022-11-16 23:47:36 +00001427 std::tie(next.when, next.readTime) =
1428 applyBluetoothTimestampSmoothening(getDeviceContext().getDeviceIdentifier(), when,
1429 readTime, last.when);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00001430
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001431 // Assign pointer ids.
1432 if (!mHavePointerIds) {
1433 assignPointerIds(last, next);
1434 }
1435
Harry Cutts45483602022-08-24 14:36:48 +00001436 ALOGD_IF(DEBUG_RAW_EVENTS,
1437 "syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1438 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1439 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1440 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1441 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1442 next.rawPointerData.canceledIdBits.value);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001443
Arthur Hung9ad18942021-06-19 02:04:46 +00001444 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1445 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1446 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1447 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1448 next.rawPointerData.hoveringIdBits.value);
1449 }
1450
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001451 out += processRawTouches(false /*timeout*/);
1452 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001453}
1454
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001455std::list<NotifyArgs> TouchInputMapper::processRawTouches(bool timeout) {
1456 std::list<NotifyArgs> out;
Michael Wright227c5542020-07-02 18:30:52 +01001457 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001458 // Do not process raw event while the device is disabled.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001459 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001460 }
1461
1462 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1463 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1464 // touching the current state will only observe the events that have been dispatched to the
1465 // rest of the pipeline.
1466 const size_t N = mRawStatesPending.size();
1467 size_t count;
1468 for (count = 0; count < N; count++) {
1469 const RawState& next = mRawStatesPending[count];
1470
1471 // A failure to assign the stylus id means that we're waiting on stylus data
1472 // and so should defer the rest of the pipeline.
1473 if (assignExternalStylusId(next, timeout)) {
1474 break;
1475 }
1476
1477 // All ready to go.
1478 clearStylusDataPendingFlags();
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001479 mCurrentRawState = next;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001480 if (mCurrentRawState.when < mLastRawState.when) {
1481 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001482 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001483 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001484 out += cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001485 }
1486 if (count != 0) {
1487 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1488 }
1489
1490 if (mExternalStylusDataPending) {
1491 if (timeout) {
1492 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1493 clearStylusDataPendingFlags();
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001494 mCurrentRawState = mLastRawState;
Harry Cutts45483602022-08-24 14:36:48 +00001495 ALOGD_IF(DEBUG_STYLUS_FUSION,
1496 "Timeout expired, synthesizing event with new stylus data");
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001497 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001498 out += cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001499 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1500 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1501 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1502 }
1503 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001504 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001505}
1506
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001507std::list<NotifyArgs> TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
1508 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001509 // Always start with a clean state.
1510 mCurrentCookedState.clear();
1511
1512 // Apply stylus buttons to current raw state.
1513 applyExternalStylusButtonState(when);
1514
1515 // Handle policy on initial down or hover events.
1516 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1517 mCurrentRawState.rawPointerData.pointerCount != 0;
1518
1519 uint32_t policyFlags = 0;
1520 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1521 if (initialDown || buttonsPressed) {
1522 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001523 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001524 getContext()->fadePointer();
1525 }
1526
1527 if (mParameters.wake) {
1528 policyFlags |= POLICY_FLAG_WAKE;
1529 }
1530 }
1531
1532 // Consume raw off-screen touches before cooking pointer data.
1533 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001534 bool consumed;
1535 out += consumeRawTouches(when, readTime, policyFlags, consumed /*byref*/);
1536 if (consumed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001537 mCurrentRawState.rawPointerData.clear();
1538 }
1539
1540 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1541 // with cooked pointer data that has the same ids and indices as the raw data.
1542 // The following code can use either the raw or cooked data, as needed.
1543 cookPointerData();
1544
1545 // Apply stylus pressure to current cooked state.
1546 applyExternalStylusTouchState(when);
1547
1548 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001549 out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1550 mSource, mViewport.displayId, policyFlags,
1551 mLastCookedState.buttonState, mCurrentCookedState.buttonState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001552
1553 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001554 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001555 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1556 uint32_t id = idBits.clearFirstMarkedBit();
1557 const RawPointerData::Pointer& pointer =
1558 mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhane5626962022-10-27 20:30:53 +00001559 if (isStylusToolType(pointer.toolType)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001560 mCurrentCookedState.stylusIdBits.markBit(id);
1561 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1562 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1563 mCurrentCookedState.fingerIdBits.markBit(id);
1564 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1565 mCurrentCookedState.mouseIdBits.markBit(id);
1566 }
1567 }
1568 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1569 uint32_t id = idBits.clearFirstMarkedBit();
1570 const RawPointerData::Pointer& pointer =
1571 mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhane5626962022-10-27 20:30:53 +00001572 if (isStylusToolType(pointer.toolType)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001573 mCurrentCookedState.stylusIdBits.markBit(id);
1574 }
1575 }
1576
1577 // Stylus takes precedence over all tools, then mouse, then finger.
1578 PointerUsage pointerUsage = mPointerUsage;
1579 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1580 mCurrentCookedState.mouseIdBits.clear();
1581 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001582 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001583 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1584 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001585 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001586 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1587 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001588 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001589 }
1590
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001591 out += dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001592 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001593 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001594 updateTouchSpots();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001595 out += dispatchButtonRelease(when, readTime, policyFlags);
1596 out += dispatchHoverExit(when, readTime, policyFlags);
1597 out += dispatchTouches(when, readTime, policyFlags);
1598 out += dispatchHoverEnterAndMove(when, readTime, policyFlags);
1599 out += dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001600 }
1601
1602 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1603 mCurrentMotionAborted = false;
1604 }
1605 }
1606
1607 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001608 out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(),
1609 mSource, mViewport.displayId, policyFlags,
1610 mLastCookedState.buttonState, mCurrentCookedState.buttonState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001611
1612 // Clear some transient state.
1613 mCurrentRawState.rawVScroll = 0;
1614 mCurrentRawState.rawHScroll = 0;
1615
1616 // Copy current touch to last touch in preparation for the next cycle.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001617 mLastRawState = mCurrentRawState;
1618 mLastCookedState = mCurrentCookedState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001619 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001620}
1621
Garfield Tanc734e4f2021-01-15 20:01:39 -08001622void TouchInputMapper::updateTouchSpots() {
1623 if (!mConfig.showTouches || mPointerController == nullptr) {
1624 return;
1625 }
1626
1627 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1628 // clear touch spots.
1629 if (mDeviceMode != DeviceMode::DIRECT &&
1630 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1631 return;
1632 }
1633
1634 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1635 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1636
1637 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001638 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords.cbegin(),
1639 mCurrentCookedState.cookedPointerData.idToIndex.cbegin(),
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001640 mCurrentCookedState.cookedPointerData.touchingIdBits,
1641 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001642}
1643
1644bool TouchInputMapper::isTouchScreen() {
1645 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1646 mParameters.hasAssociatedDisplay;
1647}
1648
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001649void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001650 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus()) {
1651 // If any of the external buttons are already pressed by the touch device, ignore them.
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00001652 const int32_t pressedButtons =
1653 filterButtonState(mConfig,
1654 ~mCurrentRawState.buttonState & mExternalStylusState.buttons);
Prabir Pradhan124ea442022-10-28 20:27:44 +00001655 const int32_t releasedButtons =
1656 mExternalStylusButtonsApplied & ~mExternalStylusState.buttons;
1657
1658 mCurrentRawState.buttonState |= pressedButtons;
1659 mCurrentRawState.buttonState &= ~releasedButtons;
1660
1661 mExternalStylusButtonsApplied |= pressedButtons;
1662 mExternalStylusButtonsApplied &= ~releasedButtons;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001663 }
1664}
1665
1666void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1667 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1668 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001669 if (!mFusedStylusPointerId || !currentPointerData.isTouching(*mFusedStylusPointerId)) {
1670 return;
1671 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001672
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001673 float pressure = lastPointerData.isTouching(*mFusedStylusPointerId)
1674 ? lastPointerData.pointerCoordsForId(*mFusedStylusPointerId)
1675 .getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)
1676 : 0.f;
1677 if (mExternalStylusState.pressure && *mExternalStylusState.pressure > 0.f) {
1678 pressure = *mExternalStylusState.pressure;
1679 }
1680 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(*mFusedStylusPointerId);
1681 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001682
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001683 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001684 PointerProperties& properties =
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001685 currentPointerData.editPointerPropertiesWithId(*mFusedStylusPointerId);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001686 properties.toolType = mExternalStylusState.toolType;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001687 }
1688}
1689
1690bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001691 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001692 return false;
1693 }
1694
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001695 // Check if the stylus pointer has gone up.
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001696 if (mFusedStylusPointerId &&
1697 !state.rawPointerData.touchingIdBits.hasBit(*mFusedStylusPointerId)) {
Harry Cutts45483602022-08-24 14:36:48 +00001698 ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus pointer is going up");
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001699 mFusedStylusPointerId.reset();
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001700 return false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001701 }
1702
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001703 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1704 state.rawPointerData.pointerCount != 0;
1705 if (!initialDown) {
1706 return false;
1707 }
1708
1709 if (!mExternalStylusState.pressure) {
1710 ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus does not support pressure, no pointer fusion needed");
1711 return false;
1712 }
1713
1714 if (*mExternalStylusState.pressure != 0.0f) {
1715 ALOGD_IF(DEBUG_STYLUS_FUSION, "Have both stylus and touch data, beginning fusion");
1716 mFusedStylusPointerId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1717 return false;
1718 }
1719
1720 if (timeout) {
1721 ALOGD_IF(DEBUG_STYLUS_FUSION, "Timeout expired, assuming touch is not a stylus.");
1722 mFusedStylusPointerId.reset();
1723 mExternalStylusFusionTimeout = LLONG_MAX;
1724 return false;
1725 }
1726
1727 // We are waiting for the external stylus to report a pressure value. Withhold touches from
1728 // being processed until we either get pressure data or timeout.
1729 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1730 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1731 }
1732 ALOGD_IF(DEBUG_STYLUS_FUSION,
1733 "No stylus data but stylus is connected, requesting timeout (%" PRId64 "ms)",
1734 mExternalStylusFusionTimeout);
1735 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1736 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001737}
1738
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001739std::list<NotifyArgs> TouchInputMapper::timeoutExpired(nsecs_t when) {
1740 std::list<NotifyArgs> out;
Michael Wright227c5542020-07-02 18:30:52 +01001741 if (mDeviceMode == DeviceMode::POINTER) {
1742 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001743 // Since this is a synthetic event, we can consider its latency to be zero
1744 const nsecs_t readTime = when;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001745 out += dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001746 }
Michael Wright227c5542020-07-02 18:30:52 +01001747 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001748 if (mExternalStylusFusionTimeout <= when) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001749 out += processRawTouches(true /*timeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001750 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1751 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1752 }
1753 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001754 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001755}
1756
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001757std::list<NotifyArgs> TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1758 std::list<NotifyArgs> out;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001759 const bool buttonsChanged = mExternalStylusState.buttons != state.buttons;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001760 mExternalStylusState = state;
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001761 if (mFusedStylusPointerId || mExternalStylusFusionTimeout != LLONG_MAX || buttonsChanged) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001762 // The following three cases are handled here:
1763 // - We're in the middle of a fused stream of data;
1764 // - We're waiting on external stylus data before dispatching the initial down; or
1765 // - Only the button state, which is not reported through a specific pointer, has changed.
1766 // Go ahead and dispatch now that we have fresh stylus data.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001767 mExternalStylusDataPending = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001768 out += processRawTouches(false /*timeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001769 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001770 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001771}
1772
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001773std::list<NotifyArgs> TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime,
1774 uint32_t policyFlags, bool& outConsumed) {
1775 outConsumed = false;
1776 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001777 // Check for release of a virtual key.
1778 if (mCurrentVirtualKey.down) {
1779 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1780 // Pointer went up while virtual key was down.
1781 mCurrentVirtualKey.down = false;
1782 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001783 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1784 "VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1785 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001786 out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
1787 AKEY_EVENT_FLAG_FROM_SYSTEM |
1788 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001789 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001790 outConsumed = true;
1791 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001792 }
1793
1794 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1795 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1796 const RawPointerData::Pointer& pointer =
1797 mCurrentRawState.rawPointerData.pointerForId(id);
1798 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1799 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1800 // Pointer is still within the space of the virtual key.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001801 outConsumed = true;
1802 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001803 }
1804 }
1805
1806 // Pointer left virtual key area or another pointer also went down.
1807 // Send key cancellation but do not consume the touch yet.
1808 // This is useful when the user swipes through from the virtual key area
1809 // into the main display surface.
1810 mCurrentVirtualKey.down = false;
1811 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001812 ALOGD_IF(DEBUG_VIRTUAL_KEYS, "VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1813 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001814 out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
1815 AKEY_EVENT_FLAG_FROM_SYSTEM |
1816 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1817 AKEY_EVENT_FLAG_CANCELED));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001818 }
1819 }
1820
1821 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1822 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1823 // Pointer just went down. Check for virtual key press or off-screen touches.
1824 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1825 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001826 // Skip checking whether the pointer is inside the physical frame if the device is in
1827 // unscaled mode.
1828 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1829 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001830 // If exactly one pointer went down, check for virtual key hit.
1831 // Otherwise we will drop the entire stroke.
1832 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1833 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1834 if (virtualKey) {
1835 mCurrentVirtualKey.down = true;
1836 mCurrentVirtualKey.downTime = when;
1837 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1838 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1839 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001840 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1841 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001842
1843 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001844 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1845 "VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1846 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001847 out.push_back(dispatchVirtualKey(when, readTime, policyFlags,
1848 AKEY_EVENT_ACTION_DOWN,
1849 AKEY_EVENT_FLAG_FROM_SYSTEM |
1850 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001851 }
1852 }
1853 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001854 outConsumed = true;
1855 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001856 }
1857 }
1858
1859 // Disable all virtual key touches that happen within a short time interval of the
1860 // most recent touch within the screen area. The idea is to filter out stray
1861 // virtual key presses when interacting with the touch screen.
1862 //
1863 // Problems we're trying to solve:
1864 //
1865 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1866 // virtual key area that is implemented by a separate touch panel and accidentally
1867 // triggers a virtual key.
1868 //
1869 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1870 // area and accidentally triggers a virtual key. This often happens when virtual keys
1871 // are layed out below the screen near to where the on screen keyboard's space bar
1872 // is displayed.
1873 if (mConfig.virtualKeyQuietTime > 0 &&
1874 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001875 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001876 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001877 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001878}
1879
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001880NotifyKeyArgs TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
1881 uint32_t policyFlags, int32_t keyEventAction,
1882 int32_t keyEventFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001883 int32_t keyCode = mCurrentVirtualKey.keyCode;
1884 int32_t scanCode = mCurrentVirtualKey.scanCode;
1885 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001886 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001887 policyFlags |= POLICY_FLAG_VIRTUAL;
1888
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001889 return NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
1890 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1891 keyEventFlags, keyCode, scanCode, metaState, downTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001892}
1893
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001894std::list<NotifyArgs> TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime,
1895 uint32_t policyFlags) {
1896 std::list<NotifyArgs> out;
lilinnan687e58f2022-07-19 16:00:50 +08001897 if (mCurrentMotionAborted) {
1898 // Current motion event was already aborted.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001899 return out;
lilinnan687e58f2022-07-19 16:00:50 +08001900 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001901 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1902 if (!currentIdBits.isEmpty()) {
1903 int32_t metaState = getContext()->getGlobalMetaState();
1904 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001905 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001906 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
1907 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001908 mCurrentCookedState.cookedPointerData.pointerProperties,
1909 mCurrentCookedState.cookedPointerData.pointerCoords,
1910 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits,
1911 -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1912 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001913 mCurrentMotionAborted = true;
1914 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001915 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001916}
1917
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001918// Updates pointer coords and properties for pointers with specified ids that have moved.
1919// Returns true if any of them changed.
1920static bool updateMovedPointers(const PropertiesArray& inProperties, CoordsArray& inCoords,
1921 const IdToIndexArray& inIdToIndex, PropertiesArray& outProperties,
1922 CoordsArray& outCoords, IdToIndexArray& outIdToIndex,
1923 BitSet32 idBits) {
1924 bool changed = false;
1925 while (!idBits.isEmpty()) {
1926 uint32_t id = idBits.clearFirstMarkedBit();
1927 uint32_t inIndex = inIdToIndex[id];
1928 uint32_t outIndex = outIdToIndex[id];
1929
1930 const PointerProperties& curInProperties = inProperties[inIndex];
1931 const PointerCoords& curInCoords = inCoords[inIndex];
1932 PointerProperties& curOutProperties = outProperties[outIndex];
1933 PointerCoords& curOutCoords = outCoords[outIndex];
1934
1935 if (curInProperties != curOutProperties) {
1936 curOutProperties.copyFrom(curInProperties);
1937 changed = true;
1938 }
1939
1940 if (curInCoords != curOutCoords) {
1941 curOutCoords.copyFrom(curInCoords);
1942 changed = true;
1943 }
1944 }
1945 return changed;
1946}
1947
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001948std::list<NotifyArgs> TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime,
1949 uint32_t policyFlags) {
1950 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001951 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1952 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1953 int32_t metaState = getContext()->getGlobalMetaState();
1954 int32_t buttonState = mCurrentCookedState.buttonState;
1955
1956 if (currentIdBits == lastIdBits) {
1957 if (!currentIdBits.isEmpty()) {
1958 // No pointer id changes so this is a move event.
1959 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001960 out.push_back(
1961 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE,
1962 0, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1963 mCurrentCookedState.cookedPointerData.pointerProperties,
1964 mCurrentCookedState.cookedPointerData.pointerCoords,
1965 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits,
1966 -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1967 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001968 }
1969 } else {
1970 // There may be pointers going up and pointers going down and pointers moving
1971 // all at the same time.
1972 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1973 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1974 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1975 BitSet32 dispatchedIdBits(lastIdBits.value);
1976
1977 // Update last coordinates of pointers that have moved so that we observe the new
1978 // pointer positions at the same time as other pointers that have just gone up.
1979 bool moveNeeded =
1980 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1981 mCurrentCookedState.cookedPointerData.pointerCoords,
1982 mCurrentCookedState.cookedPointerData.idToIndex,
1983 mLastCookedState.cookedPointerData.pointerProperties,
1984 mLastCookedState.cookedPointerData.pointerCoords,
1985 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1986 if (buttonState != mLastCookedState.buttonState) {
1987 moveNeeded = true;
1988 }
1989
1990 // Dispatch pointer up events.
1991 while (!upIdBits.isEmpty()) {
1992 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08001993 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08001994 if (isCanceled) {
1995 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
1996 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001997 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
1998 AMOTION_EVENT_ACTION_POINTER_UP, 0,
1999 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState,
2000 buttonState, 0,
2001 mLastCookedState.cookedPointerData.pointerProperties,
2002 mLastCookedState.cookedPointerData.pointerCoords,
2003 mLastCookedState.cookedPointerData.idToIndex,
2004 dispatchedIdBits, upId, mOrientedXPrecision,
2005 mOrientedYPrecision, mDownTime,
2006 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002007 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002008 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002009 }
2010
2011 // Dispatch move events if any of the remaining pointers moved from their old locations.
2012 // Although applications receive new locations as part of individual pointer up
2013 // events, they do not generally handle them except when presented in a move event.
2014 if (moveNeeded && !moveIdBits.isEmpty()) {
2015 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002016 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2017 AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, 0,
2018 mCurrentCookedState.cookedPointerData.pointerProperties,
2019 mCurrentCookedState.cookedPointerData.pointerCoords,
2020 mCurrentCookedState.cookedPointerData.idToIndex,
2021 dispatchedIdBits, -1, mOrientedXPrecision,
2022 mOrientedYPrecision, mDownTime,
2023 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002024 }
2025
2026 // Dispatch pointer down events using the new pointer locations.
2027 while (!downIdBits.isEmpty()) {
2028 uint32_t downId = downIdBits.clearFirstMarkedBit();
2029 dispatchedIdBits.markBit(downId);
2030
2031 if (dispatchedIdBits.count() == 1) {
2032 // First pointer is going down. Set down time.
2033 mDownTime = when;
2034 }
2035
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002036 out.push_back(
2037 dispatchMotion(when, readTime, policyFlags, mSource,
2038 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, metaState, buttonState,
2039 0, mCurrentCookedState.cookedPointerData.pointerProperties,
2040 mCurrentCookedState.cookedPointerData.pointerCoords,
2041 mCurrentCookedState.cookedPointerData.idToIndex,
2042 dispatchedIdBits, downId, mOrientedXPrecision,
2043 mOrientedYPrecision, mDownTime, MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002044 }
2045 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002046 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002047}
2048
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002049std::list<NotifyArgs> TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime,
2050 uint32_t policyFlags) {
2051 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002052 if (mSentHoverEnter &&
2053 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2054 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2055 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002056 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2057 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState,
2058 mLastCookedState.buttonState, 0,
2059 mLastCookedState.cookedPointerData.pointerProperties,
2060 mLastCookedState.cookedPointerData.pointerCoords,
2061 mLastCookedState.cookedPointerData.idToIndex,
2062 mLastCookedState.cookedPointerData.hoveringIdBits, -1,
2063 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2064 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002065 mSentHoverEnter = false;
2066 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002067 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002068}
2069
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002070std::list<NotifyArgs> TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2071 uint32_t policyFlags) {
2072 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002073 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2074 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2075 int32_t metaState = getContext()->getGlobalMetaState();
2076 if (!mSentHoverEnter) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002077 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2078 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
2079 mCurrentRawState.buttonState, 0,
2080 mCurrentCookedState.cookedPointerData.pointerProperties,
2081 mCurrentCookedState.cookedPointerData.pointerCoords,
2082 mCurrentCookedState.cookedPointerData.idToIndex,
2083 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2084 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2085 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002086 mSentHoverEnter = true;
2087 }
2088
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002089 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2090 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState,
2091 mCurrentRawState.buttonState, 0,
2092 mCurrentCookedState.cookedPointerData.pointerProperties,
2093 mCurrentCookedState.cookedPointerData.pointerCoords,
2094 mCurrentCookedState.cookedPointerData.idToIndex,
2095 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2096 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2097 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002098 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002099 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002100}
2101
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002102std::list<NotifyArgs> TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
2103 uint32_t policyFlags) {
2104 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002105 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2106 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2107 const int32_t metaState = getContext()->getGlobalMetaState();
2108 int32_t buttonState = mLastCookedState.buttonState;
2109 while (!releasedButtons.isEmpty()) {
2110 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2111 buttonState &= ~actionButton;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002112 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2113 AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0,
2114 metaState, buttonState, 0,
Prabir Pradhan211ba622022-10-31 21:09:21 +00002115 mLastCookedState.cookedPointerData.pointerProperties,
2116 mLastCookedState.cookedPointerData.pointerCoords,
2117 mLastCookedState.cookedPointerData.idToIndex, idBits, -1,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002118 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2119 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002120 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002121 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002122}
2123
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002124std::list<NotifyArgs> TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime,
2125 uint32_t policyFlags) {
2126 std::list<NotifyArgs> out;
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 Vishniakou2935db72022-09-22 13:35:22 -07002134 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2135 AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0, metaState,
2136 buttonState, 0,
2137 mCurrentCookedState.cookedPointerData.pointerProperties,
2138 mCurrentCookedState.cookedPointerData.pointerCoords,
2139 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2140 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2141 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002142 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002143 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002144}
2145
2146const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2147 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2148 return cookedPointerData.touchingIdBits;
2149 }
2150 return cookedPointerData.hoveringIdBits;
2151}
2152
2153void TouchInputMapper::cookPointerData() {
2154 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2155
2156 mCurrentCookedState.cookedPointerData.clear();
2157 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2158 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2159 mCurrentRawState.rawPointerData.hoveringIdBits;
2160 mCurrentCookedState.cookedPointerData.touchingIdBits =
2161 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002162 mCurrentCookedState.cookedPointerData.canceledIdBits =
2163 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002164
2165 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2166 mCurrentCookedState.buttonState = 0;
2167 } else {
2168 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2169 }
2170
2171 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002172 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002173 for (uint32_t i = 0; i < currentPointerCount; i++) {
2174 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2175
2176 // Size
2177 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2178 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002179 case Calibration::SizeCalibration::GEOMETRIC:
2180 case Calibration::SizeCalibration::DIAMETER:
2181 case Calibration::SizeCalibration::BOX:
2182 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002183 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2184 touchMajor = in.touchMajor;
2185 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2186 toolMajor = in.toolMajor;
2187 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2188 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2189 : in.touchMajor;
2190 } else if (mRawPointerAxes.touchMajor.valid) {
2191 toolMajor = touchMajor = in.touchMajor;
2192 toolMinor = touchMinor =
2193 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2194 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2195 : in.touchMajor;
2196 } else if (mRawPointerAxes.toolMajor.valid) {
2197 touchMajor = toolMajor = in.toolMajor;
2198 touchMinor = toolMinor =
2199 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2200 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2201 : in.toolMajor;
2202 } else {
2203 ALOG_ASSERT(false,
2204 "No touch or tool axes. "
2205 "Size calibration should have been resolved to NONE.");
2206 touchMajor = 0;
2207 touchMinor = 0;
2208 toolMajor = 0;
2209 toolMinor = 0;
2210 size = 0;
2211 }
2212
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002213 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002214 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2215 if (touchingCount > 1) {
2216 touchMajor /= touchingCount;
2217 touchMinor /= touchingCount;
2218 toolMajor /= touchingCount;
2219 toolMinor /= touchingCount;
2220 size /= touchingCount;
2221 }
2222 }
2223
Michael Wright227c5542020-07-02 18:30:52 +01002224 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002225 touchMajor *= mGeometricScale;
2226 touchMinor *= mGeometricScale;
2227 toolMajor *= mGeometricScale;
2228 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002229 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002230 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2231 touchMinor = touchMajor;
2232 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2233 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002234 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002235 touchMinor = touchMajor;
2236 toolMinor = toolMajor;
2237 }
2238
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002239 mCalibration.applySizeScaleAndBias(touchMajor);
2240 mCalibration.applySizeScaleAndBias(touchMinor);
2241 mCalibration.applySizeScaleAndBias(toolMajor);
2242 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002243 size *= mSizeScale;
2244 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002245 case Calibration::SizeCalibration::DEFAULT:
2246 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2247 break;
2248 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002249 touchMajor = 0;
2250 touchMinor = 0;
2251 toolMajor = 0;
2252 toolMinor = 0;
2253 size = 0;
2254 break;
2255 }
2256
2257 // Pressure
2258 float pressure;
2259 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002260 case Calibration::PressureCalibration::PHYSICAL:
2261 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002262 pressure = in.pressure * mPressureScale;
2263 break;
2264 default:
2265 pressure = in.isHovering ? 0 : 1;
2266 break;
2267 }
2268
2269 // Tilt and Orientation
2270 float tilt;
2271 float orientation;
2272 if (mHaveTilt) {
2273 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2274 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
Prabir Pradhane2e10b42022-11-17 20:59:36 +00002275 orientation = transformAngle(mRawRotation, atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002276 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2277 } else {
2278 tilt = 0;
2279
2280 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002281 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhane2e10b42022-11-17 20:59:36 +00002282 orientation = transformAngle(mRawRotation, in.orientation * mOrientationScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002283 break;
Michael Wright227c5542020-07-02 18:30:52 +01002284 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002285 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2286 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2287 if (c1 != 0 || c2 != 0) {
Prabir Pradhane2e10b42022-11-17 20:59:36 +00002288 orientation = transformAngle(mRawRotation, atan2f(c1, c2) * 0.5f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002289 float confidence = hypotf(c1, c2);
2290 float scale = 1.0f + confidence / 16.0f;
2291 touchMajor *= scale;
2292 touchMinor /= scale;
2293 toolMajor *= scale;
2294 toolMinor /= scale;
2295 } else {
2296 orientation = 0;
2297 }
2298 break;
2299 }
2300 default:
2301 orientation = 0;
2302 }
2303 }
2304
2305 // Distance
2306 float distance;
2307 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002308 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002309 distance = in.distance * mDistanceScale;
2310 break;
2311 default:
2312 distance = 0;
2313 }
2314
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002315 // Adjust X,Y coords for device calibration and convert to the natural display coordinates.
2316 vec2 transformed = {in.x, in.y};
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002317 mAffineTransform.applyTo(transformed.x /*byRef*/, transformed.y /*byRef*/);
2318 transformed = mRawToDisplay.transform(transformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002319
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002320 // Write output coords.
2321 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2322 out.clear();
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002323 out.setAxisValue(AMOTION_EVENT_AXIS_X, transformed.x);
2324 out.setAxisValue(AMOTION_EVENT_AXIS_Y, transformed.y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002325 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2326 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2327 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2328 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2329 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2330 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2331 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Prabir Pradhan64fd5202022-11-30 19:45:11 +00002332 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2333 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002334
Chris Ye364fdb52020-08-05 15:07:56 -07002335 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002336 uint32_t id = in.id;
2337 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2338 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2339 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002340 float dx = transformed.x - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2341 float dy = transformed.y - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002342 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2343 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2344 }
2345
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002346 // Write output properties.
2347 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002348 properties.clear();
2349 properties.id = id;
2350 properties.toolType = in.toolType;
2351
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002352 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002353 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002354 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002355 }
2356}
2357
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002358std::list<NotifyArgs> TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
2359 uint32_t policyFlags,
2360 PointerUsage pointerUsage) {
2361 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002362 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002363 out += abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002364 mPointerUsage = pointerUsage;
2365 }
2366
2367 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002368 case PointerUsage::GESTURES:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002369 out += dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002370 break;
Michael Wright227c5542020-07-02 18:30:52 +01002371 case PointerUsage::STYLUS:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002372 out += dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002373 break;
Michael Wright227c5542020-07-02 18:30:52 +01002374 case PointerUsage::MOUSE:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002375 out += dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002376 break;
Michael Wright227c5542020-07-02 18:30:52 +01002377 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002378 break;
2379 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002380 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002381}
2382
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002383std::list<NotifyArgs> TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime,
2384 uint32_t policyFlags) {
2385 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002386 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002387 case PointerUsage::GESTURES:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002388 out += abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002389 break;
Michael Wright227c5542020-07-02 18:30:52 +01002390 case PointerUsage::STYLUS:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002391 out += abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002392 break;
Michael Wright227c5542020-07-02 18:30:52 +01002393 case PointerUsage::MOUSE:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002394 out += abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002395 break;
Michael Wright227c5542020-07-02 18:30:52 +01002396 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002397 break;
2398 }
2399
Michael Wright227c5542020-07-02 18:30:52 +01002400 mPointerUsage = PointerUsage::NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002401 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002402}
2403
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002404std::list<NotifyArgs> TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
2405 uint32_t policyFlags,
2406 bool isTimeout) {
2407 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002408 // Update current gesture coordinates.
2409 bool cancelPreviousGesture, finishPreviousGesture;
2410 bool sendEvents =
2411 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2412 if (!sendEvents) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002413 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002414 }
2415 if (finishPreviousGesture) {
2416 cancelPreviousGesture = false;
2417 }
2418
2419 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002420 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002421 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002422 if (finishPreviousGesture || cancelPreviousGesture) {
2423 mPointerController->clearSpots();
2424 }
2425
Michael Wright227c5542020-07-02 18:30:52 +01002426 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00002427 mPointerController->setSpots(mPointerGesture.currentGestureCoords.cbegin(),
2428 mPointerGesture.currentGestureIdToIndex.cbegin(),
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002429 mPointerGesture.currentGestureIdBits,
2430 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002431 }
2432 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002433 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002434 }
2435
2436 // Show or hide the pointer if needed.
2437 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002438 case PointerGesture::Mode::NEUTRAL:
2439 case PointerGesture::Mode::QUIET:
2440 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2441 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002442 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002443 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002444 }
2445 break;
Michael Wright227c5542020-07-02 18:30:52 +01002446 case PointerGesture::Mode::TAP:
2447 case PointerGesture::Mode::TAP_DRAG:
2448 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2449 case PointerGesture::Mode::HOVER:
2450 case PointerGesture::Mode::PRESS:
2451 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002452 // Unfade the pointer when the current gesture manipulates the
2453 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002454 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002455 break;
Michael Wright227c5542020-07-02 18:30:52 +01002456 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002457 // Fade the pointer when the current gesture manipulates a different
2458 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002459 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002460 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002461 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002462 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002463 }
2464 break;
2465 }
2466
2467 // Send events!
2468 int32_t metaState = getContext()->getGlobalMetaState();
2469 int32_t buttonState = mCurrentCookedState.buttonState;
Harry Cutts2800fb02022-09-15 13:49:23 +00002470 const MotionClassification classification =
2471 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE
2472 ? MotionClassification::TWO_FINGER_SWIPE
2473 : MotionClassification::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002474
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002475 uint32_t flags = 0;
2476
2477 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2478 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2479 }
2480
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002481 // Update last coordinates of pointers that have moved so that we observe the new
2482 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002483 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2484 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2485 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2486 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2487 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2488 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002489 bool moveNeeded = false;
2490 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2491 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2492 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2493 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2494 mPointerGesture.lastGestureIdBits.value);
2495 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2496 mPointerGesture.currentGestureCoords,
2497 mPointerGesture.currentGestureIdToIndex,
2498 mPointerGesture.lastGestureProperties,
2499 mPointerGesture.lastGestureCoords,
2500 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2501 if (buttonState != mLastCookedState.buttonState) {
2502 moveNeeded = true;
2503 }
2504 }
2505
2506 // Send motion events for all pointers that went up or were canceled.
2507 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2508 if (!dispatchedGestureIdBits.isEmpty()) {
2509 if (cancelPreviousGesture) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002510 const uint32_t cancelFlags = flags | AMOTION_EVENT_FLAG_CANCELED;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002511 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002512 AMOTION_EVENT_ACTION_CANCEL, 0, cancelFlags, metaState,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002513 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2514 mPointerGesture.lastGestureProperties,
2515 mPointerGesture.lastGestureCoords,
2516 mPointerGesture.lastGestureIdToIndex,
2517 dispatchedGestureIdBits, -1, 0, 0,
2518 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002519
2520 dispatchedGestureIdBits.clear();
2521 } else {
2522 BitSet32 upGestureIdBits;
2523 if (finishPreviousGesture) {
2524 upGestureIdBits = dispatchedGestureIdBits;
2525 } else {
2526 upGestureIdBits.value =
2527 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2528 }
2529 while (!upGestureIdBits.isEmpty()) {
2530 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2531
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002532 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2533 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState,
2534 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2535 mPointerGesture.lastGestureProperties,
2536 mPointerGesture.lastGestureCoords,
2537 mPointerGesture.lastGestureIdToIndex,
2538 dispatchedGestureIdBits, id, 0, 0,
2539 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002540
2541 dispatchedGestureIdBits.clearBit(id);
2542 }
2543 }
2544 }
2545
2546 // Send motion events for all pointers that moved.
2547 if (moveNeeded) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002548 out.push_back(
2549 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0,
2550 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2551 mPointerGesture.currentGestureProperties,
2552 mPointerGesture.currentGestureCoords,
2553 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1,
2554 0, 0, mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002555 }
2556
2557 // Send motion events for all pointers that went down.
2558 if (down) {
2559 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2560 ~dispatchedGestureIdBits.value);
2561 while (!downGestureIdBits.isEmpty()) {
2562 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2563 dispatchedGestureIdBits.markBit(id);
2564
2565 if (dispatchedGestureIdBits.count() == 1) {
2566 mPointerGesture.downTime = when;
2567 }
2568
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002569 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2570 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, flags, metaState,
2571 buttonState, 0, mPointerGesture.currentGestureProperties,
2572 mPointerGesture.currentGestureCoords,
2573 mPointerGesture.currentGestureIdToIndex,
2574 dispatchedGestureIdBits, id, 0, 0,
2575 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002576 }
2577 }
2578
2579 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002580 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002581 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2582 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState,
2583 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2584 mPointerGesture.currentGestureProperties,
2585 mPointerGesture.currentGestureCoords,
2586 mPointerGesture.currentGestureIdToIndex,
2587 mPointerGesture.currentGestureIdBits, -1, 0, 0,
2588 mPointerGesture.downTime, MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002589 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2590 // Synthesize a hover move event after all pointers go up to indicate that
2591 // the pointer is hovering again even if the user is not currently touching
2592 // the touch pad. This ensures that a view will receive a fresh hover enter
2593 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002594 float x, y;
2595 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002596
2597 PointerProperties pointerProperties;
2598 pointerProperties.clear();
2599 pointerProperties.id = 0;
2600 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2601
2602 PointerCoords pointerCoords;
2603 pointerCoords.clear();
2604 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2605 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2606
2607 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002608 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
2609 mSource, displayId, policyFlags,
2610 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState,
2611 buttonState, MotionClassification::NONE,
2612 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
2613 &pointerCoords, 0, 0, x, y, mPointerGesture.downTime,
2614 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002615 }
2616
2617 // Update state.
2618 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2619 if (!down) {
2620 mPointerGesture.lastGestureIdBits.clear();
2621 } else {
2622 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2623 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2624 uint32_t id = idBits.clearFirstMarkedBit();
2625 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2626 mPointerGesture.lastGestureProperties[index].copyFrom(
2627 mPointerGesture.currentGestureProperties[index]);
2628 mPointerGesture.lastGestureCoords[index].copyFrom(
2629 mPointerGesture.currentGestureCoords[index]);
2630 mPointerGesture.lastGestureIdToIndex[id] = index;
2631 }
2632 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002633 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002634}
2635
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002636std::list<NotifyArgs> TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime,
2637 uint32_t policyFlags) {
Harry Cutts2800fb02022-09-15 13:49:23 +00002638 const MotionClassification classification =
2639 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE
2640 ? MotionClassification::TWO_FINGER_SWIPE
2641 : MotionClassification::NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002642 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002643 // Cancel previously dispatches pointers.
2644 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2645 int32_t metaState = getContext()->getGlobalMetaState();
2646 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002647 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002648 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
2649 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002650 mPointerGesture.lastGestureProperties,
2651 mPointerGesture.lastGestureCoords,
2652 mPointerGesture.lastGestureIdToIndex,
2653 mPointerGesture.lastGestureIdBits, -1, 0, 0,
2654 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002655 }
2656
2657 // Reset the current pointer gesture.
2658 mPointerGesture.reset();
2659 mPointerVelocityControl.reset();
2660
2661 // Remove any current spots.
2662 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002663 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002664 mPointerController->clearSpots();
2665 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002666 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002667}
2668
2669bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2670 bool* outFinishPreviousGesture, bool isTimeout) {
2671 *outCancelPreviousGesture = false;
2672 *outFinishPreviousGesture = false;
2673
2674 // Handle TAP timeout.
2675 if (isTimeout) {
Harry Cutts45483602022-08-24 14:36:48 +00002676 ALOGD_IF(DEBUG_GESTURES, "Gestures: Processing timeout");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002677
Michael Wright227c5542020-07-02 18:30:52 +01002678 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002679 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2680 // The tap/drag timeout has not yet expired.
2681 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2682 mConfig.pointerGestureTapDragInterval);
2683 } else {
2684 // The tap is finished.
Harry Cutts45483602022-08-24 14:36:48 +00002685 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP finished");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002686 *outFinishPreviousGesture = true;
2687
2688 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002689 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002690 mPointerGesture.currentGestureIdBits.clear();
2691
2692 mPointerVelocityControl.reset();
2693 return true;
2694 }
2695 }
2696
2697 // We did not handle this timeout.
2698 return false;
2699 }
2700
2701 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2702 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2703
2704 // Update the velocity tracker.
2705 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002706 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002707 uint32_t id = idBits.clearFirstMarkedBit();
2708 const RawPointerData::Pointer& pointer =
2709 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakou8d232032023-01-11 08:17:21 -08002710 const float x = pointer.x * mPointerXMovementScale;
2711 const float y = pointer.y * mPointerYMovementScale;
2712 mPointerGesture.velocityTracker.addMovement(when, id, AMOTION_EVENT_AXIS_X, x);
2713 mPointerGesture.velocityTracker.addMovement(when, id, AMOTION_EVENT_AXIS_Y, y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002714 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002715 }
2716
2717 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2718 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002719 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2720 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2721 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002722 mPointerGesture.resetTap();
2723 }
2724
2725 // Pick a new active touch id if needed.
2726 // Choose an arbitrary pointer that just went down, if there is one.
2727 // Otherwise choose an arbitrary remaining pointer.
2728 // This guarantees we always have an active touch id when there is at least one pointer.
2729 // We keep the same active touch id for as long as possible.
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002730 if (mPointerGesture.activeTouchId < 0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002731 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002732 mPointerGesture.activeTouchId = mCurrentCookedState.fingerIdBits.firstMarkedBit();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002733 mPointerGesture.firstTouchTime = when;
2734 }
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002735 } else if (!mCurrentCookedState.fingerIdBits.hasBit(mPointerGesture.activeTouchId)) {
2736 mPointerGesture.activeTouchId = !mCurrentCookedState.fingerIdBits.isEmpty()
2737 ? mCurrentCookedState.fingerIdBits.firstMarkedBit()
2738 : -1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002739 }
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002740 const int32_t& activeTouchId = mPointerGesture.activeTouchId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002741
2742 // Switch states based on button and pointer state.
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002743 if (checkForTouchpadQuietTime(when)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002744 // Case 1: Quiet time. (QUIET)
Harry Cutts45483602022-08-24 14:36:48 +00002745 ALOGD_IF(DEBUG_GESTURES, "Gestures: QUIET for next %0.3fms",
2746 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2747 0.000001f);
Michael Wright227c5542020-07-02 18:30:52 +01002748 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002749 *outFinishPreviousGesture = true;
2750 }
2751
2752 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002753 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002754 mPointerGesture.currentGestureIdBits.clear();
2755
2756 mPointerVelocityControl.reset();
2757 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2758 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2759 // The pointer follows the active touch point.
2760 // Emit DOWN, MOVE, UP events at the pointer location.
2761 //
2762 // Only the active touch matters; other fingers are ignored. This policy helps
2763 // to handle the case where the user places a second finger on the touch pad
2764 // to apply the necessary force to depress an integrated button below the surface.
2765 // We don't want the second finger to be delivered to applications.
2766 //
2767 // For this to work well, we need to make sure to track the pointer that is really
2768 // active. If the user first puts one finger down to click then adds another
2769 // finger to drag then the active pointer should switch to the finger that is
2770 // being dragged.
Harry Cutts45483602022-08-24 14:36:48 +00002771 ALOGD_IF(DEBUG_GESTURES,
2772 "Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, currentFingerCount=%d",
2773 activeTouchId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002774 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002775 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002776 *outFinishPreviousGesture = true;
2777 mPointerGesture.activeGestureId = 0;
2778 }
2779
2780 // Switch pointers if needed.
2781 // Find the fastest pointer and follow it.
2782 if (activeTouchId >= 0 && currentFingerCount > 1) {
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002783 const auto [bestId, bestSpeed] = getFastestFinger();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002784 if (bestId >= 0 && bestId != activeTouchId) {
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002785 mPointerGesture.activeTouchId = bestId;
Harry Cutts45483602022-08-24 14:36:48 +00002786 ALOGD_IF(DEBUG_GESTURES,
2787 "Gestures: BUTTON_CLICK_OR_DRAG switched pointers, bestId=%d, "
2788 "bestSpeed=%0.3f",
2789 bestId, bestSpeed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002790 }
2791 }
2792
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002793 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002794 // When using spots, the click will occur at the position of the anchor
2795 // spot and all other spots will move there.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002796 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002797 } else {
2798 mPointerVelocityControl.reset();
2799 }
2800
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002801 float x, y;
2802 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002803
Michael Wright227c5542020-07-02 18:30:52 +01002804 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002805 mPointerGesture.currentGestureIdBits.clear();
2806 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2807 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2808 mPointerGesture.currentGestureProperties[0].clear();
2809 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2810 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2811 mPointerGesture.currentGestureCoords[0].clear();
2812 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2813 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2814 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2815 } else if (currentFingerCount == 0) {
2816 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002817 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002818 *outFinishPreviousGesture = true;
2819 }
2820
2821 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2822 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2823 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002824 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2825 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002826 lastFingerCount == 1) {
2827 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002828 float x, y;
2829 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002830 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2831 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Harry Cutts45483602022-08-24 14:36:48 +00002832 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002833
2834 mPointerGesture.tapUpTime = when;
2835 getContext()->requestTimeoutAtTime(when +
2836 mConfig.pointerGestureTapDragInterval);
2837
2838 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002839 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002840 mPointerGesture.currentGestureIdBits.clear();
2841 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2842 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2843 mPointerGesture.currentGestureProperties[0].clear();
2844 mPointerGesture.currentGestureProperties[0].id =
2845 mPointerGesture.activeGestureId;
2846 mPointerGesture.currentGestureProperties[0].toolType =
2847 AMOTION_EVENT_TOOL_TYPE_FINGER;
2848 mPointerGesture.currentGestureCoords[0].clear();
2849 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2850 mPointerGesture.tapX);
2851 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2852 mPointerGesture.tapY);
2853 mPointerGesture.currentGestureCoords[0]
2854 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2855
2856 tapped = true;
2857 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002858 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP, deltaX=%f, deltaY=%f",
2859 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002860 }
2861 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002862 if (DEBUG_GESTURES) {
2863 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2864 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2865 (when - mPointerGesture.tapDownTime) * 0.000001f);
2866 } else {
2867 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2868 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002869 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002870 }
2871 }
2872
2873 mPointerVelocityControl.reset();
2874
2875 if (!tapped) {
Harry Cutts45483602022-08-24 14:36:48 +00002876 ALOGD_IF(DEBUG_GESTURES, "Gestures: NEUTRAL");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002877 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002878 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002879 mPointerGesture.currentGestureIdBits.clear();
2880 }
2881 } else if (currentFingerCount == 1) {
2882 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2883 // The pointer follows the active touch point.
2884 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2885 // When in TAP_DRAG, emit MOVE events at the pointer location.
2886 ALOG_ASSERT(activeTouchId >= 0);
2887
Michael Wright227c5542020-07-02 18:30:52 +01002888 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2889 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002890 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002891 float x, y;
2892 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002893 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2894 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002895 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002896 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002897 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2898 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002899 }
2900 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002901 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, %0.3fms time since up",
2902 (when - mPointerGesture.tapUpTime) * 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002903 }
Michael Wright227c5542020-07-02 18:30:52 +01002904 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2905 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002906 }
2907
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002908 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002909 // When using spots, the hover or drag will occur at the position of the anchor spot.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002910 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002911 } else {
2912 mPointerVelocityControl.reset();
2913 }
2914
2915 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01002916 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Harry Cutts45483602022-08-24 14:36:48 +00002917 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP_DRAG");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002918 down = true;
2919 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002920 ALOGD_IF(DEBUG_GESTURES, "Gestures: HOVER");
Michael Wright227c5542020-07-02 18:30:52 +01002921 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002922 *outFinishPreviousGesture = true;
2923 }
2924 mPointerGesture.activeGestureId = 0;
2925 down = false;
2926 }
2927
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002928 float x, y;
2929 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002930
2931 mPointerGesture.currentGestureIdBits.clear();
2932 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2933 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2934 mPointerGesture.currentGestureProperties[0].clear();
2935 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2936 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2937 mPointerGesture.currentGestureCoords[0].clear();
2938 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2939 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2940 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
2941 down ? 1.0f : 0.0f);
2942
2943 if (lastFingerCount == 0 && currentFingerCount != 0) {
2944 mPointerGesture.resetTap();
2945 mPointerGesture.tapDownTime = when;
2946 mPointerGesture.tapX = x;
2947 mPointerGesture.tapY = y;
2948 }
2949 } else {
2950 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002951 prepareMultiFingerPointerGestures(when, outCancelPreviousGesture, outFinishPreviousGesture);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002952 }
2953
2954 mPointerController->setButtonState(mCurrentRawState.buttonState);
2955
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002956 if (DEBUG_GESTURES) {
2957 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
2958 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
2959 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
2960 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
2961 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
2962 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
2963 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
2964 uint32_t id = idBits.clearFirstMarkedBit();
2965 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2966 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
2967 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
2968 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
2969 "x=%0.3f, y=%0.3f, pressure=%0.3f",
2970 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
2971 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
2972 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
2973 }
2974 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
2975 uint32_t id = idBits.clearFirstMarkedBit();
2976 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
2977 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
2978 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
2979 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
2980 "x=%0.3f, y=%0.3f, pressure=%0.3f",
2981 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
2982 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
2983 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
2984 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002985 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002986 return true;
2987}
2988
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002989bool TouchInputMapper::checkForTouchpadQuietTime(nsecs_t when) {
2990 if (mPointerGesture.activeTouchId < 0) {
2991 mPointerGesture.resetQuietTime();
2992 return false;
2993 }
2994
2995 if (when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval) {
2996 return true;
2997 }
2998
2999 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
3000 bool isQuietTime = false;
3001 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
3002 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
3003 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
3004 currentFingerCount < 2) {
3005 // Enter quiet time when exiting swipe or freeform state.
3006 // This is to prevent accidentally entering the hover state and flinging the
3007 // pointer when finishing a swipe and there is still one pointer left onscreen.
3008 isQuietTime = true;
3009 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
3010 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
3011 // Enter quiet time when releasing the button and there are still two or more
3012 // fingers down. This may indicate that one finger was used to press the button
3013 // but it has not gone up yet.
3014 isQuietTime = true;
3015 }
3016 if (isQuietTime) {
3017 mPointerGesture.quietTime = when;
3018 }
3019 return isQuietTime;
3020}
3021
3022std::pair<int32_t, float> TouchInputMapper::getFastestFinger() {
3023 int32_t bestId = -1;
3024 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
3025 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
3026 uint32_t id = idBits.clearFirstMarkedBit();
3027 std::optional<float> vx =
3028 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, id);
3029 std::optional<float> vy =
3030 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, id);
3031 if (vx && vy) {
3032 float speed = hypotf(*vx, *vy);
3033 if (speed > bestSpeed) {
3034 bestId = id;
3035 bestSpeed = speed;
3036 }
3037 }
3038 }
3039 return std::make_pair(bestId, bestSpeed);
3040}
3041
3042void TouchInputMapper::prepareMultiFingerPointerGestures(nsecs_t when, bool* cancelPreviousGesture,
3043 bool* finishPreviousGesture) {
3044 // We need to provide feedback for each finger that goes down so we cannot wait for the fingers
3045 // to move before deciding what to do.
3046 //
3047 // The ambiguous case is deciding what to do when there are two fingers down but they have not
3048 // moved enough to determine whether they are part of a drag or part of a freeform gesture, or
3049 // just a press or long-press at the pointer location.
3050 //
3051 // When there are two fingers we start with the PRESS hypothesis and we generate a down at the
3052 // pointer location.
3053 //
3054 // When the two fingers move enough or when additional fingers are added, we make a decision to
3055 // transition into SWIPE or FREEFORM mode accordingly.
3056 const int32_t activeTouchId = mPointerGesture.activeTouchId;
3057 ALOG_ASSERT(activeTouchId >= 0);
3058
3059 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
3060 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
3061 bool settled =
3062 when >= mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
3063 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3064 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3065 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
3066 *finishPreviousGesture = true;
3067 } else if (!settled && currentFingerCount > lastFingerCount) {
3068 // Additional pointers have gone down but not yet settled.
3069 // Reset the gesture.
3070 ALOGD_IF(DEBUG_GESTURES,
3071 "Gestures: Resetting gesture since additional pointers went down for "
3072 "MULTITOUCH, settle time remaining %0.3fms",
3073 (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval -
3074 when) * 0.000001f);
3075 *cancelPreviousGesture = true;
3076 } else {
3077 // Continue previous gesture.
3078 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3079 }
3080
3081 if (*finishPreviousGesture || *cancelPreviousGesture) {
3082 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
3083 mPointerGesture.activeGestureId = 0;
3084 mPointerGesture.referenceIdBits.clear();
3085 mPointerVelocityControl.reset();
3086
3087 // Use the centroid and pointer location as the reference points for the gesture.
3088 ALOGD_IF(DEBUG_GESTURES,
3089 "Gestures: Using centroid as reference for MULTITOUCH, settle time remaining "
3090 "%0.3fms",
3091 (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval -
3092 when) * 0.000001f);
3093 mCurrentRawState.rawPointerData
3094 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3095 &mPointerGesture.referenceTouchY);
3096 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3097 &mPointerGesture.referenceGestureY);
3098 }
3099
3100 // Clear the reference deltas for fingers not yet included in the reference calculation.
3101 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3102 ~mPointerGesture.referenceIdBits.value);
3103 !idBits.isEmpty();) {
3104 uint32_t id = idBits.clearFirstMarkedBit();
3105 mPointerGesture.referenceDeltas[id].dx = 0;
3106 mPointerGesture.referenceDeltas[id].dy = 0;
3107 }
3108 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3109
3110 // Add delta for all fingers and calculate a common movement delta.
3111 int32_t commonDeltaRawX = 0, commonDeltaRawY = 0;
3112 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3113 mCurrentCookedState.fingerIdBits.value);
3114 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3115 bool first = (idBits == commonIdBits);
3116 uint32_t id = idBits.clearFirstMarkedBit();
3117 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3118 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3119 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3120 delta.dx += cpd.x - lpd.x;
3121 delta.dy += cpd.y - lpd.y;
3122
3123 if (first) {
3124 commonDeltaRawX = delta.dx;
3125 commonDeltaRawY = delta.dy;
3126 } else {
3127 commonDeltaRawX = calculateCommonVector(commonDeltaRawX, delta.dx);
3128 commonDeltaRawY = calculateCommonVector(commonDeltaRawY, delta.dy);
3129 }
3130 }
3131
3132 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
3133 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
3134 float dist[MAX_POINTER_ID + 1];
3135 int32_t distOverThreshold = 0;
3136 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3137 uint32_t id = idBits.clearFirstMarkedBit();
3138 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3139 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3140 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3141 distOverThreshold += 1;
3142 }
3143 }
3144
3145 // Only transition when at least two pointers have moved further than
3146 // the minimum distance threshold.
3147 if (distOverThreshold >= 2) {
3148 if (currentFingerCount > 2) {
3149 // There are more than two pointers, switch to FREEFORM.
3150 ALOGD_IF(DEBUG_GESTURES,
3151 "Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3152 currentFingerCount);
3153 *cancelPreviousGesture = true;
3154 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3155 } else {
3156 // There are exactly two pointers.
3157 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3158 uint32_t id1 = idBits.clearFirstMarkedBit();
3159 uint32_t id2 = idBits.firstMarkedBit();
3160 const RawPointerData::Pointer& p1 =
3161 mCurrentRawState.rawPointerData.pointerForId(id1);
3162 const RawPointerData::Pointer& p2 =
3163 mCurrentRawState.rawPointerData.pointerForId(id2);
3164 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3165 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3166 // There are two pointers but they are too far apart for a SWIPE,
3167 // switch to FREEFORM.
3168 ALOGD_IF(DEBUG_GESTURES,
3169 "Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
3170 mutualDistance, mPointerGestureMaxSwipeWidth);
3171 *cancelPreviousGesture = true;
3172 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3173 } else {
3174 // There are two pointers. Wait for both pointers to start moving
3175 // before deciding whether this is a SWIPE or FREEFORM gesture.
3176 float dist1 = dist[id1];
3177 float dist2 = dist[id2];
3178 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3179 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3180 // Calculate the dot product of the displacement vectors.
3181 // When the vectors are oriented in approximately the same direction,
3182 // the angle betweeen them is near zero and the cosine of the angle
3183 // approaches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3184 // mag(v2).
3185 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3186 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3187 float dx1 = delta1.dx * mPointerXZoomScale;
3188 float dy1 = delta1.dy * mPointerYZoomScale;
3189 float dx2 = delta2.dx * mPointerXZoomScale;
3190 float dy2 = delta2.dy * mPointerYZoomScale;
3191 float dot = dx1 * dx2 + dy1 * dy2;
3192 float cosine = dot / (dist1 * dist2); // denominator always > 0
3193 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3194 // Pointers are moving in the same direction. Switch to SWIPE.
3195 ALOGD_IF(DEBUG_GESTURES,
3196 "Gestures: PRESS transitioned to SWIPE, "
3197 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3198 "cosine %0.3f >= %0.3f",
3199 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3200 mConfig.pointerGestureMultitouchMinDistance, cosine,
3201 mConfig.pointerGestureSwipeTransitionAngleCosine);
3202 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
3203 } else {
3204 // Pointers are moving in different directions. Switch to FREEFORM.
3205 ALOGD_IF(DEBUG_GESTURES,
3206 "Gestures: PRESS transitioned to FREEFORM, "
3207 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3208 "cosine %0.3f < %0.3f",
3209 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3210 mConfig.pointerGestureMultitouchMinDistance, cosine,
3211 mConfig.pointerGestureSwipeTransitionAngleCosine);
3212 *cancelPreviousGesture = true;
3213 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3214 }
3215 }
3216 }
3217 }
3218 }
3219 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
3220 // Switch from SWIPE to FREEFORM if additional pointers go down.
3221 // Cancel previous gesture.
3222 if (currentFingerCount > 2) {
3223 ALOGD_IF(DEBUG_GESTURES,
3224 "Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3225 currentFingerCount);
3226 *cancelPreviousGesture = true;
3227 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3228 }
3229 }
3230
3231 // Move the reference points based on the overall group motion of the fingers
3232 // except in PRESS mode while waiting for a transition to occur.
3233 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
3234 (commonDeltaRawX || commonDeltaRawY)) {
3235 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3236 uint32_t id = idBits.clearFirstMarkedBit();
3237 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3238 delta.dx = 0;
3239 delta.dy = 0;
3240 }
3241
3242 mPointerGesture.referenceTouchX += commonDeltaRawX;
3243 mPointerGesture.referenceTouchY += commonDeltaRawY;
3244
3245 float commonDeltaX = commonDeltaRawX * mPointerXMovementScale;
3246 float commonDeltaY = commonDeltaRawY * mPointerYMovementScale;
3247
3248 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
3249 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3250
3251 mPointerGesture.referenceGestureX += commonDeltaX;
3252 mPointerGesture.referenceGestureY += commonDeltaY;
3253 }
3254
3255 // Report gestures.
3256 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3257 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
3258 // PRESS or SWIPE mode.
3259 ALOGD_IF(DEBUG_GESTURES,
3260 "Gestures: PRESS or SWIPE activeTouchId=%d, activeGestureId=%d, "
3261 "currentTouchPointerCount=%d",
3262 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3263 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3264
3265 mPointerGesture.currentGestureIdBits.clear();
3266 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3267 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3268 mPointerGesture.currentGestureProperties[0].clear();
3269 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3270 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3271 mPointerGesture.currentGestureCoords[0].clear();
3272 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3273 mPointerGesture.referenceGestureX);
3274 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3275 mPointerGesture.referenceGestureY);
3276 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3277 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
3278 float xOffset = static_cast<float>(commonDeltaRawX) /
3279 (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue);
3280 float yOffset = static_cast<float>(commonDeltaRawY) /
3281 (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue);
3282 mPointerGesture.currentGestureCoords[0]
3283 .setAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET, xOffset);
3284 mPointerGesture.currentGestureCoords[0]
3285 .setAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET, yOffset);
3286 }
3287 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
3288 // FREEFORM mode.
3289 ALOGD_IF(DEBUG_GESTURES,
3290 "Gestures: FREEFORM activeTouchId=%d, activeGestureId=%d, "
3291 "currentTouchPointerCount=%d",
3292 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3293 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3294
3295 mPointerGesture.currentGestureIdBits.clear();
3296
3297 BitSet32 mappedTouchIdBits;
3298 BitSet32 usedGestureIdBits;
3299 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
3300 // Initially, assign the active gesture id to the active touch point
3301 // if there is one. No other touch id bits are mapped yet.
3302 if (!*cancelPreviousGesture) {
3303 mappedTouchIdBits.markBit(activeTouchId);
3304 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3305 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3306 mPointerGesture.activeGestureId;
3307 } else {
3308 mPointerGesture.activeGestureId = -1;
3309 }
3310 } else {
3311 // Otherwise, assume we mapped all touches from the previous frame.
3312 // Reuse all mappings that are still applicable.
3313 mappedTouchIdBits.value =
3314 mLastCookedState.fingerIdBits.value & mCurrentCookedState.fingerIdBits.value;
3315 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3316
3317 // Check whether we need to choose a new active gesture id because the
3318 // current went went up.
3319 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3320 ~mCurrentCookedState.fingerIdBits.value);
3321 !upTouchIdBits.isEmpty();) {
3322 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3323 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3324 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3325 mPointerGesture.activeGestureId = -1;
3326 break;
3327 }
3328 }
3329 }
3330
3331 ALOGD_IF(DEBUG_GESTURES,
3332 "Gestures: FREEFORM follow up mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3333 "activeGestureId=%d",
3334 mappedTouchIdBits.value, usedGestureIdBits.value, mPointerGesture.activeGestureId);
3335
3336 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3337 for (uint32_t i = 0; i < currentFingerCount; i++) {
3338 uint32_t touchId = idBits.clearFirstMarkedBit();
3339 uint32_t gestureId;
3340 if (!mappedTouchIdBits.hasBit(touchId)) {
3341 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3342 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
3343 ALOGD_IF(DEBUG_GESTURES,
3344 "Gestures: FREEFORM new mapping for touch id %d -> gesture id %d", touchId,
3345 gestureId);
3346 } else {
3347 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
3348 ALOGD_IF(DEBUG_GESTURES,
3349 "Gestures: FREEFORM existing mapping for touch id %d -> gesture id %d",
3350 touchId, gestureId);
3351 }
3352 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3353 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3354
3355 const RawPointerData::Pointer& pointer =
3356 mCurrentRawState.rawPointerData.pointerForId(touchId);
3357 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3358 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
3359 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
3360
3361 mPointerGesture.currentGestureProperties[i].clear();
3362 mPointerGesture.currentGestureProperties[i].id = gestureId;
3363 mPointerGesture.currentGestureProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3364 mPointerGesture.currentGestureCoords[i].clear();
3365 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
3366 mPointerGesture.referenceGestureX +
3367 deltaX);
3368 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
3369 mPointerGesture.referenceGestureY +
3370 deltaY);
3371 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3372 }
3373
3374 if (mPointerGesture.activeGestureId < 0) {
3375 mPointerGesture.activeGestureId = mPointerGesture.currentGestureIdBits.firstMarkedBit();
3376 ALOGD_IF(DEBUG_GESTURES, "Gestures: FREEFORM new activeGestureId=%d",
3377 mPointerGesture.activeGestureId);
3378 }
3379 }
3380}
3381
Harry Cutts714d1ad2022-08-24 16:36:43 +00003382void TouchInputMapper::moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId) {
3383 const RawPointerData::Pointer& currentPointer =
3384 mCurrentRawState.rawPointerData.pointerForId(pointerId);
3385 const RawPointerData::Pointer& lastPointer =
3386 mLastRawState.rawPointerData.pointerForId(pointerId);
3387 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3388 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3389
3390 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
3391 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3392
3393 mPointerController->move(deltaX, deltaY);
3394}
3395
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003396std::list<NotifyArgs> TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
3397 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003398 mPointerSimple.currentCoords.clear();
3399 mPointerSimple.currentProperties.clear();
3400
3401 bool down, hovering;
3402 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3403 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3404 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003405 mPointerController
3406 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3407 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003408
3409 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3410 down = !hovering;
3411
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003412 float x, y;
3413 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003414 mPointerSimple.currentCoords.copyFrom(
3415 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3416 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3417 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3418 mPointerSimple.currentProperties.id = 0;
3419 mPointerSimple.currentProperties.toolType =
3420 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3421 } else {
3422 down = false;
3423 hovering = false;
3424 }
3425
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003426 return dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003427}
3428
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003429std::list<NotifyArgs> TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime,
3430 uint32_t policyFlags) {
3431 return abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003432}
3433
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003434std::list<NotifyArgs> TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
3435 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003436 mPointerSimple.currentCoords.clear();
3437 mPointerSimple.currentProperties.clear();
3438
3439 bool down, hovering;
3440 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3441 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003442 if (mLastCookedState.mouseIdBits.hasBit(id)) {
Harry Cutts714d1ad2022-08-24 16:36:43 +00003443 moveMousePointerFromPointerDelta(when, id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003444 } else {
3445 mPointerVelocityControl.reset();
3446 }
3447
3448 down = isPointerDown(mCurrentRawState.buttonState);
3449 hovering = !down;
3450
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003451 float x, y;
3452 mPointerController->getPosition(&x, &y);
Harry Cutts714d1ad2022-08-24 16:36:43 +00003453 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003454 mPointerSimple.currentCoords.copyFrom(
3455 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3456 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3457 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3458 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3459 hovering ? 0.0f : 1.0f);
3460 mPointerSimple.currentProperties.id = 0;
3461 mPointerSimple.currentProperties.toolType =
3462 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3463 } else {
3464 mPointerVelocityControl.reset();
3465
3466 down = false;
3467 hovering = false;
3468 }
3469
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003470 return dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003471}
3472
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003473std::list<NotifyArgs> TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime,
3474 uint32_t policyFlags) {
3475 std::list<NotifyArgs> out = abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003476
3477 mPointerVelocityControl.reset();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003478
3479 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003480}
3481
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003482std::list<NotifyArgs> TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
3483 uint32_t policyFlags, bool down,
3484 bool hovering) {
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00003485 LOG_ALWAYS_FATAL_IF(mDeviceMode != DeviceMode::POINTER,
3486 "%s cannot be used when the device is not in POINTER mode.", __func__);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003487 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003488 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003489
3490 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003491 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003492 mPointerController->clearSpots();
3493 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003494 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003495 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003496 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003497 }
Garfield Tan9514d782020-11-10 16:37:23 -08003498 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003499
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003500 float xCursorPosition, yCursorPosition;
3501 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003502
3503 if (mPointerSimple.down && !down) {
3504 mPointerSimple.down = false;
3505
3506 // Send up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003507 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3508 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0,
3509 0, metaState, mLastRawState.buttonState,
3510 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3511 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3512 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3513 yCursorPosition, mPointerSimple.downTime,
3514 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003515 }
3516
3517 if (mPointerSimple.hovering && !hovering) {
3518 mPointerSimple.hovering = false;
3519
3520 // Send hover exit.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003521 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3522 mSource, displayId, policyFlags,
3523 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState,
3524 mLastRawState.buttonState, MotionClassification::NONE,
3525 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3526 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3527 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3528 yCursorPosition, mPointerSimple.downTime,
3529 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003530 }
3531
3532 if (down) {
3533 if (!mPointerSimple.down) {
3534 mPointerSimple.down = true;
3535 mPointerSimple.downTime = when;
3536
3537 // Send down.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003538 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3539 mSource, displayId, policyFlags,
3540 AMOTION_EVENT_ACTION_DOWN, 0, 0, metaState,
3541 mCurrentRawState.buttonState, MotionClassification::NONE,
3542 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3543 &mPointerSimple.currentProperties,
3544 &mPointerSimple.currentCoords, mOrientedXPrecision,
3545 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3546 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003547 }
3548
3549 // Send move.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003550 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3551 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE,
3552 0, 0, metaState, mCurrentRawState.buttonState,
3553 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3554 &mPointerSimple.currentProperties,
3555 &mPointerSimple.currentCoords, mOrientedXPrecision,
3556 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3557 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003558 }
3559
3560 if (hovering) {
3561 if (!mPointerSimple.hovering) {
3562 mPointerSimple.hovering = true;
3563
3564 // Send hover enter.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003565 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3566 mSource, displayId, policyFlags,
3567 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
3568 mCurrentRawState.buttonState, MotionClassification::NONE,
3569 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3570 &mPointerSimple.currentProperties,
3571 &mPointerSimple.currentCoords, mOrientedXPrecision,
3572 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3573 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003574 }
3575
3576 // Send hover move.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003577 out.push_back(
3578 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3579 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3580 metaState, mCurrentRawState.buttonState,
3581 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3582 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3583 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3584 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003585 }
3586
3587 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3588 float vscroll = mCurrentRawState.rawVScroll;
3589 float hscroll = mCurrentRawState.rawHScroll;
3590 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3591 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3592
3593 // Send scroll.
3594 PointerCoords pointerCoords;
3595 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3596 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3597 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3598
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003599 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3600 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL,
3601 0, 0, metaState, mCurrentRawState.buttonState,
3602 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3603 &mPointerSimple.currentProperties, &pointerCoords,
3604 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3605 yCursorPosition, mPointerSimple.downTime,
3606 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003607 }
3608
3609 // Save state.
3610 if (down || hovering) {
3611 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3612 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00003613 mPointerSimple.displayId = displayId;
3614 mPointerSimple.source = mSource;
3615 mPointerSimple.lastCursorX = xCursorPosition;
3616 mPointerSimple.lastCursorY = yCursorPosition;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003617 } else {
3618 mPointerSimple.reset();
3619 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003620 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003621}
3622
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003623std::list<NotifyArgs> TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime,
3624 uint32_t policyFlags) {
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00003625 std::list<NotifyArgs> out;
3626 if (mPointerSimple.down || mPointerSimple.hovering) {
3627 int32_t metaState = getContext()->getGlobalMetaState();
3628 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3629 mPointerSimple.source, mPointerSimple.displayId, policyFlags,
3630 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
3631 metaState, mLastRawState.buttonState,
3632 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3633 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3634 mOrientedXPrecision, mOrientedYPrecision,
3635 mPointerSimple.lastCursorX, mPointerSimple.lastCursorY,
3636 mPointerSimple.downTime,
3637 /* videoFrames */ {}));
3638 if (mPointerController != nullptr) {
3639 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
3640 }
3641 }
3642 mPointerSimple.reset();
3643 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003644}
3645
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003646NotifyMotionArgs TouchInputMapper::dispatchMotion(
3647 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
3648 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00003649 int32_t edgeFlags, const PropertiesArray& properties, const CoordsArray& coords,
3650 const IdToIndexArray& idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003651 float yPrecision, nsecs_t downTime, MotionClassification classification) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003652 PointerCoords pointerCoords[MAX_POINTERS];
3653 PointerProperties pointerProperties[MAX_POINTERS];
3654 uint32_t pointerCount = 0;
3655 while (!idBits.isEmpty()) {
3656 uint32_t id = idBits.clearFirstMarkedBit();
3657 uint32_t index = idToIndex[id];
3658 pointerProperties[pointerCount].copyFrom(properties[index]);
3659 pointerCoords[pointerCount].copyFrom(coords[index]);
3660
3661 if (changedId >= 0 && id == uint32_t(changedId)) {
3662 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3663 }
3664
3665 pointerCount += 1;
3666 }
3667
3668 ALOG_ASSERT(pointerCount != 0);
3669
3670 if (changedId >= 0 && pointerCount == 1) {
3671 // Replace initial down and final up action.
3672 // We can compare the action without masking off the changed pointer index
3673 // because we know the index is 0.
3674 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3675 action = AMOTION_EVENT_ACTION_DOWN;
3676 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003677 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3678 action = AMOTION_EVENT_ACTION_CANCEL;
3679 } else {
3680 action = AMOTION_EVENT_ACTION_UP;
3681 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003682 } else {
3683 // Can't happen.
3684 ALOG_ASSERT(false);
3685 }
3686 }
3687 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3688 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003689 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003690 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003691 }
3692 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3693 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003694 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003695 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003696 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003697 return NotifyMotionArgs(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3698 policyFlags, action, actionButton, flags, metaState, buttonState,
3699 classification, edgeFlags, pointerCount, pointerProperties,
3700 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3701 downTime, std::move(frames));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003702}
3703
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003704std::list<NotifyArgs> TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3705 std::list<NotifyArgs> out;
3706 out += abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3707 out += abortTouches(when, readTime, 0 /* policyFlags*/);
3708 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003709}
3710
Prabir Pradhan1728b212021-10-19 16:00:03 -07003711bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003712 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003713 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan675f25a2022-11-10 22:04:07 +00003714 isPointInRect(mPhysicalFrameInRotatedDisplay, mRawToRotatedDisplay.transform(x, y));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003715}
3716
3717const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3718 for (const VirtualKey& virtualKey : mVirtualKeys) {
Harry Cutts45483602022-08-24 14:36:48 +00003719 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
3720 "VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3721 "left=%d, top=%d, right=%d, bottom=%d",
3722 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3723 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003724
3725 if (virtualKey.isHit(x, y)) {
3726 return &virtualKey;
3727 }
3728 }
3729
3730 return nullptr;
3731}
3732
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003733void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3734 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3735 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003736
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003737 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003738
3739 if (currentPointerCount == 0) {
3740 // No pointers to assign.
3741 return;
3742 }
3743
3744 if (lastPointerCount == 0) {
3745 // All pointers are new.
3746 for (uint32_t i = 0; i < currentPointerCount; i++) {
3747 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003748 current.rawPointerData.pointers[i].id = id;
3749 current.rawPointerData.idToIndex[id] = i;
3750 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003751 }
3752 return;
3753 }
3754
3755 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003756 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003757 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003758 uint32_t id = last.rawPointerData.pointers[0].id;
3759 current.rawPointerData.pointers[0].id = id;
3760 current.rawPointerData.idToIndex[id] = 0;
3761 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003762 return;
3763 }
3764
3765 // General case.
3766 // We build a heap of squared euclidean distances between current and last pointers
3767 // associated with the current and last pointer indices. Then, we find the best
3768 // match (by distance) for each current pointer.
3769 // The pointers must have the same tool type but it is possible for them to
3770 // transition from hovering to touching or vice-versa while retaining the same id.
3771 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3772
3773 uint32_t heapSize = 0;
3774 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3775 currentPointerIndex++) {
3776 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3777 lastPointerIndex++) {
3778 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003779 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003780 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003781 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003782 if (currentPointer.toolType == lastPointer.toolType) {
3783 int64_t deltaX = currentPointer.x - lastPointer.x;
3784 int64_t deltaY = currentPointer.y - lastPointer.y;
3785
3786 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3787
3788 // Insert new element into the heap (sift up).
3789 heap[heapSize].currentPointerIndex = currentPointerIndex;
3790 heap[heapSize].lastPointerIndex = lastPointerIndex;
3791 heap[heapSize].distance = distance;
3792 heapSize += 1;
3793 }
3794 }
3795 }
3796
3797 // Heapify
3798 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3799 startIndex -= 1;
3800 for (uint32_t parentIndex = startIndex;;) {
3801 uint32_t childIndex = parentIndex * 2 + 1;
3802 if (childIndex >= heapSize) {
3803 break;
3804 }
3805
3806 if (childIndex + 1 < heapSize &&
3807 heap[childIndex + 1].distance < heap[childIndex].distance) {
3808 childIndex += 1;
3809 }
3810
3811 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3812 break;
3813 }
3814
3815 swap(heap[parentIndex], heap[childIndex]);
3816 parentIndex = childIndex;
3817 }
3818 }
3819
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003820 if (DEBUG_POINTER_ASSIGNMENT) {
3821 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3822 for (size_t i = 0; i < heapSize; i++) {
3823 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3824 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3825 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003826 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003827
3828 // Pull matches out by increasing order of distance.
3829 // To avoid reassigning pointers that have already been matched, the loop keeps track
3830 // of which last and current pointers have been matched using the matchedXXXBits variables.
3831 // It also tracks the used pointer id bits.
3832 BitSet32 matchedLastBits(0);
3833 BitSet32 matchedCurrentBits(0);
3834 BitSet32 usedIdBits(0);
3835 bool first = true;
3836 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3837 while (heapSize > 0) {
3838 if (first) {
3839 // The first time through the loop, we just consume the root element of
3840 // the heap (the one with smallest distance).
3841 first = false;
3842 } else {
3843 // Previous iterations consumed the root element of the heap.
3844 // Pop root element off of the heap (sift down).
3845 heap[0] = heap[heapSize];
3846 for (uint32_t parentIndex = 0;;) {
3847 uint32_t childIndex = parentIndex * 2 + 1;
3848 if (childIndex >= heapSize) {
3849 break;
3850 }
3851
3852 if (childIndex + 1 < heapSize &&
3853 heap[childIndex + 1].distance < heap[childIndex].distance) {
3854 childIndex += 1;
3855 }
3856
3857 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3858 break;
3859 }
3860
3861 swap(heap[parentIndex], heap[childIndex]);
3862 parentIndex = childIndex;
3863 }
3864
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003865 if (DEBUG_POINTER_ASSIGNMENT) {
3866 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3867 for (size_t j = 0; j < heapSize; j++) {
3868 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3869 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3870 heap[j].distance);
3871 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003872 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003873 }
3874
3875 heapSize -= 1;
3876
3877 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3878 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3879
3880 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3881 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3882
3883 matchedCurrentBits.markBit(currentPointerIndex);
3884 matchedLastBits.markBit(lastPointerIndex);
3885
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003886 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3887 current.rawPointerData.pointers[currentPointerIndex].id = id;
3888 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3889 current.rawPointerData.markIdBit(id,
3890 current.rawPointerData.isHovering(
3891 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003892 usedIdBits.markBit(id);
3893
Harry Cutts45483602022-08-24 14:36:48 +00003894 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
3895 "assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3896 ", distance=%" PRIu64,
3897 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003898 break;
3899 }
3900 }
3901
3902 // Assign fresh ids to pointers that were not matched in the process.
3903 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3904 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3905 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3906
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003907 current.rawPointerData.pointers[currentPointerIndex].id = id;
3908 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3909 current.rawPointerData.markIdBit(id,
3910 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003911
Harry Cutts45483602022-08-24 14:36:48 +00003912 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
3913 "assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3914 id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003915 }
3916}
3917
3918int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3919 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3920 return AKEY_STATE_VIRTUAL;
3921 }
3922
3923 for (const VirtualKey& virtualKey : mVirtualKeys) {
3924 if (virtualKey.keyCode == keyCode) {
3925 return AKEY_STATE_UP;
3926 }
3927 }
3928
3929 return AKEY_STATE_UNKNOWN;
3930}
3931
3932int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
3933 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
3934 return AKEY_STATE_VIRTUAL;
3935 }
3936
3937 for (const VirtualKey& virtualKey : mVirtualKeys) {
3938 if (virtualKey.scanCode == scanCode) {
3939 return AKEY_STATE_UP;
3940 }
3941 }
3942
3943 return AKEY_STATE_UNKNOWN;
3944}
3945
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003946bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
3947 const std::vector<int32_t>& keyCodes,
3948 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003949 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003950 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003951 if (virtualKey.keyCode == keyCodes[i]) {
3952 outFlags[i] = 1;
3953 }
3954 }
3955 }
3956
3957 return true;
3958}
3959
3960std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
3961 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01003962 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003963 return std::make_optional(mPointerController->getDisplayId());
3964 } else {
3965 return std::make_optional(mViewport.displayId);
3966 }
3967 }
3968 return std::nullopt;
3969}
3970
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003971} // namespace android