blob: 0c57628d80ab3b1671227cd3dd54d0c18106f45e [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 Pradhane04ffaa2022-12-13 23:04:04 +000058static std::string toString(const InputDeviceUsiVersion& v) {
59 return base::StringPrintf("%d.%d", v.majorVersion, v.minorVersion);
60}
61
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062template <typename T>
63inline static void swap(T& a, T& b) {
64 T temp = a;
65 a = b;
66 b = temp;
67}
68
69static float calculateCommonVector(float a, float b) {
70 if (a > 0 && b > 0) {
71 return a < b ? a : b;
72 } else if (a < 0 && b < 0) {
73 return a > b ? a : b;
74 } else {
75 return 0;
76 }
77}
78
79inline static float distance(float x1, float y1, float x2, float y2) {
80 return hypotf(x1 - x2, y1 - y2);
81}
82
83inline static int32_t signExtendNybble(int32_t value) {
84 return value >= 8 ? value - 16 : value;
85}
86
Prabir Pradhan675f25a2022-11-10 22:04:07 +000087static ui::Size getNaturalDisplaySize(const DisplayViewport& viewport) {
Prabir Pradhan2d613f42022-11-10 20:22:06 +000088 ui::Size rotatedDisplaySize{viewport.deviceWidth, viewport.deviceHeight};
Prabir Pradhanea31d4f2022-11-10 20:48:01 +000089 if (viewport.orientation == ui::ROTATION_90 || viewport.orientation == ui::ROTATION_270) {
Prabir Pradhan2d613f42022-11-10 20:22:06 +000090 std::swap(rotatedDisplaySize.width, rotatedDisplaySize.height);
91 }
Prabir Pradhan675f25a2022-11-10 22:04:07 +000092 return rotatedDisplaySize;
Prabir Pradhan2d613f42022-11-10 20:22:06 +000093}
94
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +000095static int32_t filterButtonState(InputReaderConfiguration& config, int32_t buttonState) {
96 if (!config.stylusButtonMotionEventsEnabled) {
97 buttonState &=
98 ~(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY | AMOTION_EVENT_BUTTON_STYLUS_SECONDARY);
99 }
100 return buttonState;
101}
102
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103// --- RawPointerData ---
104
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
106 float x = 0, y = 0;
107 uint32_t count = touchingIdBits.count();
108 if (count) {
109 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) {
110 uint32_t id = idBits.clearFirstMarkedBit();
111 const Pointer& pointer = pointerForId(id);
112 x += pointer.x;
113 y += pointer.y;
114 }
115 x /= count;
116 y /= count;
117 }
118 *outX = x;
119 *outY = y;
120}
121
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122// --- TouchInputMapper ---
123
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800124TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
125 : InputMapper(deviceContext),
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +0000126 mTouchButtonAccumulator(deviceContext),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700127 mSource(0),
Michael Wright227c5542020-07-02 18:30:52 +0100128 mDeviceMode(DeviceMode::DISABLED),
Michael Wrighta9cf4192022-12-01 23:46:39 +0000129 mInputDeviceOrientation(ui::ROTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130
131TouchInputMapper::~TouchInputMapper() {}
132
Philip Junker4af3b3d2021-12-14 10:36:55 +0100133uint32_t TouchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700134 return mSource;
135}
136
137void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
138 InputMapper::populateDeviceInfo(info);
139
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000140 if (mDeviceMode == DeviceMode::DISABLED) {
141 return;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142 }
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000143
144 info->addMotionRange(mOrientedRanges.x);
145 info->addMotionRange(mOrientedRanges.y);
146 info->addMotionRange(mOrientedRanges.pressure);
147
148 if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) {
149 // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode.
150 //
151 // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative
152 // motion, i.e. the hardware dimensions, as the finger could move completely across the
153 // touchpad in one sample cycle.
154 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
155 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
156 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat, x.fuzz,
157 x.resolution);
158 info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat, y.fuzz,
159 y.resolution);
160 }
161
162 if (mOrientedRanges.size) {
163 info->addMotionRange(*mOrientedRanges.size);
164 }
165
166 if (mOrientedRanges.touchMajor) {
167 info->addMotionRange(*mOrientedRanges.touchMajor);
168 info->addMotionRange(*mOrientedRanges.touchMinor);
169 }
170
171 if (mOrientedRanges.toolMajor) {
172 info->addMotionRange(*mOrientedRanges.toolMajor);
173 info->addMotionRange(*mOrientedRanges.toolMinor);
174 }
175
176 if (mOrientedRanges.orientation) {
177 info->addMotionRange(*mOrientedRanges.orientation);
178 }
179
180 if (mOrientedRanges.distance) {
181 info->addMotionRange(*mOrientedRanges.distance);
182 }
183
184 if (mOrientedRanges.tilt) {
185 info->addMotionRange(*mOrientedRanges.tilt);
186 }
187
188 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
189 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
190 }
191 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
192 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
193 }
Prabir Pradhanedb0ba72022-10-04 15:44:11 +0000194 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000195 info->setUsiVersion(mParameters.usiVersion);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700196}
197
198void TouchInputMapper::dump(std::string& dump) {
Chris Yea03dd232020-09-08 19:21:09 -0700199 dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n",
Dominik Laskowski75788452021-02-09 18:51:25 -0800200 ftl::enum_string(mDeviceMode).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700201 dumpParameters(dump);
202 dumpVirtualKeys(dump);
203 dumpRawPointerAxes(dump);
204 dumpCalibration(dump);
205 dumpAffineTransformation(dump);
Prabir Pradhan1728b212021-10-19 16:00:03 -0700206 dumpDisplay(dump);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700207
208 dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n");
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000209 mRawToDisplay.dump(dump, "RawToDisplay Transform:", INDENT4);
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000210 mRawRotation.dump(dump, "RawRotation Transform:", INDENT4);
211 dump += StringPrintf(INDENT4 "OrientedXPrecision: %0.3f\n", mOrientedXPrecision);
212 dump += StringPrintf(INDENT4 "OrientedYPrecision: %0.3f\n", mOrientedYPrecision);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700213 dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
214 dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
215 dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
216 dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
217 dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
218 dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
219 dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
220 dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
221 dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
222 dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
223
224 dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
225 dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n",
226 mLastRawState.rawPointerData.pointerCount);
227 for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
228 const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
229 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
230 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
231 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
232 "toolType=%d, isHovering=%s\n",
233 i, pointer.id, pointer.x, pointer.y, pointer.pressure,
234 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor,
235 pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY,
236 pointer.distance, pointer.toolType, toString(pointer.isHovering));
237 }
238
239 dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n",
240 mLastCookedState.buttonState);
241 dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
242 mLastCookedState.cookedPointerData.pointerCount);
243 for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
244 const PointerProperties& pointerProperties =
245 mLastCookedState.cookedPointerData.pointerProperties[i];
246 const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000247 dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, "
248 "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, "
249 "toolMajor=%0.3f, toolMinor=%0.3f, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700250 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
251 "toolType=%d, isHovering=%s\n",
252 i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(),
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +0000253 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
254 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
256 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
257 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
258 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
259 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
260 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
261 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
262 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
263 pointerProperties.toolType,
264 toString(mLastCookedState.cookedPointerData.isHovering(i)));
265 }
266
267 dump += INDENT3 "Stylus Fusion:\n";
268 dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
269 toString(mExternalStylusConnected));
Prabir Pradhan8d9ba912022-11-11 22:26:33 +0000270 dump += StringPrintf(INDENT4 "Fused External Stylus Pointer ID: %s\n",
271 toString(mFusedStylusPointerId).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
273 mExternalStylusFusionTimeout);
Prabir Pradhan124ea442022-10-28 20:27:44 +0000274 dump += StringPrintf(INDENT4 " External Stylus Buttons Applied: 0x%08x",
275 mExternalStylusButtonsApplied);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 dump += INDENT3 "External Stylus State:\n";
277 dumpStylusState(dump, mExternalStylusState);
278
Michael Wright227c5542020-07-02 18:30:52 +0100279 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n");
281 dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale);
282 dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale);
283 dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale);
284 dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale);
285 dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth);
286 }
287}
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289std::list<NotifyArgs> TouchInputMapper::configure(nsecs_t when,
290 const InputReaderConfiguration* config,
291 uint32_t changes) {
292 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700293
294 mConfig = *config;
295
296 if (!changes) { // first time only
297 // Configure basic parameters.
298 configureParameters();
299
300 // Configure common accumulators.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800301 mCursorScrollAccumulator.configure(getDeviceContext());
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +0000302 mTouchButtonAccumulator.configure();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700303
304 // Configure absolute axis information.
305 configureRawPointerAxes();
306
307 // Prepare input device calibration.
308 parseCalibration();
309 resolveCalibration();
310 }
311
312 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
313 // Update location calibration to reflect current settings
314 updateAffineTransformation();
315 }
316
317 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
318 // Update pointer speed.
319 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
320 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
321 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
322 }
323
324 bool resetNeeded = false;
325 if (!changes ||
326 (changes &
327 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800328 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329 InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
330 InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
331 InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
Prabir Pradhan1728b212021-10-19 16:00:03 -0700332 // Configure device sources, display dimensions, orientation and
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 // scaling factors.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700334 configureInputDevice(when, &resetNeeded);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700335 }
336
337 if (changes && resetNeeded) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700338 out += reset(when);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000339
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 // Send reset, unless this is the first time the device has been configured,
341 // in which case the reader will call reset itself after all mappers are ready.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +0000342 out.emplace_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId()));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700344 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345}
346
347void TouchInputMapper::resolveExternalStylusPresence() {
348 std::vector<InputDeviceInfo> devices;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800349 getContext()->getExternalStylusDevices(devices);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700350 mExternalStylusConnected = !devices.empty();
351
352 if (!mExternalStylusConnected) {
353 resetExternalStylus();
354 }
355}
356
357void TouchInputMapper::configureParameters() {
358 // Use the pointer presentation mode for devices that do not support distinct
359 // multitouch. The spot-based presentation relies on being able to accurately
360 // locate two or more fingers on the touch pad.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800361 mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT)
Michael Wright227c5542020-07-02 18:30:52 +0100362 ? Parameters::GestureMode::SINGLE_TOUCH
363 : Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700364
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700365 std::string gestureModeString;
366 if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800367 gestureModeString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700368 if (gestureModeString == "single-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100369 mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700370 } else if (gestureModeString == "multi-touch") {
Michael Wright227c5542020-07-02 18:30:52 +0100371 mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700372 } else if (gestureModeString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700373 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374 }
375 }
376
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000377 configureDeviceType();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700378
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800379 mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380
Michael Wright227c5542020-07-02 18:30:52 +0100381 mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700382 getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800383 mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384
Michael Wrighta9cf4192022-12-01 23:46:39 +0000385 mParameters.orientation = ui::ROTATION_0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700386 std::string orientationString;
387 if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700388 orientationString)) {
389 if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
390 ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
391 } else if (orientationString == "ORIENTATION_90") {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000392 mParameters.orientation = ui::ROTATION_90;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700393 } else if (orientationString == "ORIENTATION_180") {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000394 mParameters.orientation = ui::ROTATION_180;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700395 } else if (orientationString == "ORIENTATION_270") {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000396 mParameters.orientation = ui::ROTATION_270;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700397 } else if (orientationString != "ORIENTATION_0") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700398 ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700399 }
400 }
401
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402 mParameters.hasAssociatedDisplay = false;
403 mParameters.associatedDisplayIsExternal = false;
404 if (mParameters.orientationAware ||
Michael Wright227c5542020-07-02 18:30:52 +0100405 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN ||
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000406 mParameters.deviceType == Parameters::DeviceType::POINTER ||
407 (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION &&
408 getDeviceContext().getAssociatedViewport())) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 mParameters.hasAssociatedDisplay = true;
Michael Wright227c5542020-07-02 18:30:52 +0100410 if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800411 mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700412 std::string uniqueDisplayId;
413 getDeviceContext().getConfiguration().tryGetProperty("touch.displayId",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800414 uniqueDisplayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700415 mParameters.uniqueDisplayId = uniqueDisplayId.c_str();
416 }
417 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800418 if (getDeviceContext().getAssociatedDisplayPort()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700419 mParameters.hasAssociatedDisplay = true;
420 }
421
422 // Initial downs on external touch devices should wake the device.
423 // Normally we don't do this for internal touch screens to prevent them from waking
424 // up in your pocket but you can enable it using the input device configuration.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800425 mParameters.wake = getDeviceContext().isExternal();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700426 getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake);
Prabir Pradhan167c2702022-09-14 00:37:24 +0000427
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000428 InputDeviceUsiVersion usiVersion;
429 if (getDeviceContext().getConfiguration().tryGetProperty("touch.usiVersionMajor",
430 usiVersion.majorVersion) &&
431 getDeviceContext().getConfiguration().tryGetProperty("touch.usiVersionMinor",
432 usiVersion.minorVersion)) {
433 mParameters.usiVersion = usiVersion;
434 }
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700435
436 mParameters.enableForInactiveViewport = false;
437 getDeviceContext().getConfiguration().tryGetProperty("touch.enableForInactiveViewport",
438 mParameters.enableForInactiveViewport);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439}
440
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000441void TouchInputMapper::configureDeviceType() {
442 if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) {
443 // The device is a touch screen.
444 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
445 } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) {
446 // The device is a pointing device like a track pad.
447 mParameters.deviceType = Parameters::DeviceType::POINTER;
448 } else {
449 // The device is a touch pad of unknown purpose.
450 mParameters.deviceType = Parameters::DeviceType::POINTER;
451 }
452
453 // Type association takes precedence over the device type found in the idc file.
454 std::string deviceTypeString = getDeviceContext().getDeviceTypeAssociation().value_or("");
455 if (deviceTypeString.empty()) {
456 getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType", deviceTypeString);
457 }
458 if (deviceTypeString == "touchScreen") {
459 mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN;
460 } else if (deviceTypeString == "touchNavigation") {
461 mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION;
462 } else if (deviceTypeString == "pointer") {
463 mParameters.deviceType = Parameters::DeviceType::POINTER;
464 } else if (deviceTypeString != "default" && deviceTypeString != "") {
465 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str());
466 }
467}
468
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700469void TouchInputMapper::dumpParameters(std::string& dump) {
470 dump += INDENT3 "Parameters:\n";
471
Dominik Laskowski75788452021-02-09 18:51:25 -0800472 dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473
Dominik Laskowski75788452021-02-09 18:51:25 -0800474 dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700475
476 dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, "
477 "displayId='%s'\n",
478 toString(mParameters.hasAssociatedDisplay),
479 toString(mParameters.associatedDisplayIsExternal),
480 mParameters.uniqueDisplayId.c_str());
481 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
Dominik Laskowski75788452021-02-09 18:51:25 -0800482 dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n";
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000483 dump += StringPrintf(INDENT4 "UsiVersion: %s\n",
484 toString(mParameters.usiVersion, toString).c_str());
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700485 dump += StringPrintf(INDENT4 "EnableForInactiveViewport: %s\n",
486 toString(mParameters.enableForInactiveViewport));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700487}
488
489void TouchInputMapper::configureRawPointerAxes() {
490 mRawPointerAxes.clear();
491}
492
493void TouchInputMapper::dumpRawPointerAxes(std::string& dump) {
494 dump += INDENT3 "Raw Touch Axes:\n";
495 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
496 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
497 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
498 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
499 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
500 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
501 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
502 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
503 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
504 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
505 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
506 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
507 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
508}
509
510bool TouchInputMapper::hasExternalStylus() const {
511 return mExternalStylusConnected;
512}
513
514/**
515 * Determine which DisplayViewport to use.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000516 * 1. If a device has associated display, get the matching viewport.
Garfield Tan888a6a42020-01-09 11:39:16 -0800517 * 2. Always use the suggested viewport from WindowManagerService for pointers.
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000518 * 3. Get the matching viewport by either unique id in idc file or by the display type
519 * (internal or external).
Garfield Tan888a6a42020-01-09 11:39:16 -0800520 * 4. Otherwise, use a non-display viewport.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700521 */
522std::optional<DisplayViewport> TouchInputMapper::findViewport() {
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800523 if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000524 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800525 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700526 }
527
Christine Franks2a2293c2022-01-18 11:51:16 -0800528 const std::optional<std::string> associatedDisplayUniqueId =
529 getDeviceContext().getAssociatedDisplayUniqueId();
530 if (associatedDisplayUniqueId) {
531 return getDeviceContext().getAssociatedViewport();
532 }
533
Michael Wright227c5542020-07-02 18:30:52 +0100534 if (mDeviceMode == DeviceMode::POINTER) {
Garfield Tan888a6a42020-01-09 11:39:16 -0800535 std::optional<DisplayViewport> viewport =
536 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
537 if (viewport) {
538 return viewport;
539 } else {
540 ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.",
541 mConfig.defaultPointerDisplayId);
542 }
543 }
544
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700545 // Check if uniqueDisplayId is specified in idc file.
546 if (!mParameters.uniqueDisplayId.empty()) {
547 return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId);
548 }
549
550 ViewportType viewportTypeToUse;
551 if (mParameters.associatedDisplayIsExternal) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100552 viewportTypeToUse = ViewportType::EXTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700553 } else {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100554 viewportTypeToUse = ViewportType::INTERNAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700555 }
556
557 std::optional<DisplayViewport> viewport =
558 mConfig.getDisplayViewportByType(viewportTypeToUse);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100559 if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700560 ALOGW("Input device %s should be associated with external display, "
561 "fallback to internal one for the external viewport is not found.",
562 getDeviceName().c_str());
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100563 viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700564 }
565
566 return viewport;
567 }
568
569 // No associated display, return a non-display viewport.
570 DisplayViewport newViewport;
571 // Raw width and height in the natural orientation.
572 int32_t rawWidth = mRawPointerAxes.getRawWidth();
573 int32_t rawHeight = mRawPointerAxes.getRawHeight();
574 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
575 return std::make_optional(newViewport);
576}
577
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800578int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const {
579 if (resolution < 0) {
580 ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution,
581 getDeviceName().c_str());
582 return 0;
583 }
584 return resolution;
585}
586
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800587void TouchInputMapper::initializeSizeRanges() {
588 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) {
589 mSizeScale = 0.0f;
590 return;
591 }
592
593 // Size of diagonal axis.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000594 const float diagonalSize = hypotf(mDisplayBounds.width, mDisplayBounds.height);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800595
596 // Size factors.
597 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
598 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
599 } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
600 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
601 } else {
602 mSizeScale = 0.0f;
603 }
604
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700605 mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
606 .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
607 .source = mSource,
608 .min = 0,
609 .max = diagonalSize,
610 .flat = 0,
611 .fuzz = 0,
612 .resolution = 0,
613 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800614
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800615 if (mRawPointerAxes.touchMajor.valid) {
616 mRawPointerAxes.touchMajor.resolution =
617 clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700618 mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800619 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800620
621 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700622 mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800623 if (mRawPointerAxes.touchMinor.valid) {
624 mRawPointerAxes.touchMinor.resolution =
625 clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700626 mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800627 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800628
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700629 mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
630 .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
631 .source = mSource,
632 .min = 0,
633 .max = diagonalSize,
634 .flat = 0,
635 .fuzz = 0,
636 .resolution = 0,
637 };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800638 if (mRawPointerAxes.toolMajor.valid) {
639 mRawPointerAxes.toolMajor.resolution =
640 clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700641 mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800642 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800643
644 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700645 mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800646 if (mRawPointerAxes.toolMinor.valid) {
647 mRawPointerAxes.toolMinor.resolution =
648 clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700649 mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800650 }
651
652 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700653 mOrientedRanges.touchMajor->resolution *= mGeometricScale;
654 mOrientedRanges.touchMinor->resolution *= mGeometricScale;
655 mOrientedRanges.toolMajor->resolution *= mGeometricScale;
656 mOrientedRanges.toolMinor->resolution *= mGeometricScale;
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800657 } else {
658 // Support for other calibrations can be added here.
659 ALOGW("%s calibration is not supported for size ranges at the moment. "
660 "Using raw resolution instead",
661 ftl::enum_string(mCalibration.sizeCalibration).c_str());
662 }
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800663
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700664 mOrientedRanges.size = InputDeviceInfo::MotionRange{
665 .axis = AMOTION_EVENT_AXIS_SIZE,
666 .source = mSource,
667 .min = 0,
668 .max = 1.0,
669 .flat = 0,
670 .fuzz = 0,
671 .resolution = 0,
672 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800673}
674
675void TouchInputMapper::initializeOrientedRanges() {
676 // Configure X and Y factors.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000677 const float orientedScaleX = mRawToDisplay.getScaleX();
678 const float orientedScaleY = mRawToDisplay.getScaleY();
679 mOrientedXPrecision = 1.0f / orientedScaleX;
680 mOrientedYPrecision = 1.0f / orientedScaleY;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800681
682 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
683 mOrientedRanges.x.source = mSource;
684 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
685 mOrientedRanges.y.source = mSource;
686
687 // Scale factor for terms that are not oriented in a particular axis.
688 // If the pixels are square then xScale == yScale otherwise we fake it
689 // by choosing an average.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000690 mGeometricScale = avg(orientedScaleX, orientedScaleY);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800691
692 initializeSizeRanges();
693
694 // Pressure factors.
695 mPressureScale = 0;
696 float pressureMax = 1.0;
697 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
698 mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700699 if (mCalibration.pressureScale) {
700 mPressureScale = *mCalibration.pressureScale;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800701 pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
702 } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
703 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
704 }
705 }
706
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700707 mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
708 .axis = AMOTION_EVENT_AXIS_PRESSURE,
709 .source = mSource,
710 .min = 0,
711 .max = pressureMax,
712 .flat = 0,
713 .fuzz = 0,
714 .resolution = 0,
715 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800716
717 // Tilt
718 mTiltXCenter = 0;
719 mTiltXScale = 0;
720 mTiltYCenter = 0;
721 mTiltYScale = 0;
722 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
723 if (mHaveTilt) {
724 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue);
725 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue);
726 mTiltXScale = M_PI / 180;
727 mTiltYScale = M_PI / 180;
728
729 if (mRawPointerAxes.tiltX.resolution) {
730 mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution;
731 }
732 if (mRawPointerAxes.tiltY.resolution) {
733 mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
734 }
735
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700736 mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
737 .axis = AMOTION_EVENT_AXIS_TILT,
738 .source = mSource,
739 .min = 0,
740 .max = M_PI_2,
741 .flat = 0,
742 .fuzz = 0,
743 .resolution = 0,
744 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800745 }
746
747 // Orientation
748 mOrientationScale = 0;
749 if (mHaveTilt) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700750 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
751 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
752 .source = mSource,
753 .min = -M_PI,
754 .max = M_PI,
755 .flat = 0,
756 .fuzz = 0,
757 .resolution = 0,
758 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800759
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800760 } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
761 if (mCalibration.orientationCalibration ==
762 Calibration::OrientationCalibration::INTERPOLATED) {
763 if (mRawPointerAxes.orientation.valid) {
764 if (mRawPointerAxes.orientation.maxValue > 0) {
765 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
766 } else if (mRawPointerAxes.orientation.minValue < 0) {
767 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
768 } else {
769 mOrientationScale = 0;
770 }
771 }
772 }
773
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700774 mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
775 .axis = AMOTION_EVENT_AXIS_ORIENTATION,
776 .source = mSource,
777 .min = -M_PI_2,
778 .max = M_PI_2,
779 .flat = 0,
780 .fuzz = 0,
781 .resolution = 0,
782 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800783 }
784
785 // Distance
786 mDistanceScale = 0;
787 if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
788 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700789 mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800790 }
791
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700792 mOrientedRanges.distance = InputDeviceInfo::MotionRange{
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800793
Siarhei Vishniakou24210882022-07-15 09:42:04 -0700794 .axis = AMOTION_EVENT_AXIS_DISTANCE,
795 .source = mSource,
796 .min = mRawPointerAxes.distance.minValue * mDistanceScale,
797 .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
798 .flat = 0,
799 .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
800 .resolution = 0,
801 };
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800802 }
803
804 // Compute oriented precision, scales and ranges.
805 // Note that the maximum value reported is an inclusive maximum value so it is one
806 // unit less than the total width or height of the display.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000807 // TODO(b/20508709): Calculate the oriented ranges using the input device's raw frame.
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800808 switch (mInputDeviceOrientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000809 case ui::ROTATION_90:
810 case ui::ROTATION_270:
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800811 mOrientedRanges.x.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000812 mOrientedRanges.x.max = mDisplayBounds.height - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800813 mOrientedRanges.x.flat = 0;
814 mOrientedRanges.x.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000815 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mRawToDisplay.getScaleY();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800816
817 mOrientedRanges.y.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000818 mOrientedRanges.y.max = mDisplayBounds.width - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800819 mOrientedRanges.y.flat = 0;
820 mOrientedRanges.y.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000821 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mRawToDisplay.getScaleX();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800822 break;
823
824 default:
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800825 mOrientedRanges.x.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000826 mOrientedRanges.x.max = mDisplayBounds.width - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800827 mOrientedRanges.x.flat = 0;
828 mOrientedRanges.x.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000829 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mRawToDisplay.getScaleX();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800830
831 mOrientedRanges.y.min = 0;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000832 mOrientedRanges.y.max = mDisplayBounds.height - 1;
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800833 mOrientedRanges.y.flat = 0;
834 mOrientedRanges.y.fuzz = 0;
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000835 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mRawToDisplay.getScaleY();
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -0800836 break;
837 }
838}
839
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000840void TouchInputMapper::computeInputTransforms() {
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000841 const ui::Size rawSize{mRawPointerAxes.getRawWidth(), mRawPointerAxes.getRawHeight()};
842
843 ui::Size rotatedRawSize = rawSize;
844 if (mInputDeviceOrientation == ui::ROTATION_270 || mInputDeviceOrientation == ui::ROTATION_90) {
845 std::swap(rotatedRawSize.width, rotatedRawSize.height);
846 }
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000847 const auto rotationFlags = ui::Transform::toRotationFlags(-mInputDeviceOrientation);
848 mRawRotation = ui::Transform{rotationFlags};
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000849
850 // Step 1: Undo the raw offset so that the raw coordinate space now starts at (0, 0).
851 ui::Transform undoRawOffset;
852 undoRawOffset.set(-mRawPointerAxes.x.minValue, -mRawPointerAxes.y.minValue);
853
854 // Step 2: Rotate the raw coordinates to the expected orientation.
855 ui::Transform rotate;
856 // When rotating raw coordinates, the raw size will be used as an offset.
857 // Account for the extra unit added to the raw range when the raw size was calculated.
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000858 rotate.set(rotationFlags, rotatedRawSize.width - 1, rotatedRawSize.height - 1);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000859
860 // Step 3: Scale the raw coordinates to the display space.
861 ui::Transform scaleToDisplay;
862 const float xScale = static_cast<float>(mDisplayBounds.width) / rotatedRawSize.width;
863 const float yScale = static_cast<float>(mDisplayBounds.height) / rotatedRawSize.height;
864 scaleToDisplay.set(xScale, 0, 0, yScale);
865
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000866 mRawToDisplay = (scaleToDisplay * (rotate * undoRawOffset));
867
868 // Calculate the transform that takes raw coordinates to the rotated display space.
869 ui::Transform displayToRotatedDisplay;
870 displayToRotatedDisplay.set(ui::Transform::toRotationFlags(-mViewport.orientation),
871 mViewport.deviceWidth, mViewport.deviceHeight);
872 mRawToRotatedDisplay = displayToRotatedDisplay * mRawToDisplay;
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000873}
874
Prabir Pradhan1728b212021-10-19 16:00:03 -0700875void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000876 const DeviceMode oldDeviceMode = mDeviceMode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700877
878 resolveExternalStylusPresence();
879
880 // Determine device mode.
Michael Wright227c5542020-07-02 18:30:52 +0100881 if (mParameters.deviceType == Parameters::DeviceType::POINTER &&
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000882 mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700883 mSource = AINPUT_SOURCE_MOUSE;
Michael Wright227c5542020-07-02 18:30:52 +0100884 mDeviceMode = DeviceMode::POINTER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700885 if (hasStylus()) {
886 mSource |= AINPUT_SOURCE_STYLUS;
887 }
Garfield Tanc734e4f2021-01-15 20:01:39 -0800888 } else if (isTouchScreen()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700889 mSource = AINPUT_SOURCE_TOUCHSCREEN;
Michael Wright227c5542020-07-02 18:30:52 +0100890 mDeviceMode = DeviceMode::DIRECT;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700891 if (hasStylus()) {
892 mSource |= AINPUT_SOURCE_STYLUS;
893 }
894 if (hasExternalStylus()) {
895 mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
896 }
Michael Wright227c5542020-07-02 18:30:52 +0100897 } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700898 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Michael Wright227c5542020-07-02 18:30:52 +0100899 mDeviceMode = DeviceMode::NAVIGATION;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700900 } else {
901 mSource = AINPUT_SOURCE_TOUCHPAD;
Michael Wright227c5542020-07-02 18:30:52 +0100902 mDeviceMode = DeviceMode::UNSCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700903 }
904
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000905 const std::optional<DisplayViewport> newViewportOpt = findViewport();
906
907 // Ensure the device is valid and can be used.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700908 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
909 ALOGW("Touch device '%s' did not report support for X or Y axis! "
910 "The device will be inoperable.",
911 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100912 mDeviceMode = DeviceMode::DISABLED;
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000913 } else if (!newViewportOpt) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700914 ALOGI("Touch device '%s' could not query the properties of its associated "
915 "display. The device will be inoperable until the display size "
916 "becomes available.",
917 getDeviceName().c_str());
Michael Wright227c5542020-07-02 18:30:52 +0100918 mDeviceMode = DeviceMode::DISABLED;
Yuncheol Heo50c19b12022-11-02 20:33:08 -0700919 } else if (!mParameters.enableForInactiveViewport && !newViewportOpt->isActive) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000920 ALOGI("Disabling %s (device %i) because the associated viewport is not active",
921 getDeviceName().c_str(), getDeviceId());
922 mDeviceMode = DeviceMode::DISABLED;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000923 }
924
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700925 // Raw width and height in the natural orientation.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000926 const ui::Size rawSize{mRawPointerAxes.getRawWidth(), mRawPointerAxes.getRawHeight()};
HQ Liue6983c72022-04-19 22:14:56 +0000927 const int32_t rawXResolution = mRawPointerAxes.x.resolution;
928 const int32_t rawYResolution = mRawPointerAxes.y.resolution;
929 // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
930 const float rawMeanResolution =
931 (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700932
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000933 const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport);
934 const bool viewportChanged = mViewport != newViewport;
Prabir Pradhan93a0f912021-04-21 13:47:42 -0700935 bool skipViewportUpdate = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700936 if (viewportChanged) {
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +0000937 const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation;
938 const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId;
939 mViewport = newViewport;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700940
Michael Wright227c5542020-07-02 18:30:52 +0100941 if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhan2d613f42022-11-10 20:22:06 +0000942 const auto oldDisplayBounds = mDisplayBounds;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700943
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000944 mDisplayBounds = getNaturalDisplaySize(mViewport);
945 mPhysicalFrameInRotatedDisplay = {mViewport.physicalLeft, mViewport.physicalTop,
946 mViewport.physicalRight, mViewport.physicalBottom};
Prabir Pradhan5632d622021-09-06 07:57:20 -0700947
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +0000948 // InputReader works in the un-rotated display coordinate space, so we don't need to do
949 // anything if the device is already orientation-aware. If the device is not
950 // orientation-aware, then we need to apply the inverse rotation of the display so that
951 // when the display rotation is applied later as a part of the per-window transform, we
952 // get the expected screen coordinates.
Prabir Pradhan1728b212021-10-19 16:00:03 -0700953 mInputDeviceOrientation = mParameters.orientationAware
Michael Wrighta9cf4192022-12-01 23:46:39 +0000954 ? ui::ROTATION_0
Prabir Pradhan8b89c2f2021-07-29 16:30:14 +0000955 : getInverseRotation(mViewport.orientation);
956 // For orientation-aware devices that work in the un-rotated coordinate space, the
957 // viewport update should be skipped if it is only a change in the orientation.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +0000958 skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000959 mDisplayBounds == oldDisplayBounds && viewportOrientationChanged;
Prabir Pradhanac1c74f2021-08-20 16:09:32 -0700960
961 // Apply the input device orientation for the device.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000962 mInputDeviceOrientation = mInputDeviceOrientation + mParameters.orientation;
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000963 computeInputTransforms();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700964 } else {
Prabir Pradhan7ddbc952022-11-09 22:03:40 +0000965 mDisplayBounds = rawSize;
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000966 mPhysicalFrameInRotatedDisplay = Rect{mDisplayBounds};
Michael Wrighta9cf4192022-12-01 23:46:39 +0000967 mInputDeviceOrientation = ui::ROTATION_0;
Prabir Pradhanea31d4f2022-11-10 20:48:01 +0000968 mRawToDisplay.reset();
969 mRawToDisplay.set(-mRawPointerAxes.x.minValue, -mRawPointerAxes.y.minValue);
Prabir Pradhan675f25a2022-11-10 22:04:07 +0000970 mRawToRotatedDisplay = mRawToDisplay;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700971 }
972 }
973
974 // If moving between pointer modes, need to reset some state.
975 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
976 if (deviceModeChanged) {
977 mOrientedRanges.clear();
978 }
979
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800980 // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to
981 // preserve the cursor position.
Michael Wright227c5542020-07-02 18:30:52 +0100982 if (mDeviceMode == DeviceMode::POINTER ||
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800983 (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) ||
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000984 (mParameters.deviceType == Parameters::DeviceType::POINTER &&
985 mConfig.pointerCaptureRequest.enable)) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800986 if (mPointerController == nullptr) {
987 mPointerController = getContext()->getPointerController(getDeviceId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700988 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000989 if (mConfig.pointerCaptureRequest.enable) {
Prabir Pradhan59ecc3b2020-11-20 13:11:47 -0800990 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
991 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700992 } else {
lilinnandef700b2022-06-17 19:32:01 +0800993 if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT &&
994 !mConfig.showTouches) {
995 mPointerController->clearSpots();
996 }
Michael Wright17db18e2020-06-26 20:51:44 +0100997 mPointerController.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700998 }
999
Prabir Pradhan93a0f912021-04-21 13:47:42 -07001000 if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001001 ALOGI("Device reconfigured: id=%d, name='%s', size %s, orientation %d, mode %d, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001002 "display id %d",
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001003 getDeviceId(), getDeviceName().c_str(), toString(mDisplayBounds).c_str(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001004 mInputDeviceOrientation, mDeviceMode, mViewport.displayId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001005
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001006 configureVirtualKeys();
1007
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001008 initializeOrientedRanges();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001009
1010 // Location
1011 updateAffineTransformation();
1012
Michael Wright227c5542020-07-02 18:30:52 +01001013 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001014 // Compute pointer gesture detection parameters.
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001015 float rawDiagonal = hypotf(rawSize.width, rawSize.height);
1016 float displayDiagonal = hypotf(mDisplayBounds.width, mDisplayBounds.height);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001017
1018 // Scale movements such that one whole swipe of the touch pad covers a
1019 // given area relative to the diagonal size of the display when no acceleration
1020 // is applied.
1021 // Assume that the touch pad has a square aspect ratio such that movements in
1022 // X and Y of the same number of raw units cover the same physical distance.
1023 mPointerXMovementScale =
1024 mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal;
1025 mPointerYMovementScale = mPointerXMovementScale;
1026
1027 // Scale zooms to cover a smaller range of the display than movements do.
1028 // This value determines the area around the pointer that is affected by freeform
1029 // pointer gestures.
1030 mPointerXZoomScale =
1031 mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal;
1032 mPointerYZoomScale = mPointerXZoomScale;
1033
HQ Liue6983c72022-04-19 22:14:56 +00001034 // Calculate the min freeform gesture width. It will be 0 when the resolution of any
1035 // axis is non positive value.
1036 const float minFreeformGestureWidth =
1037 rawMeanResolution * MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER;
1038
1039 mPointerGestureMaxSwipeWidth =
1040 std::max(mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal,
1041 minFreeformGestureWidth);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001042 }
1043
1044 // Inform the dispatcher about the changes.
1045 *outResetNeeded = true;
1046 bumpGeneration();
1047 }
1048}
1049
Prabir Pradhan1728b212021-10-19 16:00:03 -07001050void TouchInputMapper::dumpDisplay(std::string& dump) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001051 dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001052 dump += StringPrintf(INDENT3 "DisplayBounds: %s\n", toString(mDisplayBounds).c_str());
Prabir Pradhan675f25a2022-11-10 22:04:07 +00001053 dump += StringPrintf(INDENT3 "PhysicalFrameInRotatedDisplay: %s\n",
1054 toString(mPhysicalFrameInRotatedDisplay).c_str());
Prabir Pradhan1728b212021-10-19 16:00:03 -07001055 dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001056}
1057
1058void TouchInputMapper::configureVirtualKeys() {
1059 std::vector<VirtualKeyDefinition> virtualKeyDefinitions;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001060 getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001061
1062 mVirtualKeys.clear();
1063
1064 if (virtualKeyDefinitions.size() == 0) {
1065 return;
1066 }
1067
1068 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
1069 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
1070 int32_t touchScreenWidth = mRawPointerAxes.getRawWidth();
1071 int32_t touchScreenHeight = mRawPointerAxes.getRawHeight();
1072
1073 for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) {
1074 VirtualKey virtualKey;
1075
1076 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1077 int32_t keyCode;
1078 int32_t dummyKeyMetaState;
1079 uint32_t flags;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001080 if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState,
1081 &flags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001082 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1083 continue; // drop the key
1084 }
1085
1086 virtualKey.keyCode = keyCode;
1087 virtualKey.flags = flags;
1088
1089 // convert the key definition's display coordinates into touch coordinates for a hit box
1090 int32_t halfWidth = virtualKeyDefinition.width / 2;
1091 int32_t halfHeight = virtualKeyDefinition.height / 2;
1092
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001093 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth /
1094 mDisplayBounds.width +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001095 touchScreenLeft;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001096 virtualKey.hitRight = (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth /
1097 mDisplayBounds.width +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001098 touchScreenLeft;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001099 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight /
1100 mDisplayBounds.height +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001101 touchScreenTop;
Prabir Pradhan7ddbc952022-11-09 22:03:40 +00001102 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight /
1103 mDisplayBounds.height +
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001104 touchScreenTop;
1105 mVirtualKeys.push_back(virtualKey);
1106 }
1107}
1108
1109void TouchInputMapper::dumpVirtualKeys(std::string& dump) {
1110 if (!mVirtualKeys.empty()) {
1111 dump += INDENT3 "Virtual Keys:\n";
1112
1113 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
1114 const VirtualKey& virtualKey = mVirtualKeys[i];
1115 dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
1116 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1117 i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft,
1118 virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1119 }
1120 }
1121}
1122
1123void TouchInputMapper::parseCalibration() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001124 const PropertyMap& in = getDeviceContext().getConfiguration();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001125 Calibration& out = mCalibration;
1126
1127 // Size
Michael Wright227c5542020-07-02 18:30:52 +01001128 out.sizeCalibration = Calibration::SizeCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001129 std::string sizeCalibrationString;
1130 if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001131 if (sizeCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001132 out.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001133 } else if (sizeCalibrationString == "geometric") {
Michael Wright227c5542020-07-02 18:30:52 +01001134 out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001135 } else if (sizeCalibrationString == "diameter") {
Michael Wright227c5542020-07-02 18:30:52 +01001136 out.sizeCalibration = Calibration::SizeCalibration::DIAMETER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001137 } else if (sizeCalibrationString == "box") {
Michael Wright227c5542020-07-02 18:30:52 +01001138 out.sizeCalibration = Calibration::SizeCalibration::BOX;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001139 } else if (sizeCalibrationString == "area") {
Michael Wright227c5542020-07-02 18:30:52 +01001140 out.sizeCalibration = Calibration::SizeCalibration::AREA;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001141 } else if (sizeCalibrationString != "default") {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001142 ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001143 }
1144 }
1145
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001146 float sizeScale;
1147
1148 if (in.tryGetProperty("touch.size.scale", sizeScale)) {
1149 out.sizeScale = sizeScale;
1150 }
1151 float sizeBias;
1152 if (in.tryGetProperty("touch.size.bias", sizeBias)) {
1153 out.sizeBias = sizeBias;
1154 }
1155 bool sizeIsSummed;
1156 if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
1157 out.sizeIsSummed = sizeIsSummed;
1158 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001159
1160 // Pressure
Michael Wright227c5542020-07-02 18:30:52 +01001161 out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001162 std::string pressureCalibrationString;
1163 if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001164 if (pressureCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001165 out.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001166 } else if (pressureCalibrationString == "physical") {
Michael Wright227c5542020-07-02 18:30:52 +01001167 out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001168 } else if (pressureCalibrationString == "amplitude") {
Michael Wright227c5542020-07-02 18:30:52 +01001169 out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001170 } else if (pressureCalibrationString != "default") {
1171 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001172 pressureCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001173 }
1174 }
1175
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001176 float pressureScale;
1177 if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
1178 out.pressureScale = pressureScale;
1179 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001180
1181 // Orientation
Michael Wright227c5542020-07-02 18:30:52 +01001182 out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001183 std::string orientationCalibrationString;
1184 if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001185 if (orientationCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001186 out.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001187 } else if (orientationCalibrationString == "interpolated") {
Michael Wright227c5542020-07-02 18:30:52 +01001188 out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001189 } else if (orientationCalibrationString == "vector") {
Michael Wright227c5542020-07-02 18:30:52 +01001190 out.orientationCalibration = Calibration::OrientationCalibration::VECTOR;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001191 } else if (orientationCalibrationString != "default") {
1192 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001193 orientationCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001194 }
1195 }
1196
1197 // Distance
Michael Wright227c5542020-07-02 18:30:52 +01001198 out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001199 std::string distanceCalibrationString;
1200 if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001201 if (distanceCalibrationString == "none") {
Michael Wright227c5542020-07-02 18:30:52 +01001202 out.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001203 } else if (distanceCalibrationString == "scaled") {
Michael Wright227c5542020-07-02 18:30:52 +01001204 out.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001205 } else if (distanceCalibrationString != "default") {
1206 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07001207 distanceCalibrationString.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001208 }
1209 }
1210
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001211 float distanceScale;
1212 if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
1213 out.distanceScale = distanceScale;
1214 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001215}
1216
1217void TouchInputMapper::resolveCalibration() {
1218 // Size
1219 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001220 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) {
1221 mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001222 }
1223 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001224 mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001225 }
1226
1227 // Pressure
1228 if (mRawPointerAxes.pressure.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001229 if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) {
1230 mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001231 }
1232 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001233 mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001234 }
1235
1236 // Orientation
1237 if (mRawPointerAxes.orientation.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001238 if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) {
1239 mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001240 }
1241 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001242 mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001243 }
1244
1245 // Distance
1246 if (mRawPointerAxes.distance.valid) {
Michael Wright227c5542020-07-02 18:30:52 +01001247 if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) {
1248 mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001249 }
1250 } else {
Michael Wright227c5542020-07-02 18:30:52 +01001251 mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001252 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001253}
1254
1255void TouchInputMapper::dumpCalibration(std::string& dump) {
1256 dump += INDENT3 "Calibration:\n";
1257
Siarhei Vishniakou4e837cc2021-12-20 23:24:33 -08001258 dump += INDENT4 "touch.size.calibration: ";
1259 dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001260
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001261 if (mCalibration.sizeScale) {
1262 dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001263 }
1264
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001265 if (mCalibration.sizeBias) {
1266 dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001267 }
1268
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001269 if (mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001270 dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001271 toString(*mCalibration.sizeIsSummed));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001272 }
1273
1274 // Pressure
1275 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001276 case Calibration::PressureCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001277 dump += INDENT4 "touch.pressure.calibration: none\n";
1278 break;
Michael Wright227c5542020-07-02 18:30:52 +01001279 case Calibration::PressureCalibration::PHYSICAL:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001280 dump += INDENT4 "touch.pressure.calibration: physical\n";
1281 break;
Michael Wright227c5542020-07-02 18:30:52 +01001282 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001283 dump += INDENT4 "touch.pressure.calibration: amplitude\n";
1284 break;
1285 default:
1286 ALOG_ASSERT(false);
1287 }
1288
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001289 if (mCalibration.pressureScale) {
1290 dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001291 }
1292
1293 // Orientation
1294 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001295 case Calibration::OrientationCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001296 dump += INDENT4 "touch.orientation.calibration: none\n";
1297 break;
Michael Wright227c5542020-07-02 18:30:52 +01001298 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001299 dump += INDENT4 "touch.orientation.calibration: interpolated\n";
1300 break;
Michael Wright227c5542020-07-02 18:30:52 +01001301 case Calibration::OrientationCalibration::VECTOR:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001302 dump += INDENT4 "touch.orientation.calibration: vector\n";
1303 break;
1304 default:
1305 ALOG_ASSERT(false);
1306 }
1307
1308 // Distance
1309 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01001310 case Calibration::DistanceCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001311 dump += INDENT4 "touch.distance.calibration: none\n";
1312 break;
Michael Wright227c5542020-07-02 18:30:52 +01001313 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001314 dump += INDENT4 "touch.distance.calibration: scaled\n";
1315 break;
1316 default:
1317 ALOG_ASSERT(false);
1318 }
1319
Siarhei Vishniakou24210882022-07-15 09:42:04 -07001320 if (mCalibration.distanceScale) {
1321 dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001322 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001323}
1324
1325void TouchInputMapper::dumpAffineTransformation(std::string& dump) {
1326 dump += INDENT3 "Affine Transformation:\n";
1327
1328 dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
1329 dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
1330 dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
1331 dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
1332 dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
1333 dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
1334}
1335
1336void TouchInputMapper::updateAffineTransformation() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001337 mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07001338 mInputDeviceOrientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001339}
1340
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001341std::list<NotifyArgs> TouchInputMapper::reset(nsecs_t when) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001342 std::list<NotifyArgs> out = cancelTouch(when, when);
1343 updateTouchSpots();
1344
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001345 mCursorButtonAccumulator.reset(getDeviceContext());
1346 mCursorScrollAccumulator.reset(getDeviceContext());
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00001347 mTouchButtonAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001348
1349 mPointerVelocityControl.reset();
1350 mWheelXVelocityControl.reset();
1351 mWheelYVelocityControl.reset();
1352
1353 mRawStatesPending.clear();
1354 mCurrentRawState.clear();
1355 mCurrentCookedState.clear();
1356 mLastRawState.clear();
1357 mLastCookedState.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001358 mPointerUsage = PointerUsage::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001359 mSentHoverEnter = false;
1360 mHavePointerIds = false;
1361 mCurrentMotionAborted = false;
1362 mDownTime = 0;
1363
1364 mCurrentVirtualKey.down = false;
1365
1366 mPointerGesture.reset();
1367 mPointerSimple.reset();
1368 resetExternalStylus();
1369
1370 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01001371 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001372 mPointerController->clearSpots();
1373 }
1374
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001375 return out += InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001376}
1377
1378void TouchInputMapper::resetExternalStylus() {
1379 mExternalStylusState.clear();
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001380 mFusedStylusPointerId.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001381 mExternalStylusFusionTimeout = LLONG_MAX;
1382 mExternalStylusDataPending = false;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001383 mExternalStylusButtonsApplied = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001384}
1385
1386void TouchInputMapper::clearStylusDataPendingFlags() {
1387 mExternalStylusDataPending = false;
1388 mExternalStylusFusionTimeout = LLONG_MAX;
1389}
1390
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001391std::list<NotifyArgs> TouchInputMapper::process(const RawEvent* rawEvent) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001392 mCursorButtonAccumulator.process(rawEvent);
1393 mCursorScrollAccumulator.process(rawEvent);
1394 mTouchButtonAccumulator.process(rawEvent);
1395
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001396 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001397 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001398 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001399 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001400 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001401}
1402
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001403std::list<NotifyArgs> TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) {
1404 std::list<NotifyArgs> out;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00001405 if (mDeviceMode == DeviceMode::DISABLED) {
1406 // Only save the last pending state when the device is disabled.
1407 mRawStatesPending.clear();
1408 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001409 // Push a new state.
1410 mRawStatesPending.emplace_back();
1411
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001412 RawState& next = mRawStatesPending.back();
1413 next.clear();
1414 next.when = when;
1415 next.readTime = readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001416
1417 // Sync button state.
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00001418 next.buttonState = filterButtonState(mConfig,
1419 mTouchButtonAccumulator.getButtonState() |
1420 mCursorButtonAccumulator.getButtonState());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001421
1422 // Sync scroll
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001423 next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
1424 next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001425 mCursorScrollAccumulator.finishSync();
1426
1427 // Sync touch
Siarhei Vishniakou57479982021-03-03 01:32:21 +00001428 syncTouch(when, &next);
1429
1430 // The last RawState is the actually second to last, since we just added a new state
1431 const RawState& last =
1432 mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001433
Prabir Pradhan61a243a2022-11-16 23:47:36 +00001434 std::tie(next.when, next.readTime) =
1435 applyBluetoothTimestampSmoothening(getDeviceContext().getDeviceIdentifier(), when,
1436 readTime, last.when);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00001437
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001438 // Assign pointer ids.
1439 if (!mHavePointerIds) {
1440 assignPointerIds(last, next);
1441 }
1442
Harry Cutts45483602022-08-24 14:36:48 +00001443 ALOGD_IF(DEBUG_RAW_EVENTS,
1444 "syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
1445 "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x",
1446 last.rawPointerData.pointerCount, next.rawPointerData.pointerCount,
1447 last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value,
1448 last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value,
1449 next.rawPointerData.canceledIdBits.value);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001450
Arthur Hung9ad18942021-06-19 02:04:46 +00001451 if (!next.rawPointerData.touchingIdBits.isEmpty() &&
1452 !next.rawPointerData.hoveringIdBits.isEmpty() &&
1453 last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) {
1454 ALOGI("Multi-touch contains some hovering ids 0x%08x",
1455 next.rawPointerData.hoveringIdBits.value);
1456 }
1457
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001458 out += processRawTouches(false /*timeout*/);
1459 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001460}
1461
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001462std::list<NotifyArgs> TouchInputMapper::processRawTouches(bool timeout) {
1463 std::list<NotifyArgs> out;
Michael Wright227c5542020-07-02 18:30:52 +01001464 if (mDeviceMode == DeviceMode::DISABLED) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001465 // Do not process raw event while the device is disabled.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001466 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001467 }
1468
1469 // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
1470 // valid and must go through the full cook and dispatch cycle. This ensures that anything
1471 // touching the current state will only observe the events that have been dispatched to the
1472 // rest of the pipeline.
1473 const size_t N = mRawStatesPending.size();
1474 size_t count;
1475 for (count = 0; count < N; count++) {
1476 const RawState& next = mRawStatesPending[count];
1477
1478 // A failure to assign the stylus id means that we're waiting on stylus data
1479 // and so should defer the rest of the pipeline.
1480 if (assignExternalStylusId(next, timeout)) {
1481 break;
1482 }
1483
1484 // All ready to go.
1485 clearStylusDataPendingFlags();
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001486 mCurrentRawState = next;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001487 if (mCurrentRawState.when < mLastRawState.when) {
1488 mCurrentRawState.when = mLastRawState.when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001489 mCurrentRawState.readTime = mLastRawState.readTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001490 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001491 out += cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001492 }
1493 if (count != 0) {
1494 mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count);
1495 }
1496
1497 if (mExternalStylusDataPending) {
1498 if (timeout) {
1499 nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
1500 clearStylusDataPendingFlags();
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001501 mCurrentRawState = mLastRawState;
Harry Cutts45483602022-08-24 14:36:48 +00001502 ALOGD_IF(DEBUG_STYLUS_FUSION,
1503 "Timeout expired, synthesizing event with new stylus data");
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001504 const nsecs_t readTime = when; // consider this synthetic event to be zero latency
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001505 out += cookAndDispatch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001506 } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
1507 mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
1508 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1509 }
1510 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001511 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001512}
1513
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001514std::list<NotifyArgs> TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) {
1515 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001516 // Always start with a clean state.
1517 mCurrentCookedState.clear();
1518
1519 // Apply stylus buttons to current raw state.
1520 applyExternalStylusButtonState(when);
1521
1522 // Handle policy on initial down or hover events.
1523 bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1524 mCurrentRawState.rawPointerData.pointerCount != 0;
1525
1526 uint32_t policyFlags = 0;
1527 bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
1528 if (initialDown || buttonsPressed) {
1529 // If this is a touch screen, hide the pointer on an initial down.
Michael Wright227c5542020-07-02 18:30:52 +01001530 if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001531 getContext()->fadePointer();
1532 }
1533
1534 if (mParameters.wake) {
1535 policyFlags |= POLICY_FLAG_WAKE;
1536 }
1537 }
1538
1539 // Consume raw off-screen touches before cooking pointer data.
1540 // If touches are consumed, subsequent code will not receive any pointer data.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001541 bool consumed;
1542 out += consumeRawTouches(when, readTime, policyFlags, consumed /*byref*/);
1543 if (consumed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001544 mCurrentRawState.rawPointerData.clear();
1545 }
1546
1547 // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
1548 // with cooked pointer data that has the same ids and indices as the raw data.
1549 // The following code can use either the raw or cooked data, as needed.
1550 cookPointerData();
1551
1552 // Apply stylus pressure to current cooked state.
1553 applyExternalStylusTouchState(when);
1554
1555 // Synthesize key down from raw buttons if needed.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001556 out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(),
1557 mSource, mViewport.displayId, policyFlags,
1558 mLastCookedState.buttonState, mCurrentCookedState.buttonState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001559
1560 // Dispatch the touches either directly or by translation through a pointer on screen.
Michael Wright227c5542020-07-02 18:30:52 +01001561 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001562 for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) {
1563 uint32_t id = idBits.clearFirstMarkedBit();
1564 const RawPointerData::Pointer& pointer =
1565 mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhane5626962022-10-27 20:30:53 +00001566 if (isStylusToolType(pointer.toolType)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001567 mCurrentCookedState.stylusIdBits.markBit(id);
1568 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
1569 pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
1570 mCurrentCookedState.fingerIdBits.markBit(id);
1571 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
1572 mCurrentCookedState.mouseIdBits.markBit(id);
1573 }
1574 }
1575 for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) {
1576 uint32_t id = idBits.clearFirstMarkedBit();
1577 const RawPointerData::Pointer& pointer =
1578 mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhane5626962022-10-27 20:30:53 +00001579 if (isStylusToolType(pointer.toolType)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001580 mCurrentCookedState.stylusIdBits.markBit(id);
1581 }
1582 }
1583
1584 // Stylus takes precedence over all tools, then mouse, then finger.
1585 PointerUsage pointerUsage = mPointerUsage;
1586 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
1587 mCurrentCookedState.mouseIdBits.clear();
1588 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001589 pointerUsage = PointerUsage::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001590 } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
1591 mCurrentCookedState.fingerIdBits.clear();
Michael Wright227c5542020-07-02 18:30:52 +01001592 pointerUsage = PointerUsage::MOUSE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001593 } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
1594 isPointerDown(mCurrentRawState.buttonState)) {
Michael Wright227c5542020-07-02 18:30:52 +01001595 pointerUsage = PointerUsage::GESTURES;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001596 }
1597
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001598 out += dispatchPointerUsage(when, readTime, policyFlags, pointerUsage);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001599 } else {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001600 if (!mCurrentMotionAborted) {
Prabir Pradhan9eb4e692022-04-27 13:19:15 +00001601 updateTouchSpots();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001602 out += dispatchButtonRelease(when, readTime, policyFlags);
1603 out += dispatchHoverExit(when, readTime, policyFlags);
1604 out += dispatchTouches(when, readTime, policyFlags);
1605 out += dispatchHoverEnterAndMove(when, readTime, policyFlags);
1606 out += dispatchButtonPress(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001607 }
1608
1609 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
1610 mCurrentMotionAborted = false;
1611 }
1612 }
1613
1614 // Synthesize key up from raw buttons if needed.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001615 out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(),
1616 mSource, mViewport.displayId, policyFlags,
1617 mLastCookedState.buttonState, mCurrentCookedState.buttonState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001618
1619 // Clear some transient state.
1620 mCurrentRawState.rawVScroll = 0;
1621 mCurrentRawState.rawHScroll = 0;
1622
1623 // Copy current touch to last touch in preparation for the next cycle.
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001624 mLastRawState = mCurrentRawState;
1625 mLastCookedState = mCurrentCookedState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001626 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001627}
1628
Garfield Tanc734e4f2021-01-15 20:01:39 -08001629void TouchInputMapper::updateTouchSpots() {
1630 if (!mConfig.showTouches || mPointerController == nullptr) {
1631 return;
1632 }
1633
1634 // Update touch spots when this is a touchscreen even when it's not enabled so that we can
1635 // clear touch spots.
1636 if (mDeviceMode != DeviceMode::DIRECT &&
1637 (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) {
1638 return;
1639 }
1640
1641 mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT);
1642 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
1643
1644 mPointerController->setButtonState(mCurrentRawState.buttonState);
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001645 mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords.cbegin(),
1646 mCurrentCookedState.cookedPointerData.idToIndex.cbegin(),
Prabir Pradhande69f8a2021-11-18 16:40:34 +00001647 mCurrentCookedState.cookedPointerData.touchingIdBits,
1648 mViewport.displayId);
Garfield Tanc734e4f2021-01-15 20:01:39 -08001649}
1650
1651bool TouchInputMapper::isTouchScreen() {
1652 return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN &&
1653 mParameters.hasAssociatedDisplay;
1654}
1655
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001656void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001657 if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus()) {
1658 // If any of the external buttons are already pressed by the touch device, ignore them.
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00001659 const int32_t pressedButtons =
1660 filterButtonState(mConfig,
1661 ~mCurrentRawState.buttonState & mExternalStylusState.buttons);
Prabir Pradhan124ea442022-10-28 20:27:44 +00001662 const int32_t releasedButtons =
1663 mExternalStylusButtonsApplied & ~mExternalStylusState.buttons;
1664
1665 mCurrentRawState.buttonState |= pressedButtons;
1666 mCurrentRawState.buttonState &= ~releasedButtons;
1667
1668 mExternalStylusButtonsApplied |= pressedButtons;
1669 mExternalStylusButtonsApplied &= ~releasedButtons;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001670 }
1671}
1672
1673void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
1674 CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
1675 const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001676 if (!mFusedStylusPointerId || !currentPointerData.isTouching(*mFusedStylusPointerId)) {
1677 return;
1678 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001679
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001680 float pressure = lastPointerData.isTouching(*mFusedStylusPointerId)
1681 ? lastPointerData.pointerCoordsForId(*mFusedStylusPointerId)
1682 .getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)
1683 : 0.f;
1684 if (mExternalStylusState.pressure && *mExternalStylusState.pressure > 0.f) {
1685 pressure = *mExternalStylusState.pressure;
1686 }
1687 PointerCoords& coords = currentPointerData.editPointerCoordsWithId(*mFusedStylusPointerId);
1688 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001689
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001690 if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001691 PointerProperties& properties =
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001692 currentPointerData.editPointerPropertiesWithId(*mFusedStylusPointerId);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001693 properties.toolType = mExternalStylusState.toolType;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001694 }
1695}
1696
1697bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
Michael Wright227c5542020-07-02 18:30:52 +01001698 if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001699 return false;
1700 }
1701
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001702 // Check if the stylus pointer has gone up.
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001703 if (mFusedStylusPointerId &&
1704 !state.rawPointerData.touchingIdBits.hasBit(*mFusedStylusPointerId)) {
Harry Cutts45483602022-08-24 14:36:48 +00001705 ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus pointer is going up");
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001706 mFusedStylusPointerId.reset();
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001707 return false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001708 }
1709
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001710 const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 &&
1711 state.rawPointerData.pointerCount != 0;
1712 if (!initialDown) {
1713 return false;
1714 }
1715
1716 if (!mExternalStylusState.pressure) {
1717 ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus does not support pressure, no pointer fusion needed");
1718 return false;
1719 }
1720
1721 if (*mExternalStylusState.pressure != 0.0f) {
1722 ALOGD_IF(DEBUG_STYLUS_FUSION, "Have both stylus and touch data, beginning fusion");
1723 mFusedStylusPointerId = state.rawPointerData.touchingIdBits.firstMarkedBit();
1724 return false;
1725 }
1726
1727 if (timeout) {
1728 ALOGD_IF(DEBUG_STYLUS_FUSION, "Timeout expired, assuming touch is not a stylus.");
1729 mFusedStylusPointerId.reset();
1730 mExternalStylusFusionTimeout = LLONG_MAX;
1731 return false;
1732 }
1733
1734 // We are waiting for the external stylus to report a pressure value. Withhold touches from
1735 // being processed until we either get pressure data or timeout.
1736 if (mExternalStylusFusionTimeout == LLONG_MAX) {
1737 mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
1738 }
1739 ALOGD_IF(DEBUG_STYLUS_FUSION,
1740 "No stylus data but stylus is connected, requesting timeout (%" PRId64 "ms)",
1741 mExternalStylusFusionTimeout);
1742 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1743 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001744}
1745
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001746std::list<NotifyArgs> TouchInputMapper::timeoutExpired(nsecs_t when) {
1747 std::list<NotifyArgs> out;
Michael Wright227c5542020-07-02 18:30:52 +01001748 if (mDeviceMode == DeviceMode::POINTER) {
1749 if (mPointerUsage == PointerUsage::GESTURES) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001750 // Since this is a synthetic event, we can consider its latency to be zero
1751 const nsecs_t readTime = when;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001752 out += dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001753 }
Michael Wright227c5542020-07-02 18:30:52 +01001754 } else if (mDeviceMode == DeviceMode::DIRECT) {
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001755 if (mExternalStylusFusionTimeout <= when) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001756 out += processRawTouches(true /*timeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001757 } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
1758 getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
1759 }
1760 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001761 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001762}
1763
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001764std::list<NotifyArgs> TouchInputMapper::updateExternalStylusState(const StylusState& state) {
1765 std::list<NotifyArgs> out;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001766 const bool buttonsChanged = mExternalStylusState.buttons != state.buttons;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00001767 mExternalStylusState = state;
Prabir Pradhan8d9ba912022-11-11 22:26:33 +00001768 if (mFusedStylusPointerId || mExternalStylusFusionTimeout != LLONG_MAX || buttonsChanged) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001769 // The following three cases are handled here:
1770 // - We're in the middle of a fused stream of data;
1771 // - We're waiting on external stylus data before dispatching the initial down; or
1772 // - Only the button state, which is not reported through a specific pointer, has changed.
1773 // Go ahead and dispatch now that we have fresh stylus data.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001774 mExternalStylusDataPending = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001775 out += processRawTouches(false /*timeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001776 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001777 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001778}
1779
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001780std::list<NotifyArgs> TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime,
1781 uint32_t policyFlags, bool& outConsumed) {
1782 outConsumed = false;
1783 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001784 // Check for release of a virtual key.
1785 if (mCurrentVirtualKey.down) {
1786 if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1787 // Pointer went up while virtual key was down.
1788 mCurrentVirtualKey.down = false;
1789 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001790 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1791 "VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1792 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001793 out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
1794 AKEY_EVENT_FLAG_FROM_SYSTEM |
1795 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001796 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001797 outConsumed = true;
1798 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001799 }
1800
1801 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1802 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1803 const RawPointerData::Pointer& pointer =
1804 mCurrentRawState.rawPointerData.pointerForId(id);
1805 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1806 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
1807 // Pointer is still within the space of the virtual key.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001808 outConsumed = true;
1809 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001810 }
1811 }
1812
1813 // Pointer left virtual key area or another pointer also went down.
1814 // Send key cancellation but do not consume the touch yet.
1815 // This is useful when the user swipes through from the virtual key area
1816 // into the main display surface.
1817 mCurrentVirtualKey.down = false;
1818 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001819 ALOGD_IF(DEBUG_VIRTUAL_KEYS, "VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1820 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001821 out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP,
1822 AKEY_EVENT_FLAG_FROM_SYSTEM |
1823 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
1824 AKEY_EVENT_FLAG_CANCELED));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001825 }
1826 }
1827
1828 if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() &&
1829 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
1830 // Pointer just went down. Check for virtual key press or off-screen touches.
1831 uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
1832 const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
Prabir Pradhan1728b212021-10-19 16:00:03 -07001833 // Skip checking whether the pointer is inside the physical frame if the device is in
1834 // unscaled mode.
1835 if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) &&
1836 mDeviceMode != DeviceMode::UNSCALED) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001837 // If exactly one pointer went down, check for virtual key hit.
1838 // Otherwise we will drop the entire stroke.
1839 if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
1840 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
1841 if (virtualKey) {
1842 mCurrentVirtualKey.down = true;
1843 mCurrentVirtualKey.downTime = when;
1844 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
1845 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
1846 mCurrentVirtualKey.ignored =
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001847 getContext()->shouldDropVirtualKey(when, virtualKey->keyCode,
1848 virtualKey->scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001849
1850 if (!mCurrentVirtualKey.ignored) {
Harry Cutts45483602022-08-24 14:36:48 +00001851 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
1852 "VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1853 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001854 out.push_back(dispatchVirtualKey(when, readTime, policyFlags,
1855 AKEY_EVENT_ACTION_DOWN,
1856 AKEY_EVENT_FLAG_FROM_SYSTEM |
1857 AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001858 }
1859 }
1860 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001861 outConsumed = true;
1862 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001863 }
1864 }
1865
1866 // Disable all virtual key touches that happen within a short time interval of the
1867 // most recent touch within the screen area. The idea is to filter out stray
1868 // virtual key presses when interacting with the touch screen.
1869 //
1870 // Problems we're trying to solve:
1871 //
1872 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
1873 // virtual key area that is implemented by a separate touch panel and accidentally
1874 // triggers a virtual key.
1875 //
1876 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
1877 // area and accidentally triggers a virtual key. This often happens when virtual keys
1878 // are layed out below the screen near to where the on screen keyboard's space bar
1879 // is displayed.
1880 if (mConfig.virtualKeyQuietTime > 0 &&
1881 !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001882 getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001883 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001884 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001885}
1886
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001887NotifyKeyArgs TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime,
1888 uint32_t policyFlags, int32_t keyEventAction,
1889 int32_t keyEventFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001890 int32_t keyCode = mCurrentVirtualKey.keyCode;
1891 int32_t scanCode = mCurrentVirtualKey.scanCode;
1892 nsecs_t downTime = mCurrentVirtualKey.downTime;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001893 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001894 policyFlags |= POLICY_FLAG_VIRTUAL;
1895
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001896 return NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
1897 AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction,
1898 keyEventFlags, keyCode, scanCode, metaState, downTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001899}
1900
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001901std::list<NotifyArgs> TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime,
1902 uint32_t policyFlags) {
1903 std::list<NotifyArgs> out;
lilinnan687e58f2022-07-19 16:00:50 +08001904 if (mCurrentMotionAborted) {
1905 // Current motion event was already aborted.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001906 return out;
lilinnan687e58f2022-07-19 16:00:50 +08001907 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001908 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1909 if (!currentIdBits.isEmpty()) {
1910 int32_t metaState = getContext()->getGlobalMetaState();
1911 int32_t buttonState = mCurrentCookedState.buttonState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001912 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00001913 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
1914 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001915 mCurrentCookedState.cookedPointerData.pointerProperties,
1916 mCurrentCookedState.cookedPointerData.pointerCoords,
1917 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits,
1918 -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1919 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001920 mCurrentMotionAborted = true;
1921 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001922 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001923}
1924
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00001925// Updates pointer coords and properties for pointers with specified ids that have moved.
1926// Returns true if any of them changed.
1927static bool updateMovedPointers(const PropertiesArray& inProperties, CoordsArray& inCoords,
1928 const IdToIndexArray& inIdToIndex, PropertiesArray& outProperties,
1929 CoordsArray& outCoords, IdToIndexArray& outIdToIndex,
1930 BitSet32 idBits) {
1931 bool changed = false;
1932 while (!idBits.isEmpty()) {
1933 uint32_t id = idBits.clearFirstMarkedBit();
1934 uint32_t inIndex = inIdToIndex[id];
1935 uint32_t outIndex = outIdToIndex[id];
1936
1937 const PointerProperties& curInProperties = inProperties[inIndex];
1938 const PointerCoords& curInCoords = inCoords[inIndex];
1939 PointerProperties& curOutProperties = outProperties[outIndex];
1940 PointerCoords& curOutCoords = outCoords[outIndex];
1941
1942 if (curInProperties != curOutProperties) {
1943 curOutProperties.copyFrom(curInProperties);
1944 changed = true;
1945 }
1946
1947 if (curInCoords != curOutCoords) {
1948 curOutCoords.copyFrom(curInCoords);
1949 changed = true;
1950 }
1951 }
1952 return changed;
1953}
1954
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001955std::list<NotifyArgs> TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime,
1956 uint32_t policyFlags) {
1957 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001958 BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
1959 BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
1960 int32_t metaState = getContext()->getGlobalMetaState();
1961 int32_t buttonState = mCurrentCookedState.buttonState;
1962
1963 if (currentIdBits == lastIdBits) {
1964 if (!currentIdBits.isEmpty()) {
1965 // No pointer id changes so this is a move event.
1966 // The listener takes care of batching moves so we don't have to deal with that here.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001967 out.push_back(
1968 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE,
1969 0, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1970 mCurrentCookedState.cookedPointerData.pointerProperties,
1971 mCurrentCookedState.cookedPointerData.pointerCoords,
1972 mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits,
1973 -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime,
1974 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001975 }
1976 } else {
1977 // There may be pointers going up and pointers going down and pointers moving
1978 // all at the same time.
1979 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
1980 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
1981 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
1982 BitSet32 dispatchedIdBits(lastIdBits.value);
1983
1984 // Update last coordinates of pointers that have moved so that we observe the new
1985 // pointer positions at the same time as other pointers that have just gone up.
1986 bool moveNeeded =
1987 updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties,
1988 mCurrentCookedState.cookedPointerData.pointerCoords,
1989 mCurrentCookedState.cookedPointerData.idToIndex,
1990 mLastCookedState.cookedPointerData.pointerProperties,
1991 mLastCookedState.cookedPointerData.pointerCoords,
1992 mLastCookedState.cookedPointerData.idToIndex, moveIdBits);
1993 if (buttonState != mLastCookedState.buttonState) {
1994 moveNeeded = true;
1995 }
1996
1997 // Dispatch pointer up events.
1998 while (!upIdBits.isEmpty()) {
1999 uint32_t upId = upIdBits.clearFirstMarkedBit();
arthurhungcc7f9802020-04-30 17:55:40 +08002000 bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId);
arthurhung17d64842021-01-21 16:01:27 +08002001 if (isCanceled) {
2002 ALOGI("Canceling pointer %d for the palm event was detected.", upId);
2003 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002004 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2005 AMOTION_EVENT_ACTION_POINTER_UP, 0,
2006 isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState,
2007 buttonState, 0,
2008 mLastCookedState.cookedPointerData.pointerProperties,
2009 mLastCookedState.cookedPointerData.pointerCoords,
2010 mLastCookedState.cookedPointerData.idToIndex,
2011 dispatchedIdBits, upId, mOrientedXPrecision,
2012 mOrientedYPrecision, mDownTime,
2013 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002014 dispatchedIdBits.clearBit(upId);
arthurhungcc7f9802020-04-30 17:55:40 +08002015 mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002016 }
2017
2018 // Dispatch move events if any of the remaining pointers moved from their old locations.
2019 // Although applications receive new locations as part of individual pointer up
2020 // events, they do not generally handle them except when presented in a move event.
2021 if (moveNeeded && !moveIdBits.isEmpty()) {
2022 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002023 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2024 AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, 0,
2025 mCurrentCookedState.cookedPointerData.pointerProperties,
2026 mCurrentCookedState.cookedPointerData.pointerCoords,
2027 mCurrentCookedState.cookedPointerData.idToIndex,
2028 dispatchedIdBits, -1, mOrientedXPrecision,
2029 mOrientedYPrecision, mDownTime,
2030 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002031 }
2032
2033 // Dispatch pointer down events using the new pointer locations.
2034 while (!downIdBits.isEmpty()) {
2035 uint32_t downId = downIdBits.clearFirstMarkedBit();
2036 dispatchedIdBits.markBit(downId);
2037
2038 if (dispatchedIdBits.count() == 1) {
2039 // First pointer is going down. Set down time.
2040 mDownTime = when;
2041 }
2042
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002043 out.push_back(
2044 dispatchMotion(when, readTime, policyFlags, mSource,
2045 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, metaState, buttonState,
2046 0, mCurrentCookedState.cookedPointerData.pointerProperties,
2047 mCurrentCookedState.cookedPointerData.pointerCoords,
2048 mCurrentCookedState.cookedPointerData.idToIndex,
2049 dispatchedIdBits, downId, mOrientedXPrecision,
2050 mOrientedYPrecision, mDownTime, MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002051 }
2052 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002053 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002054}
2055
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002056std::list<NotifyArgs> TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime,
2057 uint32_t policyFlags) {
2058 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002059 if (mSentHoverEnter &&
2060 (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() ||
2061 !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
2062 int32_t metaState = getContext()->getGlobalMetaState();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002063 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2064 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState,
2065 mLastCookedState.buttonState, 0,
2066 mLastCookedState.cookedPointerData.pointerProperties,
2067 mLastCookedState.cookedPointerData.pointerCoords,
2068 mLastCookedState.cookedPointerData.idToIndex,
2069 mLastCookedState.cookedPointerData.hoveringIdBits, -1,
2070 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2071 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002072 mSentHoverEnter = false;
2073 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002074 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002075}
2076
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002077std::list<NotifyArgs> TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime,
2078 uint32_t policyFlags) {
2079 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002080 if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() &&
2081 !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
2082 int32_t metaState = getContext()->getGlobalMetaState();
2083 if (!mSentHoverEnter) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002084 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2085 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
2086 mCurrentRawState.buttonState, 0,
2087 mCurrentCookedState.cookedPointerData.pointerProperties,
2088 mCurrentCookedState.cookedPointerData.pointerCoords,
2089 mCurrentCookedState.cookedPointerData.idToIndex,
2090 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2091 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2092 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002093 mSentHoverEnter = true;
2094 }
2095
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002096 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2097 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState,
2098 mCurrentRawState.buttonState, 0,
2099 mCurrentCookedState.cookedPointerData.pointerProperties,
2100 mCurrentCookedState.cookedPointerData.pointerCoords,
2101 mCurrentCookedState.cookedPointerData.idToIndex,
2102 mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
2103 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2104 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002105 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002106 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002107}
2108
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002109std::list<NotifyArgs> TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime,
2110 uint32_t policyFlags) {
2111 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002112 BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
2113 const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
2114 const int32_t metaState = getContext()->getGlobalMetaState();
2115 int32_t buttonState = mLastCookedState.buttonState;
2116 while (!releasedButtons.isEmpty()) {
2117 int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
2118 buttonState &= ~actionButton;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002119 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2120 AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0,
2121 metaState, buttonState, 0,
Prabir Pradhan211ba622022-10-31 21:09:21 +00002122 mLastCookedState.cookedPointerData.pointerProperties,
2123 mLastCookedState.cookedPointerData.pointerCoords,
2124 mLastCookedState.cookedPointerData.idToIndex, idBits, -1,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002125 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2126 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002127 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002128 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002129}
2130
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002131std::list<NotifyArgs> TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime,
2132 uint32_t policyFlags) {
2133 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002134 BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
2135 const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
2136 const int32_t metaState = getContext()->getGlobalMetaState();
2137 int32_t buttonState = mLastCookedState.buttonState;
2138 while (!pressedButtons.isEmpty()) {
2139 int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
2140 buttonState |= actionButton;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002141 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2142 AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0, metaState,
2143 buttonState, 0,
2144 mCurrentCookedState.cookedPointerData.pointerProperties,
2145 mCurrentCookedState.cookedPointerData.pointerCoords,
2146 mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
2147 mOrientedXPrecision, mOrientedYPrecision, mDownTime,
2148 MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002149 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002150 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002151}
2152
2153const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
2154 if (!cookedPointerData.touchingIdBits.isEmpty()) {
2155 return cookedPointerData.touchingIdBits;
2156 }
2157 return cookedPointerData.hoveringIdBits;
2158}
2159
2160void TouchInputMapper::cookPointerData() {
2161 uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
2162
2163 mCurrentCookedState.cookedPointerData.clear();
2164 mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
2165 mCurrentCookedState.cookedPointerData.hoveringIdBits =
2166 mCurrentRawState.rawPointerData.hoveringIdBits;
2167 mCurrentCookedState.cookedPointerData.touchingIdBits =
2168 mCurrentRawState.rawPointerData.touchingIdBits;
arthurhungcc7f9802020-04-30 17:55:40 +08002169 mCurrentCookedState.cookedPointerData.canceledIdBits =
2170 mCurrentRawState.rawPointerData.canceledIdBits;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002171
2172 if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
2173 mCurrentCookedState.buttonState = 0;
2174 } else {
2175 mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
2176 }
2177
2178 // Walk through the the active pointers and map device coordinates onto
Prabir Pradhan1728b212021-10-19 16:00:03 -07002179 // display coordinates and adjust for display orientation.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002180 for (uint32_t i = 0; i < currentPointerCount; i++) {
2181 const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
2182
2183 // Size
2184 float touchMajor, touchMinor, toolMajor, toolMinor, size;
2185 switch (mCalibration.sizeCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002186 case Calibration::SizeCalibration::GEOMETRIC:
2187 case Calibration::SizeCalibration::DIAMETER:
2188 case Calibration::SizeCalibration::BOX:
2189 case Calibration::SizeCalibration::AREA:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002190 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
2191 touchMajor = in.touchMajor;
2192 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2193 toolMajor = in.toolMajor;
2194 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2195 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2196 : in.touchMajor;
2197 } else if (mRawPointerAxes.touchMajor.valid) {
2198 toolMajor = touchMajor = in.touchMajor;
2199 toolMinor = touchMinor =
2200 mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
2201 size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor)
2202 : in.touchMajor;
2203 } else if (mRawPointerAxes.toolMajor.valid) {
2204 touchMajor = toolMajor = in.toolMajor;
2205 touchMinor = toolMinor =
2206 mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
2207 size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor)
2208 : in.toolMajor;
2209 } else {
2210 ALOG_ASSERT(false,
2211 "No touch or tool axes. "
2212 "Size calibration should have been resolved to NONE.");
2213 touchMajor = 0;
2214 touchMinor = 0;
2215 toolMajor = 0;
2216 toolMinor = 0;
2217 size = 0;
2218 }
2219
Siarhei Vishniakou24210882022-07-15 09:42:04 -07002220 if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002221 uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
2222 if (touchingCount > 1) {
2223 touchMajor /= touchingCount;
2224 touchMinor /= touchingCount;
2225 toolMajor /= touchingCount;
2226 toolMinor /= touchingCount;
2227 size /= touchingCount;
2228 }
2229 }
2230
Michael Wright227c5542020-07-02 18:30:52 +01002231 if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002232 touchMajor *= mGeometricScale;
2233 touchMinor *= mGeometricScale;
2234 toolMajor *= mGeometricScale;
2235 toolMinor *= mGeometricScale;
Michael Wright227c5542020-07-02 18:30:52 +01002236 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002237 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
2238 touchMinor = touchMajor;
2239 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
2240 toolMinor = toolMajor;
Michael Wright227c5542020-07-02 18:30:52 +01002241 } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002242 touchMinor = touchMajor;
2243 toolMinor = toolMajor;
2244 }
2245
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002246 mCalibration.applySizeScaleAndBias(touchMajor);
2247 mCalibration.applySizeScaleAndBias(touchMinor);
2248 mCalibration.applySizeScaleAndBias(toolMajor);
2249 mCalibration.applySizeScaleAndBias(toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002250 size *= mSizeScale;
2251 break;
Siarhei Vishniakou07247342022-07-15 14:27:37 -07002252 case Calibration::SizeCalibration::DEFAULT:
2253 LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point");
2254 break;
2255 case Calibration::SizeCalibration::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002256 touchMajor = 0;
2257 touchMinor = 0;
2258 toolMajor = 0;
2259 toolMinor = 0;
2260 size = 0;
2261 break;
2262 }
2263
2264 // Pressure
2265 float pressure;
2266 switch (mCalibration.pressureCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002267 case Calibration::PressureCalibration::PHYSICAL:
2268 case Calibration::PressureCalibration::AMPLITUDE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002269 pressure = in.pressure * mPressureScale;
2270 break;
2271 default:
2272 pressure = in.isHovering ? 0 : 1;
2273 break;
2274 }
2275
2276 // Tilt and Orientation
2277 float tilt;
2278 float orientation;
2279 if (mHaveTilt) {
2280 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
2281 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
Prabir Pradhane2e10b42022-11-17 20:59:36 +00002282 orientation = transformAngle(mRawRotation, atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002283 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
2284 } else {
2285 tilt = 0;
2286
2287 switch (mCalibration.orientationCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002288 case Calibration::OrientationCalibration::INTERPOLATED:
Prabir Pradhane2e10b42022-11-17 20:59:36 +00002289 orientation = transformAngle(mRawRotation, in.orientation * mOrientationScale);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002290 break;
Michael Wright227c5542020-07-02 18:30:52 +01002291 case Calibration::OrientationCalibration::VECTOR: {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002292 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2293 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2294 if (c1 != 0 || c2 != 0) {
Prabir Pradhane2e10b42022-11-17 20:59:36 +00002295 orientation = transformAngle(mRawRotation, atan2f(c1, c2) * 0.5f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002296 float confidence = hypotf(c1, c2);
2297 float scale = 1.0f + confidence / 16.0f;
2298 touchMajor *= scale;
2299 touchMinor /= scale;
2300 toolMajor *= scale;
2301 toolMinor /= scale;
2302 } else {
2303 orientation = 0;
2304 }
2305 break;
2306 }
2307 default:
2308 orientation = 0;
2309 }
2310 }
2311
2312 // Distance
2313 float distance;
2314 switch (mCalibration.distanceCalibration) {
Michael Wright227c5542020-07-02 18:30:52 +01002315 case Calibration::DistanceCalibration::SCALED:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002316 distance = in.distance * mDistanceScale;
2317 break;
2318 default:
2319 distance = 0;
2320 }
2321
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002322 // Adjust X,Y coords for device calibration and convert to the natural display coordinates.
2323 vec2 transformed = {in.x, in.y};
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002324 mAffineTransform.applyTo(transformed.x /*byRef*/, transformed.y /*byRef*/);
2325 transformed = mRawToDisplay.transform(transformed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002326
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002327 // Write output coords.
2328 PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
2329 out.clear();
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002330 out.setAxisValue(AMOTION_EVENT_AXIS_X, transformed.x);
2331 out.setAxisValue(AMOTION_EVENT_AXIS_Y, transformed.y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002332 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2333 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2334 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2335 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2336 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
2337 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
2338 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Prabir Pradhan64fd5202022-11-30 19:45:11 +00002339 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2340 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002341
Chris Ye364fdb52020-08-05 15:07:56 -07002342 // Write output relative fields if applicable.
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002343 uint32_t id = in.id;
2344 if (mSource == AINPUT_SOURCE_TOUCHPAD &&
2345 mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) {
2346 const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00002347 float dx = transformed.x - p.getAxisValue(AMOTION_EVENT_AXIS_X);
2348 float dy = transformed.y - p.getAxisValue(AMOTION_EVENT_AXIS_Y);
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002349 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx);
2350 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy);
2351 }
2352
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002353 // Write output properties.
2354 PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002355 properties.clear();
2356 properties.id = id;
2357 properties.toolType = in.toolType;
2358
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002359 // Write id index and mark id as valid.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002360 mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
Nathaniel R. Lewisadb58ea2019-08-21 04:46:29 +00002361 mCurrentCookedState.cookedPointerData.validIdBits.markBit(id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002362 }
2363}
2364
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002365std::list<NotifyArgs> TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime,
2366 uint32_t policyFlags,
2367 PointerUsage pointerUsage) {
2368 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002369 if (pointerUsage != mPointerUsage) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002370 out += abortPointerUsage(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002371 mPointerUsage = pointerUsage;
2372 }
2373
2374 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002375 case PointerUsage::GESTURES:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002376 out += dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002377 break;
Michael Wright227c5542020-07-02 18:30:52 +01002378 case PointerUsage::STYLUS:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002379 out += dispatchPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002380 break;
Michael Wright227c5542020-07-02 18:30:52 +01002381 case PointerUsage::MOUSE:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002382 out += dispatchPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002383 break;
Michael Wright227c5542020-07-02 18:30:52 +01002384 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002385 break;
2386 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002387 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002388}
2389
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002390std::list<NotifyArgs> TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime,
2391 uint32_t policyFlags) {
2392 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002393 switch (mPointerUsage) {
Michael Wright227c5542020-07-02 18:30:52 +01002394 case PointerUsage::GESTURES:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002395 out += abortPointerGestures(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002396 break;
Michael Wright227c5542020-07-02 18:30:52 +01002397 case PointerUsage::STYLUS:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002398 out += abortPointerStylus(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002399 break;
Michael Wright227c5542020-07-02 18:30:52 +01002400 case PointerUsage::MOUSE:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002401 out += abortPointerMouse(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002402 break;
Michael Wright227c5542020-07-02 18:30:52 +01002403 case PointerUsage::NONE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002404 break;
2405 }
2406
Michael Wright227c5542020-07-02 18:30:52 +01002407 mPointerUsage = PointerUsage::NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002408 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002409}
2410
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002411std::list<NotifyArgs> TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime,
2412 uint32_t policyFlags,
2413 bool isTimeout) {
2414 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002415 // Update current gesture coordinates.
2416 bool cancelPreviousGesture, finishPreviousGesture;
2417 bool sendEvents =
2418 preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
2419 if (!sendEvents) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002420 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002421 }
2422 if (finishPreviousGesture) {
2423 cancelPreviousGesture = false;
2424 }
2425
2426 // Update the pointer presentation and spots.
Michael Wright227c5542020-07-02 18:30:52 +01002427 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002428 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002429 if (finishPreviousGesture || cancelPreviousGesture) {
2430 mPointerController->clearSpots();
2431 }
2432
Michael Wright227c5542020-07-02 18:30:52 +01002433 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00002434 mPointerController->setSpots(mPointerGesture.currentGestureCoords.cbegin(),
2435 mPointerGesture.currentGestureIdToIndex.cbegin(),
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002436 mPointerGesture.currentGestureIdBits,
2437 mPointerController->getDisplayId());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002438 }
2439 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002440 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002441 }
2442
2443 // Show or hide the pointer if needed.
2444 switch (mPointerGesture.currentGestureMode) {
Michael Wright227c5542020-07-02 18:30:52 +01002445 case PointerGesture::Mode::NEUTRAL:
2446 case PointerGesture::Mode::QUIET:
2447 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH &&
2448 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002449 // Remind the user of where the pointer is after finishing a gesture with spots.
Michael Wrightca5bede2020-07-02 00:00:29 +01002450 mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002451 }
2452 break;
Michael Wright227c5542020-07-02 18:30:52 +01002453 case PointerGesture::Mode::TAP:
2454 case PointerGesture::Mode::TAP_DRAG:
2455 case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG:
2456 case PointerGesture::Mode::HOVER:
2457 case PointerGesture::Mode::PRESS:
2458 case PointerGesture::Mode::SWIPE:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002459 // Unfade the pointer when the current gesture manipulates the
2460 // area directly under the pointer.
Michael Wrightca5bede2020-07-02 00:00:29 +01002461 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002462 break;
Michael Wright227c5542020-07-02 18:30:52 +01002463 case PointerGesture::Mode::FREEFORM:
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002464 // Fade the pointer when the current gesture manipulates a different
2465 // area and there are spots to guide the user experience.
Michael Wright227c5542020-07-02 18:30:52 +01002466 if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002467 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002468 } else {
Michael Wrightca5bede2020-07-02 00:00:29 +01002469 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002470 }
2471 break;
2472 }
2473
2474 // Send events!
2475 int32_t metaState = getContext()->getGlobalMetaState();
2476 int32_t buttonState = mCurrentCookedState.buttonState;
Harry Cutts2800fb02022-09-15 13:49:23 +00002477 const MotionClassification classification =
2478 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE
2479 ? MotionClassification::TWO_FINGER_SWIPE
2480 : MotionClassification::NONE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002481
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08002482 uint32_t flags = 0;
2483
2484 if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) {
2485 flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
2486 }
2487
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002488 // Update last coordinates of pointers that have moved so that we observe the new
2489 // pointer positions at the same time as other pointers that have just gone up.
Michael Wright227c5542020-07-02 18:30:52 +01002490 bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP ||
2491 mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG ||
2492 mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG ||
2493 mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
2494 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE ||
2495 mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002496 bool moveNeeded = false;
2497 if (down && !cancelPreviousGesture && !finishPreviousGesture &&
2498 !mPointerGesture.lastGestureIdBits.isEmpty() &&
2499 !mPointerGesture.currentGestureIdBits.isEmpty()) {
2500 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2501 mPointerGesture.lastGestureIdBits.value);
2502 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
2503 mPointerGesture.currentGestureCoords,
2504 mPointerGesture.currentGestureIdToIndex,
2505 mPointerGesture.lastGestureProperties,
2506 mPointerGesture.lastGestureCoords,
2507 mPointerGesture.lastGestureIdToIndex, movedGestureIdBits);
2508 if (buttonState != mLastCookedState.buttonState) {
2509 moveNeeded = true;
2510 }
2511 }
2512
2513 // Send motion events for all pointers that went up or were canceled.
2514 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
2515 if (!dispatchedGestureIdBits.isEmpty()) {
2516 if (cancelPreviousGesture) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002517 const uint32_t cancelFlags = flags | AMOTION_EVENT_FLAG_CANCELED;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002518 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002519 AMOTION_EVENT_ACTION_CANCEL, 0, cancelFlags, metaState,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002520 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2521 mPointerGesture.lastGestureProperties,
2522 mPointerGesture.lastGestureCoords,
2523 mPointerGesture.lastGestureIdToIndex,
2524 dispatchedGestureIdBits, -1, 0, 0,
2525 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002526
2527 dispatchedGestureIdBits.clear();
2528 } else {
2529 BitSet32 upGestureIdBits;
2530 if (finishPreviousGesture) {
2531 upGestureIdBits = dispatchedGestureIdBits;
2532 } else {
2533 upGestureIdBits.value =
2534 dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value;
2535 }
2536 while (!upGestureIdBits.isEmpty()) {
2537 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
2538
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002539 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2540 AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState,
2541 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2542 mPointerGesture.lastGestureProperties,
2543 mPointerGesture.lastGestureCoords,
2544 mPointerGesture.lastGestureIdToIndex,
2545 dispatchedGestureIdBits, id, 0, 0,
2546 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002547
2548 dispatchedGestureIdBits.clearBit(id);
2549 }
2550 }
2551 }
2552
2553 // Send motion events for all pointers that moved.
2554 if (moveNeeded) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002555 out.push_back(
2556 dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0,
2557 flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2558 mPointerGesture.currentGestureProperties,
2559 mPointerGesture.currentGestureCoords,
2560 mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1,
2561 0, 0, mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002562 }
2563
2564 // Send motion events for all pointers that went down.
2565 if (down) {
2566 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value &
2567 ~dispatchedGestureIdBits.value);
2568 while (!downGestureIdBits.isEmpty()) {
2569 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
2570 dispatchedGestureIdBits.markBit(id);
2571
2572 if (dispatchedGestureIdBits.count() == 1) {
2573 mPointerGesture.downTime = when;
2574 }
2575
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002576 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2577 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, flags, metaState,
2578 buttonState, 0, mPointerGesture.currentGestureProperties,
2579 mPointerGesture.currentGestureCoords,
2580 mPointerGesture.currentGestureIdToIndex,
2581 dispatchedGestureIdBits, id, 0, 0,
2582 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002583 }
2584 }
2585
2586 // Send motion events for hover.
Michael Wright227c5542020-07-02 18:30:52 +01002587 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002588 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
2589 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState,
2590 buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2591 mPointerGesture.currentGestureProperties,
2592 mPointerGesture.currentGestureCoords,
2593 mPointerGesture.currentGestureIdToIndex,
2594 mPointerGesture.currentGestureIdBits, -1, 0, 0,
2595 mPointerGesture.downTime, MotionClassification::NONE));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002596 } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) {
2597 // Synthesize a hover move event after all pointers go up to indicate that
2598 // the pointer is hovering again even if the user is not currently touching
2599 // the touch pad. This ensures that a view will receive a fresh hover enter
2600 // event after a tap.
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002601 float x, y;
2602 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002603
2604 PointerProperties pointerProperties;
2605 pointerProperties.clear();
2606 pointerProperties.id = 0;
2607 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2608
2609 PointerCoords pointerCoords;
2610 pointerCoords.clear();
2611 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2612 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2613
2614 const int32_t displayId = mPointerController->getDisplayId();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002615 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
2616 mSource, displayId, policyFlags,
2617 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState,
2618 buttonState, MotionClassification::NONE,
2619 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
2620 &pointerCoords, 0, 0, x, y, mPointerGesture.downTime,
2621 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002622 }
2623
2624 // Update state.
2625 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
2626 if (!down) {
2627 mPointerGesture.lastGestureIdBits.clear();
2628 } else {
2629 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
2630 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) {
2631 uint32_t id = idBits.clearFirstMarkedBit();
2632 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2633 mPointerGesture.lastGestureProperties[index].copyFrom(
2634 mPointerGesture.currentGestureProperties[index]);
2635 mPointerGesture.lastGestureCoords[index].copyFrom(
2636 mPointerGesture.currentGestureCoords[index]);
2637 mPointerGesture.lastGestureIdToIndex[id] = index;
2638 }
2639 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002640 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002641}
2642
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002643std::list<NotifyArgs> TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime,
2644 uint32_t policyFlags) {
Harry Cutts2800fb02022-09-15 13:49:23 +00002645 const MotionClassification classification =
2646 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE
2647 ? MotionClassification::TWO_FINGER_SWIPE
2648 : MotionClassification::NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002649 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002650 // Cancel previously dispatches pointers.
2651 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
2652 int32_t metaState = getContext()->getGlobalMetaState();
2653 int32_t buttonState = mCurrentRawState.buttonState;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002654 out.push_back(dispatchMotion(when, readTime, policyFlags, mSource,
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002655 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
2656 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002657 mPointerGesture.lastGestureProperties,
2658 mPointerGesture.lastGestureCoords,
2659 mPointerGesture.lastGestureIdToIndex,
2660 mPointerGesture.lastGestureIdBits, -1, 0, 0,
2661 mPointerGesture.downTime, classification));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002662 }
2663
2664 // Reset the current pointer gesture.
2665 mPointerGesture.reset();
2666 mPointerVelocityControl.reset();
2667
2668 // Remove any current spots.
2669 if (mPointerController != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +01002670 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002671 mPointerController->clearSpots();
2672 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002673 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002674}
2675
2676bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture,
2677 bool* outFinishPreviousGesture, bool isTimeout) {
2678 *outCancelPreviousGesture = false;
2679 *outFinishPreviousGesture = false;
2680
2681 // Handle TAP timeout.
2682 if (isTimeout) {
Harry Cutts45483602022-08-24 14:36:48 +00002683 ALOGD_IF(DEBUG_GESTURES, "Gestures: Processing timeout");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002684
Michael Wright227c5542020-07-02 18:30:52 +01002685 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002686 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
2687 // The tap/drag timeout has not yet expired.
2688 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime +
2689 mConfig.pointerGestureTapDragInterval);
2690 } else {
2691 // The tap is finished.
Harry Cutts45483602022-08-24 14:36:48 +00002692 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP finished");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002693 *outFinishPreviousGesture = true;
2694
2695 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002696 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002697 mPointerGesture.currentGestureIdBits.clear();
2698
2699 mPointerVelocityControl.reset();
2700 return true;
2701 }
2702 }
2703
2704 // We did not handle this timeout.
2705 return false;
2706 }
2707
2708 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
2709 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
2710
2711 // Update the velocity tracker.
2712 {
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05002713 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002714 uint32_t id = idBits.clearFirstMarkedBit();
2715 const RawPointerData::Pointer& pointer =
2716 mCurrentRawState.rawPointerData.pointerForId(id);
Siarhei Vishniakou8d232032023-01-11 08:17:21 -08002717 const float x = pointer.x * mPointerXMovementScale;
2718 const float y = pointer.y * mPointerYMovementScale;
2719 mPointerGesture.velocityTracker.addMovement(when, id, AMOTION_EVENT_AXIS_X, x);
2720 mPointerGesture.velocityTracker.addMovement(when, id, AMOTION_EVENT_AXIS_Y, y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002721 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002722 }
2723
2724 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
2725 // to NEUTRAL, then we should not generate tap event.
Michael Wright227c5542020-07-02 18:30:52 +01002726 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER &&
2727 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP &&
2728 mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002729 mPointerGesture.resetTap();
2730 }
2731
2732 // Pick a new active touch id if needed.
2733 // Choose an arbitrary pointer that just went down, if there is one.
2734 // Otherwise choose an arbitrary remaining pointer.
2735 // This guarantees we always have an active touch id when there is at least one pointer.
2736 // We keep the same active touch id for as long as possible.
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002737 if (mPointerGesture.activeTouchId < 0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002738 if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002739 mPointerGesture.activeTouchId = mCurrentCookedState.fingerIdBits.firstMarkedBit();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002740 mPointerGesture.firstTouchTime = when;
2741 }
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002742 } else if (!mCurrentCookedState.fingerIdBits.hasBit(mPointerGesture.activeTouchId)) {
2743 mPointerGesture.activeTouchId = !mCurrentCookedState.fingerIdBits.isEmpty()
2744 ? mCurrentCookedState.fingerIdBits.firstMarkedBit()
2745 : -1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002746 }
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002747 const int32_t& activeTouchId = mPointerGesture.activeTouchId;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002748
2749 // Switch states based on button and pointer state.
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002750 if (checkForTouchpadQuietTime(when)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002751 // Case 1: Quiet time. (QUIET)
Harry Cutts45483602022-08-24 14:36:48 +00002752 ALOGD_IF(DEBUG_GESTURES, "Gestures: QUIET for next %0.3fms",
2753 (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) *
2754 0.000001f);
Michael Wright227c5542020-07-02 18:30:52 +01002755 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002756 *outFinishPreviousGesture = true;
2757 }
2758
2759 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002760 mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002761 mPointerGesture.currentGestureIdBits.clear();
2762
2763 mPointerVelocityControl.reset();
2764 } else if (isPointerDown(mCurrentRawState.buttonState)) {
2765 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
2766 // The pointer follows the active touch point.
2767 // Emit DOWN, MOVE, UP events at the pointer location.
2768 //
2769 // Only the active touch matters; other fingers are ignored. This policy helps
2770 // to handle the case where the user places a second finger on the touch pad
2771 // to apply the necessary force to depress an integrated button below the surface.
2772 // We don't want the second finger to be delivered to applications.
2773 //
2774 // For this to work well, we need to make sure to track the pointer that is really
2775 // active. If the user first puts one finger down to click then adds another
2776 // finger to drag then the active pointer should switch to the finger that is
2777 // being dragged.
Harry Cutts45483602022-08-24 14:36:48 +00002778 ALOGD_IF(DEBUG_GESTURES,
2779 "Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, currentFingerCount=%d",
2780 activeTouchId, currentFingerCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002781 // Reset state when just starting.
Michael Wright227c5542020-07-02 18:30:52 +01002782 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002783 *outFinishPreviousGesture = true;
2784 mPointerGesture.activeGestureId = 0;
2785 }
2786
2787 // Switch pointers if needed.
2788 // Find the fastest pointer and follow it.
2789 if (activeTouchId >= 0 && currentFingerCount > 1) {
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002790 const auto [bestId, bestSpeed] = getFastestFinger();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002791 if (bestId >= 0 && bestId != activeTouchId) {
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002792 mPointerGesture.activeTouchId = bestId;
Harry Cutts45483602022-08-24 14:36:48 +00002793 ALOGD_IF(DEBUG_GESTURES,
2794 "Gestures: BUTTON_CLICK_OR_DRAG switched pointers, bestId=%d, "
2795 "bestSpeed=%0.3f",
2796 bestId, bestSpeed);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002797 }
2798 }
2799
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002800 if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002801 // When using spots, the click will occur at the position of the anchor
2802 // spot and all other spots will move there.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002803 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002804 } else {
2805 mPointerVelocityControl.reset();
2806 }
2807
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002808 float x, y;
2809 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002810
Michael Wright227c5542020-07-02 18:30:52 +01002811 mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002812 mPointerGesture.currentGestureIdBits.clear();
2813 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2814 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2815 mPointerGesture.currentGestureProperties[0].clear();
2816 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2817 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2818 mPointerGesture.currentGestureCoords[0].clear();
2819 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2820 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2821 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2822 } else if (currentFingerCount == 0) {
2823 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Michael Wright227c5542020-07-02 18:30:52 +01002824 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002825 *outFinishPreviousGesture = true;
2826 }
2827
2828 // Watch for taps coming out of HOVER or TAP_DRAG mode.
2829 // Checking for taps after TAP_DRAG allows us to detect double-taps.
2830 bool tapped = false;
Michael Wright227c5542020-07-02 18:30:52 +01002831 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER ||
2832 mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002833 lastFingerCount == 1) {
2834 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002835 float x, y;
2836 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002837 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2838 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Harry Cutts45483602022-08-24 14:36:48 +00002839 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002840
2841 mPointerGesture.tapUpTime = when;
2842 getContext()->requestTimeoutAtTime(when +
2843 mConfig.pointerGestureTapDragInterval);
2844
2845 mPointerGesture.activeGestureId = 0;
Michael Wright227c5542020-07-02 18:30:52 +01002846 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002847 mPointerGesture.currentGestureIdBits.clear();
2848 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2849 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2850 mPointerGesture.currentGestureProperties[0].clear();
2851 mPointerGesture.currentGestureProperties[0].id =
2852 mPointerGesture.activeGestureId;
2853 mPointerGesture.currentGestureProperties[0].toolType =
2854 AMOTION_EVENT_TOOL_TYPE_FINGER;
2855 mPointerGesture.currentGestureCoords[0].clear();
2856 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2857 mPointerGesture.tapX);
2858 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
2859 mPointerGesture.tapY);
2860 mPointerGesture.currentGestureCoords[0]
2861 .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
2862
2863 tapped = true;
2864 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002865 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP, deltaX=%f, deltaY=%f",
2866 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002867 }
2868 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002869 if (DEBUG_GESTURES) {
2870 if (mPointerGesture.tapDownTime != LLONG_MIN) {
2871 ALOGD("Gestures: Not a TAP, %0.3fms since down",
2872 (when - mPointerGesture.tapDownTime) * 0.000001f);
2873 } else {
2874 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
2875 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002876 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002877 }
2878 }
2879
2880 mPointerVelocityControl.reset();
2881
2882 if (!tapped) {
Harry Cutts45483602022-08-24 14:36:48 +00002883 ALOGD_IF(DEBUG_GESTURES, "Gestures: NEUTRAL");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002884 mPointerGesture.activeGestureId = -1;
Michael Wright227c5542020-07-02 18:30:52 +01002885 mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002886 mPointerGesture.currentGestureIdBits.clear();
2887 }
2888 } else if (currentFingerCount == 1) {
2889 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
2890 // The pointer follows the active touch point.
2891 // When in HOVER, emit HOVER_MOVE events at the pointer location.
2892 // When in TAP_DRAG, emit MOVE events at the pointer location.
2893 ALOG_ASSERT(activeTouchId >= 0);
2894
Michael Wright227c5542020-07-02 18:30:52 +01002895 mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER;
2896 if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002897 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002898 float x, y;
2899 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002900 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop &&
2901 fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Michael Wright227c5542020-07-02 18:30:52 +01002902 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002903 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002904 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
2905 x - mPointerGesture.tapX, y - mPointerGesture.tapY);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002906 }
2907 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002908 ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, %0.3fms time since up",
2909 (when - mPointerGesture.tapUpTime) * 0.000001f);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002910 }
Michael Wright227c5542020-07-02 18:30:52 +01002911 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) {
2912 mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002913 }
2914
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002915 if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002916 // When using spots, the hover or drag will occur at the position of the anchor spot.
Harry Cutts714d1ad2022-08-24 16:36:43 +00002917 moveMousePointerFromPointerDelta(when, activeTouchId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002918 } else {
2919 mPointerVelocityControl.reset();
2920 }
2921
2922 bool down;
Michael Wright227c5542020-07-02 18:30:52 +01002923 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) {
Harry Cutts45483602022-08-24 14:36:48 +00002924 ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP_DRAG");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002925 down = true;
2926 } else {
Harry Cutts45483602022-08-24 14:36:48 +00002927 ALOGD_IF(DEBUG_GESTURES, "Gestures: HOVER");
Michael Wright227c5542020-07-02 18:30:52 +01002928 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002929 *outFinishPreviousGesture = true;
2930 }
2931 mPointerGesture.activeGestureId = 0;
2932 down = false;
2933 }
2934
Prabir Pradhande69f8a2021-11-18 16:40:34 +00002935 float x, y;
2936 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002937
2938 mPointerGesture.currentGestureIdBits.clear();
2939 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
2940 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
2941 mPointerGesture.currentGestureProperties[0].clear();
2942 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
2943 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
2944 mPointerGesture.currentGestureCoords[0].clear();
2945 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
2946 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2947 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
2948 down ? 1.0f : 0.0f);
2949
2950 if (lastFingerCount == 0 && currentFingerCount != 0) {
2951 mPointerGesture.resetTap();
2952 mPointerGesture.tapDownTime = when;
2953 mPointerGesture.tapX = x;
2954 mPointerGesture.tapY = y;
2955 }
2956 } else {
2957 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002958 prepareMultiFingerPointerGestures(when, outCancelPreviousGesture, outFinishPreviousGesture);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002959 }
2960
2961 mPointerController->setButtonState(mCurrentRawState.buttonState);
2962
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08002963 if (DEBUG_GESTURES) {
2964 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
2965 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
2966 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
2967 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
2968 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
2969 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
2970 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) {
2971 uint32_t id = idBits.clearFirstMarkedBit();
2972 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
2973 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
2974 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
2975 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
2976 "x=%0.3f, y=%0.3f, pressure=%0.3f",
2977 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
2978 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
2979 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
2980 }
2981 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) {
2982 uint32_t id = idBits.clearFirstMarkedBit();
2983 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
2984 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
2985 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
2986 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
2987 "x=%0.3f, y=%0.3f, pressure=%0.3f",
2988 id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
2989 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
2990 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
2991 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002992 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07002993 return true;
2994}
2995
Harry Cuttsbea6ce52022-10-14 15:17:30 +00002996bool TouchInputMapper::checkForTouchpadQuietTime(nsecs_t when) {
2997 if (mPointerGesture.activeTouchId < 0) {
2998 mPointerGesture.resetQuietTime();
2999 return false;
3000 }
3001
3002 if (when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval) {
3003 return true;
3004 }
3005
3006 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
3007 bool isQuietTime = false;
3008 if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS ||
3009 mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE ||
3010 mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) &&
3011 currentFingerCount < 2) {
3012 // Enter quiet time when exiting swipe or freeform state.
3013 // This is to prevent accidentally entering the hover state and flinging the
3014 // pointer when finishing a swipe and there is still one pointer left onscreen.
3015 isQuietTime = true;
3016 } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG &&
3017 currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) {
3018 // Enter quiet time when releasing the button and there are still two or more
3019 // fingers down. This may indicate that one finger was used to press the button
3020 // but it has not gone up yet.
3021 isQuietTime = true;
3022 }
3023 if (isQuietTime) {
3024 mPointerGesture.quietTime = when;
3025 }
3026 return isQuietTime;
3027}
3028
3029std::pair<int32_t, float> TouchInputMapper::getFastestFinger() {
3030 int32_t bestId = -1;
3031 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
3032 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
3033 uint32_t id = idBits.clearFirstMarkedBit();
3034 std::optional<float> vx =
3035 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, id);
3036 std::optional<float> vy =
3037 mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, id);
3038 if (vx && vy) {
3039 float speed = hypotf(*vx, *vy);
3040 if (speed > bestSpeed) {
3041 bestId = id;
3042 bestSpeed = speed;
3043 }
3044 }
3045 }
3046 return std::make_pair(bestId, bestSpeed);
3047}
3048
3049void TouchInputMapper::prepareMultiFingerPointerGestures(nsecs_t when, bool* cancelPreviousGesture,
3050 bool* finishPreviousGesture) {
3051 // We need to provide feedback for each finger that goes down so we cannot wait for the fingers
3052 // to move before deciding what to do.
3053 //
3054 // The ambiguous case is deciding what to do when there are two fingers down but they have not
3055 // moved enough to determine whether they are part of a drag or part of a freeform gesture, or
3056 // just a press or long-press at the pointer location.
3057 //
3058 // When there are two fingers we start with the PRESS hypothesis and we generate a down at the
3059 // pointer location.
3060 //
3061 // When the two fingers move enough or when additional fingers are added, we make a decision to
3062 // transition into SWIPE or FREEFORM mode accordingly.
3063 const int32_t activeTouchId = mPointerGesture.activeTouchId;
3064 ALOG_ASSERT(activeTouchId >= 0);
3065
3066 const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
3067 const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
3068 bool settled =
3069 when >= mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval;
3070 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS &&
3071 mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE &&
3072 mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
3073 *finishPreviousGesture = true;
3074 } else if (!settled && currentFingerCount > lastFingerCount) {
3075 // Additional pointers have gone down but not yet settled.
3076 // Reset the gesture.
3077 ALOGD_IF(DEBUG_GESTURES,
3078 "Gestures: Resetting gesture since additional pointers went down for "
3079 "MULTITOUCH, settle time remaining %0.3fms",
3080 (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval -
3081 when) * 0.000001f);
3082 *cancelPreviousGesture = true;
3083 } else {
3084 // Continue previous gesture.
3085 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3086 }
3087
3088 if (*finishPreviousGesture || *cancelPreviousGesture) {
3089 mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS;
3090 mPointerGesture.activeGestureId = 0;
3091 mPointerGesture.referenceIdBits.clear();
3092 mPointerVelocityControl.reset();
3093
3094 // Use the centroid and pointer location as the reference points for the gesture.
3095 ALOGD_IF(DEBUG_GESTURES,
3096 "Gestures: Using centroid as reference for MULTITOUCH, settle time remaining "
3097 "%0.3fms",
3098 (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval -
3099 when) * 0.000001f);
3100 mCurrentRawState.rawPointerData
3101 .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX,
3102 &mPointerGesture.referenceTouchY);
3103 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3104 &mPointerGesture.referenceGestureY);
3105 }
3106
3107 // Clear the reference deltas for fingers not yet included in the reference calculation.
3108 for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value &
3109 ~mPointerGesture.referenceIdBits.value);
3110 !idBits.isEmpty();) {
3111 uint32_t id = idBits.clearFirstMarkedBit();
3112 mPointerGesture.referenceDeltas[id].dx = 0;
3113 mPointerGesture.referenceDeltas[id].dy = 0;
3114 }
3115 mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
3116
3117 // Add delta for all fingers and calculate a common movement delta.
3118 int32_t commonDeltaRawX = 0, commonDeltaRawY = 0;
3119 BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value &
3120 mCurrentCookedState.fingerIdBits.value);
3121 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) {
3122 bool first = (idBits == commonIdBits);
3123 uint32_t id = idBits.clearFirstMarkedBit();
3124 const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
3125 const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
3126 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3127 delta.dx += cpd.x - lpd.x;
3128 delta.dy += cpd.y - lpd.y;
3129
3130 if (first) {
3131 commonDeltaRawX = delta.dx;
3132 commonDeltaRawY = delta.dy;
3133 } else {
3134 commonDeltaRawX = calculateCommonVector(commonDeltaRawX, delta.dx);
3135 commonDeltaRawY = calculateCommonVector(commonDeltaRawY, delta.dy);
3136 }
3137 }
3138
3139 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
3140 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) {
3141 float dist[MAX_POINTER_ID + 1];
3142 int32_t distOverThreshold = 0;
3143 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3144 uint32_t id = idBits.clearFirstMarkedBit();
3145 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3146 dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale);
3147 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
3148 distOverThreshold += 1;
3149 }
3150 }
3151
3152 // Only transition when at least two pointers have moved further than
3153 // the minimum distance threshold.
3154 if (distOverThreshold >= 2) {
3155 if (currentFingerCount > 2) {
3156 // There are more than two pointers, switch to FREEFORM.
3157 ALOGD_IF(DEBUG_GESTURES,
3158 "Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3159 currentFingerCount);
3160 *cancelPreviousGesture = true;
3161 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3162 } else {
3163 // There are exactly two pointers.
3164 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3165 uint32_t id1 = idBits.clearFirstMarkedBit();
3166 uint32_t id2 = idBits.firstMarkedBit();
3167 const RawPointerData::Pointer& p1 =
3168 mCurrentRawState.rawPointerData.pointerForId(id1);
3169 const RawPointerData::Pointer& p2 =
3170 mCurrentRawState.rawPointerData.pointerForId(id2);
3171 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
3172 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
3173 // There are two pointers but they are too far apart for a SWIPE,
3174 // switch to FREEFORM.
3175 ALOGD_IF(DEBUG_GESTURES,
3176 "Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
3177 mutualDistance, mPointerGestureMaxSwipeWidth);
3178 *cancelPreviousGesture = true;
3179 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3180 } else {
3181 // There are two pointers. Wait for both pointers to start moving
3182 // before deciding whether this is a SWIPE or FREEFORM gesture.
3183 float dist1 = dist[id1];
3184 float dist2 = dist[id2];
3185 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance &&
3186 dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
3187 // Calculate the dot product of the displacement vectors.
3188 // When the vectors are oriented in approximately the same direction,
3189 // the angle betweeen them is near zero and the cosine of the angle
3190 // approaches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) *
3191 // mag(v2).
3192 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
3193 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
3194 float dx1 = delta1.dx * mPointerXZoomScale;
3195 float dy1 = delta1.dy * mPointerYZoomScale;
3196 float dx2 = delta2.dx * mPointerXZoomScale;
3197 float dy2 = delta2.dy * mPointerYZoomScale;
3198 float dot = dx1 * dx2 + dy1 * dy2;
3199 float cosine = dot / (dist1 * dist2); // denominator always > 0
3200 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
3201 // Pointers are moving in the same direction. Switch to SWIPE.
3202 ALOGD_IF(DEBUG_GESTURES,
3203 "Gestures: PRESS transitioned to SWIPE, "
3204 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3205 "cosine %0.3f >= %0.3f",
3206 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3207 mConfig.pointerGestureMultitouchMinDistance, cosine,
3208 mConfig.pointerGestureSwipeTransitionAngleCosine);
3209 mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE;
3210 } else {
3211 // Pointers are moving in different directions. Switch to FREEFORM.
3212 ALOGD_IF(DEBUG_GESTURES,
3213 "Gestures: PRESS transitioned to FREEFORM, "
3214 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
3215 "cosine %0.3f < %0.3f",
3216 dist1, mConfig.pointerGestureMultitouchMinDistance, dist2,
3217 mConfig.pointerGestureMultitouchMinDistance, cosine,
3218 mConfig.pointerGestureSwipeTransitionAngleCosine);
3219 *cancelPreviousGesture = true;
3220 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3221 }
3222 }
3223 }
3224 }
3225 }
3226 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
3227 // Switch from SWIPE to FREEFORM if additional pointers go down.
3228 // Cancel previous gesture.
3229 if (currentFingerCount > 2) {
3230 ALOGD_IF(DEBUG_GESTURES,
3231 "Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3232 currentFingerCount);
3233 *cancelPreviousGesture = true;
3234 mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM;
3235 }
3236 }
3237
3238 // Move the reference points based on the overall group motion of the fingers
3239 // except in PRESS mode while waiting for a transition to occur.
3240 if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS &&
3241 (commonDeltaRawX || commonDeltaRawY)) {
3242 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) {
3243 uint32_t id = idBits.clearFirstMarkedBit();
3244 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
3245 delta.dx = 0;
3246 delta.dy = 0;
3247 }
3248
3249 mPointerGesture.referenceTouchX += commonDeltaRawX;
3250 mPointerGesture.referenceTouchY += commonDeltaRawY;
3251
3252 float commonDeltaX = commonDeltaRawX * mPointerXMovementScale;
3253 float commonDeltaY = commonDeltaRawY * mPointerYMovementScale;
3254
3255 rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY);
3256 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
3257
3258 mPointerGesture.referenceGestureX += commonDeltaX;
3259 mPointerGesture.referenceGestureY += commonDeltaY;
3260 }
3261
3262 // Report gestures.
3263 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS ||
3264 mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
3265 // PRESS or SWIPE mode.
3266 ALOGD_IF(DEBUG_GESTURES,
3267 "Gestures: PRESS or SWIPE activeTouchId=%d, activeGestureId=%d, "
3268 "currentTouchPointerCount=%d",
3269 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3270 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3271
3272 mPointerGesture.currentGestureIdBits.clear();
3273 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3274 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3275 mPointerGesture.currentGestureProperties[0].clear();
3276 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3277 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3278 mPointerGesture.currentGestureCoords[0].clear();
3279 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3280 mPointerGesture.referenceGestureX);
3281 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
3282 mPointerGesture.referenceGestureY);
3283 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3284 if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) {
3285 float xOffset = static_cast<float>(commonDeltaRawX) /
3286 (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue);
3287 float yOffset = static_cast<float>(commonDeltaRawY) /
3288 (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue);
3289 mPointerGesture.currentGestureCoords[0]
3290 .setAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET, xOffset);
3291 mPointerGesture.currentGestureCoords[0]
3292 .setAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET, yOffset);
3293 }
3294 } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) {
3295 // FREEFORM mode.
3296 ALOGD_IF(DEBUG_GESTURES,
3297 "Gestures: FREEFORM activeTouchId=%d, activeGestureId=%d, "
3298 "currentTouchPointerCount=%d",
3299 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
3300 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
3301
3302 mPointerGesture.currentGestureIdBits.clear();
3303
3304 BitSet32 mappedTouchIdBits;
3305 BitSet32 usedGestureIdBits;
3306 if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) {
3307 // Initially, assign the active gesture id to the active touch point
3308 // if there is one. No other touch id bits are mapped yet.
3309 if (!*cancelPreviousGesture) {
3310 mappedTouchIdBits.markBit(activeTouchId);
3311 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
3312 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
3313 mPointerGesture.activeGestureId;
3314 } else {
3315 mPointerGesture.activeGestureId = -1;
3316 }
3317 } else {
3318 // Otherwise, assume we mapped all touches from the previous frame.
3319 // Reuse all mappings that are still applicable.
3320 mappedTouchIdBits.value =
3321 mLastCookedState.fingerIdBits.value & mCurrentCookedState.fingerIdBits.value;
3322 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
3323
3324 // Check whether we need to choose a new active gesture id because the
3325 // current went went up.
3326 for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value &
3327 ~mCurrentCookedState.fingerIdBits.value);
3328 !upTouchIdBits.isEmpty();) {
3329 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
3330 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
3331 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
3332 mPointerGesture.activeGestureId = -1;
3333 break;
3334 }
3335 }
3336 }
3337
3338 ALOGD_IF(DEBUG_GESTURES,
3339 "Gestures: FREEFORM follow up mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
3340 "activeGestureId=%d",
3341 mappedTouchIdBits.value, usedGestureIdBits.value, mPointerGesture.activeGestureId);
3342
3343 BitSet32 idBits(mCurrentCookedState.fingerIdBits);
3344 for (uint32_t i = 0; i < currentFingerCount; i++) {
3345 uint32_t touchId = idBits.clearFirstMarkedBit();
3346 uint32_t gestureId;
3347 if (!mappedTouchIdBits.hasBit(touchId)) {
3348 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
3349 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
3350 ALOGD_IF(DEBUG_GESTURES,
3351 "Gestures: FREEFORM new mapping for touch id %d -> gesture id %d", touchId,
3352 gestureId);
3353 } else {
3354 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
3355 ALOGD_IF(DEBUG_GESTURES,
3356 "Gestures: FREEFORM existing mapping for touch id %d -> gesture id %d",
3357 touchId, gestureId);
3358 }
3359 mPointerGesture.currentGestureIdBits.markBit(gestureId);
3360 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
3361
3362 const RawPointerData::Pointer& pointer =
3363 mCurrentRawState.rawPointerData.pointerForId(touchId);
3364 float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale;
3365 float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale;
3366 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
3367
3368 mPointerGesture.currentGestureProperties[i].clear();
3369 mPointerGesture.currentGestureProperties[i].id = gestureId;
3370 mPointerGesture.currentGestureProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
3371 mPointerGesture.currentGestureCoords[i].clear();
3372 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
3373 mPointerGesture.referenceGestureX +
3374 deltaX);
3375 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
3376 mPointerGesture.referenceGestureY +
3377 deltaY);
3378 mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3379 }
3380
3381 if (mPointerGesture.activeGestureId < 0) {
3382 mPointerGesture.activeGestureId = mPointerGesture.currentGestureIdBits.firstMarkedBit();
3383 ALOGD_IF(DEBUG_GESTURES, "Gestures: FREEFORM new activeGestureId=%d",
3384 mPointerGesture.activeGestureId);
3385 }
3386 }
3387}
3388
Harry Cutts714d1ad2022-08-24 16:36:43 +00003389void TouchInputMapper::moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId) {
3390 const RawPointerData::Pointer& currentPointer =
3391 mCurrentRawState.rawPointerData.pointerForId(pointerId);
3392 const RawPointerData::Pointer& lastPointer =
3393 mLastRawState.rawPointerData.pointerForId(pointerId);
3394 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
3395 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
3396
3397 rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY);
3398 mPointerVelocityControl.move(when, &deltaX, &deltaY);
3399
3400 mPointerController->move(deltaX, deltaY);
3401}
3402
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003403std::list<NotifyArgs> TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime,
3404 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003405 mPointerSimple.currentCoords.clear();
3406 mPointerSimple.currentProperties.clear();
3407
3408 bool down, hovering;
3409 if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
3410 uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
3411 uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003412 mPointerController
3413 ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(),
3414 mCurrentCookedState.cookedPointerData.pointerCoords[index].getY());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003415
3416 hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
3417 down = !hovering;
3418
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003419 float x, y;
3420 mPointerController->getPosition(&x, &y);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003421 mPointerSimple.currentCoords.copyFrom(
3422 mCurrentCookedState.cookedPointerData.pointerCoords[index]);
3423 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3424 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3425 mPointerSimple.currentProperties.id = 0;
3426 mPointerSimple.currentProperties.toolType =
3427 mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
3428 } else {
3429 down = false;
3430 hovering = false;
3431 }
3432
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003433 return dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003434}
3435
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003436std::list<NotifyArgs> TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime,
3437 uint32_t policyFlags) {
3438 return abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003439}
3440
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003441std::list<NotifyArgs> TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime,
3442 uint32_t policyFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003443 mPointerSimple.currentCoords.clear();
3444 mPointerSimple.currentProperties.clear();
3445
3446 bool down, hovering;
3447 if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
3448 uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003449 if (mLastCookedState.mouseIdBits.hasBit(id)) {
Harry Cutts714d1ad2022-08-24 16:36:43 +00003450 moveMousePointerFromPointerDelta(when, id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003451 } else {
3452 mPointerVelocityControl.reset();
3453 }
3454
3455 down = isPointerDown(mCurrentRawState.buttonState);
3456 hovering = !down;
3457
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003458 float x, y;
3459 mPointerController->getPosition(&x, &y);
Harry Cutts714d1ad2022-08-24 16:36:43 +00003460 uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003461 mPointerSimple.currentCoords.copyFrom(
3462 mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
3463 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3464 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3465 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3466 hovering ? 0.0f : 1.0f);
3467 mPointerSimple.currentProperties.id = 0;
3468 mPointerSimple.currentProperties.toolType =
3469 mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
3470 } else {
3471 mPointerVelocityControl.reset();
3472
3473 down = false;
3474 hovering = false;
3475 }
3476
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003477 return dispatchPointerSimple(when, readTime, policyFlags, down, hovering);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003478}
3479
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003480std::list<NotifyArgs> TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime,
3481 uint32_t policyFlags) {
3482 std::list<NotifyArgs> out = abortPointerSimple(when, readTime, policyFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003483
3484 mPointerVelocityControl.reset();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003485
3486 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003487}
3488
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003489std::list<NotifyArgs> TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime,
3490 uint32_t policyFlags, bool down,
3491 bool hovering) {
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00003492 LOG_ALWAYS_FATAL_IF(mDeviceMode != DeviceMode::POINTER,
3493 "%s cannot be used when the device is not in POINTER mode.", __func__);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003494 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003495 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003496
3497 if (down || hovering) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003498 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003499 mPointerController->clearSpots();
3500 mPointerController->setButtonState(mCurrentRawState.buttonState);
Michael Wrightca5bede2020-07-02 00:00:29 +01003501 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003502 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
Michael Wrightca5bede2020-07-02 00:00:29 +01003503 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003504 }
Garfield Tan9514d782020-11-10 16:37:23 -08003505 int32_t displayId = mPointerController->getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003506
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003507 float xCursorPosition, yCursorPosition;
3508 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003509
3510 if (mPointerSimple.down && !down) {
3511 mPointerSimple.down = false;
3512
3513 // Send up.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003514 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3515 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0,
3516 0, metaState, mLastRawState.buttonState,
3517 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3518 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3519 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3520 yCursorPosition, mPointerSimple.downTime,
3521 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003522 }
3523
3524 if (mPointerSimple.hovering && !hovering) {
3525 mPointerSimple.hovering = false;
3526
3527 // Send hover exit.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003528 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3529 mSource, displayId, policyFlags,
3530 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState,
3531 mLastRawState.buttonState, MotionClassification::NONE,
3532 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3533 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3534 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3535 yCursorPosition, mPointerSimple.downTime,
3536 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003537 }
3538
3539 if (down) {
3540 if (!mPointerSimple.down) {
3541 mPointerSimple.down = true;
3542 mPointerSimple.downTime = when;
3543
3544 // Send down.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003545 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3546 mSource, displayId, policyFlags,
3547 AMOTION_EVENT_ACTION_DOWN, 0, 0, metaState,
3548 mCurrentRawState.buttonState, MotionClassification::NONE,
3549 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3550 &mPointerSimple.currentProperties,
3551 &mPointerSimple.currentCoords, mOrientedXPrecision,
3552 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3553 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003554 }
3555
3556 // Send move.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003557 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3558 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE,
3559 0, 0, metaState, mCurrentRawState.buttonState,
3560 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3561 &mPointerSimple.currentProperties,
3562 &mPointerSimple.currentCoords, mOrientedXPrecision,
3563 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3564 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003565 }
3566
3567 if (hovering) {
3568 if (!mPointerSimple.hovering) {
3569 mPointerSimple.hovering = true;
3570
3571 // Send hover enter.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003572 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3573 mSource, displayId, policyFlags,
3574 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
3575 mCurrentRawState.buttonState, MotionClassification::NONE,
3576 AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3577 &mPointerSimple.currentProperties,
3578 &mPointerSimple.currentCoords, mOrientedXPrecision,
3579 mOrientedYPrecision, xCursorPosition, yCursorPosition,
3580 mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003581 }
3582
3583 // Send hover move.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003584 out.push_back(
3585 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
3586 displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
3587 metaState, mCurrentRawState.buttonState,
3588 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3589 &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
3590 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3591 yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003592 }
3593
3594 if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
3595 float vscroll = mCurrentRawState.rawVScroll;
3596 float hscroll = mCurrentRawState.rawHScroll;
3597 mWheelYVelocityControl.move(when, nullptr, &vscroll);
3598 mWheelXVelocityControl.move(when, &hscroll, nullptr);
3599
3600 // Send scroll.
3601 PointerCoords pointerCoords;
3602 pointerCoords.copyFrom(mPointerSimple.currentCoords);
3603 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
3604 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
3605
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003606 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3607 mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL,
3608 0, 0, metaState, mCurrentRawState.buttonState,
3609 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3610 &mPointerSimple.currentProperties, &pointerCoords,
3611 mOrientedXPrecision, mOrientedYPrecision, xCursorPosition,
3612 yCursorPosition, mPointerSimple.downTime,
3613 /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003614 }
3615
3616 // Save state.
3617 if (down || hovering) {
3618 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
3619 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00003620 mPointerSimple.displayId = displayId;
3621 mPointerSimple.source = mSource;
3622 mPointerSimple.lastCursorX = xCursorPosition;
3623 mPointerSimple.lastCursorY = yCursorPosition;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003624 } else {
3625 mPointerSimple.reset();
3626 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003627 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003628}
3629
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003630std::list<NotifyArgs> TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime,
3631 uint32_t policyFlags) {
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00003632 std::list<NotifyArgs> out;
3633 if (mPointerSimple.down || mPointerSimple.hovering) {
3634 int32_t metaState = getContext()->getGlobalMetaState();
3635 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
3636 mPointerSimple.source, mPointerSimple.displayId, policyFlags,
3637 AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED,
3638 metaState, mLastRawState.buttonState,
3639 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
3640 &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
3641 mOrientedXPrecision, mOrientedYPrecision,
3642 mPointerSimple.lastCursorX, mPointerSimple.lastCursorY,
3643 mPointerSimple.downTime,
3644 /* videoFrames */ {}));
3645 if (mPointerController != nullptr) {
3646 mPointerController->fade(PointerControllerInterface::Transition::GRADUAL);
3647 }
3648 }
3649 mPointerSimple.reset();
3650 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003651}
3652
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003653NotifyMotionArgs TouchInputMapper::dispatchMotion(
3654 nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action,
3655 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
Prabir Pradhand6ccedb2022-09-27 21:04:06 +00003656 int32_t edgeFlags, const PropertiesArray& properties, const CoordsArray& coords,
3657 const IdToIndexArray& idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003658 float yPrecision, nsecs_t downTime, MotionClassification classification) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003659 PointerCoords pointerCoords[MAX_POINTERS];
3660 PointerProperties pointerProperties[MAX_POINTERS];
3661 uint32_t pointerCount = 0;
3662 while (!idBits.isEmpty()) {
3663 uint32_t id = idBits.clearFirstMarkedBit();
3664 uint32_t index = idToIndex[id];
3665 pointerProperties[pointerCount].copyFrom(properties[index]);
3666 pointerCoords[pointerCount].copyFrom(coords[index]);
3667
3668 if (changedId >= 0 && id == uint32_t(changedId)) {
3669 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
3670 }
3671
3672 pointerCount += 1;
3673 }
3674
3675 ALOG_ASSERT(pointerCount != 0);
3676
3677 if (changedId >= 0 && pointerCount == 1) {
3678 // Replace initial down and final up action.
3679 // We can compare the action without masking off the changed pointer index
3680 // because we know the index is 0.
3681 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
3682 action = AMOTION_EVENT_ACTION_DOWN;
3683 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
arthurhungcc7f9802020-04-30 17:55:40 +08003684 if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) {
3685 action = AMOTION_EVENT_ACTION_CANCEL;
3686 } else {
3687 action = AMOTION_EVENT_ACTION_UP;
3688 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003689 } else {
3690 // Can't happen.
3691 ALOG_ASSERT(false);
3692 }
3693 }
3694 float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
3695 float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
Michael Wright227c5542020-07-02 18:30:52 +01003696 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhande69f8a2021-11-18 16:40:34 +00003697 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003698 }
3699 const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE);
3700 const int32_t deviceId = getDeviceId();
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08003701 std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003702 std::for_each(frames.begin(), frames.end(),
Prabir Pradhan1728b212021-10-19 16:00:03 -07003703 [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003704 return NotifyMotionArgs(getContext()->getNextId(), when, readTime, deviceId, source, displayId,
3705 policyFlags, action, actionButton, flags, metaState, buttonState,
3706 classification, edgeFlags, pointerCount, pointerProperties,
3707 pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition,
3708 downTime, std::move(frames));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003709}
3710
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003711std::list<NotifyArgs> TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) {
3712 std::list<NotifyArgs> out;
3713 out += abortPointerUsage(when, readTime, 0 /*policyFlags*/);
3714 out += abortTouches(when, readTime, 0 /* policyFlags*/);
3715 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003716}
3717
Prabir Pradhan1728b212021-10-19 16:00:03 -07003718bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003719 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003720 y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
Prabir Pradhan675f25a2022-11-10 22:04:07 +00003721 isPointInRect(mPhysicalFrameInRotatedDisplay, mRawToRotatedDisplay.transform(x, y));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003722}
3723
3724const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
3725 for (const VirtualKey& virtualKey : mVirtualKeys) {
Harry Cutts45483602022-08-24 14:36:48 +00003726 ALOGD_IF(DEBUG_VIRTUAL_KEYS,
3727 "VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
3728 "left=%d, top=%d, right=%d, bottom=%d",
3729 x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft,
3730 virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003731
3732 if (virtualKey.isHit(x, y)) {
3733 return &virtualKey;
3734 }
3735 }
3736
3737 return nullptr;
3738}
3739
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003740void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) {
3741 uint32_t currentPointerCount = current.rawPointerData.pointerCount;
3742 uint32_t lastPointerCount = last.rawPointerData.pointerCount;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003743
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003744 current.rawPointerData.clearIdBits();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003745
3746 if (currentPointerCount == 0) {
3747 // No pointers to assign.
3748 return;
3749 }
3750
3751 if (lastPointerCount == 0) {
3752 // All pointers are new.
3753 for (uint32_t i = 0; i < currentPointerCount; i++) {
3754 uint32_t id = i;
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003755 current.rawPointerData.pointers[i].id = id;
3756 current.rawPointerData.idToIndex[id] = i;
3757 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003758 }
3759 return;
3760 }
3761
3762 if (currentPointerCount == 1 && lastPointerCount == 1 &&
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003763 current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003764 // Only one pointer and no change in count so it must have the same id as before.
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003765 uint32_t id = last.rawPointerData.pointers[0].id;
3766 current.rawPointerData.pointers[0].id = id;
3767 current.rawPointerData.idToIndex[id] = 0;
3768 current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003769 return;
3770 }
3771
3772 // General case.
3773 // We build a heap of squared euclidean distances between current and last pointers
3774 // associated with the current and last pointer indices. Then, we find the best
3775 // match (by distance) for each current pointer.
3776 // The pointers must have the same tool type but it is possible for them to
3777 // transition from hovering to touching or vice-versa while retaining the same id.
3778 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
3779
3780 uint32_t heapSize = 0;
3781 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
3782 currentPointerIndex++) {
3783 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
3784 lastPointerIndex++) {
3785 const RawPointerData::Pointer& currentPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003786 current.rawPointerData.pointers[currentPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003787 const RawPointerData::Pointer& lastPointer =
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003788 last.rawPointerData.pointers[lastPointerIndex];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003789 if (currentPointer.toolType == lastPointer.toolType) {
3790 int64_t deltaX = currentPointer.x - lastPointer.x;
3791 int64_t deltaY = currentPointer.y - lastPointer.y;
3792
3793 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3794
3795 // Insert new element into the heap (sift up).
3796 heap[heapSize].currentPointerIndex = currentPointerIndex;
3797 heap[heapSize].lastPointerIndex = lastPointerIndex;
3798 heap[heapSize].distance = distance;
3799 heapSize += 1;
3800 }
3801 }
3802 }
3803
3804 // Heapify
3805 for (uint32_t startIndex = heapSize / 2; startIndex != 0;) {
3806 startIndex -= 1;
3807 for (uint32_t parentIndex = startIndex;;) {
3808 uint32_t childIndex = parentIndex * 2 + 1;
3809 if (childIndex >= heapSize) {
3810 break;
3811 }
3812
3813 if (childIndex + 1 < heapSize &&
3814 heap[childIndex + 1].distance < heap[childIndex].distance) {
3815 childIndex += 1;
3816 }
3817
3818 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3819 break;
3820 }
3821
3822 swap(heap[parentIndex], heap[childIndex]);
3823 parentIndex = childIndex;
3824 }
3825 }
3826
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003827 if (DEBUG_POINTER_ASSIGNMENT) {
3828 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
3829 for (size_t i = 0; i < heapSize; i++) {
3830 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i,
3831 heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance);
3832 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003833 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003834
3835 // Pull matches out by increasing order of distance.
3836 // To avoid reassigning pointers that have already been matched, the loop keeps track
3837 // of which last and current pointers have been matched using the matchedXXXBits variables.
3838 // It also tracks the used pointer id bits.
3839 BitSet32 matchedLastBits(0);
3840 BitSet32 matchedCurrentBits(0);
3841 BitSet32 usedIdBits(0);
3842 bool first = true;
3843 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
3844 while (heapSize > 0) {
3845 if (first) {
3846 // The first time through the loop, we just consume the root element of
3847 // the heap (the one with smallest distance).
3848 first = false;
3849 } else {
3850 // Previous iterations consumed the root element of the heap.
3851 // Pop root element off of the heap (sift down).
3852 heap[0] = heap[heapSize];
3853 for (uint32_t parentIndex = 0;;) {
3854 uint32_t childIndex = parentIndex * 2 + 1;
3855 if (childIndex >= heapSize) {
3856 break;
3857 }
3858
3859 if (childIndex + 1 < heapSize &&
3860 heap[childIndex + 1].distance < heap[childIndex].distance) {
3861 childIndex += 1;
3862 }
3863
3864 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3865 break;
3866 }
3867
3868 swap(heap[parentIndex], heap[childIndex]);
3869 parentIndex = childIndex;
3870 }
3871
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -08003872 if (DEBUG_POINTER_ASSIGNMENT) {
3873 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
3874 for (size_t j = 0; j < heapSize; j++) {
3875 ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64,
3876 j, heap[j].currentPointerIndex, heap[j].lastPointerIndex,
3877 heap[j].distance);
3878 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003879 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003880 }
3881
3882 heapSize -= 1;
3883
3884 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3885 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3886
3887 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3888 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3889
3890 matchedCurrentBits.markBit(currentPointerIndex);
3891 matchedLastBits.markBit(lastPointerIndex);
3892
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003893 uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id;
3894 current.rawPointerData.pointers[currentPointerIndex].id = id;
3895 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3896 current.rawPointerData.markIdBit(id,
3897 current.rawPointerData.isHovering(
3898 currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003899 usedIdBits.markBit(id);
3900
Harry Cutts45483602022-08-24 14:36:48 +00003901 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
3902 "assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32
3903 ", distance=%" PRIu64,
3904 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003905 break;
3906 }
3907 }
3908
3909 // Assign fresh ids to pointers that were not matched in the process.
3910 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
3911 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
3912 uint32_t id = usedIdBits.markFirstUnmarkedBit();
3913
Siarhei Vishniakou57479982021-03-03 01:32:21 +00003914 current.rawPointerData.pointers[currentPointerIndex].id = id;
3915 current.rawPointerData.idToIndex[id] = currentPointerIndex;
3916 current.rawPointerData.markIdBit(id,
3917 current.rawPointerData.isHovering(currentPointerIndex));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003918
Harry Cutts45483602022-08-24 14:36:48 +00003919 ALOGD_IF(DEBUG_POINTER_ASSIGNMENT,
3920 "assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex,
3921 id);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003922 }
3923}
3924
3925int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
3926 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
3927 return AKEY_STATE_VIRTUAL;
3928 }
3929
3930 for (const VirtualKey& virtualKey : mVirtualKeys) {
3931 if (virtualKey.keyCode == keyCode) {
3932 return AKEY_STATE_UP;
3933 }
3934 }
3935
3936 return AKEY_STATE_UNKNOWN;
3937}
3938
3939int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
3940 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
3941 return AKEY_STATE_VIRTUAL;
3942 }
3943
3944 for (const VirtualKey& virtualKey : mVirtualKeys) {
3945 if (virtualKey.scanCode == scanCode) {
3946 return AKEY_STATE_UP;
3947 }
3948 }
3949
3950 return AKEY_STATE_UNKNOWN;
3951}
3952
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003953bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
3954 const std::vector<int32_t>& keyCodes,
3955 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003956 for (const VirtualKey& virtualKey : mVirtualKeys) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003957 for (size_t i = 0; i < keyCodes.size(); i++) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003958 if (virtualKey.keyCode == keyCodes[i]) {
3959 outFlags[i] = 1;
3960 }
3961 }
3962 }
3963
3964 return true;
3965}
3966
3967std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() {
3968 if (mParameters.hasAssociatedDisplay) {
Michael Wright227c5542020-07-02 18:30:52 +01003969 if (mDeviceMode == DeviceMode::POINTER) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003970 return std::make_optional(mPointerController->getDisplayId());
3971 } else {
3972 return std::make_optional(mViewport.displayId);
3973 }
3974 }
3975 return std::nullopt;
3976}
3977
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07003978} // namespace android